]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
Update year range in copyright notice of binutils files
[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. */
0ccf57bd 2302 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2303 ++i;
2304 codesecsym = i;
2305
2306 for (; i < symcount; ++i)
2307 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2308 | SEC_THREAD_LOCAL))
2309 != (SEC_CODE | SEC_ALLOC))
2310 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2311 break;
2312 codesecsymend = i;
2313
2314 for (; i < symcount; ++i)
2315 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2316 break;
2317 secsymend = i;
2318
2319 for (; i < symcount; ++i)
2320 if (strcmp (syms[i]->section->name, ".opd") != 0)
2321 break;
2322 opdsymend = i;
2323
2324 for (; i < symcount; ++i)
2cdcc330
AM
2325 if (((syms[i]->section->flags
2326 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2327 != (SEC_CODE | SEC_ALLOC))
2328 break;
2329 symcount = i;
2330 }
c9727e01 2331 count = 0;
90e3cdf2 2332
699733f6 2333 if (relocatable)
90e3cdf2 2334 {
699733f6
AM
2335 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2336 arelent *r;
2337 size_t size;
0ccf57bd 2338 size_t relcount;
90e3cdf2 2339
468392fb
AM
2340 if (opdsymend == secsymend)
2341 goto done;
2342
699733f6 2343 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2344 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2345 if (relcount == 0)
c9727e01 2346 goto done;
90e3cdf2 2347
7356fed5
AM
2348 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2349 {
2350 count = -1;
2351 goto done;
2352 }
2353
699733f6 2354 size = 0;
595da8c5 2355 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2356 {
2357 asymbol *sym;
90e3cdf2 2358
595da8c5 2359 while (r < opd->relocation + relcount
699733f6
AM
2360 && r->address < syms[i]->value + opd->vma)
2361 ++r;
90e3cdf2 2362
595da8c5 2363 if (r == opd->relocation + relcount)
699733f6 2364 break;
90e3cdf2 2365
699733f6
AM
2366 if (r->address != syms[i]->value + opd->vma)
2367 continue;
90e3cdf2 2368
699733f6
AM
2369 if (r->howto->type != R_PPC64_ADDR64)
2370 continue;
90e3cdf2 2371
699733f6
AM
2372 sym = *r->sym_ptr_ptr;
2373 if (!sym_exists_at (syms, opdsymend, symcount,
2374 sym->section->id, sym->value + r->addend))
2375 {
2376 ++count;
2377 size += sizeof (asymbol);
2378 size += strlen (syms[i]->name) + 2;
2379 }
2380 }
90e3cdf2 2381
c4b0b099
AM
2382 if (size == 0)
2383 goto done;
699733f6
AM
2384 s = *ret = bfd_malloc (size);
2385 if (s == NULL)
2386 {
7356fed5 2387 count = -1;
c9727e01 2388 goto done;
699733f6 2389 }
90e3cdf2 2390
699733f6 2391 names = (char *) (s + count);
90e3cdf2 2392
595da8c5 2393 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2394 {
699733f6 2395 asymbol *sym;
90e3cdf2 2396
595da8c5 2397 while (r < opd->relocation + relcount
699733f6
AM
2398 && r->address < syms[i]->value + opd->vma)
2399 ++r;
90e3cdf2 2400
595da8c5 2401 if (r == opd->relocation + relcount)
699733f6
AM
2402 break;
2403
2404 if (r->address != syms[i]->value + opd->vma)
2405 continue;
2406
2407 if (r->howto->type != R_PPC64_ADDR64)
2408 continue;
90e3cdf2 2409
699733f6
AM
2410 sym = *r->sym_ptr_ptr;
2411 if (!sym_exists_at (syms, opdsymend, symcount,
2412 sym->section->id, sym->value + r->addend))
2413 {
2414 size_t len;
2415
2416 *s = *syms[i];
6ba2a415 2417 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2418 s->section = sym->section;
2419 s->value = sym->value + r->addend;
2420 s->name = names;
2421 *names++ = '.';
2422 len = strlen (syms[i]->name);
2423 memcpy (names, syms[i]->name, len + 1);
2424 names += len + 1;
6f610d07
UW
2425 /* Have udata.p point back to the original symbol this
2426 synthetic symbol was derived from. */
2427 s->udata.p = syms[i];
699733f6
AM
2428 s++;
2429 }
2430 }
2431 }
2432 else
90e3cdf2 2433 {
468392fb 2434 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 2435 bfd_byte *contents = NULL;
699733f6 2436 size_t size;
0ccf57bd 2437 size_t plt_count = 0;
468392fb
AM
2438 bfd_vma glink_vma = 0, resolv_vma = 0;
2439 asection *dynamic, *glink = NULL, *relplt = NULL;
2440 arelent *p;
90e3cdf2 2441
ee67d69a 2442 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2443 {
c4b0b099
AM
2444 free_contents_and_exit_err:
2445 count = -1;
ee67d69a 2446 free_contents_and_exit:
c9594989 2447 free (contents);
c9727e01 2448 goto done;
699733f6 2449 }
90e3cdf2 2450
699733f6
AM
2451 size = 0;
2452 for (i = secsymend; i < opdsymend; ++i)
2453 {
2454 bfd_vma ent;
90e3cdf2 2455
5ef11c02
AM
2456 /* Ignore bogus symbols. */
2457 if (syms[i]->value > opd->size - 8)
2458 continue;
2459
699733f6
AM
2460 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2461 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2462 {
2463 ++count;
2464 size += sizeof (asymbol);
2465 size += strlen (syms[i]->name) + 2;
2466 }
2467 }
90e3cdf2 2468
468392fb 2469 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2470 if (dyn_count != 0
2471 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2472 {
2473 bfd_byte *dynbuf, *extdyn, *extdynend;
2474 size_t extdynsize;
2475 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2476
2477 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2478 goto free_contents_and_exit_err;
468392fb
AM
2479
2480 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2481 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2482
2483 extdyn = dynbuf;
2484 extdynend = extdyn + dynamic->size;
2485 for (; extdyn < extdynend; extdyn += extdynsize)
2486 {
2487 Elf_Internal_Dyn dyn;
2488 (*swap_dyn_in) (abfd, extdyn, &dyn);
2489
2490 if (dyn.d_tag == DT_NULL)
2491 break;
2492
2493 if (dyn.d_tag == DT_PPC64_GLINK)
2494 {
9e390558
AM
2495 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2496 See comment in ppc64_elf_finish_dynamic_sections. */
2497 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2498 /* The .glink section usually does not survive the final
2499 link; search for the section (usually .text) where the
2500 glink stubs now reside. */
2501 glink = bfd_sections_find_if (abfd, section_covers_vma,
2502 &glink_vma);
2503 break;
2504 }
2505 }
2506
2507 free (dynbuf);
2508 }
2509
2510 if (glink != NULL)
2511 {
2512 /* Determine __glink trampoline by reading the relative branch
2513 from the first glink stub. */
2514 bfd_byte buf[4];
b9e5796b
AM
2515 unsigned int off = 0;
2516
2517 while (bfd_get_section_contents (abfd, glink, buf,
2518 glink_vma + off - glink->vma, 4))
468392fb
AM
2519 {
2520 unsigned int insn = bfd_get_32 (abfd, buf);
2521 insn ^= B_DOT;
2522 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2523 {
2cdcc330
AM
2524 resolv_vma
2525 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2526 break;
2527 }
2528 off += 4;
2529 if (off > 4)
2530 break;
468392fb
AM
2531 }
2532
2533 if (resolv_vma)
2534 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2535
066ee829
AM
2536 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2537 if (relplt != NULL)
2538 {
2539 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2cdcc330 2540 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 2541 goto free_contents_and_exit_err;
68ffbac6 2542
066ee829
AM
2543 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2544 size += plt_count * sizeof (asymbol);
468392fb 2545
066ee829
AM
2546 p = relplt->relocation;
2547 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2548 {
2549 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2550 if (p->addend != 0)
2551 size += sizeof ("+0x") - 1 + 16;
2552 }
066ee829 2553 }
468392fb
AM
2554 }
2555
c4b0b099
AM
2556 if (size == 0)
2557 goto free_contents_and_exit;
699733f6
AM
2558 s = *ret = bfd_malloc (size);
2559 if (s == NULL)
c4b0b099 2560 goto free_contents_and_exit_err;
90e3cdf2 2561
468392fb 2562 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2563
699733f6 2564 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2565 {
699733f6 2566 bfd_vma ent;
90e3cdf2 2567
5ef11c02
AM
2568 if (syms[i]->value > opd->size - 8)
2569 continue;
2570
699733f6
AM
2571 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2572 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2573 {
0ccf57bd 2574 size_t lo, hi;
699733f6 2575 size_t len;
c9727e01 2576 asection *sec = abfd->sections;
90e3cdf2 2577
699733f6
AM
2578 *s = *syms[i];
2579 lo = codesecsym;
2580 hi = codesecsymend;
2581 while (lo < hi)
2582 {
0ccf57bd 2583 size_t mid = (lo + hi) >> 1;
699733f6
AM
2584 if (syms[mid]->section->vma < ent)
2585 lo = mid + 1;
2586 else if (syms[mid]->section->vma > ent)
2587 hi = mid;
2588 else
c9727e01
AM
2589 {
2590 sec = syms[mid]->section;
2591 break;
2592 }
699733f6
AM
2593 }
2594
c9727e01 2595 if (lo >= hi && lo > codesecsym)
699733f6 2596 sec = syms[lo - 1]->section;
699733f6
AM
2597
2598 for (; sec != NULL; sec = sec->next)
2599 {
2600 if (sec->vma > ent)
2601 break;
63524580
JK
2602 /* SEC_LOAD may not be set if SEC is from a separate debug
2603 info file. */
2604 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2605 break;
2606 if ((sec->flags & SEC_CODE) != 0)
2607 s->section = sec;
2608 }
6ba2a415 2609 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2610 s->value = ent - s->section->vma;
2611 s->name = names;
2612 *names++ = '.';
2613 len = strlen (syms[i]->name);
2614 memcpy (names, syms[i]->name, len + 1);
2615 names += len + 1;
6f610d07
UW
2616 /* Have udata.p point back to the original symbol this
2617 synthetic symbol was derived from. */
2618 s->udata.p = syms[i];
699733f6 2619 s++;
90e3cdf2 2620 }
90e3cdf2 2621 }
699733f6 2622 free (contents);
468392fb
AM
2623
2624 if (glink != NULL && relplt != NULL)
2625 {
2626 if (resolv_vma)
2627 {
2628 /* Add a symbol for the main glink trampoline. */
86a4952b 2629 memset (s, 0, sizeof *s);
468392fb 2630 s->the_bfd = abfd;
6ba2a415 2631 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2632 s->section = glink;
2633 s->value = resolv_vma - glink->vma;
2634 s->name = names;
2cdcc330
AM
2635 memcpy (names, "__glink_PLTresolve",
2636 sizeof ("__glink_PLTresolve"));
468392fb
AM
2637 names += sizeof ("__glink_PLTresolve");
2638 s++;
2639 count++;
2640 }
2641
2642 /* FIXME: It would be very much nicer to put sym@plt on the
2643 stub rather than on the glink branch table entry. The
2644 objdump disassembler would then use a sensible symbol
2645 name on plt calls. The difficulty in doing so is
2646 a) finding the stubs, and,
2647 b) matching stubs against plt entries, and,
2648 c) there can be multiple stubs for a given plt entry.
2649
2650 Solving (a) could be done by code scanning, but older
2651 ppc64 binaries used different stubs to current code.
2652 (b) is the tricky one since you need to known the toc
2653 pointer for at least one function that uses a pic stub to
2654 be able to calculate the plt address referenced.
2655 (c) means gdb would need to set multiple breakpoints (or
2656 find the glink branch itself) when setting breakpoints
2657 for pending shared library loads. */
2658 p = relplt->relocation;
2659 for (i = 0; i < plt_count; i++, p++)
2660 {
2661 size_t len;
2662
2663 *s = **p->sym_ptr_ptr;
2664 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2665 we are defining a symbol, ensure one of them is set. */
2666 if ((s->flags & BSF_LOCAL) == 0)
2667 s->flags |= BSF_GLOBAL;
6ba2a415 2668 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2669 s->section = glink;
2670 s->value = glink_vma - glink->vma;
2671 s->name = names;
2672 s->udata.p = NULL;
2673 len = strlen ((*p->sym_ptr_ptr)->name);
2674 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2675 names += len;
e054468f
AM
2676 if (p->addend != 0)
2677 {
2678 memcpy (names, "+0x", sizeof ("+0x") - 1);
2679 names += sizeof ("+0x") - 1;
2680 bfd_sprintf_vma (abfd, names, p->addend);
2681 names += strlen (names);
2682 }
468392fb
AM
2683 memcpy (names, "@plt", sizeof ("@plt"));
2684 names += sizeof ("@plt");
2685 s++;
b9e5796b
AM
2686 if (abi < 2)
2687 {
2688 glink_vma += 8;
2689 if (i >= 0x8000)
2690 glink_vma += 4;
2691 }
2692 else
468392fb
AM
2693 glink_vma += 4;
2694 }
2695 count += plt_count;
2696 }
90e3cdf2
JJ
2697 }
2698
c9727e01 2699 done:
a7535cf3 2700 free (syms);
90e3cdf2
JJ
2701 return count;
2702}
5bd4f169 2703\f
65f38f15
AM
2704/* The following functions are specific to the ELF linker, while
2705 functions above are used generally. Those named ppc64_elf_* are
2706 called by the main ELF linker code. They appear in this file more
2707 or less in the order in which they are called. eg.
2708 ppc64_elf_check_relocs is called early in the link process,
2709 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2710 called.
2711
2712 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2713 functions have both a function code symbol and a function descriptor
2714 symbol. A call to foo in a relocatable object file looks like:
2715
2716 . .text
2717 . x:
2718 . bl .foo
2719 . nop
2720
2721 The function definition in another object file might be:
2722
2723 . .section .opd
2724 . foo: .quad .foo
2725 . .quad .TOC.@tocbase
2726 . .quad 0
2727 .
2728 . .text
2729 . .foo: blr
2730
2731 When the linker resolves the call during a static link, the branch
2732 unsurprisingly just goes to .foo and the .opd information is unused.
2733 If the function definition is in a shared library, things are a little
2734 different: The call goes via a plt call stub, the opd information gets
2735 copied to the plt, and the linker patches the nop.
2736
2737 . x:
2738 . bl .foo_stub
2739 . ld 2,40(1)
2740 .
2741 .
2742 . .foo_stub:
71a39c98
AM
2743 . std 2,40(1) # in practice, the call stub
2744 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2745 . addi 11,11,Lfoo@toc@l # this is the general idea
2746 . ld 12,0(11)
2747 . ld 2,8(11)
2748 . mtctr 12
2749 . ld 11,16(11)
e86ce104
AM
2750 . bctr
2751 .
2752 . .section .plt
2753 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2754
2755 The "reloc ()" notation is supposed to indicate that the linker emits
2756 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2757 copying.
2758
2759 What are the difficulties here? Well, firstly, the relocations
2760 examined by the linker in check_relocs are against the function code
2761 sym .foo, while the dynamic relocation in the plt is emitted against
2762 the function descriptor symbol, foo. Somewhere along the line, we need
2763 to carefully copy dynamic link information from one symbol to the other.
2764 Secondly, the generic part of the elf linker will make .foo a dynamic
2765 symbol as is normal for most other backends. We need foo dynamic
2766 instead, at least for an application final link. However, when
2767 creating a shared library containing foo, we need to have both symbols
2768 dynamic so that references to .foo are satisfied during the early
2769 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2770 definition from some other object, eg. a static library.
2771
2772 Update: As of August 2004, we support a new convention. Function
2773 calls may use the function descriptor symbol, ie. "bl foo". This
2774 behaves exactly as "bl .foo". */
65f38f15 2775
7c8bbca5
AM
2776/* Of those relocs that might be copied as dynamic relocs, this
2777 function selects those that must be copied when linking a shared
2778 library or PIE, even when the symbol is local. */
65f38f15 2779
1d483afe
AM
2780static int
2781must_be_dyn_reloc (struct bfd_link_info *info,
2782 enum elf_ppc64_reloc_type r_type)
2783{
2784 switch (r_type)
2785 {
2786 default:
7c8bbca5
AM
2787 /* Only relative relocs can be resolved when the object load
2788 address isn't fixed. DTPREL64 is excluded because the
2789 dynamic linker needs to differentiate global dynamic from
2790 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2791 return 1;
2792
2793 case R_PPC64_REL32:
2794 case R_PPC64_REL64:
2795 case R_PPC64_REL30:
1bdd8fac
AM
2796 case R_PPC64_TOC16:
2797 case R_PPC64_TOC16_DS:
2798 case R_PPC64_TOC16_LO:
2799 case R_PPC64_TOC16_HI:
2800 case R_PPC64_TOC16_HA:
2801 case R_PPC64_TOC16_LO_DS:
1d483afe
AM
2802 return 0;
2803
2804 case R_PPC64_TPREL16:
2805 case R_PPC64_TPREL16_LO:
2806 case R_PPC64_TPREL16_HI:
2807 case R_PPC64_TPREL16_HA:
2808 case R_PPC64_TPREL16_DS:
2809 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2810 case R_PPC64_TPREL16_HIGH:
2811 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2812 case R_PPC64_TPREL16_HIGHER:
2813 case R_PPC64_TPREL16_HIGHERA:
2814 case R_PPC64_TPREL16_HIGHEST:
2815 case R_PPC64_TPREL16_HIGHESTA:
2816 case R_PPC64_TPREL64:
c213164a 2817 case R_PPC64_TPREL34:
7c8bbca5
AM
2818 /* These relocations are relative but in a shared library the
2819 linker doesn't know the thread pointer base. */
2820 return bfd_link_dll (info);
1d483afe
AM
2821 }
2822}
65f38f15 2823
f4656909 2824/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
e1c6cf61 2825 copying dynamic variables from a shared lib into an app's .dynbss
f4656909 2826 section, and instead use a dynamic relocation to point into the
e1c6cf61
AM
2827 shared lib. With code that gcc generates it is vital that this be
2828 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2829 actually the address of a function descriptor which resides in the
2830 .opd section. gcc uses the descriptor directly rather than going
2831 via the GOT as some other ABIs do, which means that initialized
2832 function pointers reference the descriptor. Thus, a function
2833 pointer initialized to the address of a function in a shared
2834 library will either require a .dynbss copy and a copy reloc, or a
2835 dynamic reloc. Using a .dynbss copy redefines the function
2836 descriptor symbol to point to the copy. This presents a problem as
2837 a PLT entry for that function is also initialized from the function
2838 descriptor symbol and the copy may not be initialized first. */
a23b6845 2839#define ELIMINATE_COPY_RELOCS 1
f4656909 2840
721956f4 2841/* Section name for stubs is the associated section name plus this
29942be8
NC
2842 string. */
2843#define STUB_SUFFIX ".stub"
721956f4
AM
2844
2845/* Linker stubs.
2846 ppc_stub_long_branch:
2847 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2848 destination, but a 24 bit branch in a stub section will reach.
2849 . b dest
2850
2851 ppc_stub_plt_branch:
2852 Similar to the above, but a 24 bit branch in the stub section won't
2853 reach its destination.
6df4c9c2
AM
2854 . addis %r12,%r2,xxx@toc@ha
2855 . ld %r12,xxx@toc@l(%r12)
71a39c98 2856 . mtctr %r12
721956f4
AM
2857 . bctr
2858
2859 ppc_stub_plt_call:
2c66dc6c
AM
2860 Used to call a function in a shared library. If it so happens that
2861 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2862 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 2863 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
2864 . addis %r11,%r2,xxx@toc@ha
2865 . ld %r12,xxx+0@toc@l(%r11)
2866 . mtctr %r12
2867 . ld %r2,xxx+8@toc@l(%r11)
2868 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2869 . bctr
ad8e1ba5
AM
2870
2871 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2872 code to adjust the value and save r2 to support multiple toc sections.
2873 A ppc_stub_long_branch with an r2 offset looks like:
2874 . std %r2,40(%r1)
2875 . addis %r2,%r2,off@ha
2876 . addi %r2,%r2,off@l
2877 . b dest
2878
2879 A ppc_stub_plt_branch with an r2 offset looks like:
2880 . std %r2,40(%r1)
6df4c9c2
AM
2881 . addis %r12,%r2,xxx@toc@ha
2882 . ld %r12,xxx@toc@l(%r12)
ad8e1ba5
AM
2883 . addis %r2,%r2,off@ha
2884 . addi %r2,%r2,off@l
71a39c98 2885 . mtctr %r12
ad8e1ba5 2886 . bctr
ac2df442 2887
05d0e962
AM
2888 All of the above stubs are shown as their ELFv1 variants. ELFv2
2889 variants exist too, simpler for plt calls since a new toc pointer
2890 and static chain are not loaded by the stub. In addition, ELFv2
2891 has some more complex stubs to handle calls marked with NOTOC
2892 relocs from functions where r2 is not a valid toc pointer. These
2893 come in two flavours, the ones shown below, and _both variants that
2894 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2895 one call is from a function where r2 is used as the toc pointer but
2896 needs a toc adjusting stub for small-model multi-toc, and another
2897 call is from a function where r2 is not valid.
2898 ppc_stub_long_branch_notoc:
2899 . mflr %r12
2900 . bcl 20,31,1f
2901 . 1:
2902 . mflr %r11
2903 . mtlr %r12
2cdcc330
AM
2904 . addis %r12,%r11,dest-1b@ha
2905 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2906 . b dest
2907
2908 ppc_stub_plt_branch_notoc:
2909 . mflr %r12
2910 . bcl 20,31,1f
2911 . 1:
2912 . mflr %r11
2913 . mtlr %r12
2914 . lis %r12,xxx-1b@highest
f891966f 2915 . ori %r12,%r12,xxx-1b@higher
05d0e962 2916 . sldi %r12,%r12,32
f891966f 2917 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2918 . ori %r12,%r12,xxx-1b@l
2919 . add %r12,%r11,%r12
2920 . mtctr %r12
2921 . bctr
2922
2923 ppc_stub_plt_call_notoc:
2924 . mflr %r12
2925 . bcl 20,31,1f
2926 . 1:
2927 . mflr %r11
2928 . mtlr %r12
2929 . lis %r12,xxx-1b@highest
f891966f 2930 . ori %r12,%r12,xxx-1b@higher
05d0e962 2931 . sldi %r12,%r12,32
f891966f 2932 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2933 . ori %r12,%r12,xxx-1b@l
2934 . ldx %r12,%r11,%r12
2935 . mtctr %r12
2936 . bctr
2937
7c1f4227 2938 There are also ELFv1 power10 variants of these stubs.
04bdff6a 2939 ppc_stub_long_branch_notoc:
d4b87b1e 2940 . pla %r12,dest@pcrel
04bdff6a
AM
2941 . b dest
2942 ppc_stub_plt_branch_notoc:
2943 . lis %r11,(dest-1f)@highesta34
2944 . ori %r11,%r11,(dest-1f)@highera34
2945 . sldi %r11,%r11,34
d4b87b1e 2946 . 1: pla %r12,dest@pcrel
04bdff6a
AM
2947 . add %r12,%r11,%r12
2948 . mtctr %r12
2949 . bctr
2950 ppc_stub_plt_call_notoc:
2951 . lis %r11,(xxx-1f)@highesta34
2952 . ori %r11,%r11,(xxx-1f)@highera34
2953 . sldi %r11,%r11,34
d4b87b1e 2954 . 1: pla %r12,xxx@pcrel
04bdff6a
AM
2955 . ldx %r12,%r11,%r12
2956 . mtctr %r12
2957 . bctr
2958
05d0e962
AM
2959 In cases where the high instructions would add zero, they are
2960 omitted and following instructions modified in some cases.
7c1f4227 2961 For example, a power10 ppc_stub_plt_call_notoc might simplify down
04bdff6a
AM
2962 to
2963 . pld %r12,xxx@pcrel
2964 . mtctr %r12
2965 . bctr
05d0e962
AM
2966
2967 For a given stub group (a set of sections all using the same toc
2968 pointer value) there will be just one stub type used for any
2969 particular function symbol. For example, if printf is called from
2970 code with the tocsave optimization (ie. r2 saved in function
2971 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2972 and from other code without the tocsave optimization requiring a
2973 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2974 type will be created. Calls with the tocsave optimization will
2975 enter this stub after the instruction saving r2. A similar
2976 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2977 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2978 to call an external function like printf. If other calls to printf
2979 require a ppc_stub_plt_call linkage stub then a single
2980 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2981 call. If other calls to printf require a ppc_stub_plt_call_r2save
2982 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2983 be created and calls not requiring r2 to be saved will enter the
2984 stub after the r2 save instruction. There is an analogous
2985 hierarchy of long branch and plt branch stubs for local call
2986 linkage. */
721956f4 2987
2cdcc330
AM
2988enum ppc_stub_type
2989{
721956f4
AM
2990 ppc_stub_none,
2991 ppc_stub_long_branch,
ad8e1ba5 2992 ppc_stub_long_branch_r2off,
05d0e962
AM
2993 ppc_stub_long_branch_notoc,
2994 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 2995 ppc_stub_plt_branch,
ad8e1ba5 2996 ppc_stub_plt_branch_r2off,
05d0e962
AM
2997 ppc_stub_plt_branch_notoc,
2998 ppc_stub_plt_branch_both,
794e51c0 2999 ppc_stub_plt_call,
7341d5e2 3000 ppc_stub_plt_call_r2save,
05d0e962
AM
3001 ppc_stub_plt_call_notoc,
3002 ppc_stub_plt_call_both,
a4b6fadd
AM
3003 ppc_stub_global_entry,
3004 ppc_stub_save_res
721956f4
AM
3005};
3006
6f20ed8a
AM
3007/* Information on stub grouping. */
3008struct map_stub
3009{
3010 /* The stub section. */
3011 asection *stub_sec;
3012 /* This is the section to which stubs in the group will be attached. */
3013 asection *link_sec;
a4b6fadd
AM
3014 /* Next group. */
3015 struct map_stub *next;
3016 /* Whether to emit a copy of register save/restore functions in this
3017 group. */
3018 int needs_save_res;
df136d64
AM
3019 /* Current offset within stubs after the insn restoring lr in a
3020 _notoc or _both stub using bcl for pc-relative addressing, or
3021 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3022 unsigned int lr_restore;
3023 /* Accumulated size of EH info emitted to describe return address
3024 if stubs modify lr. Does not include 17 byte FDE header. */
3025 unsigned int eh_size;
3026 /* Offset in glink_eh_frame to the start of EH info for this group. */
3027 unsigned int eh_base;
6f20ed8a
AM
3028};
3029
2cdcc330
AM
3030struct ppc_stub_hash_entry
3031{
721956f4
AM
3032 /* Base hash table entry structure. */
3033 struct bfd_hash_entry root;
3034
ad8e1ba5
AM
3035 enum ppc_stub_type stub_type;
3036
6f20ed8a
AM
3037 /* Group information. */
3038 struct map_stub *group;
721956f4
AM
3039
3040 /* Offset within stub_sec of the beginning of this stub. */
3041 bfd_vma stub_offset;
3042
3043 /* Given the symbol's value and its section we can determine its final
3044 value when building the stubs (so the stub knows where to jump. */
3045 bfd_vma target_value;
3046 asection *target_section;
3047
721956f4
AM
3048 /* The symbol table entry, if any, that this was derived from. */
3049 struct ppc_link_hash_entry *h;
e054468f 3050 struct plt_entry *plt_ent;
721956f4 3051
2d7ad24e
AM
3052 /* Symbol type. */
3053 unsigned char symtype;
3054
6911b7dc
AM
3055 /* Symbol st_other. */
3056 unsigned char other;
721956f4
AM
3057};
3058
2cdcc330
AM
3059struct ppc_branch_hash_entry
3060{
721956f4
AM
3061 /* Base hash table entry structure. */
3062 struct bfd_hash_entry root;
3063
c456f082 3064 /* Offset within branch lookup table. */
721956f4
AM
3065 unsigned int offset;
3066
3067 /* Generation marker. */
3068 unsigned int iter;
3069};
65f38f15 3070
19e08130
AM
3071/* Used to track dynamic relocations for local symbols. */
3072struct ppc_dyn_relocs
3073{
3074 struct ppc_dyn_relocs *next;
3075
3076 /* The input section of the reloc. */
3077 asection *sec;
3078
3079 /* Total number of relocs copied for the input section. */
3080 unsigned int count : 31;
3081
3082 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3083 unsigned int ifunc : 1;
3084};
3085
65f38f15
AM
3086struct ppc_link_hash_entry
3087{
3088 struct elf_link_hash_entry elf;
3089
2cdcc330
AM
3090 union
3091 {
b3fac117
AM
3092 /* A pointer to the most recently used stub hash entry against this
3093 symbol. */
3094 struct ppc_stub_hash_entry *stub_cache;
3095
3096 /* A pointer to the next symbol starting with a '.' */
3097 struct ppc_link_hash_entry *next_dot_sym;
3098 } u;
721956f4 3099
721956f4 3100 /* Link between function code and descriptor symbols. */
34814b9f 3101 struct ppc_link_hash_entry *oh;
721956f4 3102
e86ce104
AM
3103 /* Flag function code and descriptor symbols. */
3104 unsigned int is_func:1;
3105 unsigned int is_func_descriptor:1;
908b32fc 3106 unsigned int fake:1;
411e1bfb 3107
c5614fa4
AM
3108 /* Whether global opd/toc sym has been adjusted or not.
3109 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3110 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3111 unsigned int adjust_done:1;
3112
a4b6fadd
AM
3113 /* Set if this is an out-of-line register save/restore function,
3114 with non-standard calling convention. */
3115 unsigned int save_res:1;
3116
8b5f1ed8
AM
3117 /* Set if a duplicate symbol with non-zero localentry is detected,
3118 even when the duplicate symbol does not provide a definition. */
3119 unsigned int non_zero_localentry:1;
3120
411e1bfb 3121 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3122 Bits are or'd into the mask as the corresponding relocs are
3123 encountered during check_relocs, with TLS_TLS being set when any
3124 of the other TLS bits are set. tls_optimize clears bits when
3125 optimizing to indicate the corresponding GOT entry type is not
3126 needed. If set, TLS_TLS is never cleared. tls_optimize may also
b00a0a86 3127 set TLS_GDIE when a GD reloc turns into an IE one.
37da22e5
AM
3128 These flags are also kept for local symbols. */
3129#define TLS_TLS 1 /* Any TLS reloc. */
3130#define TLS_GD 2 /* GD reloc. */
3131#define TLS_LD 4 /* LD reloc. */
3132#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3133#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3134#define TLS_MARK 32 /* __tls_get_addr call marked. */
b00a0a86 3135#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
46e9995a 3136#define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
f961d9dd 3137 unsigned char tls_mask;
37da22e5
AM
3138
3139 /* The above field is also used to mark function symbols. In which
3140 case TLS_TLS will be 0. */
3141#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3142#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3143#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3144};
3145
ed7007c1
AM
3146static inline struct ppc_link_hash_entry *
3147ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3148{
3149 return (struct ppc_link_hash_entry *) ent;
3150}
3151
65f38f15
AM
3152/* ppc64 ELF linker hash table. */
3153
3154struct ppc_link_hash_table
3155{
3156 struct elf_link_hash_table elf;
3157
721956f4
AM
3158 /* The stub hash table. */
3159 struct bfd_hash_table stub_hash_table;
3160
3161 /* Another hash table for plt_branch stubs. */
3162 struct bfd_hash_table branch_hash_table;
3163
3b421ab3
AM
3164 /* Hash table for function prologue tocsave. */
3165 htab_t tocsave_htab;
3166
e7d1c40c
AM
3167 /* Various options and other info passed from the linker. */
3168 struct ppc64_elf_params *params;
721956f4 3169
6f20ed8a
AM
3170 /* The size of sec_info below. */
3171 unsigned int sec_info_arr_size;
3172
3173 /* Per-section array of extra section info. Done this way rather
3174 than as part of ppc64_elf_section_data so we have the info for
3175 non-ppc64 sections. */
3176 struct
3177 {
3178 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3179 bfd_vma toc_off;
6f20ed8a
AM
3180
3181 union
3182 {
3183 /* The section group that this section belongs to. */
3184 struct map_stub *group;
3185 /* A temp section list pointer. */
3186 asection *list;
3187 } u;
3188 } *sec_info;
721956f4 3189
a4b6fadd
AM
3190 /* Linked list of groups. */
3191 struct map_stub *group;
3192
ad8e1ba5
AM
3193 /* Temp used when calculating TOC pointers. */
3194 bfd_vma toc_curr;
bf102f86
AM
3195 bfd *toc_bfd;
3196 asection *toc_first_sec;
ad8e1ba5 3197
b3fac117
AM
3198 /* Used when adding symbols. */
3199 struct ppc_link_hash_entry *dot_syms;
3200
33e44f2e 3201 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3202 asection *glink;
9e390558 3203 asection *global_entry;
82bd7b59 3204 asection *sfpr;
2d7ad24e
AM
3205 asection *pltlocal;
3206 asection *relpltlocal;
4ce794b7
AM
3207 asection *brlt;
3208 asection *relbrlt;
58d180e8 3209 asection *glink_eh_frame;
ec338859 3210
8387904d
AM
3211 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3212 struct ppc_link_hash_entry *tls_get_addr;
3213 struct ppc_link_hash_entry *tls_get_addr_fd;
9e7028aa
AM
3214 struct ppc_link_hash_entry *tga_desc;
3215 struct ppc_link_hash_entry *tga_desc_fd;
a804e476 3216 struct map_stub *tga_group;
411e1bfb 3217
927be08e
AM
3218 /* The size of reliplt used by got entry relocs. */
3219 bfd_size_type got_reli_size;
3220
9b5ecbd0 3221 /* Statistics. */
7341d5e2 3222 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 3223
ee75fd95
AM
3224 /* Number of stubs against global syms. */
3225 unsigned long stub_globals;
3226
ee67d69a
AM
3227 /* Set if we're linking code with function descriptors. */
3228 unsigned int opd_abi:1;
3229
4c52953f 3230 /* Support for multiple toc sections. */
33c0ec9d 3231 unsigned int do_multi_toc:1;
4c52953f 3232 unsigned int multi_toc_needed:1;
927be08e 3233 unsigned int second_toc_pass:1;
67f0cbdb 3234 unsigned int do_toc_opt:1;
4c52953f 3235
9a23f96e
AM
3236 /* Set if tls optimization is enabled. */
3237 unsigned int do_tls_opt:1;
3238
3e04d765
AM
3239 /* Set if inline plt calls should be converted to direct calls. */
3240 unsigned int can_convert_all_inline_plt:1;
3241
5d1634d7 3242 /* Set on error. */
99877b66 3243 unsigned int stub_error:1;
721956f4 3244
8c5b4e52
AM
3245 /* Whether func_desc_adjust needs to be run over symbols. */
3246 unsigned int need_func_desc_adj:1;
721956f4 3247
f378ab09
AM
3248 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3249 unsigned int has_plt_localentry0:1;
3250
5663e321
AM
3251 /* Whether calls are made via the PLT from NOTOC functions. */
3252 unsigned int notoc_plt:1;
3253
e10a07b3
AM
3254 /* Whether any code linked seems to be Power10. */
3255 unsigned int has_power10_relocs:1;
3256
721956f4
AM
3257 /* Incremented every time we size stubs. */
3258 unsigned int stub_iteration;
65f38f15
AM
3259};
3260
4c52953f
AM
3261/* Rename some of the generic section flags to better document how they
3262 are used here. */
b0dddeec
AM
3263
3264/* Nonzero if this section has TLS related relocations. */
3265#define has_tls_reloc sec_flg0
3266
9737e8af
AM
3267/* Nonzero if this section has a call to __tls_get_addr lacking marker
3268 relocations. */
3269#define nomark_tls_get_addr sec_flg1
b0dddeec
AM
3270
3271/* Nonzero if this section has any toc or got relocs. */
3272#define has_toc_reloc sec_flg2
3273
3274/* Nonzero if this section has a call to another section that uses
3275 the toc or got. */
d77c8a4b 3276#define makes_toc_func_call sec_flg3
b0dddeec
AM
3277
3278/* Recursion protection when determining above flag. */
d77c8a4b 3279#define call_check_in_progress sec_flg4
70cc837d 3280#define call_check_done sec_flg5
4c52953f 3281
65f38f15
AM
3282/* Get the ppc64 ELF linker hash table from a link_info structure. */
3283
3284#define ppc_hash_table(p) \
573eb292
AM
3285 ((is_elf_hash_table ((p)->hash) \
3286 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3287 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
65f38f15 3288
721956f4
AM
3289#define ppc_stub_hash_lookup(table, string, create, copy) \
3290 ((struct ppc_stub_hash_entry *) \
3291 bfd_hash_lookup ((table), (string), (create), (copy)))
3292
3293#define ppc_branch_hash_lookup(table, string, create, copy) \
3294 ((struct ppc_branch_hash_entry *) \
3295 bfd_hash_lookup ((table), (string), (create), (copy)))
3296
3297/* Create an entry in the stub hash table. */
3298
3299static struct bfd_hash_entry *
4ce794b7
AM
3300stub_hash_newfunc (struct bfd_hash_entry *entry,
3301 struct bfd_hash_table *table,
3302 const char *string)
721956f4
AM
3303{
3304 /* Allocate the structure if it has not already been allocated by a
3305 subclass. */
3306 if (entry == NULL)
3307 {
3308 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3309 if (entry == NULL)
3310 return entry;
3311 }
3312
3313 /* Call the allocation method of the superclass. */
3314 entry = bfd_hash_newfunc (entry, table, string);
3315 if (entry != NULL)
3316 {
3317 struct ppc_stub_hash_entry *eh;
3318
3319 /* Initialize the local fields. */
3320 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3321 eh->stub_type = ppc_stub_none;
6f20ed8a 3322 eh->group = NULL;
721956f4
AM
3323 eh->stub_offset = 0;
3324 eh->target_value = 0;
3325 eh->target_section = NULL;
721956f4 3326 eh->h = NULL;
6911b7dc 3327 eh->plt_ent = NULL;
6911b7dc 3328 eh->other = 0;
721956f4
AM
3329 }
3330
3331 return entry;
3332}
3333
3334/* Create an entry in the branch hash table. */
3335
3336static struct bfd_hash_entry *
4ce794b7
AM
3337branch_hash_newfunc (struct bfd_hash_entry *entry,
3338 struct bfd_hash_table *table,
3339 const char *string)
721956f4
AM
3340{
3341 /* Allocate the structure if it has not already been allocated by a
3342 subclass. */
3343 if (entry == NULL)
3344 {
3345 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3346 if (entry == NULL)
3347 return entry;
3348 }
3349
3350 /* Call the allocation method of the superclass. */
3351 entry = bfd_hash_newfunc (entry, table, string);
3352 if (entry != NULL)
3353 {
3354 struct ppc_branch_hash_entry *eh;
3355
3356 /* Initialize the local fields. */
3357 eh = (struct ppc_branch_hash_entry *) entry;
3358 eh->offset = 0;
3359 eh->iter = 0;
3360 }
3361
3362 return entry;
3363}
3364
65f38f15
AM
3365/* Create an entry in a ppc64 ELF linker hash table. */
3366
3367static struct bfd_hash_entry *
4ce794b7
AM
3368link_hash_newfunc (struct bfd_hash_entry *entry,
3369 struct bfd_hash_table *table,
3370 const char *string)
65f38f15
AM
3371{
3372 /* Allocate the structure if it has not already been allocated by a
3373 subclass. */
3374 if (entry == NULL)
3375 {
3376 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3377 if (entry == NULL)
3378 return entry;
3379 }
3380
3381 /* Call the allocation method of the superclass. */
3382 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3383 if (entry != NULL)
3384 {
3385 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3386
b3fac117 3387 memset (&eh->u.stub_cache, 0,
908b32fc 3388 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3389 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3390
3391 /* When making function calls, old ABI code references function entry
3392 points (dot symbols), while new ABI code references the function
3393 descriptor symbol. We need to make any combination of reference and
3394 definition work together, without breaking archive linking.
3395
3396 For a defined function "foo" and an undefined call to "bar":
3397 An old object defines "foo" and ".foo", references ".bar" (possibly
3398 "bar" too).
3399 A new object defines "foo" and references "bar".
3400
3401 A new object thus has no problem with its undefined symbols being
3402 satisfied by definitions in an old object. On the other hand, the
3403 old object won't have ".bar" satisfied by a new object.
3404
3405 Keep a list of newly added dot-symbols. */
3406
3407 if (string[0] == '.')
3408 {
3409 struct ppc_link_hash_table *htab;
3410
3411 htab = (struct ppc_link_hash_table *) table;
3412 eh->u.next_dot_sym = htab->dot_syms;
3413 htab->dot_syms = eh;
3414 }
65f38f15
AM
3415 }
3416
3417 return entry;
3418}
3419
2cdcc330
AM
3420struct tocsave_entry
3421{
3b421ab3
AM
3422 asection *sec;
3423 bfd_vma offset;
3424};
3425
3426static hashval_t
3427tocsave_htab_hash (const void *p)
3428{
3429 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3430 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3431}
3432
3433static int
3434tocsave_htab_eq (const void *p1, const void *p2)
3435{
3436 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3437 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3438 return e1->sec == e2->sec && e1->offset == e2->offset;
3439}
3440
68faa637
AM
3441/* Destroy a ppc64 ELF linker hash table. */
3442
3443static void
d495ab0d 3444ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3445{
d495ab0d 3446 struct ppc_link_hash_table *htab;
68faa637 3447
d495ab0d 3448 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3449 if (htab->tocsave_htab)
3450 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3451 bfd_hash_table_free (&htab->branch_hash_table);
3452 bfd_hash_table_free (&htab->stub_hash_table);
3453 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3454}
3455
65f38f15
AM
3456/* Create a ppc64 ELF linker hash table. */
3457
3458static struct bfd_link_hash_table *
4ce794b7 3459ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3460{
3461 struct ppc_link_hash_table *htab;
986f0783 3462 size_t amt = sizeof (struct ppc_link_hash_table);
65f38f15 3463
4ce794b7 3464 htab = bfd_zmalloc (amt);
65f38f15
AM
3465 if (htab == NULL)
3466 return NULL;
3467
66eb6687 3468 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3469 sizeof (struct ppc_link_hash_entry),
3470 PPC64_ELF_DATA))
65f38f15 3471 {
e2d34d7d 3472 free (htab);
65f38f15
AM
3473 return NULL;
3474 }
3475
721956f4 3476 /* Init the stub hash table too. */
66eb6687
AM
3477 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3478 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3479 {
d495ab0d 3480 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3481 return NULL;
3482 }
721956f4
AM
3483
3484 /* And the branch hash table. */
66eb6687
AM
3485 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3486 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3487 {
3488 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3489 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3490 return NULL;
3491 }
721956f4 3492
3b421ab3
AM
3493 htab->tocsave_htab = htab_try_create (1024,
3494 tocsave_htab_hash,
3495 tocsave_htab_eq,
3496 NULL);
3497 if (htab->tocsave_htab == NULL)
2915c55b 3498 {
d495ab0d 3499 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3500 return NULL;
3501 }
d495ab0d 3502 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3503
3254fd24
AM
3504 /* Initializing two fields of the union is just cosmetic. We really
3505 only care about glist, but when compiled on a 32-bit host the
3506 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3507 debugger inspection of these fields look nicer. */
a6aa5195
AM
3508 htab->elf.init_got_refcount.refcount = 0;
3509 htab->elf.init_got_refcount.glist = NULL;
3510 htab->elf.init_plt_refcount.refcount = 0;
3511 htab->elf.init_plt_refcount.glist = NULL;
3512 htab->elf.init_got_offset.offset = 0;
3513 htab->elf.init_got_offset.glist = NULL;
3514 htab->elf.init_plt_offset.offset = 0;
3515 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3516
65f38f15
AM
3517 return &htab->elf.root;
3518}
3519
bfeb4a28
AM
3520/* Create sections for linker generated code. */
3521
3522static bfd_boolean
3523create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3524{
3525 struct ppc_link_hash_table *htab;
3526 flagword flags;
3527
3528 htab = ppc_hash_table (info);
3529
bfeb4a28
AM
3530 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3531 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3532 if (htab->params->save_restore_funcs)
3533 {
3534 /* Create .sfpr for code to save and restore fp regs. */
3535 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3536 flags);
3537 if (htab->sfpr == NULL
fd361982 3538 || !bfd_set_section_alignment (htab->sfpr, 2))
b32547cd
AM
3539 return FALSE;
3540 }
3541
3542 if (bfd_link_relocatable (info))
3543 return TRUE;
bfeb4a28
AM
3544
3545 /* Create .glink for lazy dynamic linking support. */
3546 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3547 flags);
3548 if (htab->glink == NULL
fd361982 3549 || !bfd_set_section_alignment (htab->glink, 3))
bfeb4a28
AM
3550 return FALSE;
3551
9e390558
AM
3552 /* The part of .glink used by global entry stubs, separate so that
3553 it can be aligned appropriately without affecting htab->glink. */
3554 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3555 flags);
3556 if (htab->global_entry == NULL
fd361982 3557 || !bfd_set_section_alignment (htab->global_entry, 2))
9e390558
AM
3558 return FALSE;
3559
bfeb4a28
AM
3560 if (!info->no_ld_generated_unwind_info)
3561 {
3562 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3563 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3564 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3565 ".eh_frame",
3566 flags);
3567 if (htab->glink_eh_frame == NULL
fd361982 3568 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
bfeb4a28
AM
3569 return FALSE;
3570 }
3571
3572 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3573 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3574 if (htab->elf.iplt == NULL
fd361982 3575 || !bfd_set_section_alignment (htab->elf.iplt, 3))
bfeb4a28
AM
3576 return FALSE;
3577
3578 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3579 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3580 htab->elf.irelplt
3581 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3582 if (htab->elf.irelplt == NULL
fd361982 3583 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
bfeb4a28
AM
3584 return FALSE;
3585
3586 /* Create branch lookup table for plt_branch stubs. */
3587 flags = (SEC_ALLOC | SEC_LOAD
3588 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3589 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3590 flags);
3591 if (htab->brlt == NULL
fd361982 3592 || !bfd_set_section_alignment (htab->brlt, 3))
bfeb4a28
AM
3593 return FALSE;
3594
2d7ad24e
AM
3595 /* Local plt entries, put in .branch_lt but a separate section for
3596 convenience. */
3597 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3598 flags);
3599 if (htab->pltlocal == NULL
fd361982 3600 || !bfd_set_section_alignment (htab->pltlocal, 3))
2d7ad24e
AM
3601 return FALSE;
3602
0e1862bb 3603 if (!bfd_link_pic (info))
bfeb4a28
AM
3604 return TRUE;
3605
3606 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3607 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3608 htab->relbrlt
3609 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3610 if (htab->relbrlt == NULL
fd361982 3611 || !bfd_set_section_alignment (htab->relbrlt, 3))
bfeb4a28
AM
3612 return FALSE;
3613
2d7ad24e
AM
3614 htab->relpltlocal
3615 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3616 if (htab->relpltlocal == NULL
fd361982 3617 || !bfd_set_section_alignment (htab->relpltlocal, 3))
2d7ad24e
AM
3618 return FALSE;
3619
bfeb4a28
AM
3620 return TRUE;
3621}
3622
e717da7e
AM
3623/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3624
bfeb4a28 3625bfd_boolean
e7d1c40c
AM
3626ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3627 struct ppc64_elf_params *params)
e717da7e
AM
3628{
3629 struct ppc_link_hash_table *htab;
3630
e7d1c40c 3631 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3632
3633/* Always hook our dynamic sections into the first bfd, which is the
3634 linker created stub bfd. This ensures that the GOT header is at
3635 the start of the output TOC section. */
3636 htab = ppc_hash_table (info);
e7d1c40c
AM
3637 htab->elf.dynobj = params->stub_bfd;
3638 htab->params = params;
bfeb4a28 3639
bfeb4a28 3640 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3641}
3642
721956f4
AM
3643/* Build a name for an entry in the stub hash table. */
3644
3645static char *
4ce794b7
AM
3646ppc_stub_name (const asection *input_section,
3647 const asection *sym_sec,
3648 const struct ppc_link_hash_entry *h,
3649 const Elf_Internal_Rela *rel)
721956f4
AM
3650{
3651 char *stub_name;
bcaa2f82 3652 ssize_t len;
721956f4
AM
3653
3654 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3655 offsets from a sym as a branch target? In fact, we could
3656 probably assume the addend is always zero. */
3657 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3658
3659 if (h)
3660 {
3661 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3662 stub_name = bfd_malloc (len);
46de2a7c
AM
3663 if (stub_name == NULL)
3664 return stub_name;
3665
bcaa2f82
AM
3666 len = sprintf (stub_name, "%08x.%s+%x",
3667 input_section->id & 0xffffffff,
3668 h->elf.root.root.string,
3669 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3670 }
3671 else
3672 {
ad8e1ba5 3673 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3674 stub_name = bfd_malloc (len);
46de2a7c
AM
3675 if (stub_name == NULL)
3676 return stub_name;
3677
bcaa2f82
AM
3678 len = sprintf (stub_name, "%08x.%x:%x+%x",
3679 input_section->id & 0xffffffff,
3680 sym_sec->id & 0xffffffff,
3681 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3682 (int) rel->r_addend & 0xffffffff);
721956f4 3683 }
bcaa2f82 3684 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3685 stub_name[len - 2] = 0;
721956f4
AM
3686 return stub_name;
3687}
3688
e10a07b3
AM
3689/* If mixing power10 with non-power10 code and --power10-stubs is not
3690 specified (or is auto) then calls using @notoc relocations that
3691 need a stub will utilize power10 instructions in the stub, and
3692 calls without @notoc relocations will not use power10 instructions.
3693 The two classes of stubs are stored in separate stub_hash_table
3694 entries having the same key string. The two entries will always be
3695 adjacent on entry->root.next chain, even if hash table resizing
3696 occurs. This function selects the correct entry to use. */
3697
3698static struct ppc_stub_hash_entry *
3699select_alt_stub (struct ppc_stub_hash_entry *entry, bfd_boolean notoc)
3700{
3701 bfd_boolean have_notoc;
3702
3703 have_notoc = (entry->stub_type == ppc_stub_plt_call_notoc
3704 || entry->stub_type == ppc_stub_plt_branch_notoc
3705 || entry->stub_type == ppc_stub_long_branch_notoc);
3706
3707 if (have_notoc != notoc)
3708 {
3709 const char *stub_name = entry->root.string;
3710
3711 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3712 if (entry != NULL
3713 && entry->root.string != stub_name)
3714 entry = NULL;
3715 }
3716
3717 return entry;
3718}
3719
721956f4
AM
3720/* Look up an entry in the stub hash. Stub entries are cached because
3721 creating the stub name takes a bit of time. */
3722
3723static struct ppc_stub_hash_entry *
4ce794b7
AM
3724ppc_get_stub_entry (const asection *input_section,
3725 const asection *sym_sec,
039b3fef 3726 struct ppc_link_hash_entry *h,
4ce794b7
AM
3727 const Elf_Internal_Rela *rel,
3728 struct ppc_link_hash_table *htab)
721956f4
AM
3729{
3730 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3731 struct map_stub *group;
721956f4
AM
3732
3733 /* If this input section is part of a group of sections sharing one
3734 stub section, then use the id of the first section in the group.
3735 Stub names need to include a section id, as there may well be
3736 more than one stub used to reach say, printf, and we need to
3737 distinguish between them. */
6f20ed8a 3738 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3739 if (group == NULL)
3740 return NULL;
721956f4 3741
b3fac117
AM
3742 if (h != NULL && h->u.stub_cache != NULL
3743 && h->u.stub_cache->h == h
6f20ed8a 3744 && h->u.stub_cache->group == group)
721956f4 3745 {
b3fac117 3746 stub_entry = h->u.stub_cache;
721956f4
AM
3747 }
3748 else
3749 {
3750 char *stub_name;
3751
6f20ed8a 3752 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3753 if (stub_name == NULL)
3754 return NULL;
3755
3756 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3757 stub_name, FALSE, FALSE);
721956f4 3758 if (h != NULL)
b3fac117 3759 h->u.stub_cache = stub_entry;
721956f4
AM
3760
3761 free (stub_name);
3762 }
3763
e10a07b3
AM
3764 if (stub_entry != NULL && htab->params->power10_stubs == -1)
3765 {
3766 bfd_boolean notoc = ELF64_R_TYPE (rel->r_info) == R_PPC64_REL24_NOTOC;
3767
3768 stub_entry = select_alt_stub (stub_entry, notoc);
3769 }
3770
721956f4
AM
3771 return stub_entry;
3772}
3773
3774/* Add a new stub entry to the stub hash. Not all fields of the new
3775 stub entry are initialised. */
3776
3777static struct ppc_stub_hash_entry *
4ce794b7
AM
3778ppc_add_stub (const char *stub_name,
3779 asection *section,
25f53a85 3780 struct bfd_link_info *info)
721956f4 3781{
25f53a85 3782 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3783 struct map_stub *group;
721956f4
AM
3784 asection *link_sec;
3785 asection *stub_sec;
3786 struct ppc_stub_hash_entry *stub_entry;
3787
6f20ed8a
AM
3788 group = htab->sec_info[section->id].u.group;
3789 link_sec = group->link_sec;
3790 stub_sec = group->stub_sec;
721956f4
AM
3791 if (stub_sec == NULL)
3792 {
6f20ed8a
AM
3793 size_t namelen;
3794 bfd_size_type len;
3795 char *s_name;
721956f4 3796
6f20ed8a
AM
3797 namelen = strlen (link_sec->name);
3798 len = namelen + sizeof (STUB_SUFFIX);
3799 s_name = bfd_alloc (htab->params->stub_bfd, len);
3800 if (s_name == NULL)
3801 return NULL;
721956f4 3802
6f20ed8a
AM
3803 memcpy (s_name, link_sec->name, namelen);
3804 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3805 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3806 if (stub_sec == NULL)
3807 return NULL;
3808 group->stub_sec = stub_sec;
721956f4
AM
3809 }
3810
3811 /* Enter this entry into the linker stub hash table. */
3812 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3813 TRUE, FALSE);
721956f4
AM
3814 if (stub_entry == NULL)
3815 {
695344c0 3816 /* xgettext:c-format */
cf97bcb0
AM
3817 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3818 section->owner, stub_name);
721956f4
AM
3819 return NULL;
3820 }
3821
6f20ed8a 3822 stub_entry->group = group;
721956f4 3823 stub_entry->stub_offset = 0;
721956f4
AM
3824 return stub_entry;
3825}
3826
e717da7e
AM
3827/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3828 not already done. */
65f38f15 3829
b34976b6 3830static bfd_boolean
e717da7e 3831create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3832{
e717da7e
AM
3833 asection *got, *relgot;
3834 flagword flags;
3835 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3836
0c8d6e5c 3837 if (!is_ppc64_elf (abfd))
0ffa91dd 3838 return FALSE;
4dfe6ac6
NC
3839 if (htab == NULL)
3840 return FALSE;
0ffa91dd 3841
33e44f2e
AM
3842 if (!htab->elf.sgot
3843 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3844 return FALSE;
e717da7e
AM
3845
3846 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3847 | SEC_LINKER_CREATED);
3848
c456f082 3849 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3850 if (!got
fd361982 3851 || !bfd_set_section_alignment (got, 3))
e717da7e 3852 return FALSE;
65f38f15 3853
c456f082
AM
3854 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3855 flags | SEC_READONLY);
e717da7e 3856 if (!relgot
fd361982 3857 || !bfd_set_section_alignment (relgot, 3))
b34976b6 3858 return FALSE;
e717da7e
AM
3859
3860 ppc64_elf_tdata (abfd)->got = got;
3861 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 3862 return TRUE;
65f38f15 3863}
5bd4f169 3864
b31867b6
AM
3865/* Follow indirect and warning symbol links. */
3866
3867static inline struct bfd_link_hash_entry *
3868follow_link (struct bfd_link_hash_entry *h)
3869{
3870 while (h->type == bfd_link_hash_indirect
3871 || h->type == bfd_link_hash_warning)
3872 h = h->u.i.link;
3873 return h;
3874}
3875
3876static inline struct elf_link_hash_entry *
3877elf_follow_link (struct elf_link_hash_entry *h)
3878{
3879 return (struct elf_link_hash_entry *) follow_link (&h->root);
3880}
3881
3882static inline struct ppc_link_hash_entry *
3883ppc_follow_link (struct ppc_link_hash_entry *h)
3884{
ed7007c1 3885 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
b31867b6
AM
3886}
3887
40d16e0b
AM
3888/* Merge PLT info on FROM with that on TO. */
3889
3890static void
3891move_plt_plist (struct ppc_link_hash_entry *from,
3892 struct ppc_link_hash_entry *to)
3893{
3894 if (from->elf.plt.plist != NULL)
3895 {
3896 if (to->elf.plt.plist != NULL)
3897 {
3898 struct plt_entry **entp;
3899 struct plt_entry *ent;
3900
3901 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3902 {
3903 struct plt_entry *dent;
3904
3905 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3906 if (dent->addend == ent->addend)
3907 {
3908 dent->plt.refcount += ent->plt.refcount;
3909 *entp = ent->next;
3910 break;
3911 }
3912 if (dent == NULL)
3913 entp = &ent->next;
3914 }
3915 *entp = to->elf.plt.plist;
3916 }
3917
3918 to->elf.plt.plist = from->elf.plt.plist;
3919 from->elf.plt.plist = NULL;
3920 }
3921}
3922
65f38f15
AM
3923/* Copy the extra info we tack onto an elf_link_hash_entry. */
3924
3925static void
fcfa13d2
AM
3926ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3927 struct elf_link_hash_entry *dir,
3928 struct elf_link_hash_entry *ind)
65f38f15
AM
3929{
3930 struct ppc_link_hash_entry *edir, *eind;
3931
ed7007c1
AM
3932 edir = ppc_elf_hash_entry (dir);
3933 eind = ppc_elf_hash_entry (ind);
65f38f15 3934
c79d6685
AM
3935 edir->is_func |= eind->is_func;
3936 edir->is_func_descriptor |= eind->is_func_descriptor;
3937 edir->tls_mask |= eind->tls_mask;
3938 if (eind->oh != NULL)
3939 edir->oh = ppc_follow_link (eind->oh);
3940
474436e6 3941 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
3942 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3943 edir->elf.ref_regular |= eind->elf.ref_regular;
3944 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 3945 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
3946 edir->elf.needs_plt |= eind->elf.needs_plt;
3947 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 3948
d311bc8b
AM
3949 /* If we were called to copy over info for a weak sym, don't copy
3950 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3951 in order to simplify readonly_dynrelocs and save a field in the
3952 symbol hash entry, but that means dyn_relocs can't be used in any
3953 tests about a specific symbol, or affect other symbol flags which
ab2477e1 3954 are then tested. */
d311bc8b 3955 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 3956 return;
d311bc8b 3957
411e1bfb 3958 /* Copy over any dynamic relocs we may have on the indirect sym. */
190eb1dd 3959 if (ind->dyn_relocs != NULL)
65f38f15 3960 {
190eb1dd 3961 if (dir->dyn_relocs != NULL)
bbd7ec4a 3962 {
6061a67d
AM
3963 struct elf_dyn_relocs **pp;
3964 struct elf_dyn_relocs *p;
bbd7ec4a 3965
fcfa13d2 3966 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a 3967 list. Merge any entries against the same section. */
190eb1dd 3968 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
bbd7ec4a 3969 {
6061a67d 3970 struct elf_dyn_relocs *q;
bbd7ec4a 3971
190eb1dd 3972 for (q = dir->dyn_relocs; q != NULL; q = q->next)
bbd7ec4a
AM
3973 if (q->sec == p->sec)
3974 {
3975 q->pc_count += p->pc_count;
3976 q->count += p->count;
3977 *pp = p->next;
3978 break;
3979 }
3980 if (q == NULL)
3981 pp = &p->next;
3982 }
190eb1dd 3983 *pp = dir->dyn_relocs;
bbd7ec4a
AM
3984 }
3985
190eb1dd
L
3986 dir->dyn_relocs = ind->dyn_relocs;
3987 ind->dyn_relocs = NULL;
65f38f15 3988 }
65f38f15 3989
81848ca0
AM
3990 /* Copy over got entries that we may have already seen to the
3991 symbol which just became indirect. */
411e1bfb
AM
3992 if (eind->elf.got.glist != NULL)
3993 {
3994 if (edir->elf.got.glist != NULL)
3995 {
3996 struct got_entry **entp;
3997 struct got_entry *ent;
3998
3999 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4000 {
4001 struct got_entry *dent;
4002
4003 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4004 if (dent->addend == ent->addend
e717da7e 4005 && dent->owner == ent->owner
411e1bfb
AM
4006 && dent->tls_type == ent->tls_type)
4007 {
4008 dent->got.refcount += ent->got.refcount;
4009 *entp = ent->next;
4010 break;
4011 }
4012 if (dent == NULL)
4013 entp = &ent->next;
4014 }
4015 *entp = edir->elf.got.glist;
4016 }
4017
4018 edir->elf.got.glist = eind->elf.got.glist;
4019 eind->elf.got.glist = NULL;
4020 }
4021
4022 /* And plt entries. */
40d16e0b 4023 move_plt_plist (eind, edir);
411e1bfb 4024
fcfa13d2 4025 if (eind->elf.dynindx != -1)
411e1bfb 4026 {
fcfa13d2
AM
4027 if (edir->elf.dynindx != -1)
4028 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4029 edir->elf.dynstr_index);
411e1bfb
AM
4030 edir->elf.dynindx = eind->elf.dynindx;
4031 edir->elf.dynstr_index = eind->elf.dynstr_index;
4032 eind->elf.dynindx = -1;
4033 eind->elf.dynstr_index = 0;
4034 }
411e1bfb
AM
4035}
4036
8387904d
AM
4037/* Find the function descriptor hash entry from the given function code
4038 hash entry FH. Link the entries via their OH fields. */
4039
4040static struct ppc_link_hash_entry *
b31867b6 4041lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4042{
4043 struct ppc_link_hash_entry *fdh = fh->oh;
4044
4045 if (fdh == NULL)
4046 {
4047 const char *fd_name = fh->elf.root.root.string + 1;
4048
ed7007c1
AM
4049 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4050 FALSE, FALSE, FALSE));
b31867b6
AM
4051 if (fdh == NULL)
4052 return fdh;
4053
4054 fdh->is_func_descriptor = 1;
4055 fdh->oh = fh;
4056 fh->is_func = 1;
4057 fh->oh = fdh;
8387904d
AM
4058 }
4059
8c5b4e52
AM
4060 fdh = ppc_follow_link (fdh);
4061 fdh->is_func_descriptor = 1;
4062 fdh->oh = fh;
4063 return fdh;
8387904d
AM
4064}
4065
8c5b4e52 4066/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4067
4068static struct ppc_link_hash_entry *
4069make_fdh (struct bfd_link_info *info,
908b32fc 4070 struct ppc_link_hash_entry *fh)
bb700d78 4071{
8c5b4e52
AM
4072 bfd *abfd = fh->elf.root.u.undef.abfd;
4073 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4074 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4075 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4076 ? BSF_WEAK
4077 : BSF_GLOBAL);
4078
4079 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4080 fh->elf.root.root.string + 1,
4081 flags, bfd_und_section_ptr, 0,
4082 NULL, FALSE, FALSE, &bh))
bb700d78
AM
4083 return NULL;
4084
4085 fdh = (struct ppc_link_hash_entry *) bh;
4086 fdh->elf.non_elf = 0;
908b32fc
AM
4087 fdh->fake = 1;
4088 fdh->is_func_descriptor = 1;
4089 fdh->oh = fh;
4090 fh->is_func = 1;
4091 fh->oh = fdh;
bb700d78
AM
4092 return fdh;
4093}
4094
8387904d
AM
4095/* Fix function descriptor symbols defined in .opd sections to be
4096 function type. */
555cd476
AM
4097
4098static bfd_boolean
c16153ae 4099ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4100 struct bfd_link_info *info,
555cd476 4101 Elf_Internal_Sym *isym,
6911b7dc 4102 const char **name,
555cd476
AM
4103 flagword *flags ATTRIBUTE_UNUSED,
4104 asection **sec,
b53dfeb2 4105 bfd_vma *value)
555cd476 4106{
b53dfeb2 4107 if (*sec != NULL
f1885d1e 4108 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4109 {
4110 asection *code_sec;
4111
4112 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4113 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4114 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4115
4116 /* If the symbol is a function defined in .opd, and the function
4117 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4118 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4119 && (*sec)->reloc_count != 0
4120 && opd_entry_value (*sec, *value, &code_sec, NULL,
4121 FALSE) != (bfd_vma) -1
4122 && discarded_section (code_sec))
4123 {
4124 *sec = bfd_und_section_ptr;
4125 isym->st_shndx = SHN_UNDEF;
4126 }
4127 }
dbd1e97e
AM
4128 else if (*sec != NULL
4129 && strcmp ((*sec)->name, ".toc") == 0
4130 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4131 {
4132 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4133 if (htab != NULL)
4134 htab->params->object_in_toc = 1;
4135 }
433817dd 4136
6911b7dc
AM
4137 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4138 {
4139 if (abiversion (ibfd) == 0)
4140 set_abiversion (ibfd, 2);
4141 else if (abiversion (ibfd) == 1)
4142 {
cf97bcb0
AM
4143 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4144 " for ABI version 1"), *name);
6911b7dc
AM
4145 bfd_set_error (bfd_error_bad_value);
4146 return FALSE;
4147 }
4148 }
4149
555cd476
AM
4150 return TRUE;
4151}
4152
6911b7dc
AM
4153/* Merge non-visibility st_other attributes: local entry point. */
4154
4155static void
4156ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5160d0f3 4157 unsigned int st_other,
6911b7dc
AM
4158 bfd_boolean definition,
4159 bfd_boolean dynamic)
4160{
f378ab09 4161 if (definition && (!dynamic || !h->def_regular))
5160d0f3 4162 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
6911b7dc
AM
4163 | ELF_ST_VISIBILITY (h->other));
4164}
4165
8c5b4e52
AM
4166/* Hook called on merging a symbol. We use this to clear "fake" since
4167 we now have a real symbol. */
4168
4169static bfd_boolean
4170ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4171 const Elf_Internal_Sym *isym,
8c5b4e52
AM
4172 asection **psec ATTRIBUTE_UNUSED,
4173 bfd_boolean newdef ATTRIBUTE_UNUSED,
4174 bfd_boolean olddef ATTRIBUTE_UNUSED,
4175 bfd *oldbfd ATTRIBUTE_UNUSED,
4176 const asection *oldsec ATTRIBUTE_UNUSED)
4177{
ed7007c1 4178 ppc_elf_hash_entry (h)->fake = 0;
8b5f1ed8 4179 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
ed7007c1 4180 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
8c5b4e52
AM
4181 return TRUE;
4182}
4183
8387904d 4184/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4185 inclusion of a new ABI object archive that defines "bar".
4186 NAME is a symbol defined in an archive. Return a symbol in the hash
4187 table that might be satisfied by the archive symbols. */
8387904d
AM
4188
4189static struct elf_link_hash_entry *
4190ppc64_elf_archive_symbol_lookup (bfd *abfd,
4191 struct bfd_link_info *info,
4192 const char *name)
4193{
4194 struct elf_link_hash_entry *h;
4195 char *dot_name;
4196 size_t len;
4197
4198 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4199 if (h != NULL
4200 /* Don't return this sym if it is a fake function descriptor
4201 created by add_symbol_adjust. */
ed7007c1 4202 && !ppc_elf_hash_entry (h)->fake)
8387904d
AM
4203 return h;
4204
4205 if (name[0] == '.')
4206 return h;
4207
4208 len = strlen (name);
4209 dot_name = bfd_alloc (abfd, len + 2);
4210 if (dot_name == NULL)
e99955cd 4211 return (struct elf_link_hash_entry *) -1;
8387904d
AM
4212 dot_name[0] = '.';
4213 memcpy (dot_name + 1, name, len + 1);
4214 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4215 bfd_release (abfd, dot_name);
a804e476
AM
4216 if (h != NULL)
4217 return h;
4218
4219 if (strcmp (name, "__tls_get_addr_opt") == 0)
4220 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
8387904d
AM
4221 return h;
4222}
4223
4224/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4225 new ABI object defines "bar". Well, at least, undefined dot symbols
4226 are made weak. This stops later archive searches from including an
4227 object if we already have a function descriptor definition. It also
35b0ce59
AM
4228 prevents the linker complaining about undefined symbols.
4229 We also check and correct mismatched symbol visibility here. The
4230 most restrictive visibility of the function descriptor and the
4231 function entry symbol is used. */
8387904d
AM
4232
4233static bfd_boolean
b3fac117 4234add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4235{
8387904d
AM
4236 struct ppc_link_hash_table *htab;
4237 struct ppc_link_hash_entry *fdh;
4238
b3fac117
AM
4239 if (eh->elf.root.type == bfd_link_hash_warning)
4240 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4241
8400d40d
AM
4242 if (eh->elf.root.type == bfd_link_hash_indirect)
4243 return TRUE;
4244
b3fac117
AM
4245 if (eh->elf.root.root.string[0] != '.')
4246 abort ();
8387904d 4247
b3fac117 4248 htab = ppc_hash_table (info);
4dfe6ac6
NC
4249 if (htab == NULL)
4250 return FALSE;
4251
b31867b6 4252 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4253 if (fdh == NULL
4254 && !bfd_link_relocatable (info)
4255 && (eh->elf.root.type == bfd_link_hash_undefined
4256 || eh->elf.root.type == bfd_link_hash_undefweak)
4257 && eh->elf.ref_regular)
4258 {
4259 /* Make an undefined function descriptor sym, in order to
4260 pull in an --as-needed shared lib. Archives are handled
4261 elsewhere. */
4262 fdh = make_fdh (info, eh);
4263 if (fdh == NULL)
4264 return FALSE;
bb700d78 4265 }
8c5b4e52
AM
4266
4267 if (fdh != NULL)
8387904d 4268 {
35b0ce59
AM
4269 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4270 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4271
4272 /* Make both descriptor and entry symbol have the most
4273 constraining visibility of either symbol. */
35b0ce59
AM
4274 if (entry_vis < descr_vis)
4275 fdh->elf.other += entry_vis - descr_vis;
4276 else if (entry_vis > descr_vis)
4277 eh->elf.other += descr_vis - entry_vis;
4278
8c5b4e52
AM
4279 /* Propagate reference flags from entry symbol to function
4280 descriptor symbol. */
bc4e12de 4281 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4282 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4283 fdh->elf.ref_regular |= eh->elf.ref_regular;
4284 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4285
4286 if (!fdh->elf.forced_local
4287 && fdh->elf.dynindx == -1
4288 && fdh->elf.versioned != versioned_hidden
4289 && (bfd_link_dll (info)
4290 || fdh->elf.def_dynamic
4291 || fdh->elf.ref_dynamic)
4292 && (eh->elf.ref_regular
4293 || eh->elf.def_regular))
4294 {
2cdcc330 4295 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
8c5b4e52 4296 return FALSE;
35b0ce59 4297 }
8387904d 4298 }
99877b66 4299
8387904d
AM
4300 return TRUE;
4301}
4302
f6c7c3e8
AM
4303/* Set up opd section info and abiversion for IBFD, and process list
4304 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4305
8387904d 4306static bfd_boolean
f6c7c3e8 4307ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4308{
99877b66 4309 struct ppc_link_hash_table *htab;
b3fac117 4310 struct ppc_link_hash_entry **p, *eh;
459609d6 4311 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4312
459609d6 4313 if (opd != NULL && opd->size != 0)
b3fac117 4314 {
b9399fcf
AM
4315 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4316 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4317
459609d6
AM
4318 if (abiversion (ibfd) == 0)
4319 set_abiversion (ibfd, 1);
8a2058b5 4320 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4321 {
695344c0 4322 /* xgettext:c-format */
cf97bcb0
AM
4323 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4324 ibfd, abiversion (ibfd));
459609d6
AM
4325 bfd_set_error (bfd_error_bad_value);
4326 return FALSE;
f6c7c3e8 4327 }
b9399fcf 4328 }
f6c7c3e8 4329
b9399fcf
AM
4330 if (is_ppc64_elf (info->output_bfd))
4331 {
4332 /* For input files without an explicit abiversion in e_flags
4333 we should have flagged any with symbol st_other bits set
4334 as ELFv1 and above flagged those with .opd as ELFv2.
4335 Set the output abiversion if not yet set, and for any input
4336 still ambiguous, take its abiversion from the output.
4337 Differences in ABI are reported later. */
4338 if (abiversion (info->output_bfd) == 0)
4339 set_abiversion (info->output_bfd, abiversion (ibfd));
4340 else if (abiversion (ibfd) == 0)
4341 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4342 }
4343
459609d6
AM
4344 htab = ppc_hash_table (info);
4345 if (htab == NULL)
b9399fcf 4346 return TRUE;
459609d6 4347
b9399fcf
AM
4348 if (opd != NULL && opd->size != 0
4349 && (ibfd->flags & DYNAMIC) == 0
4350 && (opd->flags & SEC_RELOC) != 0
4351 && opd->reloc_count != 0
4352 && !bfd_is_abs_section (opd->output_section)
4353 && info->gc_sections)
4354 {
4355 /* Garbage collection needs some extra help with .opd sections.
4356 We don't want to necessarily keep everything referenced by
4357 relocs in .opd, as that would keep all functions. Instead,
4358 if we reference an .opd symbol (a function descriptor), we
4359 want to keep the function code symbol's section. This is
4360 easy for global symbols, but for local syms we need to keep
4361 information about the associated function section. */
4362 bfd_size_type amt;
4363 asection **opd_sym_map;
4364 Elf_Internal_Shdr *symtab_hdr;
4365 Elf_Internal_Rela *relocs, *rel_end, *rel;
4366
4367 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4368 opd_sym_map = bfd_zalloc (ibfd, amt);
4369 if (opd_sym_map == NULL)
4370 return FALSE;
4371 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4372 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4373 info->keep_memory);
4374 if (relocs == NULL)
4375 return FALSE;
4376 symtab_hdr = &elf_symtab_hdr (ibfd);
4377 rel_end = relocs + opd->reloc_count - 1;
4378 for (rel = relocs; rel < rel_end; rel++)
4379 {
4380 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4381 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4382
4383 if (r_type == R_PPC64_ADDR64
4384 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4385 && r_symndx < symtab_hdr->sh_info)
4386 {
4387 Elf_Internal_Sym *isym;
4388 asection *s;
4389
f1dfbfdb
L
4390 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4391 r_symndx);
b9399fcf
AM
4392 if (isym == NULL)
4393 {
4394 if (elf_section_data (opd)->relocs != relocs)
4395 free (relocs);
4396 return FALSE;
4397 }
4398
4399 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4400 if (s != NULL && s != opd)
4401 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4402 }
4403 }
4404 if (elf_section_data (opd)->relocs != relocs)
4405 free (relocs);
4406 }
459609d6
AM
4407
4408 p = &htab->dot_syms;
4409 while ((eh = *p) != NULL)
4410 {
4411 *p = NULL;
4412 if (&eh->elf == htab->elf.hgot)
4413 ;
4414 else if (htab->elf.hgot == NULL
4415 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4416 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4417 else if (abiversion (ibfd) <= 1)
4418 {
4419 htab->need_func_desc_adj = 1;
4420 if (!add_symbol_adjust (eh, info))
4421 return FALSE;
4422 }
459609d6
AM
4423 p = &eh->u.next_dot_sym;
4424 }
b3fac117 4425 return TRUE;
8387904d
AM
4426}
4427
97fed1c9
JJ
4428/* Undo hash table changes when an --as-needed input file is determined
4429 not to be needed. */
4430
4431static bfd_boolean
e5034e59
AM
4432ppc64_elf_notice_as_needed (bfd *ibfd,
4433 struct bfd_link_info *info,
4434 enum notice_asneeded_action act)
97fed1c9 4435{
e5034e59
AM
4436 if (act == notice_not_needed)
4437 {
4438 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4439
e5034e59
AM
4440 if (htab == NULL)
4441 return FALSE;
4dfe6ac6 4442
e5034e59
AM
4443 htab->dot_syms = NULL;
4444 }
4445 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4446}
4447
aa374f67
AM
4448/* If --just-symbols against a final linked binary, then assume we need
4449 toc adjusting stubs when calling functions defined there. */
4450
4451static void
4452ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4453{
4454 if ((sec->flags & SEC_CODE) != 0
4455 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4456 && is_ppc64_elf (sec->owner))
4457 {
2c3f079f
AM
4458 if (abiversion (sec->owner) >= 2
4459 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4460 sec->has_toc_reloc = 1;
4461 }
4462 _bfd_elf_link_just_syms (sec, info);
4463}
4464
e054468f 4465static struct plt_entry **
4ce794b7
AM
4466update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4467 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4468{
4469 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4470 struct plt_entry **local_plt;
f961d9dd 4471 unsigned char *local_got_tls_masks;
411e1bfb
AM
4472
4473 if (local_got_ents == NULL)
4474 {
4475 bfd_size_type size = symtab_hdr->sh_info;
4476
e054468f
AM
4477 size *= (sizeof (*local_got_ents)
4478 + sizeof (*local_plt)
4479 + sizeof (*local_got_tls_masks));
4ce794b7 4480 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4481 if (local_got_ents == NULL)
e054468f 4482 return NULL;
411e1bfb
AM
4483 elf_local_got_ents (abfd) = local_got_ents;
4484 }
4485
37da22e5 4486 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4487 {
4488 struct got_entry *ent;
4489
4490 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4491 if (ent->addend == r_addend
4492 && ent->owner == abfd
4493 && ent->tls_type == tls_type)
411e1bfb
AM
4494 break;
4495 if (ent == NULL)
4496 {
986f0783 4497 size_t amt = sizeof (*ent);
4ce794b7 4498 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4499 if (ent == NULL)
4500 return FALSE;
4501 ent->next = local_got_ents[r_symndx];
4502 ent->addend = r_addend;
e717da7e 4503 ent->owner = abfd;
411e1bfb 4504 ent->tls_type = tls_type;
927be08e 4505 ent->is_indirect = FALSE;
411e1bfb
AM
4506 ent->got.refcount = 0;
4507 local_got_ents[r_symndx] = ent;
4508 }
4509 ent->got.refcount += 1;
4510 }
4511
e054468f 4512 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4513 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4514 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4515
4516 return local_plt + r_symndx;
65f38f15
AM
4517}
4518
411e1bfb 4519static bfd_boolean
e054468f 4520update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4521{
411e1bfb 4522 struct plt_entry *ent;
1e2f5b6e 4523
e054468f 4524 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4525 if (ent->addend == addend)
4526 break;
4527 if (ent == NULL)
1e2f5b6e 4528 {
986f0783 4529 size_t amt = sizeof (*ent);
4ce794b7 4530 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4531 if (ent == NULL)
4532 return FALSE;
e054468f 4533 ent->next = *plist;
411e1bfb
AM
4534 ent->addend = addend;
4535 ent->plt.refcount = 0;
e054468f 4536 *plist = ent;
1e2f5b6e 4537 }
411e1bfb 4538 ent->plt.refcount += 1;
b34976b6 4539 return TRUE;
1e2f5b6e
AM
4540}
4541
e054468f
AM
4542static bfd_boolean
4543is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4544{
4545 return (r_type == R_PPC64_REL24
05d0e962 4546 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
4547 || r_type == R_PPC64_REL14
4548 || r_type == R_PPC64_REL14_BRTAKEN
4549 || r_type == R_PPC64_REL14_BRNTAKEN
4550 || r_type == R_PPC64_ADDR24
4551 || r_type == R_PPC64_ADDR14
4552 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4553 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4554 || r_type == R_PPC64_PLTCALL
4555 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4556}
4557
4558/* Relocs on inline plt call sequence insns prior to the call. */
4559
4560static bfd_boolean
4561is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4562{
4563 return (r_type == R_PPC64_PLT16_HA
4564 || r_type == R_PPC64_PLT16_HI
4565 || r_type == R_PPC64_PLT16_LO
4566 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4567 || r_type == R_PPC64_PLT_PCREL34
4568 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4569 || r_type == R_PPC64_PLTSEQ
4570 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4571}
4572
5bd4f169 4573/* Look through the relocs for a section during the first phase, and
65f38f15 4574 calculate needed space in the global offset table, procedure
5d1634d7 4575 linkage table, and dynamic reloc sections. */
5bd4f169 4576
b34976b6 4577static bfd_boolean
4ce794b7
AM
4578ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4579 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4580{
65f38f15 4581 struct ppc_link_hash_table *htab;
5bd4f169 4582 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4583 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4584 const Elf_Internal_Rela *rel;
4585 const Elf_Internal_Rela *rel_end;
5bd4f169 4586 asection *sreloc;
3a71aa26 4587 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 4588 bfd_boolean is_opd;
5bd4f169 4589
0e1862bb 4590 if (bfd_link_relocatable (info))
b34976b6 4591 return TRUE;
5bd4f169 4592
0c8d6e5c 4593 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4594
65f38f15 4595 htab = ppc_hash_table (info);
4dfe6ac6
NC
4596 if (htab == NULL)
4597 return FALSE;
4598
3a71aa26
AM
4599 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4600 FALSE, FALSE, TRUE);
4601 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4602 FALSE, FALSE, TRUE);
0ffa91dd 4603 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4604 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4605 sreloc = NULL;
b9399fcf 4606 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4607 rel_end = relocs + sec->reloc_count;
4608 for (rel = relocs; rel < rel_end; rel++)
4609 {
4610 unsigned long r_symndx;
4611 struct elf_link_hash_entry *h;
04c9666a 4612 enum elf_ppc64_reloc_type r_type;
727fc41e 4613 int tls_type;
7c8fe5c4 4614 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4615 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
4616
4617 r_symndx = ELF64_R_SYM (rel->r_info);
4618 if (r_symndx < symtab_hdr->sh_info)
4619 h = NULL;
4620 else
973a3492
L
4621 {
4622 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4623 h = elf_follow_link (h);
1c865ab2
AM
4624
4625 if (h == htab->elf.hgot)
4626 sec->has_toc_reloc = 1;
973a3492 4627 }
5bd4f169 4628
5663e321
AM
4629 r_type = ELF64_R_TYPE (rel->r_info);
4630 switch (r_type)
4631 {
04bdff6a
AM
4632 case R_PPC64_D34:
4633 case R_PPC64_D34_LO:
4634 case R_PPC64_D34_HI30:
4635 case R_PPC64_D34_HA30:
4636 case R_PPC64_D28:
c213164a
AM
4637 case R_PPC64_TPREL34:
4638 case R_PPC64_DTPREL34:
04bdff6a 4639 case R_PPC64_PCREL34:
5663e321 4640 case R_PPC64_GOT_PCREL34:
87c69f97
AM
4641 case R_PPC64_GOT_TLSGD_PCREL34:
4642 case R_PPC64_GOT_TLSLD_PCREL34:
4643 case R_PPC64_GOT_TPREL_PCREL34:
4644 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
4645 case R_PPC64_PLT_PCREL34:
4646 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a 4647 case R_PPC64_PCREL28:
e10a07b3 4648 htab->has_power10_relocs = 1;
133a1f60
AM
4649 break;
4650 default:
5663e321
AM
4651 break;
4652 }
903b777d
AM
4653
4654 switch (r_type)
4655 {
4656 case R_PPC64_PLT16_HA:
4657 case R_PPC64_GOT_TLSLD16_HA:
4658 case R_PPC64_GOT_TLSGD16_HA:
4659 case R_PPC64_GOT_TPREL16_HA:
4660 case R_PPC64_GOT_DTPREL16_HA:
4661 case R_PPC64_GOT16_HA:
4662 case R_PPC64_TOC16_HA:
4663 case R_PPC64_PLT16_LO:
4664 case R_PPC64_PLT16_LO_DS:
4665 case R_PPC64_GOT_TLSLD16_LO:
4666 case R_PPC64_GOT_TLSGD16_LO:
4667 case R_PPC64_GOT_TPREL16_LO_DS:
4668 case R_PPC64_GOT_DTPREL16_LO_DS:
4669 case R_PPC64_GOT16_LO:
4670 case R_PPC64_GOT16_LO_DS:
4671 case R_PPC64_TOC16_LO:
4672 case R_PPC64_TOC16_LO_DS:
4673 case R_PPC64_GOT_PCREL34:
4674 ppc64_elf_tdata (abfd)->has_optrel = 1;
4675 ppc64_elf_section_data (sec)->has_optrel = 1;
4676 break;
4677 default:
4678 break;
4679 }
4680
f749f26e 4681 ifunc = NULL;
25f23106
AM
4682 if (h != NULL)
4683 {
4684 if (h->type == STT_GNU_IFUNC)
4685 {
4686 h->needs_plt = 1;
4687 ifunc = &h->plt.plist;
4688 }
4689 }
4690 else
4691 {
f1dfbfdb 4692 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
25f23106
AM
4693 abfd, r_symndx);
4694 if (isym == NULL)
4695 return FALSE;
4696
4697 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4698 {
4699 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4700 rel->r_addend,
37da22e5 4701 NON_GOT | PLT_IFUNC);
25f23106
AM
4702 if (ifunc == NULL)
4703 return FALSE;
4704 }
4705 }
727fc41e 4706
f749f26e 4707 tls_type = 0;
a33d1f77 4708 switch (r_type)
5bd4f169 4709 {
727fc41e
AM
4710 case R_PPC64_TLSGD:
4711 case R_PPC64_TLSLD:
4712 /* These special tls relocs tie a call to __tls_get_addr with
4713 its parameter symbol. */
37da22e5 4714 if (h != NULL)
ed7007c1 4715 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
37da22e5
AM
4716 else
4717 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4718 rel->r_addend,
37da22e5
AM
4719 NON_GOT | TLS_TLS | TLS_MARK))
4720 return FALSE;
4721 sec->has_tls_reloc = 1;
727fc41e
AM
4722 break;
4723
411e1bfb
AM
4724 case R_PPC64_GOT_TLSLD16:
4725 case R_PPC64_GOT_TLSLD16_LO:
4726 case R_PPC64_GOT_TLSLD16_HI:
4727 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 4728 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 4729 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4730 goto dogottls;
4731
4732 case R_PPC64_GOT_TLSGD16:
4733 case R_PPC64_GOT_TLSGD16_LO:
4734 case R_PPC64_GOT_TLSGD16_HI:
4735 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 4736 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 4737 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4738 goto dogottls;
4739
4740 case R_PPC64_GOT_TPREL16_DS:
4741 case R_PPC64_GOT_TPREL16_LO_DS:
4742 case R_PPC64_GOT_TPREL16_HI:
4743 case R_PPC64_GOT_TPREL16_HA:
87c69f97 4744 case R_PPC64_GOT_TPREL_PCREL34:
7c8bbca5 4745 if (bfd_link_dll (info))
411e1bfb
AM
4746 info->flags |= DF_STATIC_TLS;
4747 tls_type = TLS_TLS | TLS_TPREL;
4748 goto dogottls;
4749
4750 case R_PPC64_GOT_DTPREL16_DS:
4751 case R_PPC64_GOT_DTPREL16_LO_DS:
4752 case R_PPC64_GOT_DTPREL16_HI:
4753 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 4754 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
4755 tls_type = TLS_TLS | TLS_DTPREL;
4756 dogottls:
4757 sec->has_tls_reloc = 1;
066f4018 4758 goto dogot;
411e1bfb 4759
903b777d
AM
4760 case R_PPC64_GOT16:
4761 case R_PPC64_GOT16_LO:
4762 case R_PPC64_GOT16_HI:
65f38f15 4763 case R_PPC64_GOT16_HA:
903b777d 4764 case R_PPC64_GOT16_DS:
066f4018 4765 case R_PPC64_GOT16_LO_DS:
4a421c53 4766 case R_PPC64_GOT_PCREL34:
066f4018 4767 dogot:
65f38f15 4768 /* This symbol requires a global offset table entry. */
4c52953f 4769 sec->has_toc_reloc = 1;
33c0ec9d
AM
4770 if (r_type == R_PPC64_GOT_TLSLD16
4771 || r_type == R_PPC64_GOT_TLSGD16
4772 || r_type == R_PPC64_GOT_TPREL16_DS
4773 || r_type == R_PPC64_GOT_DTPREL16_DS
4774 || r_type == R_PPC64_GOT16
4775 || r_type == R_PPC64_GOT16_DS)
4776 {
4777 htab->do_multi_toc = 1;
d77c8a4b 4778 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4779 }
4780
e717da7e
AM
4781 if (ppc64_elf_tdata (abfd)->got == NULL
4782 && !create_got_section (abfd, info))
b34976b6 4783 return FALSE;
5bd4f169
AM
4784
4785 if (h != NULL)
4786 {
411e1bfb
AM
4787 struct ppc_link_hash_entry *eh;
4788 struct got_entry *ent;
65f38f15 4789
ed7007c1 4790 eh = ppc_elf_hash_entry (h);
411e1bfb 4791 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
133a1f60 4792 if (ent->addend == rel->r_addend
e717da7e 4793 && ent->owner == abfd
411e1bfb
AM
4794 && ent->tls_type == tls_type)
4795 break;
4796 if (ent == NULL)
5bd4f169 4797 {
986f0783 4798 size_t amt = sizeof (*ent);
4ce794b7 4799 ent = bfd_alloc (abfd, amt);
411e1bfb 4800 if (ent == NULL)
b34976b6 4801 return FALSE;
411e1bfb 4802 ent->next = eh->elf.got.glist;
133a1f60 4803 ent->addend = rel->r_addend;
e717da7e 4804 ent->owner = abfd;
411e1bfb 4805 ent->tls_type = tls_type;
927be08e 4806 ent->is_indirect = FALSE;
411e1bfb
AM
4807 ent->got.refcount = 0;
4808 eh->elf.got.glist = ent;
5bd4f169 4809 }
411e1bfb 4810 ent->got.refcount += 1;
e7b938ca 4811 eh->tls_mask |= tls_type;
5bd4f169 4812 }
411e1bfb
AM
4813 else
4814 /* This is a global offset table entry for a local symbol. */
4815 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4816 rel->r_addend, tls_type))
411e1bfb 4817 return FALSE;
5bd4f169
AM
4818 break;
4819
5bd4f169 4820 case R_PPC64_PLT16_HA:
65f38f15
AM
4821 case R_PPC64_PLT16_HI:
4822 case R_PPC64_PLT16_LO:
08be3224 4823 case R_PPC64_PLT16_LO_DS:
5663e321
AM
4824 case R_PPC64_PLT_PCREL34:
4825 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
4826 case R_PPC64_PLT32:
4827 case R_PPC64_PLT64:
cbf95972
AM
4828 /* This symbol requires a procedure linkage table entry. */
4829 plt_list = ifunc;
4830 if (h != NULL)
e054468f 4831 {
e054468f
AM
4832 h->needs_plt = 1;
4833 if (h->root.root.string[0] == '.'
4834 && h->root.root.string[1] != '\0')
ed7007c1
AM
4835 ppc_elf_hash_entry (h)->is_func = 1;
4836 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4837 plt_list = &h->plt.plist;
4838 }
4839 if (plt_list == NULL)
2d7ad24e 4840 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4841 rel->r_addend,
2d7ad24e 4842 NON_GOT | PLT_KEEP);
133a1f60 4843 if (!update_plt_info (abfd, plt_list, rel->r_addend))
cbf95972 4844 return FALSE;
5bd4f169
AM
4845 break;
4846
4847 /* The following relocations don't need to propagate the
4848 relocation if linking a shared object since they are
4849 section relative. */
4850 case R_PPC64_SECTOFF:
4851 case R_PPC64_SECTOFF_LO:
4852 case R_PPC64_SECTOFF_HI:
4853 case R_PPC64_SECTOFF_HA:
4854 case R_PPC64_SECTOFF_DS:
4855 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4856 case R_PPC64_DTPREL16:
4857 case R_PPC64_DTPREL16_LO:
4858 case R_PPC64_DTPREL16_HI:
4859 case R_PPC64_DTPREL16_HA:
4860 case R_PPC64_DTPREL16_DS:
4861 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
4862 case R_PPC64_DTPREL16_HIGH:
4863 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
4864 case R_PPC64_DTPREL16_HIGHER:
4865 case R_PPC64_DTPREL16_HIGHERA:
4866 case R_PPC64_DTPREL16_HIGHEST:
4867 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4868 break;
4869
ad8e1ba5 4870 /* Nor do these. */
25f23106
AM
4871 case R_PPC64_REL16:
4872 case R_PPC64_REL16_LO:
4873 case R_PPC64_REL16_HI:
4874 case R_PPC64_REL16_HA:
4a969973
AM
4875 case R_PPC64_REL16_HIGH:
4876 case R_PPC64_REL16_HIGHA:
4877 case R_PPC64_REL16_HIGHER:
4878 case R_PPC64_REL16_HIGHERA:
4879 case R_PPC64_REL16_HIGHEST:
4880 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
4881 case R_PPC64_REL16_HIGHER34:
4882 case R_PPC64_REL16_HIGHERA34:
4883 case R_PPC64_REL16_HIGHEST34:
4884 case R_PPC64_REL16_HIGHESTA34:
a680de9a 4885 case R_PPC64_REL16DX_HA:
25f23106
AM
4886 break;
4887
45965137
AM
4888 /* Not supported as a dynamic relocation. */
4889 case R_PPC64_ADDR64_LOCAL:
0e1862bb 4890 if (bfd_link_pic (info))
45965137
AM
4891 {
4892 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4893 ppc_howto_init ();
695344c0 4894 /* xgettext:c-format */
174d0a74 4895 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 4896 "in shared libraries and PIEs\n"),
45965137
AM
4897 abfd, sec, rel->r_offset,
4898 ppc64_elf_howto_table[r_type]->name);
4899 bfd_set_error (bfd_error_bad_value);
4900 return FALSE;
4901 }
4902 break;
4903
ad8e1ba5 4904 case R_PPC64_TOC16:
33c0ec9d
AM
4905 case R_PPC64_TOC16_DS:
4906 htab->do_multi_toc = 1;
d77c8a4b 4907 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 4908 /* Fall through. */
ad8e1ba5
AM
4909 case R_PPC64_TOC16_LO:
4910 case R_PPC64_TOC16_HI:
4911 case R_PPC64_TOC16_HA:
ad8e1ba5 4912 case R_PPC64_TOC16_LO_DS:
4c52953f 4913 sec->has_toc_reloc = 1;
ec73ddcd 4914 if (h != NULL && bfd_link_executable (info))
1bdd8fac
AM
4915 {
4916 /* We may need a copy reloc. */
4917 h->non_got_ref = 1;
4918 /* Strongly prefer a copy reloc over a dynamic reloc.
4919 glibc ld.so as of 2019-08 will error out if one of
4920 these relocations is emitted. */
4921 h->needs_copy = 1;
4922 goto dodyn;
4923 }
ad8e1ba5
AM
4924 break;
4925
006589cf
AM
4926 /* Marker reloc. */
4927 case R_PPC64_ENTRY:
4928 break;
4929
5bd4f169
AM
4930 /* This relocation describes the C++ object vtable hierarchy.
4931 Reconstruct it for later use during GC. */
4932 case R_PPC64_GNU_VTINHERIT:
c152c796 4933 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4934 return FALSE;
5bd4f169
AM
4935 break;
4936
4937 /* This relocation describes which C++ vtable entries are actually
4938 used. Record for later use during GC. */
4939 case R_PPC64_GNU_VTENTRY:
a0ea3a14 4940 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4941 return FALSE;
5bd4f169
AM
4942 break;
4943
721956f4
AM
4944 case R_PPC64_REL14:
4945 case R_PPC64_REL14_BRTAKEN:
4946 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4947 {
4948 asection *dest = NULL;
4949
4950 /* Heuristic: If jumping outside our section, chances are
4951 we are going to need a stub. */
4952 if (h != NULL)
4953 {
4954 /* If the sym is weak it may be overridden later, so
4955 don't assume we know where a weak sym lives. */
4956 if (h->root.type == bfd_link_hash_defined)
4957 dest = h->root.u.def.section;
4958 }
4959 else
87d72d41
AM
4960 {
4961 Elf_Internal_Sym *isym;
4962
f1dfbfdb 4963 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
4964 abfd, r_symndx);
4965 if (isym == NULL)
4966 return FALSE;
4967
4968 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4969 }
4970
220c76dd 4971 if (dest != sec)
7c8fe5c4 4972 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4973 }
3e04d765
AM
4974 goto rel24;
4975
4976 case R_PPC64_PLTCALL:
5663e321 4977 case R_PPC64_PLTCALL_NOTOC:
3e04d765 4978 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
4979 /* Fall through. */
4980
5d1634d7 4981 case R_PPC64_REL24:
05d0e962 4982 case R_PPC64_REL24_NOTOC:
3e04d765 4983 rel24:
cbf95972
AM
4984 plt_list = ifunc;
4985 if (h != NULL)
5d1634d7 4986 {
e054468f
AM
4987 h->needs_plt = 1;
4988 if (h->root.root.string[0] == '.'
4989 && h->root.root.string[1] != '\0')
ed7007c1 4990 ppc_elf_hash_entry (h)->is_func = 1;
cbf95972 4991
3a71aa26 4992 if (h == tga || h == dottga)
cbf95972
AM
4993 {
4994 sec->has_tls_reloc = 1;
4995 if (rel != relocs
4996 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4997 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4998 /* We have a new-style __tls_get_addr call with
4999 a marker reloc. */
5000 ;
5001 else
5002 /* Mark this section as having an old-style call. */
9737e8af 5003 sec->nomark_tls_get_addr = 1;
cbf95972
AM
5004 }
5005 plt_list = &h->plt.plist;
411e1bfb 5006 }
cbf95972
AM
5007
5008 /* We may need a .plt entry if the function this reloc
5009 refers to is in a shared lib. */
5010 if (plt_list
133a1f60 5011 && !update_plt_info (abfd, plt_list, rel->r_addend))
cbf95972 5012 return FALSE;
411e1bfb
AM
5013 break;
5014
cbf95972
AM
5015 case R_PPC64_ADDR14:
5016 case R_PPC64_ADDR14_BRNTAKEN:
5017 case R_PPC64_ADDR14_BRTAKEN:
5018 case R_PPC64_ADDR24:
5019 goto dodyn;
5020
411e1bfb
AM
5021 case R_PPC64_TPREL64:
5022 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5023 if (bfd_link_dll (info))
411e1bfb
AM
5024 info->flags |= DF_STATIC_TLS;
5025 goto dotlstoc;
5026
5027 case R_PPC64_DTPMOD64:
5028 if (rel + 1 < rel_end
5029 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5030 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5031 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5032 else
951fd09b 5033 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5034 goto dotlstoc;
5035
5036 case R_PPC64_DTPREL64:
5037 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5038 if (rel != relocs
5039 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5040 && rel[-1].r_offset == rel->r_offset - 8)
5041 /* This is the second reloc of a dtpmod, dtprel pair.
5042 Don't mark with TLS_DTPREL. */
5043 goto dodyn;
5044
5045 dotlstoc:
5046 sec->has_tls_reloc = 1;
5047 if (h != NULL)
ed7007c1 5048 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
411e1bfb
AM
5049 else
5050 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5051 rel->r_addend, tls_type))
411e1bfb
AM
5052 return FALSE;
5053
7c8fe5c4
AM
5054 ppc64_sec = ppc64_elf_section_data (sec);
5055 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5056 {
3a71aa26
AM
5057 bfd_size_type amt;
5058
e7b938ca 5059 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5060 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5061 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5062 if (ppc64_sec->u.toc.symndx == NULL)
5063 return FALSE;
5064 amt = sec->size * sizeof (bfd_vma) / 8;
5065 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5066 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5067 return FALSE;
7c8fe5c4
AM
5068 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5069 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5070 }
5071 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26 5072 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
133a1f60 5073 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5074
5075 /* Mark the second slot of a GD or LD entry.
5076 -1 to indicate GD and -2 to indicate LD. */
5077 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5078 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5079 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5080 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5081 goto dodyn;
5082
411e1bfb
AM
5083 case R_PPC64_TPREL16_HI:
5084 case R_PPC64_TPREL16_HA:
f9c6b907
AM
5085 case R_PPC64_TPREL16_HIGH:
5086 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5087 case R_PPC64_TPREL16_HIGHER:
5088 case R_PPC64_TPREL16_HIGHERA:
5089 case R_PPC64_TPREL16_HIGHEST:
5090 case R_PPC64_TPREL16_HIGHESTA:
252dcdf4
AM
5091 sec->has_tls_reloc = 1;
5092 /* Fall through. */
c213164a 5093 case R_PPC64_TPREL34:
252dcdf4
AM
5094 case R_PPC64_TPREL16:
5095 case R_PPC64_TPREL16_DS:
5096 case R_PPC64_TPREL16_LO:
5097 case R_PPC64_TPREL16_LO_DS:
7c8bbca5
AM
5098 if (bfd_link_dll (info))
5099 info->flags |= DF_STATIC_TLS;
5100 goto dodyn;
5d1634d7 5101
e86ce104 5102 case R_PPC64_ADDR64:
b9399fcf 5103 if (is_opd
1e2f5b6e 5104 && rel + 1 < rel_end
4ce794b7 5105 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5106 {
8387904d 5107 if (h != NULL)
ed7007c1 5108 ppc_elf_hash_entry (h)->is_func = 1;
1e2f5b6e 5109 }
e86ce104
AM
5110 /* Fall through. */
5111
65f38f15
AM
5112 case R_PPC64_ADDR16:
5113 case R_PPC64_ADDR16_DS:
5114 case R_PPC64_ADDR16_HA:
5115 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5116 case R_PPC64_ADDR16_HIGH:
5117 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5118 case R_PPC64_ADDR16_HIGHER:
5119 case R_PPC64_ADDR16_HIGHERA:
5120 case R_PPC64_ADDR16_HIGHEST:
5121 case R_PPC64_ADDR16_HIGHESTA:
5122 case R_PPC64_ADDR16_LO:
5123 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
5124 case R_PPC64_D34:
5125 case R_PPC64_D34_LO:
5126 case R_PPC64_D34_HI30:
5127 case R_PPC64_D34_HA30:
5128 case R_PPC64_ADDR16_HIGHER34:
5129 case R_PPC64_ADDR16_HIGHERA34:
5130 case R_PPC64_ADDR16_HIGHEST34:
5131 case R_PPC64_ADDR16_HIGHESTA34:
5132 case R_PPC64_D28:
0e1862bb 5133 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5134 && rel->r_addend == 0)
5135 {
5136 /* We may need a .plt entry if this reloc refers to a
5137 function in a shared lib. */
5663e321 5138 if (!update_plt_info (abfd, &h->plt.plist, 0))
a345bc8d
AM
5139 return FALSE;
5140 h->pointer_equality_needed = 1;
5141 }
5142 /* Fall through. */
5143
5144 case R_PPC64_REL30:
5145 case R_PPC64_REL32:
5146 case R_PPC64_REL64:
65f38f15 5147 case R_PPC64_ADDR32:
65f38f15
AM
5148 case R_PPC64_UADDR16:
5149 case R_PPC64_UADDR32:
5150 case R_PPC64_UADDR64:
5bd4f169 5151 case R_PPC64_TOC:
ec73ddcd 5152 if (h != NULL && bfd_link_executable (info))
81848ca0 5153 /* We may need a copy reloc. */
f5385ebf 5154 h->non_got_ref = 1;
81848ca0 5155
41bd81ab 5156 /* Don't propagate .opd relocs. */
b9399fcf 5157 if (NO_OPD_RELOCS && is_opd)
e86ce104 5158 break;
e86ce104 5159
65f38f15
AM
5160 /* If we are creating a shared library, and this is a reloc
5161 against a global symbol, or a non PC relative reloc
5162 against a local symbol, then we need to copy the reloc
5163 into the shared library. However, if we are linking with
5164 -Bsymbolic, we do not need to copy a reloc against a
5165 global symbol which is defined in an object we are
5166 including in the link (i.e., DEF_REGULAR is set). At
5167 this point we have not seen all the input files, so it is
5168 possible that DEF_REGULAR is not set now but will be set
5169 later (it is never cleared). In case of a weak definition,
5170 DEF_REGULAR may be cleared later by a strong definition in
5171 a shared library. We account for that possibility below by
f4656909 5172 storing information in the dyn_relocs field of the hash
65f38f15
AM
5173 table entry. A similar situation occurs when creating
5174 shared libraries and symbol visibility changes render the
5175 symbol local.
5176
5177 If on the other hand, we are creating an executable, we
5178 may need to keep relocations for symbols satisfied by a
5179 dynamic library if we manage to avoid copy relocs for the
5180 symbol. */
411e1bfb 5181 dodyn:
ec73ddcd
AM
5182 if ((h != NULL
5183 && (h->root.type == bfd_link_hash_defweak
5184 || !h->def_regular))
5185 || (h != NULL
5186 && !bfd_link_executable (info)
5187 && !SYMBOLIC_BIND (info, h))
5188 || (bfd_link_pic (info)
5189 && must_be_dyn_reloc (info, r_type))
0e1862bb 5190 || (!bfd_link_pic (info)
25f23106 5191 && ifunc != NULL))
5bd4f169 5192 {
65f38f15
AM
5193 /* We must copy these reloc types into the output file.
5194 Create a reloc section in dynobj and make room for
5195 this reloc. */
5bd4f169
AM
5196 if (sreloc == NULL)
5197 {
83bac4b0
NC
5198 sreloc = _bfd_elf_make_dynamic_reloc_section
5199 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5200
5bd4f169 5201 if (sreloc == NULL)
83bac4b0 5202 return FALSE;
5bd4f169
AM
5203 }
5204
65f38f15
AM
5205 /* If this is a global symbol, we count the number of
5206 relocations we need for this symbol. */
5207 if (h != NULL)
5208 {
19e08130
AM
5209 struct elf_dyn_relocs *p;
5210 struct elf_dyn_relocs **head;
5211
190eb1dd 5212 head = &h->dyn_relocs;
19e08130
AM
5213 p = *head;
5214 if (p == NULL || p->sec != sec)
5215 {
5216 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5217 if (p == NULL)
5218 return FALSE;
5219 p->next = *head;
5220 *head = p;
5221 p->sec = sec;
5222 p->count = 0;
5223 p->pc_count = 0;
5224 }
5225 p->count += 1;
5226 if (!must_be_dyn_reloc (info, r_type))
5227 p->pc_count += 1;
65f38f15
AM
5228 }
5229 else
5230 {
ec338859
AM
5231 /* Track dynamic relocs needed for local syms too.
5232 We really need local syms available to do this
5233 easily. Oh well. */
19e08130
AM
5234 struct ppc_dyn_relocs *p;
5235 struct ppc_dyn_relocs **head;
5236 bfd_boolean is_ifunc;
ec338859 5237 asection *s;
6edfbbad 5238 void *vpp;
87d72d41 5239 Elf_Internal_Sym *isym;
6edfbbad 5240
f1dfbfdb 5241 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
5242 abfd, r_symndx);
5243 if (isym == NULL)
b34976b6 5244 return FALSE;
ec338859 5245
87d72d41
AM
5246 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5247 if (s == NULL)
5248 s = sec;
5249
6edfbbad 5250 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5251 head = (struct ppc_dyn_relocs **) vpp;
5252 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5253 p = *head;
5254 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5255 p = p->next;
5256 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5257 {
5258 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5259 if (p == NULL)
5260 return FALSE;
5261 p->next = *head;
5262 *head = p;
5263 p->sec = sec;
5264 p->ifunc = is_ifunc;
5265 p->count = 0;
5266 }
5267 p->count += 1;
ec338859 5268 }
65f38f15 5269 }
5bd4f169 5270 break;
65f38f15
AM
5271
5272 default:
96e0dda4 5273 break;
5bd4f169
AM
5274 }
5275 }
5276
b34976b6 5277 return TRUE;
5bd4f169
AM
5278}
5279
ee67d69a
AM
5280/* Merge backend specific data from an object file to the output
5281 object file when linking. */
5282
5283static bfd_boolean
50e03d47 5284ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5285{
50e03d47 5286 bfd *obfd = info->output_bfd;
ee67d69a
AM
5287 unsigned long iflags, oflags;
5288
5289 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5290 return TRUE;
5291
5292 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5293 return TRUE;
5294
50e03d47 5295 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
5296 return FALSE;
5297
5298 iflags = elf_elfheader (ibfd)->e_flags;
5299 oflags = elf_elfheader (obfd)->e_flags;
5300
f6c7c3e8 5301 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5302 {
4eca0228 5303 _bfd_error_handler
695344c0 5304 /* xgettext:c-format */
871b3ab2 5305 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
5306 bfd_set_error (bfd_error_bad_value);
5307 return FALSE;
5308 }
f6c7c3e8 5309 else if (iflags != oflags && iflags != 0)
ee67d69a 5310 {
4eca0228 5311 _bfd_error_handler
695344c0 5312 /* xgettext:c-format */
871b3ab2 5313 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5314 ibfd, iflags, oflags);
5315 bfd_set_error (bfd_error_bad_value);
5316 return FALSE;
5317 }
5318
4a91d0ba
AM
5319 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5320 return FALSE;
005d79fd 5321
ee67d69a 5322 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5323 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5324}
5325
5326static bfd_boolean
5327ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5328{
5329 /* Print normal ELF private data. */
5330 _bfd_elf_print_private_bfd_data (abfd, ptr);
5331
5332 if (elf_elfheader (abfd)->e_flags != 0)
5333 {
5334 FILE *file = ptr;
5335
ee67d69a
AM
5336 fprintf (file, _("private flags = 0x%lx:"),
5337 elf_elfheader (abfd)->e_flags);
5338
5339 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5340 fprintf (file, _(" [abiv%ld]"),
5341 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5342 fputc ('\n', file);
5343 }
5344
5345 return TRUE;
5346}
5347
8387904d 5348/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5349 of the code entry point, and its section, which must be in the same
5350 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5351
5352static bfd_vma
5353opd_entry_value (asection *opd_sec,
5354 bfd_vma offset,
5355 asection **code_sec,
aef36ac1
AM
5356 bfd_vma *code_off,
5357 bfd_boolean in_code_sec)
8387904d
AM
5358{
5359 bfd *opd_bfd = opd_sec->owner;
8860955f 5360 Elf_Internal_Rela *relocs;
8387904d 5361 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5362 bfd_vma val;
8387904d 5363
9f296da3
AM
5364 /* No relocs implies we are linking a --just-symbols object, or looking
5365 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5366 if (opd_sec->reloc_count == 0)
5367 {
729eabd5 5368 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5369
729eabd5
AM
5370 if (contents == NULL)
5371 {
5372 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5373 return (bfd_vma) -1;
5374 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5375 }
ee1e4ede 5376
dbb3fbbb 5377 /* PR 17512: file: 64b9dfbb. */
451dfd38 5378 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5379 return (bfd_vma) -1;
5380
729eabd5 5381 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5382 if (code_sec != NULL)
5383 {
5384 asection *sec, *likely = NULL;
ee1e4ede 5385
aef36ac1 5386 if (in_code_sec)
4b85d634 5387 {
aef36ac1
AM
5388 sec = *code_sec;
5389 if (sec->vma <= val
5390 && val < sec->vma + sec->size)
5391 likely = sec;
5392 else
5393 val = -1;
5394 }
5395 else
5396 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5397 if (sec->vma <= val
5398 && (sec->flags & SEC_LOAD) != 0
5399 && (sec->flags & SEC_ALLOC) != 0)
5400 likely = sec;
5401 if (likely != NULL)
5402 {
5403 *code_sec = likely;
5404 if (code_off != NULL)
5405 *code_off = val - likely->vma;
4b85d634
AM
5406 }
5407 }
aef36ac1 5408 return val;
4b85d634
AM
5409 }
5410
0c8d6e5c 5411 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5412
729eabd5 5413 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5414 if (relocs == NULL)
5415 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
5416 /* PR 17512: file: df8e1fd6. */
5417 if (relocs == NULL)
5418 return (bfd_vma) -1;
645ea6a9 5419
8387904d 5420 /* Go find the opd reloc at the sym address. */
8860955f 5421 lo = relocs;
8387904d 5422 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5423 val = (bfd_vma) -1;
8387904d
AM
5424 while (lo < hi)
5425 {
5426 look = lo + (hi - lo) / 2;
5427 if (look->r_offset < offset)
5428 lo = look + 1;
5429 else if (look->r_offset > offset)
5430 hi = look;
5431 else
5432 {
0ffa91dd
NC
5433 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5434
8387904d
AM
5435 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5436 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5437 {
5438 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5439 asection *sec = NULL;
8387904d 5440
b53dfeb2
AM
5441 if (symndx >= symtab_hdr->sh_info
5442 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5443 {
5444 struct elf_link_hash_entry **sym_hashes;
5445 struct elf_link_hash_entry *rh;
5446
5447 sym_hashes = elf_sym_hashes (opd_bfd);
5448 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5449 if (rh != NULL)
5450 {
5451 rh = elf_follow_link (rh);
bb854a36
AM
5452 if (rh->root.type != bfd_link_hash_defined
5453 && rh->root.type != bfd_link_hash_defweak)
5454 break;
5455 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5456 {
bb854a36
AM
5457 val = rh->root.u.def.value;
5458 sec = rh->root.u.def.section;
b53dfeb2
AM
5459 }
5460 }
5461 }
5462
5463 if (sec == NULL)
5464 {
5465 Elf_Internal_Sym *sym;
5466
5467 if (symndx < symtab_hdr->sh_info)
5468 {
5469 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5470 if (sym == NULL)
5471 {
5472 size_t symcnt = symtab_hdr->sh_info;
5473 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5474 symcnt, 0,
5475 NULL, NULL, NULL);
5476 if (sym == NULL)
5477 break;
5478 symtab_hdr->contents = (bfd_byte *) sym;
5479 }
5480 sym += symndx;
128205bb
AM
5481 }
5482 else
5483 {
b53dfeb2
AM
5484 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5485 1, symndx,
5486 NULL, NULL, NULL);
128205bb
AM
5487 if (sym == NULL)
5488 break;
128205bb 5489 }
b53dfeb2
AM
5490 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5491 if (sec == NULL)
5492 break;
5493 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5494 val = sym->st_value;
8387904d 5495 }
b53dfeb2 5496
8387904d
AM
5497 val += look->r_addend;
5498 if (code_off != NULL)
5499 *code_off = val;
5500 if (code_sec != NULL)
aef36ac1
AM
5501 {
5502 if (in_code_sec && *code_sec != sec)
5503 return -1;
5504 else
5505 *code_sec = sec;
5506 }
b53dfeb2 5507 if (sec->output_section != NULL)
8387904d 5508 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5509 }
5510 break;
5511 }
5512 }
645ea6a9 5513
645ea6a9 5514 return val;
8387904d
AM
5515}
5516
aef36ac1
AM
5517/* If the ELF symbol SYM might be a function in SEC, return the
5518 function size and set *CODE_OFF to the function's entry point,
5519 otherwise return zero. */
9f296da3 5520
aef36ac1
AM
5521static bfd_size_type
5522ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5523 bfd_vma *code_off)
9f296da3 5524{
aef36ac1
AM
5525 bfd_size_type size;
5526
5527 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5528 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5529 return 0;
5530
5531 size = 0;
5532 if (!(sym->flags & BSF_SYNTHETIC))
5533 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5534
5535 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5536 {
b07bca4e
AM
5537 struct _opd_sec_data *opd = get_opd_info (sym->section);
5538 bfd_vma symval = sym->value;
5539
5540 if (opd != NULL
5541 && opd->adjust != NULL
5542 && elf_section_data (sym->section)->relocs != NULL)
5543 {
5544 /* opd_entry_value will use cached relocs that have been
5545 adjusted, but with raw symbols. That means both local
5546 and global symbols need adjusting. */
5547 long adjust = opd->adjust[OPD_NDX (symval)];
5548 if (adjust == -1)
5549 return 0;
5550 symval += adjust;
5551 }
5552
5553 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
5554 &sec, code_off, TRUE) == (bfd_vma) -1)
5555 return 0;
5556 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5557 symbol. This size has nothing to do with the code size of the
5558 function, which is what we're supposed to return, but the
5559 code size isn't available without looking up the dot-sym.
5560 However, doing that would be a waste of time particularly
5561 since elf_find_function will look at the dot-sym anyway.
5562 Now, elf_find_function will keep the largest size of any
5563 function sym found at the code address of interest, so return
5564 1 here to avoid it incorrectly caching a larger function size
5565 for a small function. This does mean we return the wrong
5566 size for a new-ABI function of size 24, but all that does is
5567 disable caching for such functions. */
5568 if (size == 24)
5569 size = 1;
9f296da3 5570 }
aef36ac1
AM
5571 else
5572 {
5573 if (sym->section != sec)
5574 return 0;
5575 *code_off = sym->value;
5576 }
5577 if (size == 0)
5578 size = 1;
5579 return size;
9f296da3
AM
5580}
5581
f378ab09
AM
5582/* Return true if symbol is a strong function defined in an ELFv2
5583 object with st_other localentry bits of zero, ie. its local entry
5584 point coincides with its global entry point. */
5585
5586static bfd_boolean
5587is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5588{
5589 return (h != NULL
5590 && h->type == STT_FUNC
5591 && h->root.type == bfd_link_hash_defined
5592 && (STO_PPC64_LOCAL_MASK & h->other) == 0
ed7007c1 5593 && !ppc_elf_hash_entry (h)->non_zero_localentry
f378ab09
AM
5594 && is_ppc64_elf (h->root.u.def.section->owner)
5595 && abiversion (h->root.u.def.section->owner) >= 2);
5596}
5597
854b41e7
AM
5598/* Return true if symbol is defined in a regular object file. */
5599
5600static bfd_boolean
5601is_static_defined (struct elf_link_hash_entry *h)
5602{
5603 return ((h->root.type == bfd_link_hash_defined
5604 || h->root.type == bfd_link_hash_defweak)
5605 && h->root.u.def.section != NULL
5606 && h->root.u.def.section->output_section != NULL);
5607}
5608
b31867b6
AM
5609/* If FDH is a function descriptor symbol, return the associated code
5610 entry symbol if it is defined. Return NULL otherwise. */
5611
5612static struct ppc_link_hash_entry *
5613defined_code_entry (struct ppc_link_hash_entry *fdh)
5614{
5615 if (fdh->is_func_descriptor)
5616 {
5617 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5618 if (fh->elf.root.type == bfd_link_hash_defined
5619 || fh->elf.root.type == bfd_link_hash_defweak)
5620 return fh;
5621 }
5622 return NULL;
5623}
5624
5625/* If FH is a function code entry symbol, return the associated
5626 function descriptor symbol if it is defined. Return NULL otherwise. */
5627
5628static struct ppc_link_hash_entry *
5629defined_func_desc (struct ppc_link_hash_entry *fh)
5630{
5631 if (fh->oh != NULL
5632 && fh->oh->is_func_descriptor)
5633 {
5634 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5635 if (fdh->elf.root.type == bfd_link_hash_defined
5636 || fdh->elf.root.type == bfd_link_hash_defweak)
5637 return fdh;
5638 }
5639 return NULL;
5640}
5641
ed7007c1
AM
5642/* Given H is a symbol that satisfies is_static_defined, return the
5643 value in the output file. */
5644
5645static bfd_vma
5646defined_sym_val (struct elf_link_hash_entry *h)
5647{
5648 return (h->root.u.def.section->output_section->vma
5649 + h->root.u.def.section->output_offset
5650 + h->root.u.def.value);
5651}
5652
5653/* Return true if H matches __tls_get_addr or one of its variants. */
5654
5655static bfd_boolean
5656is_tls_get_addr (struct elf_link_hash_entry *h,
5657 struct ppc_link_hash_table *htab)
5658{
9e51d549
AM
5659 return (h == (struct elf_link_hash_entry *) htab->tls_get_addr_fd
5660 || h == (struct elf_link_hash_entry *) htab->tga_desc_fd
5661 || h == (struct elf_link_hash_entry *) htab->tls_get_addr
5662 || h == (struct elf_link_hash_entry *) htab->tga_desc);
ed7007c1
AM
5663}
5664
8c5b4e52
AM
5665static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5666
5667/* Garbage collect sections, after first dealing with dot-symbols. */
5668
5669static bfd_boolean
5670ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5671{
5672 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5673
5674 if (htab != NULL && htab->need_func_desc_adj)
5675 {
5676 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5677 htab->need_func_desc_adj = 0;
5678 }
5679 return bfd_elf_gc_sections (abfd, info);
5680}
5681
74f0fb50
AM
5682/* Mark all our entry sym sections, both opd and code section. */
5683
5684static void
5685ppc64_elf_gc_keep (struct bfd_link_info *info)
5686{
5687 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5688 struct bfd_sym_chain *sym;
5689
4dfe6ac6
NC
5690 if (htab == NULL)
5691 return;
5692
74f0fb50
AM
5693 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5694 {
b31867b6 5695 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5696 asection *sec;
5697
ed7007c1
AM
5698 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5699 FALSE, FALSE, TRUE));
74f0fb50
AM
5700 if (eh == NULL)
5701 continue;
5702 if (eh->elf.root.type != bfd_link_hash_defined
5703 && eh->elf.root.type != bfd_link_hash_defweak)
5704 continue;
5705
b31867b6
AM
5706 fh = defined_code_entry (eh);
5707 if (fh != NULL)
74f0fb50 5708 {
b31867b6 5709 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5710 sec->flags |= SEC_KEEP;
5711 }
5712 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5713 && opd_entry_value (eh->elf.root.u.def.section,
5714 eh->elf.root.u.def.value,
aef36ac1 5715 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
5716 sec->flags |= SEC_KEEP;
5717
5718 sec = eh->elf.root.u.def.section;
5719 sec->flags |= SEC_KEEP;
5720 }
5721}
5722
64d03ab5
AM
5723/* Mark sections containing dynamically referenced symbols. When
5724 building shared libraries, we must assume that any visible symbol is
5725 referenced. */
5726
5727static bfd_boolean
5728ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5729{
5730 struct bfd_link_info *info = (struct bfd_link_info *) inf;
ed7007c1 5731 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
b31867b6 5732 struct ppc_link_hash_entry *fdh;
b407645f 5733 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5734
64d03ab5 5735 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5736 fdh = defined_func_desc (eh);
5737 if (fdh != NULL)
5738 eh = fdh;
64d03ab5
AM
5739
5740 if ((eh->elf.root.type == bfd_link_hash_defined
5741 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 5742 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5743 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5744 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5745 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5746 && (!bfd_link_executable (info)
e278ae05 5747 || info->gc_keep_exported
b407645f
AM
5748 || info->export_dynamic
5749 || (eh->elf.dynamic
5750 && d != NULL
2cdcc330
AM
5751 && (*d->match) (&d->head, NULL,
5752 eh->elf.root.root.string)))
e278ae05 5753 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5754 || !bfd_hide_sym_by_version (info->version_info,
5755 eh->elf.root.root.string)))))
64d03ab5
AM
5756 {
5757 asection *code_sec;
b31867b6 5758 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5759
5760 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5761
5762 /* Function descriptor syms cause the associated
5763 function code sym section to be marked. */
b31867b6
AM
5764 fh = defined_code_entry (eh);
5765 if (fh != NULL)
5766 {
5767 code_sec = fh->elf.root.u.def.section;
5768 code_sec->flags |= SEC_KEEP;
5769 }
64d03ab5
AM
5770 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5771 && opd_entry_value (eh->elf.root.u.def.section,
5772 eh->elf.root.u.def.value,
aef36ac1 5773 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
5774 code_sec->flags |= SEC_KEEP;
5775 }
5776
5777 return TRUE;
5778}
5779
5bd4f169
AM
5780/* Return the section that should be marked against GC for a given
5781 relocation. */
5782
5783static asection *
4ce794b7 5784ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5785 struct bfd_link_info *info,
4ce794b7
AM
5786 Elf_Internal_Rela *rel,
5787 struct elf_link_hash_entry *h,
5788 Elf_Internal_Sym *sym)
5bd4f169 5789{
ccfa59ea
AM
5790 asection *rsec;
5791
ccfa59ea
AM
5792 /* Syms return NULL if we're marking .opd, so we avoid marking all
5793 function sections, as all functions are referenced in .opd. */
5794 rsec = NULL;
5795 if (get_opd_info (sec) != NULL)
5796 return rsec;
1e2f5b6e 5797
5bd4f169
AM
5798 if (h != NULL)
5799 {
04c9666a 5800 enum elf_ppc64_reloc_type r_type;
b31867b6 5801 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5802
4ce794b7 5803 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5804 switch (r_type)
5bd4f169
AM
5805 {
5806 case R_PPC64_GNU_VTINHERIT:
5807 case R_PPC64_GNU_VTENTRY:
5808 break;
5809
5810 default:
5811 switch (h->root.type)
5812 {
5813 case bfd_link_hash_defined:
5814 case bfd_link_hash_defweak:
ed7007c1 5815 eh = ppc_elf_hash_entry (h);
b31867b6
AM
5816 fdh = defined_func_desc (eh);
5817 if (fdh != NULL)
8c5b4e52
AM
5818 {
5819 /* -mcall-aixdesc code references the dot-symbol on
5820 a call reloc. Mark the function descriptor too
5821 against garbage collection. */
5822 fdh->elf.mark = 1;
60d67dc8
AM
5823 if (fdh->elf.is_weakalias)
5824 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
5825 eh = fdh;
5826 }
1e2f5b6e
AM
5827
5828 /* Function descriptor syms cause the associated
5829 function code sym section to be marked. */
b31867b6
AM
5830 fh = defined_code_entry (eh);
5831 if (fh != NULL)
ccfa59ea
AM
5832 {
5833 /* They also mark their opd section. */
74f0fb50 5834 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5835
b31867b6 5836 rsec = fh->elf.root.u.def.section;
ccfa59ea 5837 }
8387904d
AM
5838 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5839 && opd_entry_value (eh->elf.root.u.def.section,
5840 eh->elf.root.u.def.value,
aef36ac1 5841 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 5842 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5843 else
1e2f5b6e
AM
5844 rsec = h->root.u.def.section;
5845 break;
5bd4f169
AM
5846
5847 case bfd_link_hash_common:
1e2f5b6e
AM
5848 rsec = h->root.u.c.p->section;
5849 break;
5bd4f169
AM
5850
5851 default:
fb34365b 5852 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5853 }
5854 }
5855 }
5856 else
5857 {
74f0fb50 5858 struct _opd_sec_data *opd;
1e2f5b6e
AM
5859
5860 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5861 opd = get_opd_info (rsec);
5862 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5863 {
74f0fb50 5864 rsec->gc_mark = 1;
ccfa59ea 5865
51aecdc5 5866 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 5867 }
5bd4f169
AM
5868 }
5869
1e2f5b6e 5870 return rsec;
5bd4f169
AM
5871}
5872
deb0e272
AM
5873/* The maximum size of .sfpr. */
5874#define SFPR_MAX (218*4)
5875
5876struct sfpr_def_parms
5877{
699733f6
AM
5878 const char name[12];
5879 unsigned char lo, hi;
2cdcc330
AM
5880 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5881 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
5882};
5883
a4b6fadd
AM
5884/* Auto-generate _save*, _rest* functions in .sfpr.
5885 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5886 instead. */
deb0e272 5887
4dfe6ac6 5888static bfd_boolean
a4b6fadd
AM
5889sfpr_define (struct bfd_link_info *info,
5890 const struct sfpr_def_parms *parm,
5891 asection *stub_sec)
deb0e272
AM
5892{
5893 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5894 unsigned int i;
5895 size_t len = strlen (parm->name);
5896 bfd_boolean writing = FALSE;
699733f6 5897 char sym[16];
deb0e272 5898
4dfe6ac6
NC
5899 if (htab == NULL)
5900 return FALSE;
5901
deb0e272
AM
5902 memcpy (sym, parm->name, len);
5903 sym[len + 2] = 0;
5904
5905 for (i = parm->lo; i <= parm->hi; i++)
5906 {
a4b6fadd 5907 struct ppc_link_hash_entry *h;
deb0e272
AM
5908
5909 sym[len + 0] = i / 10 + '0';
5910 sym[len + 1] = i % 10 + '0';
ed7007c1
AM
5911 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
5912 writing, TRUE, TRUE));
a4b6fadd 5913 if (stub_sec != NULL)
deb0e272 5914 {
a4b6fadd
AM
5915 if (h != NULL
5916 && h->elf.root.type == bfd_link_hash_defined
5917 && h->elf.root.u.def.section == htab->sfpr)
5918 {
5919 struct elf_link_hash_entry *s;
5920 char buf[32];
5921 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5922 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5923 if (s == NULL)
5924 return FALSE;
779f2ae7 5925 if (s->root.type == bfd_link_hash_new)
a4b6fadd
AM
5926 {
5927 s->root.type = bfd_link_hash_defined;
5928 s->root.u.def.section = stub_sec;
7dda8d3c 5929 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
5930 + h->elf.root.u.def.value);
5931 s->ref_regular = 1;
5932 s->def_regular = 1;
5933 s->ref_regular_nonweak = 1;
5934 s->forced_local = 1;
5935 s->non_elf = 0;
5936 s->root.linker_def = 1;
5937 }
5938 }
5939 continue;
5940 }
5941 if (h != NULL)
5942 {
5943 h->save_res = 1;
5944 if (!h->elf.def_regular)
deb0e272 5945 {
a4b6fadd
AM
5946 h->elf.root.type = bfd_link_hash_defined;
5947 h->elf.root.u.def.section = htab->sfpr;
5948 h->elf.root.u.def.value = htab->sfpr->size;
5949 h->elf.type = STT_FUNC;
5950 h->elf.def_regular = 1;
b32547cd 5951 h->elf.non_elf = 0;
a4b6fadd
AM
5952 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5953 writing = TRUE;
deb0e272 5954 if (htab->sfpr->contents == NULL)
a4b6fadd 5955 {
2cdcc330
AM
5956 htab->sfpr->contents
5957 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd
AM
5958 if (htab->sfpr->contents == NULL)
5959 return FALSE;
5960 }
deb0e272
AM
5961 }
5962 }
5963 if (writing)
5964 {
5965 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5966 if (i != parm->hi)
5967 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5968 else
5969 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5970 htab->sfpr->size = p - htab->sfpr->contents;
5971 }
5972 }
5973
5974 return TRUE;
5975}
5976
5977static bfd_byte *
5978savegpr0 (bfd *abfd, bfd_byte *p, int r)
5979{
5980 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5981 return p + 4;
5982}
5983
5984static bfd_byte *
5985savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5986{
5987 p = savegpr0 (abfd, p, r);
a078d95a 5988 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5989 p = p + 4;
5990 bfd_put_32 (abfd, BLR, p);
5991 return p + 4;
5992}
5993
5994static bfd_byte *
5995restgpr0 (bfd *abfd, bfd_byte *p, int r)
5996{
5997 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5998 return p + 4;
5999}
6000
6001static bfd_byte *
6002restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6003{
a078d95a 6004 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6005 p = p + 4;
6006 p = restgpr0 (abfd, p, r);
6007 bfd_put_32 (abfd, MTLR_R0, p);
6008 p = p + 4;
6009 if (r == 29)
6010 {
6011 p = restgpr0 (abfd, p, 30);
6012 p = restgpr0 (abfd, p, 31);
6013 }
6014 bfd_put_32 (abfd, BLR, p);
6015 return p + 4;
6016}
6017
6018static bfd_byte *
6019savegpr1 (bfd *abfd, bfd_byte *p, int r)
6020{
6021 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6022 return p + 4;
6023}
6024
6025static bfd_byte *
6026savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6027{
6028 p = savegpr1 (abfd, p, r);
6029 bfd_put_32 (abfd, BLR, p);
6030 return p + 4;
6031}
6032
6033static bfd_byte *
6034restgpr1 (bfd *abfd, bfd_byte *p, int r)
6035{
6036 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6037 return p + 4;
6038}
6039
6040static bfd_byte *
6041restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6042{
6043 p = restgpr1 (abfd, p, r);
6044 bfd_put_32 (abfd, BLR, p);
6045 return p + 4;
6046}
6047
6048static bfd_byte *
6049savefpr (bfd *abfd, bfd_byte *p, int r)
6050{
6051 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6052 return p + 4;
6053}
6054
6055static bfd_byte *
6056savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6057{
6058 p = savefpr (abfd, p, r);
a078d95a 6059 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6060 p = p + 4;
6061 bfd_put_32 (abfd, BLR, p);
6062 return p + 4;
6063}
6064
6065static bfd_byte *
6066restfpr (bfd *abfd, bfd_byte *p, int r)
6067{
6068 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6069 return p + 4;
6070}
6071
6072static bfd_byte *
6073restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6074{
a078d95a 6075 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6076 p = p + 4;
6077 p = restfpr (abfd, p, r);
6078 bfd_put_32 (abfd, MTLR_R0, p);
6079 p = p + 4;
6080 if (r == 29)
6081 {
6082 p = restfpr (abfd, p, 30);
6083 p = restfpr (abfd, p, 31);
6084 }
6085 bfd_put_32 (abfd, BLR, p);
6086 return p + 4;
6087}
6088
6089static bfd_byte *
6090savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6091{
6092 p = savefpr (abfd, p, r);
6093 bfd_put_32 (abfd, BLR, p);
6094 return p + 4;
6095}
6096
6097static bfd_byte *
6098restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6099{
6100 p = restfpr (abfd, p, r);
6101 bfd_put_32 (abfd, BLR, p);
6102 return p + 4;
6103}
6104
6105static bfd_byte *
6106savevr (bfd *abfd, bfd_byte *p, int r)
6107{
6108 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6109 p = p + 4;
6110 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6111 return p + 4;
6112}
6113
6114static bfd_byte *
6115savevr_tail (bfd *abfd, bfd_byte *p, int r)
6116{
6117 p = savevr (abfd, p, r);
6118 bfd_put_32 (abfd, BLR, p);
6119 return p + 4;
6120}
6121
6122static bfd_byte *
6123restvr (bfd *abfd, bfd_byte *p, int r)
6124{
6125 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6126 p = p + 4;
6127 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6128 return p + 4;
6129}
6130
6131static bfd_byte *
6132restvr_tail (bfd *abfd, bfd_byte *p, int r)
6133{
6134 p = restvr (abfd, p, r);
6135 bfd_put_32 (abfd, BLR, p);
6136 return p + 4;
6137}
6138
9e7028aa
AM
6139#define STDU_R1_0R1 0xf8210001
6140#define ADDI_R1_R1 0x38210000
6141
6142/* Emit prologue of wrapper preserving regs around a call to
6143 __tls_get_addr_opt. */
6144
6145static bfd_byte *
6146tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6147{
6148 unsigned int i;
6149
6150 bfd_put_32 (obfd, MFLR_R0, p);
6151 p += 4;
6152 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6153 p += 4;
6154
6155 if (htab->opd_abi)
6156 {
6157 for (i = 4; i < 12; i++)
6158 {
6159 bfd_put_32 (obfd,
6160 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6161 p += 4;
6162 }
6163 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6164 p += 4;
6165 }
6166 else
6167 {
6168 for (i = 4; i < 12; i++)
6169 {
6170 bfd_put_32 (obfd,
6171 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6172 p += 4;
6173 }
6174 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6175 p += 4;
6176 }
6177 return p;
6178}
6179
6180/* Emit epilogue of wrapper preserving regs around a call to
6181 __tls_get_addr_opt. */
6182
6183static bfd_byte *
6184tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6185{
6186 unsigned int i;
6187
6188 if (htab->opd_abi)
6189 {
6190 for (i = 4; i < 12; i++)
6191 {
6192 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6193 p += 4;
6194 }
6195 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6196 p += 4;
6197 }
6198 else
6199 {
6200 for (i = 4; i < 12; i++)
6201 {
6202 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6203 p += 4;
6204 }
6205 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6206 p += 4;
6207 }
6208 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6209 p += 4;
6210 bfd_put_32 (obfd, MTLR_R0, p);
6211 p += 4;
6212 bfd_put_32 (obfd, BLR, p);
6213 p += 4;
6214 return p;
6215}
6216
e86ce104
AM
6217/* Called via elf_link_hash_traverse to transfer dynamic linking
6218 information on function code symbol entries to their corresponding
6219 function descriptor symbol entries. */
deb0e272 6220
b34976b6 6221static bfd_boolean
4ce794b7 6222func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6223{
e86ce104 6224 struct bfd_link_info *info;
65f38f15 6225 struct ppc_link_hash_table *htab;
50bc7936
AM
6226 struct ppc_link_hash_entry *fh;
6227 struct ppc_link_hash_entry *fdh;
6228 bfd_boolean force_local;
5bd4f169 6229
ed7007c1 6230 fh = ppc_elf_hash_entry (h);
50bc7936 6231 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6232 return TRUE;
e86ce104 6233
8c5b4e52
AM
6234 if (!fh->is_func)
6235 return TRUE;
6236
6237 if (fh->elf.root.root.string[0] != '.'
6238 || fh->elf.root.root.string[1] == '\0')
6239 return TRUE;
6240
4ce794b7 6241 info = inf;
65f38f15 6242 htab = ppc_hash_table (info);
4dfe6ac6
NC
6243 if (htab == NULL)
6244 return FALSE;
5bd4f169 6245
8c5b4e52
AM
6246 /* Find the corresponding function descriptor symbol. */
6247 fdh = lookup_fdh (fh, htab);
6248
c09bdfe5
AM
6249 /* Resolve undefined references to dot-symbols as the value
6250 in the function descriptor, if we have one in a regular object.
6251 This is to satisfy cases like ".quad .foo". Calls to functions
6252 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6253 if ((fh->elf.root.type == bfd_link_hash_undefined
6254 || fh->elf.root.type == bfd_link_hash_undefweak)
6255 && (fdh->elf.root.type == bfd_link_hash_defined
6256 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6257 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6258 && opd_entry_value (fdh->elf.root.u.def.section,
6259 fdh->elf.root.u.def.value,
c09bdfe5 6260 &fh->elf.root.u.def.section,
aef36ac1 6261 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6262 {
b31867b6 6263 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6264 fh->elf.forced_local = 1;
b31867b6
AM
6265 fh->elf.def_regular = fdh->elf.def_regular;
6266 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6267 }
6268
8c5b4e52
AM
6269 if (!fh->elf.dynamic)
6270 {
6271 struct plt_entry *ent;
5bd4f169 6272
8c5b4e52
AM
6273 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6274 if (ent->plt.refcount > 0)
6275 break;
6276 if (ent == NULL)
6277 return TRUE;
6278 }
5bd4f169 6279
8c5b4e52 6280 /* Create a descriptor as undefined if necessary. */
50bc7936 6281 if (fdh == NULL
0e1862bb 6282 && !bfd_link_executable (info)
50bc7936
AM
6283 && (fh->elf.root.type == bfd_link_hash_undefined
6284 || fh->elf.root.type == bfd_link_hash_undefweak))
6285 {
908b32fc 6286 fdh = make_fdh (info, fh);
bb700d78
AM
6287 if (fdh == NULL)
6288 return FALSE;
50bc7936 6289 }
648cca2c 6290
8c5b4e52 6291 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6292 if (fdh != NULL
6293 && fdh->fake
8c5b4e52
AM
6294 && (fh->elf.root.type == bfd_link_hash_defined
6295 || fh->elf.root.type == bfd_link_hash_defweak))
6296 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 6297
8c5b4e52
AM
6298 /* Transfer dynamic linking information to the function descriptor. */
6299 if (fdh != NULL)
6300 {
f5385ebf
AM
6301 fdh->elf.ref_regular |= fh->elf.ref_regular;
6302 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6303 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6304 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6305 fdh->elf.dynamic |= fh->elf.dynamic;
6306 fdh->elf.needs_plt |= (fh->elf.needs_plt
6307 || fh->elf.type == STT_FUNC
6308 || fh->elf.type == STT_GNU_IFUNC);
6309 move_plt_plist (fh, fdh);
6310
6311 if (!fdh->elf.forced_local
6312 && fh->elf.dynindx != -1)
6313 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6314 return FALSE;
e86ce104
AM
6315 }
6316
50bc7936
AM
6317 /* Now that the info is on the function descriptor, clear the
6318 function code sym info. Any function code syms for which we
6319 don't have a definition in a regular file, we force local.
6320 This prevents a shared library from exporting syms that have
6321 been imported from another library. Function code syms that
6322 are really in the library we must leave global to prevent the
6323 linker dragging in a definition from a static library. */
93f3fa99
AM
6324 force_local = (!fh->elf.def_regular
6325 || fdh == NULL
6326 || !fdh->elf.def_regular
6327 || fdh->elf.forced_local);
50bc7936
AM
6328 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6329
b34976b6 6330 return TRUE;
e86ce104 6331}
40b8271b 6332
a4b6fadd
AM
6333static const struct sfpr_def_parms save_res_funcs[] =
6334 {
6335 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6336 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6337 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6338 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6339 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6340 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6341 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6342 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6343 { "._savef", 14, 31, savefpr, savefpr1_tail },
6344 { "._restf", 14, 31, restfpr, restfpr1_tail },
6345 { "_savevr_", 20, 31, savevr, savevr_tail },
6346 { "_restvr_", 20, 31, restvr, restvr_tail }
6347 };
6348
e86ce104 6349/* Called near the start of bfd_elf_size_dynamic_sections. We use
c9405344
AM
6350 this hook to a) run the edit functions in this file, b) provide
6351 some gcc support functions, and c) transfer dynamic linking
6352 information gathered so far on function code symbol entries, to
6353 their corresponding function descriptor symbol entries. */
deb0e272 6354
b34976b6 6355static bfd_boolean
c9405344 6356ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
e86ce104
AM
6357{
6358 struct ppc_link_hash_table *htab;
6359
6360 htab = ppc_hash_table (info);
4dfe6ac6
NC
6361 if (htab == NULL)
6362 return FALSE;
6363
c9405344
AM
6364 /* Call back into the linker, which then runs the edit functions. */
6365 htab->params->edit ();
6366
b32547cd
AM
6367 /* Provide any missing _save* and _rest* functions. */
6368 if (htab->sfpr != NULL)
6369 {
6370 unsigned int i;
6371
6372 htab->sfpr->size = 0;
6373 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6374 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6375 return FALSE;
6376 if (htab->sfpr->size == 0)
6377 htab->sfpr->flags |= SEC_EXCLUDE;
6378 }
6379
6380 if (bfd_link_relocatable (info))
6381 return TRUE;
6382
6383 if (htab->elf.hgot != NULL)
dba6fa9b
AM
6384 {
6385 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6386 /* Make .TOC. defined so as to prevent it being made dynamic.
6387 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6388 if (!htab->elf.hgot->def_regular
6389 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6390 {
6391 htab->elf.hgot->root.type = bfd_link_hash_defined;
6392 htab->elf.hgot->root.u.def.value = 0;
6393 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6394 htab->elf.hgot->def_regular = 1;
6395 htab->elf.hgot->root.linker_def = 1;
6396 }
dba6fa9b 6397 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6398 htab->elf.hgot->other
6399 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6400 }
c66bb0ee 6401
8c5b4e52
AM
6402 if (htab->need_func_desc_adj)
6403 {
6404 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6405 htab->need_func_desc_adj = 0;
6406 }
805fc799 6407
b34976b6 6408 return TRUE;
e86ce104
AM
6409}
6410
d311bc8b 6411/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6412 aliases, that apply to read-only sections. Cannot be used after
6413 size_dynamic_sections. */
d311bc8b
AM
6414
6415static bfd_boolean
6416alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6417{
ed7007c1 6418 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
d311bc8b
AM
6419 do
6420 {
5dbc8b37 6421 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
d311bc8b 6422 return TRUE;
ed7007c1 6423 eh = ppc_elf_hash_entry (eh->elf.u.alias);
2cdcc330
AM
6424 }
6425 while (eh != NULL && &eh->elf != h);
d311bc8b
AM
6426
6427 return FALSE;
6428}
8a2058b5 6429
8a9e8e72
AM
6430/* Return whether EH has pc-relative dynamic relocs. */
6431
6432static bfd_boolean
6433pc_dynrelocs (struct ppc_link_hash_entry *eh)
6434{
6435 struct elf_dyn_relocs *p;
6436
190eb1dd 6437 for (p = eh->elf.dyn_relocs; p != NULL; p = p->next)
8a9e8e72
AM
6438 if (p->pc_count != 0)
6439 return TRUE;
6440 return FALSE;
6441}
6442
8a2058b5
AM
6443/* Return true if a global entry stub will be created for H. Valid
6444 for ELFv2 before plt entries have been allocated. */
6445
6446static bfd_boolean
6447global_entry_stub (struct elf_link_hash_entry *h)
6448{
6449 struct plt_entry *pent;
6450
6451 if (!h->pointer_equality_needed
6452 || h->def_regular)
6453 return FALSE;
6454
6455 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6456 if (pent->plt.refcount > 0
6457 && pent->addend == 0)
6458 return TRUE;
6459
6460 return FALSE;
6461}
6462
e86ce104
AM
6463/* Adjust a symbol defined by a dynamic object and referenced by a
6464 regular object. The current definition is in some section of the
6465 dynamic object, but we're not including those sections. We have to
6466 change the definition to something the rest of the link can
6467 understand. */
6468
b34976b6 6469static bfd_boolean
4ce794b7
AM
6470ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6471 struct elf_link_hash_entry *h)
e86ce104
AM
6472{
6473 struct ppc_link_hash_table *htab;
5474d94f 6474 asection *s, *srel;
e86ce104
AM
6475
6476 htab = ppc_hash_table (info);
4dfe6ac6
NC
6477 if (htab == NULL)
6478 return FALSE;
e86ce104
AM
6479
6480 /* Deal with function syms. */
6481 if (h->type == STT_FUNC
e054468f 6482 || h->type == STT_GNU_IFUNC
f5385ebf 6483 || h->needs_plt)
e86ce104 6484 {
ed7007c1 6485 bfd_boolean local = (ppc_elf_hash_entry (h)->save_res
529fe20e
AM
6486 || SYMBOL_CALLS_LOCAL (info, h)
6487 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6488 /* Discard dyn_relocs when non-pic if we've decided that a
6489 function symbol is local and not an ifunc. We keep dynamic
6490 relocs for ifuncs when local rather than always emitting a
6491 plt call stub for them and defining the symbol on the call
6492 stub. We can't do that for ELFv1 anyway (a function symbol
6493 is defined on a descriptor, not code) and it can be faster at
6494 run-time due to not needing to bounce through a stub. The
6495 dyn_relocs for ifuncs will be applied even in a static
6496 executable. */
6497 if (!bfd_link_pic (info)
6498 && h->type != STT_GNU_IFUNC
6499 && local)
190eb1dd 6500 h->dyn_relocs = NULL;
529fe20e 6501
e86ce104
AM
6502 /* Clear procedure linkage table information for any symbol that
6503 won't need a .plt entry. */
411e1bfb
AM
6504 struct plt_entry *ent;
6505 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6506 if (ent->plt.refcount > 0)
6507 break;
8387904d 6508 if (ent == NULL
2d7ad24e
AM
6509 || (h->type != STT_GNU_IFUNC
6510 && local
3e04d765 6511 && (htab->can_convert_all_inline_plt
ed7007c1 6512 || (ppc_elf_hash_entry (h)->tls_mask
3e04d765 6513 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6514 {
411e1bfb 6515 h->plt.plist = NULL;
f5385ebf 6516 h->needs_plt = 0;
d1eca1e4 6517 h->pointer_equality_needed = 0;
40b8271b 6518 }
8a2058b5 6519 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6520 {
d1eca1e4
AM
6521 /* Taking a function's address in a read/write section
6522 doesn't require us to define the function symbol in the
6523 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6524 be used instead. The reason we prefer a few more dynamic
6525 relocs is that calling via a global entry stub costs a
6526 few more instructions, and pointer_equality_needed causes
6527 extra work in ld.so when resolving these symbols. */
529fe20e 6528 if (global_entry_stub (h))
d1eca1e4 6529 {
5dbc8b37 6530 if (!_bfd_elf_readonly_dynrelocs (h))
529fe20e
AM
6531 {
6532 h->pointer_equality_needed = 0;
04383fd1
AM
6533 /* If we haven't seen a branch reloc and the symbol
6534 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6535 if (!h->needs_plt)
6536 h->plt.plist = NULL;
6537 }
6538 else if (!bfd_link_pic (info))
6539 /* We are going to be defining the function symbol on the
6540 plt stub, so no dyn_relocs needed when non-pic. */
190eb1dd 6541 h->dyn_relocs = NULL;
d1eca1e4
AM
6542 }
6543
3988aed5
AM
6544 /* ELFv2 function symbols can't have copy relocs. */
6545 return TRUE;
6546 }
6547 else if (!h->needs_plt
5dbc8b37 6548 && !_bfd_elf_readonly_dynrelocs (h))
3988aed5 6549 {
04383fd1
AM
6550 /* If we haven't seen a branch reloc and the symbol isn't an
6551 ifunc then we don't need a plt entry. */
3988aed5
AM
6552 h->plt.plist = NULL;
6553 h->pointer_equality_needed = 0;
a345bc8d
AM
6554 return TRUE;
6555 }
5bd4f169 6556 }
bbd7ec4a 6557 else
411e1bfb 6558 h->plt.plist = NULL;
5bd4f169
AM
6559
6560 /* If this is a weak symbol, and there is a real definition, the
6561 processor independent code will have arranged for us to see the
6562 real definition first, and we can just use the same value. */
60d67dc8 6563 if (h->is_weakalias)
5bd4f169 6564 {
60d67dc8
AM
6565 struct elf_link_hash_entry *def = weakdef (h);
6566 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6567 h->root.u.def.section = def->root.u.def.section;
6568 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6569 if (def->root.u.def.section == htab->elf.sdynbss
6570 || def->root.u.def.section == htab->elf.sdynrelro)
190eb1dd 6571 h->dyn_relocs = NULL;
b34976b6 6572 return TRUE;
5bd4f169
AM
6573 }
6574
5bd4f169
AM
6575 /* If we are creating a shared library, we must presume that the
6576 only references to the symbol are via the global offset table.
6577 For such cases we need not do anything here; the relocations will
6578 be handled correctly by relocate_section. */
ec73ddcd 6579 if (!bfd_link_executable (info))
b34976b6 6580 return TRUE;
5bd4f169 6581
65f38f15
AM
6582 /* If there are no references to this symbol that do not use the
6583 GOT, we don't need to generate a copy reloc. */
f5385ebf 6584 if (!h->non_got_ref)
b34976b6 6585 return TRUE;
65f38f15 6586
b186458a 6587 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6588 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6589
d93d1c80
AM
6590 /* If -z nocopyreloc was given, don't generate them either. */
6591 || info->nocopyreloc
a127494f 6592
dce2246a 6593 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6594 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1bdd8fac
AM
6595 || (ELIMINATE_COPY_RELOCS
6596 && !h->needs_copy
6597 && !alias_readonly_dynrelocs (h))
65f38f15 6598
d93d1c80
AM
6599 /* Protected variables do not work with .dynbss. The copy in
6600 .dynbss won't be used by the shared library with the protected
6601 definition for the variable. Text relocations are preferable
6602 to an incorrect program. */
6603 || h->protected_def)
529fe20e 6604 return TRUE;
a127494f 6605
e1c6cf61
AM
6606 if (h->type == STT_FUNC
6607 || h->type == STT_GNU_IFUNC)
97b639ba 6608 {
e1c6cf61
AM
6609 /* .dynbss copies of function symbols only work if we have
6610 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6611 use dot-symbols and set the function symbol size to the text
6612 size of the function rather than the size of the descriptor.
6613 That's wrong for copying a descriptor. */
ed7007c1 6614 if (ppc_elf_hash_entry (h)->oh == NULL
e1c6cf61
AM
6615 || !(h->size == 24 || h->size == 16))
6616 return TRUE;
6617
6618 /* We should never get here, but unfortunately there are old
6619 versions of gcc (circa gcc-3.2) that improperly for the
6620 ELFv1 ABI put initialized function pointers, vtable refs and
6621 suchlike in read-only sections. Allow them to proceed, but
6622 warn that this might break at runtime. */
25f53a85 6623 info->callbacks->einfo
c1c8c1ef 6624 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6625 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6626 h->root.root.string);
6627 }
5d35169e
AM
6628
6629 /* This is a reference to a symbol defined by a dynamic object which
6630 is not a function. */
6631
5bd4f169
AM
6632 /* We must allocate the symbol in our .dynbss section, which will
6633 become part of the .bss section of the executable. There will be
6634 an entry for this symbol in the .dynsym section. The dynamic
6635 object will contain position independent code, so all references
6636 from the dynamic object to this symbol will go through the global
6637 offset table. The dynamic linker will use the .dynsym entry to
6638 determine the address it must put in the global offset table, so
6639 both the dynamic object and the regular object will refer to the
6640 same memory location for the variable. */
5474d94f
AM
6641 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6642 {
6643 s = htab->elf.sdynrelro;
6644 srel = htab->elf.sreldynrelro;
6645 }
6646 else
6647 {
6648 s = htab->elf.sdynbss;
6649 srel = htab->elf.srelbss;
6650 }
1d7e9d18 6651 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6652 {
4a7e5234
AM
6653 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6654 linker to copy the initial value out of the dynamic object
6655 and into the runtime process image. */
5474d94f 6656 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6657 h->needs_copy = 1;
5bd4f169
AM
6658 }
6659
529fe20e 6660 /* We no longer want dyn_relocs. */
190eb1dd 6661 h->dyn_relocs = NULL;
6cabe1ea 6662 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6663}
6664
e86ce104
AM
6665/* If given a function descriptor symbol, hide both the function code
6666 sym and the descriptor. */
6667static void
4ce794b7
AM
6668ppc64_elf_hide_symbol (struct bfd_link_info *info,
6669 struct elf_link_hash_entry *h,
6670 bfd_boolean force_local)
e86ce104 6671{
34814b9f 6672 struct ppc_link_hash_entry *eh;
e86ce104
AM
6673 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6674
87469ba2
AM
6675 if (ppc_hash_table (info) == NULL)
6676 return;
6677
ed7007c1 6678 eh = ppc_elf_hash_entry (h);
34814b9f 6679 if (eh->is_func_descriptor)
e86ce104 6680 {
34814b9f 6681 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6682
721956f4 6683 if (fh == NULL)
d1329ca3
AM
6684 {
6685 const char *p, *q;
b8ac2841 6686 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6687 char save;
6688
6689 /* We aren't supposed to use alloca in BFD because on
6690 systems which do not have alloca the version in libiberty
6691 calls xmalloc, which might cause the program to crash
6692 when it runs out of memory. This function doesn't have a
6693 return status, so there's no way to gracefully return an
6694 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6695 accessed; It's either a string in an ELF string table,
6696 or allocated in an objalloc structure. */
d1329ca3 6697
34814b9f 6698 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6699 save = *p;
6700 *(char *) p = '.';
ed7007c1
AM
6701 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, FALSE,
6702 FALSE, FALSE));
d1329ca3
AM
6703 *(char *) p = save;
6704
6705 /* Unfortunately, if it so happens that the string we were
6706 looking for was allocated immediately before this string,
6707 then we overwrote the string terminator. That's the only
6708 reason the lookup should fail. */
6709 if (fh == NULL)
6710 {
34814b9f
AM
6711 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6712 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6713 --q, --p;
34814b9f 6714 if (q < eh->elf.root.root.string && *p == '.')
ed7007c1
AM
6715 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, FALSE,
6716 FALSE, FALSE));
d1329ca3
AM
6717 }
6718 if (fh != NULL)
6719 {
34814b9f
AM
6720 eh->oh = fh;
6721 fh->oh = eh;
d1329ca3
AM
6722 }
6723 }
e86ce104 6724 if (fh != NULL)
34814b9f 6725 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6726 }
6727}
6728
411e1bfb 6729static bfd_boolean
8843416a
AM
6730get_sym_h (struct elf_link_hash_entry **hp,
6731 Elf_Internal_Sym **symp,
6732 asection **symsecp,
f961d9dd 6733 unsigned char **tls_maskp,
8843416a
AM
6734 Elf_Internal_Sym **locsymsp,
6735 unsigned long r_symndx,
6736 bfd *ibfd)
411e1bfb 6737{
0ffa91dd 6738 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6739
6740 if (r_symndx >= symtab_hdr->sh_info)
6741 {
6742 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6743 struct elf_link_hash_entry *h;
6744
6745 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6746 h = elf_follow_link (h);
411e1bfb
AM
6747
6748 if (hp != NULL)
6749 *hp = h;
6750
6751 if (symp != NULL)
6752 *symp = NULL;
6753
6754 if (symsecp != NULL)
6755 {
6756 asection *symsec = NULL;
6757 if (h->root.type == bfd_link_hash_defined
6758 || h->root.type == bfd_link_hash_defweak)
6759 symsec = h->root.u.def.section;
6760 *symsecp = symsec;
6761 }
6762
e7b938ca 6763 if (tls_maskp != NULL)
ed7007c1 6764 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
411e1bfb
AM
6765 }
6766 else
6767 {
6768 Elf_Internal_Sym *sym;
6769 Elf_Internal_Sym *locsyms = *locsymsp;
6770
6771 if (locsyms == NULL)
6772 {
6773 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6774 if (locsyms == NULL)
6775 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6776 symtab_hdr->sh_info,
6777 0, NULL, NULL, NULL);
6778 if (locsyms == NULL)
6779 return FALSE;
6780 *locsymsp = locsyms;
6781 }
6782 sym = locsyms + r_symndx;
6783
6784 if (hp != NULL)
6785 *hp = NULL;
6786
6787 if (symp != NULL)
6788 *symp = sym;
6789
6790 if (symsecp != NULL)
cb33740c 6791 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6792
e7b938ca 6793 if (tls_maskp != NULL)
411e1bfb
AM
6794 {
6795 struct got_entry **lgot_ents;
f961d9dd 6796 unsigned char *tls_mask;
411e1bfb 6797
e7b938ca 6798 tls_mask = NULL;
411e1bfb
AM
6799 lgot_ents = elf_local_got_ents (ibfd);
6800 if (lgot_ents != NULL)
6801 {
e054468f
AM
6802 struct plt_entry **local_plt = (struct plt_entry **)
6803 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6804 unsigned char *lgot_masks = (unsigned char *)
e054468f 6805 (local_plt + symtab_hdr->sh_info);
e7b938ca 6806 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6807 }
e7b938ca 6808 *tls_maskp = tls_mask;
411e1bfb
AM
6809 }
6810 }
6811 return TRUE;
6812}
6813
e7b938ca 6814/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6815 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6816 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6817
6818static int
f961d9dd 6819get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6820 unsigned long *toc_symndx,
6821 bfd_vma *toc_addend,
0d4792f7 6822 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6823 const Elf_Internal_Rela *rel,
6824 bfd *ibfd)
411e1bfb
AM
6825{
6826 unsigned long r_symndx;
0d4792f7 6827 int next_r;
411e1bfb
AM
6828 struct elf_link_hash_entry *h;
6829 Elf_Internal_Sym *sym;
6830 asection *sec;
6831 bfd_vma off;
6832
6833 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6834 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6835 return 0;
411e1bfb 6836
37da22e5
AM
6837 if ((*tls_maskp != NULL
6838 && (**tls_maskp & TLS_TLS) != 0
6839 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 6840 || sec == NULL
6bee8834 6841 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6842 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6843 return 1;
411e1bfb
AM
6844
6845 /* Look inside a TOC section too. */
6846 if (h != NULL)
6847 {
6848 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6849 off = h->root.u.def.value;
6850 }
6851 else
6852 off = sym->st_value;
6853 off += rel->r_addend;
6854 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6855 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6856 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6857 if (toc_symndx != NULL)
6858 *toc_symndx = r_symndx;
3a71aa26
AM
6859 if (toc_addend != NULL)
6860 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6861 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6862 return 0;
854b41e7 6863 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6864 && (next_r == -1 || next_r == -2))
6865 return 1 - next_r;
951fd09b 6866 return 1;
411e1bfb
AM
6867}
6868
3b421ab3
AM
6869/* Find (or create) an entry in the tocsave hash table. */
6870
6871static struct tocsave_entry *
6872tocsave_find (struct ppc_link_hash_table *htab,
6873 enum insert_option insert,
6874 Elf_Internal_Sym **local_syms,
6875 const Elf_Internal_Rela *irela,
6876 bfd *ibfd)
6877{
6878 unsigned long r_indx;
6879 struct elf_link_hash_entry *h;
6880 Elf_Internal_Sym *sym;
6881 struct tocsave_entry ent, *p;
6882 hashval_t hash;
6883 struct tocsave_entry **slot;
6884
6885 r_indx = ELF64_R_SYM (irela->r_info);
6886 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6887 return NULL;
6888 if (ent.sec == NULL || ent.sec->output_section == NULL)
6889 {
4eca0228 6890 _bfd_error_handler
871b3ab2 6891 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
6892 return NULL;
6893 }
6894
6895 if (h != NULL)
6896 ent.offset = h->root.u.def.value;
6897 else
6898 ent.offset = sym->st_value;
6899 ent.offset += irela->r_addend;
6900
6901 hash = tocsave_htab_hash (&ent);
6902 slot = ((struct tocsave_entry **)
6903 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6904 if (slot == NULL)
6905 return NULL;
6906
6907 if (*slot == NULL)
6908 {
6909 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6910 if (p == NULL)
6911 return NULL;
6912 *p = ent;
6913 *slot = p;
6914 }
6915 return *slot;
6916}
6917
754021d0 6918/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6919 code for the old ABI, these will already have been done. */
754021d0
AM
6920
6921static bfd_boolean
6922adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6923{
6924 struct ppc_link_hash_entry *eh;
6925 asection *sym_sec;
74f0fb50 6926 struct _opd_sec_data *opd;
754021d0
AM
6927
6928 if (h->root.type == bfd_link_hash_indirect)
6929 return TRUE;
6930
754021d0
AM
6931 if (h->root.type != bfd_link_hash_defined
6932 && h->root.type != bfd_link_hash_defweak)
6933 return TRUE;
6934
ed7007c1 6935 eh = ppc_elf_hash_entry (h);
754021d0
AM
6936 if (eh->adjust_done)
6937 return TRUE;
6938
6939 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6940 opd = get_opd_info (sym_sec);
6941 if (opd != NULL && opd->adjust != NULL)
754021d0 6942 {
51aecdc5 6943 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
6944 if (adjust == -1)
6945 {
6946 /* This entry has been deleted. */
b3fac117 6947 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6948 if (dsec == NULL)
6949 {
6950 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 6951 if (discarded_section (dsec))
81688140 6952 {
b3fac117 6953 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6954 break;
6955 }
6956 }
4025353c 6957 eh->elf.root.u.def.value = 0;
81688140 6958 eh->elf.root.u.def.section = dsec;
4025353c
AM
6959 }
6960 else
6961 eh->elf.root.u.def.value += adjust;
754021d0
AM
6962 eh->adjust_done = 1;
6963 }
6964 return TRUE;
6965}
6966
8c1d1bb8 6967/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 6968 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
6969 have already been determined. */
6970
6971static bfd_boolean
6972dec_dynrel_count (bfd_vma r_info,
6973 asection *sec,
6974 struct bfd_link_info *info,
6975 Elf_Internal_Sym **local_syms,
6976 struct elf_link_hash_entry *h,
19e08130 6977 Elf_Internal_Sym *sym)
8c1d1bb8
AM
6978{
6979 enum elf_ppc64_reloc_type r_type;
19e08130 6980 asection *sym_sec = NULL;
8c1d1bb8
AM
6981
6982 /* Can this reloc be dynamic? This switch, and later tests here
6983 should be kept in sync with the code in check_relocs. */
6984 r_type = ELF64_R_TYPE (r_info);
6985 switch (r_type)
6986 {
6987 default:
6988 return TRUE;
6989
1bdd8fac
AM
6990 case R_PPC64_TOC16:
6991 case R_PPC64_TOC16_DS:
6992 case R_PPC64_TOC16_LO:
6993 case R_PPC64_TOC16_HI:
6994 case R_PPC64_TOC16_HA:
6995 case R_PPC64_TOC16_LO_DS:
6996 if (h == NULL)
6997 return TRUE;
6998 break;
6999
8c1d1bb8
AM
7000 case R_PPC64_TPREL16:
7001 case R_PPC64_TPREL16_LO:
7002 case R_PPC64_TPREL16_HI:
7003 case R_PPC64_TPREL16_HA:
7004 case R_PPC64_TPREL16_DS:
7005 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7006 case R_PPC64_TPREL16_HIGH:
7007 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7008 case R_PPC64_TPREL16_HIGHER:
7009 case R_PPC64_TPREL16_HIGHERA:
7010 case R_PPC64_TPREL16_HIGHEST:
7011 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8 7012 case R_PPC64_TPREL64:
c213164a 7013 case R_PPC64_TPREL34:
8c1d1bb8
AM
7014 case R_PPC64_DTPMOD64:
7015 case R_PPC64_DTPREL64:
7016 case R_PPC64_ADDR64:
7017 case R_PPC64_REL30:
7018 case R_PPC64_REL32:
7019 case R_PPC64_REL64:
7020 case R_PPC64_ADDR14:
7021 case R_PPC64_ADDR14_BRNTAKEN:
7022 case R_PPC64_ADDR14_BRTAKEN:
7023 case R_PPC64_ADDR16:
7024 case R_PPC64_ADDR16_DS:
7025 case R_PPC64_ADDR16_HA:
7026 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7027 case R_PPC64_ADDR16_HIGH:
7028 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7029 case R_PPC64_ADDR16_HIGHER:
7030 case R_PPC64_ADDR16_HIGHERA:
7031 case R_PPC64_ADDR16_HIGHEST:
7032 case R_PPC64_ADDR16_HIGHESTA:
7033 case R_PPC64_ADDR16_LO:
7034 case R_PPC64_ADDR16_LO_DS:
7035 case R_PPC64_ADDR24:
7036 case R_PPC64_ADDR32:
7037 case R_PPC64_UADDR16:
7038 case R_PPC64_UADDR32:
7039 case R_PPC64_UADDR64:
7040 case R_PPC64_TOC:
5663e321
AM
7041 case R_PPC64_D34:
7042 case R_PPC64_D34_LO:
7043 case R_PPC64_D34_HI30:
7044 case R_PPC64_D34_HA30:
7045 case R_PPC64_ADDR16_HIGHER34:
7046 case R_PPC64_ADDR16_HIGHERA34:
7047 case R_PPC64_ADDR16_HIGHEST34:
7048 case R_PPC64_ADDR16_HIGHESTA34:
7049 case R_PPC64_D28:
8c1d1bb8
AM
7050 break;
7051 }
7052
7053 if (local_syms != NULL)
7054 {
7055 unsigned long r_symndx;
8c1d1bb8
AM
7056 bfd *ibfd = sec->owner;
7057
7058 r_symndx = ELF64_R_SYM (r_info);
7059 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7060 return FALSE;
7061 }
7062
ec73ddcd
AM
7063 if ((h != NULL
7064 && (h->root.type == bfd_link_hash_defweak
7065 || !h->def_regular))
7066 || (h != NULL
7067 && !bfd_link_executable (info)
7068 && !SYMBOLIC_BIND (info, h))
7069 || (bfd_link_pic (info)
7070 && must_be_dyn_reloc (info, r_type))
7071 || (!bfd_link_pic (info)
7072 && (h != NULL
7073 ? h->type == STT_GNU_IFUNC
7074 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
8c1d1bb8
AM
7075 ;
7076 else
7077 return TRUE;
7078
7079 if (h != NULL)
6edfbbad 7080 {
19e08130
AM
7081 struct elf_dyn_relocs *p;
7082 struct elf_dyn_relocs **pp;
190eb1dd 7083 pp = &h->dyn_relocs;
19e08130
AM
7084
7085 /* elf_gc_sweep may have already removed all dyn relocs associated
7086 with local syms for a given section. Also, symbol flags are
7087 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7088 report a dynreloc miscount. */
7089 if (*pp == NULL && info->gc_sections)
7090 return TRUE;
7091
7092 while ((p = *pp) != NULL)
60124e18 7093 {
19e08130
AM
7094 if (p->sec == sec)
7095 {
7096 if (!must_be_dyn_reloc (info, r_type))
7097 p->pc_count -= 1;
7098 p->count -= 1;
7099 if (p->count == 0)
7100 *pp = p->next;
7101 return TRUE;
7102 }
7103 pp = &p->next;
60124e18 7104 }
6edfbbad 7105 }
19e08130
AM
7106 else
7107 {
7108 struct ppc_dyn_relocs *p;
7109 struct ppc_dyn_relocs **pp;
7110 void *vpp;
7111 bfd_boolean is_ifunc;
8c1d1bb8 7112
19e08130
AM
7113 if (local_syms == NULL)
7114 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7115 if (sym_sec == NULL)
7116 sym_sec = sec;
c57da1a7 7117
19e08130
AM
7118 vpp = &elf_section_data (sym_sec)->local_dynrel;
7119 pp = (struct ppc_dyn_relocs **) vpp;
7120
7121 if (*pp == NULL && info->gc_sections)
7122 return TRUE;
7123
7124 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7125 while ((p = *pp) != NULL)
8c1d1bb8 7126 {
19e08130
AM
7127 if (p->sec == sec && p->ifunc == is_ifunc)
7128 {
7129 p->count -= 1;
7130 if (p->count == 0)
7131 *pp = p->next;
7132 return TRUE;
7133 }
7134 pp = &p->next;
8c1d1bb8 7135 }
8c1d1bb8
AM
7136 }
7137
695344c0 7138 /* xgettext:c-format */
cf97bcb0
AM
7139 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7140 sec->owner, sec);
8c1d1bb8
AM
7141 bfd_set_error (bfd_error_bad_value);
7142 return FALSE;
7143}
7144
754021d0
AM
7145/* Remove unused Official Procedure Descriptor entries. Currently we
7146 only remove those associated with functions in discarded link-once
7147 sections, or weakly defined functions that have been overridden. It
7148 would be possible to remove many more entries for statically linked
7149 applications. */
7150
b34976b6 7151bfd_boolean
e7d1c40c 7152ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7153{
7154 bfd *ibfd;
754021d0 7155 bfd_boolean some_edited = FALSE;
3f764659 7156 asection *need_pad = NULL;
e7d1c40c
AM
7157 struct ppc_link_hash_table *htab;
7158
7159 htab = ppc_hash_table (info);
7160 if (htab == NULL)
7161 return FALSE;
1e2f5b6e 7162
c72f2fb2 7163 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7164 {
7165 asection *sec;
7166 Elf_Internal_Rela *relstart, *rel, *relend;
7167 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7168 Elf_Internal_Sym *local_syms;
74f0fb50 7169 struct _opd_sec_data *opd;
51aecdc5 7170 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7171 bfd_size_type cnt_16b = 0;
1e2f5b6e 7172
854b41e7
AM
7173 if (!is_ppc64_elf (ibfd))
7174 continue;
7175
1e2f5b6e 7176 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7177 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7178 continue;
7179
dbaa2011 7180 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7181 continue;
7182
1e2f5b6e
AM
7183 if (sec->output_section == bfd_abs_section_ptr)
7184 continue;
7185
7186 /* Look through the section relocs. */
7187 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7188 continue;
7189
6cdc0ccc 7190 local_syms = NULL;
0ffa91dd 7191 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7192
7193 /* Read the relocations. */
4ce794b7 7194 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7195 info->keep_memory);
1e2f5b6e 7196 if (relstart == NULL)
b34976b6 7197 return FALSE;
1e2f5b6e
AM
7198
7199 /* First run through the relocs to check they are sane, and to
7200 determine whether we need to edit this opd section. */
b34976b6 7201 need_edit = FALSE;
51aecdc5 7202 broken = FALSE;
3f764659 7203 need_pad = sec;
1e2f5b6e 7204 relend = relstart + sec->reloc_count;
50bc7936 7205 for (rel = relstart; rel < relend; )
1e2f5b6e 7206 {
04c9666a 7207 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7208 unsigned long r_symndx;
7209 asection *sym_sec;
7210 struct elf_link_hash_entry *h;
7211 Elf_Internal_Sym *sym;
51aecdc5 7212 bfd_vma offset;
1e2f5b6e 7213
51aecdc5 7214 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7215 only interested in the reloc pointing to a function entry
7216 point. */
51aecdc5
AM
7217 offset = rel->r_offset;
7218 if (rel + 1 == relend
7219 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7220 {
7221 /* If someone messes with .opd alignment then after a
7222 "ld -r" we might have padding in the middle of .opd.
7223 Also, there's nothing to prevent someone putting
7224 something silly in .opd with the assembler. No .opd
b34976b6 7225 optimization for them! */
3f764659 7226 broken_opd:
4eca0228 7227 _bfd_error_handler
871b3ab2 7228 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7229 broken = TRUE;
1e2f5b6e
AM
7230 break;
7231 }
7232
50bc7936
AM
7233 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7234 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7235 {
4eca0228 7236 _bfd_error_handler
695344c0 7237 /* xgettext:c-format */
871b3ab2 7238 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7239 ibfd, r_type);
51aecdc5 7240 broken = TRUE;
50bc7936
AM
7241 break;
7242 }
7243
1e2f5b6e 7244 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7245 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7246 r_symndx, ibfd))
50bc7936 7247 goto error_ret;
1e2f5b6e
AM
7248
7249 if (sym_sec == NULL || sym_sec->owner == NULL)
7250 {
411e1bfb
AM
7251 const char *sym_name;
7252 if (h != NULL)
7253 sym_name = h->root.root.string;
7254 else
26c61ae5
L
7255 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7256 sym_sec);
411e1bfb 7257
4eca0228 7258 _bfd_error_handler
695344c0 7259 /* xgettext:c-format */
871b3ab2 7260 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7261 ibfd, sym_name);
51aecdc5 7262 broken = TRUE;
1e2f5b6e
AM
7263 break;
7264 }
7265
51020317
AM
7266 /* opd entries are always for functions defined in the
7267 current input bfd. If the symbol isn't defined in the
7268 input bfd, then we won't be using the function in this
7269 bfd; It must be defined in a linkonce section in another
7270 bfd, or is weak. It's also possible that we are
7271 discarding the function due to a linker script /DISCARD/,
7272 which we test for via the output_section. */
7273 if (sym_sec->owner != ibfd
7274 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7275 need_edit = TRUE;
1e2f5b6e 7276
50bc7936 7277 rel += 2;
51aecdc5
AM
7278 if (rel + 1 == relend
7279 || (rel + 2 < relend
7280 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7281 ++rel;
7282
7283 if (rel == relend)
3f764659
JJ
7284 {
7285 if (sec->size == offset + 24)
7286 {
7287 need_pad = NULL;
7288 break;
7289 }
51aecdc5 7290 if (sec->size == offset + 16)
3f764659
JJ
7291 {
7292 cnt_16b++;
7293 break;
7294 }
7295 goto broken_opd;
7296 }
3f764659
JJ
7297 else if (rel + 1 < relend
7298 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7299 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7300 {
51aecdc5
AM
7301 if (rel[0].r_offset == offset + 16)
7302 cnt_16b++;
7303 else if (rel[0].r_offset != offset + 24)
7304 goto broken_opd;
3f764659
JJ
7305 }
7306 else
7307 goto broken_opd;
1e2f5b6e
AM
7308 }
7309
e7d1c40c 7310 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7311
51aecdc5 7312 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7313 {
7314 Elf_Internal_Rela *write_rel;
d4730f92 7315 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7316 bfd_byte *rptr, *wptr;
983bddc8 7317 bfd_byte *new_contents;
74f0fb50
AM
7318 bfd_size_type amt;
7319
983bddc8 7320 new_contents = NULL;
51aecdc5 7321 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7322 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7323 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7324 if (opd->adjust == NULL)
7325 return FALSE;
1e2f5b6e
AM
7326
7327 /* This seems a waste of time as input .opd sections are all
7328 zeros as generated by gcc, but I suppose there's no reason
7329 this will always be so. We might start putting something in
7330 the third word of .opd entries. */
7331 if ((sec->flags & SEC_IN_MEMORY) == 0)
7332 {
eea6121a
AM
7333 bfd_byte *loc;
7334 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7335 {
c9594989 7336 free (loc);
50bc7936 7337 error_ret:
c9594989 7338 if (symtab_hdr->contents != (unsigned char *) local_syms)
6cdc0ccc 7339 free (local_syms);
6cdc0ccc
AM
7340 if (elf_section_data (sec)->relocs != relstart)
7341 free (relstart);
b34976b6 7342 return FALSE;
6cdc0ccc 7343 }
1e2f5b6e
AM
7344 sec->contents = loc;
7345 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7346 }
7347
7348 elf_section_data (sec)->relocs = relstart;
7349
3f764659 7350 new_contents = sec->contents;
3f764659
JJ
7351 if (add_aux_fields)
7352 {
7353 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7354 if (new_contents == NULL)
7355 return FALSE;
51aecdc5 7356 need_pad = NULL;
3f764659 7357 }
b4f4e59f
AM
7358 wptr = new_contents;
7359 rptr = sec->contents;
1e2f5b6e 7360 write_rel = relstart;
51aecdc5 7361 for (rel = relstart; rel < relend; )
1e2f5b6e 7362 {
50bc7936
AM
7363 unsigned long r_symndx;
7364 asection *sym_sec;
7365 struct elf_link_hash_entry *h;
51aecdc5 7366 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7367 Elf_Internal_Sym *sym;
51aecdc5
AM
7368 long opd_ent_size;
7369 Elf_Internal_Rela *next_rel;
7370 bfd_boolean skip;
50bc7936
AM
7371
7372 r_symndx = ELF64_R_SYM (rel->r_info);
7373 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7374 r_symndx, ibfd))
50bc7936
AM
7375 goto error_ret;
7376
51aecdc5
AM
7377 next_rel = rel + 2;
7378 if (next_rel + 1 == relend
7379 || (next_rel + 2 < relend
7380 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7381 ++next_rel;
7382
7383 /* See if the .opd entry is full 24 byte or
7384 16 byte (with fd_aux entry overlapped with next
7385 fd_func). */
7386 opd_ent_size = 24;
7387 if (next_rel == relend)
1e2f5b6e 7388 {
51aecdc5 7389 if (sec->size == rel->r_offset + 16)
3f764659 7390 opd_ent_size = 16;
51aecdc5
AM
7391 }
7392 else if (next_rel->r_offset == rel->r_offset + 16)
7393 opd_ent_size = 16;
3f764659 7394
51aecdc5
AM
7395 if (h != NULL
7396 && h->root.root.string[0] == '.')
7397 {
ed7007c1 7398 fdh = ppc_elf_hash_entry (h)->oh;
8c5b4e52
AM
7399 if (fdh != NULL)
7400 {
7401 fdh = ppc_follow_link (fdh);
7402 if (fdh->elf.root.type != bfd_link_hash_defined
7403 && fdh->elf.root.type != bfd_link_hash_defweak)
7404 fdh = NULL;
7405 }
51aecdc5 7406 }
1e2f5b6e 7407
51aecdc5
AM
7408 skip = (sym_sec->owner != ibfd
7409 || sym_sec->output_section == bfd_abs_section_ptr);
7410 if (skip)
7411 {
7412 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7413 {
51aecdc5
AM
7414 /* Arrange for the function descriptor sym
7415 to be dropped. */
7416 fdh->elf.root.u.def.value = 0;
7417 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7418 }
51aecdc5 7419 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7420
0e1862bb 7421 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7422 rel = next_rel;
7423 else
7424 while (1)
7425 {
7426 if (!dec_dynrel_count (rel->r_info, sec, info,
7427 NULL, h, sym))
7428 goto error_ret;
754021d0 7429
51aecdc5
AM
7430 if (++rel == next_rel)
7431 break;
1e2f5b6e 7432
51aecdc5
AM
7433 r_symndx = ELF64_R_SYM (rel->r_info);
7434 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7435 r_symndx, ibfd))
7436 goto error_ret;
7437 }
50bc7936
AM
7438 }
7439 else
1e2f5b6e 7440 {
51aecdc5
AM
7441 /* We'll be keeping this opd entry. */
7442 long adjust;
7443
7444 if (fdh != NULL)
7445 {
7446 /* Redefine the function descriptor symbol to
7447 this location in the opd section. It is
7448 necessary to update the value here rather
7449 than using an array of adjustments as we do
7450 for local symbols, because various places
7451 in the generic ELF code use the value
7452 stored in u.def.value. */
7453 fdh->elf.root.u.def.value = wptr - new_contents;
7454 fdh->adjust_done = 1;
7455 }
7456
7457 /* Local syms are a bit tricky. We could
7458 tweak them as they can be cached, but
7459 we'd need to look through the local syms
7460 for the function descriptor sym which we
7461 don't have at the moment. So keep an
7462 array of adjustments. */
7463 adjust = (wptr - new_contents) - (rptr - sec->contents);
7464 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7465
7466 if (wptr != rptr)
7467 memcpy (wptr, rptr, opd_ent_size);
7468 wptr += opd_ent_size;
7469 if (add_aux_fields && opd_ent_size == 16)
7470 {
7471 memset (wptr, '\0', 8);
7472 wptr += 8;
7473 }
7474
50bc7936 7475 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7476 new opd entries. */
7477 for ( ; rel != next_rel; ++rel)
7478 {
7479 rel->r_offset += adjust;
7480 if (write_rel != rel)
7481 memcpy (write_rel, rel, sizeof (*rel));
7482 ++write_rel;
7483 }
1e2f5b6e 7484 }
51aecdc5
AM
7485
7486 rptr += opd_ent_size;
1e2f5b6e
AM
7487 }
7488
3f764659 7489 sec->size = wptr - new_contents;
1e2f5b6e 7490 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7491 if (add_aux_fields)
7492 {
7493 free (sec->contents);
7494 sec->contents = new_contents;
7495 }
7496
05bf9422 7497 /* Fudge the header size too, as this is used later in
cdcf6e38 7498 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7499 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7500 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7501 some_edited = TRUE;
1e2f5b6e 7502 }
6cdc0ccc 7503 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7504 free (relstart);
6cdc0ccc 7505
411e1bfb
AM
7506 if (local_syms != NULL
7507 && symtab_hdr->contents != (unsigned char *) local_syms)
7508 {
7509 if (!info->keep_memory)
7510 free (local_syms);
7511 else
7512 symtab_hdr->contents = (unsigned char *) local_syms;
7513 }
7514 }
7515
754021d0
AM
7516 if (some_edited)
7517 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7518
3f764659
JJ
7519 /* If we are doing a final link and the last .opd entry is just 16 byte
7520 long, add a 8 byte padding after it. */
0e1862bb 7521 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7522 {
7523 bfd_byte *p;
7524
7525 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7526 {
7527 BFD_ASSERT (need_pad->size > 0);
7528
7529 p = bfd_malloc (need_pad->size + 8);
7530 if (p == NULL)
7531 return FALSE;
699733f6 7532
2cdcc330
AM
7533 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7534 p, 0, need_pad->size))
3f764659
JJ
7535 return FALSE;
7536
7537 need_pad->contents = p;
7538 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7539 }
7540 else
7541 {
7542 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7543 if (p == NULL)
7544 return FALSE;
7545
7546 need_pad->contents = p;
7547 }
7548
7549 memset (need_pad->contents + need_pad->size, 0, 8);
7550 need_pad->size += 8;
7551 }
7552
411e1bfb
AM
7553 return TRUE;
7554}
7555
3e04d765
AM
7556/* Analyze inline PLT call relocations to see whether calls to locally
7557 defined functions can be converted to direct calls. */
7558
7559bfd_boolean
7560ppc64_elf_inline_plt (struct bfd_link_info *info)
7561{
7562 struct ppc_link_hash_table *htab;
7563 bfd *ibfd;
7564 asection *sec;
7565 bfd_vma low_vma, high_vma, limit;
7566
7567 htab = ppc_hash_table (info);
7568 if (htab == NULL)
7569 return FALSE;
7570
7571 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7572 reduced somewhat to cater for possible stubs that might be added
7573 between the call and its destination. */
7574 if (htab->params->group_size < 0)
7575 {
7576 limit = -htab->params->group_size;
7577 if (limit == 1)
7578 limit = 0x1e00000;
7579 }
7580 else
7581 {
7582 limit = htab->params->group_size;
7583 if (limit == 1)
7584 limit = 0x1c00000;
7585 }
7586
7587 low_vma = -1;
7588 high_vma = 0;
7589 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7590 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7591 {
7592 if (low_vma > sec->vma)
7593 low_vma = sec->vma;
7594 if (high_vma < sec->vma + sec->size)
7595 high_vma = sec->vma + sec->size;
7596 }
7597
7598 /* If a "bl" can reach anywhere in local code sections, then we can
7599 convert all inline PLT sequences to direct calls when the symbol
7600 is local. */
7601 if (high_vma - low_vma < limit)
7602 {
7603 htab->can_convert_all_inline_plt = 1;
7604 return TRUE;
7605 }
7606
7607 /* Otherwise, go looking through relocs for cases where a direct
7608 call won't reach. Mark the symbol on any such reloc to disable
7609 the optimization and keep the PLT entry as it seems likely that
7610 this will be better than creating trampolines. Note that this
7611 will disable the optimization for all inline PLT calls to a
7612 particular symbol, not just those that won't reach. The
7613 difficulty in doing a more precise optimization is that the
7614 linker needs to make a decision depending on whether a
7615 particular R_PPC64_PLTCALL insn can be turned into a direct
7616 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7617 the sequence, and there is nothing that ties those relocs
7618 together except their symbol. */
7619
7620 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7621 {
7622 Elf_Internal_Shdr *symtab_hdr;
7623 Elf_Internal_Sym *local_syms;
7624
7625 if (!is_ppc64_elf (ibfd))
7626 continue;
7627
7628 local_syms = NULL;
7629 symtab_hdr = &elf_symtab_hdr (ibfd);
7630
7631 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7632 if (ppc64_elf_section_data (sec)->has_pltcall
7633 && !bfd_is_abs_section (sec->output_section))
7634 {
7635 Elf_Internal_Rela *relstart, *rel, *relend;
7636
7637 /* Read the relocations. */
7638 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7639 info->keep_memory);
7640 if (relstart == NULL)
7641 return FALSE;
7642
7643 relend = relstart + sec->reloc_count;
435edf0b 7644 for (rel = relstart; rel < relend; rel++)
3e04d765
AM
7645 {
7646 enum elf_ppc64_reloc_type r_type;
7647 unsigned long r_symndx;
7648 asection *sym_sec;
7649 struct elf_link_hash_entry *h;
7650 Elf_Internal_Sym *sym;
7651 unsigned char *tls_maskp;
7652
7653 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7654 if (r_type != R_PPC64_PLTCALL
7655 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7656 continue;
7657
7658 r_symndx = ELF64_R_SYM (rel->r_info);
7659 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7660 r_symndx, ibfd))
7661 {
7662 if (elf_section_data (sec)->relocs != relstart)
7663 free (relstart);
c9594989 7664 if (symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765
AM
7665 free (local_syms);
7666 return FALSE;
7667 }
7668
7669 if (sym_sec != NULL && sym_sec->output_section != NULL)
7670 {
7671 bfd_vma from, to;
7672 if (h != NULL)
7673 to = h->root.u.def.value;
7674 else
7675 to = sym->st_value;
7676 to += (rel->r_addend
7677 + sym_sec->output_offset
7678 + sym_sec->output_section->vma);
7679 from = (rel->r_offset
7680 + sec->output_offset
7681 + sec->output_section->vma);
5663e321
AM
7682 if (to - from + limit < 2 * limit
7683 && !(r_type == R_PPC64_PLTCALL_NOTOC
7684 && (((h ? h->other : sym->st_other)
7685 & STO_PPC64_LOCAL_MASK)
4a4e7361 7686 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7687 *tls_maskp &= ~PLT_KEEP;
7688 }
7689 }
7690 if (elf_section_data (sec)->relocs != relstart)
7691 free (relstart);
7692 }
7693
7694 if (local_syms != NULL
7695 && symtab_hdr->contents != (unsigned char *) local_syms)
7696 {
7697 if (!info->keep_memory)
7698 free (local_syms);
7699 else
7700 symtab_hdr->contents = (unsigned char *) local_syms;
7701 }
7702 }
7703
7704 return TRUE;
7705}
7706
c9405344
AM
7707/* Set htab->tls_get_addr and various other info specific to TLS.
7708 This needs to run before dynamic symbols are processed in
7709 bfd_elf_size_dynamic_sections. */
411e1bfb 7710
c9405344 7711bfd_boolean
e7d1c40c 7712ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7713{
411e1bfb 7714 struct ppc_link_hash_table *htab;
9e7028aa 7715 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
411e1bfb 7716
411e1bfb 7717 htab = ppc_hash_table (info);
4dfe6ac6 7718 if (htab == NULL)
c9405344 7719 return FALSE;
4dfe6ac6 7720
ee67d69a
AM
7721 if (abiversion (info->output_bfd) == 1)
7722 htab->opd_abi = 1;
7723
e7d1c40c 7724 if (htab->params->no_multi_toc)
33c0ec9d
AM
7725 htab->do_multi_toc = 0;
7726 else if (!htab->do_multi_toc)
e7d1c40c 7727 htab->params->no_multi_toc = 1;
33c0ec9d 7728
8b5f1ed8
AM
7729 /* Default to --no-plt-localentry, as this option can cause problems
7730 with symbol interposition. For example, glibc libpthread.so and
7731 libc.so duplicate many pthread symbols, with a fallback
7732 implementation in libc.so. In some cases the fallback does more
7733 work than the pthread implementation. __pthread_condattr_destroy
7734 is one such symbol: the libpthread.so implementation is
7735 localentry:0 while the libc.so implementation is localentry:8.
7736 An app that "cleverly" uses dlopen to only load necessary
7737 libraries at runtime may omit loading libpthread.so when not
7738 running multi-threaded, which then results in the libc.so
7739 fallback symbols being used and ld.so complaining. Now there
7740 are workarounds in ld (see non_zero_localentry) to detect the
7741 pthread situation, but that may not be the only case where
7742 --plt-localentry can cause trouble. */
f378ab09 7743 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7744 htab->params->plt_localentry0 = 0;
3cd7c7d7
AM
7745 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7746 {
7747 /* The issue is that __glink_PLTresolve saves r2, which is done
7748 because glibc ld.so _dl_runtime_resolve restores r2 to support
7749 a glibc plt call optimisation where global entry code is
7750 skipped on calls that resolve to the same binary. The
7751 __glink_PLTresolve save of r2 is incompatible with code
7752 making tail calls, because the tail call might go via the
7753 resolver and thus overwrite the proper saved r2. */
7754 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7755 "power10 pc-relative code"));
7756 htab->params->plt_localentry0 = 0;
7757 }
d44c746a
AM
7758 if (htab->params->plt_localentry0
7759 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7760 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
7761 _bfd_error_handler
7762 (_("warning: --plt-localentry is especially dangerous without "
7763 "ld.so support to detect ABI violations"));
f378ab09 7764
9e7028aa
AM
7765 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7766 FALSE, FALSE, TRUE);
7767 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7768
a7f2871e 7769 /* Move dynamic linking info to the function descriptor sym. */
9e7028aa
AM
7770 if (tga != NULL)
7771 func_desc_adjust (tga, info);
7772 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7773 FALSE, FALSE, TRUE);
7774 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7775
7776 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
7777 FALSE, FALSE, TRUE);
7778 htab->tga_desc = ppc_elf_hash_entry (desc);
7779 if (desc != NULL)
7780 func_desc_adjust (desc, info);
7781 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
7782 FALSE, FALSE, TRUE);
7783 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7784
7c9cf415 7785 if (htab->params->tls_get_addr_opt)
a7f2871e 7786 {
9e7028aa 7787 struct elf_link_hash_entry *opt, *opt_fd;
a7f2871e
AM
7788
7789 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7790 FALSE, FALSE, TRUE);
7791 if (opt != NULL)
7792 func_desc_adjust (opt, info);
7793 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7794 FALSE, FALSE, TRUE);
7795 if (opt_fd != NULL
7796 && (opt_fd->root.type == bfd_link_hash_defined
7797 || opt_fd->root.type == bfd_link_hash_defweak))
7798 {
7799 /* If glibc supports an optimized __tls_get_addr call stub,
7800 signalled by the presence of __tls_get_addr_opt, and we'll
7801 be calling __tls_get_addr via a plt call stub, then
7802 make __tls_get_addr point to __tls_get_addr_opt. */
9e7028aa
AM
7803 if (!(htab->elf.dynamic_sections_created
7804 && tga_fd != NULL
7805 && (tga_fd->type == STT_FUNC
7806 || tga_fd->needs_plt)
7807 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7808 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
7809 tga_fd = NULL;
7810 if (!(htab->elf.dynamic_sections_created
7811 && desc_fd != NULL
7812 && (desc_fd->type == STT_FUNC
7813 || desc_fd->needs_plt)
7814 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
7815 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
7816 desc_fd = NULL;
7817
7818 if (tga_fd != NULL || desc_fd != NULL)
7819 {
7820 struct plt_entry *ent = NULL;
7821
7822 if (tga_fd != NULL)
7823 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7824 if (ent->plt.refcount > 0)
7825 break;
7826 if (ent == NULL && desc_fd != NULL)
7827 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
7828 if (ent->plt.refcount > 0)
7829 break;
a7f2871e
AM
7830 if (ent != NULL)
7831 {
9e7028aa
AM
7832 if (tga_fd != NULL)
7833 {
7834 tga_fd->root.type = bfd_link_hash_indirect;
7835 tga_fd->root.u.i.link = &opt_fd->root;
7836 tga_fd->root.u.i.warning = NULL;
7837 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7838 }
7839 if (desc_fd != NULL)
7840 {
7841 desc_fd->root.type = bfd_link_hash_indirect;
7842 desc_fd->root.u.i.link = &opt_fd->root;
7843 desc_fd->root.u.i.warning = NULL;
7844 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
7845 }
b531344c 7846 opt_fd->mark = 1;
a7f2871e
AM
7847 if (opt_fd->dynindx != -1)
7848 {
7849 /* Use __tls_get_addr_opt in dynamic relocations. */
7850 opt_fd->dynindx = -1;
7851 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7852 opt_fd->dynstr_index);
7853 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
c9405344 7854 return FALSE;
a7f2871e 7855 }
9e7028aa 7856 if (tga_fd != NULL)
a7f2871e 7857 {
9e7028aa 7858 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
9e51d549 7859 tga = (struct elf_link_hash_entry *) htab->tls_get_addr;
9e7028aa
AM
7860 if (opt != NULL && tga != NULL)
7861 {
7862 tga->root.type = bfd_link_hash_indirect;
7863 tga->root.u.i.link = &opt->root;
7864 tga->root.u.i.warning = NULL;
7865 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7866 opt->mark = 1;
7867 _bfd_elf_link_hash_hide_symbol (info, opt,
7868 tga->forced_local);
7869 htab->tls_get_addr = ppc_elf_hash_entry (opt);
7870 }
7871 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7872 htab->tls_get_addr_fd->is_func_descriptor = 1;
7873 if (htab->tls_get_addr != NULL)
7874 {
7875 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7876 htab->tls_get_addr->is_func = 1;
7877 }
a7f2871e 7878 }
9e7028aa 7879 if (desc_fd != NULL)
a7f2871e 7880 {
9e7028aa
AM
7881 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
7882 if (opt != NULL && desc != NULL)
7883 {
7884 desc->root.type = bfd_link_hash_indirect;
7885 desc->root.u.i.link = &opt->root;
7886 desc->root.u.i.warning = NULL;
7887 ppc64_elf_copy_indirect_symbol (info, opt, desc);
7888 opt->mark = 1;
7889 _bfd_elf_link_hash_hide_symbol (info, opt,
7890 desc->forced_local);
7891 htab->tga_desc = ppc_elf_hash_entry (opt);
7892 }
7893 htab->tga_desc_fd->oh = htab->tga_desc;
7894 htab->tga_desc_fd->is_func_descriptor = 1;
7895 if (htab->tga_desc != NULL)
7896 {
7897 htab->tga_desc->oh = htab->tga_desc_fd;
7898 htab->tga_desc->is_func = 1;
7899 }
a7f2871e
AM
7900 }
7901 }
7902 }
7903 }
7c9cf415
AM
7904 else if (htab->params->tls_get_addr_opt < 0)
7905 htab->params->tls_get_addr_opt = 0;
a7f2871e 7906 }
9e7028aa
AM
7907
7908 if (htab->tga_desc_fd != NULL
7909 && htab->params->tls_get_addr_opt
7910 && htab->params->no_tls_get_addr_regsave == -1)
7911 htab->params->no_tls_get_addr_regsave = 0;
7912
c9405344 7913 return TRUE;
3a71aa26 7914}
8387904d 7915
3a71aa26 7916/* Return TRUE iff REL is a branch reloc with a global symbol matching
9e7028aa 7917 any of HASH1, HASH2, HASH3, or HASH4. */
8387904d 7918
3a71aa26
AM
7919static bfd_boolean
7920branch_reloc_hash_match (const bfd *ibfd,
7921 const Elf_Internal_Rela *rel,
7922 const struct ppc_link_hash_entry *hash1,
9e7028aa
AM
7923 const struct ppc_link_hash_entry *hash2,
7924 const struct ppc_link_hash_entry *hash3,
7925 const struct ppc_link_hash_entry *hash4)
3a71aa26
AM
7926{
7927 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7928 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7929 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7930
e054468f 7931 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7932 {
3a71aa26
AM
7933 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7934 struct elf_link_hash_entry *h;
8387904d 7935
3a71aa26 7936 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7937 h = elf_follow_link (h);
9e51d549
AM
7938 if (h == (struct elf_link_hash_entry *) hash1
7939 || h == (struct elf_link_hash_entry *) hash2
7940 || h == (struct elf_link_hash_entry *) hash3
7941 || h == (struct elf_link_hash_entry *) hash4)
3a71aa26 7942 return TRUE;
a48ebf4d 7943 }
3a71aa26 7944 return FALSE;
951fd09b 7945}
411e1bfb 7946
951fd09b
AM
7947/* Run through all the TLS relocs looking for optimization
7948 opportunities. The linker has been hacked (see ppc64elf.em) to do
7949 a preliminary section layout so that we know the TLS segment
7950 offsets. We can't optimize earlier because some optimizations need
7951 to know the tp offset, and we need to optimize before allocating
7952 dynamic relocations. */
7953
7954bfd_boolean
33c0ec9d 7955ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7956{
7957 bfd *ibfd;
7958 asection *sec;
7959 struct ppc_link_hash_table *htab;
663a1470 7960 unsigned char *toc_ref;
102890f0 7961 int pass;
951fd09b 7962
3cbc1e5e 7963 if (!bfd_link_executable (info))
411e1bfb
AM
7964 return TRUE;
7965
951fd09b 7966 htab = ppc_hash_table (info);
4dfe6ac6
NC
7967 if (htab == NULL)
7968 return FALSE;
7969
252dcdf4
AM
7970 htab->do_tls_opt = 1;
7971
663a1470
AM
7972 /* Make two passes over the relocs. On the first pass, mark toc
7973 entries involved with tls relocs, and check that tls relocs
7974 involved in setting up a tls_get_addr call are indeed followed by
7975 such a call. If they are not, we can't do any tls optimization.
7976 On the second pass twiddle tls_mask flags to notify
7977 relocate_section that optimization can be done, and adjust got
7978 and plt refcounts. */
7979 toc_ref = NULL;
7980 for (pass = 0; pass < 2; ++pass)
c72f2fb2 7981 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
7982 {
7983 Elf_Internal_Sym *locsyms = NULL;
7984 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7985
102890f0
AM
7986 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7987 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7988 {
7989 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7990 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7991
102890f0
AM
7992 /* Read the relocations. */
7993 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7994 info->keep_memory);
7995 if (relstart == NULL)
2915c55b
JK
7996 {
7997 free (toc_ref);
7998 return FALSE;
7999 }
411e1bfb 8000
102890f0
AM
8001 relend = relstart + sec->reloc_count;
8002 for (rel = relstart; rel < relend; rel++)
8003 {
8004 enum elf_ppc64_reloc_type r_type;
8005 unsigned long r_symndx;
8006 struct elf_link_hash_entry *h;
8007 Elf_Internal_Sym *sym;
8008 asection *sym_sec;
f961d9dd 8009 unsigned char *tls_mask;
46e9995a 8010 unsigned int tls_set, tls_clear, tls_type = 0;
102890f0
AM
8011 bfd_vma value;
8012 bfd_boolean ok_tprel, is_local;
8013 long toc_ref_index = 0;
8014 int expecting_tls_get_addr = 0;
663a1470 8015 bfd_boolean ret = FALSE;
411e1bfb 8016
102890f0
AM
8017 r_symndx = ELF64_R_SYM (rel->r_info);
8018 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8019 r_symndx, ibfd))
8020 {
8021 err_free_rel:
8022 if (elf_section_data (sec)->relocs != relstart)
8023 free (relstart);
c9594989
AM
8024 free (toc_ref);
8025 if (elf_symtab_hdr (ibfd).contents
8026 != (unsigned char *) locsyms)
102890f0 8027 free (locsyms);
663a1470 8028 return ret;
102890f0 8029 }
411e1bfb 8030
102890f0
AM
8031 if (h != NULL)
8032 {
766bc656
AM
8033 if (h->root.type == bfd_link_hash_defined
8034 || h->root.type == bfd_link_hash_defweak)
8035 value = h->root.u.def.value;
8036 else if (h->root.type == bfd_link_hash_undefweak)
8037 value = 0;
8038 else
663a1470
AM
8039 {
8040 found_tls_get_addr_arg = 0;
8041 continue;
8042 }
102890f0
AM
8043 }
8044 else
8045 /* Symbols referenced by TLS relocs must be of type
8046 STT_TLS. So no need for .opd local sym adjust. */
8047 value = sym->st_value;
8048
8049 ok_tprel = FALSE;
f749f26e
AM
8050 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8051 if (is_local)
102890f0 8052 {
766bc656
AM
8053 if (h != NULL
8054 && h->root.type == bfd_link_hash_undefweak)
8055 ok_tprel = TRUE;
c27b8c2a
AM
8056 else if (sym_sec != NULL
8057 && sym_sec->output_section != NULL)
766bc656
AM
8058 {
8059 value += sym_sec->output_offset;
8060 value += sym_sec->output_section->vma;
0b147428 8061 value -= htab->elf.tls_sec->vma + TP_OFFSET;
c213164a
AM
8062 /* Note that even though the prefix insns
8063 allow a 1<<33 offset we use the same test
8064 as for addis;addi. There may be a mix of
8065 pcrel and non-pcrel code and the decision
8066 to optimise is per symbol, not per TLS
8067 sequence. */
0b147428 8068 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
766bc656 8069 }
102890f0 8070 }
951fd09b 8071
102890f0 8072 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8073 /* If this section has old-style __tls_get_addr calls
8074 without marker relocs, then check that each
8075 __tls_get_addr call reloc is preceded by a reloc
8076 that conceivably belongs to the __tls_get_addr arg
8077 setup insn. If we don't find matching arg setup
8078 relocs, don't do any tls optimization. */
8079 if (pass == 0
9737e8af 8080 && sec->nomark_tls_get_addr
663a1470 8081 && h != NULL
ed7007c1 8082 && is_tls_get_addr (h, htab)
663a1470
AM
8083 && !found_tls_get_addr_arg
8084 && is_branch_reloc (r_type))
8085 {
25f53a85 8086 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8087 "TLS optimization disabled\n"),
8088 ibfd, sec, rel->r_offset);
8089 ret = TRUE;
8090 goto err_free_rel;
8091 }
8092
8093 found_tls_get_addr_arg = 0;
102890f0
AM
8094 switch (r_type)
8095 {
8096 case R_PPC64_GOT_TLSLD16:
8097 case R_PPC64_GOT_TLSLD16_LO:
87c69f97 8098 case R_PPC64_GOT_TLSLD_PCREL34:
102890f0 8099 expecting_tls_get_addr = 1;
663a1470 8100 found_tls_get_addr_arg = 1;
1a0670f3 8101 /* Fall through. */
102890f0
AM
8102
8103 case R_PPC64_GOT_TLSLD16_HI:
8104 case R_PPC64_GOT_TLSLD16_HA:
8105 /* These relocs should never be against a symbol
8106 defined in a shared lib. Leave them alone if
8107 that turns out to be the case. */
8108 if (!is_local)
8109 continue;
411e1bfb 8110
102890f0 8111 /* LD -> LE */
411e1bfb 8112 tls_set = 0;
102890f0
AM
8113 tls_clear = TLS_LD;
8114 tls_type = TLS_TLS | TLS_LD;
8115 break;
411e1bfb 8116
102890f0
AM
8117 case R_PPC64_GOT_TLSGD16:
8118 case R_PPC64_GOT_TLSGD16_LO:
87c69f97 8119 case R_PPC64_GOT_TLSGD_PCREL34:
102890f0 8120 expecting_tls_get_addr = 1;
663a1470 8121 found_tls_get_addr_arg = 1;
1a0670f3 8122 /* Fall through. */
102890f0
AM
8123
8124 case R_PPC64_GOT_TLSGD16_HI:
8125 case R_PPC64_GOT_TLSGD16_HA:
8126 if (ok_tprel)
8127 /* GD -> LE */
411e1bfb 8128 tls_set = 0;
102890f0
AM
8129 else
8130 /* GD -> IE */
b00a0a86 8131 tls_set = TLS_TLS | TLS_GDIE;
102890f0
AM
8132 tls_clear = TLS_GD;
8133 tls_type = TLS_TLS | TLS_GD;
8134 break;
8135
87c69f97 8136 case R_PPC64_GOT_TPREL_PCREL34:
102890f0
AM
8137 case R_PPC64_GOT_TPREL16_DS:
8138 case R_PPC64_GOT_TPREL16_LO_DS:
8139 case R_PPC64_GOT_TPREL16_HI:
8140 case R_PPC64_GOT_TPREL16_HA:
8141 if (ok_tprel)
8142 {
8143 /* IE -> LE */
8144 tls_set = 0;
8145 tls_clear = TLS_TPREL;
8146 tls_type = TLS_TLS | TLS_TPREL;
8147 break;
8148 }
411e1bfb
AM
8149 continue;
8150
727fc41e 8151 case R_PPC64_TLSLD:
7d04a20a
AM
8152 if (!is_local)
8153 continue;
8154 /* Fall through. */
8155 case R_PPC64_TLSGD:
23cedd1d
AM
8156 if (rel + 1 < relend
8157 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8158 {
8159 if (pass != 0
2cdcc330 8160 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
8161 != R_PPC64_PLTSEQ)
8162 && (ELF64_R_TYPE (rel[1].r_info)
8163 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
8164 {
8165 r_symndx = ELF64_R_SYM (rel[1].r_info);
8166 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 8167 r_symndx, ibfd))
23cedd1d
AM
8168 goto err_free_rel;
8169 if (h != NULL)
8170 {
8171 struct plt_entry *ent = NULL;
8172
8173 for (ent = h->plt.plist;
8174 ent != NULL;
8175 ent = ent->next)
8176 if (ent->addend == rel[1].r_addend)
8177 break;
8178
8179 if (ent != NULL
8180 && ent->plt.refcount > 0)
8181 ent->plt.refcount -= 1;
8182 }
8183 }
8184 continue;
8185 }
663a1470 8186 found_tls_get_addr_arg = 1;
1a0670f3 8187 /* Fall through. */
663a1470
AM
8188
8189 case R_PPC64_TLS:
8190 case R_PPC64_TOC16:
8191 case R_PPC64_TOC16_LO:
102890f0
AM
8192 if (sym_sec == NULL || sym_sec != toc)
8193 continue;
8194
8195 /* Mark this toc entry as referenced by a TLS
8196 code sequence. We can do that now in the
8197 case of R_PPC64_TLS, and after checking for
8198 tls_get_addr for the TOC16 relocs. */
8199 if (toc_ref == NULL)
2cdcc330
AM
8200 toc_ref
8201 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
8202 if (toc_ref == NULL)
8203 goto err_free_rel;
8204
102890f0
AM
8205 if (h != NULL)
8206 value = h->root.u.def.value;
8207 else
8208 value = sym->st_value;
8209 value += rel->r_addend;
73242275
AM
8210 if (value % 8 != 0)
8211 continue;
8212 BFD_ASSERT (value < toc->size
8213 && toc->output_offset % 8 == 0);
663a1470 8214 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8215 if (r_type == R_PPC64_TLS
8216 || r_type == R_PPC64_TLSGD
8217 || r_type == R_PPC64_TLSLD)
102890f0
AM
8218 {
8219 toc_ref[toc_ref_index] = 1;
8220 continue;
8221 }
8222
8223 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8224 continue;
8225
8226 tls_set = 0;
8227 tls_clear = 0;
8228 expecting_tls_get_addr = 2;
8229 break;
8230
8231 case R_PPC64_TPREL64:
8232 if (pass == 0
8233 || sec != toc
8234 || toc_ref == NULL
663a1470 8235 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8236 continue;
8237 if (ok_tprel)
8238 {
8239 /* IE -> LE */
8240 tls_set = TLS_EXPLICIT;
8241 tls_clear = TLS_TPREL;
8242 break;
8243 }
8244 continue;
8245
8246 case R_PPC64_DTPMOD64:
8247 if (pass == 0
8248 || sec != toc
8249 || toc_ref == NULL
663a1470 8250 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8251 continue;
8252 if (rel + 1 < relend
8253 && (rel[1].r_info
8254 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8255 && rel[1].r_offset == rel->r_offset + 8)
8256 {
8257 if (ok_tprel)
8258 /* GD -> LE */
8259 tls_set = TLS_EXPLICIT | TLS_GD;
8260 else
8261 /* GD -> IE */
b00a0a86 8262 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
102890f0
AM
8263 tls_clear = TLS_GD;
8264 }
8265 else
8266 {
8267 if (!is_local)
8268 continue;
8269
8270 /* LD -> LE */
8271 tls_set = TLS_EXPLICIT;
8272 tls_clear = TLS_LD;
8273 }
8274 break;
8275
252dcdf4
AM
8276 case R_PPC64_TPREL16_HA:
8277 if (pass == 0)
8278 {
8279 unsigned char buf[4];
8280 unsigned int insn;
8281 bfd_vma off = rel->r_offset & ~3;
8282 if (!bfd_get_section_contents (ibfd, sec, buf,
8283 off, 4))
8284 goto err_free_rel;
8285 insn = bfd_get_32 (ibfd, buf);
8286 /* addis rt,13,imm */
8287 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8288 != ((15u << 26) | (13 << 16)))
8289 {
8290 /* xgettext:c-format */
8291 info->callbacks->minfo
8292 (_("%H: warning: %s unexpected insn %#x.\n"),
8293 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8294 htab->do_tls_opt = 0;
8295 }
8296 }
8297 continue;
8298
8299 case R_PPC64_TPREL16_HI:
8300 case R_PPC64_TPREL16_HIGH:
8301 case R_PPC64_TPREL16_HIGHA:
8302 case R_PPC64_TPREL16_HIGHER:
8303 case R_PPC64_TPREL16_HIGHERA:
8304 case R_PPC64_TPREL16_HIGHEST:
8305 case R_PPC64_TPREL16_HIGHESTA:
8306 /* These can all be used in sequences along with
8307 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8308 able to verify easily. */
8309 htab->do_tls_opt = 0;
8310 continue;
8311
102890f0
AM
8312 default:
8313 continue;
8314 }
8315
8316 if (pass == 0)
8317 {
727fc41e 8318 if (!expecting_tls_get_addr
9737e8af 8319 || !sec->nomark_tls_get_addr)
102890f0
AM
8320 continue;
8321
3a71aa26
AM
8322 if (rel + 1 < relend
8323 && branch_reloc_hash_match (ibfd, rel + 1,
9e7028aa
AM
8324 htab->tls_get_addr_fd,
8325 htab->tga_desc_fd,
3a71aa26 8326 htab->tls_get_addr,
9e7028aa 8327 htab->tga_desc))
102890f0 8328 {
3a71aa26 8329 if (expecting_tls_get_addr == 2)
102890f0 8330 {
3a71aa26 8331 /* Check for toc tls entries. */
f961d9dd 8332 unsigned char *toc_tls;
3a71aa26
AM
8333 int retval;
8334
8335 retval = get_tls_mask (&toc_tls, NULL, NULL,
8336 &locsyms,
8337 rel, ibfd);
8338 if (retval == 0)
8339 goto err_free_rel;
663a1470
AM
8340 if (toc_tls != NULL)
8341 {
37da22e5
AM
8342 if ((*toc_tls & TLS_TLS) != 0
8343 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8344 found_tls_get_addr_arg = 1;
8345 if (retval > 1)
8346 toc_ref[toc_ref_index] = 1;
8347 }
102890f0 8348 }
3a71aa26 8349 continue;
102890f0
AM
8350 }
8351
102890f0
AM
8352 /* Uh oh, we didn't find the expected call. We
8353 could just mark this symbol to exclude it
8354 from tls optimization but it's safer to skip
663a1470 8355 the entire optimization. */
695344c0 8356 /* xgettext:c-format */
25f53a85 8357 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8358 "TLS optimization disabled\n"),
8359 ibfd, sec, rel->r_offset);
8360 ret = TRUE;
8361 goto err_free_rel;
102890f0
AM
8362 }
8363
37da22e5
AM
8364 /* If we don't have old-style __tls_get_addr calls
8365 without TLSGD/TLSLD marker relocs, and we haven't
8366 found a new-style __tls_get_addr call with a
8367 marker for this symbol, then we either have a
8368 broken object file or an -mlongcall style
8369 indirect call to __tls_get_addr without a marker.
8370 Disable optimization in this case. */
8371 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8372 && (tls_set & TLS_EXPLICIT) == 0
9737e8af 8373 && !sec->nomark_tls_get_addr
37da22e5
AM
8374 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8375 != (TLS_TLS | TLS_MARK)))
8376 continue;
8377
7d04a20a 8378 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
102890f0 8379 {
23cedd1d
AM
8380 struct plt_entry *ent = NULL;
8381
9e7028aa
AM
8382 if (htab->tls_get_addr_fd != NULL)
8383 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8384 ent != NULL;
8385 ent = ent->next)
8386 if (ent->addend == 0)
8387 break;
8388
8389 if (ent == NULL && htab->tga_desc_fd != NULL)
8390 for (ent = htab->tga_desc_fd->elf.plt.plist;
8391 ent != NULL;
8392 ent = ent->next)
8393 if (ent->addend == 0)
8394 break;
8395
8396 if (ent == NULL && htab->tls_get_addr != NULL)
23cedd1d
AM
8397 for (ent = htab->tls_get_addr->elf.plt.plist;
8398 ent != NULL;
8399 ent = ent->next)
8400 if (ent->addend == 0)
102890f0 8401 break;
411e1bfb 8402
9e7028aa
AM
8403 if (ent == NULL && htab->tga_desc != NULL)
8404 for (ent = htab->tga_desc->elf.plt.plist;
23cedd1d
AM
8405 ent != NULL;
8406 ent = ent->next)
8407 if (ent->addend == 0)
102890f0 8408 break;
23cedd1d
AM
8409
8410 if (ent != NULL
8411 && ent->plt.refcount > 0)
8412 ent->plt.refcount -= 1;
102890f0 8413 }
411e1bfb 8414
102890f0 8415 if (tls_clear == 0)
30038c59
AM
8416 continue;
8417
102890f0
AM
8418 if ((tls_set & TLS_EXPLICIT) == 0)
8419 {
8420 struct got_entry *ent;
411e1bfb 8421
102890f0
AM
8422 /* Adjust got entry for this reloc. */
8423 if (h != NULL)
8424 ent = h->got.glist;
8425 else
8426 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8427
102890f0
AM
8428 for (; ent != NULL; ent = ent->next)
8429 if (ent->addend == rel->r_addend
8430 && ent->owner == ibfd
8431 && ent->tls_type == tls_type)
8432 break;
8433 if (ent == NULL)
8434 abort ();
411e1bfb 8435
102890f0
AM
8436 if (tls_set == 0)
8437 {
8438 /* We managed to get rid of a got entry. */
8439 if (ent->got.refcount > 0)
8440 ent->got.refcount -= 1;
8441 }
8442 }
8443 else
8444 {
8445 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8446 we'll lose one or two dyn relocs. */
8447 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8448 NULL, h, sym))
102890f0 8449 return FALSE;
411e1bfb 8450
102890f0
AM
8451 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8452 {
8453 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8454 NULL, h, sym))
102890f0
AM
8455 return FALSE;
8456 }
8457 }
411e1bfb 8458
46e9995a 8459 *tls_mask |= tls_set & 0xff;
102890f0
AM
8460 *tls_mask &= ~tls_clear;
8461 }
8c1d1bb8 8462
102890f0
AM
8463 if (elf_section_data (sec)->relocs != relstart)
8464 free (relstart);
8465 }
411e1bfb 8466
663a1470
AM
8467 if (locsyms != NULL
8468 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8469 {
8470 if (!info->keep_memory)
8471 free (locsyms);
8472 else
8473 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8474 }
8475 }
411e1bfb 8476
c9594989 8477 free (toc_ref);
b34976b6 8478 return TRUE;
1e2f5b6e 8479}
b34976b6 8480
c5614fa4
AM
8481/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8482 the values of any global symbols in a toc section that has been
8483 edited. Globals in toc sections should be a rarity, so this function
8484 sets a flag if any are found in toc sections other than the one just
de194d85 8485 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8486
8487struct adjust_toc_info
8488{
8489 asection *toc;
8490 unsigned long *skip;
8491 bfd_boolean global_toc_syms;
8492};
8493
ba761f19
AM
8494enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8495
c5614fa4
AM
8496static bfd_boolean
8497adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8498{
8499 struct ppc_link_hash_entry *eh;
8500 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8501 unsigned long i;
c5614fa4 8502
c5614fa4
AM
8503 if (h->root.type != bfd_link_hash_defined
8504 && h->root.type != bfd_link_hash_defweak)
8505 return TRUE;
8506
ed7007c1 8507 eh = ppc_elf_hash_entry (h);
c5614fa4
AM
8508 if (eh->adjust_done)
8509 return TRUE;
8510
8511 if (eh->elf.root.u.def.section == toc_inf->toc)
8512 {
854b41e7
AM
8513 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8514 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8515 else
854b41e7
AM
8516 i = eh->elf.root.u.def.value >> 3;
8517
ba761f19 8518 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8519 {
4eca0228 8520 _bfd_error_handler
854b41e7
AM
8521 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8522 do
8523 ++i;
ba761f19 8524 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8525 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8526 }
854b41e7
AM
8527
8528 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8529 eh->adjust_done = 1;
8530 }
8531 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8532 toc_inf->global_toc_syms = TRUE;
8533
8534 return TRUE;
8535}
8536
39eeab25
AM
8537/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8538 on a _LO variety toc/got reloc. */
560c8763
AM
8539
8540static bfd_boolean
39eeab25 8541ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8542{
2365f8d7
AM
8543 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8544 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8545 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8546 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8547 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8548 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8549 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8550 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8551 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8552 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8553 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8554 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8555 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8556 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8557 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8558 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8559 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
39eeab25
AM
8560 /* Exclude lfqu by testing reloc. If relocs are ever
8561 defined for the reduced D field in psq_lu then those
8562 will need testing too. */
8563 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8564 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
39eeab25 8565 && (insn & 1) == 0)
2365f8d7
AM
8566 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8567 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
39eeab25
AM
8568 /* Exclude stfqu. psq_stu as above for psq_lu. */
8569 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8570 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
39eeab25 8571 && (insn & 1) == 0));
560c8763
AM
8572}
8573
4a421c53
AM
8574/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8575 pld ra,symbol@got@pcrel
dd9b12c2 8576 load/store rt,off(ra)
4a421c53 8577 or
d4b87b1e 8578 pla ra,symbol@pcrel
dd9b12c2 8579 load/store rt,off(ra)
4a421c53 8580 may be translated to
dd9b12c2 8581 pload/pstore rt,symbol+off@pcrel
4a421c53
AM
8582 nop.
8583 This function returns true if the optimization is possible, placing
dd9b12c2 8584 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
4a421c53
AM
8585
8586 On entry to this function, the linker has already determined that
d4b87b1e 8587 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8588 while *PINSN2 is the second instruction. */
8589
8590static bfd_boolean
dd9b12c2 8591xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
4a421c53 8592{
77486630
AM
8593 uint64_t insn1 = *pinsn1;
8594 uint64_t insn2 = *pinsn2;
dd9b12c2 8595 bfd_signed_vma off;
4a421c53 8596
77486630
AM
8597 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8598 {
8599 /* Check that regs match. */
8600 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8601 return FALSE;
8602
8603 /* P8LS or PMLS form, non-pcrel. */
8604 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8605 return FALSE;
8606
8607 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8608 *pinsn2 = PNOP;
8609 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8610 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8611 return TRUE;
8612 }
8613
8614 insn2 >>= 32;
8615
4a421c53 8616 /* Check that regs match. */
77486630 8617 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
4a421c53
AM
8618 return FALSE;
8619
8620 switch ((insn2 >> 26) & 63)
8621 {
8622 default:
8623 return FALSE;
8624
8625 case 32: /* lwz */
8626 case 34: /* lbz */
8627 case 36: /* stw */
8628 case 38: /* stb */
8629 case 40: /* lhz */
8630 case 42: /* lha */
8631 case 44: /* sth */
8632 case 48: /* lfs */
8633 case 50: /* lfd */
8634 case 52: /* stfs */
8635 case 54: /* stfd */
8636 /* These are the PMLS cases, where we just need to tack a prefix
dd9b12c2 8637 on the insn. */
77486630 8638 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
4a421c53 8639 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8640 off = insn2 & 0xffff;
4a421c53
AM
8641 break;
8642
8643 case 58: /* lwa, ld */
dd9b12c2 8644 if ((insn2 & 1) != 0)
4a421c53 8645 return FALSE;
77486630 8646 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8647 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8648 | (insn2 & (31ULL << 21)));
dd9b12c2 8649 off = insn2 & 0xfffc;
4a421c53
AM
8650 break;
8651
8652 case 57: /* lxsd, lxssp */
dd9b12c2 8653 if ((insn2 & 3) < 2)
4a421c53 8654 return FALSE;
77486630 8655 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8656 | ((40ULL | (insn2 & 3)) << 26)
8657 | (insn2 & (31ULL << 21)));
dd9b12c2 8658 off = insn2 & 0xfffc;
4a421c53
AM
8659 break;
8660
8661 case 61: /* stxsd, stxssp, lxv, stxv */
8662 if ((insn2 & 3) == 0)
8663 return FALSE;
8664 else if ((insn2 & 3) >= 2)
8665 {
77486630 8666 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8667 | ((44ULL | (insn2 & 3)) << 26)
8668 | (insn2 & (31ULL << 21)));
dd9b12c2 8669 off = insn2 & 0xfffc;
4a421c53
AM
8670 }
8671 else
8672 {
77486630 8673 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8674 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8675 | (insn2 & (31ULL << 21)));
dd9b12c2 8676 off = insn2 & 0xfff0;
4a421c53
AM
8677 }
8678 break;
8679
8680 case 56: /* lq */
77486630 8681 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53 8682 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8683 off = insn2 & 0xffff;
4a421c53
AM
8684 break;
8685
94ba9882
AM
8686 case 6: /* lxvp, stxvp */
8687 if ((insn2 & 0xe) != 0)
8688 return FALSE;
8689 insn1 = ((1ULL << 58) | (1ULL << 52)
8690 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8691 | (insn2 & (31ULL << 21)));
8692 off = insn2 & 0xfff0;
8693 break;
8694
4a421c53 8695 case 62: /* std, stq */
dd9b12c2 8696 if ((insn2 & 1) != 0)
4a421c53 8697 return FALSE;
77486630 8698 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8699 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8700 | (insn2 & (31ULL << 21)));
dd9b12c2 8701 off = insn2 & 0xfffc;
4a421c53
AM
8702 break;
8703 }
8704
77486630 8705 *pinsn1 = insn1;
4a421c53 8706 *pinsn2 = (uint64_t) NOP << 32;
dd9b12c2 8707 *poff = (off ^ 0x8000) - 0x8000;
4a421c53
AM
8708 return TRUE;
8709}
8710
c5614fa4
AM
8711/* Examine all relocs referencing .toc sections in order to remove
8712 unused .toc entries. */
8713
8714bfd_boolean
33c0ec9d 8715ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8716{
8717 bfd *ibfd;
8718 struct adjust_toc_info toc_inf;
67f0cbdb 8719 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8720
67f0cbdb 8721 htab->do_toc_opt = 1;
c5614fa4 8722 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8723 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8724 {
8725 asection *toc, *sec;
8726 Elf_Internal_Shdr *symtab_hdr;
8727 Elf_Internal_Sym *local_syms;
425b145b 8728 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8729 unsigned long *skip, *drop;
8730 unsigned char *used;
8731 unsigned char *keep, last, some_unused;
8732
854b41e7
AM
8733 if (!is_ppc64_elf (ibfd))
8734 continue;
8735
c5614fa4
AM
8736 toc = bfd_get_section_by_name (ibfd, ".toc");
8737 if (toc == NULL
92b7a70f 8738 || toc->size == 0
dbaa2011
AM
8739 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8740 || discarded_section (toc))
c5614fa4
AM
8741 continue;
8742
425b145b 8743 toc_relocs = NULL;
c5614fa4 8744 local_syms = NULL;
0ffa91dd 8745 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8746
8747 /* Look at sections dropped from the final link. */
8748 skip = NULL;
8749 relstart = NULL;
8750 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8751 {
8752 if (sec->reloc_count == 0
dbaa2011 8753 || !discarded_section (sec)
c5614fa4
AM
8754 || get_opd_info (sec)
8755 || (sec->flags & SEC_ALLOC) == 0
8756 || (sec->flags & SEC_DEBUGGING) != 0)
8757 continue;
8758
8759 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8760 if (relstart == NULL)
8761 goto error_ret;
8762
8763 /* Run through the relocs to see which toc entries might be
8764 unused. */
8765 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8766 {
8767 enum elf_ppc64_reloc_type r_type;
8768 unsigned long r_symndx;
8769 asection *sym_sec;
8770 struct elf_link_hash_entry *h;
8771 Elf_Internal_Sym *sym;
8772 bfd_vma val;
8773
8774 r_type = ELF64_R_TYPE (rel->r_info);
8775 switch (r_type)
8776 {
8777 default:
8778 continue;
8779
8780 case R_PPC64_TOC16:
8781 case R_PPC64_TOC16_LO:
8782 case R_PPC64_TOC16_HI:
8783 case R_PPC64_TOC16_HA:
8784 case R_PPC64_TOC16_DS:
8785 case R_PPC64_TOC16_LO_DS:
8786 break;
8787 }
8788
8789 r_symndx = ELF64_R_SYM (rel->r_info);
8790 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8791 r_symndx, ibfd))
8792 goto error_ret;
8793
8794 if (sym_sec != toc)
8795 continue;
8796
8797 if (h != NULL)
8798 val = h->root.u.def.value;
8799 else
8800 val = sym->st_value;
8801 val += rel->r_addend;
8802
8803 if (val >= toc->size)
8804 continue;
8805
8806 /* Anything in the toc ought to be aligned to 8 bytes.
8807 If not, don't mark as unused. */
8808 if (val & 7)
8809 continue;
8810
8811 if (skip == NULL)
8812 {
854b41e7 8813 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8814 if (skip == NULL)
8815 goto error_ret;
8816 }
8817
ba761f19 8818 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8819 }
8820
8821 if (elf_section_data (sec)->relocs != relstart)
8822 free (relstart);
8823 }
8824
ba761f19
AM
8825 /* For largetoc loads of address constants, we can convert
8826 . addis rx,2,addr@got@ha
8827 . ld ry,addr@got@l(rx)
8828 to
8829 . addis rx,2,addr@toc@ha
8830 . addi ry,rx,addr@toc@l
8831 when addr is within 2G of the toc pointer. This then means
8832 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8833
ba761f19
AM
8834 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8835 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8836 && toc->reloc_count != 0)
8837 {
8838 /* Read toc relocs. */
425b145b
AM
8839 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8840 info->keep_memory);
8841 if (toc_relocs == NULL)
ba761f19
AM
8842 goto error_ret;
8843
425b145b 8844 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8845 {
8846 enum elf_ppc64_reloc_type r_type;
8847 unsigned long r_symndx;
8848 asection *sym_sec;
8849 struct elf_link_hash_entry *h;
8850 Elf_Internal_Sym *sym;
8851 bfd_vma val, addr;
8852
8853 r_type = ELF64_R_TYPE (rel->r_info);
8854 if (r_type != R_PPC64_ADDR64)
8855 continue;
8856
8857 r_symndx = ELF64_R_SYM (rel->r_info);
8858 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8859 r_symndx, ibfd))
8860 goto error_ret;
8861
425b145b 8862 if (sym_sec == NULL
c27b8c2a 8863 || sym_sec->output_section == NULL
dbaa2011 8864 || discarded_section (sym_sec))
425b145b
AM
8865 continue;
8866
afe397ea 8867 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8868 continue;
8869
8870 if (h != NULL)
bddc25c9
AM
8871 {
8872 if (h->type == STT_GNU_IFUNC)
8873 continue;
8874 val = h->root.u.def.value;
8875 }
ba761f19 8876 else
bddc25c9
AM
8877 {
8878 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8879 continue;
8880 val = sym->st_value;
8881 }
ba761f19
AM
8882 val += rel->r_addend;
8883 val += sym_sec->output_section->vma + sym_sec->output_offset;
8884
8885 /* We don't yet know the exact toc pointer value, but we
8886 know it will be somewhere in the toc section. Don't
8887 optimize if the difference from any possible toc
8888 pointer is outside [ff..f80008000, 7fff7fff]. */
8889 addr = toc->output_section->vma + TOC_BASE_OFF;
8890 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8891 continue;
8892
8893 addr = toc->output_section->vma + toc->output_section->rawsize;
8894 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8895 continue;
8896
8897 if (skip == NULL)
8898 {
8899 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8900 if (skip == NULL)
8901 goto error_ret;
8902 }
8903
8904 skip[rel->r_offset >> 3]
425b145b 8905 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8906 }
ba761f19
AM
8907 }
8908
c5614fa4
AM
8909 if (skip == NULL)
8910 continue;
8911
8912 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8913 if (used == NULL)
8914 {
8915 error_ret:
c9594989 8916 if (symtab_hdr->contents != (unsigned char *) local_syms)
c5614fa4
AM
8917 free (local_syms);
8918 if (sec != NULL
c5614fa4
AM
8919 && elf_section_data (sec)->relocs != relstart)
8920 free (relstart);
c9594989 8921 if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 8922 free (toc_relocs);
c9594989 8923 free (skip);
c5614fa4
AM
8924 return FALSE;
8925 }
8926
30038c59
AM
8927 /* Now check all kept sections that might reference the toc.
8928 Check the toc itself last. */
8929 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8930 : ibfd->sections);
c5614fa4 8931 sec != NULL;
c5614fa4 8932 sec = (sec == toc ? NULL
c5614fa4 8933 : sec->next == NULL ? toc
30038c59 8934 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8935 : sec->next))
8936 {
8937 int repeat;
8938
8939 if (sec->reloc_count == 0
dbaa2011 8940 || discarded_section (sec)
c5614fa4
AM
8941 || get_opd_info (sec)
8942 || (sec->flags & SEC_ALLOC) == 0
8943 || (sec->flags & SEC_DEBUGGING) != 0)
8944 continue;
8945
854b41e7
AM
8946 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8947 info->keep_memory);
c5614fa4 8948 if (relstart == NULL)
2915c55b
JK
8949 {
8950 free (used);
8951 goto error_ret;
8952 }
c5614fa4
AM
8953
8954 /* Mark toc entries referenced as used. */
c5614fa4 8955 do
d4f1ee75
AM
8956 {
8957 repeat = 0;
8958 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8959 {
8960 enum elf_ppc64_reloc_type r_type;
8961 unsigned long r_symndx;
8962 asection *sym_sec;
8963 struct elf_link_hash_entry *h;
8964 Elf_Internal_Sym *sym;
8965 bfd_vma val;
98528052 8966
d4f1ee75 8967 r_type = ELF64_R_TYPE (rel->r_info);
d4f1ee75
AM
8968 switch (r_type)
8969 {
8970 case R_PPC64_TOC16:
8971 case R_PPC64_TOC16_LO:
8972 case R_PPC64_TOC16_HI:
8973 case R_PPC64_TOC16_HA:
8974 case R_PPC64_TOC16_DS:
8975 case R_PPC64_TOC16_LO_DS:
8976 /* In case we're taking addresses of toc entries. */
8977 case R_PPC64_ADDR64:
8978 break;
c5614fa4 8979
d4f1ee75
AM
8980 default:
8981 continue;
8982 }
c5614fa4 8983
d4f1ee75
AM
8984 r_symndx = ELF64_R_SYM (rel->r_info);
8985 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8986 r_symndx, ibfd))
8987 {
8988 free (used);
8989 goto error_ret;
8990 }
c5614fa4 8991
d4f1ee75
AM
8992 if (sym_sec != toc)
8993 continue;
c5614fa4 8994
d4f1ee75
AM
8995 if (h != NULL)
8996 val = h->root.u.def.value;
8997 else
8998 val = sym->st_value;
8999 val += rel->r_addend;
ba761f19 9000
d4f1ee75
AM
9001 if (val >= toc->size)
9002 continue;
ba761f19 9003
d4f1ee75
AM
9004 if ((skip[val >> 3] & can_optimize) != 0)
9005 {
9006 bfd_vma off;
9007 unsigned char opc;
9008
9009 switch (r_type)
9010 {
9011 case R_PPC64_TOC16_HA:
ba761f19 9012 break;
ba761f19 9013
d4f1ee75
AM
9014 case R_PPC64_TOC16_LO_DS:
9015 off = rel->r_offset;
9016 off += (bfd_big_endian (ibfd) ? -2 : 3);
9017 if (!bfd_get_section_contents (ibfd, sec, &opc,
9018 off, 1))
9019 {
9020 free (used);
9021 goto error_ret;
9022 }
9023 if ((opc & (0x3f << 2)) == (58u << 2))
9024 break;
1a0670f3 9025 /* Fall through. */
ba761f19 9026
d4f1ee75
AM
9027 default:
9028 /* Wrong sort of reloc, or not a ld. We may
9029 as well clear ref_from_discarded too. */
9030 skip[val >> 3] = 0;
9031 }
9032 }
9033
9034 if (sec != toc)
9035 used[val >> 3] = 1;
9036 /* For the toc section, we only mark as used if this
9037 entry itself isn't unused. */
9038 else if ((used[rel->r_offset >> 3]
9039 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9040 && !used[val >> 3])
9041 {
9042 /* Do all the relocs again, to catch reference
9043 chains. */
9044 repeat = 1;
9045 used[val >> 3] = 1;
9046 }
9047 }
9048 }
c5614fa4 9049 while (repeat);
854b41e7
AM
9050
9051 if (elf_section_data (sec)->relocs != relstart)
9052 free (relstart);
c5614fa4
AM
9053 }
9054
9055 /* Merge the used and skip arrays. Assume that TOC
9056 doublewords not appearing as either used or unused belong
de194d85 9057 to an entry more than one doubleword in size. */
c5614fa4
AM
9058 for (drop = skip, keep = used, last = 0, some_unused = 0;
9059 drop < skip + (toc->size + 7) / 8;
9060 ++drop, ++keep)
9061 {
9062 if (*keep)
9063 {
ba761f19
AM
9064 *drop &= ~ref_from_discarded;
9065 if ((*drop & can_optimize) != 0)
9066 some_unused = 1;
c5614fa4
AM
9067 last = 0;
9068 }
b140b010 9069 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9070 {
9071 some_unused = 1;
ba761f19 9072 last = ref_from_discarded;
c5614fa4
AM
9073 }
9074 else
9075 *drop = last;
9076 }
9077
9078 free (used);
9079
9080 if (some_unused)
9081 {
9082 bfd_byte *contents, *src;
9083 unsigned long off;
d62b3684 9084 Elf_Internal_Sym *sym;
ba761f19 9085 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9086
9087 /* Shuffle the toc contents, and at the same time convert the
9088 skip array from booleans into offsets. */
9089 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9090 goto error_ret;
9091
9092 elf_section_data (toc)->this_hdr.contents = contents;
9093
9094 for (src = contents, off = 0, drop = skip;
9095 src < contents + toc->size;
9096 src += 8, ++drop)
9097 {
ba761f19
AM
9098 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9099 off += 8;
c5614fa4
AM
9100 else if (off != 0)
9101 {
9102 *drop = off;
9103 memcpy (src - off, src, 8);
9104 }
9105 }
854b41e7 9106 *drop = off;
c5614fa4
AM
9107 toc->rawsize = toc->size;
9108 toc->size = src - contents - off;
9109
ba761f19
AM
9110 /* Adjust addends for relocs against the toc section sym,
9111 and optimize any accesses we can. */
c5614fa4
AM
9112 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9113 {
9114 if (sec->reloc_count == 0
dbaa2011 9115 || discarded_section (sec))
c5614fa4
AM
9116 continue;
9117
9118 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9119 info->keep_memory);
c5614fa4
AM
9120 if (relstart == NULL)
9121 goto error_ret;
9122
9123 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9124 {
9125 enum elf_ppc64_reloc_type r_type;
9126 unsigned long r_symndx;
9127 asection *sym_sec;
9128 struct elf_link_hash_entry *h;
854b41e7 9129 bfd_vma val;
c5614fa4
AM
9130
9131 r_type = ELF64_R_TYPE (rel->r_info);
9132 switch (r_type)
9133 {
9134 default:
9135 continue;
9136
9137 case R_PPC64_TOC16:
9138 case R_PPC64_TOC16_LO:
9139 case R_PPC64_TOC16_HI:
9140 case R_PPC64_TOC16_HA:
9141 case R_PPC64_TOC16_DS:
9142 case R_PPC64_TOC16_LO_DS:
9143 case R_PPC64_ADDR64:
9144 break;
9145 }
9146
9147 r_symndx = ELF64_R_SYM (rel->r_info);
9148 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9149 r_symndx, ibfd))
9150 goto error_ret;
9151
ba761f19 9152 if (sym_sec != toc)
c5614fa4
AM
9153 continue;
9154
ba761f19
AM
9155 if (h != NULL)
9156 val = h->root.u.def.value;
9157 else
9158 {
9159 val = sym->st_value;
9160 if (val != 0)
9161 local_toc_syms = TRUE;
9162 }
9163
9164 val += rel->r_addend;
854b41e7
AM
9165
9166 if (val > toc->rawsize)
9167 val = toc->rawsize;
ba761f19
AM
9168 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9169 continue;
9170 else if ((skip[val >> 3] & can_optimize) != 0)
9171 {
9172 Elf_Internal_Rela *tocrel
425b145b 9173 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9174 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9175
9176 switch (r_type)
9177 {
9178 case R_PPC64_TOC16_HA:
9179 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9180 break;
9181
9182 case R_PPC64_TOC16_LO_DS:
9183 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9184 break;
9185
9186 default:
28942f62
AM
9187 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9188 ppc_howto_init ();
b140b010 9189 info->callbacks->einfo
695344c0 9190 /* xgettext:c-format */
174d0a74 9191 (_("%H: %s references "
b140b010
AM
9192 "optimized away TOC entry\n"),
9193 ibfd, sec, rel->r_offset,
9194 ppc64_elf_howto_table[r_type]->name);
9195 bfd_set_error (bfd_error_bad_value);
9196 goto error_ret;
ba761f19
AM
9197 }
9198 rel->r_addend = tocrel->r_addend;
9199 elf_section_data (sec)->relocs = relstart;
9200 continue;
9201 }
9202
9203 if (h != NULL || sym->st_value != 0)
9204 continue;
854b41e7
AM
9205
9206 rel->r_addend -= skip[val >> 3];
9207 elf_section_data (sec)->relocs = relstart;
c5614fa4 9208 }
854b41e7
AM
9209
9210 if (elf_section_data (sec)->relocs != relstart)
9211 free (relstart);
c5614fa4
AM
9212 }
9213
9214 /* We shouldn't have local or global symbols defined in the TOC,
9215 but handle them anyway. */
df22d223
AM
9216 if (local_syms != NULL)
9217 for (sym = local_syms;
9218 sym < local_syms + symtab_hdr->sh_info;
9219 ++sym)
9220 if (sym->st_value != 0
9221 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9222 {
9223 unsigned long i;
854b41e7 9224
df22d223
AM
9225 if (sym->st_value > toc->rawsize)
9226 i = toc->rawsize >> 3;
9227 else
9228 i = sym->st_value >> 3;
854b41e7 9229
df22d223
AM
9230 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9231 {
9232 if (local_toc_syms)
4eca0228 9233 _bfd_error_handler
df22d223
AM
9234 (_("%s defined on removed toc entry"),
9235 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9236 do
9237 ++i;
9238 while ((skip[i] & (ref_from_discarded | can_optimize)));
9239 sym->st_value = (bfd_vma) i << 3;
9240 }
d62b3684 9241
df22d223
AM
9242 sym->st_value -= skip[i];
9243 symtab_hdr->contents = (unsigned char *) local_syms;
9244 }
c5614fa4 9245
854b41e7 9246 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9247 if (toc_inf.global_toc_syms)
9248 {
9249 toc_inf.toc = toc;
9250 toc_inf.skip = skip;
9251 toc_inf.global_toc_syms = FALSE;
9252 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9253 &toc_inf);
9254 }
854b41e7
AM
9255
9256 if (toc->reloc_count != 0)
9257 {
d4730f92 9258 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9259 Elf_Internal_Rela *wrel;
9260 bfd_size_type sz;
9261
854b41e7 9262 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9263 if (toc_relocs == NULL)
9264 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9265 info->keep_memory);
9266 if (toc_relocs == NULL)
9267 goto error_ret;
9268
425b145b
AM
9269 wrel = toc_relocs;
9270 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9271 if ((skip[rel->r_offset >> 3]
9272 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9273 {
9274 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9275 wrel->r_info = rel->r_info;
9276 wrel->r_addend = rel->r_addend;
9277 ++wrel;
9278 }
9279 else if (!dec_dynrel_count (rel->r_info, toc, info,
9280 &local_syms, NULL, NULL))
9281 goto error_ret;
9282
425b145b
AM
9283 elf_section_data (toc)->relocs = toc_relocs;
9284 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9285 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9286 sz = rel_hdr->sh_entsize;
9287 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9288 }
c5614fa4 9289 }
c9594989 9290 else if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 9291 free (toc_relocs);
c5614fa4
AM
9292
9293 if (local_syms != NULL
9294 && symtab_hdr->contents != (unsigned char *) local_syms)
9295 {
9296 if (!info->keep_memory)
9297 free (local_syms);
9298 else
9299 symtab_hdr->contents = (unsigned char *) local_syms;
9300 }
9301 free (skip);
9302 }
9303
066f4018 9304 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
9305 a GOT relative or PC relative access, possibly reducing the
9306 number of GOT entries. */
066f4018
AM
9307 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9308 {
9309 asection *sec;
9310 Elf_Internal_Shdr *symtab_hdr;
9311 Elf_Internal_Sym *local_syms;
9312 Elf_Internal_Rela *relstart, *rel;
9313 bfd_vma got;
9314
9315 if (!is_ppc64_elf (ibfd))
9316 continue;
9317
903b777d 9318 if (!ppc64_elf_tdata (ibfd)->has_optrel)
066f4018
AM
9319 continue;
9320
9321 sec = ppc64_elf_tdata (ibfd)->got;
903b777d
AM
9322 got = 0;
9323 if (sec != NULL)
9324 got = sec->output_section->vma + sec->output_offset + 0x8000;
066f4018
AM
9325
9326 local_syms = NULL;
9327 symtab_hdr = &elf_symtab_hdr (ibfd);
9328
9329 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9330 {
9331 if (sec->reloc_count == 0
903b777d 9332 || !ppc64_elf_section_data (sec)->has_optrel
066f4018
AM
9333 || discarded_section (sec))
9334 continue;
9335
9336 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9337 info->keep_memory);
9338 if (relstart == NULL)
9339 {
9340 got_error_ret:
c9594989 9341 if (symtab_hdr->contents != (unsigned char *) local_syms)
066f4018
AM
9342 free (local_syms);
9343 if (sec != NULL
066f4018
AM
9344 && elf_section_data (sec)->relocs != relstart)
9345 free (relstart);
9346 return FALSE;
9347 }
9348
9349 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9350 {
9351 enum elf_ppc64_reloc_type r_type;
9352 unsigned long r_symndx;
9353 Elf_Internal_Sym *sym;
9354 asection *sym_sec;
9355 struct elf_link_hash_entry *h;
9356 struct got_entry *ent;
133a1f60 9357 bfd_vma val, pc;
4a421c53 9358 unsigned char buf[8];
066f4018 9359 unsigned int insn;
903b777d 9360 enum {no_check, check_lo, check_ha} insn_check;
066f4018
AM
9361
9362 r_type = ELF64_R_TYPE (rel->r_info);
903b777d
AM
9363 switch (r_type)
9364 {
9365 default:
9366 insn_check = no_check;
9367 break;
9368
9369 case R_PPC64_PLT16_HA:
9370 case R_PPC64_GOT_TLSLD16_HA:
9371 case R_PPC64_GOT_TLSGD16_HA:
9372 case R_PPC64_GOT_TPREL16_HA:
9373 case R_PPC64_GOT_DTPREL16_HA:
9374 case R_PPC64_GOT16_HA:
9375 case R_PPC64_TOC16_HA:
9376 insn_check = check_ha;
9377 break;
9378
9379 case R_PPC64_PLT16_LO:
9380 case R_PPC64_PLT16_LO_DS:
9381 case R_PPC64_GOT_TLSLD16_LO:
9382 case R_PPC64_GOT_TLSGD16_LO:
9383 case R_PPC64_GOT_TPREL16_LO_DS:
9384 case R_PPC64_GOT_DTPREL16_LO_DS:
9385 case R_PPC64_GOT16_LO:
9386 case R_PPC64_GOT16_LO_DS:
9387 case R_PPC64_TOC16_LO:
9388 case R_PPC64_TOC16_LO_DS:
9389 insn_check = check_lo;
9390 break;
9391 }
9392
9393 if (insn_check != no_check)
9394 {
9395 bfd_vma off = rel->r_offset & ~3;
9396
9397 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9398 goto got_error_ret;
9399
9400 insn = bfd_get_32 (ibfd, buf);
9401 if (insn_check == check_lo
9402 ? !ok_lo_toc_insn (insn, r_type)
2365f8d7 9403 : ((insn & ((0x3fu << 26) | 0x1f << 16))
903b777d
AM
9404 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9405 {
9406 char str[12];
9407
9408 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9409 sprintf (str, "%#08x", insn);
9410 info->callbacks->einfo
9411 /* xgettext:c-format */
9412 (_("%H: got/toc optimization is not supported for"
9413 " %s instruction\n"),
9414 ibfd, sec, rel->r_offset & ~3, str);
9415 continue;
9416 }
9417 }
9418
066f4018
AM
9419 switch (r_type)
9420 {
bb22a418
AM
9421 /* Note that we don't delete GOT entries for
9422 R_PPC64_GOT16_DS since we'd need a lot more
9423 analysis. For starters, the preliminary layout is
9424 before the GOT, PLT, dynamic sections and stubs are
9425 laid out. Then we'd need to allow for changes in
9426 distance between sections caused by alignment. */
066f4018
AM
9427 default:
9428 continue;
9429
066f4018
AM
9430 case R_PPC64_GOT16_HA:
9431 case R_PPC64_GOT16_LO_DS:
4a421c53 9432 case R_PPC64_GOT_PCREL34:
066f4018
AM
9433 break;
9434 }
9435
9436 r_symndx = ELF64_R_SYM (rel->r_info);
9437 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9438 r_symndx, ibfd))
9439 goto got_error_ret;
9440
6d5554a6
AM
9441 if (sym_sec == NULL
9442 || sym_sec->output_section == NULL
9443 || discarded_section (sym_sec))
9444 continue;
9445
06507dab
AM
9446 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9447 continue;
9448
066f4018
AM
9449 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9450 continue;
9451
9452 if (h != NULL)
9453 val = h->root.u.def.value;
9454 else
9455 val = sym->st_value;
133a1f60 9456 val += rel->r_addend;
066f4018
AM
9457 val += sym_sec->output_section->vma + sym_sec->output_offset;
9458
bb22a418
AM
9459/* Fudge factor to allow for the fact that the preliminary layout
9460 isn't exact. Reduce limits by this factor. */
9461#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9462
066f4018
AM
9463 switch (r_type)
9464 {
9465 default:
9466 continue;
9467
066f4018 9468 case R_PPC64_GOT16_HA:
bb22a418
AM
9469 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9470 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9471 continue;
9472
9473 if (!bfd_get_section_contents (ibfd, sec, buf,
9474 rel->r_offset & ~3, 4))
9475 goto got_error_ret;
9476 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9477 if (((insn & ((0x3fu << 26) | 0x1f << 16))
066f4018
AM
9478 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9479 continue;
9480 break;
9481
9482 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9483 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9484 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9485 continue;
9486 if (!bfd_get_section_contents (ibfd, sec, buf,
9487 rel->r_offset & ~3, 4))
9488 goto got_error_ret;
9489 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9490 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
066f4018
AM
9491 continue;
9492 break;
4a421c53
AM
9493
9494 case R_PPC64_GOT_PCREL34:
9495 pc = rel->r_offset;
9496 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9497 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9498 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9499 continue;
9500 if (!bfd_get_section_contents (ibfd, sec, buf,
9501 rel->r_offset & ~3, 8))
9502 goto got_error_ret;
9503 insn = bfd_get_32 (ibfd, buf);
9504 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9505 continue;
9506 insn = bfd_get_32 (ibfd, buf + 4);
2365f8d7 9507 if ((insn & (0x3fu << 26)) != 57u << 26)
4a421c53
AM
9508 continue;
9509 break;
066f4018 9510 }
bb22a418 9511#undef LIMIT_ADJUST
066f4018
AM
9512
9513 if (h != NULL)
9514 ent = h->got.glist;
9515 else
9516 {
9517 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9518 ent = local_got_ents[r_symndx];
9519 }
9520 for (; ent != NULL; ent = ent->next)
133a1f60 9521 if (ent->addend == rel->r_addend
066f4018
AM
9522 && ent->owner == ibfd
9523 && ent->tls_type == 0)
9524 break;
9525 BFD_ASSERT (ent && ent->got.refcount > 0);
9526 ent->got.refcount -= 1;
9527 }
9528
9529 if (elf_section_data (sec)->relocs != relstart)
9530 free (relstart);
9531 }
9532
9533 if (local_syms != NULL
9534 && symtab_hdr->contents != (unsigned char *) local_syms)
9535 {
9536 if (!info->keep_memory)
9537 free (local_syms);
9538 else
9539 symtab_hdr->contents = (unsigned char *) local_syms;
9540 }
9541 }
9542
c5614fa4
AM
9543 return TRUE;
9544}
9545
1bbe0902
AM
9546/* Return true iff input section I references the TOC using
9547 instructions limited to +/-32k offsets. */
9548
9549bfd_boolean
9550ppc64_elf_has_small_toc_reloc (asection *i)
9551{
9552 return (is_ppc64_elf (i->owner)
9553 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9554}
9555
927be08e
AM
9556/* Allocate space for one GOT entry. */
9557
9558static void
9559allocate_got (struct elf_link_hash_entry *h,
9560 struct bfd_link_info *info,
9561 struct got_entry *gent)
9562{
9563 struct ppc_link_hash_table *htab = ppc_hash_table (info);
ed7007c1 9564 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
927be08e
AM
9565 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9566 ? 16 : 8);
9567 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9568 ? 2 : 1) * sizeof (Elf64_External_Rela);
9569 asection *got = ppc64_elf_tdata (gent->owner)->got;
9570
9571 gent->got.offset = got->size;
9572 got->size += entsize;
9573
19e08130 9574 if (h->type == STT_GNU_IFUNC)
927be08e 9575 {
33e44f2e 9576 htab->elf.irelplt->size += rentsize;
19e08130 9577 htab->got_reli_size += rentsize;
927be08e 9578 }
f15d0b54 9579 else if (((bfd_link_pic (info)
f749f26e 9580 && !(gent->tls_type != 0
f15d0b54
AM
9581 && bfd_link_executable (info)
9582 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9583 || (htab->elf.dynamic_sections_created
9584 && h->dynindx != -1
9585 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9586 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9587 {
19e08130 9588 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9589 relgot->size += rentsize;
927be08e
AM
9590 }
9591}
9592
7865406b
AM
9593/* This function merges got entries in the same toc group. */
9594
9595static void
9596merge_got_entries (struct got_entry **pent)
9597{
9598 struct got_entry *ent, *ent2;
9599
9600 for (ent = *pent; ent != NULL; ent = ent->next)
9601 if (!ent->is_indirect)
9602 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9603 if (!ent2->is_indirect
9604 && ent2->addend == ent->addend
9605 && ent2->tls_type == ent->tls_type
9606 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9607 {
9608 ent2->is_indirect = TRUE;
9609 ent2->got.ent = ent;
9610 }
9611}
9612
46434633 9613/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9614
9615static bfd_boolean
46434633
AM
9616ensure_undef_dynamic (struct bfd_link_info *info,
9617 struct elf_link_hash_entry *h)
f0158f44
AM
9618{
9619 struct elf_link_hash_table *htab = elf_hash_table (info);
9620
9621 if (htab->dynamic_sections_created
46434633
AM
9622 && ((info->dynamic_undefined_weak != 0
9623 && h->root.type == bfd_link_hash_undefweak)
9624 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9625 && h->dynindx == -1
9626 && !h->forced_local
9627 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9628 return bfd_elf_link_record_dynamic_symbol (info, h);
9629 return TRUE;
9630}
9631
65f38f15
AM
9632/* Allocate space in .plt, .got and associated reloc sections for
9633 dynamic relocs. */
5bd4f169 9634
b34976b6 9635static bfd_boolean
4ce794b7 9636allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9637{
65f38f15
AM
9638 struct bfd_link_info *info;
9639 struct ppc_link_hash_table *htab;
5bd4f169 9640 asection *s;
65f38f15 9641 struct ppc_link_hash_entry *eh;
0b8bcf0d 9642 struct got_entry **pgent, *gent;
5bd4f169 9643
e92d460e 9644 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9645 return TRUE;
5bd4f169 9646
65f38f15
AM
9647 info = (struct bfd_link_info *) inf;
9648 htab = ppc_hash_table (info);
4dfe6ac6
NC
9649 if (htab == NULL)
9650 return FALSE;
5bd4f169 9651
ed7007c1 9652 eh = ppc_elf_hash_entry (h);
951fd09b
AM
9653 /* Run through the TLS GD got entries first if we're changing them
9654 to TPREL. */
b00a0a86 9655 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
951fd09b
AM
9656 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9657 if (gent->got.refcount > 0
9658 && (gent->tls_type & TLS_GD) != 0)
9659 {
9660 /* This was a GD entry that has been converted to TPREL. If
9661 there happens to be a TPREL entry we can use that one. */
9662 struct got_entry *ent;
9663 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9664 if (ent->got.refcount > 0
9665 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9666 && ent->addend == gent->addend
9667 && ent->owner == gent->owner)
951fd09b
AM
9668 {
9669 gent->got.refcount = 0;
9670 break;
9671 }
9672
9673 /* If not, then we'll be using our own TPREL entry. */
9674 if (gent->got.refcount != 0)
9675 gent->tls_type = TLS_TLS | TLS_TPREL;
9676 }
9677
7865406b
AM
9678 /* Remove any list entry that won't generate a word in the GOT before
9679 we call merge_got_entries. Otherwise we risk merging to empty
9680 entries. */
0b8bcf0d
AM
9681 pgent = &h->got.glist;
9682 while ((gent = *pgent) != NULL)
411e1bfb 9683 if (gent->got.refcount > 0)
7865406b
AM
9684 {
9685 if ((gent->tls_type & TLS_LD) != 0
f749f26e 9686 && SYMBOL_REFERENCES_LOCAL (info, h))
7865406b
AM
9687 {
9688 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9689 *pgent = gent->next;
9690 }
9691 else
9692 pgent = &gent->next;
9693 }
9694 else
9695 *pgent = gent->next;
9696
9697 if (!htab->do_multi_toc)
9698 merge_got_entries (&h->got.glist);
9699
9700 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9701 if (!gent->is_indirect)
411e1bfb 9702 {
ec73ddcd
AM
9703 /* Ensure we catch all the cases where this symbol should
9704 be made dynamic. */
46434633 9705 if (!ensure_undef_dynamic (info, h))
f0158f44 9706 return FALSE;
65f38f15 9707
0c8d6e5c 9708 if (!is_ppc64_elf (gent->owner))
927be08e 9709 abort ();
0ffa91dd 9710
927be08e 9711 allocate_got (h, info, gent);
411e1bfb 9712 }
65f38f15 9713
954b63d4
AM
9714 /* If no dynamic sections we can't have dynamic relocs, except for
9715 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9716 if (!htab->elf.dynamic_sections_created
9717 && h->type != STT_GNU_IFUNC)
190eb1dd 9718 h->dyn_relocs = NULL;
8a2058b5 9719
529fe20e
AM
9720 /* Discard relocs on undefined symbols that must be local. */
9721 else if (h->root.type == bfd_link_hash_undefined
9722 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
190eb1dd 9723 h->dyn_relocs = NULL;
529fe20e 9724
954b63d4
AM
9725 /* Also discard relocs on undefined weak syms with non-default
9726 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9727 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 9728 h->dyn_relocs = NULL;
954b63d4 9729
190eb1dd 9730 if (h->dyn_relocs != NULL)
65f38f15 9731 {
8a2058b5
AM
9732 struct elf_dyn_relocs *p, **pp;
9733
57e7d118
AM
9734 /* In the shared -Bsymbolic case, discard space allocated for
9735 dynamic pc-relative relocs against symbols which turn out to
9736 be defined in regular objects. For the normal shared case,
9737 discard space for relocs that have become local due to symbol
9738 visibility changes. */
57e7d118 9739 if (bfd_link_pic (info))
65f38f15 9740 {
57e7d118
AM
9741 /* Relocs that use pc_count are those that appear on a call
9742 insn, or certain REL relocs (see must_be_dyn_reloc) that
9743 can be generated via assembly. We want calls to
9744 protected symbols to resolve directly to the function
9745 rather than going via the plt. If people want function
9746 pointer comparisons to work as expected then they should
9747 avoid writing weird assembly. */
9748 if (SYMBOL_CALLS_LOCAL (info, h))
9749 {
190eb1dd 9750 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
57e7d118
AM
9751 {
9752 p->count -= p->pc_count;
9753 p->pc_count = 0;
9754 if (p->count == 0)
9755 *pp = p->next;
9756 else
9757 pp = &p->next;
9758 }
9759 }
65f38f15 9760
190eb1dd 9761 if (h->dyn_relocs != NULL)
5bd4f169 9762 {
ec73ddcd
AM
9763 /* Ensure we catch all the cases where this symbol
9764 should be made dynamic. */
46434633 9765 if (!ensure_undef_dynamic (info, h))
f0158f44 9766 return FALSE;
5bd4f169 9767 }
65f38f15 9768 }
ec73ddcd
AM
9769
9770 /* For a fixed position executable, discard space for
9771 relocs against symbols which are not dynamic. */
9772 else if (h->type != STT_GNU_IFUNC)
57e7d118 9773 {
529fe20e
AM
9774 if (h->dynamic_adjusted
9775 && !h->def_regular
9776 && !ELF_COMMON_DEF_P (h))
f0158f44 9777 {
ec73ddcd
AM
9778 /* Ensure we catch all the cases where this symbol
9779 should be made dynamic. */
46434633 9780 if (!ensure_undef_dynamic (info, h))
f0158f44 9781 return FALSE;
dfbb6ac9 9782
ec73ddcd 9783 /* But if that didn't work out, discard dynamic relocs. */
f0158f44 9784 if (h->dynindx == -1)
190eb1dd 9785 h->dyn_relocs = NULL;
f0158f44
AM
9786 }
9787 else
190eb1dd 9788 h->dyn_relocs = NULL;
57e7d118
AM
9789 }
9790
9791 /* Finally, allocate space. */
190eb1dd 9792 for (p = h->dyn_relocs; p != NULL; p = p->next)
57e7d118
AM
9793 {
9794 asection *sreloc = elf_section_data (p->sec)->sreloc;
9795 if (eh->elf.type == STT_GNU_IFUNC)
9796 sreloc = htab->elf.irelplt;
9797 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9798 }
65f38f15 9799 }
57e7d118 9800
2d7ad24e
AM
9801 /* We might need a PLT entry when the symbol
9802 a) is dynamic, or
9803 b) is an ifunc, or
9804 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9805 d) has plt16 relocs and we are linking statically. */
9806 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9807 || h->type == STT_GNU_IFUNC
9808 || (h->needs_plt && h->dynamic_adjusted)
9809 || (h->needs_plt
9810 && h->def_regular
9811 && !htab->elf.dynamic_sections_created
3e04d765 9812 && !htab->can_convert_all_inline_plt
ed7007c1 9813 && (ppc_elf_hash_entry (h)->tls_mask
2d7ad24e 9814 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 9815 {
57e7d118
AM
9816 struct plt_entry *pent;
9817 bfd_boolean doneone = FALSE;
9818 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9819 if (pent->plt.refcount > 0)
9820 {
9821 if (!htab->elf.dynamic_sections_created
9822 || h->dynindx == -1)
9823 {
2d7ad24e
AM
9824 if (h->type == STT_GNU_IFUNC)
9825 {
9826 s = htab->elf.iplt;
9827 pent->plt.offset = s->size;
9828 s->size += PLT_ENTRY_SIZE (htab);
9829 s = htab->elf.irelplt;
9830 }
9831 else
9832 {
9833 s = htab->pltlocal;
9834 pent->plt.offset = s->size;
9835 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9836 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9837 }
57e7d118
AM
9838 }
9839 else
9840 {
9841 /* If this is the first .plt entry, make room for the special
9842 first entry. */
9843 s = htab->elf.splt;
9844 if (s->size == 0)
9845 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9846
57e7d118 9847 pent->plt.offset = s->size;
65f38f15 9848
57e7d118
AM
9849 /* Make room for this entry. */
9850 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9851
57e7d118
AM
9852 /* Make room for the .glink code. */
9853 s = htab->glink;
9854 if (s->size == 0)
9e390558 9855 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9856 if (htab->opd_abi)
9857 {
9858 /* We need bigger stubs past index 32767. */
9e390558 9859 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9860 s->size += 4;
9861 s->size += 2*4;
9862 }
9863 else
9864 s->size += 4;
65f38f15 9865
57e7d118
AM
9866 /* We also need to make an entry in the .rela.plt section. */
9867 s = htab->elf.srelplt;
9868 }
2d7ad24e
AM
9869 if (s != NULL)
9870 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
9871 doneone = TRUE;
9872 }
9873 else
9874 pent->plt.offset = (bfd_vma) -1;
9875 if (!doneone)
9876 {
9877 h->plt.plist = NULL;
9878 h->needs_plt = 0;
9879 }
65f38f15 9880 }
57e7d118 9881 else
65f38f15 9882 {
57e7d118
AM
9883 h->plt.plist = NULL;
9884 h->needs_plt = 0;
65f38f15
AM
9885 }
9886
b34976b6 9887 return TRUE;
65f38f15
AM
9888}
9889
9e390558
AM
9890#define PPC_LO(v) ((v) & 0xffff)
9891#define PPC_HI(v) (((v) >> 16) & 0xffff)
9892#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
9893#define D34(v) \
9894 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9895#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 9896
a345bc8d
AM
9897/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9898 to set up space for global entry stubs. These are put in glink,
9899 after the branch table. */
65f38f15 9900
b34976b6 9901static bfd_boolean
a345bc8d 9902size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9903{
a345bc8d
AM
9904 struct bfd_link_info *info;
9905 struct ppc_link_hash_table *htab;
9906 struct plt_entry *pent;
9e390558 9907 asection *s, *plt;
65f38f15 9908
a345bc8d
AM
9909 if (h->root.type == bfd_link_hash_indirect)
9910 return TRUE;
65f38f15 9911
a345bc8d
AM
9912 if (!h->pointer_equality_needed)
9913 return TRUE;
65f38f15 9914
a345bc8d
AM
9915 if (h->def_regular)
9916 return TRUE;
65f38f15 9917
a345bc8d
AM
9918 info = inf;
9919 htab = ppc_hash_table (info);
9920 if (htab == NULL)
9921 return FALSE;
9922
9e390558
AM
9923 s = htab->global_entry;
9924 plt = htab->elf.splt;
a345bc8d
AM
9925 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9926 if (pent->plt.offset != (bfd_vma) -1
9927 && pent->addend == 0)
9928 {
afe397ea
AM
9929 /* For ELFv2, if this symbol is not defined in a regular file
9930 and we are not generating a shared library or pie, then we
9931 need to define the symbol in the executable on a call stub.
9932 This is to avoid text relocations. */
9e390558
AM
9933 bfd_vma off, stub_align, stub_off, stub_size;
9934 unsigned int align_power;
9935
9936 stub_size = 16;
9937 stub_off = s->size;
9938 if (htab->params->plt_stub_align >= 0)
9939 align_power = htab->params->plt_stub_align;
9940 else
9941 align_power = -htab->params->plt_stub_align;
9942 /* Setting section alignment is delayed until we know it is
9943 non-empty. Otherwise the .text output section will be
9944 aligned at least to plt_stub_align even when no global
9945 entry stubs are needed. */
9946 if (s->alignment_power < align_power)
9947 s->alignment_power = align_power;
9948 stub_align = (bfd_vma) 1 << align_power;
9949 if (htab->params->plt_stub_align >= 0
9950 || ((((stub_off + stub_size - 1) & -stub_align)
9951 - (stub_off & -stub_align))
9952 > ((stub_size - 1) & -stub_align)))
9953 stub_off = (stub_off + stub_align - 1) & -stub_align;
9954 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9955 off -= stub_off + s->output_offset + s->output_section->vma;
9956 /* Note that for --plt-stub-align negative we have a possible
9957 dependency between stub offset and size. Break that
9958 dependency by assuming the max stub size when calculating
9959 the stub offset. */
9960 if (PPC_HA (off) == 0)
9961 stub_size -= 4;
8a2058b5 9962 h->root.type = bfd_link_hash_defined;
afe397ea 9963 h->root.u.def.section = s;
9e390558
AM
9964 h->root.u.def.value = stub_off;
9965 s->size = stub_off + stub_size;
a345bc8d
AM
9966 break;
9967 }
9968 return TRUE;
9969}
9970
65f38f15
AM
9971/* Set the sizes of the dynamic sections. */
9972
b34976b6 9973static bfd_boolean
ee67d69a 9974ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9975 struct bfd_link_info *info)
65f38f15
AM
9976{
9977 struct ppc_link_hash_table *htab;
9978 bfd *dynobj;
9979 asection *s;
b34976b6 9980 bfd_boolean relocs;
65f38f15 9981 bfd *ibfd;
7865406b 9982 struct got_entry *first_tlsld;
65f38f15
AM
9983
9984 htab = ppc_hash_table (info);
4dfe6ac6
NC
9985 if (htab == NULL)
9986 return FALSE;
9987
65f38f15
AM
9988 dynobj = htab->elf.dynobj;
9989 if (dynobj == NULL)
9990 abort ();
9991
9992 if (htab->elf.dynamic_sections_created)
9993 {
9994 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9995 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9996 {
3d4d4302 9997 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9998 if (s == NULL)
9999 abort ();
eea6121a 10000 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10001 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10002 }
10003 }
10004
10005 /* Set up .got offsets for local syms, and space for local dynamic
10006 relocs. */
c72f2fb2 10007 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10008 {
411e1bfb
AM
10009 struct got_entry **lgot_ents;
10010 struct got_entry **end_lgot_ents;
e054468f
AM
10011 struct plt_entry **local_plt;
10012 struct plt_entry **end_local_plt;
f961d9dd 10013 unsigned char *lgot_masks;
65f38f15
AM
10014 bfd_size_type locsymcount;
10015 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10016
0c8d6e5c 10017 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10018 continue;
10019
10020 for (s = ibfd->sections; s != NULL; s = s->next)
10021 {
19e08130 10022 struct ppc_dyn_relocs *p;
65f38f15 10023
6edfbbad 10024 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10025 {
ec338859
AM
10026 if (!bfd_is_abs_section (p->sec)
10027 && bfd_is_abs_section (p->sec->output_section))
10028 {
10029 /* Input section has been discarded, either because
10030 it is a copy of a linkonce section or due to
10031 linker script /DISCARD/, so we'll be discarding
10032 the relocs too. */
10033 }
248866a8 10034 else if (p->count != 0)
ec338859 10035 {
19e08130
AM
10036 asection *srel = elf_section_data (p->sec)->sreloc;
10037 if (p->ifunc)
33e44f2e 10038 srel = htab->elf.irelplt;
eea6121a 10039 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10040 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10041 info->flags |= DF_TEXTREL;
ec338859 10042 }
65f38f15
AM
10043 }
10044 }
10045
411e1bfb
AM
10046 lgot_ents = elf_local_got_ents (ibfd);
10047 if (!lgot_ents)
65f38f15
AM
10048 continue;
10049
0ffa91dd 10050 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10051 locsymcount = symtab_hdr->sh_info;
411e1bfb 10052 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10053 local_plt = (struct plt_entry **) end_lgot_ents;
10054 end_local_plt = local_plt + locsymcount;
f961d9dd 10055 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10056 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10057 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10058 {
0b8bcf0d 10059 struct got_entry **pent, *ent;
411e1bfb 10060
0b8bcf0d
AM
10061 pent = lgot_ents;
10062 while ((ent = *pent) != NULL)
411e1bfb
AM
10063 if (ent->got.refcount > 0)
10064 {
e7b938ca 10065 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10066 {
927be08e 10067 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10068 *pent = ent->next;
411e1bfb
AM
10069 }
10070 else
10071 {
19e08130
AM
10072 unsigned int ent_size = 8;
10073 unsigned int rel_size = sizeof (Elf64_External_Rela);
10074
eea6121a 10075 ent->got.offset = s->size;
e7b938ca 10076 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10077 {
19e08130
AM
10078 ent_size *= 2;
10079 rel_size *= 2;
10080 }
10081 s->size += ent_size;
37da22e5 10082 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 10083 {
33e44f2e 10084 htab->elf.irelplt->size += rel_size;
19e08130
AM
10085 htab->got_reli_size += rel_size;
10086 }
93370e8e
AM
10087 else if (bfd_link_pic (info)
10088 && !(ent->tls_type != 0
10089 && bfd_link_executable (info)))
19e08130
AM
10090 {
10091 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10092 srel->size += rel_size;
927be08e 10093 }
0b8bcf0d 10094 pent = &ent->next;
411e1bfb
AM
10095 }
10096 }
10097 else
0b8bcf0d 10098 *pent = ent->next;
65f38f15 10099 }
e054468f 10100
2d7ad24e
AM
10101 /* Allocate space for plt calls to local syms. */
10102 lgot_masks = (unsigned char *) end_local_plt;
10103 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
10104 {
10105 struct plt_entry *ent;
10106
10107 for (ent = *local_plt; ent != NULL; ent = ent->next)
10108 if (ent->plt.refcount > 0)
10109 {
2d7ad24e
AM
10110 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10111 {
10112 s = htab->elf.iplt;
10113 ent->plt.offset = s->size;
10114 s->size += PLT_ENTRY_SIZE (htab);
10115 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10116 }
3e04d765
AM
10117 else if (htab->can_convert_all_inline_plt
10118 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
10119 ent->plt.offset = (bfd_vma) -1;
10120 else
10121 {
10122 s = htab->pltlocal;
10123 ent->plt.offset = s->size;
10124 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10125 if (bfd_link_pic (info))
10126 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10127 }
e054468f
AM
10128 }
10129 else
10130 ent->plt.offset = (bfd_vma) -1;
10131 }
65f38f15
AM
10132 }
10133
10134 /* Allocate global sym .plt and .got entries, and space for global
10135 sym dynamic relocs. */
4ce794b7 10136 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10137
0e1862bb 10138 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10139 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10140
7865406b 10141 first_tlsld = NULL;
c72f2fb2 10142 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10143 {
7865406b
AM
10144 struct got_entry *ent;
10145
0c8d6e5c 10146 if (!is_ppc64_elf (ibfd))
102890f0
AM
10147 continue;
10148
7865406b
AM
10149 ent = ppc64_tlsld_got (ibfd);
10150 if (ent->got.refcount > 0)
102890f0 10151 {
7865406b 10152 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10153 {
7865406b
AM
10154 ent->is_indirect = TRUE;
10155 ent->got.ent = first_tlsld;
10156 }
10157 else
10158 {
10159 if (first_tlsld == NULL)
10160 first_tlsld = ent;
10161 s = ppc64_elf_tdata (ibfd)->got;
10162 ent->got.offset = s->size;
10163 ent->owner = ibfd;
10164 s->size += 16;
f749f26e 10165 if (bfd_link_dll (info))
7865406b
AM
10166 {
10167 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10168 srel->size += sizeof (Elf64_External_Rela);
10169 }
102890f0
AM
10170 }
10171 }
10172 else
7865406b 10173 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10174 }
10175
65f38f15
AM
10176 /* We now have determined the sizes of the various dynamic sections.
10177 Allocate memory for them. */
b34976b6 10178 relocs = FALSE;
65f38f15
AM
10179 for (s = dynobj->sections; s != NULL; s = s->next)
10180 {
10181 if ((s->flags & SEC_LINKER_CREATED) == 0)
10182 continue;
10183
4ce794b7 10184 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10185 /* These haven't been allocated yet; don't strip. */
10186 continue;
33e44f2e
AM
10187 else if (s == htab->elf.sgot
10188 || s == htab->elf.splt
10189 || s == htab->elf.iplt
2d7ad24e 10190 || s == htab->pltlocal
c456f082 10191 || s == htab->glink
9e390558 10192 || s == htab->global_entry
5474d94f
AM
10193 || s == htab->elf.sdynbss
10194 || s == htab->elf.sdynrelro)
65f38f15
AM
10195 {
10196 /* Strip this section if we don't need it; see the
10197 comment below. */
5bd4f169 10198 }
58d180e8
AM
10199 else if (s == htab->glink_eh_frame)
10200 {
10201 if (!bfd_is_abs_section (s->output_section))
10202 /* Not sized yet. */
10203 continue;
10204 }
70cc837d 10205 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10206 {
c456f082 10207 if (s->size != 0)
5bd4f169 10208 {
33e44f2e 10209 if (s != htab->elf.srelplt)
b34976b6 10210 relocs = TRUE;
5bd4f169
AM
10211
10212 /* We use the reloc_count field as a counter if we need
10213 to copy relocs into the output file. */
10214 s->reloc_count = 0;
10215 }
10216 }
65f38f15 10217 else
5bd4f169
AM
10218 {
10219 /* It's not one of our sections, so don't allocate space. */
10220 continue;
10221 }
10222
eea6121a 10223 if (s->size == 0)
5bd4f169 10224 {
c456f082
AM
10225 /* If we don't need this section, strip it from the
10226 output file. This is mostly to handle .rela.bss and
10227 .rela.plt. We must create both sections in
10228 create_dynamic_sections, because they must be created
10229 before the linker maps input sections to output
10230 sections. The linker does that before
10231 adjust_dynamic_symbol is called, and it is that
10232 function which decides whether anything needs to go
10233 into these sections. */
8423293d 10234 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10235 continue;
10236 }
10237
06bcf541
AM
10238 if (bfd_is_abs_section (s->output_section))
10239 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10240 s->name);
10241
c456f082 10242 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10243 continue;
10244
65f38f15
AM
10245 /* Allocate memory for the section contents. We use bfd_zalloc
10246 here in case unused entries are not reclaimed before the
10247 section's contents are written out. This should not happen,
411e1bfb
AM
10248 but this way if it does we get a R_PPC64_NONE reloc in .rela
10249 sections instead of garbage.
10250 We also rely on the section contents being zero when writing
5474d94f 10251 the GOT and .dynrelro. */
eea6121a 10252 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10253 if (s->contents == NULL)
b34976b6 10254 return FALSE;
5bd4f169
AM
10255 }
10256
c72f2fb2 10257 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10258 {
0c8d6e5c 10259 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10260 continue;
10261
e717da7e 10262 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10263 if (s != NULL && s != htab->elf.sgot)
e717da7e 10264 {
eea6121a 10265 if (s->size == 0)
8423293d 10266 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10267 else
10268 {
eea6121a 10269 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10270 if (s->contents == NULL)
10271 return FALSE;
10272 }
10273 }
10274 s = ppc64_elf_tdata (ibfd)->relgot;
10275 if (s != NULL)
10276 {
eea6121a 10277 if (s->size == 0)
8423293d 10278 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10279 else
10280 {
eea6121a 10281 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10282 if (s->contents == NULL)
10283 return FALSE;
10284 relocs = TRUE;
10285 s->reloc_count = 0;
10286 }
10287 }
10288 }
10289
e86ce104 10290 if (htab->elf.dynamic_sections_created)
5bd4f169 10291 {
e8910a83
AM
10292 bfd_boolean tls_opt;
10293
5bd4f169
AM
10294 /* Add some entries to the .dynamic section. We fill in the
10295 values later, in ppc64_elf_finish_dynamic_sections, but we
10296 must add the entries now so that we get the correct size for
10297 the .dynamic section. The DT_DEBUG entry is filled in by the
10298 dynamic linker and used by the debugger. */
dc810e39 10299#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10300 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10301
0e1862bb 10302 if (bfd_link_executable (info))
5bd4f169 10303 {
dc810e39 10304 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10305 return FALSE;
5bd4f169
AM
10306 }
10307
33e44f2e 10308 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10309 {
dc810e39
AM
10310 if (!add_dynamic_entry (DT_PLTGOT, 0)
10311 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10312 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10313 || !add_dynamic_entry (DT_JMPREL, 0)
10314 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10315 return FALSE;
5bd4f169
AM
10316 }
10317
ee67d69a 10318 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10319 {
10320 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10321 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10322 return FALSE;
19397422
AM
10323 }
10324
7c9cf415 10325 tls_opt = (htab->params->tls_get_addr_opt
9e7028aa
AM
10326 && ((htab->tls_get_addr_fd != NULL
10327 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10328 || (htab->tga_desc_fd != NULL
10329 && htab->tga_desc_fd->elf.plt.plist != NULL)));
e8910a83
AM
10330 if (tls_opt || !htab->opd_abi)
10331 {
10332 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10333 return FALSE;
10334 }
a7f2871e 10335
5bd4f169
AM
10336 if (relocs)
10337 {
dc810e39
AM
10338 if (!add_dynamic_entry (DT_RELA, 0)
10339 || !add_dynamic_entry (DT_RELASZ, 0)
10340 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10341 return FALSE;
5bd4f169 10342
65f38f15
AM
10343 /* If any dynamic relocs apply to a read-only section,
10344 then we need a DT_TEXTREL entry. */
248866a8 10345 if ((info->flags & DF_TEXTREL) == 0)
d49e5065
L
10346 elf_link_hash_traverse (&htab->elf,
10347 _bfd_elf_maybe_set_textrel, info);
5bd4f169 10348
65f38f15 10349 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10350 {
65f38f15 10351 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10352 return FALSE;
5bd4f169 10353 }
5bd4f169 10354 }
5bd4f169 10355 }
65f38f15 10356#undef add_dynamic_entry
5bd4f169 10357
b34976b6 10358 return TRUE;
5bd4f169
AM
10359}
10360
a345bc8d
AM
10361/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10362
10363static bfd_boolean
10364ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10365{
10366 if (h->plt.plist != NULL
10367 && !h->def_regular
10368 && !h->pointer_equality_needed)
10369 return FALSE;
10370
10371 return _bfd_elf_hash_symbol (h);
10372}
10373
721956f4 10374/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10375
4ce794b7
AM
10376static inline enum ppc_stub_type
10377ppc_type_of_stub (asection *input_sec,
10378 const Elf_Internal_Rela *rel,
10379 struct ppc_link_hash_entry **hash,
e054468f 10380 struct plt_entry **plt_ent,
6911b7dc
AM
10381 bfd_vma destination,
10382 unsigned long local_off)
5bd4f169 10383{
721956f4
AM
10384 struct ppc_link_hash_entry *h = *hash;
10385 bfd_vma location;
10386 bfd_vma branch_offset;
10387 bfd_vma max_branch_offset;
4ce794b7 10388 enum elf_ppc64_reloc_type r_type;
5bd4f169 10389
721956f4
AM
10390 if (h != NULL)
10391 {
e054468f 10392 struct plt_entry *ent;
7fe2b9a6 10393 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10394 if (h->oh != NULL
10395 && h->oh->is_func_descriptor)
7b8f6675
AM
10396 {
10397 fdh = ppc_follow_link (h->oh);
10398 *hash = fdh;
10399 }
8387904d 10400
e054468f
AM
10401 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10402 if (ent->addend == rel->r_addend
10403 && ent->plt.offset != (bfd_vma) -1)
10404 {
e054468f
AM
10405 *plt_ent = ent;
10406 return ppc_stub_plt_call;
10407 }
5bd4f169 10408
7fe2b9a6
AM
10409 /* Here, we know we don't have a plt entry. If we don't have a
10410 either a defined function descriptor or a defined entry symbol
10411 in a regular object file, then it is pointless trying to make
10412 any other type of stub. */
854b41e7
AM
10413 if (!is_static_defined (&fdh->elf)
10414 && !is_static_defined (&h->elf))
721956f4 10415 return ppc_stub_none;
5d1634d7 10416 }
e054468f
AM
10417 else if (elf_local_got_ents (input_sec->owner) != NULL)
10418 {
10419 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10420 struct plt_entry **local_plt = (struct plt_entry **)
10421 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10422 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10423
10424 if (local_plt[r_symndx] != NULL)
10425 {
10426 struct plt_entry *ent;
10427
10428 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10429 if (ent->addend == rel->r_addend
10430 && ent->plt.offset != (bfd_vma) -1)
10431 {
10432 *plt_ent = ent;
10433 return ppc_stub_plt_call;
10434 }
10435 }
10436 }
5d1634d7 10437
721956f4
AM
10438 /* Determine where the call point is. */
10439 location = (input_sec->output_offset
10440 + input_sec->output_section->vma
10441 + rel->r_offset);
5d1634d7 10442
721956f4
AM
10443 branch_offset = destination - location;
10444 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10445
721956f4
AM
10446 /* Determine if a long branch stub is needed. */
10447 max_branch_offset = 1 << 25;
23cedd1d
AM
10448 if (r_type == R_PPC64_REL14
10449 || r_type == R_PPC64_REL14_BRTAKEN
10450 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10451 max_branch_offset = 1 << 15;
5d1634d7 10452
6911b7dc 10453 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10454 /* We need a stub. Figure out whether a long_branch or plt_branch
10455 is needed later. */
10456 return ppc_stub_long_branch;
5d1634d7 10457
721956f4 10458 return ppc_stub_none;
5d1634d7
AM
10459}
10460
f891966f
AM
10461/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10462 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10463 . mflr %r12
10464 . bcl 20,31,1f
10465 .1: mflr %r11
10466 . mtlr %r12
05d0e962 10467 . lis %r12,xxx-1b@highest
f891966f 10468 . ori %r12,%r12,xxx-1b@higher
05d0e962 10469 . sldi %r12,%r12,32
f891966f 10470 . oris %r12,%r12,xxx-1b@high
05d0e962 10471 . ori %r12,%r12,xxx-1b@l
f891966f 10472 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10473
10474static bfd_byte *
10475build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10476{
f891966f
AM
10477 bfd_put_32 (abfd, MFLR_R12, p);
10478 p += 4;
10479 bfd_put_32 (abfd, BCL_20_31, p);
10480 p += 4;
10481 bfd_put_32 (abfd, MFLR_R11, p);
10482 p += 4;
10483 bfd_put_32 (abfd, MTLR_R12, p);
10484 p += 4;
05d0e962
AM
10485 if (off + 0x8000 < 0x10000)
10486 {
10487 if (load)
10488 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10489 else
10490 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10491 p += 4;
10492 }
10493 else if (off + 0x80008000ULL < 0x100000000ULL)
10494 {
10495 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10496 p += 4;
10497 if (load)
10498 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10499 else
10500 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10501 p += 4;
10502 }
10503 else
10504 {
10505 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10506 {
10507 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10508 p += 4;
10509 }
10510 else
10511 {
10512 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10513 p += 4;
10514 if (((off >> 32) & 0xffff) != 0)
10515 {
10516 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10517 p += 4;
10518 }
10519 }
10520 if (((off >> 32) & 0xffffffffULL) != 0)
10521 {
10522 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10523 p += 4;
10524 }
10525 if (PPC_HI (off) != 0)
10526 {
10527 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10528 p += 4;
10529 }
10530 if (PPC_LO (off) != 0)
10531 {
10532 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10533 p += 4;
10534 }
10535 if (load)
10536 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10537 else
10538 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10539 p += 4;
10540 }
10541 return p;
10542}
10543
10544static unsigned int
10545size_offset (bfd_vma off)
10546{
10547 unsigned int size;
10548 if (off + 0x8000 < 0x10000)
10549 size = 4;
10550 else if (off + 0x80008000ULL < 0x100000000ULL)
10551 size = 8;
10552 else
10553 {
10554 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10555 size = 4;
10556 else
10557 {
10558 size = 4;
10559 if (((off >> 32) & 0xffff) != 0)
10560 size += 4;
10561 }
10562 if (((off >> 32) & 0xffffffffULL) != 0)
10563 size += 4;
10564 if (PPC_HI (off) != 0)
10565 size += 4;
10566 if (PPC_LO (off) != 0)
10567 size += 4;
10568 size += 4;
10569 }
f891966f 10570 return size + 16;
05d0e962
AM
10571}
10572
3d58e1fc
AM
10573static unsigned int
10574num_relocs_for_offset (bfd_vma off)
10575{
10576 unsigned int num_rel;
10577 if (off + 0x8000 < 0x10000)
10578 num_rel = 1;
10579 else if (off + 0x80008000ULL < 0x100000000ULL)
10580 num_rel = 2;
10581 else
10582 {
10583 num_rel = 1;
10584 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10585 && ((off >> 32) & 0xffff) != 0)
10586 num_rel += 1;
10587 if (PPC_HI (off) != 0)
10588 num_rel += 1;
10589 if (PPC_LO (off) != 0)
10590 num_rel += 1;
10591 }
10592 return num_rel;
10593}
10594
10595static Elf_Internal_Rela *
10596emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10597 bfd_vma roff, bfd_vma targ, bfd_vma off)
10598{
10599 bfd_vma relative_targ = targ - (roff - 8);
10600 if (bfd_big_endian (info->output_bfd))
10601 roff += 2;
10602 r->r_offset = roff;
10603 r->r_addend = relative_targ + roff;
10604 if (off + 0x8000 < 0x10000)
10605 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10606 else if (off + 0x80008000ULL < 0x100000000ULL)
10607 {
10608 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10609 ++r;
10610 roff += 4;
10611 r->r_offset = roff;
10612 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10613 r->r_addend = relative_targ + roff;
10614 }
10615 else
10616 {
10617 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10618 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10619 else
10620 {
10621 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10622 if (((off >> 32) & 0xffff) != 0)
10623 {
10624 ++r;
10625 roff += 4;
10626 r->r_offset = roff;
10627 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10628 r->r_addend = relative_targ + roff;
10629 }
10630 }
10631 if (((off >> 32) & 0xffffffffULL) != 0)
10632 roff += 4;
10633 if (PPC_HI (off) != 0)
10634 {
10635 ++r;
10636 roff += 4;
10637 r->r_offset = roff;
10638 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10639 r->r_addend = relative_targ + roff;
10640 }
10641 if (PPC_LO (off) != 0)
10642 {
10643 ++r;
10644 roff += 4;
10645 r->r_offset = roff;
10646 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10647 r->r_addend = relative_targ + roff;
10648 }
10649 }
10650 return r;
10651}
10652
04bdff6a 10653static bfd_byte *
7c1f4227 10654build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
04bdff6a
AM
10655 bfd_boolean load)
10656{
10657 uint64_t insn;
10658 if (off - odd + (1ULL << 33) < 1ULL << 34)
10659 {
10660 off -= odd;
10661 if (odd)
10662 {
10663 bfd_put_32 (abfd, NOP, p);
10664 p += 4;
10665 }
10666 if (load)
10667 insn = PLD_R12_PC;
10668 else
10669 insn = PADDI_R12_PC;
10670 insn |= D34 (off);
10671 bfd_put_32 (abfd, insn >> 32, p);
10672 p += 4;
10673 bfd_put_32 (abfd, insn, p);
10674 }
10675 /* The minimum value for paddi is -0x200000000. The minimum value
10676 for li is -0x8000, which when shifted by 34 and added gives a
10677 minimum value of -0x2000200000000. The maximum value is
10678 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10679 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10680 {
10681 off -= 8 - odd;
10682 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10683 p += 4;
10684 if (!odd)
10685 {
10686 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10687 p += 4;
10688 }
10689 insn = PADDI_R12_PC | D34 (off);
10690 bfd_put_32 (abfd, insn >> 32, p);
10691 p += 4;
10692 bfd_put_32 (abfd, insn, p);
10693 p += 4;
10694 if (odd)
10695 {
10696 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10697 p += 4;
10698 }
10699 if (load)
10700 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10701 else
10702 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10703 }
10704 else
10705 {
10706 off -= odd + 8;
10707 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10708 p += 4;
10709 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10710 p += 4;
10711 if (odd)
10712 {
10713 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10714 p += 4;
10715 }
10716 insn = PADDI_R12_PC | D34 (off);
10717 bfd_put_32 (abfd, insn >> 32, p);
10718 p += 4;
10719 bfd_put_32 (abfd, insn, p);
10720 p += 4;
10721 if (!odd)
10722 {
10723 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10724 p += 4;
10725 }
10726 if (load)
10727 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10728 else
10729 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10730 }
10731 p += 4;
10732 return p;
10733}
10734
10735static unsigned int
7c1f4227 10736size_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
10737{
10738 if (off - odd + (1ULL << 33) < 1ULL << 34)
10739 return odd + 8;
10740 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10741 return 20;
10742 else
10743 return 24;
10744}
10745
10746static unsigned int
7c1f4227 10747num_relocs_for_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
10748{
10749 if (off - odd + (1ULL << 33) < 1ULL << 34)
10750 return 1;
10751 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10752 return 2;
10753 else
10754 return 3;
10755}
10756
10757static Elf_Internal_Rela *
7c1f4227 10758emit_relocs_for_power10_offset (struct bfd_link_info *info,
04bdff6a
AM
10759 Elf_Internal_Rela *r, bfd_vma roff,
10760 bfd_vma targ, bfd_vma off, int odd)
10761{
10762 if (off - odd + (1ULL << 33) < 1ULL << 34)
10763 roff += odd;
10764 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10765 {
10766 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10767 r->r_offset = roff + d_offset;
10768 r->r_addend = targ + 8 - odd - d_offset;
10769 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10770 ++r;
10771 roff += 8 - odd;
10772 }
10773 else
10774 {
10775 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10776 r->r_offset = roff + d_offset;
10777 r->r_addend = targ + 8 + odd - d_offset;
10778 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10779 ++r;
10780 roff += 4;
10781 r->r_offset = roff + d_offset;
10782 r->r_addend = targ + 4 + odd - d_offset;
10783 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10784 ++r;
10785 roff += 4 + odd;
10786 }
10787 r->r_offset = roff;
10788 r->r_addend = targ;
10789 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10790 return r;
10791}
10792
df136d64
AM
10793/* Emit .eh_frame opcode to advance pc by DELTA. */
10794
10795static bfd_byte *
10796eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10797{
10798 delta /= 4;
10799 if (delta < 64)
10800 *eh++ = DW_CFA_advance_loc + delta;
10801 else if (delta < 256)
10802 {
10803 *eh++ = DW_CFA_advance_loc1;
10804 *eh++ = delta;
10805 }
10806 else if (delta < 65536)
10807 {
10808 *eh++ = DW_CFA_advance_loc2;
10809 bfd_put_16 (abfd, delta, eh);
10810 eh += 2;
10811 }
10812 else
10813 {
10814 *eh++ = DW_CFA_advance_loc4;
10815 bfd_put_32 (abfd, delta, eh);
10816 eh += 4;
10817 }
10818 return eh;
10819}
10820
10821/* Size of required .eh_frame opcode to advance pc by DELTA. */
10822
10823static unsigned int
10824eh_advance_size (unsigned int delta)
10825{
10826 if (delta < 64 * 4)
10827 /* DW_CFA_advance_loc+[1..63]. */
10828 return 1;
10829 if (delta < 256 * 4)
10830 /* DW_CFA_advance_loc1, byte. */
10831 return 2;
10832 if (delta < 65536 * 4)
10833 /* DW_CFA_advance_loc2, 2 bytes. */
10834 return 3;
10835 /* DW_CFA_advance_loc4, 4 bytes. */
10836 return 5;
10837}
10838
794e51c0
AM
10839/* With power7 weakly ordered memory model, it is possible for ld.so
10840 to update a plt entry in one thread and have another thread see a
10841 stale zero toc entry. To avoid this we need some sort of acquire
10842 barrier in the call stub. One solution is to make the load of the
10843 toc word seem to appear to depend on the load of the function entry
10844 word. Another solution is to test for r2 being zero, and branch to
10845 the appropriate glink entry if so.
10846
10847 . fake dep barrier compare
71a39c98
AM
10848 . ld 12,xxx(2) ld 12,xxx(2)
10849 . mtctr 12 mtctr 12
10850 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10851 . add 2,2,11 cmpldi 2,0
10852 . ld 2,xxx+8(2) bnectr+
10853 . bctr b <glink_entry>
10854
10855 The solution involving the compare turns out to be faster, so
10856 that's what we use unless the branch won't reach. */
10857
10858#define ALWAYS_USE_FAKE_DEP 0
10859#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10860
794e51c0
AM
10861static inline unsigned int
10862plt_stub_size (struct ppc_link_hash_table *htab,
10863 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
10864 bfd_vma off,
10865 unsigned int odd)
794e51c0 10866{
05d0e962 10867 unsigned size;
b9e5796b 10868
05d0e962
AM
10869 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10870 {
e10a07b3 10871 if (htab->params->power10_stubs != 0)
29433886 10872 size = 8 + size_power10_offset (off, odd);
04bdff6a
AM
10873 else
10874 size = 8 + size_offset (off - 8);
05d0e962
AM
10875 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10876 size += 4;
05d0e962 10877 }
29433886 10878 else
b9e5796b 10879 {
29433886
AM
10880 size = 12;
10881 if (ALWAYS_EMIT_R2SAVE
10882 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
b9e5796b 10883 size += 4;
29433886 10884 if (PPC_HA (off) != 0)
b9e5796b 10885 size += 4;
29433886
AM
10886 if (htab->opd_abi)
10887 {
10888 size += 4;
10889 if (htab->params->plt_static_chain)
10890 size += 4;
10891 if (htab->params->plt_thread_safe
10892 && htab->elf.dynamic_sections_created
10893 && stub_entry->h != NULL
10894 && stub_entry->h->elf.dynindx != -1)
10895 size += 8;
10896 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
10897 != PPC_HA (off))
10898 size += 4;
10899 }
b9e5796b 10900 }
794e51c0 10901 if (stub_entry->h != NULL
ed7007c1 10902 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 10903 && htab->params->tls_get_addr_opt)
f378ab09 10904 {
29433886 10905 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 10906 {
29433886
AM
10907 size += 30 * 4;
10908 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
10909 || stub_entry->stub_type == ppc_stub_plt_call_both)
10910 size += 4;
9e7028aa
AM
10911 }
10912 else
10913 {
29433886
AM
10914 size += 7 * 4;
10915 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
10916 || stub_entry->stub_type == ppc_stub_plt_call_both)
10917 size += 6 * 4;
9e7028aa 10918 }
f378ab09 10919 }
794e51c0
AM
10920 return size;
10921}
10922
2420fff6
AM
10923/* Depending on the sign of plt_stub_align:
10924 If positive, return the padding to align to a 2**plt_stub_align
10925 boundary.
10926 If negative, if this stub would cross fewer 2**plt_stub_align
10927 boundaries if we align, then return the padding needed to do so. */
10928
794e51c0
AM
10929static inline unsigned int
10930plt_stub_pad (struct ppc_link_hash_table *htab,
10931 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
10932 bfd_vma plt_off,
10933 unsigned int odd)
794e51c0 10934{
2420fff6 10935 int stub_align;
1aa42141 10936 unsigned stub_size;
6f20ed8a 10937 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10938
2420fff6
AM
10939 if (htab->params->plt_stub_align >= 0)
10940 {
10941 stub_align = 1 << htab->params->plt_stub_align;
10942 if ((stub_off & (stub_align - 1)) != 0)
10943 return stub_align - (stub_off & (stub_align - 1));
10944 return 0;
10945 }
10946
10947 stub_align = 1 << -htab->params->plt_stub_align;
29433886 10948 stub_size = plt_stub_size (htab, stub_entry, plt_off, odd);
794e51c0 10949 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10950 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10951 return stub_align - (stub_off & (stub_align - 1));
10952 return 0;
10953}
10954
10955/* Build a .plt call stub. */
10956
10957static inline bfd_byte *
10958build_plt_stub (struct ppc_link_hash_table *htab,
10959 struct ppc_stub_hash_entry *stub_entry,
10960 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10961{
e7d1c40c 10962 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10963 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10964 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10965 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10966 && htab->elf.dynamic_sections_created
10967 && stub_entry->h != NULL
10968 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10969 bfd_boolean use_fake_dep = plt_thread_safe;
10970 bfd_vma cmp_branch_off = 0;
10971
10972 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10973 && plt_load_toc
794e51c0 10974 && plt_thread_safe
9e51d549
AM
10975 && !(stub_entry->h != NULL
10976 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 10977 && htab->params->tls_get_addr_opt))
794e51c0
AM
10978 {
10979 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10980 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10981 / PLT_ENTRY_SIZE (htab));
9e390558 10982 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10983 bfd_vma to, from;
10984
68d62958
AM
10985 if (pltindex > 32768)
10986 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10987 to = (glinkoff
10988 + htab->glink->output_offset
10989 + htab->glink->output_section->vma);
6f20ed8a 10990 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10991 + 4 * (ALWAYS_EMIT_R2SAVE
10992 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10993 + 4 * (PPC_HA (offset) != 0)
10994 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10995 != PPC_HA (offset))
10996 + 4 * (plt_static_chain != 0)
10997 + 20
6f20ed8a
AM
10998 + stub_entry->group->stub_sec->output_offset
10999 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
11000 cmp_branch_off = to - from;
11001 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11002 }
11003
ac2df442
AM
11004 if (PPC_HA (offset) != 0)
11005 {
176a0d42
AM
11006 if (r != NULL)
11007 {
794e51c0
AM
11008 if (ALWAYS_EMIT_R2SAVE
11009 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11010 r[0].r_offset += 4;
176a0d42 11011 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 11012 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
11013 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11014 r[1].r_addend = r[0].r_addend;
b9e5796b 11015 if (plt_load_toc)
176a0d42 11016 {
b9e5796b 11017 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11018 {
b9e5796b
AM
11019 r[2].r_offset = r[1].r_offset + 4;
11020 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11021 r[2].r_addend = r[0].r_addend;
11022 }
11023 else
11024 {
11025 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11026 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11027 r[2].r_addend = r[0].r_addend + 8;
11028 if (plt_static_chain)
11029 {
11030 r[3].r_offset = r[2].r_offset + 4;
11031 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11032 r[3].r_addend = r[0].r_addend + 16;
11033 }
c7131b65 11034 }
176a0d42
AM
11035 }
11036 }
794e51c0
AM
11037 if (ALWAYS_EMIT_R2SAVE
11038 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 11039 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
11040 if (plt_load_toc)
11041 {
11042 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11043 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11044 }
11045 else
11046 {
11047 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11048 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11049 }
b9e5796b
AM
11050 if (plt_load_toc
11051 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 11052 {
71a39c98 11053 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
11054 offset = 0;
11055 }
71a39c98 11056 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11057 if (plt_load_toc)
794e51c0 11058 {
b9e5796b
AM
11059 if (use_fake_dep)
11060 {
11061 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11062 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11063 }
11064 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11065 if (plt_static_chain)
11066 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 11067 }
ac2df442
AM
11068 }
11069 else
11070 {
176a0d42
AM
11071 if (r != NULL)
11072 {
794e51c0
AM
11073 if (ALWAYS_EMIT_R2SAVE
11074 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11075 r[0].r_offset += 4;
176a0d42 11076 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 11077 if (plt_load_toc)
176a0d42 11078 {
b9e5796b 11079 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11080 {
b9e5796b
AM
11081 r[1].r_offset = r[0].r_offset + 4;
11082 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11083 r[1].r_addend = r[0].r_addend;
11084 }
11085 else
11086 {
11087 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11088 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11089 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11090 if (plt_static_chain)
11091 {
11092 r[2].r_offset = r[1].r_offset + 4;
11093 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11094 r[2].r_addend = r[0].r_addend + 8;
11095 }
c7131b65 11096 }
176a0d42
AM
11097 }
11098 }
794e51c0
AM
11099 if (ALWAYS_EMIT_R2SAVE
11100 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 11101 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 11102 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
11103 if (plt_load_toc
11104 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
11105 {
11106 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11107 offset = 0;
11108 }
71a39c98 11109 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11110 if (plt_load_toc)
794e51c0 11111 {
b9e5796b
AM
11112 if (use_fake_dep)
11113 {
11114 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11115 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11116 }
11117 if (plt_static_chain)
11118 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11119 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 11120 }
ac2df442 11121 }
b9e5796b 11122 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
11123 {
11124 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11125 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 11126 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
11127 }
11128 else
407aa07c 11129 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
11130 return p;
11131}
11132
a7f2871e
AM
11133/* Build a special .plt call stub for __tls_get_addr. */
11134
b9ca1af6 11135#define LD_R0_0R3 0xe8030000
a7f2871e
AM
11136#define LD_R12_0R3 0xe9830000
11137#define MR_R0_R3 0x7c601b78
b9ca1af6 11138#define CMPDI_R0_0 0x2c200000
a7f2871e
AM
11139#define ADD_R3_R12_R13 0x7c6c6a14
11140#define BEQLR 0x4d820020
11141#define MR_R3_R0 0x7c030378
a7f2871e 11142#define BCTRL 0x4e800421
a7f2871e 11143
29433886
AM
11144static bfd_byte *
11145build_tls_get_addr_head (struct ppc_link_hash_table *htab,
794e51c0 11146 struct ppc_stub_hash_entry *stub_entry,
29433886 11147 bfd_byte *p)
a7f2871e 11148{
e7d1c40c 11149 bfd *obfd = htab->params->stub_bfd;
794e51c0 11150
b9ca1af6 11151 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
a7f2871e 11152 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
b9ca1af6 11153 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
a7f2871e 11154 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
a7f2871e
AM
11155 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11156 bfd_put_32 (obfd, BEQLR, p), p += 4;
11157 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9e7028aa 11158
29433886
AM
11159 if (!htab->params->no_tls_get_addr_regsave)
11160 p = tls_get_addr_prologue (obfd, p, htab);
11161 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11162 || stub_entry->stub_type == ppc_stub_plt_call_both)
11163 {
9e7028aa
AM
11164 bfd_put_32 (obfd, MFLR_R0, p);
11165 p += 4;
11166 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11167 p += 4;
29433886
AM
11168 }
11169 return p;
11170}
9e7028aa 11171
29433886
AM
11172static bfd_byte *
11173build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11174 struct ppc_stub_hash_entry *stub_entry,
11175 bfd_byte *p,
11176 bfd_byte *loc)
11177{
11178 bfd *obfd = htab->params->stub_bfd;
11179
11180 if (!htab->params->no_tls_get_addr_regsave)
11181 {
11182 bfd_put_32 (obfd, BCTRL, p - 4);
11183
11184 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11185 || stub_entry->stub_type == ppc_stub_plt_call_both)
11186 {
11187 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11188 p += 4;
11189 }
11190 p = tls_get_addr_epilogue (obfd, p, htab);
11191 }
11192 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11193 || stub_entry->stub_type == ppc_stub_plt_call_both)
11194 {
9e7028aa
AM
11195 bfd_put_32 (obfd, BCTRL, p - 4);
11196
11197 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11198 p += 4;
11199 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11200 p += 4;
11201 bfd_put_32 (obfd, MTLR_R0, p);
11202 p += 4;
11203 bfd_put_32 (obfd, BLR, p);
11204 p += 4;
11205 }
a7f2871e 11206
df136d64
AM
11207 if (htab->glink_eh_frame != NULL
11208 && htab->glink_eh_frame->size != 0)
11209 {
11210 bfd_byte *base, *eh;
df136d64
AM
11211
11212 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11213 eh = base + stub_entry->group->eh_size;
29433886
AM
11214
11215 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 11216 {
29433886
AM
11217 unsigned int cfa_updt, delta, i;
11218
9e7028aa
AM
11219 /* After the bctrl, lr has been modified so we need to emit
11220 .eh_frame info saying the return address is on the stack. In
11221 fact we must put the EH info at or before the call rather
11222 than after it, because the EH info for a call needs to be
11223 specified by that point.
11224 See libgcc/unwind-dw2.c execute_cfa_program.
11225 Any stack pointer update must be described immediately after
11226 the instruction making the change, and since the stdu occurs
11227 after saving regs we put all the reg saves and the cfa
11228 change there. */
11229 cfa_updt = stub_entry->stub_offset + 18 * 4;
11230 delta = cfa_updt - stub_entry->group->lr_restore;
11231 stub_entry->group->lr_restore
11232 = stub_entry->stub_offset + (p - loc) - 4;
11233 eh = eh_advance (htab->elf.dynobj, eh, delta);
11234 *eh++ = DW_CFA_def_cfa_offset;
11235 if (htab->opd_abi)
11236 {
11237 *eh++ = 128;
11238 *eh++ = 1;
11239 }
11240 else
11241 *eh++ = 96;
11242 *eh++ = DW_CFA_offset_extended_sf;
11243 *eh++ = 65;
11244 *eh++ = (-16 / 8) & 0x7f;
11245 for (i = 4; i < 12; i++)
11246 {
11247 *eh++ = DW_CFA_offset + i;
11248 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11249 }
11250 *eh++ = (DW_CFA_advance_loc
11251 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11252 *eh++ = DW_CFA_def_cfa_offset;
11253 *eh++ = 0;
11254 for (i = 4; i < 12; i++)
11255 *eh++ = DW_CFA_restore + i;
11256 *eh++ = DW_CFA_advance_loc + 2;
29433886
AM
11257 *eh++ = DW_CFA_restore_extended;
11258 *eh++ = 65;
11259 stub_entry->group->eh_size = eh - base;
11260 }
11261 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11262 || stub_entry->stub_type == ppc_stub_plt_call_both)
11263 {
11264 unsigned int lr_used, delta;
11265
11266 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11267 delta = lr_used - stub_entry->group->lr_restore;
11268 stub_entry->group->lr_restore = lr_used + 16;
11269 eh = eh_advance (htab->elf.dynobj, eh, delta);
11270 *eh++ = DW_CFA_offset_extended_sf;
11271 *eh++ = 65;
11272 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11273 *eh++ = DW_CFA_advance_loc + 4;
11274 *eh++ = DW_CFA_restore_extended;
11275 *eh++ = 65;
11276 stub_entry->group->eh_size = eh - base;
9e7028aa 11277 }
df136d64 11278 }
a7f2871e
AM
11279 return p;
11280}
11281
176a0d42
AM
11282static Elf_Internal_Rela *
11283get_relocs (asection *sec, int count)
11284{
11285 Elf_Internal_Rela *relocs;
11286 struct bfd_elf_section_data *elfsec_data;
11287
11288 elfsec_data = elf_section_data (sec);
11289 relocs = elfsec_data->relocs;
11290 if (relocs == NULL)
11291 {
11292 bfd_size_type relsize;
11293 relsize = sec->reloc_count * sizeof (*relocs);
11294 relocs = bfd_alloc (sec->owner, relsize);
11295 if (relocs == NULL)
11296 return NULL;
11297 elfsec_data->relocs = relocs;
d4730f92
BS
11298 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11299 sizeof (Elf_Internal_Shdr));
11300 if (elfsec_data->rela.hdr == NULL)
11301 return NULL;
11302 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11303 * sizeof (Elf64_External_Rela));
11304 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
11305 sec->reloc_count = 0;
11306 }
11307 relocs += sec->reloc_count;
11308 sec->reloc_count += count;
11309 return relocs;
11310}
11311
3d58e1fc
AM
11312/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11313 forms, to the equivalent relocs against the global symbol given by
11314 STUB_ENTRY->H. */
11315
11316static bfd_boolean
11317use_global_in_relocs (struct ppc_link_hash_table *htab,
11318 struct ppc_stub_hash_entry *stub_entry,
11319 Elf_Internal_Rela *r, unsigned int num_rel)
11320{
11321 struct elf_link_hash_entry **hashes;
11322 unsigned long symndx;
11323 struct ppc_link_hash_entry *h;
11324 bfd_vma symval;
11325
11326 /* Relocs are always against symbols in their own object file. Fake
11327 up global sym hashes for the stub bfd (which has no symbols). */
11328 hashes = elf_sym_hashes (htab->params->stub_bfd);
11329 if (hashes == NULL)
11330 {
11331 bfd_size_type hsize;
11332
11333 /* When called the first time, stub_globals will contain the
11334 total number of symbols seen during stub sizing. After
11335 allocating, stub_globals is used as an index to fill the
11336 hashes array. */
11337 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11338 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11339 if (hashes == NULL)
11340 return FALSE;
11341 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11342 htab->stub_globals = 1;
11343 }
11344 symndx = htab->stub_globals++;
11345 h = stub_entry->h;
11346 hashes[symndx] = &h->elf;
11347 if (h->oh != NULL && h->oh->is_func)
11348 h = ppc_follow_link (h->oh);
11349 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11350 || h->elf.root.type == bfd_link_hash_defweak);
ed7007c1 11351 symval = defined_sym_val (&h->elf);
3d58e1fc
AM
11352 while (num_rel-- != 0)
11353 {
11354 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11355 if (h->elf.root.u.def.section != stub_entry->target_section)
11356 {
11357 /* H is an opd symbol. The addend must be zero, and the
11358 branch reloc is the only one we can convert. */
11359 r->r_addend = 0;
11360 break;
11361 }
11362 else
11363 r->r_addend -= symval;
11364 --r;
11365 }
11366 return TRUE;
11367}
11368
aa374f67 11369static bfd_vma
25f53a85 11370get_r2off (struct bfd_link_info *info,
aa374f67
AM
11371 struct ppc_stub_hash_entry *stub_entry)
11372{
25f53a85 11373 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 11374 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
11375
11376 if (r2off == 0)
11377 {
11378 /* Support linking -R objects. Get the toc pointer from the
11379 opd entry. */
11380 char buf[8];
b9e5796b
AM
11381 if (!htab->opd_abi)
11382 return r2off;
aa374f67
AM
11383 asection *opd = stub_entry->h->elf.root.u.def.section;
11384 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11385
11386 if (strcmp (opd->name, ".opd") != 0
11387 || opd->reloc_count != 0)
11388 {
2cdcc330
AM
11389 info->callbacks->einfo
11390 (_("%P: cannot find opd entry toc for `%pT'\n"),
11391 stub_entry->h->elf.root.root.string);
aa374f67 11392 bfd_set_error (bfd_error_bad_value);
a7c49797 11393 return (bfd_vma) -1;
aa374f67
AM
11394 }
11395 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11396 return (bfd_vma) -1;
aa374f67 11397 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11398 r2off -= elf_gp (info->output_bfd);
aa374f67 11399 }
6f20ed8a 11400 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11401 return r2off;
11402}
11403
b34976b6 11404static bfd_boolean
4ce794b7 11405ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11406{
721956f4
AM
11407 struct ppc_stub_hash_entry *stub_entry;
11408 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11409 struct bfd_link_info *info;
11410 struct ppc_link_hash_table *htab;
29433886 11411 bfd *obfd;
721956f4 11412 bfd_byte *loc;
3d58e1fc 11413 bfd_byte *p, *relp;
1aa42141 11414 bfd_vma targ, off;
176a0d42 11415 Elf_Internal_Rela *r;
e054468f 11416 asection *plt;
3d58e1fc 11417 int num_rel;
04bdff6a 11418 int odd;
29433886 11419 bfd_boolean is_tga;
5d1634d7 11420
721956f4
AM
11421 /* Massage our args to the form they really have. */
11422 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11423 info = in_arg;
5d1634d7 11424
abf874aa
CL
11425 /* Fail if the target section could not be assigned to an output
11426 section. The user should fix his linker script. */
11427 if (stub_entry->target_section != NULL
11428 && stub_entry->target_section->output_section == NULL
11429 && info->non_contiguous_regions)
53215f21
CL
11430 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
11431 "Retry without --enable-non-contiguous-regions.\n"),
11432 stub_entry->target_section);
abf874aa
CL
11433
11434 /* Same for the group. */
11435 if (stub_entry->group->stub_sec != NULL
11436 && stub_entry->group->stub_sec->output_section == NULL
11437 && info->non_contiguous_regions)
53215f21
CL
11438 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11439 "output section. Retry without "
11440 "--enable-non-contiguous-regions.\n"),
11441 stub_entry->group->stub_sec,
11442 stub_entry->target_section);
abf874aa 11443
5d1634d7 11444 htab = ppc_hash_table (info);
4dfe6ac6
NC
11445 if (htab == NULL)
11446 return FALSE;
5d1634d7 11447
1aa42141 11448 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 11449 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11450
4ce794b7 11451 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 11452 switch (stub_entry->stub_type)
5d1634d7 11453 {
721956f4 11454 case ppc_stub_long_branch:
ad8e1ba5 11455 case ppc_stub_long_branch_r2off:
721956f4 11456 /* Branches are relative. This is where we are going to. */
1aa42141 11457 targ = (stub_entry->target_value
6911b7dc
AM
11458 + stub_entry->target_section->output_offset
11459 + stub_entry->target_section->output_section->vma);
1aa42141 11460 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11461
721956f4 11462 /* And this is where we are coming from. */
1aa42141
AM
11463 off = (stub_entry->stub_offset
11464 + stub_entry->group->stub_sec->output_offset
11465 + stub_entry->group->stub_sec->output_section->vma);
11466 off = targ - off;
e86ce104 11467
9e390558 11468 p = loc;
29433886 11469 obfd = htab->params->stub_bfd;
ac2df442 11470 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 11471 {
25f53a85 11472 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11473
a7c49797 11474 if (r2off == (bfd_vma) -1)
aa374f67
AM
11475 {
11476 htab->stub_error = TRUE;
11477 return FALSE;
11478 }
29433886 11479 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11480 p += 4;
ac2df442
AM
11481 if (PPC_HA (r2off) != 0)
11482 {
29433886 11483 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
9e390558 11484 p += 4;
a7c49797
AM
11485 }
11486 if (PPC_LO (r2off) != 0)
11487 {
29433886 11488 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
9e390558 11489 p += 4;
ac2df442 11490 }
9e390558 11491 off -= p - loc;
ad8e1ba5 11492 }
29433886 11493 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
9e390558 11494 p += 4;
ad8e1ba5 11495
5c3dead3
AM
11496 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11497 {
cf97bcb0
AM
11498 _bfd_error_handler
11499 (_("long branch stub `%s' offset overflow"),
bc30df16 11500 stub_entry->root.string);
5c3dead3
AM
11501 htab->stub_error = TRUE;
11502 return FALSE;
11503 }
ee75fd95
AM
11504
11505 if (info->emitrelocations)
11506 {
6f20ed8a 11507 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
11508 if (r == NULL)
11509 return FALSE;
9e390558 11510 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11511 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11512 r->r_addend = targ;
3d58e1fc
AM
11513 if (stub_entry->h != NULL
11514 && !use_global_in_relocs (htab, stub_entry, r, 1))
11515 return FALSE;
ee75fd95 11516 }
721956f4 11517 break;
e86ce104 11518
721956f4 11519 case ppc_stub_plt_branch:
ad8e1ba5 11520 case ppc_stub_plt_branch_r2off:
721956f4
AM
11521 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11522 stub_entry->root.string + 9,
b34976b6 11523 FALSE, FALSE);
721956f4
AM
11524 if (br_entry == NULL)
11525 {
cf97bcb0
AM
11526 _bfd_error_handler (_("can't find branch stub `%s'"),
11527 stub_entry->root.string);
b34976b6
AM
11528 htab->stub_error = TRUE;
11529 return FALSE;
721956f4
AM
11530 }
11531
1aa42141 11532 targ = (stub_entry->target_value
176a0d42
AM
11533 + stub_entry->target_section->output_offset
11534 + stub_entry->target_section->output_section->vma);
6911b7dc 11535 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 11536 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11537
1aa42141 11538 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11539 htab->brlt->contents + br_entry->offset);
721956f4 11540
f94498ff 11541 if (br_entry->iter == htab->stub_iteration)
721956f4 11542 {
f94498ff 11543 br_entry->iter = 0;
84f5d08e 11544
f94498ff 11545 if (htab->relbrlt != NULL)
84f5d08e 11546 {
f94498ff
AM
11547 /* Create a reloc for the branch lookup table entry. */
11548 Elf_Internal_Rela rela;
11549 bfd_byte *rl;
11550
11551 rela.r_offset = (br_entry->offset
11552 + htab->brlt->output_offset
11553 + htab->brlt->output_section->vma);
11554 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11555 rela.r_addend = targ;
f94498ff
AM
11556
11557 rl = htab->relbrlt->contents;
11558 rl += (htab->relbrlt->reloc_count++
11559 * sizeof (Elf64_External_Rela));
11560 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11561 }
11562 else if (info->emitrelocations)
11563 {
176a0d42
AM
11564 r = get_relocs (htab->brlt, 1);
11565 if (r == NULL)
11566 return FALSE;
11567 /* brlt, being SEC_LINKER_CREATED does not go through the
11568 normal reloc processing. Symbols and offsets are not
11569 translated from input file to output file form, so
11570 set up the offset per the output file. */
f94498ff
AM
11571 r->r_offset = (br_entry->offset
11572 + htab->brlt->output_offset
11573 + htab->brlt->output_section->vma);
11574 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11575 r->r_addend = targ;
84f5d08e 11576 }
84f5d08e 11577 }
721956f4 11578
1aa42141 11579 targ = (br_entry->offset
176a0d42
AM
11580 + htab->brlt->output_offset
11581 + htab->brlt->output_section->vma);
11582
1aa42141
AM
11583 off = (elf_gp (info->output_bfd)
11584 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11585 off = targ - off;
721956f4 11586
ad8e1ba5 11587 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11588 {
25f53a85 11589 info->callbacks->einfo
c1c8c1ef 11590 (_("%P: linkage table error against `%pT'\n"),
721956f4 11591 stub_entry->root.string);
5d1634d7 11592 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11593 htab->stub_error = TRUE;
11594 return FALSE;
5d1634d7 11595 }
41bd81ab 11596
176a0d42
AM
11597 if (info->emitrelocations)
11598 {
6f20ed8a 11599 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11600 if (r == NULL)
11601 return FALSE;
6f20ed8a 11602 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11603 if (bfd_big_endian (info->output_bfd))
11604 r[0].r_offset += 2;
00f412ee 11605 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11606 r[0].r_offset += 4;
11607 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11608 r[0].r_addend = targ;
176a0d42
AM
11609 if (PPC_HA (off) != 0)
11610 {
11611 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11612 r[1].r_offset = r[0].r_offset + 4;
11613 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11614 r[1].r_addend = r[0].r_addend;
11615 }
11616 }
11617
9e390558 11618 p = loc;
29433886 11619 obfd = htab->params->stub_bfd;
00f412ee 11620 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11621 {
176a0d42 11622 if (PPC_HA (off) != 0)
ac2df442 11623 {
29433886 11624 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11625 p += 4;
29433886 11626 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11627 }
11628 else
29433886 11629 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11630 }
11631 else
11632 {
25f53a85 11633 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11634
a7c49797 11635 if (r2off == (bfd_vma) -1)
aa374f67
AM
11636 {
11637 htab->stub_error = TRUE;
11638 return FALSE;
11639 }
ad8e1ba5 11640
29433886 11641 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11642 p += 4;
176a0d42 11643 if (PPC_HA (off) != 0)
ac2df442 11644 {
29433886 11645 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11646 p += 4;
29433886 11647 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11648 }
11649 else
29433886 11650 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11651
11652 if (PPC_HA (r2off) != 0)
11653 {
9e390558 11654 p += 4;
29433886 11655 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11656 }
11657 if (PPC_LO (r2off) != 0)
11658 {
9e390558 11659 p += 4;
29433886 11660 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11661 }
ad8e1ba5 11662 }
9e390558 11663 p += 4;
29433886 11664 bfd_put_32 (obfd, MTCTR_R12, p);
9e390558 11665 p += 4;
29433886 11666 bfd_put_32 (obfd, BCTR, p);
407aa07c 11667 p += 4;
721956f4 11668 break;
5d1634d7 11669
05d0e962
AM
11670 case ppc_stub_long_branch_notoc:
11671 case ppc_stub_long_branch_both:
11672 case ppc_stub_plt_branch_notoc:
11673 case ppc_stub_plt_branch_both:
11674 case ppc_stub_plt_call_notoc:
11675 case ppc_stub_plt_call_both:
11676 p = loc;
f891966f 11677 off = (stub_entry->stub_offset
05d0e962
AM
11678 + stub_entry->group->stub_sec->output_offset
11679 + stub_entry->group->stub_sec->output_section->vma);
29433886
AM
11680 obfd = htab->params->stub_bfd;
11681 is_tga = ((stub_entry->stub_type == ppc_stub_plt_call_notoc
11682 || stub_entry->stub_type == ppc_stub_plt_call_both)
12cf8b93 11683 && stub_entry->h != NULL
29433886
AM
11684 && is_tls_get_addr (&stub_entry->h->elf, htab)
11685 && htab->params->tls_get_addr_opt);
11686 if (is_tga)
11687 {
11688 p = build_tls_get_addr_head (htab, stub_entry, p);
11689 off += p - loc;
11690 }
05d0e962
AM
11691 if (stub_entry->stub_type == ppc_stub_long_branch_both
11692 || stub_entry->stub_type == ppc_stub_plt_branch_both
11693 || stub_entry->stub_type == ppc_stub_plt_call_both)
11694 {
11695 off += 4;
29433886 11696 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
05d0e962
AM
11697 p += 4;
11698 }
11699 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11700 {
11701 targ = stub_entry->plt_ent->plt.offset & ~1;
11702 if (targ >= (bfd_vma) -2)
11703 abort ();
11704
11705 plt = htab->elf.splt;
11706 if (!htab->elf.dynamic_sections_created
11707 || stub_entry->h == NULL
11708 || stub_entry->h->elf.dynindx == -1)
11709 {
11710 if (stub_entry->symtype == STT_GNU_IFUNC)
11711 plt = htab->elf.iplt;
11712 else
11713 plt = htab->pltlocal;
11714 }
11715 targ += plt->output_offset + plt->output_section->vma;
11716 }
11717 else
11718 targ = (stub_entry->target_value
11719 + stub_entry->target_section->output_offset
11720 + stub_entry->target_section->output_section->vma);
04bdff6a 11721 odd = off & 4;
05d0e962 11722 off = targ - off;
f891966f 11723
3d58e1fc
AM
11724 relp = p;
11725 num_rel = 0;
e10a07b3 11726 if (htab->params->power10_stubs != 0)
04bdff6a
AM
11727 {
11728 bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
29433886 11729 p = build_power10_offset (obfd, p, off, odd, load);
04bdff6a
AM
11730 }
11731 else
11732 {
29433886
AM
11733 if (htab->glink_eh_frame != NULL
11734 && htab->glink_eh_frame->size != 0)
11735 {
11736 bfd_byte *base, *eh;
11737 unsigned int lr_used, delta;
11738
11739 base = (htab->glink_eh_frame->contents
11740 + stub_entry->group->eh_base + 17);
11741 eh = base + stub_entry->group->eh_size;
11742 lr_used = stub_entry->stub_offset + (p - loc) + 8;
11743 delta = lr_used - stub_entry->group->lr_restore;
11744 stub_entry->group->lr_restore = lr_used + 8;
11745 eh = eh_advance (htab->elf.dynobj, eh, delta);
11746 *eh++ = DW_CFA_register;
11747 *eh++ = 65;
11748 *eh++ = 12;
11749 *eh++ = DW_CFA_advance_loc + 2;
11750 *eh++ = DW_CFA_restore_extended;
11751 *eh++ = 65;
11752 stub_entry->group->eh_size = eh - base;
11753 }
11754
04bdff6a
AM
11755 /* The notoc stubs calculate their target (either a PLT entry or
11756 the global entry point of a function) relative to the PC
11757 returned by the "bcl" two instructions past the start of the
11758 sequence emitted by build_offset. The offset is therefore 8
11759 less than calculated from the start of the sequence. */
11760 off -= 8;
29433886 11761 p = build_offset (obfd, p, off,
04bdff6a
AM
11762 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11763 }
11764
f891966f 11765 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
05d0e962 11766 {
f891966f 11767 bfd_vma from;
3d58e1fc 11768 num_rel = 1;
f891966f
AM
11769 from = (stub_entry->stub_offset
11770 + stub_entry->group->stub_sec->output_offset
11771 + stub_entry->group->stub_sec->output_section->vma
11772 + (p - loc));
29433886 11773 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
11774 }
11775 else
11776 {
29433886 11777 bfd_put_32 (obfd, MTCTR_R12, p);
05d0e962 11778 p += 4;
29433886 11779 bfd_put_32 (obfd, BCTR, p);
05d0e962
AM
11780 }
11781 p += 4;
df136d64 11782
29433886
AM
11783 if (is_tga)
11784 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
11785
3d58e1fc
AM
11786 if (info->emitrelocations)
11787 {
04bdff6a 11788 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
e10a07b3 11789 if (htab->params->power10_stubs != 0)
7c1f4227 11790 num_rel += num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
11791 else
11792 {
11793 num_rel += num_relocs_for_offset (off);
11794 roff += 16;
11795 }
3d58e1fc
AM
11796 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11797 if (r == NULL)
11798 return FALSE;
e10a07b3 11799 if (htab->params->power10_stubs != 0)
7c1f4227 11800 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
04bdff6a
AM
11801 else
11802 r = emit_relocs_for_offset (info, r, roff, targ, off);
3d58e1fc
AM
11803 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11804 || stub_entry->stub_type == ppc_stub_long_branch_both)
11805 {
11806 ++r;
11807 roff = p - 4 - stub_entry->group->stub_sec->contents;
11808 r->r_offset = roff;
11809 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11810 r->r_addend = targ;
11811 if (stub_entry->h != NULL
11812 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11813 return FALSE;
11814 }
11815 }
05d0e962
AM
11816 break;
11817
721956f4 11818 case ppc_stub_plt_call:
794e51c0 11819 case ppc_stub_plt_call_r2save:
e054468f 11820 if (stub_entry->h != NULL
b31867b6
AM
11821 && stub_entry->h->is_func_descriptor
11822 && stub_entry->h->oh != NULL)
c862ae31 11823 {
b31867b6
AM
11824 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11825
11826 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11827 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11828 if (fh->elf.root.type == bfd_link_hash_undefined
11829 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11830 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11831 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11832 }
11833
721956f4 11834 /* Now build the stub. */
1aa42141
AM
11835 targ = stub_entry->plt_ent->plt.offset & ~1;
11836 if (targ >= (bfd_vma) -2)
721956f4
AM
11837 abort ();
11838
33e44f2e 11839 plt = htab->elf.splt;
25f23106
AM
11840 if (!htab->elf.dynamic_sections_created
11841 || stub_entry->h == NULL
11842 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11843 {
11844 if (stub_entry->symtype == STT_GNU_IFUNC)
11845 plt = htab->elf.iplt;
11846 else
11847 plt = htab->pltlocal;
11848 }
1aa42141 11849 targ += plt->output_offset + plt->output_section->vma;
e054468f 11850
1aa42141
AM
11851 off = (elf_gp (info->output_bfd)
11852 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11853 off = targ - off;
721956f4 11854
ad8e1ba5 11855 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11856 {
25f53a85 11857 info->callbacks->einfo
695344c0 11858 /* xgettext:c-format */
c1c8c1ef 11859 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11860 stub_entry->h != NULL
11861 ? stub_entry->h->elf.root.root.string
11862 : "<local sym>");
721956f4 11863 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11864 htab->stub_error = TRUE;
11865 return FALSE;
721956f4
AM
11866 }
11867
176a0d42
AM
11868 r = NULL;
11869 if (info->emitrelocations)
11870 {
6f20ed8a 11871 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11872 ((PPC_HA (off) != 0)
11873 + (htab->opd_abi
e7d1c40c 11874 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11875 && PPC_HA (off + 16) == PPC_HA (off))
11876 : 1)));
176a0d42
AM
11877 if (r == NULL)
11878 return FALSE;
6f20ed8a 11879 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11880 if (bfd_big_endian (info->output_bfd))
11881 r[0].r_offset += 2;
1aa42141 11882 r[0].r_addend = targ;
176a0d42 11883 }
29433886
AM
11884 p = loc;
11885 obfd = htab->params->stub_bfd;
11886 is_tga = (stub_entry->h != NULL
11887 && is_tls_get_addr (&stub_entry->h->elf, htab)
11888 && htab->params->tls_get_addr_opt);
11889 if (is_tga)
11890 {
11891 p = build_tls_get_addr_head (htab, stub_entry, p);
11892 if (r != NULL)
11893 r[0].r_offset += p - loc;
11894 }
11895 p = build_plt_stub (htab, stub_entry, p, off, r);
11896 if (is_tga)
11897 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
721956f4
AM
11898 break;
11899
a4b6fadd
AM
11900 case ppc_stub_save_res:
11901 return TRUE;
11902
721956f4
AM
11903 default:
11904 BFD_FAIL ();
b34976b6 11905 return FALSE;
721956f4
AM
11906 }
11907
1aa42141 11908 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 11909
e7d1c40c 11910 if (htab->params->emit_stub_syms)
97b639ba
AM
11911 {
11912 struct elf_link_hash_entry *h;
ee75fd95
AM
11913 size_t len1, len2;
11914 char *name;
11915 const char *const stub_str[] = { "long_branch",
05d0e962
AM
11916 "long_branch",
11917 "long_branch",
3f6ff479
AM
11918 "long_branch",
11919 "plt_branch",
ee75fd95 11920 "plt_branch",
05d0e962
AM
11921 "plt_branch",
11922 "plt_branch",
11923 "plt_call",
11924 "plt_call",
794e51c0 11925 "plt_call",
ee75fd95
AM
11926 "plt_call" };
11927
11928 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11929 len2 = strlen (stub_entry->root.string);
11930 name = bfd_malloc (len1 + len2 + 2);
11931 if (name == NULL)
11932 return FALSE;
11933 memcpy (name, stub_entry->root.string, 9);
11934 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11935 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11936 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11937 if (h == NULL)
11938 return FALSE;
11939 if (h->root.type == bfd_link_hash_new)
11940 {
11941 h->root.type = bfd_link_hash_defined;
6f20ed8a 11942 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11943 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11944 h->ref_regular = 1;
11945 h->def_regular = 1;
11946 h->ref_regular_nonweak = 1;
11947 h->forced_local = 1;
11948 h->non_elf = 0;
2ec55de3 11949 h->root.linker_def = 1;
97b639ba
AM
11950 }
11951 }
11952
b34976b6 11953 return TRUE;
721956f4
AM
11954}
11955
11956/* As above, but don't actually build the stub. Just bump offset so
11957 we know stub section sizes, and select plt_branch stubs where
11958 long_branch stubs won't do. */
11959
b34976b6 11960static bfd_boolean
4ce794b7 11961ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11962{
11963 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11964 struct bfd_link_info *info;
721956f4 11965 struct ppc_link_hash_table *htab;
f891966f
AM
11966 asection *plt;
11967 bfd_vma targ, off, r2off;
04bdff6a 11968 unsigned int size, extra, lr_used, delta, odd;
721956f4
AM
11969
11970 /* Massage our args to the form they really have. */
11971 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11972 info = in_arg;
11973
11974 htab = ppc_hash_table (info);
4dfe6ac6
NC
11975 if (htab == NULL)
11976 return FALSE;
721956f4 11977
abf874aa
CL
11978 /* Fail if the target section could not be assigned to an output
11979 section. The user should fix his linker script. */
11980 if (stub_entry->target_section != NULL
11981 && stub_entry->target_section->output_section == NULL
11982 && info->non_contiguous_regions)
53215f21
CL
11983 info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
11984 "Retry without --enable-non-contiguous-regions.\n"),
11985 stub_entry->target_section);
abf874aa
CL
11986
11987 /* Same for the group. */
11988 if (stub_entry->group->stub_sec != NULL
11989 && stub_entry->group->stub_sec->output_section == NULL
11990 && info->non_contiguous_regions)
53215f21
CL
11991 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11992 "output section. Retry without "
11993 "--enable-non-contiguous-regions.\n"),
11994 stub_entry->group->stub_sec,
11995 stub_entry->target_section);
abf874aa 11996
1aa42141
AM
11997 /* Make a note of the offset within the stubs for this entry. */
11998 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11999
a4b6fadd
AM
12000 if (stub_entry->h != NULL
12001 && stub_entry->h->save_res
12002 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12003 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12004 {
12005 /* Don't make stubs to out-of-line register save/restore
12006 functions. Instead, emit copies of the functions. */
12007 stub_entry->group->needs_save_res = 1;
12008 stub_entry->stub_type = ppc_stub_save_res;
12009 return TRUE;
12010 }
12011
f891966f 12012 switch (stub_entry->stub_type)
721956f4 12013 {
f891966f
AM
12014 case ppc_stub_plt_branch:
12015 case ppc_stub_plt_branch_r2off:
12016 /* Reset the stub type from the plt branch variant in case we now
12017 can reach with a shorter stub. */
12018 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12019 /* Fall through. */
12020 case ppc_stub_long_branch:
12021 case ppc_stub_long_branch_r2off:
1aa42141
AM
12022 targ = (stub_entry->target_value
12023 + stub_entry->target_section->output_offset
12024 + stub_entry->target_section->output_section->vma);
f891966f 12025 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
1aa42141
AM
12026 off = (stub_entry->stub_offset
12027 + stub_entry->group->stub_sec->output_offset
12028 + stub_entry->group->stub_sec->output_section->vma);
721956f4 12029
ad8e1ba5 12030 size = 4;
f891966f 12031 r2off = 0;
ad8e1ba5
AM
12032 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
12033 {
25f53a85 12034 r2off = get_r2off (info, stub_entry);
a7c49797 12035 if (r2off == (bfd_vma) -1)
aa374f67
AM
12036 {
12037 htab->stub_error = TRUE;
12038 return FALSE;
12039 }
a7c49797 12040 size = 8;
ac2df442 12041 if (PPC_HA (r2off) != 0)
a7c49797
AM
12042 size += 4;
12043 if (PPC_LO (r2off) != 0)
12044 size += 4;
1aa42141 12045 off += size - 4;
ad8e1ba5 12046 }
1aa42141 12047 off = targ - off;
ad8e1ba5 12048
f891966f
AM
12049 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12050 Do the same for -R objects without function descriptors. */
12051 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
12052 && r2off == 0
12053 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12054 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 12055 {
f891966f 12056 struct ppc_branch_hash_entry *br_entry;
df136d64 12057
f891966f
AM
12058 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12059 stub_entry->root.string + 9,
12060 TRUE, FALSE);
12061 if (br_entry == NULL)
721956f4 12062 {
f891966f
AM
12063 _bfd_error_handler (_("can't build branch stub `%s'"),
12064 stub_entry->root.string);
12065 htab->stub_error = TRUE;
12066 return FALSE;
721956f4
AM
12067 }
12068
f891966f 12069 if (br_entry->iter != htab->stub_iteration)
721956f4 12070 {
f891966f
AM
12071 br_entry->iter = htab->stub_iteration;
12072 br_entry->offset = htab->brlt->size;
12073 htab->brlt->size += 8;
63bc6f6c 12074
f891966f
AM
12075 if (htab->relbrlt != NULL)
12076 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12077 else if (info->emitrelocations)
84f5d08e 12078 {
f891966f
AM
12079 htab->brlt->reloc_count += 1;
12080 htab->brlt->flags |= SEC_RELOC;
05d0e962 12081 }
f891966f 12082 }
ac2df442 12083
f891966f
AM
12084 targ = (br_entry->offset
12085 + htab->brlt->output_offset
12086 + htab->brlt->output_section->vma);
12087 off = (elf_gp (info->output_bfd)
12088 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12089 off = targ - off;
176a0d42 12090
f891966f
AM
12091 if (info->emitrelocations)
12092 {
12093 stub_entry->group->stub_sec->reloc_count
12094 += 1 + (PPC_HA (off) != 0);
12095 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12096 }
05d0e962 12097
f891966f
AM
12098 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
12099 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
12100 {
12101 size = 12;
12102 if (PPC_HA (off) != 0)
12103 size = 16;
ac2df442 12104 }
f891966f 12105 else
ac2df442 12106 {
f891966f
AM
12107 size = 16;
12108 if (PPC_HA (off) != 0)
12109 size += 4;
12110
12111 if (PPC_HA (r2off) != 0)
12112 size += 4;
12113 if (PPC_LO (r2off) != 0)
12114 size += 4;
ac2df442 12115 }
721956f4 12116 }
f891966f
AM
12117 else if (info->emitrelocations)
12118 {
12119 stub_entry->group->stub_sec->reloc_count += 1;
12120 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12121 }
12122 break;
12123
12124 case ppc_stub_plt_branch_notoc:
12125 case ppc_stub_plt_branch_both:
12126 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12127 /* Fall through. */
12128 case ppc_stub_long_branch_notoc:
12129 case ppc_stub_long_branch_both:
12130 off = (stub_entry->stub_offset
12131 + stub_entry->group->stub_sec->output_offset
12132 + stub_entry->group->stub_sec->output_section->vma);
12133 size = 0;
12134 if (stub_entry->stub_type == ppc_stub_long_branch_both)
12135 size = 4;
12136 off += size;
12137 targ = (stub_entry->target_value
12138 + stub_entry->target_section->output_offset
12139 + stub_entry->target_section->output_section->vma);
04bdff6a 12140 odd = off & 4;
f891966f
AM
12141 off = targ - off;
12142
3d58e1fc
AM
12143 if (info->emitrelocations)
12144 {
04bdff6a 12145 unsigned int num_rel;
e10a07b3 12146 if (htab->params->power10_stubs != 0)
7c1f4227 12147 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12148 else
12149 num_rel = num_relocs_for_offset (off - 8);
12150 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12151 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12152 }
12153
e10a07b3 12154 if (htab->params->power10_stubs != 0)
7c1f4227 12155 extra = size_power10_offset (off, odd);
04bdff6a
AM
12156 else
12157 extra = size_offset (off - 8);
f891966f
AM
12158 /* Include branch insn plus those in the offset sequence. */
12159 size += 4 + extra;
12160 /* The branch insn is at the end, or "extra" bytes along. So
12161 its offset will be "extra" bytes less that that already
12162 calculated. */
12163 off -= extra;
12164
e10a07b3 12165 if (htab->params->power10_stubs == 0)
04bdff6a
AM
12166 {
12167 /* After the bcl, lr has been modified so we need to emit
12168 .eh_frame info saying the return address is in r12. */
12169 lr_used = stub_entry->stub_offset + 8;
12170 if (stub_entry->stub_type == ppc_stub_long_branch_both)
12171 lr_used += 4;
12172 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12173 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12174 DW_CFA_restore_extended 65. */
12175 delta = lr_used - stub_entry->group->lr_restore;
12176 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12177 stub_entry->group->lr_restore = lr_used + 8;
12178 }
f891966f
AM
12179
12180 /* If the branch can't reach, use a plt_branch. */
12181 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12182 {
12183 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
12184 - ppc_stub_long_branch_notoc);
12185 size += 4;
12186 }
3d58e1fc
AM
12187 else if (info->emitrelocations)
12188 stub_entry->group->stub_sec->reloc_count +=1;
f891966f
AM
12189 break;
12190
12191 case ppc_stub_plt_call_notoc:
12192 case ppc_stub_plt_call_both:
29433886 12193 lr_used = 0;
12cf8b93
AM
12194 if (stub_entry->h != NULL
12195 && is_tls_get_addr (&stub_entry->h->elf, htab)
29433886
AM
12196 && htab->params->tls_get_addr_opt)
12197 {
12198 lr_used += 7 * 4;
12199 if (!htab->params->no_tls_get_addr_regsave)
12200 lr_used += 11 * 4;
12201 else if (stub_entry->stub_type == ppc_stub_plt_call_both)
12202 lr_used += 2 * 4;
12203 }
f891966f 12204 if (stub_entry->stub_type == ppc_stub_plt_call_both)
29433886 12205 lr_used += 4;
f891966f
AM
12206 targ = stub_entry->plt_ent->plt.offset & ~1;
12207 if (targ >= (bfd_vma) -2)
12208 abort ();
12209
12210 plt = htab->elf.splt;
12211 if (!htab->elf.dynamic_sections_created
12212 || stub_entry->h == NULL
12213 || stub_entry->h->elf.dynindx == -1)
12214 {
12215 if (stub_entry->symtype == STT_GNU_IFUNC)
12216 plt = htab->elf.iplt;
12217 else
12218 plt = htab->pltlocal;
12219 }
12220 targ += plt->output_offset + plt->output_section->vma;
29433886
AM
12221 off = (stub_entry->stub_offset
12222 + stub_entry->group->stub_sec->output_offset
12223 + stub_entry->group->stub_sec->output_section->vma
12224 + lr_used);
04bdff6a 12225 odd = off & 4;
f891966f
AM
12226 off = targ - off;
12227
12228 if (htab->params->plt_stub_align != 0)
12229 {
29433886 12230 unsigned pad = plt_stub_pad (htab, stub_entry, off, odd);
f891966f
AM
12231
12232 stub_entry->group->stub_sec->size += pad;
12233 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12234 off -= pad;
29433886 12235 odd ^= pad & 4;
f891966f
AM
12236 }
12237
3d58e1fc
AM
12238 if (info->emitrelocations)
12239 {
04bdff6a 12240 unsigned int num_rel;
e10a07b3 12241 if (htab->params->power10_stubs != 0)
7c1f4227 12242 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12243 else
12244 num_rel = num_relocs_for_offset (off - 8);
12245 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12246 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12247 }
12248
29433886 12249 size = plt_stub_size (htab, stub_entry, off, odd);
f891966f 12250
e10a07b3 12251 if (htab->params->power10_stubs == 0)
04bdff6a
AM
12252 {
12253 /* After the bcl, lr has been modified so we need to emit
12254 .eh_frame info saying the return address is in r12. */
29433886 12255 lr_used += stub_entry->stub_offset + 8;
04bdff6a
AM
12256 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12257 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12258 DW_CFA_restore_extended 65. */
12259 delta = lr_used - stub_entry->group->lr_restore;
12260 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12261 stub_entry->group->lr_restore = lr_used + 8;
12262 }
29433886
AM
12263 if ((stub_entry->stub_type == ppc_stub_plt_call_notoc
12264 || stub_entry->stub_type == ppc_stub_plt_call_both)
12cf8b93 12265 && stub_entry->h != NULL
29433886
AM
12266 && is_tls_get_addr (&stub_entry->h->elf, htab)
12267 && htab->params->tls_get_addr_opt)
12268 {
12269 if (!htab->params->no_tls_get_addr_regsave)
12270 {
12271 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12272 delta = cfa_updt - stub_entry->group->lr_restore;
12273 stub_entry->group->eh_size += eh_advance_size (delta);
12274 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12275 stub_entry->group->lr_restore
12276 = stub_entry->stub_offset + size - 4;
12277 }
12278 else if (stub_entry->stub_type == ppc_stub_plt_call_both)
12279 {
12280 lr_used = stub_entry->stub_offset + size - 20;
12281 delta = lr_used - stub_entry->group->lr_restore;
12282 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12283 stub_entry->group->lr_restore
12284 = stub_entry->stub_offset + size - 4;
12285 }
12286 }
f891966f
AM
12287 break;
12288
12289 case ppc_stub_plt_call:
12290 case ppc_stub_plt_call_r2save:
12291 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12292 if (targ >= (bfd_vma) -2)
12293 abort ();
12294 plt = htab->elf.splt;
12295 if (!htab->elf.dynamic_sections_created
12296 || stub_entry->h == NULL
12297 || stub_entry->h->elf.dynindx == -1)
12298 {
12299 if (stub_entry->symtype == STT_GNU_IFUNC)
12300 plt = htab->elf.iplt;
12301 else
12302 plt = htab->pltlocal;
12303 }
12304 targ += plt->output_offset + plt->output_section->vma;
12305
12306 off = (elf_gp (info->output_bfd)
12307 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12308 off = targ - off;
12309
12310 if (htab->params->plt_stub_align != 0)
12311 {
29433886 12312 unsigned pad = plt_stub_pad (htab, stub_entry, off, 0);
f891966f
AM
12313
12314 stub_entry->group->stub_sec->size += pad;
12315 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12316 }
12317
12318 if (info->emitrelocations)
12319 {
12320 stub_entry->group->stub_sec->reloc_count
12321 += ((PPC_HA (off) != 0)
12322 + (htab->opd_abi
12323 ? 2 + (htab->params->plt_static_chain
12324 && PPC_HA (off + 16) == PPC_HA (off))
12325 : 1));
12326 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12327 }
12328
29433886 12329 size = plt_stub_size (htab, stub_entry, off, 0);
f891966f
AM
12330
12331 if (stub_entry->h != NULL
ed7007c1 12332 && is_tls_get_addr (&stub_entry->h->elf, htab)
f891966f
AM
12333 && htab->params->tls_get_addr_opt
12334 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
12335 {
29433886
AM
12336 if (!htab->params->no_tls_get_addr_regsave)
12337 {
12338 /* Adjustments to r1 need to be described. */
12339 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12340 delta = cfa_updt - stub_entry->group->lr_restore;
12341 stub_entry->group->eh_size += eh_advance_size (delta);
12342 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12343 }
12344 else
9e7028aa
AM
12345 {
12346 lr_used = stub_entry->stub_offset + size - 20;
12347 /* The eh_frame info will consist of a DW_CFA_advance_loc
12348 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12349 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12350 delta = lr_used - stub_entry->group->lr_restore;
12351 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12352 }
29433886 12353 stub_entry->group->lr_restore = stub_entry->stub_offset + size - 4;
f891966f
AM
12354 }
12355 break;
12356
12357 default:
12358 BFD_FAIL ();
12359 return FALSE;
721956f4
AM
12360 }
12361
6f20ed8a 12362 stub_entry->group->stub_sec->size += size;
b34976b6 12363 return TRUE;
721956f4
AM
12364}
12365
12366/* Set up various things so that we can make a list of input sections
12367 for each output section included in the link. Returns -1 on error,
cedb70c5 12368 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
12369
12370int
e7d1c40c 12371ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 12372{
6f20ed8a 12373 unsigned int id;
986f0783 12374 size_t amt;
721956f4
AM
12375 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12376
4dfe6ac6
NC
12377 if (htab == NULL)
12378 return -1;
4c52953f 12379
7cf7fcc8 12380 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
12381 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12382 htab->sec_info = bfd_zmalloc (amt);
12383 if (htab->sec_info == NULL)
721956f4
AM
12384 return -1;
12385
3d6f9012
AM
12386 /* Set toc_off for com, und, abs and ind sections. */
12387 for (id = 0; id < 3; id++)
6f20ed8a 12388 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 12389
721956f4
AM
12390 return 1;
12391}
12392
927be08e
AM
12393/* Set up for first pass at multitoc partitioning. */
12394
12395void
12396ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12397{
12398 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12399
1c865ab2 12400 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
12401 htab->toc_bfd = NULL;
12402 htab->toc_first_sec = NULL;
12403}
12404
e717da7e
AM
12405/* The linker repeatedly calls this function for each TOC input section
12406 and linker generated GOT section. Group input bfds such that the toc
927be08e 12407 within a group is less than 64k in size. */
ad8e1ba5 12408
927be08e 12409bfd_boolean
4ce794b7 12410ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
12411{
12412 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 12413 bfd_vma addr, off, limit;
ad8e1ba5 12414
4dfe6ac6
NC
12415 if (htab == NULL)
12416 return FALSE;
12417
927be08e 12418 if (!htab->second_toc_pass)
4c52953f 12419 {
927be08e 12420 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
12421 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
12422
12423 if (new_bfd)
bf102f86
AM
12424 {
12425 htab->toc_bfd = isec->owner;
12426 htab->toc_first_sec = isec;
12427 }
927be08e 12428
bf102f86
AM
12429 addr = isec->output_offset + isec->output_section->vma;
12430 off = addr - htab->toc_curr;
d77c8a4b
AM
12431 limit = 0x80008000;
12432 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12433 limit = 0x10000;
12434 if (off + isec->size > limit)
bf102f86
AM
12435 {
12436 addr = (htab->toc_first_sec->output_offset
12437 + htab->toc_first_sec->output_section->vma);
12438 htab->toc_curr = addr;
a27e685f 12439 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 12440 }
99877b66 12441
927be08e
AM
12442 /* toc_curr is the base address of this toc group. Set elf_gp
12443 for the input section to be the offset relative to the
12444 output toc base plus 0x8000. Making the input elf_gp an
12445 offset allows us to move the toc as a whole without
12446 recalculating input elf_gp. */
06bcf541 12447 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
12448 off += TOC_BASE_OFF;
12449
12450 /* Die if someone uses a linker script that doesn't keep input
12451 file .toc and .got together. */
a4fd3de5
AM
12452 if (new_bfd
12453 && elf_gp (isec->owner) != 0
927be08e
AM
12454 && elf_gp (isec->owner) != off)
12455 return FALSE;
12456
12457 elf_gp (isec->owner) = off;
12458 return TRUE;
4c52953f 12459 }
927be08e
AM
12460
12461 /* During the second pass toc_first_sec points to the start of
12462 a toc group, and toc_curr is used to track the old elf_gp.
12463 We use toc_bfd to ensure we only look at each bfd once. */
12464 if (htab->toc_bfd == isec->owner)
12465 return TRUE;
12466 htab->toc_bfd = isec->owner;
12467
12468 if (htab->toc_first_sec == NULL
12469 || htab->toc_curr != elf_gp (isec->owner))
12470 {
12471 htab->toc_curr = elf_gp (isec->owner);
12472 htab->toc_first_sec = isec;
12473 }
12474 addr = (htab->toc_first_sec->output_offset
12475 + htab->toc_first_sec->output_section->vma);
06bcf541 12476 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
12477 elf_gp (isec->owner) = off;
12478
12479 return TRUE;
ad8e1ba5
AM
12480}
12481
927be08e
AM
12482/* Called via elf_link_hash_traverse to merge GOT entries for global
12483 symbol H. */
12484
12485static bfd_boolean
12486merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12487{
12488 if (h->root.type == bfd_link_hash_indirect)
12489 return TRUE;
12490
927be08e
AM
12491 merge_got_entries (&h->got.glist);
12492
12493 return TRUE;
12494}
12495
12496/* Called via elf_link_hash_traverse to allocate GOT entries for global
12497 symbol H. */
12498
12499static bfd_boolean
12500reallocate_got (struct elf_link_hash_entry *h, void *inf)
12501{
12502 struct got_entry *gent;
12503
12504 if (h->root.type == bfd_link_hash_indirect)
12505 return TRUE;
12506
927be08e
AM
12507 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12508 if (!gent->is_indirect)
12509 allocate_got (h, (struct bfd_link_info *) inf, gent);
12510 return TRUE;
12511}
12512
12513/* Called on the first multitoc pass after the last call to
12514 ppc64_elf_next_toc_section. This function removes duplicate GOT
12515 entries. */
12516
12517bfd_boolean
12518ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12519{
12520 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
12521 struct bfd *ibfd, *ibfd2;
12522 bfd_boolean done_something;
12523
12524 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12525
7865406b
AM
12526 if (!htab->do_multi_toc)
12527 return FALSE;
12528
d0fae19d 12529 /* Merge global sym got entries within a toc group. */
927be08e
AM
12530 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12531
12532 /* And tlsld_got. */
c72f2fb2 12533 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12534 {
12535 struct got_entry *ent, *ent2;
12536
12537 if (!is_ppc64_elf (ibfd))
12538 continue;
12539
12540 ent = ppc64_tlsld_got (ibfd);
12541 if (!ent->is_indirect
12542 && ent->got.offset != (bfd_vma) -1)
12543 {
c72f2fb2 12544 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12545 {
12546 if (!is_ppc64_elf (ibfd2))
12547 continue;
12548
12549 ent2 = ppc64_tlsld_got (ibfd2);
12550 if (!ent2->is_indirect
12551 && ent2->got.offset != (bfd_vma) -1
12552 && elf_gp (ibfd2) == elf_gp (ibfd))
12553 {
12554 ent2->is_indirect = TRUE;
12555 ent2->got.ent = ent;
12556 }
12557 }
12558 }
12559 }
12560
12561 /* Zap sizes of got sections. */
33e44f2e
AM
12562 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12563 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12564 htab->got_reli_size = 0;
12565
c72f2fb2 12566 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12567 {
12568 asection *got, *relgot;
12569
12570 if (!is_ppc64_elf (ibfd))
12571 continue;
12572
12573 got = ppc64_elf_tdata (ibfd)->got;
12574 if (got != NULL)
12575 {
12576 got->rawsize = got->size;
12577 got->size = 0;
12578 relgot = ppc64_elf_tdata (ibfd)->relgot;
12579 relgot->rawsize = relgot->size;
12580 relgot->size = 0;
12581 }
12582 }
12583
12584 /* Now reallocate the got, local syms first. We don't need to
12585 allocate section contents again since we never increase size. */
c72f2fb2 12586 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12587 {
12588 struct got_entry **lgot_ents;
12589 struct got_entry **end_lgot_ents;
12590 struct plt_entry **local_plt;
12591 struct plt_entry **end_local_plt;
f961d9dd 12592 unsigned char *lgot_masks;
927be08e
AM
12593 bfd_size_type locsymcount;
12594 Elf_Internal_Shdr *symtab_hdr;
19e08130 12595 asection *s;
927be08e
AM
12596
12597 if (!is_ppc64_elf (ibfd))
12598 continue;
12599
12600 lgot_ents = elf_local_got_ents (ibfd);
12601 if (!lgot_ents)
12602 continue;
12603
12604 symtab_hdr = &elf_symtab_hdr (ibfd);
12605 locsymcount = symtab_hdr->sh_info;
12606 end_lgot_ents = lgot_ents + locsymcount;
12607 local_plt = (struct plt_entry **) end_lgot_ents;
12608 end_local_plt = local_plt + locsymcount;
f961d9dd 12609 lgot_masks = (unsigned char *) end_local_plt;
927be08e 12610 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
12611 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12612 {
12613 struct got_entry *ent;
12614
12615 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12616 {
19e08130
AM
12617 unsigned int ent_size = 8;
12618 unsigned int rel_size = sizeof (Elf64_External_Rela);
12619
d0fae19d
AM
12620 ent->got.offset = s->size;
12621 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12622 {
19e08130
AM
12623 ent_size *= 2;
12624 rel_size *= 2;
12625 }
12626 s->size += ent_size;
37da22e5 12627 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12628 {
33e44f2e 12629 htab->elf.irelplt->size += rel_size;
19e08130
AM
12630 htab->got_reli_size += rel_size;
12631 }
6a3858a6 12632 else if (bfd_link_pic (info)
f749f26e 12633 && !(ent->tls_type != 0
6a3858a6 12634 && bfd_link_executable (info)))
19e08130
AM
12635 {
12636 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12637 srel->size += rel_size;
d0fae19d
AM
12638 }
12639 }
927be08e
AM
12640 }
12641 }
12642
12643 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12644
c72f2fb2 12645 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12646 {
12647 struct got_entry *ent;
12648
12649 if (!is_ppc64_elf (ibfd))
12650 continue;
12651
12652 ent = ppc64_tlsld_got (ibfd);
12653 if (!ent->is_indirect
12654 && ent->got.offset != (bfd_vma) -1)
12655 {
12656 asection *s = ppc64_elf_tdata (ibfd)->got;
12657 ent->got.offset = s->size;
12658 s->size += 16;
f749f26e 12659 if (bfd_link_dll (info))
927be08e
AM
12660 {
12661 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12662 srel->size += sizeof (Elf64_External_Rela);
12663 }
12664 }
12665 }
12666
33e44f2e 12667 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12668 if (!done_something)
c72f2fb2 12669 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12670 {
12671 asection *got;
12672
12673 if (!is_ppc64_elf (ibfd))
12674 continue;
12675
12676 got = ppc64_elf_tdata (ibfd)->got;
12677 if (got != NULL)
12678 {
12679 done_something = got->rawsize != got->size;
12680 if (done_something)
12681 break;
12682 }
12683 }
12684
12685 if (done_something)
e7d1c40c 12686 (*htab->params->layout_sections_again) ();
927be08e
AM
12687
12688 /* Set up for second pass over toc sections to recalculate elf_gp
12689 on input sections. */
12690 htab->toc_bfd = NULL;
12691 htab->toc_first_sec = NULL;
12692 htab->second_toc_pass = TRUE;
12693 return done_something;
12694}
12695
12696/* Called after second pass of multitoc partitioning. */
12697
12698void
12699ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12700{
12701 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12702
12703 /* After the second pass, toc_curr tracks the TOC offset used
12704 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12705 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12706}
12707
9b5ecbd0
AM
12708/* No toc references were found in ISEC. If the code in ISEC makes no
12709 calls, then there's no need to use toc adjusting stubs when branching
12710 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12711 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12712 needed, and 2 if a cyclical call-graph was found but no other reason
12713 for a stub was detected. If called from the top level, a return of
12714 2 means the same as a return of 0. */
9b5ecbd0
AM
12715
12716static int
4ce794b7 12717toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12718{
9b5ecbd0 12719 int ret;
70cc837d
AM
12720
12721 /* Mark this section as checked. */
12722 isec->call_check_done = 1;
9b5ecbd0 12723
772119ce
AM
12724 /* We know none of our code bearing sections will need toc stubs. */
12725 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12726 return 0;
12727
eea6121a 12728 if (isec->size == 0)
082c50f8
AM
12729 return 0;
12730
4c52953f
AM
12731 if (isec->output_section == NULL)
12732 return 0;
12733
4c52953f 12734 ret = 0;
70cc837d 12735 if (isec->reloc_count != 0)
9b5ecbd0 12736 {
70cc837d
AM
12737 Elf_Internal_Rela *relstart, *rel;
12738 Elf_Internal_Sym *local_syms;
12739 struct ppc_link_hash_table *htab;
2917689a 12740
70cc837d
AM
12741 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12742 info->keep_memory);
12743 if (relstart == NULL)
12744 return -1;
90aecf7a 12745
70cc837d
AM
12746 /* Look for branches to outside of this section. */
12747 local_syms = NULL;
12748 htab = ppc_hash_table (info);
12749 if (htab == NULL)
12750 return -1;
4c52953f 12751
70cc837d 12752 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12753 {
70cc837d
AM
12754 enum elf_ppc64_reloc_type r_type;
12755 unsigned long r_symndx;
12756 struct elf_link_hash_entry *h;
12757 struct ppc_link_hash_entry *eh;
12758 Elf_Internal_Sym *sym;
12759 asection *sym_sec;
12760 struct _opd_sec_data *opd;
12761 bfd_vma sym_value;
12762 bfd_vma dest;
12763
12764 r_type = ELF64_R_TYPE (rel->r_info);
12765 if (r_type != R_PPC64_REL24
05d0e962 12766 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
12767 && r_type != R_PPC64_REL14
12768 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 12769 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
12770 && r_type != R_PPC64_PLTCALL
12771 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 12772 continue;
4c52953f 12773
70cc837d
AM
12774 r_symndx = ELF64_R_SYM (rel->r_info);
12775 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12776 isec->owner))
4c52953f 12777 {
70cc837d
AM
12778 ret = -1;
12779 break;
12780 }
4c52953f 12781
70cc837d
AM
12782 /* Calls to dynamic lib functions go through a plt call stub
12783 that uses r2. */
ed7007c1 12784 eh = ppc_elf_hash_entry (h);
70cc837d
AM
12785 if (eh != NULL
12786 && (eh->elf.plt.plist != NULL
12787 || (eh->oh != NULL
12788 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12789 {
12790 ret = 1;
12791 break;
4c52953f
AM
12792 }
12793
70cc837d
AM
12794 if (sym_sec == NULL)
12795 /* Ignore other undefined symbols. */
4c52953f 12796 continue;
4c52953f 12797
70cc837d
AM
12798 /* Assume branches to other sections not included in the
12799 link need stubs too, to cover -R and absolute syms. */
12800 if (sym_sec->output_section == NULL)
12801 {
12802 ret = 1;
12803 break;
12804 }
4c52953f 12805
70cc837d
AM
12806 if (h == NULL)
12807 sym_value = sym->st_value;
12808 else
12809 {
12810 if (h->root.type != bfd_link_hash_defined
12811 && h->root.type != bfd_link_hash_defweak)
12812 abort ();
12813 sym_value = h->root.u.def.value;
12814 }
12815 sym_value += rel->r_addend;
4c52953f 12816
70cc837d
AM
12817 /* If this branch reloc uses an opd sym, find the code section. */
12818 opd = get_opd_info (sym_sec);
12819 if (opd != NULL)
12820 {
12821 if (h == NULL && opd->adjust != NULL)
12822 {
12823 long adjust;
4c52953f 12824
92a9c616 12825 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12826 if (adjust == -1)
12827 /* Assume deleted functions won't ever be called. */
12828 continue;
12829 sym_value += adjust;
12830 }
4c52953f 12831
aef36ac1
AM
12832 dest = opd_entry_value (sym_sec, sym_value,
12833 &sym_sec, NULL, FALSE);
70cc837d
AM
12834 if (dest == (bfd_vma) -1)
12835 continue;
12836 }
12837 else
12838 dest = (sym_value
12839 + sym_sec->output_offset
12840 + sym_sec->output_section->vma);
4c52953f 12841
70cc837d
AM
12842 /* Ignore branch to self. */
12843 if (sym_sec == isec)
12844 continue;
4c52953f 12845
70cc837d
AM
12846 /* If the called function uses the toc, we need a stub. */
12847 if (sym_sec->has_toc_reloc
12848 || sym_sec->makes_toc_func_call)
4c52953f 12849 {
70cc837d 12850 ret = 1;
4c52953f
AM
12851 break;
12852 }
70cc837d
AM
12853
12854 /* Assume any branch that needs a long branch stub might in fact
12855 need a plt_branch stub. A plt_branch stub uses r2. */
12856 else if (dest - (isec->output_offset
12857 + isec->output_section->vma
6911b7dc
AM
12858 + rel->r_offset) + (1 << 25)
12859 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12860 ? h->other
12861 : sym->st_other))
4c52953f 12862 {
70cc837d
AM
12863 ret = 1;
12864 break;
12865 }
12866
12867 /* If calling back to a section in the process of being
12868 tested, we can't say for sure that no toc adjusting stubs
12869 are needed, so don't return zero. */
12870 else if (sym_sec->call_check_in_progress)
12871 ret = 2;
12872
12873 /* Branches to another section that itself doesn't have any TOC
12874 references are OK. Recursively call ourselves to check. */
12875 else if (!sym_sec->call_check_done)
12876 {
12877 int recur;
12878
12879 /* Mark current section as indeterminate, so that other
12880 sections that call back to current won't be marked as
12881 known. */
12882 isec->call_check_in_progress = 1;
12883 recur = toc_adjusting_stub_needed (info, sym_sec);
12884 isec->call_check_in_progress = 0;
12885
4c52953f
AM
12886 if (recur != 0)
12887 {
70cc837d
AM
12888 ret = recur;
12889 if (recur != 2)
12890 break;
4c52953f
AM
12891 }
12892 }
4c52953f 12893 }
70cc837d 12894
c9594989
AM
12895 if (elf_symtab_hdr (isec->owner).contents
12896 != (unsigned char *) local_syms)
70cc837d
AM
12897 free (local_syms);
12898 if (elf_section_data (isec)->relocs != relstart)
12899 free (relstart);
9b5ecbd0
AM
12900 }
12901
70cc837d
AM
12902 if ((ret & 1) == 0
12903 && isec->map_head.s != NULL
12904 && (strcmp (isec->output_section->name, ".init") == 0
12905 || strcmp (isec->output_section->name, ".fini") == 0))
12906 {
12907 if (isec->map_head.s->has_toc_reloc
12908 || isec->map_head.s->makes_toc_func_call)
12909 ret = 1;
12910 else if (!isec->map_head.s->call_check_done)
12911 {
12912 int recur;
12913 isec->call_check_in_progress = 1;
12914 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12915 isec->call_check_in_progress = 0;
12916 if (recur != 0)
12917 ret = recur;
12918 }
12919 }
12920
12921 if (ret == 1)
12922 isec->makes_toc_func_call = 1;
4c52953f 12923
9b5ecbd0
AM
12924 return ret;
12925}
12926
721956f4
AM
12927/* The linker repeatedly calls this function for each input section,
12928 in the order that input sections are linked into output sections.
12929 Build lists of input sections to determine groupings between which
12930 we may insert linker stubs. */
12931
9b5ecbd0 12932bfd_boolean
4ce794b7 12933ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12934{
12935 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12936
4dfe6ac6
NC
12937 if (htab == NULL)
12938 return FALSE;
12939
734b6cf9 12940 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12941 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12942 {
3d6f9012
AM
12943 /* This happens to make the list in reverse order,
12944 which is what we want. */
6f20ed8a
AM
12945 htab->sec_info[isec->id].u.list
12946 = htab->sec_info[isec->output_section->id].u.list;
12947 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12948 }
ad8e1ba5 12949
4c52953f 12950 if (htab->multi_toc_needed)
9b5ecbd0 12951 {
8b974ba3
AM
12952 /* Analyse sections that aren't already flagged as needing a
12953 valid toc pointer. Exclude .fixup for the linux kernel.
12954 .fixup contains branches, but only back to the function that
12955 hit an exception. */
12956 if (!(isec->has_toc_reloc
12957 || (isec->flags & SEC_CODE) == 0
12958 || strcmp (isec->name, ".fixup") == 0
12959 || isec->call_check_done))
12960 {
12961 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12962 return FALSE;
8b974ba3
AM
12963 }
12964 /* Make all sections use the TOC assigned for this object file.
12965 This will be wrong for pasted sections; We fix that in
12966 check_pasted_section(). */
12967 if (elf_gp (isec->owner) != 0)
12968 htab->toc_curr = elf_gp (isec->owner);
12969 }
12970
6f20ed8a 12971 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12972 return TRUE;
721956f4
AM
12973}
12974
70cc837d
AM
12975/* Check that all .init and .fini sections use the same toc, if they
12976 have toc relocs. */
12977
12978static bfd_boolean
12979check_pasted_section (struct bfd_link_info *info, const char *name)
12980{
12981 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12982
12983 if (o != NULL)
12984 {
12985 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12986 bfd_vma toc_off = 0;
12987 asection *i;
12988
12989 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12990 if (i->has_toc_reloc)
12991 {
12992 if (toc_off == 0)
6f20ed8a
AM
12993 toc_off = htab->sec_info[i->id].toc_off;
12994 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12995 return FALSE;
12996 }
6683a28d
AM
12997
12998 if (toc_off == 0)
12999 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13000 if (i->makes_toc_func_call)
13001 {
6f20ed8a 13002 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
13003 break;
13004 }
13005
70cc837d
AM
13006 /* Make sure the whole pasted function uses the same toc offset. */
13007 if (toc_off != 0)
13008 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 13009 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
13010 }
13011 return TRUE;
13012}
13013
13014bfd_boolean
13015ppc64_elf_check_init_fini (struct bfd_link_info *info)
13016{
13017 return (check_pasted_section (info, ".init")
13018 & check_pasted_section (info, ".fini"));
13019}
13020
721956f4
AM
13021/* See whether we can group stub sections together. Grouping stub
13022 sections may result in fewer stubs. More importantly, we need to
13023 put all .init* and .fini* stubs at the beginning of the .init or
13024 .fini output sections respectively, because glibc splits the
13025 _init and _fini functions into multiple parts. Putting a stub in
13026 the middle of a function is not a good idea. */
13027
6f20ed8a
AM
13028static bfd_boolean
13029group_sections (struct bfd_link_info *info,
4ce794b7
AM
13030 bfd_size_type stub_group_size,
13031 bfd_boolean stubs_always_before_branch)
721956f4 13032{
6f20ed8a
AM
13033 struct ppc_link_hash_table *htab;
13034 asection *osec;
7c8fe5c4
AM
13035 bfd_boolean suppress_size_errors;
13036
6f20ed8a
AM
13037 htab = ppc_hash_table (info);
13038 if (htab == NULL)
13039 return FALSE;
13040
7c8fe5c4 13041 suppress_size_errors = FALSE;
7c8fe5c4
AM
13042 if (stub_group_size == 1)
13043 {
13044 /* Default values. */
13045 if (stubs_always_before_branch)
09f92717 13046 stub_group_size = 0x1e00000;
7c8fe5c4 13047 else
09f92717 13048 stub_group_size = 0x1c00000;
7c8fe5c4
AM
13049 suppress_size_errors = TRUE;
13050 }
13051
6f20ed8a 13052 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 13053 {
6f20ed8a
AM
13054 asection *tail;
13055
13056 if (osec->id >= htab->sec_info_arr_size)
13057 continue;
13058
13059 tail = htab->sec_info[osec->id].u.list;
734b6cf9 13060 while (tail != NULL)
721956f4 13061 {
734b6cf9
AM
13062 asection *curr;
13063 asection *prev;
13064 bfd_size_type total;
13065 bfd_boolean big_sec;
13066 bfd_vma curr_toc;
6f20ed8a 13067 struct map_stub *group;
09f92717 13068 bfd_size_type group_size;
734b6cf9
AM
13069
13070 curr = tail;
eea6121a 13071 total = tail->size;
09f92717
AM
13072 group_size = (ppc64_elf_section_data (tail) != NULL
13073 && ppc64_elf_section_data (tail)->has_14bit_branch
13074 ? stub_group_size >> 10 : stub_group_size);
13075
13076 big_sec = total > group_size;
7c8fe5c4 13077 if (big_sec && !suppress_size_errors)
695344c0 13078 /* xgettext:c-format */
871b3ab2 13079 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 13080 tail->owner, tail);
6f20ed8a 13081 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 13082
6f20ed8a 13083 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 13084 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
13085 < (ppc64_elf_section_data (prev) != NULL
13086 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 13087 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 13088 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13089 curr = prev;
13090
13091 /* OK, the size from the start of CURR to the end is less
09f92717 13092 than group_size and thus can be handled by one stub
734b6cf9 13093 section. (or the tail section is itself larger than
09f92717
AM
13094 group_size, in which case we may be toast.) We should
13095 really be keeping track of the total size of stubs added
13096 here, as stubs contribute to the final output section
13097 size. That's a little tricky, and this way will only
13098 break if stubs added make the total size more than 2^25,
13099 ie. for the default stub_group_size, if stubs total more
13100 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
13101 group = bfd_alloc (curr->owner, sizeof (*group));
13102 if (group == NULL)
13103 return FALSE;
13104 group->link_sec = curr;
13105 group->stub_sec = NULL;
a4b6fadd 13106 group->needs_save_res = 0;
df136d64
AM
13107 group->lr_restore = 0;
13108 group->eh_size = 0;
13109 group->eh_base = 0;
a4b6fadd
AM
13110 group->next = htab->group;
13111 htab->group = group;
734b6cf9 13112 do
721956f4 13113 {
6f20ed8a 13114 prev = htab->sec_info[tail->id].u.list;
734b6cf9 13115 /* Set up this stub group. */
6f20ed8a 13116 htab->sec_info[tail->id].u.group = group;
721956f4 13117 }
734b6cf9
AM
13118 while (tail != curr && (tail = prev) != NULL);
13119
09f92717 13120 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
13121 bytes before the stub section can be handled by it too.
13122 Don't do this if we have a really large section after the
13123 stubs, as adding more stubs increases the chance that
13124 branches may not reach into the stub section. */
13125 if (!stubs_always_before_branch && !big_sec)
13126 {
13127 total = 0;
13128 while (prev != NULL
13129 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
13130 < (ppc64_elf_section_data (prev) != NULL
13131 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
13132 ? (group_size = stub_group_size >> 10)
13133 : group_size))
6f20ed8a 13134 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13135 {
13136 tail = prev;
6f20ed8a
AM
13137 prev = htab->sec_info[tail->id].u.list;
13138 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
13139 }
13140 }
13141 tail = prev;
721956f4
AM
13142 }
13143 }
6f20ed8a 13144 return TRUE;
721956f4
AM
13145}
13146
58d180e8
AM
13147static const unsigned char glink_eh_frame_cie[] =
13148{
13149 0, 0, 0, 16, /* length. */
13150 0, 0, 0, 0, /* id. */
13151 1, /* CIE version. */
13152 'z', 'R', 0, /* Augmentation string. */
13153 4, /* Code alignment. */
13154 0x78, /* Data alignment. */
13155 65, /* RA reg. */
13156 1, /* Augmentation size. */
13157 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 13158 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
13159};
13160
d969d15f
AM
13161/* Stripping output sections is normally done before dynamic section
13162 symbols have been allocated. This function is called later, and
13163 handles cases like htab->brlt which is mapped to its own output
13164 section. */
13165
13166static void
13167maybe_strip_output (struct bfd_link_info *info, asection *isec)
13168{
13169 if (isec->size == 0
13170 && isec->output_section->size == 0
53d8967a 13171 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
13172 && !bfd_section_removed_from_list (info->output_bfd,
13173 isec->output_section)
13174 && elf_section_data (isec->output_section)->dynindx == 0)
13175 {
13176 isec->output_section->flags |= SEC_EXCLUDE;
13177 bfd_section_list_remove (info->output_bfd, isec->output_section);
13178 info->output_bfd->section_count--;
13179 }
13180}
13181
721956f4
AM
13182/* Determine and set the size of the stub section for a final link.
13183
13184 The basic idea here is to examine all the relocations looking for
13185 PC-relative calls to a target that is unreachable with a "bl"
13186 instruction. */
13187
b34976b6 13188bfd_boolean
e7d1c40c 13189ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
13190{
13191 bfd_size_type stub_group_size;
b34976b6 13192 bfd_boolean stubs_always_before_branch;
721956f4
AM
13193 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13194
4dfe6ac6
NC
13195 if (htab == NULL)
13196 return FALSE;
13197
e10a07b3
AM
13198 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13199 htab->params->power10_stubs = 0;
13200
0e1862bb 13201 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 13202 htab->params->plt_thread_safe = 1;
b9e5796b 13203 if (!htab->opd_abi)
e7d1c40c
AM
13204 htab->params->plt_thread_safe = 0;
13205 else if (htab->params->plt_thread_safe == -1)
794e51c0 13206 {
e2458743 13207 static const char *const thread_starter[] =
794e51c0
AM
13208 {
13209 "pthread_create",
13210 /* libstdc++ */
13211 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13212 /* librt */
13213 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13214 "mq_notify", "create_timer",
13215 /* libanl */
13216 "getaddrinfo_a",
13217 /* libgomp */
2300b5a1 13218 "GOMP_parallel",
794e51c0 13219 "GOMP_parallel_start",
2300b5a1 13220 "GOMP_parallel_loop_static",
794e51c0 13221 "GOMP_parallel_loop_static_start",
2300b5a1 13222 "GOMP_parallel_loop_dynamic",
794e51c0 13223 "GOMP_parallel_loop_dynamic_start",
2300b5a1 13224 "GOMP_parallel_loop_guided",
794e51c0 13225 "GOMP_parallel_loop_guided_start",
2300b5a1 13226 "GOMP_parallel_loop_runtime",
794e51c0 13227 "GOMP_parallel_loop_runtime_start",
2300b5a1 13228 "GOMP_parallel_sections",
68ffbac6 13229 "GOMP_parallel_sections_start",
f9dffbf0
AM
13230 /* libgo */
13231 "__go_go",
794e51c0
AM
13232 };
13233 unsigned i;
13234
a4b6fadd 13235 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
13236 {
13237 struct elf_link_hash_entry *h;
13238 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13239 FALSE, FALSE, TRUE);
e7d1c40c
AM
13240 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13241 if (htab->params->plt_thread_safe)
794e51c0
AM
13242 break;
13243 }
13244 }
e7d1c40c
AM
13245 stubs_always_before_branch = htab->params->group_size < 0;
13246 if (htab->params->group_size < 0)
13247 stub_group_size = -htab->params->group_size;
721956f4 13248 else
e7d1c40c 13249 stub_group_size = htab->params->group_size;
721956f4 13250
6f20ed8a
AM
13251 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13252 return FALSE;
721956f4 13253
a804e476
AM
13254 htab->tga_group = NULL;
13255 if (!htab->params->no_tls_get_addr_regsave
13256 && htab->tga_desc_fd != NULL
13257 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13258 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13259 && htab->tls_get_addr_fd != NULL
13260 && is_static_defined (&htab->tls_get_addr_fd->elf))
13261 {
13262 asection *sym_sec, *code_sec, *stub_sec;
13263 bfd_vma sym_value;
13264 struct _opd_sec_data *opd;
13265
13266 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13267 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13268 code_sec = sym_sec;
13269 opd = get_opd_info (sym_sec);
13270 if (opd != NULL)
13271 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, FALSE);
13272 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13273 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13274 htab->tga_group->link_sec);
13275 if (stub_sec == NULL)
13276 return FALSE;
13277 htab->tga_group->stub_sec = stub_sec;
13278
13279 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13280 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13281 htab->tga_desc_fd->elf.root.u.def.value = 0;
13282 htab->tga_desc_fd->elf.type = STT_FUNC;
13283 htab->tga_desc_fd->elf.def_regular = 1;
13284 htab->tga_desc_fd->elf.non_elf = 0;
13285 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, TRUE);
13286 }
13287
c9301e31
AM
13288#define STUB_SHRINK_ITER 20
13289 /* Loop until no stubs added. After iteration 20 of this loop we may
13290 exit on a stub section shrinking. This is to break out of a
13291 pathological case where adding stubs on one iteration decreases
13292 section gaps (perhaps due to alignment), which then requires
13293 fewer or smaller stubs on the next iteration. */
13294
721956f4
AM
13295 while (1)
13296 {
13297 bfd *input_bfd;
13298 unsigned int bfd_indx;
a4b6fadd 13299 struct map_stub *group;
721956f4
AM
13300
13301 htab->stub_iteration += 1;
721956f4
AM
13302
13303 for (input_bfd = info->input_bfds, bfd_indx = 0;
13304 input_bfd != NULL;
c72f2fb2 13305 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
13306 {
13307 Elf_Internal_Shdr *symtab_hdr;
13308 asection *section;
6cdc0ccc 13309 Elf_Internal_Sym *local_syms = NULL;
721956f4 13310
0c8d6e5c 13311 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
13312 continue;
13313
721956f4 13314 /* We'll need the symbol table in a second. */
0ffa91dd 13315 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
13316 if (symtab_hdr->sh_info == 0)
13317 continue;
13318
721956f4
AM
13319 /* Walk over each section attached to the input bfd. */
13320 for (section = input_bfd->sections;
13321 section != NULL;
13322 section = section->next)
13323 {
721956f4 13324 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
13325
13326 /* If there aren't any relocs, then there's nothing more
13327 to do. */
13328 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
13329 || (section->flags & SEC_ALLOC) == 0
13330 || (section->flags & SEC_LOAD) == 0
13331 || (section->flags & SEC_CODE) == 0
721956f4
AM
13332 || section->reloc_count == 0)
13333 continue;
13334
13335 /* If this section is a link-once section that will be
13336 discarded, then don't create any stubs. */
13337 if (section->output_section == NULL
927be08e 13338 || section->output_section->owner != info->output_bfd)
721956f4
AM
13339 continue;
13340
1e2f5b6e
AM
13341 /* Get the relocs. */
13342 internal_relocs
4ce794b7 13343 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 13344 info->keep_memory);
721956f4 13345 if (internal_relocs == NULL)
1e2f5b6e 13346 goto error_ret_free_local;
721956f4
AM
13347
13348 /* Now examine each relocation. */
13349 irela = internal_relocs;
13350 irelaend = irela + section->reloc_count;
13351 for (; irela < irelaend; irela++)
13352 {
4ce794b7
AM
13353 enum elf_ppc64_reloc_type r_type;
13354 unsigned int r_indx;
721956f4
AM
13355 enum ppc_stub_type stub_type;
13356 struct ppc_stub_hash_entry *stub_entry;
8387904d 13357 asection *sym_sec, *code_sec;
e054468f 13358 bfd_vma sym_value, code_value;
721956f4 13359 bfd_vma destination;
6911b7dc 13360 unsigned long local_off;
8843416a 13361 bfd_boolean ok_dest;
721956f4 13362 struct ppc_link_hash_entry *hash;
8387904d 13363 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
13364 struct elf_link_hash_entry *h;
13365 Elf_Internal_Sym *sym;
721956f4
AM
13366 char *stub_name;
13367 const asection *id_sec;
74f0fb50 13368 struct _opd_sec_data *opd;
e054468f 13369 struct plt_entry *plt_ent;
721956f4
AM
13370
13371 r_type = ELF64_R_TYPE (irela->r_info);
13372 r_indx = ELF64_R_SYM (irela->r_info);
13373
4ce794b7 13374 if (r_type >= R_PPC64_max)
721956f4
AM
13375 {
13376 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 13377 goto error_ret_free_internal;
721956f4
AM
13378 }
13379
13380 /* Only look for stubs on branch instructions. */
4ce794b7 13381 if (r_type != R_PPC64_REL24
05d0e962 13382 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
13383 && r_type != R_PPC64_REL14
13384 && r_type != R_PPC64_REL14_BRTAKEN
13385 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
13386 continue;
13387
13388 /* Now determine the call target, its name, value,
13389 section. */
411e1bfb
AM
13390 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13391 r_indx, input_bfd))
13392 goto error_ret_free_internal;
ed7007c1 13393 hash = ppc_elf_hash_entry (h);
411e1bfb 13394
8843416a 13395 ok_dest = FALSE;
8387904d 13396 fdh = NULL;
7fe2b9a6 13397 sym_value = 0;
411e1bfb 13398 if (hash == NULL)
721956f4 13399 {
411e1bfb 13400 sym_value = sym->st_value;
c27b8c2a
AM
13401 if (sym_sec != NULL
13402 && sym_sec->output_section != NULL)
13403 ok_dest = TRUE;
721956f4 13404 }
7fe2b9a6
AM
13405 else if (hash->elf.root.type == bfd_link_hash_defined
13406 || hash->elf.root.type == bfd_link_hash_defweak)
13407 {
13408 sym_value = hash->elf.root.u.def.value;
13409 if (sym_sec->output_section != NULL)
13410 ok_dest = TRUE;
13411 }
13412 else if (hash->elf.root.type == bfd_link_hash_undefweak
13413 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 13414 {
99877b66 13415 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
13416 use the func descriptor sym instead if it is
13417 defined. */
ceb1f1ef 13418 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 13419 && hash->oh != NULL)
8387904d 13420 {
8c5b4e52 13421 fdh = ppc_follow_link (hash->oh);
8387904d
AM
13422 if (fdh->elf.root.type == bfd_link_hash_defined
13423 || fdh->elf.root.type == bfd_link_hash_defweak)
13424 {
13425 sym_sec = fdh->elf.root.u.def.section;
13426 sym_value = fdh->elf.root.u.def.value;
13427 if (sym_sec->output_section != NULL)
13428 ok_dest = TRUE;
13429 }
99877b66
AM
13430 else
13431 fdh = NULL;
8387904d 13432 }
7fe2b9a6
AM
13433 }
13434 else
13435 {
13436 bfd_set_error (bfd_error_bad_value);
13437 goto error_ret_free_internal;
721956f4
AM
13438 }
13439
8843416a 13440 destination = 0;
6911b7dc 13441 local_off = 0;
8843416a
AM
13442 if (ok_dest)
13443 {
13444 sym_value += irela->r_addend;
13445 destination = (sym_value
13446 + sym_sec->output_offset
13447 + sym_sec->output_section->vma);
6911b7dc
AM
13448 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13449 ? hash->elf.other
13450 : sym->st_other);
8843416a
AM
13451 }
13452
8387904d 13453 code_sec = sym_sec;
e054468f 13454 code_value = sym_value;
74f0fb50
AM
13455 opd = get_opd_info (sym_sec);
13456 if (opd != NULL)
8387904d
AM
13457 {
13458 bfd_vma dest;
13459
74f0fb50 13460 if (hash == NULL && opd->adjust != NULL)
8387904d 13461 {
51aecdc5 13462 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
13463 if (adjust == -1)
13464 continue;
e054468f 13465 code_value += adjust;
8387904d
AM
13466 sym_value += adjust;
13467 }
13468 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 13469 &code_sec, &code_value, FALSE);
8387904d
AM
13470 if (dest != (bfd_vma) -1)
13471 {
13472 destination = dest;
13473 if (fdh != NULL)
13474 {
13475 /* Fixup old ABI sym to point at code
13476 entry. */
99877b66 13477 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 13478 hash->elf.root.u.def.section = code_sec;
e054468f 13479 hash->elf.root.u.def.value = code_value;
8387904d
AM
13480 }
13481 }
13482 }
13483
721956f4 13484 /* Determine what (if any) linker stub is needed. */
e054468f 13485 plt_ent = NULL;
721956f4 13486 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
13487 &plt_ent, destination,
13488 local_off);
ad8e1ba5 13489
05d0e962
AM
13490 if (r_type == R_PPC64_REL24_NOTOC)
13491 {
13492 if (stub_type == ppc_stub_plt_call)
13493 stub_type = ppc_stub_plt_call_notoc;
13494 else if (stub_type == ppc_stub_long_branch
13495 || (code_sec != NULL
13496 && code_sec->output_section != NULL
13497 && (((hash ? hash->elf.other : sym->st_other)
13498 & STO_PPC64_LOCAL_MASK)
4a4e7361 13499 > 1 << STO_PPC64_LOCAL_BIT)))
05d0e962
AM
13500 stub_type = ppc_stub_long_branch_notoc;
13501 }
13502 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
13503 {
13504 /* Check whether we need a TOC adjusting stub.
13505 Since the linker pastes together pieces from
13506 different object files when creating the
13507 _init and _fini functions, it may be that a
13508 call to what looks like a local sym is in
13509 fact a call needing a TOC adjustment. */
33cb30a1
AM
13510 if ((code_sec != NULL
13511 && code_sec->output_section != NULL
33cb30a1 13512 && (code_sec->has_toc_reloc
918dc783
AM
13513 || code_sec->makes_toc_func_call)
13514 && (htab->sec_info[code_sec->id].toc_off
13515 != htab->sec_info[section->id].toc_off))
33cb30a1
AM
13516 || (((hash ? hash->elf.other : sym->st_other)
13517 & STO_PPC64_LOCAL_MASK)
13518 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
13519 stub_type = ppc_stub_long_branch_r2off;
13520 }
13521
721956f4
AM
13522 if (stub_type == ppc_stub_none)
13523 continue;
13524
411e1bfb
AM
13525 /* __tls_get_addr calls might be eliminated. */
13526 if (stub_type != ppc_stub_plt_call
05d0e962 13527 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 13528 && hash != NULL
ed7007c1 13529 && is_tls_get_addr (&hash->elf, htab)
411e1bfb
AM
13530 && section->has_tls_reloc
13531 && irela != internal_relocs)
13532 {
13533 /* Get tls info. */
f961d9dd 13534 unsigned char *tls_mask;
411e1bfb 13535
3a71aa26 13536 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
13537 irela - 1, input_bfd))
13538 goto error_ret_free_internal;
abc489c6
AM
13539 if ((*tls_mask & TLS_TLS) != 0
13540 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
411e1bfb
AM
13541 continue;
13542 }
13543
f378ab09 13544 if (stub_type == ppc_stub_plt_call)
794e51c0 13545 {
6e1816be
AM
13546 if (!htab->opd_abi
13547 && htab->params->plt_localentry0 != 0
13548 && is_elfv2_localentry0 (&hash->elf))
13549 htab->has_plt_localentry0 = 1;
13550 else if (irela + 1 < irelaend
13551 && irela[1].r_offset == irela->r_offset + 4
13552 && (ELF64_R_TYPE (irela[1].r_info)
13553 == R_PPC64_TOCSAVE))
f378ab09
AM
13554 {
13555 if (!tocsave_find (htab, INSERT,
13556 &local_syms, irela + 1, input_bfd))
13557 goto error_ret_free_internal;
13558 }
f378ab09
AM
13559 else
13560 stub_type = ppc_stub_plt_call_r2save;
794e51c0 13561 }
3b421ab3 13562
721956f4 13563 /* Support for grouping stub sections. */
6f20ed8a 13564 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
13565
13566 /* Get the name of this stub. */
13567 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13568 if (!stub_name)
13569 goto error_ret_free_internal;
13570
13571 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 13572 stub_name, FALSE, FALSE);
721956f4
AM
13573 if (stub_entry != NULL)
13574 {
05d0e962 13575 enum ppc_stub_type old_type;
e10a07b3 13576
05d0e962
AM
13577 /* A stub has already been created, but it may
13578 not be the required type. We shouldn't be
13579 transitioning from plt_call to long_branch
13580 stubs or vice versa, but we might be
13581 upgrading from plt_call to plt_call_r2save or
13582 from long_branch to long_branch_r2off. */
721956f4 13583 free (stub_name);
e10a07b3
AM
13584 if (htab->params->power10_stubs == -1)
13585 {
13586 /* For --power10-stubs=auto, don't merge _notoc
13587 and other varieties of stubs. (The _both
13588 variety won't be created.) */
13589 bfd_boolean notoc = r_type == R_PPC64_REL24_NOTOC;
13590 struct ppc_stub_hash_entry *alt_stub
13591 = select_alt_stub (stub_entry, notoc);
13592
13593 if (alt_stub == NULL)
13594 {
13595 alt_stub = (struct ppc_stub_hash_entry *)
13596 stub_hash_newfunc (NULL,
13597 &htab->stub_hash_table,
13598 stub_entry->root.string);
13599 if (alt_stub == NULL)
13600 {
13601 /* xgettext:c-format */
13602 _bfd_error_handler
13603 (_("%pB: cannot create stub entry %s"),
13604 section->owner, stub_entry->root.string);
13605 goto error_ret_free_internal;
13606 }
13607 *alt_stub = *stub_entry;
13608 stub_entry->root.next = &alt_stub->root;
13609 if (notoc)
13610 /* Sort notoc stubs first, for no good
13611 reason. */
13612 alt_stub = stub_entry;
13613 alt_stub->stub_type = stub_type;
13614 }
13615 stub_entry = alt_stub;
13616 }
05d0e962
AM
13617 old_type = stub_entry->stub_type;
13618 switch (old_type)
13619 {
13620 default:
13621 abort ();
13622
13623 case ppc_stub_save_res:
13624 continue;
13625
13626 case ppc_stub_plt_call:
13627 case ppc_stub_plt_call_r2save:
13628 case ppc_stub_plt_call_notoc:
13629 case ppc_stub_plt_call_both:
13630 if (stub_type == ppc_stub_plt_call)
13631 continue;
13632 else if (stub_type == ppc_stub_plt_call_r2save)
13633 {
13634 if (old_type == ppc_stub_plt_call_notoc)
13635 stub_type = ppc_stub_plt_call_both;
13636 }
13637 else if (stub_type == ppc_stub_plt_call_notoc)
13638 {
13639 if (old_type == ppc_stub_plt_call_r2save)
13640 stub_type = ppc_stub_plt_call_both;
13641 }
13642 else
13643 abort ();
13644 break;
13645
13646 case ppc_stub_plt_branch:
13647 case ppc_stub_plt_branch_r2off:
13648 case ppc_stub_plt_branch_notoc:
13649 case ppc_stub_plt_branch_both:
13650 old_type += (ppc_stub_long_branch
13651 - ppc_stub_plt_branch);
13652 /* Fall through. */
13653 case ppc_stub_long_branch:
13654 case ppc_stub_long_branch_r2off:
13655 case ppc_stub_long_branch_notoc:
13656 case ppc_stub_long_branch_both:
13657 if (stub_type == ppc_stub_long_branch)
13658 continue;
13659 else if (stub_type == ppc_stub_long_branch_r2off)
13660 {
13661 if (old_type == ppc_stub_long_branch_notoc)
13662 stub_type = ppc_stub_long_branch_both;
13663 }
13664 else if (stub_type == ppc_stub_long_branch_notoc)
13665 {
13666 if (old_type == ppc_stub_long_branch_r2off)
13667 stub_type = ppc_stub_long_branch_both;
13668 }
13669 else
13670 abort ();
13671 break;
13672 }
13673 if (old_type < stub_type)
794e51c0 13674 stub_entry->stub_type = stub_type;
721956f4
AM
13675 continue;
13676 }
13677
25f53a85 13678 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
13679 if (stub_entry == NULL)
13680 {
13681 free (stub_name);
6cdc0ccc
AM
13682 error_ret_free_internal:
13683 if (elf_section_data (section)->relocs == NULL)
13684 free (internal_relocs);
13685 error_ret_free_local:
c9594989
AM
13686 if (symtab_hdr->contents
13687 != (unsigned char *) local_syms)
6cdc0ccc 13688 free (local_syms);
b34976b6 13689 return FALSE;
721956f4
AM
13690 }
13691
ad8e1ba5 13692 stub_entry->stub_type = stub_type;
05d0e962
AM
13693 if (stub_type >= ppc_stub_plt_call
13694 && stub_type <= ppc_stub_plt_call_both)
e054468f 13695 {
05d0e962
AM
13696 stub_entry->target_value = sym_value;
13697 stub_entry->target_section = sym_sec;
e054468f
AM
13698 }
13699 else
13700 {
05d0e962
AM
13701 stub_entry->target_value = code_value;
13702 stub_entry->target_section = code_sec;
e054468f 13703 }
721956f4 13704 stub_entry->h = hash;
e054468f 13705 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
13706 stub_entry->symtype
13707 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 13708 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 13709
3d58e1fc
AM
13710 if (hash != NULL
13711 && (hash->elf.root.type == bfd_link_hash_defined
13712 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 13713 htab->stub_globals += 1;
721956f4
AM
13714 }
13715
13716 /* We're done with the internal relocs, free them. */
6cdc0ccc 13717 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 13718 free (internal_relocs);
721956f4 13719 }
6cdc0ccc
AM
13720
13721 if (local_syms != NULL
13722 && symtab_hdr->contents != (unsigned char *) local_syms)
13723 {
13724 if (!info->keep_memory)
13725 free (local_syms);
13726 else
13727 symtab_hdr->contents = (unsigned char *) local_syms;
13728 }
721956f4
AM
13729 }
13730
5c3dead3 13731 /* We may have added some stubs. Find out the new size of the
721956f4 13732 stub sections. */
d4aaa2a0 13733 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13734 {
13735 group->lr_restore = 0;
13736 group->eh_size = 0;
13737 if (group->stub_sec != NULL)
13738 {
13739 asection *stub_sec = group->stub_sec;
13740
13741 if (htab->stub_iteration <= STUB_SHRINK_ITER
13742 || stub_sec->rawsize < stub_sec->size)
13743 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13744 stub_sec->rawsize = stub_sec->size;
13745 stub_sec->size = 0;
13746 stub_sec->reloc_count = 0;
13747 stub_sec->flags &= ~SEC_RELOC;
13748 }
13749 }
a804e476
AM
13750 if (htab->tga_group != NULL)
13751 {
13752 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
13753 htab->tga_group->eh_size
13754 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
13755 htab->tga_group->lr_restore = 23 * 4;
13756 htab->tga_group->stub_sec->size = 24 * 4;
13757 }
eea6121a 13758
ba21f564
AM
13759 if (htab->stub_iteration <= STUB_SHRINK_ITER
13760 || htab->brlt->rawsize < htab->brlt->size)
13761 htab->brlt->rawsize = htab->brlt->size;
eea6121a 13762 htab->brlt->size = 0;
84f5d08e
AM
13763 htab->brlt->reloc_count = 0;
13764 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 13765 if (htab->relbrlt != NULL)
eea6121a 13766 htab->relbrlt->size = 0;
721956f4 13767
63bc6f6c 13768 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 13769
a4b6fadd
AM
13770 for (group = htab->group; group != NULL; group = group->next)
13771 if (group->needs_save_res)
13772 group->stub_sec->size += htab->sfpr->size;
13773
176a0d42
AM
13774 if (info->emitrelocations
13775 && htab->glink != NULL && htab->glink->size != 0)
13776 {
13777 htab->glink->reloc_count = 1;
13778 htab->glink->flags |= SEC_RELOC;
13779 }
13780
58d180e8
AM
13781 if (htab->glink_eh_frame != NULL
13782 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 13783 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 13784 {
2e0ce1c8 13785 size_t size = 0, align = 4;
58d180e8 13786
d4aaa2a0 13787 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13788 if (group->eh_size != 0)
13789 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 13790 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 13791 size += (24 + align - 1) & -align;
58d180e8 13792 if (size != 0)
2e0ce1c8
AM
13793 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13794 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13795 size = (size + align - 1) & -align;
58d180e8
AM
13796 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13797 htab->glink_eh_frame->size = size;
13798 }
13799
e7d1c40c 13800 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13801 for (group = htab->group; group != NULL; group = group->next)
13802 if (group->stub_sec != NULL)
691d2e9a
AM
13803 {
13804 int align = abs (htab->params->plt_stub_align);
13805 group->stub_sec->size
13806 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13807 }
d4aaa2a0
AM
13808
13809 for (group = htab->group; group != NULL; group = group->next)
13810 if (group->stub_sec != NULL
13811 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 13812 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 13813 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
13814 break;
13815
d4aaa2a0 13816 if (group == NULL
ba21f564
AM
13817 && (htab->brlt->rawsize == htab->brlt->size
13818 || (htab->stub_iteration > STUB_SHRINK_ITER
13819 && htab->brlt->rawsize > htab->brlt->size))
58d180e8 13820 && (htab->glink_eh_frame == NULL
a804e476
AM
13821 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
13822 && (htab->tga_group == NULL
13823 || htab->stub_iteration > 1))
5c3dead3
AM
13824 break;
13825
721956f4 13826 /* Ask the linker to do its stuff. */
e7d1c40c 13827 (*htab->params->layout_sections_again) ();
721956f4
AM
13828 }
13829
da44f4e5
AM
13830 if (htab->glink_eh_frame != NULL
13831 && htab->glink_eh_frame->size != 0)
13832 {
13833 bfd_vma val;
13834 bfd_byte *p, *last_fde;
13835 size_t last_fde_len, size, align, pad;
d4aaa2a0 13836 struct map_stub *group;
da44f4e5 13837
df136d64
AM
13838 /* It is necessary to at least have a rough outline of the
13839 linker generated CIEs and FDEs written before
13840 bfd_elf_discard_info is run, in order for these FDEs to be
13841 indexed in .eh_frame_hdr. */
da44f4e5
AM
13842 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13843 if (p == NULL)
13844 return FALSE;
13845 htab->glink_eh_frame->contents = p;
13846 last_fde = p;
2e0ce1c8 13847 align = 4;
da44f4e5
AM
13848
13849 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13850 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 13851 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 13852 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 13853 p += last_fde_len + 4;
da44f4e5 13854
d4aaa2a0 13855 for (group = htab->group; group != NULL; group = group->next)
df136d64 13856 if (group->eh_size != 0)
da44f4e5 13857 {
df136d64 13858 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 13859 last_fde = p;
df136d64 13860 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 13861 /* FDE length. */
2e0ce1c8 13862 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13863 p += 4;
13864 /* CIE pointer. */
13865 val = p - htab->glink_eh_frame->contents;
13866 bfd_put_32 (htab->elf.dynobj, val, p);
13867 p += 4;
13868 /* Offset to stub section, written later. */
13869 p += 4;
13870 /* stub section size. */
d4aaa2a0 13871 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
13872 p += 4;
13873 /* Augmentation. */
13874 p += 1;
df136d64
AM
13875 /* Make sure we don't have all nops. This is enough for
13876 elf-eh-frame.c to detect the last non-nop opcode. */
13877 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 13878 p = last_fde + last_fde_len + 4;
da44f4e5
AM
13879 }
13880 if (htab->glink != NULL && htab->glink->size != 0)
13881 {
13882 last_fde = p;
2e0ce1c8 13883 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13884 /* FDE length. */
2e0ce1c8 13885 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13886 p += 4;
13887 /* CIE pointer. */
13888 val = p - htab->glink_eh_frame->contents;
13889 bfd_put_32 (htab->elf.dynobj, val, p);
13890 p += 4;
13891 /* Offset to .glink, written later. */
13892 p += 4;
13893 /* .glink size. */
13894 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13895 p += 4;
13896 /* Augmentation. */
13897 p += 1;
13898
3cd7c7d7 13899 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
da44f4e5
AM
13900 *p++ = DW_CFA_register;
13901 *p++ = 65;
9f08fa5c 13902 *p++ = htab->opd_abi ? 12 : 0;
3cd7c7d7 13903 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
da44f4e5
AM
13904 *p++ = DW_CFA_restore_extended;
13905 *p++ = 65;
2e0ce1c8 13906 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13907 }
13908 /* Subsume any padding into the last FDE if user .eh_frame
13909 sections are aligned more than glink_eh_frame. Otherwise any
13910 zero padding will be seen as a terminator. */
2e0ce1c8 13911 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13912 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13913 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13914 htab->glink_eh_frame->size = size + pad;
13915 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13916 }
13917
d969d15f 13918 maybe_strip_output (info, htab->brlt);
2efec98b
AM
13919 if (htab->relbrlt != NULL)
13920 maybe_strip_output (info, htab->relbrlt);
d969d15f
AM
13921 if (htab->glink_eh_frame != NULL)
13922 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13923
b34976b6 13924 return TRUE;
721956f4
AM
13925}
13926
13927/* Called after we have determined section placement. If sections
805fc799 13928 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13929
805fc799 13930bfd_vma
1c865ab2 13931ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13932{
805fc799 13933 asection *s;
a27e685f 13934 bfd_vma TOCstart, adjust;
721956f4 13935
43417696
AM
13936 if (info != NULL)
13937 {
13938 struct elf_link_hash_entry *h;
13939 struct elf_link_hash_table *htab = elf_hash_table (info);
13940
13941 if (is_elf_hash_table (htab)
13942 && htab->hgot != NULL)
13943 h = htab->hgot;
13944 else
13945 {
13946 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13947 if (is_elf_hash_table (htab))
13948 htab->hgot = h;
13949 }
13950 if (h != NULL
13951 && h->root.type == bfd_link_hash_defined
13952 && !h->root.linker_def
13953 && (!is_elf_hash_table (htab)
13954 || h->def_regular))
13955 {
ed7007c1 13956 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
43417696
AM
13957 _bfd_set_gp_value (obfd, TOCstart);
13958 return TOCstart;
13959 }
13960 }
13961
805fc799
AM
13962 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13963 order. The TOC starts where the first of these sections starts. */
13964 s = bfd_get_section_by_name (obfd, ".got");
e054468f 13965 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13966 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 13967 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13968 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 13969 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13970 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 13971 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
13972 {
13973 /* This may happen for
13974 o references to TOC base (SYM@toc / TOC[tc0]) without a
13975 .toc directive
13976 o bad linker script
13977 o --gc-sections and empty TOC sections
13978
13979 FIXME: Warn user? */
13980
13981 /* Look for a likely section. We probably won't even be
13982 using TOCstart. */
13983 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13984 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13985 | SEC_EXCLUDE))
805fc799
AM
13986 == (SEC_ALLOC | SEC_SMALL_DATA))
13987 break;
721956f4 13988 if (s == NULL)
805fc799 13989 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13990 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
13991 == (SEC_ALLOC | SEC_SMALL_DATA))
13992 break;
721956f4 13993 if (s == NULL)
805fc799 13994 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13995 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13996 == SEC_ALLOC)
805fc799 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_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
14001 break;
14002 }
721956f4 14003
805fc799
AM
14004 TOCstart = 0;
14005 if (s != NULL)
14006 TOCstart = s->output_section->vma + s->output_offset;
721956f4 14007
a27e685f
AM
14008 /* Force alignment. */
14009 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14010 TOCstart -= adjust;
1c865ab2
AM
14011 _bfd_set_gp_value (obfd, TOCstart);
14012
810d4e75 14013 if (info != NULL && s != NULL)
1c865ab2
AM
14014 {
14015 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14016
810d4e75
AM
14017 if (htab != NULL)
14018 {
14019 if (htab->elf.hgot != NULL)
14020 {
a27e685f 14021 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
14022 htab->elf.hgot->root.u.def.section = s;
14023 }
14024 }
14025 else
1c865ab2 14026 {
810d4e75
AM
14027 struct bfd_link_hash_entry *bh = NULL;
14028 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
14029 s, TOC_BASE_OFF - adjust,
14030 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
14031 }
14032 }
805fc799 14033 return TOCstart;
721956f4
AM
14034}
14035
a345bc8d 14036/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 14037 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
14038
14039static bfd_boolean
49c09209 14040build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
14041{
14042 struct bfd_link_info *info;
14043 struct ppc_link_hash_table *htab;
49c09209 14044 struct plt_entry *ent;
a345bc8d
AM
14045 asection *s;
14046
14047 if (h->root.type == bfd_link_hash_indirect)
14048 return TRUE;
14049
49c09209
AM
14050 info = inf;
14051 htab = ppc_hash_table (info);
14052 if (htab == NULL)
14053 return FALSE;
14054
14055 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14056 if (ent->plt.offset != (bfd_vma) -1)
14057 {
14058 /* This symbol has an entry in the procedure linkage
14059 table. Set it up. */
14060 Elf_Internal_Rela rela;
2d7ad24e 14061 asection *plt, *relplt;
49c09209
AM
14062 bfd_byte *loc;
14063
14064 if (!htab->elf.dynamic_sections_created
14065 || h->dynindx == -1)
14066 {
14067 if (!(h->def_regular
14068 && (h->root.type == bfd_link_hash_defined
14069 || h->root.type == bfd_link_hash_defweak)))
14070 continue;
2d7ad24e
AM
14071 if (h->type == STT_GNU_IFUNC)
14072 {
14073 plt = htab->elf.iplt;
14074 relplt = htab->elf.irelplt;
cebd6b8a 14075 htab->elf.ifunc_resolvers = TRUE;
2d7ad24e
AM
14076 if (htab->opd_abi)
14077 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14078 else
14079 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14080 }
49c09209 14081 else
2d7ad24e
AM
14082 {
14083 plt = htab->pltlocal;
14084 if (bfd_link_pic (info))
14085 {
14086 relplt = htab->relpltlocal;
14087 if (htab->opd_abi)
14088 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14089 else
14090 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14091 }
14092 else
14093 relplt = NULL;
14094 }
ed7007c1 14095 rela.r_addend = defined_sym_val (h) + ent->addend;
2d7ad24e
AM
14096
14097 if (relplt == NULL)
14098 {
14099 loc = plt->contents + ent->plt.offset;
14100 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14101 if (htab->opd_abi)
14102 {
14103 bfd_vma toc = elf_gp (info->output_bfd);
14104 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14105 bfd_put_64 (info->output_bfd, toc, loc + 8);
14106 }
14107 }
14108 else
14109 {
14110 rela.r_offset = (plt->output_section->vma
14111 + plt->output_offset
14112 + ent->plt.offset);
14113 loc = relplt->contents + (relplt->reloc_count++
14114 * sizeof (Elf64_External_Rela));
14115 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14116 }
49c09209
AM
14117 }
14118 else
14119 {
14120 rela.r_offset = (htab->elf.splt->output_section->vma
14121 + htab->elf.splt->output_offset
14122 + ent->plt.offset);
14123 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14124 rela.r_addend = ent->addend;
14125 loc = (htab->elf.srelplt->contents
14126 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14127 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14128 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
cebd6b8a 14129 htab->elf.ifunc_resolvers = TRUE;
2d7ad24e 14130 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 14131 }
49c09209
AM
14132 }
14133
a345bc8d
AM
14134 if (!h->pointer_equality_needed)
14135 return TRUE;
14136
14137 if (h->def_regular)
14138 return TRUE;
14139
9e390558 14140 s = htab->global_entry;
49c09209
AM
14141 if (s == NULL || s->size == 0)
14142 return TRUE;
14143
14144 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14145 if (ent->plt.offset != (bfd_vma) -1
14146 && ent->addend == 0)
a345bc8d
AM
14147 {
14148 bfd_byte *p;
14149 asection *plt;
14150 bfd_vma off;
14151
a345bc8d 14152 p = s->contents + h->root.u.def.value;
33e44f2e 14153 plt = htab->elf.splt;
a345bc8d
AM
14154 if (!htab->elf.dynamic_sections_created
14155 || h->dynindx == -1)
2d7ad24e
AM
14156 {
14157 if (h->type == STT_GNU_IFUNC)
14158 plt = htab->elf.iplt;
14159 else
14160 plt = htab->pltlocal;
14161 }
49c09209 14162 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
14163 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14164
14165 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14166 {
14167 info->callbacks->einfo
c1c8c1ef 14168 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
14169 h->root.root.string);
14170 bfd_set_error (bfd_error_bad_value);
14171 htab->stub_error = TRUE;
14172 }
14173
7341d5e2
AM
14174 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14175 if (htab->params->emit_stub_syms)
14176 {
14177 size_t len = strlen (h->root.root.string);
14178 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14179
14180 if (name == NULL)
14181 return FALSE;
14182
14183 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14184 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
14185 if (h == NULL)
14186 return FALSE;
14187 if (h->root.type == bfd_link_hash_new)
14188 {
14189 h->root.type = bfd_link_hash_defined;
14190 h->root.u.def.section = s;
14191 h->root.u.def.value = p - s->contents;
14192 h->ref_regular = 1;
14193 h->def_regular = 1;
14194 h->ref_regular_nonweak = 1;
14195 h->forced_local = 1;
14196 h->non_elf = 0;
2ec55de3 14197 h->root.linker_def = 1;
7341d5e2
AM
14198 }
14199 }
14200
a345bc8d
AM
14201 if (PPC_HA (off) != 0)
14202 {
14203 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14204 p += 4;
14205 }
14206 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14207 p += 4;
14208 bfd_put_32 (s->owner, MTCTR_R12, p);
14209 p += 4;
407aa07c 14210 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
14211 break;
14212 }
14213 return TRUE;
14214}
14215
49c09209
AM
14216/* Write PLT relocs for locals. */
14217
14218static bfd_boolean
14219write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14220{
14221 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14222 bfd *ibfd;
14223
14224 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14225 {
14226 struct got_entry **lgot_ents, **end_lgot_ents;
14227 struct plt_entry **local_plt, **lplt, **end_local_plt;
14228 Elf_Internal_Shdr *symtab_hdr;
14229 bfd_size_type locsymcount;
14230 Elf_Internal_Sym *local_syms = NULL;
14231 struct plt_entry *ent;
14232
14233 if (!is_ppc64_elf (ibfd))
14234 continue;
14235
14236 lgot_ents = elf_local_got_ents (ibfd);
14237 if (!lgot_ents)
14238 continue;
14239
14240 symtab_hdr = &elf_symtab_hdr (ibfd);
14241 locsymcount = symtab_hdr->sh_info;
14242 end_lgot_ents = lgot_ents + locsymcount;
14243 local_plt = (struct plt_entry **) end_lgot_ents;
14244 end_local_plt = local_plt + locsymcount;
14245 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14246 for (ent = *lplt; ent != NULL; ent = ent->next)
14247 if (ent->plt.offset != (bfd_vma) -1)
14248 {
14249 Elf_Internal_Sym *sym;
14250 asection *sym_sec;
14251 asection *plt, *relplt;
14252 bfd_byte *loc;
14253 bfd_vma val;
49c09209
AM
14254
14255 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14256 lplt - local_plt, ibfd))
14257 {
c9594989 14258 if (symtab_hdr->contents != (unsigned char *) local_syms)
49c09209
AM
14259 free (local_syms);
14260 return FALSE;
14261 }
14262
14263 val = sym->st_value + ent->addend;
49c09209
AM
14264 if (sym_sec != NULL && sym_sec->output_section != NULL)
14265 val += sym_sec->output_offset + sym_sec->output_section->vma;
14266
2d7ad24e
AM
14267 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14268 {
cebd6b8a 14269 htab->elf.ifunc_resolvers = TRUE;
2d7ad24e
AM
14270 plt = htab->elf.iplt;
14271 relplt = htab->elf.irelplt;
14272 }
14273 else
14274 {
14275 plt = htab->pltlocal;
14276 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
14277 }
49c09209 14278
2d7ad24e
AM
14279 if (relplt == NULL)
14280 {
14281 loc = plt->contents + ent->plt.offset;
14282 bfd_put_64 (info->output_bfd, val, loc);
14283 if (htab->opd_abi)
14284 {
14285 bfd_vma toc = elf_gp (ibfd);
14286 bfd_put_64 (info->output_bfd, toc, loc + 8);
14287 }
14288 }
49c09209 14289 else
2d7ad24e
AM
14290 {
14291 Elf_Internal_Rela rela;
14292 rela.r_offset = (ent->plt.offset
14293 + plt->output_offset
14294 + plt->output_section->vma);
14295 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14296 {
14297 if (htab->opd_abi)
14298 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14299 else
14300 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14301 }
14302 else
14303 {
14304 if (htab->opd_abi)
14305 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14306 else
14307 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14308 }
14309 rela.r_addend = val;
14310 loc = relplt->contents + (relplt->reloc_count++
14311 * sizeof (Elf64_External_Rela));
14312 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14313 }
49c09209
AM
14314 }
14315
14316 if (local_syms != NULL
14317 && symtab_hdr->contents != (unsigned char *) local_syms)
14318 {
14319 if (!info->keep_memory)
14320 free (local_syms);
14321 else
14322 symtab_hdr->contents = (unsigned char *) local_syms;
14323 }
14324 }
14325 return TRUE;
14326}
14327
a804e476
AM
14328/* Emit the static wrapper function preserving registers around a
14329 __tls_get_addr_opt call. */
14330
14331static bfd_boolean
14332emit_tga_desc (struct ppc_link_hash_table *htab)
14333{
14334 asection *stub_sec = htab->tga_group->stub_sec;
14335 unsigned int cfa_updt = 11 * 4;
14336 bfd_byte *p;
14337 bfd_vma to, from, delta;
14338
14339 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14340 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14341 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14342 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14343 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14344 delta = to - from;
14345 if (delta + (1 << 25) >= 1 << 26)
14346 {
14347 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14348 htab->stub_error = TRUE;
14349 return FALSE;
14350 }
14351
14352 p = stub_sec->contents;
14353 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14354 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14355 p += 4;
14356 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14357 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14358}
14359
14360/* Emit eh_frame describing the static wrapper function. */
14361
14362static bfd_byte *
14363emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14364{
14365 unsigned int cfa_updt = 11 * 4;
14366 unsigned int i;
14367
14368 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14369 *p++ = DW_CFA_def_cfa_offset;
14370 if (htab->opd_abi)
14371 {
14372 *p++ = 128;
14373 *p++ = 1;
14374 }
14375 else
14376 *p++ = 96;
14377 *p++ = DW_CFA_offset_extended_sf;
14378 *p++ = 65;
14379 *p++ = (-16 / 8) & 0x7f;
14380 for (i = 4; i < 12; i++)
14381 {
14382 *p++ = DW_CFA_offset + i;
14383 *p++ = (htab->opd_abi ? 13 : 12) - i;
14384 }
14385 *p++ = DW_CFA_advance_loc + 10;
14386 *p++ = DW_CFA_def_cfa_offset;
14387 *p++ = 0;
14388 for (i = 4; i < 12; i++)
14389 *p++ = DW_CFA_restore + i;
14390 *p++ = DW_CFA_advance_loc + 2;
14391 *p++ = DW_CFA_restore_extended;
14392 *p++ = 65;
14393 return p;
14394}
14395
721956f4
AM
14396/* Build all the stubs associated with the current output file.
14397 The stubs are kept in a hash table attached to the main linker
14398 hash table. This function is called via gldelf64ppc_finish. */
14399
b34976b6 14400bfd_boolean
e7d1c40c 14401ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 14402 char **stats)
5d1634d7
AM
14403{
14404 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 14405 struct map_stub *group;
721956f4 14406 asection *stub_sec;
5d1634d7 14407 bfd_byte *p;
e717da7e 14408 int stub_sec_count = 0;
5d1634d7 14409
4dfe6ac6
NC
14410 if (htab == NULL)
14411 return FALSE;
14412
eea6121a 14413 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 14414 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14415 {
14416 group->eh_size = 0;
14417 group->lr_restore = 0;
14418 if ((stub_sec = group->stub_sec) != NULL
14419 && stub_sec->size != 0)
14420 {
14421 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14422 stub_sec->size);
14423 if (stub_sec->contents == NULL)
14424 return FALSE;
14425 stub_sec->size = 0;
14426 }
14427 }
5d1634d7 14428
23eb7e01 14429 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 14430 {
9f951329 14431 unsigned int indx;
ad8e1ba5 14432 bfd_vma plt0;
9f951329 14433
721956f4 14434 /* Build the .glink plt call stub. */
e7d1c40c 14435 if (htab->params->emit_stub_syms)
97b639ba
AM
14436 {
14437 struct elf_link_hash_entry *h;
468392fb
AM
14438 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14439 TRUE, FALSE, FALSE);
97b639ba
AM
14440 if (h == NULL)
14441 return FALSE;
14442 if (h->root.type == bfd_link_hash_new)
14443 {
14444 h->root.type = bfd_link_hash_defined;
14445 h->root.u.def.section = htab->glink;
ee4bf8d2 14446 h->root.u.def.value = 8;
f5385ebf
AM
14447 h->ref_regular = 1;
14448 h->def_regular = 1;
14449 h->ref_regular_nonweak = 1;
14450 h->forced_local = 1;
14451 h->non_elf = 0;
2ec55de3 14452 h->root.linker_def = 1;
97b639ba
AM
14453 }
14454 }
33e44f2e
AM
14455 plt0 = (htab->elf.splt->output_section->vma
14456 + htab->elf.splt->output_offset
14457 - 16);
176a0d42
AM
14458 if (info->emitrelocations)
14459 {
14460 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14461 if (r == NULL)
14462 return FALSE;
14463 r->r_offset = (htab->glink->output_offset
14464 + htab->glink->output_section->vma);
14465 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14466 r->r_addend = plt0;
14467 }
4ce794b7 14468 p = htab->glink->contents;
176a0d42 14469 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
14470 bfd_put_64 (htab->glink->owner, plt0, p);
14471 p += 8;
b9e5796b
AM
14472 if (htab->opd_abi)
14473 {
14474 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14475 p += 4;
14476 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14477 p += 4;
14478 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14479 p += 4;
14480 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14481 p += 4;
14482 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14483 p += 4;
14484 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14485 p += 4;
14486 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14487 p += 4;
14488 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14489 p += 4;
14490 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14491 p += 4;
14492 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14493 p += 4;
14494 }
14495 else
14496 {
3cd7c7d7
AM
14497 unsigned int insn;
14498
14499 /* 0:
14500 . .quad plt0-1f # plt0 entry relative to 1:
14501 #
14502 # We get here with r12 initially @ a glink branch
14503 # Load the address of _dl_runtime_resolve from plt0 and
14504 # jump to it, with r0 set to the index of the PLT entry
14505 # to be resolved and r11 the link map.
14506 __glink_PLTresolve:
14507 . std %r2,24(%r1) # optional
14508 . mflr %r0
14509 . bcl 20,31,1f
14510 1:
14511 . mflr %r11
14512 . mtlr %r0
14513 . ld %r0,(0b-1b)(%r11)
14514 . sub %r12,%r12,%r11
14515 . add %r11,%r0,%r11
14516 . addi %r0,%r12,1b-2f
14517 . ld %r12,0(%r11)
14518 . srdi %r0,%r0,2
14519 . mtctr %r12
14520 . ld %r11,8(%r11)
14521 . bctr
14522 2:
14523 . b __glink_PLTresolve
14524 . ...
14525 . b __glink_PLTresolve */
14526
14527 if (htab->has_plt_localentry0)
14528 {
14529 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14530 p += 4;
14531 }
b9e5796b
AM
14532 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14533 p += 4;
14534 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14535 p += 4;
14536 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14537 p += 4;
b9e5796b
AM
14538 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14539 p += 4;
3cd7c7d7
AM
14540 if (htab->has_plt_localentry0)
14541 insn = LD_R0_0R11 | (-20 & 0xfffc);
14542 else
14543 insn = LD_R0_0R11 | (-16 & 0xfffc);
14544 bfd_put_32 (htab->glink->owner, insn, p);
14545 p += 4;
b9e5796b
AM
14546 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14547 p += 4;
3cd7c7d7 14548 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
b9e5796b 14549 p += 4;
3cd7c7d7 14550 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
b9e5796b
AM
14551 p += 4;
14552 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14553 p += 4;
14554 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14555 p += 4;
14556 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14557 p += 4;
14558 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14559 p += 4;
14560 }
407aa07c
AM
14561 bfd_put_32 (htab->glink->owner, BCTR, p);
14562 p += 4;
c75bc4f7 14563 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 14564
9f951329
AM
14565 /* Build the .glink lazy link call stubs. */
14566 indx = 0;
9e390558 14567 while (p < htab->glink->contents + htab->glink->size)
9f951329 14568 {
b9e5796b 14569 if (htab->opd_abi)
9f951329 14570 {
b9e5796b
AM
14571 if (indx < 0x8000)
14572 {
14573 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14574 p += 4;
14575 }
14576 else
14577 {
14578 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14579 p += 4;
14580 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14581 p);
14582 p += 4;
14583 }
9f951329 14584 }
4ce794b7 14585 bfd_put_32 (htab->glink->owner,
ee4bf8d2 14586 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 14587 indx++;
9f951329
AM
14588 p += 4;
14589 }
5d1634d7 14590 }
5d1634d7 14591
a804e476
AM
14592 if (htab->tga_group != NULL)
14593 {
14594 htab->tga_group->lr_restore = 23 * 4;
14595 htab->tga_group->stub_sec->size = 24 * 4;
14596 if (!emit_tga_desc (htab))
14597 return FALSE;
14598 if (htab->glink_eh_frame != NULL
14599 && htab->glink_eh_frame->size != 0)
14600 {
14601 size_t align = 4;
14602
14603 p = htab->glink_eh_frame->contents;
14604 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14605 p += 17;
14606 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
14607 }
14608 }
14609
49c09209
AM
14610 /* Build .glink global entry stubs, and PLT relocs for globals. */
14611 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
14612
14613 if (!write_plt_relocs_for_local_syms (info))
14614 return FALSE;
9e390558 14615
7341d5e2 14616 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 14617 {
4ce794b7 14618 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 14619 htab->brlt->size);
4ce794b7 14620 if (htab->brlt->contents == NULL)
b34976b6 14621 return FALSE;
721956f4 14622 }
ee75fd95 14623 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
14624 {
14625 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 14626 htab->relbrlt->size);
63bc6f6c
AM
14627 if (htab->relbrlt->contents == NULL)
14628 return FALSE;
14629 }
5d1634d7 14630
721956f4
AM
14631 /* Build the stubs as directed by the stub hash table. */
14632 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 14633
a4b6fadd
AM
14634 for (group = htab->group; group != NULL; group = group->next)
14635 if (group->needs_save_res)
7dda8d3c 14636 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 14637
aa8a7074
AM
14638 if (htab->relbrlt != NULL)
14639 htab->relbrlt->reloc_count = 0;
14640
e7d1c40c 14641 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
14642 for (group = htab->group; group != NULL; group = group->next)
14643 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
14644 {
14645 int align = abs (htab->params->plt_stub_align);
14646 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14647 }
794e51c0 14648
7dda8d3c
AM
14649 for (group = htab->group; group != NULL; group = group->next)
14650 if (group->needs_save_res)
14651 {
14652 stub_sec = group->stub_sec;
14653 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14654 htab->sfpr->contents, htab->sfpr->size);
14655 if (htab->params->emit_stub_syms)
14656 {
14657 unsigned int i;
14658
14659 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14660 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14661 return FALSE;
14662 }
14663 }
14664
df136d64
AM
14665 if (htab->glink_eh_frame != NULL
14666 && htab->glink_eh_frame->size != 0)
14667 {
14668 bfd_vma val;
14669 size_t align = 4;
14670
14671 p = htab->glink_eh_frame->contents;
14672 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14673
14674 for (group = htab->group; group != NULL; group = group->next)
14675 if (group->eh_size != 0)
14676 {
14677 /* Offset to stub section. */
14678 val = (group->stub_sec->output_section->vma
14679 + group->stub_sec->output_offset);
14680 val -= (htab->glink_eh_frame->output_section->vma
14681 + htab->glink_eh_frame->output_offset
14682 + (p + 8 - htab->glink_eh_frame->contents));
14683 if (val + 0x80000000 > 0xffffffff)
14684 {
14685 _bfd_error_handler
14686 (_("%s offset too large for .eh_frame sdata4 encoding"),
14687 group->stub_sec->name);
14688 return FALSE;
14689 }
14690 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14691 p += (group->eh_size + 17 + 3) & -4;
14692 }
14693 if (htab->glink != NULL && htab->glink->size != 0)
14694 {
14695 /* Offset to .glink. */
14696 val = (htab->glink->output_section->vma
14697 + htab->glink->output_offset
14698 + 8);
14699 val -= (htab->glink_eh_frame->output_section->vma
14700 + htab->glink_eh_frame->output_offset
14701 + (p + 8 - htab->glink_eh_frame->contents));
14702 if (val + 0x80000000 > 0xffffffff)
14703 {
14704 _bfd_error_handler
14705 (_("%s offset too large for .eh_frame sdata4 encoding"),
14706 htab->glink->name);
14707 return FALSE;
14708 }
14709 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14710 p += (24 + align - 1) & -align;
14711 }
14712 }
14713
d4aaa2a0
AM
14714 for (group = htab->group; group != NULL; group = group->next)
14715 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
14716 {
14717 stub_sec_count += 1;
c9301e31
AM
14718 if (stub_sec->rawsize != stub_sec->size
14719 && (htab->stub_iteration <= STUB_SHRINK_ITER
14720 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
14721 break;
14722 }
5d1634d7 14723
25516cc5 14724 if (group != NULL)
5d1634d7 14725 {
b34976b6 14726 htab->stub_error = TRUE;
cf97bcb0 14727 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 14728 }
721956f4 14729
d2a300cf
AM
14730 if (htab->stub_error)
14731 return FALSE;
14732
14733 if (stats != NULL)
14734 {
988b7300
AM
14735 char *groupmsg;
14736 if (asprintf (&groupmsg,
14737 ngettext ("linker stubs in %u group\n",
14738 "linker stubs in %u groups\n",
14739 stub_sec_count),
14740 stub_sec_count) < 0)
14741 *stats = NULL;
14742 else
14743 {
14744 if (asprintf (stats, _("%s"
14745 " branch %lu\n"
14746 " branch toc adj %lu\n"
14747 " branch notoc %lu\n"
14748 " branch both %lu\n"
14749 " long branch %lu\n"
14750 " long toc adj %lu\n"
14751 " long notoc %lu\n"
14752 " long both %lu\n"
14753 " plt call %lu\n"
14754 " plt call save %lu\n"
14755 " plt call notoc %lu\n"
14756 " plt call both %lu\n"
14757 " global entry %lu"),
14758 groupmsg,
14759 htab->stub_count[ppc_stub_long_branch - 1],
14760 htab->stub_count[ppc_stub_long_branch_r2off - 1],
14761 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14762 htab->stub_count[ppc_stub_long_branch_both - 1],
14763 htab->stub_count[ppc_stub_plt_branch - 1],
14764 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14765 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14766 htab->stub_count[ppc_stub_plt_branch_both - 1],
14767 htab->stub_count[ppc_stub_plt_call - 1],
14768 htab->stub_count[ppc_stub_plt_call_r2save - 1],
14769 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14770 htab->stub_count[ppc_stub_plt_call_both - 1],
14771 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
14772 *stats = NULL;
14773 free (groupmsg);
14774 }
d2a300cf
AM
14775 }
14776 return TRUE;
5bd4f169
AM
14777}
14778
60124e18
AM
14779/* What to do when ld finds relocations against symbols defined in
14780 discarded sections. */
14781
14782static unsigned int
14783ppc64_elf_action_discarded (asection *sec)
14784{
14785 if (strcmp (".opd", sec->name) == 0)
14786 return 0;
14787
14788 if (strcmp (".toc", sec->name) == 0)
14789 return 0;
14790
bce50a28
JJ
14791 if (strcmp (".toc1", sec->name) == 0)
14792 return 0;
14793
60124e18
AM
14794 return _bfd_elf_default_action_discarded (sec);
14795}
14796
e59a1001
AM
14797/* These are the dynamic relocations supported by glibc. */
14798
14799static bfd_boolean
14800ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
14801{
14802 switch (r_type)
14803 {
14804 case R_PPC64_RELATIVE:
14805 case R_PPC64_NONE:
14806 case R_PPC64_ADDR64:
14807 case R_PPC64_GLOB_DAT:
14808 case R_PPC64_IRELATIVE:
14809 case R_PPC64_JMP_IREL:
14810 case R_PPC64_JMP_SLOT:
14811 case R_PPC64_DTPMOD64:
14812 case R_PPC64_DTPREL64:
14813 case R_PPC64_TPREL64:
14814 case R_PPC64_TPREL16_LO_DS:
14815 case R_PPC64_TPREL16_DS:
14816 case R_PPC64_TPREL16:
14817 case R_PPC64_TPREL16_LO:
14818 case R_PPC64_TPREL16_HI:
14819 case R_PPC64_TPREL16_HIGH:
14820 case R_PPC64_TPREL16_HA:
14821 case R_PPC64_TPREL16_HIGHA:
14822 case R_PPC64_TPREL16_HIGHER:
14823 case R_PPC64_TPREL16_HIGHEST:
14824 case R_PPC64_TPREL16_HIGHERA:
14825 case R_PPC64_TPREL16_HIGHESTA:
14826 case R_PPC64_ADDR16_LO_DS:
14827 case R_PPC64_ADDR16_LO:
14828 case R_PPC64_ADDR16_HI:
14829 case R_PPC64_ADDR16_HIGH:
14830 case R_PPC64_ADDR16_HA:
14831 case R_PPC64_ADDR16_HIGHA:
14832 case R_PPC64_REL30:
14833 case R_PPC64_COPY:
14834 case R_PPC64_UADDR64:
14835 case R_PPC64_UADDR32:
14836 case R_PPC64_ADDR32:
14837 case R_PPC64_ADDR24:
14838 case R_PPC64_ADDR16:
14839 case R_PPC64_UADDR16:
14840 case R_PPC64_ADDR16_DS:
14841 case R_PPC64_ADDR16_HIGHER:
14842 case R_PPC64_ADDR16_HIGHEST:
14843 case R_PPC64_ADDR16_HIGHERA:
14844 case R_PPC64_ADDR16_HIGHESTA:
14845 case R_PPC64_ADDR14:
14846 case R_PPC64_ADDR14_BRTAKEN:
14847 case R_PPC64_ADDR14_BRNTAKEN:
14848 case R_PPC64_REL32:
14849 case R_PPC64_REL64:
14850 return TRUE;
14851
14852 default:
14853 return FALSE;
14854 }
14855}
14856
5bd4f169
AM
14857/* The RELOCATE_SECTION function is called by the ELF backend linker
14858 to handle the relocations for a section.
14859
14860 The relocs are always passed as Rela structures; if the section
14861 actually uses Rel structures, the r_addend field will always be
14862 zero.
14863
14864 This function is responsible for adjust the section contents as
14865 necessary, and (if using Rela relocs and generating a
1049f94e 14866 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
14867 necessary.
14868
14869 This function does not have to worry about setting the reloc
14870 address or the reloc symbol index.
14871
14872 LOCAL_SYMS is a pointer to the swapped in local symbols.
14873
14874 LOCAL_SECTIONS is an array giving the section in the input file
14875 corresponding to the st_shndx field of each local symbol.
14876
14877 The global hash table entry for the global symbols can be found
14878 via elf_sym_hashes (input_bfd).
14879
1049f94e 14880 When generating relocatable output, this function must handle
5bd4f169
AM
14881 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14882 going to be the section symbol corresponding to the output
14883 section, which means that the addend must be adjusted
14884 accordingly. */
14885
b34976b6 14886static bfd_boolean
4ce794b7
AM
14887ppc64_elf_relocate_section (bfd *output_bfd,
14888 struct bfd_link_info *info,
14889 bfd *input_bfd,
14890 asection *input_section,
14891 bfd_byte *contents,
14892 Elf_Internal_Rela *relocs,
14893 Elf_Internal_Sym *local_syms,
14894 asection **local_sections)
5bd4f169 14895{
65f38f15 14896 struct ppc_link_hash_table *htab;
5bd4f169
AM
14897 Elf_Internal_Shdr *symtab_hdr;
14898 struct elf_link_hash_entry **sym_hashes;
5bd4f169 14899 Elf_Internal_Rela *rel;
c316a17c 14900 Elf_Internal_Rela *wrel;
5bd4f169 14901 Elf_Internal_Rela *relend;
411e1bfb
AM
14902 Elf_Internal_Rela outrel;
14903 bfd_byte *loc;
411e1bfb 14904 struct got_entry **local_got_ents;
5bd4f169 14905 bfd_vma TOCstart;
b34976b6
AM
14906 bfd_boolean ret = TRUE;
14907 bfd_boolean is_opd;
794e51c0
AM
14908 /* Assume 'at' branch hints. */
14909 bfd_boolean is_isa_v2 = TRUE;
e59a1001 14910 bfd_boolean warned_dynamic = FALSE;
95f0d0d2 14911 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 14912
65f38f15 14913 /* Initialize howto table if needed. */
5bd4f169 14914 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
14915 ppc_howto_init ();
14916
65f38f15 14917 htab = ppc_hash_table (info);
4dfe6ac6
NC
14918 if (htab == NULL)
14919 return FALSE;
ee75fd95
AM
14920
14921 /* Don't relocate stub sections. */
e7d1c40c 14922 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
14923 return TRUE;
14924
7af5d5c4
AM
14925 if (!is_ppc64_elf (input_bfd))
14926 {
14927 bfd_set_error (bfd_error_wrong_format);
14928 return FALSE;
14929 }
0ffa91dd 14930
411e1bfb 14931 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 14932 TOCstart = elf_gp (output_bfd);
0ffa91dd 14933 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 14934 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 14935 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 14936
c316a17c 14937 rel = wrel = relocs;
5bd4f169 14938 relend = relocs + input_section->reloc_count;
c316a17c 14939 for (; rel < relend; wrel++, rel++)
5bd4f169 14940 {
04c9666a 14941 enum elf_ppc64_reloc_type r_type;
31c76678 14942 bfd_vma addend;
5bd4f169
AM
14943 bfd_reloc_status_type r;
14944 Elf_Internal_Sym *sym;
14945 asection *sec;
039b3fef
AM
14946 struct elf_link_hash_entry *h_elf;
14947 struct ppc_link_hash_entry *h;
14948 struct ppc_link_hash_entry *fdh;
5bd4f169 14949 const char *sym_name;
0d4792f7 14950 unsigned long r_symndx, toc_symndx;
3a71aa26 14951 bfd_vma toc_addend;
f961d9dd
AM
14952 unsigned char tls_mask, tls_gd, tls_type;
14953 unsigned char sym_type;
5bd4f169 14954 bfd_vma relocation;
23cedd1d 14955 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 14956 bfd_boolean warned;
bc30df16 14957 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 14958 unsigned int insn;
e11840f9 14959 unsigned int mask;
721956f4
AM
14960 struct ppc_stub_hash_entry *stub_entry;
14961 bfd_vma max_br_offset;
14962 bfd_vma from;
c316a17c 14963 Elf_Internal_Rela orig_rel;
b80eed39
AM
14964 reloc_howto_type *howto;
14965 struct reloc_howto_struct alt_howto;
4a421c53
AM
14966 uint64_t pinsn;
14967 bfd_vma offset;
5bd4f169 14968
c316a17c
AM
14969 again:
14970 orig_rel = *rel;
14971
4ce794b7 14972 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 14973 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
14974
14975 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14976 symbol of the previous ADDR64 reloc. The symbol gives us the
14977 proper TOC base to use. */
14978 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
14979 && wrel != relocs
14980 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 14981 && is_opd)
c316a17c 14982 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 14983
4ce794b7
AM
14984 sym = NULL;
14985 sec = NULL;
039b3fef 14986 h_elf = NULL;
4ce794b7 14987 sym_name = NULL;
b34976b6
AM
14988 unresolved_reloc = FALSE;
14989 warned = FALSE;
65f38f15 14990
0b13192e 14991 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
14992 {
14993 /* It's a local symbol. */
74f0fb50 14994 struct _opd_sec_data *opd;
4025353c 14995
5bd4f169
AM
14996 sym = local_syms + r_symndx;
14997 sec = local_sections[r_symndx];
26c61ae5 14998 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 14999 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 15000 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
15001 opd = get_opd_info (sec);
15002 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 15003 {
51aecdc5
AM
15004 long adjust = opd->adjust[OPD_NDX (sym->st_value
15005 + rel->r_addend)];
4025353c
AM
15006 if (adjust == -1)
15007 relocation = 0;
15008 else
4cc603a5
AM
15009 {
15010 /* If this is a relocation against the opd section sym
15011 and we have edited .opd, adjust the reloc addend so
15012 that ld -r and ld --emit-relocs output is correct.
15013 If it is a reloc against some other .opd symbol,
15014 then the symbol value will be adjusted later. */
15015 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15016 rel->r_addend += adjust;
15017 else
15018 relocation += adjust;
15019 }
1e2f5b6e 15020 }
5bd4f169
AM
15021 }
15022 else
15023 {
62d887d4
L
15024 bfd_boolean ignored;
15025
b2a8e766
AM
15026 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15027 r_symndx, symtab_hdr, sym_hashes,
039b3fef 15028 h_elf, sec, relocation,
62d887d4 15029 unresolved_reloc, warned, ignored);
039b3fef
AM
15030 sym_name = h_elf->root.root.string;
15031 sym_type = h_elf->type;
b69fdb4e
AM
15032 if (sec != NULL
15033 && sec->owner == output_bfd
15034 && strcmp (sec->name, ".opd") == 0)
15035 {
15036 /* This is a symbol defined in a linker script. All
15037 such are defined in output sections, even those
15038 defined by simple assignment from a symbol defined in
15039 an input section. Transfer the symbol to an
15040 appropriate input .opd section, so that a branch to
15041 this symbol will be mapped to the location specified
15042 by the opd entry. */
15043 struct bfd_link_order *lo;
15044 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15045 if (lo->type == bfd_indirect_link_order)
15046 {
15047 asection *isec = lo->u.indirect.section;
15048 if (h_elf->root.u.def.value >= isec->output_offset
15049 && h_elf->root.u.def.value < (isec->output_offset
15050 + isec->size))
15051 {
15052 h_elf->root.u.def.value -= isec->output_offset;
15053 h_elf->root.u.def.section = isec;
15054 sec = isec;
15055 break;
15056 }
15057 }
15058 }
5bd4f169 15059 }
ed7007c1 15060 h = ppc_elf_hash_entry (h_elf);
5bd4f169 15061
dbaa2011 15062 if (sec != NULL && discarded_section (sec))
c316a17c
AM
15063 {
15064 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15065 input_bfd, input_section,
0930cb30 15066 contents, rel->r_offset);
c316a17c
AM
15067 wrel->r_offset = rel->r_offset;
15068 wrel->r_info = 0;
15069 wrel->r_addend = 0;
15070
15071 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 15072 symbols defined in discarded sections. Not done for
c316a17c
AM
15073 non-debug to preserve relocs in .eh_frame which the
15074 eh_frame editing code expects to be present. */
15075 if (bfd_link_relocatable (info)
15076 && (input_section->flags & SEC_DEBUGGING))
15077 wrel--;
15078
15079 continue;
15080 }
ab96bf03 15081
0e1862bb 15082 if (bfd_link_relocatable (info))
c316a17c 15083 goto copy_reloc;
ab96bf03 15084
f40da81b
AM
15085 if (h != NULL && &h->elf == htab->elf.hgot)
15086 {
6f20ed8a 15087 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
15088 sec = bfd_abs_section_ptr;
15089 unresolved_reloc = FALSE;
15090 }
15091
951fd09b
AM
15092 /* TLS optimizations. Replace instruction sequences and relocs
15093 based on information we collected in tls_optimize. We edit
15094 RELOCS so that --emit-relocs will output something sensible
15095 for the final instruction stream. */
15096 tls_mask = 0;
15097 tls_gd = 0;
0d4792f7 15098 toc_symndx = 0;
727fc41e
AM
15099 if (h != NULL)
15100 tls_mask = h->tls_mask;
15101 else if (local_got_ents != NULL)
411e1bfb 15102 {
e054468f
AM
15103 struct plt_entry **local_plt = (struct plt_entry **)
15104 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 15105 unsigned char *lgot_masks = (unsigned char *)
e054468f 15106 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
15107 tls_mask = lgot_masks[r_symndx];
15108 }
37da22e5 15109 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
15110 && (r_type == R_PPC64_TLS
15111 || r_type == R_PPC64_TLSGD
15112 || r_type == R_PPC64_TLSLD))
15113 {
15114 /* Check for toc tls entries. */
f961d9dd 15115 unsigned char *toc_tls;
0d4792f7 15116
727fc41e
AM
15117 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15118 &local_syms, rel, input_bfd))
15119 return FALSE;
0d4792f7 15120
727fc41e
AM
15121 if (toc_tls)
15122 tls_mask = *toc_tls;
0d4792f7
AM
15123 }
15124
15125 /* Check that tls relocs are used with tls syms, and non-tls
15126 relocs are used with non-tls syms. */
cf35638d 15127 if (r_symndx != STN_UNDEF
0d4792f7
AM
15128 && r_type != R_PPC64_NONE
15129 && (h == NULL
039b3fef
AM
15130 || h->elf.root.type == bfd_link_hash_defined
15131 || h->elf.root.type == bfd_link_hash_defweak)
71c4e95a 15132 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
0d4792f7 15133 {
37da22e5 15134 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
15135 && (r_type == R_PPC64_TLS
15136 || r_type == R_PPC64_TLSGD
15137 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
15138 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15139 ;
15140 else
25f53a85 15141 info->callbacks->einfo
1d483afe 15142 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 15143 /* xgettext:c-format */
c1c8c1ef 15144 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 15145 /* xgettext:c-format */
c1c8c1ef 15146 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 15147 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
15148 ppc64_elf_howto_table[r_type]->name,
15149 sym_name);
411e1bfb
AM
15150 }
15151
15152 /* Ensure reloc mapping code below stays sane. */
15153 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15154 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15155 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15156 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15157 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15158 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15159 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15160 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15161 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15162 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15163 abort ();
0d4792f7 15164
411e1bfb
AM
15165 switch (r_type)
15166 {
15167 default:
411e1bfb
AM
15168 break;
15169
ba761f19 15170 case R_PPC64_LO_DS_OPT:
95f0d0d2 15171 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
2365f8d7 15172 if ((insn & (0x3fu << 26)) != 58u << 26)
ba761f19
AM
15173 abort ();
15174 insn += (14u << 26) - (58u << 26);
95f0d0d2 15175 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
15176 r_type = R_PPC64_TOC16_LO;
15177 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15178 break;
15179
411e1bfb
AM
15180 case R_PPC64_TOC16:
15181 case R_PPC64_TOC16_LO:
15182 case R_PPC64_TOC16_DS:
15183 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
15184 {
15185 /* Check for toc tls entries. */
f961d9dd 15186 unsigned char *toc_tls;
951fd09b 15187 int retval;
411e1bfb 15188
3a71aa26
AM
15189 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15190 &local_syms, rel, input_bfd);
951fd09b 15191 if (retval == 0)
411e1bfb
AM
15192 return FALSE;
15193
15194 if (toc_tls)
15195 {
951fd09b 15196 tls_mask = *toc_tls;
411e1bfb
AM
15197 if (r_type == R_PPC64_TOC16_DS
15198 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 15199 {
37da22e5 15200 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
15201 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15202 goto toctprel;
15203 }
411e1bfb 15204 else
951fd09b
AM
15205 {
15206 /* If we found a GD reloc pair, then we might be
15207 doing a GD->IE transition. */
15208 if (retval == 2)
15209 {
b00a0a86 15210 tls_gd = TLS_GDIE;
37da22e5
AM
15211 if ((tls_mask & TLS_TLS) != 0
15212 && (tls_mask & TLS_GD) == 0)
102890f0 15213 goto tls_ldgd_opt;
951fd09b
AM
15214 }
15215 else if (retval == 3)
15216 {
37da22e5
AM
15217 if ((tls_mask & TLS_TLS) != 0
15218 && (tls_mask & TLS_LD) == 0)
102890f0 15219 goto tls_ldgd_opt;
951fd09b
AM
15220 }
15221 }
411e1bfb
AM
15222 }
15223 }
15224 break;
15225
9d6ded02
AM
15226 case R_PPC64_GOT_TPREL16_HI:
15227 case R_PPC64_GOT_TPREL16_HA:
37da22e5 15228 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
15229 && (tls_mask & TLS_TPREL) == 0)
15230 {
15231 rel->r_offset -= d_offset;
95f0d0d2 15232 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
15233 r_type = R_PPC64_NONE;
15234 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15235 }
15236 break;
15237
411e1bfb
AM
15238 case R_PPC64_GOT_TPREL16_DS:
15239 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 15240 if ((tls_mask & TLS_TLS) != 0
951fd09b 15241 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 15242 {
81407a69 15243 toctprel:
95f0d0d2 15244 insn = bfd_get_32 (input_bfd,
c316a17c 15245 contents + rel->r_offset - d_offset);
411e1bfb
AM
15246 insn &= 31 << 21;
15247 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 15248 bfd_put_32 (input_bfd, insn,
c316a17c 15249 contents + rel->r_offset - d_offset);
411e1bfb 15250 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
15251 if (toc_symndx != 0)
15252 {
15253 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 15254 rel->r_addend = toc_addend;
0d4792f7
AM
15255 /* We changed the symbol. Start over in order to
15256 get h, sym, sec etc. right. */
c316a17c 15257 goto again;
0d4792f7
AM
15258 }
15259 else
15260 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15261 }
15262 break;
15263
87c69f97 15264 case R_PPC64_GOT_TPREL_PCREL34:
c213164a
AM
15265 if ((tls_mask & TLS_TLS) != 0
15266 && (tls_mask & TLS_TPREL) == 0)
15267 {
15268 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15269 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15270 pinsn <<= 32;
15271 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15272 pinsn += ((2ULL << 56) + (-1ULL << 52)
15273 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15274 bfd_put_32 (input_bfd, pinsn >> 32,
15275 contents + rel->r_offset);
15276 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15277 contents + rel->r_offset + 4);
15278 r_type = R_PPC64_TPREL34;
15279 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15280 }
15281 break;
15282
411e1bfb 15283 case R_PPC64_TLS:
37da22e5 15284 if ((tls_mask & TLS_TLS) != 0
951fd09b 15285 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 15286 {
c213164a 15287 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2d0f3896
AM
15288 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15289 if (insn == 0)
c213164a
AM
15290 break;
15291 if ((rel->r_offset & 3) == 0)
0d4792f7 15292 {
c213164a
AM
15293 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15294 /* Was PPC64_TLS which sits on insn boundary, now
15295 PPC64_TPREL16_LO which is at low-order half-word. */
15296 rel->r_offset += d_offset;
15297 r_type = R_PPC64_TPREL16_LO;
15298 if (toc_symndx != 0)
15299 {
15300 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15301 rel->r_addend = toc_addend;
15302 /* We changed the symbol. Start over in order to
15303 get h, sym, sec etc. right. */
15304 goto again;
15305 }
15306 else
15307 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15308 }
15309 else if ((rel->r_offset & 3) == 1)
15310 {
15311 /* For pcrel IE to LE we already have the full
15312 offset and thus don't need an addi here. A nop
15313 or mr will do. */
2365f8d7 15314 if ((insn & (0x3fu << 26)) == 14 << 26)
c213164a
AM
15315 {
15316 /* Extract regs from addi rt,ra,si. */
15317 unsigned int rt = (insn >> 21) & 0x1f;
15318 unsigned int ra = (insn >> 16) & 0x1f;
15319 if (rt == ra)
15320 insn = NOP;
15321 else
15322 {
15323 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15324 insn = (rt << 16) | (ra << 21) | (ra << 11);
15325 insn |= (31u << 26) | (444u << 1);
15326 }
15327 }
15328 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
0d4792f7 15329 }
411e1bfb
AM
15330 }
15331 break;
15332
411e1bfb
AM
15333 case R_PPC64_GOT_TLSGD16_HI:
15334 case R_PPC64_GOT_TLSGD16_HA:
b00a0a86 15335 tls_gd = TLS_GDIE;
37da22e5 15336 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
15337 goto tls_gdld_hi;
15338 break;
15339
411e1bfb
AM
15340 case R_PPC64_GOT_TLSLD16_HI:
15341 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 15342 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 15343 {
951fd09b
AM
15344 tls_gdld_hi:
15345 if ((tls_mask & tls_gd) != 0)
15346 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15347 + R_PPC64_GOT_TPREL16_DS);
15348 else
411e1bfb 15349 {
4fe5ca5b 15350 rel->r_offset -= d_offset;
95f0d0d2 15351 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 15352 r_type = R_PPC64_NONE;
411e1bfb 15353 }
951fd09b 15354 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15355 }
15356 break;
15357
951fd09b
AM
15358 case R_PPC64_GOT_TLSGD16:
15359 case R_PPC64_GOT_TLSGD16_LO:
b00a0a86 15360 tls_gd = TLS_GDIE;
37da22e5 15361 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 15362 goto tls_ldgd_opt;
951fd09b 15363 break;
411e1bfb 15364
951fd09b
AM
15365 case R_PPC64_GOT_TLSLD16:
15366 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 15367 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 15368 {
b9f04fe0 15369 unsigned int insn1, insn2;
102890f0
AM
15370
15371 tls_ldgd_opt:
727fc41e
AM
15372 offset = (bfd_vma) -1;
15373 /* If not using the newer R_PPC64_TLSGD/LD to mark
15374 __tls_get_addr calls, we must trust that the call
15375 stays with its arg setup insns, ie. that the next
15376 reloc is the __tls_get_addr call associated with
15377 the current reloc. Edit both insns. */
9737e8af 15378 if (input_section->nomark_tls_get_addr
727fc41e
AM
15379 && rel + 1 < relend
15380 && branch_reloc_hash_match (input_bfd, rel + 1,
9e7028aa
AM
15381 htab->tls_get_addr_fd,
15382 htab->tga_desc_fd,
727fc41e 15383 htab->tls_get_addr,
9e7028aa 15384 htab->tga_desc))
727fc41e 15385 offset = rel[1].r_offset;
b86ac8e3
AM
15386 /* We read the low GOT_TLS (or TOC16) insn because we
15387 need to keep the destination reg. It may be
15388 something other than the usual r3, and moved to r3
15389 before the call by intervening code. */
95f0d0d2 15390 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 15391 contents + rel->r_offset - d_offset);
102890f0 15392 if ((tls_mask & tls_gd) != 0)
411e1bfb 15393 {
102890f0 15394 /* IE */
b86ac8e3 15395 insn1 &= (0x1f << 21) | (0x1f << 16);
2365f8d7 15396 insn1 |= 58u << 26; /* ld */
102890f0 15397 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 15398 if (offset != (bfd_vma) -1)
f58d5a2d 15399 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
46e9995a
AM
15400 if (r_type == R_PPC64_TOC16
15401 || r_type == R_PPC64_TOC16_LO)
102890f0 15402 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
46e9995a
AM
15403 else
15404 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15405 + R_PPC64_GOT_TPREL16_DS);
102890f0
AM
15406 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15407 }
15408 else
15409 {
15410 /* LE */
b86ac8e3
AM
15411 insn1 &= 0x1f << 21;
15412 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
15413 insn2 = 0x38630000; /* addi 3,3,0 */
15414 if (tls_gd == 0)
951fd09b 15415 {
102890f0 15416 /* Was an LD reloc. */
71c4e95a 15417 r_symndx = STN_UNDEF;
102890f0 15418 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 15419 }
102890f0 15420 else if (toc_symndx != 0)
3a71aa26
AM
15421 {
15422 r_symndx = toc_symndx;
15423 rel->r_addend = toc_addend;
15424 }
102890f0
AM
15425 r_type = R_PPC64_TPREL16_HA;
15426 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
15427 if (offset != (bfd_vma) -1)
15428 {
15429 rel[1].r_info = ELF64_R_INFO (r_symndx,
15430 R_PPC64_TPREL16_LO);
15431 rel[1].r_offset = offset + d_offset;
15432 rel[1].r_addend = rel->r_addend;
15433 }
102890f0 15434 }
95f0d0d2 15435 bfd_put_32 (input_bfd, insn1,
3a71aa26 15436 contents + rel->r_offset - d_offset);
727fc41e 15437 if (offset != (bfd_vma) -1)
c96e0573
AM
15438 {
15439 bfd_put_32 (input_bfd, insn2, contents + offset);
15440 if (offset + 8 <= input_section->size)
15441 {
15442 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15443 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15444 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15445 }
15446 }
727fc41e
AM
15447 if ((tls_mask & tls_gd) == 0
15448 && (tls_gd == 0 || toc_symndx != 0))
15449 {
15450 /* We changed the symbol. Start over in order
15451 to get h, sym, sec etc. right. */
c316a17c 15452 goto again;
727fc41e
AM
15453 }
15454 }
15455 break;
15456
87c69f97 15457 case R_PPC64_GOT_TLSGD_PCREL34:
c213164a
AM
15458 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
15459 {
15460 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15461 pinsn <<= 32;
15462 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15463 if ((tls_mask & TLS_GDIE) != 0)
15464 {
15465 /* IE, pla -> pld */
15466 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
87c69f97 15467 r_type = R_PPC64_GOT_TPREL_PCREL34;
c213164a
AM
15468 }
15469 else
15470 {
15471 /* LE, pla pcrel -> paddi r13 */
15472 pinsn += (-1ULL << 52) + (13ULL << 16);
15473 r_type = R_PPC64_TPREL34;
15474 }
15475 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15476 bfd_put_32 (input_bfd, pinsn >> 32,
15477 contents + rel->r_offset);
15478 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15479 contents + rel->r_offset + 4);
15480 }
15481 break;
15482
87c69f97 15483 case R_PPC64_GOT_TLSLD_PCREL34:
c213164a
AM
15484 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
15485 {
15486 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15487 pinsn <<= 32;
15488 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15489 pinsn += (-1ULL << 52) + (13ULL << 16);
15490 bfd_put_32 (input_bfd, pinsn >> 32,
15491 contents + rel->r_offset);
15492 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15493 contents + rel->r_offset + 4);
15494 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15495 r_symndx = STN_UNDEF;
15496 r_type = R_PPC64_TPREL34;
15497 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15498 goto again;
15499 }
15500 break;
15501
727fc41e 15502 case R_PPC64_TLSGD:
37da22e5 15503 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 15504 && rel + 1 < relend)
727fc41e 15505 {
b9f04fe0 15506 unsigned int insn2;
5663e321 15507 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 15508
4a421c53 15509 offset = rel->r_offset;
5663e321 15510 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
15511 {
15512 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
15513 if (r_type1 == R_PPC64_PLT_PCREL34
15514 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15515 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
15516 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15517 break;
15518 }
15519
15520 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15521 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15522
b00a0a86 15523 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
15524 {
15525 /* IE */
15526 r_type = R_PPC64_NONE;
15527 insn2 = 0x7c636a14; /* add 3,3,13 */
15528 }
15529 else
15530 {
15531 /* LE */
15532 if (toc_symndx != 0)
15533 {
15534 r_symndx = toc_symndx;
15535 rel->r_addend = toc_addend;
15536 }
c213164a
AM
15537 if (r_type1 == R_PPC64_REL24_NOTOC
15538 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15539 {
15540 r_type = R_PPC64_NONE;
15541 insn2 = NOP;
15542 }
15543 else
15544 {
15545 rel->r_offset = offset + d_offset;
15546 r_type = R_PPC64_TPREL16_LO;
15547 insn2 = 0x38630000; /* addi 3,3,0 */
15548 }
727fc41e
AM
15549 }
15550 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15551 /* Zap the reloc on the _tls_get_addr call too. */
15552 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 15553 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 15554 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
15555 if ((tls_mask & TLS_GDIE) == 0
15556 && toc_symndx != 0
15557 && r_type != R_PPC64_NONE)
c316a17c 15558 goto again;
411e1bfb 15559 }
411e1bfb
AM
15560 break;
15561
727fc41e 15562 case R_PPC64_TLSLD:
37da22e5 15563 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 15564 && rel + 1 < relend)
727fc41e 15565 {
b9f04fe0 15566 unsigned int insn2;
5663e321 15567 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 15568
4a421c53 15569 offset = rel->r_offset;
5663e321 15570 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
15571 {
15572 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
15573 if (r_type1 == R_PPC64_PLT_PCREL34
15574 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15575 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
15576 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15577 break;
15578 }
15579
15580 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15581 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15582
c213164a
AM
15583 if (r_type1 == R_PPC64_REL24_NOTOC
15584 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15585 {
15586 r_type = R_PPC64_NONE;
15587 insn2 = NOP;
15588 }
15589 else
15590 {
15591 rel->r_offset = offset + d_offset;
15592 r_symndx = STN_UNDEF;
15593 r_type = R_PPC64_TPREL16_LO;
15594 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15595 insn2 = 0x38630000; /* addi 3,3,0 */
15596 }
727fc41e 15597 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
15598 /* Zap the reloc on the _tls_get_addr call too. */
15599 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 15600 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 15601 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
15602 if (r_type != R_PPC64_NONE)
15603 goto again;
727fc41e
AM
15604 }
15605 break;
15606
411e1bfb 15607 case R_PPC64_DTPMOD64:
951fd09b
AM
15608 if (rel + 1 < relend
15609 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
15610 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 15611 {
951fd09b
AM
15612 if ((tls_mask & TLS_GD) == 0)
15613 {
15614 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
b00a0a86 15615 if ((tls_mask & TLS_GDIE) != 0)
951fd09b
AM
15616 r_type = R_PPC64_TPREL64;
15617 else
15618 {
4ce794b7 15619 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
15620 r_type = R_PPC64_NONE;
15621 }
15622 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15623 }
15624 }
15625 else
15626 {
15627 if ((tls_mask & TLS_LD) == 0)
411e1bfb 15628 {
4ce794b7 15629 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 15630 r_type = R_PPC64_NONE;
951fd09b 15631 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 15632 }
411e1bfb
AM
15633 }
15634 break;
15635
15636 case R_PPC64_TPREL64:
951fd09b 15637 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
15638 {
15639 r_type = R_PPC64_NONE;
15640 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15641 }
15642 break;
52a82034 15643
006589cf
AM
15644 case R_PPC64_ENTRY:
15645 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15646 if (!bfd_link_pic (info)
15647 && !info->traditional_format
15648 && relocation + 0x80008000 <= 0xffffffff)
15649 {
15650 unsigned int insn1, insn2;
15651
15652 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15653 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15654 if ((insn1 & ~0xfffc) == LD_R2_0R12
15655 && insn2 == ADD_R2_R2_R12)
15656 {
95f0d0d2 15657 bfd_put_32 (input_bfd,
006589cf
AM
15658 LIS_R2 + PPC_HA (relocation),
15659 contents + rel->r_offset);
95f0d0d2 15660 bfd_put_32 (input_bfd,
006589cf
AM
15661 ADDI_R2_R2 + PPC_LO (relocation),
15662 contents + rel->r_offset + 4);
15663 }
15664 }
15665 else
15666 {
15667 relocation -= (rel->r_offset
15668 + input_section->output_offset
15669 + input_section->output_section->vma);
15670 if (relocation + 0x80008000 <= 0xffffffff)
15671 {
15672 unsigned int insn1, insn2;
15673
15674 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15675 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15676 if ((insn1 & ~0xfffc) == LD_R2_0R12
15677 && insn2 == ADD_R2_R2_R12)
15678 {
95f0d0d2 15679 bfd_put_32 (input_bfd,
006589cf
AM
15680 ADDIS_R2_R12 + PPC_HA (relocation),
15681 contents + rel->r_offset);
95f0d0d2 15682 bfd_put_32 (input_bfd,
006589cf
AM
15683 ADDI_R2_R2 + PPC_LO (relocation),
15684 contents + rel->r_offset + 4);
15685 }
15686 }
15687 }
15688 break;
15689
52a82034
AM
15690 case R_PPC64_REL16_HA:
15691 /* If we are generating a non-PIC executable, edit
15692 . 0: addis 2,12,.TOC.-0b@ha
15693 . addi 2,2,.TOC.-0b@l
15694 used by ELFv2 global entry points to set up r2, to
15695 . lis 2,.TOC.@ha
15696 . addi 2,2,.TOC.@l
15697 if .TOC. is in range. */
0e1862bb 15698 if (!bfd_link_pic (info)
810d4e75 15699 && !info->traditional_format
006589cf 15700 && !htab->opd_abi
4f038ee5 15701 && rel->r_addend == d_offset
52a82034
AM
15702 && h != NULL && &h->elf == htab->elf.hgot
15703 && rel + 1 < relend
15704 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15705 && rel[1].r_offset == rel->r_offset + 4
15706 && rel[1].r_addend == rel->r_addend + 4
15707 && relocation + 0x80008000 <= 0xffffffff)
15708 {
15709 unsigned int insn1, insn2;
4a421c53 15710 offset = rel->r_offset - d_offset;
95f0d0d2
AM
15711 insn1 = bfd_get_32 (input_bfd, contents + offset);
15712 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
15713 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15714 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
15715 {
15716 r_type = R_PPC64_ADDR16_HA;
15717 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15718 rel->r_addend -= d_offset;
15719 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15720 rel[1].r_addend -= d_offset + 4;
95f0d0d2 15721 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
15722 }
15723 }
15724 break;
411e1bfb
AM
15725 }
15726
15727 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 15728 insn = 0;
b25116a9
AM
15729 max_br_offset = 1 << 25;
15730 addend = rel->r_addend;
bc30df16 15731 reloc_dest = DEST_NORMAL;
65f38f15 15732 switch (r_type)
5bd4f169
AM
15733 {
15734 default:
65f38f15 15735 break;
5bd4f169 15736
3b421ab3
AM
15737 case R_PPC64_TOCSAVE:
15738 if (relocation + addend == (rel->r_offset
15739 + input_section->output_offset
15740 + input_section->output_section->vma)
15741 && tocsave_find (htab, NO_INSERT,
15742 &local_syms, rel, input_bfd))
15743 {
15744 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15745 if (insn == NOP
15746 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
15747 bfd_put_32 (input_bfd,
15748 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
15749 contents + rel->r_offset);
15750 }
15751 break;
15752
65f38f15
AM
15753 /* Branch taken prediction relocations. */
15754 case R_PPC64_ADDR14_BRTAKEN:
15755 case R_PPC64_REL14_BRTAKEN:
cedb70c5 15756 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 15757 /* Fall through. */
65f38f15 15758
86c76c7b 15759 /* Branch not taken prediction relocations. */
65f38f15
AM
15760 case R_PPC64_ADDR14_BRNTAKEN:
15761 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 15762 insn |= bfd_get_32 (input_bfd,
411e1bfb 15763 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 15764 /* Fall through. */
86c76c7b 15765
b25116a9
AM
15766 case R_PPC64_REL14:
15767 max_br_offset = 1 << 15;
1a0670f3 15768 /* Fall through. */
5bd4f169 15769
65f38f15 15770 case R_PPC64_REL24:
05d0e962 15771 case R_PPC64_REL24_NOTOC:
23cedd1d 15772 case R_PPC64_PLTCALL:
5663e321 15773 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
15774 /* Calls to functions with a different TOC, such as calls to
15775 shared objects, need to alter the TOC pointer. This is
15776 done using a linkage stub. A REL24 branching to these
15777 linkage stubs needs to be followed by a nop, as the nop
15778 will be replaced with an instruction to restore the TOC
15779 base pointer. */
8387904d 15780 fdh = h;
b31867b6
AM
15781 if (h != NULL
15782 && h->oh != NULL
15783 && h->oh->is_func_descriptor)
15784 fdh = ppc_follow_link (h->oh);
31c76678
DK
15785 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15786 htab);
5663e321
AM
15787 if ((r_type == R_PPC64_PLTCALL
15788 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 15789 && stub_entry != NULL
05d0e962
AM
15790 && stub_entry->stub_type >= ppc_stub_plt_call
15791 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
15792 stub_entry = NULL;
15793
6abec6bc 15794 if (stub_entry != NULL
d0abeec8
AM
15795 && ((stub_entry->stub_type >= ppc_stub_plt_call
15796 && stub_entry->stub_type <= ppc_stub_plt_call_both)
ad8e1ba5 15797 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
15798 || stub_entry->stub_type == ppc_stub_plt_branch_both
15799 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15800 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 15801 {
b25116a9 15802 bfd_boolean can_plt_call = FALSE;
721956f4 15803
6e1816be
AM
15804 if (stub_entry->stub_type == ppc_stub_plt_call
15805 && !htab->opd_abi
15806 && htab->params->plt_localentry0 != 0
9e51d549 15807 && h != NULL
6e1816be
AM
15808 && is_elfv2_localentry0 (&h->elf))
15809 {
15810 /* The function doesn't use or change r2. */
15811 can_plt_call = TRUE;
15812 }
05d0e962
AM
15813 else if (r_type == R_PPC64_REL24_NOTOC)
15814 {
15815 /* NOTOC calls don't need to restore r2. */
15816 can_plt_call = TRUE;
15817 }
6e1816be 15818
f378ab09 15819 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
15820 branch and link followed by a nop. The nop is
15821 replaced by an insn to restore r2. */
6e1816be 15822 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 15823 {
ba8ca3e7
AM
15824 unsigned long br;
15825
15826 br = bfd_get_32 (input_bfd,
15827 contents + rel->r_offset);
15828 if ((br & 1) != 0)
41bd81ab 15829 {
ba8ca3e7
AM
15830 unsigned long nop;
15831
15832 nop = bfd_get_32 (input_bfd,
15833 contents + rel->r_offset + 4);
23cedd1d
AM
15834 if (nop == LD_R2_0R1 + STK_TOC (htab))
15835 can_plt_call = TRUE;
15836 else if (nop == NOP
15837 || nop == CROR_151515
15838 || nop == CROR_313131)
a7f2871e 15839 {
ba8ca3e7 15840 if (h != NULL
ed7007c1 15841 && is_tls_get_addr (&h->elf, htab)
7c9cf415 15842 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
15843 {
15844 /* Special stub used, leave nop alone. */
15845 }
15846 else
a078d95a
AM
15847 bfd_put_32 (input_bfd,
15848 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
15849 contents + rel->r_offset + 4);
15850 can_plt_call = TRUE;
a7f2871e 15851 }
41bd81ab 15852 }
5bd4f169 15853 }
721956f4 15854
ba8ca3e7 15855 if (!can_plt_call && h != NULL)
721956f4 15856 {
ba8ca3e7
AM
15857 const char *name = h->elf.root.root.string;
15858
15859 if (*name == '.')
15860 ++name;
15861
15862 if (strncmp (name, "__libc_start_main", 17) == 0
15863 && (name[17] == 0 || name[17] == '@'))
6ab189d5 15864 {
ba8ca3e7
AM
15865 /* Allow crt1 branch to go via a toc adjusting
15866 stub. Other calls that never return could do
15867 the same, if we could detect such. */
b25116a9 15868 can_plt_call = TRUE;
6ab189d5 15869 }
ba8ca3e7
AM
15870 }
15871
15872 if (!can_plt_call)
15873 {
15874 /* g++ as of 20130507 emits self-calls without a
15875 following nop. This is arguably wrong since we
15876 have conflicting information. On the one hand a
15877 global symbol and on the other a local call
15878 sequence, but don't error for this special case.
15879 It isn't possible to cheaply verify we have
15880 exactly such a call. Allow all calls to the same
15881 section. */
15882 asection *code_sec = sec;
15883
15884 if (get_opd_info (sec) != NULL)
ad8e1ba5 15885 {
ba8ca3e7
AM
15886 bfd_vma off = (relocation + addend
15887 - sec->output_section->vma
15888 - sec->output_offset);
bc30df16 15889
ba8ca3e7 15890 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 15891 }
ba8ca3e7
AM
15892 if (code_sec == input_section)
15893 can_plt_call = TRUE;
15894 }
15895
15896 if (!can_plt_call)
15897 {
05d0e962
AM
15898 if (stub_entry->stub_type >= ppc_stub_plt_call
15899 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 15900 info->callbacks->einfo
695344c0 15901 /* xgettext:c-format */
c1c8c1ef 15902 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15903 "(plt call stub)\n"),
4805fc55
AM
15904 input_bfd, input_section, rel->r_offset, sym_name);
15905 else
15906 info->callbacks->einfo
695344c0 15907 /* xgettext:c-format */
c1c8c1ef 15908 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15909 "(toc save/adjust stub)\n"),
4805fc55 15910 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
15911
15912 bfd_set_error (bfd_error_bad_value);
15913 ret = FALSE;
721956f4
AM
15914 }
15915
b25116a9 15916 if (can_plt_call
05d0e962
AM
15917 && stub_entry->stub_type >= ppc_stub_plt_call
15918 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
15919 unresolved_reloc = FALSE;
15920 }
15921
6abec6bc
AM
15922 if ((stub_entry == NULL
15923 || stub_entry->stub_type == ppc_stub_long_branch
15924 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
15925 && get_opd_info (sec) != NULL)
15926 {
15927 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
15928 bfd_vma off = (relocation + addend
15929 - sec->output_section->vma
15930 - sec->output_offset);
aef36ac1 15931 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
15932 if (dest != (bfd_vma) -1)
15933 {
15934 relocation = dest;
15935 addend = 0;
bc30df16 15936 reloc_dest = DEST_OPD;
8387904d
AM
15937 }
15938 }
15939
b25116a9
AM
15940 /* If the branch is out of reach we ought to have a long
15941 branch stub. */
15942 from = (rel->r_offset
15943 + input_section->output_offset
15944 + input_section->output_section->vma);
15945
6911b7dc
AM
15946 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15947 ? fdh->elf.other
15948 : sym->st_other);
15949
6abec6bc
AM
15950 if (stub_entry != NULL
15951 && (stub_entry->stub_type == ppc_stub_long_branch
15952 || stub_entry->stub_type == ppc_stub_plt_branch)
15953 && (r_type == R_PPC64_ADDR14_BRTAKEN
15954 || r_type == R_PPC64_ADDR14_BRNTAKEN
15955 || (relocation + addend - from + max_br_offset
15956 < 2 * max_br_offset)))
15957 /* Don't use the stub if this branch is in range. */
15958 stub_entry = NULL;
b25116a9 15959
05d0e962
AM
15960 if (stub_entry != NULL
15961 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15962 || stub_entry->stub_type == ppc_stub_long_branch_both
15963 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15964 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15965 && (r_type != R_PPC64_REL24_NOTOC
15966 || ((fdh ? fdh->elf.other : sym->st_other)
4a4e7361 15967 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
05d0e962
AM
15968 && (relocation + addend - from + max_br_offset
15969 < 2 * max_br_offset))
15970 stub_entry = NULL;
15971
15972 if (stub_entry != NULL
15973 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15974 || stub_entry->stub_type == ppc_stub_long_branch_both
15975 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15976 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15977 && r_type == R_PPC64_REL24_NOTOC
15978 && (relocation + addend - from + max_br_offset
15979 < 2 * max_br_offset))
15980 stub_entry = NULL;
15981
b25116a9
AM
15982 if (stub_entry != NULL)
15983 {
15984 /* Munge up the value and addend so that we call the stub
15985 rather than the procedure directly. */
a4b6fadd
AM
15986 asection *stub_sec = stub_entry->group->stub_sec;
15987
15988 if (stub_entry->stub_type == ppc_stub_save_res)
15989 relocation += (stub_sec->output_offset
15990 + stub_sec->output_section->vma
15991 + stub_sec->size - htab->sfpr->size
15992 - htab->sfpr->output_offset
15993 - htab->sfpr->output_section->vma);
15994 else
15995 relocation = (stub_entry->stub_offset
15996 + stub_sec->output_offset
15997 + stub_sec->output_section->vma);
b25116a9 15998 addend = 0;
bc30df16 15999 reloc_dest = DEST_STUB;
3b421ab3 16000
29433886
AM
16001 if ((((stub_entry->stub_type == ppc_stub_plt_call
16002 && ALWAYS_EMIT_R2SAVE)
16003 || stub_entry->stub_type == ppc_stub_plt_call_r2save
16004 || stub_entry->stub_type == ppc_stub_plt_call_both)
16005 && rel + 1 < relend
16006 && rel[1].r_offset == rel->r_offset + 4
16007 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
16008 || ((stub_entry->stub_type == ppc_stub_long_branch_both
16009 || stub_entry->stub_type == ppc_stub_plt_branch_both
16010 || stub_entry->stub_type == ppc_stub_plt_call_both)
16011 && r_type == R_PPC64_REL24_NOTOC))
16012 {
16013 /* Skip over the r2 store at the start of the stub. */
16014 if (!(stub_entry->stub_type >= ppc_stub_plt_call
16015 && htab->params->tls_get_addr_opt
16016 && h != NULL
16017 && is_tls_get_addr (&h->elf, htab)))
16018 relocation += 4;
16019 }
5663e321
AM
16020
16021 if (r_type == R_PPC64_REL24_NOTOC
16022 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
16023 || stub_entry->stub_type == ppc_stub_plt_call_both))
16024 htab->notoc_plt = 1;
b25116a9
AM
16025 }
16026
16027 if (insn != 0)
16028 {
794e51c0 16029 if (is_isa_v2)
721956f4 16030 {
b25116a9
AM
16031 /* Set 'a' bit. This is 0b00010 in BO field for branch
16032 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16033 for branch on CTR insns (BO == 1a00t or 1a01t). */
16034 if ((insn & (0x14 << 21)) == (0x04 << 21))
16035 insn |= 0x02 << 21;
16036 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16037 insn |= 0x08 << 21;
16038 else
16039 break;
16040 }
16041 else
16042 {
16043 /* Invert 'y' bit if not the default. */
4cc603a5 16044 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 16045 insn ^= 0x01 << 21;
721956f4 16046 }
b25116a9 16047
95f0d0d2 16048 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 16049 }
e86ce104 16050
06da1e8e
AM
16051 /* NOP out calls to undefined weak functions.
16052 We can thus call a weak function without first
16053 checking whether the function is defined. */
b25116a9 16054 else if (h != NULL
039b3fef 16055 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 16056 && h->elf.dynindx == -1
05d0e962
AM
16057 && (r_type == R_PPC64_REL24
16058 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 16059 && relocation == 0
4cc603a5 16060 && addend == 0)
e86ce104 16061 {
95f0d0d2 16062 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 16063 goto copy_reloc;
e86ce104 16064 }
65f38f15 16065 break;
066f4018
AM
16066
16067 case R_PPC64_GOT16_DS:
6738c8a7
AM
16068 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16069 || !htab->do_toc_opt)
06507dab 16070 break;
066f4018
AM
16071 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16072 if (relocation + addend - from + 0x8000 < 0x10000
9e51d549 16073 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
066f4018
AM
16074 {
16075 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 16076 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16077 {
16078 insn += (14u << 26) - (58u << 26);
16079 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16080 r_type = R_PPC64_TOC16;
16081 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16082 }
16083 }
16084 break;
16085
16086 case R_PPC64_GOT16_LO_DS:
16087 case R_PPC64_GOT16_HA:
6738c8a7
AM
16088 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16089 || !htab->do_toc_opt)
06507dab 16090 break;
066f4018
AM
16091 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16092 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
9e51d549 16093 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
066f4018
AM
16094 {
16095 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
fba8689a
AM
16096 if (r_type == R_PPC64_GOT16_LO_DS
16097 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16098 {
16099 insn += (14u << 26) - (58u << 26);
16100 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16101 r_type = R_PPC64_TOC16_LO;
16102 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16103 }
fba8689a
AM
16104 else if (r_type == R_PPC64_GOT16_HA
16105 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
066f4018
AM
16106 {
16107 r_type = R_PPC64_TOC16_HA;
16108 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16109 }
16110 }
16111 break;
4a421c53
AM
16112
16113 case R_PPC64_GOT_PCREL34:
6738c8a7
AM
16114 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16115 || !htab->do_toc_opt)
06507dab 16116 break;
4a421c53
AM
16117 from = (rel->r_offset
16118 + input_section->output_section->vma
16119 + input_section->output_offset);
6738c8a7 16120 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
9e51d549 16121 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
6738c8a7
AM
16122 break;
16123
16124 offset = rel->r_offset;
16125 pinsn = bfd_get_32 (input_bfd, contents + offset);
16126 pinsn <<= 32;
16127 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16128 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16129 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16130 break;
16131
16132 /* Replace with paddi. */
16133 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16134 r_type = R_PPC64_PCREL34;
16135 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16136 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16137 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16138 /* Fall through. */
4a421c53
AM
16139
16140 case R_PPC64_PCREL34:
6738c8a7
AM
16141 if (!htab->params->no_pcrel_opt
16142 && rel + 1 < relend
16143 && rel[1].r_offset == rel->r_offset
16144 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
9e51d549 16145 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
4a421c53
AM
16146 {
16147 offset = rel->r_offset;
16148 pinsn = bfd_get_32 (input_bfd, contents + offset);
16149 pinsn <<= 32;
16150 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16151 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16152 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16153 | (14ULL << 26) /* paddi */))
16154 {
6738c8a7
AM
16155 bfd_vma off2 = rel[1].r_addend;
16156 if (off2 == 0)
16157 /* zero means next insn. */
16158 off2 = 8;
16159 off2 += offset;
16160 if (off2 + 4 <= input_section->size)
4a421c53 16161 {
6738c8a7
AM
16162 uint64_t pinsn2;
16163 bfd_signed_vma addend_off;
16164 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16165 pinsn2 <<= 32;
16166 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
4a421c53 16167 {
6738c8a7
AM
16168 if (off2 + 8 > input_section->size)
16169 break;
16170 pinsn2 |= bfd_get_32 (input_bfd,
16171 contents + off2 + 4);
16172 }
16173 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16174 {
16175 addend += addend_off;
16176 rel->r_addend = addend;
16177 bfd_put_32 (input_bfd, pinsn >> 32,
16178 contents + offset);
16179 bfd_put_32 (input_bfd, pinsn,
16180 contents + offset + 4);
16181 bfd_put_32 (input_bfd, pinsn2 >> 32,
16182 contents + off2);
4a421c53 16183 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
6738c8a7
AM
16184 bfd_put_32 (input_bfd, pinsn2,
16185 contents + off2 + 4);
4a421c53
AM
16186 }
16187 }
16188 }
16189 }
16190 break;
65f38f15 16191 }
5bd4f169 16192
411e1bfb 16193 tls_type = 0;
23cedd1d 16194 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
16195 switch (r_type)
16196 {
16197 default:
cf97bcb0
AM
16198 /* xgettext:c-format */
16199 _bfd_error_handler (_("%pB: %s unsupported"),
16200 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 16201
65f38f15 16202 bfd_set_error (bfd_error_bad_value);
b34976b6 16203 ret = FALSE;
c316a17c 16204 goto copy_reloc;
5bd4f169 16205
65f38f15 16206 case R_PPC64_NONE:
411e1bfb 16207 case R_PPC64_TLS:
727fc41e
AM
16208 case R_PPC64_TLSGD:
16209 case R_PPC64_TLSLD:
3b421ab3 16210 case R_PPC64_TOCSAVE:
04c9666a
AM
16211 case R_PPC64_GNU_VTINHERIT:
16212 case R_PPC64_GNU_VTENTRY:
006589cf 16213 case R_PPC64_ENTRY:
4a421c53 16214 case R_PPC64_PCREL_OPT:
c316a17c 16215 goto copy_reloc;
5bd4f169
AM
16216
16217 /* GOT16 relocations. Like an ADDR16 using the symbol's
16218 address in the GOT as relocation value instead of the
411e1bfb 16219 symbol's value itself. Also, create a GOT entry for the
5bd4f169 16220 symbol and put the symbol value there. */
411e1bfb
AM
16221 case R_PPC64_GOT_TLSGD16:
16222 case R_PPC64_GOT_TLSGD16_LO:
16223 case R_PPC64_GOT_TLSGD16_HI:
16224 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 16225 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 16226 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
16227 goto dogot;
16228
16229 case R_PPC64_GOT_TLSLD16:
16230 case R_PPC64_GOT_TLSLD16_LO:
16231 case R_PPC64_GOT_TLSLD16_HI:
16232 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 16233 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 16234 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
16235 goto dogot;
16236
16237 case R_PPC64_GOT_TPREL16_DS:
16238 case R_PPC64_GOT_TPREL16_LO_DS:
16239 case R_PPC64_GOT_TPREL16_HI:
16240 case R_PPC64_GOT_TPREL16_HA:
87c69f97 16241 case R_PPC64_GOT_TPREL_PCREL34:
411e1bfb
AM
16242 tls_type = TLS_TLS | TLS_TPREL;
16243 goto dogot;
16244
16245 case R_PPC64_GOT_DTPREL16_DS:
16246 case R_PPC64_GOT_DTPREL16_LO_DS:
16247 case R_PPC64_GOT_DTPREL16_HI:
16248 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 16249 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
16250 tls_type = TLS_TLS | TLS_DTPREL;
16251 goto dogot;
16252
65f38f15
AM
16253 case R_PPC64_GOT16:
16254 case R_PPC64_GOT16_LO:
16255 case R_PPC64_GOT16_HI:
16256 case R_PPC64_GOT16_HA:
16257 case R_PPC64_GOT16_DS:
16258 case R_PPC64_GOT16_LO_DS:
5663e321 16259 case R_PPC64_GOT_PCREL34:
411e1bfb 16260 dogot:
5bd4f169
AM
16261 {
16262 /* Relocation is to the entry for this symbol in the global
16263 offset table. */
e717da7e 16264 asection *got;
d881513a 16265 bfd_vma *offp;
5bd4f169 16266 bfd_vma off;
d881513a 16267 unsigned long indx = 0;
927be08e 16268 struct got_entry *ent;
65f38f15 16269
d881513a 16270 if (tls_type == (TLS_TLS | TLS_LD)
9e51d549 16271 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
927be08e 16272 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 16273 else
5bd4f169 16274 {
d881513a
AM
16275 if (h != NULL)
16276 {
f0158f44
AM
16277 if (!htab->elf.dynamic_sections_created
16278 || h->elf.dynindx == -1
16279 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 16280 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
16281 /* This is actually a static link, or it is a
16282 -Bsymbolic link and the symbol is defined
16283 locally, or the symbol was forced to be local
16284 because of a version file. */
16285 ;
16286 else
16287 {
039b3fef 16288 indx = h->elf.dynindx;
d881513a
AM
16289 unresolved_reloc = FALSE;
16290 }
039b3fef 16291 ent = h->elf.got.glist;
d881513a 16292 }
411e1bfb 16293 else
5bd4f169 16294 {
d881513a
AM
16295 if (local_got_ents == NULL)
16296 abort ();
16297 ent = local_got_ents[r_symndx];
5bd4f169 16298 }
d881513a
AM
16299
16300 for (; ent != NULL; ent = ent->next)
133a1f60 16301 if (ent->addend == orig_rel.r_addend
e717da7e 16302 && ent->owner == input_bfd
d881513a
AM
16303 && ent->tls_type == tls_type)
16304 break;
5bd4f169 16305 }
411e1bfb 16306
927be08e
AM
16307 if (ent == NULL)
16308 abort ();
16309 if (ent->is_indirect)
16310 ent = ent->got.ent;
16311 offp = &ent->got.offset;
16312 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
16313 if (got == NULL)
16314 abort ();
16315
411e1bfb
AM
16316 /* The offset must always be a multiple of 8. We use the
16317 least significant bit to record whether we have already
16318 processed this entry. */
d881513a 16319 off = *offp;
411e1bfb
AM
16320 if ((off & 1) != 0)
16321 off &= ~1;
5bd4f169
AM
16322 else
16323 {
411e1bfb
AM
16324 /* Generate relocs for the dynamic linker, except in
16325 the case of TLSLD where we'll use one entry per
16326 module. */
25f23106
AM
16327 asection *relgot;
16328 bfd_boolean ifunc;
e717da7e 16329
d881513a 16330 *offp = off | 1;
25f23106
AM
16331 relgot = NULL;
16332 ifunc = (h != NULL
16333 ? h->elf.type == STT_GNU_IFUNC
16334 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 16335 if (ifunc)
82e66161
AM
16336 {
16337 relgot = htab->elf.irelplt;
efb2a7b4 16338 if (indx == 0 || is_static_defined (&h->elf))
cebd6b8a 16339 htab->elf.ifunc_resolvers = TRUE;
82e66161 16340 }
f0158f44
AM
16341 else if (indx != 0
16342 || (bfd_link_pic (info)
16343 && (h == NULL
f749f26e
AM
16344 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16345 && !(tls_type != 0
f15d0b54 16346 && bfd_link_executable (info)
9e51d549
AM
16347 && (h == NULL
16348 || SYMBOL_REFERENCES_LOCAL (info,
16349 &h->elf)))))
19e08130 16350 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 16351 if (relgot != NULL)
5bd4f169 16352 {
e717da7e
AM
16353 outrel.r_offset = (got->output_section->vma
16354 + got->output_offset
411e1bfb 16355 + off);
133a1f60 16356 outrel.r_addend = orig_rel.r_addend;
d881513a 16357 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 16358 {
411e1bfb 16359 outrel.r_addend = 0;
e515b051 16360 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
16361 if (tls_type == (TLS_TLS | TLS_GD))
16362 {
e717da7e
AM
16363 loc = relgot->contents;
16364 loc += (relgot->reloc_count++
d881513a
AM
16365 * sizeof (Elf64_External_Rela));
16366 bfd_elf64_swap_reloca_out (output_bfd,
16367 &outrel, loc);
e515b051 16368 outrel.r_offset += 8;
133a1f60 16369 outrel.r_addend = orig_rel.r_addend;
d881513a
AM
16370 outrel.r_info
16371 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 16372 }
411e1bfb 16373 }
951fd09b 16374 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 16375 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 16376 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 16377 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
16378 else if (indx != 0)
16379 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16380 else
81407a69 16381 {
25f23106
AM
16382 if (ifunc)
16383 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16384 else
16385 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
16386
16387 /* Write the .got section contents for the sake
16388 of prelink. */
e717da7e 16389 loc = got->contents + off;
23fbd6fa
JJ
16390 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16391 loc);
81407a69 16392 }
81407a69
AM
16393
16394 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
16395 {
16396 outrel.r_addend += relocation;
16397 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
16398 {
16399 if (htab->elf.tls_sec == NULL)
16400 outrel.r_addend = 0;
16401 else
16402 outrel.r_addend -= htab->elf.tls_sec->vma;
16403 }
e515b051 16404 }
e717da7e
AM
16405 loc = relgot->contents;
16406 loc += (relgot->reloc_count++
411e1bfb
AM
16407 * sizeof (Elf64_External_Rela));
16408 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16409 }
16410
ad8e1ba5 16411 /* Init the .got section contents here if we're not
81407a69 16412 emitting a reloc. */
d881513a 16413 else
411e1bfb 16414 {
133a1f60 16415 relocation += orig_rel.r_addend;
f0158f44 16416 if (tls_type != 0)
411e1bfb 16417 {
989f9879
AM
16418 if (htab->elf.tls_sec == NULL)
16419 relocation = 0;
16420 else
16421 {
f0158f44
AM
16422 if (tls_type & TLS_LD)
16423 relocation = 0;
16424 else
16425 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 16426 if (tls_type & TLS_TPREL)
989f9879
AM
16427 relocation += DTP_OFFSET - TP_OFFSET;
16428 }
5bd4f169 16429
f0158f44 16430 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
16431 {
16432 bfd_put_64 (output_bfd, relocation,
e717da7e 16433 got->contents + off + 8);
676ee2b5 16434 relocation = 1;
7b609f53 16435 }
411e1bfb
AM
16436 }
16437 bfd_put_64 (output_bfd, relocation,
e717da7e 16438 got->contents + off);
5bd4f169
AM
16439 }
16440 }
16441
65f38f15
AM
16442 if (off >= (bfd_vma) -2)
16443 abort ();
16444
bf102f86 16445 relocation = got->output_section->vma + got->output_offset + off;
133a1f60 16446 addend = 0;
c213164a 16447 if (!(r_type == R_PPC64_GOT_PCREL34
87c69f97
AM
16448 || r_type == R_PPC64_GOT_TLSGD_PCREL34
16449 || r_type == R_PPC64_GOT_TLSLD_PCREL34
16450 || r_type == R_PPC64_GOT_TPREL_PCREL34
16451 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
5663e321 16452 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 16453 }
65f38f15
AM
16454 break;
16455
16456 case R_PPC64_PLT16_HA:
16457 case R_PPC64_PLT16_HI:
16458 case R_PPC64_PLT16_LO:
08be3224 16459 case R_PPC64_PLT16_LO_DS:
5663e321
AM
16460 case R_PPC64_PLT_PCREL34:
16461 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
16462 case R_PPC64_PLT32:
16463 case R_PPC64_PLT64:
23cedd1d 16464 case R_PPC64_PLTSEQ:
5663e321 16465 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 16466 case R_PPC64_PLTCALL:
5663e321 16467 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
16468 /* Relocation is to the entry for this symbol in the
16469 procedure linkage table. */
23cedd1d 16470 unresolved_reloc = TRUE;
cbf95972
AM
16471 {
16472 struct plt_entry **plt_list = NULL;
16473 if (h != NULL)
16474 plt_list = &h->elf.plt.plist;
16475 else if (local_got_ents != NULL)
16476 {
16477 struct plt_entry **local_plt = (struct plt_entry **)
16478 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 16479 plt_list = local_plt + r_symndx;
cbf95972
AM
16480 }
16481 if (plt_list)
16482 {
16483 struct plt_entry *ent;
65f38f15 16484
cbf95972
AM
16485 for (ent = *plt_list; ent != NULL; ent = ent->next)
16486 if (ent->plt.offset != (bfd_vma) -1
133a1f60 16487 && ent->addend == orig_rel.r_addend)
cbf95972
AM
16488 {
16489 asection *plt;
08be3224 16490 bfd_vma got;
cbf95972
AM
16491
16492 plt = htab->elf.splt;
16493 if (!htab->elf.dynamic_sections_created
16494 || h == NULL
16495 || h->elf.dynindx == -1)
2d7ad24e
AM
16496 {
16497 if (h != NULL
16498 ? h->elf.type == STT_GNU_IFUNC
16499 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16500 plt = htab->elf.iplt;
16501 else
16502 plt = htab->pltlocal;
16503 }
16504 relocation = (plt->output_section->vma
16505 + plt->output_offset
16506 + ent->plt.offset);
08be3224 16507 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
16508 || r_type == R_PPC64_PLT16_HI
16509 || r_type == R_PPC64_PLT16_LO
16510 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
16511 {
16512 got = (elf_gp (output_bfd)
16513 + htab->sec_info[input_section->id].toc_off);
16514 relocation -= got;
16515 }
133a1f60 16516 addend = 0;
cbf95972
AM
16517 unresolved_reloc = FALSE;
16518 break;
16519 }
16520 }
16521 }
65f38f15 16522 break;
5bd4f169 16523
0b13192e
AM
16524 case R_PPC64_TOC:
16525 /* Relocation value is TOC base. */
16526 relocation = TOCstart;
cf35638d 16527 if (r_symndx == STN_UNDEF)
6f20ed8a 16528 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
16529 else if (unresolved_reloc)
16530 ;
6f20ed8a
AM
16531 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
16532 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
16533 else
16534 unresolved_reloc = TRUE;
ab96bf03 16535 goto dodyn;
0b13192e 16536
5bd4f169
AM
16537 /* TOC16 relocs. We want the offset relative to the TOC base,
16538 which is the address of the start of the TOC plus 0x8000.
16539 The TOC consists of sections .got, .toc, .tocbss, and .plt,
16540 in this order. */
65f38f15
AM
16541 case R_PPC64_TOC16:
16542 case R_PPC64_TOC16_LO:
16543 case R_PPC64_TOC16_HI:
16544 case R_PPC64_TOC16_DS:
16545 case R_PPC64_TOC16_LO_DS:
16546 case R_PPC64_TOC16_HA:
6f20ed8a 16547 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
1bdd8fac
AM
16548 if (h != NULL)
16549 goto dodyn;
5bd4f169
AM
16550 break;
16551
16552 /* Relocate against the beginning of the section. */
65f38f15
AM
16553 case R_PPC64_SECTOFF:
16554 case R_PPC64_SECTOFF_LO:
16555 case R_PPC64_SECTOFF_HI:
16556 case R_PPC64_SECTOFF_DS:
16557 case R_PPC64_SECTOFF_LO_DS:
16558 case R_PPC64_SECTOFF_HA:
4ce794b7 16559 if (sec != NULL)
65f38f15 16560 addend -= sec->output_section->vma;
5bd4f169
AM
16561 break;
16562
25f23106
AM
16563 case R_PPC64_REL16:
16564 case R_PPC64_REL16_LO:
16565 case R_PPC64_REL16_HI:
16566 case R_PPC64_REL16_HA:
4a969973
AM
16567 case R_PPC64_REL16_HIGH:
16568 case R_PPC64_REL16_HIGHA:
16569 case R_PPC64_REL16_HIGHER:
16570 case R_PPC64_REL16_HIGHERA:
16571 case R_PPC64_REL16_HIGHEST:
16572 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
16573 case R_PPC64_REL16_HIGHER34:
16574 case R_PPC64_REL16_HIGHERA34:
16575 case R_PPC64_REL16_HIGHEST34:
16576 case R_PPC64_REL16_HIGHESTA34:
a680de9a 16577 case R_PPC64_REL16DX_HA:
721956f4
AM
16578 case R_PPC64_REL14:
16579 case R_PPC64_REL14_BRNTAKEN:
16580 case R_PPC64_REL14_BRTAKEN:
5d1634d7 16581 case R_PPC64_REL24:
05d0e962 16582 case R_PPC64_REL24_NOTOC:
5663e321
AM
16583 case R_PPC64_PCREL34:
16584 case R_PPC64_PCREL28:
5d1634d7
AM
16585 break;
16586
411e1bfb
AM
16587 case R_PPC64_TPREL16:
16588 case R_PPC64_TPREL16_LO:
16589 case R_PPC64_TPREL16_HI:
16590 case R_PPC64_TPREL16_HA:
16591 case R_PPC64_TPREL16_DS:
16592 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
16593 case R_PPC64_TPREL16_HIGH:
16594 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
16595 case R_PPC64_TPREL16_HIGHER:
16596 case R_PPC64_TPREL16_HIGHERA:
16597 case R_PPC64_TPREL16_HIGHEST:
16598 case R_PPC64_TPREL16_HIGHESTA:
c213164a 16599 case R_PPC64_TPREL34:
766bc656
AM
16600 if (h != NULL
16601 && h->elf.root.type == bfd_link_hash_undefweak
16602 && h->elf.dynindx == -1)
16603 {
16604 /* Make this relocation against an undefined weak symbol
16605 resolve to zero. This is really just a tweak, since
16606 code using weak externs ought to check that they are
16607 defined before using them. */
16608 bfd_byte *p = contents + rel->r_offset - d_offset;
16609
95f0d0d2 16610 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
16611 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
16612 if (insn != 0)
95f0d0d2 16613 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
16614 break;
16615 }
989f9879
AM
16616 if (htab->elf.tls_sec != NULL)
16617 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
16618 /* The TPREL16 relocs shouldn't really be used in shared
16619 libs or with non-local symbols as that will result in
16620 DT_TEXTREL being set, but support them anyway. */
16621 goto dodyn;
411e1bfb
AM
16622
16623 case R_PPC64_DTPREL16:
16624 case R_PPC64_DTPREL16_LO:
16625 case R_PPC64_DTPREL16_HI:
16626 case R_PPC64_DTPREL16_HA:
16627 case R_PPC64_DTPREL16_DS:
16628 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
16629 case R_PPC64_DTPREL16_HIGH:
16630 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
16631 case R_PPC64_DTPREL16_HIGHER:
16632 case R_PPC64_DTPREL16_HIGHERA:
16633 case R_PPC64_DTPREL16_HIGHEST:
16634 case R_PPC64_DTPREL16_HIGHESTA:
c213164a 16635 case R_PPC64_DTPREL34:
989f9879
AM
16636 if (htab->elf.tls_sec != NULL)
16637 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
16638 break;
16639
45965137
AM
16640 case R_PPC64_ADDR64_LOCAL:
16641 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
16642 ? h->elf.other
16643 : sym->st_other);
16644 break;
16645
e515b051
AM
16646 case R_PPC64_DTPMOD64:
16647 relocation = 1;
16648 addend = 0;
16649 goto dodyn;
16650
411e1bfb 16651 case R_PPC64_TPREL64:
989f9879
AM
16652 if (htab->elf.tls_sec != NULL)
16653 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
16654 goto dodyn;
16655
16656 case R_PPC64_DTPREL64:
989f9879
AM
16657 if (htab->elf.tls_sec != NULL)
16658 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 16659 /* Fall through. */
411e1bfb 16660
65f38f15
AM
16661 /* Relocations that may need to be propagated if this is a
16662 dynamic object. */
04c9666a 16663 case R_PPC64_REL30:
65f38f15
AM
16664 case R_PPC64_REL32:
16665 case R_PPC64_REL64:
16666 case R_PPC64_ADDR14:
16667 case R_PPC64_ADDR14_BRNTAKEN:
16668 case R_PPC64_ADDR14_BRTAKEN:
16669 case R_PPC64_ADDR16:
16670 case R_PPC64_ADDR16_DS:
16671 case R_PPC64_ADDR16_HA:
16672 case R_PPC64_ADDR16_HI:
f9c6b907
AM
16673 case R_PPC64_ADDR16_HIGH:
16674 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
16675 case R_PPC64_ADDR16_HIGHER:
16676 case R_PPC64_ADDR16_HIGHERA:
16677 case R_PPC64_ADDR16_HIGHEST:
16678 case R_PPC64_ADDR16_HIGHESTA:
16679 case R_PPC64_ADDR16_LO:
16680 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
16681 case R_PPC64_ADDR16_HIGHER34:
16682 case R_PPC64_ADDR16_HIGHERA34:
16683 case R_PPC64_ADDR16_HIGHEST34:
16684 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 16685 case R_PPC64_ADDR24:
65f38f15
AM
16686 case R_PPC64_ADDR32:
16687 case R_PPC64_ADDR64:
16688 case R_PPC64_UADDR16:
16689 case R_PPC64_UADDR32:
16690 case R_PPC64_UADDR64:
5663e321
AM
16691 case R_PPC64_D34:
16692 case R_PPC64_D34_LO:
16693 case R_PPC64_D34_HI30:
16694 case R_PPC64_D34_HA30:
16695 case R_PPC64_D28:
411e1bfb 16696 dodyn:
5d1634d7 16697 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
16698 break;
16699
41bd81ab
AM
16700 if (NO_OPD_RELOCS && is_opd)
16701 break;
16702
8a9e8e72 16703 if (bfd_link_pic (info)
b1b07054 16704 ? ((h == NULL
190eb1dd 16705 || h->elf.dyn_relocs != NULL)
b1b07054
AM
16706 && ((h != NULL && pc_dynrelocs (h))
16707 || must_be_dyn_reloc (info, r_type)))
8a9e8e72 16708 : (h != NULL
190eb1dd 16709 ? h->elf.dyn_relocs != NULL
d311bc8b 16710 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 16711 {
b34976b6 16712 bfd_boolean skip, relocate;
65f38f15 16713 asection *sreloc;
1cf1f670 16714 bfd_vma out_off;
82e66161 16715 long indx = 0;
65f38f15
AM
16716
16717 /* When generating a dynamic object, these relocations
16718 are copied into the output file to be resolved at run
16719 time. */
16720
b34976b6
AM
16721 skip = FALSE;
16722 relocate = FALSE;
65f38f15 16723
1cf1f670
AM
16724 out_off = _bfd_elf_section_offset (output_bfd, info,
16725 input_section, rel->r_offset);
16726 if (out_off == (bfd_vma) -1)
b34976b6 16727 skip = TRUE;
1cf1f670 16728 else if (out_off == (bfd_vma) -2)
b34976b6 16729 skip = TRUE, relocate = TRUE;
1cf1f670
AM
16730 out_off += (input_section->output_section->vma
16731 + input_section->output_offset);
16732 outrel.r_offset = out_off;
411e1bfb 16733 outrel.r_addend = rel->r_addend;
65f38f15 16734
1cf1f670
AM
16735 /* Optimize unaligned reloc use. */
16736 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
16737 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
16738 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
16739 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
16740 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
16741 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
16742 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16743 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16744 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16745
65f38f15 16746 if (skip)
0bb2d96a 16747 memset (&outrel, 0, sizeof outrel);
9e51d549
AM
16748 else if (h != NULL
16749 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
16750 && !is_opd
16751 && r_type != R_PPC64_TOC)
14acf4dc 16752 {
82e66161
AM
16753 indx = h->elf.dynindx;
16754 BFD_ASSERT (indx != -1);
16755 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 16756 }
65f38f15
AM
16757 else
16758 {
41bd81ab
AM
16759 /* This symbol is local, or marked to become local,
16760 or this is an opd section reloc which must point
16761 at a local function. */
65f38f15 16762 outrel.r_addend += relocation;
e86ce104 16763 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 16764 {
3fad3c7c 16765 if (is_opd && h != NULL)
afbe61cf
AM
16766 {
16767 /* Lie about opd entries. This case occurs
16768 when building shared libraries and we
16769 reference a function in another shared
3fad3c7c
AM
16770 lib. The same thing happens for a weak
16771 definition in an application that's
16772 overridden by a strong definition in a
16773 shared lib. (I believe this is a generic
16774 bug in binutils handling of weak syms.)
16775 In these cases we won't use the opd
1e2f5b6e 16776 entry in this lib. */
b34976b6 16777 unresolved_reloc = FALSE;
afbe61cf 16778 }
25f23106
AM
16779 if (!is_opd
16780 && r_type == R_PPC64_ADDR64
16781 && (h != NULL
16782 ? h->elf.type == STT_GNU_IFUNC
16783 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16784 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16785 else
16786 {
16787 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 16788
25f23106
AM
16789 /* We need to relocate .opd contents for ld.so.
16790 Prelink also wants simple and consistent rules
16791 for relocs. This make all RELATIVE relocs have
16792 *r_offset equal to r_addend. */
16793 relocate = TRUE;
16794 }
65f38f15
AM
16795 }
16796 else
16797 {
25f23106
AM
16798 if (h != NULL
16799 ? h->elf.type == STT_GNU_IFUNC
16800 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16801 {
25f53a85 16802 info->callbacks->einfo
695344c0 16803 /* xgettext:c-format */
174d0a74 16804 (_("%H: %s for indirect "
c1c8c1ef 16805 "function `%pT' unsupported\n"),
25f53a85 16806 input_bfd, input_section, rel->r_offset,
25f23106
AM
16807 ppc64_elf_howto_table[r_type]->name,
16808 sym_name);
16809 ret = FALSE;
16810 }
cf35638d 16811 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
16812 ;
16813 else if (sec == NULL || sec->owner == NULL)
16814 {
16815 bfd_set_error (bfd_error_bad_value);
b34976b6 16816 return FALSE;
65f38f15
AM
16817 }
16818 else
16819 {
f26a3287 16820 asection *osec = sec->output_section;
65f38f15 16821
f26a3287
AM
16822 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16823 {
16824 /* TLS symbol values are relative to the
16825 TLS segment. Dynamic relocations for
16826 local TLS symbols therefore can't be
16827 reduced to a relocation against their
16828 section symbol because it holds the
16829 address of the section, not a value
16830 relative to the TLS segment. We could
16831 change the .tdata dynamic section symbol
16832 to be zero value but STN_UNDEF works
16833 and is used elsewhere, eg. for TPREL64
16834 GOT relocs against local TLS symbols. */
16835 osec = htab->elf.tls_sec;
16836 indx = 0;
16837 }
16838 else
74541ad4 16839 {
74541ad4 16840 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
16841 if (indx == 0)
16842 {
16843 if ((osec->flags & SEC_READONLY) == 0
16844 && htab->elf.data_index_section != NULL)
16845 osec = htab->elf.data_index_section;
16846 else
16847 osec = htab->elf.text_index_section;
16848 indx = elf_section_data (osec)->dynindx;
16849 }
16850 BFD_ASSERT (indx != 0);
74541ad4 16851 }
74541ad4 16852
65f38f15
AM
16853 /* We are turning this relocation into one
16854 against a section symbol, so subtract out
16855 the output section's address but not the
16856 offset of the input section in the output
16857 section. */
16858 outrel.r_addend -= osec->vma;
16859 }
16860
16861 outrel.r_info = ELF64_R_INFO (indx, r_type);
16862 }
16863 }
16864
16865 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
16866 if (h != NULL
16867 ? h->elf.type == STT_GNU_IFUNC
16868 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
16869 {
16870 sreloc = htab->elf.irelplt;
efb2a7b4 16871 if (indx == 0 || is_static_defined (&h->elf))
cebd6b8a 16872 htab->elf.ifunc_resolvers = TRUE;
82e66161 16873 }
65f38f15
AM
16874 if (sreloc == NULL)
16875 abort ();
16876
dfbb6ac9
AM
16877 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16878 >= sreloc->size)
16879 abort ();
947216bf
AM
16880 loc = sreloc->contents;
16881 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
16882 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16883
e59a1001
AM
16884 if (!warned_dynamic
16885 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
16886 {
16887 info->callbacks->einfo
16888 /* xgettext:c-format */
16889 (_("%X%P: %pB: %s against %pT "
16890 "is not supported by glibc as a dynamic relocation\n"),
16891 input_bfd,
16892 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
16893 sym_name);
16894 warned_dynamic = TRUE;
16895 }
16896
65f38f15
AM
16897 /* If this reloc is against an external symbol, it will
16898 be computed at runtime, so there's no need to do
81407a69
AM
16899 anything now. However, for the sake of prelink ensure
16900 that the section contents are a known value. */
2cdcc330 16901 if (!relocate)
81407a69
AM
16902 {
16903 unresolved_reloc = FALSE;
16904 /* The value chosen here is quite arbitrary as ld.so
16905 ignores section contents except for the special
16906 case of .opd where the contents might be accessed
16907 before relocation. Choose zero, as that won't
16908 cause reloc overflow. */
16909 relocation = 0;
16910 addend = 0;
16911 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16912 to improve backward compatibility with older
16913 versions of ld. */
16914 if (r_type == R_PPC64_ADDR64)
16915 addend = outrel.r_addend;
16916 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 16917 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 16918 addend = outrel.r_offset;
81407a69 16919 }
65f38f15 16920 }
5bd4f169
AM
16921 break;
16922
65f38f15
AM
16923 case R_PPC64_COPY:
16924 case R_PPC64_GLOB_DAT:
16925 case R_PPC64_JMP_SLOT:
25f23106 16926 case R_PPC64_JMP_IREL:
65f38f15
AM
16927 case R_PPC64_RELATIVE:
16928 /* We shouldn't ever see these dynamic relocs in relocatable
16929 files. */
ae9a127f 16930 /* Fall through. */
65f38f15
AM
16931
16932 case R_PPC64_PLTGOT16:
16933 case R_PPC64_PLTGOT16_DS:
16934 case R_PPC64_PLTGOT16_HA:
16935 case R_PPC64_PLTGOT16_HI:
16936 case R_PPC64_PLTGOT16_LO:
16937 case R_PPC64_PLTGOT16_LO_DS:
16938 case R_PPC64_PLTREL32:
16939 case R_PPC64_PLTREL64:
16940 /* These ones haven't been implemented yet. */
16941
25f53a85 16942 info->callbacks->einfo
695344c0 16943 /* xgettext:c-format */
c1c8c1ef 16944 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 16945 input_bfd,
4ce794b7 16946 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
16947
16948 bfd_set_error (bfd_error_invalid_operation);
b34976b6 16949 ret = FALSE;
c316a17c 16950 goto copy_reloc;
65f38f15 16951 }
5bd4f169 16952
67f0cbdb
AM
16953 /* Multi-instruction sequences that access the TOC can be
16954 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 16955 to nop; addi rb,r2,x; */
67f0cbdb
AM
16956 switch (r_type)
16957 {
16958 default:
16959 break;
16960
16961 case R_PPC64_GOT_TLSLD16_HI:
16962 case R_PPC64_GOT_TLSGD16_HI:
16963 case R_PPC64_GOT_TPREL16_HI:
16964 case R_PPC64_GOT_DTPREL16_HI:
16965 case R_PPC64_GOT16_HI:
16966 case R_PPC64_TOC16_HI:
16967 /* These relocs would only be useful if building up an
16968 offset to later add to r2, perhaps in an indexed
16969 addressing mode instruction. Don't try to optimize.
16970 Unfortunately, the possibility of someone building up an
16971 offset like this or even with the HA relocs, means that
16972 we need to check the high insn when optimizing the low
16973 insn. */
16974 break;
16975
5663e321
AM
16976 case R_PPC64_PLTCALL_NOTOC:
16977 if (!unresolved_reloc)
16978 htab->notoc_plt = 1;
16979 /* Fall through. */
23cedd1d
AM
16980 case R_PPC64_PLTCALL:
16981 if (unresolved_reloc)
16982 {
16983 /* No plt entry. Make this into a direct call. */
16984 bfd_byte *p = contents + rel->r_offset;
16985 insn = bfd_get_32 (input_bfd, p);
16986 insn &= 1;
16987 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
16988 if (r_type == R_PPC64_PLTCALL)
16989 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
16990 unresolved_reloc = save_unresolved_reloc;
16991 r_type = R_PPC64_REL24;
16992 }
16993 break;
16994
5663e321 16995 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
16996 case R_PPC64_PLTSEQ:
16997 if (unresolved_reloc)
16998 {
16999 unresolved_reloc = FALSE;
17000 goto nop_it;
17001 }
17002 break;
17003
5663e321
AM
17004 case R_PPC64_PLT_PCREL34_NOTOC:
17005 if (!unresolved_reloc)
17006 htab->notoc_plt = 1;
17007 /* Fall through. */
17008 case R_PPC64_PLT_PCREL34:
17009 if (unresolved_reloc)
17010 {
17011 bfd_byte *p = contents + rel->r_offset;
17012 bfd_put_32 (input_bfd, PNOP >> 32, p);
17013 bfd_put_32 (input_bfd, PNOP, p + 4);
17014 unresolved_reloc = FALSE;
17015 goto copy_reloc;
17016 }
17017 break;
17018
23cedd1d
AM
17019 case R_PPC64_PLT16_HA:
17020 if (unresolved_reloc)
17021 {
17022 unresolved_reloc = FALSE;
17023 goto nop_it;
17024 }
17025 /* Fall through. */
67f0cbdb
AM
17026 case R_PPC64_GOT_TLSLD16_HA:
17027 case R_PPC64_GOT_TLSGD16_HA:
17028 case R_PPC64_GOT_TPREL16_HA:
17029 case R_PPC64_GOT_DTPREL16_HA:
17030 case R_PPC64_GOT16_HA:
17031 case R_PPC64_TOC16_HA:
98528052 17032 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 17033 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 17034 {
23cedd1d
AM
17035 bfd_byte *p;
17036 nop_it:
17037 p = contents + (rel->r_offset & ~3);
98528052 17038 bfd_put_32 (input_bfd, NOP, p);
d830549d 17039 goto copy_reloc;
98528052 17040 }
67f0cbdb
AM
17041 break;
17042
23cedd1d
AM
17043 case R_PPC64_PLT16_LO:
17044 case R_PPC64_PLT16_LO_DS:
17045 if (unresolved_reloc)
17046 {
17047 unresolved_reloc = FALSE;
17048 goto nop_it;
17049 }
17050 /* Fall through. */
67f0cbdb
AM
17051 case R_PPC64_GOT_TLSLD16_LO:
17052 case R_PPC64_GOT_TLSGD16_LO:
17053 case R_PPC64_GOT_TPREL16_LO_DS:
17054 case R_PPC64_GOT_DTPREL16_LO_DS:
17055 case R_PPC64_GOT16_LO:
17056 case R_PPC64_GOT16_LO_DS:
17057 case R_PPC64_TOC16_LO:
17058 case R_PPC64_TOC16_LO_DS:
98528052 17059 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 17060 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
17061 {
17062 bfd_byte *p = contents + (rel->r_offset & ~3);
17063 insn = bfd_get_32 (input_bfd, p);
2365f8d7 17064 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
560c8763
AM
17065 {
17066 /* Transform addic to addi when we change reg. */
2365f8d7 17067 insn &= ~((0x3fu << 26) | (0x1f << 16));
560c8763
AM
17068 insn |= (14u << 26) | (2 << 16);
17069 }
17070 else
67f0cbdb 17071 {
98528052
AM
17072 insn &= ~(0x1f << 16);
17073 insn |= 2 << 16;
67f0cbdb 17074 }
560c8763 17075 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
17076 }
17077 break;
9a23f96e
AM
17078
17079 case R_PPC64_TPREL16_HA:
17080 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
17081 {
17082 bfd_byte *p = contents + (rel->r_offset & ~3);
252dcdf4
AM
17083 bfd_put_32 (input_bfd, NOP, p);
17084 goto copy_reloc;
9a23f96e
AM
17085 }
17086 break;
17087
17088 case R_PPC64_TPREL16_LO:
17089 case R_PPC64_TPREL16_LO_DS:
17090 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
17091 {
17092 bfd_byte *p = contents + (rel->r_offset & ~3);
17093 insn = bfd_get_32 (input_bfd, p);
17094 insn &= ~(0x1f << 16);
17095 insn |= 13 << 16;
17096 bfd_put_32 (input_bfd, insn, p);
17097 }
17098 break;
67f0cbdb
AM
17099 }
17100
65f38f15
AM
17101 /* Do any further special processing. */
17102 switch (r_type)
17103 {
17104 default:
17105 break;
17106
25f23106 17107 case R_PPC64_REL16_HA:
4a969973
AM
17108 case R_PPC64_REL16_HIGHA:
17109 case R_PPC64_REL16_HIGHERA:
17110 case R_PPC64_REL16_HIGHESTA:
a680de9a 17111 case R_PPC64_REL16DX_HA:
f9c6b907
AM
17112 case R_PPC64_ADDR16_HA:
17113 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
17114 case R_PPC64_ADDR16_HIGHERA:
17115 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
17116 case R_PPC64_TOC16_HA:
17117 case R_PPC64_SECTOFF_HA:
411e1bfb 17118 case R_PPC64_TPREL16_HA:
f9c6b907 17119 case R_PPC64_TPREL16_HIGHA:
411e1bfb 17120 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 17121 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
17122 case R_PPC64_DTPREL16_HA:
17123 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 17124 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 17125 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
17126 /* It's just possible that this symbol is a weak symbol
17127 that's not actually defined anywhere. In that case,
17128 'sec' would be NULL, and we should leave the symbol
17129 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
17130 if (sec == NULL)
17131 break;
1a0670f3 17132 /* Fall through. */
5c5f6e17
AM
17133
17134 case R_PPC64_GOT16_HA:
17135 case R_PPC64_PLTGOT16_HA:
17136 case R_PPC64_PLT16_HA:
17137 case R_PPC64_GOT_TLSGD16_HA:
17138 case R_PPC64_GOT_TLSLD16_HA:
17139 case R_PPC64_GOT_TPREL16_HA:
17140 case R_PPC64_GOT_DTPREL16_HA:
17141 /* Add 0x10000 if sign bit in 0:15 is set.
17142 Bits 0:15 are not used. */
17143 addend += 0x8000;
65f38f15
AM
17144 break;
17145
5663e321
AM
17146 case R_PPC64_D34_HA30:
17147 case R_PPC64_ADDR16_HIGHERA34:
17148 case R_PPC64_ADDR16_HIGHESTA34:
17149 case R_PPC64_REL16_HIGHERA34:
17150 case R_PPC64_REL16_HIGHESTA34:
17151 if (sec != NULL)
17152 addend += 1ULL << 33;
17153 break;
17154
65f38f15
AM
17155 case R_PPC64_ADDR16_DS:
17156 case R_PPC64_ADDR16_LO_DS:
17157 case R_PPC64_GOT16_DS:
17158 case R_PPC64_GOT16_LO_DS:
17159 case R_PPC64_PLT16_LO_DS:
17160 case R_PPC64_SECTOFF_DS:
17161 case R_PPC64_SECTOFF_LO_DS:
17162 case R_PPC64_TOC16_DS:
17163 case R_PPC64_TOC16_LO_DS:
17164 case R_PPC64_PLTGOT16_DS:
17165 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
17166 case R_PPC64_GOT_TPREL16_DS:
17167 case R_PPC64_GOT_TPREL16_LO_DS:
17168 case R_PPC64_GOT_DTPREL16_DS:
17169 case R_PPC64_GOT_DTPREL16_LO_DS:
17170 case R_PPC64_TPREL16_DS:
17171 case R_PPC64_TPREL16_LO_DS:
17172 case R_PPC64_DTPREL16_DS:
17173 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
17174 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17175 mask = 3;
a680de9a
PB
17176 /* If this reloc is against an lq, lxv, or stxv insn, then
17177 the value must be a multiple of 16. This is somewhat of
17178 a hack, but the "correct" way to do this by defining _DQ
17179 forms of all the _DS relocs bloats all reloc switches in
17180 this file. It doesn't make much sense to use these
17181 relocs in data, so testing the insn should be safe. */
2365f8d7
AM
17182 if ((insn & (0x3fu << 26)) == (56u << 26)
17183 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 17184 mask = 15;
a680de9a
PB
17185 relocation += addend;
17186 addend = insn & (mask ^ 3);
17187 if ((relocation & mask) != 0)
65f38f15 17188 {
a680de9a 17189 relocation ^= relocation & mask;
25f53a85 17190 info->callbacks->einfo
695344c0 17191 /* xgettext:c-format */
174d0a74 17192 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 17193 input_bfd, input_section, rel->r_offset,
d830549d 17194 ppc64_elf_howto_table[r_type]->name,
adadcc0c 17195 mask + 1);
65f38f15 17196 bfd_set_error (bfd_error_bad_value);
b34976b6 17197 ret = FALSE;
c316a17c 17198 goto copy_reloc;
65f38f15
AM
17199 }
17200 break;
5bd4f169
AM
17201 }
17202
239e1f3a
AM
17203 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17204 because such sections are not SEC_ALLOC and thus ld.so will
17205 not process them. */
d830549d 17206 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 17207 if (unresolved_reloc
239e1f3a 17208 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
17209 && h->elf.def_dynamic)
17210 && _bfd_elf_section_offset (output_bfd, info, input_section,
17211 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 17212 {
25f53a85 17213 info->callbacks->einfo
695344c0 17214 /* xgettext:c-format */
c1c8c1ef 17215 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 17216 input_bfd, input_section, rel->r_offset,
b80eed39 17217 howto->name,
039b3fef 17218 h->elf.root.root.string);
b34976b6 17219 ret = FALSE;
9c07fe7c 17220 }
5bd4f169 17221
b80eed39
AM
17222 /* 16-bit fields in insns mostly have signed values, but a
17223 few insns have 16-bit unsigned values. Really, we should
17224 have different reloc types. */
17225 if (howto->complain_on_overflow != complain_overflow_dont
17226 && howto->dst_mask == 0xffff
17227 && (input_section->flags & SEC_CODE) != 0)
17228 {
17229 enum complain_overflow complain = complain_overflow_signed;
17230
17231 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 17232 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
a47622ac
AM
17233 complain = complain_overflow_bitfield;
17234 else if (howto->rightshift == 0
2365f8d7
AM
17235 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17236 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17237 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17238 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17239 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17240 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
17241 complain = complain_overflow_unsigned;
17242 if (howto->complain_on_overflow != complain)
17243 {
17244 alt_howto = *howto;
17245 alt_howto.complain_on_overflow = complain;
17246 howto = &alt_howto;
17247 }
17248 }
17249
5663e321 17250 switch (r_type)
a680de9a 17251 {
5663e321
AM
17252 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17253 case R_PPC64_D34:
17254 case R_PPC64_D34_LO:
17255 case R_PPC64_D34_HI30:
17256 case R_PPC64_D34_HA30:
17257 case R_PPC64_PCREL34:
17258 case R_PPC64_GOT_PCREL34:
c213164a
AM
17259 case R_PPC64_TPREL34:
17260 case R_PPC64_DTPREL34:
87c69f97
AM
17261 case R_PPC64_GOT_TLSGD_PCREL34:
17262 case R_PPC64_GOT_TLSLD_PCREL34:
17263 case R_PPC64_GOT_TPREL_PCREL34:
17264 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
17265 case R_PPC64_PLT_PCREL34:
17266 case R_PPC64_PLT_PCREL34_NOTOC:
17267 case R_PPC64_D28:
17268 case R_PPC64_PCREL28:
17269 if (rel->r_offset + 8 > input_section->size)
17270 r = bfd_reloc_outofrange;
17271 else
17272 {
5663e321
AM
17273 relocation += addend;
17274 if (howto->pc_relative)
17275 relocation -= (rel->r_offset
17276 + input_section->output_offset
17277 + input_section->output_section->vma);
17278 relocation >>= howto->rightshift;
17279
17280 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17281 pinsn <<= 32;
17282 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17283
17284 pinsn &= ~howto->dst_mask;
17285 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17286 & howto->dst_mask);
17287 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17288 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17289 r = bfd_reloc_ok;
17290 if (howto->complain_on_overflow == complain_overflow_signed
17291 && (relocation + (1ULL << (howto->bitsize - 1))
17292 >= 1ULL << howto->bitsize))
17293 r = bfd_reloc_overflow;
17294 }
17295 break;
17296
17297 case R_PPC64_REL16DX_HA:
a680de9a
PB
17298 if (rel->r_offset + 4 > input_section->size)
17299 r = bfd_reloc_outofrange;
17300 else
17301 {
17302 relocation += addend;
17303 relocation -= (rel->r_offset
17304 + input_section->output_offset
17305 + input_section->output_section->vma);
3de43e7b 17306 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
17307 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17308 insn &= ~0x1fffc1;
3de43e7b 17309 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
17310 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17311 r = bfd_reloc_ok;
3de43e7b 17312 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
17313 r = bfd_reloc_overflow;
17314 }
5663e321
AM
17315 break;
17316
17317 default:
17318 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17319 contents, rel->r_offset,
17320 relocation, addend);
a680de9a 17321 }
5bd4f169 17322
ef60b7ff 17323 if (r != bfd_reloc_ok)
5bd4f169 17324 {
bc30df16 17325 char *more_info = NULL;
b80eed39 17326 const char *reloc_name = howto->name;
bc30df16
AM
17327
17328 if (reloc_dest != DEST_NORMAL)
17329 {
17330 more_info = bfd_malloc (strlen (reloc_name) + 8);
17331 if (more_info != NULL)
17332 {
17333 strcpy (more_info, reloc_name);
17334 strcat (more_info, (reloc_dest == DEST_OPD
17335 ? " (OPD)" : " (stub)"));
17336 reloc_name = more_info;
17337 }
17338 }
17339
cd27b276 17340 if (r == bfd_reloc_overflow)
5bd4f169 17341 {
8131c122
AM
17342 /* On code like "if (foo) foo();" don't report overflow
17343 on a branch to zero when foo is undefined. */
17344 if (!warned
17345 && (reloc_dest == DEST_STUB
17346 || !(h != NULL
17347 && (h->elf.root.type == bfd_link_hash_undefweak
17348 || h->elf.root.type == bfd_link_hash_undefined)
17349 && is_branch_reloc (r_type))))
9e51d549
AM
17350 info->callbacks->reloc_overflow
17351 (info, (struct bfd_link_hash_entry *) h, sym_name,
17352 reloc_name, orig_rel.r_addend, input_bfd, input_section,
17353 rel->r_offset);
ef60b7ff
AM
17354 }
17355 else
17356 {
25f53a85 17357 info->callbacks->einfo
695344c0 17358 /* xgettext:c-format */
c1c8c1ef 17359 (_("%H: %s against `%pT': error %d\n"),
25f53a85 17360 input_bfd, input_section, rel->r_offset,
bc30df16 17361 reloc_name, sym_name, (int) r);
b34976b6 17362 ret = FALSE;
ef60b7ff 17363 }
c9594989 17364 free (more_info);
5bd4f169 17365 }
c316a17c
AM
17366 copy_reloc:
17367 if (wrel != rel)
17368 *wrel = *rel;
17369 }
17370
17371 if (wrel != rel)
17372 {
17373 Elf_Internal_Shdr *rel_hdr;
17374 size_t deleted = rel - wrel;
17375
17376 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17377 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17378 if (rel_hdr->sh_size == 0)
17379 {
17380 /* It is too late to remove an empty reloc section. Leave
17381 one NONE reloc.
17382 ??? What is wrong with an empty section??? */
17383 rel_hdr->sh_size = rel_hdr->sh_entsize;
17384 deleted -= 1;
17385 }
17386 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
17387 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17388 input_section->reloc_count -= deleted;
5bd4f169
AM
17389 }
17390
645ea6a9
AM
17391 /* If we're emitting relocations, then shortly after this function
17392 returns, reloc offsets and addends for this section will be
17393 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
17394 file rather than the input. Save a copy of the relocs for
17395 opd_entry_value. */
0e1862bb 17396 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
17397 {
17398 bfd_size_type amt;
17399 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
17400 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
17401 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
17402 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
17403 if (rel == NULL)
17404 return FALSE;
17405 memcpy (rel, relocs, amt);
17406 }
5bd4f169
AM
17407 return ret;
17408}
17409
754021d0
AM
17410/* Adjust the value of any local symbols in opd sections. */
17411
6e0b88f1 17412static int
754021d0
AM
17413ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
17414 const char *name ATTRIBUTE_UNUSED,
17415 Elf_Internal_Sym *elfsym,
17416 asection *input_sec,
17417 struct elf_link_hash_entry *h)
17418{
74f0fb50
AM
17419 struct _opd_sec_data *opd;
17420 long adjust;
754021d0
AM
17421 bfd_vma value;
17422
4025353c 17423 if (h != NULL)
6e0b88f1 17424 return 1;
4025353c 17425
74f0fb50
AM
17426 opd = get_opd_info (input_sec);
17427 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 17428 return 1;
754021d0
AM
17429
17430 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 17431 if (!bfd_link_relocatable (info))
754021d0
AM
17432 value -= input_sec->output_section->vma;
17433
51aecdc5 17434 adjust = opd->adjust[OPD_NDX (value)];
4025353c 17435 if (adjust == -1)
6e0b88f1
AM
17436 return 2;
17437
17438 elfsym->st_value += adjust;
17439 return 1;
754021d0
AM
17440}
17441
5bd4f169
AM
17442/* Finish up dynamic symbol handling. We set the contents of various
17443 dynamic sections here. */
17444
b34976b6 17445static bfd_boolean
4ce794b7
AM
17446ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
17447 struct bfd_link_info *info,
17448 struct elf_link_hash_entry *h,
4aef7643 17449 Elf_Internal_Sym *sym)
5bd4f169 17450{
65f38f15 17451 struct ppc_link_hash_table *htab;
8387904d 17452 struct plt_entry *ent;
5bd4f169 17453
65f38f15 17454 htab = ppc_hash_table (info);
4dfe6ac6
NC
17455 if (htab == NULL)
17456 return FALSE;
5bd4f169 17457
49c09209
AM
17458 if (!htab->opd_abi && !h->def_regular)
17459 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
17460 if (ent->plt.offset != (bfd_vma) -1)
17461 {
17462 /* Mark the symbol as undefined, rather than as
17463 defined in glink. Leave the value if there were
17464 any relocations where pointer equality matters
17465 (this is a clue for the dynamic linker, to make
17466 function pointer comparisons work between an
17467 application and shared library), otherwise set it
17468 to zero. */
17469 sym->st_shndx = SHN_UNDEF;
17470 if (!h->pointer_equality_needed)
17471 sym->st_value = 0;
17472 else if (!h->ref_regular_nonweak)
17473 {
17474 /* This breaks function pointer comparisons, but
17475 that is better than breaking tests for a NULL
17476 function pointer. */
17477 sym->st_value = 0;
17478 }
17479 break;
17480 }
5bd4f169 17481
1bdd8fac
AM
17482 if (h->needs_copy
17483 && (h->root.type == bfd_link_hash_defined
17484 || h->root.type == bfd_link_hash_defweak)
17485 && (h->root.u.def.section == htab->elf.sdynbss
17486 || h->root.u.def.section == htab->elf.sdynrelro))
5bd4f169 17487 {
65f38f15 17488 /* This symbol needs a copy reloc. Set it up. */
49c09209 17489 Elf_Internal_Rela rela;
5474d94f 17490 asection *srel;
49c09209 17491 bfd_byte *loc;
5bd4f169 17492
1bdd8fac 17493 if (h->dynindx == -1)
65f38f15 17494 abort ();
5bd4f169 17495
ed7007c1 17496 rela.r_offset = defined_sym_val (h);
5bd4f169
AM
17497 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
17498 rela.r_addend = 0;
afbf7e8e 17499 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
17500 srel = htab->elf.sreldynrelro;
17501 else
17502 srel = htab->elf.srelbss;
17503 loc = srel->contents;
17504 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 17505 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
17506 }
17507
b34976b6 17508 return TRUE;
5bd4f169
AM
17509}
17510
65f38f15
AM
17511/* Used to decide how to sort relocs in an optimal manner for the
17512 dynamic linker, before writing them out. */
17513
17514static enum elf_reloc_type_class
7e612e98
AM
17515ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
17516 const asection *rel_sec,
17517 const Elf_Internal_Rela *rela)
65f38f15 17518{
04c9666a 17519 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
17520 struct ppc_link_hash_table *htab = ppc_hash_table (info);
17521
33e44f2e 17522 if (rel_sec == htab->elf.irelplt)
7e612e98 17523 return reloc_class_ifunc;
a33d1f77 17524
4ce794b7 17525 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 17526 switch (r_type)
65f38f15
AM
17527 {
17528 case R_PPC64_RELATIVE:
17529 return reloc_class_relative;
17530 case R_PPC64_JMP_SLOT:
17531 return reloc_class_plt;
17532 case R_PPC64_COPY:
17533 return reloc_class_copy;
17534 default:
17535 return reloc_class_normal;
17536 }
17537}
17538
5bd4f169
AM
17539/* Finish up the dynamic sections. */
17540
b34976b6 17541static bfd_boolean
4ce794b7
AM
17542ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
17543 struct bfd_link_info *info)
5bd4f169 17544{
65f38f15
AM
17545 struct ppc_link_hash_table *htab;
17546 bfd *dynobj;
5bd4f169 17547 asection *sdyn;
5bd4f169 17548
65f38f15 17549 htab = ppc_hash_table (info);
4dfe6ac6
NC
17550 if (htab == NULL)
17551 return FALSE;
17552
65f38f15 17553 dynobj = htab->elf.dynobj;
3d4d4302 17554 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 17555
65f38f15 17556 if (htab->elf.dynamic_sections_created)
5bd4f169 17557 {
5bd4f169
AM
17558 Elf64_External_Dyn *dyncon, *dynconend;
17559
33e44f2e 17560 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 17561 abort ();
5bd4f169
AM
17562
17563 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 17564 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
17565 for (; dyncon < dynconend; dyncon++)
17566 {
17567 Elf_Internal_Dyn dyn;
19397422 17568 asection *s;
5bd4f169
AM
17569
17570 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
17571
17572 switch (dyn.d_tag)
17573 {
65f38f15
AM
17574 default:
17575 continue;
5bd4f169 17576
5d1634d7 17577 case DT_PPC64_GLINK:
4ce794b7 17578 s = htab->glink;
6348e046 17579 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
17580 /* We stupidly defined DT_PPC64_GLINK to be the start
17581 of glink rather than the first entry point, which is
17582 what ld.so needs, and now have a bigger stub to
17583 support automatic multiple TOCs. */
9e390558 17584 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
17585 break;
17586
19397422
AM
17587 case DT_PPC64_OPD:
17588 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
17589 if (s == NULL)
17590 continue;
17591 dyn.d_un.d_ptr = s->vma;
19397422
AM
17592 break;
17593
e8910a83 17594 case DT_PPC64_OPT:
5663e321
AM
17595 if ((htab->do_multi_toc && htab->multi_toc_needed)
17596 || htab->notoc_plt)
e8910a83 17597 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
17598 if (htab->has_plt_localentry0)
17599 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
17600 break;
17601
19397422
AM
17602 case DT_PPC64_OPDSZ:
17603 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
17604 if (s == NULL)
17605 continue;
eea6121a 17606 dyn.d_un.d_val = s->size;
19397422
AM
17607 break;
17608
65f38f15 17609 case DT_PLTGOT:
33e44f2e 17610 s = htab->elf.splt;
6348e046 17611 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
17612 break;
17613
17614 case DT_JMPREL:
33e44f2e 17615 s = htab->elf.srelplt;
6348e046 17616 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 17617 break;
5bd4f169 17618
65f38f15 17619 case DT_PLTRELSZ:
33e44f2e 17620 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 17621 break;
82e66161
AM
17622
17623 case DT_TEXTREL:
cebd6b8a 17624 if (htab->elf.ifunc_resolvers)
82e66161
AM
17625 info->callbacks->einfo
17626 (_("%P: warning: text relocations and GNU indirect "
17627 "functions may result in a segfault at runtime\n"));
17628 continue;
5bd4f169 17629 }
5bd4f169 17630
65f38f15 17631 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 17632 }
5bd4f169
AM
17633 }
17634
6528b6eb
AM
17635 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
17636 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
17637 {
17638 /* Fill in the first entry in the global offset table.
17639 We use it to hold the link-time TOCbase. */
17640 bfd_put_64 (output_bfd,
60ee0d4a 17641 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 17642 htab->elf.sgot->contents);
5d1634d7
AM
17643
17644 /* Set .got entry size. */
2cdcc330
AM
17645 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
17646 = 8;
5d1634d7
AM
17647 }
17648
6528b6eb
AM
17649 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
17650 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
17651 {
17652 /* Set .plt entry size. */
33e44f2e 17653 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 17654 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
17655 }
17656
84f5d08e
AM
17657 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
17658 brlt ourselves if emitrelocations. */
17659 if (htab->brlt != NULL
17660 && htab->brlt->reloc_count != 0
17661 && !_bfd_elf_link_output_relocs (output_bfd,
17662 htab->brlt,
d4730f92 17663 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
17664 elf_section_data (htab->brlt)->relocs,
17665 NULL))
17666 return FALSE;
17667
176a0d42
AM
17668 if (htab->glink != NULL
17669 && htab->glink->reloc_count != 0
17670 && !_bfd_elf_link_output_relocs (output_bfd,
17671 htab->glink,
d4730f92 17672 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
17673 elf_section_data (htab->glink)->relocs,
17674 NULL))
17675 return FALSE;
17676
da44f4e5 17677
df136d64
AM
17678 if (htab->glink_eh_frame != NULL
17679 && htab->glink_eh_frame->size != 0
17680 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
17681 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
17682 htab->glink_eh_frame,
17683 htab->glink_eh_frame->contents))
17684 return FALSE;
58d180e8 17685
e717da7e 17686 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
17687 since we didn't add them to DYNOBJ. We know dynobj is the first
17688 bfd. */
c72f2fb2 17689 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
17690 {
17691 asection *s;
7b53ace3 17692
0c8d6e5c 17693 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
17694 continue;
17695
e717da7e
AM
17696 s = ppc64_elf_tdata (dynobj)->got;
17697 if (s != NULL
eea6121a 17698 && s->size != 0
e717da7e
AM
17699 && s->output_section != bfd_abs_section_ptr
17700 && !bfd_set_section_contents (output_bfd, s->output_section,
17701 s->contents, s->output_offset,
eea6121a 17702 s->size))
e717da7e
AM
17703 return FALSE;
17704 s = ppc64_elf_tdata (dynobj)->relgot;
17705 if (s != NULL
eea6121a 17706 && s->size != 0
e717da7e
AM
17707 && s->output_section != bfd_abs_section_ptr
17708 && !bfd_set_section_contents (output_bfd, s->output_section,
17709 s->contents, s->output_offset,
eea6121a 17710 s->size))
e717da7e
AM
17711 return FALSE;
17712 }
f6c52c13 17713
b34976b6 17714 return TRUE;
5bd4f169
AM
17715}
17716
5bd4f169 17717#include "elf64-target.h"
7b8e7dad
AM
17718
17719/* FreeBSD support */
17720
17721#undef TARGET_LITTLE_SYM
6afcdeb3 17722#define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
7b8e7dad 17723#undef TARGET_LITTLE_NAME
6afcdeb3 17724#define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
7b8e7dad
AM
17725
17726#undef TARGET_BIG_SYM
6d00b590 17727#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
17728#undef TARGET_BIG_NAME
17729#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
17730
17731#undef ELF_OSABI
17732#define ELF_OSABI ELFOSABI_FREEBSD
17733
17734#undef elf64_bed
17735#define elf64_bed elf64_powerpc_fbsd_bed
17736
17737#include "elf64-target.h"