]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
PowerPC64 synthetic 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
3db64b00 28#include "sysdep.h"
183e98be 29#include <stdarg.h>
5bd4f169 30#include "bfd.h"
5bd4f169
AM
31#include "bfdlink.h"
32#include "libbfd.h"
33#include "elf-bfd.h"
04c9666a 34#include "elf/ppc64.h"
5d1634d7 35#include "elf64-ppc.h"
58d180e8 36#include "dwarf2.h"
5bd4f169 37
bb294208
AM
38/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
39#define OCTETS_PER_BYTE(ABFD, SEC) 1
40
805fc799 41static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
43static bfd_reloc_status_type ppc64_elf_branch_reloc
44 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 45static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 46 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 47static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 48 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 49static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 50 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 51static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 52 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 53static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 54 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 55static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 56 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
5663e321
AM
57static bfd_reloc_status_type ppc64_elf_prefix_reloc
58 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 59static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 60 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 61static bfd_vma opd_entry_value
aef36ac1 62 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
5bd4f169 63
6d00b590 64#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
ad8e1ba5 65#define TARGET_LITTLE_NAME "elf64-powerpcle"
6d00b590 66#define TARGET_BIG_SYM powerpc_elf64_vec
ad8e1ba5
AM
67#define TARGET_BIG_NAME "elf64-powerpc"
68#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 69#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
70#define ELF_MACHINE_CODE EM_PPC64
71#define ELF_MAXPAGESIZE 0x10000
702d1671
AM
72#define ELF_COMMONPAGESIZE 0x1000
73#define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
ad8e1ba5
AM
74#define elf_info_to_howto ppc64_elf_info_to_howto
75
76#define elf_backend_want_got_sym 0
77#define elf_backend_want_plt_sym 0
78#define elf_backend_plt_alignment 3
79#define elf_backend_plt_not_loaded 1
ad8e1ba5 80#define elf_backend_got_header_size 8
5474d94f 81#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
82#define elf_backend_can_gc_sections 1
83#define elf_backend_can_refcount 1
84#define elf_backend_rela_normal 1
64f52338 85#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 86#define elf_backend_default_execstack 0
ad8e1ba5 87
e717da7e 88#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 89#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 90#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
91#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
92#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
93#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
94#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 95#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 96#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 97#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
98
99#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
100#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
101#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 102#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 103#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 104#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 105#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 106#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 107#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 108#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 109#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 110#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 111#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 112#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 113#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
114#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
115#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 116#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
c9405344 117#define elf_backend_always_size_sections ppc64_elf_edit
ad8e1ba5 118#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 119#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 120#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 121#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
122#define elf_backend_relocate_section ppc64_elf_relocate_section
123#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
124#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
125#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 126#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 127#define elf_backend_special_sections ppc64_elf_special_sections
bf577467 128#define elf_backend_section_flags ppc64_elf_section_flags
6911b7dc 129#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 130#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 131#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 132
5bd4f169
AM
133/* The name of the dynamic interpreter. This is put in the .interp
134 section. */
135#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
136
137/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 138#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
2d7ad24e 139#define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
5bd4f169
AM
140
141/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 142#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 143
a078d95a
AM
144/* Offsets to some stack save slots. */
145#define STK_LR 16
146#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 147/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
148 CR save slot. Used only by optimised __tls_get_addr call stub,
149 relying on __tls_get_addr_opt not saving CR.. */
150#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
151
5bd4f169 152/* TOC base pointers offset from start of TOC. */
411e1bfb 153#define TOC_BASE_OFF 0x8000
a27e685f
AM
154/* TOC base alignment. */
155#define TOC_BASE_ALIGN 256
411e1bfb
AM
156
157/* Offset of tp and dtp pointers from start of TLS block. */
158#define TP_OFFSET 0x7000
159#define DTP_OFFSET 0x8000
5bd4f169 160
ad8e1ba5
AM
161/* .plt call stub instructions. The normal stub is like this, but
162 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 163 insert an addi to adjust r11. */
a078d95a 164#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
165#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
166#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
167#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
168#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
169#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
170#define BCTR 0x4e800420 /* bctr */
171
07d6d2b8 172#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
05d0e962
AM
173#define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
174#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
07d6d2b8
AM
175#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
176#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
177
178#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
179#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
180#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
181#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
182#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
183#define BNECTR 0x4ca20420 /* bnectr+ */
184#define BNECTR_P4 0x4ce20420 /* bnectr+ */
794e51c0 185
71a39c98 186#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 187#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 188#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 189
07d6d2b8
AM
190#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
191#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
192#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 193
04bdff6a 194#define LI_R11_0 0x39600000 /* li %r11,0 */
07d6d2b8 195#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
04bdff6a 196#define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
05d0e962 197#define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
006589cf
AM
198#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
199#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
05d0e962 200#define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
a345bc8d 201#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
05d0e962 202#define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
04bdff6a 203#define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
05d0e962 204#define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
07d6d2b8 205#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
04bdff6a 206#define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
05d0e962
AM
207#define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
208#define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
209#define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
04bdff6a
AM
210#define PADDI_R12_PC 0x0610000039800000ULL
211#define PLD_R12_PC 0x04100000e5800000ULL
5663e321 212#define PNOP 0x0700000000000000ULL
a345bc8d 213
3cd7c7d7
AM
214/* __glink_PLTresolve stub instructions. We enter with the index in
215 R0 for ELFv1, and the address of a glink branch in R12 for ELFv2. */
9e390558 216#define GLINK_PLTRESOLVE_SIZE(htab) \
3cd7c7d7 217 (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
ee4bf8d2
AM
218 /* 0: */
219 /* .quad plt0-1f */
220 /* __glink: */
221#define MFLR_R12 0x7d8802a6 /* mflr %12 */
222#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
223 /* 1: */
224#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 225 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 226#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
227#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
228 /* ld %12,0(%11) */
229 /* ld %2,8(%11) */
230 /* mtctr %12 */
231 /* ld %11,16(%11) */
ee4bf8d2 232 /* bctr */
3cd7c7d7
AM
233
234#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
235#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
236#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
237#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
238#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
239#define LD_R0_0R11 0xe80b0000 /* ld %r0,0(%r11) */
240#define ADD_R11_R0_R11 0x7d605a14 /* add %r11,%r0,%r11 */
5d1634d7
AM
241
242/* Pad with this. */
243#define NOP 0x60000000
244
721956f4
AM
245/* Some other nops. */
246#define CROR_151515 0x4def7b82
247#define CROR_313131 0x4ffffb82
248
cedb70c5 249/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
250#define LI_R0_0 0x38000000 /* li %r0,0 */
251#define B_DOT 0x48000000 /* b . */
252
253/* After that, we need two instructions to load the index, followed by
254 a branch. */
255#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 256#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 257
deb0e272
AM
258/* Instructions used by the save and restore reg functions. */
259#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
260#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
261#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
262#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
263#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
264#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
265#define LI_R12_0 0x39800000 /* li %r12,0 */
266#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
267#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
268#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
269#define BLR 0x4e800020 /* blr */
270
41bd81ab
AM
271/* Since .opd is an array of descriptors and each entry will end up
272 with identical R_PPC64_RELATIVE relocs, there is really no need to
273 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 274 relocate .opd without reloc entries. */
41bd81ab
AM
275#ifndef NO_OPD_RELOCS
276#define NO_OPD_RELOCS 0
277#endif
810d4e75 278
a4b6fadd
AM
279#ifndef ARRAY_SIZE
280#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
281#endif
282
810d4e75
AM
283static inline int
284abiversion (bfd *abfd)
285{
286 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
287}
288
289static inline void
290set_abiversion (bfd *abfd, int ver)
291{
292 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
293 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
294}
5bd4f169
AM
295\f
296/* Relocation HOWTO's. */
46807bf4
AM
297/* Like other ELF RELA targets that don't apply multiple
298 field-altering relocations to the same localation, src_mask is
299 always zero and pcrel_offset is the same as pc_relative.
300 PowerPC can always use a zero bitpos, even when the field is not at
301 the LSB. For example, a REL24 could use rightshift=2, bisize=24
302 and bitpos=2 which matches the ABI description, or as we do here,
303 rightshift=0, bitsize=26 and bitpos=0. */
304#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
305 complain, special_func) \
306 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
307 complain_overflow_ ## complain, special_func, \
308 #type, FALSE, 0, mask, pc_relative)
309
04c9666a 310static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 311
f3185997
NC
312static reloc_howto_type ppc64_elf_howto_raw[] =
313{
5bd4f169 314 /* This reloc does nothing. */
46807bf4
AM
315 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
316 bfd_elf_generic_reloc),
5bd4f169
AM
317
318 /* A standard 32 bit relocation. */
46807bf4
AM
319 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
320 bfd_elf_generic_reloc),
5bd4f169
AM
321
322 /* An absolute 26 bit branch; the lower two bits must be zero.
323 FIXME: we don't check that, we just clear them. */
46807bf4
AM
324 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
325 bfd_elf_generic_reloc),
5bd4f169
AM
326
327 /* A standard 16 bit relocation. */
46807bf4
AM
328 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
329 bfd_elf_generic_reloc),
5bd4f169
AM
330
331 /* A 16 bit relocation without overflow. */
46807bf4
AM
332 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
333 bfd_elf_generic_reloc),
5bd4f169
AM
334
335 /* Bits 16-31 of an address. */
46807bf4
AM
336 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
337 bfd_elf_generic_reloc),
5bd4f169
AM
338
339 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
340 bits, treated as a signed number, is negative. */
46807bf4
AM
341 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
342 ppc64_elf_ha_reloc),
5bd4f169
AM
343
344 /* An absolute 16 bit branch; the lower two bits must be zero.
345 FIXME: we don't check that, we just clear them. */
46807bf4
AM
346 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
347 ppc64_elf_branch_reloc),
5bd4f169
AM
348
349 /* An absolute 16 bit branch, for which bit 10 should be set to
350 indicate that the branch is expected to be taken. The lower two
351 bits must be zero. */
46807bf4
AM
352 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
353 ppc64_elf_brtaken_reloc),
5bd4f169
AM
354
355 /* An absolute 16 bit branch, for which bit 10 should be set to
356 indicate that the branch is not expected to be taken. The lower
357 two bits must be zero. */
46807bf4
AM
358 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
359 ppc64_elf_brtaken_reloc),
5bd4f169
AM
360
361 /* A relative 26 bit branch; the lower two bits must be zero. */
46807bf4
AM
362 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
363 ppc64_elf_branch_reloc),
5bd4f169 364
05d0e962 365 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
46807bf4
AM
366 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
367 ppc64_elf_branch_reloc),
05d0e962 368
5bd4f169 369 /* A relative 16 bit branch; the lower two bits must be zero. */
46807bf4
AM
370 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
371 ppc64_elf_branch_reloc),
5bd4f169
AM
372
373 /* A relative 16 bit branch. Bit 10 should be set to indicate that
374 the branch is expected to be taken. The lower two bits must be
375 zero. */
46807bf4
AM
376 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
377 ppc64_elf_brtaken_reloc),
5bd4f169
AM
378
379 /* A relative 16 bit branch. Bit 10 should be set to indicate that
380 the branch is not expected to be taken. The lower two bits must
381 be zero. */
46807bf4
AM
382 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
383 ppc64_elf_brtaken_reloc),
5bd4f169
AM
384
385 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
386 symbol. */
46807bf4
AM
387 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
388 ppc64_elf_unhandled_reloc),
5bd4f169
AM
389
390 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
391 the symbol. */
46807bf4
AM
392 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
393 ppc64_elf_unhandled_reloc),
5bd4f169
AM
394
395 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
396 the symbol. */
46807bf4
AM
397 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
398 ppc64_elf_unhandled_reloc),
5bd4f169
AM
399
400 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
401 the symbol. */
46807bf4
AM
402 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
403 ppc64_elf_unhandled_reloc),
5bd4f169
AM
404
405 /* This is used only by the dynamic linker. The symbol should exist
406 both in the object being run and in some shared library. The
407 dynamic linker copies the data addressed by the symbol from the
408 shared library into the object, because the object being
409 run has to have the data at some particular address. */
46807bf4
AM
410 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
411 ppc64_elf_unhandled_reloc),
5bd4f169
AM
412
413 /* Like R_PPC64_ADDR64, but used when setting global offset table
414 entries. */
46807bf4
AM
415 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
416 ppc64_elf_unhandled_reloc),
5bd4f169
AM
417
418 /* Created by the link editor. Marks a procedure linkage table
419 entry for a symbol. */
46807bf4
AM
420 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
421 ppc64_elf_unhandled_reloc),
5bd4f169
AM
422
423 /* Used only by the dynamic linker. When the object is run, this
424 doubleword64 is set to the load address of the object, plus the
425 addend. */
46807bf4
AM
426 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
427 bfd_elf_generic_reloc),
5bd4f169
AM
428
429 /* Like R_PPC64_ADDR32, but may be unaligned. */
46807bf4
AM
430 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
431 bfd_elf_generic_reloc),
5bd4f169
AM
432
433 /* Like R_PPC64_ADDR16, but may be unaligned. */
46807bf4
AM
434 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
435 bfd_elf_generic_reloc),
5bd4f169
AM
436
437 /* 32-bit PC relative. */
46807bf4
AM
438 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
439 bfd_elf_generic_reloc),
5bd4f169 440
10ed1bba 441 /* 32-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
442 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
443 ppc64_elf_unhandled_reloc),
5bd4f169
AM
444
445 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
446 FIXME: R_PPC64_PLTREL32 not supported. */
46807bf4
AM
447 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
448 ppc64_elf_unhandled_reloc),
5bd4f169
AM
449
450 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
451 the symbol. */
46807bf4
AM
452 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
453 ppc64_elf_unhandled_reloc),
5bd4f169
AM
454
455 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
456 the symbol. */
46807bf4
AM
457 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
458 ppc64_elf_unhandled_reloc),
5bd4f169
AM
459
460 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
461 the symbol. */
46807bf4
AM
462 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
463 ppc64_elf_unhandled_reloc),
5bd4f169 464
c061c2d8 465 /* 16-bit section relative relocation. */
46807bf4
AM
466 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
467 ppc64_elf_sectoff_reloc),
5bd4f169 468
c061c2d8 469 /* Like R_PPC64_SECTOFF, but no overflow warning. */
46807bf4
AM
470 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
471 ppc64_elf_sectoff_reloc),
5bd4f169
AM
472
473 /* 16-bit upper half section relative relocation. */
46807bf4
AM
474 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
475 ppc64_elf_sectoff_reloc),
5bd4f169
AM
476
477 /* 16-bit upper half adjusted section relative relocation. */
46807bf4
AM
478 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
479 ppc64_elf_sectoff_ha_reloc),
5bd4f169 480
04c9666a 481 /* Like R_PPC64_REL24 without touching the two least significant bits. */
46807bf4
AM
482 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
483 bfd_elf_generic_reloc),
5bd4f169
AM
484
485 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
486
487 /* A standard 64-bit relocation. */
46807bf4
AM
488 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
489 bfd_elf_generic_reloc),
5bd4f169
AM
490
491 /* The bits 32-47 of an address. */
46807bf4
AM
492 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
493 bfd_elf_generic_reloc),
5bd4f169
AM
494
495 /* The bits 32-47 of an address, plus 1 if the contents of the low
496 16 bits, treated as a signed number, is negative. */
46807bf4
AM
497 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
498 ppc64_elf_ha_reloc),
5bd4f169
AM
499
500 /* The bits 48-63 of an address. */
46807bf4
AM
501 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
502 bfd_elf_generic_reloc),
5bd4f169
AM
503
504 /* The bits 48-63 of an address, plus 1 if the contents of the low
505 16 bits, treated as a signed number, is negative. */
46807bf4
AM
506 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
507 ppc64_elf_ha_reloc),
5bd4f169
AM
508
509 /* Like ADDR64, but may be unaligned. */
46807bf4
AM
510 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
511 bfd_elf_generic_reloc),
5bd4f169
AM
512
513 /* 64-bit relative relocation. */
46807bf4
AM
514 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
515 bfd_elf_generic_reloc),
5bd4f169 516
cedb70c5 517 /* 64-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
518 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
519 ppc64_elf_unhandled_reloc),
5bd4f169
AM
520
521 /* 64-bit PC relative relocation to the symbol's procedure linkage
522 table. */
523 /* FIXME: R_PPC64_PLTREL64 not supported. */
46807bf4
AM
524 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
525 ppc64_elf_unhandled_reloc),
5bd4f169
AM
526
527 /* 16 bit TOC-relative relocation. */
5bd4f169 528 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
46807bf4
AM
529 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
530 ppc64_elf_toc_reloc),
5bd4f169
AM
531
532 /* 16 bit TOC-relative relocation without overflow. */
5bd4f169 533 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
46807bf4
AM
534 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
535 ppc64_elf_toc_reloc),
5bd4f169
AM
536
537 /* 16 bit TOC-relative relocation, high 16 bits. */
5bd4f169 538 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
46807bf4
AM
539 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
540 ppc64_elf_toc_reloc),
5bd4f169
AM
541
542 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
543 contents of the low 16 bits, treated as a signed number, is
544 negative. */
5bd4f169 545 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
46807bf4
AM
546 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
547 ppc64_elf_toc_ha_reloc),
5bd4f169
AM
548
549 /* 64-bit relocation; insert value of TOC base (.TOC.). */
5bd4f169 550 /* R_PPC64_TOC 51 doubleword64 .TOC. */
46807bf4
AM
551 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
552 ppc64_elf_toc64_reloc),
5bd4f169
AM
553
554 /* Like R_PPC64_GOT16, but also informs the link editor that the
555 value to relocate may (!) refer to a PLT entry which the link
556 editor (a) may replace with the symbol value. If the link editor
557 is unable to fully resolve the symbol, it may (b) create a PLT
558 entry and store the address to the new PLT entry in the GOT.
559 This permits lazy resolution of function symbols at run time.
560 The link editor may also skip all of this and just (c) emit a
561 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
562 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
46807bf4
AM
563 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
564 ppc64_elf_unhandled_reloc),
411e1bfb
AM
565
566 /* Like R_PPC64_PLTGOT16, but without overflow. */
567 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
568 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
569 ppc64_elf_unhandled_reloc),
411e1bfb
AM
570
571 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
572 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
46807bf4
AM
573 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
574 ppc64_elf_unhandled_reloc),
411e1bfb
AM
575
576 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
577 1 if the contents of the low 16 bits, treated as a signed number,
578 is negative. */
579 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
46807bf4
AM
580 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
581 ppc64_elf_unhandled_reloc),
411e1bfb
AM
582
583 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
46807bf4
AM
584 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
585 bfd_elf_generic_reloc),
411e1bfb
AM
586
587 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
46807bf4
AM
588 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
589 bfd_elf_generic_reloc),
411e1bfb
AM
590
591 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
46807bf4
AM
592 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
593 ppc64_elf_unhandled_reloc),
411e1bfb
AM
594
595 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
46807bf4
AM
596 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
597 ppc64_elf_unhandled_reloc),
411e1bfb
AM
598
599 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
46807bf4
AM
600 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
601 ppc64_elf_unhandled_reloc),
411e1bfb
AM
602
603 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
46807bf4
AM
604 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
605 ppc64_elf_sectoff_reloc),
411e1bfb
AM
606
607 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
46807bf4
AM
608 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
609 ppc64_elf_sectoff_reloc),
411e1bfb
AM
610
611 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
46807bf4
AM
612 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
613 ppc64_elf_toc_reloc),
411e1bfb
AM
614
615 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
46807bf4
AM
616 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
617 ppc64_elf_toc_reloc),
411e1bfb
AM
618
619 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
620 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
46807bf4
AM
621 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
622 ppc64_elf_unhandled_reloc),
411e1bfb
AM
623
624 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
625 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
626 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
627 ppc64_elf_unhandled_reloc),
411e1bfb 628
727fc41e 629 /* Marker relocs for TLS. */
46807bf4
AM
630 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
631 bfd_elf_generic_reloc),
632
633 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
634 bfd_elf_generic_reloc),
635
636 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
637 bfd_elf_generic_reloc),
727fc41e 638
23cedd1d
AM
639 /* Marker reloc for optimizing r2 save in prologue rather than on
640 each plt call stub. */
46807bf4
AM
641 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
642 bfd_elf_generic_reloc),
3b421ab3 643
23cedd1d 644 /* Marker relocs on inline plt call instructions. */
46807bf4
AM
645 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
646 bfd_elf_generic_reloc),
647
648 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
649 bfd_elf_generic_reloc),
23cedd1d 650
411e1bfb
AM
651 /* Computes the load module index of the load module that contains the
652 definition of its TLS sym. */
46807bf4
AM
653 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
654 ppc64_elf_unhandled_reloc),
411e1bfb
AM
655
656 /* Computes a dtv-relative displacement, the difference between the value
657 of sym+add and the base address of the thread-local storage block that
658 contains the definition of sym, minus 0x8000. */
46807bf4
AM
659 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
660 ppc64_elf_unhandled_reloc),
411e1bfb
AM
661
662 /* A 16 bit dtprel reloc. */
46807bf4
AM
663 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
664 ppc64_elf_unhandled_reloc),
411e1bfb
AM
665
666 /* Like DTPREL16, but no overflow. */
46807bf4
AM
667 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
668 ppc64_elf_unhandled_reloc),
411e1bfb
AM
669
670 /* Like DTPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
671 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
672 ppc64_elf_unhandled_reloc),
411e1bfb
AM
673
674 /* Like DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
675 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
676 ppc64_elf_unhandled_reloc),
411e1bfb
AM
677
678 /* Like DTPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
679 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
680 ppc64_elf_unhandled_reloc),
411e1bfb
AM
681
682 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
683 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
684 ppc64_elf_unhandled_reloc),
411e1bfb
AM
685
686 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
687 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
688 ppc64_elf_unhandled_reloc),
411e1bfb
AM
689
690 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
691 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
692 ppc64_elf_unhandled_reloc),
411e1bfb
AM
693
694 /* Like DTPREL16, but for insns with a DS field. */
46807bf4
AM
695 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
696 ppc64_elf_unhandled_reloc),
411e1bfb
AM
697
698 /* Like DTPREL16_DS, but no overflow. */
46807bf4
AM
699 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
700 ppc64_elf_unhandled_reloc),
411e1bfb
AM
701
702 /* Computes a tp-relative displacement, the difference between the value of
703 sym+add and the value of the thread pointer (r13). */
46807bf4
AM
704 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
705 ppc64_elf_unhandled_reloc),
411e1bfb
AM
706
707 /* A 16 bit tprel reloc. */
46807bf4
AM
708 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
709 ppc64_elf_unhandled_reloc),
411e1bfb
AM
710
711 /* Like TPREL16, but no overflow. */
46807bf4
AM
712 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
713 ppc64_elf_unhandled_reloc),
411e1bfb
AM
714
715 /* Like TPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
716 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
717 ppc64_elf_unhandled_reloc),
411e1bfb
AM
718
719 /* Like TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
720 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
721 ppc64_elf_unhandled_reloc),
411e1bfb
AM
722
723 /* Like TPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
724 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
725 ppc64_elf_unhandled_reloc),
411e1bfb
AM
726
727 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
728 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
729 ppc64_elf_unhandled_reloc),
411e1bfb
AM
730
731 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
732 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
733 ppc64_elf_unhandled_reloc),
411e1bfb
AM
734
735 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
736 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
737 ppc64_elf_unhandled_reloc),
411e1bfb
AM
738
739 /* Like TPREL16, but for insns with a DS field. */
46807bf4
AM
740 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
741 ppc64_elf_unhandled_reloc),
411e1bfb
AM
742
743 /* Like TPREL16_DS, but no overflow. */
46807bf4
AM
744 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
745 ppc64_elf_unhandled_reloc),
411e1bfb
AM
746
747 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
748 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
749 to the first entry relative to the TOC base (r2). */
46807bf4
AM
750 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
751 ppc64_elf_unhandled_reloc),
5bd4f169 752
411e1bfb 753 /* Like GOT_TLSGD16, but no overflow. */
46807bf4
AM
754 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
755 ppc64_elf_unhandled_reloc),
5bd4f169 756
411e1bfb 757 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
46807bf4
AM
758 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
759 ppc64_elf_unhandled_reloc),
5bd4f169 760
411e1bfb 761 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
46807bf4
AM
762 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
763 ppc64_elf_unhandled_reloc),
5bd4f169 764
411e1bfb
AM
765 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
766 with values (sym+add)@dtpmod and zero, and computes the offset to the
767 first entry relative to the TOC base (r2). */
46807bf4
AM
768 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
769 ppc64_elf_unhandled_reloc),
5bd4f169 770
411e1bfb 771 /* Like GOT_TLSLD16, but no overflow. */
46807bf4
AM
772 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
773 ppc64_elf_unhandled_reloc),
5bd4f169 774
411e1bfb 775 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
46807bf4
AM
776 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
777 ppc64_elf_unhandled_reloc),
5bd4f169 778
411e1bfb 779 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
46807bf4
AM
780 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
781 ppc64_elf_unhandled_reloc),
5bd4f169 782
411e1bfb
AM
783 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
784 the offset to the entry relative to the TOC base (r2). */
46807bf4
AM
785 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
786 ppc64_elf_unhandled_reloc),
5bd4f169 787
411e1bfb 788 /* Like GOT_DTPREL16_DS, but no overflow. */
46807bf4
AM
789 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
790 ppc64_elf_unhandled_reloc),
5bd4f169 791
411e1bfb 792 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
793 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
794 ppc64_elf_unhandled_reloc),
5bd4f169 795
411e1bfb 796 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
797 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
798 ppc64_elf_unhandled_reloc),
411e1bfb
AM
799
800 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
801 offset to the entry relative to the TOC base (r2). */
46807bf4
AM
802 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
803 ppc64_elf_unhandled_reloc),
5bd4f169 804
411e1bfb 805 /* Like GOT_TPREL16_DS, but no overflow. */
46807bf4
AM
806 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
807 ppc64_elf_unhandled_reloc),
5bd4f169 808
411e1bfb 809 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
810 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
811 ppc64_elf_unhandled_reloc),
5bd4f169 812
411e1bfb 813 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
814 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
815 ppc64_elf_unhandled_reloc),
816
817 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
818 ppc64_elf_unhandled_reloc),
819
820 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
821 bfd_elf_generic_reloc),
e054468f 822
25f23106 823 /* A 16 bit relative relocation. */
46807bf4
AM
824 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
825 bfd_elf_generic_reloc),
25f23106
AM
826
827 /* A 16 bit relative relocation without overflow. */
46807bf4
AM
828 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
829 bfd_elf_generic_reloc),
25f23106
AM
830
831 /* The high order 16 bits of a relative address. */
46807bf4
AM
832 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
833 bfd_elf_generic_reloc),
25f23106
AM
834
835 /* The high order 16 bits of a relative address, plus 1 if the contents of
836 the low 16 bits, treated as a signed number, is negative. */
46807bf4
AM
837 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
838 ppc64_elf_ha_reloc),
25f23106 839
4a969973
AM
840 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
841 bfd_elf_generic_reloc),
842
843 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
844 ppc64_elf_ha_reloc),
845
846 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
847 bfd_elf_generic_reloc),
848
849 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
850 ppc64_elf_ha_reloc),
851
852 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
853 bfd_elf_generic_reloc),
854
855 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
856 ppc64_elf_ha_reloc),
857
a680de9a 858 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
46807bf4
AM
859 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
860 ppc64_elf_ha_reloc),
a680de9a 861
7ba71655 862 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
46807bf4
AM
863 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
864 ppc64_elf_ha_reloc),
7ba71655 865
f9c6b907 866 /* Like R_PPC64_ADDR16_HI, but no overflow. */
46807bf4
AM
867 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
868 bfd_elf_generic_reloc),
f9c6b907
AM
869
870 /* Like R_PPC64_ADDR16_HA, but no overflow. */
46807bf4
AM
871 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
872 ppc64_elf_ha_reloc),
f9c6b907
AM
873
874 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
46807bf4
AM
875 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
876 ppc64_elf_unhandled_reloc),
f9c6b907
AM
877
878 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
46807bf4
AM
879 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
880 ppc64_elf_unhandled_reloc),
f9c6b907
AM
881
882 /* Like R_PPC64_TPREL16_HI, but no overflow. */
46807bf4
AM
883 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
884 ppc64_elf_unhandled_reloc),
f9c6b907
AM
885
886 /* Like R_PPC64_TPREL16_HA, but no overflow. */
46807bf4
AM
887 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
888 ppc64_elf_unhandled_reloc),
f9c6b907 889
006589cf 890 /* Marker reloc on ELFv2 large-model function entry. */
46807bf4
AM
891 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
892 bfd_elf_generic_reloc),
006589cf 893
45965137 894 /* Like ADDR64, but use local entry point of function. */
46807bf4
AM
895 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
896 bfd_elf_generic_reloc),
45965137 897
5663e321
AM
898 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, FALSE, dont,
899 bfd_elf_generic_reloc),
900
901 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, FALSE, dont,
902 bfd_elf_generic_reloc),
903
904 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, FALSE, dont,
905 bfd_elf_generic_reloc),
906
907 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
908 ppc64_elf_prefix_reloc),
909
910 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, dont,
911 ppc64_elf_prefix_reloc),
912
913 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
914 ppc64_elf_prefix_reloc),
915
916 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
917 ppc64_elf_prefix_reloc),
918
919 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
920 ppc64_elf_prefix_reloc),
921
922 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
923 ppc64_elf_unhandled_reloc),
924
925 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
926 ppc64_elf_unhandled_reloc),
927
928 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
929 ppc64_elf_unhandled_reloc),
930
c213164a
AM
931 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
932 ppc64_elf_unhandled_reloc),
933
934 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
935 ppc64_elf_unhandled_reloc),
936
87c69f97 937 HOW (R_PPC64_GOT_TLSGD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
c213164a
AM
938 ppc64_elf_unhandled_reloc),
939
87c69f97 940 HOW (R_PPC64_GOT_TLSLD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
c213164a
AM
941 ppc64_elf_unhandled_reloc),
942
87c69f97 943 HOW (R_PPC64_GOT_TPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
c213164a
AM
944 ppc64_elf_unhandled_reloc),
945
87c69f97 946 HOW (R_PPC64_GOT_DTPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
c213164a
AM
947 ppc64_elf_unhandled_reloc),
948
5663e321
AM
949 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
950 bfd_elf_generic_reloc),
951
952 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
953 ppc64_elf_ha_reloc),
954
955 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
956 bfd_elf_generic_reloc),
957
958 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
959 ppc64_elf_ha_reloc),
960
961 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
962 bfd_elf_generic_reloc),
963
964 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
965 ppc64_elf_ha_reloc),
966
967 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
968 bfd_elf_generic_reloc),
969
970 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
971 ppc64_elf_ha_reloc),
972
973 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
974 ppc64_elf_prefix_reloc),
975
976 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
977 ppc64_elf_prefix_reloc),
978
5bd4f169 979 /* GNU extension to record C++ vtable hierarchy. */
46807bf4
AM
980 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
981 NULL),
5bd4f169
AM
982
983 /* GNU extension to record C++ vtable member usage. */
46807bf4
AM
984 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
985 NULL),
5bd4f169
AM
986};
987
988\f
989/* Initialize the ppc64_elf_howto_table, so that linear accesses can
990 be done. */
991
992static void
4ce794b7 993ppc_howto_init (void)
5bd4f169
AM
994{
995 unsigned int i, type;
996
a4b6fadd 997 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
998 {
999 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 1000 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
1001 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1002 }
1003}
1004
1005static reloc_howto_type *
87c69f97 1006ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
5bd4f169 1007{
411e1bfb 1008 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
1009
1010 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1011 /* Initialize howto table if needed. */
1012 ppc_howto_init ();
1013
4ce794b7 1014 switch (code)
5bd4f169
AM
1015 {
1016 default:
f3185997 1017 /* xgettext:c-format */
2cdcc330
AM
1018 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1019 (int) code);
f3185997 1020 bfd_set_error (bfd_error_bad_value);
4ce794b7 1021 return NULL;
5bd4f169 1022
411e1bfb
AM
1023 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1024 break;
1025 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1026 break;
1027 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1028 break;
1029 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1030 break;
1031 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1032 break;
1033 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1034 break;
f9c6b907
AM
1035 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1036 break;
411e1bfb 1037 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 1038 break;
f9c6b907
AM
1039 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1040 break;
411e1bfb 1041 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 1042 break;
411e1bfb 1043 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 1044 break;
411e1bfb 1045 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 1046 break;
411e1bfb 1047 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 1048 break;
05d0e962
AM
1049 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1050 break;
411e1bfb 1051 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 1052 break;
411e1bfb 1053 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 1054 break;
411e1bfb 1055 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 1056 break;
411e1bfb 1057 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 1058 break;
411e1bfb 1059 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 1060 break;
411e1bfb 1061 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 1062 break;
411e1bfb 1063 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 1064 break;
411e1bfb 1065 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 1066 break;
411e1bfb 1067 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 1068 break;
411e1bfb 1069 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 1070 break;
411e1bfb 1071 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 1072 break;
411e1bfb 1073 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 1074 break;
411e1bfb 1075 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 1076 break;
411e1bfb 1077 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 1078 break;
411e1bfb 1079 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 1080 break;
411e1bfb 1081 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 1082 break;
411e1bfb 1083 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 1084 break;
411e1bfb 1085 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 1086 break;
411e1bfb 1087 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 1088 break;
411e1bfb 1089 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 1090 break;
411e1bfb 1091 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 1092 break;
411e1bfb 1093 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 1094 break;
411e1bfb 1095 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 1096 break;
411e1bfb 1097 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1098 break;
411e1bfb 1099 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1100 break;
411e1bfb 1101 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1102 break;
411e1bfb 1103 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1104 break;
411e1bfb 1105 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1106 break;
411e1bfb 1107 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1108 break;
411e1bfb 1109 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1110 break;
411e1bfb 1111 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1112 break;
411e1bfb 1113 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1114 break;
411e1bfb 1115 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1116 break;
411e1bfb 1117 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1118 break;
411e1bfb 1119 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1120 break;
411e1bfb 1121 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1122 break;
411e1bfb 1123 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1124 break;
411e1bfb 1125 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1126 break;
411e1bfb 1127 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1128 break;
411e1bfb 1129 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1130 break;
411e1bfb 1131 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1132 break;
411e1bfb 1133 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1134 break;
411e1bfb 1135 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1136 break;
411e1bfb 1137 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1138 break;
411e1bfb 1139 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1140 break;
411e1bfb 1141 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1142 break;
411e1bfb 1143 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1144 break;
411e1bfb 1145 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1146 break;
c213164a 1147 case BFD_RELOC_PPC64_TLS_PCREL:
411e1bfb 1148 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1149 break;
727fc41e
AM
1150 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1151 break;
1152 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1153 break;
411e1bfb 1154 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1155 break;
411e1bfb 1156 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1157 break;
411e1bfb 1158 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1159 break;
411e1bfb 1160 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1161 break;
f9c6b907
AM
1162 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1163 break;
411e1bfb 1164 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1165 break;
f9c6b907
AM
1166 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1167 break;
411e1bfb 1168 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1169 break;
411e1bfb
AM
1170 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1171 break;
1172 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1173 break;
1174 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1175 break;
f9c6b907
AM
1176 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1177 break;
411e1bfb
AM
1178 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1179 break;
f9c6b907
AM
1180 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1181 break;
411e1bfb
AM
1182 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1183 break;
1184 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1185 break;
1186 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1187 break;
1188 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1189 break;
1190 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1191 break;
1192 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1193 break;
1194 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1195 break;
1196 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1197 break;
1198 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1199 break;
1200 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1201 break;
1202 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1203 break;
1204 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1205 break;
1206 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1207 break;
1208 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1209 break;
1210 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1211 break;
1212 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1213 break;
1214 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1215 break;
1216 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1217 break;
1218 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1219 break;
1220 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1221 break;
1222 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1223 break;
1224 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1225 break;
1226 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1227 break;
1228 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1229 break;
1230 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1231 break;
1232 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1233 break;
1234 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1235 break;
1236 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1237 break;
1238 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1239 break;
25f23106
AM
1240 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1241 break;
1242 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1243 break;
1244 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1245 break;
1246 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1247 break;
4a969973
AM
1248 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1249 break;
1250 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1251 break;
1252 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1253 break;
1254 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1255 break;
1256 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1257 break;
1258 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1259 break;
7ba71655
AM
1260 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1261 break;
a680de9a
PB
1262 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1263 break;
006589cf
AM
1264 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1265 break;
45965137
AM
1266 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1267 break;
5663e321
AM
1268 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1269 break;
1270 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1271 break;
1272 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1273 break;
1274 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1275 break;
1276 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1277 break;
1278 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1279 break;
1280 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1281 break;
c213164a
AM
1282 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1283 break;
1284 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1285 break;
87c69f97 1286 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
c213164a 1287 break;
87c69f97 1288 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
c213164a 1289 break;
87c69f97 1290 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
c213164a 1291 break;
87c69f97 1292 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
c213164a 1293 break;
5663e321
AM
1294 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1295 break;
1296 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1297 break;
1298 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1299 break;
1300 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1301 break;
1302 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1303 break;
1304 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1305 break;
1306 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1307 break;
1308 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1309 break;
1310 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1311 break;
1312 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1313 break;
411e1bfb
AM
1314 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1315 break;
1316 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
1317 break;
1318 }
1319
4ce794b7 1320 return ppc64_elf_howto_table[r];
5bd4f169
AM
1321};
1322
157090f7 1323static reloc_howto_type *
87c69f97 1324ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
157090f7
AM
1325{
1326 unsigned int i;
87c69f97
AM
1327 static char *compat_map[][2] = {
1328 { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1329 { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1330 { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1331 { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1332 };
157090f7 1333
a4b6fadd 1334 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
1335 if (ppc64_elf_howto_raw[i].name != NULL
1336 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1337 return &ppc64_elf_howto_raw[i];
1338
87c69f97
AM
1339 /* Handle old names of relocations in case they were used by
1340 .reloc directives.
1341 FIXME: Remove this soon. Mapping the reloc names is very likely
1342 completely unnecessary. */
1343 for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1344 if (strcasecmp (compat_map[i][0], r_name) == 0)
1345 {
1346 _bfd_error_handler (_("warning: %s should be used rather than %s"),
1347 compat_map[i][1], compat_map[i][0]);
1348 return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1349 }
1350
157090f7
AM
1351 return NULL;
1352}
1353
5bd4f169
AM
1354/* Set the howto pointer for a PowerPC ELF reloc. */
1355
f3185997 1356static bfd_boolean
4aef7643 1357ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 1358 Elf_Internal_Rela *dst)
5bd4f169 1359{
65f38f15
AM
1360 unsigned int type;
1361
ef60b7ff 1362 /* Initialize howto table if needed. */
5bd4f169 1363 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
1364 ppc_howto_init ();
1365
65f38f15 1366 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 1367 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 1368 {
695344c0 1369 /* xgettext:c-format */
0aa13fee 1370 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 1371 abfd, type);
f3185997
NC
1372 bfd_set_error (bfd_error_bad_value);
1373 return FALSE;
d0fb9a8d 1374 }
65f38f15 1375 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
1376 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1377 {
1378 /* xgettext:c-format */
1379 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1380 abfd, type);
1381 bfd_set_error (bfd_error_bad_value);
1382 return FALSE;
1383 }
2cdcc330 1384
f3185997 1385 return TRUE;
5bd4f169
AM
1386}
1387
04c9666a 1388/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
1389
1390static bfd_reloc_status_type
4ce794b7
AM
1391ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1392 void *data, asection *input_section,
1393 bfd *output_bfd, char **error_message)
5bd4f169 1394{
a680de9a
PB
1395 enum elf_ppc64_reloc_type r_type;
1396 long insn;
1397 bfd_size_type octets;
3de43e7b 1398 bfd_vma value;
a680de9a 1399
805fc799
AM
1400 /* If this is a relocatable link (output_bfd test tells us), just
1401 call the generic function. Any adjustment will be done at final
1402 link time. */
1403 if (output_bfd != NULL)
cedb70c5 1404 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1405 input_section, output_bfd, error_message);
1406
5663e321
AM
1407 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1408 We won't actually be using the low bits, so trashing them
805fc799 1409 doesn't matter. */
a680de9a 1410 r_type = reloc_entry->howto->type;
5663e321
AM
1411 if (r_type == R_PPC64_ADDR16_HIGHERA34
1412 || r_type == R_PPC64_ADDR16_HIGHESTA34
1413 || r_type == R_PPC64_REL16_HIGHERA34
1414 || r_type == R_PPC64_REL16_HIGHESTA34)
1415 reloc_entry->addend += 1ULL << 33;
1416 else
1417 reloc_entry->addend += 1U << 15;
a680de9a
PB
1418 if (r_type != R_PPC64_REL16DX_HA)
1419 return bfd_reloc_continue;
1420
1421 value = 0;
1422 if (!bfd_is_com_section (symbol->section))
1423 value = symbol->value;
1424 value += (reloc_entry->addend
1425 + symbol->section->output_offset
1426 + symbol->section->output_section->vma);
1427 value -= (reloc_entry->address
1428 + input_section->output_offset
1429 + input_section->output_section->vma);
3de43e7b 1430 value = (bfd_signed_vma) value >> 16;
a680de9a 1431
bb294208 1432 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
a680de9a
PB
1433 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1434 insn &= ~0x1fffc1;
3de43e7b 1435 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 1436 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 1437 if (value + 0x8000 > 0xffff)
a680de9a
PB
1438 return bfd_reloc_overflow;
1439 return bfd_reloc_ok;
805fc799 1440}
5bd4f169 1441
2441e016
AM
1442static bfd_reloc_status_type
1443ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1444 void *data, asection *input_section,
1445 bfd *output_bfd, char **error_message)
1446{
1447 if (output_bfd != NULL)
1448 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1449 input_section, output_bfd, error_message);
1450
699733f6
AM
1451 if (strcmp (symbol->section->name, ".opd") == 0
1452 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
1453 {
1454 bfd_vma dest = opd_entry_value (symbol->section,
1455 symbol->value + reloc_entry->addend,
aef36ac1 1456 NULL, NULL, FALSE);
2441e016
AM
1457 if (dest != (bfd_vma) -1)
1458 reloc_entry->addend = dest - (symbol->value
1459 + symbol->section->output_section->vma
1460 + symbol->section->output_offset);
1461 }
810d4e75
AM
1462 else
1463 {
1464 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1465
1466 if (symbol->section->owner != abfd
9f284bf9 1467 && symbol->section->owner != NULL
810d4e75
AM
1468 && abiversion (symbol->section->owner) >= 2)
1469 {
1470 unsigned int i;
1471
1472 for (i = 0; i < symbol->section->owner->symcount; ++i)
1473 {
1474 asymbol *symdef = symbol->section->owner->outsymbols[i];
1475
1476 if (strcmp (symdef->name, symbol->name) == 0)
1477 {
1478 elfsym = (elf_symbol_type *) symdef;
1479 break;
1480 }
1481 }
1482 }
1483 reloc_entry->addend
1484 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1485 }
2441e016
AM
1486 return bfd_reloc_continue;
1487}
1488
805fc799 1489static bfd_reloc_status_type
4ce794b7
AM
1490ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1491 void *data, asection *input_section,
1492 bfd *output_bfd, char **error_message)
805fc799
AM
1493{
1494 long insn;
04c9666a 1495 enum elf_ppc64_reloc_type r_type;
805fc799 1496 bfd_size_type octets;
794e51c0
AM
1497 /* Assume 'at' branch hints. */
1498 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
1499
1500 /* If this is a relocatable link (output_bfd test tells us), just
1501 call the generic function. Any adjustment will be done at final
1502 link time. */
5bd4f169 1503 if (output_bfd != NULL)
cedb70c5 1504 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1505 input_section, output_bfd, error_message);
1506
bb294208 1507 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
805fc799
AM
1508 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1509 insn &= ~(0x01 << 21);
4ce794b7 1510 r_type = reloc_entry->howto->type;
805fc799
AM
1511 if (r_type == R_PPC64_ADDR14_BRTAKEN
1512 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 1513 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 1514
794e51c0 1515 if (is_isa_v2)
5bd4f169 1516 {
805fc799
AM
1517 /* Set 'a' bit. This is 0b00010 in BO field for branch
1518 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1519 for branch on CTR insns (BO == 1a00t or 1a01t). */
1520 if ((insn & (0x14 << 21)) == (0x04 << 21))
1521 insn |= 0x02 << 21;
1522 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1523 insn |= 0x08 << 21;
1524 else
2441e016 1525 goto out;
5bd4f169 1526 }
805fc799
AM
1527 else
1528 {
1529 bfd_vma target = 0;
1530 bfd_vma from;
5bd4f169 1531
805fc799
AM
1532 if (!bfd_is_com_section (symbol->section))
1533 target = symbol->value;
1534 target += symbol->section->output_section->vma;
1535 target += symbol->section->output_offset;
1536 target += reloc_entry->addend;
5bd4f169 1537
805fc799
AM
1538 from = (reloc_entry->address
1539 + input_section->output_offset
1540 + input_section->output_section->vma);
5bd4f169 1541
805fc799
AM
1542 /* Invert 'y' bit if not the default. */
1543 if ((bfd_signed_vma) (target - from) < 0)
1544 insn ^= 0x01 << 21;
1545 }
4ce794b7 1546 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
1547 out:
1548 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1549 input_section, output_bfd, error_message);
805fc799 1550}
5bd4f169 1551
805fc799 1552static bfd_reloc_status_type
4ce794b7
AM
1553ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1554 void *data, asection *input_section,
1555 bfd *output_bfd, char **error_message)
805fc799
AM
1556{
1557 /* If this is a relocatable link (output_bfd test tells us), just
1558 call the generic function. Any adjustment will be done at final
1559 link time. */
1560 if (output_bfd != NULL)
cedb70c5 1561 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 1562 input_section, output_bfd, error_message);
5bd4f169 1563
805fc799
AM
1564 /* Subtract the symbol section base address. */
1565 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
1566 return bfd_reloc_continue;
1567}
1568
805fc799 1569static bfd_reloc_status_type
4ce794b7
AM
1570ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1571 void *data, asection *input_section,
1572 bfd *output_bfd, char **error_message)
805fc799
AM
1573{
1574 /* If this is a relocatable link (output_bfd test tells us), just
1575 call the generic function. Any adjustment will be done at final
1576 link time. */
1577 if (output_bfd != NULL)
cedb70c5 1578 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1579 input_section, output_bfd, error_message);
1580
1581 /* Subtract the symbol section base address. */
1582 reloc_entry->addend -= symbol->section->output_section->vma;
1583
1584 /* Adjust the addend for sign extension of the low 16 bits. */
1585 reloc_entry->addend += 0x8000;
1586 return bfd_reloc_continue;
1587}
1588
1589static bfd_reloc_status_type
4ce794b7
AM
1590ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1591 void *data, asection *input_section,
1592 bfd *output_bfd, char **error_message)
805fc799
AM
1593{
1594 bfd_vma TOCstart;
1595
1596 /* If this is a relocatable link (output_bfd test tells us), just
1597 call the generic function. Any adjustment will be done at final
1598 link time. */
1599 if (output_bfd != NULL)
cedb70c5 1600 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1601 input_section, output_bfd, error_message);
1602
1603 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1604 if (TOCstart == 0)
1c865ab2 1605 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1606
1607 /* Subtract the TOC base address. */
1608 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1609 return bfd_reloc_continue;
1610}
1611
1612static bfd_reloc_status_type
4ce794b7
AM
1613ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1614 void *data, asection *input_section,
1615 bfd *output_bfd, char **error_message)
805fc799
AM
1616{
1617 bfd_vma TOCstart;
1618
1619 /* If this is a relocatable link (output_bfd test tells us), just
1620 call the generic function. Any adjustment will be done at final
1621 link time. */
1622 if (output_bfd != NULL)
cedb70c5 1623 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1624 input_section, output_bfd, error_message);
1625
1626 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1627 if (TOCstart == 0)
1c865ab2 1628 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1629
1630 /* Subtract the TOC base address. */
1631 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1632
1633 /* Adjust the addend for sign extension of the low 16 bits. */
1634 reloc_entry->addend += 0x8000;
1635 return bfd_reloc_continue;
1636}
1637
1638static bfd_reloc_status_type
4ce794b7
AM
1639ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1640 void *data, asection *input_section,
1641 bfd *output_bfd, char **error_message)
805fc799
AM
1642{
1643 bfd_vma TOCstart;
1644 bfd_size_type octets;
1645
1646 /* If this is a relocatable link (output_bfd test tells us), just
1647 call the generic function. Any adjustment will be done at final
1648 link time. */
1649 if (output_bfd != NULL)
cedb70c5 1650 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1651 input_section, output_bfd, error_message);
1652
1653 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1654 if (TOCstart == 0)
1c865ab2 1655 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799 1656
bb294208 1657 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
805fc799
AM
1658 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1659 return bfd_reloc_ok;
1660}
1661
5663e321
AM
1662static bfd_reloc_status_type
1663ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1664 void *data, asection *input_section,
1665 bfd *output_bfd, char **error_message)
1666{
1667 uint64_t insn;
1668 bfd_vma targ;
1669
1670 if (output_bfd != NULL)
1671 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1672 input_section, output_bfd, error_message);
1673
1674 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1675 insn <<= 32;
1676 insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1677
1678 targ = (symbol->section->output_section->vma
1679 + symbol->section->output_offset
1680 + reloc_entry->addend);
1681 if (!bfd_is_com_section (symbol->section))
1682 targ += symbol->value;
1683 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1684 targ += 1ULL << 33;
1685 if (reloc_entry->howto->pc_relative)
1686 {
1687 bfd_vma from = (reloc_entry->address
1688 + input_section->output_offset
1689 + input_section->output_section->vma);
1690 targ -=from;
1691 }
1692 targ >>= reloc_entry->howto->rightshift;
1693 insn &= ~reloc_entry->howto->dst_mask;
1694 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1695 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1696 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1697 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1698 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1699 >= 1ULL << reloc_entry->howto->bitsize))
1700 return bfd_reloc_overflow;
1701 return bfd_reloc_ok;
1702}
1703
805fc799 1704static bfd_reloc_status_type
4ce794b7
AM
1705ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1706 void *data, asection *input_section,
1707 bfd *output_bfd, char **error_message)
805fc799
AM
1708{
1709 /* If this is a relocatable link (output_bfd test tells us), just
1710 call the generic function. Any adjustment will be done at final
1711 link time. */
1712 if (output_bfd != NULL)
cedb70c5 1713 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1714 input_section, output_bfd, error_message);
1715
1716 if (error_message != NULL)
1717 {
7fbd5f4e
AM
1718 static char *message;
1719 free (message);
1720 if (asprintf (&message, _("generic linker can't handle %s"),
1721 reloc_entry->howto->name) < 0)
1722 message = NULL;
1723 *error_message = message;
805fc799
AM
1724 }
1725 return bfd_reloc_dangerous;
1726}
1727
927be08e
AM
1728/* Track GOT entries needed for a given symbol. We might need more
1729 than one got entry per symbol. */
1730struct got_entry
1731{
1732 struct got_entry *next;
1733
1734 /* The symbol addend that we'll be placing in the GOT. */
1735 bfd_vma addend;
1736
1737 /* Unlike other ELF targets, we use separate GOT entries for the same
1738 symbol referenced from different input files. This is to support
1739 automatic multiple TOC/GOT sections, where the TOC base can vary
1740 from one input file to another. After partitioning into TOC groups
1741 we merge entries within the group.
1742
1743 Point to the BFD owning this GOT entry. */
1744 bfd *owner;
1745
1746 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1747 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1748 unsigned char tls_type;
927be08e
AM
1749
1750 /* Non-zero if got.ent points to real entry. */
f961d9dd 1751 unsigned char is_indirect;
927be08e
AM
1752
1753 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1754 union
2cdcc330
AM
1755 {
1756 bfd_signed_vma refcount;
1757 bfd_vma offset;
1758 struct got_entry *ent;
1759 } got;
927be08e
AM
1760};
1761
1762/* The same for PLT. */
1763struct plt_entry
1764{
1765 struct plt_entry *next;
1766
1767 bfd_vma addend;
1768
1769 union
2cdcc330
AM
1770 {
1771 bfd_signed_vma refcount;
1772 bfd_vma offset;
1773 } plt;
927be08e
AM
1774};
1775
e717da7e
AM
1776struct ppc64_elf_obj_tdata
1777{
1778 struct elf_obj_tdata elf;
1779
1780 /* Shortcuts to dynamic linker sections. */
1781 asection *got;
1782 asection *relgot;
1783
b3fac117
AM
1784 /* Used during garbage collection. We attach global symbols defined
1785 on removed .opd entries to this section so that the sym is removed. */
1786 asection *deleted_section;
81688140 1787
927be08e 1788 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1789 sections means we potentially need one of these for each input bfd. */
927be08e 1790 struct got_entry tlsld_got;
8860955f 1791
2cdcc330
AM
1792 union
1793 {
729eabd5
AM
1794 /* A copy of relocs before they are modified for --emit-relocs. */
1795 Elf_Internal_Rela *relocs;
1796
1797 /* Section contents. */
1798 bfd_byte *contents;
1799 } opd;
d77c8a4b
AM
1800
1801 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1802 the reloc to be in the range -32768 to 32767. */
98528052
AM
1803 unsigned int has_small_toc_reloc : 1;
1804
560c8763
AM
1805 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1806 instruction not one we handle. */
1807 unsigned int unexpected_toc_insn : 1;
066f4018 1808
903b777d
AM
1809 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1810 this file. */
1811 unsigned int has_optrel : 1;
e717da7e
AM
1812};
1813
1814#define ppc64_elf_tdata(bfd) \
1815 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1816
1817#define ppc64_tlsld_got(bfd) \
1818 (&ppc64_elf_tdata (bfd)->tlsld_got)
1819
0c8d6e5c
AM
1820#define is_ppc64_elf(bfd) \
1821 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1822 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1823
e717da7e
AM
1824/* Override the generic function because we store some extras. */
1825
1826static bfd_boolean
1827ppc64_elf_mkobject (bfd *abfd)
1828{
0ffa91dd 1829 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1830 PPC64_ELF_DATA);
e717da7e
AM
1831}
1832
feee612b 1833/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1834 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1835
b34976b6 1836static bfd_boolean
4ce794b7 1837ppc64_elf_object_p (bfd *abfd)
feee612b 1838{
14b57c7c
AM
1839 if (!abfd->arch_info->the_default)
1840 return TRUE;
1841
1842 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1843 {
1844 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1845
1846 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1847 {
1848 /* Relies on arch after 32 bit default being 64 bit default. */
1849 abfd->arch_info = abfd->arch_info->next;
1850 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1851 }
1852 }
14b57c7c 1853 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1854}
1855
d37c89e5
AM
1856/* Support for core dump NOTE sections. */
1857
1858static bfd_boolean
1859ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1860{
eea6121a 1861 size_t offset, size;
d37c89e5
AM
1862
1863 if (note->descsz != 504)
1864 return FALSE;
1865
1866 /* pr_cursig */
228e534f 1867 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1868
1869 /* pr_pid */
228e534f 1870 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1871
1872 /* pr_reg */
1873 offset = 112;
eea6121a 1874 size = 384;
d37c89e5
AM
1875
1876 /* Make a ".reg/999" section. */
1877 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1878 size, note->descpos + offset);
d37c89e5
AM
1879}
1880
1881static bfd_boolean
1882ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1883{
1884 if (note->descsz != 136)
1885 return FALSE;
1886
228e534f 1887 elf_tdata (abfd)->core->pid
bc989cdc 1888 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1889 elf_tdata (abfd)->core->program
d37c89e5 1890 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1891 elf_tdata (abfd)->core->command
d37c89e5
AM
1892 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1893
1894 return TRUE;
1895}
1896
183e98be
AM
1897static char *
1898ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1899 ...)
1900{
1901 switch (note_type)
1902 {
1903 default:
1904 return NULL;
1905
1906 case NT_PRPSINFO:
1907 {
9ef6d1e3 1908 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1909 va_list ap;
1910
1911 va_start (ap, note_type);
75cd47ed 1912 memset (data, 0, sizeof (data));
183e98be 1913 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1914#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1915 DIAGNOSTIC_PUSH;
be3e27bb 1916 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1917 -Wstringop-truncation:
1918 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1919 */
95da9854
L
1920 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1921#endif
183e98be 1922 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1923#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1924 DIAGNOSTIC_POP;
fe75810f 1925#endif
183e98be
AM
1926 va_end (ap);
1927 return elfcore_write_note (abfd, buf, bufsiz,
1928 "CORE", note_type, data, sizeof (data));
1929 }
1930
1931 case NT_PRSTATUS:
1932 {
1933 char data[504];
1934 va_list ap;
1935 long pid;
1936 int cursig;
1937 const void *greg;
1938
1939 va_start (ap, note_type);
1940 memset (data, 0, 112);
1941 pid = va_arg (ap, long);
1942 bfd_put_32 (abfd, pid, data + 32);
1943 cursig = va_arg (ap, int);
1944 bfd_put_16 (abfd, cursig, data + 12);
1945 greg = va_arg (ap, const void *);
1946 memcpy (data + 112, greg, 384);
1947 memset (data + 496, 0, 8);
1948 va_end (ap);
1949 return elfcore_write_note (abfd, buf, bufsiz,
1950 "CORE", note_type, data, sizeof (data));
1951 }
1952 }
1953}
1954
5d35169e
AM
1955/* Add extra PPC sections. */
1956
2cdcc330 1957static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1958{
07d6d2b8
AM
1959 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1960 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1961 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1962 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1963 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1964 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1965 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1966};
1967
7c8fe5c4
AM
1968enum _ppc64_sec_type {
1969 sec_normal = 0,
1970 sec_opd = 1,
1971 sec_toc = 2
1972};
1973
f0abc2a1
AM
1974struct _ppc64_elf_section_data
1975{
1976 struct bfd_elf_section_data elf;
411e1bfb 1977
f0abc2a1
AM
1978 union
1979 {
51aecdc5
AM
1980 /* An array with one entry for each opd function descriptor,
1981 and some spares since opd entries may be either 16 or 24 bytes. */
1982#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
1983 struct _opd_sec_data
1984 {
1985 /* Points to the function code section for local opd entries. */
1986 asection **func_sec;
1987
1988 /* After editing .opd, adjust references to opd local syms. */
1989 long *adjust;
1990 } opd;
7c8fe5c4 1991
3a71aa26
AM
1992 /* An array for toc sections, indexed by offset/8. */
1993 struct _toc_sec_data
1994 {
1995 /* Specifies the relocation symbol index used at a given toc offset. */
1996 unsigned *symndx;
1997
1998 /* And the relocation addend. */
1999 bfd_vma *add;
2000 } toc;
7c8fe5c4
AM
2001 } u;
2002
2003 enum _ppc64_sec_type sec_type:2;
411e1bfb 2004
7c8fe5c4
AM
2005 /* Flag set when small branches are detected. Used to
2006 select suitable defaults for the stub group size. */
2007 unsigned int has_14bit_branch:1;
3e04d765
AM
2008
2009 /* Flag set when PLTCALL relocs are detected. */
2010 unsigned int has_pltcall:1;
066f4018 2011
903b777d
AM
2012 /* Flag set when section has PLT/GOT/TOC relocations that can be
2013 optimised. */
2014 unsigned int has_optrel:1;
f0abc2a1
AM
2015};
2016
2017#define ppc64_elf_section_data(sec) \
411e1bfb 2018 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
2019
2020static bfd_boolean
4ce794b7 2021ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2022{
f592407e
AM
2023 if (!sec->used_by_bfd)
2024 {
2025 struct _ppc64_elf_section_data *sdata;
986f0783 2026 size_t amt = sizeof (*sdata);
f0abc2a1 2027
f592407e
AM
2028 sdata = bfd_zalloc (abfd, amt);
2029 if (sdata == NULL)
2030 return FALSE;
2031 sec->used_by_bfd = sdata;
2032 }
f0abc2a1
AM
2033
2034 return _bfd_elf_new_section_hook (abfd, sec);
2035}
4025353c 2036
bf577467
AM
2037static bfd_boolean
2038ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2039{
2040 const char *name = hdr->bfd_section->name;
2041
2042 if (strncmp (name, ".sbss", 5) == 0
2043 || strncmp (name, ".sdata", 6) == 0)
2044 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2045
2046 return TRUE;
2047}
2048
74f0fb50 2049static struct _opd_sec_data *
4025353c
AM
2050get_opd_info (asection * sec)
2051{
2052 if (sec != NULL
2053 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2054 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2055 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2056 return NULL;
2057}
90e3cdf2
JJ
2058\f
2059/* Parameters for the qsort hook. */
90e3cdf2 2060static bfd_boolean synthetic_relocatable;
8cb1c2c8 2061static const asection *synthetic_opd;
90e3cdf2 2062
699733f6 2063/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2064
2065static int
2066compare_symbols (const void *ap, const void *bp)
2067{
2cdcc330
AM
2068 const asymbol *a = *(const asymbol **) ap;
2069 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 2070
699733f6
AM
2071 /* Section symbols first. */
2072 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2073 return -1;
699733f6 2074 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2075 return 1;
2076
699733f6 2077 /* then .opd symbols. */
cd285db5
AM
2078 if (synthetic_opd != NULL)
2079 {
2080 if (strcmp (a->section->name, ".opd") == 0
2081 && strcmp (b->section->name, ".opd") != 0)
2082 return -1;
2083 if (strcmp (a->section->name, ".opd") != 0
2084 && strcmp (b->section->name, ".opd") == 0)
2085 return 1;
2086 }
90e3cdf2 2087
699733f6 2088 /* then other code symbols. */
2cdcc330
AM
2089 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2090 == (SEC_CODE | SEC_ALLOC))
2091 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2092 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2093 return -1;
2094
2cdcc330
AM
2095 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2096 != (SEC_CODE | SEC_ALLOC))
2097 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2098 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2099 return 1;
2100
2101 if (synthetic_relocatable)
2102 {
2103 if (a->section->id < b->section->id)
2104 return -1;
2105
2106 if (a->section->id > b->section->id)
2107 return 1;
2108 }
2109
2110 if (a->value + a->section->vma < b->value + b->section->vma)
2111 return -1;
2112
2113 if (a->value + a->section->vma > b->value + b->section->vma)
2114 return 1;
2115
4d35a0aa
AM
2116 /* For syms with the same value, prefer strong dynamic global function
2117 syms over other syms. */
2118 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2119 return -1;
2120
2121 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2122 return 1;
2123
2124 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 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_WEAK) == 0 && (b->flags & BSF_WEAK) != 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_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2137 return -1;
2138
2139 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2140 return 1;
2141
dcea6a95
AM
2142 /* Finally, sort on where the symbol is in memory. The symbols will
2143 be in at most two malloc'd blocks, one for static syms, one for
2144 dynamic syms, and we distinguish the two blocks above by testing
2145 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2146 originally in the same order as the symbols (and we're not
2147 sorting the symbols themselves), this ensures a stable sort. */
2148 if (a < b)
2149 return -1;
2150 if (a > b)
2151 return 1;
2152 return 0;
90e3cdf2
JJ
2153}
2154
699733f6 2155/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2156
699733f6 2157static asymbol *
9ad9b810
AM
2158sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2159 bfd_vma value)
90e3cdf2 2160{
9ad9b810 2161 size_t mid;
90e3cdf2 2162
7292b3ac 2163 if (id == (unsigned) -1)
699733f6
AM
2164 {
2165 while (lo < hi)
2166 {
2167 mid = (lo + hi) >> 1;
2168 if (syms[mid]->value + syms[mid]->section->vma < value)
2169 lo = mid + 1;
2170 else if (syms[mid]->value + syms[mid]->section->vma > value)
2171 hi = mid;
2172 else
2173 return syms[mid];
2174 }
2175 }
2176 else
2177 {
2178 while (lo < hi)
2179 {
2180 mid = (lo + hi) >> 1;
2181 if (syms[mid]->section->id < id)
2182 lo = mid + 1;
2183 else if (syms[mid]->section->id > id)
2184 hi = mid;
2185 else if (syms[mid]->value < value)
2186 lo = mid + 1;
2187 else if (syms[mid]->value > value)
2188 hi = mid;
2189 else
2190 return syms[mid];
2191 }
2192 }
2193 return NULL;
90e3cdf2
JJ
2194}
2195
468392fb
AM
2196static bfd_boolean
2197section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2198{
2199 bfd_vma vma = *(bfd_vma *) ptr;
2200 return ((section->flags & SEC_ALLOC) != 0
2201 && section->vma <= vma
2202 && vma < section->vma + section->size);
2203}
2204
699733f6 2205/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
2206 entry syms. Also generate @plt symbols for the glink branch table.
2207 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
2208
2209static long
a7535cf3
AM
2210ppc64_elf_get_synthetic_symtab (bfd *abfd,
2211 long static_count, asymbol **static_syms,
2212 long dyn_count, asymbol **dyn_syms,
c9727e01 2213 asymbol **ret)
90e3cdf2
JJ
2214{
2215 asymbol *s;
0ccf57bd 2216 size_t i, j, count;
90e3cdf2 2217 char *names;
0ccf57bd 2218 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 2219 asection *opd = NULL;
90e3cdf2 2220 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2221 asymbol **syms;
ee67d69a 2222 int abi = abiversion (abfd);
90e3cdf2
JJ
2223
2224 *ret = NULL;
2225
ee67d69a
AM
2226 if (abi < 2)
2227 {
2228 opd = bfd_get_section_by_name (abfd, ".opd");
2229 if (opd == NULL && abi == 1)
2230 return 0;
2231 }
90e3cdf2 2232
a5259595
AM
2233 syms = NULL;
2234 codesecsym = 0;
2235 codesecsymend = 0;
2236 secsymend = 0;
2237 opdsymend = 0;
2238 symcount = 0;
2239 if (opd != NULL)
c9727e01 2240 {
a5259595
AM
2241 symcount = static_count;
2242 if (!relocatable)
2243 symcount += dyn_count;
2244 if (symcount == 0)
2245 return 0;
c9727e01 2246
a5259595
AM
2247 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2248 if (syms == NULL)
2249 return -1;
90e3cdf2 2250
a5259595
AM
2251 if (!relocatable && static_count != 0 && dyn_count != 0)
2252 {
2253 /* Use both symbol tables. */
2254 memcpy (syms, static_syms, static_count * sizeof (*syms));
2255 memcpy (syms + static_count, dyn_syms,
2256 (dyn_count + 1) * sizeof (*syms));
2257 }
2258 else if (!relocatable && static_count == 0)
2259 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2260 else
2261 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2262
0ccf57bd
AM
2263 /* Trim uninteresting symbols. Interesting symbols are section,
2264 function, and notype symbols. */
2265 for (i = 0, j = 0; i < symcount; ++i)
2266 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2267 | BSF_RELC | BSF_SRELC)) == 0)
2268 syms[j++] = syms[i];
2269 symcount = j;
2270
a5259595
AM
2271 synthetic_relocatable = relocatable;
2272 synthetic_opd = opd;
2273 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2274
a5259595
AM
2275 if (!relocatable && symcount > 1)
2276 {
bfa5bd2a
PA
2277 /* Trim duplicate syms, since we may have merged the normal
2278 and dynamic symbols. Actually, we only care about syms
2279 that have different values, so trim any with the same
2280 value. Don't consider ifunc and ifunc resolver symbols
2281 duplicates however, because GDB wants to know whether a
2282 text symbol is an ifunc resolver. */
a5259595 2283 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2284 {
2285 const asymbol *s0 = syms[i - 1];
2286 const asymbol *s1 = syms[i];
2287
2288 if ((s0->value + s0->section->vma
2289 != s1->value + s1->section->vma)
2290 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2291 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2292 syms[j++] = syms[i];
2293 }
a5259595
AM
2294 symcount = j;
2295 }
699733f6 2296
a5259595
AM
2297 i = 0;
2298 /* Note that here and in compare_symbols we can't compare opd and
2299 sym->section directly. With separate debug info files, the
2300 symbols will be extracted from the debug file while abfd passed
2301 to this function is the real binary. */
8bd10d6b
AM
2302 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2303 && strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2304 ++i;
2305 codesecsym = i;
2306
2307 for (; i < symcount; ++i)
2308 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2309 | SEC_THREAD_LOCAL))
2310 != (SEC_CODE | SEC_ALLOC))
2311 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2312 break;
2313 codesecsymend = i;
2314
2315 for (; i < symcount; ++i)
2316 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2317 break;
2318 secsymend = i;
2319
2320 for (; i < symcount; ++i)
2321 if (strcmp (syms[i]->section->name, ".opd") != 0)
2322 break;
2323 opdsymend = i;
2324
2325 for (; i < symcount; ++i)
2cdcc330
AM
2326 if (((syms[i]->section->flags
2327 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2328 != (SEC_CODE | SEC_ALLOC))
2329 break;
2330 symcount = i;
2331 }
c9727e01 2332 count = 0;
90e3cdf2 2333
699733f6 2334 if (relocatable)
90e3cdf2 2335 {
699733f6
AM
2336 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2337 arelent *r;
2338 size_t size;
0ccf57bd 2339 size_t relcount;
90e3cdf2 2340
468392fb
AM
2341 if (opdsymend == secsymend)
2342 goto done;
2343
699733f6 2344 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2345 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2346 if (relcount == 0)
c9727e01 2347 goto done;
90e3cdf2 2348
7356fed5
AM
2349 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2350 {
2351 count = -1;
2352 goto done;
2353 }
2354
699733f6 2355 size = 0;
595da8c5 2356 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2357 {
2358 asymbol *sym;
90e3cdf2 2359
595da8c5 2360 while (r < opd->relocation + relcount
699733f6
AM
2361 && r->address < syms[i]->value + opd->vma)
2362 ++r;
90e3cdf2 2363
595da8c5 2364 if (r == opd->relocation + relcount)
699733f6 2365 break;
90e3cdf2 2366
699733f6
AM
2367 if (r->address != syms[i]->value + opd->vma)
2368 continue;
90e3cdf2 2369
699733f6
AM
2370 if (r->howto->type != R_PPC64_ADDR64)
2371 continue;
90e3cdf2 2372
699733f6
AM
2373 sym = *r->sym_ptr_ptr;
2374 if (!sym_exists_at (syms, opdsymend, symcount,
2375 sym->section->id, sym->value + r->addend))
2376 {
2377 ++count;
2378 size += sizeof (asymbol);
2379 size += strlen (syms[i]->name) + 2;
2380 }
2381 }
90e3cdf2 2382
c4b0b099
AM
2383 if (size == 0)
2384 goto done;
699733f6
AM
2385 s = *ret = bfd_malloc (size);
2386 if (s == NULL)
2387 {
7356fed5 2388 count = -1;
c9727e01 2389 goto done;
699733f6 2390 }
90e3cdf2 2391
699733f6 2392 names = (char *) (s + count);
90e3cdf2 2393
595da8c5 2394 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2395 {
699733f6 2396 asymbol *sym;
90e3cdf2 2397
595da8c5 2398 while (r < opd->relocation + relcount
699733f6
AM
2399 && r->address < syms[i]->value + opd->vma)
2400 ++r;
90e3cdf2 2401
595da8c5 2402 if (r == opd->relocation + relcount)
699733f6
AM
2403 break;
2404
2405 if (r->address != syms[i]->value + opd->vma)
2406 continue;
2407
2408 if (r->howto->type != R_PPC64_ADDR64)
2409 continue;
90e3cdf2 2410
699733f6
AM
2411 sym = *r->sym_ptr_ptr;
2412 if (!sym_exists_at (syms, opdsymend, symcount,
2413 sym->section->id, sym->value + r->addend))
2414 {
2415 size_t len;
2416
2417 *s = *syms[i];
6ba2a415 2418 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2419 s->section = sym->section;
2420 s->value = sym->value + r->addend;
2421 s->name = names;
2422 *names++ = '.';
2423 len = strlen (syms[i]->name);
2424 memcpy (names, syms[i]->name, len + 1);
2425 names += len + 1;
6f610d07
UW
2426 /* Have udata.p point back to the original symbol this
2427 synthetic symbol was derived from. */
2428 s->udata.p = syms[i];
699733f6
AM
2429 s++;
2430 }
2431 }
2432 }
2433 else
90e3cdf2 2434 {
468392fb 2435 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 2436 bfd_byte *contents = NULL;
699733f6 2437 size_t size;
0ccf57bd 2438 size_t plt_count = 0;
468392fb
AM
2439 bfd_vma glink_vma = 0, resolv_vma = 0;
2440 asection *dynamic, *glink = NULL, *relplt = NULL;
2441 arelent *p;
90e3cdf2 2442
ee67d69a 2443 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2444 {
c4b0b099
AM
2445 free_contents_and_exit_err:
2446 count = -1;
ee67d69a 2447 free_contents_and_exit:
c9594989 2448 free (contents);
c9727e01 2449 goto done;
699733f6 2450 }
90e3cdf2 2451
699733f6
AM
2452 size = 0;
2453 for (i = secsymend; i < opdsymend; ++i)
2454 {
2455 bfd_vma ent;
90e3cdf2 2456
5ef11c02
AM
2457 /* Ignore bogus symbols. */
2458 if (syms[i]->value > opd->size - 8)
2459 continue;
2460
699733f6
AM
2461 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2462 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2463 {
2464 ++count;
2465 size += sizeof (asymbol);
2466 size += strlen (syms[i]->name) + 2;
2467 }
2468 }
90e3cdf2 2469
468392fb 2470 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2471 if (dyn_count != 0
2472 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2473 {
2474 bfd_byte *dynbuf, *extdyn, *extdynend;
2475 size_t extdynsize;
2476 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2477
2478 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2479 goto free_contents_and_exit_err;
468392fb
AM
2480
2481 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2482 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2483
2484 extdyn = dynbuf;
2485 extdynend = extdyn + dynamic->size;
2486 for (; extdyn < extdynend; extdyn += extdynsize)
2487 {
2488 Elf_Internal_Dyn dyn;
2489 (*swap_dyn_in) (abfd, extdyn, &dyn);
2490
2491 if (dyn.d_tag == DT_NULL)
2492 break;
2493
2494 if (dyn.d_tag == DT_PPC64_GLINK)
2495 {
9e390558
AM
2496 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2497 See comment in ppc64_elf_finish_dynamic_sections. */
2498 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2499 /* The .glink section usually does not survive the final
2500 link; search for the section (usually .text) where the
2501 glink stubs now reside. */
2502 glink = bfd_sections_find_if (abfd, section_covers_vma,
2503 &glink_vma);
2504 break;
2505 }
2506 }
2507
2508 free (dynbuf);
2509 }
2510
2511 if (glink != NULL)
2512 {
2513 /* Determine __glink trampoline by reading the relative branch
2514 from the first glink stub. */
2515 bfd_byte buf[4];
b9e5796b
AM
2516 unsigned int off = 0;
2517
2518 while (bfd_get_section_contents (abfd, glink, buf,
2519 glink_vma + off - glink->vma, 4))
468392fb
AM
2520 {
2521 unsigned int insn = bfd_get_32 (abfd, buf);
2522 insn ^= B_DOT;
2523 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2524 {
2cdcc330
AM
2525 resolv_vma
2526 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2527 break;
2528 }
2529 off += 4;
2530 if (off > 4)
2531 break;
468392fb
AM
2532 }
2533
2534 if (resolv_vma)
2535 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2536
066ee829
AM
2537 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2538 if (relplt != NULL)
2539 {
2540 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2cdcc330 2541 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 2542 goto free_contents_and_exit_err;
68ffbac6 2543
066ee829
AM
2544 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2545 size += plt_count * sizeof (asymbol);
468392fb 2546
066ee829
AM
2547 p = relplt->relocation;
2548 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2549 {
2550 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2551 if (p->addend != 0)
2552 size += sizeof ("+0x") - 1 + 16;
2553 }
066ee829 2554 }
468392fb
AM
2555 }
2556
c4b0b099
AM
2557 if (size == 0)
2558 goto free_contents_and_exit;
699733f6
AM
2559 s = *ret = bfd_malloc (size);
2560 if (s == NULL)
c4b0b099 2561 goto free_contents_and_exit_err;
90e3cdf2 2562
468392fb 2563 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2564
699733f6 2565 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2566 {
699733f6 2567 bfd_vma ent;
90e3cdf2 2568
5ef11c02
AM
2569 if (syms[i]->value > opd->size - 8)
2570 continue;
2571
699733f6
AM
2572 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2573 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2574 {
0ccf57bd 2575 size_t lo, hi;
699733f6 2576 size_t len;
c9727e01 2577 asection *sec = abfd->sections;
90e3cdf2 2578
699733f6
AM
2579 *s = *syms[i];
2580 lo = codesecsym;
2581 hi = codesecsymend;
2582 while (lo < hi)
2583 {
0ccf57bd 2584 size_t mid = (lo + hi) >> 1;
699733f6
AM
2585 if (syms[mid]->section->vma < ent)
2586 lo = mid + 1;
2587 else if (syms[mid]->section->vma > ent)
2588 hi = mid;
2589 else
c9727e01
AM
2590 {
2591 sec = syms[mid]->section;
2592 break;
2593 }
699733f6
AM
2594 }
2595
c9727e01 2596 if (lo >= hi && lo > codesecsym)
699733f6 2597 sec = syms[lo - 1]->section;
699733f6
AM
2598
2599 for (; sec != NULL; sec = sec->next)
2600 {
2601 if (sec->vma > ent)
2602 break;
63524580
JK
2603 /* SEC_LOAD may not be set if SEC is from a separate debug
2604 info file. */
2605 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2606 break;
2607 if ((sec->flags & SEC_CODE) != 0)
2608 s->section = sec;
2609 }
6ba2a415 2610 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2611 s->value = ent - s->section->vma;
2612 s->name = names;
2613 *names++ = '.';
2614 len = strlen (syms[i]->name);
2615 memcpy (names, syms[i]->name, len + 1);
2616 names += len + 1;
6f610d07
UW
2617 /* Have udata.p point back to the original symbol this
2618 synthetic symbol was derived from. */
2619 s->udata.p = syms[i];
699733f6 2620 s++;
90e3cdf2 2621 }
90e3cdf2 2622 }
699733f6 2623 free (contents);
468392fb
AM
2624
2625 if (glink != NULL && relplt != NULL)
2626 {
2627 if (resolv_vma)
2628 {
2629 /* Add a symbol for the main glink trampoline. */
86a4952b 2630 memset (s, 0, sizeof *s);
468392fb 2631 s->the_bfd = abfd;
6ba2a415 2632 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2633 s->section = glink;
2634 s->value = resolv_vma - glink->vma;
2635 s->name = names;
2cdcc330
AM
2636 memcpy (names, "__glink_PLTresolve",
2637 sizeof ("__glink_PLTresolve"));
468392fb
AM
2638 names += sizeof ("__glink_PLTresolve");
2639 s++;
2640 count++;
2641 }
2642
2643 /* FIXME: It would be very much nicer to put sym@plt on the
2644 stub rather than on the glink branch table entry. The
2645 objdump disassembler would then use a sensible symbol
2646 name on plt calls. The difficulty in doing so is
2647 a) finding the stubs, and,
2648 b) matching stubs against plt entries, and,
2649 c) there can be multiple stubs for a given plt entry.
2650
2651 Solving (a) could be done by code scanning, but older
2652 ppc64 binaries used different stubs to current code.
2653 (b) is the tricky one since you need to known the toc
2654 pointer for at least one function that uses a pic stub to
2655 be able to calculate the plt address referenced.
2656 (c) means gdb would need to set multiple breakpoints (or
2657 find the glink branch itself) when setting breakpoints
2658 for pending shared library loads. */
2659 p = relplt->relocation;
2660 for (i = 0; i < plt_count; i++, p++)
2661 {
2662 size_t len;
2663
2664 *s = **p->sym_ptr_ptr;
2665 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2666 we are defining a symbol, ensure one of them is set. */
2667 if ((s->flags & BSF_LOCAL) == 0)
2668 s->flags |= BSF_GLOBAL;
6ba2a415 2669 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2670 s->section = glink;
2671 s->value = glink_vma - glink->vma;
2672 s->name = names;
2673 s->udata.p = NULL;
2674 len = strlen ((*p->sym_ptr_ptr)->name);
2675 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2676 names += len;
e054468f
AM
2677 if (p->addend != 0)
2678 {
2679 memcpy (names, "+0x", sizeof ("+0x") - 1);
2680 names += sizeof ("+0x") - 1;
2681 bfd_sprintf_vma (abfd, names, p->addend);
2682 names += strlen (names);
2683 }
468392fb
AM
2684 memcpy (names, "@plt", sizeof ("@plt"));
2685 names += sizeof ("@plt");
2686 s++;
b9e5796b
AM
2687 if (abi < 2)
2688 {
2689 glink_vma += 8;
2690 if (i >= 0x8000)
2691 glink_vma += 4;
2692 }
2693 else
468392fb
AM
2694 glink_vma += 4;
2695 }
2696 count += plt_count;
2697 }
90e3cdf2
JJ
2698 }
2699
c9727e01 2700 done:
a7535cf3 2701 free (syms);
90e3cdf2
JJ
2702 return count;
2703}
5bd4f169 2704\f
65f38f15
AM
2705/* The following functions are specific to the ELF linker, while
2706 functions above are used generally. Those named ppc64_elf_* are
2707 called by the main ELF linker code. They appear in this file more
2708 or less in the order in which they are called. eg.
2709 ppc64_elf_check_relocs is called early in the link process,
2710 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2711 called.
2712
2713 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2714 functions have both a function code symbol and a function descriptor
2715 symbol. A call to foo in a relocatable object file looks like:
2716
2717 . .text
2718 . x:
2719 . bl .foo
2720 . nop
2721
2722 The function definition in another object file might be:
2723
2724 . .section .opd
2725 . foo: .quad .foo
2726 . .quad .TOC.@tocbase
2727 . .quad 0
2728 .
2729 . .text
2730 . .foo: blr
2731
2732 When the linker resolves the call during a static link, the branch
2733 unsurprisingly just goes to .foo and the .opd information is unused.
2734 If the function definition is in a shared library, things are a little
2735 different: The call goes via a plt call stub, the opd information gets
2736 copied to the plt, and the linker patches the nop.
2737
2738 . x:
2739 . bl .foo_stub
2740 . ld 2,40(1)
2741 .
2742 .
2743 . .foo_stub:
71a39c98
AM
2744 . std 2,40(1) # in practice, the call stub
2745 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2746 . addi 11,11,Lfoo@toc@l # this is the general idea
2747 . ld 12,0(11)
2748 . ld 2,8(11)
2749 . mtctr 12
2750 . ld 11,16(11)
e86ce104
AM
2751 . bctr
2752 .
2753 . .section .plt
2754 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2755
2756 The "reloc ()" notation is supposed to indicate that the linker emits
2757 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2758 copying.
2759
2760 What are the difficulties here? Well, firstly, the relocations
2761 examined by the linker in check_relocs are against the function code
2762 sym .foo, while the dynamic relocation in the plt is emitted against
2763 the function descriptor symbol, foo. Somewhere along the line, we need
2764 to carefully copy dynamic link information from one symbol to the other.
2765 Secondly, the generic part of the elf linker will make .foo a dynamic
2766 symbol as is normal for most other backends. We need foo dynamic
2767 instead, at least for an application final link. However, when
2768 creating a shared library containing foo, we need to have both symbols
2769 dynamic so that references to .foo are satisfied during the early
2770 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2771 definition from some other object, eg. a static library.
2772
2773 Update: As of August 2004, we support a new convention. Function
2774 calls may use the function descriptor symbol, ie. "bl foo". This
2775 behaves exactly as "bl .foo". */
65f38f15 2776
7c8bbca5
AM
2777/* Of those relocs that might be copied as dynamic relocs, this
2778 function selects those that must be copied when linking a shared
2779 library or PIE, even when the symbol is local. */
65f38f15 2780
1d483afe
AM
2781static int
2782must_be_dyn_reloc (struct bfd_link_info *info,
2783 enum elf_ppc64_reloc_type r_type)
2784{
2785 switch (r_type)
2786 {
2787 default:
7c8bbca5
AM
2788 /* Only relative relocs can be resolved when the object load
2789 address isn't fixed. DTPREL64 is excluded because the
2790 dynamic linker needs to differentiate global dynamic from
2791 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2792 return 1;
2793
2794 case R_PPC64_REL32:
2795 case R_PPC64_REL64:
2796 case R_PPC64_REL30:
1bdd8fac
AM
2797 case R_PPC64_TOC16:
2798 case R_PPC64_TOC16_DS:
2799 case R_PPC64_TOC16_LO:
2800 case R_PPC64_TOC16_HI:
2801 case R_PPC64_TOC16_HA:
2802 case R_PPC64_TOC16_LO_DS:
1d483afe
AM
2803 return 0;
2804
2805 case R_PPC64_TPREL16:
2806 case R_PPC64_TPREL16_LO:
2807 case R_PPC64_TPREL16_HI:
2808 case R_PPC64_TPREL16_HA:
2809 case R_PPC64_TPREL16_DS:
2810 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2811 case R_PPC64_TPREL16_HIGH:
2812 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2813 case R_PPC64_TPREL16_HIGHER:
2814 case R_PPC64_TPREL16_HIGHERA:
2815 case R_PPC64_TPREL16_HIGHEST:
2816 case R_PPC64_TPREL16_HIGHESTA:
2817 case R_PPC64_TPREL64:
c213164a 2818 case R_PPC64_TPREL34:
7c8bbca5
AM
2819 /* These relocations are relative but in a shared library the
2820 linker doesn't know the thread pointer base. */
2821 return bfd_link_dll (info);
1d483afe
AM
2822 }
2823}
65f38f15 2824
f4656909 2825/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
e1c6cf61 2826 copying dynamic variables from a shared lib into an app's .dynbss
f4656909 2827 section, and instead use a dynamic relocation to point into the
e1c6cf61
AM
2828 shared lib. With code that gcc generates it is vital that this be
2829 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2830 actually the address of a function descriptor which resides in the
2831 .opd section. gcc uses the descriptor directly rather than going
2832 via the GOT as some other ABIs do, which means that initialized
2833 function pointers reference the descriptor. Thus, a function
2834 pointer initialized to the address of a function in a shared
2835 library will either require a .dynbss copy and a copy reloc, or a
2836 dynamic reloc. Using a .dynbss copy redefines the function
2837 descriptor symbol to point to the copy. This presents a problem as
2838 a PLT entry for that function is also initialized from the function
2839 descriptor symbol and the copy may not be initialized first. */
a23b6845 2840#define ELIMINATE_COPY_RELOCS 1
f4656909 2841
721956f4 2842/* Section name for stubs is the associated section name plus this
29942be8
NC
2843 string. */
2844#define STUB_SUFFIX ".stub"
721956f4
AM
2845
2846/* Linker stubs.
2847 ppc_stub_long_branch:
2848 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2849 destination, but a 24 bit branch in a stub section will reach.
2850 . b dest
2851
2852 ppc_stub_plt_branch:
2853 Similar to the above, but a 24 bit branch in the stub section won't
2854 reach its destination.
6df4c9c2
AM
2855 . addis %r12,%r2,xxx@toc@ha
2856 . ld %r12,xxx@toc@l(%r12)
71a39c98 2857 . mtctr %r12
721956f4
AM
2858 . bctr
2859
2860 ppc_stub_plt_call:
2c66dc6c
AM
2861 Used to call a function in a shared library. If it so happens that
2862 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2863 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 2864 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
2865 . addis %r11,%r2,xxx@toc@ha
2866 . ld %r12,xxx+0@toc@l(%r11)
2867 . mtctr %r12
2868 . ld %r2,xxx+8@toc@l(%r11)
2869 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2870 . bctr
ad8e1ba5
AM
2871
2872 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2873 code to adjust the value and save r2 to support multiple toc sections.
2874 A ppc_stub_long_branch with an r2 offset looks like:
2875 . std %r2,40(%r1)
2876 . addis %r2,%r2,off@ha
2877 . addi %r2,%r2,off@l
2878 . b dest
2879
2880 A ppc_stub_plt_branch with an r2 offset looks like:
2881 . std %r2,40(%r1)
6df4c9c2
AM
2882 . addis %r12,%r2,xxx@toc@ha
2883 . ld %r12,xxx@toc@l(%r12)
ad8e1ba5
AM
2884 . addis %r2,%r2,off@ha
2885 . addi %r2,%r2,off@l
71a39c98 2886 . mtctr %r12
ad8e1ba5 2887 . bctr
ac2df442 2888
05d0e962
AM
2889 All of the above stubs are shown as their ELFv1 variants. ELFv2
2890 variants exist too, simpler for plt calls since a new toc pointer
2891 and static chain are not loaded by the stub. In addition, ELFv2
2892 has some more complex stubs to handle calls marked with NOTOC
2893 relocs from functions where r2 is not a valid toc pointer. These
2894 come in two flavours, the ones shown below, and _both variants that
2895 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2896 one call is from a function where r2 is used as the toc pointer but
2897 needs a toc adjusting stub for small-model multi-toc, and another
2898 call is from a function where r2 is not valid.
2899 ppc_stub_long_branch_notoc:
2900 . mflr %r12
2901 . bcl 20,31,1f
2902 . 1:
2903 . mflr %r11
2904 . mtlr %r12
2cdcc330
AM
2905 . addis %r12,%r11,dest-1b@ha
2906 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2907 . b dest
2908
2909 ppc_stub_plt_branch_notoc:
2910 . mflr %r12
2911 . bcl 20,31,1f
2912 . 1:
2913 . mflr %r11
2914 . mtlr %r12
2915 . lis %r12,xxx-1b@highest
f891966f 2916 . ori %r12,%r12,xxx-1b@higher
05d0e962 2917 . sldi %r12,%r12,32
f891966f 2918 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2919 . ori %r12,%r12,xxx-1b@l
2920 . add %r12,%r11,%r12
2921 . mtctr %r12
2922 . bctr
2923
2924 ppc_stub_plt_call_notoc:
2925 . mflr %r12
2926 . bcl 20,31,1f
2927 . 1:
2928 . mflr %r11
2929 . mtlr %r12
2930 . lis %r12,xxx-1b@highest
f891966f 2931 . ori %r12,%r12,xxx-1b@higher
05d0e962 2932 . sldi %r12,%r12,32
f891966f 2933 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2934 . ori %r12,%r12,xxx-1b@l
2935 . ldx %r12,%r11,%r12
2936 . mtctr %r12
2937 . bctr
2938
7c1f4227 2939 There are also ELFv1 power10 variants of these stubs.
04bdff6a 2940 ppc_stub_long_branch_notoc:
d4b87b1e 2941 . pla %r12,dest@pcrel
04bdff6a
AM
2942 . b dest
2943 ppc_stub_plt_branch_notoc:
2944 . lis %r11,(dest-1f)@highesta34
2945 . ori %r11,%r11,(dest-1f)@highera34
2946 . sldi %r11,%r11,34
d4b87b1e 2947 . 1: pla %r12,dest@pcrel
04bdff6a
AM
2948 . add %r12,%r11,%r12
2949 . mtctr %r12
2950 . bctr
2951 ppc_stub_plt_call_notoc:
2952 . lis %r11,(xxx-1f)@highesta34
2953 . ori %r11,%r11,(xxx-1f)@highera34
2954 . sldi %r11,%r11,34
d4b87b1e 2955 . 1: pla %r12,xxx@pcrel
04bdff6a
AM
2956 . ldx %r12,%r11,%r12
2957 . mtctr %r12
2958 . bctr
2959
05d0e962
AM
2960 In cases where the high instructions would add zero, they are
2961 omitted and following instructions modified in some cases.
7c1f4227 2962 For example, a power10 ppc_stub_plt_call_notoc might simplify down
04bdff6a
AM
2963 to
2964 . pld %r12,xxx@pcrel
2965 . mtctr %r12
2966 . bctr
05d0e962
AM
2967
2968 For a given stub group (a set of sections all using the same toc
2969 pointer value) there will be just one stub type used for any
2970 particular function symbol. For example, if printf is called from
2971 code with the tocsave optimization (ie. r2 saved in function
2972 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2973 and from other code without the tocsave optimization requiring a
2974 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2975 type will be created. Calls with the tocsave optimization will
2976 enter this stub after the instruction saving r2. A similar
2977 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2978 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2979 to call an external function like printf. If other calls to printf
2980 require a ppc_stub_plt_call linkage stub then a single
2981 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2982 call. If other calls to printf require a ppc_stub_plt_call_r2save
2983 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2984 be created and calls not requiring r2 to be saved will enter the
2985 stub after the r2 save instruction. There is an analogous
2986 hierarchy of long branch and plt branch stubs for local call
2987 linkage. */
721956f4 2988
2cdcc330
AM
2989enum ppc_stub_type
2990{
721956f4
AM
2991 ppc_stub_none,
2992 ppc_stub_long_branch,
ad8e1ba5 2993 ppc_stub_long_branch_r2off,
05d0e962
AM
2994 ppc_stub_long_branch_notoc,
2995 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 2996 ppc_stub_plt_branch,
ad8e1ba5 2997 ppc_stub_plt_branch_r2off,
05d0e962
AM
2998 ppc_stub_plt_branch_notoc,
2999 ppc_stub_plt_branch_both,
794e51c0 3000 ppc_stub_plt_call,
7341d5e2 3001 ppc_stub_plt_call_r2save,
05d0e962
AM
3002 ppc_stub_plt_call_notoc,
3003 ppc_stub_plt_call_both,
a4b6fadd
AM
3004 ppc_stub_global_entry,
3005 ppc_stub_save_res
721956f4
AM
3006};
3007
6f20ed8a
AM
3008/* Information on stub grouping. */
3009struct map_stub
3010{
3011 /* The stub section. */
3012 asection *stub_sec;
3013 /* This is the section to which stubs in the group will be attached. */
3014 asection *link_sec;
a4b6fadd
AM
3015 /* Next group. */
3016 struct map_stub *next;
3017 /* Whether to emit a copy of register save/restore functions in this
3018 group. */
3019 int needs_save_res;
df136d64
AM
3020 /* Current offset within stubs after the insn restoring lr in a
3021 _notoc or _both stub using bcl for pc-relative addressing, or
3022 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3023 unsigned int lr_restore;
3024 /* Accumulated size of EH info emitted to describe return address
3025 if stubs modify lr. Does not include 17 byte FDE header. */
3026 unsigned int eh_size;
3027 /* Offset in glink_eh_frame to the start of EH info for this group. */
3028 unsigned int eh_base;
6f20ed8a
AM
3029};
3030
2cdcc330
AM
3031struct ppc_stub_hash_entry
3032{
721956f4
AM
3033 /* Base hash table entry structure. */
3034 struct bfd_hash_entry root;
3035
ad8e1ba5
AM
3036 enum ppc_stub_type stub_type;
3037
6f20ed8a
AM
3038 /* Group information. */
3039 struct map_stub *group;
721956f4
AM
3040
3041 /* Offset within stub_sec of the beginning of this stub. */
3042 bfd_vma stub_offset;
3043
3044 /* Given the symbol's value and its section we can determine its final
3045 value when building the stubs (so the stub knows where to jump. */
3046 bfd_vma target_value;
3047 asection *target_section;
3048
721956f4
AM
3049 /* The symbol table entry, if any, that this was derived from. */
3050 struct ppc_link_hash_entry *h;
e054468f 3051 struct plt_entry *plt_ent;
721956f4 3052
2d7ad24e
AM
3053 /* Symbol type. */
3054 unsigned char symtype;
3055
6911b7dc
AM
3056 /* Symbol st_other. */
3057 unsigned char other;
721956f4
AM
3058};
3059
2cdcc330
AM
3060struct ppc_branch_hash_entry
3061{
721956f4
AM
3062 /* Base hash table entry structure. */
3063 struct bfd_hash_entry root;
3064
c456f082 3065 /* Offset within branch lookup table. */
721956f4
AM
3066 unsigned int offset;
3067
3068 /* Generation marker. */
3069 unsigned int iter;
3070};
65f38f15 3071
19e08130
AM
3072/* Used to track dynamic relocations for local symbols. */
3073struct ppc_dyn_relocs
3074{
3075 struct ppc_dyn_relocs *next;
3076
3077 /* The input section of the reloc. */
3078 asection *sec;
3079
3080 /* Total number of relocs copied for the input section. */
3081 unsigned int count : 31;
3082
3083 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3084 unsigned int ifunc : 1;
3085};
3086
65f38f15
AM
3087struct ppc_link_hash_entry
3088{
3089 struct elf_link_hash_entry elf;
3090
2cdcc330
AM
3091 union
3092 {
b3fac117
AM
3093 /* A pointer to the most recently used stub hash entry against this
3094 symbol. */
3095 struct ppc_stub_hash_entry *stub_cache;
3096
3097 /* A pointer to the next symbol starting with a '.' */
3098 struct ppc_link_hash_entry *next_dot_sym;
3099 } u;
721956f4 3100
721956f4 3101 /* Link between function code and descriptor symbols. */
34814b9f 3102 struct ppc_link_hash_entry *oh;
721956f4 3103
e86ce104
AM
3104 /* Flag function code and descriptor symbols. */
3105 unsigned int is_func:1;
3106 unsigned int is_func_descriptor:1;
908b32fc 3107 unsigned int fake:1;
411e1bfb 3108
c5614fa4
AM
3109 /* Whether global opd/toc sym has been adjusted or not.
3110 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3111 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3112 unsigned int adjust_done:1;
3113
a4b6fadd
AM
3114 /* Set if this is an out-of-line register save/restore function,
3115 with non-standard calling convention. */
3116 unsigned int save_res:1;
3117
8b5f1ed8
AM
3118 /* Set if a duplicate symbol with non-zero localentry is detected,
3119 even when the duplicate symbol does not provide a definition. */
3120 unsigned int non_zero_localentry:1;
3121
411e1bfb 3122 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3123 Bits are or'd into the mask as the corresponding relocs are
3124 encountered during check_relocs, with TLS_TLS being set when any
3125 of the other TLS bits are set. tls_optimize clears bits when
3126 optimizing to indicate the corresponding GOT entry type is not
3127 needed. If set, TLS_TLS is never cleared. tls_optimize may also
b00a0a86 3128 set TLS_GDIE when a GD reloc turns into an IE one.
37da22e5
AM
3129 These flags are also kept for local symbols. */
3130#define TLS_TLS 1 /* Any TLS reloc. */
3131#define TLS_GD 2 /* GD reloc. */
3132#define TLS_LD 4 /* LD reloc. */
3133#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3134#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3135#define TLS_MARK 32 /* __tls_get_addr call marked. */
b00a0a86 3136#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
46e9995a 3137#define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
f961d9dd 3138 unsigned char tls_mask;
37da22e5
AM
3139
3140 /* The above field is also used to mark function symbols. In which
3141 case TLS_TLS will be 0. */
3142#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3143#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3144#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3145};
3146
ed7007c1
AM
3147static inline struct ppc_link_hash_entry *
3148ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3149{
3150 return (struct ppc_link_hash_entry *) ent;
3151}
3152
30845f11
AM
3153static inline struct elf_link_hash_entry *
3154elf_hash_entry (struct ppc_link_hash_entry *ent)
3155{
3156 return (struct elf_link_hash_entry *) ent;
3157}
3158
65f38f15
AM
3159/* ppc64 ELF linker hash table. */
3160
3161struct ppc_link_hash_table
3162{
3163 struct elf_link_hash_table elf;
3164
721956f4
AM
3165 /* The stub hash table. */
3166 struct bfd_hash_table stub_hash_table;
3167
3168 /* Another hash table for plt_branch stubs. */
3169 struct bfd_hash_table branch_hash_table;
3170
3b421ab3
AM
3171 /* Hash table for function prologue tocsave. */
3172 htab_t tocsave_htab;
3173
e7d1c40c
AM
3174 /* Various options and other info passed from the linker. */
3175 struct ppc64_elf_params *params;
721956f4 3176
6f20ed8a
AM
3177 /* The size of sec_info below. */
3178 unsigned int sec_info_arr_size;
3179
3180 /* Per-section array of extra section info. Done this way rather
3181 than as part of ppc64_elf_section_data so we have the info for
3182 non-ppc64 sections. */
3183 struct
3184 {
3185 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3186 bfd_vma toc_off;
6f20ed8a
AM
3187
3188 union
3189 {
3190 /* The section group that this section belongs to. */
3191 struct map_stub *group;
3192 /* A temp section list pointer. */
3193 asection *list;
3194 } u;
3195 } *sec_info;
721956f4 3196
a4b6fadd
AM
3197 /* Linked list of groups. */
3198 struct map_stub *group;
3199
ad8e1ba5
AM
3200 /* Temp used when calculating TOC pointers. */
3201 bfd_vma toc_curr;
bf102f86
AM
3202 bfd *toc_bfd;
3203 asection *toc_first_sec;
ad8e1ba5 3204
b3fac117
AM
3205 /* Used when adding symbols. */
3206 struct ppc_link_hash_entry *dot_syms;
3207
33e44f2e 3208 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3209 asection *glink;
9e390558 3210 asection *global_entry;
82bd7b59 3211 asection *sfpr;
2d7ad24e
AM
3212 asection *pltlocal;
3213 asection *relpltlocal;
4ce794b7
AM
3214 asection *brlt;
3215 asection *relbrlt;
58d180e8 3216 asection *glink_eh_frame;
ec338859 3217
8387904d
AM
3218 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3219 struct ppc_link_hash_entry *tls_get_addr;
3220 struct ppc_link_hash_entry *tls_get_addr_fd;
9e7028aa
AM
3221 struct ppc_link_hash_entry *tga_desc;
3222 struct ppc_link_hash_entry *tga_desc_fd;
a804e476 3223 struct map_stub *tga_group;
411e1bfb 3224
927be08e
AM
3225 /* The size of reliplt used by got entry relocs. */
3226 bfd_size_type got_reli_size;
3227
9b5ecbd0 3228 /* Statistics. */
7341d5e2 3229 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 3230
ee75fd95
AM
3231 /* Number of stubs against global syms. */
3232 unsigned long stub_globals;
3233
ee67d69a
AM
3234 /* Set if we're linking code with function descriptors. */
3235 unsigned int opd_abi:1;
3236
4c52953f 3237 /* Support for multiple toc sections. */
33c0ec9d 3238 unsigned int do_multi_toc:1;
4c52953f 3239 unsigned int multi_toc_needed:1;
927be08e 3240 unsigned int second_toc_pass:1;
67f0cbdb 3241 unsigned int do_toc_opt:1;
4c52953f 3242
9a23f96e
AM
3243 /* Set if tls optimization is enabled. */
3244 unsigned int do_tls_opt:1;
3245
3e04d765
AM
3246 /* Set if inline plt calls should be converted to direct calls. */
3247 unsigned int can_convert_all_inline_plt:1;
3248
5d1634d7 3249 /* Set on error. */
99877b66 3250 unsigned int stub_error:1;
721956f4 3251
8c5b4e52
AM
3252 /* Whether func_desc_adjust needs to be run over symbols. */
3253 unsigned int need_func_desc_adj:1;
721956f4 3254
f378ab09
AM
3255 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3256 unsigned int has_plt_localentry0:1;
3257
5663e321
AM
3258 /* Whether calls are made via the PLT from NOTOC functions. */
3259 unsigned int notoc_plt:1;
3260
e10a07b3
AM
3261 /* Whether any code linked seems to be Power10. */
3262 unsigned int has_power10_relocs:1;
3263
721956f4
AM
3264 /* Incremented every time we size stubs. */
3265 unsigned int stub_iteration;
65f38f15
AM
3266};
3267
4c52953f
AM
3268/* Rename some of the generic section flags to better document how they
3269 are used here. */
b0dddeec
AM
3270
3271/* Nonzero if this section has TLS related relocations. */
3272#define has_tls_reloc sec_flg0
3273
9737e8af
AM
3274/* Nonzero if this section has a call to __tls_get_addr lacking marker
3275 relocations. */
3276#define nomark_tls_get_addr sec_flg1
b0dddeec
AM
3277
3278/* Nonzero if this section has any toc or got relocs. */
3279#define has_toc_reloc sec_flg2
3280
3281/* Nonzero if this section has a call to another section that uses
3282 the toc or got. */
d77c8a4b 3283#define makes_toc_func_call sec_flg3
b0dddeec
AM
3284
3285/* Recursion protection when determining above flag. */
d77c8a4b 3286#define call_check_in_progress sec_flg4
70cc837d 3287#define call_check_done sec_flg5
4c52953f 3288
65f38f15
AM
3289/* Get the ppc64 ELF linker hash table from a link_info structure. */
3290
3291#define ppc_hash_table(p) \
573eb292
AM
3292 ((is_elf_hash_table ((p)->hash) \
3293 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3294 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
65f38f15 3295
721956f4
AM
3296#define ppc_stub_hash_lookup(table, string, create, copy) \
3297 ((struct ppc_stub_hash_entry *) \
3298 bfd_hash_lookup ((table), (string), (create), (copy)))
3299
3300#define ppc_branch_hash_lookup(table, string, create, copy) \
3301 ((struct ppc_branch_hash_entry *) \
3302 bfd_hash_lookup ((table), (string), (create), (copy)))
3303
3304/* Create an entry in the stub hash table. */
3305
3306static struct bfd_hash_entry *
4ce794b7
AM
3307stub_hash_newfunc (struct bfd_hash_entry *entry,
3308 struct bfd_hash_table *table,
3309 const char *string)
721956f4
AM
3310{
3311 /* Allocate the structure if it has not already been allocated by a
3312 subclass. */
3313 if (entry == NULL)
3314 {
3315 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3316 if (entry == NULL)
3317 return entry;
3318 }
3319
3320 /* Call the allocation method of the superclass. */
3321 entry = bfd_hash_newfunc (entry, table, string);
3322 if (entry != NULL)
3323 {
3324 struct ppc_stub_hash_entry *eh;
3325
3326 /* Initialize the local fields. */
3327 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3328 eh->stub_type = ppc_stub_none;
6f20ed8a 3329 eh->group = NULL;
721956f4
AM
3330 eh->stub_offset = 0;
3331 eh->target_value = 0;
3332 eh->target_section = NULL;
721956f4 3333 eh->h = NULL;
6911b7dc 3334 eh->plt_ent = NULL;
6911b7dc 3335 eh->other = 0;
721956f4
AM
3336 }
3337
3338 return entry;
3339}
3340
3341/* Create an entry in the branch hash table. */
3342
3343static struct bfd_hash_entry *
4ce794b7
AM
3344branch_hash_newfunc (struct bfd_hash_entry *entry,
3345 struct bfd_hash_table *table,
3346 const char *string)
721956f4
AM
3347{
3348 /* Allocate the structure if it has not already been allocated by a
3349 subclass. */
3350 if (entry == NULL)
3351 {
3352 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3353 if (entry == NULL)
3354 return entry;
3355 }
3356
3357 /* Call the allocation method of the superclass. */
3358 entry = bfd_hash_newfunc (entry, table, string);
3359 if (entry != NULL)
3360 {
3361 struct ppc_branch_hash_entry *eh;
3362
3363 /* Initialize the local fields. */
3364 eh = (struct ppc_branch_hash_entry *) entry;
3365 eh->offset = 0;
3366 eh->iter = 0;
3367 }
3368
3369 return entry;
3370}
3371
65f38f15
AM
3372/* Create an entry in a ppc64 ELF linker hash table. */
3373
3374static struct bfd_hash_entry *
4ce794b7
AM
3375link_hash_newfunc (struct bfd_hash_entry *entry,
3376 struct bfd_hash_table *table,
3377 const char *string)
65f38f15
AM
3378{
3379 /* Allocate the structure if it has not already been allocated by a
3380 subclass. */
3381 if (entry == NULL)
3382 {
3383 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3384 if (entry == NULL)
3385 return entry;
3386 }
3387
3388 /* Call the allocation method of the superclass. */
3389 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3390 if (entry != NULL)
3391 {
3392 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3393
b3fac117 3394 memset (&eh->u.stub_cache, 0,
908b32fc 3395 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3396 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3397
3398 /* When making function calls, old ABI code references function entry
3399 points (dot symbols), while new ABI code references the function
3400 descriptor symbol. We need to make any combination of reference and
3401 definition work together, without breaking archive linking.
3402
3403 For a defined function "foo" and an undefined call to "bar":
3404 An old object defines "foo" and ".foo", references ".bar" (possibly
3405 "bar" too).
3406 A new object defines "foo" and references "bar".
3407
3408 A new object thus has no problem with its undefined symbols being
3409 satisfied by definitions in an old object. On the other hand, the
3410 old object won't have ".bar" satisfied by a new object.
3411
3412 Keep a list of newly added dot-symbols. */
3413
3414 if (string[0] == '.')
3415 {
3416 struct ppc_link_hash_table *htab;
3417
3418 htab = (struct ppc_link_hash_table *) table;
3419 eh->u.next_dot_sym = htab->dot_syms;
3420 htab->dot_syms = eh;
3421 }
65f38f15
AM
3422 }
3423
3424 return entry;
3425}
3426
2cdcc330
AM
3427struct tocsave_entry
3428{
3b421ab3
AM
3429 asection *sec;
3430 bfd_vma offset;
3431};
3432
3433static hashval_t
3434tocsave_htab_hash (const void *p)
3435{
3436 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3437 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3438}
3439
3440static int
3441tocsave_htab_eq (const void *p1, const void *p2)
3442{
3443 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3444 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3445 return e1->sec == e2->sec && e1->offset == e2->offset;
3446}
3447
68faa637
AM
3448/* Destroy a ppc64 ELF linker hash table. */
3449
3450static void
d495ab0d 3451ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3452{
d495ab0d 3453 struct ppc_link_hash_table *htab;
68faa637 3454
d495ab0d 3455 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3456 if (htab->tocsave_htab)
3457 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3458 bfd_hash_table_free (&htab->branch_hash_table);
3459 bfd_hash_table_free (&htab->stub_hash_table);
3460 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3461}
3462
65f38f15
AM
3463/* Create a ppc64 ELF linker hash table. */
3464
3465static struct bfd_link_hash_table *
4ce794b7 3466ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3467{
3468 struct ppc_link_hash_table *htab;
986f0783 3469 size_t amt = sizeof (struct ppc_link_hash_table);
65f38f15 3470
4ce794b7 3471 htab = bfd_zmalloc (amt);
65f38f15
AM
3472 if (htab == NULL)
3473 return NULL;
3474
66eb6687 3475 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3476 sizeof (struct ppc_link_hash_entry),
3477 PPC64_ELF_DATA))
65f38f15 3478 {
e2d34d7d 3479 free (htab);
65f38f15
AM
3480 return NULL;
3481 }
3482
721956f4 3483 /* Init the stub hash table too. */
66eb6687
AM
3484 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3485 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3486 {
d495ab0d 3487 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3488 return NULL;
3489 }
721956f4
AM
3490
3491 /* And the branch hash table. */
66eb6687
AM
3492 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3493 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3494 {
3495 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3496 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3497 return NULL;
3498 }
721956f4 3499
3b421ab3
AM
3500 htab->tocsave_htab = htab_try_create (1024,
3501 tocsave_htab_hash,
3502 tocsave_htab_eq,
3503 NULL);
3504 if (htab->tocsave_htab == NULL)
2915c55b 3505 {
d495ab0d 3506 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3507 return NULL;
3508 }
d495ab0d 3509 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3510
3254fd24
AM
3511 /* Initializing two fields of the union is just cosmetic. We really
3512 only care about glist, but when compiled on a 32-bit host the
3513 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3514 debugger inspection of these fields look nicer. */
a6aa5195
AM
3515 htab->elf.init_got_refcount.refcount = 0;
3516 htab->elf.init_got_refcount.glist = NULL;
3517 htab->elf.init_plt_refcount.refcount = 0;
3518 htab->elf.init_plt_refcount.glist = NULL;
3519 htab->elf.init_got_offset.offset = 0;
3520 htab->elf.init_got_offset.glist = NULL;
3521 htab->elf.init_plt_offset.offset = 0;
3522 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3523
65f38f15
AM
3524 return &htab->elf.root;
3525}
3526
bfeb4a28
AM
3527/* Create sections for linker generated code. */
3528
3529static bfd_boolean
3530create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3531{
3532 struct ppc_link_hash_table *htab;
3533 flagword flags;
3534
3535 htab = ppc_hash_table (info);
3536
bfeb4a28
AM
3537 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3538 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3539 if (htab->params->save_restore_funcs)
3540 {
3541 /* Create .sfpr for code to save and restore fp regs. */
3542 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3543 flags);
3544 if (htab->sfpr == NULL
fd361982 3545 || !bfd_set_section_alignment (htab->sfpr, 2))
b32547cd
AM
3546 return FALSE;
3547 }
3548
3549 if (bfd_link_relocatable (info))
3550 return TRUE;
bfeb4a28
AM
3551
3552 /* Create .glink for lazy dynamic linking support. */
3553 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3554 flags);
3555 if (htab->glink == NULL
fd361982 3556 || !bfd_set_section_alignment (htab->glink, 3))
bfeb4a28
AM
3557 return FALSE;
3558
9e390558
AM
3559 /* The part of .glink used by global entry stubs, separate so that
3560 it can be aligned appropriately without affecting htab->glink. */
3561 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3562 flags);
3563 if (htab->global_entry == NULL
fd361982 3564 || !bfd_set_section_alignment (htab->global_entry, 2))
9e390558
AM
3565 return FALSE;
3566
bfeb4a28
AM
3567 if (!info->no_ld_generated_unwind_info)
3568 {
3569 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3570 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3571 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3572 ".eh_frame",
3573 flags);
3574 if (htab->glink_eh_frame == NULL
fd361982 3575 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
bfeb4a28
AM
3576 return FALSE;
3577 }
3578
3579 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3580 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3581 if (htab->elf.iplt == NULL
fd361982 3582 || !bfd_set_section_alignment (htab->elf.iplt, 3))
bfeb4a28
AM
3583 return FALSE;
3584
3585 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3586 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3587 htab->elf.irelplt
3588 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3589 if (htab->elf.irelplt == NULL
fd361982 3590 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
bfeb4a28
AM
3591 return FALSE;
3592
3593 /* Create branch lookup table for plt_branch stubs. */
3594 flags = (SEC_ALLOC | SEC_LOAD
3595 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3596 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3597 flags);
3598 if (htab->brlt == NULL
fd361982 3599 || !bfd_set_section_alignment (htab->brlt, 3))
bfeb4a28
AM
3600 return FALSE;
3601
2d7ad24e
AM
3602 /* Local plt entries, put in .branch_lt but a separate section for
3603 convenience. */
3604 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3605 flags);
3606 if (htab->pltlocal == NULL
fd361982 3607 || !bfd_set_section_alignment (htab->pltlocal, 3))
2d7ad24e
AM
3608 return FALSE;
3609
0e1862bb 3610 if (!bfd_link_pic (info))
bfeb4a28
AM
3611 return TRUE;
3612
3613 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3614 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3615 htab->relbrlt
3616 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3617 if (htab->relbrlt == NULL
fd361982 3618 || !bfd_set_section_alignment (htab->relbrlt, 3))
bfeb4a28
AM
3619 return FALSE;
3620
2d7ad24e
AM
3621 htab->relpltlocal
3622 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3623 if (htab->relpltlocal == NULL
fd361982 3624 || !bfd_set_section_alignment (htab->relpltlocal, 3))
2d7ad24e
AM
3625 return FALSE;
3626
bfeb4a28
AM
3627 return TRUE;
3628}
3629
e717da7e
AM
3630/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3631
bfeb4a28 3632bfd_boolean
e7d1c40c
AM
3633ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3634 struct ppc64_elf_params *params)
e717da7e
AM
3635{
3636 struct ppc_link_hash_table *htab;
3637
e7d1c40c 3638 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3639
3640/* Always hook our dynamic sections into the first bfd, which is the
3641 linker created stub bfd. This ensures that the GOT header is at
3642 the start of the output TOC section. */
3643 htab = ppc_hash_table (info);
e7d1c40c
AM
3644 htab->elf.dynobj = params->stub_bfd;
3645 htab->params = params;
bfeb4a28 3646
bfeb4a28 3647 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3648}
3649
721956f4
AM
3650/* Build a name for an entry in the stub hash table. */
3651
3652static char *
4ce794b7
AM
3653ppc_stub_name (const asection *input_section,
3654 const asection *sym_sec,
3655 const struct ppc_link_hash_entry *h,
3656 const Elf_Internal_Rela *rel)
721956f4
AM
3657{
3658 char *stub_name;
bcaa2f82 3659 ssize_t len;
721956f4
AM
3660
3661 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3662 offsets from a sym as a branch target? In fact, we could
3663 probably assume the addend is always zero. */
3664 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3665
3666 if (h)
3667 {
3668 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3669 stub_name = bfd_malloc (len);
46de2a7c
AM
3670 if (stub_name == NULL)
3671 return stub_name;
3672
bcaa2f82
AM
3673 len = sprintf (stub_name, "%08x.%s+%x",
3674 input_section->id & 0xffffffff,
3675 h->elf.root.root.string,
3676 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3677 }
3678 else
3679 {
ad8e1ba5 3680 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3681 stub_name = bfd_malloc (len);
46de2a7c
AM
3682 if (stub_name == NULL)
3683 return stub_name;
3684
bcaa2f82
AM
3685 len = sprintf (stub_name, "%08x.%x:%x+%x",
3686 input_section->id & 0xffffffff,
3687 sym_sec->id & 0xffffffff,
3688 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3689 (int) rel->r_addend & 0xffffffff);
721956f4 3690 }
bcaa2f82 3691 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3692 stub_name[len - 2] = 0;
721956f4
AM
3693 return stub_name;
3694}
3695
e10a07b3
AM
3696/* If mixing power10 with non-power10 code and --power10-stubs is not
3697 specified (or is auto) then calls using @notoc relocations that
3698 need a stub will utilize power10 instructions in the stub, and
3699 calls without @notoc relocations will not use power10 instructions.
3700 The two classes of stubs are stored in separate stub_hash_table
3701 entries having the same key string. The two entries will always be
3702 adjacent on entry->root.next chain, even if hash table resizing
3703 occurs. This function selects the correct entry to use. */
3704
3705static struct ppc_stub_hash_entry *
3706select_alt_stub (struct ppc_stub_hash_entry *entry, bfd_boolean notoc)
3707{
3708 bfd_boolean have_notoc;
3709
3710 have_notoc = (entry->stub_type == ppc_stub_plt_call_notoc
3711 || entry->stub_type == ppc_stub_plt_branch_notoc
3712 || entry->stub_type == ppc_stub_long_branch_notoc);
3713
3714 if (have_notoc != notoc)
3715 {
3716 const char *stub_name = entry->root.string;
3717
3718 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3719 if (entry != NULL
3720 && entry->root.string != stub_name)
3721 entry = NULL;
3722 }
3723
3724 return entry;
3725}
3726
721956f4
AM
3727/* Look up an entry in the stub hash. Stub entries are cached because
3728 creating the stub name takes a bit of time. */
3729
3730static struct ppc_stub_hash_entry *
4ce794b7
AM
3731ppc_get_stub_entry (const asection *input_section,
3732 const asection *sym_sec,
039b3fef 3733 struct ppc_link_hash_entry *h,
4ce794b7
AM
3734 const Elf_Internal_Rela *rel,
3735 struct ppc_link_hash_table *htab)
721956f4
AM
3736{
3737 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3738 struct map_stub *group;
721956f4
AM
3739
3740 /* If this input section is part of a group of sections sharing one
3741 stub section, then use the id of the first section in the group.
3742 Stub names need to include a section id, as there may well be
3743 more than one stub used to reach say, printf, and we need to
3744 distinguish between them. */
6f20ed8a 3745 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3746 if (group == NULL)
3747 return NULL;
721956f4 3748
b3fac117
AM
3749 if (h != NULL && h->u.stub_cache != NULL
3750 && h->u.stub_cache->h == h
6f20ed8a 3751 && h->u.stub_cache->group == group)
721956f4 3752 {
b3fac117 3753 stub_entry = h->u.stub_cache;
721956f4
AM
3754 }
3755 else
3756 {
3757 char *stub_name;
3758
6f20ed8a 3759 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3760 if (stub_name == NULL)
3761 return NULL;
3762
3763 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3764 stub_name, FALSE, FALSE);
721956f4 3765 if (h != NULL)
b3fac117 3766 h->u.stub_cache = stub_entry;
721956f4
AM
3767
3768 free (stub_name);
3769 }
3770
e10a07b3
AM
3771 if (stub_entry != NULL && htab->params->power10_stubs == -1)
3772 {
3773 bfd_boolean notoc = ELF64_R_TYPE (rel->r_info) == R_PPC64_REL24_NOTOC;
3774
3775 stub_entry = select_alt_stub (stub_entry, notoc);
3776 }
3777
721956f4
AM
3778 return stub_entry;
3779}
3780
3781/* Add a new stub entry to the stub hash. Not all fields of the new
3782 stub entry are initialised. */
3783
3784static struct ppc_stub_hash_entry *
4ce794b7
AM
3785ppc_add_stub (const char *stub_name,
3786 asection *section,
25f53a85 3787 struct bfd_link_info *info)
721956f4 3788{
25f53a85 3789 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3790 struct map_stub *group;
721956f4
AM
3791 asection *link_sec;
3792 asection *stub_sec;
3793 struct ppc_stub_hash_entry *stub_entry;
3794
6f20ed8a
AM
3795 group = htab->sec_info[section->id].u.group;
3796 link_sec = group->link_sec;
3797 stub_sec = group->stub_sec;
721956f4
AM
3798 if (stub_sec == NULL)
3799 {
6f20ed8a
AM
3800 size_t namelen;
3801 bfd_size_type len;
3802 char *s_name;
721956f4 3803
6f20ed8a
AM
3804 namelen = strlen (link_sec->name);
3805 len = namelen + sizeof (STUB_SUFFIX);
3806 s_name = bfd_alloc (htab->params->stub_bfd, len);
3807 if (s_name == NULL)
3808 return NULL;
721956f4 3809
6f20ed8a
AM
3810 memcpy (s_name, link_sec->name, namelen);
3811 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3812 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3813 if (stub_sec == NULL)
3814 return NULL;
3815 group->stub_sec = stub_sec;
721956f4
AM
3816 }
3817
3818 /* Enter this entry into the linker stub hash table. */
3819 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3820 TRUE, FALSE);
721956f4
AM
3821 if (stub_entry == NULL)
3822 {
695344c0 3823 /* xgettext:c-format */
cf97bcb0
AM
3824 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3825 section->owner, stub_name);
721956f4
AM
3826 return NULL;
3827 }
3828
6f20ed8a 3829 stub_entry->group = group;
721956f4 3830 stub_entry->stub_offset = 0;
721956f4
AM
3831 return stub_entry;
3832}
3833
e717da7e
AM
3834/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3835 not already done. */
65f38f15 3836
b34976b6 3837static bfd_boolean
e717da7e 3838create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3839{
e717da7e
AM
3840 asection *got, *relgot;
3841 flagword flags;
3842 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3843
0c8d6e5c 3844 if (!is_ppc64_elf (abfd))
0ffa91dd 3845 return FALSE;
4dfe6ac6
NC
3846 if (htab == NULL)
3847 return FALSE;
0ffa91dd 3848
33e44f2e
AM
3849 if (!htab->elf.sgot
3850 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3851 return FALSE;
e717da7e
AM
3852
3853 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3854 | SEC_LINKER_CREATED);
3855
c456f082 3856 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3857 if (!got
fd361982 3858 || !bfd_set_section_alignment (got, 3))
e717da7e 3859 return FALSE;
65f38f15 3860
c456f082
AM
3861 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3862 flags | SEC_READONLY);
e717da7e 3863 if (!relgot
fd361982 3864 || !bfd_set_section_alignment (relgot, 3))
b34976b6 3865 return FALSE;
e717da7e
AM
3866
3867 ppc64_elf_tdata (abfd)->got = got;
3868 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 3869 return TRUE;
65f38f15 3870}
5bd4f169 3871
b31867b6
AM
3872/* Follow indirect and warning symbol links. */
3873
3874static inline struct bfd_link_hash_entry *
3875follow_link (struct bfd_link_hash_entry *h)
3876{
3877 while (h->type == bfd_link_hash_indirect
3878 || h->type == bfd_link_hash_warning)
3879 h = h->u.i.link;
3880 return h;
3881}
3882
3883static inline struct elf_link_hash_entry *
3884elf_follow_link (struct elf_link_hash_entry *h)
3885{
3886 return (struct elf_link_hash_entry *) follow_link (&h->root);
3887}
3888
3889static inline struct ppc_link_hash_entry *
3890ppc_follow_link (struct ppc_link_hash_entry *h)
3891{
ed7007c1 3892 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
b31867b6
AM
3893}
3894
40d16e0b
AM
3895/* Merge PLT info on FROM with that on TO. */
3896
3897static void
3898move_plt_plist (struct ppc_link_hash_entry *from,
3899 struct ppc_link_hash_entry *to)
3900{
3901 if (from->elf.plt.plist != NULL)
3902 {
3903 if (to->elf.plt.plist != NULL)
3904 {
3905 struct plt_entry **entp;
3906 struct plt_entry *ent;
3907
3908 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3909 {
3910 struct plt_entry *dent;
3911
3912 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3913 if (dent->addend == ent->addend)
3914 {
3915 dent->plt.refcount += ent->plt.refcount;
3916 *entp = ent->next;
3917 break;
3918 }
3919 if (dent == NULL)
3920 entp = &ent->next;
3921 }
3922 *entp = to->elf.plt.plist;
3923 }
3924
3925 to->elf.plt.plist = from->elf.plt.plist;
3926 from->elf.plt.plist = NULL;
3927 }
3928}
3929
65f38f15
AM
3930/* Copy the extra info we tack onto an elf_link_hash_entry. */
3931
3932static void
fcfa13d2
AM
3933ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3934 struct elf_link_hash_entry *dir,
3935 struct elf_link_hash_entry *ind)
65f38f15
AM
3936{
3937 struct ppc_link_hash_entry *edir, *eind;
3938
ed7007c1
AM
3939 edir = ppc_elf_hash_entry (dir);
3940 eind = ppc_elf_hash_entry (ind);
65f38f15 3941
c79d6685
AM
3942 edir->is_func |= eind->is_func;
3943 edir->is_func_descriptor |= eind->is_func_descriptor;
3944 edir->tls_mask |= eind->tls_mask;
3945 if (eind->oh != NULL)
3946 edir->oh = ppc_follow_link (eind->oh);
3947
474436e6 3948 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
3949 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3950 edir->elf.ref_regular |= eind->elf.ref_regular;
3951 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 3952 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
3953 edir->elf.needs_plt |= eind->elf.needs_plt;
3954 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 3955
d311bc8b
AM
3956 /* If we were called to copy over info for a weak sym, don't copy
3957 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3958 in order to simplify readonly_dynrelocs and save a field in the
3959 symbol hash entry, but that means dyn_relocs can't be used in any
3960 tests about a specific symbol, or affect other symbol flags which
ab2477e1 3961 are then tested. */
d311bc8b 3962 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 3963 return;
d311bc8b 3964
411e1bfb 3965 /* Copy over any dynamic relocs we may have on the indirect sym. */
190eb1dd 3966 if (ind->dyn_relocs != NULL)
65f38f15 3967 {
190eb1dd 3968 if (dir->dyn_relocs != NULL)
bbd7ec4a 3969 {
6061a67d
AM
3970 struct elf_dyn_relocs **pp;
3971 struct elf_dyn_relocs *p;
bbd7ec4a 3972
fcfa13d2 3973 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a 3974 list. Merge any entries against the same section. */
190eb1dd 3975 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
bbd7ec4a 3976 {
6061a67d 3977 struct elf_dyn_relocs *q;
bbd7ec4a 3978
190eb1dd 3979 for (q = dir->dyn_relocs; q != NULL; q = q->next)
bbd7ec4a
AM
3980 if (q->sec == p->sec)
3981 {
3982 q->pc_count += p->pc_count;
3983 q->count += p->count;
3984 *pp = p->next;
3985 break;
3986 }
3987 if (q == NULL)
3988 pp = &p->next;
3989 }
190eb1dd 3990 *pp = dir->dyn_relocs;
bbd7ec4a
AM
3991 }
3992
190eb1dd
L
3993 dir->dyn_relocs = ind->dyn_relocs;
3994 ind->dyn_relocs = NULL;
65f38f15 3995 }
65f38f15 3996
81848ca0
AM
3997 /* Copy over got entries that we may have already seen to the
3998 symbol which just became indirect. */
411e1bfb
AM
3999 if (eind->elf.got.glist != NULL)
4000 {
4001 if (edir->elf.got.glist != NULL)
4002 {
4003 struct got_entry **entp;
4004 struct got_entry *ent;
4005
4006 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4007 {
4008 struct got_entry *dent;
4009
4010 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4011 if (dent->addend == ent->addend
e717da7e 4012 && dent->owner == ent->owner
411e1bfb
AM
4013 && dent->tls_type == ent->tls_type)
4014 {
4015 dent->got.refcount += ent->got.refcount;
4016 *entp = ent->next;
4017 break;
4018 }
4019 if (dent == NULL)
4020 entp = &ent->next;
4021 }
4022 *entp = edir->elf.got.glist;
4023 }
4024
4025 edir->elf.got.glist = eind->elf.got.glist;
4026 eind->elf.got.glist = NULL;
4027 }
4028
4029 /* And plt entries. */
40d16e0b 4030 move_plt_plist (eind, edir);
411e1bfb 4031
fcfa13d2 4032 if (eind->elf.dynindx != -1)
411e1bfb 4033 {
fcfa13d2
AM
4034 if (edir->elf.dynindx != -1)
4035 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4036 edir->elf.dynstr_index);
411e1bfb
AM
4037 edir->elf.dynindx = eind->elf.dynindx;
4038 edir->elf.dynstr_index = eind->elf.dynstr_index;
4039 eind->elf.dynindx = -1;
4040 eind->elf.dynstr_index = 0;
4041 }
411e1bfb
AM
4042}
4043
8387904d
AM
4044/* Find the function descriptor hash entry from the given function code
4045 hash entry FH. Link the entries via their OH fields. */
4046
4047static struct ppc_link_hash_entry *
b31867b6 4048lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4049{
4050 struct ppc_link_hash_entry *fdh = fh->oh;
4051
4052 if (fdh == NULL)
4053 {
4054 const char *fd_name = fh->elf.root.root.string + 1;
4055
ed7007c1
AM
4056 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4057 FALSE, FALSE, FALSE));
b31867b6
AM
4058 if (fdh == NULL)
4059 return fdh;
4060
4061 fdh->is_func_descriptor = 1;
4062 fdh->oh = fh;
4063 fh->is_func = 1;
4064 fh->oh = fdh;
8387904d
AM
4065 }
4066
8c5b4e52
AM
4067 fdh = ppc_follow_link (fdh);
4068 fdh->is_func_descriptor = 1;
4069 fdh->oh = fh;
4070 return fdh;
8387904d
AM
4071}
4072
8c5b4e52 4073/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4074
4075static struct ppc_link_hash_entry *
4076make_fdh (struct bfd_link_info *info,
908b32fc 4077 struct ppc_link_hash_entry *fh)
bb700d78 4078{
8c5b4e52
AM
4079 bfd *abfd = fh->elf.root.u.undef.abfd;
4080 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4081 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4082 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4083 ? BSF_WEAK
4084 : BSF_GLOBAL);
4085
4086 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4087 fh->elf.root.root.string + 1,
4088 flags, bfd_und_section_ptr, 0,
4089 NULL, FALSE, FALSE, &bh))
bb700d78
AM
4090 return NULL;
4091
4092 fdh = (struct ppc_link_hash_entry *) bh;
4093 fdh->elf.non_elf = 0;
908b32fc
AM
4094 fdh->fake = 1;
4095 fdh->is_func_descriptor = 1;
4096 fdh->oh = fh;
4097 fh->is_func = 1;
4098 fh->oh = fdh;
bb700d78
AM
4099 return fdh;
4100}
4101
8387904d
AM
4102/* Fix function descriptor symbols defined in .opd sections to be
4103 function type. */
555cd476
AM
4104
4105static bfd_boolean
c16153ae 4106ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4107 struct bfd_link_info *info,
555cd476 4108 Elf_Internal_Sym *isym,
6911b7dc 4109 const char **name,
555cd476
AM
4110 flagword *flags ATTRIBUTE_UNUSED,
4111 asection **sec,
b53dfeb2 4112 bfd_vma *value)
555cd476 4113{
b53dfeb2 4114 if (*sec != NULL
f1885d1e 4115 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4116 {
4117 asection *code_sec;
4118
4119 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4120 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4121 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4122
4123 /* If the symbol is a function defined in .opd, and the function
4124 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4125 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4126 && (*sec)->reloc_count != 0
4127 && opd_entry_value (*sec, *value, &code_sec, NULL,
4128 FALSE) != (bfd_vma) -1
4129 && discarded_section (code_sec))
4130 {
4131 *sec = bfd_und_section_ptr;
4132 isym->st_shndx = SHN_UNDEF;
4133 }
4134 }
dbd1e97e
AM
4135 else if (*sec != NULL
4136 && strcmp ((*sec)->name, ".toc") == 0
4137 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4138 {
4139 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4140 if (htab != NULL)
4141 htab->params->object_in_toc = 1;
4142 }
433817dd 4143
6911b7dc
AM
4144 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4145 {
4146 if (abiversion (ibfd) == 0)
4147 set_abiversion (ibfd, 2);
4148 else if (abiversion (ibfd) == 1)
4149 {
cf97bcb0
AM
4150 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4151 " for ABI version 1"), *name);
6911b7dc
AM
4152 bfd_set_error (bfd_error_bad_value);
4153 return FALSE;
4154 }
4155 }
4156
555cd476
AM
4157 return TRUE;
4158}
4159
6911b7dc
AM
4160/* Merge non-visibility st_other attributes: local entry point. */
4161
4162static void
4163ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5160d0f3 4164 unsigned int st_other,
6911b7dc
AM
4165 bfd_boolean definition,
4166 bfd_boolean dynamic)
4167{
f378ab09 4168 if (definition && (!dynamic || !h->def_regular))
5160d0f3 4169 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
6911b7dc
AM
4170 | ELF_ST_VISIBILITY (h->other));
4171}
4172
8c5b4e52
AM
4173/* Hook called on merging a symbol. We use this to clear "fake" since
4174 we now have a real symbol. */
4175
4176static bfd_boolean
4177ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4178 const Elf_Internal_Sym *isym,
8c5b4e52
AM
4179 asection **psec ATTRIBUTE_UNUSED,
4180 bfd_boolean newdef ATTRIBUTE_UNUSED,
4181 bfd_boolean olddef ATTRIBUTE_UNUSED,
4182 bfd *oldbfd ATTRIBUTE_UNUSED,
4183 const asection *oldsec ATTRIBUTE_UNUSED)
4184{
ed7007c1 4185 ppc_elf_hash_entry (h)->fake = 0;
8b5f1ed8 4186 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
ed7007c1 4187 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
8c5b4e52
AM
4188 return TRUE;
4189}
4190
8387904d 4191/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4192 inclusion of a new ABI object archive that defines "bar".
4193 NAME is a symbol defined in an archive. Return a symbol in the hash
4194 table that might be satisfied by the archive symbols. */
8387904d
AM
4195
4196static struct elf_link_hash_entry *
4197ppc64_elf_archive_symbol_lookup (bfd *abfd,
4198 struct bfd_link_info *info,
4199 const char *name)
4200{
4201 struct elf_link_hash_entry *h;
4202 char *dot_name;
4203 size_t len;
4204
4205 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4206 if (h != NULL
4207 /* Don't return this sym if it is a fake function descriptor
4208 created by add_symbol_adjust. */
ed7007c1 4209 && !ppc_elf_hash_entry (h)->fake)
8387904d
AM
4210 return h;
4211
4212 if (name[0] == '.')
4213 return h;
4214
4215 len = strlen (name);
4216 dot_name = bfd_alloc (abfd, len + 2);
4217 if (dot_name == NULL)
e99955cd 4218 return (struct elf_link_hash_entry *) -1;
8387904d
AM
4219 dot_name[0] = '.';
4220 memcpy (dot_name + 1, name, len + 1);
4221 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4222 bfd_release (abfd, dot_name);
a804e476
AM
4223 if (h != NULL)
4224 return h;
4225
4226 if (strcmp (name, "__tls_get_addr_opt") == 0)
4227 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
8387904d
AM
4228 return h;
4229}
4230
4231/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4232 new ABI object defines "bar". Well, at least, undefined dot symbols
4233 are made weak. This stops later archive searches from including an
4234 object if we already have a function descriptor definition. It also
35b0ce59
AM
4235 prevents the linker complaining about undefined symbols.
4236 We also check and correct mismatched symbol visibility here. The
4237 most restrictive visibility of the function descriptor and the
4238 function entry symbol is used. */
8387904d
AM
4239
4240static bfd_boolean
b3fac117 4241add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4242{
8387904d
AM
4243 struct ppc_link_hash_table *htab;
4244 struct ppc_link_hash_entry *fdh;
4245
b3fac117
AM
4246 if (eh->elf.root.type == bfd_link_hash_warning)
4247 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4248
8400d40d
AM
4249 if (eh->elf.root.type == bfd_link_hash_indirect)
4250 return TRUE;
4251
b3fac117
AM
4252 if (eh->elf.root.root.string[0] != '.')
4253 abort ();
8387904d 4254
b3fac117 4255 htab = ppc_hash_table (info);
4dfe6ac6
NC
4256 if (htab == NULL)
4257 return FALSE;
4258
b31867b6 4259 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4260 if (fdh == NULL
4261 && !bfd_link_relocatable (info)
4262 && (eh->elf.root.type == bfd_link_hash_undefined
4263 || eh->elf.root.type == bfd_link_hash_undefweak)
4264 && eh->elf.ref_regular)
4265 {
4266 /* Make an undefined function descriptor sym, in order to
4267 pull in an --as-needed shared lib. Archives are handled
4268 elsewhere. */
4269 fdh = make_fdh (info, eh);
4270 if (fdh == NULL)
4271 return FALSE;
bb700d78 4272 }
8c5b4e52
AM
4273
4274 if (fdh != NULL)
8387904d 4275 {
35b0ce59
AM
4276 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4277 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4278
4279 /* Make both descriptor and entry symbol have the most
4280 constraining visibility of either symbol. */
35b0ce59
AM
4281 if (entry_vis < descr_vis)
4282 fdh->elf.other += entry_vis - descr_vis;
4283 else if (entry_vis > descr_vis)
4284 eh->elf.other += descr_vis - entry_vis;
4285
8c5b4e52
AM
4286 /* Propagate reference flags from entry symbol to function
4287 descriptor symbol. */
bc4e12de 4288 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4289 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4290 fdh->elf.ref_regular |= eh->elf.ref_regular;
4291 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4292
4293 if (!fdh->elf.forced_local
4294 && fdh->elf.dynindx == -1
4295 && fdh->elf.versioned != versioned_hidden
4296 && (bfd_link_dll (info)
4297 || fdh->elf.def_dynamic
4298 || fdh->elf.ref_dynamic)
4299 && (eh->elf.ref_regular
4300 || eh->elf.def_regular))
4301 {
2cdcc330 4302 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
8c5b4e52 4303 return FALSE;
35b0ce59 4304 }
8387904d 4305 }
99877b66 4306
8387904d
AM
4307 return TRUE;
4308}
4309
f6c7c3e8
AM
4310/* Set up opd section info and abiversion for IBFD, and process list
4311 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4312
8387904d 4313static bfd_boolean
f6c7c3e8 4314ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4315{
99877b66 4316 struct ppc_link_hash_table *htab;
b3fac117 4317 struct ppc_link_hash_entry **p, *eh;
459609d6 4318 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4319
459609d6 4320 if (opd != NULL && opd->size != 0)
b3fac117 4321 {
b9399fcf
AM
4322 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4323 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4324
459609d6
AM
4325 if (abiversion (ibfd) == 0)
4326 set_abiversion (ibfd, 1);
8a2058b5 4327 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4328 {
695344c0 4329 /* xgettext:c-format */
cf97bcb0
AM
4330 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4331 ibfd, abiversion (ibfd));
459609d6
AM
4332 bfd_set_error (bfd_error_bad_value);
4333 return FALSE;
f6c7c3e8 4334 }
b9399fcf 4335 }
f6c7c3e8 4336
b9399fcf
AM
4337 if (is_ppc64_elf (info->output_bfd))
4338 {
4339 /* For input files without an explicit abiversion in e_flags
4340 we should have flagged any with symbol st_other bits set
4341 as ELFv1 and above flagged those with .opd as ELFv2.
4342 Set the output abiversion if not yet set, and for any input
4343 still ambiguous, take its abiversion from the output.
4344 Differences in ABI are reported later. */
4345 if (abiversion (info->output_bfd) == 0)
4346 set_abiversion (info->output_bfd, abiversion (ibfd));
4347 else if (abiversion (ibfd) == 0)
4348 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4349 }
4350
459609d6
AM
4351 htab = ppc_hash_table (info);
4352 if (htab == NULL)
b9399fcf 4353 return TRUE;
459609d6 4354
b9399fcf
AM
4355 if (opd != NULL && opd->size != 0
4356 && (ibfd->flags & DYNAMIC) == 0
4357 && (opd->flags & SEC_RELOC) != 0
4358 && opd->reloc_count != 0
4359 && !bfd_is_abs_section (opd->output_section)
4360 && info->gc_sections)
4361 {
4362 /* Garbage collection needs some extra help with .opd sections.
4363 We don't want to necessarily keep everything referenced by
4364 relocs in .opd, as that would keep all functions. Instead,
4365 if we reference an .opd symbol (a function descriptor), we
4366 want to keep the function code symbol's section. This is
4367 easy for global symbols, but for local syms we need to keep
4368 information about the associated function section. */
4369 bfd_size_type amt;
4370 asection **opd_sym_map;
4371 Elf_Internal_Shdr *symtab_hdr;
4372 Elf_Internal_Rela *relocs, *rel_end, *rel;
4373
4374 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4375 opd_sym_map = bfd_zalloc (ibfd, amt);
4376 if (opd_sym_map == NULL)
4377 return FALSE;
4378 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4379 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4380 info->keep_memory);
4381 if (relocs == NULL)
4382 return FALSE;
4383 symtab_hdr = &elf_symtab_hdr (ibfd);
4384 rel_end = relocs + opd->reloc_count - 1;
4385 for (rel = relocs; rel < rel_end; rel++)
4386 {
4387 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4388 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4389
4390 if (r_type == R_PPC64_ADDR64
4391 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4392 && r_symndx < symtab_hdr->sh_info)
4393 {
4394 Elf_Internal_Sym *isym;
4395 asection *s;
4396
f1dfbfdb
L
4397 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4398 r_symndx);
b9399fcf
AM
4399 if (isym == NULL)
4400 {
4401 if (elf_section_data (opd)->relocs != relocs)
4402 free (relocs);
4403 return FALSE;
4404 }
4405
4406 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4407 if (s != NULL && s != opd)
4408 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4409 }
4410 }
4411 if (elf_section_data (opd)->relocs != relocs)
4412 free (relocs);
4413 }
459609d6
AM
4414
4415 p = &htab->dot_syms;
4416 while ((eh = *p) != NULL)
4417 {
4418 *p = NULL;
4419 if (&eh->elf == htab->elf.hgot)
4420 ;
4421 else if (htab->elf.hgot == NULL
4422 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4423 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4424 else if (abiversion (ibfd) <= 1)
4425 {
4426 htab->need_func_desc_adj = 1;
4427 if (!add_symbol_adjust (eh, info))
4428 return FALSE;
4429 }
459609d6
AM
4430 p = &eh->u.next_dot_sym;
4431 }
b3fac117 4432 return TRUE;
8387904d
AM
4433}
4434
97fed1c9
JJ
4435/* Undo hash table changes when an --as-needed input file is determined
4436 not to be needed. */
4437
4438static bfd_boolean
e5034e59
AM
4439ppc64_elf_notice_as_needed (bfd *ibfd,
4440 struct bfd_link_info *info,
4441 enum notice_asneeded_action act)
97fed1c9 4442{
e5034e59
AM
4443 if (act == notice_not_needed)
4444 {
4445 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4446
e5034e59
AM
4447 if (htab == NULL)
4448 return FALSE;
4dfe6ac6 4449
e5034e59
AM
4450 htab->dot_syms = NULL;
4451 }
4452 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4453}
4454
aa374f67
AM
4455/* If --just-symbols against a final linked binary, then assume we need
4456 toc adjusting stubs when calling functions defined there. */
4457
4458static void
4459ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4460{
4461 if ((sec->flags & SEC_CODE) != 0
4462 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4463 && is_ppc64_elf (sec->owner))
4464 {
2c3f079f
AM
4465 if (abiversion (sec->owner) >= 2
4466 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4467 sec->has_toc_reloc = 1;
4468 }
4469 _bfd_elf_link_just_syms (sec, info);
4470}
4471
e054468f 4472static struct plt_entry **
4ce794b7
AM
4473update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4474 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4475{
4476 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4477 struct plt_entry **local_plt;
f961d9dd 4478 unsigned char *local_got_tls_masks;
411e1bfb
AM
4479
4480 if (local_got_ents == NULL)
4481 {
4482 bfd_size_type size = symtab_hdr->sh_info;
4483
e054468f
AM
4484 size *= (sizeof (*local_got_ents)
4485 + sizeof (*local_plt)
4486 + sizeof (*local_got_tls_masks));
4ce794b7 4487 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4488 if (local_got_ents == NULL)
e054468f 4489 return NULL;
411e1bfb
AM
4490 elf_local_got_ents (abfd) = local_got_ents;
4491 }
4492
37da22e5 4493 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4494 {
4495 struct got_entry *ent;
4496
4497 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4498 if (ent->addend == r_addend
4499 && ent->owner == abfd
4500 && ent->tls_type == tls_type)
411e1bfb
AM
4501 break;
4502 if (ent == NULL)
4503 {
986f0783 4504 size_t amt = sizeof (*ent);
4ce794b7 4505 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4506 if (ent == NULL)
4507 return FALSE;
4508 ent->next = local_got_ents[r_symndx];
4509 ent->addend = r_addend;
e717da7e 4510 ent->owner = abfd;
411e1bfb 4511 ent->tls_type = tls_type;
927be08e 4512 ent->is_indirect = FALSE;
411e1bfb
AM
4513 ent->got.refcount = 0;
4514 local_got_ents[r_symndx] = ent;
4515 }
4516 ent->got.refcount += 1;
4517 }
4518
e054468f 4519 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4520 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4521 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4522
4523 return local_plt + r_symndx;
65f38f15
AM
4524}
4525
411e1bfb 4526static bfd_boolean
e054468f 4527update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4528{
411e1bfb 4529 struct plt_entry *ent;
1e2f5b6e 4530
e054468f 4531 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4532 if (ent->addend == addend)
4533 break;
4534 if (ent == NULL)
1e2f5b6e 4535 {
986f0783 4536 size_t amt = sizeof (*ent);
4ce794b7 4537 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4538 if (ent == NULL)
4539 return FALSE;
e054468f 4540 ent->next = *plist;
411e1bfb
AM
4541 ent->addend = addend;
4542 ent->plt.refcount = 0;
e054468f 4543 *plist = ent;
1e2f5b6e 4544 }
411e1bfb 4545 ent->plt.refcount += 1;
b34976b6 4546 return TRUE;
1e2f5b6e
AM
4547}
4548
e054468f
AM
4549static bfd_boolean
4550is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4551{
4552 return (r_type == R_PPC64_REL24
05d0e962 4553 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
4554 || r_type == R_PPC64_REL14
4555 || r_type == R_PPC64_REL14_BRTAKEN
4556 || r_type == R_PPC64_REL14_BRNTAKEN
4557 || r_type == R_PPC64_ADDR24
4558 || r_type == R_PPC64_ADDR14
4559 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4560 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4561 || r_type == R_PPC64_PLTCALL
4562 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4563}
4564
4565/* Relocs on inline plt call sequence insns prior to the call. */
4566
4567static bfd_boolean
4568is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4569{
4570 return (r_type == R_PPC64_PLT16_HA
4571 || r_type == R_PPC64_PLT16_HI
4572 || r_type == R_PPC64_PLT16_LO
4573 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4574 || r_type == R_PPC64_PLT_PCREL34
4575 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4576 || r_type == R_PPC64_PLTSEQ
4577 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4578}
4579
5bd4f169 4580/* Look through the relocs for a section during the first phase, and
65f38f15 4581 calculate needed space in the global offset table, procedure
5d1634d7 4582 linkage table, and dynamic reloc sections. */
5bd4f169 4583
b34976b6 4584static bfd_boolean
4ce794b7
AM
4585ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4586 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4587{
65f38f15 4588 struct ppc_link_hash_table *htab;
5bd4f169 4589 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4590 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4591 const Elf_Internal_Rela *rel;
4592 const Elf_Internal_Rela *rel_end;
5bd4f169 4593 asection *sreloc;
3a71aa26 4594 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 4595 bfd_boolean is_opd;
5bd4f169 4596
0e1862bb 4597 if (bfd_link_relocatable (info))
b34976b6 4598 return TRUE;
5bd4f169 4599
0c8d6e5c 4600 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4601
65f38f15 4602 htab = ppc_hash_table (info);
4dfe6ac6
NC
4603 if (htab == NULL)
4604 return FALSE;
4605
3a71aa26
AM
4606 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4607 FALSE, FALSE, TRUE);
4608 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4609 FALSE, FALSE, TRUE);
0ffa91dd 4610 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4611 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4612 sreloc = NULL;
b9399fcf 4613 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4614 rel_end = relocs + sec->reloc_count;
4615 for (rel = relocs; rel < rel_end; rel++)
4616 {
4617 unsigned long r_symndx;
4618 struct elf_link_hash_entry *h;
04c9666a 4619 enum elf_ppc64_reloc_type r_type;
727fc41e 4620 int tls_type;
7c8fe5c4 4621 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4622 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
4623
4624 r_symndx = ELF64_R_SYM (rel->r_info);
4625 if (r_symndx < symtab_hdr->sh_info)
4626 h = NULL;
4627 else
973a3492
L
4628 {
4629 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4630 h = elf_follow_link (h);
1c865ab2
AM
4631
4632 if (h == htab->elf.hgot)
4633 sec->has_toc_reloc = 1;
973a3492 4634 }
5bd4f169 4635
5663e321
AM
4636 r_type = ELF64_R_TYPE (rel->r_info);
4637 switch (r_type)
4638 {
04bdff6a
AM
4639 case R_PPC64_D34:
4640 case R_PPC64_D34_LO:
4641 case R_PPC64_D34_HI30:
4642 case R_PPC64_D34_HA30:
4643 case R_PPC64_D28:
c213164a
AM
4644 case R_PPC64_TPREL34:
4645 case R_PPC64_DTPREL34:
04bdff6a 4646 case R_PPC64_PCREL34:
5663e321 4647 case R_PPC64_GOT_PCREL34:
87c69f97
AM
4648 case R_PPC64_GOT_TLSGD_PCREL34:
4649 case R_PPC64_GOT_TLSLD_PCREL34:
4650 case R_PPC64_GOT_TPREL_PCREL34:
4651 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
4652 case R_PPC64_PLT_PCREL34:
4653 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a 4654 case R_PPC64_PCREL28:
e10a07b3 4655 htab->has_power10_relocs = 1;
133a1f60
AM
4656 break;
4657 default:
5663e321
AM
4658 break;
4659 }
903b777d
AM
4660
4661 switch (r_type)
4662 {
4663 case R_PPC64_PLT16_HA:
4664 case R_PPC64_GOT_TLSLD16_HA:
4665 case R_PPC64_GOT_TLSGD16_HA:
4666 case R_PPC64_GOT_TPREL16_HA:
4667 case R_PPC64_GOT_DTPREL16_HA:
4668 case R_PPC64_GOT16_HA:
4669 case R_PPC64_TOC16_HA:
4670 case R_PPC64_PLT16_LO:
4671 case R_PPC64_PLT16_LO_DS:
4672 case R_PPC64_GOT_TLSLD16_LO:
4673 case R_PPC64_GOT_TLSGD16_LO:
4674 case R_PPC64_GOT_TPREL16_LO_DS:
4675 case R_PPC64_GOT_DTPREL16_LO_DS:
4676 case R_PPC64_GOT16_LO:
4677 case R_PPC64_GOT16_LO_DS:
4678 case R_PPC64_TOC16_LO:
4679 case R_PPC64_TOC16_LO_DS:
4680 case R_PPC64_GOT_PCREL34:
4681 ppc64_elf_tdata (abfd)->has_optrel = 1;
4682 ppc64_elf_section_data (sec)->has_optrel = 1;
4683 break;
4684 default:
4685 break;
4686 }
4687
f749f26e 4688 ifunc = NULL;
25f23106
AM
4689 if (h != NULL)
4690 {
4691 if (h->type == STT_GNU_IFUNC)
4692 {
4693 h->needs_plt = 1;
4694 ifunc = &h->plt.plist;
4695 }
4696 }
4697 else
4698 {
f1dfbfdb 4699 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
25f23106
AM
4700 abfd, r_symndx);
4701 if (isym == NULL)
4702 return FALSE;
4703
4704 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4705 {
4706 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4707 rel->r_addend,
37da22e5 4708 NON_GOT | PLT_IFUNC);
25f23106
AM
4709 if (ifunc == NULL)
4710 return FALSE;
4711 }
4712 }
727fc41e 4713
f749f26e 4714 tls_type = 0;
a33d1f77 4715 switch (r_type)
5bd4f169 4716 {
727fc41e
AM
4717 case R_PPC64_TLSGD:
4718 case R_PPC64_TLSLD:
4719 /* These special tls relocs tie a call to __tls_get_addr with
4720 its parameter symbol. */
37da22e5 4721 if (h != NULL)
ed7007c1 4722 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
37da22e5
AM
4723 else
4724 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4725 rel->r_addend,
37da22e5
AM
4726 NON_GOT | TLS_TLS | TLS_MARK))
4727 return FALSE;
4728 sec->has_tls_reloc = 1;
727fc41e
AM
4729 break;
4730
411e1bfb
AM
4731 case R_PPC64_GOT_TLSLD16:
4732 case R_PPC64_GOT_TLSLD16_LO:
4733 case R_PPC64_GOT_TLSLD16_HI:
4734 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 4735 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 4736 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4737 goto dogottls;
4738
4739 case R_PPC64_GOT_TLSGD16:
4740 case R_PPC64_GOT_TLSGD16_LO:
4741 case R_PPC64_GOT_TLSGD16_HI:
4742 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 4743 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 4744 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4745 goto dogottls;
4746
4747 case R_PPC64_GOT_TPREL16_DS:
4748 case R_PPC64_GOT_TPREL16_LO_DS:
4749 case R_PPC64_GOT_TPREL16_HI:
4750 case R_PPC64_GOT_TPREL16_HA:
87c69f97 4751 case R_PPC64_GOT_TPREL_PCREL34:
7c8bbca5 4752 if (bfd_link_dll (info))
411e1bfb
AM
4753 info->flags |= DF_STATIC_TLS;
4754 tls_type = TLS_TLS | TLS_TPREL;
4755 goto dogottls;
4756
4757 case R_PPC64_GOT_DTPREL16_DS:
4758 case R_PPC64_GOT_DTPREL16_LO_DS:
4759 case R_PPC64_GOT_DTPREL16_HI:
4760 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 4761 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
4762 tls_type = TLS_TLS | TLS_DTPREL;
4763 dogottls:
4764 sec->has_tls_reloc = 1;
066f4018 4765 goto dogot;
411e1bfb 4766
903b777d
AM
4767 case R_PPC64_GOT16:
4768 case R_PPC64_GOT16_LO:
4769 case R_PPC64_GOT16_HI:
65f38f15 4770 case R_PPC64_GOT16_HA:
903b777d 4771 case R_PPC64_GOT16_DS:
066f4018 4772 case R_PPC64_GOT16_LO_DS:
4a421c53 4773 case R_PPC64_GOT_PCREL34:
066f4018 4774 dogot:
65f38f15 4775 /* This symbol requires a global offset table entry. */
4c52953f 4776 sec->has_toc_reloc = 1;
33c0ec9d
AM
4777 if (r_type == R_PPC64_GOT_TLSLD16
4778 || r_type == R_PPC64_GOT_TLSGD16
4779 || r_type == R_PPC64_GOT_TPREL16_DS
4780 || r_type == R_PPC64_GOT_DTPREL16_DS
4781 || r_type == R_PPC64_GOT16
4782 || r_type == R_PPC64_GOT16_DS)
4783 {
4784 htab->do_multi_toc = 1;
d77c8a4b 4785 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4786 }
4787
e717da7e
AM
4788 if (ppc64_elf_tdata (abfd)->got == NULL
4789 && !create_got_section (abfd, info))
b34976b6 4790 return FALSE;
5bd4f169
AM
4791
4792 if (h != NULL)
4793 {
411e1bfb
AM
4794 struct ppc_link_hash_entry *eh;
4795 struct got_entry *ent;
65f38f15 4796
ed7007c1 4797 eh = ppc_elf_hash_entry (h);
411e1bfb 4798 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
133a1f60 4799 if (ent->addend == rel->r_addend
e717da7e 4800 && ent->owner == abfd
411e1bfb
AM
4801 && ent->tls_type == tls_type)
4802 break;
4803 if (ent == NULL)
5bd4f169 4804 {
986f0783 4805 size_t amt = sizeof (*ent);
4ce794b7 4806 ent = bfd_alloc (abfd, amt);
411e1bfb 4807 if (ent == NULL)
b34976b6 4808 return FALSE;
411e1bfb 4809 ent->next = eh->elf.got.glist;
133a1f60 4810 ent->addend = rel->r_addend;
e717da7e 4811 ent->owner = abfd;
411e1bfb 4812 ent->tls_type = tls_type;
927be08e 4813 ent->is_indirect = FALSE;
411e1bfb
AM
4814 ent->got.refcount = 0;
4815 eh->elf.got.glist = ent;
5bd4f169 4816 }
411e1bfb 4817 ent->got.refcount += 1;
e7b938ca 4818 eh->tls_mask |= tls_type;
5bd4f169 4819 }
411e1bfb
AM
4820 else
4821 /* This is a global offset table entry for a local symbol. */
4822 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4823 rel->r_addend, tls_type))
411e1bfb 4824 return FALSE;
5bd4f169
AM
4825 break;
4826
5bd4f169 4827 case R_PPC64_PLT16_HA:
65f38f15
AM
4828 case R_PPC64_PLT16_HI:
4829 case R_PPC64_PLT16_LO:
08be3224 4830 case R_PPC64_PLT16_LO_DS:
5663e321
AM
4831 case R_PPC64_PLT_PCREL34:
4832 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
4833 case R_PPC64_PLT32:
4834 case R_PPC64_PLT64:
cbf95972
AM
4835 /* This symbol requires a procedure linkage table entry. */
4836 plt_list = ifunc;
4837 if (h != NULL)
e054468f 4838 {
e054468f
AM
4839 h->needs_plt = 1;
4840 if (h->root.root.string[0] == '.'
4841 && h->root.root.string[1] != '\0')
ed7007c1
AM
4842 ppc_elf_hash_entry (h)->is_func = 1;
4843 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4844 plt_list = &h->plt.plist;
4845 }
4846 if (plt_list == NULL)
2d7ad24e 4847 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4848 rel->r_addend,
2d7ad24e 4849 NON_GOT | PLT_KEEP);
133a1f60 4850 if (!update_plt_info (abfd, plt_list, rel->r_addend))
cbf95972 4851 return FALSE;
5bd4f169
AM
4852 break;
4853
4854 /* The following relocations don't need to propagate the
4855 relocation if linking a shared object since they are
4856 section relative. */
4857 case R_PPC64_SECTOFF:
4858 case R_PPC64_SECTOFF_LO:
4859 case R_PPC64_SECTOFF_HI:
4860 case R_PPC64_SECTOFF_HA:
4861 case R_PPC64_SECTOFF_DS:
4862 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4863 case R_PPC64_DTPREL16:
4864 case R_PPC64_DTPREL16_LO:
4865 case R_PPC64_DTPREL16_HI:
4866 case R_PPC64_DTPREL16_HA:
4867 case R_PPC64_DTPREL16_DS:
4868 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
4869 case R_PPC64_DTPREL16_HIGH:
4870 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
4871 case R_PPC64_DTPREL16_HIGHER:
4872 case R_PPC64_DTPREL16_HIGHERA:
4873 case R_PPC64_DTPREL16_HIGHEST:
4874 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4875 break;
4876
ad8e1ba5 4877 /* Nor do these. */
25f23106
AM
4878 case R_PPC64_REL16:
4879 case R_PPC64_REL16_LO:
4880 case R_PPC64_REL16_HI:
4881 case R_PPC64_REL16_HA:
4a969973
AM
4882 case R_PPC64_REL16_HIGH:
4883 case R_PPC64_REL16_HIGHA:
4884 case R_PPC64_REL16_HIGHER:
4885 case R_PPC64_REL16_HIGHERA:
4886 case R_PPC64_REL16_HIGHEST:
4887 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
4888 case R_PPC64_REL16_HIGHER34:
4889 case R_PPC64_REL16_HIGHERA34:
4890 case R_PPC64_REL16_HIGHEST34:
4891 case R_PPC64_REL16_HIGHESTA34:
a680de9a 4892 case R_PPC64_REL16DX_HA:
25f23106
AM
4893 break;
4894
45965137
AM
4895 /* Not supported as a dynamic relocation. */
4896 case R_PPC64_ADDR64_LOCAL:
0e1862bb 4897 if (bfd_link_pic (info))
45965137
AM
4898 {
4899 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4900 ppc_howto_init ();
695344c0 4901 /* xgettext:c-format */
174d0a74 4902 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 4903 "in shared libraries and PIEs\n"),
45965137
AM
4904 abfd, sec, rel->r_offset,
4905 ppc64_elf_howto_table[r_type]->name);
4906 bfd_set_error (bfd_error_bad_value);
4907 return FALSE;
4908 }
4909 break;
4910
ad8e1ba5 4911 case R_PPC64_TOC16:
33c0ec9d
AM
4912 case R_PPC64_TOC16_DS:
4913 htab->do_multi_toc = 1;
d77c8a4b 4914 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 4915 /* Fall through. */
ad8e1ba5
AM
4916 case R_PPC64_TOC16_LO:
4917 case R_PPC64_TOC16_HI:
4918 case R_PPC64_TOC16_HA:
ad8e1ba5 4919 case R_PPC64_TOC16_LO_DS:
4c52953f 4920 sec->has_toc_reloc = 1;
ec73ddcd 4921 if (h != NULL && bfd_link_executable (info))
1bdd8fac
AM
4922 {
4923 /* We may need a copy reloc. */
4924 h->non_got_ref = 1;
4925 /* Strongly prefer a copy reloc over a dynamic reloc.
4926 glibc ld.so as of 2019-08 will error out if one of
4927 these relocations is emitted. */
4928 h->needs_copy = 1;
4929 goto dodyn;
4930 }
ad8e1ba5
AM
4931 break;
4932
006589cf
AM
4933 /* Marker reloc. */
4934 case R_PPC64_ENTRY:
4935 break;
4936
5bd4f169
AM
4937 /* This relocation describes the C++ object vtable hierarchy.
4938 Reconstruct it for later use during GC. */
4939 case R_PPC64_GNU_VTINHERIT:
c152c796 4940 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4941 return FALSE;
5bd4f169
AM
4942 break;
4943
4944 /* This relocation describes which C++ vtable entries are actually
4945 used. Record for later use during GC. */
4946 case R_PPC64_GNU_VTENTRY:
a0ea3a14 4947 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4948 return FALSE;
5bd4f169
AM
4949 break;
4950
721956f4
AM
4951 case R_PPC64_REL14:
4952 case R_PPC64_REL14_BRTAKEN:
4953 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4954 {
4955 asection *dest = NULL;
4956
4957 /* Heuristic: If jumping outside our section, chances are
4958 we are going to need a stub. */
4959 if (h != NULL)
4960 {
4961 /* If the sym is weak it may be overridden later, so
4962 don't assume we know where a weak sym lives. */
4963 if (h->root.type == bfd_link_hash_defined)
4964 dest = h->root.u.def.section;
4965 }
4966 else
87d72d41
AM
4967 {
4968 Elf_Internal_Sym *isym;
4969
f1dfbfdb 4970 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
4971 abfd, r_symndx);
4972 if (isym == NULL)
4973 return FALSE;
4974
4975 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4976 }
4977
220c76dd 4978 if (dest != sec)
7c8fe5c4 4979 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4980 }
3e04d765
AM
4981 goto rel24;
4982
4983 case R_PPC64_PLTCALL:
5663e321 4984 case R_PPC64_PLTCALL_NOTOC:
3e04d765 4985 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
4986 /* Fall through. */
4987
5d1634d7 4988 case R_PPC64_REL24:
05d0e962 4989 case R_PPC64_REL24_NOTOC:
3e04d765 4990 rel24:
cbf95972
AM
4991 plt_list = ifunc;
4992 if (h != NULL)
5d1634d7 4993 {
e054468f
AM
4994 h->needs_plt = 1;
4995 if (h->root.root.string[0] == '.'
4996 && h->root.root.string[1] != '\0')
ed7007c1 4997 ppc_elf_hash_entry (h)->is_func = 1;
cbf95972 4998
3a71aa26 4999 if (h == tga || h == dottga)
cbf95972
AM
5000 {
5001 sec->has_tls_reloc = 1;
5002 if (rel != relocs
5003 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5004 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5005 /* We have a new-style __tls_get_addr call with
5006 a marker reloc. */
5007 ;
5008 else
5009 /* Mark this section as having an old-style call. */
9737e8af 5010 sec->nomark_tls_get_addr = 1;
cbf95972
AM
5011 }
5012 plt_list = &h->plt.plist;
411e1bfb 5013 }
cbf95972
AM
5014
5015 /* We may need a .plt entry if the function this reloc
5016 refers to is in a shared lib. */
5017 if (plt_list
133a1f60 5018 && !update_plt_info (abfd, plt_list, rel->r_addend))
cbf95972 5019 return FALSE;
411e1bfb
AM
5020 break;
5021
cbf95972
AM
5022 case R_PPC64_ADDR14:
5023 case R_PPC64_ADDR14_BRNTAKEN:
5024 case R_PPC64_ADDR14_BRTAKEN:
5025 case R_PPC64_ADDR24:
5026 goto dodyn;
5027
411e1bfb
AM
5028 case R_PPC64_TPREL64:
5029 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5030 if (bfd_link_dll (info))
411e1bfb
AM
5031 info->flags |= DF_STATIC_TLS;
5032 goto dotlstoc;
5033
5034 case R_PPC64_DTPMOD64:
5035 if (rel + 1 < rel_end
5036 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5037 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5038 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5039 else
951fd09b 5040 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5041 goto dotlstoc;
5042
5043 case R_PPC64_DTPREL64:
5044 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5045 if (rel != relocs
5046 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5047 && rel[-1].r_offset == rel->r_offset - 8)
5048 /* This is the second reloc of a dtpmod, dtprel pair.
5049 Don't mark with TLS_DTPREL. */
5050 goto dodyn;
5051
5052 dotlstoc:
5053 sec->has_tls_reloc = 1;
5054 if (h != NULL)
ed7007c1 5055 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
411e1bfb
AM
5056 else
5057 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5058 rel->r_addend, tls_type))
411e1bfb
AM
5059 return FALSE;
5060
7c8fe5c4
AM
5061 ppc64_sec = ppc64_elf_section_data (sec);
5062 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5063 {
3a71aa26
AM
5064 bfd_size_type amt;
5065
e7b938ca 5066 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5067 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5068 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5069 if (ppc64_sec->u.toc.symndx == NULL)
5070 return FALSE;
5071 amt = sec->size * sizeof (bfd_vma) / 8;
5072 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5073 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5074 return FALSE;
7c8fe5c4
AM
5075 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5076 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5077 }
5078 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26 5079 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
133a1f60 5080 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5081
5082 /* Mark the second slot of a GD or LD entry.
5083 -1 to indicate GD and -2 to indicate LD. */
5084 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5085 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5086 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5087 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5088 goto dodyn;
5089
411e1bfb
AM
5090 case R_PPC64_TPREL16_HI:
5091 case R_PPC64_TPREL16_HA:
f9c6b907
AM
5092 case R_PPC64_TPREL16_HIGH:
5093 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5094 case R_PPC64_TPREL16_HIGHER:
5095 case R_PPC64_TPREL16_HIGHERA:
5096 case R_PPC64_TPREL16_HIGHEST:
5097 case R_PPC64_TPREL16_HIGHESTA:
252dcdf4
AM
5098 sec->has_tls_reloc = 1;
5099 /* Fall through. */
c213164a 5100 case R_PPC64_TPREL34:
252dcdf4
AM
5101 case R_PPC64_TPREL16:
5102 case R_PPC64_TPREL16_DS:
5103 case R_PPC64_TPREL16_LO:
5104 case R_PPC64_TPREL16_LO_DS:
7c8bbca5
AM
5105 if (bfd_link_dll (info))
5106 info->flags |= DF_STATIC_TLS;
5107 goto dodyn;
5d1634d7 5108
e86ce104 5109 case R_PPC64_ADDR64:
b9399fcf 5110 if (is_opd
1e2f5b6e 5111 && rel + 1 < rel_end
4ce794b7 5112 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5113 {
8387904d 5114 if (h != NULL)
ed7007c1 5115 ppc_elf_hash_entry (h)->is_func = 1;
1e2f5b6e 5116 }
e86ce104
AM
5117 /* Fall through. */
5118
65f38f15
AM
5119 case R_PPC64_ADDR16:
5120 case R_PPC64_ADDR16_DS:
5121 case R_PPC64_ADDR16_HA:
5122 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5123 case R_PPC64_ADDR16_HIGH:
5124 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5125 case R_PPC64_ADDR16_HIGHER:
5126 case R_PPC64_ADDR16_HIGHERA:
5127 case R_PPC64_ADDR16_HIGHEST:
5128 case R_PPC64_ADDR16_HIGHESTA:
5129 case R_PPC64_ADDR16_LO:
5130 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
5131 case R_PPC64_D34:
5132 case R_PPC64_D34_LO:
5133 case R_PPC64_D34_HI30:
5134 case R_PPC64_D34_HA30:
5135 case R_PPC64_ADDR16_HIGHER34:
5136 case R_PPC64_ADDR16_HIGHERA34:
5137 case R_PPC64_ADDR16_HIGHEST34:
5138 case R_PPC64_ADDR16_HIGHESTA34:
5139 case R_PPC64_D28:
0e1862bb 5140 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5141 && rel->r_addend == 0)
5142 {
5143 /* We may need a .plt entry if this reloc refers to a
5144 function in a shared lib. */
5663e321 5145 if (!update_plt_info (abfd, &h->plt.plist, 0))
a345bc8d
AM
5146 return FALSE;
5147 h->pointer_equality_needed = 1;
5148 }
5149 /* Fall through. */
5150
5151 case R_PPC64_REL30:
5152 case R_PPC64_REL32:
5153 case R_PPC64_REL64:
65f38f15 5154 case R_PPC64_ADDR32:
65f38f15
AM
5155 case R_PPC64_UADDR16:
5156 case R_PPC64_UADDR32:
5157 case R_PPC64_UADDR64:
5bd4f169 5158 case R_PPC64_TOC:
ec73ddcd 5159 if (h != NULL && bfd_link_executable (info))
81848ca0 5160 /* We may need a copy reloc. */
f5385ebf 5161 h->non_got_ref = 1;
81848ca0 5162
41bd81ab 5163 /* Don't propagate .opd relocs. */
b9399fcf 5164 if (NO_OPD_RELOCS && is_opd)
e86ce104 5165 break;
e86ce104 5166
65f38f15
AM
5167 /* If we are creating a shared library, and this is a reloc
5168 against a global symbol, or a non PC relative reloc
5169 against a local symbol, then we need to copy the reloc
5170 into the shared library. However, if we are linking with
5171 -Bsymbolic, we do not need to copy a reloc against a
5172 global symbol which is defined in an object we are
5173 including in the link (i.e., DEF_REGULAR is set). At
5174 this point we have not seen all the input files, so it is
5175 possible that DEF_REGULAR is not set now but will be set
5176 later (it is never cleared). In case of a weak definition,
5177 DEF_REGULAR may be cleared later by a strong definition in
5178 a shared library. We account for that possibility below by
f4656909 5179 storing information in the dyn_relocs field of the hash
65f38f15
AM
5180 table entry. A similar situation occurs when creating
5181 shared libraries and symbol visibility changes render the
5182 symbol local.
5183
5184 If on the other hand, we are creating an executable, we
5185 may need to keep relocations for symbols satisfied by a
5186 dynamic library if we manage to avoid copy relocs for the
5187 symbol. */
411e1bfb 5188 dodyn:
ec73ddcd
AM
5189 if ((h != NULL
5190 && (h->root.type == bfd_link_hash_defweak
5191 || !h->def_regular))
5192 || (h != NULL
5193 && !bfd_link_executable (info)
5194 && !SYMBOLIC_BIND (info, h))
5195 || (bfd_link_pic (info)
5196 && must_be_dyn_reloc (info, r_type))
0e1862bb 5197 || (!bfd_link_pic (info)
25f23106 5198 && ifunc != NULL))
5bd4f169 5199 {
65f38f15
AM
5200 /* We must copy these reloc types into the output file.
5201 Create a reloc section in dynobj and make room for
5202 this reloc. */
5bd4f169
AM
5203 if (sreloc == NULL)
5204 {
83bac4b0
NC
5205 sreloc = _bfd_elf_make_dynamic_reloc_section
5206 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5207
5bd4f169 5208 if (sreloc == NULL)
83bac4b0 5209 return FALSE;
5bd4f169
AM
5210 }
5211
65f38f15
AM
5212 /* If this is a global symbol, we count the number of
5213 relocations we need for this symbol. */
5214 if (h != NULL)
5215 {
19e08130
AM
5216 struct elf_dyn_relocs *p;
5217 struct elf_dyn_relocs **head;
5218
190eb1dd 5219 head = &h->dyn_relocs;
19e08130
AM
5220 p = *head;
5221 if (p == NULL || p->sec != sec)
5222 {
5223 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5224 if (p == NULL)
5225 return FALSE;
5226 p->next = *head;
5227 *head = p;
5228 p->sec = sec;
5229 p->count = 0;
5230 p->pc_count = 0;
5231 }
5232 p->count += 1;
5233 if (!must_be_dyn_reloc (info, r_type))
5234 p->pc_count += 1;
65f38f15
AM
5235 }
5236 else
5237 {
ec338859
AM
5238 /* Track dynamic relocs needed for local syms too.
5239 We really need local syms available to do this
5240 easily. Oh well. */
19e08130
AM
5241 struct ppc_dyn_relocs *p;
5242 struct ppc_dyn_relocs **head;
5243 bfd_boolean is_ifunc;
ec338859 5244 asection *s;
6edfbbad 5245 void *vpp;
87d72d41 5246 Elf_Internal_Sym *isym;
6edfbbad 5247
f1dfbfdb 5248 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
5249 abfd, r_symndx);
5250 if (isym == NULL)
b34976b6 5251 return FALSE;
ec338859 5252
87d72d41
AM
5253 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5254 if (s == NULL)
5255 s = sec;
5256
6edfbbad 5257 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5258 head = (struct ppc_dyn_relocs **) vpp;
5259 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5260 p = *head;
5261 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5262 p = p->next;
5263 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5264 {
5265 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5266 if (p == NULL)
5267 return FALSE;
5268 p->next = *head;
5269 *head = p;
5270 p->sec = sec;
5271 p->ifunc = is_ifunc;
5272 p->count = 0;
5273 }
5274 p->count += 1;
ec338859 5275 }
65f38f15 5276 }
5bd4f169 5277 break;
65f38f15
AM
5278
5279 default:
96e0dda4 5280 break;
5bd4f169
AM
5281 }
5282 }
5283
b34976b6 5284 return TRUE;
5bd4f169
AM
5285}
5286
ee67d69a
AM
5287/* Merge backend specific data from an object file to the output
5288 object file when linking. */
5289
5290static bfd_boolean
50e03d47 5291ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5292{
50e03d47 5293 bfd *obfd = info->output_bfd;
ee67d69a
AM
5294 unsigned long iflags, oflags;
5295
5296 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5297 return TRUE;
5298
5299 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5300 return TRUE;
5301
50e03d47 5302 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
5303 return FALSE;
5304
5305 iflags = elf_elfheader (ibfd)->e_flags;
5306 oflags = elf_elfheader (obfd)->e_flags;
5307
f6c7c3e8 5308 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5309 {
4eca0228 5310 _bfd_error_handler
695344c0 5311 /* xgettext:c-format */
871b3ab2 5312 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
5313 bfd_set_error (bfd_error_bad_value);
5314 return FALSE;
5315 }
f6c7c3e8 5316 else if (iflags != oflags && iflags != 0)
ee67d69a 5317 {
4eca0228 5318 _bfd_error_handler
695344c0 5319 /* xgettext:c-format */
871b3ab2 5320 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5321 ibfd, iflags, oflags);
5322 bfd_set_error (bfd_error_bad_value);
5323 return FALSE;
5324 }
5325
4a91d0ba
AM
5326 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5327 return FALSE;
005d79fd 5328
ee67d69a 5329 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5330 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5331}
5332
5333static bfd_boolean
5334ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5335{
5336 /* Print normal ELF private data. */
5337 _bfd_elf_print_private_bfd_data (abfd, ptr);
5338
5339 if (elf_elfheader (abfd)->e_flags != 0)
5340 {
5341 FILE *file = ptr;
5342
ee67d69a
AM
5343 fprintf (file, _("private flags = 0x%lx:"),
5344 elf_elfheader (abfd)->e_flags);
5345
5346 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5347 fprintf (file, _(" [abiv%ld]"),
5348 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5349 fputc ('\n', file);
5350 }
5351
5352 return TRUE;
5353}
5354
8387904d 5355/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5356 of the code entry point, and its section, which must be in the same
5357 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5358
5359static bfd_vma
5360opd_entry_value (asection *opd_sec,
5361 bfd_vma offset,
5362 asection **code_sec,
aef36ac1
AM
5363 bfd_vma *code_off,
5364 bfd_boolean in_code_sec)
8387904d
AM
5365{
5366 bfd *opd_bfd = opd_sec->owner;
8860955f 5367 Elf_Internal_Rela *relocs;
8387904d 5368 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5369 bfd_vma val;
8387904d 5370
9f296da3
AM
5371 /* No relocs implies we are linking a --just-symbols object, or looking
5372 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5373 if (opd_sec->reloc_count == 0)
5374 {
729eabd5 5375 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5376
729eabd5
AM
5377 if (contents == NULL)
5378 {
5379 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5380 return (bfd_vma) -1;
5381 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5382 }
ee1e4ede 5383
dbb3fbbb 5384 /* PR 17512: file: 64b9dfbb. */
451dfd38 5385 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5386 return (bfd_vma) -1;
5387
729eabd5 5388 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5389 if (code_sec != NULL)
5390 {
5391 asection *sec, *likely = NULL;
ee1e4ede 5392
aef36ac1 5393 if (in_code_sec)
4b85d634 5394 {
aef36ac1
AM
5395 sec = *code_sec;
5396 if (sec->vma <= val
5397 && val < sec->vma + sec->size)
5398 likely = sec;
5399 else
5400 val = -1;
5401 }
5402 else
5403 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5404 if (sec->vma <= val
5405 && (sec->flags & SEC_LOAD) != 0
5406 && (sec->flags & SEC_ALLOC) != 0)
5407 likely = sec;
5408 if (likely != NULL)
5409 {
5410 *code_sec = likely;
5411 if (code_off != NULL)
5412 *code_off = val - likely->vma;
4b85d634
AM
5413 }
5414 }
aef36ac1 5415 return val;
4b85d634
AM
5416 }
5417
0c8d6e5c 5418 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5419
729eabd5 5420 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5421 if (relocs == NULL)
5422 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
5423 /* PR 17512: file: df8e1fd6. */
5424 if (relocs == NULL)
5425 return (bfd_vma) -1;
645ea6a9 5426
8387904d 5427 /* Go find the opd reloc at the sym address. */
8860955f 5428 lo = relocs;
8387904d 5429 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5430 val = (bfd_vma) -1;
8387904d
AM
5431 while (lo < hi)
5432 {
5433 look = lo + (hi - lo) / 2;
5434 if (look->r_offset < offset)
5435 lo = look + 1;
5436 else if (look->r_offset > offset)
5437 hi = look;
5438 else
5439 {
0ffa91dd
NC
5440 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5441
8387904d
AM
5442 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5443 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5444 {
5445 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5446 asection *sec = NULL;
8387904d 5447
b53dfeb2
AM
5448 if (symndx >= symtab_hdr->sh_info
5449 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5450 {
5451 struct elf_link_hash_entry **sym_hashes;
5452 struct elf_link_hash_entry *rh;
5453
5454 sym_hashes = elf_sym_hashes (opd_bfd);
5455 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5456 if (rh != NULL)
5457 {
5458 rh = elf_follow_link (rh);
bb854a36
AM
5459 if (rh->root.type != bfd_link_hash_defined
5460 && rh->root.type != bfd_link_hash_defweak)
5461 break;
5462 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5463 {
bb854a36
AM
5464 val = rh->root.u.def.value;
5465 sec = rh->root.u.def.section;
b53dfeb2
AM
5466 }
5467 }
5468 }
5469
5470 if (sec == NULL)
5471 {
5472 Elf_Internal_Sym *sym;
5473
5474 if (symndx < symtab_hdr->sh_info)
5475 {
5476 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5477 if (sym == NULL)
5478 {
5479 size_t symcnt = symtab_hdr->sh_info;
5480 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5481 symcnt, 0,
5482 NULL, NULL, NULL);
5483 if (sym == NULL)
5484 break;
5485 symtab_hdr->contents = (bfd_byte *) sym;
5486 }
5487 sym += symndx;
128205bb
AM
5488 }
5489 else
5490 {
b53dfeb2
AM
5491 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5492 1, symndx,
5493 NULL, NULL, NULL);
128205bb
AM
5494 if (sym == NULL)
5495 break;
128205bb 5496 }
b53dfeb2
AM
5497 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5498 if (sec == NULL)
5499 break;
5500 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5501 val = sym->st_value;
8387904d 5502 }
b53dfeb2 5503
8387904d
AM
5504 val += look->r_addend;
5505 if (code_off != NULL)
5506 *code_off = val;
5507 if (code_sec != NULL)
aef36ac1
AM
5508 {
5509 if (in_code_sec && *code_sec != sec)
5510 return -1;
5511 else
5512 *code_sec = sec;
5513 }
b53dfeb2 5514 if (sec->output_section != NULL)
8387904d 5515 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5516 }
5517 break;
5518 }
5519 }
645ea6a9 5520
645ea6a9 5521 return val;
8387904d
AM
5522}
5523
aef36ac1
AM
5524/* If the ELF symbol SYM might be a function in SEC, return the
5525 function size and set *CODE_OFF to the function's entry point,
5526 otherwise return zero. */
9f296da3 5527
aef36ac1
AM
5528static bfd_size_type
5529ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5530 bfd_vma *code_off)
9f296da3 5531{
aef36ac1
AM
5532 bfd_size_type size;
5533
5534 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5535 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5536 return 0;
5537
5538 size = 0;
5539 if (!(sym->flags & BSF_SYNTHETIC))
5540 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5541
5542 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5543 {
b07bca4e
AM
5544 struct _opd_sec_data *opd = get_opd_info (sym->section);
5545 bfd_vma symval = sym->value;
5546
5547 if (opd != NULL
5548 && opd->adjust != NULL
5549 && elf_section_data (sym->section)->relocs != NULL)
5550 {
5551 /* opd_entry_value will use cached relocs that have been
5552 adjusted, but with raw symbols. That means both local
5553 and global symbols need adjusting. */
5554 long adjust = opd->adjust[OPD_NDX (symval)];
5555 if (adjust == -1)
5556 return 0;
5557 symval += adjust;
5558 }
5559
5560 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
5561 &sec, code_off, TRUE) == (bfd_vma) -1)
5562 return 0;
5563 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5564 symbol. This size has nothing to do with the code size of the
5565 function, which is what we're supposed to return, but the
5566 code size isn't available without looking up the dot-sym.
5567 However, doing that would be a waste of time particularly
5568 since elf_find_function will look at the dot-sym anyway.
5569 Now, elf_find_function will keep the largest size of any
5570 function sym found at the code address of interest, so return
5571 1 here to avoid it incorrectly caching a larger function size
5572 for a small function. This does mean we return the wrong
5573 size for a new-ABI function of size 24, but all that does is
5574 disable caching for such functions. */
5575 if (size == 24)
5576 size = 1;
9f296da3 5577 }
aef36ac1
AM
5578 else
5579 {
5580 if (sym->section != sec)
5581 return 0;
5582 *code_off = sym->value;
5583 }
5584 if (size == 0)
5585 size = 1;
5586 return size;
9f296da3
AM
5587}
5588
f378ab09
AM
5589/* Return true if symbol is a strong function defined in an ELFv2
5590 object with st_other localentry bits of zero, ie. its local entry
5591 point coincides with its global entry point. */
5592
5593static bfd_boolean
5594is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5595{
5596 return (h != NULL
5597 && h->type == STT_FUNC
5598 && h->root.type == bfd_link_hash_defined
5599 && (STO_PPC64_LOCAL_MASK & h->other) == 0
ed7007c1 5600 && !ppc_elf_hash_entry (h)->non_zero_localentry
f378ab09
AM
5601 && is_ppc64_elf (h->root.u.def.section->owner)
5602 && abiversion (h->root.u.def.section->owner) >= 2);
5603}
5604
854b41e7
AM
5605/* Return true if symbol is defined in a regular object file. */
5606
5607static bfd_boolean
5608is_static_defined (struct elf_link_hash_entry *h)
5609{
5610 return ((h->root.type == bfd_link_hash_defined
5611 || h->root.type == bfd_link_hash_defweak)
5612 && h->root.u.def.section != NULL
5613 && h->root.u.def.section->output_section != NULL);
5614}
5615
b31867b6
AM
5616/* If FDH is a function descriptor symbol, return the associated code
5617 entry symbol if it is defined. Return NULL otherwise. */
5618
5619static struct ppc_link_hash_entry *
5620defined_code_entry (struct ppc_link_hash_entry *fdh)
5621{
5622 if (fdh->is_func_descriptor)
5623 {
5624 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5625 if (fh->elf.root.type == bfd_link_hash_defined
5626 || fh->elf.root.type == bfd_link_hash_defweak)
5627 return fh;
5628 }
5629 return NULL;
5630}
5631
5632/* If FH is a function code entry symbol, return the associated
5633 function descriptor symbol if it is defined. Return NULL otherwise. */
5634
5635static struct ppc_link_hash_entry *
5636defined_func_desc (struct ppc_link_hash_entry *fh)
5637{
5638 if (fh->oh != NULL
5639 && fh->oh->is_func_descriptor)
5640 {
5641 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5642 if (fdh->elf.root.type == bfd_link_hash_defined
5643 || fdh->elf.root.type == bfd_link_hash_defweak)
5644 return fdh;
5645 }
5646 return NULL;
5647}
5648
ed7007c1
AM
5649/* Given H is a symbol that satisfies is_static_defined, return the
5650 value in the output file. */
5651
5652static bfd_vma
5653defined_sym_val (struct elf_link_hash_entry *h)
5654{
5655 return (h->root.u.def.section->output_section->vma
5656 + h->root.u.def.section->output_offset
5657 + h->root.u.def.value);
5658}
5659
5660/* Return true if H matches __tls_get_addr or one of its variants. */
5661
5662static bfd_boolean
5663is_tls_get_addr (struct elf_link_hash_entry *h,
5664 struct ppc_link_hash_table *htab)
5665{
30845f11
AM
5666 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5667 || h == elf_hash_entry (htab->tga_desc_fd)
5668 || h == elf_hash_entry (htab->tls_get_addr)
5669 || h == elf_hash_entry (htab->tga_desc));
ed7007c1
AM
5670}
5671
8c5b4e52
AM
5672static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5673
5674/* Garbage collect sections, after first dealing with dot-symbols. */
5675
5676static bfd_boolean
5677ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5678{
5679 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5680
5681 if (htab != NULL && htab->need_func_desc_adj)
5682 {
5683 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5684 htab->need_func_desc_adj = 0;
5685 }
5686 return bfd_elf_gc_sections (abfd, info);
5687}
5688
74f0fb50
AM
5689/* Mark all our entry sym sections, both opd and code section. */
5690
5691static void
5692ppc64_elf_gc_keep (struct bfd_link_info *info)
5693{
5694 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5695 struct bfd_sym_chain *sym;
5696
4dfe6ac6
NC
5697 if (htab == NULL)
5698 return;
5699
74f0fb50
AM
5700 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5701 {
b31867b6 5702 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5703 asection *sec;
5704
ed7007c1
AM
5705 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5706 FALSE, FALSE, TRUE));
74f0fb50
AM
5707 if (eh == NULL)
5708 continue;
5709 if (eh->elf.root.type != bfd_link_hash_defined
5710 && eh->elf.root.type != bfd_link_hash_defweak)
5711 continue;
5712
b31867b6
AM
5713 fh = defined_code_entry (eh);
5714 if (fh != NULL)
74f0fb50 5715 {
b31867b6 5716 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5717 sec->flags |= SEC_KEEP;
5718 }
5719 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5720 && opd_entry_value (eh->elf.root.u.def.section,
5721 eh->elf.root.u.def.value,
aef36ac1 5722 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
5723 sec->flags |= SEC_KEEP;
5724
5725 sec = eh->elf.root.u.def.section;
5726 sec->flags |= SEC_KEEP;
5727 }
5728}
5729
64d03ab5
AM
5730/* Mark sections containing dynamically referenced symbols. When
5731 building shared libraries, we must assume that any visible symbol is
5732 referenced. */
5733
5734static bfd_boolean
5735ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5736{
5737 struct bfd_link_info *info = (struct bfd_link_info *) inf;
ed7007c1 5738 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
b31867b6 5739 struct ppc_link_hash_entry *fdh;
b407645f 5740 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5741
64d03ab5 5742 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5743 fdh = defined_func_desc (eh);
5744 if (fdh != NULL)
5745 eh = fdh;
64d03ab5
AM
5746
5747 if ((eh->elf.root.type == bfd_link_hash_defined
5748 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 5749 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5750 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5751 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5752 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5753 && (!bfd_link_executable (info)
e278ae05 5754 || info->gc_keep_exported
b407645f
AM
5755 || info->export_dynamic
5756 || (eh->elf.dynamic
5757 && d != NULL
2cdcc330
AM
5758 && (*d->match) (&d->head, NULL,
5759 eh->elf.root.root.string)))
e278ae05 5760 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5761 || !bfd_hide_sym_by_version (info->version_info,
5762 eh->elf.root.root.string)))))
64d03ab5
AM
5763 {
5764 asection *code_sec;
b31867b6 5765 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5766
5767 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5768
5769 /* Function descriptor syms cause the associated
5770 function code sym section to be marked. */
b31867b6
AM
5771 fh = defined_code_entry (eh);
5772 if (fh != NULL)
5773 {
5774 code_sec = fh->elf.root.u.def.section;
5775 code_sec->flags |= SEC_KEEP;
5776 }
64d03ab5
AM
5777 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5778 && opd_entry_value (eh->elf.root.u.def.section,
5779 eh->elf.root.u.def.value,
aef36ac1 5780 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
5781 code_sec->flags |= SEC_KEEP;
5782 }
5783
5784 return TRUE;
5785}
5786
5bd4f169
AM
5787/* Return the section that should be marked against GC for a given
5788 relocation. */
5789
5790static asection *
4ce794b7 5791ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5792 struct bfd_link_info *info,
4ce794b7
AM
5793 Elf_Internal_Rela *rel,
5794 struct elf_link_hash_entry *h,
5795 Elf_Internal_Sym *sym)
5bd4f169 5796{
ccfa59ea
AM
5797 asection *rsec;
5798
ccfa59ea
AM
5799 /* Syms return NULL if we're marking .opd, so we avoid marking all
5800 function sections, as all functions are referenced in .opd. */
5801 rsec = NULL;
5802 if (get_opd_info (sec) != NULL)
5803 return rsec;
1e2f5b6e 5804
5bd4f169
AM
5805 if (h != NULL)
5806 {
04c9666a 5807 enum elf_ppc64_reloc_type r_type;
b31867b6 5808 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5809
4ce794b7 5810 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5811 switch (r_type)
5bd4f169
AM
5812 {
5813 case R_PPC64_GNU_VTINHERIT:
5814 case R_PPC64_GNU_VTENTRY:
5815 break;
5816
5817 default:
5818 switch (h->root.type)
5819 {
5820 case bfd_link_hash_defined:
5821 case bfd_link_hash_defweak:
ed7007c1 5822 eh = ppc_elf_hash_entry (h);
b31867b6
AM
5823 fdh = defined_func_desc (eh);
5824 if (fdh != NULL)
8c5b4e52
AM
5825 {
5826 /* -mcall-aixdesc code references the dot-symbol on
5827 a call reloc. Mark the function descriptor too
5828 against garbage collection. */
5829 fdh->elf.mark = 1;
60d67dc8
AM
5830 if (fdh->elf.is_weakalias)
5831 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
5832 eh = fdh;
5833 }
1e2f5b6e
AM
5834
5835 /* Function descriptor syms cause the associated
5836 function code sym section to be marked. */
b31867b6
AM
5837 fh = defined_code_entry (eh);
5838 if (fh != NULL)
ccfa59ea
AM
5839 {
5840 /* They also mark their opd section. */
74f0fb50 5841 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5842
b31867b6 5843 rsec = fh->elf.root.u.def.section;
ccfa59ea 5844 }
8387904d
AM
5845 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5846 && opd_entry_value (eh->elf.root.u.def.section,
5847 eh->elf.root.u.def.value,
aef36ac1 5848 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 5849 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5850 else
1e2f5b6e
AM
5851 rsec = h->root.u.def.section;
5852 break;
5bd4f169
AM
5853
5854 case bfd_link_hash_common:
1e2f5b6e
AM
5855 rsec = h->root.u.c.p->section;
5856 break;
5bd4f169
AM
5857
5858 default:
fb34365b 5859 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5860 }
5861 }
5862 }
5863 else
5864 {
74f0fb50 5865 struct _opd_sec_data *opd;
1e2f5b6e
AM
5866
5867 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5868 opd = get_opd_info (rsec);
5869 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5870 {
74f0fb50 5871 rsec->gc_mark = 1;
ccfa59ea 5872
51aecdc5 5873 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 5874 }
5bd4f169
AM
5875 }
5876
1e2f5b6e 5877 return rsec;
5bd4f169
AM
5878}
5879
deb0e272
AM
5880/* The maximum size of .sfpr. */
5881#define SFPR_MAX (218*4)
5882
5883struct sfpr_def_parms
5884{
699733f6
AM
5885 const char name[12];
5886 unsigned char lo, hi;
2cdcc330
AM
5887 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5888 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
5889};
5890
a4b6fadd
AM
5891/* Auto-generate _save*, _rest* functions in .sfpr.
5892 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5893 instead. */
deb0e272 5894
4dfe6ac6 5895static bfd_boolean
a4b6fadd
AM
5896sfpr_define (struct bfd_link_info *info,
5897 const struct sfpr_def_parms *parm,
5898 asection *stub_sec)
deb0e272
AM
5899{
5900 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5901 unsigned int i;
5902 size_t len = strlen (parm->name);
5903 bfd_boolean writing = FALSE;
699733f6 5904 char sym[16];
deb0e272 5905
4dfe6ac6
NC
5906 if (htab == NULL)
5907 return FALSE;
5908
deb0e272
AM
5909 memcpy (sym, parm->name, len);
5910 sym[len + 2] = 0;
5911
5912 for (i = parm->lo; i <= parm->hi; i++)
5913 {
a4b6fadd 5914 struct ppc_link_hash_entry *h;
deb0e272
AM
5915
5916 sym[len + 0] = i / 10 + '0';
5917 sym[len + 1] = i % 10 + '0';
ed7007c1
AM
5918 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
5919 writing, TRUE, TRUE));
a4b6fadd 5920 if (stub_sec != NULL)
deb0e272 5921 {
a4b6fadd
AM
5922 if (h != NULL
5923 && h->elf.root.type == bfd_link_hash_defined
5924 && h->elf.root.u.def.section == htab->sfpr)
5925 {
5926 struct elf_link_hash_entry *s;
5927 char buf[32];
5928 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5929 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5930 if (s == NULL)
5931 return FALSE;
779f2ae7 5932 if (s->root.type == bfd_link_hash_new)
a4b6fadd
AM
5933 {
5934 s->root.type = bfd_link_hash_defined;
5935 s->root.u.def.section = stub_sec;
7dda8d3c 5936 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
5937 + h->elf.root.u.def.value);
5938 s->ref_regular = 1;
5939 s->def_regular = 1;
5940 s->ref_regular_nonweak = 1;
5941 s->forced_local = 1;
5942 s->non_elf = 0;
5943 s->root.linker_def = 1;
5944 }
5945 }
5946 continue;
5947 }
5948 if (h != NULL)
5949 {
5950 h->save_res = 1;
5951 if (!h->elf.def_regular)
deb0e272 5952 {
a4b6fadd
AM
5953 h->elf.root.type = bfd_link_hash_defined;
5954 h->elf.root.u.def.section = htab->sfpr;
5955 h->elf.root.u.def.value = htab->sfpr->size;
5956 h->elf.type = STT_FUNC;
5957 h->elf.def_regular = 1;
b32547cd 5958 h->elf.non_elf = 0;
a4b6fadd
AM
5959 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5960 writing = TRUE;
deb0e272 5961 if (htab->sfpr->contents == NULL)
a4b6fadd 5962 {
2cdcc330
AM
5963 htab->sfpr->contents
5964 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd
AM
5965 if (htab->sfpr->contents == NULL)
5966 return FALSE;
5967 }
deb0e272
AM
5968 }
5969 }
5970 if (writing)
5971 {
5972 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5973 if (i != parm->hi)
5974 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5975 else
5976 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5977 htab->sfpr->size = p - htab->sfpr->contents;
5978 }
5979 }
5980
5981 return TRUE;
5982}
5983
5984static bfd_byte *
5985savegpr0 (bfd *abfd, bfd_byte *p, int r)
5986{
5987 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5988 return p + 4;
5989}
5990
5991static bfd_byte *
5992savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5993{
5994 p = savegpr0 (abfd, p, r);
a078d95a 5995 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5996 p = p + 4;
5997 bfd_put_32 (abfd, BLR, p);
5998 return p + 4;
5999}
6000
6001static bfd_byte *
6002restgpr0 (bfd *abfd, bfd_byte *p, int r)
6003{
6004 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6005 return p + 4;
6006}
6007
6008static bfd_byte *
6009restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6010{
a078d95a 6011 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6012 p = p + 4;
6013 p = restgpr0 (abfd, p, r);
6014 bfd_put_32 (abfd, MTLR_R0, p);
6015 p = p + 4;
6016 if (r == 29)
6017 {
6018 p = restgpr0 (abfd, p, 30);
6019 p = restgpr0 (abfd, p, 31);
6020 }
6021 bfd_put_32 (abfd, BLR, p);
6022 return p + 4;
6023}
6024
6025static bfd_byte *
6026savegpr1 (bfd *abfd, bfd_byte *p, int r)
6027{
6028 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6029 return p + 4;
6030}
6031
6032static bfd_byte *
6033savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6034{
6035 p = savegpr1 (abfd, p, r);
6036 bfd_put_32 (abfd, BLR, p);
6037 return p + 4;
6038}
6039
6040static bfd_byte *
6041restgpr1 (bfd *abfd, bfd_byte *p, int r)
6042{
6043 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6044 return p + 4;
6045}
6046
6047static bfd_byte *
6048restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6049{
6050 p = restgpr1 (abfd, p, r);
6051 bfd_put_32 (abfd, BLR, p);
6052 return p + 4;
6053}
6054
6055static bfd_byte *
6056savefpr (bfd *abfd, bfd_byte *p, int r)
6057{
6058 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6059 return p + 4;
6060}
6061
6062static bfd_byte *
6063savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6064{
6065 p = savefpr (abfd, p, r);
a078d95a 6066 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6067 p = p + 4;
6068 bfd_put_32 (abfd, BLR, p);
6069 return p + 4;
6070}
6071
6072static bfd_byte *
6073restfpr (bfd *abfd, bfd_byte *p, int r)
6074{
6075 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6076 return p + 4;
6077}
6078
6079static bfd_byte *
6080restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6081{
a078d95a 6082 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6083 p = p + 4;
6084 p = restfpr (abfd, p, r);
6085 bfd_put_32 (abfd, MTLR_R0, p);
6086 p = p + 4;
6087 if (r == 29)
6088 {
6089 p = restfpr (abfd, p, 30);
6090 p = restfpr (abfd, p, 31);
6091 }
6092 bfd_put_32 (abfd, BLR, p);
6093 return p + 4;
6094}
6095
6096static bfd_byte *
6097savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6098{
6099 p = savefpr (abfd, p, r);
6100 bfd_put_32 (abfd, BLR, p);
6101 return p + 4;
6102}
6103
6104static bfd_byte *
6105restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6106{
6107 p = restfpr (abfd, p, r);
6108 bfd_put_32 (abfd, BLR, p);
6109 return p + 4;
6110}
6111
6112static bfd_byte *
6113savevr (bfd *abfd, bfd_byte *p, int r)
6114{
6115 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6116 p = p + 4;
6117 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6118 return p + 4;
6119}
6120
6121static bfd_byte *
6122savevr_tail (bfd *abfd, bfd_byte *p, int r)
6123{
6124 p = savevr (abfd, p, r);
6125 bfd_put_32 (abfd, BLR, p);
6126 return p + 4;
6127}
6128
6129static bfd_byte *
6130restvr (bfd *abfd, bfd_byte *p, int r)
6131{
6132 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6133 p = p + 4;
6134 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6135 return p + 4;
6136}
6137
6138static bfd_byte *
6139restvr_tail (bfd *abfd, bfd_byte *p, int r)
6140{
6141 p = restvr (abfd, p, r);
6142 bfd_put_32 (abfd, BLR, p);
6143 return p + 4;
6144}
6145
9e7028aa
AM
6146#define STDU_R1_0R1 0xf8210001
6147#define ADDI_R1_R1 0x38210000
6148
6149/* Emit prologue of wrapper preserving regs around a call to
6150 __tls_get_addr_opt. */
6151
6152static bfd_byte *
6153tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6154{
6155 unsigned int i;
6156
6157 bfd_put_32 (obfd, MFLR_R0, p);
6158 p += 4;
6159 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6160 p += 4;
6161
6162 if (htab->opd_abi)
6163 {
6164 for (i = 4; i < 12; i++)
6165 {
6166 bfd_put_32 (obfd,
6167 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6168 p += 4;
6169 }
6170 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6171 p += 4;
6172 }
6173 else
6174 {
6175 for (i = 4; i < 12; i++)
6176 {
6177 bfd_put_32 (obfd,
6178 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6179 p += 4;
6180 }
6181 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6182 p += 4;
6183 }
6184 return p;
6185}
6186
6187/* Emit epilogue of wrapper preserving regs around a call to
6188 __tls_get_addr_opt. */
6189
6190static bfd_byte *
6191tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6192{
6193 unsigned int i;
6194
6195 if (htab->opd_abi)
6196 {
6197 for (i = 4; i < 12; i++)
6198 {
6199 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6200 p += 4;
6201 }
6202 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6203 p += 4;
6204 }
6205 else
6206 {
6207 for (i = 4; i < 12; i++)
6208 {
6209 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6210 p += 4;
6211 }
6212 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6213 p += 4;
6214 }
6215 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6216 p += 4;
6217 bfd_put_32 (obfd, MTLR_R0, p);
6218 p += 4;
6219 bfd_put_32 (obfd, BLR, p);
6220 p += 4;
6221 return p;
6222}
6223
e86ce104
AM
6224/* Called via elf_link_hash_traverse to transfer dynamic linking
6225 information on function code symbol entries to their corresponding
6226 function descriptor symbol entries. */
deb0e272 6227
b34976b6 6228static bfd_boolean
4ce794b7 6229func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6230{
e86ce104 6231 struct bfd_link_info *info;
65f38f15 6232 struct ppc_link_hash_table *htab;
50bc7936
AM
6233 struct ppc_link_hash_entry *fh;
6234 struct ppc_link_hash_entry *fdh;
6235 bfd_boolean force_local;
5bd4f169 6236
ed7007c1 6237 fh = ppc_elf_hash_entry (h);
50bc7936 6238 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6239 return TRUE;
e86ce104 6240
8c5b4e52
AM
6241 if (!fh->is_func)
6242 return TRUE;
6243
6244 if (fh->elf.root.root.string[0] != '.'
6245 || fh->elf.root.root.string[1] == '\0')
6246 return TRUE;
6247
4ce794b7 6248 info = inf;
65f38f15 6249 htab = ppc_hash_table (info);
4dfe6ac6
NC
6250 if (htab == NULL)
6251 return FALSE;
5bd4f169 6252
8c5b4e52
AM
6253 /* Find the corresponding function descriptor symbol. */
6254 fdh = lookup_fdh (fh, htab);
6255
c09bdfe5
AM
6256 /* Resolve undefined references to dot-symbols as the value
6257 in the function descriptor, if we have one in a regular object.
6258 This is to satisfy cases like ".quad .foo". Calls to functions
6259 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6260 if ((fh->elf.root.type == bfd_link_hash_undefined
6261 || fh->elf.root.type == bfd_link_hash_undefweak)
6262 && (fdh->elf.root.type == bfd_link_hash_defined
6263 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6264 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6265 && opd_entry_value (fdh->elf.root.u.def.section,
6266 fdh->elf.root.u.def.value,
c09bdfe5 6267 &fh->elf.root.u.def.section,
aef36ac1 6268 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6269 {
b31867b6 6270 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6271 fh->elf.forced_local = 1;
b31867b6
AM
6272 fh->elf.def_regular = fdh->elf.def_regular;
6273 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6274 }
6275
8c5b4e52
AM
6276 if (!fh->elf.dynamic)
6277 {
6278 struct plt_entry *ent;
5bd4f169 6279
8c5b4e52
AM
6280 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6281 if (ent->plt.refcount > 0)
6282 break;
6283 if (ent == NULL)
6284 return TRUE;
6285 }
5bd4f169 6286
8c5b4e52 6287 /* Create a descriptor as undefined if necessary. */
50bc7936 6288 if (fdh == NULL
0e1862bb 6289 && !bfd_link_executable (info)
50bc7936
AM
6290 && (fh->elf.root.type == bfd_link_hash_undefined
6291 || fh->elf.root.type == bfd_link_hash_undefweak))
6292 {
908b32fc 6293 fdh = make_fdh (info, fh);
bb700d78
AM
6294 if (fdh == NULL)
6295 return FALSE;
50bc7936 6296 }
648cca2c 6297
8c5b4e52 6298 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6299 if (fdh != NULL
6300 && fdh->fake
8c5b4e52
AM
6301 && (fh->elf.root.type == bfd_link_hash_defined
6302 || fh->elf.root.type == bfd_link_hash_defweak))
6303 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 6304
8c5b4e52
AM
6305 /* Transfer dynamic linking information to the function descriptor. */
6306 if (fdh != NULL)
6307 {
f5385ebf
AM
6308 fdh->elf.ref_regular |= fh->elf.ref_regular;
6309 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6310 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6311 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6312 fdh->elf.dynamic |= fh->elf.dynamic;
6313 fdh->elf.needs_plt |= (fh->elf.needs_plt
6314 || fh->elf.type == STT_FUNC
6315 || fh->elf.type == STT_GNU_IFUNC);
6316 move_plt_plist (fh, fdh);
6317
6318 if (!fdh->elf.forced_local
6319 && fh->elf.dynindx != -1)
6320 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6321 return FALSE;
e86ce104
AM
6322 }
6323
50bc7936
AM
6324 /* Now that the info is on the function descriptor, clear the
6325 function code sym info. Any function code syms for which we
6326 don't have a definition in a regular file, we force local.
6327 This prevents a shared library from exporting syms that have
6328 been imported from another library. Function code syms that
6329 are really in the library we must leave global to prevent the
6330 linker dragging in a definition from a static library. */
93f3fa99
AM
6331 force_local = (!fh->elf.def_regular
6332 || fdh == NULL
6333 || !fdh->elf.def_regular
6334 || fdh->elf.forced_local);
50bc7936
AM
6335 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6336
b34976b6 6337 return TRUE;
e86ce104 6338}
40b8271b 6339
a4b6fadd
AM
6340static const struct sfpr_def_parms save_res_funcs[] =
6341 {
6342 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6343 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6344 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6345 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6346 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6347 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6348 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6349 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6350 { "._savef", 14, 31, savefpr, savefpr1_tail },
6351 { "._restf", 14, 31, restfpr, restfpr1_tail },
6352 { "_savevr_", 20, 31, savevr, savevr_tail },
6353 { "_restvr_", 20, 31, restvr, restvr_tail }
6354 };
6355
e86ce104 6356/* Called near the start of bfd_elf_size_dynamic_sections. We use
c9405344
AM
6357 this hook to a) run the edit functions in this file, b) provide
6358 some gcc support functions, and c) transfer dynamic linking
6359 information gathered so far on function code symbol entries, to
6360 their corresponding function descriptor symbol entries. */
deb0e272 6361
b34976b6 6362static bfd_boolean
c9405344 6363ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
e86ce104
AM
6364{
6365 struct ppc_link_hash_table *htab;
6366
6367 htab = ppc_hash_table (info);
4dfe6ac6
NC
6368 if (htab == NULL)
6369 return FALSE;
6370
c9405344
AM
6371 /* Call back into the linker, which then runs the edit functions. */
6372 htab->params->edit ();
6373
b32547cd
AM
6374 /* Provide any missing _save* and _rest* functions. */
6375 if (htab->sfpr != NULL)
6376 {
6377 unsigned int i;
6378
6379 htab->sfpr->size = 0;
6380 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6381 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6382 return FALSE;
6383 if (htab->sfpr->size == 0)
6384 htab->sfpr->flags |= SEC_EXCLUDE;
6385 }
6386
6387 if (bfd_link_relocatable (info))
6388 return TRUE;
6389
6390 if (htab->elf.hgot != NULL)
dba6fa9b
AM
6391 {
6392 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6393 /* Make .TOC. defined so as to prevent it being made dynamic.
6394 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6395 if (!htab->elf.hgot->def_regular
6396 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6397 {
6398 htab->elf.hgot->root.type = bfd_link_hash_defined;
6399 htab->elf.hgot->root.u.def.value = 0;
6400 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6401 htab->elf.hgot->def_regular = 1;
6402 htab->elf.hgot->root.linker_def = 1;
6403 }
dba6fa9b 6404 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6405 htab->elf.hgot->other
6406 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6407 }
c66bb0ee 6408
8c5b4e52
AM
6409 if (htab->need_func_desc_adj)
6410 {
6411 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6412 htab->need_func_desc_adj = 0;
6413 }
805fc799 6414
b34976b6 6415 return TRUE;
e86ce104
AM
6416}
6417
d311bc8b 6418/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6419 aliases, that apply to read-only sections. Cannot be used after
6420 size_dynamic_sections. */
d311bc8b
AM
6421
6422static bfd_boolean
6423alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6424{
ed7007c1 6425 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
d311bc8b
AM
6426 do
6427 {
5dbc8b37 6428 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
d311bc8b 6429 return TRUE;
ed7007c1 6430 eh = ppc_elf_hash_entry (eh->elf.u.alias);
2cdcc330
AM
6431 }
6432 while (eh != NULL && &eh->elf != h);
d311bc8b
AM
6433
6434 return FALSE;
6435}
8a2058b5 6436
8a9e8e72
AM
6437/* Return whether EH has pc-relative dynamic relocs. */
6438
6439static bfd_boolean
6440pc_dynrelocs (struct ppc_link_hash_entry *eh)
6441{
6442 struct elf_dyn_relocs *p;
6443
190eb1dd 6444 for (p = eh->elf.dyn_relocs; p != NULL; p = p->next)
8a9e8e72
AM
6445 if (p->pc_count != 0)
6446 return TRUE;
6447 return FALSE;
6448}
6449
8a2058b5
AM
6450/* Return true if a global entry stub will be created for H. Valid
6451 for ELFv2 before plt entries have been allocated. */
6452
6453static bfd_boolean
6454global_entry_stub (struct elf_link_hash_entry *h)
6455{
6456 struct plt_entry *pent;
6457
6458 if (!h->pointer_equality_needed
6459 || h->def_regular)
6460 return FALSE;
6461
6462 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6463 if (pent->plt.refcount > 0
6464 && pent->addend == 0)
6465 return TRUE;
6466
6467 return FALSE;
6468}
6469
e86ce104
AM
6470/* Adjust a symbol defined by a dynamic object and referenced by a
6471 regular object. The current definition is in some section of the
6472 dynamic object, but we're not including those sections. We have to
6473 change the definition to something the rest of the link can
6474 understand. */
6475
b34976b6 6476static bfd_boolean
4ce794b7
AM
6477ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6478 struct elf_link_hash_entry *h)
e86ce104
AM
6479{
6480 struct ppc_link_hash_table *htab;
5474d94f 6481 asection *s, *srel;
e86ce104
AM
6482
6483 htab = ppc_hash_table (info);
4dfe6ac6
NC
6484 if (htab == NULL)
6485 return FALSE;
e86ce104
AM
6486
6487 /* Deal with function syms. */
6488 if (h->type == STT_FUNC
e054468f 6489 || h->type == STT_GNU_IFUNC
f5385ebf 6490 || h->needs_plt)
e86ce104 6491 {
ed7007c1 6492 bfd_boolean local = (ppc_elf_hash_entry (h)->save_res
529fe20e
AM
6493 || SYMBOL_CALLS_LOCAL (info, h)
6494 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6495 /* Discard dyn_relocs when non-pic if we've decided that a
6496 function symbol is local and not an ifunc. We keep dynamic
6497 relocs for ifuncs when local rather than always emitting a
6498 plt call stub for them and defining the symbol on the call
6499 stub. We can't do that for ELFv1 anyway (a function symbol
6500 is defined on a descriptor, not code) and it can be faster at
6501 run-time due to not needing to bounce through a stub. The
6502 dyn_relocs for ifuncs will be applied even in a static
6503 executable. */
6504 if (!bfd_link_pic (info)
6505 && h->type != STT_GNU_IFUNC
6506 && local)
190eb1dd 6507 h->dyn_relocs = NULL;
529fe20e 6508
e86ce104
AM
6509 /* Clear procedure linkage table information for any symbol that
6510 won't need a .plt entry. */
411e1bfb
AM
6511 struct plt_entry *ent;
6512 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6513 if (ent->plt.refcount > 0)
6514 break;
8387904d 6515 if (ent == NULL
2d7ad24e
AM
6516 || (h->type != STT_GNU_IFUNC
6517 && local
3e04d765 6518 && (htab->can_convert_all_inline_plt
ed7007c1 6519 || (ppc_elf_hash_entry (h)->tls_mask
3e04d765 6520 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6521 {
411e1bfb 6522 h->plt.plist = NULL;
f5385ebf 6523 h->needs_plt = 0;
d1eca1e4 6524 h->pointer_equality_needed = 0;
40b8271b 6525 }
8a2058b5 6526 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6527 {
d1eca1e4
AM
6528 /* Taking a function's address in a read/write section
6529 doesn't require us to define the function symbol in the
6530 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6531 be used instead. The reason we prefer a few more dynamic
6532 relocs is that calling via a global entry stub costs a
6533 few more instructions, and pointer_equality_needed causes
6534 extra work in ld.so when resolving these symbols. */
529fe20e 6535 if (global_entry_stub (h))
d1eca1e4 6536 {
5dbc8b37 6537 if (!_bfd_elf_readonly_dynrelocs (h))
529fe20e
AM
6538 {
6539 h->pointer_equality_needed = 0;
04383fd1
AM
6540 /* If we haven't seen a branch reloc and the symbol
6541 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6542 if (!h->needs_plt)
6543 h->plt.plist = NULL;
6544 }
6545 else if (!bfd_link_pic (info))
6546 /* We are going to be defining the function symbol on the
6547 plt stub, so no dyn_relocs needed when non-pic. */
190eb1dd 6548 h->dyn_relocs = NULL;
d1eca1e4
AM
6549 }
6550
3988aed5
AM
6551 /* ELFv2 function symbols can't have copy relocs. */
6552 return TRUE;
6553 }
6554 else if (!h->needs_plt
5dbc8b37 6555 && !_bfd_elf_readonly_dynrelocs (h))
3988aed5 6556 {
04383fd1
AM
6557 /* If we haven't seen a branch reloc and the symbol isn't an
6558 ifunc then we don't need a plt entry. */
3988aed5
AM
6559 h->plt.plist = NULL;
6560 h->pointer_equality_needed = 0;
a345bc8d
AM
6561 return TRUE;
6562 }
5bd4f169 6563 }
bbd7ec4a 6564 else
411e1bfb 6565 h->plt.plist = NULL;
5bd4f169
AM
6566
6567 /* If this is a weak symbol, and there is a real definition, the
6568 processor independent code will have arranged for us to see the
6569 real definition first, and we can just use the same value. */
60d67dc8 6570 if (h->is_weakalias)
5bd4f169 6571 {
60d67dc8
AM
6572 struct elf_link_hash_entry *def = weakdef (h);
6573 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6574 h->root.u.def.section = def->root.u.def.section;
6575 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6576 if (def->root.u.def.section == htab->elf.sdynbss
6577 || def->root.u.def.section == htab->elf.sdynrelro)
190eb1dd 6578 h->dyn_relocs = NULL;
b34976b6 6579 return TRUE;
5bd4f169
AM
6580 }
6581
5bd4f169
AM
6582 /* If we are creating a shared library, we must presume that the
6583 only references to the symbol are via the global offset table.
6584 For such cases we need not do anything here; the relocations will
6585 be handled correctly by relocate_section. */
ec73ddcd 6586 if (!bfd_link_executable (info))
b34976b6 6587 return TRUE;
5bd4f169 6588
65f38f15
AM
6589 /* If there are no references to this symbol that do not use the
6590 GOT, we don't need to generate a copy reloc. */
f5385ebf 6591 if (!h->non_got_ref)
b34976b6 6592 return TRUE;
65f38f15 6593
b186458a 6594 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6595 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6596
d93d1c80
AM
6597 /* If -z nocopyreloc was given, don't generate them either. */
6598 || info->nocopyreloc
a127494f 6599
dce2246a 6600 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6601 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1bdd8fac
AM
6602 || (ELIMINATE_COPY_RELOCS
6603 && !h->needs_copy
6604 && !alias_readonly_dynrelocs (h))
65f38f15 6605
d93d1c80
AM
6606 /* Protected variables do not work with .dynbss. The copy in
6607 .dynbss won't be used by the shared library with the protected
6608 definition for the variable. Text relocations are preferable
6609 to an incorrect program. */
6610 || h->protected_def)
529fe20e 6611 return TRUE;
a127494f 6612
e1c6cf61
AM
6613 if (h->type == STT_FUNC
6614 || h->type == STT_GNU_IFUNC)
97b639ba 6615 {
e1c6cf61
AM
6616 /* .dynbss copies of function symbols only work if we have
6617 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6618 use dot-symbols and set the function symbol size to the text
6619 size of the function rather than the size of the descriptor.
6620 That's wrong for copying a descriptor. */
ed7007c1 6621 if (ppc_elf_hash_entry (h)->oh == NULL
e1c6cf61
AM
6622 || !(h->size == 24 || h->size == 16))
6623 return TRUE;
6624
6625 /* We should never get here, but unfortunately there are old
6626 versions of gcc (circa gcc-3.2) that improperly for the
6627 ELFv1 ABI put initialized function pointers, vtable refs and
6628 suchlike in read-only sections. Allow them to proceed, but
6629 warn that this might break at runtime. */
25f53a85 6630 info->callbacks->einfo
c1c8c1ef 6631 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6632 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6633 h->root.root.string);
6634 }
5d35169e
AM
6635
6636 /* This is a reference to a symbol defined by a dynamic object which
6637 is not a function. */
6638
5bd4f169
AM
6639 /* We must allocate the symbol in our .dynbss section, which will
6640 become part of the .bss section of the executable. There will be
6641 an entry for this symbol in the .dynsym section. The dynamic
6642 object will contain position independent code, so all references
6643 from the dynamic object to this symbol will go through the global
6644 offset table. The dynamic linker will use the .dynsym entry to
6645 determine the address it must put in the global offset table, so
6646 both the dynamic object and the regular object will refer to the
6647 same memory location for the variable. */
5474d94f
AM
6648 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6649 {
6650 s = htab->elf.sdynrelro;
6651 srel = htab->elf.sreldynrelro;
6652 }
6653 else
6654 {
6655 s = htab->elf.sdynbss;
6656 srel = htab->elf.srelbss;
6657 }
1d7e9d18 6658 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6659 {
4a7e5234
AM
6660 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6661 linker to copy the initial value out of the dynamic object
6662 and into the runtime process image. */
5474d94f 6663 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6664 h->needs_copy = 1;
5bd4f169
AM
6665 }
6666
529fe20e 6667 /* We no longer want dyn_relocs. */
190eb1dd 6668 h->dyn_relocs = NULL;
6cabe1ea 6669 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6670}
6671
e86ce104
AM
6672/* If given a function descriptor symbol, hide both the function code
6673 sym and the descriptor. */
6674static void
4ce794b7
AM
6675ppc64_elf_hide_symbol (struct bfd_link_info *info,
6676 struct elf_link_hash_entry *h,
6677 bfd_boolean force_local)
e86ce104 6678{
34814b9f 6679 struct ppc_link_hash_entry *eh;
e86ce104
AM
6680 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6681
87469ba2
AM
6682 if (ppc_hash_table (info) == NULL)
6683 return;
6684
ed7007c1 6685 eh = ppc_elf_hash_entry (h);
34814b9f 6686 if (eh->is_func_descriptor)
e86ce104 6687 {
34814b9f 6688 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6689
721956f4 6690 if (fh == NULL)
d1329ca3
AM
6691 {
6692 const char *p, *q;
b8ac2841 6693 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6694 char save;
6695
6696 /* We aren't supposed to use alloca in BFD because on
6697 systems which do not have alloca the version in libiberty
6698 calls xmalloc, which might cause the program to crash
6699 when it runs out of memory. This function doesn't have a
6700 return status, so there's no way to gracefully return an
6701 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6702 accessed; It's either a string in an ELF string table,
6703 or allocated in an objalloc structure. */
d1329ca3 6704
34814b9f 6705 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6706 save = *p;
6707 *(char *) p = '.';
ed7007c1
AM
6708 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, FALSE,
6709 FALSE, FALSE));
d1329ca3
AM
6710 *(char *) p = save;
6711
6712 /* Unfortunately, if it so happens that the string we were
6713 looking for was allocated immediately before this string,
6714 then we overwrote the string terminator. That's the only
6715 reason the lookup should fail. */
6716 if (fh == NULL)
6717 {
34814b9f
AM
6718 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6719 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6720 --q, --p;
34814b9f 6721 if (q < eh->elf.root.root.string && *p == '.')
ed7007c1
AM
6722 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, FALSE,
6723 FALSE, FALSE));
d1329ca3
AM
6724 }
6725 if (fh != NULL)
6726 {
34814b9f
AM
6727 eh->oh = fh;
6728 fh->oh = eh;
d1329ca3
AM
6729 }
6730 }
e86ce104 6731 if (fh != NULL)
34814b9f 6732 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6733 }
6734}
6735
411e1bfb 6736static bfd_boolean
8843416a
AM
6737get_sym_h (struct elf_link_hash_entry **hp,
6738 Elf_Internal_Sym **symp,
6739 asection **symsecp,
f961d9dd 6740 unsigned char **tls_maskp,
8843416a
AM
6741 Elf_Internal_Sym **locsymsp,
6742 unsigned long r_symndx,
6743 bfd *ibfd)
411e1bfb 6744{
0ffa91dd 6745 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6746
6747 if (r_symndx >= symtab_hdr->sh_info)
6748 {
6749 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6750 struct elf_link_hash_entry *h;
6751
6752 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6753 h = elf_follow_link (h);
411e1bfb
AM
6754
6755 if (hp != NULL)
6756 *hp = h;
6757
6758 if (symp != NULL)
6759 *symp = NULL;
6760
6761 if (symsecp != NULL)
6762 {
6763 asection *symsec = NULL;
6764 if (h->root.type == bfd_link_hash_defined
6765 || h->root.type == bfd_link_hash_defweak)
6766 symsec = h->root.u.def.section;
6767 *symsecp = symsec;
6768 }
6769
e7b938ca 6770 if (tls_maskp != NULL)
ed7007c1 6771 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
411e1bfb
AM
6772 }
6773 else
6774 {
6775 Elf_Internal_Sym *sym;
6776 Elf_Internal_Sym *locsyms = *locsymsp;
6777
6778 if (locsyms == NULL)
6779 {
6780 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6781 if (locsyms == NULL)
6782 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6783 symtab_hdr->sh_info,
6784 0, NULL, NULL, NULL);
6785 if (locsyms == NULL)
6786 return FALSE;
6787 *locsymsp = locsyms;
6788 }
6789 sym = locsyms + r_symndx;
6790
6791 if (hp != NULL)
6792 *hp = NULL;
6793
6794 if (symp != NULL)
6795 *symp = sym;
6796
6797 if (symsecp != NULL)
cb33740c 6798 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6799
e7b938ca 6800 if (tls_maskp != NULL)
411e1bfb
AM
6801 {
6802 struct got_entry **lgot_ents;
f961d9dd 6803 unsigned char *tls_mask;
411e1bfb 6804
e7b938ca 6805 tls_mask = NULL;
411e1bfb
AM
6806 lgot_ents = elf_local_got_ents (ibfd);
6807 if (lgot_ents != NULL)
6808 {
e054468f
AM
6809 struct plt_entry **local_plt = (struct plt_entry **)
6810 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6811 unsigned char *lgot_masks = (unsigned char *)
e054468f 6812 (local_plt + symtab_hdr->sh_info);
e7b938ca 6813 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6814 }
e7b938ca 6815 *tls_maskp = tls_mask;
411e1bfb
AM
6816 }
6817 }
6818 return TRUE;
6819}
6820
e7b938ca 6821/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6822 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6823 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6824
6825static int
f961d9dd 6826get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6827 unsigned long *toc_symndx,
6828 bfd_vma *toc_addend,
0d4792f7 6829 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6830 const Elf_Internal_Rela *rel,
6831 bfd *ibfd)
411e1bfb
AM
6832{
6833 unsigned long r_symndx;
0d4792f7 6834 int next_r;
411e1bfb
AM
6835 struct elf_link_hash_entry *h;
6836 Elf_Internal_Sym *sym;
6837 asection *sec;
6838 bfd_vma off;
6839
6840 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6841 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6842 return 0;
411e1bfb 6843
37da22e5
AM
6844 if ((*tls_maskp != NULL
6845 && (**tls_maskp & TLS_TLS) != 0
6846 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 6847 || sec == NULL
6bee8834 6848 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6849 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6850 return 1;
411e1bfb
AM
6851
6852 /* Look inside a TOC section too. */
6853 if (h != NULL)
6854 {
6855 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6856 off = h->root.u.def.value;
6857 }
6858 else
6859 off = sym->st_value;
6860 off += rel->r_addend;
6861 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6862 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6863 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6864 if (toc_symndx != NULL)
6865 *toc_symndx = r_symndx;
3a71aa26
AM
6866 if (toc_addend != NULL)
6867 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6868 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6869 return 0;
854b41e7 6870 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6871 && (next_r == -1 || next_r == -2))
6872 return 1 - next_r;
951fd09b 6873 return 1;
411e1bfb
AM
6874}
6875
3b421ab3
AM
6876/* Find (or create) an entry in the tocsave hash table. */
6877
6878static struct tocsave_entry *
6879tocsave_find (struct ppc_link_hash_table *htab,
6880 enum insert_option insert,
6881 Elf_Internal_Sym **local_syms,
6882 const Elf_Internal_Rela *irela,
6883 bfd *ibfd)
6884{
6885 unsigned long r_indx;
6886 struct elf_link_hash_entry *h;
6887 Elf_Internal_Sym *sym;
6888 struct tocsave_entry ent, *p;
6889 hashval_t hash;
6890 struct tocsave_entry **slot;
6891
6892 r_indx = ELF64_R_SYM (irela->r_info);
6893 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6894 return NULL;
6895 if (ent.sec == NULL || ent.sec->output_section == NULL)
6896 {
4eca0228 6897 _bfd_error_handler
871b3ab2 6898 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
6899 return NULL;
6900 }
6901
6902 if (h != NULL)
6903 ent.offset = h->root.u.def.value;
6904 else
6905 ent.offset = sym->st_value;
6906 ent.offset += irela->r_addend;
6907
6908 hash = tocsave_htab_hash (&ent);
6909 slot = ((struct tocsave_entry **)
6910 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6911 if (slot == NULL)
6912 return NULL;
6913
6914 if (*slot == NULL)
6915 {
6916 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6917 if (p == NULL)
6918 return NULL;
6919 *p = ent;
6920 *slot = p;
6921 }
6922 return *slot;
6923}
6924
754021d0 6925/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6926 code for the old ABI, these will already have been done. */
754021d0
AM
6927
6928static bfd_boolean
6929adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6930{
6931 struct ppc_link_hash_entry *eh;
6932 asection *sym_sec;
74f0fb50 6933 struct _opd_sec_data *opd;
754021d0
AM
6934
6935 if (h->root.type == bfd_link_hash_indirect)
6936 return TRUE;
6937
754021d0
AM
6938 if (h->root.type != bfd_link_hash_defined
6939 && h->root.type != bfd_link_hash_defweak)
6940 return TRUE;
6941
ed7007c1 6942 eh = ppc_elf_hash_entry (h);
754021d0
AM
6943 if (eh->adjust_done)
6944 return TRUE;
6945
6946 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6947 opd = get_opd_info (sym_sec);
6948 if (opd != NULL && opd->adjust != NULL)
754021d0 6949 {
51aecdc5 6950 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
6951 if (adjust == -1)
6952 {
6953 /* This entry has been deleted. */
b3fac117 6954 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6955 if (dsec == NULL)
6956 {
6957 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 6958 if (discarded_section (dsec))
81688140 6959 {
b3fac117 6960 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6961 break;
6962 }
6963 }
4025353c 6964 eh->elf.root.u.def.value = 0;
81688140 6965 eh->elf.root.u.def.section = dsec;
4025353c
AM
6966 }
6967 else
6968 eh->elf.root.u.def.value += adjust;
754021d0
AM
6969 eh->adjust_done = 1;
6970 }
6971 return TRUE;
6972}
6973
8c1d1bb8 6974/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 6975 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
6976 have already been determined. */
6977
6978static bfd_boolean
6979dec_dynrel_count (bfd_vma r_info,
6980 asection *sec,
6981 struct bfd_link_info *info,
6982 Elf_Internal_Sym **local_syms,
6983 struct elf_link_hash_entry *h,
19e08130 6984 Elf_Internal_Sym *sym)
8c1d1bb8
AM
6985{
6986 enum elf_ppc64_reloc_type r_type;
19e08130 6987 asection *sym_sec = NULL;
8c1d1bb8
AM
6988
6989 /* Can this reloc be dynamic? This switch, and later tests here
6990 should be kept in sync with the code in check_relocs. */
6991 r_type = ELF64_R_TYPE (r_info);
6992 switch (r_type)
6993 {
6994 default:
6995 return TRUE;
6996
1bdd8fac
AM
6997 case R_PPC64_TOC16:
6998 case R_PPC64_TOC16_DS:
6999 case R_PPC64_TOC16_LO:
7000 case R_PPC64_TOC16_HI:
7001 case R_PPC64_TOC16_HA:
7002 case R_PPC64_TOC16_LO_DS:
7003 if (h == NULL)
7004 return TRUE;
7005 break;
7006
8c1d1bb8
AM
7007 case R_PPC64_TPREL16:
7008 case R_PPC64_TPREL16_LO:
7009 case R_PPC64_TPREL16_HI:
7010 case R_PPC64_TPREL16_HA:
7011 case R_PPC64_TPREL16_DS:
7012 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7013 case R_PPC64_TPREL16_HIGH:
7014 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7015 case R_PPC64_TPREL16_HIGHER:
7016 case R_PPC64_TPREL16_HIGHERA:
7017 case R_PPC64_TPREL16_HIGHEST:
7018 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8 7019 case R_PPC64_TPREL64:
c213164a 7020 case R_PPC64_TPREL34:
8c1d1bb8
AM
7021 case R_PPC64_DTPMOD64:
7022 case R_PPC64_DTPREL64:
7023 case R_PPC64_ADDR64:
7024 case R_PPC64_REL30:
7025 case R_PPC64_REL32:
7026 case R_PPC64_REL64:
7027 case R_PPC64_ADDR14:
7028 case R_PPC64_ADDR14_BRNTAKEN:
7029 case R_PPC64_ADDR14_BRTAKEN:
7030 case R_PPC64_ADDR16:
7031 case R_PPC64_ADDR16_DS:
7032 case R_PPC64_ADDR16_HA:
7033 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7034 case R_PPC64_ADDR16_HIGH:
7035 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7036 case R_PPC64_ADDR16_HIGHER:
7037 case R_PPC64_ADDR16_HIGHERA:
7038 case R_PPC64_ADDR16_HIGHEST:
7039 case R_PPC64_ADDR16_HIGHESTA:
7040 case R_PPC64_ADDR16_LO:
7041 case R_PPC64_ADDR16_LO_DS:
7042 case R_PPC64_ADDR24:
7043 case R_PPC64_ADDR32:
7044 case R_PPC64_UADDR16:
7045 case R_PPC64_UADDR32:
7046 case R_PPC64_UADDR64:
7047 case R_PPC64_TOC:
5663e321
AM
7048 case R_PPC64_D34:
7049 case R_PPC64_D34_LO:
7050 case R_PPC64_D34_HI30:
7051 case R_PPC64_D34_HA30:
7052 case R_PPC64_ADDR16_HIGHER34:
7053 case R_PPC64_ADDR16_HIGHERA34:
7054 case R_PPC64_ADDR16_HIGHEST34:
7055 case R_PPC64_ADDR16_HIGHESTA34:
7056 case R_PPC64_D28:
8c1d1bb8
AM
7057 break;
7058 }
7059
7060 if (local_syms != NULL)
7061 {
7062 unsigned long r_symndx;
8c1d1bb8
AM
7063 bfd *ibfd = sec->owner;
7064
7065 r_symndx = ELF64_R_SYM (r_info);
7066 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7067 return FALSE;
7068 }
7069
ec73ddcd
AM
7070 if ((h != NULL
7071 && (h->root.type == bfd_link_hash_defweak
7072 || !h->def_regular))
7073 || (h != NULL
7074 && !bfd_link_executable (info)
7075 && !SYMBOLIC_BIND (info, h))
7076 || (bfd_link_pic (info)
7077 && must_be_dyn_reloc (info, r_type))
7078 || (!bfd_link_pic (info)
7079 && (h != NULL
7080 ? h->type == STT_GNU_IFUNC
7081 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
8c1d1bb8
AM
7082 ;
7083 else
7084 return TRUE;
7085
7086 if (h != NULL)
6edfbbad 7087 {
19e08130
AM
7088 struct elf_dyn_relocs *p;
7089 struct elf_dyn_relocs **pp;
190eb1dd 7090 pp = &h->dyn_relocs;
19e08130
AM
7091
7092 /* elf_gc_sweep may have already removed all dyn relocs associated
7093 with local syms for a given section. Also, symbol flags are
7094 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7095 report a dynreloc miscount. */
7096 if (*pp == NULL && info->gc_sections)
7097 return TRUE;
7098
7099 while ((p = *pp) != NULL)
60124e18 7100 {
19e08130
AM
7101 if (p->sec == sec)
7102 {
7103 if (!must_be_dyn_reloc (info, r_type))
7104 p->pc_count -= 1;
7105 p->count -= 1;
7106 if (p->count == 0)
7107 *pp = p->next;
7108 return TRUE;
7109 }
7110 pp = &p->next;
60124e18 7111 }
6edfbbad 7112 }
19e08130
AM
7113 else
7114 {
7115 struct ppc_dyn_relocs *p;
7116 struct ppc_dyn_relocs **pp;
7117 void *vpp;
7118 bfd_boolean is_ifunc;
8c1d1bb8 7119
19e08130
AM
7120 if (local_syms == NULL)
7121 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7122 if (sym_sec == NULL)
7123 sym_sec = sec;
c57da1a7 7124
19e08130
AM
7125 vpp = &elf_section_data (sym_sec)->local_dynrel;
7126 pp = (struct ppc_dyn_relocs **) vpp;
7127
7128 if (*pp == NULL && info->gc_sections)
7129 return TRUE;
7130
7131 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7132 while ((p = *pp) != NULL)
8c1d1bb8 7133 {
19e08130
AM
7134 if (p->sec == sec && p->ifunc == is_ifunc)
7135 {
7136 p->count -= 1;
7137 if (p->count == 0)
7138 *pp = p->next;
7139 return TRUE;
7140 }
7141 pp = &p->next;
8c1d1bb8 7142 }
8c1d1bb8
AM
7143 }
7144
695344c0 7145 /* xgettext:c-format */
cf97bcb0
AM
7146 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7147 sec->owner, sec);
8c1d1bb8
AM
7148 bfd_set_error (bfd_error_bad_value);
7149 return FALSE;
7150}
7151
754021d0
AM
7152/* Remove unused Official Procedure Descriptor entries. Currently we
7153 only remove those associated with functions in discarded link-once
7154 sections, or weakly defined functions that have been overridden. It
7155 would be possible to remove many more entries for statically linked
7156 applications. */
7157
b34976b6 7158bfd_boolean
e7d1c40c 7159ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7160{
7161 bfd *ibfd;
754021d0 7162 bfd_boolean some_edited = FALSE;
3f764659 7163 asection *need_pad = NULL;
e7d1c40c
AM
7164 struct ppc_link_hash_table *htab;
7165
7166 htab = ppc_hash_table (info);
7167 if (htab == NULL)
7168 return FALSE;
1e2f5b6e 7169
c72f2fb2 7170 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7171 {
7172 asection *sec;
7173 Elf_Internal_Rela *relstart, *rel, *relend;
7174 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7175 Elf_Internal_Sym *local_syms;
74f0fb50 7176 struct _opd_sec_data *opd;
51aecdc5 7177 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7178 bfd_size_type cnt_16b = 0;
1e2f5b6e 7179
854b41e7
AM
7180 if (!is_ppc64_elf (ibfd))
7181 continue;
7182
1e2f5b6e 7183 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7184 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7185 continue;
7186
dbaa2011 7187 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7188 continue;
7189
1e2f5b6e
AM
7190 if (sec->output_section == bfd_abs_section_ptr)
7191 continue;
7192
7193 /* Look through the section relocs. */
7194 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7195 continue;
7196
6cdc0ccc 7197 local_syms = NULL;
0ffa91dd 7198 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7199
7200 /* Read the relocations. */
4ce794b7 7201 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7202 info->keep_memory);
1e2f5b6e 7203 if (relstart == NULL)
b34976b6 7204 return FALSE;
1e2f5b6e
AM
7205
7206 /* First run through the relocs to check they are sane, and to
7207 determine whether we need to edit this opd section. */
b34976b6 7208 need_edit = FALSE;
51aecdc5 7209 broken = FALSE;
3f764659 7210 need_pad = sec;
1e2f5b6e 7211 relend = relstart + sec->reloc_count;
50bc7936 7212 for (rel = relstart; rel < relend; )
1e2f5b6e 7213 {
04c9666a 7214 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7215 unsigned long r_symndx;
7216 asection *sym_sec;
7217 struct elf_link_hash_entry *h;
7218 Elf_Internal_Sym *sym;
51aecdc5 7219 bfd_vma offset;
1e2f5b6e 7220
51aecdc5 7221 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7222 only interested in the reloc pointing to a function entry
7223 point. */
51aecdc5
AM
7224 offset = rel->r_offset;
7225 if (rel + 1 == relend
7226 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7227 {
7228 /* If someone messes with .opd alignment then after a
7229 "ld -r" we might have padding in the middle of .opd.
7230 Also, there's nothing to prevent someone putting
7231 something silly in .opd with the assembler. No .opd
b34976b6 7232 optimization for them! */
3f764659 7233 broken_opd:
4eca0228 7234 _bfd_error_handler
871b3ab2 7235 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7236 broken = TRUE;
1e2f5b6e
AM
7237 break;
7238 }
7239
50bc7936
AM
7240 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7241 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7242 {
4eca0228 7243 _bfd_error_handler
695344c0 7244 /* xgettext:c-format */
871b3ab2 7245 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7246 ibfd, r_type);
51aecdc5 7247 broken = TRUE;
50bc7936
AM
7248 break;
7249 }
7250
1e2f5b6e 7251 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7252 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7253 r_symndx, ibfd))
50bc7936 7254 goto error_ret;
1e2f5b6e
AM
7255
7256 if (sym_sec == NULL || sym_sec->owner == NULL)
7257 {
411e1bfb
AM
7258 const char *sym_name;
7259 if (h != NULL)
7260 sym_name = h->root.root.string;
7261 else
26c61ae5
L
7262 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7263 sym_sec);
411e1bfb 7264
4eca0228 7265 _bfd_error_handler
695344c0 7266 /* xgettext:c-format */
871b3ab2 7267 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7268 ibfd, sym_name);
51aecdc5 7269 broken = TRUE;
1e2f5b6e
AM
7270 break;
7271 }
7272
51020317
AM
7273 /* opd entries are always for functions defined in the
7274 current input bfd. If the symbol isn't defined in the
7275 input bfd, then we won't be using the function in this
7276 bfd; It must be defined in a linkonce section in another
7277 bfd, or is weak. It's also possible that we are
7278 discarding the function due to a linker script /DISCARD/,
7279 which we test for via the output_section. */
7280 if (sym_sec->owner != ibfd
7281 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7282 need_edit = TRUE;
1e2f5b6e 7283
50bc7936 7284 rel += 2;
51aecdc5
AM
7285 if (rel + 1 == relend
7286 || (rel + 2 < relend
7287 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7288 ++rel;
7289
7290 if (rel == relend)
3f764659
JJ
7291 {
7292 if (sec->size == offset + 24)
7293 {
7294 need_pad = NULL;
7295 break;
7296 }
51aecdc5 7297 if (sec->size == offset + 16)
3f764659
JJ
7298 {
7299 cnt_16b++;
7300 break;
7301 }
7302 goto broken_opd;
7303 }
3f764659
JJ
7304 else if (rel + 1 < relend
7305 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7306 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7307 {
51aecdc5
AM
7308 if (rel[0].r_offset == offset + 16)
7309 cnt_16b++;
7310 else if (rel[0].r_offset != offset + 24)
7311 goto broken_opd;
3f764659
JJ
7312 }
7313 else
7314 goto broken_opd;
1e2f5b6e
AM
7315 }
7316
e7d1c40c 7317 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7318
51aecdc5 7319 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7320 {
7321 Elf_Internal_Rela *write_rel;
d4730f92 7322 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7323 bfd_byte *rptr, *wptr;
983bddc8 7324 bfd_byte *new_contents;
74f0fb50
AM
7325 bfd_size_type amt;
7326
983bddc8 7327 new_contents = NULL;
51aecdc5 7328 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7329 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7330 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7331 if (opd->adjust == NULL)
7332 return FALSE;
1e2f5b6e
AM
7333
7334 /* This seems a waste of time as input .opd sections are all
7335 zeros as generated by gcc, but I suppose there's no reason
7336 this will always be so. We might start putting something in
7337 the third word of .opd entries. */
7338 if ((sec->flags & SEC_IN_MEMORY) == 0)
7339 {
eea6121a
AM
7340 bfd_byte *loc;
7341 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7342 {
c9594989 7343 free (loc);
50bc7936 7344 error_ret:
c9594989 7345 if (symtab_hdr->contents != (unsigned char *) local_syms)
6cdc0ccc 7346 free (local_syms);
6cdc0ccc
AM
7347 if (elf_section_data (sec)->relocs != relstart)
7348 free (relstart);
b34976b6 7349 return FALSE;
6cdc0ccc 7350 }
1e2f5b6e
AM
7351 sec->contents = loc;
7352 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7353 }
7354
7355 elf_section_data (sec)->relocs = relstart;
7356
3f764659 7357 new_contents = sec->contents;
3f764659
JJ
7358 if (add_aux_fields)
7359 {
7360 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7361 if (new_contents == NULL)
7362 return FALSE;
51aecdc5 7363 need_pad = NULL;
3f764659 7364 }
b4f4e59f
AM
7365 wptr = new_contents;
7366 rptr = sec->contents;
1e2f5b6e 7367 write_rel = relstart;
51aecdc5 7368 for (rel = relstart; rel < relend; )
1e2f5b6e 7369 {
50bc7936
AM
7370 unsigned long r_symndx;
7371 asection *sym_sec;
7372 struct elf_link_hash_entry *h;
51aecdc5 7373 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7374 Elf_Internal_Sym *sym;
51aecdc5
AM
7375 long opd_ent_size;
7376 Elf_Internal_Rela *next_rel;
7377 bfd_boolean skip;
50bc7936
AM
7378
7379 r_symndx = ELF64_R_SYM (rel->r_info);
7380 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7381 r_symndx, ibfd))
50bc7936
AM
7382 goto error_ret;
7383
51aecdc5
AM
7384 next_rel = rel + 2;
7385 if (next_rel + 1 == relend
7386 || (next_rel + 2 < relend
7387 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7388 ++next_rel;
7389
7390 /* See if the .opd entry is full 24 byte or
7391 16 byte (with fd_aux entry overlapped with next
7392 fd_func). */
7393 opd_ent_size = 24;
7394 if (next_rel == relend)
1e2f5b6e 7395 {
51aecdc5 7396 if (sec->size == rel->r_offset + 16)
3f764659 7397 opd_ent_size = 16;
51aecdc5
AM
7398 }
7399 else if (next_rel->r_offset == rel->r_offset + 16)
7400 opd_ent_size = 16;
3f764659 7401
51aecdc5
AM
7402 if (h != NULL
7403 && h->root.root.string[0] == '.')
7404 {
ed7007c1 7405 fdh = ppc_elf_hash_entry (h)->oh;
8c5b4e52
AM
7406 if (fdh != NULL)
7407 {
7408 fdh = ppc_follow_link (fdh);
7409 if (fdh->elf.root.type != bfd_link_hash_defined
7410 && fdh->elf.root.type != bfd_link_hash_defweak)
7411 fdh = NULL;
7412 }
51aecdc5 7413 }
1e2f5b6e 7414
51aecdc5
AM
7415 skip = (sym_sec->owner != ibfd
7416 || sym_sec->output_section == bfd_abs_section_ptr);
7417 if (skip)
7418 {
7419 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7420 {
51aecdc5
AM
7421 /* Arrange for the function descriptor sym
7422 to be dropped. */
7423 fdh->elf.root.u.def.value = 0;
7424 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7425 }
51aecdc5 7426 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7427
0e1862bb 7428 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7429 rel = next_rel;
7430 else
7431 while (1)
7432 {
7433 if (!dec_dynrel_count (rel->r_info, sec, info,
7434 NULL, h, sym))
7435 goto error_ret;
754021d0 7436
51aecdc5
AM
7437 if (++rel == next_rel)
7438 break;
1e2f5b6e 7439
51aecdc5
AM
7440 r_symndx = ELF64_R_SYM (rel->r_info);
7441 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7442 r_symndx, ibfd))
7443 goto error_ret;
7444 }
50bc7936
AM
7445 }
7446 else
1e2f5b6e 7447 {
51aecdc5
AM
7448 /* We'll be keeping this opd entry. */
7449 long adjust;
7450
7451 if (fdh != NULL)
7452 {
7453 /* Redefine the function descriptor symbol to
7454 this location in the opd section. It is
7455 necessary to update the value here rather
7456 than using an array of adjustments as we do
7457 for local symbols, because various places
7458 in the generic ELF code use the value
7459 stored in u.def.value. */
7460 fdh->elf.root.u.def.value = wptr - new_contents;
7461 fdh->adjust_done = 1;
7462 }
7463
7464 /* Local syms are a bit tricky. We could
7465 tweak them as they can be cached, but
7466 we'd need to look through the local syms
7467 for the function descriptor sym which we
7468 don't have at the moment. So keep an
7469 array of adjustments. */
7470 adjust = (wptr - new_contents) - (rptr - sec->contents);
7471 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7472
7473 if (wptr != rptr)
7474 memcpy (wptr, rptr, opd_ent_size);
7475 wptr += opd_ent_size;
7476 if (add_aux_fields && opd_ent_size == 16)
7477 {
7478 memset (wptr, '\0', 8);
7479 wptr += 8;
7480 }
7481
50bc7936 7482 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7483 new opd entries. */
7484 for ( ; rel != next_rel; ++rel)
7485 {
7486 rel->r_offset += adjust;
7487 if (write_rel != rel)
7488 memcpy (write_rel, rel, sizeof (*rel));
7489 ++write_rel;
7490 }
1e2f5b6e 7491 }
51aecdc5
AM
7492
7493 rptr += opd_ent_size;
1e2f5b6e
AM
7494 }
7495
3f764659 7496 sec->size = wptr - new_contents;
1e2f5b6e 7497 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7498 if (add_aux_fields)
7499 {
7500 free (sec->contents);
7501 sec->contents = new_contents;
7502 }
7503
05bf9422 7504 /* Fudge the header size too, as this is used later in
cdcf6e38 7505 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7506 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7507 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7508 some_edited = TRUE;
1e2f5b6e 7509 }
6cdc0ccc 7510 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7511 free (relstart);
6cdc0ccc 7512
411e1bfb
AM
7513 if (local_syms != NULL
7514 && symtab_hdr->contents != (unsigned char *) local_syms)
7515 {
7516 if (!info->keep_memory)
7517 free (local_syms);
7518 else
7519 symtab_hdr->contents = (unsigned char *) local_syms;
7520 }
7521 }
7522
754021d0
AM
7523 if (some_edited)
7524 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7525
3f764659
JJ
7526 /* If we are doing a final link and the last .opd entry is just 16 byte
7527 long, add a 8 byte padding after it. */
0e1862bb 7528 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7529 {
7530 bfd_byte *p;
7531
7532 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7533 {
7534 BFD_ASSERT (need_pad->size > 0);
7535
7536 p = bfd_malloc (need_pad->size + 8);
7537 if (p == NULL)
7538 return FALSE;
699733f6 7539
2cdcc330
AM
7540 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7541 p, 0, need_pad->size))
3f764659
JJ
7542 return FALSE;
7543
7544 need_pad->contents = p;
7545 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7546 }
7547 else
7548 {
7549 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7550 if (p == NULL)
7551 return FALSE;
7552
7553 need_pad->contents = p;
7554 }
7555
7556 memset (need_pad->contents + need_pad->size, 0, 8);
7557 need_pad->size += 8;
7558 }
7559
411e1bfb
AM
7560 return TRUE;
7561}
7562
3e04d765
AM
7563/* Analyze inline PLT call relocations to see whether calls to locally
7564 defined functions can be converted to direct calls. */
7565
7566bfd_boolean
7567ppc64_elf_inline_plt (struct bfd_link_info *info)
7568{
7569 struct ppc_link_hash_table *htab;
7570 bfd *ibfd;
7571 asection *sec;
7572 bfd_vma low_vma, high_vma, limit;
7573
7574 htab = ppc_hash_table (info);
7575 if (htab == NULL)
7576 return FALSE;
7577
7578 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7579 reduced somewhat to cater for possible stubs that might be added
7580 between the call and its destination. */
7581 if (htab->params->group_size < 0)
7582 {
7583 limit = -htab->params->group_size;
7584 if (limit == 1)
7585 limit = 0x1e00000;
7586 }
7587 else
7588 {
7589 limit = htab->params->group_size;
7590 if (limit == 1)
7591 limit = 0x1c00000;
7592 }
7593
7594 low_vma = -1;
7595 high_vma = 0;
7596 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7597 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7598 {
7599 if (low_vma > sec->vma)
7600 low_vma = sec->vma;
7601 if (high_vma < sec->vma + sec->size)
7602 high_vma = sec->vma + sec->size;
7603 }
7604
7605 /* If a "bl" can reach anywhere in local code sections, then we can
7606 convert all inline PLT sequences to direct calls when the symbol
7607 is local. */
7608 if (high_vma - low_vma < limit)
7609 {
7610 htab->can_convert_all_inline_plt = 1;
7611 return TRUE;
7612 }
7613
7614 /* Otherwise, go looking through relocs for cases where a direct
7615 call won't reach. Mark the symbol on any such reloc to disable
7616 the optimization and keep the PLT entry as it seems likely that
7617 this will be better than creating trampolines. Note that this
7618 will disable the optimization for all inline PLT calls to a
7619 particular symbol, not just those that won't reach. The
7620 difficulty in doing a more precise optimization is that the
7621 linker needs to make a decision depending on whether a
7622 particular R_PPC64_PLTCALL insn can be turned into a direct
7623 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7624 the sequence, and there is nothing that ties those relocs
7625 together except their symbol. */
7626
7627 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7628 {
7629 Elf_Internal_Shdr *symtab_hdr;
7630 Elf_Internal_Sym *local_syms;
7631
7632 if (!is_ppc64_elf (ibfd))
7633 continue;
7634
7635 local_syms = NULL;
7636 symtab_hdr = &elf_symtab_hdr (ibfd);
7637
7638 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7639 if (ppc64_elf_section_data (sec)->has_pltcall
7640 && !bfd_is_abs_section (sec->output_section))
7641 {
7642 Elf_Internal_Rela *relstart, *rel, *relend;
7643
7644 /* Read the relocations. */
7645 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7646 info->keep_memory);
7647 if (relstart == NULL)
7648 return FALSE;
7649
7650 relend = relstart + sec->reloc_count;
435edf0b 7651 for (rel = relstart; rel < relend; rel++)
3e04d765
AM
7652 {
7653 enum elf_ppc64_reloc_type r_type;
7654 unsigned long r_symndx;
7655 asection *sym_sec;
7656 struct elf_link_hash_entry *h;
7657 Elf_Internal_Sym *sym;
7658 unsigned char *tls_maskp;
7659
7660 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7661 if (r_type != R_PPC64_PLTCALL
7662 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7663 continue;
7664
7665 r_symndx = ELF64_R_SYM (rel->r_info);
7666 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7667 r_symndx, ibfd))
7668 {
7669 if (elf_section_data (sec)->relocs != relstart)
7670 free (relstart);
c9594989 7671 if (symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765
AM
7672 free (local_syms);
7673 return FALSE;
7674 }
7675
7676 if (sym_sec != NULL && sym_sec->output_section != NULL)
7677 {
7678 bfd_vma from, to;
7679 if (h != NULL)
7680 to = h->root.u.def.value;
7681 else
7682 to = sym->st_value;
7683 to += (rel->r_addend
7684 + sym_sec->output_offset
7685 + sym_sec->output_section->vma);
7686 from = (rel->r_offset
7687 + sec->output_offset
7688 + sec->output_section->vma);
5663e321
AM
7689 if (to - from + limit < 2 * limit
7690 && !(r_type == R_PPC64_PLTCALL_NOTOC
7691 && (((h ? h->other : sym->st_other)
7692 & STO_PPC64_LOCAL_MASK)
4a4e7361 7693 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7694 *tls_maskp &= ~PLT_KEEP;
7695 }
7696 }
7697 if (elf_section_data (sec)->relocs != relstart)
7698 free (relstart);
7699 }
7700
7701 if (local_syms != NULL
7702 && symtab_hdr->contents != (unsigned char *) local_syms)
7703 {
7704 if (!info->keep_memory)
7705 free (local_syms);
7706 else
7707 symtab_hdr->contents = (unsigned char *) local_syms;
7708 }
7709 }
7710
7711 return TRUE;
7712}
7713
c9405344
AM
7714/* Set htab->tls_get_addr and various other info specific to TLS.
7715 This needs to run before dynamic symbols are processed in
7716 bfd_elf_size_dynamic_sections. */
411e1bfb 7717
c9405344 7718bfd_boolean
e7d1c40c 7719ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7720{
411e1bfb 7721 struct ppc_link_hash_table *htab;
9e7028aa 7722 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
411e1bfb 7723
411e1bfb 7724 htab = ppc_hash_table (info);
4dfe6ac6 7725 if (htab == NULL)
c9405344 7726 return FALSE;
4dfe6ac6 7727
ee67d69a
AM
7728 if (abiversion (info->output_bfd) == 1)
7729 htab->opd_abi = 1;
7730
e7d1c40c 7731 if (htab->params->no_multi_toc)
33c0ec9d
AM
7732 htab->do_multi_toc = 0;
7733 else if (!htab->do_multi_toc)
e7d1c40c 7734 htab->params->no_multi_toc = 1;
33c0ec9d 7735
8b5f1ed8
AM
7736 /* Default to --no-plt-localentry, as this option can cause problems
7737 with symbol interposition. For example, glibc libpthread.so and
7738 libc.so duplicate many pthread symbols, with a fallback
7739 implementation in libc.so. In some cases the fallback does more
7740 work than the pthread implementation. __pthread_condattr_destroy
7741 is one such symbol: the libpthread.so implementation is
7742 localentry:0 while the libc.so implementation is localentry:8.
7743 An app that "cleverly" uses dlopen to only load necessary
7744 libraries at runtime may omit loading libpthread.so when not
7745 running multi-threaded, which then results in the libc.so
7746 fallback symbols being used and ld.so complaining. Now there
7747 are workarounds in ld (see non_zero_localentry) to detect the
7748 pthread situation, but that may not be the only case where
7749 --plt-localentry can cause trouble. */
f378ab09 7750 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7751 htab->params->plt_localentry0 = 0;
3cd7c7d7
AM
7752 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7753 {
7754 /* The issue is that __glink_PLTresolve saves r2, which is done
7755 because glibc ld.so _dl_runtime_resolve restores r2 to support
7756 a glibc plt call optimisation where global entry code is
7757 skipped on calls that resolve to the same binary. The
7758 __glink_PLTresolve save of r2 is incompatible with code
7759 making tail calls, because the tail call might go via the
7760 resolver and thus overwrite the proper saved r2. */
7761 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7762 "power10 pc-relative code"));
7763 htab->params->plt_localentry0 = 0;
7764 }
d44c746a
AM
7765 if (htab->params->plt_localentry0
7766 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7767 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
7768 _bfd_error_handler
7769 (_("warning: --plt-localentry is especially dangerous without "
7770 "ld.so support to detect ABI violations"));
f378ab09 7771
9e7028aa
AM
7772 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7773 FALSE, FALSE, TRUE);
7774 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7775
a7f2871e 7776 /* Move dynamic linking info to the function descriptor sym. */
9e7028aa
AM
7777 if (tga != NULL)
7778 func_desc_adjust (tga, info);
7779 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7780 FALSE, FALSE, TRUE);
7781 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7782
7783 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
7784 FALSE, FALSE, TRUE);
7785 htab->tga_desc = ppc_elf_hash_entry (desc);
7786 if (desc != NULL)
7787 func_desc_adjust (desc, info);
7788 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
7789 FALSE, FALSE, TRUE);
7790 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7791
7c9cf415 7792 if (htab->params->tls_get_addr_opt)
a7f2871e 7793 {
9e7028aa 7794 struct elf_link_hash_entry *opt, *opt_fd;
a7f2871e
AM
7795
7796 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7797 FALSE, FALSE, TRUE);
7798 if (opt != NULL)
7799 func_desc_adjust (opt, info);
7800 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7801 FALSE, FALSE, TRUE);
7802 if (opt_fd != NULL
7803 && (opt_fd->root.type == bfd_link_hash_defined
7804 || opt_fd->root.type == bfd_link_hash_defweak))
7805 {
7806 /* If glibc supports an optimized __tls_get_addr call stub,
7807 signalled by the presence of __tls_get_addr_opt, and we'll
7808 be calling __tls_get_addr via a plt call stub, then
7809 make __tls_get_addr point to __tls_get_addr_opt. */
9e7028aa
AM
7810 if (!(htab->elf.dynamic_sections_created
7811 && tga_fd != NULL
7812 && (tga_fd->type == STT_FUNC
7813 || tga_fd->needs_plt)
7814 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7815 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
7816 tga_fd = NULL;
7817 if (!(htab->elf.dynamic_sections_created
7818 && desc_fd != NULL
7819 && (desc_fd->type == STT_FUNC
7820 || desc_fd->needs_plt)
7821 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
7822 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
7823 desc_fd = NULL;
7824
7825 if (tga_fd != NULL || desc_fd != NULL)
7826 {
7827 struct plt_entry *ent = NULL;
7828
7829 if (tga_fd != NULL)
7830 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7831 if (ent->plt.refcount > 0)
7832 break;
7833 if (ent == NULL && desc_fd != NULL)
7834 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
7835 if (ent->plt.refcount > 0)
7836 break;
a7f2871e
AM
7837 if (ent != NULL)
7838 {
9e7028aa
AM
7839 if (tga_fd != NULL)
7840 {
7841 tga_fd->root.type = bfd_link_hash_indirect;
7842 tga_fd->root.u.i.link = &opt_fd->root;
7843 tga_fd->root.u.i.warning = NULL;
7844 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7845 }
7846 if (desc_fd != NULL)
7847 {
7848 desc_fd->root.type = bfd_link_hash_indirect;
7849 desc_fd->root.u.i.link = &opt_fd->root;
7850 desc_fd->root.u.i.warning = NULL;
7851 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
7852 }
b531344c 7853 opt_fd->mark = 1;
a7f2871e
AM
7854 if (opt_fd->dynindx != -1)
7855 {
7856 /* Use __tls_get_addr_opt in dynamic relocations. */
7857 opt_fd->dynindx = -1;
7858 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7859 opt_fd->dynstr_index);
7860 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
c9405344 7861 return FALSE;
a7f2871e 7862 }
9e7028aa 7863 if (tga_fd != NULL)
a7f2871e 7864 {
9e7028aa 7865 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
30845f11 7866 tga = elf_hash_entry (htab->tls_get_addr);
9e7028aa
AM
7867 if (opt != NULL && tga != NULL)
7868 {
7869 tga->root.type = bfd_link_hash_indirect;
7870 tga->root.u.i.link = &opt->root;
7871 tga->root.u.i.warning = NULL;
7872 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7873 opt->mark = 1;
7874 _bfd_elf_link_hash_hide_symbol (info, opt,
7875 tga->forced_local);
7876 htab->tls_get_addr = ppc_elf_hash_entry (opt);
7877 }
7878 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7879 htab->tls_get_addr_fd->is_func_descriptor = 1;
7880 if (htab->tls_get_addr != NULL)
7881 {
7882 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7883 htab->tls_get_addr->is_func = 1;
7884 }
a7f2871e 7885 }
9e7028aa 7886 if (desc_fd != NULL)
a7f2871e 7887 {
9e7028aa
AM
7888 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
7889 if (opt != NULL && desc != NULL)
7890 {
7891 desc->root.type = bfd_link_hash_indirect;
7892 desc->root.u.i.link = &opt->root;
7893 desc->root.u.i.warning = NULL;
7894 ppc64_elf_copy_indirect_symbol (info, opt, desc);
7895 opt->mark = 1;
7896 _bfd_elf_link_hash_hide_symbol (info, opt,
7897 desc->forced_local);
7898 htab->tga_desc = ppc_elf_hash_entry (opt);
7899 }
7900 htab->tga_desc_fd->oh = htab->tga_desc;
7901 htab->tga_desc_fd->is_func_descriptor = 1;
7902 if (htab->tga_desc != NULL)
7903 {
7904 htab->tga_desc->oh = htab->tga_desc_fd;
7905 htab->tga_desc->is_func = 1;
7906 }
a7f2871e
AM
7907 }
7908 }
7909 }
7910 }
7c9cf415
AM
7911 else if (htab->params->tls_get_addr_opt < 0)
7912 htab->params->tls_get_addr_opt = 0;
a7f2871e 7913 }
9e7028aa
AM
7914
7915 if (htab->tga_desc_fd != NULL
7916 && htab->params->tls_get_addr_opt
7917 && htab->params->no_tls_get_addr_regsave == -1)
7918 htab->params->no_tls_get_addr_regsave = 0;
7919
c9405344 7920 return TRUE;
3a71aa26 7921}
8387904d 7922
3a71aa26 7923/* Return TRUE iff REL is a branch reloc with a global symbol matching
9e7028aa 7924 any of HASH1, HASH2, HASH3, or HASH4. */
8387904d 7925
3a71aa26 7926static bfd_boolean
30845f11
AM
7927branch_reloc_hash_match (bfd *ibfd,
7928 Elf_Internal_Rela *rel,
7929 struct ppc_link_hash_entry *hash1,
7930 struct ppc_link_hash_entry *hash2,
7931 struct ppc_link_hash_entry *hash3,
7932 struct ppc_link_hash_entry *hash4)
3a71aa26
AM
7933{
7934 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7935 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7936 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7937
e054468f 7938 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7939 {
3a71aa26
AM
7940 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7941 struct elf_link_hash_entry *h;
8387904d 7942
3a71aa26 7943 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7944 h = elf_follow_link (h);
30845f11
AM
7945 if (h == elf_hash_entry (hash1)
7946 || h == elf_hash_entry (hash2)
7947 || h == elf_hash_entry (hash3)
7948 || h == elf_hash_entry (hash4))
3a71aa26 7949 return TRUE;
a48ebf4d 7950 }
3a71aa26 7951 return FALSE;
951fd09b 7952}
411e1bfb 7953
951fd09b
AM
7954/* Run through all the TLS relocs looking for optimization
7955 opportunities. The linker has been hacked (see ppc64elf.em) to do
7956 a preliminary section layout so that we know the TLS segment
7957 offsets. We can't optimize earlier because some optimizations need
7958 to know the tp offset, and we need to optimize before allocating
7959 dynamic relocations. */
7960
7961bfd_boolean
33c0ec9d 7962ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7963{
7964 bfd *ibfd;
7965 asection *sec;
7966 struct ppc_link_hash_table *htab;
663a1470 7967 unsigned char *toc_ref;
102890f0 7968 int pass;
951fd09b 7969
3cbc1e5e 7970 if (!bfd_link_executable (info))
411e1bfb
AM
7971 return TRUE;
7972
951fd09b 7973 htab = ppc_hash_table (info);
4dfe6ac6
NC
7974 if (htab == NULL)
7975 return FALSE;
7976
252dcdf4
AM
7977 htab->do_tls_opt = 1;
7978
663a1470
AM
7979 /* Make two passes over the relocs. On the first pass, mark toc
7980 entries involved with tls relocs, and check that tls relocs
7981 involved in setting up a tls_get_addr call are indeed followed by
7982 such a call. If they are not, we can't do any tls optimization.
7983 On the second pass twiddle tls_mask flags to notify
7984 relocate_section that optimization can be done, and adjust got
7985 and plt refcounts. */
7986 toc_ref = NULL;
7987 for (pass = 0; pass < 2; ++pass)
c72f2fb2 7988 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
7989 {
7990 Elf_Internal_Sym *locsyms = NULL;
7991 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7992
102890f0
AM
7993 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7994 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7995 {
7996 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7997 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7998
102890f0
AM
7999 /* Read the relocations. */
8000 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8001 info->keep_memory);
8002 if (relstart == NULL)
2915c55b
JK
8003 {
8004 free (toc_ref);
8005 return FALSE;
8006 }
411e1bfb 8007
102890f0
AM
8008 relend = relstart + sec->reloc_count;
8009 for (rel = relstart; rel < relend; rel++)
8010 {
8011 enum elf_ppc64_reloc_type r_type;
8012 unsigned long r_symndx;
8013 struct elf_link_hash_entry *h;
8014 Elf_Internal_Sym *sym;
8015 asection *sym_sec;
f961d9dd 8016 unsigned char *tls_mask;
46e9995a 8017 unsigned int tls_set, tls_clear, tls_type = 0;
102890f0
AM
8018 bfd_vma value;
8019 bfd_boolean ok_tprel, is_local;
8020 long toc_ref_index = 0;
8021 int expecting_tls_get_addr = 0;
663a1470 8022 bfd_boolean ret = FALSE;
411e1bfb 8023
102890f0
AM
8024 r_symndx = ELF64_R_SYM (rel->r_info);
8025 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8026 r_symndx, ibfd))
8027 {
8028 err_free_rel:
8029 if (elf_section_data (sec)->relocs != relstart)
8030 free (relstart);
c9594989
AM
8031 free (toc_ref);
8032 if (elf_symtab_hdr (ibfd).contents
8033 != (unsigned char *) locsyms)
102890f0 8034 free (locsyms);
663a1470 8035 return ret;
102890f0 8036 }
411e1bfb 8037
102890f0
AM
8038 if (h != NULL)
8039 {
766bc656
AM
8040 if (h->root.type == bfd_link_hash_defined
8041 || h->root.type == bfd_link_hash_defweak)
8042 value = h->root.u.def.value;
8043 else if (h->root.type == bfd_link_hash_undefweak)
8044 value = 0;
8045 else
663a1470
AM
8046 {
8047 found_tls_get_addr_arg = 0;
8048 continue;
8049 }
102890f0
AM
8050 }
8051 else
8052 /* Symbols referenced by TLS relocs must be of type
8053 STT_TLS. So no need for .opd local sym adjust. */
8054 value = sym->st_value;
8055
8056 ok_tprel = FALSE;
f749f26e
AM
8057 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8058 if (is_local)
102890f0 8059 {
766bc656
AM
8060 if (h != NULL
8061 && h->root.type == bfd_link_hash_undefweak)
8062 ok_tprel = TRUE;
c27b8c2a
AM
8063 else if (sym_sec != NULL
8064 && sym_sec->output_section != NULL)
766bc656
AM
8065 {
8066 value += sym_sec->output_offset;
8067 value += sym_sec->output_section->vma;
0b147428 8068 value -= htab->elf.tls_sec->vma + TP_OFFSET;
c213164a
AM
8069 /* Note that even though the prefix insns
8070 allow a 1<<33 offset we use the same test
8071 as for addis;addi. There may be a mix of
8072 pcrel and non-pcrel code and the decision
8073 to optimise is per symbol, not per TLS
8074 sequence. */
0b147428 8075 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
766bc656 8076 }
102890f0 8077 }
951fd09b 8078
102890f0 8079 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8080 /* If this section has old-style __tls_get_addr calls
8081 without marker relocs, then check that each
8082 __tls_get_addr call reloc is preceded by a reloc
8083 that conceivably belongs to the __tls_get_addr arg
8084 setup insn. If we don't find matching arg setup
8085 relocs, don't do any tls optimization. */
8086 if (pass == 0
9737e8af 8087 && sec->nomark_tls_get_addr
663a1470 8088 && h != NULL
ed7007c1 8089 && is_tls_get_addr (h, htab)
663a1470
AM
8090 && !found_tls_get_addr_arg
8091 && is_branch_reloc (r_type))
8092 {
25f53a85 8093 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8094 "TLS optimization disabled\n"),
8095 ibfd, sec, rel->r_offset);
8096 ret = TRUE;
8097 goto err_free_rel;
8098 }
8099
8100 found_tls_get_addr_arg = 0;
102890f0
AM
8101 switch (r_type)
8102 {
8103 case R_PPC64_GOT_TLSLD16:
8104 case R_PPC64_GOT_TLSLD16_LO:
87c69f97 8105 case R_PPC64_GOT_TLSLD_PCREL34:
102890f0 8106 expecting_tls_get_addr = 1;
663a1470 8107 found_tls_get_addr_arg = 1;
1a0670f3 8108 /* Fall through. */
102890f0
AM
8109
8110 case R_PPC64_GOT_TLSLD16_HI:
8111 case R_PPC64_GOT_TLSLD16_HA:
8112 /* These relocs should never be against a symbol
8113 defined in a shared lib. Leave them alone if
8114 that turns out to be the case. */
8115 if (!is_local)
8116 continue;
411e1bfb 8117
102890f0 8118 /* LD -> LE */
411e1bfb 8119 tls_set = 0;
102890f0
AM
8120 tls_clear = TLS_LD;
8121 tls_type = TLS_TLS | TLS_LD;
8122 break;
411e1bfb 8123
102890f0
AM
8124 case R_PPC64_GOT_TLSGD16:
8125 case R_PPC64_GOT_TLSGD16_LO:
87c69f97 8126 case R_PPC64_GOT_TLSGD_PCREL34:
102890f0 8127 expecting_tls_get_addr = 1;
663a1470 8128 found_tls_get_addr_arg = 1;
1a0670f3 8129 /* Fall through. */
102890f0
AM
8130
8131 case R_PPC64_GOT_TLSGD16_HI:
8132 case R_PPC64_GOT_TLSGD16_HA:
8133 if (ok_tprel)
8134 /* GD -> LE */
411e1bfb 8135 tls_set = 0;
102890f0
AM
8136 else
8137 /* GD -> IE */
b00a0a86 8138 tls_set = TLS_TLS | TLS_GDIE;
102890f0
AM
8139 tls_clear = TLS_GD;
8140 tls_type = TLS_TLS | TLS_GD;
8141 break;
8142
87c69f97 8143 case R_PPC64_GOT_TPREL_PCREL34:
102890f0
AM
8144 case R_PPC64_GOT_TPREL16_DS:
8145 case R_PPC64_GOT_TPREL16_LO_DS:
8146 case R_PPC64_GOT_TPREL16_HI:
8147 case R_PPC64_GOT_TPREL16_HA:
8148 if (ok_tprel)
8149 {
8150 /* IE -> LE */
8151 tls_set = 0;
8152 tls_clear = TLS_TPREL;
8153 tls_type = TLS_TLS | TLS_TPREL;
8154 break;
8155 }
411e1bfb
AM
8156 continue;
8157
727fc41e 8158 case R_PPC64_TLSLD:
7d04a20a
AM
8159 if (!is_local)
8160 continue;
8161 /* Fall through. */
8162 case R_PPC64_TLSGD:
23cedd1d
AM
8163 if (rel + 1 < relend
8164 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8165 {
8166 if (pass != 0
2cdcc330 8167 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
8168 != R_PPC64_PLTSEQ)
8169 && (ELF64_R_TYPE (rel[1].r_info)
8170 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
8171 {
8172 r_symndx = ELF64_R_SYM (rel[1].r_info);
8173 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 8174 r_symndx, ibfd))
23cedd1d
AM
8175 goto err_free_rel;
8176 if (h != NULL)
8177 {
8178 struct plt_entry *ent = NULL;
8179
8180 for (ent = h->plt.plist;
8181 ent != NULL;
8182 ent = ent->next)
8183 if (ent->addend == rel[1].r_addend)
8184 break;
8185
8186 if (ent != NULL
8187 && ent->plt.refcount > 0)
8188 ent->plt.refcount -= 1;
8189 }
8190 }
8191 continue;
8192 }
663a1470 8193 found_tls_get_addr_arg = 1;
1a0670f3 8194 /* Fall through. */
663a1470
AM
8195
8196 case R_PPC64_TLS:
8197 case R_PPC64_TOC16:
8198 case R_PPC64_TOC16_LO:
102890f0
AM
8199 if (sym_sec == NULL || sym_sec != toc)
8200 continue;
8201
8202 /* Mark this toc entry as referenced by a TLS
8203 code sequence. We can do that now in the
8204 case of R_PPC64_TLS, and after checking for
8205 tls_get_addr for the TOC16 relocs. */
8206 if (toc_ref == NULL)
2cdcc330
AM
8207 toc_ref
8208 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
8209 if (toc_ref == NULL)
8210 goto err_free_rel;
8211
102890f0
AM
8212 if (h != NULL)
8213 value = h->root.u.def.value;
8214 else
8215 value = sym->st_value;
8216 value += rel->r_addend;
73242275
AM
8217 if (value % 8 != 0)
8218 continue;
8219 BFD_ASSERT (value < toc->size
8220 && toc->output_offset % 8 == 0);
663a1470 8221 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8222 if (r_type == R_PPC64_TLS
8223 || r_type == R_PPC64_TLSGD
8224 || r_type == R_PPC64_TLSLD)
102890f0
AM
8225 {
8226 toc_ref[toc_ref_index] = 1;
8227 continue;
8228 }
8229
8230 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8231 continue;
8232
8233 tls_set = 0;
8234 tls_clear = 0;
8235 expecting_tls_get_addr = 2;
8236 break;
8237
8238 case R_PPC64_TPREL64:
8239 if (pass == 0
8240 || sec != toc
8241 || toc_ref == NULL
663a1470 8242 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8243 continue;
8244 if (ok_tprel)
8245 {
8246 /* IE -> LE */
8247 tls_set = TLS_EXPLICIT;
8248 tls_clear = TLS_TPREL;
8249 break;
8250 }
8251 continue;
8252
8253 case R_PPC64_DTPMOD64:
8254 if (pass == 0
8255 || sec != toc
8256 || toc_ref == NULL
663a1470 8257 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8258 continue;
8259 if (rel + 1 < relend
8260 && (rel[1].r_info
8261 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8262 && rel[1].r_offset == rel->r_offset + 8)
8263 {
8264 if (ok_tprel)
8265 /* GD -> LE */
8266 tls_set = TLS_EXPLICIT | TLS_GD;
8267 else
8268 /* GD -> IE */
b00a0a86 8269 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
102890f0
AM
8270 tls_clear = TLS_GD;
8271 }
8272 else
8273 {
8274 if (!is_local)
8275 continue;
8276
8277 /* LD -> LE */
8278 tls_set = TLS_EXPLICIT;
8279 tls_clear = TLS_LD;
8280 }
8281 break;
8282
252dcdf4
AM
8283 case R_PPC64_TPREL16_HA:
8284 if (pass == 0)
8285 {
8286 unsigned char buf[4];
8287 unsigned int insn;
8288 bfd_vma off = rel->r_offset & ~3;
8289 if (!bfd_get_section_contents (ibfd, sec, buf,
8290 off, 4))
8291 goto err_free_rel;
8292 insn = bfd_get_32 (ibfd, buf);
8293 /* addis rt,13,imm */
8294 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8295 != ((15u << 26) | (13 << 16)))
8296 {
8297 /* xgettext:c-format */
8298 info->callbacks->minfo
8299 (_("%H: warning: %s unexpected insn %#x.\n"),
8300 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8301 htab->do_tls_opt = 0;
8302 }
8303 }
8304 continue;
8305
8306 case R_PPC64_TPREL16_HI:
8307 case R_PPC64_TPREL16_HIGH:
8308 case R_PPC64_TPREL16_HIGHA:
8309 case R_PPC64_TPREL16_HIGHER:
8310 case R_PPC64_TPREL16_HIGHERA:
8311 case R_PPC64_TPREL16_HIGHEST:
8312 case R_PPC64_TPREL16_HIGHESTA:
8313 /* These can all be used in sequences along with
8314 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8315 able to verify easily. */
8316 htab->do_tls_opt = 0;
8317 continue;
8318
102890f0
AM
8319 default:
8320 continue;
8321 }
8322
8323 if (pass == 0)
8324 {
727fc41e 8325 if (!expecting_tls_get_addr
9737e8af 8326 || !sec->nomark_tls_get_addr)
102890f0
AM
8327 continue;
8328
3a71aa26
AM
8329 if (rel + 1 < relend
8330 && branch_reloc_hash_match (ibfd, rel + 1,
9e7028aa
AM
8331 htab->tls_get_addr_fd,
8332 htab->tga_desc_fd,
3a71aa26 8333 htab->tls_get_addr,
9e7028aa 8334 htab->tga_desc))
102890f0 8335 {
3a71aa26 8336 if (expecting_tls_get_addr == 2)
102890f0 8337 {
3a71aa26 8338 /* Check for toc tls entries. */
f961d9dd 8339 unsigned char *toc_tls;
3a71aa26
AM
8340 int retval;
8341
8342 retval = get_tls_mask (&toc_tls, NULL, NULL,
8343 &locsyms,
8344 rel, ibfd);
8345 if (retval == 0)
8346 goto err_free_rel;
663a1470
AM
8347 if (toc_tls != NULL)
8348 {
37da22e5
AM
8349 if ((*toc_tls & TLS_TLS) != 0
8350 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8351 found_tls_get_addr_arg = 1;
8352 if (retval > 1)
8353 toc_ref[toc_ref_index] = 1;
8354 }
102890f0 8355 }
3a71aa26 8356 continue;
102890f0
AM
8357 }
8358
102890f0
AM
8359 /* Uh oh, we didn't find the expected call. We
8360 could just mark this symbol to exclude it
8361 from tls optimization but it's safer to skip
663a1470 8362 the entire optimization. */
695344c0 8363 /* xgettext:c-format */
25f53a85 8364 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8365 "TLS optimization disabled\n"),
8366 ibfd, sec, rel->r_offset);
8367 ret = TRUE;
8368 goto err_free_rel;
102890f0
AM
8369 }
8370
37da22e5
AM
8371 /* If we don't have old-style __tls_get_addr calls
8372 without TLSGD/TLSLD marker relocs, and we haven't
8373 found a new-style __tls_get_addr call with a
8374 marker for this symbol, then we either have a
8375 broken object file or an -mlongcall style
8376 indirect call to __tls_get_addr without a marker.
8377 Disable optimization in this case. */
8378 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8379 && (tls_set & TLS_EXPLICIT) == 0
9737e8af 8380 && !sec->nomark_tls_get_addr
37da22e5
AM
8381 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8382 != (TLS_TLS | TLS_MARK)))
8383 continue;
8384
7d04a20a 8385 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
102890f0 8386 {
23cedd1d
AM
8387 struct plt_entry *ent = NULL;
8388
9e7028aa
AM
8389 if (htab->tls_get_addr_fd != NULL)
8390 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8391 ent != NULL;
8392 ent = ent->next)
8393 if (ent->addend == 0)
8394 break;
8395
8396 if (ent == NULL && htab->tga_desc_fd != NULL)
8397 for (ent = htab->tga_desc_fd->elf.plt.plist;
8398 ent != NULL;
8399 ent = ent->next)
8400 if (ent->addend == 0)
8401 break;
8402
8403 if (ent == NULL && htab->tls_get_addr != NULL)
23cedd1d
AM
8404 for (ent = htab->tls_get_addr->elf.plt.plist;
8405 ent != NULL;
8406 ent = ent->next)
8407 if (ent->addend == 0)
102890f0 8408 break;
411e1bfb 8409
9e7028aa
AM
8410 if (ent == NULL && htab->tga_desc != NULL)
8411 for (ent = htab->tga_desc->elf.plt.plist;
23cedd1d
AM
8412 ent != NULL;
8413 ent = ent->next)
8414 if (ent->addend == 0)
102890f0 8415 break;
23cedd1d
AM
8416
8417 if (ent != NULL
8418 && ent->plt.refcount > 0)
8419 ent->plt.refcount -= 1;
102890f0 8420 }
411e1bfb 8421
102890f0 8422 if (tls_clear == 0)
30038c59
AM
8423 continue;
8424
102890f0
AM
8425 if ((tls_set & TLS_EXPLICIT) == 0)
8426 {
8427 struct got_entry *ent;
411e1bfb 8428
102890f0
AM
8429 /* Adjust got entry for this reloc. */
8430 if (h != NULL)
8431 ent = h->got.glist;
8432 else
8433 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8434
102890f0
AM
8435 for (; ent != NULL; ent = ent->next)
8436 if (ent->addend == rel->r_addend
8437 && ent->owner == ibfd
8438 && ent->tls_type == tls_type)
8439 break;
8440 if (ent == NULL)
8441 abort ();
411e1bfb 8442
102890f0
AM
8443 if (tls_set == 0)
8444 {
8445 /* We managed to get rid of a got entry. */
8446 if (ent->got.refcount > 0)
8447 ent->got.refcount -= 1;
8448 }
8449 }
8450 else
8451 {
8452 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8453 we'll lose one or two dyn relocs. */
8454 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8455 NULL, h, sym))
102890f0 8456 return FALSE;
411e1bfb 8457
102890f0
AM
8458 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8459 {
8460 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8461 NULL, h, sym))
102890f0
AM
8462 return FALSE;
8463 }
8464 }
411e1bfb 8465
46e9995a 8466 *tls_mask |= tls_set & 0xff;
102890f0
AM
8467 *tls_mask &= ~tls_clear;
8468 }
8c1d1bb8 8469
102890f0
AM
8470 if (elf_section_data (sec)->relocs != relstart)
8471 free (relstart);
8472 }
411e1bfb 8473
663a1470
AM
8474 if (locsyms != NULL
8475 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8476 {
8477 if (!info->keep_memory)
8478 free (locsyms);
8479 else
8480 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8481 }
8482 }
411e1bfb 8483
c9594989 8484 free (toc_ref);
b34976b6 8485 return TRUE;
1e2f5b6e 8486}
b34976b6 8487
c5614fa4
AM
8488/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8489 the values of any global symbols in a toc section that has been
8490 edited. Globals in toc sections should be a rarity, so this function
8491 sets a flag if any are found in toc sections other than the one just
de194d85 8492 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8493
8494struct adjust_toc_info
8495{
8496 asection *toc;
8497 unsigned long *skip;
8498 bfd_boolean global_toc_syms;
8499};
8500
ba761f19
AM
8501enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8502
c5614fa4
AM
8503static bfd_boolean
8504adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8505{
8506 struct ppc_link_hash_entry *eh;
8507 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8508 unsigned long i;
c5614fa4 8509
c5614fa4
AM
8510 if (h->root.type != bfd_link_hash_defined
8511 && h->root.type != bfd_link_hash_defweak)
8512 return TRUE;
8513
ed7007c1 8514 eh = ppc_elf_hash_entry (h);
c5614fa4
AM
8515 if (eh->adjust_done)
8516 return TRUE;
8517
8518 if (eh->elf.root.u.def.section == toc_inf->toc)
8519 {
854b41e7
AM
8520 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8521 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8522 else
854b41e7
AM
8523 i = eh->elf.root.u.def.value >> 3;
8524
ba761f19 8525 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8526 {
4eca0228 8527 _bfd_error_handler
854b41e7
AM
8528 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8529 do
8530 ++i;
ba761f19 8531 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8532 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8533 }
854b41e7
AM
8534
8535 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8536 eh->adjust_done = 1;
8537 }
8538 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8539 toc_inf->global_toc_syms = TRUE;
8540
8541 return TRUE;
8542}
8543
39eeab25
AM
8544/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8545 on a _LO variety toc/got reloc. */
560c8763
AM
8546
8547static bfd_boolean
39eeab25 8548ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8549{
2365f8d7
AM
8550 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8551 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8552 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8553 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8554 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8555 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8556 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8557 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8558 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8559 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8560 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8561 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8562 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8563 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8564 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8565 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8566 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
39eeab25
AM
8567 /* Exclude lfqu by testing reloc. If relocs are ever
8568 defined for the reduced D field in psq_lu then those
8569 will need testing too. */
8570 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8571 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
39eeab25 8572 && (insn & 1) == 0)
2365f8d7
AM
8573 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8574 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
39eeab25
AM
8575 /* Exclude stfqu. psq_stu as above for psq_lu. */
8576 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8577 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
39eeab25 8578 && (insn & 1) == 0));
560c8763
AM
8579}
8580
4a421c53
AM
8581/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8582 pld ra,symbol@got@pcrel
dd9b12c2 8583 load/store rt,off(ra)
4a421c53 8584 or
d4b87b1e 8585 pla ra,symbol@pcrel
dd9b12c2 8586 load/store rt,off(ra)
4a421c53 8587 may be translated to
dd9b12c2 8588 pload/pstore rt,symbol+off@pcrel
4a421c53
AM
8589 nop.
8590 This function returns true if the optimization is possible, placing
dd9b12c2 8591 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
4a421c53
AM
8592
8593 On entry to this function, the linker has already determined that
d4b87b1e 8594 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8595 while *PINSN2 is the second instruction. */
8596
8597static bfd_boolean
dd9b12c2 8598xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
4a421c53 8599{
77486630
AM
8600 uint64_t insn1 = *pinsn1;
8601 uint64_t insn2 = *pinsn2;
dd9b12c2 8602 bfd_signed_vma off;
4a421c53 8603
77486630
AM
8604 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8605 {
8606 /* Check that regs match. */
8607 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8608 return FALSE;
8609
8610 /* P8LS or PMLS form, non-pcrel. */
8611 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8612 return FALSE;
8613
8614 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8615 *pinsn2 = PNOP;
8616 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8617 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8618 return TRUE;
8619 }
8620
8621 insn2 >>= 32;
8622
4a421c53 8623 /* Check that regs match. */
77486630 8624 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
4a421c53
AM
8625 return FALSE;
8626
8627 switch ((insn2 >> 26) & 63)
8628 {
8629 default:
8630 return FALSE;
8631
8632 case 32: /* lwz */
8633 case 34: /* lbz */
8634 case 36: /* stw */
8635 case 38: /* stb */
8636 case 40: /* lhz */
8637 case 42: /* lha */
8638 case 44: /* sth */
8639 case 48: /* lfs */
8640 case 50: /* lfd */
8641 case 52: /* stfs */
8642 case 54: /* stfd */
8643 /* These are the PMLS cases, where we just need to tack a prefix
dd9b12c2 8644 on the insn. */
77486630 8645 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
4a421c53 8646 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8647 off = insn2 & 0xffff;
4a421c53
AM
8648 break;
8649
8650 case 58: /* lwa, ld */
dd9b12c2 8651 if ((insn2 & 1) != 0)
4a421c53 8652 return FALSE;
77486630 8653 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8654 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8655 | (insn2 & (31ULL << 21)));
dd9b12c2 8656 off = insn2 & 0xfffc;
4a421c53
AM
8657 break;
8658
8659 case 57: /* lxsd, lxssp */
dd9b12c2 8660 if ((insn2 & 3) < 2)
4a421c53 8661 return FALSE;
77486630 8662 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8663 | ((40ULL | (insn2 & 3)) << 26)
8664 | (insn2 & (31ULL << 21)));
dd9b12c2 8665 off = insn2 & 0xfffc;
4a421c53
AM
8666 break;
8667
8668 case 61: /* stxsd, stxssp, lxv, stxv */
8669 if ((insn2 & 3) == 0)
8670 return FALSE;
8671 else if ((insn2 & 3) >= 2)
8672 {
77486630 8673 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8674 | ((44ULL | (insn2 & 3)) << 26)
8675 | (insn2 & (31ULL << 21)));
dd9b12c2 8676 off = insn2 & 0xfffc;
4a421c53
AM
8677 }
8678 else
8679 {
77486630 8680 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8681 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8682 | (insn2 & (31ULL << 21)));
dd9b12c2 8683 off = insn2 & 0xfff0;
4a421c53
AM
8684 }
8685 break;
8686
8687 case 56: /* lq */
77486630 8688 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53 8689 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8690 off = insn2 & 0xffff;
4a421c53
AM
8691 break;
8692
94ba9882
AM
8693 case 6: /* lxvp, stxvp */
8694 if ((insn2 & 0xe) != 0)
8695 return FALSE;
8696 insn1 = ((1ULL << 58) | (1ULL << 52)
8697 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8698 | (insn2 & (31ULL << 21)));
8699 off = insn2 & 0xfff0;
8700 break;
8701
4a421c53 8702 case 62: /* std, stq */
dd9b12c2 8703 if ((insn2 & 1) != 0)
4a421c53 8704 return FALSE;
77486630 8705 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8706 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8707 | (insn2 & (31ULL << 21)));
dd9b12c2 8708 off = insn2 & 0xfffc;
4a421c53
AM
8709 break;
8710 }
8711
77486630 8712 *pinsn1 = insn1;
4a421c53 8713 *pinsn2 = (uint64_t) NOP << 32;
dd9b12c2 8714 *poff = (off ^ 0x8000) - 0x8000;
4a421c53
AM
8715 return TRUE;
8716}
8717
c5614fa4
AM
8718/* Examine all relocs referencing .toc sections in order to remove
8719 unused .toc entries. */
8720
8721bfd_boolean
33c0ec9d 8722ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8723{
8724 bfd *ibfd;
8725 struct adjust_toc_info toc_inf;
67f0cbdb 8726 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8727
67f0cbdb 8728 htab->do_toc_opt = 1;
c5614fa4 8729 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8730 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8731 {
8732 asection *toc, *sec;
8733 Elf_Internal_Shdr *symtab_hdr;
8734 Elf_Internal_Sym *local_syms;
425b145b 8735 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8736 unsigned long *skip, *drop;
8737 unsigned char *used;
8738 unsigned char *keep, last, some_unused;
8739
854b41e7
AM
8740 if (!is_ppc64_elf (ibfd))
8741 continue;
8742
c5614fa4
AM
8743 toc = bfd_get_section_by_name (ibfd, ".toc");
8744 if (toc == NULL
92b7a70f 8745 || toc->size == 0
dbaa2011
AM
8746 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8747 || discarded_section (toc))
c5614fa4
AM
8748 continue;
8749
425b145b 8750 toc_relocs = NULL;
c5614fa4 8751 local_syms = NULL;
0ffa91dd 8752 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8753
8754 /* Look at sections dropped from the final link. */
8755 skip = NULL;
8756 relstart = NULL;
8757 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8758 {
8759 if (sec->reloc_count == 0
dbaa2011 8760 || !discarded_section (sec)
c5614fa4
AM
8761 || get_opd_info (sec)
8762 || (sec->flags & SEC_ALLOC) == 0
8763 || (sec->flags & SEC_DEBUGGING) != 0)
8764 continue;
8765
8766 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8767 if (relstart == NULL)
8768 goto error_ret;
8769
8770 /* Run through the relocs to see which toc entries might be
8771 unused. */
8772 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8773 {
8774 enum elf_ppc64_reloc_type r_type;
8775 unsigned long r_symndx;
8776 asection *sym_sec;
8777 struct elf_link_hash_entry *h;
8778 Elf_Internal_Sym *sym;
8779 bfd_vma val;
8780
8781 r_type = ELF64_R_TYPE (rel->r_info);
8782 switch (r_type)
8783 {
8784 default:
8785 continue;
8786
8787 case R_PPC64_TOC16:
8788 case R_PPC64_TOC16_LO:
8789 case R_PPC64_TOC16_HI:
8790 case R_PPC64_TOC16_HA:
8791 case R_PPC64_TOC16_DS:
8792 case R_PPC64_TOC16_LO_DS:
8793 break;
8794 }
8795
8796 r_symndx = ELF64_R_SYM (rel->r_info);
8797 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8798 r_symndx, ibfd))
8799 goto error_ret;
8800
8801 if (sym_sec != toc)
8802 continue;
8803
8804 if (h != NULL)
8805 val = h->root.u.def.value;
8806 else
8807 val = sym->st_value;
8808 val += rel->r_addend;
8809
8810 if (val >= toc->size)
8811 continue;
8812
8813 /* Anything in the toc ought to be aligned to 8 bytes.
8814 If not, don't mark as unused. */
8815 if (val & 7)
8816 continue;
8817
8818 if (skip == NULL)
8819 {
854b41e7 8820 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8821 if (skip == NULL)
8822 goto error_ret;
8823 }
8824
ba761f19 8825 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8826 }
8827
8828 if (elf_section_data (sec)->relocs != relstart)
8829 free (relstart);
8830 }
8831
ba761f19
AM
8832 /* For largetoc loads of address constants, we can convert
8833 . addis rx,2,addr@got@ha
8834 . ld ry,addr@got@l(rx)
8835 to
8836 . addis rx,2,addr@toc@ha
8837 . addi ry,rx,addr@toc@l
8838 when addr is within 2G of the toc pointer. This then means
8839 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8840
ba761f19
AM
8841 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8842 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8843 && toc->reloc_count != 0)
8844 {
8845 /* Read toc relocs. */
425b145b
AM
8846 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8847 info->keep_memory);
8848 if (toc_relocs == NULL)
ba761f19
AM
8849 goto error_ret;
8850
425b145b 8851 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8852 {
8853 enum elf_ppc64_reloc_type r_type;
8854 unsigned long r_symndx;
8855 asection *sym_sec;
8856 struct elf_link_hash_entry *h;
8857 Elf_Internal_Sym *sym;
8858 bfd_vma val, addr;
8859
8860 r_type = ELF64_R_TYPE (rel->r_info);
8861 if (r_type != R_PPC64_ADDR64)
8862 continue;
8863
8864 r_symndx = ELF64_R_SYM (rel->r_info);
8865 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8866 r_symndx, ibfd))
8867 goto error_ret;
8868
425b145b 8869 if (sym_sec == NULL
c27b8c2a 8870 || sym_sec->output_section == NULL
dbaa2011 8871 || discarded_section (sym_sec))
425b145b
AM
8872 continue;
8873
afe397ea 8874 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8875 continue;
8876
8877 if (h != NULL)
bddc25c9
AM
8878 {
8879 if (h->type == STT_GNU_IFUNC)
8880 continue;
8881 val = h->root.u.def.value;
8882 }
ba761f19 8883 else
bddc25c9
AM
8884 {
8885 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8886 continue;
8887 val = sym->st_value;
8888 }
ba761f19
AM
8889 val += rel->r_addend;
8890 val += sym_sec->output_section->vma + sym_sec->output_offset;
8891
8892 /* We don't yet know the exact toc pointer value, but we
8893 know it will be somewhere in the toc section. Don't
8894 optimize if the difference from any possible toc
8895 pointer is outside [ff..f80008000, 7fff7fff]. */
8896 addr = toc->output_section->vma + TOC_BASE_OFF;
8897 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8898 continue;
8899
8900 addr = toc->output_section->vma + toc->output_section->rawsize;
8901 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8902 continue;
8903
8904 if (skip == NULL)
8905 {
8906 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8907 if (skip == NULL)
8908 goto error_ret;
8909 }
8910
8911 skip[rel->r_offset >> 3]
425b145b 8912 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8913 }
ba761f19
AM
8914 }
8915
c5614fa4
AM
8916 if (skip == NULL)
8917 continue;
8918
8919 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8920 if (used == NULL)
8921 {
8922 error_ret:
c9594989 8923 if (symtab_hdr->contents != (unsigned char *) local_syms)
c5614fa4
AM
8924 free (local_syms);
8925 if (sec != NULL
c5614fa4
AM
8926 && elf_section_data (sec)->relocs != relstart)
8927 free (relstart);
c9594989 8928 if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 8929 free (toc_relocs);
c9594989 8930 free (skip);
c5614fa4
AM
8931 return FALSE;
8932 }
8933
30038c59
AM
8934 /* Now check all kept sections that might reference the toc.
8935 Check the toc itself last. */
8936 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8937 : ibfd->sections);
c5614fa4 8938 sec != NULL;
c5614fa4 8939 sec = (sec == toc ? NULL
c5614fa4 8940 : sec->next == NULL ? toc
30038c59 8941 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8942 : sec->next))
8943 {
8944 int repeat;
8945
8946 if (sec->reloc_count == 0
dbaa2011 8947 || discarded_section (sec)
c5614fa4
AM
8948 || get_opd_info (sec)
8949 || (sec->flags & SEC_ALLOC) == 0
8950 || (sec->flags & SEC_DEBUGGING) != 0)
8951 continue;
8952
854b41e7
AM
8953 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8954 info->keep_memory);
c5614fa4 8955 if (relstart == NULL)
2915c55b
JK
8956 {
8957 free (used);
8958 goto error_ret;
8959 }
c5614fa4
AM
8960
8961 /* Mark toc entries referenced as used. */
c5614fa4 8962 do
d4f1ee75
AM
8963 {
8964 repeat = 0;
8965 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8966 {
8967 enum elf_ppc64_reloc_type r_type;
8968 unsigned long r_symndx;
8969 asection *sym_sec;
8970 struct elf_link_hash_entry *h;
8971 Elf_Internal_Sym *sym;
8972 bfd_vma val;
98528052 8973
d4f1ee75 8974 r_type = ELF64_R_TYPE (rel->r_info);
d4f1ee75
AM
8975 switch (r_type)
8976 {
8977 case R_PPC64_TOC16:
8978 case R_PPC64_TOC16_LO:
8979 case R_PPC64_TOC16_HI:
8980 case R_PPC64_TOC16_HA:
8981 case R_PPC64_TOC16_DS:
8982 case R_PPC64_TOC16_LO_DS:
8983 /* In case we're taking addresses of toc entries. */
8984 case R_PPC64_ADDR64:
8985 break;
c5614fa4 8986
d4f1ee75
AM
8987 default:
8988 continue;
8989 }
c5614fa4 8990
d4f1ee75
AM
8991 r_symndx = ELF64_R_SYM (rel->r_info);
8992 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8993 r_symndx, ibfd))
8994 {
8995 free (used);
8996 goto error_ret;
8997 }
c5614fa4 8998
d4f1ee75
AM
8999 if (sym_sec != toc)
9000 continue;
c5614fa4 9001
d4f1ee75
AM
9002 if (h != NULL)
9003 val = h->root.u.def.value;
9004 else
9005 val = sym->st_value;
9006 val += rel->r_addend;
ba761f19 9007
d4f1ee75
AM
9008 if (val >= toc->size)
9009 continue;
ba761f19 9010
d4f1ee75
AM
9011 if ((skip[val >> 3] & can_optimize) != 0)
9012 {
9013 bfd_vma off;
9014 unsigned char opc;
9015
9016 switch (r_type)
9017 {
9018 case R_PPC64_TOC16_HA:
ba761f19 9019 break;
ba761f19 9020
d4f1ee75
AM
9021 case R_PPC64_TOC16_LO_DS:
9022 off = rel->r_offset;
9023 off += (bfd_big_endian (ibfd) ? -2 : 3);
9024 if (!bfd_get_section_contents (ibfd, sec, &opc,
9025 off, 1))
9026 {
9027 free (used);
9028 goto error_ret;
9029 }
9030 if ((opc & (0x3f << 2)) == (58u << 2))
9031 break;
1a0670f3 9032 /* Fall through. */
ba761f19 9033
d4f1ee75
AM
9034 default:
9035 /* Wrong sort of reloc, or not a ld. We may
9036 as well clear ref_from_discarded too. */
9037 skip[val >> 3] = 0;
9038 }
9039 }
9040
9041 if (sec != toc)
9042 used[val >> 3] = 1;
9043 /* For the toc section, we only mark as used if this
9044 entry itself isn't unused. */
9045 else if ((used[rel->r_offset >> 3]
9046 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9047 && !used[val >> 3])
9048 {
9049 /* Do all the relocs again, to catch reference
9050 chains. */
9051 repeat = 1;
9052 used[val >> 3] = 1;
9053 }
9054 }
9055 }
c5614fa4 9056 while (repeat);
854b41e7
AM
9057
9058 if (elf_section_data (sec)->relocs != relstart)
9059 free (relstart);
c5614fa4
AM
9060 }
9061
9062 /* Merge the used and skip arrays. Assume that TOC
9063 doublewords not appearing as either used or unused belong
de194d85 9064 to an entry more than one doubleword in size. */
c5614fa4
AM
9065 for (drop = skip, keep = used, last = 0, some_unused = 0;
9066 drop < skip + (toc->size + 7) / 8;
9067 ++drop, ++keep)
9068 {
9069 if (*keep)
9070 {
ba761f19
AM
9071 *drop &= ~ref_from_discarded;
9072 if ((*drop & can_optimize) != 0)
9073 some_unused = 1;
c5614fa4
AM
9074 last = 0;
9075 }
b140b010 9076 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9077 {
9078 some_unused = 1;
ba761f19 9079 last = ref_from_discarded;
c5614fa4
AM
9080 }
9081 else
9082 *drop = last;
9083 }
9084
9085 free (used);
9086
9087 if (some_unused)
9088 {
9089 bfd_byte *contents, *src;
9090 unsigned long off;
d62b3684 9091 Elf_Internal_Sym *sym;
ba761f19 9092 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9093
9094 /* Shuffle the toc contents, and at the same time convert the
9095 skip array from booleans into offsets. */
9096 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9097 goto error_ret;
9098
9099 elf_section_data (toc)->this_hdr.contents = contents;
9100
9101 for (src = contents, off = 0, drop = skip;
9102 src < contents + toc->size;
9103 src += 8, ++drop)
9104 {
ba761f19
AM
9105 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9106 off += 8;
c5614fa4
AM
9107 else if (off != 0)
9108 {
9109 *drop = off;
9110 memcpy (src - off, src, 8);
9111 }
9112 }
854b41e7 9113 *drop = off;
c5614fa4
AM
9114 toc->rawsize = toc->size;
9115 toc->size = src - contents - off;
9116
ba761f19
AM
9117 /* Adjust addends for relocs against the toc section sym,
9118 and optimize any accesses we can. */
c5614fa4
AM
9119 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9120 {
9121 if (sec->reloc_count == 0
dbaa2011 9122 || discarded_section (sec))
c5614fa4
AM
9123 continue;
9124
9125 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9126 info->keep_memory);
c5614fa4
AM
9127 if (relstart == NULL)
9128 goto error_ret;
9129
9130 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9131 {
9132 enum elf_ppc64_reloc_type r_type;
9133 unsigned long r_symndx;
9134 asection *sym_sec;
9135 struct elf_link_hash_entry *h;
854b41e7 9136 bfd_vma val;
c5614fa4
AM
9137
9138 r_type = ELF64_R_TYPE (rel->r_info);
9139 switch (r_type)
9140 {
9141 default:
9142 continue;
9143
9144 case R_PPC64_TOC16:
9145 case R_PPC64_TOC16_LO:
9146 case R_PPC64_TOC16_HI:
9147 case R_PPC64_TOC16_HA:
9148 case R_PPC64_TOC16_DS:
9149 case R_PPC64_TOC16_LO_DS:
9150 case R_PPC64_ADDR64:
9151 break;
9152 }
9153
9154 r_symndx = ELF64_R_SYM (rel->r_info);
9155 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9156 r_symndx, ibfd))
9157 goto error_ret;
9158
ba761f19 9159 if (sym_sec != toc)
c5614fa4
AM
9160 continue;
9161
ba761f19
AM
9162 if (h != NULL)
9163 val = h->root.u.def.value;
9164 else
9165 {
9166 val = sym->st_value;
9167 if (val != 0)
9168 local_toc_syms = TRUE;
9169 }
9170
9171 val += rel->r_addend;
854b41e7
AM
9172
9173 if (val > toc->rawsize)
9174 val = toc->rawsize;
ba761f19
AM
9175 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9176 continue;
9177 else if ((skip[val >> 3] & can_optimize) != 0)
9178 {
9179 Elf_Internal_Rela *tocrel
425b145b 9180 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9181 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9182
9183 switch (r_type)
9184 {
9185 case R_PPC64_TOC16_HA:
9186 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9187 break;
9188
9189 case R_PPC64_TOC16_LO_DS:
9190 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9191 break;
9192
9193 default:
28942f62
AM
9194 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9195 ppc_howto_init ();
b140b010 9196 info->callbacks->einfo
695344c0 9197 /* xgettext:c-format */
174d0a74 9198 (_("%H: %s references "
b140b010
AM
9199 "optimized away TOC entry\n"),
9200 ibfd, sec, rel->r_offset,
9201 ppc64_elf_howto_table[r_type]->name);
9202 bfd_set_error (bfd_error_bad_value);
9203 goto error_ret;
ba761f19
AM
9204 }
9205 rel->r_addend = tocrel->r_addend;
9206 elf_section_data (sec)->relocs = relstart;
9207 continue;
9208 }
9209
9210 if (h != NULL || sym->st_value != 0)
9211 continue;
854b41e7
AM
9212
9213 rel->r_addend -= skip[val >> 3];
9214 elf_section_data (sec)->relocs = relstart;
c5614fa4 9215 }
854b41e7
AM
9216
9217 if (elf_section_data (sec)->relocs != relstart)
9218 free (relstart);
c5614fa4
AM
9219 }
9220
9221 /* We shouldn't have local or global symbols defined in the TOC,
9222 but handle them anyway. */
df22d223
AM
9223 if (local_syms != NULL)
9224 for (sym = local_syms;
9225 sym < local_syms + symtab_hdr->sh_info;
9226 ++sym)
9227 if (sym->st_value != 0
9228 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9229 {
9230 unsigned long i;
854b41e7 9231
df22d223
AM
9232 if (sym->st_value > toc->rawsize)
9233 i = toc->rawsize >> 3;
9234 else
9235 i = sym->st_value >> 3;
854b41e7 9236
df22d223
AM
9237 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9238 {
9239 if (local_toc_syms)
4eca0228 9240 _bfd_error_handler
df22d223
AM
9241 (_("%s defined on removed toc entry"),
9242 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9243 do
9244 ++i;
9245 while ((skip[i] & (ref_from_discarded | can_optimize)));
9246 sym->st_value = (bfd_vma) i << 3;
9247 }
d62b3684 9248
df22d223
AM
9249 sym->st_value -= skip[i];
9250 symtab_hdr->contents = (unsigned char *) local_syms;
9251 }
c5614fa4 9252
854b41e7 9253 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9254 if (toc_inf.global_toc_syms)
9255 {
9256 toc_inf.toc = toc;
9257 toc_inf.skip = skip;
9258 toc_inf.global_toc_syms = FALSE;
9259 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9260 &toc_inf);
9261 }
854b41e7
AM
9262
9263 if (toc->reloc_count != 0)
9264 {
d4730f92 9265 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9266 Elf_Internal_Rela *wrel;
9267 bfd_size_type sz;
9268
854b41e7 9269 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9270 if (toc_relocs == NULL)
9271 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9272 info->keep_memory);
9273 if (toc_relocs == NULL)
9274 goto error_ret;
9275
425b145b
AM
9276 wrel = toc_relocs;
9277 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9278 if ((skip[rel->r_offset >> 3]
9279 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9280 {
9281 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9282 wrel->r_info = rel->r_info;
9283 wrel->r_addend = rel->r_addend;
9284 ++wrel;
9285 }
9286 else if (!dec_dynrel_count (rel->r_info, toc, info,
9287 &local_syms, NULL, NULL))
9288 goto error_ret;
9289
425b145b
AM
9290 elf_section_data (toc)->relocs = toc_relocs;
9291 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9292 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9293 sz = rel_hdr->sh_entsize;
9294 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9295 }
c5614fa4 9296 }
c9594989 9297 else if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 9298 free (toc_relocs);
c5614fa4
AM
9299
9300 if (local_syms != NULL
9301 && symtab_hdr->contents != (unsigned char *) local_syms)
9302 {
9303 if (!info->keep_memory)
9304 free (local_syms);
9305 else
9306 symtab_hdr->contents = (unsigned char *) local_syms;
9307 }
9308 free (skip);
9309 }
9310
066f4018 9311 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
9312 a GOT relative or PC relative access, possibly reducing the
9313 number of GOT entries. */
066f4018
AM
9314 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9315 {
9316 asection *sec;
9317 Elf_Internal_Shdr *symtab_hdr;
9318 Elf_Internal_Sym *local_syms;
9319 Elf_Internal_Rela *relstart, *rel;
9320 bfd_vma got;
9321
9322 if (!is_ppc64_elf (ibfd))
9323 continue;
9324
903b777d 9325 if (!ppc64_elf_tdata (ibfd)->has_optrel)
066f4018
AM
9326 continue;
9327
9328 sec = ppc64_elf_tdata (ibfd)->got;
903b777d
AM
9329 got = 0;
9330 if (sec != NULL)
9331 got = sec->output_section->vma + sec->output_offset + 0x8000;
066f4018
AM
9332
9333 local_syms = NULL;
9334 symtab_hdr = &elf_symtab_hdr (ibfd);
9335
9336 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9337 {
9338 if (sec->reloc_count == 0
903b777d 9339 || !ppc64_elf_section_data (sec)->has_optrel
066f4018
AM
9340 || discarded_section (sec))
9341 continue;
9342
9343 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9344 info->keep_memory);
9345 if (relstart == NULL)
9346 {
9347 got_error_ret:
c9594989 9348 if (symtab_hdr->contents != (unsigned char *) local_syms)
066f4018
AM
9349 free (local_syms);
9350 if (sec != NULL
066f4018
AM
9351 && elf_section_data (sec)->relocs != relstart)
9352 free (relstart);
9353 return FALSE;
9354 }
9355
9356 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9357 {
9358 enum elf_ppc64_reloc_type r_type;
9359 unsigned long r_symndx;
9360 Elf_Internal_Sym *sym;
9361 asection *sym_sec;
9362 struct elf_link_hash_entry *h;
9363 struct got_entry *ent;
133a1f60 9364 bfd_vma val, pc;
4a421c53 9365 unsigned char buf[8];
066f4018 9366 unsigned int insn;
903b777d 9367 enum {no_check, check_lo, check_ha} insn_check;
066f4018
AM
9368
9369 r_type = ELF64_R_TYPE (rel->r_info);
903b777d
AM
9370 switch (r_type)
9371 {
9372 default:
9373 insn_check = no_check;
9374 break;
9375
9376 case R_PPC64_PLT16_HA:
9377 case R_PPC64_GOT_TLSLD16_HA:
9378 case R_PPC64_GOT_TLSGD16_HA:
9379 case R_PPC64_GOT_TPREL16_HA:
9380 case R_PPC64_GOT_DTPREL16_HA:
9381 case R_PPC64_GOT16_HA:
9382 case R_PPC64_TOC16_HA:
9383 insn_check = check_ha;
9384 break;
9385
9386 case R_PPC64_PLT16_LO:
9387 case R_PPC64_PLT16_LO_DS:
9388 case R_PPC64_GOT_TLSLD16_LO:
9389 case R_PPC64_GOT_TLSGD16_LO:
9390 case R_PPC64_GOT_TPREL16_LO_DS:
9391 case R_PPC64_GOT_DTPREL16_LO_DS:
9392 case R_PPC64_GOT16_LO:
9393 case R_PPC64_GOT16_LO_DS:
9394 case R_PPC64_TOC16_LO:
9395 case R_PPC64_TOC16_LO_DS:
9396 insn_check = check_lo;
9397 break;
9398 }
9399
9400 if (insn_check != no_check)
9401 {
9402 bfd_vma off = rel->r_offset & ~3;
9403
9404 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9405 goto got_error_ret;
9406
9407 insn = bfd_get_32 (ibfd, buf);
9408 if (insn_check == check_lo
9409 ? !ok_lo_toc_insn (insn, r_type)
2365f8d7 9410 : ((insn & ((0x3fu << 26) | 0x1f << 16))
903b777d
AM
9411 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9412 {
9413 char str[12];
9414
9415 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9416 sprintf (str, "%#08x", insn);
9417 info->callbacks->einfo
9418 /* xgettext:c-format */
9419 (_("%H: got/toc optimization is not supported for"
9420 " %s instruction\n"),
9421 ibfd, sec, rel->r_offset & ~3, str);
9422 continue;
9423 }
9424 }
9425
066f4018
AM
9426 switch (r_type)
9427 {
bb22a418
AM
9428 /* Note that we don't delete GOT entries for
9429 R_PPC64_GOT16_DS since we'd need a lot more
9430 analysis. For starters, the preliminary layout is
9431 before the GOT, PLT, dynamic sections and stubs are
9432 laid out. Then we'd need to allow for changes in
9433 distance between sections caused by alignment. */
066f4018
AM
9434 default:
9435 continue;
9436
066f4018
AM
9437 case R_PPC64_GOT16_HA:
9438 case R_PPC64_GOT16_LO_DS:
4a421c53 9439 case R_PPC64_GOT_PCREL34:
066f4018
AM
9440 break;
9441 }
9442
9443 r_symndx = ELF64_R_SYM (rel->r_info);
9444 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9445 r_symndx, ibfd))
9446 goto got_error_ret;
9447
6d5554a6
AM
9448 if (sym_sec == NULL
9449 || sym_sec->output_section == NULL
9450 || discarded_section (sym_sec))
9451 continue;
9452
06507dab
AM
9453 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9454 continue;
9455
066f4018
AM
9456 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9457 continue;
9458
9459 if (h != NULL)
9460 val = h->root.u.def.value;
9461 else
9462 val = sym->st_value;
133a1f60 9463 val += rel->r_addend;
066f4018
AM
9464 val += sym_sec->output_section->vma + sym_sec->output_offset;
9465
bb22a418
AM
9466/* Fudge factor to allow for the fact that the preliminary layout
9467 isn't exact. Reduce limits by this factor. */
9468#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9469
066f4018
AM
9470 switch (r_type)
9471 {
9472 default:
9473 continue;
9474
066f4018 9475 case R_PPC64_GOT16_HA:
bb22a418
AM
9476 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9477 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9478 continue;
9479
9480 if (!bfd_get_section_contents (ibfd, sec, buf,
9481 rel->r_offset & ~3, 4))
9482 goto got_error_ret;
9483 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9484 if (((insn & ((0x3fu << 26) | 0x1f << 16))
066f4018
AM
9485 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9486 continue;
9487 break;
9488
9489 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9490 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9491 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9492 continue;
9493 if (!bfd_get_section_contents (ibfd, sec, buf,
9494 rel->r_offset & ~3, 4))
9495 goto got_error_ret;
9496 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9497 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
066f4018
AM
9498 continue;
9499 break;
4a421c53
AM
9500
9501 case R_PPC64_GOT_PCREL34:
9502 pc = rel->r_offset;
9503 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9504 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9505 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9506 continue;
9507 if (!bfd_get_section_contents (ibfd, sec, buf,
9508 rel->r_offset & ~3, 8))
9509 goto got_error_ret;
9510 insn = bfd_get_32 (ibfd, buf);
9511 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9512 continue;
9513 insn = bfd_get_32 (ibfd, buf + 4);
2365f8d7 9514 if ((insn & (0x3fu << 26)) != 57u << 26)
4a421c53
AM
9515 continue;
9516 break;
066f4018 9517 }
bb22a418 9518#undef LIMIT_ADJUST
066f4018
AM
9519
9520 if (h != NULL)
9521 ent = h->got.glist;
9522 else
9523 {
9524 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9525 ent = local_got_ents[r_symndx];
9526 }
9527 for (; ent != NULL; ent = ent->next)
133a1f60 9528 if (ent->addend == rel->r_addend
066f4018
AM
9529 && ent->owner == ibfd
9530 && ent->tls_type == 0)
9531 break;
9532 BFD_ASSERT (ent && ent->got.refcount > 0);
9533 ent->got.refcount -= 1;
9534 }
9535
9536 if (elf_section_data (sec)->relocs != relstart)
9537 free (relstart);
9538 }
9539
9540 if (local_syms != NULL
9541 && symtab_hdr->contents != (unsigned char *) local_syms)
9542 {
9543 if (!info->keep_memory)
9544 free (local_syms);
9545 else
9546 symtab_hdr->contents = (unsigned char *) local_syms;
9547 }
9548 }
9549
c5614fa4
AM
9550 return TRUE;
9551}
9552
1bbe0902
AM
9553/* Return true iff input section I references the TOC using
9554 instructions limited to +/-32k offsets. */
9555
9556bfd_boolean
9557ppc64_elf_has_small_toc_reloc (asection *i)
9558{
9559 return (is_ppc64_elf (i->owner)
9560 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9561}
9562
927be08e
AM
9563/* Allocate space for one GOT entry. */
9564
9565static void
9566allocate_got (struct elf_link_hash_entry *h,
9567 struct bfd_link_info *info,
9568 struct got_entry *gent)
9569{
9570 struct ppc_link_hash_table *htab = ppc_hash_table (info);
ed7007c1 9571 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
927be08e
AM
9572 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9573 ? 16 : 8);
9574 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9575 ? 2 : 1) * sizeof (Elf64_External_Rela);
9576 asection *got = ppc64_elf_tdata (gent->owner)->got;
9577
9578 gent->got.offset = got->size;
9579 got->size += entsize;
9580
19e08130 9581 if (h->type == STT_GNU_IFUNC)
927be08e 9582 {
33e44f2e 9583 htab->elf.irelplt->size += rentsize;
19e08130 9584 htab->got_reli_size += rentsize;
927be08e 9585 }
f15d0b54 9586 else if (((bfd_link_pic (info)
f749f26e 9587 && !(gent->tls_type != 0
f15d0b54
AM
9588 && bfd_link_executable (info)
9589 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9590 || (htab->elf.dynamic_sections_created
9591 && h->dynindx != -1
9592 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9593 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9594 {
19e08130 9595 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9596 relgot->size += rentsize;
927be08e
AM
9597 }
9598}
9599
7865406b
AM
9600/* This function merges got entries in the same toc group. */
9601
9602static void
9603merge_got_entries (struct got_entry **pent)
9604{
9605 struct got_entry *ent, *ent2;
9606
9607 for (ent = *pent; ent != NULL; ent = ent->next)
9608 if (!ent->is_indirect)
9609 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9610 if (!ent2->is_indirect
9611 && ent2->addend == ent->addend
9612 && ent2->tls_type == ent->tls_type
9613 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9614 {
9615 ent2->is_indirect = TRUE;
9616 ent2->got.ent = ent;
9617 }
9618}
9619
46434633 9620/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9621
9622static bfd_boolean
46434633
AM
9623ensure_undef_dynamic (struct bfd_link_info *info,
9624 struct elf_link_hash_entry *h)
f0158f44
AM
9625{
9626 struct elf_link_hash_table *htab = elf_hash_table (info);
9627
9628 if (htab->dynamic_sections_created
46434633
AM
9629 && ((info->dynamic_undefined_weak != 0
9630 && h->root.type == bfd_link_hash_undefweak)
9631 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9632 && h->dynindx == -1
9633 && !h->forced_local
9634 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9635 return bfd_elf_link_record_dynamic_symbol (info, h);
9636 return TRUE;
9637}
9638
30845f11
AM
9639/* Choose whether to use htab->iplt or htab->pltlocal rather than the
9640 usual htab->elf.splt section for a PLT entry. */
9641
9642static inline
9643bfd_boolean use_local_plt (struct bfd_link_info *info,
9644 struct elf_link_hash_entry *h)
9645{
9646 return (h == NULL
9647 || h->dynindx == -1
9648 || !elf_hash_table (info)->dynamic_sections_created);
9649}
9650
65f38f15
AM
9651/* Allocate space in .plt, .got and associated reloc sections for
9652 dynamic relocs. */
5bd4f169 9653
b34976b6 9654static bfd_boolean
4ce794b7 9655allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9656{
65f38f15
AM
9657 struct bfd_link_info *info;
9658 struct ppc_link_hash_table *htab;
5bd4f169 9659 asection *s;
65f38f15 9660 struct ppc_link_hash_entry *eh;
0b8bcf0d 9661 struct got_entry **pgent, *gent;
5bd4f169 9662
e92d460e 9663 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9664 return TRUE;
5bd4f169 9665
65f38f15
AM
9666 info = (struct bfd_link_info *) inf;
9667 htab = ppc_hash_table (info);
4dfe6ac6
NC
9668 if (htab == NULL)
9669 return FALSE;
5bd4f169 9670
ed7007c1 9671 eh = ppc_elf_hash_entry (h);
951fd09b
AM
9672 /* Run through the TLS GD got entries first if we're changing them
9673 to TPREL. */
b00a0a86 9674 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
951fd09b
AM
9675 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9676 if (gent->got.refcount > 0
9677 && (gent->tls_type & TLS_GD) != 0)
9678 {
9679 /* This was a GD entry that has been converted to TPREL. If
9680 there happens to be a TPREL entry we can use that one. */
9681 struct got_entry *ent;
9682 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9683 if (ent->got.refcount > 0
9684 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9685 && ent->addend == gent->addend
9686 && ent->owner == gent->owner)
951fd09b
AM
9687 {
9688 gent->got.refcount = 0;
9689 break;
9690 }
9691
9692 /* If not, then we'll be using our own TPREL entry. */
9693 if (gent->got.refcount != 0)
9694 gent->tls_type = TLS_TLS | TLS_TPREL;
9695 }
9696
7865406b
AM
9697 /* Remove any list entry that won't generate a word in the GOT before
9698 we call merge_got_entries. Otherwise we risk merging to empty
9699 entries. */
0b8bcf0d
AM
9700 pgent = &h->got.glist;
9701 while ((gent = *pgent) != NULL)
411e1bfb 9702 if (gent->got.refcount > 0)
7865406b
AM
9703 {
9704 if ((gent->tls_type & TLS_LD) != 0
f749f26e 9705 && SYMBOL_REFERENCES_LOCAL (info, h))
7865406b
AM
9706 {
9707 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9708 *pgent = gent->next;
9709 }
9710 else
9711 pgent = &gent->next;
9712 }
9713 else
9714 *pgent = gent->next;
9715
9716 if (!htab->do_multi_toc)
9717 merge_got_entries (&h->got.glist);
9718
9719 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9720 if (!gent->is_indirect)
411e1bfb 9721 {
ec73ddcd
AM
9722 /* Ensure we catch all the cases where this symbol should
9723 be made dynamic. */
46434633 9724 if (!ensure_undef_dynamic (info, h))
f0158f44 9725 return FALSE;
65f38f15 9726
0c8d6e5c 9727 if (!is_ppc64_elf (gent->owner))
927be08e 9728 abort ();
0ffa91dd 9729
927be08e 9730 allocate_got (h, info, gent);
411e1bfb 9731 }
65f38f15 9732
954b63d4
AM
9733 /* If no dynamic sections we can't have dynamic relocs, except for
9734 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9735 if (!htab->elf.dynamic_sections_created
9736 && h->type != STT_GNU_IFUNC)
190eb1dd 9737 h->dyn_relocs = NULL;
8a2058b5 9738
529fe20e
AM
9739 /* Discard relocs on undefined symbols that must be local. */
9740 else if (h->root.type == bfd_link_hash_undefined
9741 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
190eb1dd 9742 h->dyn_relocs = NULL;
529fe20e 9743
954b63d4
AM
9744 /* Also discard relocs on undefined weak syms with non-default
9745 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9746 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 9747 h->dyn_relocs = NULL;
954b63d4 9748
190eb1dd 9749 if (h->dyn_relocs != NULL)
65f38f15 9750 {
8a2058b5
AM
9751 struct elf_dyn_relocs *p, **pp;
9752
57e7d118
AM
9753 /* In the shared -Bsymbolic case, discard space allocated for
9754 dynamic pc-relative relocs against symbols which turn out to
9755 be defined in regular objects. For the normal shared case,
9756 discard space for relocs that have become local due to symbol
9757 visibility changes. */
57e7d118 9758 if (bfd_link_pic (info))
65f38f15 9759 {
57e7d118
AM
9760 /* Relocs that use pc_count are those that appear on a call
9761 insn, or certain REL relocs (see must_be_dyn_reloc) that
9762 can be generated via assembly. We want calls to
9763 protected symbols to resolve directly to the function
9764 rather than going via the plt. If people want function
9765 pointer comparisons to work as expected then they should
9766 avoid writing weird assembly. */
9767 if (SYMBOL_CALLS_LOCAL (info, h))
9768 {
190eb1dd 9769 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
57e7d118
AM
9770 {
9771 p->count -= p->pc_count;
9772 p->pc_count = 0;
9773 if (p->count == 0)
9774 *pp = p->next;
9775 else
9776 pp = &p->next;
9777 }
9778 }
65f38f15 9779
190eb1dd 9780 if (h->dyn_relocs != NULL)
5bd4f169 9781 {
ec73ddcd
AM
9782 /* Ensure we catch all the cases where this symbol
9783 should be made dynamic. */
46434633 9784 if (!ensure_undef_dynamic (info, h))
f0158f44 9785 return FALSE;
5bd4f169 9786 }
65f38f15 9787 }
ec73ddcd
AM
9788
9789 /* For a fixed position executable, discard space for
9790 relocs against symbols which are not dynamic. */
9791 else if (h->type != STT_GNU_IFUNC)
57e7d118 9792 {
529fe20e
AM
9793 if (h->dynamic_adjusted
9794 && !h->def_regular
9795 && !ELF_COMMON_DEF_P (h))
f0158f44 9796 {
ec73ddcd
AM
9797 /* Ensure we catch all the cases where this symbol
9798 should be made dynamic. */
46434633 9799 if (!ensure_undef_dynamic (info, h))
f0158f44 9800 return FALSE;
dfbb6ac9 9801
ec73ddcd 9802 /* But if that didn't work out, discard dynamic relocs. */
f0158f44 9803 if (h->dynindx == -1)
190eb1dd 9804 h->dyn_relocs = NULL;
f0158f44
AM
9805 }
9806 else
190eb1dd 9807 h->dyn_relocs = NULL;
57e7d118
AM
9808 }
9809
9810 /* Finally, allocate space. */
190eb1dd 9811 for (p = h->dyn_relocs; p != NULL; p = p->next)
57e7d118
AM
9812 {
9813 asection *sreloc = elf_section_data (p->sec)->sreloc;
9814 if (eh->elf.type == STT_GNU_IFUNC)
9815 sreloc = htab->elf.irelplt;
9816 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9817 }
65f38f15 9818 }
57e7d118 9819
2d7ad24e
AM
9820 /* We might need a PLT entry when the symbol
9821 a) is dynamic, or
9822 b) is an ifunc, or
9823 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9824 d) has plt16 relocs and we are linking statically. */
9825 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9826 || h->type == STT_GNU_IFUNC
9827 || (h->needs_plt && h->dynamic_adjusted)
9828 || (h->needs_plt
9829 && h->def_regular
9830 && !htab->elf.dynamic_sections_created
3e04d765 9831 && !htab->can_convert_all_inline_plt
ed7007c1 9832 && (ppc_elf_hash_entry (h)->tls_mask
2d7ad24e 9833 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 9834 {
57e7d118
AM
9835 struct plt_entry *pent;
9836 bfd_boolean doneone = FALSE;
9837 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9838 if (pent->plt.refcount > 0)
9839 {
30845f11 9840 if (use_local_plt (info, h))
57e7d118 9841 {
2d7ad24e
AM
9842 if (h->type == STT_GNU_IFUNC)
9843 {
9844 s = htab->elf.iplt;
9845 pent->plt.offset = s->size;
9846 s->size += PLT_ENTRY_SIZE (htab);
9847 s = htab->elf.irelplt;
9848 }
9849 else
9850 {
9851 s = htab->pltlocal;
9852 pent->plt.offset = s->size;
9853 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9854 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9855 }
57e7d118
AM
9856 }
9857 else
9858 {
9859 /* If this is the first .plt entry, make room for the special
9860 first entry. */
9861 s = htab->elf.splt;
9862 if (s->size == 0)
9863 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9864
57e7d118 9865 pent->plt.offset = s->size;
65f38f15 9866
57e7d118
AM
9867 /* Make room for this entry. */
9868 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9869
57e7d118
AM
9870 /* Make room for the .glink code. */
9871 s = htab->glink;
9872 if (s->size == 0)
9e390558 9873 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9874 if (htab->opd_abi)
9875 {
9876 /* We need bigger stubs past index 32767. */
9e390558 9877 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9878 s->size += 4;
9879 s->size += 2*4;
9880 }
9881 else
9882 s->size += 4;
65f38f15 9883
57e7d118
AM
9884 /* We also need to make an entry in the .rela.plt section. */
9885 s = htab->elf.srelplt;
9886 }
2d7ad24e
AM
9887 if (s != NULL)
9888 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
9889 doneone = TRUE;
9890 }
9891 else
9892 pent->plt.offset = (bfd_vma) -1;
9893 if (!doneone)
9894 {
9895 h->plt.plist = NULL;
9896 h->needs_plt = 0;
9897 }
65f38f15 9898 }
57e7d118 9899 else
65f38f15 9900 {
57e7d118
AM
9901 h->plt.plist = NULL;
9902 h->needs_plt = 0;
65f38f15
AM
9903 }
9904
b34976b6 9905 return TRUE;
65f38f15
AM
9906}
9907
9e390558
AM
9908#define PPC_LO(v) ((v) & 0xffff)
9909#define PPC_HI(v) (((v) >> 16) & 0xffff)
9910#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
9911#define D34(v) \
9912 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9913#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 9914
a345bc8d
AM
9915/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9916 to set up space for global entry stubs. These are put in glink,
9917 after the branch table. */
65f38f15 9918
b34976b6 9919static bfd_boolean
a345bc8d 9920size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9921{
a345bc8d
AM
9922 struct bfd_link_info *info;
9923 struct ppc_link_hash_table *htab;
9924 struct plt_entry *pent;
9e390558 9925 asection *s, *plt;
65f38f15 9926
a345bc8d
AM
9927 if (h->root.type == bfd_link_hash_indirect)
9928 return TRUE;
65f38f15 9929
a345bc8d
AM
9930 if (!h->pointer_equality_needed)
9931 return TRUE;
65f38f15 9932
a345bc8d
AM
9933 if (h->def_regular)
9934 return TRUE;
65f38f15 9935
a345bc8d
AM
9936 info = inf;
9937 htab = ppc_hash_table (info);
9938 if (htab == NULL)
9939 return FALSE;
9940
9e390558
AM
9941 s = htab->global_entry;
9942 plt = htab->elf.splt;
a345bc8d
AM
9943 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9944 if (pent->plt.offset != (bfd_vma) -1
9945 && pent->addend == 0)
9946 {
afe397ea
AM
9947 /* For ELFv2, if this symbol is not defined in a regular file
9948 and we are not generating a shared library or pie, then we
9949 need to define the symbol in the executable on a call stub.
9950 This is to avoid text relocations. */
9e390558
AM
9951 bfd_vma off, stub_align, stub_off, stub_size;
9952 unsigned int align_power;
9953
9954 stub_size = 16;
9955 stub_off = s->size;
9956 if (htab->params->plt_stub_align >= 0)
9957 align_power = htab->params->plt_stub_align;
9958 else
9959 align_power = -htab->params->plt_stub_align;
9960 /* Setting section alignment is delayed until we know it is
9961 non-empty. Otherwise the .text output section will be
9962 aligned at least to plt_stub_align even when no global
9963 entry stubs are needed. */
9964 if (s->alignment_power < align_power)
9965 s->alignment_power = align_power;
9966 stub_align = (bfd_vma) 1 << align_power;
9967 if (htab->params->plt_stub_align >= 0
9968 || ((((stub_off + stub_size - 1) & -stub_align)
9969 - (stub_off & -stub_align))
9970 > ((stub_size - 1) & -stub_align)))
9971 stub_off = (stub_off + stub_align - 1) & -stub_align;
9972 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9973 off -= stub_off + s->output_offset + s->output_section->vma;
9974 /* Note that for --plt-stub-align negative we have a possible
9975 dependency between stub offset and size. Break that
9976 dependency by assuming the max stub size when calculating
9977 the stub offset. */
9978 if (PPC_HA (off) == 0)
9979 stub_size -= 4;
8a2058b5 9980 h->root.type = bfd_link_hash_defined;
afe397ea 9981 h->root.u.def.section = s;
9e390558
AM
9982 h->root.u.def.value = stub_off;
9983 s->size = stub_off + stub_size;
a345bc8d
AM
9984 break;
9985 }
9986 return TRUE;
9987}
9988
65f38f15
AM
9989/* Set the sizes of the dynamic sections. */
9990
b34976b6 9991static bfd_boolean
ee67d69a 9992ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9993 struct bfd_link_info *info)
65f38f15
AM
9994{
9995 struct ppc_link_hash_table *htab;
9996 bfd *dynobj;
9997 asection *s;
b34976b6 9998 bfd_boolean relocs;
65f38f15 9999 bfd *ibfd;
7865406b 10000 struct got_entry *first_tlsld;
65f38f15
AM
10001
10002 htab = ppc_hash_table (info);
4dfe6ac6
NC
10003 if (htab == NULL)
10004 return FALSE;
10005
65f38f15
AM
10006 dynobj = htab->elf.dynobj;
10007 if (dynobj == NULL)
10008 abort ();
10009
10010 if (htab->elf.dynamic_sections_created)
10011 {
10012 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 10013 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 10014 {
3d4d4302 10015 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
10016 if (s == NULL)
10017 abort ();
eea6121a 10018 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10019 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10020 }
10021 }
10022
10023 /* Set up .got offsets for local syms, and space for local dynamic
10024 relocs. */
c72f2fb2 10025 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10026 {
411e1bfb
AM
10027 struct got_entry **lgot_ents;
10028 struct got_entry **end_lgot_ents;
e054468f
AM
10029 struct plt_entry **local_plt;
10030 struct plt_entry **end_local_plt;
f961d9dd 10031 unsigned char *lgot_masks;
65f38f15
AM
10032 bfd_size_type locsymcount;
10033 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10034
0c8d6e5c 10035 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10036 continue;
10037
10038 for (s = ibfd->sections; s != NULL; s = s->next)
10039 {
19e08130 10040 struct ppc_dyn_relocs *p;
65f38f15 10041
6edfbbad 10042 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10043 {
ec338859
AM
10044 if (!bfd_is_abs_section (p->sec)
10045 && bfd_is_abs_section (p->sec->output_section))
10046 {
10047 /* Input section has been discarded, either because
10048 it is a copy of a linkonce section or due to
10049 linker script /DISCARD/, so we'll be discarding
10050 the relocs too. */
10051 }
248866a8 10052 else if (p->count != 0)
ec338859 10053 {
19e08130
AM
10054 asection *srel = elf_section_data (p->sec)->sreloc;
10055 if (p->ifunc)
33e44f2e 10056 srel = htab->elf.irelplt;
eea6121a 10057 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10058 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10059 info->flags |= DF_TEXTREL;
ec338859 10060 }
65f38f15
AM
10061 }
10062 }
10063
411e1bfb
AM
10064 lgot_ents = elf_local_got_ents (ibfd);
10065 if (!lgot_ents)
65f38f15
AM
10066 continue;
10067
0ffa91dd 10068 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10069 locsymcount = symtab_hdr->sh_info;
411e1bfb 10070 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10071 local_plt = (struct plt_entry **) end_lgot_ents;
10072 end_local_plt = local_plt + locsymcount;
f961d9dd 10073 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10074 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10075 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10076 {
0b8bcf0d 10077 struct got_entry **pent, *ent;
411e1bfb 10078
0b8bcf0d
AM
10079 pent = lgot_ents;
10080 while ((ent = *pent) != NULL)
411e1bfb
AM
10081 if (ent->got.refcount > 0)
10082 {
e7b938ca 10083 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10084 {
927be08e 10085 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10086 *pent = ent->next;
411e1bfb
AM
10087 }
10088 else
10089 {
19e08130
AM
10090 unsigned int ent_size = 8;
10091 unsigned int rel_size = sizeof (Elf64_External_Rela);
10092
eea6121a 10093 ent->got.offset = s->size;
e7b938ca 10094 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10095 {
19e08130
AM
10096 ent_size *= 2;
10097 rel_size *= 2;
10098 }
10099 s->size += ent_size;
37da22e5 10100 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 10101 {
33e44f2e 10102 htab->elf.irelplt->size += rel_size;
19e08130
AM
10103 htab->got_reli_size += rel_size;
10104 }
93370e8e
AM
10105 else if (bfd_link_pic (info)
10106 && !(ent->tls_type != 0
10107 && bfd_link_executable (info)))
19e08130
AM
10108 {
10109 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10110 srel->size += rel_size;
927be08e 10111 }
0b8bcf0d 10112 pent = &ent->next;
411e1bfb
AM
10113 }
10114 }
10115 else
0b8bcf0d 10116 *pent = ent->next;
65f38f15 10117 }
e054468f 10118
2d7ad24e
AM
10119 /* Allocate space for plt calls to local syms. */
10120 lgot_masks = (unsigned char *) end_local_plt;
10121 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
10122 {
10123 struct plt_entry *ent;
10124
10125 for (ent = *local_plt; ent != NULL; ent = ent->next)
10126 if (ent->plt.refcount > 0)
10127 {
2d7ad24e
AM
10128 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10129 {
10130 s = htab->elf.iplt;
10131 ent->plt.offset = s->size;
10132 s->size += PLT_ENTRY_SIZE (htab);
10133 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10134 }
3e04d765
AM
10135 else if (htab->can_convert_all_inline_plt
10136 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
10137 ent->plt.offset = (bfd_vma) -1;
10138 else
10139 {
10140 s = htab->pltlocal;
10141 ent->plt.offset = s->size;
10142 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10143 if (bfd_link_pic (info))
10144 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10145 }
e054468f
AM
10146 }
10147 else
10148 ent->plt.offset = (bfd_vma) -1;
10149 }
65f38f15
AM
10150 }
10151
10152 /* Allocate global sym .plt and .got entries, and space for global
10153 sym dynamic relocs. */
4ce794b7 10154 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10155
0e1862bb 10156 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10157 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10158
7865406b 10159 first_tlsld = NULL;
c72f2fb2 10160 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10161 {
7865406b
AM
10162 struct got_entry *ent;
10163
0c8d6e5c 10164 if (!is_ppc64_elf (ibfd))
102890f0
AM
10165 continue;
10166
7865406b
AM
10167 ent = ppc64_tlsld_got (ibfd);
10168 if (ent->got.refcount > 0)
102890f0 10169 {
7865406b 10170 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10171 {
7865406b
AM
10172 ent->is_indirect = TRUE;
10173 ent->got.ent = first_tlsld;
10174 }
10175 else
10176 {
10177 if (first_tlsld == NULL)
10178 first_tlsld = ent;
10179 s = ppc64_elf_tdata (ibfd)->got;
10180 ent->got.offset = s->size;
10181 ent->owner = ibfd;
10182 s->size += 16;
f749f26e 10183 if (bfd_link_dll (info))
7865406b
AM
10184 {
10185 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10186 srel->size += sizeof (Elf64_External_Rela);
10187 }
102890f0
AM
10188 }
10189 }
10190 else
7865406b 10191 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10192 }
10193
65f38f15
AM
10194 /* We now have determined the sizes of the various dynamic sections.
10195 Allocate memory for them. */
b34976b6 10196 relocs = FALSE;
65f38f15
AM
10197 for (s = dynobj->sections; s != NULL; s = s->next)
10198 {
10199 if ((s->flags & SEC_LINKER_CREATED) == 0)
10200 continue;
10201
4ce794b7 10202 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10203 /* These haven't been allocated yet; don't strip. */
10204 continue;
33e44f2e
AM
10205 else if (s == htab->elf.sgot
10206 || s == htab->elf.splt
10207 || s == htab->elf.iplt
2d7ad24e 10208 || s == htab->pltlocal
c456f082 10209 || s == htab->glink
9e390558 10210 || s == htab->global_entry
5474d94f
AM
10211 || s == htab->elf.sdynbss
10212 || s == htab->elf.sdynrelro)
65f38f15
AM
10213 {
10214 /* Strip this section if we don't need it; see the
10215 comment below. */
5bd4f169 10216 }
58d180e8
AM
10217 else if (s == htab->glink_eh_frame)
10218 {
10219 if (!bfd_is_abs_section (s->output_section))
10220 /* Not sized yet. */
10221 continue;
10222 }
70cc837d 10223 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10224 {
c456f082 10225 if (s->size != 0)
5bd4f169 10226 {
33e44f2e 10227 if (s != htab->elf.srelplt)
b34976b6 10228 relocs = TRUE;
5bd4f169
AM
10229
10230 /* We use the reloc_count field as a counter if we need
10231 to copy relocs into the output file. */
10232 s->reloc_count = 0;
10233 }
10234 }
65f38f15 10235 else
5bd4f169
AM
10236 {
10237 /* It's not one of our sections, so don't allocate space. */
10238 continue;
10239 }
10240
eea6121a 10241 if (s->size == 0)
5bd4f169 10242 {
c456f082
AM
10243 /* If we don't need this section, strip it from the
10244 output file. This is mostly to handle .rela.bss and
10245 .rela.plt. We must create both sections in
10246 create_dynamic_sections, because they must be created
10247 before the linker maps input sections to output
10248 sections. The linker does that before
10249 adjust_dynamic_symbol is called, and it is that
10250 function which decides whether anything needs to go
10251 into these sections. */
8423293d 10252 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10253 continue;
10254 }
10255
06bcf541
AM
10256 if (bfd_is_abs_section (s->output_section))
10257 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10258 s->name);
10259
c456f082 10260 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10261 continue;
10262
65f38f15
AM
10263 /* Allocate memory for the section contents. We use bfd_zalloc
10264 here in case unused entries are not reclaimed before the
10265 section's contents are written out. This should not happen,
411e1bfb
AM
10266 but this way if it does we get a R_PPC64_NONE reloc in .rela
10267 sections instead of garbage.
10268 We also rely on the section contents being zero when writing
5474d94f 10269 the GOT and .dynrelro. */
eea6121a 10270 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10271 if (s->contents == NULL)
b34976b6 10272 return FALSE;
5bd4f169
AM
10273 }
10274
c72f2fb2 10275 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10276 {
0c8d6e5c 10277 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10278 continue;
10279
e717da7e 10280 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10281 if (s != NULL && s != htab->elf.sgot)
e717da7e 10282 {
eea6121a 10283 if (s->size == 0)
8423293d 10284 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10285 else
10286 {
eea6121a 10287 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10288 if (s->contents == NULL)
10289 return FALSE;
10290 }
10291 }
10292 s = ppc64_elf_tdata (ibfd)->relgot;
10293 if (s != NULL)
10294 {
eea6121a 10295 if (s->size == 0)
8423293d 10296 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10297 else
10298 {
eea6121a 10299 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10300 if (s->contents == NULL)
10301 return FALSE;
10302 relocs = TRUE;
10303 s->reloc_count = 0;
10304 }
10305 }
10306 }
10307
e86ce104 10308 if (htab->elf.dynamic_sections_created)
5bd4f169 10309 {
e8910a83
AM
10310 bfd_boolean tls_opt;
10311
5bd4f169
AM
10312 /* Add some entries to the .dynamic section. We fill in the
10313 values later, in ppc64_elf_finish_dynamic_sections, but we
10314 must add the entries now so that we get the correct size for
10315 the .dynamic section. The DT_DEBUG entry is filled in by the
10316 dynamic linker and used by the debugger. */
dc810e39 10317#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10318 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10319
0e1862bb 10320 if (bfd_link_executable (info))
5bd4f169 10321 {
dc810e39 10322 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10323 return FALSE;
5bd4f169
AM
10324 }
10325
33e44f2e 10326 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10327 {
dc810e39
AM
10328 if (!add_dynamic_entry (DT_PLTGOT, 0)
10329 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10330 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10331 || !add_dynamic_entry (DT_JMPREL, 0)
10332 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10333 return FALSE;
5bd4f169
AM
10334 }
10335
ee67d69a 10336 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10337 {
10338 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10339 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10340 return FALSE;
19397422
AM
10341 }
10342
7c9cf415 10343 tls_opt = (htab->params->tls_get_addr_opt
9e7028aa
AM
10344 && ((htab->tls_get_addr_fd != NULL
10345 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10346 || (htab->tga_desc_fd != NULL
10347 && htab->tga_desc_fd->elf.plt.plist != NULL)));
e8910a83
AM
10348 if (tls_opt || !htab->opd_abi)
10349 {
10350 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10351 return FALSE;
10352 }
a7f2871e 10353
5bd4f169
AM
10354 if (relocs)
10355 {
dc810e39
AM
10356 if (!add_dynamic_entry (DT_RELA, 0)
10357 || !add_dynamic_entry (DT_RELASZ, 0)
10358 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10359 return FALSE;
5bd4f169 10360
65f38f15
AM
10361 /* If any dynamic relocs apply to a read-only section,
10362 then we need a DT_TEXTREL entry. */
248866a8 10363 if ((info->flags & DF_TEXTREL) == 0)
d49e5065
L
10364 elf_link_hash_traverse (&htab->elf,
10365 _bfd_elf_maybe_set_textrel, info);
5bd4f169 10366
65f38f15 10367 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10368 {
65f38f15 10369 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10370 return FALSE;
5bd4f169 10371 }
5bd4f169 10372 }
5bd4f169 10373 }
65f38f15 10374#undef add_dynamic_entry
5bd4f169 10375
b34976b6 10376 return TRUE;
5bd4f169
AM
10377}
10378
a345bc8d
AM
10379/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10380
10381static bfd_boolean
10382ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10383{
10384 if (h->plt.plist != NULL
10385 && !h->def_regular
10386 && !h->pointer_equality_needed)
10387 return FALSE;
10388
10389 return _bfd_elf_hash_symbol (h);
10390}
10391
721956f4 10392/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10393
4ce794b7
AM
10394static inline enum ppc_stub_type
10395ppc_type_of_stub (asection *input_sec,
10396 const Elf_Internal_Rela *rel,
10397 struct ppc_link_hash_entry **hash,
e054468f 10398 struct plt_entry **plt_ent,
6911b7dc
AM
10399 bfd_vma destination,
10400 unsigned long local_off)
5bd4f169 10401{
721956f4
AM
10402 struct ppc_link_hash_entry *h = *hash;
10403 bfd_vma location;
10404 bfd_vma branch_offset;
10405 bfd_vma max_branch_offset;
4ce794b7 10406 enum elf_ppc64_reloc_type r_type;
5bd4f169 10407
721956f4
AM
10408 if (h != NULL)
10409 {
e054468f 10410 struct plt_entry *ent;
7fe2b9a6 10411 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10412 if (h->oh != NULL
10413 && h->oh->is_func_descriptor)
7b8f6675
AM
10414 {
10415 fdh = ppc_follow_link (h->oh);
10416 *hash = fdh;
10417 }
8387904d 10418
e054468f
AM
10419 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10420 if (ent->addend == rel->r_addend
10421 && ent->plt.offset != (bfd_vma) -1)
10422 {
e054468f
AM
10423 *plt_ent = ent;
10424 return ppc_stub_plt_call;
10425 }
5bd4f169 10426
7fe2b9a6
AM
10427 /* Here, we know we don't have a plt entry. If we don't have a
10428 either a defined function descriptor or a defined entry symbol
10429 in a regular object file, then it is pointless trying to make
10430 any other type of stub. */
854b41e7
AM
10431 if (!is_static_defined (&fdh->elf)
10432 && !is_static_defined (&h->elf))
721956f4 10433 return ppc_stub_none;
5d1634d7 10434 }
e054468f
AM
10435 else if (elf_local_got_ents (input_sec->owner) != NULL)
10436 {
10437 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10438 struct plt_entry **local_plt = (struct plt_entry **)
10439 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10440 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10441
10442 if (local_plt[r_symndx] != NULL)
10443 {
10444 struct plt_entry *ent;
10445
10446 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10447 if (ent->addend == rel->r_addend
10448 && ent->plt.offset != (bfd_vma) -1)
10449 {
10450 *plt_ent = ent;
10451 return ppc_stub_plt_call;
10452 }
10453 }
10454 }
5d1634d7 10455
721956f4
AM
10456 /* Determine where the call point is. */
10457 location = (input_sec->output_offset
10458 + input_sec->output_section->vma
10459 + rel->r_offset);
5d1634d7 10460
721956f4
AM
10461 branch_offset = destination - location;
10462 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10463
721956f4
AM
10464 /* Determine if a long branch stub is needed. */
10465 max_branch_offset = 1 << 25;
23cedd1d
AM
10466 if (r_type == R_PPC64_REL14
10467 || r_type == R_PPC64_REL14_BRTAKEN
10468 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10469 max_branch_offset = 1 << 15;
5d1634d7 10470
6911b7dc 10471 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10472 /* We need a stub. Figure out whether a long_branch or plt_branch
10473 is needed later. */
10474 return ppc_stub_long_branch;
5d1634d7 10475
721956f4 10476 return ppc_stub_none;
5d1634d7
AM
10477}
10478
f891966f
AM
10479/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10480 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10481 . mflr %r12
10482 . bcl 20,31,1f
10483 .1: mflr %r11
10484 . mtlr %r12
05d0e962 10485 . lis %r12,xxx-1b@highest
f891966f 10486 . ori %r12,%r12,xxx-1b@higher
05d0e962 10487 . sldi %r12,%r12,32
f891966f 10488 . oris %r12,%r12,xxx-1b@high
05d0e962 10489 . ori %r12,%r12,xxx-1b@l
f891966f 10490 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10491
10492static bfd_byte *
10493build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10494{
f891966f
AM
10495 bfd_put_32 (abfd, MFLR_R12, p);
10496 p += 4;
10497 bfd_put_32 (abfd, BCL_20_31, p);
10498 p += 4;
10499 bfd_put_32 (abfd, MFLR_R11, p);
10500 p += 4;
10501 bfd_put_32 (abfd, MTLR_R12, p);
10502 p += 4;
05d0e962
AM
10503 if (off + 0x8000 < 0x10000)
10504 {
10505 if (load)
10506 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10507 else
10508 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10509 p += 4;
10510 }
10511 else if (off + 0x80008000ULL < 0x100000000ULL)
10512 {
10513 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10514 p += 4;
10515 if (load)
10516 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10517 else
10518 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10519 p += 4;
10520 }
10521 else
10522 {
10523 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10524 {
10525 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10526 p += 4;
10527 }
10528 else
10529 {
10530 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10531 p += 4;
10532 if (((off >> 32) & 0xffff) != 0)
10533 {
10534 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10535 p += 4;
10536 }
10537 }
10538 if (((off >> 32) & 0xffffffffULL) != 0)
10539 {
10540 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10541 p += 4;
10542 }
10543 if (PPC_HI (off) != 0)
10544 {
10545 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10546 p += 4;
10547 }
10548 if (PPC_LO (off) != 0)
10549 {
10550 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10551 p += 4;
10552 }
10553 if (load)
10554 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10555 else
10556 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10557 p += 4;
10558 }
10559 return p;
10560}
10561
10562static unsigned int
10563size_offset (bfd_vma off)
10564{
10565 unsigned int size;
10566 if (off + 0x8000 < 0x10000)
10567 size = 4;
10568 else if (off + 0x80008000ULL < 0x100000000ULL)
10569 size = 8;
10570 else
10571 {
10572 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10573 size = 4;
10574 else
10575 {
10576 size = 4;
10577 if (((off >> 32) & 0xffff) != 0)
10578 size += 4;
10579 }
10580 if (((off >> 32) & 0xffffffffULL) != 0)
10581 size += 4;
10582 if (PPC_HI (off) != 0)
10583 size += 4;
10584 if (PPC_LO (off) != 0)
10585 size += 4;
10586 size += 4;
10587 }
f891966f 10588 return size + 16;
05d0e962
AM
10589}
10590
3d58e1fc
AM
10591static unsigned int
10592num_relocs_for_offset (bfd_vma off)
10593{
10594 unsigned int num_rel;
10595 if (off + 0x8000 < 0x10000)
10596 num_rel = 1;
10597 else if (off + 0x80008000ULL < 0x100000000ULL)
10598 num_rel = 2;
10599 else
10600 {
10601 num_rel = 1;
10602 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10603 && ((off >> 32) & 0xffff) != 0)
10604 num_rel += 1;
10605 if (PPC_HI (off) != 0)
10606 num_rel += 1;
10607 if (PPC_LO (off) != 0)
10608 num_rel += 1;
10609 }
10610 return num_rel;
10611}
10612
10613static Elf_Internal_Rela *
10614emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10615 bfd_vma roff, bfd_vma targ, bfd_vma off)
10616{
10617 bfd_vma relative_targ = targ - (roff - 8);
10618 if (bfd_big_endian (info->output_bfd))
10619 roff += 2;
10620 r->r_offset = roff;
10621 r->r_addend = relative_targ + roff;
10622 if (off + 0x8000 < 0x10000)
10623 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10624 else if (off + 0x80008000ULL < 0x100000000ULL)
10625 {
10626 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10627 ++r;
10628 roff += 4;
10629 r->r_offset = roff;
10630 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10631 r->r_addend = relative_targ + roff;
10632 }
10633 else
10634 {
10635 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10636 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10637 else
10638 {
10639 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10640 if (((off >> 32) & 0xffff) != 0)
10641 {
10642 ++r;
10643 roff += 4;
10644 r->r_offset = roff;
10645 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10646 r->r_addend = relative_targ + roff;
10647 }
10648 }
10649 if (((off >> 32) & 0xffffffffULL) != 0)
10650 roff += 4;
10651 if (PPC_HI (off) != 0)
10652 {
10653 ++r;
10654 roff += 4;
10655 r->r_offset = roff;
10656 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10657 r->r_addend = relative_targ + roff;
10658 }
10659 if (PPC_LO (off) != 0)
10660 {
10661 ++r;
10662 roff += 4;
10663 r->r_offset = roff;
10664 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10665 r->r_addend = relative_targ + roff;
10666 }
10667 }
10668 return r;
10669}
10670
04bdff6a 10671static bfd_byte *
7c1f4227 10672build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
04bdff6a
AM
10673 bfd_boolean load)
10674{
10675 uint64_t insn;
10676 if (off - odd + (1ULL << 33) < 1ULL << 34)
10677 {
10678 off -= odd;
10679 if (odd)
10680 {
10681 bfd_put_32 (abfd, NOP, p);
10682 p += 4;
10683 }
10684 if (load)
10685 insn = PLD_R12_PC;
10686 else
10687 insn = PADDI_R12_PC;
10688 insn |= D34 (off);
10689 bfd_put_32 (abfd, insn >> 32, p);
10690 p += 4;
10691 bfd_put_32 (abfd, insn, p);
10692 }
10693 /* The minimum value for paddi is -0x200000000. The minimum value
10694 for li is -0x8000, which when shifted by 34 and added gives a
10695 minimum value of -0x2000200000000. The maximum value is
10696 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10697 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10698 {
10699 off -= 8 - odd;
10700 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10701 p += 4;
10702 if (!odd)
10703 {
10704 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10705 p += 4;
10706 }
10707 insn = PADDI_R12_PC | D34 (off);
10708 bfd_put_32 (abfd, insn >> 32, p);
10709 p += 4;
10710 bfd_put_32 (abfd, insn, p);
10711 p += 4;
10712 if (odd)
10713 {
10714 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10715 p += 4;
10716 }
10717 if (load)
10718 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10719 else
10720 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10721 }
10722 else
10723 {
10724 off -= odd + 8;
10725 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10726 p += 4;
10727 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10728 p += 4;
10729 if (odd)
10730 {
10731 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10732 p += 4;
10733 }
10734 insn = PADDI_R12_PC | D34 (off);
10735 bfd_put_32 (abfd, insn >> 32, p);
10736 p += 4;
10737 bfd_put_32 (abfd, insn, p);
10738 p += 4;
10739 if (!odd)
10740 {
10741 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10742 p += 4;
10743 }
10744 if (load)
10745 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10746 else
10747 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10748 }
10749 p += 4;
10750 return p;
10751}
10752
10753static unsigned int
7c1f4227 10754size_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
10755{
10756 if (off - odd + (1ULL << 33) < 1ULL << 34)
10757 return odd + 8;
10758 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10759 return 20;
10760 else
10761 return 24;
10762}
10763
10764static unsigned int
7c1f4227 10765num_relocs_for_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
10766{
10767 if (off - odd + (1ULL << 33) < 1ULL << 34)
10768 return 1;
10769 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10770 return 2;
10771 else
10772 return 3;
10773}
10774
10775static Elf_Internal_Rela *
7c1f4227 10776emit_relocs_for_power10_offset (struct bfd_link_info *info,
04bdff6a
AM
10777 Elf_Internal_Rela *r, bfd_vma roff,
10778 bfd_vma targ, bfd_vma off, int odd)
10779{
10780 if (off - odd + (1ULL << 33) < 1ULL << 34)
10781 roff += odd;
10782 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10783 {
10784 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10785 r->r_offset = roff + d_offset;
10786 r->r_addend = targ + 8 - odd - d_offset;
10787 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10788 ++r;
10789 roff += 8 - odd;
10790 }
10791 else
10792 {
10793 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10794 r->r_offset = roff + d_offset;
10795 r->r_addend = targ + 8 + odd - d_offset;
10796 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10797 ++r;
10798 roff += 4;
10799 r->r_offset = roff + d_offset;
10800 r->r_addend = targ + 4 + odd - d_offset;
10801 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10802 ++r;
10803 roff += 4 + odd;
10804 }
10805 r->r_offset = roff;
10806 r->r_addend = targ;
10807 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10808 return r;
10809}
10810
df136d64
AM
10811/* Emit .eh_frame opcode to advance pc by DELTA. */
10812
10813static bfd_byte *
10814eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10815{
10816 delta /= 4;
10817 if (delta < 64)
10818 *eh++ = DW_CFA_advance_loc + delta;
10819 else if (delta < 256)
10820 {
10821 *eh++ = DW_CFA_advance_loc1;
10822 *eh++ = delta;
10823 }
10824 else if (delta < 65536)
10825 {
10826 *eh++ = DW_CFA_advance_loc2;
10827 bfd_put_16 (abfd, delta, eh);
10828 eh += 2;
10829 }
10830 else
10831 {
10832 *eh++ = DW_CFA_advance_loc4;
10833 bfd_put_32 (abfd, delta, eh);
10834 eh += 4;
10835 }
10836 return eh;
10837}
10838
10839/* Size of required .eh_frame opcode to advance pc by DELTA. */
10840
10841static unsigned int
10842eh_advance_size (unsigned int delta)
10843{
10844 if (delta < 64 * 4)
10845 /* DW_CFA_advance_loc+[1..63]. */
10846 return 1;
10847 if (delta < 256 * 4)
10848 /* DW_CFA_advance_loc1, byte. */
10849 return 2;
10850 if (delta < 65536 * 4)
10851 /* DW_CFA_advance_loc2, 2 bytes. */
10852 return 3;
10853 /* DW_CFA_advance_loc4, 4 bytes. */
10854 return 5;
10855}
10856
794e51c0
AM
10857/* With power7 weakly ordered memory model, it is possible for ld.so
10858 to update a plt entry in one thread and have another thread see a
10859 stale zero toc entry. To avoid this we need some sort of acquire
10860 barrier in the call stub. One solution is to make the load of the
10861 toc word seem to appear to depend on the load of the function entry
10862 word. Another solution is to test for r2 being zero, and branch to
10863 the appropriate glink entry if so.
10864
10865 . fake dep barrier compare
71a39c98
AM
10866 . ld 12,xxx(2) ld 12,xxx(2)
10867 . mtctr 12 mtctr 12
10868 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10869 . add 2,2,11 cmpldi 2,0
10870 . ld 2,xxx+8(2) bnectr+
10871 . bctr b <glink_entry>
10872
10873 The solution involving the compare turns out to be faster, so
10874 that's what we use unless the branch won't reach. */
10875
10876#define ALWAYS_USE_FAKE_DEP 0
10877#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10878
794e51c0
AM
10879static inline unsigned int
10880plt_stub_size (struct ppc_link_hash_table *htab,
10881 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
10882 bfd_vma off,
10883 unsigned int odd)
794e51c0 10884{
05d0e962 10885 unsigned size;
b9e5796b 10886
05d0e962
AM
10887 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10888 {
e10a07b3 10889 if (htab->params->power10_stubs != 0)
29433886 10890 size = 8 + size_power10_offset (off, odd);
04bdff6a
AM
10891 else
10892 size = 8 + size_offset (off - 8);
05d0e962
AM
10893 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10894 size += 4;
05d0e962 10895 }
29433886 10896 else
b9e5796b 10897 {
29433886
AM
10898 size = 12;
10899 if (ALWAYS_EMIT_R2SAVE
10900 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
b9e5796b 10901 size += 4;
29433886 10902 if (PPC_HA (off) != 0)
b9e5796b 10903 size += 4;
29433886
AM
10904 if (htab->opd_abi)
10905 {
10906 size += 4;
10907 if (htab->params->plt_static_chain)
10908 size += 4;
10909 if (htab->params->plt_thread_safe
10910 && htab->elf.dynamic_sections_created
10911 && stub_entry->h != NULL
10912 && stub_entry->h->elf.dynindx != -1)
10913 size += 8;
10914 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
10915 != PPC_HA (off))
10916 size += 4;
10917 }
b9e5796b 10918 }
794e51c0 10919 if (stub_entry->h != NULL
ed7007c1 10920 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 10921 && htab->params->tls_get_addr_opt)
f378ab09 10922 {
29433886 10923 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 10924 {
29433886
AM
10925 size += 30 * 4;
10926 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
10927 || stub_entry->stub_type == ppc_stub_plt_call_both)
10928 size += 4;
9e7028aa
AM
10929 }
10930 else
10931 {
29433886
AM
10932 size += 7 * 4;
10933 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
10934 || stub_entry->stub_type == ppc_stub_plt_call_both)
10935 size += 6 * 4;
9e7028aa 10936 }
f378ab09 10937 }
794e51c0
AM
10938 return size;
10939}
10940
2420fff6
AM
10941/* Depending on the sign of plt_stub_align:
10942 If positive, return the padding to align to a 2**plt_stub_align
10943 boundary.
10944 If negative, if this stub would cross fewer 2**plt_stub_align
10945 boundaries if we align, then return the padding needed to do so. */
10946
794e51c0
AM
10947static inline unsigned int
10948plt_stub_pad (struct ppc_link_hash_table *htab,
10949 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
10950 bfd_vma plt_off,
10951 unsigned int odd)
794e51c0 10952{
2420fff6 10953 int stub_align;
1aa42141 10954 unsigned stub_size;
6f20ed8a 10955 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10956
2420fff6
AM
10957 if (htab->params->plt_stub_align >= 0)
10958 {
10959 stub_align = 1 << htab->params->plt_stub_align;
10960 if ((stub_off & (stub_align - 1)) != 0)
10961 return stub_align - (stub_off & (stub_align - 1));
10962 return 0;
10963 }
10964
10965 stub_align = 1 << -htab->params->plt_stub_align;
29433886 10966 stub_size = plt_stub_size (htab, stub_entry, plt_off, odd);
794e51c0 10967 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10968 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10969 return stub_align - (stub_off & (stub_align - 1));
10970 return 0;
10971}
10972
10973/* Build a .plt call stub. */
10974
10975static inline bfd_byte *
10976build_plt_stub (struct ppc_link_hash_table *htab,
10977 struct ppc_stub_hash_entry *stub_entry,
10978 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10979{
e7d1c40c 10980 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10981 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10982 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10983 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10984 && htab->elf.dynamic_sections_created
10985 && stub_entry->h != NULL
10986 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10987 bfd_boolean use_fake_dep = plt_thread_safe;
10988 bfd_vma cmp_branch_off = 0;
10989
10990 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10991 && plt_load_toc
794e51c0 10992 && plt_thread_safe
9e51d549
AM
10993 && !(stub_entry->h != NULL
10994 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 10995 && htab->params->tls_get_addr_opt))
794e51c0
AM
10996 {
10997 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10998 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10999 / PLT_ENTRY_SIZE (htab));
9e390558 11000 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
11001 bfd_vma to, from;
11002
68d62958
AM
11003 if (pltindex > 32768)
11004 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
11005 to = (glinkoff
11006 + htab->glink->output_offset
11007 + htab->glink->output_section->vma);
6f20ed8a 11008 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
11009 + 4 * (ALWAYS_EMIT_R2SAVE
11010 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11011 + 4 * (PPC_HA (offset) != 0)
11012 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11013 != PPC_HA (offset))
11014 + 4 * (plt_static_chain != 0)
11015 + 20
6f20ed8a
AM
11016 + stub_entry->group->stub_sec->output_offset
11017 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
11018 cmp_branch_off = to - from;
11019 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11020 }
11021
ac2df442
AM
11022 if (PPC_HA (offset) != 0)
11023 {
176a0d42
AM
11024 if (r != NULL)
11025 {
794e51c0
AM
11026 if (ALWAYS_EMIT_R2SAVE
11027 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11028 r[0].r_offset += 4;
176a0d42 11029 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 11030 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
11031 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11032 r[1].r_addend = r[0].r_addend;
b9e5796b 11033 if (plt_load_toc)
176a0d42 11034 {
b9e5796b 11035 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11036 {
b9e5796b
AM
11037 r[2].r_offset = r[1].r_offset + 4;
11038 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11039 r[2].r_addend = r[0].r_addend;
11040 }
11041 else
11042 {
11043 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11044 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11045 r[2].r_addend = r[0].r_addend + 8;
11046 if (plt_static_chain)
11047 {
11048 r[3].r_offset = r[2].r_offset + 4;
11049 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11050 r[3].r_addend = r[0].r_addend + 16;
11051 }
c7131b65 11052 }
176a0d42
AM
11053 }
11054 }
794e51c0
AM
11055 if (ALWAYS_EMIT_R2SAVE
11056 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 11057 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
11058 if (plt_load_toc)
11059 {
11060 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11061 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11062 }
11063 else
11064 {
11065 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11066 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11067 }
b9e5796b
AM
11068 if (plt_load_toc
11069 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 11070 {
71a39c98 11071 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
11072 offset = 0;
11073 }
71a39c98 11074 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11075 if (plt_load_toc)
794e51c0 11076 {
b9e5796b
AM
11077 if (use_fake_dep)
11078 {
11079 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11080 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11081 }
11082 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11083 if (plt_static_chain)
11084 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 11085 }
ac2df442
AM
11086 }
11087 else
11088 {
176a0d42
AM
11089 if (r != NULL)
11090 {
794e51c0
AM
11091 if (ALWAYS_EMIT_R2SAVE
11092 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11093 r[0].r_offset += 4;
176a0d42 11094 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 11095 if (plt_load_toc)
176a0d42 11096 {
b9e5796b 11097 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11098 {
b9e5796b
AM
11099 r[1].r_offset = r[0].r_offset + 4;
11100 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11101 r[1].r_addend = r[0].r_addend;
11102 }
11103 else
11104 {
11105 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11106 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11107 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11108 if (plt_static_chain)
11109 {
11110 r[2].r_offset = r[1].r_offset + 4;
11111 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11112 r[2].r_addend = r[0].r_addend + 8;
11113 }
c7131b65 11114 }
176a0d42
AM
11115 }
11116 }
794e51c0
AM
11117 if (ALWAYS_EMIT_R2SAVE
11118 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 11119 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 11120 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
11121 if (plt_load_toc
11122 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
11123 {
11124 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11125 offset = 0;
11126 }
71a39c98 11127 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11128 if (plt_load_toc)
794e51c0 11129 {
b9e5796b
AM
11130 if (use_fake_dep)
11131 {
11132 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11133 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11134 }
11135 if (plt_static_chain)
11136 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11137 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 11138 }
ac2df442 11139 }
b9e5796b 11140 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
11141 {
11142 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11143 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 11144 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
11145 }
11146 else
407aa07c 11147 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
11148 return p;
11149}
11150
a7f2871e
AM
11151/* Build a special .plt call stub for __tls_get_addr. */
11152
b9ca1af6 11153#define LD_R0_0R3 0xe8030000
a7f2871e
AM
11154#define LD_R12_0R3 0xe9830000
11155#define MR_R0_R3 0x7c601b78
b9ca1af6 11156#define CMPDI_R0_0 0x2c200000
a7f2871e
AM
11157#define ADD_R3_R12_R13 0x7c6c6a14
11158#define BEQLR 0x4d820020
11159#define MR_R3_R0 0x7c030378
a7f2871e 11160#define BCTRL 0x4e800421
a7f2871e 11161
29433886
AM
11162static bfd_byte *
11163build_tls_get_addr_head (struct ppc_link_hash_table *htab,
794e51c0 11164 struct ppc_stub_hash_entry *stub_entry,
29433886 11165 bfd_byte *p)
a7f2871e 11166{
e7d1c40c 11167 bfd *obfd = htab->params->stub_bfd;
794e51c0 11168
b9ca1af6 11169 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
a7f2871e 11170 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
b9ca1af6 11171 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
a7f2871e 11172 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
a7f2871e
AM
11173 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11174 bfd_put_32 (obfd, BEQLR, p), p += 4;
11175 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9e7028aa 11176
29433886
AM
11177 if (!htab->params->no_tls_get_addr_regsave)
11178 p = tls_get_addr_prologue (obfd, p, htab);
11179 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11180 || stub_entry->stub_type == ppc_stub_plt_call_both)
11181 {
9e7028aa
AM
11182 bfd_put_32 (obfd, MFLR_R0, p);
11183 p += 4;
11184 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11185 p += 4;
29433886
AM
11186 }
11187 return p;
11188}
9e7028aa 11189
29433886
AM
11190static bfd_byte *
11191build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11192 struct ppc_stub_hash_entry *stub_entry,
11193 bfd_byte *p,
11194 bfd_byte *loc)
11195{
11196 bfd *obfd = htab->params->stub_bfd;
11197
11198 if (!htab->params->no_tls_get_addr_regsave)
11199 {
11200 bfd_put_32 (obfd, BCTRL, p - 4);
11201
11202 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11203 || stub_entry->stub_type == ppc_stub_plt_call_both)
11204 {
11205 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11206 p += 4;
11207 }
11208 p = tls_get_addr_epilogue (obfd, p, htab);
11209 }
11210 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11211 || stub_entry->stub_type == ppc_stub_plt_call_both)
11212 {
9e7028aa
AM
11213 bfd_put_32 (obfd, BCTRL, p - 4);
11214
11215 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11216 p += 4;
11217 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11218 p += 4;
11219 bfd_put_32 (obfd, MTLR_R0, p);
11220 p += 4;
11221 bfd_put_32 (obfd, BLR, p);
11222 p += 4;
11223 }
a7f2871e 11224
df136d64
AM
11225 if (htab->glink_eh_frame != NULL
11226 && htab->glink_eh_frame->size != 0)
11227 {
11228 bfd_byte *base, *eh;
df136d64
AM
11229
11230 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11231 eh = base + stub_entry->group->eh_size;
29433886
AM
11232
11233 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 11234 {
29433886
AM
11235 unsigned int cfa_updt, delta, i;
11236
9e7028aa
AM
11237 /* After the bctrl, lr has been modified so we need to emit
11238 .eh_frame info saying the return address is on the stack. In
11239 fact we must put the EH info at or before the call rather
11240 than after it, because the EH info for a call needs to be
11241 specified by that point.
11242 See libgcc/unwind-dw2.c execute_cfa_program.
11243 Any stack pointer update must be described immediately after
11244 the instruction making the change, and since the stdu occurs
11245 after saving regs we put all the reg saves and the cfa
11246 change there. */
11247 cfa_updt = stub_entry->stub_offset + 18 * 4;
11248 delta = cfa_updt - stub_entry->group->lr_restore;
11249 stub_entry->group->lr_restore
11250 = stub_entry->stub_offset + (p - loc) - 4;
11251 eh = eh_advance (htab->elf.dynobj, eh, delta);
11252 *eh++ = DW_CFA_def_cfa_offset;
11253 if (htab->opd_abi)
11254 {
11255 *eh++ = 128;
11256 *eh++ = 1;
11257 }
11258 else
11259 *eh++ = 96;
11260 *eh++ = DW_CFA_offset_extended_sf;
11261 *eh++ = 65;
11262 *eh++ = (-16 / 8) & 0x7f;
11263 for (i = 4; i < 12; i++)
11264 {
11265 *eh++ = DW_CFA_offset + i;
11266 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11267 }
11268 *eh++ = (DW_CFA_advance_loc
11269 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11270 *eh++ = DW_CFA_def_cfa_offset;
11271 *eh++ = 0;
11272 for (i = 4; i < 12; i++)
11273 *eh++ = DW_CFA_restore + i;
11274 *eh++ = DW_CFA_advance_loc + 2;
29433886
AM
11275 *eh++ = DW_CFA_restore_extended;
11276 *eh++ = 65;
11277 stub_entry->group->eh_size = eh - base;
11278 }
11279 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11280 || stub_entry->stub_type == ppc_stub_plt_call_both)
11281 {
11282 unsigned int lr_used, delta;
11283
11284 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11285 delta = lr_used - stub_entry->group->lr_restore;
11286 stub_entry->group->lr_restore = lr_used + 16;
11287 eh = eh_advance (htab->elf.dynobj, eh, delta);
11288 *eh++ = DW_CFA_offset_extended_sf;
11289 *eh++ = 65;
11290 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11291 *eh++ = DW_CFA_advance_loc + 4;
11292 *eh++ = DW_CFA_restore_extended;
11293 *eh++ = 65;
11294 stub_entry->group->eh_size = eh - base;
9e7028aa 11295 }
df136d64 11296 }
a7f2871e
AM
11297 return p;
11298}
11299
176a0d42
AM
11300static Elf_Internal_Rela *
11301get_relocs (asection *sec, int count)
11302{
11303 Elf_Internal_Rela *relocs;
11304 struct bfd_elf_section_data *elfsec_data;
11305
11306 elfsec_data = elf_section_data (sec);
11307 relocs = elfsec_data->relocs;
11308 if (relocs == NULL)
11309 {
11310 bfd_size_type relsize;
11311 relsize = sec->reloc_count * sizeof (*relocs);
11312 relocs = bfd_alloc (sec->owner, relsize);
11313 if (relocs == NULL)
11314 return NULL;
11315 elfsec_data->relocs = relocs;
d4730f92
BS
11316 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11317 sizeof (Elf_Internal_Shdr));
11318 if (elfsec_data->rela.hdr == NULL)
11319 return NULL;
11320 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11321 * sizeof (Elf64_External_Rela));
11322 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
11323 sec->reloc_count = 0;
11324 }
11325 relocs += sec->reloc_count;
11326 sec->reloc_count += count;
11327 return relocs;
11328}
11329
3d58e1fc
AM
11330/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11331 forms, to the equivalent relocs against the global symbol given by
11332 STUB_ENTRY->H. */
11333
11334static bfd_boolean
11335use_global_in_relocs (struct ppc_link_hash_table *htab,
11336 struct ppc_stub_hash_entry *stub_entry,
11337 Elf_Internal_Rela *r, unsigned int num_rel)
11338{
11339 struct elf_link_hash_entry **hashes;
11340 unsigned long symndx;
11341 struct ppc_link_hash_entry *h;
11342 bfd_vma symval;
11343
11344 /* Relocs are always against symbols in their own object file. Fake
11345 up global sym hashes for the stub bfd (which has no symbols). */
11346 hashes = elf_sym_hashes (htab->params->stub_bfd);
11347 if (hashes == NULL)
11348 {
11349 bfd_size_type hsize;
11350
11351 /* When called the first time, stub_globals will contain the
11352 total number of symbols seen during stub sizing. After
11353 allocating, stub_globals is used as an index to fill the
11354 hashes array. */
11355 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11356 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11357 if (hashes == NULL)
11358 return FALSE;
11359 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11360 htab->stub_globals = 1;
11361 }
11362 symndx = htab->stub_globals++;
11363 h = stub_entry->h;
11364 hashes[symndx] = &h->elf;
11365 if (h->oh != NULL && h->oh->is_func)
11366 h = ppc_follow_link (h->oh);
11367 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11368 || h->elf.root.type == bfd_link_hash_defweak);
ed7007c1 11369 symval = defined_sym_val (&h->elf);
3d58e1fc
AM
11370 while (num_rel-- != 0)
11371 {
11372 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11373 if (h->elf.root.u.def.section != stub_entry->target_section)
11374 {
11375 /* H is an opd symbol. The addend must be zero, and the
11376 branch reloc is the only one we can convert. */
11377 r->r_addend = 0;
11378 break;
11379 }
11380 else
11381 r->r_addend -= symval;
11382 --r;
11383 }
11384 return TRUE;
11385}
11386
aa374f67 11387static bfd_vma
25f53a85 11388get_r2off (struct bfd_link_info *info,
aa374f67
AM
11389 struct ppc_stub_hash_entry *stub_entry)
11390{
25f53a85 11391 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 11392 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
11393
11394 if (r2off == 0)
11395 {
11396 /* Support linking -R objects. Get the toc pointer from the
11397 opd entry. */
11398 char buf[8];
b9e5796b
AM
11399 if (!htab->opd_abi)
11400 return r2off;
aa374f67
AM
11401 asection *opd = stub_entry->h->elf.root.u.def.section;
11402 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11403
11404 if (strcmp (opd->name, ".opd") != 0
11405 || opd->reloc_count != 0)
11406 {
2cdcc330
AM
11407 info->callbacks->einfo
11408 (_("%P: cannot find opd entry toc for `%pT'\n"),
11409 stub_entry->h->elf.root.root.string);
aa374f67 11410 bfd_set_error (bfd_error_bad_value);
a7c49797 11411 return (bfd_vma) -1;
aa374f67
AM
11412 }
11413 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11414 return (bfd_vma) -1;
aa374f67 11415 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11416 r2off -= elf_gp (info->output_bfd);
aa374f67 11417 }
6f20ed8a 11418 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11419 return r2off;
11420}
11421
b34976b6 11422static bfd_boolean
4ce794b7 11423ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11424{
721956f4
AM
11425 struct ppc_stub_hash_entry *stub_entry;
11426 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11427 struct bfd_link_info *info;
11428 struct ppc_link_hash_table *htab;
29433886 11429 bfd *obfd;
721956f4 11430 bfd_byte *loc;
3d58e1fc 11431 bfd_byte *p, *relp;
1aa42141 11432 bfd_vma targ, off;
176a0d42 11433 Elf_Internal_Rela *r;
e054468f 11434 asection *plt;
3d58e1fc 11435 int num_rel;
04bdff6a 11436 int odd;
29433886 11437 bfd_boolean is_tga;
5d1634d7 11438
721956f4
AM
11439 /* Massage our args to the form they really have. */
11440 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11441 info = in_arg;
5d1634d7 11442
abf874aa
CL
11443 /* Fail if the target section could not be assigned to an output
11444 section. The user should fix his linker script. */
11445 if (stub_entry->target_section != NULL
11446 && stub_entry->target_section->output_section == NULL
11447 && info->non_contiguous_regions)
53215f21
CL
11448 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
11449 "Retry without --enable-non-contiguous-regions.\n"),
11450 stub_entry->target_section);
abf874aa
CL
11451
11452 /* Same for the group. */
11453 if (stub_entry->group->stub_sec != NULL
11454 && stub_entry->group->stub_sec->output_section == NULL
11455 && info->non_contiguous_regions)
53215f21
CL
11456 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11457 "output section. Retry without "
11458 "--enable-non-contiguous-regions.\n"),
11459 stub_entry->group->stub_sec,
11460 stub_entry->target_section);
abf874aa 11461
5d1634d7 11462 htab = ppc_hash_table (info);
4dfe6ac6
NC
11463 if (htab == NULL)
11464 return FALSE;
5d1634d7 11465
1aa42141 11466 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 11467 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11468
4ce794b7 11469 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 11470 switch (stub_entry->stub_type)
5d1634d7 11471 {
721956f4 11472 case ppc_stub_long_branch:
ad8e1ba5 11473 case ppc_stub_long_branch_r2off:
721956f4 11474 /* Branches are relative. This is where we are going to. */
1aa42141 11475 targ = (stub_entry->target_value
6911b7dc
AM
11476 + stub_entry->target_section->output_offset
11477 + stub_entry->target_section->output_section->vma);
1aa42141 11478 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11479
721956f4 11480 /* And this is where we are coming from. */
1aa42141
AM
11481 off = (stub_entry->stub_offset
11482 + stub_entry->group->stub_sec->output_offset
11483 + stub_entry->group->stub_sec->output_section->vma);
11484 off = targ - off;
e86ce104 11485
9e390558 11486 p = loc;
29433886 11487 obfd = htab->params->stub_bfd;
ac2df442 11488 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 11489 {
25f53a85 11490 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11491
a7c49797 11492 if (r2off == (bfd_vma) -1)
aa374f67
AM
11493 {
11494 htab->stub_error = TRUE;
11495 return FALSE;
11496 }
29433886 11497 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11498 p += 4;
ac2df442
AM
11499 if (PPC_HA (r2off) != 0)
11500 {
29433886 11501 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
9e390558 11502 p += 4;
a7c49797
AM
11503 }
11504 if (PPC_LO (r2off) != 0)
11505 {
29433886 11506 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
9e390558 11507 p += 4;
ac2df442 11508 }
9e390558 11509 off -= p - loc;
ad8e1ba5 11510 }
29433886 11511 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
9e390558 11512 p += 4;
ad8e1ba5 11513
5c3dead3
AM
11514 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11515 {
cf97bcb0
AM
11516 _bfd_error_handler
11517 (_("long branch stub `%s' offset overflow"),
bc30df16 11518 stub_entry->root.string);
5c3dead3
AM
11519 htab->stub_error = TRUE;
11520 return FALSE;
11521 }
ee75fd95
AM
11522
11523 if (info->emitrelocations)
11524 {
6f20ed8a 11525 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
11526 if (r == NULL)
11527 return FALSE;
9e390558 11528 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11529 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11530 r->r_addend = targ;
3d58e1fc
AM
11531 if (stub_entry->h != NULL
11532 && !use_global_in_relocs (htab, stub_entry, r, 1))
11533 return FALSE;
ee75fd95 11534 }
721956f4 11535 break;
e86ce104 11536
721956f4 11537 case ppc_stub_plt_branch:
ad8e1ba5 11538 case ppc_stub_plt_branch_r2off:
721956f4
AM
11539 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11540 stub_entry->root.string + 9,
b34976b6 11541 FALSE, FALSE);
721956f4
AM
11542 if (br_entry == NULL)
11543 {
cf97bcb0
AM
11544 _bfd_error_handler (_("can't find branch stub `%s'"),
11545 stub_entry->root.string);
b34976b6
AM
11546 htab->stub_error = TRUE;
11547 return FALSE;
721956f4
AM
11548 }
11549
1aa42141 11550 targ = (stub_entry->target_value
176a0d42
AM
11551 + stub_entry->target_section->output_offset
11552 + stub_entry->target_section->output_section->vma);
6911b7dc 11553 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 11554 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11555
1aa42141 11556 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11557 htab->brlt->contents + br_entry->offset);
721956f4 11558
f94498ff 11559 if (br_entry->iter == htab->stub_iteration)
721956f4 11560 {
f94498ff 11561 br_entry->iter = 0;
84f5d08e 11562
f94498ff 11563 if (htab->relbrlt != NULL)
84f5d08e 11564 {
f94498ff
AM
11565 /* Create a reloc for the branch lookup table entry. */
11566 Elf_Internal_Rela rela;
11567 bfd_byte *rl;
11568
11569 rela.r_offset = (br_entry->offset
11570 + htab->brlt->output_offset
11571 + htab->brlt->output_section->vma);
11572 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11573 rela.r_addend = targ;
f94498ff
AM
11574
11575 rl = htab->relbrlt->contents;
11576 rl += (htab->relbrlt->reloc_count++
11577 * sizeof (Elf64_External_Rela));
11578 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11579 }
11580 else if (info->emitrelocations)
11581 {
176a0d42
AM
11582 r = get_relocs (htab->brlt, 1);
11583 if (r == NULL)
11584 return FALSE;
11585 /* brlt, being SEC_LINKER_CREATED does not go through the
11586 normal reloc processing. Symbols and offsets are not
11587 translated from input file to output file form, so
11588 set up the offset per the output file. */
f94498ff
AM
11589 r->r_offset = (br_entry->offset
11590 + htab->brlt->output_offset
11591 + htab->brlt->output_section->vma);
11592 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11593 r->r_addend = targ;
84f5d08e 11594 }
84f5d08e 11595 }
721956f4 11596
1aa42141 11597 targ = (br_entry->offset
176a0d42
AM
11598 + htab->brlt->output_offset
11599 + htab->brlt->output_section->vma);
11600
1aa42141
AM
11601 off = (elf_gp (info->output_bfd)
11602 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11603 off = targ - off;
721956f4 11604
ad8e1ba5 11605 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11606 {
25f53a85 11607 info->callbacks->einfo
c1c8c1ef 11608 (_("%P: linkage table error against `%pT'\n"),
721956f4 11609 stub_entry->root.string);
5d1634d7 11610 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11611 htab->stub_error = TRUE;
11612 return FALSE;
5d1634d7 11613 }
41bd81ab 11614
176a0d42
AM
11615 if (info->emitrelocations)
11616 {
6f20ed8a 11617 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11618 if (r == NULL)
11619 return FALSE;
6f20ed8a 11620 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11621 if (bfd_big_endian (info->output_bfd))
11622 r[0].r_offset += 2;
00f412ee 11623 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11624 r[0].r_offset += 4;
11625 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11626 r[0].r_addend = targ;
176a0d42
AM
11627 if (PPC_HA (off) != 0)
11628 {
11629 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11630 r[1].r_offset = r[0].r_offset + 4;
11631 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11632 r[1].r_addend = r[0].r_addend;
11633 }
11634 }
11635
9e390558 11636 p = loc;
29433886 11637 obfd = htab->params->stub_bfd;
00f412ee 11638 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11639 {
176a0d42 11640 if (PPC_HA (off) != 0)
ac2df442 11641 {
29433886 11642 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11643 p += 4;
29433886 11644 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11645 }
11646 else
29433886 11647 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11648 }
11649 else
11650 {
25f53a85 11651 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11652
a7c49797 11653 if (r2off == (bfd_vma) -1)
aa374f67
AM
11654 {
11655 htab->stub_error = TRUE;
11656 return FALSE;
11657 }
ad8e1ba5 11658
29433886 11659 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11660 p += 4;
176a0d42 11661 if (PPC_HA (off) != 0)
ac2df442 11662 {
29433886 11663 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11664 p += 4;
29433886 11665 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11666 }
11667 else
29433886 11668 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11669
11670 if (PPC_HA (r2off) != 0)
11671 {
9e390558 11672 p += 4;
29433886 11673 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11674 }
11675 if (PPC_LO (r2off) != 0)
11676 {
9e390558 11677 p += 4;
29433886 11678 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11679 }
ad8e1ba5 11680 }
9e390558 11681 p += 4;
29433886 11682 bfd_put_32 (obfd, MTCTR_R12, p);
9e390558 11683 p += 4;
29433886 11684 bfd_put_32 (obfd, BCTR, p);
407aa07c 11685 p += 4;
721956f4 11686 break;
5d1634d7 11687
05d0e962
AM
11688 case ppc_stub_long_branch_notoc:
11689 case ppc_stub_long_branch_both:
11690 case ppc_stub_plt_branch_notoc:
11691 case ppc_stub_plt_branch_both:
11692 case ppc_stub_plt_call_notoc:
11693 case ppc_stub_plt_call_both:
11694 p = loc;
f891966f 11695 off = (stub_entry->stub_offset
05d0e962
AM
11696 + stub_entry->group->stub_sec->output_offset
11697 + stub_entry->group->stub_sec->output_section->vma);
29433886
AM
11698 obfd = htab->params->stub_bfd;
11699 is_tga = ((stub_entry->stub_type == ppc_stub_plt_call_notoc
11700 || stub_entry->stub_type == ppc_stub_plt_call_both)
12cf8b93 11701 && stub_entry->h != NULL
29433886
AM
11702 && is_tls_get_addr (&stub_entry->h->elf, htab)
11703 && htab->params->tls_get_addr_opt);
11704 if (is_tga)
11705 {
11706 p = build_tls_get_addr_head (htab, stub_entry, p);
11707 off += p - loc;
11708 }
05d0e962
AM
11709 if (stub_entry->stub_type == ppc_stub_long_branch_both
11710 || stub_entry->stub_type == ppc_stub_plt_branch_both
11711 || stub_entry->stub_type == ppc_stub_plt_call_both)
11712 {
11713 off += 4;
29433886 11714 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
05d0e962
AM
11715 p += 4;
11716 }
11717 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11718 {
11719 targ = stub_entry->plt_ent->plt.offset & ~1;
11720 if (targ >= (bfd_vma) -2)
11721 abort ();
11722
11723 plt = htab->elf.splt;
30845f11 11724 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
05d0e962
AM
11725 {
11726 if (stub_entry->symtype == STT_GNU_IFUNC)
11727 plt = htab->elf.iplt;
11728 else
11729 plt = htab->pltlocal;
11730 }
11731 targ += plt->output_offset + plt->output_section->vma;
11732 }
11733 else
11734 targ = (stub_entry->target_value
11735 + stub_entry->target_section->output_offset
11736 + stub_entry->target_section->output_section->vma);
04bdff6a 11737 odd = off & 4;
05d0e962 11738 off = targ - off;
f891966f 11739
3d58e1fc
AM
11740 relp = p;
11741 num_rel = 0;
e10a07b3 11742 if (htab->params->power10_stubs != 0)
04bdff6a
AM
11743 {
11744 bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
29433886 11745 p = build_power10_offset (obfd, p, off, odd, load);
04bdff6a
AM
11746 }
11747 else
11748 {
29433886
AM
11749 if (htab->glink_eh_frame != NULL
11750 && htab->glink_eh_frame->size != 0)
11751 {
11752 bfd_byte *base, *eh;
11753 unsigned int lr_used, delta;
11754
11755 base = (htab->glink_eh_frame->contents
11756 + stub_entry->group->eh_base + 17);
11757 eh = base + stub_entry->group->eh_size;
11758 lr_used = stub_entry->stub_offset + (p - loc) + 8;
11759 delta = lr_used - stub_entry->group->lr_restore;
11760 stub_entry->group->lr_restore = lr_used + 8;
11761 eh = eh_advance (htab->elf.dynobj, eh, delta);
11762 *eh++ = DW_CFA_register;
11763 *eh++ = 65;
11764 *eh++ = 12;
11765 *eh++ = DW_CFA_advance_loc + 2;
11766 *eh++ = DW_CFA_restore_extended;
11767 *eh++ = 65;
11768 stub_entry->group->eh_size = eh - base;
11769 }
11770
04bdff6a
AM
11771 /* The notoc stubs calculate their target (either a PLT entry or
11772 the global entry point of a function) relative to the PC
11773 returned by the "bcl" two instructions past the start of the
11774 sequence emitted by build_offset. The offset is therefore 8
11775 less than calculated from the start of the sequence. */
11776 off -= 8;
29433886 11777 p = build_offset (obfd, p, off,
04bdff6a
AM
11778 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11779 }
11780
f891966f 11781 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
05d0e962 11782 {
f891966f 11783 bfd_vma from;
3d58e1fc 11784 num_rel = 1;
f891966f
AM
11785 from = (stub_entry->stub_offset
11786 + stub_entry->group->stub_sec->output_offset
11787 + stub_entry->group->stub_sec->output_section->vma
11788 + (p - loc));
29433886 11789 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
11790 }
11791 else
11792 {
29433886 11793 bfd_put_32 (obfd, MTCTR_R12, p);
05d0e962 11794 p += 4;
29433886 11795 bfd_put_32 (obfd, BCTR, p);
05d0e962
AM
11796 }
11797 p += 4;
df136d64 11798
29433886
AM
11799 if (is_tga)
11800 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
11801
3d58e1fc
AM
11802 if (info->emitrelocations)
11803 {
04bdff6a 11804 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
e10a07b3 11805 if (htab->params->power10_stubs != 0)
7c1f4227 11806 num_rel += num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
11807 else
11808 {
11809 num_rel += num_relocs_for_offset (off);
11810 roff += 16;
11811 }
3d58e1fc
AM
11812 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11813 if (r == NULL)
11814 return FALSE;
e10a07b3 11815 if (htab->params->power10_stubs != 0)
7c1f4227 11816 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
04bdff6a
AM
11817 else
11818 r = emit_relocs_for_offset (info, r, roff, targ, off);
3d58e1fc
AM
11819 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11820 || stub_entry->stub_type == ppc_stub_long_branch_both)
11821 {
11822 ++r;
11823 roff = p - 4 - stub_entry->group->stub_sec->contents;
11824 r->r_offset = roff;
11825 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11826 r->r_addend = targ;
11827 if (stub_entry->h != NULL
11828 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11829 return FALSE;
11830 }
11831 }
05d0e962
AM
11832 break;
11833
721956f4 11834 case ppc_stub_plt_call:
794e51c0 11835 case ppc_stub_plt_call_r2save:
e054468f 11836 if (stub_entry->h != NULL
b31867b6
AM
11837 && stub_entry->h->is_func_descriptor
11838 && stub_entry->h->oh != NULL)
c862ae31 11839 {
b31867b6
AM
11840 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11841
11842 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11843 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11844 if (fh->elf.root.type == bfd_link_hash_undefined
11845 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11846 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11847 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11848 }
11849
721956f4 11850 /* Now build the stub. */
1aa42141
AM
11851 targ = stub_entry->plt_ent->plt.offset & ~1;
11852 if (targ >= (bfd_vma) -2)
721956f4
AM
11853 abort ();
11854
33e44f2e 11855 plt = htab->elf.splt;
30845f11 11856 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
2d7ad24e
AM
11857 {
11858 if (stub_entry->symtype == STT_GNU_IFUNC)
11859 plt = htab->elf.iplt;
11860 else
11861 plt = htab->pltlocal;
11862 }
1aa42141 11863 targ += plt->output_offset + plt->output_section->vma;
e054468f 11864
1aa42141
AM
11865 off = (elf_gp (info->output_bfd)
11866 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11867 off = targ - off;
721956f4 11868
ad8e1ba5 11869 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11870 {
25f53a85 11871 info->callbacks->einfo
695344c0 11872 /* xgettext:c-format */
c1c8c1ef 11873 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11874 stub_entry->h != NULL
11875 ? stub_entry->h->elf.root.root.string
11876 : "<local sym>");
721956f4 11877 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11878 htab->stub_error = TRUE;
11879 return FALSE;
721956f4
AM
11880 }
11881
176a0d42
AM
11882 r = NULL;
11883 if (info->emitrelocations)
11884 {
6f20ed8a 11885 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11886 ((PPC_HA (off) != 0)
11887 + (htab->opd_abi
e7d1c40c 11888 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11889 && PPC_HA (off + 16) == PPC_HA (off))
11890 : 1)));
176a0d42
AM
11891 if (r == NULL)
11892 return FALSE;
6f20ed8a 11893 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11894 if (bfd_big_endian (info->output_bfd))
11895 r[0].r_offset += 2;
1aa42141 11896 r[0].r_addend = targ;
176a0d42 11897 }
29433886
AM
11898 p = loc;
11899 obfd = htab->params->stub_bfd;
11900 is_tga = (stub_entry->h != NULL
11901 && is_tls_get_addr (&stub_entry->h->elf, htab)
11902 && htab->params->tls_get_addr_opt);
11903 if (is_tga)
11904 {
11905 p = build_tls_get_addr_head (htab, stub_entry, p);
11906 if (r != NULL)
11907 r[0].r_offset += p - loc;
11908 }
11909 p = build_plt_stub (htab, stub_entry, p, off, r);
11910 if (is_tga)
11911 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
721956f4
AM
11912 break;
11913
a4b6fadd
AM
11914 case ppc_stub_save_res:
11915 return TRUE;
11916
721956f4
AM
11917 default:
11918 BFD_FAIL ();
b34976b6 11919 return FALSE;
721956f4
AM
11920 }
11921
1aa42141 11922 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 11923
e7d1c40c 11924 if (htab->params->emit_stub_syms)
97b639ba
AM
11925 {
11926 struct elf_link_hash_entry *h;
ee75fd95
AM
11927 size_t len1, len2;
11928 char *name;
11929 const char *const stub_str[] = { "long_branch",
05d0e962
AM
11930 "long_branch",
11931 "long_branch",
3f6ff479
AM
11932 "long_branch",
11933 "plt_branch",
ee75fd95 11934 "plt_branch",
05d0e962
AM
11935 "plt_branch",
11936 "plt_branch",
11937 "plt_call",
11938 "plt_call",
794e51c0 11939 "plt_call",
ee75fd95
AM
11940 "plt_call" };
11941
11942 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11943 len2 = strlen (stub_entry->root.string);
11944 name = bfd_malloc (len1 + len2 + 2);
11945 if (name == NULL)
11946 return FALSE;
11947 memcpy (name, stub_entry->root.string, 9);
11948 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11949 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11950 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11951 if (h == NULL)
11952 return FALSE;
11953 if (h->root.type == bfd_link_hash_new)
11954 {
11955 h->root.type = bfd_link_hash_defined;
6f20ed8a 11956 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11957 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11958 h->ref_regular = 1;
11959 h->def_regular = 1;
11960 h->ref_regular_nonweak = 1;
11961 h->forced_local = 1;
11962 h->non_elf = 0;
2ec55de3 11963 h->root.linker_def = 1;
97b639ba
AM
11964 }
11965 }
11966
b34976b6 11967 return TRUE;
721956f4
AM
11968}
11969
11970/* As above, but don't actually build the stub. Just bump offset so
11971 we know stub section sizes, and select plt_branch stubs where
11972 long_branch stubs won't do. */
11973
b34976b6 11974static bfd_boolean
4ce794b7 11975ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11976{
11977 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11978 struct bfd_link_info *info;
721956f4 11979 struct ppc_link_hash_table *htab;
f891966f
AM
11980 asection *plt;
11981 bfd_vma targ, off, r2off;
04bdff6a 11982 unsigned int size, extra, lr_used, delta, odd;
721956f4
AM
11983
11984 /* Massage our args to the form they really have. */
11985 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11986 info = in_arg;
11987
11988 htab = ppc_hash_table (info);
4dfe6ac6
NC
11989 if (htab == NULL)
11990 return FALSE;
721956f4 11991
abf874aa
CL
11992 /* Fail if the target section could not be assigned to an output
11993 section. The user should fix his linker script. */
11994 if (stub_entry->target_section != NULL
11995 && stub_entry->target_section->output_section == NULL
11996 && info->non_contiguous_regions)
53215f21
CL
11997 info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
11998 "Retry without --enable-non-contiguous-regions.\n"),
11999 stub_entry->target_section);
abf874aa
CL
12000
12001 /* Same for the group. */
12002 if (stub_entry->group->stub_sec != NULL
12003 && stub_entry->group->stub_sec->output_section == NULL
12004 && info->non_contiguous_regions)
53215f21
CL
12005 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
12006 "output section. Retry without "
12007 "--enable-non-contiguous-regions.\n"),
12008 stub_entry->group->stub_sec,
12009 stub_entry->target_section);
abf874aa 12010
1aa42141
AM
12011 /* Make a note of the offset within the stubs for this entry. */
12012 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12013
a4b6fadd
AM
12014 if (stub_entry->h != NULL
12015 && stub_entry->h->save_res
12016 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12017 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12018 {
12019 /* Don't make stubs to out-of-line register save/restore
12020 functions. Instead, emit copies of the functions. */
12021 stub_entry->group->needs_save_res = 1;
12022 stub_entry->stub_type = ppc_stub_save_res;
12023 return TRUE;
12024 }
12025
f891966f 12026 switch (stub_entry->stub_type)
721956f4 12027 {
f891966f
AM
12028 case ppc_stub_plt_branch:
12029 case ppc_stub_plt_branch_r2off:
12030 /* Reset the stub type from the plt branch variant in case we now
12031 can reach with a shorter stub. */
12032 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12033 /* Fall through. */
12034 case ppc_stub_long_branch:
12035 case ppc_stub_long_branch_r2off:
1aa42141
AM
12036 targ = (stub_entry->target_value
12037 + stub_entry->target_section->output_offset
12038 + stub_entry->target_section->output_section->vma);
f891966f 12039 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
1aa42141
AM
12040 off = (stub_entry->stub_offset
12041 + stub_entry->group->stub_sec->output_offset
12042 + stub_entry->group->stub_sec->output_section->vma);
721956f4 12043
ad8e1ba5 12044 size = 4;
f891966f 12045 r2off = 0;
ad8e1ba5
AM
12046 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
12047 {
25f53a85 12048 r2off = get_r2off (info, stub_entry);
a7c49797 12049 if (r2off == (bfd_vma) -1)
aa374f67
AM
12050 {
12051 htab->stub_error = TRUE;
12052 return FALSE;
12053 }
a7c49797 12054 size = 8;
ac2df442 12055 if (PPC_HA (r2off) != 0)
a7c49797
AM
12056 size += 4;
12057 if (PPC_LO (r2off) != 0)
12058 size += 4;
1aa42141 12059 off += size - 4;
ad8e1ba5 12060 }
1aa42141 12061 off = targ - off;
ad8e1ba5 12062
f891966f
AM
12063 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12064 Do the same for -R objects without function descriptors. */
12065 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
12066 && r2off == 0
12067 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12068 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 12069 {
f891966f 12070 struct ppc_branch_hash_entry *br_entry;
df136d64 12071
f891966f
AM
12072 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12073 stub_entry->root.string + 9,
12074 TRUE, FALSE);
12075 if (br_entry == NULL)
721956f4 12076 {
f891966f
AM
12077 _bfd_error_handler (_("can't build branch stub `%s'"),
12078 stub_entry->root.string);
12079 htab->stub_error = TRUE;
12080 return FALSE;
721956f4
AM
12081 }
12082
f891966f 12083 if (br_entry->iter != htab->stub_iteration)
721956f4 12084 {
f891966f
AM
12085 br_entry->iter = htab->stub_iteration;
12086 br_entry->offset = htab->brlt->size;
12087 htab->brlt->size += 8;
63bc6f6c 12088
f891966f
AM
12089 if (htab->relbrlt != NULL)
12090 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12091 else if (info->emitrelocations)
84f5d08e 12092 {
f891966f
AM
12093 htab->brlt->reloc_count += 1;
12094 htab->brlt->flags |= SEC_RELOC;
05d0e962 12095 }
f891966f 12096 }
ac2df442 12097
f891966f
AM
12098 targ = (br_entry->offset
12099 + htab->brlt->output_offset
12100 + htab->brlt->output_section->vma);
12101 off = (elf_gp (info->output_bfd)
12102 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12103 off = targ - off;
176a0d42 12104
f891966f
AM
12105 if (info->emitrelocations)
12106 {
12107 stub_entry->group->stub_sec->reloc_count
12108 += 1 + (PPC_HA (off) != 0);
12109 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12110 }
05d0e962 12111
f891966f
AM
12112 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
12113 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
12114 {
12115 size = 12;
12116 if (PPC_HA (off) != 0)
12117 size = 16;
ac2df442 12118 }
f891966f 12119 else
ac2df442 12120 {
f891966f
AM
12121 size = 16;
12122 if (PPC_HA (off) != 0)
12123 size += 4;
12124
12125 if (PPC_HA (r2off) != 0)
12126 size += 4;
12127 if (PPC_LO (r2off) != 0)
12128 size += 4;
ac2df442 12129 }
721956f4 12130 }
f891966f
AM
12131 else if (info->emitrelocations)
12132 {
12133 stub_entry->group->stub_sec->reloc_count += 1;
12134 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12135 }
12136 break;
12137
12138 case ppc_stub_plt_branch_notoc:
12139 case ppc_stub_plt_branch_both:
12140 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12141 /* Fall through. */
12142 case ppc_stub_long_branch_notoc:
12143 case ppc_stub_long_branch_both:
12144 off = (stub_entry->stub_offset
12145 + stub_entry->group->stub_sec->output_offset
12146 + stub_entry->group->stub_sec->output_section->vma);
12147 size = 0;
12148 if (stub_entry->stub_type == ppc_stub_long_branch_both)
12149 size = 4;
12150 off += size;
12151 targ = (stub_entry->target_value
12152 + stub_entry->target_section->output_offset
12153 + stub_entry->target_section->output_section->vma);
04bdff6a 12154 odd = off & 4;
f891966f
AM
12155 off = targ - off;
12156
3d58e1fc
AM
12157 if (info->emitrelocations)
12158 {
04bdff6a 12159 unsigned int num_rel;
e10a07b3 12160 if (htab->params->power10_stubs != 0)
7c1f4227 12161 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12162 else
12163 num_rel = num_relocs_for_offset (off - 8);
12164 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12165 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12166 }
12167
e10a07b3 12168 if (htab->params->power10_stubs != 0)
7c1f4227 12169 extra = size_power10_offset (off, odd);
04bdff6a
AM
12170 else
12171 extra = size_offset (off - 8);
f891966f
AM
12172 /* Include branch insn plus those in the offset sequence. */
12173 size += 4 + extra;
12174 /* The branch insn is at the end, or "extra" bytes along. So
12175 its offset will be "extra" bytes less that that already
12176 calculated. */
12177 off -= extra;
12178
e10a07b3 12179 if (htab->params->power10_stubs == 0)
04bdff6a
AM
12180 {
12181 /* After the bcl, lr has been modified so we need to emit
12182 .eh_frame info saying the return address is in r12. */
12183 lr_used = stub_entry->stub_offset + 8;
12184 if (stub_entry->stub_type == ppc_stub_long_branch_both)
12185 lr_used += 4;
12186 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12187 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12188 DW_CFA_restore_extended 65. */
12189 delta = lr_used - stub_entry->group->lr_restore;
12190 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12191 stub_entry->group->lr_restore = lr_used + 8;
12192 }
f891966f
AM
12193
12194 /* If the branch can't reach, use a plt_branch. */
12195 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12196 {
12197 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
12198 - ppc_stub_long_branch_notoc);
12199 size += 4;
12200 }
3d58e1fc
AM
12201 else if (info->emitrelocations)
12202 stub_entry->group->stub_sec->reloc_count +=1;
f891966f
AM
12203 break;
12204
12205 case ppc_stub_plt_call_notoc:
12206 case ppc_stub_plt_call_both:
29433886 12207 lr_used = 0;
12cf8b93
AM
12208 if (stub_entry->h != NULL
12209 && is_tls_get_addr (&stub_entry->h->elf, htab)
29433886
AM
12210 && htab->params->tls_get_addr_opt)
12211 {
12212 lr_used += 7 * 4;
12213 if (!htab->params->no_tls_get_addr_regsave)
12214 lr_used += 11 * 4;
12215 else if (stub_entry->stub_type == ppc_stub_plt_call_both)
12216 lr_used += 2 * 4;
12217 }
f891966f 12218 if (stub_entry->stub_type == ppc_stub_plt_call_both)
29433886 12219 lr_used += 4;
f891966f
AM
12220 targ = stub_entry->plt_ent->plt.offset & ~1;
12221 if (targ >= (bfd_vma) -2)
12222 abort ();
12223
12224 plt = htab->elf.splt;
30845f11 12225 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12226 {
12227 if (stub_entry->symtype == STT_GNU_IFUNC)
12228 plt = htab->elf.iplt;
12229 else
12230 plt = htab->pltlocal;
12231 }
12232 targ += plt->output_offset + plt->output_section->vma;
29433886
AM
12233 off = (stub_entry->stub_offset
12234 + stub_entry->group->stub_sec->output_offset
12235 + stub_entry->group->stub_sec->output_section->vma
12236 + lr_used);
04bdff6a 12237 odd = off & 4;
f891966f
AM
12238 off = targ - off;
12239
12240 if (htab->params->plt_stub_align != 0)
12241 {
29433886 12242 unsigned pad = plt_stub_pad (htab, stub_entry, off, odd);
f891966f
AM
12243
12244 stub_entry->group->stub_sec->size += pad;
12245 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12246 off -= pad;
29433886 12247 odd ^= pad & 4;
f891966f
AM
12248 }
12249
3d58e1fc
AM
12250 if (info->emitrelocations)
12251 {
04bdff6a 12252 unsigned int num_rel;
e10a07b3 12253 if (htab->params->power10_stubs != 0)
7c1f4227 12254 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12255 else
12256 num_rel = num_relocs_for_offset (off - 8);
12257 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12258 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12259 }
12260
29433886 12261 size = plt_stub_size (htab, stub_entry, off, odd);
f891966f 12262
e10a07b3 12263 if (htab->params->power10_stubs == 0)
04bdff6a
AM
12264 {
12265 /* After the bcl, lr has been modified so we need to emit
12266 .eh_frame info saying the return address is in r12. */
29433886 12267 lr_used += stub_entry->stub_offset + 8;
04bdff6a
AM
12268 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12269 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12270 DW_CFA_restore_extended 65. */
12271 delta = lr_used - stub_entry->group->lr_restore;
12272 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12273 stub_entry->group->lr_restore = lr_used + 8;
12274 }
29433886
AM
12275 if ((stub_entry->stub_type == ppc_stub_plt_call_notoc
12276 || stub_entry->stub_type == ppc_stub_plt_call_both)
12cf8b93 12277 && stub_entry->h != NULL
29433886
AM
12278 && is_tls_get_addr (&stub_entry->h->elf, htab)
12279 && htab->params->tls_get_addr_opt)
12280 {
12281 if (!htab->params->no_tls_get_addr_regsave)
12282 {
12283 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12284 delta = cfa_updt - stub_entry->group->lr_restore;
12285 stub_entry->group->eh_size += eh_advance_size (delta);
12286 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12287 stub_entry->group->lr_restore
12288 = stub_entry->stub_offset + size - 4;
12289 }
12290 else if (stub_entry->stub_type == ppc_stub_plt_call_both)
12291 {
12292 lr_used = stub_entry->stub_offset + size - 20;
12293 delta = lr_used - stub_entry->group->lr_restore;
12294 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12295 stub_entry->group->lr_restore
12296 = stub_entry->stub_offset + size - 4;
12297 }
12298 }
f891966f
AM
12299 break;
12300
12301 case ppc_stub_plt_call:
12302 case ppc_stub_plt_call_r2save:
12303 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12304 if (targ >= (bfd_vma) -2)
12305 abort ();
12306 plt = htab->elf.splt;
30845f11 12307 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12308 {
12309 if (stub_entry->symtype == STT_GNU_IFUNC)
12310 plt = htab->elf.iplt;
12311 else
12312 plt = htab->pltlocal;
12313 }
12314 targ += plt->output_offset + plt->output_section->vma;
12315
12316 off = (elf_gp (info->output_bfd)
12317 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12318 off = targ - off;
12319
12320 if (htab->params->plt_stub_align != 0)
12321 {
29433886 12322 unsigned pad = plt_stub_pad (htab, stub_entry, off, 0);
f891966f
AM
12323
12324 stub_entry->group->stub_sec->size += pad;
12325 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12326 }
12327
12328 if (info->emitrelocations)
12329 {
12330 stub_entry->group->stub_sec->reloc_count
12331 += ((PPC_HA (off) != 0)
12332 + (htab->opd_abi
12333 ? 2 + (htab->params->plt_static_chain
12334 && PPC_HA (off + 16) == PPC_HA (off))
12335 : 1));
12336 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12337 }
12338
29433886 12339 size = plt_stub_size (htab, stub_entry, off, 0);
f891966f
AM
12340
12341 if (stub_entry->h != NULL
ed7007c1 12342 && is_tls_get_addr (&stub_entry->h->elf, htab)
f891966f
AM
12343 && htab->params->tls_get_addr_opt
12344 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
12345 {
29433886
AM
12346 if (!htab->params->no_tls_get_addr_regsave)
12347 {
12348 /* Adjustments to r1 need to be described. */
12349 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12350 delta = cfa_updt - stub_entry->group->lr_restore;
12351 stub_entry->group->eh_size += eh_advance_size (delta);
12352 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12353 }
12354 else
9e7028aa
AM
12355 {
12356 lr_used = stub_entry->stub_offset + size - 20;
12357 /* The eh_frame info will consist of a DW_CFA_advance_loc
12358 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12359 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12360 delta = lr_used - stub_entry->group->lr_restore;
12361 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12362 }
29433886 12363 stub_entry->group->lr_restore = stub_entry->stub_offset + size - 4;
f891966f
AM
12364 }
12365 break;
12366
12367 default:
12368 BFD_FAIL ();
12369 return FALSE;
721956f4
AM
12370 }
12371
6f20ed8a 12372 stub_entry->group->stub_sec->size += size;
b34976b6 12373 return TRUE;
721956f4
AM
12374}
12375
12376/* Set up various things so that we can make a list of input sections
12377 for each output section included in the link. Returns -1 on error,
cedb70c5 12378 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
12379
12380int
e7d1c40c 12381ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 12382{
6f20ed8a 12383 unsigned int id;
986f0783 12384 size_t amt;
721956f4
AM
12385 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12386
4dfe6ac6
NC
12387 if (htab == NULL)
12388 return -1;
4c52953f 12389
7cf7fcc8 12390 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
12391 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12392 htab->sec_info = bfd_zmalloc (amt);
12393 if (htab->sec_info == NULL)
721956f4
AM
12394 return -1;
12395
3d6f9012
AM
12396 /* Set toc_off for com, und, abs and ind sections. */
12397 for (id = 0; id < 3; id++)
6f20ed8a 12398 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 12399
721956f4
AM
12400 return 1;
12401}
12402
927be08e
AM
12403/* Set up for first pass at multitoc partitioning. */
12404
12405void
12406ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12407{
12408 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12409
1c865ab2 12410 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
12411 htab->toc_bfd = NULL;
12412 htab->toc_first_sec = NULL;
12413}
12414
e717da7e
AM
12415/* The linker repeatedly calls this function for each TOC input section
12416 and linker generated GOT section. Group input bfds such that the toc
927be08e 12417 within a group is less than 64k in size. */
ad8e1ba5 12418
927be08e 12419bfd_boolean
4ce794b7 12420ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
12421{
12422 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 12423 bfd_vma addr, off, limit;
ad8e1ba5 12424
4dfe6ac6
NC
12425 if (htab == NULL)
12426 return FALSE;
12427
927be08e 12428 if (!htab->second_toc_pass)
4c52953f 12429 {
927be08e 12430 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
12431 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
12432
12433 if (new_bfd)
bf102f86
AM
12434 {
12435 htab->toc_bfd = isec->owner;
12436 htab->toc_first_sec = isec;
12437 }
927be08e 12438
bf102f86
AM
12439 addr = isec->output_offset + isec->output_section->vma;
12440 off = addr - htab->toc_curr;
d77c8a4b
AM
12441 limit = 0x80008000;
12442 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12443 limit = 0x10000;
12444 if (off + isec->size > limit)
bf102f86
AM
12445 {
12446 addr = (htab->toc_first_sec->output_offset
12447 + htab->toc_first_sec->output_section->vma);
12448 htab->toc_curr = addr;
a27e685f 12449 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 12450 }
99877b66 12451
927be08e
AM
12452 /* toc_curr is the base address of this toc group. Set elf_gp
12453 for the input section to be the offset relative to the
12454 output toc base plus 0x8000. Making the input elf_gp an
12455 offset allows us to move the toc as a whole without
12456 recalculating input elf_gp. */
06bcf541 12457 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
12458 off += TOC_BASE_OFF;
12459
12460 /* Die if someone uses a linker script that doesn't keep input
12461 file .toc and .got together. */
a4fd3de5
AM
12462 if (new_bfd
12463 && elf_gp (isec->owner) != 0
927be08e
AM
12464 && elf_gp (isec->owner) != off)
12465 return FALSE;
12466
12467 elf_gp (isec->owner) = off;
12468 return TRUE;
4c52953f 12469 }
927be08e
AM
12470
12471 /* During the second pass toc_first_sec points to the start of
12472 a toc group, and toc_curr is used to track the old elf_gp.
12473 We use toc_bfd to ensure we only look at each bfd once. */
12474 if (htab->toc_bfd == isec->owner)
12475 return TRUE;
12476 htab->toc_bfd = isec->owner;
12477
12478 if (htab->toc_first_sec == NULL
12479 || htab->toc_curr != elf_gp (isec->owner))
12480 {
12481 htab->toc_curr = elf_gp (isec->owner);
12482 htab->toc_first_sec = isec;
12483 }
12484 addr = (htab->toc_first_sec->output_offset
12485 + htab->toc_first_sec->output_section->vma);
06bcf541 12486 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
12487 elf_gp (isec->owner) = off;
12488
12489 return TRUE;
ad8e1ba5
AM
12490}
12491
927be08e
AM
12492/* Called via elf_link_hash_traverse to merge GOT entries for global
12493 symbol H. */
12494
12495static bfd_boolean
12496merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12497{
12498 if (h->root.type == bfd_link_hash_indirect)
12499 return TRUE;
12500
927be08e
AM
12501 merge_got_entries (&h->got.glist);
12502
12503 return TRUE;
12504}
12505
12506/* Called via elf_link_hash_traverse to allocate GOT entries for global
12507 symbol H. */
12508
12509static bfd_boolean
12510reallocate_got (struct elf_link_hash_entry *h, void *inf)
12511{
12512 struct got_entry *gent;
12513
12514 if (h->root.type == bfd_link_hash_indirect)
12515 return TRUE;
12516
927be08e
AM
12517 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12518 if (!gent->is_indirect)
12519 allocate_got (h, (struct bfd_link_info *) inf, gent);
12520 return TRUE;
12521}
12522
12523/* Called on the first multitoc pass after the last call to
12524 ppc64_elf_next_toc_section. This function removes duplicate GOT
12525 entries. */
12526
12527bfd_boolean
12528ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12529{
12530 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
12531 struct bfd *ibfd, *ibfd2;
12532 bfd_boolean done_something;
12533
12534 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12535
7865406b
AM
12536 if (!htab->do_multi_toc)
12537 return FALSE;
12538
d0fae19d 12539 /* Merge global sym got entries within a toc group. */
927be08e
AM
12540 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12541
12542 /* And tlsld_got. */
c72f2fb2 12543 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12544 {
12545 struct got_entry *ent, *ent2;
12546
12547 if (!is_ppc64_elf (ibfd))
12548 continue;
12549
12550 ent = ppc64_tlsld_got (ibfd);
12551 if (!ent->is_indirect
12552 && ent->got.offset != (bfd_vma) -1)
12553 {
c72f2fb2 12554 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12555 {
12556 if (!is_ppc64_elf (ibfd2))
12557 continue;
12558
12559 ent2 = ppc64_tlsld_got (ibfd2);
12560 if (!ent2->is_indirect
12561 && ent2->got.offset != (bfd_vma) -1
12562 && elf_gp (ibfd2) == elf_gp (ibfd))
12563 {
12564 ent2->is_indirect = TRUE;
12565 ent2->got.ent = ent;
12566 }
12567 }
12568 }
12569 }
12570
12571 /* Zap sizes of got sections. */
33e44f2e
AM
12572 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12573 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12574 htab->got_reli_size = 0;
12575
c72f2fb2 12576 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12577 {
12578 asection *got, *relgot;
12579
12580 if (!is_ppc64_elf (ibfd))
12581 continue;
12582
12583 got = ppc64_elf_tdata (ibfd)->got;
12584 if (got != NULL)
12585 {
12586 got->rawsize = got->size;
12587 got->size = 0;
12588 relgot = ppc64_elf_tdata (ibfd)->relgot;
12589 relgot->rawsize = relgot->size;
12590 relgot->size = 0;
12591 }
12592 }
12593
12594 /* Now reallocate the got, local syms first. We don't need to
12595 allocate section contents again since we never increase size. */
c72f2fb2 12596 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12597 {
12598 struct got_entry **lgot_ents;
12599 struct got_entry **end_lgot_ents;
12600 struct plt_entry **local_plt;
12601 struct plt_entry **end_local_plt;
f961d9dd 12602 unsigned char *lgot_masks;
927be08e
AM
12603 bfd_size_type locsymcount;
12604 Elf_Internal_Shdr *symtab_hdr;
19e08130 12605 asection *s;
927be08e
AM
12606
12607 if (!is_ppc64_elf (ibfd))
12608 continue;
12609
12610 lgot_ents = elf_local_got_ents (ibfd);
12611 if (!lgot_ents)
12612 continue;
12613
12614 symtab_hdr = &elf_symtab_hdr (ibfd);
12615 locsymcount = symtab_hdr->sh_info;
12616 end_lgot_ents = lgot_ents + locsymcount;
12617 local_plt = (struct plt_entry **) end_lgot_ents;
12618 end_local_plt = local_plt + locsymcount;
f961d9dd 12619 lgot_masks = (unsigned char *) end_local_plt;
927be08e 12620 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
12621 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12622 {
12623 struct got_entry *ent;
12624
12625 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12626 {
19e08130
AM
12627 unsigned int ent_size = 8;
12628 unsigned int rel_size = sizeof (Elf64_External_Rela);
12629
d0fae19d
AM
12630 ent->got.offset = s->size;
12631 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12632 {
19e08130
AM
12633 ent_size *= 2;
12634 rel_size *= 2;
12635 }
12636 s->size += ent_size;
37da22e5 12637 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12638 {
33e44f2e 12639 htab->elf.irelplt->size += rel_size;
19e08130
AM
12640 htab->got_reli_size += rel_size;
12641 }
6a3858a6 12642 else if (bfd_link_pic (info)
f749f26e 12643 && !(ent->tls_type != 0
6a3858a6 12644 && bfd_link_executable (info)))
19e08130
AM
12645 {
12646 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12647 srel->size += rel_size;
d0fae19d
AM
12648 }
12649 }
927be08e
AM
12650 }
12651 }
12652
12653 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12654
c72f2fb2 12655 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12656 {
12657 struct got_entry *ent;
12658
12659 if (!is_ppc64_elf (ibfd))
12660 continue;
12661
12662 ent = ppc64_tlsld_got (ibfd);
12663 if (!ent->is_indirect
12664 && ent->got.offset != (bfd_vma) -1)
12665 {
12666 asection *s = ppc64_elf_tdata (ibfd)->got;
12667 ent->got.offset = s->size;
12668 s->size += 16;
f749f26e 12669 if (bfd_link_dll (info))
927be08e
AM
12670 {
12671 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12672 srel->size += sizeof (Elf64_External_Rela);
12673 }
12674 }
12675 }
12676
33e44f2e 12677 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12678 if (!done_something)
c72f2fb2 12679 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12680 {
12681 asection *got;
12682
12683 if (!is_ppc64_elf (ibfd))
12684 continue;
12685
12686 got = ppc64_elf_tdata (ibfd)->got;
12687 if (got != NULL)
12688 {
12689 done_something = got->rawsize != got->size;
12690 if (done_something)
12691 break;
12692 }
12693 }
12694
12695 if (done_something)
e7d1c40c 12696 (*htab->params->layout_sections_again) ();
927be08e
AM
12697
12698 /* Set up for second pass over toc sections to recalculate elf_gp
12699 on input sections. */
12700 htab->toc_bfd = NULL;
12701 htab->toc_first_sec = NULL;
12702 htab->second_toc_pass = TRUE;
12703 return done_something;
12704}
12705
12706/* Called after second pass of multitoc partitioning. */
12707
12708void
12709ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12710{
12711 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12712
12713 /* After the second pass, toc_curr tracks the TOC offset used
12714 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12715 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12716}
12717
9b5ecbd0
AM
12718/* No toc references were found in ISEC. If the code in ISEC makes no
12719 calls, then there's no need to use toc adjusting stubs when branching
12720 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12721 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12722 needed, and 2 if a cyclical call-graph was found but no other reason
12723 for a stub was detected. If called from the top level, a return of
12724 2 means the same as a return of 0. */
9b5ecbd0
AM
12725
12726static int
4ce794b7 12727toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12728{
9b5ecbd0 12729 int ret;
70cc837d
AM
12730
12731 /* Mark this section as checked. */
12732 isec->call_check_done = 1;
9b5ecbd0 12733
772119ce
AM
12734 /* We know none of our code bearing sections will need toc stubs. */
12735 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12736 return 0;
12737
eea6121a 12738 if (isec->size == 0)
082c50f8
AM
12739 return 0;
12740
4c52953f
AM
12741 if (isec->output_section == NULL)
12742 return 0;
12743
4c52953f 12744 ret = 0;
70cc837d 12745 if (isec->reloc_count != 0)
9b5ecbd0 12746 {
70cc837d
AM
12747 Elf_Internal_Rela *relstart, *rel;
12748 Elf_Internal_Sym *local_syms;
12749 struct ppc_link_hash_table *htab;
2917689a 12750
70cc837d
AM
12751 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12752 info->keep_memory);
12753 if (relstart == NULL)
12754 return -1;
90aecf7a 12755
70cc837d
AM
12756 /* Look for branches to outside of this section. */
12757 local_syms = NULL;
12758 htab = ppc_hash_table (info);
12759 if (htab == NULL)
12760 return -1;
4c52953f 12761
70cc837d 12762 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12763 {
70cc837d
AM
12764 enum elf_ppc64_reloc_type r_type;
12765 unsigned long r_symndx;
12766 struct elf_link_hash_entry *h;
12767 struct ppc_link_hash_entry *eh;
12768 Elf_Internal_Sym *sym;
12769 asection *sym_sec;
12770 struct _opd_sec_data *opd;
12771 bfd_vma sym_value;
12772 bfd_vma dest;
12773
12774 r_type = ELF64_R_TYPE (rel->r_info);
12775 if (r_type != R_PPC64_REL24
05d0e962 12776 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
12777 && r_type != R_PPC64_REL14
12778 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 12779 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
12780 && r_type != R_PPC64_PLTCALL
12781 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 12782 continue;
4c52953f 12783
70cc837d
AM
12784 r_symndx = ELF64_R_SYM (rel->r_info);
12785 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12786 isec->owner))
4c52953f 12787 {
70cc837d
AM
12788 ret = -1;
12789 break;
12790 }
4c52953f 12791
70cc837d
AM
12792 /* Calls to dynamic lib functions go through a plt call stub
12793 that uses r2. */
ed7007c1 12794 eh = ppc_elf_hash_entry (h);
70cc837d
AM
12795 if (eh != NULL
12796 && (eh->elf.plt.plist != NULL
12797 || (eh->oh != NULL
12798 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12799 {
12800 ret = 1;
12801 break;
4c52953f
AM
12802 }
12803
70cc837d
AM
12804 if (sym_sec == NULL)
12805 /* Ignore other undefined symbols. */
4c52953f 12806 continue;
4c52953f 12807
70cc837d
AM
12808 /* Assume branches to other sections not included in the
12809 link need stubs too, to cover -R and absolute syms. */
12810 if (sym_sec->output_section == NULL)
12811 {
12812 ret = 1;
12813 break;
12814 }
4c52953f 12815
70cc837d
AM
12816 if (h == NULL)
12817 sym_value = sym->st_value;
12818 else
12819 {
12820 if (h->root.type != bfd_link_hash_defined
12821 && h->root.type != bfd_link_hash_defweak)
12822 abort ();
12823 sym_value = h->root.u.def.value;
12824 }
12825 sym_value += rel->r_addend;
4c52953f 12826
70cc837d
AM
12827 /* If this branch reloc uses an opd sym, find the code section. */
12828 opd = get_opd_info (sym_sec);
12829 if (opd != NULL)
12830 {
12831 if (h == NULL && opd->adjust != NULL)
12832 {
12833 long adjust;
4c52953f 12834
92a9c616 12835 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12836 if (adjust == -1)
12837 /* Assume deleted functions won't ever be called. */
12838 continue;
12839 sym_value += adjust;
12840 }
4c52953f 12841
aef36ac1
AM
12842 dest = opd_entry_value (sym_sec, sym_value,
12843 &sym_sec, NULL, FALSE);
70cc837d
AM
12844 if (dest == (bfd_vma) -1)
12845 continue;
12846 }
12847 else
12848 dest = (sym_value
12849 + sym_sec->output_offset
12850 + sym_sec->output_section->vma);
4c52953f 12851
70cc837d
AM
12852 /* Ignore branch to self. */
12853 if (sym_sec == isec)
12854 continue;
4c52953f 12855
70cc837d
AM
12856 /* If the called function uses the toc, we need a stub. */
12857 if (sym_sec->has_toc_reloc
12858 || sym_sec->makes_toc_func_call)
4c52953f 12859 {
70cc837d 12860 ret = 1;
4c52953f
AM
12861 break;
12862 }
70cc837d
AM
12863
12864 /* Assume any branch that needs a long branch stub might in fact
12865 need a plt_branch stub. A plt_branch stub uses r2. */
12866 else if (dest - (isec->output_offset
12867 + isec->output_section->vma
6911b7dc
AM
12868 + rel->r_offset) + (1 << 25)
12869 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12870 ? h->other
12871 : sym->st_other))
4c52953f 12872 {
70cc837d
AM
12873 ret = 1;
12874 break;
12875 }
12876
12877 /* If calling back to a section in the process of being
12878 tested, we can't say for sure that no toc adjusting stubs
12879 are needed, so don't return zero. */
12880 else if (sym_sec->call_check_in_progress)
12881 ret = 2;
12882
12883 /* Branches to another section that itself doesn't have any TOC
12884 references are OK. Recursively call ourselves to check. */
12885 else if (!sym_sec->call_check_done)
12886 {
12887 int recur;
12888
12889 /* Mark current section as indeterminate, so that other
12890 sections that call back to current won't be marked as
12891 known. */
12892 isec->call_check_in_progress = 1;
12893 recur = toc_adjusting_stub_needed (info, sym_sec);
12894 isec->call_check_in_progress = 0;
12895
4c52953f
AM
12896 if (recur != 0)
12897 {
70cc837d
AM
12898 ret = recur;
12899 if (recur != 2)
12900 break;
4c52953f
AM
12901 }
12902 }
4c52953f 12903 }
70cc837d 12904
c9594989
AM
12905 if (elf_symtab_hdr (isec->owner).contents
12906 != (unsigned char *) local_syms)
70cc837d
AM
12907 free (local_syms);
12908 if (elf_section_data (isec)->relocs != relstart)
12909 free (relstart);
9b5ecbd0
AM
12910 }
12911
70cc837d
AM
12912 if ((ret & 1) == 0
12913 && isec->map_head.s != NULL
12914 && (strcmp (isec->output_section->name, ".init") == 0
12915 || strcmp (isec->output_section->name, ".fini") == 0))
12916 {
12917 if (isec->map_head.s->has_toc_reloc
12918 || isec->map_head.s->makes_toc_func_call)
12919 ret = 1;
12920 else if (!isec->map_head.s->call_check_done)
12921 {
12922 int recur;
12923 isec->call_check_in_progress = 1;
12924 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12925 isec->call_check_in_progress = 0;
12926 if (recur != 0)
12927 ret = recur;
12928 }
12929 }
12930
12931 if (ret == 1)
12932 isec->makes_toc_func_call = 1;
4c52953f 12933
9b5ecbd0
AM
12934 return ret;
12935}
12936
721956f4
AM
12937/* The linker repeatedly calls this function for each input section,
12938 in the order that input sections are linked into output sections.
12939 Build lists of input sections to determine groupings between which
12940 we may insert linker stubs. */
12941
9b5ecbd0 12942bfd_boolean
4ce794b7 12943ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12944{
12945 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12946
4dfe6ac6
NC
12947 if (htab == NULL)
12948 return FALSE;
12949
734b6cf9 12950 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12951 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12952 {
3d6f9012
AM
12953 /* This happens to make the list in reverse order,
12954 which is what we want. */
6f20ed8a
AM
12955 htab->sec_info[isec->id].u.list
12956 = htab->sec_info[isec->output_section->id].u.list;
12957 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12958 }
ad8e1ba5 12959
4c52953f 12960 if (htab->multi_toc_needed)
9b5ecbd0 12961 {
8b974ba3
AM
12962 /* Analyse sections that aren't already flagged as needing a
12963 valid toc pointer. Exclude .fixup for the linux kernel.
12964 .fixup contains branches, but only back to the function that
12965 hit an exception. */
12966 if (!(isec->has_toc_reloc
12967 || (isec->flags & SEC_CODE) == 0
12968 || strcmp (isec->name, ".fixup") == 0
12969 || isec->call_check_done))
12970 {
12971 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12972 return FALSE;
8b974ba3
AM
12973 }
12974 /* Make all sections use the TOC assigned for this object file.
12975 This will be wrong for pasted sections; We fix that in
12976 check_pasted_section(). */
12977 if (elf_gp (isec->owner) != 0)
12978 htab->toc_curr = elf_gp (isec->owner);
12979 }
12980
6f20ed8a 12981 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12982 return TRUE;
721956f4
AM
12983}
12984
70cc837d
AM
12985/* Check that all .init and .fini sections use the same toc, if they
12986 have toc relocs. */
12987
12988static bfd_boolean
12989check_pasted_section (struct bfd_link_info *info, const char *name)
12990{
12991 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12992
12993 if (o != NULL)
12994 {
12995 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12996 bfd_vma toc_off = 0;
12997 asection *i;
12998
12999 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13000 if (i->has_toc_reloc)
13001 {
13002 if (toc_off == 0)
6f20ed8a
AM
13003 toc_off = htab->sec_info[i->id].toc_off;
13004 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
13005 return FALSE;
13006 }
6683a28d
AM
13007
13008 if (toc_off == 0)
13009 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13010 if (i->makes_toc_func_call)
13011 {
6f20ed8a 13012 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
13013 break;
13014 }
13015
70cc837d
AM
13016 /* Make sure the whole pasted function uses the same toc offset. */
13017 if (toc_off != 0)
13018 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 13019 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
13020 }
13021 return TRUE;
13022}
13023
13024bfd_boolean
13025ppc64_elf_check_init_fini (struct bfd_link_info *info)
13026{
13027 return (check_pasted_section (info, ".init")
13028 & check_pasted_section (info, ".fini"));
13029}
13030
721956f4
AM
13031/* See whether we can group stub sections together. Grouping stub
13032 sections may result in fewer stubs. More importantly, we need to
13033 put all .init* and .fini* stubs at the beginning of the .init or
13034 .fini output sections respectively, because glibc splits the
13035 _init and _fini functions into multiple parts. Putting a stub in
13036 the middle of a function is not a good idea. */
13037
6f20ed8a
AM
13038static bfd_boolean
13039group_sections (struct bfd_link_info *info,
4ce794b7
AM
13040 bfd_size_type stub_group_size,
13041 bfd_boolean stubs_always_before_branch)
721956f4 13042{
6f20ed8a
AM
13043 struct ppc_link_hash_table *htab;
13044 asection *osec;
7c8fe5c4
AM
13045 bfd_boolean suppress_size_errors;
13046
6f20ed8a
AM
13047 htab = ppc_hash_table (info);
13048 if (htab == NULL)
13049 return FALSE;
13050
7c8fe5c4 13051 suppress_size_errors = FALSE;
7c8fe5c4
AM
13052 if (stub_group_size == 1)
13053 {
13054 /* Default values. */
13055 if (stubs_always_before_branch)
09f92717 13056 stub_group_size = 0x1e00000;
7c8fe5c4 13057 else
09f92717 13058 stub_group_size = 0x1c00000;
7c8fe5c4
AM
13059 suppress_size_errors = TRUE;
13060 }
13061
6f20ed8a 13062 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 13063 {
6f20ed8a
AM
13064 asection *tail;
13065
13066 if (osec->id >= htab->sec_info_arr_size)
13067 continue;
13068
13069 tail = htab->sec_info[osec->id].u.list;
734b6cf9 13070 while (tail != NULL)
721956f4 13071 {
734b6cf9
AM
13072 asection *curr;
13073 asection *prev;
13074 bfd_size_type total;
13075 bfd_boolean big_sec;
13076 bfd_vma curr_toc;
6f20ed8a 13077 struct map_stub *group;
09f92717 13078 bfd_size_type group_size;
734b6cf9
AM
13079
13080 curr = tail;
eea6121a 13081 total = tail->size;
09f92717
AM
13082 group_size = (ppc64_elf_section_data (tail) != NULL
13083 && ppc64_elf_section_data (tail)->has_14bit_branch
13084 ? stub_group_size >> 10 : stub_group_size);
13085
13086 big_sec = total > group_size;
7c8fe5c4 13087 if (big_sec && !suppress_size_errors)
695344c0 13088 /* xgettext:c-format */
871b3ab2 13089 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 13090 tail->owner, tail);
6f20ed8a 13091 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 13092
6f20ed8a 13093 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 13094 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
13095 < (ppc64_elf_section_data (prev) != NULL
13096 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 13097 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 13098 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13099 curr = prev;
13100
13101 /* OK, the size from the start of CURR to the end is less
09f92717 13102 than group_size and thus can be handled by one stub
734b6cf9 13103 section. (or the tail section is itself larger than
09f92717
AM
13104 group_size, in which case we may be toast.) We should
13105 really be keeping track of the total size of stubs added
13106 here, as stubs contribute to the final output section
13107 size. That's a little tricky, and this way will only
13108 break if stubs added make the total size more than 2^25,
13109 ie. for the default stub_group_size, if stubs total more
13110 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
13111 group = bfd_alloc (curr->owner, sizeof (*group));
13112 if (group == NULL)
13113 return FALSE;
13114 group->link_sec = curr;
13115 group->stub_sec = NULL;
a4b6fadd 13116 group->needs_save_res = 0;
df136d64
AM
13117 group->lr_restore = 0;
13118 group->eh_size = 0;
13119 group->eh_base = 0;
a4b6fadd
AM
13120 group->next = htab->group;
13121 htab->group = group;
734b6cf9 13122 do
721956f4 13123 {
6f20ed8a 13124 prev = htab->sec_info[tail->id].u.list;
734b6cf9 13125 /* Set up this stub group. */
6f20ed8a 13126 htab->sec_info[tail->id].u.group = group;
721956f4 13127 }
734b6cf9
AM
13128 while (tail != curr && (tail = prev) != NULL);
13129
09f92717 13130 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
13131 bytes before the stub section can be handled by it too.
13132 Don't do this if we have a really large section after the
13133 stubs, as adding more stubs increases the chance that
13134 branches may not reach into the stub section. */
13135 if (!stubs_always_before_branch && !big_sec)
13136 {
13137 total = 0;
13138 while (prev != NULL
13139 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
13140 < (ppc64_elf_section_data (prev) != NULL
13141 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
13142 ? (group_size = stub_group_size >> 10)
13143 : group_size))
6f20ed8a 13144 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13145 {
13146 tail = prev;
6f20ed8a
AM
13147 prev = htab->sec_info[tail->id].u.list;
13148 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
13149 }
13150 }
13151 tail = prev;
721956f4
AM
13152 }
13153 }
6f20ed8a 13154 return TRUE;
721956f4
AM
13155}
13156
58d180e8
AM
13157static const unsigned char glink_eh_frame_cie[] =
13158{
13159 0, 0, 0, 16, /* length. */
13160 0, 0, 0, 0, /* id. */
13161 1, /* CIE version. */
13162 'z', 'R', 0, /* Augmentation string. */
13163 4, /* Code alignment. */
13164 0x78, /* Data alignment. */
13165 65, /* RA reg. */
13166 1, /* Augmentation size. */
13167 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 13168 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
13169};
13170
d969d15f
AM
13171/* Stripping output sections is normally done before dynamic section
13172 symbols have been allocated. This function is called later, and
13173 handles cases like htab->brlt which is mapped to its own output
13174 section. */
13175
13176static void
13177maybe_strip_output (struct bfd_link_info *info, asection *isec)
13178{
13179 if (isec->size == 0
13180 && isec->output_section->size == 0
53d8967a 13181 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
13182 && !bfd_section_removed_from_list (info->output_bfd,
13183 isec->output_section)
13184 && elf_section_data (isec->output_section)->dynindx == 0)
13185 {
13186 isec->output_section->flags |= SEC_EXCLUDE;
13187 bfd_section_list_remove (info->output_bfd, isec->output_section);
13188 info->output_bfd->section_count--;
13189 }
13190}
13191
721956f4
AM
13192/* Determine and set the size of the stub section for a final link.
13193
13194 The basic idea here is to examine all the relocations looking for
13195 PC-relative calls to a target that is unreachable with a "bl"
13196 instruction. */
13197
b34976b6 13198bfd_boolean
e7d1c40c 13199ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
13200{
13201 bfd_size_type stub_group_size;
b34976b6 13202 bfd_boolean stubs_always_before_branch;
721956f4
AM
13203 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13204
4dfe6ac6
NC
13205 if (htab == NULL)
13206 return FALSE;
13207
e10a07b3
AM
13208 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13209 htab->params->power10_stubs = 0;
13210
0e1862bb 13211 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 13212 htab->params->plt_thread_safe = 1;
b9e5796b 13213 if (!htab->opd_abi)
e7d1c40c
AM
13214 htab->params->plt_thread_safe = 0;
13215 else if (htab->params->plt_thread_safe == -1)
794e51c0 13216 {
e2458743 13217 static const char *const thread_starter[] =
794e51c0
AM
13218 {
13219 "pthread_create",
13220 /* libstdc++ */
13221 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13222 /* librt */
13223 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13224 "mq_notify", "create_timer",
13225 /* libanl */
13226 "getaddrinfo_a",
13227 /* libgomp */
2300b5a1 13228 "GOMP_parallel",
794e51c0 13229 "GOMP_parallel_start",
2300b5a1 13230 "GOMP_parallel_loop_static",
794e51c0 13231 "GOMP_parallel_loop_static_start",
2300b5a1 13232 "GOMP_parallel_loop_dynamic",
794e51c0 13233 "GOMP_parallel_loop_dynamic_start",
2300b5a1 13234 "GOMP_parallel_loop_guided",
794e51c0 13235 "GOMP_parallel_loop_guided_start",
2300b5a1 13236 "GOMP_parallel_loop_runtime",
794e51c0 13237 "GOMP_parallel_loop_runtime_start",
2300b5a1 13238 "GOMP_parallel_sections",
68ffbac6 13239 "GOMP_parallel_sections_start",
f9dffbf0
AM
13240 /* libgo */
13241 "__go_go",
794e51c0
AM
13242 };
13243 unsigned i;
13244
a4b6fadd 13245 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
13246 {
13247 struct elf_link_hash_entry *h;
13248 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13249 FALSE, FALSE, TRUE);
e7d1c40c
AM
13250 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13251 if (htab->params->plt_thread_safe)
794e51c0
AM
13252 break;
13253 }
13254 }
e7d1c40c
AM
13255 stubs_always_before_branch = htab->params->group_size < 0;
13256 if (htab->params->group_size < 0)
13257 stub_group_size = -htab->params->group_size;
721956f4 13258 else
e7d1c40c 13259 stub_group_size = htab->params->group_size;
721956f4 13260
6f20ed8a
AM
13261 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13262 return FALSE;
721956f4 13263
a804e476
AM
13264 htab->tga_group = NULL;
13265 if (!htab->params->no_tls_get_addr_regsave
13266 && htab->tga_desc_fd != NULL
13267 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13268 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13269 && htab->tls_get_addr_fd != NULL
13270 && is_static_defined (&htab->tls_get_addr_fd->elf))
13271 {
13272 asection *sym_sec, *code_sec, *stub_sec;
13273 bfd_vma sym_value;
13274 struct _opd_sec_data *opd;
13275
13276 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13277 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13278 code_sec = sym_sec;
13279 opd = get_opd_info (sym_sec);
13280 if (opd != NULL)
13281 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, FALSE);
13282 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13283 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13284 htab->tga_group->link_sec);
13285 if (stub_sec == NULL)
13286 return FALSE;
13287 htab->tga_group->stub_sec = stub_sec;
13288
13289 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13290 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13291 htab->tga_desc_fd->elf.root.u.def.value = 0;
13292 htab->tga_desc_fd->elf.type = STT_FUNC;
13293 htab->tga_desc_fd->elf.def_regular = 1;
13294 htab->tga_desc_fd->elf.non_elf = 0;
13295 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, TRUE);
13296 }
13297
c9301e31
AM
13298#define STUB_SHRINK_ITER 20
13299 /* Loop until no stubs added. After iteration 20 of this loop we may
13300 exit on a stub section shrinking. This is to break out of a
13301 pathological case where adding stubs on one iteration decreases
13302 section gaps (perhaps due to alignment), which then requires
13303 fewer or smaller stubs on the next iteration. */
13304
721956f4
AM
13305 while (1)
13306 {
13307 bfd *input_bfd;
13308 unsigned int bfd_indx;
a4b6fadd 13309 struct map_stub *group;
721956f4
AM
13310
13311 htab->stub_iteration += 1;
721956f4
AM
13312
13313 for (input_bfd = info->input_bfds, bfd_indx = 0;
13314 input_bfd != NULL;
c72f2fb2 13315 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
13316 {
13317 Elf_Internal_Shdr *symtab_hdr;
13318 asection *section;
6cdc0ccc 13319 Elf_Internal_Sym *local_syms = NULL;
721956f4 13320
0c8d6e5c 13321 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
13322 continue;
13323
721956f4 13324 /* We'll need the symbol table in a second. */
0ffa91dd 13325 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
13326 if (symtab_hdr->sh_info == 0)
13327 continue;
13328
721956f4
AM
13329 /* Walk over each section attached to the input bfd. */
13330 for (section = input_bfd->sections;
13331 section != NULL;
13332 section = section->next)
13333 {
721956f4 13334 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
13335
13336 /* If there aren't any relocs, then there's nothing more
13337 to do. */
13338 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
13339 || (section->flags & SEC_ALLOC) == 0
13340 || (section->flags & SEC_LOAD) == 0
13341 || (section->flags & SEC_CODE) == 0
721956f4
AM
13342 || section->reloc_count == 0)
13343 continue;
13344
13345 /* If this section is a link-once section that will be
13346 discarded, then don't create any stubs. */
13347 if (section->output_section == NULL
927be08e 13348 || section->output_section->owner != info->output_bfd)
721956f4
AM
13349 continue;
13350
1e2f5b6e
AM
13351 /* Get the relocs. */
13352 internal_relocs
4ce794b7 13353 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 13354 info->keep_memory);
721956f4 13355 if (internal_relocs == NULL)
1e2f5b6e 13356 goto error_ret_free_local;
721956f4
AM
13357
13358 /* Now examine each relocation. */
13359 irela = internal_relocs;
13360 irelaend = irela + section->reloc_count;
13361 for (; irela < irelaend; irela++)
13362 {
4ce794b7
AM
13363 enum elf_ppc64_reloc_type r_type;
13364 unsigned int r_indx;
721956f4
AM
13365 enum ppc_stub_type stub_type;
13366 struct ppc_stub_hash_entry *stub_entry;
8387904d 13367 asection *sym_sec, *code_sec;
e054468f 13368 bfd_vma sym_value, code_value;
721956f4 13369 bfd_vma destination;
6911b7dc 13370 unsigned long local_off;
8843416a 13371 bfd_boolean ok_dest;
721956f4 13372 struct ppc_link_hash_entry *hash;
8387904d 13373 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
13374 struct elf_link_hash_entry *h;
13375 Elf_Internal_Sym *sym;
721956f4
AM
13376 char *stub_name;
13377 const asection *id_sec;
74f0fb50 13378 struct _opd_sec_data *opd;
e054468f 13379 struct plt_entry *plt_ent;
721956f4
AM
13380
13381 r_type = ELF64_R_TYPE (irela->r_info);
13382 r_indx = ELF64_R_SYM (irela->r_info);
13383
4ce794b7 13384 if (r_type >= R_PPC64_max)
721956f4
AM
13385 {
13386 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 13387 goto error_ret_free_internal;
721956f4
AM
13388 }
13389
13390 /* Only look for stubs on branch instructions. */
4ce794b7 13391 if (r_type != R_PPC64_REL24
05d0e962 13392 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
13393 && r_type != R_PPC64_REL14
13394 && r_type != R_PPC64_REL14_BRTAKEN
13395 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
13396 continue;
13397
13398 /* Now determine the call target, its name, value,
13399 section. */
411e1bfb
AM
13400 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13401 r_indx, input_bfd))
13402 goto error_ret_free_internal;
ed7007c1 13403 hash = ppc_elf_hash_entry (h);
411e1bfb 13404
8843416a 13405 ok_dest = FALSE;
8387904d 13406 fdh = NULL;
7fe2b9a6 13407 sym_value = 0;
411e1bfb 13408 if (hash == NULL)
721956f4 13409 {
411e1bfb 13410 sym_value = sym->st_value;
c27b8c2a
AM
13411 if (sym_sec != NULL
13412 && sym_sec->output_section != NULL)
13413 ok_dest = TRUE;
721956f4 13414 }
7fe2b9a6
AM
13415 else if (hash->elf.root.type == bfd_link_hash_defined
13416 || hash->elf.root.type == bfd_link_hash_defweak)
13417 {
13418 sym_value = hash->elf.root.u.def.value;
13419 if (sym_sec->output_section != NULL)
13420 ok_dest = TRUE;
13421 }
13422 else if (hash->elf.root.type == bfd_link_hash_undefweak
13423 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 13424 {
99877b66 13425 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
13426 use the func descriptor sym instead if it is
13427 defined. */
ceb1f1ef 13428 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 13429 && hash->oh != NULL)
8387904d 13430 {
8c5b4e52 13431 fdh = ppc_follow_link (hash->oh);
8387904d
AM
13432 if (fdh->elf.root.type == bfd_link_hash_defined
13433 || fdh->elf.root.type == bfd_link_hash_defweak)
13434 {
13435 sym_sec = fdh->elf.root.u.def.section;
13436 sym_value = fdh->elf.root.u.def.value;
13437 if (sym_sec->output_section != NULL)
13438 ok_dest = TRUE;
13439 }
99877b66
AM
13440 else
13441 fdh = NULL;
8387904d 13442 }
7fe2b9a6
AM
13443 }
13444 else
13445 {
13446 bfd_set_error (bfd_error_bad_value);
13447 goto error_ret_free_internal;
721956f4
AM
13448 }
13449
8843416a 13450 destination = 0;
6911b7dc 13451 local_off = 0;
8843416a
AM
13452 if (ok_dest)
13453 {
13454 sym_value += irela->r_addend;
13455 destination = (sym_value
13456 + sym_sec->output_offset
13457 + sym_sec->output_section->vma);
6911b7dc
AM
13458 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13459 ? hash->elf.other
13460 : sym->st_other);
8843416a
AM
13461 }
13462
8387904d 13463 code_sec = sym_sec;
e054468f 13464 code_value = sym_value;
74f0fb50
AM
13465 opd = get_opd_info (sym_sec);
13466 if (opd != NULL)
8387904d
AM
13467 {
13468 bfd_vma dest;
13469
74f0fb50 13470 if (hash == NULL && opd->adjust != NULL)
8387904d 13471 {
51aecdc5 13472 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
13473 if (adjust == -1)
13474 continue;
e054468f 13475 code_value += adjust;
8387904d
AM
13476 sym_value += adjust;
13477 }
13478 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 13479 &code_sec, &code_value, FALSE);
8387904d
AM
13480 if (dest != (bfd_vma) -1)
13481 {
13482 destination = dest;
13483 if (fdh != NULL)
13484 {
13485 /* Fixup old ABI sym to point at code
13486 entry. */
99877b66 13487 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 13488 hash->elf.root.u.def.section = code_sec;
e054468f 13489 hash->elf.root.u.def.value = code_value;
8387904d
AM
13490 }
13491 }
13492 }
13493
721956f4 13494 /* Determine what (if any) linker stub is needed. */
e054468f 13495 plt_ent = NULL;
721956f4 13496 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
13497 &plt_ent, destination,
13498 local_off);
ad8e1ba5 13499
05d0e962
AM
13500 if (r_type == R_PPC64_REL24_NOTOC)
13501 {
13502 if (stub_type == ppc_stub_plt_call)
13503 stub_type = ppc_stub_plt_call_notoc;
13504 else if (stub_type == ppc_stub_long_branch
13505 || (code_sec != NULL
13506 && code_sec->output_section != NULL
13507 && (((hash ? hash->elf.other : sym->st_other)
13508 & STO_PPC64_LOCAL_MASK)
4a4e7361 13509 > 1 << STO_PPC64_LOCAL_BIT)))
05d0e962
AM
13510 stub_type = ppc_stub_long_branch_notoc;
13511 }
13512 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
13513 {
13514 /* Check whether we need a TOC adjusting stub.
13515 Since the linker pastes together pieces from
13516 different object files when creating the
13517 _init and _fini functions, it may be that a
13518 call to what looks like a local sym is in
13519 fact a call needing a TOC adjustment. */
33cb30a1
AM
13520 if ((code_sec != NULL
13521 && code_sec->output_section != NULL
33cb30a1 13522 && (code_sec->has_toc_reloc
918dc783
AM
13523 || code_sec->makes_toc_func_call)
13524 && (htab->sec_info[code_sec->id].toc_off
13525 != htab->sec_info[section->id].toc_off))
33cb30a1
AM
13526 || (((hash ? hash->elf.other : sym->st_other)
13527 & STO_PPC64_LOCAL_MASK)
13528 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
13529 stub_type = ppc_stub_long_branch_r2off;
13530 }
13531
721956f4
AM
13532 if (stub_type == ppc_stub_none)
13533 continue;
13534
411e1bfb
AM
13535 /* __tls_get_addr calls might be eliminated. */
13536 if (stub_type != ppc_stub_plt_call
05d0e962 13537 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 13538 && hash != NULL
ed7007c1 13539 && is_tls_get_addr (&hash->elf, htab)
411e1bfb
AM
13540 && section->has_tls_reloc
13541 && irela != internal_relocs)
13542 {
13543 /* Get tls info. */
f961d9dd 13544 unsigned char *tls_mask;
411e1bfb 13545
3a71aa26 13546 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
13547 irela - 1, input_bfd))
13548 goto error_ret_free_internal;
abc489c6
AM
13549 if ((*tls_mask & TLS_TLS) != 0
13550 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
411e1bfb
AM
13551 continue;
13552 }
13553
f378ab09 13554 if (stub_type == ppc_stub_plt_call)
794e51c0 13555 {
6e1816be
AM
13556 if (!htab->opd_abi
13557 && htab->params->plt_localentry0 != 0
13558 && is_elfv2_localentry0 (&hash->elf))
13559 htab->has_plt_localentry0 = 1;
13560 else if (irela + 1 < irelaend
13561 && irela[1].r_offset == irela->r_offset + 4
13562 && (ELF64_R_TYPE (irela[1].r_info)
13563 == R_PPC64_TOCSAVE))
f378ab09
AM
13564 {
13565 if (!tocsave_find (htab, INSERT,
13566 &local_syms, irela + 1, input_bfd))
13567 goto error_ret_free_internal;
13568 }
f378ab09
AM
13569 else
13570 stub_type = ppc_stub_plt_call_r2save;
794e51c0 13571 }
3b421ab3 13572
721956f4 13573 /* Support for grouping stub sections. */
6f20ed8a 13574 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
13575
13576 /* Get the name of this stub. */
13577 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13578 if (!stub_name)
13579 goto error_ret_free_internal;
13580
13581 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 13582 stub_name, FALSE, FALSE);
721956f4
AM
13583 if (stub_entry != NULL)
13584 {
05d0e962 13585 enum ppc_stub_type old_type;
e10a07b3 13586
05d0e962
AM
13587 /* A stub has already been created, but it may
13588 not be the required type. We shouldn't be
13589 transitioning from plt_call to long_branch
13590 stubs or vice versa, but we might be
13591 upgrading from plt_call to plt_call_r2save or
13592 from long_branch to long_branch_r2off. */
721956f4 13593 free (stub_name);
e10a07b3
AM
13594 if (htab->params->power10_stubs == -1)
13595 {
13596 /* For --power10-stubs=auto, don't merge _notoc
13597 and other varieties of stubs. (The _both
13598 variety won't be created.) */
13599 bfd_boolean notoc = r_type == R_PPC64_REL24_NOTOC;
13600 struct ppc_stub_hash_entry *alt_stub
13601 = select_alt_stub (stub_entry, notoc);
13602
13603 if (alt_stub == NULL)
13604 {
13605 alt_stub = (struct ppc_stub_hash_entry *)
13606 stub_hash_newfunc (NULL,
13607 &htab->stub_hash_table,
13608 stub_entry->root.string);
13609 if (alt_stub == NULL)
13610 {
13611 /* xgettext:c-format */
13612 _bfd_error_handler
13613 (_("%pB: cannot create stub entry %s"),
13614 section->owner, stub_entry->root.string);
13615 goto error_ret_free_internal;
13616 }
13617 *alt_stub = *stub_entry;
13618 stub_entry->root.next = &alt_stub->root;
13619 if (notoc)
13620 /* Sort notoc stubs first, for no good
13621 reason. */
13622 alt_stub = stub_entry;
13623 alt_stub->stub_type = stub_type;
13624 }
13625 stub_entry = alt_stub;
13626 }
05d0e962
AM
13627 old_type = stub_entry->stub_type;
13628 switch (old_type)
13629 {
13630 default:
13631 abort ();
13632
13633 case ppc_stub_save_res:
13634 continue;
13635
13636 case ppc_stub_plt_call:
13637 case ppc_stub_plt_call_r2save:
13638 case ppc_stub_plt_call_notoc:
13639 case ppc_stub_plt_call_both:
13640 if (stub_type == ppc_stub_plt_call)
13641 continue;
13642 else if (stub_type == ppc_stub_plt_call_r2save)
13643 {
13644 if (old_type == ppc_stub_plt_call_notoc)
13645 stub_type = ppc_stub_plt_call_both;
13646 }
13647 else if (stub_type == ppc_stub_plt_call_notoc)
13648 {
13649 if (old_type == ppc_stub_plt_call_r2save)
13650 stub_type = ppc_stub_plt_call_both;
13651 }
13652 else
13653 abort ();
13654 break;
13655
13656 case ppc_stub_plt_branch:
13657 case ppc_stub_plt_branch_r2off:
13658 case ppc_stub_plt_branch_notoc:
13659 case ppc_stub_plt_branch_both:
13660 old_type += (ppc_stub_long_branch
13661 - ppc_stub_plt_branch);
13662 /* Fall through. */
13663 case ppc_stub_long_branch:
13664 case ppc_stub_long_branch_r2off:
13665 case ppc_stub_long_branch_notoc:
13666 case ppc_stub_long_branch_both:
13667 if (stub_type == ppc_stub_long_branch)
13668 continue;
13669 else if (stub_type == ppc_stub_long_branch_r2off)
13670 {
13671 if (old_type == ppc_stub_long_branch_notoc)
13672 stub_type = ppc_stub_long_branch_both;
13673 }
13674 else if (stub_type == ppc_stub_long_branch_notoc)
13675 {
13676 if (old_type == ppc_stub_long_branch_r2off)
13677 stub_type = ppc_stub_long_branch_both;
13678 }
13679 else
13680 abort ();
13681 break;
13682 }
13683 if (old_type < stub_type)
794e51c0 13684 stub_entry->stub_type = stub_type;
721956f4
AM
13685 continue;
13686 }
13687
25f53a85 13688 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
13689 if (stub_entry == NULL)
13690 {
13691 free (stub_name);
6cdc0ccc
AM
13692 error_ret_free_internal:
13693 if (elf_section_data (section)->relocs == NULL)
13694 free (internal_relocs);
13695 error_ret_free_local:
c9594989
AM
13696 if (symtab_hdr->contents
13697 != (unsigned char *) local_syms)
6cdc0ccc 13698 free (local_syms);
b34976b6 13699 return FALSE;
721956f4
AM
13700 }
13701
ad8e1ba5 13702 stub_entry->stub_type = stub_type;
05d0e962
AM
13703 if (stub_type >= ppc_stub_plt_call
13704 && stub_type <= ppc_stub_plt_call_both)
e054468f 13705 {
05d0e962
AM
13706 stub_entry->target_value = sym_value;
13707 stub_entry->target_section = sym_sec;
e054468f
AM
13708 }
13709 else
13710 {
05d0e962
AM
13711 stub_entry->target_value = code_value;
13712 stub_entry->target_section = code_sec;
e054468f 13713 }
721956f4 13714 stub_entry->h = hash;
e054468f 13715 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
13716 stub_entry->symtype
13717 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 13718 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 13719
3d58e1fc
AM
13720 if (hash != NULL
13721 && (hash->elf.root.type == bfd_link_hash_defined
13722 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 13723 htab->stub_globals += 1;
721956f4
AM
13724 }
13725
13726 /* We're done with the internal relocs, free them. */
6cdc0ccc 13727 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 13728 free (internal_relocs);
721956f4 13729 }
6cdc0ccc
AM
13730
13731 if (local_syms != NULL
13732 && symtab_hdr->contents != (unsigned char *) local_syms)
13733 {
13734 if (!info->keep_memory)
13735 free (local_syms);
13736 else
13737 symtab_hdr->contents = (unsigned char *) local_syms;
13738 }
721956f4
AM
13739 }
13740
5c3dead3 13741 /* We may have added some stubs. Find out the new size of the
721956f4 13742 stub sections. */
d4aaa2a0 13743 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13744 {
13745 group->lr_restore = 0;
13746 group->eh_size = 0;
13747 if (group->stub_sec != NULL)
13748 {
13749 asection *stub_sec = group->stub_sec;
13750
13751 if (htab->stub_iteration <= STUB_SHRINK_ITER
13752 || stub_sec->rawsize < stub_sec->size)
13753 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13754 stub_sec->rawsize = stub_sec->size;
13755 stub_sec->size = 0;
13756 stub_sec->reloc_count = 0;
13757 stub_sec->flags &= ~SEC_RELOC;
13758 }
13759 }
a804e476
AM
13760 if (htab->tga_group != NULL)
13761 {
13762 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
13763 htab->tga_group->eh_size
13764 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
13765 htab->tga_group->lr_restore = 23 * 4;
13766 htab->tga_group->stub_sec->size = 24 * 4;
13767 }
eea6121a 13768
ba21f564
AM
13769 if (htab->stub_iteration <= STUB_SHRINK_ITER
13770 || htab->brlt->rawsize < htab->brlt->size)
13771 htab->brlt->rawsize = htab->brlt->size;
eea6121a 13772 htab->brlt->size = 0;
84f5d08e
AM
13773 htab->brlt->reloc_count = 0;
13774 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 13775 if (htab->relbrlt != NULL)
eea6121a 13776 htab->relbrlt->size = 0;
721956f4 13777
63bc6f6c 13778 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 13779
a4b6fadd
AM
13780 for (group = htab->group; group != NULL; group = group->next)
13781 if (group->needs_save_res)
13782 group->stub_sec->size += htab->sfpr->size;
13783
176a0d42
AM
13784 if (info->emitrelocations
13785 && htab->glink != NULL && htab->glink->size != 0)
13786 {
13787 htab->glink->reloc_count = 1;
13788 htab->glink->flags |= SEC_RELOC;
13789 }
13790
58d180e8
AM
13791 if (htab->glink_eh_frame != NULL
13792 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 13793 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 13794 {
2e0ce1c8 13795 size_t size = 0, align = 4;
58d180e8 13796
d4aaa2a0 13797 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13798 if (group->eh_size != 0)
13799 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 13800 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 13801 size += (24 + align - 1) & -align;
58d180e8 13802 if (size != 0)
2e0ce1c8
AM
13803 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13804 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13805 size = (size + align - 1) & -align;
58d180e8
AM
13806 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13807 htab->glink_eh_frame->size = size;
13808 }
13809
e7d1c40c 13810 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13811 for (group = htab->group; group != NULL; group = group->next)
13812 if (group->stub_sec != NULL)
691d2e9a
AM
13813 {
13814 int align = abs (htab->params->plt_stub_align);
13815 group->stub_sec->size
13816 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13817 }
d4aaa2a0
AM
13818
13819 for (group = htab->group; group != NULL; group = group->next)
13820 if (group->stub_sec != NULL
13821 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 13822 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 13823 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
13824 break;
13825
d4aaa2a0 13826 if (group == NULL
ba21f564
AM
13827 && (htab->brlt->rawsize == htab->brlt->size
13828 || (htab->stub_iteration > STUB_SHRINK_ITER
13829 && htab->brlt->rawsize > htab->brlt->size))
58d180e8 13830 && (htab->glink_eh_frame == NULL
a804e476
AM
13831 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
13832 && (htab->tga_group == NULL
13833 || htab->stub_iteration > 1))
5c3dead3
AM
13834 break;
13835
721956f4 13836 /* Ask the linker to do its stuff. */
e7d1c40c 13837 (*htab->params->layout_sections_again) ();
721956f4
AM
13838 }
13839
da44f4e5
AM
13840 if (htab->glink_eh_frame != NULL
13841 && htab->glink_eh_frame->size != 0)
13842 {
13843 bfd_vma val;
13844 bfd_byte *p, *last_fde;
13845 size_t last_fde_len, size, align, pad;
d4aaa2a0 13846 struct map_stub *group;
da44f4e5 13847
df136d64
AM
13848 /* It is necessary to at least have a rough outline of the
13849 linker generated CIEs and FDEs written before
13850 bfd_elf_discard_info is run, in order for these FDEs to be
13851 indexed in .eh_frame_hdr. */
da44f4e5
AM
13852 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13853 if (p == NULL)
13854 return FALSE;
13855 htab->glink_eh_frame->contents = p;
13856 last_fde = p;
2e0ce1c8 13857 align = 4;
da44f4e5
AM
13858
13859 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13860 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 13861 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 13862 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 13863 p += last_fde_len + 4;
da44f4e5 13864
d4aaa2a0 13865 for (group = htab->group; group != NULL; group = group->next)
df136d64 13866 if (group->eh_size != 0)
da44f4e5 13867 {
df136d64 13868 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 13869 last_fde = p;
df136d64 13870 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 13871 /* FDE length. */
2e0ce1c8 13872 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13873 p += 4;
13874 /* CIE pointer. */
13875 val = p - htab->glink_eh_frame->contents;
13876 bfd_put_32 (htab->elf.dynobj, val, p);
13877 p += 4;
13878 /* Offset to stub section, written later. */
13879 p += 4;
13880 /* stub section size. */
d4aaa2a0 13881 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
13882 p += 4;
13883 /* Augmentation. */
13884 p += 1;
df136d64
AM
13885 /* Make sure we don't have all nops. This is enough for
13886 elf-eh-frame.c to detect the last non-nop opcode. */
13887 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 13888 p = last_fde + last_fde_len + 4;
da44f4e5
AM
13889 }
13890 if (htab->glink != NULL && htab->glink->size != 0)
13891 {
13892 last_fde = p;
2e0ce1c8 13893 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13894 /* FDE length. */
2e0ce1c8 13895 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13896 p += 4;
13897 /* CIE pointer. */
13898 val = p - htab->glink_eh_frame->contents;
13899 bfd_put_32 (htab->elf.dynobj, val, p);
13900 p += 4;
13901 /* Offset to .glink, written later. */
13902 p += 4;
13903 /* .glink size. */
13904 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13905 p += 4;
13906 /* Augmentation. */
13907 p += 1;
13908
3cd7c7d7 13909 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
da44f4e5
AM
13910 *p++ = DW_CFA_register;
13911 *p++ = 65;
9f08fa5c 13912 *p++ = htab->opd_abi ? 12 : 0;
3cd7c7d7 13913 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
da44f4e5
AM
13914 *p++ = DW_CFA_restore_extended;
13915 *p++ = 65;
2e0ce1c8 13916 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13917 }
13918 /* Subsume any padding into the last FDE if user .eh_frame
13919 sections are aligned more than glink_eh_frame. Otherwise any
13920 zero padding will be seen as a terminator. */
2e0ce1c8 13921 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13922 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13923 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13924 htab->glink_eh_frame->size = size + pad;
13925 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13926 }
13927
d969d15f 13928 maybe_strip_output (info, htab->brlt);
2efec98b
AM
13929 if (htab->relbrlt != NULL)
13930 maybe_strip_output (info, htab->relbrlt);
d969d15f
AM
13931 if (htab->glink_eh_frame != NULL)
13932 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13933
b34976b6 13934 return TRUE;
721956f4
AM
13935}
13936
13937/* Called after we have determined section placement. If sections
805fc799 13938 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13939
805fc799 13940bfd_vma
1c865ab2 13941ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13942{
805fc799 13943 asection *s;
a27e685f 13944 bfd_vma TOCstart, adjust;
721956f4 13945
43417696
AM
13946 if (info != NULL)
13947 {
13948 struct elf_link_hash_entry *h;
13949 struct elf_link_hash_table *htab = elf_hash_table (info);
13950
13951 if (is_elf_hash_table (htab)
13952 && htab->hgot != NULL)
13953 h = htab->hgot;
13954 else
13955 {
13956 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13957 if (is_elf_hash_table (htab))
13958 htab->hgot = h;
13959 }
13960 if (h != NULL
13961 && h->root.type == bfd_link_hash_defined
13962 && !h->root.linker_def
13963 && (!is_elf_hash_table (htab)
13964 || h->def_regular))
13965 {
ed7007c1 13966 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
43417696
AM
13967 _bfd_set_gp_value (obfd, TOCstart);
13968 return TOCstart;
13969 }
13970 }
13971
805fc799
AM
13972 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13973 order. The TOC starts where the first of these sections starts. */
13974 s = bfd_get_section_by_name (obfd, ".got");
e054468f 13975 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13976 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 13977 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13978 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 13979 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13980 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 13981 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
13982 {
13983 /* This may happen for
13984 o references to TOC base (SYM@toc / TOC[tc0]) without a
13985 .toc directive
13986 o bad linker script
13987 o --gc-sections and empty TOC sections
13988
13989 FIXME: Warn user? */
13990
13991 /* Look for a likely section. We probably won't even be
13992 using TOCstart. */
13993 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13994 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13995 | SEC_EXCLUDE))
805fc799
AM
13996 == (SEC_ALLOC | SEC_SMALL_DATA))
13997 break;
721956f4 13998 if (s == NULL)
805fc799 13999 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14000 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
14001 == (SEC_ALLOC | SEC_SMALL_DATA))
14002 break;
721956f4 14003 if (s == NULL)
805fc799 14004 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
14005 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14006 == SEC_ALLOC)
805fc799 14007 break;
721956f4 14008 if (s == NULL)
805fc799 14009 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14010 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
14011 break;
14012 }
721956f4 14013
805fc799
AM
14014 TOCstart = 0;
14015 if (s != NULL)
14016 TOCstart = s->output_section->vma + s->output_offset;
721956f4 14017
a27e685f
AM
14018 /* Force alignment. */
14019 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14020 TOCstart -= adjust;
1c865ab2
AM
14021 _bfd_set_gp_value (obfd, TOCstart);
14022
810d4e75 14023 if (info != NULL && s != NULL)
1c865ab2
AM
14024 {
14025 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14026
810d4e75
AM
14027 if (htab != NULL)
14028 {
14029 if (htab->elf.hgot != NULL)
14030 {
a27e685f 14031 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
14032 htab->elf.hgot->root.u.def.section = s;
14033 }
14034 }
14035 else
1c865ab2 14036 {
810d4e75
AM
14037 struct bfd_link_hash_entry *bh = NULL;
14038 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
14039 s, TOC_BASE_OFF - adjust,
14040 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
14041 }
14042 }
805fc799 14043 return TOCstart;
721956f4
AM
14044}
14045
a345bc8d 14046/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 14047 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
14048
14049static bfd_boolean
49c09209 14050build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
14051{
14052 struct bfd_link_info *info;
14053 struct ppc_link_hash_table *htab;
49c09209 14054 struct plt_entry *ent;
a345bc8d
AM
14055 asection *s;
14056
14057 if (h->root.type == bfd_link_hash_indirect)
14058 return TRUE;
14059
49c09209
AM
14060 info = inf;
14061 htab = ppc_hash_table (info);
14062 if (htab == NULL)
14063 return FALSE;
14064
14065 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14066 if (ent->plt.offset != (bfd_vma) -1)
14067 {
14068 /* This symbol has an entry in the procedure linkage
14069 table. Set it up. */
14070 Elf_Internal_Rela rela;
2d7ad24e 14071 asection *plt, *relplt;
49c09209
AM
14072 bfd_byte *loc;
14073
30845f11 14074 if (use_local_plt (info, h))
49c09209
AM
14075 {
14076 if (!(h->def_regular
14077 && (h->root.type == bfd_link_hash_defined
14078 || h->root.type == bfd_link_hash_defweak)))
14079 continue;
2d7ad24e
AM
14080 if (h->type == STT_GNU_IFUNC)
14081 {
14082 plt = htab->elf.iplt;
14083 relplt = htab->elf.irelplt;
cebd6b8a 14084 htab->elf.ifunc_resolvers = TRUE;
2d7ad24e
AM
14085 if (htab->opd_abi)
14086 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14087 else
14088 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14089 }
49c09209 14090 else
2d7ad24e
AM
14091 {
14092 plt = htab->pltlocal;
14093 if (bfd_link_pic (info))
14094 {
14095 relplt = htab->relpltlocal;
14096 if (htab->opd_abi)
14097 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14098 else
14099 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14100 }
14101 else
14102 relplt = NULL;
14103 }
ed7007c1 14104 rela.r_addend = defined_sym_val (h) + ent->addend;
2d7ad24e
AM
14105
14106 if (relplt == NULL)
14107 {
14108 loc = plt->contents + ent->plt.offset;
14109 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14110 if (htab->opd_abi)
14111 {
14112 bfd_vma toc = elf_gp (info->output_bfd);
14113 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14114 bfd_put_64 (info->output_bfd, toc, loc + 8);
14115 }
14116 }
14117 else
14118 {
14119 rela.r_offset = (plt->output_section->vma
14120 + plt->output_offset
14121 + ent->plt.offset);
14122 loc = relplt->contents + (relplt->reloc_count++
14123 * sizeof (Elf64_External_Rela));
14124 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14125 }
49c09209
AM
14126 }
14127 else
14128 {
14129 rela.r_offset = (htab->elf.splt->output_section->vma
14130 + htab->elf.splt->output_offset
14131 + ent->plt.offset);
14132 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14133 rela.r_addend = ent->addend;
14134 loc = (htab->elf.srelplt->contents
14135 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14136 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14137 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
cebd6b8a 14138 htab->elf.ifunc_resolvers = TRUE;
2d7ad24e 14139 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 14140 }
49c09209
AM
14141 }
14142
a345bc8d
AM
14143 if (!h->pointer_equality_needed)
14144 return TRUE;
14145
14146 if (h->def_regular)
14147 return TRUE;
14148
9e390558 14149 s = htab->global_entry;
49c09209
AM
14150 if (s == NULL || s->size == 0)
14151 return TRUE;
14152
14153 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14154 if (ent->plt.offset != (bfd_vma) -1
14155 && ent->addend == 0)
a345bc8d
AM
14156 {
14157 bfd_byte *p;
14158 asection *plt;
14159 bfd_vma off;
14160
a345bc8d 14161 p = s->contents + h->root.u.def.value;
33e44f2e 14162 plt = htab->elf.splt;
30845f11 14163 if (use_local_plt (info, h))
2d7ad24e
AM
14164 {
14165 if (h->type == STT_GNU_IFUNC)
14166 plt = htab->elf.iplt;
14167 else
14168 plt = htab->pltlocal;
14169 }
49c09209 14170 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
14171 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14172
14173 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14174 {
14175 info->callbacks->einfo
c1c8c1ef 14176 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
14177 h->root.root.string);
14178 bfd_set_error (bfd_error_bad_value);
14179 htab->stub_error = TRUE;
14180 }
14181
7341d5e2
AM
14182 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14183 if (htab->params->emit_stub_syms)
14184 {
14185 size_t len = strlen (h->root.root.string);
14186 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14187
14188 if (name == NULL)
14189 return FALSE;
14190
14191 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14192 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
14193 if (h == NULL)
14194 return FALSE;
14195 if (h->root.type == bfd_link_hash_new)
14196 {
14197 h->root.type = bfd_link_hash_defined;
14198 h->root.u.def.section = s;
14199 h->root.u.def.value = p - s->contents;
14200 h->ref_regular = 1;
14201 h->def_regular = 1;
14202 h->ref_regular_nonweak = 1;
14203 h->forced_local = 1;
14204 h->non_elf = 0;
2ec55de3 14205 h->root.linker_def = 1;
7341d5e2
AM
14206 }
14207 }
14208
a345bc8d
AM
14209 if (PPC_HA (off) != 0)
14210 {
14211 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14212 p += 4;
14213 }
14214 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14215 p += 4;
14216 bfd_put_32 (s->owner, MTCTR_R12, p);
14217 p += 4;
407aa07c 14218 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
14219 break;
14220 }
14221 return TRUE;
14222}
14223
49c09209
AM
14224/* Write PLT relocs for locals. */
14225
14226static bfd_boolean
14227write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14228{
14229 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14230 bfd *ibfd;
14231
14232 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14233 {
14234 struct got_entry **lgot_ents, **end_lgot_ents;
14235 struct plt_entry **local_plt, **lplt, **end_local_plt;
14236 Elf_Internal_Shdr *symtab_hdr;
14237 bfd_size_type locsymcount;
14238 Elf_Internal_Sym *local_syms = NULL;
14239 struct plt_entry *ent;
14240
14241 if (!is_ppc64_elf (ibfd))
14242 continue;
14243
14244 lgot_ents = elf_local_got_ents (ibfd);
14245 if (!lgot_ents)
14246 continue;
14247
14248 symtab_hdr = &elf_symtab_hdr (ibfd);
14249 locsymcount = symtab_hdr->sh_info;
14250 end_lgot_ents = lgot_ents + locsymcount;
14251 local_plt = (struct plt_entry **) end_lgot_ents;
14252 end_local_plt = local_plt + locsymcount;
14253 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14254 for (ent = *lplt; ent != NULL; ent = ent->next)
14255 if (ent->plt.offset != (bfd_vma) -1)
14256 {
14257 Elf_Internal_Sym *sym;
14258 asection *sym_sec;
14259 asection *plt, *relplt;
14260 bfd_byte *loc;
14261 bfd_vma val;
49c09209
AM
14262
14263 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14264 lplt - local_plt, ibfd))
14265 {
c9594989 14266 if (symtab_hdr->contents != (unsigned char *) local_syms)
49c09209
AM
14267 free (local_syms);
14268 return FALSE;
14269 }
14270
14271 val = sym->st_value + ent->addend;
49c09209
AM
14272 if (sym_sec != NULL && sym_sec->output_section != NULL)
14273 val += sym_sec->output_offset + sym_sec->output_section->vma;
14274
2d7ad24e
AM
14275 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14276 {
cebd6b8a 14277 htab->elf.ifunc_resolvers = TRUE;
2d7ad24e
AM
14278 plt = htab->elf.iplt;
14279 relplt = htab->elf.irelplt;
14280 }
14281 else
14282 {
14283 plt = htab->pltlocal;
14284 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
14285 }
49c09209 14286
2d7ad24e
AM
14287 if (relplt == NULL)
14288 {
14289 loc = plt->contents + ent->plt.offset;
14290 bfd_put_64 (info->output_bfd, val, loc);
14291 if (htab->opd_abi)
14292 {
14293 bfd_vma toc = elf_gp (ibfd);
14294 bfd_put_64 (info->output_bfd, toc, loc + 8);
14295 }
14296 }
49c09209 14297 else
2d7ad24e
AM
14298 {
14299 Elf_Internal_Rela rela;
14300 rela.r_offset = (ent->plt.offset
14301 + plt->output_offset
14302 + plt->output_section->vma);
14303 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14304 {
14305 if (htab->opd_abi)
14306 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14307 else
14308 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14309 }
14310 else
14311 {
14312 if (htab->opd_abi)
14313 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14314 else
14315 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14316 }
14317 rela.r_addend = val;
14318 loc = relplt->contents + (relplt->reloc_count++
14319 * sizeof (Elf64_External_Rela));
14320 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14321 }
49c09209
AM
14322 }
14323
14324 if (local_syms != NULL
14325 && symtab_hdr->contents != (unsigned char *) local_syms)
14326 {
14327 if (!info->keep_memory)
14328 free (local_syms);
14329 else
14330 symtab_hdr->contents = (unsigned char *) local_syms;
14331 }
14332 }
14333 return TRUE;
14334}
14335
a804e476
AM
14336/* Emit the static wrapper function preserving registers around a
14337 __tls_get_addr_opt call. */
14338
14339static bfd_boolean
14340emit_tga_desc (struct ppc_link_hash_table *htab)
14341{
14342 asection *stub_sec = htab->tga_group->stub_sec;
14343 unsigned int cfa_updt = 11 * 4;
14344 bfd_byte *p;
14345 bfd_vma to, from, delta;
14346
14347 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14348 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14349 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14350 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14351 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14352 delta = to - from;
14353 if (delta + (1 << 25) >= 1 << 26)
14354 {
14355 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14356 htab->stub_error = TRUE;
14357 return FALSE;
14358 }
14359
14360 p = stub_sec->contents;
14361 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14362 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14363 p += 4;
14364 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14365 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14366}
14367
14368/* Emit eh_frame describing the static wrapper function. */
14369
14370static bfd_byte *
14371emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14372{
14373 unsigned int cfa_updt = 11 * 4;
14374 unsigned int i;
14375
14376 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14377 *p++ = DW_CFA_def_cfa_offset;
14378 if (htab->opd_abi)
14379 {
14380 *p++ = 128;
14381 *p++ = 1;
14382 }
14383 else
14384 *p++ = 96;
14385 *p++ = DW_CFA_offset_extended_sf;
14386 *p++ = 65;
14387 *p++ = (-16 / 8) & 0x7f;
14388 for (i = 4; i < 12; i++)
14389 {
14390 *p++ = DW_CFA_offset + i;
14391 *p++ = (htab->opd_abi ? 13 : 12) - i;
14392 }
14393 *p++ = DW_CFA_advance_loc + 10;
14394 *p++ = DW_CFA_def_cfa_offset;
14395 *p++ = 0;
14396 for (i = 4; i < 12; i++)
14397 *p++ = DW_CFA_restore + i;
14398 *p++ = DW_CFA_advance_loc + 2;
14399 *p++ = DW_CFA_restore_extended;
14400 *p++ = 65;
14401 return p;
14402}
14403
721956f4
AM
14404/* Build all the stubs associated with the current output file.
14405 The stubs are kept in a hash table attached to the main linker
14406 hash table. This function is called via gldelf64ppc_finish. */
14407
b34976b6 14408bfd_boolean
e7d1c40c 14409ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 14410 char **stats)
5d1634d7
AM
14411{
14412 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 14413 struct map_stub *group;
721956f4 14414 asection *stub_sec;
5d1634d7 14415 bfd_byte *p;
e717da7e 14416 int stub_sec_count = 0;
5d1634d7 14417
4dfe6ac6
NC
14418 if (htab == NULL)
14419 return FALSE;
14420
eea6121a 14421 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 14422 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14423 {
14424 group->eh_size = 0;
14425 group->lr_restore = 0;
14426 if ((stub_sec = group->stub_sec) != NULL
14427 && stub_sec->size != 0)
14428 {
14429 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14430 stub_sec->size);
14431 if (stub_sec->contents == NULL)
14432 return FALSE;
14433 stub_sec->size = 0;
14434 }
14435 }
5d1634d7 14436
23eb7e01 14437 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 14438 {
9f951329 14439 unsigned int indx;
ad8e1ba5 14440 bfd_vma plt0;
9f951329 14441
721956f4 14442 /* Build the .glink plt call stub. */
e7d1c40c 14443 if (htab->params->emit_stub_syms)
97b639ba
AM
14444 {
14445 struct elf_link_hash_entry *h;
468392fb
AM
14446 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14447 TRUE, FALSE, FALSE);
97b639ba
AM
14448 if (h == NULL)
14449 return FALSE;
14450 if (h->root.type == bfd_link_hash_new)
14451 {
14452 h->root.type = bfd_link_hash_defined;
14453 h->root.u.def.section = htab->glink;
ee4bf8d2 14454 h->root.u.def.value = 8;
f5385ebf
AM
14455 h->ref_regular = 1;
14456 h->def_regular = 1;
14457 h->ref_regular_nonweak = 1;
14458 h->forced_local = 1;
14459 h->non_elf = 0;
2ec55de3 14460 h->root.linker_def = 1;
97b639ba
AM
14461 }
14462 }
33e44f2e
AM
14463 plt0 = (htab->elf.splt->output_section->vma
14464 + htab->elf.splt->output_offset
14465 - 16);
176a0d42
AM
14466 if (info->emitrelocations)
14467 {
14468 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14469 if (r == NULL)
14470 return FALSE;
14471 r->r_offset = (htab->glink->output_offset
14472 + htab->glink->output_section->vma);
14473 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14474 r->r_addend = plt0;
14475 }
4ce794b7 14476 p = htab->glink->contents;
176a0d42 14477 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
14478 bfd_put_64 (htab->glink->owner, plt0, p);
14479 p += 8;
b9e5796b
AM
14480 if (htab->opd_abi)
14481 {
14482 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14483 p += 4;
14484 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14485 p += 4;
14486 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14487 p += 4;
14488 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14489 p += 4;
14490 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14491 p += 4;
14492 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14493 p += 4;
14494 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14495 p += 4;
14496 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14497 p += 4;
14498 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14499 p += 4;
14500 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14501 p += 4;
14502 }
14503 else
14504 {
3cd7c7d7
AM
14505 unsigned int insn;
14506
14507 /* 0:
14508 . .quad plt0-1f # plt0 entry relative to 1:
14509 #
14510 # We get here with r12 initially @ a glink branch
14511 # Load the address of _dl_runtime_resolve from plt0 and
14512 # jump to it, with r0 set to the index of the PLT entry
14513 # to be resolved and r11 the link map.
14514 __glink_PLTresolve:
14515 . std %r2,24(%r1) # optional
14516 . mflr %r0
14517 . bcl 20,31,1f
14518 1:
14519 . mflr %r11
14520 . mtlr %r0
14521 . ld %r0,(0b-1b)(%r11)
14522 . sub %r12,%r12,%r11
14523 . add %r11,%r0,%r11
14524 . addi %r0,%r12,1b-2f
14525 . ld %r12,0(%r11)
14526 . srdi %r0,%r0,2
14527 . mtctr %r12
14528 . ld %r11,8(%r11)
14529 . bctr
14530 2:
14531 . b __glink_PLTresolve
14532 . ...
14533 . b __glink_PLTresolve */
14534
14535 if (htab->has_plt_localentry0)
14536 {
14537 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14538 p += 4;
14539 }
b9e5796b
AM
14540 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14541 p += 4;
14542 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14543 p += 4;
14544 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14545 p += 4;
b9e5796b
AM
14546 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14547 p += 4;
3cd7c7d7
AM
14548 if (htab->has_plt_localentry0)
14549 insn = LD_R0_0R11 | (-20 & 0xfffc);
14550 else
14551 insn = LD_R0_0R11 | (-16 & 0xfffc);
14552 bfd_put_32 (htab->glink->owner, insn, p);
14553 p += 4;
b9e5796b
AM
14554 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14555 p += 4;
3cd7c7d7 14556 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
b9e5796b 14557 p += 4;
3cd7c7d7 14558 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
b9e5796b
AM
14559 p += 4;
14560 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14561 p += 4;
14562 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14563 p += 4;
14564 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14565 p += 4;
14566 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14567 p += 4;
14568 }
407aa07c
AM
14569 bfd_put_32 (htab->glink->owner, BCTR, p);
14570 p += 4;
c75bc4f7 14571 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 14572
9f951329
AM
14573 /* Build the .glink lazy link call stubs. */
14574 indx = 0;
9e390558 14575 while (p < htab->glink->contents + htab->glink->size)
9f951329 14576 {
b9e5796b 14577 if (htab->opd_abi)
9f951329 14578 {
b9e5796b
AM
14579 if (indx < 0x8000)
14580 {
14581 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14582 p += 4;
14583 }
14584 else
14585 {
14586 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14587 p += 4;
14588 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14589 p);
14590 p += 4;
14591 }
9f951329 14592 }
4ce794b7 14593 bfd_put_32 (htab->glink->owner,
ee4bf8d2 14594 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 14595 indx++;
9f951329
AM
14596 p += 4;
14597 }
5d1634d7 14598 }
5d1634d7 14599
a804e476
AM
14600 if (htab->tga_group != NULL)
14601 {
14602 htab->tga_group->lr_restore = 23 * 4;
14603 htab->tga_group->stub_sec->size = 24 * 4;
14604 if (!emit_tga_desc (htab))
14605 return FALSE;
14606 if (htab->glink_eh_frame != NULL
14607 && htab->glink_eh_frame->size != 0)
14608 {
14609 size_t align = 4;
14610
14611 p = htab->glink_eh_frame->contents;
14612 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14613 p += 17;
14614 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
14615 }
14616 }
14617
49c09209
AM
14618 /* Build .glink global entry stubs, and PLT relocs for globals. */
14619 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
14620
14621 if (!write_plt_relocs_for_local_syms (info))
14622 return FALSE;
9e390558 14623
7341d5e2 14624 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 14625 {
4ce794b7 14626 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 14627 htab->brlt->size);
4ce794b7 14628 if (htab->brlt->contents == NULL)
b34976b6 14629 return FALSE;
721956f4 14630 }
ee75fd95 14631 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
14632 {
14633 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 14634 htab->relbrlt->size);
63bc6f6c
AM
14635 if (htab->relbrlt->contents == NULL)
14636 return FALSE;
14637 }
5d1634d7 14638
721956f4
AM
14639 /* Build the stubs as directed by the stub hash table. */
14640 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 14641
a4b6fadd
AM
14642 for (group = htab->group; group != NULL; group = group->next)
14643 if (group->needs_save_res)
7dda8d3c 14644 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 14645
aa8a7074
AM
14646 if (htab->relbrlt != NULL)
14647 htab->relbrlt->reloc_count = 0;
14648
e7d1c40c 14649 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
14650 for (group = htab->group; group != NULL; group = group->next)
14651 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
14652 {
14653 int align = abs (htab->params->plt_stub_align);
14654 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14655 }
794e51c0 14656
7dda8d3c
AM
14657 for (group = htab->group; group != NULL; group = group->next)
14658 if (group->needs_save_res)
14659 {
14660 stub_sec = group->stub_sec;
14661 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14662 htab->sfpr->contents, htab->sfpr->size);
14663 if (htab->params->emit_stub_syms)
14664 {
14665 unsigned int i;
14666
14667 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14668 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14669 return FALSE;
14670 }
14671 }
14672
df136d64
AM
14673 if (htab->glink_eh_frame != NULL
14674 && htab->glink_eh_frame->size != 0)
14675 {
14676 bfd_vma val;
14677 size_t align = 4;
14678
14679 p = htab->glink_eh_frame->contents;
14680 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14681
14682 for (group = htab->group; group != NULL; group = group->next)
14683 if (group->eh_size != 0)
14684 {
14685 /* Offset to stub section. */
14686 val = (group->stub_sec->output_section->vma
14687 + group->stub_sec->output_offset);
14688 val -= (htab->glink_eh_frame->output_section->vma
14689 + htab->glink_eh_frame->output_offset
14690 + (p + 8 - htab->glink_eh_frame->contents));
14691 if (val + 0x80000000 > 0xffffffff)
14692 {
14693 _bfd_error_handler
14694 (_("%s offset too large for .eh_frame sdata4 encoding"),
14695 group->stub_sec->name);
14696 return FALSE;
14697 }
14698 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14699 p += (group->eh_size + 17 + 3) & -4;
14700 }
14701 if (htab->glink != NULL && htab->glink->size != 0)
14702 {
14703 /* Offset to .glink. */
14704 val = (htab->glink->output_section->vma
14705 + htab->glink->output_offset
14706 + 8);
14707 val -= (htab->glink_eh_frame->output_section->vma
14708 + htab->glink_eh_frame->output_offset
14709 + (p + 8 - htab->glink_eh_frame->contents));
14710 if (val + 0x80000000 > 0xffffffff)
14711 {
14712 _bfd_error_handler
14713 (_("%s offset too large for .eh_frame sdata4 encoding"),
14714 htab->glink->name);
14715 return FALSE;
14716 }
14717 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14718 p += (24 + align - 1) & -align;
14719 }
14720 }
14721
d4aaa2a0
AM
14722 for (group = htab->group; group != NULL; group = group->next)
14723 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
14724 {
14725 stub_sec_count += 1;
c9301e31
AM
14726 if (stub_sec->rawsize != stub_sec->size
14727 && (htab->stub_iteration <= STUB_SHRINK_ITER
14728 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
14729 break;
14730 }
5d1634d7 14731
25516cc5 14732 if (group != NULL)
5d1634d7 14733 {
b34976b6 14734 htab->stub_error = TRUE;
cf97bcb0 14735 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 14736 }
721956f4 14737
d2a300cf
AM
14738 if (htab->stub_error)
14739 return FALSE;
14740
14741 if (stats != NULL)
14742 {
988b7300
AM
14743 char *groupmsg;
14744 if (asprintf (&groupmsg,
14745 ngettext ("linker stubs in %u group\n",
14746 "linker stubs in %u groups\n",
14747 stub_sec_count),
14748 stub_sec_count) < 0)
14749 *stats = NULL;
14750 else
14751 {
14752 if (asprintf (stats, _("%s"
14753 " branch %lu\n"
14754 " branch toc adj %lu\n"
14755 " branch notoc %lu\n"
14756 " branch both %lu\n"
14757 " long branch %lu\n"
14758 " long toc adj %lu\n"
14759 " long notoc %lu\n"
14760 " long both %lu\n"
14761 " plt call %lu\n"
14762 " plt call save %lu\n"
14763 " plt call notoc %lu\n"
14764 " plt call both %lu\n"
14765 " global entry %lu"),
14766 groupmsg,
14767 htab->stub_count[ppc_stub_long_branch - 1],
14768 htab->stub_count[ppc_stub_long_branch_r2off - 1],
14769 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14770 htab->stub_count[ppc_stub_long_branch_both - 1],
14771 htab->stub_count[ppc_stub_plt_branch - 1],
14772 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14773 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14774 htab->stub_count[ppc_stub_plt_branch_both - 1],
14775 htab->stub_count[ppc_stub_plt_call - 1],
14776 htab->stub_count[ppc_stub_plt_call_r2save - 1],
14777 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14778 htab->stub_count[ppc_stub_plt_call_both - 1],
14779 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
14780 *stats = NULL;
14781 free (groupmsg);
14782 }
d2a300cf
AM
14783 }
14784 return TRUE;
5bd4f169
AM
14785}
14786
60124e18
AM
14787/* What to do when ld finds relocations against symbols defined in
14788 discarded sections. */
14789
14790static unsigned int
14791ppc64_elf_action_discarded (asection *sec)
14792{
14793 if (strcmp (".opd", sec->name) == 0)
14794 return 0;
14795
14796 if (strcmp (".toc", sec->name) == 0)
14797 return 0;
14798
bce50a28
JJ
14799 if (strcmp (".toc1", sec->name) == 0)
14800 return 0;
14801
60124e18
AM
14802 return _bfd_elf_default_action_discarded (sec);
14803}
14804
e59a1001
AM
14805/* These are the dynamic relocations supported by glibc. */
14806
14807static bfd_boolean
14808ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
14809{
14810 switch (r_type)
14811 {
14812 case R_PPC64_RELATIVE:
14813 case R_PPC64_NONE:
14814 case R_PPC64_ADDR64:
14815 case R_PPC64_GLOB_DAT:
14816 case R_PPC64_IRELATIVE:
14817 case R_PPC64_JMP_IREL:
14818 case R_PPC64_JMP_SLOT:
14819 case R_PPC64_DTPMOD64:
14820 case R_PPC64_DTPREL64:
14821 case R_PPC64_TPREL64:
14822 case R_PPC64_TPREL16_LO_DS:
14823 case R_PPC64_TPREL16_DS:
14824 case R_PPC64_TPREL16:
14825 case R_PPC64_TPREL16_LO:
14826 case R_PPC64_TPREL16_HI:
14827 case R_PPC64_TPREL16_HIGH:
14828 case R_PPC64_TPREL16_HA:
14829 case R_PPC64_TPREL16_HIGHA:
14830 case R_PPC64_TPREL16_HIGHER:
14831 case R_PPC64_TPREL16_HIGHEST:
14832 case R_PPC64_TPREL16_HIGHERA:
14833 case R_PPC64_TPREL16_HIGHESTA:
14834 case R_PPC64_ADDR16_LO_DS:
14835 case R_PPC64_ADDR16_LO:
14836 case R_PPC64_ADDR16_HI:
14837 case R_PPC64_ADDR16_HIGH:
14838 case R_PPC64_ADDR16_HA:
14839 case R_PPC64_ADDR16_HIGHA:
14840 case R_PPC64_REL30:
14841 case R_PPC64_COPY:
14842 case R_PPC64_UADDR64:
14843 case R_PPC64_UADDR32:
14844 case R_PPC64_ADDR32:
14845 case R_PPC64_ADDR24:
14846 case R_PPC64_ADDR16:
14847 case R_PPC64_UADDR16:
14848 case R_PPC64_ADDR16_DS:
14849 case R_PPC64_ADDR16_HIGHER:
14850 case R_PPC64_ADDR16_HIGHEST:
14851 case R_PPC64_ADDR16_HIGHERA:
14852 case R_PPC64_ADDR16_HIGHESTA:
14853 case R_PPC64_ADDR14:
14854 case R_PPC64_ADDR14_BRTAKEN:
14855 case R_PPC64_ADDR14_BRNTAKEN:
14856 case R_PPC64_REL32:
14857 case R_PPC64_REL64:
14858 return TRUE;
14859
14860 default:
14861 return FALSE;
14862 }
14863}
14864
5bd4f169
AM
14865/* The RELOCATE_SECTION function is called by the ELF backend linker
14866 to handle the relocations for a section.
14867
14868 The relocs are always passed as Rela structures; if the section
14869 actually uses Rel structures, the r_addend field will always be
14870 zero.
14871
14872 This function is responsible for adjust the section contents as
14873 necessary, and (if using Rela relocs and generating a
1049f94e 14874 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
14875 necessary.
14876
14877 This function does not have to worry about setting the reloc
14878 address or the reloc symbol index.
14879
14880 LOCAL_SYMS is a pointer to the swapped in local symbols.
14881
14882 LOCAL_SECTIONS is an array giving the section in the input file
14883 corresponding to the st_shndx field of each local symbol.
14884
14885 The global hash table entry for the global symbols can be found
14886 via elf_sym_hashes (input_bfd).
14887
1049f94e 14888 When generating relocatable output, this function must handle
5bd4f169
AM
14889 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14890 going to be the section symbol corresponding to the output
14891 section, which means that the addend must be adjusted
14892 accordingly. */
14893
b34976b6 14894static bfd_boolean
4ce794b7
AM
14895ppc64_elf_relocate_section (bfd *output_bfd,
14896 struct bfd_link_info *info,
14897 bfd *input_bfd,
14898 asection *input_section,
14899 bfd_byte *contents,
14900 Elf_Internal_Rela *relocs,
14901 Elf_Internal_Sym *local_syms,
14902 asection **local_sections)
5bd4f169 14903{
65f38f15 14904 struct ppc_link_hash_table *htab;
5bd4f169
AM
14905 Elf_Internal_Shdr *symtab_hdr;
14906 struct elf_link_hash_entry **sym_hashes;
5bd4f169 14907 Elf_Internal_Rela *rel;
c316a17c 14908 Elf_Internal_Rela *wrel;
5bd4f169 14909 Elf_Internal_Rela *relend;
411e1bfb
AM
14910 Elf_Internal_Rela outrel;
14911 bfd_byte *loc;
411e1bfb 14912 struct got_entry **local_got_ents;
5bd4f169 14913 bfd_vma TOCstart;
b34976b6
AM
14914 bfd_boolean ret = TRUE;
14915 bfd_boolean is_opd;
794e51c0
AM
14916 /* Assume 'at' branch hints. */
14917 bfd_boolean is_isa_v2 = TRUE;
e59a1001 14918 bfd_boolean warned_dynamic = FALSE;
95f0d0d2 14919 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 14920
65f38f15 14921 /* Initialize howto table if needed. */
5bd4f169 14922 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
14923 ppc_howto_init ();
14924
65f38f15 14925 htab = ppc_hash_table (info);
4dfe6ac6
NC
14926 if (htab == NULL)
14927 return FALSE;
ee75fd95
AM
14928
14929 /* Don't relocate stub sections. */
e7d1c40c 14930 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
14931 return TRUE;
14932
7af5d5c4
AM
14933 if (!is_ppc64_elf (input_bfd))
14934 {
14935 bfd_set_error (bfd_error_wrong_format);
14936 return FALSE;
14937 }
0ffa91dd 14938
411e1bfb 14939 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 14940 TOCstart = elf_gp (output_bfd);
0ffa91dd 14941 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 14942 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 14943 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 14944
c316a17c 14945 rel = wrel = relocs;
5bd4f169 14946 relend = relocs + input_section->reloc_count;
c316a17c 14947 for (; rel < relend; wrel++, rel++)
5bd4f169 14948 {
04c9666a 14949 enum elf_ppc64_reloc_type r_type;
31c76678 14950 bfd_vma addend;
5bd4f169
AM
14951 bfd_reloc_status_type r;
14952 Elf_Internal_Sym *sym;
14953 asection *sec;
039b3fef
AM
14954 struct elf_link_hash_entry *h_elf;
14955 struct ppc_link_hash_entry *h;
14956 struct ppc_link_hash_entry *fdh;
5bd4f169 14957 const char *sym_name;
0d4792f7 14958 unsigned long r_symndx, toc_symndx;
3a71aa26 14959 bfd_vma toc_addend;
f961d9dd
AM
14960 unsigned char tls_mask, tls_gd, tls_type;
14961 unsigned char sym_type;
5bd4f169 14962 bfd_vma relocation;
23cedd1d 14963 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 14964 bfd_boolean warned;
bc30df16 14965 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 14966 unsigned int insn;
e11840f9 14967 unsigned int mask;
721956f4
AM
14968 struct ppc_stub_hash_entry *stub_entry;
14969 bfd_vma max_br_offset;
14970 bfd_vma from;
c316a17c 14971 Elf_Internal_Rela orig_rel;
b80eed39
AM
14972 reloc_howto_type *howto;
14973 struct reloc_howto_struct alt_howto;
4a421c53
AM
14974 uint64_t pinsn;
14975 bfd_vma offset;
5bd4f169 14976
c316a17c
AM
14977 again:
14978 orig_rel = *rel;
14979
4ce794b7 14980 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 14981 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
14982
14983 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14984 symbol of the previous ADDR64 reloc. The symbol gives us the
14985 proper TOC base to use. */
14986 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
14987 && wrel != relocs
14988 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 14989 && is_opd)
c316a17c 14990 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 14991
4ce794b7
AM
14992 sym = NULL;
14993 sec = NULL;
039b3fef 14994 h_elf = NULL;
4ce794b7 14995 sym_name = NULL;
b34976b6
AM
14996 unresolved_reloc = FALSE;
14997 warned = FALSE;
65f38f15 14998
0b13192e 14999 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
15000 {
15001 /* It's a local symbol. */
74f0fb50 15002 struct _opd_sec_data *opd;
4025353c 15003
5bd4f169
AM
15004 sym = local_syms + r_symndx;
15005 sec = local_sections[r_symndx];
26c61ae5 15006 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 15007 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 15008 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
15009 opd = get_opd_info (sec);
15010 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 15011 {
51aecdc5
AM
15012 long adjust = opd->adjust[OPD_NDX (sym->st_value
15013 + rel->r_addend)];
4025353c
AM
15014 if (adjust == -1)
15015 relocation = 0;
15016 else
4cc603a5
AM
15017 {
15018 /* If this is a relocation against the opd section sym
15019 and we have edited .opd, adjust the reloc addend so
15020 that ld -r and ld --emit-relocs output is correct.
15021 If it is a reloc against some other .opd symbol,
15022 then the symbol value will be adjusted later. */
15023 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15024 rel->r_addend += adjust;
15025 else
15026 relocation += adjust;
15027 }
1e2f5b6e 15028 }
5bd4f169
AM
15029 }
15030 else
15031 {
62d887d4
L
15032 bfd_boolean ignored;
15033
b2a8e766
AM
15034 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15035 r_symndx, symtab_hdr, sym_hashes,
039b3fef 15036 h_elf, sec, relocation,
62d887d4 15037 unresolved_reloc, warned, ignored);
039b3fef
AM
15038 sym_name = h_elf->root.root.string;
15039 sym_type = h_elf->type;
b69fdb4e
AM
15040 if (sec != NULL
15041 && sec->owner == output_bfd
15042 && strcmp (sec->name, ".opd") == 0)
15043 {
15044 /* This is a symbol defined in a linker script. All
15045 such are defined in output sections, even those
15046 defined by simple assignment from a symbol defined in
15047 an input section. Transfer the symbol to an
15048 appropriate input .opd section, so that a branch to
15049 this symbol will be mapped to the location specified
15050 by the opd entry. */
15051 struct bfd_link_order *lo;
15052 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15053 if (lo->type == bfd_indirect_link_order)
15054 {
15055 asection *isec = lo->u.indirect.section;
15056 if (h_elf->root.u.def.value >= isec->output_offset
15057 && h_elf->root.u.def.value < (isec->output_offset
15058 + isec->size))
15059 {
15060 h_elf->root.u.def.value -= isec->output_offset;
15061 h_elf->root.u.def.section = isec;
15062 sec = isec;
15063 break;
15064 }
15065 }
15066 }
5bd4f169 15067 }
ed7007c1 15068 h = ppc_elf_hash_entry (h_elf);
5bd4f169 15069
dbaa2011 15070 if (sec != NULL && discarded_section (sec))
c316a17c
AM
15071 {
15072 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15073 input_bfd, input_section,
0930cb30 15074 contents, rel->r_offset);
c316a17c
AM
15075 wrel->r_offset = rel->r_offset;
15076 wrel->r_info = 0;
15077 wrel->r_addend = 0;
15078
15079 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 15080 symbols defined in discarded sections. Not done for
c316a17c
AM
15081 non-debug to preserve relocs in .eh_frame which the
15082 eh_frame editing code expects to be present. */
15083 if (bfd_link_relocatable (info)
15084 && (input_section->flags & SEC_DEBUGGING))
15085 wrel--;
15086
15087 continue;
15088 }
ab96bf03 15089
0e1862bb 15090 if (bfd_link_relocatable (info))
c316a17c 15091 goto copy_reloc;
ab96bf03 15092
f40da81b
AM
15093 if (h != NULL && &h->elf == htab->elf.hgot)
15094 {
6f20ed8a 15095 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
15096 sec = bfd_abs_section_ptr;
15097 unresolved_reloc = FALSE;
15098 }
15099
951fd09b
AM
15100 /* TLS optimizations. Replace instruction sequences and relocs
15101 based on information we collected in tls_optimize. We edit
15102 RELOCS so that --emit-relocs will output something sensible
15103 for the final instruction stream. */
15104 tls_mask = 0;
15105 tls_gd = 0;
0d4792f7 15106 toc_symndx = 0;
727fc41e
AM
15107 if (h != NULL)
15108 tls_mask = h->tls_mask;
15109 else if (local_got_ents != NULL)
411e1bfb 15110 {
e054468f
AM
15111 struct plt_entry **local_plt = (struct plt_entry **)
15112 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 15113 unsigned char *lgot_masks = (unsigned char *)
e054468f 15114 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
15115 tls_mask = lgot_masks[r_symndx];
15116 }
37da22e5 15117 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
15118 && (r_type == R_PPC64_TLS
15119 || r_type == R_PPC64_TLSGD
15120 || r_type == R_PPC64_TLSLD))
15121 {
15122 /* Check for toc tls entries. */
f961d9dd 15123 unsigned char *toc_tls;
0d4792f7 15124
727fc41e
AM
15125 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15126 &local_syms, rel, input_bfd))
15127 return FALSE;
0d4792f7 15128
727fc41e
AM
15129 if (toc_tls)
15130 tls_mask = *toc_tls;
0d4792f7
AM
15131 }
15132
15133 /* Check that tls relocs are used with tls syms, and non-tls
15134 relocs are used with non-tls syms. */
cf35638d 15135 if (r_symndx != STN_UNDEF
0d4792f7
AM
15136 && r_type != R_PPC64_NONE
15137 && (h == NULL
039b3fef
AM
15138 || h->elf.root.type == bfd_link_hash_defined
15139 || h->elf.root.type == bfd_link_hash_defweak)
71c4e95a 15140 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
0d4792f7 15141 {
37da22e5 15142 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
15143 && (r_type == R_PPC64_TLS
15144 || r_type == R_PPC64_TLSGD
15145 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
15146 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15147 ;
15148 else
25f53a85 15149 info->callbacks->einfo
1d483afe 15150 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 15151 /* xgettext:c-format */
c1c8c1ef 15152 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 15153 /* xgettext:c-format */
c1c8c1ef 15154 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 15155 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
15156 ppc64_elf_howto_table[r_type]->name,
15157 sym_name);
411e1bfb
AM
15158 }
15159
15160 /* Ensure reloc mapping code below stays sane. */
15161 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15162 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15163 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15164 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15165 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15166 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15167 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15168 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15169 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15170 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15171 abort ();
0d4792f7 15172
411e1bfb
AM
15173 switch (r_type)
15174 {
15175 default:
411e1bfb
AM
15176 break;
15177
ba761f19 15178 case R_PPC64_LO_DS_OPT:
95f0d0d2 15179 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
2365f8d7 15180 if ((insn & (0x3fu << 26)) != 58u << 26)
ba761f19
AM
15181 abort ();
15182 insn += (14u << 26) - (58u << 26);
95f0d0d2 15183 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
15184 r_type = R_PPC64_TOC16_LO;
15185 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15186 break;
15187
411e1bfb
AM
15188 case R_PPC64_TOC16:
15189 case R_PPC64_TOC16_LO:
15190 case R_PPC64_TOC16_DS:
15191 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
15192 {
15193 /* Check for toc tls entries. */
f961d9dd 15194 unsigned char *toc_tls;
951fd09b 15195 int retval;
411e1bfb 15196
3a71aa26
AM
15197 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15198 &local_syms, rel, input_bfd);
951fd09b 15199 if (retval == 0)
411e1bfb
AM
15200 return FALSE;
15201
15202 if (toc_tls)
15203 {
951fd09b 15204 tls_mask = *toc_tls;
411e1bfb
AM
15205 if (r_type == R_PPC64_TOC16_DS
15206 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 15207 {
37da22e5 15208 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
15209 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15210 goto toctprel;
15211 }
411e1bfb 15212 else
951fd09b
AM
15213 {
15214 /* If we found a GD reloc pair, then we might be
15215 doing a GD->IE transition. */
15216 if (retval == 2)
15217 {
b00a0a86 15218 tls_gd = TLS_GDIE;
37da22e5
AM
15219 if ((tls_mask & TLS_TLS) != 0
15220 && (tls_mask & TLS_GD) == 0)
102890f0 15221 goto tls_ldgd_opt;
951fd09b
AM
15222 }
15223 else if (retval == 3)
15224 {
37da22e5
AM
15225 if ((tls_mask & TLS_TLS) != 0
15226 && (tls_mask & TLS_LD) == 0)
102890f0 15227 goto tls_ldgd_opt;
951fd09b
AM
15228 }
15229 }
411e1bfb
AM
15230 }
15231 }
15232 break;
15233
9d6ded02
AM
15234 case R_PPC64_GOT_TPREL16_HI:
15235 case R_PPC64_GOT_TPREL16_HA:
37da22e5 15236 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
15237 && (tls_mask & TLS_TPREL) == 0)
15238 {
15239 rel->r_offset -= d_offset;
95f0d0d2 15240 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
15241 r_type = R_PPC64_NONE;
15242 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15243 }
15244 break;
15245
411e1bfb
AM
15246 case R_PPC64_GOT_TPREL16_DS:
15247 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 15248 if ((tls_mask & TLS_TLS) != 0
951fd09b 15249 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 15250 {
81407a69 15251 toctprel:
95f0d0d2 15252 insn = bfd_get_32 (input_bfd,
c316a17c 15253 contents + rel->r_offset - d_offset);
411e1bfb
AM
15254 insn &= 31 << 21;
15255 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 15256 bfd_put_32 (input_bfd, insn,
c316a17c 15257 contents + rel->r_offset - d_offset);
411e1bfb 15258 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
15259 if (toc_symndx != 0)
15260 {
15261 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 15262 rel->r_addend = toc_addend;
0d4792f7
AM
15263 /* We changed the symbol. Start over in order to
15264 get h, sym, sec etc. right. */
c316a17c 15265 goto again;
0d4792f7
AM
15266 }
15267 else
15268 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15269 }
15270 break;
15271
87c69f97 15272 case R_PPC64_GOT_TPREL_PCREL34:
c213164a
AM
15273 if ((tls_mask & TLS_TLS) != 0
15274 && (tls_mask & TLS_TPREL) == 0)
15275 {
15276 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15277 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15278 pinsn <<= 32;
15279 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15280 pinsn += ((2ULL << 56) + (-1ULL << 52)
15281 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15282 bfd_put_32 (input_bfd, pinsn >> 32,
15283 contents + rel->r_offset);
15284 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15285 contents + rel->r_offset + 4);
15286 r_type = R_PPC64_TPREL34;
15287 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15288 }
15289 break;
15290
411e1bfb 15291 case R_PPC64_TLS:
37da22e5 15292 if ((tls_mask & TLS_TLS) != 0
951fd09b 15293 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 15294 {
c213164a 15295 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2d0f3896
AM
15296 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15297 if (insn == 0)
c213164a
AM
15298 break;
15299 if ((rel->r_offset & 3) == 0)
0d4792f7 15300 {
c213164a
AM
15301 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15302 /* Was PPC64_TLS which sits on insn boundary, now
15303 PPC64_TPREL16_LO which is at low-order half-word. */
15304 rel->r_offset += d_offset;
15305 r_type = R_PPC64_TPREL16_LO;
15306 if (toc_symndx != 0)
15307 {
15308 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15309 rel->r_addend = toc_addend;
15310 /* We changed the symbol. Start over in order to
15311 get h, sym, sec etc. right. */
15312 goto again;
15313 }
15314 else
15315 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15316 }
15317 else if ((rel->r_offset & 3) == 1)
15318 {
15319 /* For pcrel IE to LE we already have the full
15320 offset and thus don't need an addi here. A nop
15321 or mr will do. */
2365f8d7 15322 if ((insn & (0x3fu << 26)) == 14 << 26)
c213164a
AM
15323 {
15324 /* Extract regs from addi rt,ra,si. */
15325 unsigned int rt = (insn >> 21) & 0x1f;
15326 unsigned int ra = (insn >> 16) & 0x1f;
15327 if (rt == ra)
15328 insn = NOP;
15329 else
15330 {
15331 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15332 insn = (rt << 16) | (ra << 21) | (ra << 11);
15333 insn |= (31u << 26) | (444u << 1);
15334 }
15335 }
15336 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
0d4792f7 15337 }
411e1bfb
AM
15338 }
15339 break;
15340
411e1bfb
AM
15341 case R_PPC64_GOT_TLSGD16_HI:
15342 case R_PPC64_GOT_TLSGD16_HA:
b00a0a86 15343 tls_gd = TLS_GDIE;
37da22e5 15344 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
15345 goto tls_gdld_hi;
15346 break;
15347
411e1bfb
AM
15348 case R_PPC64_GOT_TLSLD16_HI:
15349 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 15350 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 15351 {
951fd09b
AM
15352 tls_gdld_hi:
15353 if ((tls_mask & tls_gd) != 0)
15354 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15355 + R_PPC64_GOT_TPREL16_DS);
15356 else
411e1bfb 15357 {
4fe5ca5b 15358 rel->r_offset -= d_offset;
95f0d0d2 15359 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 15360 r_type = R_PPC64_NONE;
411e1bfb 15361 }
951fd09b 15362 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15363 }
15364 break;
15365
951fd09b
AM
15366 case R_PPC64_GOT_TLSGD16:
15367 case R_PPC64_GOT_TLSGD16_LO:
b00a0a86 15368 tls_gd = TLS_GDIE;
37da22e5 15369 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 15370 goto tls_ldgd_opt;
951fd09b 15371 break;
411e1bfb 15372
951fd09b
AM
15373 case R_PPC64_GOT_TLSLD16:
15374 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 15375 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 15376 {
b9f04fe0 15377 unsigned int insn1, insn2;
102890f0
AM
15378
15379 tls_ldgd_opt:
727fc41e
AM
15380 offset = (bfd_vma) -1;
15381 /* If not using the newer R_PPC64_TLSGD/LD to mark
15382 __tls_get_addr calls, we must trust that the call
15383 stays with its arg setup insns, ie. that the next
15384 reloc is the __tls_get_addr call associated with
15385 the current reloc. Edit both insns. */
9737e8af 15386 if (input_section->nomark_tls_get_addr
727fc41e
AM
15387 && rel + 1 < relend
15388 && branch_reloc_hash_match (input_bfd, rel + 1,
9e7028aa
AM
15389 htab->tls_get_addr_fd,
15390 htab->tga_desc_fd,
727fc41e 15391 htab->tls_get_addr,
9e7028aa 15392 htab->tga_desc))
727fc41e 15393 offset = rel[1].r_offset;
b86ac8e3
AM
15394 /* We read the low GOT_TLS (or TOC16) insn because we
15395 need to keep the destination reg. It may be
15396 something other than the usual r3, and moved to r3
15397 before the call by intervening code. */
95f0d0d2 15398 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 15399 contents + rel->r_offset - d_offset);
102890f0 15400 if ((tls_mask & tls_gd) != 0)
411e1bfb 15401 {
102890f0 15402 /* IE */
b86ac8e3 15403 insn1 &= (0x1f << 21) | (0x1f << 16);
2365f8d7 15404 insn1 |= 58u << 26; /* ld */
102890f0 15405 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 15406 if (offset != (bfd_vma) -1)
f58d5a2d 15407 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
46e9995a
AM
15408 if (r_type == R_PPC64_TOC16
15409 || r_type == R_PPC64_TOC16_LO)
102890f0 15410 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
46e9995a
AM
15411 else
15412 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15413 + R_PPC64_GOT_TPREL16_DS);
102890f0
AM
15414 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15415 }
15416 else
15417 {
15418 /* LE */
b86ac8e3
AM
15419 insn1 &= 0x1f << 21;
15420 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
15421 insn2 = 0x38630000; /* addi 3,3,0 */
15422 if (tls_gd == 0)
951fd09b 15423 {
102890f0 15424 /* Was an LD reloc. */
71c4e95a 15425 r_symndx = STN_UNDEF;
102890f0 15426 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 15427 }
102890f0 15428 else if (toc_symndx != 0)
3a71aa26
AM
15429 {
15430 r_symndx = toc_symndx;
15431 rel->r_addend = toc_addend;
15432 }
102890f0
AM
15433 r_type = R_PPC64_TPREL16_HA;
15434 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
15435 if (offset != (bfd_vma) -1)
15436 {
15437 rel[1].r_info = ELF64_R_INFO (r_symndx,
15438 R_PPC64_TPREL16_LO);
15439 rel[1].r_offset = offset + d_offset;
15440 rel[1].r_addend = rel->r_addend;
15441 }
102890f0 15442 }
95f0d0d2 15443 bfd_put_32 (input_bfd, insn1,
3a71aa26 15444 contents + rel->r_offset - d_offset);
727fc41e 15445 if (offset != (bfd_vma) -1)
c96e0573
AM
15446 {
15447 bfd_put_32 (input_bfd, insn2, contents + offset);
15448 if (offset + 8 <= input_section->size)
15449 {
15450 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15451 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15452 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15453 }
15454 }
727fc41e
AM
15455 if ((tls_mask & tls_gd) == 0
15456 && (tls_gd == 0 || toc_symndx != 0))
15457 {
15458 /* We changed the symbol. Start over in order
15459 to get h, sym, sec etc. right. */
c316a17c 15460 goto again;
727fc41e
AM
15461 }
15462 }
15463 break;
15464
87c69f97 15465 case R_PPC64_GOT_TLSGD_PCREL34:
c213164a
AM
15466 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
15467 {
15468 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15469 pinsn <<= 32;
15470 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15471 if ((tls_mask & TLS_GDIE) != 0)
15472 {
15473 /* IE, pla -> pld */
15474 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
87c69f97 15475 r_type = R_PPC64_GOT_TPREL_PCREL34;
c213164a
AM
15476 }
15477 else
15478 {
15479 /* LE, pla pcrel -> paddi r13 */
15480 pinsn += (-1ULL << 52) + (13ULL << 16);
15481 r_type = R_PPC64_TPREL34;
15482 }
15483 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15484 bfd_put_32 (input_bfd, pinsn >> 32,
15485 contents + rel->r_offset);
15486 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15487 contents + rel->r_offset + 4);
15488 }
15489 break;
15490
87c69f97 15491 case R_PPC64_GOT_TLSLD_PCREL34:
c213164a
AM
15492 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
15493 {
15494 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15495 pinsn <<= 32;
15496 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15497 pinsn += (-1ULL << 52) + (13ULL << 16);
15498 bfd_put_32 (input_bfd, pinsn >> 32,
15499 contents + rel->r_offset);
15500 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15501 contents + rel->r_offset + 4);
15502 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15503 r_symndx = STN_UNDEF;
15504 r_type = R_PPC64_TPREL34;
15505 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15506 goto again;
15507 }
15508 break;
15509
727fc41e 15510 case R_PPC64_TLSGD:
37da22e5 15511 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 15512 && rel + 1 < relend)
727fc41e 15513 {
b9f04fe0 15514 unsigned int insn2;
5663e321 15515 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 15516
4a421c53 15517 offset = rel->r_offset;
5663e321 15518 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
15519 {
15520 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
15521 if (r_type1 == R_PPC64_PLT_PCREL34
15522 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15523 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
15524 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15525 break;
15526 }
15527
15528 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15529 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15530
b00a0a86 15531 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
15532 {
15533 /* IE */
15534 r_type = R_PPC64_NONE;
15535 insn2 = 0x7c636a14; /* add 3,3,13 */
15536 }
15537 else
15538 {
15539 /* LE */
15540 if (toc_symndx != 0)
15541 {
15542 r_symndx = toc_symndx;
15543 rel->r_addend = toc_addend;
15544 }
c213164a
AM
15545 if (r_type1 == R_PPC64_REL24_NOTOC
15546 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15547 {
15548 r_type = R_PPC64_NONE;
15549 insn2 = NOP;
15550 }
15551 else
15552 {
15553 rel->r_offset = offset + d_offset;
15554 r_type = R_PPC64_TPREL16_LO;
15555 insn2 = 0x38630000; /* addi 3,3,0 */
15556 }
727fc41e
AM
15557 }
15558 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15559 /* Zap the reloc on the _tls_get_addr call too. */
15560 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 15561 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 15562 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
15563 if ((tls_mask & TLS_GDIE) == 0
15564 && toc_symndx != 0
15565 && r_type != R_PPC64_NONE)
c316a17c 15566 goto again;
411e1bfb 15567 }
411e1bfb
AM
15568 break;
15569
727fc41e 15570 case R_PPC64_TLSLD:
37da22e5 15571 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 15572 && rel + 1 < relend)
727fc41e 15573 {
b9f04fe0 15574 unsigned int insn2;
5663e321 15575 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 15576
4a421c53 15577 offset = rel->r_offset;
5663e321 15578 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
15579 {
15580 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
15581 if (r_type1 == R_PPC64_PLT_PCREL34
15582 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15583 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
15584 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15585 break;
15586 }
15587
15588 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15589 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15590
c213164a
AM
15591 if (r_type1 == R_PPC64_REL24_NOTOC
15592 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15593 {
15594 r_type = R_PPC64_NONE;
15595 insn2 = NOP;
15596 }
15597 else
15598 {
15599 rel->r_offset = offset + d_offset;
15600 r_symndx = STN_UNDEF;
15601 r_type = R_PPC64_TPREL16_LO;
15602 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15603 insn2 = 0x38630000; /* addi 3,3,0 */
15604 }
727fc41e 15605 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
15606 /* Zap the reloc on the _tls_get_addr call too. */
15607 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 15608 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 15609 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
15610 if (r_type != R_PPC64_NONE)
15611 goto again;
727fc41e
AM
15612 }
15613 break;
15614
411e1bfb 15615 case R_PPC64_DTPMOD64:
951fd09b
AM
15616 if (rel + 1 < relend
15617 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
15618 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 15619 {
951fd09b
AM
15620 if ((tls_mask & TLS_GD) == 0)
15621 {
15622 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
b00a0a86 15623 if ((tls_mask & TLS_GDIE) != 0)
951fd09b
AM
15624 r_type = R_PPC64_TPREL64;
15625 else
15626 {
4ce794b7 15627 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
15628 r_type = R_PPC64_NONE;
15629 }
15630 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15631 }
15632 }
15633 else
15634 {
15635 if ((tls_mask & TLS_LD) == 0)
411e1bfb 15636 {
4ce794b7 15637 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 15638 r_type = R_PPC64_NONE;
951fd09b 15639 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 15640 }
411e1bfb
AM
15641 }
15642 break;
15643
15644 case R_PPC64_TPREL64:
951fd09b 15645 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
15646 {
15647 r_type = R_PPC64_NONE;
15648 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15649 }
15650 break;
52a82034 15651
006589cf
AM
15652 case R_PPC64_ENTRY:
15653 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15654 if (!bfd_link_pic (info)
15655 && !info->traditional_format
15656 && relocation + 0x80008000 <= 0xffffffff)
15657 {
15658 unsigned int insn1, insn2;
15659
15660 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15661 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15662 if ((insn1 & ~0xfffc) == LD_R2_0R12
15663 && insn2 == ADD_R2_R2_R12)
15664 {
95f0d0d2 15665 bfd_put_32 (input_bfd,
006589cf
AM
15666 LIS_R2 + PPC_HA (relocation),
15667 contents + rel->r_offset);
95f0d0d2 15668 bfd_put_32 (input_bfd,
006589cf
AM
15669 ADDI_R2_R2 + PPC_LO (relocation),
15670 contents + rel->r_offset + 4);
15671 }
15672 }
15673 else
15674 {
15675 relocation -= (rel->r_offset
15676 + input_section->output_offset
15677 + input_section->output_section->vma);
15678 if (relocation + 0x80008000 <= 0xffffffff)
15679 {
15680 unsigned int insn1, insn2;
15681
15682 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15683 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15684 if ((insn1 & ~0xfffc) == LD_R2_0R12
15685 && insn2 == ADD_R2_R2_R12)
15686 {
95f0d0d2 15687 bfd_put_32 (input_bfd,
006589cf
AM
15688 ADDIS_R2_R12 + PPC_HA (relocation),
15689 contents + rel->r_offset);
95f0d0d2 15690 bfd_put_32 (input_bfd,
006589cf
AM
15691 ADDI_R2_R2 + PPC_LO (relocation),
15692 contents + rel->r_offset + 4);
15693 }
15694 }
15695 }
15696 break;
15697
52a82034
AM
15698 case R_PPC64_REL16_HA:
15699 /* If we are generating a non-PIC executable, edit
15700 . 0: addis 2,12,.TOC.-0b@ha
15701 . addi 2,2,.TOC.-0b@l
15702 used by ELFv2 global entry points to set up r2, to
15703 . lis 2,.TOC.@ha
15704 . addi 2,2,.TOC.@l
15705 if .TOC. is in range. */
0e1862bb 15706 if (!bfd_link_pic (info)
810d4e75 15707 && !info->traditional_format
006589cf 15708 && !htab->opd_abi
4f038ee5 15709 && rel->r_addend == d_offset
52a82034
AM
15710 && h != NULL && &h->elf == htab->elf.hgot
15711 && rel + 1 < relend
15712 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15713 && rel[1].r_offset == rel->r_offset + 4
15714 && rel[1].r_addend == rel->r_addend + 4
15715 && relocation + 0x80008000 <= 0xffffffff)
15716 {
15717 unsigned int insn1, insn2;
4a421c53 15718 offset = rel->r_offset - d_offset;
95f0d0d2
AM
15719 insn1 = bfd_get_32 (input_bfd, contents + offset);
15720 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
15721 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15722 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
15723 {
15724 r_type = R_PPC64_ADDR16_HA;
15725 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15726 rel->r_addend -= d_offset;
15727 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15728 rel[1].r_addend -= d_offset + 4;
95f0d0d2 15729 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
15730 }
15731 }
15732 break;
411e1bfb
AM
15733 }
15734
15735 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 15736 insn = 0;
b25116a9
AM
15737 max_br_offset = 1 << 25;
15738 addend = rel->r_addend;
bc30df16 15739 reloc_dest = DEST_NORMAL;
65f38f15 15740 switch (r_type)
5bd4f169
AM
15741 {
15742 default:
65f38f15 15743 break;
5bd4f169 15744
3b421ab3
AM
15745 case R_PPC64_TOCSAVE:
15746 if (relocation + addend == (rel->r_offset
15747 + input_section->output_offset
15748 + input_section->output_section->vma)
15749 && tocsave_find (htab, NO_INSERT,
15750 &local_syms, rel, input_bfd))
15751 {
15752 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15753 if (insn == NOP
15754 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
15755 bfd_put_32 (input_bfd,
15756 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
15757 contents + rel->r_offset);
15758 }
15759 break;
15760
65f38f15
AM
15761 /* Branch taken prediction relocations. */
15762 case R_PPC64_ADDR14_BRTAKEN:
15763 case R_PPC64_REL14_BRTAKEN:
cedb70c5 15764 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 15765 /* Fall through. */
65f38f15 15766
86c76c7b 15767 /* Branch not taken prediction relocations. */
65f38f15
AM
15768 case R_PPC64_ADDR14_BRNTAKEN:
15769 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 15770 insn |= bfd_get_32 (input_bfd,
411e1bfb 15771 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 15772 /* Fall through. */
86c76c7b 15773
b25116a9
AM
15774 case R_PPC64_REL14:
15775 max_br_offset = 1 << 15;
1a0670f3 15776 /* Fall through. */
5bd4f169 15777
65f38f15 15778 case R_PPC64_REL24:
05d0e962 15779 case R_PPC64_REL24_NOTOC:
23cedd1d 15780 case R_PPC64_PLTCALL:
5663e321 15781 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
15782 /* Calls to functions with a different TOC, such as calls to
15783 shared objects, need to alter the TOC pointer. This is
15784 done using a linkage stub. A REL24 branching to these
15785 linkage stubs needs to be followed by a nop, as the nop
15786 will be replaced with an instruction to restore the TOC
15787 base pointer. */
8387904d 15788 fdh = h;
b31867b6
AM
15789 if (h != NULL
15790 && h->oh != NULL
15791 && h->oh->is_func_descriptor)
15792 fdh = ppc_follow_link (h->oh);
31c76678
DK
15793 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15794 htab);
5663e321
AM
15795 if ((r_type == R_PPC64_PLTCALL
15796 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 15797 && stub_entry != NULL
05d0e962
AM
15798 && stub_entry->stub_type >= ppc_stub_plt_call
15799 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
15800 stub_entry = NULL;
15801
6abec6bc 15802 if (stub_entry != NULL
d0abeec8
AM
15803 && ((stub_entry->stub_type >= ppc_stub_plt_call
15804 && stub_entry->stub_type <= ppc_stub_plt_call_both)
ad8e1ba5 15805 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
15806 || stub_entry->stub_type == ppc_stub_plt_branch_both
15807 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15808 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 15809 {
b25116a9 15810 bfd_boolean can_plt_call = FALSE;
721956f4 15811
6e1816be
AM
15812 if (stub_entry->stub_type == ppc_stub_plt_call
15813 && !htab->opd_abi
15814 && htab->params->plt_localentry0 != 0
9e51d549 15815 && h != NULL
6e1816be
AM
15816 && is_elfv2_localentry0 (&h->elf))
15817 {
15818 /* The function doesn't use or change r2. */
15819 can_plt_call = TRUE;
15820 }
05d0e962
AM
15821 else if (r_type == R_PPC64_REL24_NOTOC)
15822 {
15823 /* NOTOC calls don't need to restore r2. */
15824 can_plt_call = TRUE;
15825 }
6e1816be 15826
f378ab09 15827 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
15828 branch and link followed by a nop. The nop is
15829 replaced by an insn to restore r2. */
6e1816be 15830 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 15831 {
ba8ca3e7
AM
15832 unsigned long br;
15833
15834 br = bfd_get_32 (input_bfd,
15835 contents + rel->r_offset);
15836 if ((br & 1) != 0)
41bd81ab 15837 {
ba8ca3e7
AM
15838 unsigned long nop;
15839
15840 nop = bfd_get_32 (input_bfd,
15841 contents + rel->r_offset + 4);
23cedd1d
AM
15842 if (nop == LD_R2_0R1 + STK_TOC (htab))
15843 can_plt_call = TRUE;
15844 else if (nop == NOP
15845 || nop == CROR_151515
15846 || nop == CROR_313131)
a7f2871e 15847 {
ba8ca3e7 15848 if (h != NULL
ed7007c1 15849 && is_tls_get_addr (&h->elf, htab)
7c9cf415 15850 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
15851 {
15852 /* Special stub used, leave nop alone. */
15853 }
15854 else
a078d95a
AM
15855 bfd_put_32 (input_bfd,
15856 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
15857 contents + rel->r_offset + 4);
15858 can_plt_call = TRUE;
a7f2871e 15859 }
41bd81ab 15860 }
5bd4f169 15861 }
721956f4 15862
ba8ca3e7 15863 if (!can_plt_call && h != NULL)
721956f4 15864 {
ba8ca3e7
AM
15865 const char *name = h->elf.root.root.string;
15866
15867 if (*name == '.')
15868 ++name;
15869
15870 if (strncmp (name, "__libc_start_main", 17) == 0
15871 && (name[17] == 0 || name[17] == '@'))
6ab189d5 15872 {
ba8ca3e7
AM
15873 /* Allow crt1 branch to go via a toc adjusting
15874 stub. Other calls that never return could do
15875 the same, if we could detect such. */
b25116a9 15876 can_plt_call = TRUE;
6ab189d5 15877 }
ba8ca3e7
AM
15878 }
15879
15880 if (!can_plt_call)
15881 {
15882 /* g++ as of 20130507 emits self-calls without a
15883 following nop. This is arguably wrong since we
15884 have conflicting information. On the one hand a
15885 global symbol and on the other a local call
15886 sequence, but don't error for this special case.
15887 It isn't possible to cheaply verify we have
15888 exactly such a call. Allow all calls to the same
15889 section. */
15890 asection *code_sec = sec;
15891
15892 if (get_opd_info (sec) != NULL)
ad8e1ba5 15893 {
ba8ca3e7
AM
15894 bfd_vma off = (relocation + addend
15895 - sec->output_section->vma
15896 - sec->output_offset);
bc30df16 15897
ba8ca3e7 15898 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 15899 }
ba8ca3e7
AM
15900 if (code_sec == input_section)
15901 can_plt_call = TRUE;
15902 }
15903
15904 if (!can_plt_call)
15905 {
05d0e962
AM
15906 if (stub_entry->stub_type >= ppc_stub_plt_call
15907 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 15908 info->callbacks->einfo
695344c0 15909 /* xgettext:c-format */
c1c8c1ef 15910 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15911 "(plt call stub)\n"),
4805fc55
AM
15912 input_bfd, input_section, rel->r_offset, sym_name);
15913 else
15914 info->callbacks->einfo
695344c0 15915 /* xgettext:c-format */
c1c8c1ef 15916 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15917 "(toc save/adjust stub)\n"),
4805fc55 15918 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
15919
15920 bfd_set_error (bfd_error_bad_value);
15921 ret = FALSE;
721956f4
AM
15922 }
15923
b25116a9 15924 if (can_plt_call
05d0e962
AM
15925 && stub_entry->stub_type >= ppc_stub_plt_call
15926 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
15927 unresolved_reloc = FALSE;
15928 }
15929
6abec6bc
AM
15930 if ((stub_entry == NULL
15931 || stub_entry->stub_type == ppc_stub_long_branch
15932 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
15933 && get_opd_info (sec) != NULL)
15934 {
15935 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
15936 bfd_vma off = (relocation + addend
15937 - sec->output_section->vma
15938 - sec->output_offset);
aef36ac1 15939 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
15940 if (dest != (bfd_vma) -1)
15941 {
15942 relocation = dest;
15943 addend = 0;
bc30df16 15944 reloc_dest = DEST_OPD;
8387904d
AM
15945 }
15946 }
15947
b25116a9
AM
15948 /* If the branch is out of reach we ought to have a long
15949 branch stub. */
15950 from = (rel->r_offset
15951 + input_section->output_offset
15952 + input_section->output_section->vma);
15953
6911b7dc
AM
15954 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15955 ? fdh->elf.other
15956 : sym->st_other);
15957
6abec6bc
AM
15958 if (stub_entry != NULL
15959 && (stub_entry->stub_type == ppc_stub_long_branch
15960 || stub_entry->stub_type == ppc_stub_plt_branch)
15961 && (r_type == R_PPC64_ADDR14_BRTAKEN
15962 || r_type == R_PPC64_ADDR14_BRNTAKEN
15963 || (relocation + addend - from + max_br_offset
15964 < 2 * max_br_offset)))
15965 /* Don't use the stub if this branch is in range. */
15966 stub_entry = NULL;
b25116a9 15967
05d0e962
AM
15968 if (stub_entry != NULL
15969 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15970 || stub_entry->stub_type == ppc_stub_long_branch_both
15971 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15972 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15973 && (r_type != R_PPC64_REL24_NOTOC
15974 || ((fdh ? fdh->elf.other : sym->st_other)
4a4e7361 15975 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
05d0e962
AM
15976 && (relocation + addend - from + max_br_offset
15977 < 2 * max_br_offset))
15978 stub_entry = NULL;
15979
15980 if (stub_entry != NULL
15981 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15982 || stub_entry->stub_type == ppc_stub_long_branch_both
15983 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15984 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15985 && r_type == R_PPC64_REL24_NOTOC
15986 && (relocation + addend - from + max_br_offset
15987 < 2 * max_br_offset))
15988 stub_entry = NULL;
15989
b25116a9
AM
15990 if (stub_entry != NULL)
15991 {
15992 /* Munge up the value and addend so that we call the stub
15993 rather than the procedure directly. */
a4b6fadd
AM
15994 asection *stub_sec = stub_entry->group->stub_sec;
15995
15996 if (stub_entry->stub_type == ppc_stub_save_res)
15997 relocation += (stub_sec->output_offset
15998 + stub_sec->output_section->vma
15999 + stub_sec->size - htab->sfpr->size
16000 - htab->sfpr->output_offset
16001 - htab->sfpr->output_section->vma);
16002 else
16003 relocation = (stub_entry->stub_offset
16004 + stub_sec->output_offset
16005 + stub_sec->output_section->vma);
b25116a9 16006 addend = 0;
bc30df16 16007 reloc_dest = DEST_STUB;
3b421ab3 16008
29433886
AM
16009 if ((((stub_entry->stub_type == ppc_stub_plt_call
16010 && ALWAYS_EMIT_R2SAVE)
16011 || stub_entry->stub_type == ppc_stub_plt_call_r2save
16012 || stub_entry->stub_type == ppc_stub_plt_call_both)
16013 && rel + 1 < relend
16014 && rel[1].r_offset == rel->r_offset + 4
16015 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
16016 || ((stub_entry->stub_type == ppc_stub_long_branch_both
16017 || stub_entry->stub_type == ppc_stub_plt_branch_both
16018 || stub_entry->stub_type == ppc_stub_plt_call_both)
16019 && r_type == R_PPC64_REL24_NOTOC))
16020 {
16021 /* Skip over the r2 store at the start of the stub. */
16022 if (!(stub_entry->stub_type >= ppc_stub_plt_call
16023 && htab->params->tls_get_addr_opt
16024 && h != NULL
16025 && is_tls_get_addr (&h->elf, htab)))
16026 relocation += 4;
16027 }
5663e321
AM
16028
16029 if (r_type == R_PPC64_REL24_NOTOC
16030 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
16031 || stub_entry->stub_type == ppc_stub_plt_call_both))
16032 htab->notoc_plt = 1;
b25116a9
AM
16033 }
16034
16035 if (insn != 0)
16036 {
794e51c0 16037 if (is_isa_v2)
721956f4 16038 {
b25116a9
AM
16039 /* Set 'a' bit. This is 0b00010 in BO field for branch
16040 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16041 for branch on CTR insns (BO == 1a00t or 1a01t). */
16042 if ((insn & (0x14 << 21)) == (0x04 << 21))
16043 insn |= 0x02 << 21;
16044 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16045 insn |= 0x08 << 21;
16046 else
16047 break;
16048 }
16049 else
16050 {
16051 /* Invert 'y' bit if not the default. */
4cc603a5 16052 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 16053 insn ^= 0x01 << 21;
721956f4 16054 }
b25116a9 16055
95f0d0d2 16056 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 16057 }
e86ce104 16058
06da1e8e
AM
16059 /* NOP out calls to undefined weak functions.
16060 We can thus call a weak function without first
16061 checking whether the function is defined. */
b25116a9 16062 else if (h != NULL
039b3fef 16063 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 16064 && h->elf.dynindx == -1
05d0e962
AM
16065 && (r_type == R_PPC64_REL24
16066 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 16067 && relocation == 0
4cc603a5 16068 && addend == 0)
e86ce104 16069 {
95f0d0d2 16070 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 16071 goto copy_reloc;
e86ce104 16072 }
65f38f15 16073 break;
066f4018
AM
16074
16075 case R_PPC64_GOT16_DS:
6738c8a7
AM
16076 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16077 || !htab->do_toc_opt)
06507dab 16078 break;
066f4018
AM
16079 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16080 if (relocation + addend - from + 0x8000 < 0x10000
9e51d549 16081 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
066f4018
AM
16082 {
16083 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 16084 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16085 {
16086 insn += (14u << 26) - (58u << 26);
16087 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16088 r_type = R_PPC64_TOC16;
16089 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16090 }
16091 }
16092 break;
16093
16094 case R_PPC64_GOT16_LO_DS:
16095 case R_PPC64_GOT16_HA:
6738c8a7
AM
16096 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16097 || !htab->do_toc_opt)
06507dab 16098 break;
066f4018
AM
16099 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16100 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
9e51d549 16101 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
066f4018
AM
16102 {
16103 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
fba8689a
AM
16104 if (r_type == R_PPC64_GOT16_LO_DS
16105 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16106 {
16107 insn += (14u << 26) - (58u << 26);
16108 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16109 r_type = R_PPC64_TOC16_LO;
16110 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16111 }
fba8689a
AM
16112 else if (r_type == R_PPC64_GOT16_HA
16113 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
066f4018
AM
16114 {
16115 r_type = R_PPC64_TOC16_HA;
16116 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16117 }
16118 }
16119 break;
4a421c53
AM
16120
16121 case R_PPC64_GOT_PCREL34:
6738c8a7
AM
16122 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16123 || !htab->do_toc_opt)
06507dab 16124 break;
4a421c53
AM
16125 from = (rel->r_offset
16126 + input_section->output_section->vma
16127 + input_section->output_offset);
6738c8a7 16128 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
9e51d549 16129 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
6738c8a7
AM
16130 break;
16131
16132 offset = rel->r_offset;
16133 pinsn = bfd_get_32 (input_bfd, contents + offset);
16134 pinsn <<= 32;
16135 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16136 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16137 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16138 break;
16139
16140 /* Replace with paddi. */
16141 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16142 r_type = R_PPC64_PCREL34;
16143 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16144 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16145 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16146 /* Fall through. */
4a421c53
AM
16147
16148 case R_PPC64_PCREL34:
6738c8a7
AM
16149 if (!htab->params->no_pcrel_opt
16150 && rel + 1 < relend
16151 && rel[1].r_offset == rel->r_offset
16152 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
9e51d549 16153 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
4a421c53
AM
16154 {
16155 offset = rel->r_offset;
16156 pinsn = bfd_get_32 (input_bfd, contents + offset);
16157 pinsn <<= 32;
16158 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16159 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16160 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16161 | (14ULL << 26) /* paddi */))
16162 {
6738c8a7
AM
16163 bfd_vma off2 = rel[1].r_addend;
16164 if (off2 == 0)
16165 /* zero means next insn. */
16166 off2 = 8;
16167 off2 += offset;
16168 if (off2 + 4 <= input_section->size)
4a421c53 16169 {
6738c8a7
AM
16170 uint64_t pinsn2;
16171 bfd_signed_vma addend_off;
16172 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16173 pinsn2 <<= 32;
16174 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
4a421c53 16175 {
6738c8a7
AM
16176 if (off2 + 8 > input_section->size)
16177 break;
16178 pinsn2 |= bfd_get_32 (input_bfd,
16179 contents + off2 + 4);
16180 }
16181 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16182 {
16183 addend += addend_off;
16184 rel->r_addend = addend;
16185 bfd_put_32 (input_bfd, pinsn >> 32,
16186 contents + offset);
16187 bfd_put_32 (input_bfd, pinsn,
16188 contents + offset + 4);
16189 bfd_put_32 (input_bfd, pinsn2 >> 32,
16190 contents + off2);
4a421c53 16191 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
6738c8a7
AM
16192 bfd_put_32 (input_bfd, pinsn2,
16193 contents + off2 + 4);
4a421c53
AM
16194 }
16195 }
16196 }
16197 }
16198 break;
65f38f15 16199 }
5bd4f169 16200
411e1bfb 16201 tls_type = 0;
23cedd1d 16202 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
16203 switch (r_type)
16204 {
16205 default:
cf97bcb0
AM
16206 /* xgettext:c-format */
16207 _bfd_error_handler (_("%pB: %s unsupported"),
16208 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 16209
65f38f15 16210 bfd_set_error (bfd_error_bad_value);
b34976b6 16211 ret = FALSE;
c316a17c 16212 goto copy_reloc;
5bd4f169 16213
65f38f15 16214 case R_PPC64_NONE:
411e1bfb 16215 case R_PPC64_TLS:
727fc41e
AM
16216 case R_PPC64_TLSGD:
16217 case R_PPC64_TLSLD:
3b421ab3 16218 case R_PPC64_TOCSAVE:
04c9666a
AM
16219 case R_PPC64_GNU_VTINHERIT:
16220 case R_PPC64_GNU_VTENTRY:
006589cf 16221 case R_PPC64_ENTRY:
4a421c53 16222 case R_PPC64_PCREL_OPT:
c316a17c 16223 goto copy_reloc;
5bd4f169
AM
16224
16225 /* GOT16 relocations. Like an ADDR16 using the symbol's
16226 address in the GOT as relocation value instead of the
411e1bfb 16227 symbol's value itself. Also, create a GOT entry for the
5bd4f169 16228 symbol and put the symbol value there. */
411e1bfb
AM
16229 case R_PPC64_GOT_TLSGD16:
16230 case R_PPC64_GOT_TLSGD16_LO:
16231 case R_PPC64_GOT_TLSGD16_HI:
16232 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 16233 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 16234 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
16235 goto dogot;
16236
16237 case R_PPC64_GOT_TLSLD16:
16238 case R_PPC64_GOT_TLSLD16_LO:
16239 case R_PPC64_GOT_TLSLD16_HI:
16240 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 16241 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 16242 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
16243 goto dogot;
16244
16245 case R_PPC64_GOT_TPREL16_DS:
16246 case R_PPC64_GOT_TPREL16_LO_DS:
16247 case R_PPC64_GOT_TPREL16_HI:
16248 case R_PPC64_GOT_TPREL16_HA:
87c69f97 16249 case R_PPC64_GOT_TPREL_PCREL34:
411e1bfb
AM
16250 tls_type = TLS_TLS | TLS_TPREL;
16251 goto dogot;
16252
16253 case R_PPC64_GOT_DTPREL16_DS:
16254 case R_PPC64_GOT_DTPREL16_LO_DS:
16255 case R_PPC64_GOT_DTPREL16_HI:
16256 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 16257 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
16258 tls_type = TLS_TLS | TLS_DTPREL;
16259 goto dogot;
16260
65f38f15
AM
16261 case R_PPC64_GOT16:
16262 case R_PPC64_GOT16_LO:
16263 case R_PPC64_GOT16_HI:
16264 case R_PPC64_GOT16_HA:
16265 case R_PPC64_GOT16_DS:
16266 case R_PPC64_GOT16_LO_DS:
5663e321 16267 case R_PPC64_GOT_PCREL34:
411e1bfb 16268 dogot:
5bd4f169
AM
16269 {
16270 /* Relocation is to the entry for this symbol in the global
16271 offset table. */
e717da7e 16272 asection *got;
d881513a 16273 bfd_vma *offp;
5bd4f169 16274 bfd_vma off;
d881513a 16275 unsigned long indx = 0;
927be08e 16276 struct got_entry *ent;
65f38f15 16277
d881513a 16278 if (tls_type == (TLS_TLS | TLS_LD)
9e51d549 16279 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
927be08e 16280 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 16281 else
5bd4f169 16282 {
d881513a
AM
16283 if (h != NULL)
16284 {
f0158f44
AM
16285 if (!htab->elf.dynamic_sections_created
16286 || h->elf.dynindx == -1
16287 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 16288 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
16289 /* This is actually a static link, or it is a
16290 -Bsymbolic link and the symbol is defined
16291 locally, or the symbol was forced to be local
16292 because of a version file. */
16293 ;
16294 else
16295 {
039b3fef 16296 indx = h->elf.dynindx;
d881513a
AM
16297 unresolved_reloc = FALSE;
16298 }
039b3fef 16299 ent = h->elf.got.glist;
d881513a 16300 }
411e1bfb 16301 else
5bd4f169 16302 {
d881513a
AM
16303 if (local_got_ents == NULL)
16304 abort ();
16305 ent = local_got_ents[r_symndx];
5bd4f169 16306 }
d881513a
AM
16307
16308 for (; ent != NULL; ent = ent->next)
133a1f60 16309 if (ent->addend == orig_rel.r_addend
e717da7e 16310 && ent->owner == input_bfd
d881513a
AM
16311 && ent->tls_type == tls_type)
16312 break;
5bd4f169 16313 }
411e1bfb 16314
927be08e
AM
16315 if (ent == NULL)
16316 abort ();
16317 if (ent->is_indirect)
16318 ent = ent->got.ent;
16319 offp = &ent->got.offset;
16320 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
16321 if (got == NULL)
16322 abort ();
16323
411e1bfb
AM
16324 /* The offset must always be a multiple of 8. We use the
16325 least significant bit to record whether we have already
16326 processed this entry. */
d881513a 16327 off = *offp;
411e1bfb
AM
16328 if ((off & 1) != 0)
16329 off &= ~1;
5bd4f169
AM
16330 else
16331 {
411e1bfb
AM
16332 /* Generate relocs for the dynamic linker, except in
16333 the case of TLSLD where we'll use one entry per
16334 module. */
25f23106
AM
16335 asection *relgot;
16336 bfd_boolean ifunc;
e717da7e 16337
d881513a 16338 *offp = off | 1;
25f23106
AM
16339 relgot = NULL;
16340 ifunc = (h != NULL
16341 ? h->elf.type == STT_GNU_IFUNC
16342 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 16343 if (ifunc)
82e66161
AM
16344 {
16345 relgot = htab->elf.irelplt;
efb2a7b4 16346 if (indx == 0 || is_static_defined (&h->elf))
cebd6b8a 16347 htab->elf.ifunc_resolvers = TRUE;
82e66161 16348 }
f0158f44
AM
16349 else if (indx != 0
16350 || (bfd_link_pic (info)
16351 && (h == NULL
f749f26e
AM
16352 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16353 && !(tls_type != 0
f15d0b54 16354 && bfd_link_executable (info)
9e51d549
AM
16355 && (h == NULL
16356 || SYMBOL_REFERENCES_LOCAL (info,
16357 &h->elf)))))
19e08130 16358 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 16359 if (relgot != NULL)
5bd4f169 16360 {
e717da7e
AM
16361 outrel.r_offset = (got->output_section->vma
16362 + got->output_offset
411e1bfb 16363 + off);
133a1f60 16364 outrel.r_addend = orig_rel.r_addend;
d881513a 16365 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 16366 {
411e1bfb 16367 outrel.r_addend = 0;
e515b051 16368 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
16369 if (tls_type == (TLS_TLS | TLS_GD))
16370 {
e717da7e
AM
16371 loc = relgot->contents;
16372 loc += (relgot->reloc_count++
d881513a
AM
16373 * sizeof (Elf64_External_Rela));
16374 bfd_elf64_swap_reloca_out (output_bfd,
16375 &outrel, loc);
e515b051 16376 outrel.r_offset += 8;
133a1f60 16377 outrel.r_addend = orig_rel.r_addend;
d881513a
AM
16378 outrel.r_info
16379 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 16380 }
411e1bfb 16381 }
951fd09b 16382 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 16383 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 16384 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 16385 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
16386 else if (indx != 0)
16387 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16388 else
81407a69 16389 {
25f23106
AM
16390 if (ifunc)
16391 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16392 else
16393 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
16394
16395 /* Write the .got section contents for the sake
16396 of prelink. */
e717da7e 16397 loc = got->contents + off;
23fbd6fa
JJ
16398 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16399 loc);
81407a69 16400 }
81407a69
AM
16401
16402 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
16403 {
16404 outrel.r_addend += relocation;
16405 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
16406 {
16407 if (htab->elf.tls_sec == NULL)
16408 outrel.r_addend = 0;
16409 else
16410 outrel.r_addend -= htab->elf.tls_sec->vma;
16411 }
e515b051 16412 }
e717da7e
AM
16413 loc = relgot->contents;
16414 loc += (relgot->reloc_count++
411e1bfb
AM
16415 * sizeof (Elf64_External_Rela));
16416 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16417 }
16418
ad8e1ba5 16419 /* Init the .got section contents here if we're not
81407a69 16420 emitting a reloc. */
d881513a 16421 else
411e1bfb 16422 {
133a1f60 16423 relocation += orig_rel.r_addend;
f0158f44 16424 if (tls_type != 0)
411e1bfb 16425 {
989f9879
AM
16426 if (htab->elf.tls_sec == NULL)
16427 relocation = 0;
16428 else
16429 {
f0158f44
AM
16430 if (tls_type & TLS_LD)
16431 relocation = 0;
16432 else
16433 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 16434 if (tls_type & TLS_TPREL)
989f9879
AM
16435 relocation += DTP_OFFSET - TP_OFFSET;
16436 }
5bd4f169 16437
f0158f44 16438 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
16439 {
16440 bfd_put_64 (output_bfd, relocation,
e717da7e 16441 got->contents + off + 8);
676ee2b5 16442 relocation = 1;
7b609f53 16443 }
411e1bfb
AM
16444 }
16445 bfd_put_64 (output_bfd, relocation,
e717da7e 16446 got->contents + off);
5bd4f169
AM
16447 }
16448 }
16449
65f38f15
AM
16450 if (off >= (bfd_vma) -2)
16451 abort ();
16452
bf102f86 16453 relocation = got->output_section->vma + got->output_offset + off;
133a1f60 16454 addend = 0;
c213164a 16455 if (!(r_type == R_PPC64_GOT_PCREL34
87c69f97
AM
16456 || r_type == R_PPC64_GOT_TLSGD_PCREL34
16457 || r_type == R_PPC64_GOT_TLSLD_PCREL34
16458 || r_type == R_PPC64_GOT_TPREL_PCREL34
16459 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
5663e321 16460 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 16461 }
65f38f15
AM
16462 break;
16463
16464 case R_PPC64_PLT16_HA:
16465 case R_PPC64_PLT16_HI:
16466 case R_PPC64_PLT16_LO:
08be3224 16467 case R_PPC64_PLT16_LO_DS:
5663e321
AM
16468 case R_PPC64_PLT_PCREL34:
16469 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
16470 case R_PPC64_PLT32:
16471 case R_PPC64_PLT64:
23cedd1d 16472 case R_PPC64_PLTSEQ:
5663e321 16473 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 16474 case R_PPC64_PLTCALL:
5663e321 16475 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
16476 /* Relocation is to the entry for this symbol in the
16477 procedure linkage table. */
23cedd1d 16478 unresolved_reloc = TRUE;
cbf95972
AM
16479 {
16480 struct plt_entry **plt_list = NULL;
16481 if (h != NULL)
16482 plt_list = &h->elf.plt.plist;
16483 else if (local_got_ents != NULL)
16484 {
16485 struct plt_entry **local_plt = (struct plt_entry **)
16486 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 16487 plt_list = local_plt + r_symndx;
cbf95972
AM
16488 }
16489 if (plt_list)
16490 {
16491 struct plt_entry *ent;
65f38f15 16492
cbf95972
AM
16493 for (ent = *plt_list; ent != NULL; ent = ent->next)
16494 if (ent->plt.offset != (bfd_vma) -1
133a1f60 16495 && ent->addend == orig_rel.r_addend)
cbf95972
AM
16496 {
16497 asection *plt;
08be3224 16498 bfd_vma got;
cbf95972
AM
16499
16500 plt = htab->elf.splt;
30845f11 16501 if (use_local_plt (info, elf_hash_entry (h)))
2d7ad24e
AM
16502 {
16503 if (h != NULL
16504 ? h->elf.type == STT_GNU_IFUNC
16505 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16506 plt = htab->elf.iplt;
16507 else
16508 plt = htab->pltlocal;
16509 }
16510 relocation = (plt->output_section->vma
16511 + plt->output_offset
16512 + ent->plt.offset);
08be3224 16513 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
16514 || r_type == R_PPC64_PLT16_HI
16515 || r_type == R_PPC64_PLT16_LO
16516 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
16517 {
16518 got = (elf_gp (output_bfd)
16519 + htab->sec_info[input_section->id].toc_off);
16520 relocation -= got;
16521 }
133a1f60 16522 addend = 0;
cbf95972
AM
16523 unresolved_reloc = FALSE;
16524 break;
16525 }
16526 }
16527 }
65f38f15 16528 break;
5bd4f169 16529
0b13192e
AM
16530 case R_PPC64_TOC:
16531 /* Relocation value is TOC base. */
16532 relocation = TOCstart;
cf35638d 16533 if (r_symndx == STN_UNDEF)
6f20ed8a 16534 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
16535 else if (unresolved_reloc)
16536 ;
6f20ed8a
AM
16537 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
16538 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
16539 else
16540 unresolved_reloc = TRUE;
ab96bf03 16541 goto dodyn;
0b13192e 16542
5bd4f169
AM
16543 /* TOC16 relocs. We want the offset relative to the TOC base,
16544 which is the address of the start of the TOC plus 0x8000.
16545 The TOC consists of sections .got, .toc, .tocbss, and .plt,
16546 in this order. */
65f38f15
AM
16547 case R_PPC64_TOC16:
16548 case R_PPC64_TOC16_LO:
16549 case R_PPC64_TOC16_HI:
16550 case R_PPC64_TOC16_DS:
16551 case R_PPC64_TOC16_LO_DS:
16552 case R_PPC64_TOC16_HA:
6f20ed8a 16553 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
1bdd8fac
AM
16554 if (h != NULL)
16555 goto dodyn;
5bd4f169
AM
16556 break;
16557
16558 /* Relocate against the beginning of the section. */
65f38f15
AM
16559 case R_PPC64_SECTOFF:
16560 case R_PPC64_SECTOFF_LO:
16561 case R_PPC64_SECTOFF_HI:
16562 case R_PPC64_SECTOFF_DS:
16563 case R_PPC64_SECTOFF_LO_DS:
16564 case R_PPC64_SECTOFF_HA:
4ce794b7 16565 if (sec != NULL)
65f38f15 16566 addend -= sec->output_section->vma;
5bd4f169
AM
16567 break;
16568
25f23106
AM
16569 case R_PPC64_REL16:
16570 case R_PPC64_REL16_LO:
16571 case R_PPC64_REL16_HI:
16572 case R_PPC64_REL16_HA:
4a969973
AM
16573 case R_PPC64_REL16_HIGH:
16574 case R_PPC64_REL16_HIGHA:
16575 case R_PPC64_REL16_HIGHER:
16576 case R_PPC64_REL16_HIGHERA:
16577 case R_PPC64_REL16_HIGHEST:
16578 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
16579 case R_PPC64_REL16_HIGHER34:
16580 case R_PPC64_REL16_HIGHERA34:
16581 case R_PPC64_REL16_HIGHEST34:
16582 case R_PPC64_REL16_HIGHESTA34:
a680de9a 16583 case R_PPC64_REL16DX_HA:
721956f4
AM
16584 case R_PPC64_REL14:
16585 case R_PPC64_REL14_BRNTAKEN:
16586 case R_PPC64_REL14_BRTAKEN:
5d1634d7 16587 case R_PPC64_REL24:
05d0e962 16588 case R_PPC64_REL24_NOTOC:
5663e321
AM
16589 case R_PPC64_PCREL34:
16590 case R_PPC64_PCREL28:
5d1634d7
AM
16591 break;
16592
411e1bfb
AM
16593 case R_PPC64_TPREL16:
16594 case R_PPC64_TPREL16_LO:
16595 case R_PPC64_TPREL16_HI:
16596 case R_PPC64_TPREL16_HA:
16597 case R_PPC64_TPREL16_DS:
16598 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
16599 case R_PPC64_TPREL16_HIGH:
16600 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
16601 case R_PPC64_TPREL16_HIGHER:
16602 case R_PPC64_TPREL16_HIGHERA:
16603 case R_PPC64_TPREL16_HIGHEST:
16604 case R_PPC64_TPREL16_HIGHESTA:
c213164a 16605 case R_PPC64_TPREL34:
766bc656
AM
16606 if (h != NULL
16607 && h->elf.root.type == bfd_link_hash_undefweak
16608 && h->elf.dynindx == -1)
16609 {
16610 /* Make this relocation against an undefined weak symbol
16611 resolve to zero. This is really just a tweak, since
16612 code using weak externs ought to check that they are
16613 defined before using them. */
16614 bfd_byte *p = contents + rel->r_offset - d_offset;
16615
95f0d0d2 16616 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
16617 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
16618 if (insn != 0)
95f0d0d2 16619 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
16620 break;
16621 }
989f9879
AM
16622 if (htab->elf.tls_sec != NULL)
16623 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
16624 /* The TPREL16 relocs shouldn't really be used in shared
16625 libs or with non-local symbols as that will result in
16626 DT_TEXTREL being set, but support them anyway. */
16627 goto dodyn;
411e1bfb
AM
16628
16629 case R_PPC64_DTPREL16:
16630 case R_PPC64_DTPREL16_LO:
16631 case R_PPC64_DTPREL16_HI:
16632 case R_PPC64_DTPREL16_HA:
16633 case R_PPC64_DTPREL16_DS:
16634 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
16635 case R_PPC64_DTPREL16_HIGH:
16636 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
16637 case R_PPC64_DTPREL16_HIGHER:
16638 case R_PPC64_DTPREL16_HIGHERA:
16639 case R_PPC64_DTPREL16_HIGHEST:
16640 case R_PPC64_DTPREL16_HIGHESTA:
c213164a 16641 case R_PPC64_DTPREL34:
989f9879
AM
16642 if (htab->elf.tls_sec != NULL)
16643 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
16644 break;
16645
45965137
AM
16646 case R_PPC64_ADDR64_LOCAL:
16647 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
16648 ? h->elf.other
16649 : sym->st_other);
16650 break;
16651
e515b051
AM
16652 case R_PPC64_DTPMOD64:
16653 relocation = 1;
16654 addend = 0;
16655 goto dodyn;
16656
411e1bfb 16657 case R_PPC64_TPREL64:
989f9879
AM
16658 if (htab->elf.tls_sec != NULL)
16659 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
16660 goto dodyn;
16661
16662 case R_PPC64_DTPREL64:
989f9879
AM
16663 if (htab->elf.tls_sec != NULL)
16664 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 16665 /* Fall through. */
411e1bfb 16666
65f38f15
AM
16667 /* Relocations that may need to be propagated if this is a
16668 dynamic object. */
04c9666a 16669 case R_PPC64_REL30:
65f38f15
AM
16670 case R_PPC64_REL32:
16671 case R_PPC64_REL64:
16672 case R_PPC64_ADDR14:
16673 case R_PPC64_ADDR14_BRNTAKEN:
16674 case R_PPC64_ADDR14_BRTAKEN:
16675 case R_PPC64_ADDR16:
16676 case R_PPC64_ADDR16_DS:
16677 case R_PPC64_ADDR16_HA:
16678 case R_PPC64_ADDR16_HI:
f9c6b907
AM
16679 case R_PPC64_ADDR16_HIGH:
16680 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
16681 case R_PPC64_ADDR16_HIGHER:
16682 case R_PPC64_ADDR16_HIGHERA:
16683 case R_PPC64_ADDR16_HIGHEST:
16684 case R_PPC64_ADDR16_HIGHESTA:
16685 case R_PPC64_ADDR16_LO:
16686 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
16687 case R_PPC64_ADDR16_HIGHER34:
16688 case R_PPC64_ADDR16_HIGHERA34:
16689 case R_PPC64_ADDR16_HIGHEST34:
16690 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 16691 case R_PPC64_ADDR24:
65f38f15
AM
16692 case R_PPC64_ADDR32:
16693 case R_PPC64_ADDR64:
16694 case R_PPC64_UADDR16:
16695 case R_PPC64_UADDR32:
16696 case R_PPC64_UADDR64:
5663e321
AM
16697 case R_PPC64_D34:
16698 case R_PPC64_D34_LO:
16699 case R_PPC64_D34_HI30:
16700 case R_PPC64_D34_HA30:
16701 case R_PPC64_D28:
411e1bfb 16702 dodyn:
5d1634d7 16703 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
16704 break;
16705
41bd81ab
AM
16706 if (NO_OPD_RELOCS && is_opd)
16707 break;
16708
8a9e8e72 16709 if (bfd_link_pic (info)
b1b07054 16710 ? ((h == NULL
190eb1dd 16711 || h->elf.dyn_relocs != NULL)
b1b07054
AM
16712 && ((h != NULL && pc_dynrelocs (h))
16713 || must_be_dyn_reloc (info, r_type)))
8a9e8e72 16714 : (h != NULL
190eb1dd 16715 ? h->elf.dyn_relocs != NULL
d311bc8b 16716 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 16717 {
b34976b6 16718 bfd_boolean skip, relocate;
65f38f15 16719 asection *sreloc;
1cf1f670 16720 bfd_vma out_off;
82e66161 16721 long indx = 0;
65f38f15
AM
16722
16723 /* When generating a dynamic object, these relocations
16724 are copied into the output file to be resolved at run
16725 time. */
16726
b34976b6
AM
16727 skip = FALSE;
16728 relocate = FALSE;
65f38f15 16729
1cf1f670
AM
16730 out_off = _bfd_elf_section_offset (output_bfd, info,
16731 input_section, rel->r_offset);
16732 if (out_off == (bfd_vma) -1)
b34976b6 16733 skip = TRUE;
1cf1f670 16734 else if (out_off == (bfd_vma) -2)
b34976b6 16735 skip = TRUE, relocate = TRUE;
1cf1f670
AM
16736 out_off += (input_section->output_section->vma
16737 + input_section->output_offset);
16738 outrel.r_offset = out_off;
411e1bfb 16739 outrel.r_addend = rel->r_addend;
65f38f15 16740
1cf1f670
AM
16741 /* Optimize unaligned reloc use. */
16742 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
16743 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
16744 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
16745 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
16746 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
16747 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
16748 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16749 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16750 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16751
65f38f15 16752 if (skip)
0bb2d96a 16753 memset (&outrel, 0, sizeof outrel);
9e51d549
AM
16754 else if (h != NULL
16755 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
16756 && !is_opd
16757 && r_type != R_PPC64_TOC)
14acf4dc 16758 {
82e66161
AM
16759 indx = h->elf.dynindx;
16760 BFD_ASSERT (indx != -1);
16761 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 16762 }
65f38f15
AM
16763 else
16764 {
41bd81ab
AM
16765 /* This symbol is local, or marked to become local,
16766 or this is an opd section reloc which must point
16767 at a local function. */
65f38f15 16768 outrel.r_addend += relocation;
e86ce104 16769 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 16770 {
3fad3c7c 16771 if (is_opd && h != NULL)
afbe61cf
AM
16772 {
16773 /* Lie about opd entries. This case occurs
16774 when building shared libraries and we
16775 reference a function in another shared
3fad3c7c
AM
16776 lib. The same thing happens for a weak
16777 definition in an application that's
16778 overridden by a strong definition in a
16779 shared lib. (I believe this is a generic
16780 bug in binutils handling of weak syms.)
16781 In these cases we won't use the opd
1e2f5b6e 16782 entry in this lib. */
b34976b6 16783 unresolved_reloc = FALSE;
afbe61cf 16784 }
25f23106
AM
16785 if (!is_opd
16786 && r_type == R_PPC64_ADDR64
16787 && (h != NULL
16788 ? h->elf.type == STT_GNU_IFUNC
16789 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16790 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16791 else
16792 {
16793 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 16794
25f23106
AM
16795 /* We need to relocate .opd contents for ld.so.
16796 Prelink also wants simple and consistent rules
16797 for relocs. This make all RELATIVE relocs have
16798 *r_offset equal to r_addend. */
16799 relocate = TRUE;
16800 }
65f38f15
AM
16801 }
16802 else
16803 {
25f23106
AM
16804 if (h != NULL
16805 ? h->elf.type == STT_GNU_IFUNC
16806 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16807 {
25f53a85 16808 info->callbacks->einfo
695344c0 16809 /* xgettext:c-format */
174d0a74 16810 (_("%H: %s for indirect "
c1c8c1ef 16811 "function `%pT' unsupported\n"),
25f53a85 16812 input_bfd, input_section, rel->r_offset,
25f23106
AM
16813 ppc64_elf_howto_table[r_type]->name,
16814 sym_name);
16815 ret = FALSE;
16816 }
cf35638d 16817 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
16818 ;
16819 else if (sec == NULL || sec->owner == NULL)
16820 {
16821 bfd_set_error (bfd_error_bad_value);
b34976b6 16822 return FALSE;
65f38f15
AM
16823 }
16824 else
16825 {
f26a3287 16826 asection *osec = sec->output_section;
65f38f15 16827
f26a3287
AM
16828 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16829 {
16830 /* TLS symbol values are relative to the
16831 TLS segment. Dynamic relocations for
16832 local TLS symbols therefore can't be
16833 reduced to a relocation against their
16834 section symbol because it holds the
16835 address of the section, not a value
16836 relative to the TLS segment. We could
16837 change the .tdata dynamic section symbol
16838 to be zero value but STN_UNDEF works
16839 and is used elsewhere, eg. for TPREL64
16840 GOT relocs against local TLS symbols. */
16841 osec = htab->elf.tls_sec;
16842 indx = 0;
16843 }
16844 else
74541ad4 16845 {
74541ad4 16846 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
16847 if (indx == 0)
16848 {
16849 if ((osec->flags & SEC_READONLY) == 0
16850 && htab->elf.data_index_section != NULL)
16851 osec = htab->elf.data_index_section;
16852 else
16853 osec = htab->elf.text_index_section;
16854 indx = elf_section_data (osec)->dynindx;
16855 }
16856 BFD_ASSERT (indx != 0);
74541ad4 16857 }
74541ad4 16858
65f38f15
AM
16859 /* We are turning this relocation into one
16860 against a section symbol, so subtract out
16861 the output section's address but not the
16862 offset of the input section in the output
16863 section. */
16864 outrel.r_addend -= osec->vma;
16865 }
16866
16867 outrel.r_info = ELF64_R_INFO (indx, r_type);
16868 }
16869 }
16870
16871 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
16872 if (h != NULL
16873 ? h->elf.type == STT_GNU_IFUNC
16874 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
16875 {
16876 sreloc = htab->elf.irelplt;
efb2a7b4 16877 if (indx == 0 || is_static_defined (&h->elf))
cebd6b8a 16878 htab->elf.ifunc_resolvers = TRUE;
82e66161 16879 }
65f38f15
AM
16880 if (sreloc == NULL)
16881 abort ();
16882
dfbb6ac9
AM
16883 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16884 >= sreloc->size)
16885 abort ();
947216bf
AM
16886 loc = sreloc->contents;
16887 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
16888 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16889
e59a1001
AM
16890 if (!warned_dynamic
16891 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
16892 {
16893 info->callbacks->einfo
16894 /* xgettext:c-format */
16895 (_("%X%P: %pB: %s against %pT "
16896 "is not supported by glibc as a dynamic relocation\n"),
16897 input_bfd,
16898 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
16899 sym_name);
16900 warned_dynamic = TRUE;
16901 }
16902
65f38f15
AM
16903 /* If this reloc is against an external symbol, it will
16904 be computed at runtime, so there's no need to do
81407a69
AM
16905 anything now. However, for the sake of prelink ensure
16906 that the section contents are a known value. */
2cdcc330 16907 if (!relocate)
81407a69
AM
16908 {
16909 unresolved_reloc = FALSE;
16910 /* The value chosen here is quite arbitrary as ld.so
16911 ignores section contents except for the special
16912 case of .opd where the contents might be accessed
16913 before relocation. Choose zero, as that won't
16914 cause reloc overflow. */
16915 relocation = 0;
16916 addend = 0;
16917 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16918 to improve backward compatibility with older
16919 versions of ld. */
16920 if (r_type == R_PPC64_ADDR64)
16921 addend = outrel.r_addend;
16922 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 16923 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 16924 addend = outrel.r_offset;
81407a69 16925 }
65f38f15 16926 }
5bd4f169
AM
16927 break;
16928
65f38f15
AM
16929 case R_PPC64_COPY:
16930 case R_PPC64_GLOB_DAT:
16931 case R_PPC64_JMP_SLOT:
25f23106 16932 case R_PPC64_JMP_IREL:
65f38f15
AM
16933 case R_PPC64_RELATIVE:
16934 /* We shouldn't ever see these dynamic relocs in relocatable
16935 files. */
ae9a127f 16936 /* Fall through. */
65f38f15
AM
16937
16938 case R_PPC64_PLTGOT16:
16939 case R_PPC64_PLTGOT16_DS:
16940 case R_PPC64_PLTGOT16_HA:
16941 case R_PPC64_PLTGOT16_HI:
16942 case R_PPC64_PLTGOT16_LO:
16943 case R_PPC64_PLTGOT16_LO_DS:
16944 case R_PPC64_PLTREL32:
16945 case R_PPC64_PLTREL64:
16946 /* These ones haven't been implemented yet. */
16947
25f53a85 16948 info->callbacks->einfo
695344c0 16949 /* xgettext:c-format */
c1c8c1ef 16950 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 16951 input_bfd,
4ce794b7 16952 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
16953
16954 bfd_set_error (bfd_error_invalid_operation);
b34976b6 16955 ret = FALSE;
c316a17c 16956 goto copy_reloc;
65f38f15 16957 }
5bd4f169 16958
67f0cbdb
AM
16959 /* Multi-instruction sequences that access the TOC can be
16960 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 16961 to nop; addi rb,r2,x; */
67f0cbdb
AM
16962 switch (r_type)
16963 {
16964 default:
16965 break;
16966
16967 case R_PPC64_GOT_TLSLD16_HI:
16968 case R_PPC64_GOT_TLSGD16_HI:
16969 case R_PPC64_GOT_TPREL16_HI:
16970 case R_PPC64_GOT_DTPREL16_HI:
16971 case R_PPC64_GOT16_HI:
16972 case R_PPC64_TOC16_HI:
16973 /* These relocs would only be useful if building up an
16974 offset to later add to r2, perhaps in an indexed
16975 addressing mode instruction. Don't try to optimize.
16976 Unfortunately, the possibility of someone building up an
16977 offset like this or even with the HA relocs, means that
16978 we need to check the high insn when optimizing the low
16979 insn. */
16980 break;
16981
5663e321
AM
16982 case R_PPC64_PLTCALL_NOTOC:
16983 if (!unresolved_reloc)
16984 htab->notoc_plt = 1;
16985 /* Fall through. */
23cedd1d
AM
16986 case R_PPC64_PLTCALL:
16987 if (unresolved_reloc)
16988 {
16989 /* No plt entry. Make this into a direct call. */
16990 bfd_byte *p = contents + rel->r_offset;
16991 insn = bfd_get_32 (input_bfd, p);
16992 insn &= 1;
16993 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
16994 if (r_type == R_PPC64_PLTCALL)
16995 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
16996 unresolved_reloc = save_unresolved_reloc;
16997 r_type = R_PPC64_REL24;
16998 }
16999 break;
17000
5663e321 17001 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
17002 case R_PPC64_PLTSEQ:
17003 if (unresolved_reloc)
17004 {
17005 unresolved_reloc = FALSE;
17006 goto nop_it;
17007 }
17008 break;
17009
5663e321
AM
17010 case R_PPC64_PLT_PCREL34_NOTOC:
17011 if (!unresolved_reloc)
17012 htab->notoc_plt = 1;
17013 /* Fall through. */
17014 case R_PPC64_PLT_PCREL34:
17015 if (unresolved_reloc)
17016 {
17017 bfd_byte *p = contents + rel->r_offset;
17018 bfd_put_32 (input_bfd, PNOP >> 32, p);
17019 bfd_put_32 (input_bfd, PNOP, p + 4);
17020 unresolved_reloc = FALSE;
17021 goto copy_reloc;
17022 }
17023 break;
17024
23cedd1d
AM
17025 case R_PPC64_PLT16_HA:
17026 if (unresolved_reloc)
17027 {
17028 unresolved_reloc = FALSE;
17029 goto nop_it;
17030 }
17031 /* Fall through. */
67f0cbdb
AM
17032 case R_PPC64_GOT_TLSLD16_HA:
17033 case R_PPC64_GOT_TLSGD16_HA:
17034 case R_PPC64_GOT_TPREL16_HA:
17035 case R_PPC64_GOT_DTPREL16_HA:
17036 case R_PPC64_GOT16_HA:
17037 case R_PPC64_TOC16_HA:
98528052 17038 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 17039 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 17040 {
23cedd1d
AM
17041 bfd_byte *p;
17042 nop_it:
17043 p = contents + (rel->r_offset & ~3);
98528052 17044 bfd_put_32 (input_bfd, NOP, p);
d830549d 17045 goto copy_reloc;
98528052 17046 }
67f0cbdb
AM
17047 break;
17048
23cedd1d
AM
17049 case R_PPC64_PLT16_LO:
17050 case R_PPC64_PLT16_LO_DS:
17051 if (unresolved_reloc)
17052 {
17053 unresolved_reloc = FALSE;
17054 goto nop_it;
17055 }
17056 /* Fall through. */
67f0cbdb
AM
17057 case R_PPC64_GOT_TLSLD16_LO:
17058 case R_PPC64_GOT_TLSGD16_LO:
17059 case R_PPC64_GOT_TPREL16_LO_DS:
17060 case R_PPC64_GOT_DTPREL16_LO_DS:
17061 case R_PPC64_GOT16_LO:
17062 case R_PPC64_GOT16_LO_DS:
17063 case R_PPC64_TOC16_LO:
17064 case R_PPC64_TOC16_LO_DS:
98528052 17065 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 17066 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
17067 {
17068 bfd_byte *p = contents + (rel->r_offset & ~3);
17069 insn = bfd_get_32 (input_bfd, p);
2365f8d7 17070 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
560c8763
AM
17071 {
17072 /* Transform addic to addi when we change reg. */
2365f8d7 17073 insn &= ~((0x3fu << 26) | (0x1f << 16));
560c8763
AM
17074 insn |= (14u << 26) | (2 << 16);
17075 }
17076 else
67f0cbdb 17077 {
98528052
AM
17078 insn &= ~(0x1f << 16);
17079 insn |= 2 << 16;
67f0cbdb 17080 }
560c8763 17081 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
17082 }
17083 break;
9a23f96e
AM
17084
17085 case R_PPC64_TPREL16_HA:
17086 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
17087 {
17088 bfd_byte *p = contents + (rel->r_offset & ~3);
252dcdf4
AM
17089 bfd_put_32 (input_bfd, NOP, p);
17090 goto copy_reloc;
9a23f96e
AM
17091 }
17092 break;
17093
17094 case R_PPC64_TPREL16_LO:
17095 case R_PPC64_TPREL16_LO_DS:
17096 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
17097 {
17098 bfd_byte *p = contents + (rel->r_offset & ~3);
17099 insn = bfd_get_32 (input_bfd, p);
17100 insn &= ~(0x1f << 16);
17101 insn |= 13 << 16;
17102 bfd_put_32 (input_bfd, insn, p);
17103 }
17104 break;
67f0cbdb
AM
17105 }
17106
65f38f15
AM
17107 /* Do any further special processing. */
17108 switch (r_type)
17109 {
17110 default:
17111 break;
17112
25f23106 17113 case R_PPC64_REL16_HA:
4a969973
AM
17114 case R_PPC64_REL16_HIGHA:
17115 case R_PPC64_REL16_HIGHERA:
17116 case R_PPC64_REL16_HIGHESTA:
a680de9a 17117 case R_PPC64_REL16DX_HA:
f9c6b907
AM
17118 case R_PPC64_ADDR16_HA:
17119 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
17120 case R_PPC64_ADDR16_HIGHERA:
17121 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
17122 case R_PPC64_TOC16_HA:
17123 case R_PPC64_SECTOFF_HA:
411e1bfb 17124 case R_PPC64_TPREL16_HA:
f9c6b907 17125 case R_PPC64_TPREL16_HIGHA:
411e1bfb 17126 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 17127 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
17128 case R_PPC64_DTPREL16_HA:
17129 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 17130 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 17131 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
17132 /* It's just possible that this symbol is a weak symbol
17133 that's not actually defined anywhere. In that case,
17134 'sec' would be NULL, and we should leave the symbol
17135 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
17136 if (sec == NULL)
17137 break;
1a0670f3 17138 /* Fall through. */
5c5f6e17
AM
17139
17140 case R_PPC64_GOT16_HA:
17141 case R_PPC64_PLTGOT16_HA:
17142 case R_PPC64_PLT16_HA:
17143 case R_PPC64_GOT_TLSGD16_HA:
17144 case R_PPC64_GOT_TLSLD16_HA:
17145 case R_PPC64_GOT_TPREL16_HA:
17146 case R_PPC64_GOT_DTPREL16_HA:
17147 /* Add 0x10000 if sign bit in 0:15 is set.
17148 Bits 0:15 are not used. */
17149 addend += 0x8000;
65f38f15
AM
17150 break;
17151
5663e321
AM
17152 case R_PPC64_D34_HA30:
17153 case R_PPC64_ADDR16_HIGHERA34:
17154 case R_PPC64_ADDR16_HIGHESTA34:
17155 case R_PPC64_REL16_HIGHERA34:
17156 case R_PPC64_REL16_HIGHESTA34:
17157 if (sec != NULL)
17158 addend += 1ULL << 33;
17159 break;
17160
65f38f15
AM
17161 case R_PPC64_ADDR16_DS:
17162 case R_PPC64_ADDR16_LO_DS:
17163 case R_PPC64_GOT16_DS:
17164 case R_PPC64_GOT16_LO_DS:
17165 case R_PPC64_PLT16_LO_DS:
17166 case R_PPC64_SECTOFF_DS:
17167 case R_PPC64_SECTOFF_LO_DS:
17168 case R_PPC64_TOC16_DS:
17169 case R_PPC64_TOC16_LO_DS:
17170 case R_PPC64_PLTGOT16_DS:
17171 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
17172 case R_PPC64_GOT_TPREL16_DS:
17173 case R_PPC64_GOT_TPREL16_LO_DS:
17174 case R_PPC64_GOT_DTPREL16_DS:
17175 case R_PPC64_GOT_DTPREL16_LO_DS:
17176 case R_PPC64_TPREL16_DS:
17177 case R_PPC64_TPREL16_LO_DS:
17178 case R_PPC64_DTPREL16_DS:
17179 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
17180 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17181 mask = 3;
a680de9a
PB
17182 /* If this reloc is against an lq, lxv, or stxv insn, then
17183 the value must be a multiple of 16. This is somewhat of
17184 a hack, but the "correct" way to do this by defining _DQ
17185 forms of all the _DS relocs bloats all reloc switches in
17186 this file. It doesn't make much sense to use these
17187 relocs in data, so testing the insn should be safe. */
2365f8d7
AM
17188 if ((insn & (0x3fu << 26)) == (56u << 26)
17189 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 17190 mask = 15;
a680de9a
PB
17191 relocation += addend;
17192 addend = insn & (mask ^ 3);
17193 if ((relocation & mask) != 0)
65f38f15 17194 {
a680de9a 17195 relocation ^= relocation & mask;
25f53a85 17196 info->callbacks->einfo
695344c0 17197 /* xgettext:c-format */
174d0a74 17198 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 17199 input_bfd, input_section, rel->r_offset,
d830549d 17200 ppc64_elf_howto_table[r_type]->name,
adadcc0c 17201 mask + 1);
65f38f15 17202 bfd_set_error (bfd_error_bad_value);
b34976b6 17203 ret = FALSE;
c316a17c 17204 goto copy_reloc;
65f38f15
AM
17205 }
17206 break;
5bd4f169
AM
17207 }
17208
239e1f3a
AM
17209 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17210 because such sections are not SEC_ALLOC and thus ld.so will
17211 not process them. */
d830549d 17212 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 17213 if (unresolved_reloc
239e1f3a 17214 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
17215 && h->elf.def_dynamic)
17216 && _bfd_elf_section_offset (output_bfd, info, input_section,
17217 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 17218 {
25f53a85 17219 info->callbacks->einfo
695344c0 17220 /* xgettext:c-format */
c1c8c1ef 17221 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 17222 input_bfd, input_section, rel->r_offset,
b80eed39 17223 howto->name,
039b3fef 17224 h->elf.root.root.string);
b34976b6 17225 ret = FALSE;
9c07fe7c 17226 }
5bd4f169 17227
b80eed39
AM
17228 /* 16-bit fields in insns mostly have signed values, but a
17229 few insns have 16-bit unsigned values. Really, we should
17230 have different reloc types. */
17231 if (howto->complain_on_overflow != complain_overflow_dont
17232 && howto->dst_mask == 0xffff
17233 && (input_section->flags & SEC_CODE) != 0)
17234 {
17235 enum complain_overflow complain = complain_overflow_signed;
17236
17237 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 17238 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
a47622ac
AM
17239 complain = complain_overflow_bitfield;
17240 else if (howto->rightshift == 0
2365f8d7
AM
17241 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17242 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17243 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17244 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17245 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17246 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
17247 complain = complain_overflow_unsigned;
17248 if (howto->complain_on_overflow != complain)
17249 {
17250 alt_howto = *howto;
17251 alt_howto.complain_on_overflow = complain;
17252 howto = &alt_howto;
17253 }
17254 }
17255
5663e321 17256 switch (r_type)
a680de9a 17257 {
5663e321
AM
17258 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17259 case R_PPC64_D34:
17260 case R_PPC64_D34_LO:
17261 case R_PPC64_D34_HI30:
17262 case R_PPC64_D34_HA30:
17263 case R_PPC64_PCREL34:
17264 case R_PPC64_GOT_PCREL34:
c213164a
AM
17265 case R_PPC64_TPREL34:
17266 case R_PPC64_DTPREL34:
87c69f97
AM
17267 case R_PPC64_GOT_TLSGD_PCREL34:
17268 case R_PPC64_GOT_TLSLD_PCREL34:
17269 case R_PPC64_GOT_TPREL_PCREL34:
17270 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
17271 case R_PPC64_PLT_PCREL34:
17272 case R_PPC64_PLT_PCREL34_NOTOC:
17273 case R_PPC64_D28:
17274 case R_PPC64_PCREL28:
17275 if (rel->r_offset + 8 > input_section->size)
17276 r = bfd_reloc_outofrange;
17277 else
17278 {
5663e321
AM
17279 relocation += addend;
17280 if (howto->pc_relative)
17281 relocation -= (rel->r_offset
17282 + input_section->output_offset
17283 + input_section->output_section->vma);
17284 relocation >>= howto->rightshift;
17285
17286 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17287 pinsn <<= 32;
17288 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17289
17290 pinsn &= ~howto->dst_mask;
17291 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17292 & howto->dst_mask);
17293 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17294 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17295 r = bfd_reloc_ok;
17296 if (howto->complain_on_overflow == complain_overflow_signed
17297 && (relocation + (1ULL << (howto->bitsize - 1))
17298 >= 1ULL << howto->bitsize))
17299 r = bfd_reloc_overflow;
17300 }
17301 break;
17302
17303 case R_PPC64_REL16DX_HA:
a680de9a
PB
17304 if (rel->r_offset + 4 > input_section->size)
17305 r = bfd_reloc_outofrange;
17306 else
17307 {
17308 relocation += addend;
17309 relocation -= (rel->r_offset
17310 + input_section->output_offset
17311 + input_section->output_section->vma);
3de43e7b 17312 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
17313 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17314 insn &= ~0x1fffc1;
3de43e7b 17315 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
17316 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17317 r = bfd_reloc_ok;
3de43e7b 17318 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
17319 r = bfd_reloc_overflow;
17320 }
5663e321
AM
17321 break;
17322
17323 default:
17324 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17325 contents, rel->r_offset,
17326 relocation, addend);
a680de9a 17327 }
5bd4f169 17328
ef60b7ff 17329 if (r != bfd_reloc_ok)
5bd4f169 17330 {
bc30df16 17331 char *more_info = NULL;
b80eed39 17332 const char *reloc_name = howto->name;
bc30df16
AM
17333
17334 if (reloc_dest != DEST_NORMAL)
17335 {
17336 more_info = bfd_malloc (strlen (reloc_name) + 8);
17337 if (more_info != NULL)
17338 {
17339 strcpy (more_info, reloc_name);
17340 strcat (more_info, (reloc_dest == DEST_OPD
17341 ? " (OPD)" : " (stub)"));
17342 reloc_name = more_info;
17343 }
17344 }
17345
cd27b276 17346 if (r == bfd_reloc_overflow)
5bd4f169 17347 {
8131c122
AM
17348 /* On code like "if (foo) foo();" don't report overflow
17349 on a branch to zero when foo is undefined. */
17350 if (!warned
17351 && (reloc_dest == DEST_STUB
17352 || !(h != NULL
17353 && (h->elf.root.type == bfd_link_hash_undefweak
17354 || h->elf.root.type == bfd_link_hash_undefined)
17355 && is_branch_reloc (r_type))))
9e51d549
AM
17356 info->callbacks->reloc_overflow
17357 (info, (struct bfd_link_hash_entry *) h, sym_name,
17358 reloc_name, orig_rel.r_addend, input_bfd, input_section,
17359 rel->r_offset);
ef60b7ff
AM
17360 }
17361 else
17362 {
25f53a85 17363 info->callbacks->einfo
695344c0 17364 /* xgettext:c-format */
c1c8c1ef 17365 (_("%H: %s against `%pT': error %d\n"),
25f53a85 17366 input_bfd, input_section, rel->r_offset,
bc30df16 17367 reloc_name, sym_name, (int) r);
b34976b6 17368 ret = FALSE;
ef60b7ff 17369 }
c9594989 17370 free (more_info);
5bd4f169 17371 }
c316a17c
AM
17372 copy_reloc:
17373 if (wrel != rel)
17374 *wrel = *rel;
17375 }
17376
17377 if (wrel != rel)
17378 {
17379 Elf_Internal_Shdr *rel_hdr;
17380 size_t deleted = rel - wrel;
17381
17382 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17383 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17384 if (rel_hdr->sh_size == 0)
17385 {
17386 /* It is too late to remove an empty reloc section. Leave
17387 one NONE reloc.
17388 ??? What is wrong with an empty section??? */
17389 rel_hdr->sh_size = rel_hdr->sh_entsize;
17390 deleted -= 1;
17391 }
17392 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
17393 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17394 input_section->reloc_count -= deleted;
5bd4f169
AM
17395 }
17396
645ea6a9
AM
17397 /* If we're emitting relocations, then shortly after this function
17398 returns, reloc offsets and addends for this section will be
17399 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
17400 file rather than the input. Save a copy of the relocs for
17401 opd_entry_value. */
0e1862bb 17402 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
17403 {
17404 bfd_size_type amt;
17405 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
17406 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
17407 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
17408 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
17409 if (rel == NULL)
17410 return FALSE;
17411 memcpy (rel, relocs, amt);
17412 }
5bd4f169
AM
17413 return ret;
17414}
17415
754021d0
AM
17416/* Adjust the value of any local symbols in opd sections. */
17417
6e0b88f1 17418static int
754021d0
AM
17419ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
17420 const char *name ATTRIBUTE_UNUSED,
17421 Elf_Internal_Sym *elfsym,
17422 asection *input_sec,
17423 struct elf_link_hash_entry *h)
17424{
74f0fb50
AM
17425 struct _opd_sec_data *opd;
17426 long adjust;
754021d0
AM
17427 bfd_vma value;
17428
4025353c 17429 if (h != NULL)
6e0b88f1 17430 return 1;
4025353c 17431
74f0fb50
AM
17432 opd = get_opd_info (input_sec);
17433 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 17434 return 1;
754021d0
AM
17435
17436 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 17437 if (!bfd_link_relocatable (info))
754021d0
AM
17438 value -= input_sec->output_section->vma;
17439
51aecdc5 17440 adjust = opd->adjust[OPD_NDX (value)];
4025353c 17441 if (adjust == -1)
6e0b88f1
AM
17442 return 2;
17443
17444 elfsym->st_value += adjust;
17445 return 1;
754021d0
AM
17446}
17447
5bd4f169
AM
17448/* Finish up dynamic symbol handling. We set the contents of various
17449 dynamic sections here. */
17450
b34976b6 17451static bfd_boolean
4ce794b7
AM
17452ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
17453 struct bfd_link_info *info,
17454 struct elf_link_hash_entry *h,
4aef7643 17455 Elf_Internal_Sym *sym)
5bd4f169 17456{
65f38f15 17457 struct ppc_link_hash_table *htab;
8387904d 17458 struct plt_entry *ent;
5bd4f169 17459
65f38f15 17460 htab = ppc_hash_table (info);
4dfe6ac6
NC
17461 if (htab == NULL)
17462 return FALSE;
5bd4f169 17463
49c09209
AM
17464 if (!htab->opd_abi && !h->def_regular)
17465 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
17466 if (ent->plt.offset != (bfd_vma) -1)
17467 {
17468 /* Mark the symbol as undefined, rather than as
17469 defined in glink. Leave the value if there were
17470 any relocations where pointer equality matters
17471 (this is a clue for the dynamic linker, to make
17472 function pointer comparisons work between an
17473 application and shared library), otherwise set it
17474 to zero. */
17475 sym->st_shndx = SHN_UNDEF;
17476 if (!h->pointer_equality_needed)
17477 sym->st_value = 0;
17478 else if (!h->ref_regular_nonweak)
17479 {
17480 /* This breaks function pointer comparisons, but
17481 that is better than breaking tests for a NULL
17482 function pointer. */
17483 sym->st_value = 0;
17484 }
17485 break;
17486 }
5bd4f169 17487
1bdd8fac
AM
17488 if (h->needs_copy
17489 && (h->root.type == bfd_link_hash_defined
17490 || h->root.type == bfd_link_hash_defweak)
17491 && (h->root.u.def.section == htab->elf.sdynbss
17492 || h->root.u.def.section == htab->elf.sdynrelro))
5bd4f169 17493 {
65f38f15 17494 /* This symbol needs a copy reloc. Set it up. */
49c09209 17495 Elf_Internal_Rela rela;
5474d94f 17496 asection *srel;
49c09209 17497 bfd_byte *loc;
5bd4f169 17498
1bdd8fac 17499 if (h->dynindx == -1)
65f38f15 17500 abort ();
5bd4f169 17501
ed7007c1 17502 rela.r_offset = defined_sym_val (h);
5bd4f169
AM
17503 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
17504 rela.r_addend = 0;
afbf7e8e 17505 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
17506 srel = htab->elf.sreldynrelro;
17507 else
17508 srel = htab->elf.srelbss;
17509 loc = srel->contents;
17510 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 17511 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
17512 }
17513
b34976b6 17514 return TRUE;
5bd4f169
AM
17515}
17516
65f38f15
AM
17517/* Used to decide how to sort relocs in an optimal manner for the
17518 dynamic linker, before writing them out. */
17519
17520static enum elf_reloc_type_class
7e612e98
AM
17521ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
17522 const asection *rel_sec,
17523 const Elf_Internal_Rela *rela)
65f38f15 17524{
04c9666a 17525 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
17526 struct ppc_link_hash_table *htab = ppc_hash_table (info);
17527
33e44f2e 17528 if (rel_sec == htab->elf.irelplt)
7e612e98 17529 return reloc_class_ifunc;
a33d1f77 17530
4ce794b7 17531 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 17532 switch (r_type)
65f38f15
AM
17533 {
17534 case R_PPC64_RELATIVE:
17535 return reloc_class_relative;
17536 case R_PPC64_JMP_SLOT:
17537 return reloc_class_plt;
17538 case R_PPC64_COPY:
17539 return reloc_class_copy;
17540 default:
17541 return reloc_class_normal;
17542 }
17543}
17544
5bd4f169
AM
17545/* Finish up the dynamic sections. */
17546
b34976b6 17547static bfd_boolean
4ce794b7
AM
17548ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
17549 struct bfd_link_info *info)
5bd4f169 17550{
65f38f15
AM
17551 struct ppc_link_hash_table *htab;
17552 bfd *dynobj;
5bd4f169 17553 asection *sdyn;
5bd4f169 17554
65f38f15 17555 htab = ppc_hash_table (info);
4dfe6ac6
NC
17556 if (htab == NULL)
17557 return FALSE;
17558
65f38f15 17559 dynobj = htab->elf.dynobj;
3d4d4302 17560 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 17561
65f38f15 17562 if (htab->elf.dynamic_sections_created)
5bd4f169 17563 {
5bd4f169
AM
17564 Elf64_External_Dyn *dyncon, *dynconend;
17565
33e44f2e 17566 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 17567 abort ();
5bd4f169
AM
17568
17569 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 17570 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
17571 for (; dyncon < dynconend; dyncon++)
17572 {
17573 Elf_Internal_Dyn dyn;
19397422 17574 asection *s;
5bd4f169
AM
17575
17576 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
17577
17578 switch (dyn.d_tag)
17579 {
65f38f15
AM
17580 default:
17581 continue;
5bd4f169 17582
5d1634d7 17583 case DT_PPC64_GLINK:
4ce794b7 17584 s = htab->glink;
6348e046 17585 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
17586 /* We stupidly defined DT_PPC64_GLINK to be the start
17587 of glink rather than the first entry point, which is
17588 what ld.so needs, and now have a bigger stub to
17589 support automatic multiple TOCs. */
9e390558 17590 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
17591 break;
17592
19397422
AM
17593 case DT_PPC64_OPD:
17594 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
17595 if (s == NULL)
17596 continue;
17597 dyn.d_un.d_ptr = s->vma;
19397422
AM
17598 break;
17599
e8910a83 17600 case DT_PPC64_OPT:
5663e321
AM
17601 if ((htab->do_multi_toc && htab->multi_toc_needed)
17602 || htab->notoc_plt)
e8910a83 17603 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
17604 if (htab->has_plt_localentry0)
17605 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
17606 break;
17607
19397422
AM
17608 case DT_PPC64_OPDSZ:
17609 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
17610 if (s == NULL)
17611 continue;
eea6121a 17612 dyn.d_un.d_val = s->size;
19397422
AM
17613 break;
17614
65f38f15 17615 case DT_PLTGOT:
33e44f2e 17616 s = htab->elf.splt;
6348e046 17617 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
17618 break;
17619
17620 case DT_JMPREL:
33e44f2e 17621 s = htab->elf.srelplt;
6348e046 17622 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 17623 break;
5bd4f169 17624
65f38f15 17625 case DT_PLTRELSZ:
33e44f2e 17626 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 17627 break;
82e66161
AM
17628
17629 case DT_TEXTREL:
cebd6b8a 17630 if (htab->elf.ifunc_resolvers)
82e66161
AM
17631 info->callbacks->einfo
17632 (_("%P: warning: text relocations and GNU indirect "
17633 "functions may result in a segfault at runtime\n"));
17634 continue;
5bd4f169 17635 }
5bd4f169 17636
65f38f15 17637 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 17638 }
5bd4f169
AM
17639 }
17640
6528b6eb
AM
17641 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
17642 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
17643 {
17644 /* Fill in the first entry in the global offset table.
17645 We use it to hold the link-time TOCbase. */
17646 bfd_put_64 (output_bfd,
60ee0d4a 17647 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 17648 htab->elf.sgot->contents);
5d1634d7
AM
17649
17650 /* Set .got entry size. */
2cdcc330
AM
17651 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
17652 = 8;
5d1634d7
AM
17653 }
17654
6528b6eb
AM
17655 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
17656 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
17657 {
17658 /* Set .plt entry size. */
33e44f2e 17659 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 17660 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
17661 }
17662
84f5d08e
AM
17663 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
17664 brlt ourselves if emitrelocations. */
17665 if (htab->brlt != NULL
17666 && htab->brlt->reloc_count != 0
17667 && !_bfd_elf_link_output_relocs (output_bfd,
17668 htab->brlt,
d4730f92 17669 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
17670 elf_section_data (htab->brlt)->relocs,
17671 NULL))
17672 return FALSE;
17673
176a0d42
AM
17674 if (htab->glink != NULL
17675 && htab->glink->reloc_count != 0
17676 && !_bfd_elf_link_output_relocs (output_bfd,
17677 htab->glink,
d4730f92 17678 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
17679 elf_section_data (htab->glink)->relocs,
17680 NULL))
17681 return FALSE;
17682
da44f4e5 17683
df136d64
AM
17684 if (htab->glink_eh_frame != NULL
17685 && htab->glink_eh_frame->size != 0
17686 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
17687 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
17688 htab->glink_eh_frame,
17689 htab->glink_eh_frame->contents))
17690 return FALSE;
58d180e8 17691
e717da7e 17692 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
17693 since we didn't add them to DYNOBJ. We know dynobj is the first
17694 bfd. */
c72f2fb2 17695 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
17696 {
17697 asection *s;
7b53ace3 17698
0c8d6e5c 17699 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
17700 continue;
17701
e717da7e
AM
17702 s = ppc64_elf_tdata (dynobj)->got;
17703 if (s != NULL
eea6121a 17704 && s->size != 0
e717da7e
AM
17705 && s->output_section != bfd_abs_section_ptr
17706 && !bfd_set_section_contents (output_bfd, s->output_section,
17707 s->contents, s->output_offset,
eea6121a 17708 s->size))
e717da7e
AM
17709 return FALSE;
17710 s = ppc64_elf_tdata (dynobj)->relgot;
17711 if (s != NULL
eea6121a 17712 && s->size != 0
e717da7e
AM
17713 && s->output_section != bfd_abs_section_ptr
17714 && !bfd_set_section_contents (output_bfd, s->output_section,
17715 s->contents, s->output_offset,
eea6121a 17716 s->size))
e717da7e
AM
17717 return FALSE;
17718 }
f6c52c13 17719
b34976b6 17720 return TRUE;
5bd4f169
AM
17721}
17722
5bd4f169 17723#include "elf64-target.h"
7b8e7dad
AM
17724
17725/* FreeBSD support */
17726
17727#undef TARGET_LITTLE_SYM
6afcdeb3 17728#define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
7b8e7dad 17729#undef TARGET_LITTLE_NAME
6afcdeb3 17730#define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
7b8e7dad
AM
17731
17732#undef TARGET_BIG_SYM
6d00b590 17733#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
17734#undef TARGET_BIG_NAME
17735#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
17736
17737#undef ELF_OSABI
17738#define ELF_OSABI ELFOSABI_FREEBSD
17739
17740#undef elf64_bed
17741#define elf64_bed elf64_powerpc_fbsd_bed
17742
17743#include "elf64-target.h"