Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
b3adc24a 2 Copyright (C) 1999-2020 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 {
1718 static char buf[60];
1719 sprintf (buf, "generic linker can't handle %s",
1720 reloc_entry->howto->name);
1721 *error_message = buf;
1722 }
1723 return bfd_reloc_dangerous;
1724}
1725
927be08e
AM
1726/* Track GOT entries needed for a given symbol. We might need more
1727 than one got entry per symbol. */
1728struct got_entry
1729{
1730 struct got_entry *next;
1731
1732 /* The symbol addend that we'll be placing in the GOT. */
1733 bfd_vma addend;
1734
1735 /* Unlike other ELF targets, we use separate GOT entries for the same
1736 symbol referenced from different input files. This is to support
1737 automatic multiple TOC/GOT sections, where the TOC base can vary
1738 from one input file to another. After partitioning into TOC groups
1739 we merge entries within the group.
1740
1741 Point to the BFD owning this GOT entry. */
1742 bfd *owner;
1743
1744 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1745 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1746 unsigned char tls_type;
927be08e
AM
1747
1748 /* Non-zero if got.ent points to real entry. */
f961d9dd 1749 unsigned char is_indirect;
927be08e
AM
1750
1751 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1752 union
2cdcc330
AM
1753 {
1754 bfd_signed_vma refcount;
1755 bfd_vma offset;
1756 struct got_entry *ent;
1757 } got;
927be08e
AM
1758};
1759
1760/* The same for PLT. */
1761struct plt_entry
1762{
1763 struct plt_entry *next;
1764
1765 bfd_vma addend;
1766
1767 union
2cdcc330
AM
1768 {
1769 bfd_signed_vma refcount;
1770 bfd_vma offset;
1771 } plt;
927be08e
AM
1772};
1773
e717da7e
AM
1774struct ppc64_elf_obj_tdata
1775{
1776 struct elf_obj_tdata elf;
1777
1778 /* Shortcuts to dynamic linker sections. */
1779 asection *got;
1780 asection *relgot;
1781
b3fac117
AM
1782 /* Used during garbage collection. We attach global symbols defined
1783 on removed .opd entries to this section so that the sym is removed. */
1784 asection *deleted_section;
81688140 1785
927be08e 1786 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1787 sections means we potentially need one of these for each input bfd. */
927be08e 1788 struct got_entry tlsld_got;
8860955f 1789
2cdcc330
AM
1790 union
1791 {
729eabd5
AM
1792 /* A copy of relocs before they are modified for --emit-relocs. */
1793 Elf_Internal_Rela *relocs;
1794
1795 /* Section contents. */
1796 bfd_byte *contents;
1797 } opd;
d77c8a4b
AM
1798
1799 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1800 the reloc to be in the range -32768 to 32767. */
98528052
AM
1801 unsigned int has_small_toc_reloc : 1;
1802
560c8763
AM
1803 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1804 instruction not one we handle. */
1805 unsigned int unexpected_toc_insn : 1;
066f4018 1806
903b777d
AM
1807 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1808 this file. */
1809 unsigned int has_optrel : 1;
e717da7e
AM
1810};
1811
1812#define ppc64_elf_tdata(bfd) \
1813 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1814
1815#define ppc64_tlsld_got(bfd) \
1816 (&ppc64_elf_tdata (bfd)->tlsld_got)
1817
0c8d6e5c
AM
1818#define is_ppc64_elf(bfd) \
1819 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1820 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1821
e717da7e
AM
1822/* Override the generic function because we store some extras. */
1823
1824static bfd_boolean
1825ppc64_elf_mkobject (bfd *abfd)
1826{
0ffa91dd 1827 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1828 PPC64_ELF_DATA);
e717da7e
AM
1829}
1830
feee612b 1831/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1832 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1833
b34976b6 1834static bfd_boolean
4ce794b7 1835ppc64_elf_object_p (bfd *abfd)
feee612b 1836{
14b57c7c
AM
1837 if (!abfd->arch_info->the_default)
1838 return TRUE;
1839
1840 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1841 {
1842 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1843
1844 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1845 {
1846 /* Relies on arch after 32 bit default being 64 bit default. */
1847 abfd->arch_info = abfd->arch_info->next;
1848 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1849 }
1850 }
14b57c7c 1851 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1852}
1853
d37c89e5
AM
1854/* Support for core dump NOTE sections. */
1855
1856static bfd_boolean
1857ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1858{
eea6121a 1859 size_t offset, size;
d37c89e5
AM
1860
1861 if (note->descsz != 504)
1862 return FALSE;
1863
1864 /* pr_cursig */
228e534f 1865 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1866
1867 /* pr_pid */
228e534f 1868 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1869
1870 /* pr_reg */
1871 offset = 112;
eea6121a 1872 size = 384;
d37c89e5
AM
1873
1874 /* Make a ".reg/999" section. */
1875 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1876 size, note->descpos + offset);
d37c89e5
AM
1877}
1878
1879static bfd_boolean
1880ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1881{
1882 if (note->descsz != 136)
1883 return FALSE;
1884
228e534f 1885 elf_tdata (abfd)->core->pid
bc989cdc 1886 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1887 elf_tdata (abfd)->core->program
d37c89e5 1888 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1889 elf_tdata (abfd)->core->command
d37c89e5
AM
1890 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1891
1892 return TRUE;
1893}
1894
183e98be
AM
1895static char *
1896ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1897 ...)
1898{
1899 switch (note_type)
1900 {
1901 default:
1902 return NULL;
1903
1904 case NT_PRPSINFO:
1905 {
9ef6d1e3 1906 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1907 va_list ap;
1908
1909 va_start (ap, note_type);
75cd47ed 1910 memset (data, 0, sizeof (data));
183e98be 1911 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1912#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1913 DIAGNOSTIC_PUSH;
be3e27bb 1914 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1915 -Wstringop-truncation:
1916 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1917 */
95da9854
L
1918 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1919#endif
183e98be 1920 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1921#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1922 DIAGNOSTIC_POP;
fe75810f 1923#endif
183e98be
AM
1924 va_end (ap);
1925 return elfcore_write_note (abfd, buf, bufsiz,
1926 "CORE", note_type, data, sizeof (data));
1927 }
1928
1929 case NT_PRSTATUS:
1930 {
1931 char data[504];
1932 va_list ap;
1933 long pid;
1934 int cursig;
1935 const void *greg;
1936
1937 va_start (ap, note_type);
1938 memset (data, 0, 112);
1939 pid = va_arg (ap, long);
1940 bfd_put_32 (abfd, pid, data + 32);
1941 cursig = va_arg (ap, int);
1942 bfd_put_16 (abfd, cursig, data + 12);
1943 greg = va_arg (ap, const void *);
1944 memcpy (data + 112, greg, 384);
1945 memset (data + 496, 0, 8);
1946 va_end (ap);
1947 return elfcore_write_note (abfd, buf, bufsiz,
1948 "CORE", note_type, data, sizeof (data));
1949 }
1950 }
1951}
1952
5d35169e
AM
1953/* Add extra PPC sections. */
1954
2cdcc330 1955static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1956{
07d6d2b8
AM
1957 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1958 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1959 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1960 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1961 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1962 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1963 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1964};
1965
7c8fe5c4
AM
1966enum _ppc64_sec_type {
1967 sec_normal = 0,
1968 sec_opd = 1,
1969 sec_toc = 2
1970};
1971
f0abc2a1
AM
1972struct _ppc64_elf_section_data
1973{
1974 struct bfd_elf_section_data elf;
411e1bfb 1975
f0abc2a1
AM
1976 union
1977 {
51aecdc5
AM
1978 /* An array with one entry for each opd function descriptor,
1979 and some spares since opd entries may be either 16 or 24 bytes. */
1980#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
1981 struct _opd_sec_data
1982 {
1983 /* Points to the function code section for local opd entries. */
1984 asection **func_sec;
1985
1986 /* After editing .opd, adjust references to opd local syms. */
1987 long *adjust;
1988 } opd;
7c8fe5c4 1989
3a71aa26
AM
1990 /* An array for toc sections, indexed by offset/8. */
1991 struct _toc_sec_data
1992 {
1993 /* Specifies the relocation symbol index used at a given toc offset. */
1994 unsigned *symndx;
1995
1996 /* And the relocation addend. */
1997 bfd_vma *add;
1998 } toc;
7c8fe5c4
AM
1999 } u;
2000
2001 enum _ppc64_sec_type sec_type:2;
411e1bfb 2002
7c8fe5c4
AM
2003 /* Flag set when small branches are detected. Used to
2004 select suitable defaults for the stub group size. */
2005 unsigned int has_14bit_branch:1;
3e04d765
AM
2006
2007 /* Flag set when PLTCALL relocs are detected. */
2008 unsigned int has_pltcall:1;
066f4018 2009
903b777d
AM
2010 /* Flag set when section has PLT/GOT/TOC relocations that can be
2011 optimised. */
2012 unsigned int has_optrel:1;
f0abc2a1
AM
2013};
2014
2015#define ppc64_elf_section_data(sec) \
411e1bfb 2016 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
2017
2018static bfd_boolean
4ce794b7 2019ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2020{
f592407e
AM
2021 if (!sec->used_by_bfd)
2022 {
2023 struct _ppc64_elf_section_data *sdata;
986f0783 2024 size_t amt = sizeof (*sdata);
f0abc2a1 2025
f592407e
AM
2026 sdata = bfd_zalloc (abfd, amt);
2027 if (sdata == NULL)
2028 return FALSE;
2029 sec->used_by_bfd = sdata;
2030 }
f0abc2a1
AM
2031
2032 return _bfd_elf_new_section_hook (abfd, sec);
2033}
4025353c 2034
bf577467
AM
2035static bfd_boolean
2036ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2037{
2038 const char *name = hdr->bfd_section->name;
2039
2040 if (strncmp (name, ".sbss", 5) == 0
2041 || strncmp (name, ".sdata", 6) == 0)
2042 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2043
2044 return TRUE;
2045}
2046
74f0fb50 2047static struct _opd_sec_data *
4025353c
AM
2048get_opd_info (asection * sec)
2049{
2050 if (sec != NULL
2051 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2052 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2053 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2054 return NULL;
2055}
90e3cdf2
JJ
2056\f
2057/* Parameters for the qsort hook. */
90e3cdf2 2058static bfd_boolean synthetic_relocatable;
cd285db5 2059static asection *synthetic_opd;
90e3cdf2 2060
699733f6 2061/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2062
2063static int
2064compare_symbols (const void *ap, const void *bp)
2065{
2cdcc330
AM
2066 const asymbol *a = *(const asymbol **) ap;
2067 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 2068
699733f6
AM
2069 /* Section symbols first. */
2070 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2071 return -1;
699733f6 2072 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2073 return 1;
2074
699733f6 2075 /* then .opd symbols. */
cd285db5
AM
2076 if (synthetic_opd != NULL)
2077 {
2078 if (strcmp (a->section->name, ".opd") == 0
2079 && strcmp (b->section->name, ".opd") != 0)
2080 return -1;
2081 if (strcmp (a->section->name, ".opd") != 0
2082 && strcmp (b->section->name, ".opd") == 0)
2083 return 1;
2084 }
90e3cdf2 2085
699733f6 2086 /* then other code symbols. */
2cdcc330
AM
2087 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2088 == (SEC_CODE | SEC_ALLOC))
2089 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2090 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2091 return -1;
2092
2cdcc330
AM
2093 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2094 != (SEC_CODE | SEC_ALLOC))
2095 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2096 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2097 return 1;
2098
2099 if (synthetic_relocatable)
2100 {
2101 if (a->section->id < b->section->id)
2102 return -1;
2103
2104 if (a->section->id > b->section->id)
2105 return 1;
2106 }
2107
2108 if (a->value + a->section->vma < b->value + b->section->vma)
2109 return -1;
2110
2111 if (a->value + a->section->vma > b->value + b->section->vma)
2112 return 1;
2113
4d35a0aa
AM
2114 /* For syms with the same value, prefer strong dynamic global function
2115 syms over other syms. */
2116 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2117 return -1;
2118
2119 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2120 return 1;
2121
2122 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2123 return -1;
2124
2125 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2126 return 1;
2127
2128 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2129 return -1;
2130
2131 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2132 return 1;
2133
2134 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2135 return -1;
2136
2137 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2138 return 1;
2139
dcea6a95
AM
2140 /* Finally, sort on where the symbol is in memory. The symbols will
2141 be in at most two malloc'd blocks, one for static syms, one for
2142 dynamic syms, and we distinguish the two blocks above by testing
2143 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2144 originally in the same order as the symbols (and we're not
2145 sorting the symbols themselves), this ensures a stable sort. */
2146 if (a < b)
2147 return -1;
2148 if (a > b)
2149 return 1;
2150 return 0;
90e3cdf2
JJ
2151}
2152
699733f6 2153/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2154
699733f6 2155static asymbol *
9ad9b810
AM
2156sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2157 bfd_vma value)
90e3cdf2 2158{
9ad9b810 2159 size_t mid;
90e3cdf2 2160
7292b3ac 2161 if (id == (unsigned) -1)
699733f6
AM
2162 {
2163 while (lo < hi)
2164 {
2165 mid = (lo + hi) >> 1;
2166 if (syms[mid]->value + syms[mid]->section->vma < value)
2167 lo = mid + 1;
2168 else if (syms[mid]->value + syms[mid]->section->vma > value)
2169 hi = mid;
2170 else
2171 return syms[mid];
2172 }
2173 }
2174 else
2175 {
2176 while (lo < hi)
2177 {
2178 mid = (lo + hi) >> 1;
2179 if (syms[mid]->section->id < id)
2180 lo = mid + 1;
2181 else if (syms[mid]->section->id > id)
2182 hi = mid;
2183 else if (syms[mid]->value < value)
2184 lo = mid + 1;
2185 else if (syms[mid]->value > value)
2186 hi = mid;
2187 else
2188 return syms[mid];
2189 }
2190 }
2191 return NULL;
90e3cdf2
JJ
2192}
2193
468392fb
AM
2194static bfd_boolean
2195section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2196{
2197 bfd_vma vma = *(bfd_vma *) ptr;
2198 return ((section->flags & SEC_ALLOC) != 0
2199 && section->vma <= vma
2200 && vma < section->vma + section->size);
2201}
2202
699733f6 2203/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
2204 entry syms. Also generate @plt symbols for the glink branch table.
2205 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
2206
2207static long
a7535cf3
AM
2208ppc64_elf_get_synthetic_symtab (bfd *abfd,
2209 long static_count, asymbol **static_syms,
2210 long dyn_count, asymbol **dyn_syms,
c9727e01 2211 asymbol **ret)
90e3cdf2
JJ
2212{
2213 asymbol *s;
0ccf57bd 2214 size_t i, j, count;
90e3cdf2 2215 char *names;
0ccf57bd 2216 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 2217 asection *opd = NULL;
90e3cdf2 2218 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2219 asymbol **syms;
ee67d69a 2220 int abi = abiversion (abfd);
90e3cdf2
JJ
2221
2222 *ret = NULL;
2223
ee67d69a
AM
2224 if (abi < 2)
2225 {
2226 opd = bfd_get_section_by_name (abfd, ".opd");
2227 if (opd == NULL && abi == 1)
2228 return 0;
2229 }
90e3cdf2 2230
a5259595
AM
2231 syms = NULL;
2232 codesecsym = 0;
2233 codesecsymend = 0;
2234 secsymend = 0;
2235 opdsymend = 0;
2236 symcount = 0;
2237 if (opd != NULL)
c9727e01 2238 {
a5259595
AM
2239 symcount = static_count;
2240 if (!relocatable)
2241 symcount += dyn_count;
2242 if (symcount == 0)
2243 return 0;
c9727e01 2244
a5259595
AM
2245 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2246 if (syms == NULL)
2247 return -1;
90e3cdf2 2248
a5259595
AM
2249 if (!relocatable && static_count != 0 && dyn_count != 0)
2250 {
2251 /* Use both symbol tables. */
2252 memcpy (syms, static_syms, static_count * sizeof (*syms));
2253 memcpy (syms + static_count, dyn_syms,
2254 (dyn_count + 1) * sizeof (*syms));
2255 }
2256 else if (!relocatable && static_count == 0)
2257 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2258 else
2259 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2260
0ccf57bd
AM
2261 /* Trim uninteresting symbols. Interesting symbols are section,
2262 function, and notype symbols. */
2263 for (i = 0, j = 0; i < symcount; ++i)
2264 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2265 | BSF_RELC | BSF_SRELC)) == 0)
2266 syms[j++] = syms[i];
2267 symcount = j;
2268
a5259595
AM
2269 synthetic_relocatable = relocatable;
2270 synthetic_opd = opd;
2271 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2272
a5259595
AM
2273 if (!relocatable && symcount > 1)
2274 {
bfa5bd2a
PA
2275 /* Trim duplicate syms, since we may have merged the normal
2276 and dynamic symbols. Actually, we only care about syms
2277 that have different values, so trim any with the same
2278 value. Don't consider ifunc and ifunc resolver symbols
2279 duplicates however, because GDB wants to know whether a
2280 text symbol is an ifunc resolver. */
a5259595 2281 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2282 {
2283 const asymbol *s0 = syms[i - 1];
2284 const asymbol *s1 = syms[i];
2285
2286 if ((s0->value + s0->section->vma
2287 != s1->value + s1->section->vma)
2288 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2289 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2290 syms[j++] = syms[i];
2291 }
a5259595
AM
2292 symcount = j;
2293 }
699733f6 2294
a5259595
AM
2295 i = 0;
2296 /* Note that here and in compare_symbols we can't compare opd and
2297 sym->section directly. With separate debug info files, the
2298 symbols will be extracted from the debug file while abfd passed
2299 to this function is the real binary. */
0ccf57bd 2300 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2301 ++i;
2302 codesecsym = i;
2303
2304 for (; i < symcount; ++i)
2305 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2306 | SEC_THREAD_LOCAL))
2307 != (SEC_CODE | SEC_ALLOC))
2308 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2309 break;
2310 codesecsymend = i;
2311
2312 for (; i < symcount; ++i)
2313 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2314 break;
2315 secsymend = i;
2316
2317 for (; i < symcount; ++i)
2318 if (strcmp (syms[i]->section->name, ".opd") != 0)
2319 break;
2320 opdsymend = i;
2321
2322 for (; i < symcount; ++i)
2cdcc330
AM
2323 if (((syms[i]->section->flags
2324 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2325 != (SEC_CODE | SEC_ALLOC))
2326 break;
2327 symcount = i;
2328 }
c9727e01 2329 count = 0;
90e3cdf2 2330
699733f6 2331 if (relocatable)
90e3cdf2 2332 {
699733f6
AM
2333 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2334 arelent *r;
2335 size_t size;
0ccf57bd 2336 size_t relcount;
90e3cdf2 2337
468392fb
AM
2338 if (opdsymend == secsymend)
2339 goto done;
2340
699733f6 2341 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2342 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2343 if (relcount == 0)
c9727e01 2344 goto done;
90e3cdf2 2345
7356fed5
AM
2346 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2347 {
2348 count = -1;
2349 goto done;
2350 }
2351
699733f6 2352 size = 0;
595da8c5 2353 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2354 {
2355 asymbol *sym;
90e3cdf2 2356
595da8c5 2357 while (r < opd->relocation + relcount
699733f6
AM
2358 && r->address < syms[i]->value + opd->vma)
2359 ++r;
90e3cdf2 2360
595da8c5 2361 if (r == opd->relocation + relcount)
699733f6 2362 break;
90e3cdf2 2363
699733f6
AM
2364 if (r->address != syms[i]->value + opd->vma)
2365 continue;
90e3cdf2 2366
699733f6
AM
2367 if (r->howto->type != R_PPC64_ADDR64)
2368 continue;
90e3cdf2 2369
699733f6
AM
2370 sym = *r->sym_ptr_ptr;
2371 if (!sym_exists_at (syms, opdsymend, symcount,
2372 sym->section->id, sym->value + r->addend))
2373 {
2374 ++count;
2375 size += sizeof (asymbol);
2376 size += strlen (syms[i]->name) + 2;
2377 }
2378 }
90e3cdf2 2379
c4b0b099
AM
2380 if (size == 0)
2381 goto done;
699733f6
AM
2382 s = *ret = bfd_malloc (size);
2383 if (s == NULL)
2384 {
7356fed5 2385 count = -1;
c9727e01 2386 goto done;
699733f6 2387 }
90e3cdf2 2388
699733f6 2389 names = (char *) (s + count);
90e3cdf2 2390
595da8c5 2391 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2392 {
699733f6 2393 asymbol *sym;
90e3cdf2 2394
595da8c5 2395 while (r < opd->relocation + relcount
699733f6
AM
2396 && r->address < syms[i]->value + opd->vma)
2397 ++r;
90e3cdf2 2398
595da8c5 2399 if (r == opd->relocation + relcount)
699733f6
AM
2400 break;
2401
2402 if (r->address != syms[i]->value + opd->vma)
2403 continue;
2404
2405 if (r->howto->type != R_PPC64_ADDR64)
2406 continue;
90e3cdf2 2407
699733f6
AM
2408 sym = *r->sym_ptr_ptr;
2409 if (!sym_exists_at (syms, opdsymend, symcount,
2410 sym->section->id, sym->value + r->addend))
2411 {
2412 size_t len;
2413
2414 *s = *syms[i];
6ba2a415 2415 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2416 s->section = sym->section;
2417 s->value = sym->value + r->addend;
2418 s->name = names;
2419 *names++ = '.';
2420 len = strlen (syms[i]->name);
2421 memcpy (names, syms[i]->name, len + 1);
2422 names += len + 1;
6f610d07
UW
2423 /* Have udata.p point back to the original symbol this
2424 synthetic symbol was derived from. */
2425 s->udata.p = syms[i];
699733f6
AM
2426 s++;
2427 }
2428 }
2429 }
2430 else
90e3cdf2 2431 {
468392fb 2432 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 2433 bfd_byte *contents = NULL;
699733f6 2434 size_t size;
0ccf57bd 2435 size_t plt_count = 0;
468392fb
AM
2436 bfd_vma glink_vma = 0, resolv_vma = 0;
2437 asection *dynamic, *glink = NULL, *relplt = NULL;
2438 arelent *p;
90e3cdf2 2439
ee67d69a 2440 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2441 {
c4b0b099
AM
2442 free_contents_and_exit_err:
2443 count = -1;
ee67d69a 2444 free_contents_and_exit:
c9594989 2445 free (contents);
c9727e01 2446 goto done;
699733f6 2447 }
90e3cdf2 2448
699733f6
AM
2449 size = 0;
2450 for (i = secsymend; i < opdsymend; ++i)
2451 {
2452 bfd_vma ent;
90e3cdf2 2453
5ef11c02
AM
2454 /* Ignore bogus symbols. */
2455 if (syms[i]->value > opd->size - 8)
2456 continue;
2457
699733f6
AM
2458 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2459 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2460 {
2461 ++count;
2462 size += sizeof (asymbol);
2463 size += strlen (syms[i]->name) + 2;
2464 }
2465 }
90e3cdf2 2466
468392fb 2467 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2468 if (dyn_count != 0
2469 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2470 {
2471 bfd_byte *dynbuf, *extdyn, *extdynend;
2472 size_t extdynsize;
2473 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2474
2475 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2476 goto free_contents_and_exit_err;
468392fb
AM
2477
2478 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2479 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2480
2481 extdyn = dynbuf;
2482 extdynend = extdyn + dynamic->size;
2483 for (; extdyn < extdynend; extdyn += extdynsize)
2484 {
2485 Elf_Internal_Dyn dyn;
2486 (*swap_dyn_in) (abfd, extdyn, &dyn);
2487
2488 if (dyn.d_tag == DT_NULL)
2489 break;
2490
2491 if (dyn.d_tag == DT_PPC64_GLINK)
2492 {
9e390558
AM
2493 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2494 See comment in ppc64_elf_finish_dynamic_sections. */
2495 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2496 /* The .glink section usually does not survive the final
2497 link; search for the section (usually .text) where the
2498 glink stubs now reside. */
2499 glink = bfd_sections_find_if (abfd, section_covers_vma,
2500 &glink_vma);
2501 break;
2502 }
2503 }
2504
2505 free (dynbuf);
2506 }
2507
2508 if (glink != NULL)
2509 {
2510 /* Determine __glink trampoline by reading the relative branch
2511 from the first glink stub. */
2512 bfd_byte buf[4];
b9e5796b
AM
2513 unsigned int off = 0;
2514
2515 while (bfd_get_section_contents (abfd, glink, buf,
2516 glink_vma + off - glink->vma, 4))
468392fb
AM
2517 {
2518 unsigned int insn = bfd_get_32 (abfd, buf);
2519 insn ^= B_DOT;
2520 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2521 {
2cdcc330
AM
2522 resolv_vma
2523 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2524 break;
2525 }
2526 off += 4;
2527 if (off > 4)
2528 break;
468392fb
AM
2529 }
2530
2531 if (resolv_vma)
2532 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2533
066ee829
AM
2534 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2535 if (relplt != NULL)
2536 {
2537 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2cdcc330 2538 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 2539 goto free_contents_and_exit_err;
68ffbac6 2540
066ee829
AM
2541 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2542 size += plt_count * sizeof (asymbol);
468392fb 2543
066ee829
AM
2544 p = relplt->relocation;
2545 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2546 {
2547 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2548 if (p->addend != 0)
2549 size += sizeof ("+0x") - 1 + 16;
2550 }
066ee829 2551 }
468392fb
AM
2552 }
2553
c4b0b099
AM
2554 if (size == 0)
2555 goto free_contents_and_exit;
699733f6
AM
2556 s = *ret = bfd_malloc (size);
2557 if (s == NULL)
c4b0b099 2558 goto free_contents_and_exit_err;
90e3cdf2 2559
468392fb 2560 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2561
699733f6 2562 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2563 {
699733f6 2564 bfd_vma ent;
90e3cdf2 2565
5ef11c02
AM
2566 if (syms[i]->value > opd->size - 8)
2567 continue;
2568
699733f6
AM
2569 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2570 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2571 {
0ccf57bd 2572 size_t lo, hi;
699733f6 2573 size_t len;
c9727e01 2574 asection *sec = abfd->sections;
90e3cdf2 2575
699733f6
AM
2576 *s = *syms[i];
2577 lo = codesecsym;
2578 hi = codesecsymend;
2579 while (lo < hi)
2580 {
0ccf57bd 2581 size_t mid = (lo + hi) >> 1;
699733f6
AM
2582 if (syms[mid]->section->vma < ent)
2583 lo = mid + 1;
2584 else if (syms[mid]->section->vma > ent)
2585 hi = mid;
2586 else
c9727e01
AM
2587 {
2588 sec = syms[mid]->section;
2589 break;
2590 }
699733f6
AM
2591 }
2592
c9727e01 2593 if (lo >= hi && lo > codesecsym)
699733f6 2594 sec = syms[lo - 1]->section;
699733f6
AM
2595
2596 for (; sec != NULL; sec = sec->next)
2597 {
2598 if (sec->vma > ent)
2599 break;
63524580
JK
2600 /* SEC_LOAD may not be set if SEC is from a separate debug
2601 info file. */
2602 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2603 break;
2604 if ((sec->flags & SEC_CODE) != 0)
2605 s->section = sec;
2606 }
6ba2a415 2607 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2608 s->value = ent - s->section->vma;
2609 s->name = names;
2610 *names++ = '.';
2611 len = strlen (syms[i]->name);
2612 memcpy (names, syms[i]->name, len + 1);
2613 names += len + 1;
6f610d07
UW
2614 /* Have udata.p point back to the original symbol this
2615 synthetic symbol was derived from. */
2616 s->udata.p = syms[i];
699733f6 2617 s++;
90e3cdf2 2618 }
90e3cdf2 2619 }
699733f6 2620 free (contents);
468392fb
AM
2621
2622 if (glink != NULL && relplt != NULL)
2623 {
2624 if (resolv_vma)
2625 {
2626 /* Add a symbol for the main glink trampoline. */
86a4952b 2627 memset (s, 0, sizeof *s);
468392fb 2628 s->the_bfd = abfd;
6ba2a415 2629 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2630 s->section = glink;
2631 s->value = resolv_vma - glink->vma;
2632 s->name = names;
2cdcc330
AM
2633 memcpy (names, "__glink_PLTresolve",
2634 sizeof ("__glink_PLTresolve"));
468392fb
AM
2635 names += sizeof ("__glink_PLTresolve");
2636 s++;
2637 count++;
2638 }
2639
2640 /* FIXME: It would be very much nicer to put sym@plt on the
2641 stub rather than on the glink branch table entry. The
2642 objdump disassembler would then use a sensible symbol
2643 name on plt calls. The difficulty in doing so is
2644 a) finding the stubs, and,
2645 b) matching stubs against plt entries, and,
2646 c) there can be multiple stubs for a given plt entry.
2647
2648 Solving (a) could be done by code scanning, but older
2649 ppc64 binaries used different stubs to current code.
2650 (b) is the tricky one since you need to known the toc
2651 pointer for at least one function that uses a pic stub to
2652 be able to calculate the plt address referenced.
2653 (c) means gdb would need to set multiple breakpoints (or
2654 find the glink branch itself) when setting breakpoints
2655 for pending shared library loads. */
2656 p = relplt->relocation;
2657 for (i = 0; i < plt_count; i++, p++)
2658 {
2659 size_t len;
2660
2661 *s = **p->sym_ptr_ptr;
2662 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2663 we are defining a symbol, ensure one of them is set. */
2664 if ((s->flags & BSF_LOCAL) == 0)
2665 s->flags |= BSF_GLOBAL;
6ba2a415 2666 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2667 s->section = glink;
2668 s->value = glink_vma - glink->vma;
2669 s->name = names;
2670 s->udata.p = NULL;
2671 len = strlen ((*p->sym_ptr_ptr)->name);
2672 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2673 names += len;
e054468f
AM
2674 if (p->addend != 0)
2675 {
2676 memcpy (names, "+0x", sizeof ("+0x") - 1);
2677 names += sizeof ("+0x") - 1;
2678 bfd_sprintf_vma (abfd, names, p->addend);
2679 names += strlen (names);
2680 }
468392fb
AM
2681 memcpy (names, "@plt", sizeof ("@plt"));
2682 names += sizeof ("@plt");
2683 s++;
b9e5796b
AM
2684 if (abi < 2)
2685 {
2686 glink_vma += 8;
2687 if (i >= 0x8000)
2688 glink_vma += 4;
2689 }
2690 else
468392fb
AM
2691 glink_vma += 4;
2692 }
2693 count += plt_count;
2694 }
90e3cdf2
JJ
2695 }
2696
c9727e01 2697 done:
a7535cf3 2698 free (syms);
90e3cdf2
JJ
2699 return count;
2700}
5bd4f169 2701\f
65f38f15
AM
2702/* The following functions are specific to the ELF linker, while
2703 functions above are used generally. Those named ppc64_elf_* are
2704 called by the main ELF linker code. They appear in this file more
2705 or less in the order in which they are called. eg.
2706 ppc64_elf_check_relocs is called early in the link process,
2707 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2708 called.
2709
2710 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2711 functions have both a function code symbol and a function descriptor
2712 symbol. A call to foo in a relocatable object file looks like:
2713
2714 . .text
2715 . x:
2716 . bl .foo
2717 . nop
2718
2719 The function definition in another object file might be:
2720
2721 . .section .opd
2722 . foo: .quad .foo
2723 . .quad .TOC.@tocbase
2724 . .quad 0
2725 .
2726 . .text
2727 . .foo: blr
2728
2729 When the linker resolves the call during a static link, the branch
2730 unsurprisingly just goes to .foo and the .opd information is unused.
2731 If the function definition is in a shared library, things are a little
2732 different: The call goes via a plt call stub, the opd information gets
2733 copied to the plt, and the linker patches the nop.
2734
2735 . x:
2736 . bl .foo_stub
2737 . ld 2,40(1)
2738 .
2739 .
2740 . .foo_stub:
71a39c98
AM
2741 . std 2,40(1) # in practice, the call stub
2742 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2743 . addi 11,11,Lfoo@toc@l # this is the general idea
2744 . ld 12,0(11)
2745 . ld 2,8(11)
2746 . mtctr 12
2747 . ld 11,16(11)
e86ce104
AM
2748 . bctr
2749 .
2750 . .section .plt
2751 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2752
2753 The "reloc ()" notation is supposed to indicate that the linker emits
2754 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2755 copying.
2756
2757 What are the difficulties here? Well, firstly, the relocations
2758 examined by the linker in check_relocs are against the function code
2759 sym .foo, while the dynamic relocation in the plt is emitted against
2760 the function descriptor symbol, foo. Somewhere along the line, we need
2761 to carefully copy dynamic link information from one symbol to the other.
2762 Secondly, the generic part of the elf linker will make .foo a dynamic
2763 symbol as is normal for most other backends. We need foo dynamic
2764 instead, at least for an application final link. However, when
2765 creating a shared library containing foo, we need to have both symbols
2766 dynamic so that references to .foo are satisfied during the early
2767 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2768 definition from some other object, eg. a static library.
2769
2770 Update: As of August 2004, we support a new convention. Function
2771 calls may use the function descriptor symbol, ie. "bl foo". This
2772 behaves exactly as "bl .foo". */
65f38f15 2773
7c8bbca5
AM
2774/* Of those relocs that might be copied as dynamic relocs, this
2775 function selects those that must be copied when linking a shared
2776 library or PIE, even when the symbol is local. */
65f38f15 2777
1d483afe
AM
2778static int
2779must_be_dyn_reloc (struct bfd_link_info *info,
2780 enum elf_ppc64_reloc_type r_type)
2781{
2782 switch (r_type)
2783 {
2784 default:
7c8bbca5
AM
2785 /* Only relative relocs can be resolved when the object load
2786 address isn't fixed. DTPREL64 is excluded because the
2787 dynamic linker needs to differentiate global dynamic from
2788 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2789 return 1;
2790
2791 case R_PPC64_REL32:
2792 case R_PPC64_REL64:
2793 case R_PPC64_REL30:
1bdd8fac
AM
2794 case R_PPC64_TOC16:
2795 case R_PPC64_TOC16_DS:
2796 case R_PPC64_TOC16_LO:
2797 case R_PPC64_TOC16_HI:
2798 case R_PPC64_TOC16_HA:
2799 case R_PPC64_TOC16_LO_DS:
1d483afe
AM
2800 return 0;
2801
2802 case R_PPC64_TPREL16:
2803 case R_PPC64_TPREL16_LO:
2804 case R_PPC64_TPREL16_HI:
2805 case R_PPC64_TPREL16_HA:
2806 case R_PPC64_TPREL16_DS:
2807 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2808 case R_PPC64_TPREL16_HIGH:
2809 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2810 case R_PPC64_TPREL16_HIGHER:
2811 case R_PPC64_TPREL16_HIGHERA:
2812 case R_PPC64_TPREL16_HIGHEST:
2813 case R_PPC64_TPREL16_HIGHESTA:
2814 case R_PPC64_TPREL64:
c213164a 2815 case R_PPC64_TPREL34:
7c8bbca5
AM
2816 /* These relocations are relative but in a shared library the
2817 linker doesn't know the thread pointer base. */
2818 return bfd_link_dll (info);
1d483afe
AM
2819 }
2820}
65f38f15 2821
f4656909 2822/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
e1c6cf61 2823 copying dynamic variables from a shared lib into an app's .dynbss
f4656909 2824 section, and instead use a dynamic relocation to point into the
e1c6cf61
AM
2825 shared lib. With code that gcc generates it is vital that this be
2826 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2827 actually the address of a function descriptor which resides in the
2828 .opd section. gcc uses the descriptor directly rather than going
2829 via the GOT as some other ABIs do, which means that initialized
2830 function pointers reference the descriptor. Thus, a function
2831 pointer initialized to the address of a function in a shared
2832 library will either require a .dynbss copy and a copy reloc, or a
2833 dynamic reloc. Using a .dynbss copy redefines the function
2834 descriptor symbol to point to the copy. This presents a problem as
2835 a PLT entry for that function is also initialized from the function
2836 descriptor symbol and the copy may not be initialized first. */
a23b6845 2837#define ELIMINATE_COPY_RELOCS 1
f4656909 2838
721956f4 2839/* Section name for stubs is the associated section name plus this
29942be8
NC
2840 string. */
2841#define STUB_SUFFIX ".stub"
721956f4
AM
2842
2843/* Linker stubs.
2844 ppc_stub_long_branch:
2845 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2846 destination, but a 24 bit branch in a stub section will reach.
2847 . b dest
2848
2849 ppc_stub_plt_branch:
2850 Similar to the above, but a 24 bit branch in the stub section won't
2851 reach its destination.
6df4c9c2
AM
2852 . addis %r12,%r2,xxx@toc@ha
2853 . ld %r12,xxx@toc@l(%r12)
71a39c98 2854 . mtctr %r12
721956f4
AM
2855 . bctr
2856
2857 ppc_stub_plt_call:
2c66dc6c
AM
2858 Used to call a function in a shared library. If it so happens that
2859 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2860 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 2861 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
2862 . addis %r11,%r2,xxx@toc@ha
2863 . ld %r12,xxx+0@toc@l(%r11)
2864 . mtctr %r12
2865 . ld %r2,xxx+8@toc@l(%r11)
2866 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2867 . bctr
ad8e1ba5
AM
2868
2869 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2870 code to adjust the value and save r2 to support multiple toc sections.
2871 A ppc_stub_long_branch with an r2 offset looks like:
2872 . std %r2,40(%r1)
2873 . addis %r2,%r2,off@ha
2874 . addi %r2,%r2,off@l
2875 . b dest
2876
2877 A ppc_stub_plt_branch with an r2 offset looks like:
2878 . std %r2,40(%r1)
6df4c9c2
AM
2879 . addis %r12,%r2,xxx@toc@ha
2880 . ld %r12,xxx@toc@l(%r12)
ad8e1ba5
AM
2881 . addis %r2,%r2,off@ha
2882 . addi %r2,%r2,off@l
71a39c98 2883 . mtctr %r12
ad8e1ba5 2884 . bctr
ac2df442 2885
05d0e962
AM
2886 All of the above stubs are shown as their ELFv1 variants. ELFv2
2887 variants exist too, simpler for plt calls since a new toc pointer
2888 and static chain are not loaded by the stub. In addition, ELFv2
2889 has some more complex stubs to handle calls marked with NOTOC
2890 relocs from functions where r2 is not a valid toc pointer. These
2891 come in two flavours, the ones shown below, and _both variants that
2892 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2893 one call is from a function where r2 is used as the toc pointer but
2894 needs a toc adjusting stub for small-model multi-toc, and another
2895 call is from a function where r2 is not valid.
2896 ppc_stub_long_branch_notoc:
2897 . mflr %r12
2898 . bcl 20,31,1f
2899 . 1:
2900 . mflr %r11
2901 . mtlr %r12
2cdcc330
AM
2902 . addis %r12,%r11,dest-1b@ha
2903 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2904 . b dest
2905
2906 ppc_stub_plt_branch_notoc:
2907 . mflr %r12
2908 . bcl 20,31,1f
2909 . 1:
2910 . mflr %r11
2911 . mtlr %r12
2912 . lis %r12,xxx-1b@highest
f891966f 2913 . ori %r12,%r12,xxx-1b@higher
05d0e962 2914 . sldi %r12,%r12,32
f891966f 2915 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2916 . ori %r12,%r12,xxx-1b@l
2917 . add %r12,%r11,%r12
2918 . mtctr %r12
2919 . bctr
2920
2921 ppc_stub_plt_call_notoc:
2922 . mflr %r12
2923 . bcl 20,31,1f
2924 . 1:
2925 . mflr %r11
2926 . mtlr %r12
2927 . lis %r12,xxx-1b@highest
f891966f 2928 . ori %r12,%r12,xxx-1b@higher
05d0e962 2929 . sldi %r12,%r12,32
f891966f 2930 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2931 . ori %r12,%r12,xxx-1b@l
2932 . ldx %r12,%r11,%r12
2933 . mtctr %r12
2934 . bctr
2935
7c1f4227 2936 There are also ELFv1 power10 variants of these stubs.
04bdff6a 2937 ppc_stub_long_branch_notoc:
d4b87b1e 2938 . pla %r12,dest@pcrel
04bdff6a
AM
2939 . b dest
2940 ppc_stub_plt_branch_notoc:
2941 . lis %r11,(dest-1f)@highesta34
2942 . ori %r11,%r11,(dest-1f)@highera34
2943 . sldi %r11,%r11,34
d4b87b1e 2944 . 1: pla %r12,dest@pcrel
04bdff6a
AM
2945 . add %r12,%r11,%r12
2946 . mtctr %r12
2947 . bctr
2948 ppc_stub_plt_call_notoc:
2949 . lis %r11,(xxx-1f)@highesta34
2950 . ori %r11,%r11,(xxx-1f)@highera34
2951 . sldi %r11,%r11,34
d4b87b1e 2952 . 1: pla %r12,xxx@pcrel
04bdff6a
AM
2953 . ldx %r12,%r11,%r12
2954 . mtctr %r12
2955 . bctr
2956
05d0e962
AM
2957 In cases where the high instructions would add zero, they are
2958 omitted and following instructions modified in some cases.
7c1f4227 2959 For example, a power10 ppc_stub_plt_call_notoc might simplify down
04bdff6a
AM
2960 to
2961 . pld %r12,xxx@pcrel
2962 . mtctr %r12
2963 . bctr
05d0e962
AM
2964
2965 For a given stub group (a set of sections all using the same toc
2966 pointer value) there will be just one stub type used for any
2967 particular function symbol. For example, if printf is called from
2968 code with the tocsave optimization (ie. r2 saved in function
2969 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2970 and from other code without the tocsave optimization requiring a
2971 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2972 type will be created. Calls with the tocsave optimization will
2973 enter this stub after the instruction saving r2. A similar
2974 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2975 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2976 to call an external function like printf. If other calls to printf
2977 require a ppc_stub_plt_call linkage stub then a single
2978 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2979 call. If other calls to printf require a ppc_stub_plt_call_r2save
2980 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2981 be created and calls not requiring r2 to be saved will enter the
2982 stub after the r2 save instruction. There is an analogous
2983 hierarchy of long branch and plt branch stubs for local call
2984 linkage. */
721956f4 2985
2cdcc330
AM
2986enum ppc_stub_type
2987{
721956f4
AM
2988 ppc_stub_none,
2989 ppc_stub_long_branch,
ad8e1ba5 2990 ppc_stub_long_branch_r2off,
05d0e962
AM
2991 ppc_stub_long_branch_notoc,
2992 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 2993 ppc_stub_plt_branch,
ad8e1ba5 2994 ppc_stub_plt_branch_r2off,
05d0e962
AM
2995 ppc_stub_plt_branch_notoc,
2996 ppc_stub_plt_branch_both,
794e51c0 2997 ppc_stub_plt_call,
7341d5e2 2998 ppc_stub_plt_call_r2save,
05d0e962
AM
2999 ppc_stub_plt_call_notoc,
3000 ppc_stub_plt_call_both,
a4b6fadd
AM
3001 ppc_stub_global_entry,
3002 ppc_stub_save_res
721956f4
AM
3003};
3004
6f20ed8a
AM
3005/* Information on stub grouping. */
3006struct map_stub
3007{
3008 /* The stub section. */
3009 asection *stub_sec;
3010 /* This is the section to which stubs in the group will be attached. */
3011 asection *link_sec;
a4b6fadd
AM
3012 /* Next group. */
3013 struct map_stub *next;
3014 /* Whether to emit a copy of register save/restore functions in this
3015 group. */
3016 int needs_save_res;
df136d64
AM
3017 /* Current offset within stubs after the insn restoring lr in a
3018 _notoc or _both stub using bcl for pc-relative addressing, or
3019 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3020 unsigned int lr_restore;
3021 /* Accumulated size of EH info emitted to describe return address
3022 if stubs modify lr. Does not include 17 byte FDE header. */
3023 unsigned int eh_size;
3024 /* Offset in glink_eh_frame to the start of EH info for this group. */
3025 unsigned int eh_base;
6f20ed8a
AM
3026};
3027
2cdcc330
AM
3028struct ppc_stub_hash_entry
3029{
721956f4
AM
3030 /* Base hash table entry structure. */
3031 struct bfd_hash_entry root;
3032
ad8e1ba5
AM
3033 enum ppc_stub_type stub_type;
3034
6f20ed8a
AM
3035 /* Group information. */
3036 struct map_stub *group;
721956f4
AM
3037
3038 /* Offset within stub_sec of the beginning of this stub. */
3039 bfd_vma stub_offset;
3040
3041 /* Given the symbol's value and its section we can determine its final
3042 value when building the stubs (so the stub knows where to jump. */
3043 bfd_vma target_value;
3044 asection *target_section;
3045
721956f4
AM
3046 /* The symbol table entry, if any, that this was derived from. */
3047 struct ppc_link_hash_entry *h;
e054468f 3048 struct plt_entry *plt_ent;
721956f4 3049
2d7ad24e
AM
3050 /* Symbol type. */
3051 unsigned char symtype;
3052
6911b7dc
AM
3053 /* Symbol st_other. */
3054 unsigned char other;
721956f4
AM
3055};
3056
2cdcc330
AM
3057struct ppc_branch_hash_entry
3058{
721956f4
AM
3059 /* Base hash table entry structure. */
3060 struct bfd_hash_entry root;
3061
c456f082 3062 /* Offset within branch lookup table. */
721956f4
AM
3063 unsigned int offset;
3064
3065 /* Generation marker. */
3066 unsigned int iter;
3067};
65f38f15 3068
19e08130
AM
3069/* Used to track dynamic relocations for local symbols. */
3070struct ppc_dyn_relocs
3071{
3072 struct ppc_dyn_relocs *next;
3073
3074 /* The input section of the reloc. */
3075 asection *sec;
3076
3077 /* Total number of relocs copied for the input section. */
3078 unsigned int count : 31;
3079
3080 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3081 unsigned int ifunc : 1;
3082};
3083
65f38f15
AM
3084struct ppc_link_hash_entry
3085{
3086 struct elf_link_hash_entry elf;
3087
2cdcc330
AM
3088 union
3089 {
b3fac117
AM
3090 /* A pointer to the most recently used stub hash entry against this
3091 symbol. */
3092 struct ppc_stub_hash_entry *stub_cache;
3093
3094 /* A pointer to the next symbol starting with a '.' */
3095 struct ppc_link_hash_entry *next_dot_sym;
3096 } u;
721956f4 3097
721956f4 3098 /* Link between function code and descriptor symbols. */
34814b9f 3099 struct ppc_link_hash_entry *oh;
721956f4 3100
e86ce104
AM
3101 /* Flag function code and descriptor symbols. */
3102 unsigned int is_func:1;
3103 unsigned int is_func_descriptor:1;
908b32fc 3104 unsigned int fake:1;
411e1bfb 3105
c5614fa4
AM
3106 /* Whether global opd/toc sym has been adjusted or not.
3107 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3108 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3109 unsigned int adjust_done:1;
3110
a4b6fadd
AM
3111 /* Set if this is an out-of-line register save/restore function,
3112 with non-standard calling convention. */
3113 unsigned int save_res:1;
3114
8b5f1ed8
AM
3115 /* Set if a duplicate symbol with non-zero localentry is detected,
3116 even when the duplicate symbol does not provide a definition. */
3117 unsigned int non_zero_localentry:1;
3118
411e1bfb 3119 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3120 Bits are or'd into the mask as the corresponding relocs are
3121 encountered during check_relocs, with TLS_TLS being set when any
3122 of the other TLS bits are set. tls_optimize clears bits when
3123 optimizing to indicate the corresponding GOT entry type is not
3124 needed. If set, TLS_TLS is never cleared. tls_optimize may also
b00a0a86 3125 set TLS_GDIE when a GD reloc turns into an IE one.
37da22e5
AM
3126 These flags are also kept for local symbols. */
3127#define TLS_TLS 1 /* Any TLS reloc. */
3128#define TLS_GD 2 /* GD reloc. */
3129#define TLS_LD 4 /* LD reloc. */
3130#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3131#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3132#define TLS_MARK 32 /* __tls_get_addr call marked. */
b00a0a86 3133#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
46e9995a 3134#define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
f961d9dd 3135 unsigned char tls_mask;
37da22e5
AM
3136
3137 /* The above field is also used to mark function symbols. In which
3138 case TLS_TLS will be 0. */
3139#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3140#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3141#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3142};
3143
ed7007c1
AM
3144static inline struct ppc_link_hash_entry *
3145ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3146{
3147 return (struct ppc_link_hash_entry *) ent;
3148}
3149
65f38f15
AM
3150/* ppc64 ELF linker hash table. */
3151
3152struct ppc_link_hash_table
3153{
3154 struct elf_link_hash_table elf;
3155
721956f4
AM
3156 /* The stub hash table. */
3157 struct bfd_hash_table stub_hash_table;
3158
3159 /* Another hash table for plt_branch stubs. */
3160 struct bfd_hash_table branch_hash_table;
3161
3b421ab3
AM
3162 /* Hash table for function prologue tocsave. */
3163 htab_t tocsave_htab;
3164
e7d1c40c
AM
3165 /* Various options and other info passed from the linker. */
3166 struct ppc64_elf_params *params;
721956f4 3167
6f20ed8a
AM
3168 /* The size of sec_info below. */
3169 unsigned int sec_info_arr_size;
3170
3171 /* Per-section array of extra section info. Done this way rather
3172 than as part of ppc64_elf_section_data so we have the info for
3173 non-ppc64 sections. */
3174 struct
3175 {
3176 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3177 bfd_vma toc_off;
6f20ed8a
AM
3178
3179 union
3180 {
3181 /* The section group that this section belongs to. */
3182 struct map_stub *group;
3183 /* A temp section list pointer. */
3184 asection *list;
3185 } u;
3186 } *sec_info;
721956f4 3187
a4b6fadd
AM
3188 /* Linked list of groups. */
3189 struct map_stub *group;
3190
ad8e1ba5
AM
3191 /* Temp used when calculating TOC pointers. */
3192 bfd_vma toc_curr;
bf102f86
AM
3193 bfd *toc_bfd;
3194 asection *toc_first_sec;
ad8e1ba5 3195
b3fac117
AM
3196 /* Used when adding symbols. */
3197 struct ppc_link_hash_entry *dot_syms;
3198
33e44f2e 3199 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3200 asection *glink;
9e390558 3201 asection *global_entry;
82bd7b59 3202 asection *sfpr;
2d7ad24e
AM
3203 asection *pltlocal;
3204 asection *relpltlocal;
4ce794b7
AM
3205 asection *brlt;
3206 asection *relbrlt;
58d180e8 3207 asection *glink_eh_frame;
ec338859 3208
8387904d
AM
3209 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3210 struct ppc_link_hash_entry *tls_get_addr;
3211 struct ppc_link_hash_entry *tls_get_addr_fd;
9e7028aa
AM
3212 struct ppc_link_hash_entry *tga_desc;
3213 struct ppc_link_hash_entry *tga_desc_fd;
a804e476 3214 struct map_stub *tga_group;
411e1bfb 3215
927be08e
AM
3216 /* The size of reliplt used by got entry relocs. */
3217 bfd_size_type got_reli_size;
3218
9b5ecbd0 3219 /* Statistics. */
7341d5e2 3220 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 3221
ee75fd95
AM
3222 /* Number of stubs against global syms. */
3223 unsigned long stub_globals;
3224
ee67d69a
AM
3225 /* Set if we're linking code with function descriptors. */
3226 unsigned int opd_abi:1;
3227
4c52953f 3228 /* Support for multiple toc sections. */
33c0ec9d 3229 unsigned int do_multi_toc:1;
4c52953f 3230 unsigned int multi_toc_needed:1;
927be08e 3231 unsigned int second_toc_pass:1;
67f0cbdb 3232 unsigned int do_toc_opt:1;
4c52953f 3233
9a23f96e
AM
3234 /* Set if tls optimization is enabled. */
3235 unsigned int do_tls_opt:1;
3236
3e04d765
AM
3237 /* Set if inline plt calls should be converted to direct calls. */
3238 unsigned int can_convert_all_inline_plt:1;
3239
5d1634d7 3240 /* Set on error. */
99877b66 3241 unsigned int stub_error:1;
721956f4 3242
8c5b4e52
AM
3243 /* Whether func_desc_adjust needs to be run over symbols. */
3244 unsigned int need_func_desc_adj:1;
721956f4 3245
f378ab09
AM
3246 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3247 unsigned int has_plt_localentry0:1;
3248
5663e321
AM
3249 /* Whether calls are made via the PLT from NOTOC functions. */
3250 unsigned int notoc_plt:1;
3251
e10a07b3
AM
3252 /* Whether any code linked seems to be Power10. */
3253 unsigned int has_power10_relocs:1;
3254
721956f4
AM
3255 /* Incremented every time we size stubs. */
3256 unsigned int stub_iteration;
65f38f15
AM
3257};
3258
4c52953f
AM
3259/* Rename some of the generic section flags to better document how they
3260 are used here. */
b0dddeec
AM
3261
3262/* Nonzero if this section has TLS related relocations. */
3263#define has_tls_reloc sec_flg0
3264
9737e8af
AM
3265/* Nonzero if this section has a call to __tls_get_addr lacking marker
3266 relocations. */
3267#define nomark_tls_get_addr sec_flg1
b0dddeec
AM
3268
3269/* Nonzero if this section has any toc or got relocs. */
3270#define has_toc_reloc sec_flg2
3271
3272/* Nonzero if this section has a call to another section that uses
3273 the toc or got. */
d77c8a4b 3274#define makes_toc_func_call sec_flg3
b0dddeec
AM
3275
3276/* Recursion protection when determining above flag. */
d77c8a4b 3277#define call_check_in_progress sec_flg4
70cc837d 3278#define call_check_done sec_flg5
4c52953f 3279
65f38f15
AM
3280/* Get the ppc64 ELF linker hash table from a link_info structure. */
3281
3282#define ppc_hash_table(p) \
573eb292
AM
3283 ((is_elf_hash_table ((p)->hash) \
3284 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3285 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
65f38f15 3286
721956f4
AM
3287#define ppc_stub_hash_lookup(table, string, create, copy) \
3288 ((struct ppc_stub_hash_entry *) \
3289 bfd_hash_lookup ((table), (string), (create), (copy)))
3290
3291#define ppc_branch_hash_lookup(table, string, create, copy) \
3292 ((struct ppc_branch_hash_entry *) \
3293 bfd_hash_lookup ((table), (string), (create), (copy)))
3294
3295/* Create an entry in the stub hash table. */
3296
3297static struct bfd_hash_entry *
4ce794b7
AM
3298stub_hash_newfunc (struct bfd_hash_entry *entry,
3299 struct bfd_hash_table *table,
3300 const char *string)
721956f4
AM
3301{
3302 /* Allocate the structure if it has not already been allocated by a
3303 subclass. */
3304 if (entry == NULL)
3305 {
3306 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3307 if (entry == NULL)
3308 return entry;
3309 }
3310
3311 /* Call the allocation method of the superclass. */
3312 entry = bfd_hash_newfunc (entry, table, string);
3313 if (entry != NULL)
3314 {
3315 struct ppc_stub_hash_entry *eh;
3316
3317 /* Initialize the local fields. */
3318 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3319 eh->stub_type = ppc_stub_none;
6f20ed8a 3320 eh->group = NULL;
721956f4
AM
3321 eh->stub_offset = 0;
3322 eh->target_value = 0;
3323 eh->target_section = NULL;
721956f4 3324 eh->h = NULL;
6911b7dc 3325 eh->plt_ent = NULL;
6911b7dc 3326 eh->other = 0;
721956f4
AM
3327 }
3328
3329 return entry;
3330}
3331
3332/* Create an entry in the branch hash table. */
3333
3334static struct bfd_hash_entry *
4ce794b7
AM
3335branch_hash_newfunc (struct bfd_hash_entry *entry,
3336 struct bfd_hash_table *table,
3337 const char *string)
721956f4
AM
3338{
3339 /* Allocate the structure if it has not already been allocated by a
3340 subclass. */
3341 if (entry == NULL)
3342 {
3343 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3344 if (entry == NULL)
3345 return entry;
3346 }
3347
3348 /* Call the allocation method of the superclass. */
3349 entry = bfd_hash_newfunc (entry, table, string);
3350 if (entry != NULL)
3351 {
3352 struct ppc_branch_hash_entry *eh;
3353
3354 /* Initialize the local fields. */
3355 eh = (struct ppc_branch_hash_entry *) entry;
3356 eh->offset = 0;
3357 eh->iter = 0;
3358 }
3359
3360 return entry;
3361}
3362
65f38f15
AM
3363/* Create an entry in a ppc64 ELF linker hash table. */
3364
3365static struct bfd_hash_entry *
4ce794b7
AM
3366link_hash_newfunc (struct bfd_hash_entry *entry,
3367 struct bfd_hash_table *table,
3368 const char *string)
65f38f15
AM
3369{
3370 /* Allocate the structure if it has not already been allocated by a
3371 subclass. */
3372 if (entry == NULL)
3373 {
3374 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3375 if (entry == NULL)
3376 return entry;
3377 }
3378
3379 /* Call the allocation method of the superclass. */
3380 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3381 if (entry != NULL)
3382 {
3383 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3384
b3fac117 3385 memset (&eh->u.stub_cache, 0,
908b32fc 3386 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3387 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3388
3389 /* When making function calls, old ABI code references function entry
3390 points (dot symbols), while new ABI code references the function
3391 descriptor symbol. We need to make any combination of reference and
3392 definition work together, without breaking archive linking.
3393
3394 For a defined function "foo" and an undefined call to "bar":
3395 An old object defines "foo" and ".foo", references ".bar" (possibly
3396 "bar" too).
3397 A new object defines "foo" and references "bar".
3398
3399 A new object thus has no problem with its undefined symbols being
3400 satisfied by definitions in an old object. On the other hand, the
3401 old object won't have ".bar" satisfied by a new object.
3402
3403 Keep a list of newly added dot-symbols. */
3404
3405 if (string[0] == '.')
3406 {
3407 struct ppc_link_hash_table *htab;
3408
3409 htab = (struct ppc_link_hash_table *) table;
3410 eh->u.next_dot_sym = htab->dot_syms;
3411 htab->dot_syms = eh;
3412 }
65f38f15
AM
3413 }
3414
3415 return entry;
3416}
3417
2cdcc330
AM
3418struct tocsave_entry
3419{
3b421ab3
AM
3420 asection *sec;
3421 bfd_vma offset;
3422};
3423
3424static hashval_t
3425tocsave_htab_hash (const void *p)
3426{
3427 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3428 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3429}
3430
3431static int
3432tocsave_htab_eq (const void *p1, const void *p2)
3433{
3434 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3435 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3436 return e1->sec == e2->sec && e1->offset == e2->offset;
3437}
3438
68faa637
AM
3439/* Destroy a ppc64 ELF linker hash table. */
3440
3441static void
d495ab0d 3442ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3443{
d495ab0d 3444 struct ppc_link_hash_table *htab;
68faa637 3445
d495ab0d 3446 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3447 if (htab->tocsave_htab)
3448 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3449 bfd_hash_table_free (&htab->branch_hash_table);
3450 bfd_hash_table_free (&htab->stub_hash_table);
3451 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3452}
3453
65f38f15
AM
3454/* Create a ppc64 ELF linker hash table. */
3455
3456static struct bfd_link_hash_table *
4ce794b7 3457ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3458{
3459 struct ppc_link_hash_table *htab;
986f0783 3460 size_t amt = sizeof (struct ppc_link_hash_table);
65f38f15 3461
4ce794b7 3462 htab = bfd_zmalloc (amt);
65f38f15
AM
3463 if (htab == NULL)
3464 return NULL;
3465
66eb6687 3466 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3467 sizeof (struct ppc_link_hash_entry),
3468 PPC64_ELF_DATA))
65f38f15 3469 {
e2d34d7d 3470 free (htab);
65f38f15
AM
3471 return NULL;
3472 }
3473
721956f4 3474 /* Init the stub hash table too. */
66eb6687
AM
3475 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3476 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3477 {
d495ab0d 3478 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3479 return NULL;
3480 }
721956f4
AM
3481
3482 /* And the branch hash table. */
66eb6687
AM
3483 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3484 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3485 {
3486 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3487 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3488 return NULL;
3489 }
721956f4 3490
3b421ab3
AM
3491 htab->tocsave_htab = htab_try_create (1024,
3492 tocsave_htab_hash,
3493 tocsave_htab_eq,
3494 NULL);
3495 if (htab->tocsave_htab == NULL)
2915c55b 3496 {
d495ab0d 3497 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3498 return NULL;
3499 }
d495ab0d 3500 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3501
3254fd24
AM
3502 /* Initializing two fields of the union is just cosmetic. We really
3503 only care about glist, but when compiled on a 32-bit host the
3504 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3505 debugger inspection of these fields look nicer. */
a6aa5195
AM
3506 htab->elf.init_got_refcount.refcount = 0;
3507 htab->elf.init_got_refcount.glist = NULL;
3508 htab->elf.init_plt_refcount.refcount = 0;
3509 htab->elf.init_plt_refcount.glist = NULL;
3510 htab->elf.init_got_offset.offset = 0;
3511 htab->elf.init_got_offset.glist = NULL;
3512 htab->elf.init_plt_offset.offset = 0;
3513 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3514
65f38f15
AM
3515 return &htab->elf.root;
3516}
3517
bfeb4a28
AM
3518/* Create sections for linker generated code. */
3519
3520static bfd_boolean
3521create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3522{
3523 struct ppc_link_hash_table *htab;
3524 flagword flags;
3525
3526 htab = ppc_hash_table (info);
3527
bfeb4a28
AM
3528 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3529 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3530 if (htab->params->save_restore_funcs)
3531 {
3532 /* Create .sfpr for code to save and restore fp regs. */
3533 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3534 flags);
3535 if (htab->sfpr == NULL
fd361982 3536 || !bfd_set_section_alignment (htab->sfpr, 2))
b32547cd
AM
3537 return FALSE;
3538 }
3539
3540 if (bfd_link_relocatable (info))
3541 return TRUE;
bfeb4a28
AM
3542
3543 /* Create .glink for lazy dynamic linking support. */
3544 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3545 flags);
3546 if (htab->glink == NULL
fd361982 3547 || !bfd_set_section_alignment (htab->glink, 3))
bfeb4a28
AM
3548 return FALSE;
3549
9e390558
AM
3550 /* The part of .glink used by global entry stubs, separate so that
3551 it can be aligned appropriately without affecting htab->glink. */
3552 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3553 flags);
3554 if (htab->global_entry == NULL
fd361982 3555 || !bfd_set_section_alignment (htab->global_entry, 2))
9e390558
AM
3556 return FALSE;
3557
bfeb4a28
AM
3558 if (!info->no_ld_generated_unwind_info)
3559 {
3560 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3561 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3562 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3563 ".eh_frame",
3564 flags);
3565 if (htab->glink_eh_frame == NULL
fd361982 3566 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
bfeb4a28
AM
3567 return FALSE;
3568 }
3569
3570 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3571 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3572 if (htab->elf.iplt == NULL
fd361982 3573 || !bfd_set_section_alignment (htab->elf.iplt, 3))
bfeb4a28
AM
3574 return FALSE;
3575
3576 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3577 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3578 htab->elf.irelplt
3579 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3580 if (htab->elf.irelplt == NULL
fd361982 3581 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
bfeb4a28
AM
3582 return FALSE;
3583
3584 /* Create branch lookup table for plt_branch stubs. */
3585 flags = (SEC_ALLOC | SEC_LOAD
3586 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3587 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3588 flags);
3589 if (htab->brlt == NULL
fd361982 3590 || !bfd_set_section_alignment (htab->brlt, 3))
bfeb4a28
AM
3591 return FALSE;
3592
2d7ad24e
AM
3593 /* Local plt entries, put in .branch_lt but a separate section for
3594 convenience. */
3595 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3596 flags);
3597 if (htab->pltlocal == NULL
fd361982 3598 || !bfd_set_section_alignment (htab->pltlocal, 3))
2d7ad24e
AM
3599 return FALSE;
3600
0e1862bb 3601 if (!bfd_link_pic (info))
bfeb4a28
AM
3602 return TRUE;
3603
3604 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3605 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3606 htab->relbrlt
3607 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3608 if (htab->relbrlt == NULL
fd361982 3609 || !bfd_set_section_alignment (htab->relbrlt, 3))
bfeb4a28
AM
3610 return FALSE;
3611
2d7ad24e
AM
3612 htab->relpltlocal
3613 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3614 if (htab->relpltlocal == NULL
fd361982 3615 || !bfd_set_section_alignment (htab->relpltlocal, 3))
2d7ad24e
AM
3616 return FALSE;
3617
bfeb4a28
AM
3618 return TRUE;
3619}
3620
e717da7e
AM
3621/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3622
bfeb4a28 3623bfd_boolean
e7d1c40c
AM
3624ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3625 struct ppc64_elf_params *params)
e717da7e
AM
3626{
3627 struct ppc_link_hash_table *htab;
3628
e7d1c40c 3629 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3630
3631/* Always hook our dynamic sections into the first bfd, which is the
3632 linker created stub bfd. This ensures that the GOT header is at
3633 the start of the output TOC section. */
3634 htab = ppc_hash_table (info);
e7d1c40c
AM
3635 htab->elf.dynobj = params->stub_bfd;
3636 htab->params = params;
bfeb4a28 3637
bfeb4a28 3638 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3639}
3640
721956f4
AM
3641/* Build a name for an entry in the stub hash table. */
3642
3643static char *
4ce794b7
AM
3644ppc_stub_name (const asection *input_section,
3645 const asection *sym_sec,
3646 const struct ppc_link_hash_entry *h,
3647 const Elf_Internal_Rela *rel)
721956f4
AM
3648{
3649 char *stub_name;
bcaa2f82 3650 ssize_t len;
721956f4
AM
3651
3652 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3653 offsets from a sym as a branch target? In fact, we could
3654 probably assume the addend is always zero. */
3655 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3656
3657 if (h)
3658 {
3659 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3660 stub_name = bfd_malloc (len);
46de2a7c
AM
3661 if (stub_name == NULL)
3662 return stub_name;
3663
bcaa2f82
AM
3664 len = sprintf (stub_name, "%08x.%s+%x",
3665 input_section->id & 0xffffffff,
3666 h->elf.root.root.string,
3667 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3668 }
3669 else
3670 {
ad8e1ba5 3671 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3672 stub_name = bfd_malloc (len);
46de2a7c
AM
3673 if (stub_name == NULL)
3674 return stub_name;
3675
bcaa2f82
AM
3676 len = sprintf (stub_name, "%08x.%x:%x+%x",
3677 input_section->id & 0xffffffff,
3678 sym_sec->id & 0xffffffff,
3679 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3680 (int) rel->r_addend & 0xffffffff);
721956f4 3681 }
bcaa2f82 3682 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3683 stub_name[len - 2] = 0;
721956f4
AM
3684 return stub_name;
3685}
3686
e10a07b3
AM
3687/* If mixing power10 with non-power10 code and --power10-stubs is not
3688 specified (or is auto) then calls using @notoc relocations that
3689 need a stub will utilize power10 instructions in the stub, and
3690 calls without @notoc relocations will not use power10 instructions.
3691 The two classes of stubs are stored in separate stub_hash_table
3692 entries having the same key string. The two entries will always be
3693 adjacent on entry->root.next chain, even if hash table resizing
3694 occurs. This function selects the correct entry to use. */
3695
3696static struct ppc_stub_hash_entry *
3697select_alt_stub (struct ppc_stub_hash_entry *entry, bfd_boolean notoc)
3698{
3699 bfd_boolean have_notoc;
3700
3701 have_notoc = (entry->stub_type == ppc_stub_plt_call_notoc
3702 || entry->stub_type == ppc_stub_plt_branch_notoc
3703 || entry->stub_type == ppc_stub_long_branch_notoc);
3704
3705 if (have_notoc != notoc)
3706 {
3707 const char *stub_name = entry->root.string;
3708
3709 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3710 if (entry != NULL
3711 && entry->root.string != stub_name)
3712 entry = NULL;
3713 }
3714
3715 return entry;
3716}
3717
721956f4
AM
3718/* Look up an entry in the stub hash. Stub entries are cached because
3719 creating the stub name takes a bit of time. */
3720
3721static struct ppc_stub_hash_entry *
4ce794b7
AM
3722ppc_get_stub_entry (const asection *input_section,
3723 const asection *sym_sec,
039b3fef 3724 struct ppc_link_hash_entry *h,
4ce794b7
AM
3725 const Elf_Internal_Rela *rel,
3726 struct ppc_link_hash_table *htab)
721956f4
AM
3727{
3728 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3729 struct map_stub *group;
721956f4
AM
3730
3731 /* If this input section is part of a group of sections sharing one
3732 stub section, then use the id of the first section in the group.
3733 Stub names need to include a section id, as there may well be
3734 more than one stub used to reach say, printf, and we need to
3735 distinguish between them. */
6f20ed8a 3736 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3737 if (group == NULL)
3738 return NULL;
721956f4 3739
b3fac117
AM
3740 if (h != NULL && h->u.stub_cache != NULL
3741 && h->u.stub_cache->h == h
6f20ed8a 3742 && h->u.stub_cache->group == group)
721956f4 3743 {
b3fac117 3744 stub_entry = h->u.stub_cache;
721956f4
AM
3745 }
3746 else
3747 {
3748 char *stub_name;
3749
6f20ed8a 3750 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3751 if (stub_name == NULL)
3752 return NULL;
3753
3754 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3755 stub_name, FALSE, FALSE);
721956f4 3756 if (h != NULL)
b3fac117 3757 h->u.stub_cache = stub_entry;
721956f4
AM
3758
3759 free (stub_name);
3760 }
3761
e10a07b3
AM
3762 if (stub_entry != NULL && htab->params->power10_stubs == -1)
3763 {
3764 bfd_boolean notoc = ELF64_R_TYPE (rel->r_info) == R_PPC64_REL24_NOTOC;
3765
3766 stub_entry = select_alt_stub (stub_entry, notoc);
3767 }
3768
721956f4
AM
3769 return stub_entry;
3770}
3771
3772/* Add a new stub entry to the stub hash. Not all fields of the new
3773 stub entry are initialised. */
3774
3775static struct ppc_stub_hash_entry *
4ce794b7
AM
3776ppc_add_stub (const char *stub_name,
3777 asection *section,
25f53a85 3778 struct bfd_link_info *info)
721956f4 3779{
25f53a85 3780 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3781 struct map_stub *group;
721956f4
AM
3782 asection *link_sec;
3783 asection *stub_sec;
3784 struct ppc_stub_hash_entry *stub_entry;
3785
6f20ed8a
AM
3786 group = htab->sec_info[section->id].u.group;
3787 link_sec = group->link_sec;
3788 stub_sec = group->stub_sec;
721956f4
AM
3789 if (stub_sec == NULL)
3790 {
6f20ed8a
AM
3791 size_t namelen;
3792 bfd_size_type len;
3793 char *s_name;
721956f4 3794
6f20ed8a
AM
3795 namelen = strlen (link_sec->name);
3796 len = namelen + sizeof (STUB_SUFFIX);
3797 s_name = bfd_alloc (htab->params->stub_bfd, len);
3798 if (s_name == NULL)
3799 return NULL;
721956f4 3800
6f20ed8a
AM
3801 memcpy (s_name, link_sec->name, namelen);
3802 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3803 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3804 if (stub_sec == NULL)
3805 return NULL;
3806 group->stub_sec = stub_sec;
721956f4
AM
3807 }
3808
3809 /* Enter this entry into the linker stub hash table. */
3810 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3811 TRUE, FALSE);
721956f4
AM
3812 if (stub_entry == NULL)
3813 {
695344c0 3814 /* xgettext:c-format */
cf97bcb0
AM
3815 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3816 section->owner, stub_name);
721956f4
AM
3817 return NULL;
3818 }
3819
6f20ed8a 3820 stub_entry->group = group;
721956f4 3821 stub_entry->stub_offset = 0;
721956f4
AM
3822 return stub_entry;
3823}
3824
e717da7e
AM
3825/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3826 not already done. */
65f38f15 3827
b34976b6 3828static bfd_boolean
e717da7e 3829create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3830{
e717da7e
AM
3831 asection *got, *relgot;
3832 flagword flags;
3833 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3834
0c8d6e5c 3835 if (!is_ppc64_elf (abfd))
0ffa91dd 3836 return FALSE;
4dfe6ac6
NC
3837 if (htab == NULL)
3838 return FALSE;
0ffa91dd 3839
33e44f2e
AM
3840 if (!htab->elf.sgot
3841 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3842 return FALSE;
e717da7e
AM
3843
3844 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3845 | SEC_LINKER_CREATED);
3846
c456f082 3847 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3848 if (!got
fd361982 3849 || !bfd_set_section_alignment (got, 3))
e717da7e 3850 return FALSE;
65f38f15 3851
c456f082
AM
3852 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3853 flags | SEC_READONLY);
e717da7e 3854 if (!relgot
fd361982 3855 || !bfd_set_section_alignment (relgot, 3))
b34976b6 3856 return FALSE;
e717da7e
AM
3857
3858 ppc64_elf_tdata (abfd)->got = got;
3859 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 3860 return TRUE;
65f38f15 3861}
5bd4f169 3862
b31867b6
AM
3863/* Follow indirect and warning symbol links. */
3864
3865static inline struct bfd_link_hash_entry *
3866follow_link (struct bfd_link_hash_entry *h)
3867{
3868 while (h->type == bfd_link_hash_indirect
3869 || h->type == bfd_link_hash_warning)
3870 h = h->u.i.link;
3871 return h;
3872}
3873
3874static inline struct elf_link_hash_entry *
3875elf_follow_link (struct elf_link_hash_entry *h)
3876{
3877 return (struct elf_link_hash_entry *) follow_link (&h->root);
3878}
3879
3880static inline struct ppc_link_hash_entry *
3881ppc_follow_link (struct ppc_link_hash_entry *h)
3882{
ed7007c1 3883 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
b31867b6
AM
3884}
3885
40d16e0b
AM
3886/* Merge PLT info on FROM with that on TO. */
3887
3888static void
3889move_plt_plist (struct ppc_link_hash_entry *from,
3890 struct ppc_link_hash_entry *to)
3891{
3892 if (from->elf.plt.plist != NULL)
3893 {
3894 if (to->elf.plt.plist != NULL)
3895 {
3896 struct plt_entry **entp;
3897 struct plt_entry *ent;
3898
3899 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3900 {
3901 struct plt_entry *dent;
3902
3903 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3904 if (dent->addend == ent->addend)
3905 {
3906 dent->plt.refcount += ent->plt.refcount;
3907 *entp = ent->next;
3908 break;
3909 }
3910 if (dent == NULL)
3911 entp = &ent->next;
3912 }
3913 *entp = to->elf.plt.plist;
3914 }
3915
3916 to->elf.plt.plist = from->elf.plt.plist;
3917 from->elf.plt.plist = NULL;
3918 }
3919}
3920
65f38f15
AM
3921/* Copy the extra info we tack onto an elf_link_hash_entry. */
3922
3923static void
fcfa13d2
AM
3924ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3925 struct elf_link_hash_entry *dir,
3926 struct elf_link_hash_entry *ind)
65f38f15
AM
3927{
3928 struct ppc_link_hash_entry *edir, *eind;
3929
ed7007c1
AM
3930 edir = ppc_elf_hash_entry (dir);
3931 eind = ppc_elf_hash_entry (ind);
65f38f15 3932
c79d6685
AM
3933 edir->is_func |= eind->is_func;
3934 edir->is_func_descriptor |= eind->is_func_descriptor;
3935 edir->tls_mask |= eind->tls_mask;
3936 if (eind->oh != NULL)
3937 edir->oh = ppc_follow_link (eind->oh);
3938
474436e6 3939 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
3940 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3941 edir->elf.ref_regular |= eind->elf.ref_regular;
3942 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 3943 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
3944 edir->elf.needs_plt |= eind->elf.needs_plt;
3945 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 3946
d311bc8b
AM
3947 /* If we were called to copy over info for a weak sym, don't copy
3948 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3949 in order to simplify readonly_dynrelocs and save a field in the
3950 symbol hash entry, but that means dyn_relocs can't be used in any
3951 tests about a specific symbol, or affect other symbol flags which
ab2477e1 3952 are then tested. */
d311bc8b 3953 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 3954 return;
d311bc8b 3955
411e1bfb 3956 /* Copy over any dynamic relocs we may have on the indirect sym. */
190eb1dd 3957 if (ind->dyn_relocs != NULL)
65f38f15 3958 {
190eb1dd 3959 if (dir->dyn_relocs != NULL)
bbd7ec4a 3960 {
6061a67d
AM
3961 struct elf_dyn_relocs **pp;
3962 struct elf_dyn_relocs *p;
bbd7ec4a 3963
fcfa13d2 3964 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a 3965 list. Merge any entries against the same section. */
190eb1dd 3966 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
bbd7ec4a 3967 {
6061a67d 3968 struct elf_dyn_relocs *q;
bbd7ec4a 3969
190eb1dd 3970 for (q = dir->dyn_relocs; q != NULL; q = q->next)
bbd7ec4a
AM
3971 if (q->sec == p->sec)
3972 {
3973 q->pc_count += p->pc_count;
3974 q->count += p->count;
3975 *pp = p->next;
3976 break;
3977 }
3978 if (q == NULL)
3979 pp = &p->next;
3980 }
190eb1dd 3981 *pp = dir->dyn_relocs;
bbd7ec4a
AM
3982 }
3983
190eb1dd
L
3984 dir->dyn_relocs = ind->dyn_relocs;
3985 ind->dyn_relocs = NULL;
65f38f15 3986 }
65f38f15 3987
81848ca0
AM
3988 /* Copy over got entries that we may have already seen to the
3989 symbol which just became indirect. */
411e1bfb
AM
3990 if (eind->elf.got.glist != NULL)
3991 {
3992 if (edir->elf.got.glist != NULL)
3993 {
3994 struct got_entry **entp;
3995 struct got_entry *ent;
3996
3997 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3998 {
3999 struct got_entry *dent;
4000
4001 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4002 if (dent->addend == ent->addend
e717da7e 4003 && dent->owner == ent->owner
411e1bfb
AM
4004 && dent->tls_type == ent->tls_type)
4005 {
4006 dent->got.refcount += ent->got.refcount;
4007 *entp = ent->next;
4008 break;
4009 }
4010 if (dent == NULL)
4011 entp = &ent->next;
4012 }
4013 *entp = edir->elf.got.glist;
4014 }
4015
4016 edir->elf.got.glist = eind->elf.got.glist;
4017 eind->elf.got.glist = NULL;
4018 }
4019
4020 /* And plt entries. */
40d16e0b 4021 move_plt_plist (eind, edir);
411e1bfb 4022
fcfa13d2 4023 if (eind->elf.dynindx != -1)
411e1bfb 4024 {
fcfa13d2
AM
4025 if (edir->elf.dynindx != -1)
4026 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4027 edir->elf.dynstr_index);
411e1bfb
AM
4028 edir->elf.dynindx = eind->elf.dynindx;
4029 edir->elf.dynstr_index = eind->elf.dynstr_index;
4030 eind->elf.dynindx = -1;
4031 eind->elf.dynstr_index = 0;
4032 }
411e1bfb
AM
4033}
4034
8387904d
AM
4035/* Find the function descriptor hash entry from the given function code
4036 hash entry FH. Link the entries via their OH fields. */
4037
4038static struct ppc_link_hash_entry *
b31867b6 4039lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4040{
4041 struct ppc_link_hash_entry *fdh = fh->oh;
4042
4043 if (fdh == NULL)
4044 {
4045 const char *fd_name = fh->elf.root.root.string + 1;
4046
ed7007c1
AM
4047 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4048 FALSE, FALSE, FALSE));
b31867b6
AM
4049 if (fdh == NULL)
4050 return fdh;
4051
4052 fdh->is_func_descriptor = 1;
4053 fdh->oh = fh;
4054 fh->is_func = 1;
4055 fh->oh = fdh;
8387904d
AM
4056 }
4057
8c5b4e52
AM
4058 fdh = ppc_follow_link (fdh);
4059 fdh->is_func_descriptor = 1;
4060 fdh->oh = fh;
4061 return fdh;
8387904d
AM
4062}
4063
8c5b4e52 4064/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4065
4066static struct ppc_link_hash_entry *
4067make_fdh (struct bfd_link_info *info,
908b32fc 4068 struct ppc_link_hash_entry *fh)
bb700d78 4069{
8c5b4e52
AM
4070 bfd *abfd = fh->elf.root.u.undef.abfd;
4071 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4072 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4073 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4074 ? BSF_WEAK
4075 : BSF_GLOBAL);
4076
4077 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4078 fh->elf.root.root.string + 1,
4079 flags, bfd_und_section_ptr, 0,
4080 NULL, FALSE, FALSE, &bh))
bb700d78
AM
4081 return NULL;
4082
4083 fdh = (struct ppc_link_hash_entry *) bh;
4084 fdh->elf.non_elf = 0;
908b32fc
AM
4085 fdh->fake = 1;
4086 fdh->is_func_descriptor = 1;
4087 fdh->oh = fh;
4088 fh->is_func = 1;
4089 fh->oh = fdh;
bb700d78
AM
4090 return fdh;
4091}
4092
8387904d
AM
4093/* Fix function descriptor symbols defined in .opd sections to be
4094 function type. */
555cd476
AM
4095
4096static bfd_boolean
c16153ae 4097ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4098 struct bfd_link_info *info,
555cd476 4099 Elf_Internal_Sym *isym,
6911b7dc 4100 const char **name,
555cd476
AM
4101 flagword *flags ATTRIBUTE_UNUSED,
4102 asection **sec,
b53dfeb2 4103 bfd_vma *value)
555cd476 4104{
b53dfeb2 4105 if (*sec != NULL
f1885d1e 4106 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4107 {
4108 asection *code_sec;
4109
4110 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4111 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4112 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4113
4114 /* If the symbol is a function defined in .opd, and the function
4115 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4116 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4117 && (*sec)->reloc_count != 0
4118 && opd_entry_value (*sec, *value, &code_sec, NULL,
4119 FALSE) != (bfd_vma) -1
4120 && discarded_section (code_sec))
4121 {
4122 *sec = bfd_und_section_ptr;
4123 isym->st_shndx = SHN_UNDEF;
4124 }
4125 }
dbd1e97e
AM
4126 else if (*sec != NULL
4127 && strcmp ((*sec)->name, ".toc") == 0
4128 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4129 {
4130 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4131 if (htab != NULL)
4132 htab->params->object_in_toc = 1;
4133 }
433817dd 4134
6911b7dc
AM
4135 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4136 {
4137 if (abiversion (ibfd) == 0)
4138 set_abiversion (ibfd, 2);
4139 else if (abiversion (ibfd) == 1)
4140 {
cf97bcb0
AM
4141 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4142 " for ABI version 1"), *name);
6911b7dc
AM
4143 bfd_set_error (bfd_error_bad_value);
4144 return FALSE;
4145 }
4146 }
4147
555cd476
AM
4148 return TRUE;
4149}
4150
6911b7dc
AM
4151/* Merge non-visibility st_other attributes: local entry point. */
4152
4153static void
4154ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4155 const Elf_Internal_Sym *isym,
4156 bfd_boolean definition,
4157 bfd_boolean dynamic)
4158{
f378ab09 4159 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
4160 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4161 | ELF_ST_VISIBILITY (h->other));
4162}
4163
8c5b4e52
AM
4164/* Hook called on merging a symbol. We use this to clear "fake" since
4165 we now have a real symbol. */
4166
4167static bfd_boolean
4168ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4169 const Elf_Internal_Sym *isym,
8c5b4e52
AM
4170 asection **psec ATTRIBUTE_UNUSED,
4171 bfd_boolean newdef ATTRIBUTE_UNUSED,
4172 bfd_boolean olddef ATTRIBUTE_UNUSED,
4173 bfd *oldbfd ATTRIBUTE_UNUSED,
4174 const asection *oldsec ATTRIBUTE_UNUSED)
4175{
ed7007c1 4176 ppc_elf_hash_entry (h)->fake = 0;
8b5f1ed8 4177 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
ed7007c1 4178 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
8c5b4e52
AM
4179 return TRUE;
4180}
4181
8387904d 4182/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4183 inclusion of a new ABI object archive that defines "bar".
4184 NAME is a symbol defined in an archive. Return a symbol in the hash
4185 table that might be satisfied by the archive symbols. */
8387904d
AM
4186
4187static struct elf_link_hash_entry *
4188ppc64_elf_archive_symbol_lookup (bfd *abfd,
4189 struct bfd_link_info *info,
4190 const char *name)
4191{
4192 struct elf_link_hash_entry *h;
4193 char *dot_name;
4194 size_t len;
4195
4196 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4197 if (h != NULL
4198 /* Don't return this sym if it is a fake function descriptor
4199 created by add_symbol_adjust. */
ed7007c1 4200 && !ppc_elf_hash_entry (h)->fake)
8387904d
AM
4201 return h;
4202
4203 if (name[0] == '.')
4204 return h;
4205
4206 len = strlen (name);
4207 dot_name = bfd_alloc (abfd, len + 2);
4208 if (dot_name == NULL)
e99955cd 4209 return (struct elf_link_hash_entry *) -1;
8387904d
AM
4210 dot_name[0] = '.';
4211 memcpy (dot_name + 1, name, len + 1);
4212 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4213 bfd_release (abfd, dot_name);
a804e476
AM
4214 if (h != NULL)
4215 return h;
4216
4217 if (strcmp (name, "__tls_get_addr_opt") == 0)
4218 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
8387904d
AM
4219 return h;
4220}
4221
4222/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4223 new ABI object defines "bar". Well, at least, undefined dot symbols
4224 are made weak. This stops later archive searches from including an
4225 object if we already have a function descriptor definition. It also
35b0ce59
AM
4226 prevents the linker complaining about undefined symbols.
4227 We also check and correct mismatched symbol visibility here. The
4228 most restrictive visibility of the function descriptor and the
4229 function entry symbol is used. */
8387904d
AM
4230
4231static bfd_boolean
b3fac117 4232add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4233{
8387904d
AM
4234 struct ppc_link_hash_table *htab;
4235 struct ppc_link_hash_entry *fdh;
4236
b3fac117
AM
4237 if (eh->elf.root.type == bfd_link_hash_warning)
4238 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4239
8400d40d
AM
4240 if (eh->elf.root.type == bfd_link_hash_indirect)
4241 return TRUE;
4242
b3fac117
AM
4243 if (eh->elf.root.root.string[0] != '.')
4244 abort ();
8387904d 4245
b3fac117 4246 htab = ppc_hash_table (info);
4dfe6ac6
NC
4247 if (htab == NULL)
4248 return FALSE;
4249
b31867b6 4250 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4251 if (fdh == NULL
4252 && !bfd_link_relocatable (info)
4253 && (eh->elf.root.type == bfd_link_hash_undefined
4254 || eh->elf.root.type == bfd_link_hash_undefweak)
4255 && eh->elf.ref_regular)
4256 {
4257 /* Make an undefined function descriptor sym, in order to
4258 pull in an --as-needed shared lib. Archives are handled
4259 elsewhere. */
4260 fdh = make_fdh (info, eh);
4261 if (fdh == NULL)
4262 return FALSE;
bb700d78 4263 }
8c5b4e52
AM
4264
4265 if (fdh != NULL)
8387904d 4266 {
35b0ce59
AM
4267 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4268 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4269
4270 /* Make both descriptor and entry symbol have the most
4271 constraining visibility of either symbol. */
35b0ce59
AM
4272 if (entry_vis < descr_vis)
4273 fdh->elf.other += entry_vis - descr_vis;
4274 else if (entry_vis > descr_vis)
4275 eh->elf.other += descr_vis - entry_vis;
4276
8c5b4e52
AM
4277 /* Propagate reference flags from entry symbol to function
4278 descriptor symbol. */
bc4e12de 4279 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4280 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4281 fdh->elf.ref_regular |= eh->elf.ref_regular;
4282 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4283
4284 if (!fdh->elf.forced_local
4285 && fdh->elf.dynindx == -1
4286 && fdh->elf.versioned != versioned_hidden
4287 && (bfd_link_dll (info)
4288 || fdh->elf.def_dynamic
4289 || fdh->elf.ref_dynamic)
4290 && (eh->elf.ref_regular
4291 || eh->elf.def_regular))
4292 {
2cdcc330 4293 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
8c5b4e52 4294 return FALSE;
35b0ce59 4295 }
8387904d 4296 }
99877b66 4297
8387904d
AM
4298 return TRUE;
4299}
4300
f6c7c3e8
AM
4301/* Set up opd section info and abiversion for IBFD, and process list
4302 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4303
8387904d 4304static bfd_boolean
f6c7c3e8 4305ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4306{
99877b66 4307 struct ppc_link_hash_table *htab;
b3fac117 4308 struct ppc_link_hash_entry **p, *eh;
459609d6 4309 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4310
459609d6 4311 if (opd != NULL && opd->size != 0)
b3fac117 4312 {
b9399fcf
AM
4313 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4314 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4315
459609d6
AM
4316 if (abiversion (ibfd) == 0)
4317 set_abiversion (ibfd, 1);
8a2058b5 4318 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4319 {
695344c0 4320 /* xgettext:c-format */
cf97bcb0
AM
4321 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4322 ibfd, abiversion (ibfd));
459609d6
AM
4323 bfd_set_error (bfd_error_bad_value);
4324 return FALSE;
f6c7c3e8 4325 }
b9399fcf 4326 }
f6c7c3e8 4327
b9399fcf
AM
4328 if (is_ppc64_elf (info->output_bfd))
4329 {
4330 /* For input files without an explicit abiversion in e_flags
4331 we should have flagged any with symbol st_other bits set
4332 as ELFv1 and above flagged those with .opd as ELFv2.
4333 Set the output abiversion if not yet set, and for any input
4334 still ambiguous, take its abiversion from the output.
4335 Differences in ABI are reported later. */
4336 if (abiversion (info->output_bfd) == 0)
4337 set_abiversion (info->output_bfd, abiversion (ibfd));
4338 else if (abiversion (ibfd) == 0)
4339 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4340 }
4341
459609d6
AM
4342 htab = ppc_hash_table (info);
4343 if (htab == NULL)
b9399fcf 4344 return TRUE;
459609d6 4345
b9399fcf
AM
4346 if (opd != NULL && opd->size != 0
4347 && (ibfd->flags & DYNAMIC) == 0
4348 && (opd->flags & SEC_RELOC) != 0
4349 && opd->reloc_count != 0
4350 && !bfd_is_abs_section (opd->output_section)
4351 && info->gc_sections)
4352 {
4353 /* Garbage collection needs some extra help with .opd sections.
4354 We don't want to necessarily keep everything referenced by
4355 relocs in .opd, as that would keep all functions. Instead,
4356 if we reference an .opd symbol (a function descriptor), we
4357 want to keep the function code symbol's section. This is
4358 easy for global symbols, but for local syms we need to keep
4359 information about the associated function section. */
4360 bfd_size_type amt;
4361 asection **opd_sym_map;
4362 Elf_Internal_Shdr *symtab_hdr;
4363 Elf_Internal_Rela *relocs, *rel_end, *rel;
4364
4365 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4366 opd_sym_map = bfd_zalloc (ibfd, amt);
4367 if (opd_sym_map == NULL)
4368 return FALSE;
4369 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4370 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4371 info->keep_memory);
4372 if (relocs == NULL)
4373 return FALSE;
4374 symtab_hdr = &elf_symtab_hdr (ibfd);
4375 rel_end = relocs + opd->reloc_count - 1;
4376 for (rel = relocs; rel < rel_end; rel++)
4377 {
4378 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4379 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4380
4381 if (r_type == R_PPC64_ADDR64
4382 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4383 && r_symndx < symtab_hdr->sh_info)
4384 {
4385 Elf_Internal_Sym *isym;
4386 asection *s;
4387
f1dfbfdb
L
4388 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4389 r_symndx);
b9399fcf
AM
4390 if (isym == NULL)
4391 {
4392 if (elf_section_data (opd)->relocs != relocs)
4393 free (relocs);
4394 return FALSE;
4395 }
4396
4397 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4398 if (s != NULL && s != opd)
4399 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4400 }
4401 }
4402 if (elf_section_data (opd)->relocs != relocs)
4403 free (relocs);
4404 }
459609d6
AM
4405
4406 p = &htab->dot_syms;
4407 while ((eh = *p) != NULL)
4408 {
4409 *p = NULL;
4410 if (&eh->elf == htab->elf.hgot)
4411 ;
4412 else if (htab->elf.hgot == NULL
4413 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4414 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4415 else if (abiversion (ibfd) <= 1)
4416 {
4417 htab->need_func_desc_adj = 1;
4418 if (!add_symbol_adjust (eh, info))
4419 return FALSE;
4420 }
459609d6
AM
4421 p = &eh->u.next_dot_sym;
4422 }
b3fac117 4423 return TRUE;
8387904d
AM
4424}
4425
97fed1c9
JJ
4426/* Undo hash table changes when an --as-needed input file is determined
4427 not to be needed. */
4428
4429static bfd_boolean
e5034e59
AM
4430ppc64_elf_notice_as_needed (bfd *ibfd,
4431 struct bfd_link_info *info,
4432 enum notice_asneeded_action act)
97fed1c9 4433{
e5034e59
AM
4434 if (act == notice_not_needed)
4435 {
4436 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4437
e5034e59
AM
4438 if (htab == NULL)
4439 return FALSE;
4dfe6ac6 4440
e5034e59
AM
4441 htab->dot_syms = NULL;
4442 }
4443 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4444}
4445
aa374f67
AM
4446/* If --just-symbols against a final linked binary, then assume we need
4447 toc adjusting stubs when calling functions defined there. */
4448
4449static void
4450ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4451{
4452 if ((sec->flags & SEC_CODE) != 0
4453 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4454 && is_ppc64_elf (sec->owner))
4455 {
2c3f079f
AM
4456 if (abiversion (sec->owner) >= 2
4457 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4458 sec->has_toc_reloc = 1;
4459 }
4460 _bfd_elf_link_just_syms (sec, info);
4461}
4462
e054468f 4463static struct plt_entry **
4ce794b7
AM
4464update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4465 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4466{
4467 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4468 struct plt_entry **local_plt;
f961d9dd 4469 unsigned char *local_got_tls_masks;
411e1bfb
AM
4470
4471 if (local_got_ents == NULL)
4472 {
4473 bfd_size_type size = symtab_hdr->sh_info;
4474
e054468f
AM
4475 size *= (sizeof (*local_got_ents)
4476 + sizeof (*local_plt)
4477 + sizeof (*local_got_tls_masks));
4ce794b7 4478 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4479 if (local_got_ents == NULL)
e054468f 4480 return NULL;
411e1bfb
AM
4481 elf_local_got_ents (abfd) = local_got_ents;
4482 }
4483
37da22e5 4484 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4485 {
4486 struct got_entry *ent;
4487
4488 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4489 if (ent->addend == r_addend
4490 && ent->owner == abfd
4491 && ent->tls_type == tls_type)
411e1bfb
AM
4492 break;
4493 if (ent == NULL)
4494 {
986f0783 4495 size_t amt = sizeof (*ent);
4ce794b7 4496 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4497 if (ent == NULL)
4498 return FALSE;
4499 ent->next = local_got_ents[r_symndx];
4500 ent->addend = r_addend;
e717da7e 4501 ent->owner = abfd;
411e1bfb 4502 ent->tls_type = tls_type;
927be08e 4503 ent->is_indirect = FALSE;
411e1bfb
AM
4504 ent->got.refcount = 0;
4505 local_got_ents[r_symndx] = ent;
4506 }
4507 ent->got.refcount += 1;
4508 }
4509
e054468f 4510 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4511 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4512 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4513
4514 return local_plt + r_symndx;
65f38f15
AM
4515}
4516
411e1bfb 4517static bfd_boolean
e054468f 4518update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4519{
411e1bfb 4520 struct plt_entry *ent;
1e2f5b6e 4521
e054468f 4522 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4523 if (ent->addend == addend)
4524 break;
4525 if (ent == NULL)
1e2f5b6e 4526 {
986f0783 4527 size_t amt = sizeof (*ent);
4ce794b7 4528 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4529 if (ent == NULL)
4530 return FALSE;
e054468f 4531 ent->next = *plist;
411e1bfb
AM
4532 ent->addend = addend;
4533 ent->plt.refcount = 0;
e054468f 4534 *plist = ent;
1e2f5b6e 4535 }
411e1bfb 4536 ent->plt.refcount += 1;
b34976b6 4537 return TRUE;
1e2f5b6e
AM
4538}
4539
e054468f
AM
4540static bfd_boolean
4541is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4542{
4543 return (r_type == R_PPC64_REL24
05d0e962 4544 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
4545 || r_type == R_PPC64_REL14
4546 || r_type == R_PPC64_REL14_BRTAKEN
4547 || r_type == R_PPC64_REL14_BRNTAKEN
4548 || r_type == R_PPC64_ADDR24
4549 || r_type == R_PPC64_ADDR14
4550 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4551 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4552 || r_type == R_PPC64_PLTCALL
4553 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4554}
4555
4556/* Relocs on inline plt call sequence insns prior to the call. */
4557
4558static bfd_boolean
4559is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4560{
4561 return (r_type == R_PPC64_PLT16_HA
4562 || r_type == R_PPC64_PLT16_HI
4563 || r_type == R_PPC64_PLT16_LO
4564 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4565 || r_type == R_PPC64_PLT_PCREL34
4566 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4567 || r_type == R_PPC64_PLTSEQ
4568 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4569}
4570
5bd4f169 4571/* Look through the relocs for a section during the first phase, and
65f38f15 4572 calculate needed space in the global offset table, procedure
5d1634d7 4573 linkage table, and dynamic reloc sections. */
5bd4f169 4574
b34976b6 4575static bfd_boolean
4ce794b7
AM
4576ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4577 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4578{
65f38f15 4579 struct ppc_link_hash_table *htab;
5bd4f169 4580 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4581 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4582 const Elf_Internal_Rela *rel;
4583 const Elf_Internal_Rela *rel_end;
5bd4f169 4584 asection *sreloc;
3a71aa26 4585 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 4586 bfd_boolean is_opd;
5bd4f169 4587
0e1862bb 4588 if (bfd_link_relocatable (info))
b34976b6 4589 return TRUE;
5bd4f169 4590
0c8d6e5c 4591 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4592
65f38f15 4593 htab = ppc_hash_table (info);
4dfe6ac6
NC
4594 if (htab == NULL)
4595 return FALSE;
4596
3a71aa26
AM
4597 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4598 FALSE, FALSE, TRUE);
4599 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4600 FALSE, FALSE, TRUE);
0ffa91dd 4601 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4602 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4603 sreloc = NULL;
b9399fcf 4604 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4605 rel_end = relocs + sec->reloc_count;
4606 for (rel = relocs; rel < rel_end; rel++)
4607 {
4608 unsigned long r_symndx;
4609 struct elf_link_hash_entry *h;
04c9666a 4610 enum elf_ppc64_reloc_type r_type;
727fc41e 4611 int tls_type;
7c8fe5c4 4612 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4613 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
4614
4615 r_symndx = ELF64_R_SYM (rel->r_info);
4616 if (r_symndx < symtab_hdr->sh_info)
4617 h = NULL;
4618 else
973a3492
L
4619 {
4620 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4621 h = elf_follow_link (h);
1c865ab2
AM
4622
4623 if (h == htab->elf.hgot)
4624 sec->has_toc_reloc = 1;
973a3492 4625 }
5bd4f169 4626
5663e321
AM
4627 r_type = ELF64_R_TYPE (rel->r_info);
4628 switch (r_type)
4629 {
04bdff6a
AM
4630 case R_PPC64_D34:
4631 case R_PPC64_D34_LO:
4632 case R_PPC64_D34_HI30:
4633 case R_PPC64_D34_HA30:
4634 case R_PPC64_D28:
c213164a
AM
4635 case R_PPC64_TPREL34:
4636 case R_PPC64_DTPREL34:
04bdff6a 4637 case R_PPC64_PCREL34:
5663e321 4638 case R_PPC64_GOT_PCREL34:
87c69f97
AM
4639 case R_PPC64_GOT_TLSGD_PCREL34:
4640 case R_PPC64_GOT_TLSLD_PCREL34:
4641 case R_PPC64_GOT_TPREL_PCREL34:
4642 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
4643 case R_PPC64_PLT_PCREL34:
4644 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a 4645 case R_PPC64_PCREL28:
e10a07b3 4646 htab->has_power10_relocs = 1;
133a1f60
AM
4647 break;
4648 default:
5663e321
AM
4649 break;
4650 }
903b777d
AM
4651
4652 switch (r_type)
4653 {
4654 case R_PPC64_PLT16_HA:
4655 case R_PPC64_GOT_TLSLD16_HA:
4656 case R_PPC64_GOT_TLSGD16_HA:
4657 case R_PPC64_GOT_TPREL16_HA:
4658 case R_PPC64_GOT_DTPREL16_HA:
4659 case R_PPC64_GOT16_HA:
4660 case R_PPC64_TOC16_HA:
4661 case R_PPC64_PLT16_LO:
4662 case R_PPC64_PLT16_LO_DS:
4663 case R_PPC64_GOT_TLSLD16_LO:
4664 case R_PPC64_GOT_TLSGD16_LO:
4665 case R_PPC64_GOT_TPREL16_LO_DS:
4666 case R_PPC64_GOT_DTPREL16_LO_DS:
4667 case R_PPC64_GOT16_LO:
4668 case R_PPC64_GOT16_LO_DS:
4669 case R_PPC64_TOC16_LO:
4670 case R_PPC64_TOC16_LO_DS:
4671 case R_PPC64_GOT_PCREL34:
4672 ppc64_elf_tdata (abfd)->has_optrel = 1;
4673 ppc64_elf_section_data (sec)->has_optrel = 1;
4674 break;
4675 default:
4676 break;
4677 }
4678
f749f26e 4679 ifunc = NULL;
25f23106
AM
4680 if (h != NULL)
4681 {
4682 if (h->type == STT_GNU_IFUNC)
4683 {
4684 h->needs_plt = 1;
4685 ifunc = &h->plt.plist;
4686 }
4687 }
4688 else
4689 {
f1dfbfdb 4690 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
25f23106
AM
4691 abfd, r_symndx);
4692 if (isym == NULL)
4693 return FALSE;
4694
4695 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4696 {
4697 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4698 rel->r_addend,
37da22e5 4699 NON_GOT | PLT_IFUNC);
25f23106
AM
4700 if (ifunc == NULL)
4701 return FALSE;
4702 }
4703 }
727fc41e 4704
f749f26e 4705 tls_type = 0;
a33d1f77 4706 switch (r_type)
5bd4f169 4707 {
727fc41e
AM
4708 case R_PPC64_TLSGD:
4709 case R_PPC64_TLSLD:
4710 /* These special tls relocs tie a call to __tls_get_addr with
4711 its parameter symbol. */
37da22e5 4712 if (h != NULL)
ed7007c1 4713 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
37da22e5
AM
4714 else
4715 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4716 rel->r_addend,
37da22e5
AM
4717 NON_GOT | TLS_TLS | TLS_MARK))
4718 return FALSE;
4719 sec->has_tls_reloc = 1;
727fc41e
AM
4720 break;
4721
411e1bfb
AM
4722 case R_PPC64_GOT_TLSLD16:
4723 case R_PPC64_GOT_TLSLD16_LO:
4724 case R_PPC64_GOT_TLSLD16_HI:
4725 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 4726 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 4727 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4728 goto dogottls;
4729
4730 case R_PPC64_GOT_TLSGD16:
4731 case R_PPC64_GOT_TLSGD16_LO:
4732 case R_PPC64_GOT_TLSGD16_HI:
4733 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 4734 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 4735 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4736 goto dogottls;
4737
4738 case R_PPC64_GOT_TPREL16_DS:
4739 case R_PPC64_GOT_TPREL16_LO_DS:
4740 case R_PPC64_GOT_TPREL16_HI:
4741 case R_PPC64_GOT_TPREL16_HA:
87c69f97 4742 case R_PPC64_GOT_TPREL_PCREL34:
7c8bbca5 4743 if (bfd_link_dll (info))
411e1bfb
AM
4744 info->flags |= DF_STATIC_TLS;
4745 tls_type = TLS_TLS | TLS_TPREL;
4746 goto dogottls;
4747
4748 case R_PPC64_GOT_DTPREL16_DS:
4749 case R_PPC64_GOT_DTPREL16_LO_DS:
4750 case R_PPC64_GOT_DTPREL16_HI:
4751 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 4752 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
4753 tls_type = TLS_TLS | TLS_DTPREL;
4754 dogottls:
4755 sec->has_tls_reloc = 1;
066f4018 4756 goto dogot;
411e1bfb 4757
903b777d
AM
4758 case R_PPC64_GOT16:
4759 case R_PPC64_GOT16_LO:
4760 case R_PPC64_GOT16_HI:
65f38f15 4761 case R_PPC64_GOT16_HA:
903b777d 4762 case R_PPC64_GOT16_DS:
066f4018 4763 case R_PPC64_GOT16_LO_DS:
4a421c53 4764 case R_PPC64_GOT_PCREL34:
066f4018 4765 dogot:
65f38f15 4766 /* This symbol requires a global offset table entry. */
4c52953f 4767 sec->has_toc_reloc = 1;
33c0ec9d
AM
4768 if (r_type == R_PPC64_GOT_TLSLD16
4769 || r_type == R_PPC64_GOT_TLSGD16
4770 || r_type == R_PPC64_GOT_TPREL16_DS
4771 || r_type == R_PPC64_GOT_DTPREL16_DS
4772 || r_type == R_PPC64_GOT16
4773 || r_type == R_PPC64_GOT16_DS)
4774 {
4775 htab->do_multi_toc = 1;
d77c8a4b 4776 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4777 }
4778
e717da7e
AM
4779 if (ppc64_elf_tdata (abfd)->got == NULL
4780 && !create_got_section (abfd, info))
b34976b6 4781 return FALSE;
5bd4f169
AM
4782
4783 if (h != NULL)
4784 {
411e1bfb
AM
4785 struct ppc_link_hash_entry *eh;
4786 struct got_entry *ent;
65f38f15 4787
ed7007c1 4788 eh = ppc_elf_hash_entry (h);
411e1bfb 4789 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
133a1f60 4790 if (ent->addend == rel->r_addend
e717da7e 4791 && ent->owner == abfd
411e1bfb
AM
4792 && ent->tls_type == tls_type)
4793 break;
4794 if (ent == NULL)
5bd4f169 4795 {
986f0783 4796 size_t amt = sizeof (*ent);
4ce794b7 4797 ent = bfd_alloc (abfd, amt);
411e1bfb 4798 if (ent == NULL)
b34976b6 4799 return FALSE;
411e1bfb 4800 ent->next = eh->elf.got.glist;
133a1f60 4801 ent->addend = rel->r_addend;
e717da7e 4802 ent->owner = abfd;
411e1bfb 4803 ent->tls_type = tls_type;
927be08e 4804 ent->is_indirect = FALSE;
411e1bfb
AM
4805 ent->got.refcount = 0;
4806 eh->elf.got.glist = ent;
5bd4f169 4807 }
411e1bfb 4808 ent->got.refcount += 1;
e7b938ca 4809 eh->tls_mask |= tls_type;
5bd4f169 4810 }
411e1bfb
AM
4811 else
4812 /* This is a global offset table entry for a local symbol. */
4813 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4814 rel->r_addend, tls_type))
411e1bfb 4815 return FALSE;
5bd4f169
AM
4816 break;
4817
5bd4f169 4818 case R_PPC64_PLT16_HA:
65f38f15
AM
4819 case R_PPC64_PLT16_HI:
4820 case R_PPC64_PLT16_LO:
08be3224 4821 case R_PPC64_PLT16_LO_DS:
5663e321
AM
4822 case R_PPC64_PLT_PCREL34:
4823 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
4824 case R_PPC64_PLT32:
4825 case R_PPC64_PLT64:
cbf95972
AM
4826 /* This symbol requires a procedure linkage table entry. */
4827 plt_list = ifunc;
4828 if (h != NULL)
e054468f 4829 {
e054468f
AM
4830 h->needs_plt = 1;
4831 if (h->root.root.string[0] == '.'
4832 && h->root.root.string[1] != '\0')
ed7007c1
AM
4833 ppc_elf_hash_entry (h)->is_func = 1;
4834 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4835 plt_list = &h->plt.plist;
4836 }
4837 if (plt_list == NULL)
2d7ad24e 4838 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4839 rel->r_addend,
2d7ad24e 4840 NON_GOT | PLT_KEEP);
133a1f60 4841 if (!update_plt_info (abfd, plt_list, rel->r_addend))
cbf95972 4842 return FALSE;
5bd4f169
AM
4843 break;
4844
4845 /* The following relocations don't need to propagate the
4846 relocation if linking a shared object since they are
4847 section relative. */
4848 case R_PPC64_SECTOFF:
4849 case R_PPC64_SECTOFF_LO:
4850 case R_PPC64_SECTOFF_HI:
4851 case R_PPC64_SECTOFF_HA:
4852 case R_PPC64_SECTOFF_DS:
4853 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4854 case R_PPC64_DTPREL16:
4855 case R_PPC64_DTPREL16_LO:
4856 case R_PPC64_DTPREL16_HI:
4857 case R_PPC64_DTPREL16_HA:
4858 case R_PPC64_DTPREL16_DS:
4859 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
4860 case R_PPC64_DTPREL16_HIGH:
4861 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
4862 case R_PPC64_DTPREL16_HIGHER:
4863 case R_PPC64_DTPREL16_HIGHERA:
4864 case R_PPC64_DTPREL16_HIGHEST:
4865 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4866 break;
4867
ad8e1ba5 4868 /* Nor do these. */
25f23106
AM
4869 case R_PPC64_REL16:
4870 case R_PPC64_REL16_LO:
4871 case R_PPC64_REL16_HI:
4872 case R_PPC64_REL16_HA:
4a969973
AM
4873 case R_PPC64_REL16_HIGH:
4874 case R_PPC64_REL16_HIGHA:
4875 case R_PPC64_REL16_HIGHER:
4876 case R_PPC64_REL16_HIGHERA:
4877 case R_PPC64_REL16_HIGHEST:
4878 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
4879 case R_PPC64_REL16_HIGHER34:
4880 case R_PPC64_REL16_HIGHERA34:
4881 case R_PPC64_REL16_HIGHEST34:
4882 case R_PPC64_REL16_HIGHESTA34:
a680de9a 4883 case R_PPC64_REL16DX_HA:
25f23106
AM
4884 break;
4885
45965137
AM
4886 /* Not supported as a dynamic relocation. */
4887 case R_PPC64_ADDR64_LOCAL:
0e1862bb 4888 if (bfd_link_pic (info))
45965137
AM
4889 {
4890 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4891 ppc_howto_init ();
695344c0 4892 /* xgettext:c-format */
174d0a74 4893 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 4894 "in shared libraries and PIEs\n"),
45965137
AM
4895 abfd, sec, rel->r_offset,
4896 ppc64_elf_howto_table[r_type]->name);
4897 bfd_set_error (bfd_error_bad_value);
4898 return FALSE;
4899 }
4900 break;
4901
ad8e1ba5 4902 case R_PPC64_TOC16:
33c0ec9d
AM
4903 case R_PPC64_TOC16_DS:
4904 htab->do_multi_toc = 1;
d77c8a4b 4905 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 4906 /* Fall through. */
ad8e1ba5
AM
4907 case R_PPC64_TOC16_LO:
4908 case R_PPC64_TOC16_HI:
4909 case R_PPC64_TOC16_HA:
ad8e1ba5 4910 case R_PPC64_TOC16_LO_DS:
4c52953f 4911 sec->has_toc_reloc = 1;
ec73ddcd 4912 if (h != NULL && bfd_link_executable (info))
1bdd8fac
AM
4913 {
4914 /* We may need a copy reloc. */
4915 h->non_got_ref = 1;
4916 /* Strongly prefer a copy reloc over a dynamic reloc.
4917 glibc ld.so as of 2019-08 will error out if one of
4918 these relocations is emitted. */
4919 h->needs_copy = 1;
4920 goto dodyn;
4921 }
ad8e1ba5
AM
4922 break;
4923
006589cf
AM
4924 /* Marker reloc. */
4925 case R_PPC64_ENTRY:
4926 break;
4927
5bd4f169
AM
4928 /* This relocation describes the C++ object vtable hierarchy.
4929 Reconstruct it for later use during GC. */
4930 case R_PPC64_GNU_VTINHERIT:
c152c796 4931 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4932 return FALSE;
5bd4f169
AM
4933 break;
4934
4935 /* This relocation describes which C++ vtable entries are actually
4936 used. Record for later use during GC. */
4937 case R_PPC64_GNU_VTENTRY:
a0ea3a14 4938 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4939 return FALSE;
5bd4f169
AM
4940 break;
4941
721956f4
AM
4942 case R_PPC64_REL14:
4943 case R_PPC64_REL14_BRTAKEN:
4944 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4945 {
4946 asection *dest = NULL;
4947
4948 /* Heuristic: If jumping outside our section, chances are
4949 we are going to need a stub. */
4950 if (h != NULL)
4951 {
4952 /* If the sym is weak it may be overridden later, so
4953 don't assume we know where a weak sym lives. */
4954 if (h->root.type == bfd_link_hash_defined)
4955 dest = h->root.u.def.section;
4956 }
4957 else
87d72d41
AM
4958 {
4959 Elf_Internal_Sym *isym;
4960
f1dfbfdb 4961 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
4962 abfd, r_symndx);
4963 if (isym == NULL)
4964 return FALSE;
4965
4966 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4967 }
4968
220c76dd 4969 if (dest != sec)
7c8fe5c4 4970 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4971 }
3e04d765
AM
4972 goto rel24;
4973
4974 case R_PPC64_PLTCALL:
5663e321 4975 case R_PPC64_PLTCALL_NOTOC:
3e04d765 4976 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
4977 /* Fall through. */
4978
5d1634d7 4979 case R_PPC64_REL24:
05d0e962 4980 case R_PPC64_REL24_NOTOC:
3e04d765 4981 rel24:
cbf95972
AM
4982 plt_list = ifunc;
4983 if (h != NULL)
5d1634d7 4984 {
e054468f
AM
4985 h->needs_plt = 1;
4986 if (h->root.root.string[0] == '.'
4987 && h->root.root.string[1] != '\0')
ed7007c1 4988 ppc_elf_hash_entry (h)->is_func = 1;
cbf95972 4989
3a71aa26 4990 if (h == tga || h == dottga)
cbf95972
AM
4991 {
4992 sec->has_tls_reloc = 1;
4993 if (rel != relocs
4994 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4995 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4996 /* We have a new-style __tls_get_addr call with
4997 a marker reloc. */
4998 ;
4999 else
5000 /* Mark this section as having an old-style call. */
9737e8af 5001 sec->nomark_tls_get_addr = 1;
cbf95972
AM
5002 }
5003 plt_list = &h->plt.plist;
411e1bfb 5004 }
cbf95972
AM
5005
5006 /* We may need a .plt entry if the function this reloc
5007 refers to is in a shared lib. */
5008 if (plt_list
133a1f60 5009 && !update_plt_info (abfd, plt_list, rel->r_addend))
cbf95972 5010 return FALSE;
411e1bfb
AM
5011 break;
5012
cbf95972
AM
5013 case R_PPC64_ADDR14:
5014 case R_PPC64_ADDR14_BRNTAKEN:
5015 case R_PPC64_ADDR14_BRTAKEN:
5016 case R_PPC64_ADDR24:
5017 goto dodyn;
5018
411e1bfb
AM
5019 case R_PPC64_TPREL64:
5020 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5021 if (bfd_link_dll (info))
411e1bfb
AM
5022 info->flags |= DF_STATIC_TLS;
5023 goto dotlstoc;
5024
5025 case R_PPC64_DTPMOD64:
5026 if (rel + 1 < rel_end
5027 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5028 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5029 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5030 else
951fd09b 5031 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5032 goto dotlstoc;
5033
5034 case R_PPC64_DTPREL64:
5035 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5036 if (rel != relocs
5037 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5038 && rel[-1].r_offset == rel->r_offset - 8)
5039 /* This is the second reloc of a dtpmod, dtprel pair.
5040 Don't mark with TLS_DTPREL. */
5041 goto dodyn;
5042
5043 dotlstoc:
5044 sec->has_tls_reloc = 1;
5045 if (h != NULL)
ed7007c1 5046 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
411e1bfb
AM
5047 else
5048 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5049 rel->r_addend, tls_type))
411e1bfb
AM
5050 return FALSE;
5051
7c8fe5c4
AM
5052 ppc64_sec = ppc64_elf_section_data (sec);
5053 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5054 {
3a71aa26
AM
5055 bfd_size_type amt;
5056
e7b938ca 5057 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5058 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5059 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5060 if (ppc64_sec->u.toc.symndx == NULL)
5061 return FALSE;
5062 amt = sec->size * sizeof (bfd_vma) / 8;
5063 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5064 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5065 return FALSE;
7c8fe5c4
AM
5066 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5067 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5068 }
5069 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26 5070 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
133a1f60 5071 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5072
5073 /* Mark the second slot of a GD or LD entry.
5074 -1 to indicate GD and -2 to indicate LD. */
5075 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5076 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5077 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5078 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5079 goto dodyn;
5080
411e1bfb
AM
5081 case R_PPC64_TPREL16_HI:
5082 case R_PPC64_TPREL16_HA:
f9c6b907
AM
5083 case R_PPC64_TPREL16_HIGH:
5084 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5085 case R_PPC64_TPREL16_HIGHER:
5086 case R_PPC64_TPREL16_HIGHERA:
5087 case R_PPC64_TPREL16_HIGHEST:
5088 case R_PPC64_TPREL16_HIGHESTA:
252dcdf4
AM
5089 sec->has_tls_reloc = 1;
5090 /* Fall through. */
c213164a 5091 case R_PPC64_TPREL34:
252dcdf4
AM
5092 case R_PPC64_TPREL16:
5093 case R_PPC64_TPREL16_DS:
5094 case R_PPC64_TPREL16_LO:
5095 case R_PPC64_TPREL16_LO_DS:
7c8bbca5
AM
5096 if (bfd_link_dll (info))
5097 info->flags |= DF_STATIC_TLS;
5098 goto dodyn;
5d1634d7 5099
e86ce104 5100 case R_PPC64_ADDR64:
b9399fcf 5101 if (is_opd
1e2f5b6e 5102 && rel + 1 < rel_end
4ce794b7 5103 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5104 {
8387904d 5105 if (h != NULL)
ed7007c1 5106 ppc_elf_hash_entry (h)->is_func = 1;
1e2f5b6e 5107 }
e86ce104
AM
5108 /* Fall through. */
5109
65f38f15
AM
5110 case R_PPC64_ADDR16:
5111 case R_PPC64_ADDR16_DS:
5112 case R_PPC64_ADDR16_HA:
5113 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5114 case R_PPC64_ADDR16_HIGH:
5115 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5116 case R_PPC64_ADDR16_HIGHER:
5117 case R_PPC64_ADDR16_HIGHERA:
5118 case R_PPC64_ADDR16_HIGHEST:
5119 case R_PPC64_ADDR16_HIGHESTA:
5120 case R_PPC64_ADDR16_LO:
5121 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
5122 case R_PPC64_D34:
5123 case R_PPC64_D34_LO:
5124 case R_PPC64_D34_HI30:
5125 case R_PPC64_D34_HA30:
5126 case R_PPC64_ADDR16_HIGHER34:
5127 case R_PPC64_ADDR16_HIGHERA34:
5128 case R_PPC64_ADDR16_HIGHEST34:
5129 case R_PPC64_ADDR16_HIGHESTA34:
5130 case R_PPC64_D28:
0e1862bb 5131 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5132 && rel->r_addend == 0)
5133 {
5134 /* We may need a .plt entry if this reloc refers to a
5135 function in a shared lib. */
5663e321 5136 if (!update_plt_info (abfd, &h->plt.plist, 0))
a345bc8d
AM
5137 return FALSE;
5138 h->pointer_equality_needed = 1;
5139 }
5140 /* Fall through. */
5141
5142 case R_PPC64_REL30:
5143 case R_PPC64_REL32:
5144 case R_PPC64_REL64:
65f38f15 5145 case R_PPC64_ADDR32:
65f38f15
AM
5146 case R_PPC64_UADDR16:
5147 case R_PPC64_UADDR32:
5148 case R_PPC64_UADDR64:
5bd4f169 5149 case R_PPC64_TOC:
ec73ddcd 5150 if (h != NULL && bfd_link_executable (info))
81848ca0 5151 /* We may need a copy reloc. */
f5385ebf 5152 h->non_got_ref = 1;
81848ca0 5153
41bd81ab 5154 /* Don't propagate .opd relocs. */
b9399fcf 5155 if (NO_OPD_RELOCS && is_opd)
e86ce104 5156 break;
e86ce104 5157
65f38f15
AM
5158 /* If we are creating a shared library, and this is a reloc
5159 against a global symbol, or a non PC relative reloc
5160 against a local symbol, then we need to copy the reloc
5161 into the shared library. However, if we are linking with
5162 -Bsymbolic, we do not need to copy a reloc against a
5163 global symbol which is defined in an object we are
5164 including in the link (i.e., DEF_REGULAR is set). At
5165 this point we have not seen all the input files, so it is
5166 possible that DEF_REGULAR is not set now but will be set
5167 later (it is never cleared). In case of a weak definition,
5168 DEF_REGULAR may be cleared later by a strong definition in
5169 a shared library. We account for that possibility below by
f4656909 5170 storing information in the dyn_relocs field of the hash
65f38f15
AM
5171 table entry. A similar situation occurs when creating
5172 shared libraries and symbol visibility changes render the
5173 symbol local.
5174
5175 If on the other hand, we are creating an executable, we
5176 may need to keep relocations for symbols satisfied by a
5177 dynamic library if we manage to avoid copy relocs for the
5178 symbol. */
411e1bfb 5179 dodyn:
ec73ddcd
AM
5180 if ((h != NULL
5181 && (h->root.type == bfd_link_hash_defweak
5182 || !h->def_regular))
5183 || (h != NULL
5184 && !bfd_link_executable (info)
5185 && !SYMBOLIC_BIND (info, h))
5186 || (bfd_link_pic (info)
5187 && must_be_dyn_reloc (info, r_type))
0e1862bb 5188 || (!bfd_link_pic (info)
25f23106 5189 && ifunc != NULL))
5bd4f169 5190 {
65f38f15
AM
5191 /* We must copy these reloc types into the output file.
5192 Create a reloc section in dynobj and make room for
5193 this reloc. */
5bd4f169
AM
5194 if (sreloc == NULL)
5195 {
83bac4b0
NC
5196 sreloc = _bfd_elf_make_dynamic_reloc_section
5197 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5198
5bd4f169 5199 if (sreloc == NULL)
83bac4b0 5200 return FALSE;
5bd4f169
AM
5201 }
5202
65f38f15
AM
5203 /* If this is a global symbol, we count the number of
5204 relocations we need for this symbol. */
5205 if (h != NULL)
5206 {
19e08130
AM
5207 struct elf_dyn_relocs *p;
5208 struct elf_dyn_relocs **head;
5209
190eb1dd 5210 head = &h->dyn_relocs;
19e08130
AM
5211 p = *head;
5212 if (p == NULL || p->sec != sec)
5213 {
5214 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5215 if (p == NULL)
5216 return FALSE;
5217 p->next = *head;
5218 *head = p;
5219 p->sec = sec;
5220 p->count = 0;
5221 p->pc_count = 0;
5222 }
5223 p->count += 1;
5224 if (!must_be_dyn_reloc (info, r_type))
5225 p->pc_count += 1;
65f38f15
AM
5226 }
5227 else
5228 {
ec338859
AM
5229 /* Track dynamic relocs needed for local syms too.
5230 We really need local syms available to do this
5231 easily. Oh well. */
19e08130
AM
5232 struct ppc_dyn_relocs *p;
5233 struct ppc_dyn_relocs **head;
5234 bfd_boolean is_ifunc;
ec338859 5235 asection *s;
6edfbbad 5236 void *vpp;
87d72d41 5237 Elf_Internal_Sym *isym;
6edfbbad 5238
f1dfbfdb 5239 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
5240 abfd, r_symndx);
5241 if (isym == NULL)
b34976b6 5242 return FALSE;
ec338859 5243
87d72d41
AM
5244 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5245 if (s == NULL)
5246 s = sec;
5247
6edfbbad 5248 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5249 head = (struct ppc_dyn_relocs **) vpp;
5250 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5251 p = *head;
5252 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5253 p = p->next;
5254 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5255 {
5256 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5257 if (p == NULL)
5258 return FALSE;
5259 p->next = *head;
5260 *head = p;
5261 p->sec = sec;
5262 p->ifunc = is_ifunc;
5263 p->count = 0;
5264 }
5265 p->count += 1;
ec338859 5266 }
65f38f15 5267 }
5bd4f169 5268 break;
65f38f15
AM
5269
5270 default:
96e0dda4 5271 break;
5bd4f169
AM
5272 }
5273 }
5274
b34976b6 5275 return TRUE;
5bd4f169
AM
5276}
5277
ee67d69a
AM
5278/* Merge backend specific data from an object file to the output
5279 object file when linking. */
5280
5281static bfd_boolean
50e03d47 5282ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5283{
50e03d47 5284 bfd *obfd = info->output_bfd;
ee67d69a
AM
5285 unsigned long iflags, oflags;
5286
5287 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5288 return TRUE;
5289
5290 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5291 return TRUE;
5292
50e03d47 5293 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
5294 return FALSE;
5295
5296 iflags = elf_elfheader (ibfd)->e_flags;
5297 oflags = elf_elfheader (obfd)->e_flags;
5298
f6c7c3e8 5299 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5300 {
4eca0228 5301 _bfd_error_handler
695344c0 5302 /* xgettext:c-format */
871b3ab2 5303 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
5304 bfd_set_error (bfd_error_bad_value);
5305 return FALSE;
5306 }
f6c7c3e8 5307 else if (iflags != oflags && iflags != 0)
ee67d69a 5308 {
4eca0228 5309 _bfd_error_handler
695344c0 5310 /* xgettext:c-format */
871b3ab2 5311 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5312 ibfd, iflags, oflags);
5313 bfd_set_error (bfd_error_bad_value);
5314 return FALSE;
5315 }
5316
4a91d0ba
AM
5317 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5318 return FALSE;
005d79fd 5319
ee67d69a 5320 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5321 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5322}
5323
5324static bfd_boolean
5325ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5326{
5327 /* Print normal ELF private data. */
5328 _bfd_elf_print_private_bfd_data (abfd, ptr);
5329
5330 if (elf_elfheader (abfd)->e_flags != 0)
5331 {
5332 FILE *file = ptr;
5333
ee67d69a
AM
5334 fprintf (file, _("private flags = 0x%lx:"),
5335 elf_elfheader (abfd)->e_flags);
5336
5337 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5338 fprintf (file, _(" [abiv%ld]"),
5339 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5340 fputc ('\n', file);
5341 }
5342
5343 return TRUE;
5344}
5345
8387904d 5346/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5347 of the code entry point, and its section, which must be in the same
5348 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5349
5350static bfd_vma
5351opd_entry_value (asection *opd_sec,
5352 bfd_vma offset,
5353 asection **code_sec,
aef36ac1
AM
5354 bfd_vma *code_off,
5355 bfd_boolean in_code_sec)
8387904d
AM
5356{
5357 bfd *opd_bfd = opd_sec->owner;
8860955f 5358 Elf_Internal_Rela *relocs;
8387904d 5359 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5360 bfd_vma val;
8387904d 5361
9f296da3
AM
5362 /* No relocs implies we are linking a --just-symbols object, or looking
5363 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5364 if (opd_sec->reloc_count == 0)
5365 {
729eabd5 5366 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5367
729eabd5
AM
5368 if (contents == NULL)
5369 {
5370 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5371 return (bfd_vma) -1;
5372 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5373 }
ee1e4ede 5374
dbb3fbbb 5375 /* PR 17512: file: 64b9dfbb. */
451dfd38 5376 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5377 return (bfd_vma) -1;
5378
729eabd5 5379 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5380 if (code_sec != NULL)
5381 {
5382 asection *sec, *likely = NULL;
ee1e4ede 5383
aef36ac1 5384 if (in_code_sec)
4b85d634 5385 {
aef36ac1
AM
5386 sec = *code_sec;
5387 if (sec->vma <= val
5388 && val < sec->vma + sec->size)
5389 likely = sec;
5390 else
5391 val = -1;
5392 }
5393 else
5394 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5395 if (sec->vma <= val
5396 && (sec->flags & SEC_LOAD) != 0
5397 && (sec->flags & SEC_ALLOC) != 0)
5398 likely = sec;
5399 if (likely != NULL)
5400 {
5401 *code_sec = likely;
5402 if (code_off != NULL)
5403 *code_off = val - likely->vma;
4b85d634
AM
5404 }
5405 }
aef36ac1 5406 return val;
4b85d634
AM
5407 }
5408
0c8d6e5c 5409 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5410
729eabd5 5411 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5412 if (relocs == NULL)
5413 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
5414 /* PR 17512: file: df8e1fd6. */
5415 if (relocs == NULL)
5416 return (bfd_vma) -1;
645ea6a9 5417
8387904d 5418 /* Go find the opd reloc at the sym address. */
8860955f 5419 lo = relocs;
8387904d 5420 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5421 val = (bfd_vma) -1;
8387904d
AM
5422 while (lo < hi)
5423 {
5424 look = lo + (hi - lo) / 2;
5425 if (look->r_offset < offset)
5426 lo = look + 1;
5427 else if (look->r_offset > offset)
5428 hi = look;
5429 else
5430 {
0ffa91dd
NC
5431 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5432
8387904d
AM
5433 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5434 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5435 {
5436 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5437 asection *sec = NULL;
8387904d 5438
b53dfeb2
AM
5439 if (symndx >= symtab_hdr->sh_info
5440 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5441 {
5442 struct elf_link_hash_entry **sym_hashes;
5443 struct elf_link_hash_entry *rh;
5444
5445 sym_hashes = elf_sym_hashes (opd_bfd);
5446 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5447 if (rh != NULL)
5448 {
5449 rh = elf_follow_link (rh);
bb854a36
AM
5450 if (rh->root.type != bfd_link_hash_defined
5451 && rh->root.type != bfd_link_hash_defweak)
5452 break;
5453 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5454 {
bb854a36
AM
5455 val = rh->root.u.def.value;
5456 sec = rh->root.u.def.section;
b53dfeb2
AM
5457 }
5458 }
5459 }
5460
5461 if (sec == NULL)
5462 {
5463 Elf_Internal_Sym *sym;
5464
5465 if (symndx < symtab_hdr->sh_info)
5466 {
5467 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5468 if (sym == NULL)
5469 {
5470 size_t symcnt = symtab_hdr->sh_info;
5471 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5472 symcnt, 0,
5473 NULL, NULL, NULL);
5474 if (sym == NULL)
5475 break;
5476 symtab_hdr->contents = (bfd_byte *) sym;
5477 }
5478 sym += symndx;
128205bb
AM
5479 }
5480 else
5481 {
b53dfeb2
AM
5482 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5483 1, symndx,
5484 NULL, NULL, NULL);
128205bb
AM
5485 if (sym == NULL)
5486 break;
128205bb 5487 }
b53dfeb2
AM
5488 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5489 if (sec == NULL)
5490 break;
5491 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5492 val = sym->st_value;
8387904d 5493 }
b53dfeb2 5494
8387904d
AM
5495 val += look->r_addend;
5496 if (code_off != NULL)
5497 *code_off = val;
5498 if (code_sec != NULL)
aef36ac1
AM
5499 {
5500 if (in_code_sec && *code_sec != sec)
5501 return -1;
5502 else
5503 *code_sec = sec;
5504 }
b53dfeb2 5505 if (sec->output_section != NULL)
8387904d 5506 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5507 }
5508 break;
5509 }
5510 }
645ea6a9 5511
645ea6a9 5512 return val;
8387904d
AM
5513}
5514
aef36ac1
AM
5515/* If the ELF symbol SYM might be a function in SEC, return the
5516 function size and set *CODE_OFF to the function's entry point,
5517 otherwise return zero. */
9f296da3 5518
aef36ac1
AM
5519static bfd_size_type
5520ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5521 bfd_vma *code_off)
9f296da3 5522{
aef36ac1
AM
5523 bfd_size_type size;
5524
5525 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5526 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5527 return 0;
5528
5529 size = 0;
5530 if (!(sym->flags & BSF_SYNTHETIC))
5531 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5532
5533 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5534 {
b07bca4e
AM
5535 struct _opd_sec_data *opd = get_opd_info (sym->section);
5536 bfd_vma symval = sym->value;
5537
5538 if (opd != NULL
5539 && opd->adjust != NULL
5540 && elf_section_data (sym->section)->relocs != NULL)
5541 {
5542 /* opd_entry_value will use cached relocs that have been
5543 adjusted, but with raw symbols. That means both local
5544 and global symbols need adjusting. */
5545 long adjust = opd->adjust[OPD_NDX (symval)];
5546 if (adjust == -1)
5547 return 0;
5548 symval += adjust;
5549 }
5550
5551 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
5552 &sec, code_off, TRUE) == (bfd_vma) -1)
5553 return 0;
5554 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5555 symbol. This size has nothing to do with the code size of the
5556 function, which is what we're supposed to return, but the
5557 code size isn't available without looking up the dot-sym.
5558 However, doing that would be a waste of time particularly
5559 since elf_find_function will look at the dot-sym anyway.
5560 Now, elf_find_function will keep the largest size of any
5561 function sym found at the code address of interest, so return
5562 1 here to avoid it incorrectly caching a larger function size
5563 for a small function. This does mean we return the wrong
5564 size for a new-ABI function of size 24, but all that does is
5565 disable caching for such functions. */
5566 if (size == 24)
5567 size = 1;
9f296da3 5568 }
aef36ac1
AM
5569 else
5570 {
5571 if (sym->section != sec)
5572 return 0;
5573 *code_off = sym->value;
5574 }
5575 if (size == 0)
5576 size = 1;
5577 return size;
9f296da3
AM
5578}
5579
f378ab09
AM
5580/* Return true if symbol is a strong function defined in an ELFv2
5581 object with st_other localentry bits of zero, ie. its local entry
5582 point coincides with its global entry point. */
5583
5584static bfd_boolean
5585is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5586{
5587 return (h != NULL
5588 && h->type == STT_FUNC
5589 && h->root.type == bfd_link_hash_defined
5590 && (STO_PPC64_LOCAL_MASK & h->other) == 0
ed7007c1 5591 && !ppc_elf_hash_entry (h)->non_zero_localentry
f378ab09
AM
5592 && is_ppc64_elf (h->root.u.def.section->owner)
5593 && abiversion (h->root.u.def.section->owner) >= 2);
5594}
5595
854b41e7
AM
5596/* Return true if symbol is defined in a regular object file. */
5597
5598static bfd_boolean
5599is_static_defined (struct elf_link_hash_entry *h)
5600{
5601 return ((h->root.type == bfd_link_hash_defined
5602 || h->root.type == bfd_link_hash_defweak)
5603 && h->root.u.def.section != NULL
5604 && h->root.u.def.section->output_section != NULL);
5605}
5606
b31867b6
AM
5607/* If FDH is a function descriptor symbol, return the associated code
5608 entry symbol if it is defined. Return NULL otherwise. */
5609
5610static struct ppc_link_hash_entry *
5611defined_code_entry (struct ppc_link_hash_entry *fdh)
5612{
5613 if (fdh->is_func_descriptor)
5614 {
5615 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5616 if (fh->elf.root.type == bfd_link_hash_defined
5617 || fh->elf.root.type == bfd_link_hash_defweak)
5618 return fh;
5619 }
5620 return NULL;
5621}
5622
5623/* If FH is a function code entry symbol, return the associated
5624 function descriptor symbol if it is defined. Return NULL otherwise. */
5625
5626static struct ppc_link_hash_entry *
5627defined_func_desc (struct ppc_link_hash_entry *fh)
5628{
5629 if (fh->oh != NULL
5630 && fh->oh->is_func_descriptor)
5631 {
5632 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5633 if (fdh->elf.root.type == bfd_link_hash_defined
5634 || fdh->elf.root.type == bfd_link_hash_defweak)
5635 return fdh;
5636 }
5637 return NULL;
5638}
5639
ed7007c1
AM
5640/* Given H is a symbol that satisfies is_static_defined, return the
5641 value in the output file. */
5642
5643static bfd_vma
5644defined_sym_val (struct elf_link_hash_entry *h)
5645{
5646 return (h->root.u.def.section->output_section->vma
5647 + h->root.u.def.section->output_offset
5648 + h->root.u.def.value);
5649}
5650
5651/* Return true if H matches __tls_get_addr or one of its variants. */
5652
5653static bfd_boolean
5654is_tls_get_addr (struct elf_link_hash_entry *h,
5655 struct ppc_link_hash_table *htab)
5656{
9e51d549
AM
5657 return (h == (struct elf_link_hash_entry *) htab->tls_get_addr_fd
5658 || h == (struct elf_link_hash_entry *) htab->tga_desc_fd
5659 || h == (struct elf_link_hash_entry *) htab->tls_get_addr
5660 || h == (struct elf_link_hash_entry *) htab->tga_desc);
ed7007c1
AM
5661}
5662
8c5b4e52
AM
5663static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5664
5665/* Garbage collect sections, after first dealing with dot-symbols. */
5666
5667static bfd_boolean
5668ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5669{
5670 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5671
5672 if (htab != NULL && htab->need_func_desc_adj)
5673 {
5674 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5675 htab->need_func_desc_adj = 0;
5676 }
5677 return bfd_elf_gc_sections (abfd, info);
5678}
5679
74f0fb50
AM
5680/* Mark all our entry sym sections, both opd and code section. */
5681
5682static void
5683ppc64_elf_gc_keep (struct bfd_link_info *info)
5684{
5685 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5686 struct bfd_sym_chain *sym;
5687
4dfe6ac6
NC
5688 if (htab == NULL)
5689 return;
5690
74f0fb50
AM
5691 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5692 {
b31867b6 5693 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5694 asection *sec;
5695
ed7007c1
AM
5696 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5697 FALSE, FALSE, TRUE));
74f0fb50
AM
5698 if (eh == NULL)
5699 continue;
5700 if (eh->elf.root.type != bfd_link_hash_defined
5701 && eh->elf.root.type != bfd_link_hash_defweak)
5702 continue;
5703
b31867b6
AM
5704 fh = defined_code_entry (eh);
5705 if (fh != NULL)
74f0fb50 5706 {
b31867b6 5707 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5708 sec->flags |= SEC_KEEP;
5709 }
5710 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5711 && opd_entry_value (eh->elf.root.u.def.section,
5712 eh->elf.root.u.def.value,
aef36ac1 5713 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
5714 sec->flags |= SEC_KEEP;
5715
5716 sec = eh->elf.root.u.def.section;
5717 sec->flags |= SEC_KEEP;
5718 }
5719}
5720
64d03ab5
AM
5721/* Mark sections containing dynamically referenced symbols. When
5722 building shared libraries, we must assume that any visible symbol is
5723 referenced. */
5724
5725static bfd_boolean
5726ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5727{
5728 struct bfd_link_info *info = (struct bfd_link_info *) inf;
ed7007c1 5729 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
b31867b6 5730 struct ppc_link_hash_entry *fdh;
b407645f 5731 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5732
64d03ab5 5733 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5734 fdh = defined_func_desc (eh);
5735 if (fdh != NULL)
5736 eh = fdh;
64d03ab5
AM
5737
5738 if ((eh->elf.root.type == bfd_link_hash_defined
5739 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 5740 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5741 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5742 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5743 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5744 && (!bfd_link_executable (info)
e278ae05 5745 || info->gc_keep_exported
b407645f
AM
5746 || info->export_dynamic
5747 || (eh->elf.dynamic
5748 && d != NULL
2cdcc330
AM
5749 && (*d->match) (&d->head, NULL,
5750 eh->elf.root.root.string)))
e278ae05 5751 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5752 || !bfd_hide_sym_by_version (info->version_info,
5753 eh->elf.root.root.string)))))
64d03ab5
AM
5754 {
5755 asection *code_sec;
b31867b6 5756 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5757
5758 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5759
5760 /* Function descriptor syms cause the associated
5761 function code sym section to be marked. */
b31867b6
AM
5762 fh = defined_code_entry (eh);
5763 if (fh != NULL)
5764 {
5765 code_sec = fh->elf.root.u.def.section;
5766 code_sec->flags |= SEC_KEEP;
5767 }
64d03ab5
AM
5768 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5769 && opd_entry_value (eh->elf.root.u.def.section,
5770 eh->elf.root.u.def.value,
aef36ac1 5771 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
5772 code_sec->flags |= SEC_KEEP;
5773 }
5774
5775 return TRUE;
5776}
5777
5bd4f169
AM
5778/* Return the section that should be marked against GC for a given
5779 relocation. */
5780
5781static asection *
4ce794b7 5782ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5783 struct bfd_link_info *info,
4ce794b7
AM
5784 Elf_Internal_Rela *rel,
5785 struct elf_link_hash_entry *h,
5786 Elf_Internal_Sym *sym)
5bd4f169 5787{
ccfa59ea
AM
5788 asection *rsec;
5789
ccfa59ea
AM
5790 /* Syms return NULL if we're marking .opd, so we avoid marking all
5791 function sections, as all functions are referenced in .opd. */
5792 rsec = NULL;
5793 if (get_opd_info (sec) != NULL)
5794 return rsec;
1e2f5b6e 5795
5bd4f169
AM
5796 if (h != NULL)
5797 {
04c9666a 5798 enum elf_ppc64_reloc_type r_type;
b31867b6 5799 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5800
4ce794b7 5801 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5802 switch (r_type)
5bd4f169
AM
5803 {
5804 case R_PPC64_GNU_VTINHERIT:
5805 case R_PPC64_GNU_VTENTRY:
5806 break;
5807
5808 default:
5809 switch (h->root.type)
5810 {
5811 case bfd_link_hash_defined:
5812 case bfd_link_hash_defweak:
ed7007c1 5813 eh = ppc_elf_hash_entry (h);
b31867b6
AM
5814 fdh = defined_func_desc (eh);
5815 if (fdh != NULL)
8c5b4e52
AM
5816 {
5817 /* -mcall-aixdesc code references the dot-symbol on
5818 a call reloc. Mark the function descriptor too
5819 against garbage collection. */
5820 fdh->elf.mark = 1;
60d67dc8
AM
5821 if (fdh->elf.is_weakalias)
5822 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
5823 eh = fdh;
5824 }
1e2f5b6e
AM
5825
5826 /* Function descriptor syms cause the associated
5827 function code sym section to be marked. */
b31867b6
AM
5828 fh = defined_code_entry (eh);
5829 if (fh != NULL)
ccfa59ea
AM
5830 {
5831 /* They also mark their opd section. */
74f0fb50 5832 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5833
b31867b6 5834 rsec = fh->elf.root.u.def.section;
ccfa59ea 5835 }
8387904d
AM
5836 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5837 && opd_entry_value (eh->elf.root.u.def.section,
5838 eh->elf.root.u.def.value,
aef36ac1 5839 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 5840 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5841 else
1e2f5b6e
AM
5842 rsec = h->root.u.def.section;
5843 break;
5bd4f169
AM
5844
5845 case bfd_link_hash_common:
1e2f5b6e
AM
5846 rsec = h->root.u.c.p->section;
5847 break;
5bd4f169
AM
5848
5849 default:
fb34365b 5850 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5851 }
5852 }
5853 }
5854 else
5855 {
74f0fb50 5856 struct _opd_sec_data *opd;
1e2f5b6e
AM
5857
5858 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5859 opd = get_opd_info (rsec);
5860 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5861 {
74f0fb50 5862 rsec->gc_mark = 1;
ccfa59ea 5863
51aecdc5 5864 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 5865 }
5bd4f169
AM
5866 }
5867
1e2f5b6e 5868 return rsec;
5bd4f169
AM
5869}
5870
deb0e272
AM
5871/* The maximum size of .sfpr. */
5872#define SFPR_MAX (218*4)
5873
5874struct sfpr_def_parms
5875{
699733f6
AM
5876 const char name[12];
5877 unsigned char lo, hi;
2cdcc330
AM
5878 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5879 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
5880};
5881
a4b6fadd
AM
5882/* Auto-generate _save*, _rest* functions in .sfpr.
5883 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5884 instead. */
deb0e272 5885
4dfe6ac6 5886static bfd_boolean
a4b6fadd
AM
5887sfpr_define (struct bfd_link_info *info,
5888 const struct sfpr_def_parms *parm,
5889 asection *stub_sec)
deb0e272
AM
5890{
5891 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5892 unsigned int i;
5893 size_t len = strlen (parm->name);
5894 bfd_boolean writing = FALSE;
699733f6 5895 char sym[16];
deb0e272 5896
4dfe6ac6
NC
5897 if (htab == NULL)
5898 return FALSE;
5899
deb0e272
AM
5900 memcpy (sym, parm->name, len);
5901 sym[len + 2] = 0;
5902
5903 for (i = parm->lo; i <= parm->hi; i++)
5904 {
a4b6fadd 5905 struct ppc_link_hash_entry *h;
deb0e272
AM
5906
5907 sym[len + 0] = i / 10 + '0';
5908 sym[len + 1] = i % 10 + '0';
ed7007c1
AM
5909 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
5910 writing, TRUE, TRUE));
a4b6fadd 5911 if (stub_sec != NULL)
deb0e272 5912 {
a4b6fadd
AM
5913 if (h != NULL
5914 && h->elf.root.type == bfd_link_hash_defined
5915 && h->elf.root.u.def.section == htab->sfpr)
5916 {
5917 struct elf_link_hash_entry *s;
5918 char buf[32];
5919 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5920 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5921 if (s == NULL)
5922 return FALSE;
779f2ae7 5923 if (s->root.type == bfd_link_hash_new)
a4b6fadd
AM
5924 {
5925 s->root.type = bfd_link_hash_defined;
5926 s->root.u.def.section = stub_sec;
7dda8d3c 5927 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
5928 + h->elf.root.u.def.value);
5929 s->ref_regular = 1;
5930 s->def_regular = 1;
5931 s->ref_regular_nonweak = 1;
5932 s->forced_local = 1;
5933 s->non_elf = 0;
5934 s->root.linker_def = 1;
5935 }
5936 }
5937 continue;
5938 }
5939 if (h != NULL)
5940 {
5941 h->save_res = 1;
5942 if (!h->elf.def_regular)
deb0e272 5943 {
a4b6fadd
AM
5944 h->elf.root.type = bfd_link_hash_defined;
5945 h->elf.root.u.def.section = htab->sfpr;
5946 h->elf.root.u.def.value = htab->sfpr->size;
5947 h->elf.type = STT_FUNC;
5948 h->elf.def_regular = 1;
b32547cd 5949 h->elf.non_elf = 0;
a4b6fadd
AM
5950 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5951 writing = TRUE;
deb0e272 5952 if (htab->sfpr->contents == NULL)
a4b6fadd 5953 {
2cdcc330
AM
5954 htab->sfpr->contents
5955 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd
AM
5956 if (htab->sfpr->contents == NULL)
5957 return FALSE;
5958 }
deb0e272
AM
5959 }
5960 }
5961 if (writing)
5962 {
5963 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5964 if (i != parm->hi)
5965 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5966 else
5967 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5968 htab->sfpr->size = p - htab->sfpr->contents;
5969 }
5970 }
5971
5972 return TRUE;
5973}
5974
5975static bfd_byte *
5976savegpr0 (bfd *abfd, bfd_byte *p, int r)
5977{
5978 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5979 return p + 4;
5980}
5981
5982static bfd_byte *
5983savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5984{
5985 p = savegpr0 (abfd, p, r);
a078d95a 5986 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5987 p = p + 4;
5988 bfd_put_32 (abfd, BLR, p);
5989 return p + 4;
5990}
5991
5992static bfd_byte *
5993restgpr0 (bfd *abfd, bfd_byte *p, int r)
5994{
5995 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5996 return p + 4;
5997}
5998
5999static bfd_byte *
6000restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6001{
a078d95a 6002 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6003 p = p + 4;
6004 p = restgpr0 (abfd, p, r);
6005 bfd_put_32 (abfd, MTLR_R0, p);
6006 p = p + 4;
6007 if (r == 29)
6008 {
6009 p = restgpr0 (abfd, p, 30);
6010 p = restgpr0 (abfd, p, 31);
6011 }
6012 bfd_put_32 (abfd, BLR, p);
6013 return p + 4;
6014}
6015
6016static bfd_byte *
6017savegpr1 (bfd *abfd, bfd_byte *p, int r)
6018{
6019 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6020 return p + 4;
6021}
6022
6023static bfd_byte *
6024savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6025{
6026 p = savegpr1 (abfd, p, r);
6027 bfd_put_32 (abfd, BLR, p);
6028 return p + 4;
6029}
6030
6031static bfd_byte *
6032restgpr1 (bfd *abfd, bfd_byte *p, int r)
6033{
6034 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6035 return p + 4;
6036}
6037
6038static bfd_byte *
6039restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6040{
6041 p = restgpr1 (abfd, p, r);
6042 bfd_put_32 (abfd, BLR, p);
6043 return p + 4;
6044}
6045
6046static bfd_byte *
6047savefpr (bfd *abfd, bfd_byte *p, int r)
6048{
6049 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6050 return p + 4;
6051}
6052
6053static bfd_byte *
6054savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6055{
6056 p = savefpr (abfd, p, r);
a078d95a 6057 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6058 p = p + 4;
6059 bfd_put_32 (abfd, BLR, p);
6060 return p + 4;
6061}
6062
6063static bfd_byte *
6064restfpr (bfd *abfd, bfd_byte *p, int r)
6065{
6066 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6067 return p + 4;
6068}
6069
6070static bfd_byte *
6071restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6072{
a078d95a 6073 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6074 p = p + 4;
6075 p = restfpr (abfd, p, r);
6076 bfd_put_32 (abfd, MTLR_R0, p);
6077 p = p + 4;
6078 if (r == 29)
6079 {
6080 p = restfpr (abfd, p, 30);
6081 p = restfpr (abfd, p, 31);
6082 }
6083 bfd_put_32 (abfd, BLR, p);
6084 return p + 4;
6085}
6086
6087static bfd_byte *
6088savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6089{
6090 p = savefpr (abfd, p, r);
6091 bfd_put_32 (abfd, BLR, p);
6092 return p + 4;
6093}
6094
6095static bfd_byte *
6096restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6097{
6098 p = restfpr (abfd, p, r);
6099 bfd_put_32 (abfd, BLR, p);
6100 return p + 4;
6101}
6102
6103static bfd_byte *
6104savevr (bfd *abfd, bfd_byte *p, int r)
6105{
6106 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6107 p = p + 4;
6108 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6109 return p + 4;
6110}
6111
6112static bfd_byte *
6113savevr_tail (bfd *abfd, bfd_byte *p, int r)
6114{
6115 p = savevr (abfd, p, r);
6116 bfd_put_32 (abfd, BLR, p);
6117 return p + 4;
6118}
6119
6120static bfd_byte *
6121restvr (bfd *abfd, bfd_byte *p, int r)
6122{
6123 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6124 p = p + 4;
6125 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6126 return p + 4;
6127}
6128
6129static bfd_byte *
6130restvr_tail (bfd *abfd, bfd_byte *p, int r)
6131{
6132 p = restvr (abfd, p, r);
6133 bfd_put_32 (abfd, BLR, p);
6134 return p + 4;
6135}
6136
9e7028aa
AM
6137#define STDU_R1_0R1 0xf8210001
6138#define ADDI_R1_R1 0x38210000
6139
6140/* Emit prologue of wrapper preserving regs around a call to
6141 __tls_get_addr_opt. */
6142
6143static bfd_byte *
6144tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6145{
6146 unsigned int i;
6147
6148 bfd_put_32 (obfd, MFLR_R0, p);
6149 p += 4;
6150 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6151 p += 4;
6152
6153 if (htab->opd_abi)
6154 {
6155 for (i = 4; i < 12; i++)
6156 {
6157 bfd_put_32 (obfd,
6158 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6159 p += 4;
6160 }
6161 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6162 p += 4;
6163 }
6164 else
6165 {
6166 for (i = 4; i < 12; i++)
6167 {
6168 bfd_put_32 (obfd,
6169 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6170 p += 4;
6171 }
6172 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6173 p += 4;
6174 }
6175 return p;
6176}
6177
6178/* Emit epilogue of wrapper preserving regs around a call to
6179 __tls_get_addr_opt. */
6180
6181static bfd_byte *
6182tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6183{
6184 unsigned int i;
6185
6186 if (htab->opd_abi)
6187 {
6188 for (i = 4; i < 12; i++)
6189 {
6190 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6191 p += 4;
6192 }
6193 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6194 p += 4;
6195 }
6196 else
6197 {
6198 for (i = 4; i < 12; i++)
6199 {
6200 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6201 p += 4;
6202 }
6203 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6204 p += 4;
6205 }
6206 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6207 p += 4;
6208 bfd_put_32 (obfd, MTLR_R0, p);
6209 p += 4;
6210 bfd_put_32 (obfd, BLR, p);
6211 p += 4;
6212 return p;
6213}
6214
e86ce104
AM
6215/* Called via elf_link_hash_traverse to transfer dynamic linking
6216 information on function code symbol entries to their corresponding
6217 function descriptor symbol entries. */
deb0e272 6218
b34976b6 6219static bfd_boolean
4ce794b7 6220func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6221{
e86ce104 6222 struct bfd_link_info *info;
65f38f15 6223 struct ppc_link_hash_table *htab;
50bc7936
AM
6224 struct ppc_link_hash_entry *fh;
6225 struct ppc_link_hash_entry *fdh;
6226 bfd_boolean force_local;
5bd4f169 6227
ed7007c1 6228 fh = ppc_elf_hash_entry (h);
50bc7936 6229 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6230 return TRUE;
e86ce104 6231
8c5b4e52
AM
6232 if (!fh->is_func)
6233 return TRUE;
6234
6235 if (fh->elf.root.root.string[0] != '.'
6236 || fh->elf.root.root.string[1] == '\0')
6237 return TRUE;
6238
4ce794b7 6239 info = inf;
65f38f15 6240 htab = ppc_hash_table (info);
4dfe6ac6
NC
6241 if (htab == NULL)
6242 return FALSE;
5bd4f169 6243
8c5b4e52
AM
6244 /* Find the corresponding function descriptor symbol. */
6245 fdh = lookup_fdh (fh, htab);
6246
c09bdfe5
AM
6247 /* Resolve undefined references to dot-symbols as the value
6248 in the function descriptor, if we have one in a regular object.
6249 This is to satisfy cases like ".quad .foo". Calls to functions
6250 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6251 if ((fh->elf.root.type == bfd_link_hash_undefined
6252 || fh->elf.root.type == bfd_link_hash_undefweak)
6253 && (fdh->elf.root.type == bfd_link_hash_defined
6254 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6255 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6256 && opd_entry_value (fdh->elf.root.u.def.section,
6257 fdh->elf.root.u.def.value,
c09bdfe5 6258 &fh->elf.root.u.def.section,
aef36ac1 6259 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6260 {
b31867b6 6261 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6262 fh->elf.forced_local = 1;
b31867b6
AM
6263 fh->elf.def_regular = fdh->elf.def_regular;
6264 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6265 }
6266
8c5b4e52
AM
6267 if (!fh->elf.dynamic)
6268 {
6269 struct plt_entry *ent;
5bd4f169 6270
8c5b4e52
AM
6271 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6272 if (ent->plt.refcount > 0)
6273 break;
6274 if (ent == NULL)
6275 return TRUE;
6276 }
5bd4f169 6277
8c5b4e52 6278 /* Create a descriptor as undefined if necessary. */
50bc7936 6279 if (fdh == NULL
0e1862bb 6280 && !bfd_link_executable (info)
50bc7936
AM
6281 && (fh->elf.root.type == bfd_link_hash_undefined
6282 || fh->elf.root.type == bfd_link_hash_undefweak))
6283 {
908b32fc 6284 fdh = make_fdh (info, fh);
bb700d78
AM
6285 if (fdh == NULL)
6286 return FALSE;
50bc7936 6287 }
648cca2c 6288
8c5b4e52 6289 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6290 if (fdh != NULL
6291 && fdh->fake
8c5b4e52
AM
6292 && (fh->elf.root.type == bfd_link_hash_defined
6293 || fh->elf.root.type == bfd_link_hash_defweak))
6294 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 6295
8c5b4e52
AM
6296 /* Transfer dynamic linking information to the function descriptor. */
6297 if (fdh != NULL)
6298 {
f5385ebf
AM
6299 fdh->elf.ref_regular |= fh->elf.ref_regular;
6300 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6301 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6302 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6303 fdh->elf.dynamic |= fh->elf.dynamic;
6304 fdh->elf.needs_plt |= (fh->elf.needs_plt
6305 || fh->elf.type == STT_FUNC
6306 || fh->elf.type == STT_GNU_IFUNC);
6307 move_plt_plist (fh, fdh);
6308
6309 if (!fdh->elf.forced_local
6310 && fh->elf.dynindx != -1)
6311 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6312 return FALSE;
e86ce104
AM
6313 }
6314
50bc7936
AM
6315 /* Now that the info is on the function descriptor, clear the
6316 function code sym info. Any function code syms for which we
6317 don't have a definition in a regular file, we force local.
6318 This prevents a shared library from exporting syms that have
6319 been imported from another library. Function code syms that
6320 are really in the library we must leave global to prevent the
6321 linker dragging in a definition from a static library. */
93f3fa99
AM
6322 force_local = (!fh->elf.def_regular
6323 || fdh == NULL
6324 || !fdh->elf.def_regular
6325 || fdh->elf.forced_local);
50bc7936
AM
6326 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6327
b34976b6 6328 return TRUE;
e86ce104 6329}
40b8271b 6330
a4b6fadd
AM
6331static const struct sfpr_def_parms save_res_funcs[] =
6332 {
6333 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6334 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6335 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6336 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6337 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6338 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6339 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6340 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6341 { "._savef", 14, 31, savefpr, savefpr1_tail },
6342 { "._restf", 14, 31, restfpr, restfpr1_tail },
6343 { "_savevr_", 20, 31, savevr, savevr_tail },
6344 { "_restvr_", 20, 31, restvr, restvr_tail }
6345 };
6346
e86ce104 6347/* Called near the start of bfd_elf_size_dynamic_sections. We use
c9405344
AM
6348 this hook to a) run the edit functions in this file, b) provide
6349 some gcc support functions, and c) transfer dynamic linking
6350 information gathered so far on function code symbol entries, to
6351 their corresponding function descriptor symbol entries. */
deb0e272 6352
b34976b6 6353static bfd_boolean
c9405344 6354ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
e86ce104
AM
6355{
6356 struct ppc_link_hash_table *htab;
6357
6358 htab = ppc_hash_table (info);
4dfe6ac6
NC
6359 if (htab == NULL)
6360 return FALSE;
6361
c9405344
AM
6362 /* Call back into the linker, which then runs the edit functions. */
6363 htab->params->edit ();
6364
b32547cd
AM
6365 /* Provide any missing _save* and _rest* functions. */
6366 if (htab->sfpr != NULL)
6367 {
6368 unsigned int i;
6369
6370 htab->sfpr->size = 0;
6371 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6372 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6373 return FALSE;
6374 if (htab->sfpr->size == 0)
6375 htab->sfpr->flags |= SEC_EXCLUDE;
6376 }
6377
6378 if (bfd_link_relocatable (info))
6379 return TRUE;
6380
6381 if (htab->elf.hgot != NULL)
dba6fa9b
AM
6382 {
6383 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6384 /* Make .TOC. defined so as to prevent it being made dynamic.
6385 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6386 if (!htab->elf.hgot->def_regular
6387 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6388 {
6389 htab->elf.hgot->root.type = bfd_link_hash_defined;
6390 htab->elf.hgot->root.u.def.value = 0;
6391 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6392 htab->elf.hgot->def_regular = 1;
6393 htab->elf.hgot->root.linker_def = 1;
6394 }
dba6fa9b 6395 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6396 htab->elf.hgot->other
6397 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6398 }
c66bb0ee 6399
8c5b4e52
AM
6400 if (htab->need_func_desc_adj)
6401 {
6402 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6403 htab->need_func_desc_adj = 0;
6404 }
805fc799 6405
b34976b6 6406 return TRUE;
e86ce104
AM
6407}
6408
d311bc8b 6409/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6410 aliases, that apply to read-only sections. Cannot be used after
6411 size_dynamic_sections. */
d311bc8b
AM
6412
6413static bfd_boolean
6414alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6415{
ed7007c1 6416 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
d311bc8b
AM
6417 do
6418 {
5dbc8b37 6419 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
d311bc8b 6420 return TRUE;
ed7007c1 6421 eh = ppc_elf_hash_entry (eh->elf.u.alias);
2cdcc330
AM
6422 }
6423 while (eh != NULL && &eh->elf != h);
d311bc8b
AM
6424
6425 return FALSE;
6426}
8a2058b5 6427
8a9e8e72
AM
6428/* Return whether EH has pc-relative dynamic relocs. */
6429
6430static bfd_boolean
6431pc_dynrelocs (struct ppc_link_hash_entry *eh)
6432{
6433 struct elf_dyn_relocs *p;
6434
190eb1dd 6435 for (p = eh->elf.dyn_relocs; p != NULL; p = p->next)
8a9e8e72
AM
6436 if (p->pc_count != 0)
6437 return TRUE;
6438 return FALSE;
6439}
6440
8a2058b5
AM
6441/* Return true if a global entry stub will be created for H. Valid
6442 for ELFv2 before plt entries have been allocated. */
6443
6444static bfd_boolean
6445global_entry_stub (struct elf_link_hash_entry *h)
6446{
6447 struct plt_entry *pent;
6448
6449 if (!h->pointer_equality_needed
6450 || h->def_regular)
6451 return FALSE;
6452
6453 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6454 if (pent->plt.refcount > 0
6455 && pent->addend == 0)
6456 return TRUE;
6457
6458 return FALSE;
6459}
6460
e86ce104
AM
6461/* Adjust a symbol defined by a dynamic object and referenced by a
6462 regular object. The current definition is in some section of the
6463 dynamic object, but we're not including those sections. We have to
6464 change the definition to something the rest of the link can
6465 understand. */
6466
b34976b6 6467static bfd_boolean
4ce794b7
AM
6468ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6469 struct elf_link_hash_entry *h)
e86ce104
AM
6470{
6471 struct ppc_link_hash_table *htab;
5474d94f 6472 asection *s, *srel;
e86ce104
AM
6473
6474 htab = ppc_hash_table (info);
4dfe6ac6
NC
6475 if (htab == NULL)
6476 return FALSE;
e86ce104
AM
6477
6478 /* Deal with function syms. */
6479 if (h->type == STT_FUNC
e054468f 6480 || h->type == STT_GNU_IFUNC
f5385ebf 6481 || h->needs_plt)
e86ce104 6482 {
ed7007c1 6483 bfd_boolean local = (ppc_elf_hash_entry (h)->save_res
529fe20e
AM
6484 || SYMBOL_CALLS_LOCAL (info, h)
6485 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6486 /* Discard dyn_relocs when non-pic if we've decided that a
6487 function symbol is local and not an ifunc. We keep dynamic
6488 relocs for ifuncs when local rather than always emitting a
6489 plt call stub for them and defining the symbol on the call
6490 stub. We can't do that for ELFv1 anyway (a function symbol
6491 is defined on a descriptor, not code) and it can be faster at
6492 run-time due to not needing to bounce through a stub. The
6493 dyn_relocs for ifuncs will be applied even in a static
6494 executable. */
6495 if (!bfd_link_pic (info)
6496 && h->type != STT_GNU_IFUNC
6497 && local)
190eb1dd 6498 h->dyn_relocs = NULL;
529fe20e 6499
e86ce104
AM
6500 /* Clear procedure linkage table information for any symbol that
6501 won't need a .plt entry. */
411e1bfb
AM
6502 struct plt_entry *ent;
6503 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6504 if (ent->plt.refcount > 0)
6505 break;
8387904d 6506 if (ent == NULL
2d7ad24e
AM
6507 || (h->type != STT_GNU_IFUNC
6508 && local
3e04d765 6509 && (htab->can_convert_all_inline_plt
ed7007c1 6510 || (ppc_elf_hash_entry (h)->tls_mask
3e04d765 6511 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6512 {
411e1bfb 6513 h->plt.plist = NULL;
f5385ebf 6514 h->needs_plt = 0;
d1eca1e4 6515 h->pointer_equality_needed = 0;
40b8271b 6516 }
8a2058b5 6517 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6518 {
d1eca1e4
AM
6519 /* Taking a function's address in a read/write section
6520 doesn't require us to define the function symbol in the
6521 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6522 be used instead. The reason we prefer a few more dynamic
6523 relocs is that calling via a global entry stub costs a
6524 few more instructions, and pointer_equality_needed causes
6525 extra work in ld.so when resolving these symbols. */
529fe20e 6526 if (global_entry_stub (h))
d1eca1e4 6527 {
5dbc8b37 6528 if (!_bfd_elf_readonly_dynrelocs (h))
529fe20e
AM
6529 {
6530 h->pointer_equality_needed = 0;
04383fd1
AM
6531 /* If we haven't seen a branch reloc and the symbol
6532 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6533 if (!h->needs_plt)
6534 h->plt.plist = NULL;
6535 }
6536 else if (!bfd_link_pic (info))
6537 /* We are going to be defining the function symbol on the
6538 plt stub, so no dyn_relocs needed when non-pic. */
190eb1dd 6539 h->dyn_relocs = NULL;
d1eca1e4
AM
6540 }
6541
3988aed5
AM
6542 /* ELFv2 function symbols can't have copy relocs. */
6543 return TRUE;
6544 }
6545 else if (!h->needs_plt
5dbc8b37 6546 && !_bfd_elf_readonly_dynrelocs (h))
3988aed5 6547 {
04383fd1
AM
6548 /* If we haven't seen a branch reloc and the symbol isn't an
6549 ifunc then we don't need a plt entry. */
3988aed5
AM
6550 h->plt.plist = NULL;
6551 h->pointer_equality_needed = 0;
a345bc8d
AM
6552 return TRUE;
6553 }
5bd4f169 6554 }
bbd7ec4a 6555 else
411e1bfb 6556 h->plt.plist = NULL;
5bd4f169
AM
6557
6558 /* If this is a weak symbol, and there is a real definition, the
6559 processor independent code will have arranged for us to see the
6560 real definition first, and we can just use the same value. */
60d67dc8 6561 if (h->is_weakalias)
5bd4f169 6562 {
60d67dc8
AM
6563 struct elf_link_hash_entry *def = weakdef (h);
6564 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6565 h->root.u.def.section = def->root.u.def.section;
6566 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6567 if (def->root.u.def.section == htab->elf.sdynbss
6568 || def->root.u.def.section == htab->elf.sdynrelro)
190eb1dd 6569 h->dyn_relocs = NULL;
b34976b6 6570 return TRUE;
5bd4f169
AM
6571 }
6572
5bd4f169
AM
6573 /* If we are creating a shared library, we must presume that the
6574 only references to the symbol are via the global offset table.
6575 For such cases we need not do anything here; the relocations will
6576 be handled correctly by relocate_section. */
ec73ddcd 6577 if (!bfd_link_executable (info))
b34976b6 6578 return TRUE;
5bd4f169 6579
65f38f15
AM
6580 /* If there are no references to this symbol that do not use the
6581 GOT, we don't need to generate a copy reloc. */
f5385ebf 6582 if (!h->non_got_ref)
b34976b6 6583 return TRUE;
65f38f15 6584
b186458a 6585 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6586 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6587
d93d1c80
AM
6588 /* If -z nocopyreloc was given, don't generate them either. */
6589 || info->nocopyreloc
a127494f 6590
dce2246a 6591 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6592 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1bdd8fac
AM
6593 || (ELIMINATE_COPY_RELOCS
6594 && !h->needs_copy
6595 && !alias_readonly_dynrelocs (h))
65f38f15 6596
d93d1c80
AM
6597 /* Protected variables do not work with .dynbss. The copy in
6598 .dynbss won't be used by the shared library with the protected
6599 definition for the variable. Text relocations are preferable
6600 to an incorrect program. */
6601 || h->protected_def)
529fe20e 6602 return TRUE;
a127494f 6603
e1c6cf61
AM
6604 if (h->type == STT_FUNC
6605 || h->type == STT_GNU_IFUNC)
97b639ba 6606 {
e1c6cf61
AM
6607 /* .dynbss copies of function symbols only work if we have
6608 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6609 use dot-symbols and set the function symbol size to the text
6610 size of the function rather than the size of the descriptor.
6611 That's wrong for copying a descriptor. */
ed7007c1 6612 if (ppc_elf_hash_entry (h)->oh == NULL
e1c6cf61
AM
6613 || !(h->size == 24 || h->size == 16))
6614 return TRUE;
6615
6616 /* We should never get here, but unfortunately there are old
6617 versions of gcc (circa gcc-3.2) that improperly for the
6618 ELFv1 ABI put initialized function pointers, vtable refs and
6619 suchlike in read-only sections. Allow them to proceed, but
6620 warn that this might break at runtime. */
25f53a85 6621 info->callbacks->einfo
c1c8c1ef 6622 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6623 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6624 h->root.root.string);
6625 }
5d35169e
AM
6626
6627 /* This is a reference to a symbol defined by a dynamic object which
6628 is not a function. */
6629
5bd4f169
AM
6630 /* We must allocate the symbol in our .dynbss section, which will
6631 become part of the .bss section of the executable. There will be
6632 an entry for this symbol in the .dynsym section. The dynamic
6633 object will contain position independent code, so all references
6634 from the dynamic object to this symbol will go through the global
6635 offset table. The dynamic linker will use the .dynsym entry to
6636 determine the address it must put in the global offset table, so
6637 both the dynamic object and the regular object will refer to the
6638 same memory location for the variable. */
5474d94f
AM
6639 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6640 {
6641 s = htab->elf.sdynrelro;
6642 srel = htab->elf.sreldynrelro;
6643 }
6644 else
6645 {
6646 s = htab->elf.sdynbss;
6647 srel = htab->elf.srelbss;
6648 }
1d7e9d18 6649 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6650 {
4a7e5234
AM
6651 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6652 linker to copy the initial value out of the dynamic object
6653 and into the runtime process image. */
5474d94f 6654 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6655 h->needs_copy = 1;
5bd4f169
AM
6656 }
6657
529fe20e 6658 /* We no longer want dyn_relocs. */
190eb1dd 6659 h->dyn_relocs = NULL;
6cabe1ea 6660 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6661}
6662
e86ce104
AM
6663/* If given a function descriptor symbol, hide both the function code
6664 sym and the descriptor. */
6665static void
4ce794b7
AM
6666ppc64_elf_hide_symbol (struct bfd_link_info *info,
6667 struct elf_link_hash_entry *h,
6668 bfd_boolean force_local)
e86ce104 6669{
34814b9f 6670 struct ppc_link_hash_entry *eh;
e86ce104
AM
6671 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6672
87469ba2
AM
6673 if (ppc_hash_table (info) == NULL)
6674 return;
6675
ed7007c1 6676 eh = ppc_elf_hash_entry (h);
34814b9f 6677 if (eh->is_func_descriptor)
e86ce104 6678 {
34814b9f 6679 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6680
721956f4 6681 if (fh == NULL)
d1329ca3
AM
6682 {
6683 const char *p, *q;
b8ac2841 6684 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6685 char save;
6686
6687 /* We aren't supposed to use alloca in BFD because on
6688 systems which do not have alloca the version in libiberty
6689 calls xmalloc, which might cause the program to crash
6690 when it runs out of memory. This function doesn't have a
6691 return status, so there's no way to gracefully return an
6692 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6693 accessed; It's either a string in an ELF string table,
6694 or allocated in an objalloc structure. */
d1329ca3 6695
34814b9f 6696 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6697 save = *p;
6698 *(char *) p = '.';
ed7007c1
AM
6699 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, FALSE,
6700 FALSE, FALSE));
d1329ca3
AM
6701 *(char *) p = save;
6702
6703 /* Unfortunately, if it so happens that the string we were
6704 looking for was allocated immediately before this string,
6705 then we overwrote the string terminator. That's the only
6706 reason the lookup should fail. */
6707 if (fh == NULL)
6708 {
34814b9f
AM
6709 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6710 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6711 --q, --p;
34814b9f 6712 if (q < eh->elf.root.root.string && *p == '.')
ed7007c1
AM
6713 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, FALSE,
6714 FALSE, FALSE));
d1329ca3
AM
6715 }
6716 if (fh != NULL)
6717 {
34814b9f
AM
6718 eh->oh = fh;
6719 fh->oh = eh;
d1329ca3
AM
6720 }
6721 }
e86ce104 6722 if (fh != NULL)
34814b9f 6723 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6724 }
6725}
6726
411e1bfb 6727static bfd_boolean
8843416a
AM
6728get_sym_h (struct elf_link_hash_entry **hp,
6729 Elf_Internal_Sym **symp,
6730 asection **symsecp,
f961d9dd 6731 unsigned char **tls_maskp,
8843416a
AM
6732 Elf_Internal_Sym **locsymsp,
6733 unsigned long r_symndx,
6734 bfd *ibfd)
411e1bfb 6735{
0ffa91dd 6736 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6737
6738 if (r_symndx >= symtab_hdr->sh_info)
6739 {
6740 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6741 struct elf_link_hash_entry *h;
6742
6743 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6744 h = elf_follow_link (h);
411e1bfb
AM
6745
6746 if (hp != NULL)
6747 *hp = h;
6748
6749 if (symp != NULL)
6750 *symp = NULL;
6751
6752 if (symsecp != NULL)
6753 {
6754 asection *symsec = NULL;
6755 if (h->root.type == bfd_link_hash_defined
6756 || h->root.type == bfd_link_hash_defweak)
6757 symsec = h->root.u.def.section;
6758 *symsecp = symsec;
6759 }
6760
e7b938ca 6761 if (tls_maskp != NULL)
ed7007c1 6762 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
411e1bfb
AM
6763 }
6764 else
6765 {
6766 Elf_Internal_Sym *sym;
6767 Elf_Internal_Sym *locsyms = *locsymsp;
6768
6769 if (locsyms == NULL)
6770 {
6771 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6772 if (locsyms == NULL)
6773 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6774 symtab_hdr->sh_info,
6775 0, NULL, NULL, NULL);
6776 if (locsyms == NULL)
6777 return FALSE;
6778 *locsymsp = locsyms;
6779 }
6780 sym = locsyms + r_symndx;
6781
6782 if (hp != NULL)
6783 *hp = NULL;
6784
6785 if (symp != NULL)
6786 *symp = sym;
6787
6788 if (symsecp != NULL)
cb33740c 6789 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6790
e7b938ca 6791 if (tls_maskp != NULL)
411e1bfb
AM
6792 {
6793 struct got_entry **lgot_ents;
f961d9dd 6794 unsigned char *tls_mask;
411e1bfb 6795
e7b938ca 6796 tls_mask = NULL;
411e1bfb
AM
6797 lgot_ents = elf_local_got_ents (ibfd);
6798 if (lgot_ents != NULL)
6799 {
e054468f
AM
6800 struct plt_entry **local_plt = (struct plt_entry **)
6801 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6802 unsigned char *lgot_masks = (unsigned char *)
e054468f 6803 (local_plt + symtab_hdr->sh_info);
e7b938ca 6804 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6805 }
e7b938ca 6806 *tls_maskp = tls_mask;
411e1bfb
AM
6807 }
6808 }
6809 return TRUE;
6810}
6811
e7b938ca 6812/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6813 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6814 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6815
6816static int
f961d9dd 6817get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6818 unsigned long *toc_symndx,
6819 bfd_vma *toc_addend,
0d4792f7 6820 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6821 const Elf_Internal_Rela *rel,
6822 bfd *ibfd)
411e1bfb
AM
6823{
6824 unsigned long r_symndx;
0d4792f7 6825 int next_r;
411e1bfb
AM
6826 struct elf_link_hash_entry *h;
6827 Elf_Internal_Sym *sym;
6828 asection *sec;
6829 bfd_vma off;
6830
6831 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6832 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6833 return 0;
411e1bfb 6834
37da22e5
AM
6835 if ((*tls_maskp != NULL
6836 && (**tls_maskp & TLS_TLS) != 0
6837 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 6838 || sec == NULL
6bee8834 6839 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6840 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6841 return 1;
411e1bfb
AM
6842
6843 /* Look inside a TOC section too. */
6844 if (h != NULL)
6845 {
6846 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6847 off = h->root.u.def.value;
6848 }
6849 else
6850 off = sym->st_value;
6851 off += rel->r_addend;
6852 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6853 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6854 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6855 if (toc_symndx != NULL)
6856 *toc_symndx = r_symndx;
3a71aa26
AM
6857 if (toc_addend != NULL)
6858 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6859 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6860 return 0;
854b41e7 6861 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6862 && (next_r == -1 || next_r == -2))
6863 return 1 - next_r;
951fd09b 6864 return 1;
411e1bfb
AM
6865}
6866
3b421ab3
AM
6867/* Find (or create) an entry in the tocsave hash table. */
6868
6869static struct tocsave_entry *
6870tocsave_find (struct ppc_link_hash_table *htab,
6871 enum insert_option insert,
6872 Elf_Internal_Sym **local_syms,
6873 const Elf_Internal_Rela *irela,
6874 bfd *ibfd)
6875{
6876 unsigned long r_indx;
6877 struct elf_link_hash_entry *h;
6878 Elf_Internal_Sym *sym;
6879 struct tocsave_entry ent, *p;
6880 hashval_t hash;
6881 struct tocsave_entry **slot;
6882
6883 r_indx = ELF64_R_SYM (irela->r_info);
6884 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6885 return NULL;
6886 if (ent.sec == NULL || ent.sec->output_section == NULL)
6887 {
4eca0228 6888 _bfd_error_handler
871b3ab2 6889 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
6890 return NULL;
6891 }
6892
6893 if (h != NULL)
6894 ent.offset = h->root.u.def.value;
6895 else
6896 ent.offset = sym->st_value;
6897 ent.offset += irela->r_addend;
6898
6899 hash = tocsave_htab_hash (&ent);
6900 slot = ((struct tocsave_entry **)
6901 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6902 if (slot == NULL)
6903 return NULL;
6904
6905 if (*slot == NULL)
6906 {
6907 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6908 if (p == NULL)
6909 return NULL;
6910 *p = ent;
6911 *slot = p;
6912 }
6913 return *slot;
6914}
6915
754021d0 6916/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6917 code for the old ABI, these will already have been done. */
754021d0
AM
6918
6919static bfd_boolean
6920adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6921{
6922 struct ppc_link_hash_entry *eh;
6923 asection *sym_sec;
74f0fb50 6924 struct _opd_sec_data *opd;
754021d0
AM
6925
6926 if (h->root.type == bfd_link_hash_indirect)
6927 return TRUE;
6928
754021d0
AM
6929 if (h->root.type != bfd_link_hash_defined
6930 && h->root.type != bfd_link_hash_defweak)
6931 return TRUE;
6932
ed7007c1 6933 eh = ppc_elf_hash_entry (h);
754021d0
AM
6934 if (eh->adjust_done)
6935 return TRUE;
6936
6937 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6938 opd = get_opd_info (sym_sec);
6939 if (opd != NULL && opd->adjust != NULL)
754021d0 6940 {
51aecdc5 6941 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
6942 if (adjust == -1)
6943 {
6944 /* This entry has been deleted. */
b3fac117 6945 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6946 if (dsec == NULL)
6947 {
6948 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 6949 if (discarded_section (dsec))
81688140 6950 {
b3fac117 6951 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6952 break;
6953 }
6954 }
4025353c 6955 eh->elf.root.u.def.value = 0;
81688140 6956 eh->elf.root.u.def.section = dsec;
4025353c
AM
6957 }
6958 else
6959 eh->elf.root.u.def.value += adjust;
754021d0
AM
6960 eh->adjust_done = 1;
6961 }
6962 return TRUE;
6963}
6964
8c1d1bb8 6965/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 6966 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
6967 have already been determined. */
6968
6969static bfd_boolean
6970dec_dynrel_count (bfd_vma r_info,
6971 asection *sec,
6972 struct bfd_link_info *info,
6973 Elf_Internal_Sym **local_syms,
6974 struct elf_link_hash_entry *h,
19e08130 6975 Elf_Internal_Sym *sym)
8c1d1bb8
AM
6976{
6977 enum elf_ppc64_reloc_type r_type;
19e08130 6978 asection *sym_sec = NULL;
8c1d1bb8
AM
6979
6980 /* Can this reloc be dynamic? This switch, and later tests here
6981 should be kept in sync with the code in check_relocs. */
6982 r_type = ELF64_R_TYPE (r_info);
6983 switch (r_type)
6984 {
6985 default:
6986 return TRUE;
6987
1bdd8fac
AM
6988 case R_PPC64_TOC16:
6989 case R_PPC64_TOC16_DS:
6990 case R_PPC64_TOC16_LO:
6991 case R_PPC64_TOC16_HI:
6992 case R_PPC64_TOC16_HA:
6993 case R_PPC64_TOC16_LO_DS:
6994 if (h == NULL)
6995 return TRUE;
6996 break;
6997
8c1d1bb8
AM
6998 case R_PPC64_TPREL16:
6999 case R_PPC64_TPREL16_LO:
7000 case R_PPC64_TPREL16_HI:
7001 case R_PPC64_TPREL16_HA:
7002 case R_PPC64_TPREL16_DS:
7003 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7004 case R_PPC64_TPREL16_HIGH:
7005 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7006 case R_PPC64_TPREL16_HIGHER:
7007 case R_PPC64_TPREL16_HIGHERA:
7008 case R_PPC64_TPREL16_HIGHEST:
7009 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8 7010 case R_PPC64_TPREL64:
c213164a 7011 case R_PPC64_TPREL34:
8c1d1bb8
AM
7012 case R_PPC64_DTPMOD64:
7013 case R_PPC64_DTPREL64:
7014 case R_PPC64_ADDR64:
7015 case R_PPC64_REL30:
7016 case R_PPC64_REL32:
7017 case R_PPC64_REL64:
7018 case R_PPC64_ADDR14:
7019 case R_PPC64_ADDR14_BRNTAKEN:
7020 case R_PPC64_ADDR14_BRTAKEN:
7021 case R_PPC64_ADDR16:
7022 case R_PPC64_ADDR16_DS:
7023 case R_PPC64_ADDR16_HA:
7024 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7025 case R_PPC64_ADDR16_HIGH:
7026 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7027 case R_PPC64_ADDR16_HIGHER:
7028 case R_PPC64_ADDR16_HIGHERA:
7029 case R_PPC64_ADDR16_HIGHEST:
7030 case R_PPC64_ADDR16_HIGHESTA:
7031 case R_PPC64_ADDR16_LO:
7032 case R_PPC64_ADDR16_LO_DS:
7033 case R_PPC64_ADDR24:
7034 case R_PPC64_ADDR32:
7035 case R_PPC64_UADDR16:
7036 case R_PPC64_UADDR32:
7037 case R_PPC64_UADDR64:
7038 case R_PPC64_TOC:
5663e321
AM
7039 case R_PPC64_D34:
7040 case R_PPC64_D34_LO:
7041 case R_PPC64_D34_HI30:
7042 case R_PPC64_D34_HA30:
7043 case R_PPC64_ADDR16_HIGHER34:
7044 case R_PPC64_ADDR16_HIGHERA34:
7045 case R_PPC64_ADDR16_HIGHEST34:
7046 case R_PPC64_ADDR16_HIGHESTA34:
7047 case R_PPC64_D28:
8c1d1bb8
AM
7048 break;
7049 }
7050
7051 if (local_syms != NULL)
7052 {
7053 unsigned long r_symndx;
8c1d1bb8
AM
7054 bfd *ibfd = sec->owner;
7055
7056 r_symndx = ELF64_R_SYM (r_info);
7057 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7058 return FALSE;
7059 }
7060
ec73ddcd
AM
7061 if ((h != NULL
7062 && (h->root.type == bfd_link_hash_defweak
7063 || !h->def_regular))
7064 || (h != NULL
7065 && !bfd_link_executable (info)
7066 && !SYMBOLIC_BIND (info, h))
7067 || (bfd_link_pic (info)
7068 && must_be_dyn_reloc (info, r_type))
7069 || (!bfd_link_pic (info)
7070 && (h != NULL
7071 ? h->type == STT_GNU_IFUNC
7072 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
8c1d1bb8
AM
7073 ;
7074 else
7075 return TRUE;
7076
7077 if (h != NULL)
6edfbbad 7078 {
19e08130
AM
7079 struct elf_dyn_relocs *p;
7080 struct elf_dyn_relocs **pp;
190eb1dd 7081 pp = &h->dyn_relocs;
19e08130
AM
7082
7083 /* elf_gc_sweep may have already removed all dyn relocs associated
7084 with local syms for a given section. Also, symbol flags are
7085 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7086 report a dynreloc miscount. */
7087 if (*pp == NULL && info->gc_sections)
7088 return TRUE;
7089
7090 while ((p = *pp) != NULL)
60124e18 7091 {
19e08130
AM
7092 if (p->sec == sec)
7093 {
7094 if (!must_be_dyn_reloc (info, r_type))
7095 p->pc_count -= 1;
7096 p->count -= 1;
7097 if (p->count == 0)
7098 *pp = p->next;
7099 return TRUE;
7100 }
7101 pp = &p->next;
60124e18 7102 }
6edfbbad 7103 }
19e08130
AM
7104 else
7105 {
7106 struct ppc_dyn_relocs *p;
7107 struct ppc_dyn_relocs **pp;
7108 void *vpp;
7109 bfd_boolean is_ifunc;
8c1d1bb8 7110
19e08130
AM
7111 if (local_syms == NULL)
7112 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7113 if (sym_sec == NULL)
7114 sym_sec = sec;
c57da1a7 7115
19e08130
AM
7116 vpp = &elf_section_data (sym_sec)->local_dynrel;
7117 pp = (struct ppc_dyn_relocs **) vpp;
7118
7119 if (*pp == NULL && info->gc_sections)
7120 return TRUE;
7121
7122 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7123 while ((p = *pp) != NULL)
8c1d1bb8 7124 {
19e08130
AM
7125 if (p->sec == sec && p->ifunc == is_ifunc)
7126 {
7127 p->count -= 1;
7128 if (p->count == 0)
7129 *pp = p->next;
7130 return TRUE;
7131 }
7132 pp = &p->next;
8c1d1bb8 7133 }
8c1d1bb8
AM
7134 }
7135
695344c0 7136 /* xgettext:c-format */
cf97bcb0
AM
7137 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7138 sec->owner, sec);
8c1d1bb8
AM
7139 bfd_set_error (bfd_error_bad_value);
7140 return FALSE;
7141}
7142
754021d0
AM
7143/* Remove unused Official Procedure Descriptor entries. Currently we
7144 only remove those associated with functions in discarded link-once
7145 sections, or weakly defined functions that have been overridden. It
7146 would be possible to remove many more entries for statically linked
7147 applications. */
7148
b34976b6 7149bfd_boolean
e7d1c40c 7150ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7151{
7152 bfd *ibfd;
754021d0 7153 bfd_boolean some_edited = FALSE;
3f764659 7154 asection *need_pad = NULL;
e7d1c40c
AM
7155 struct ppc_link_hash_table *htab;
7156
7157 htab = ppc_hash_table (info);
7158 if (htab == NULL)
7159 return FALSE;
1e2f5b6e 7160
c72f2fb2 7161 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7162 {
7163 asection *sec;
7164 Elf_Internal_Rela *relstart, *rel, *relend;
7165 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7166 Elf_Internal_Sym *local_syms;
74f0fb50 7167 struct _opd_sec_data *opd;
51aecdc5 7168 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7169 bfd_size_type cnt_16b = 0;
1e2f5b6e 7170
854b41e7
AM
7171 if (!is_ppc64_elf (ibfd))
7172 continue;
7173
1e2f5b6e 7174 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7175 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7176 continue;
7177
dbaa2011 7178 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7179 continue;
7180
1e2f5b6e
AM
7181 if (sec->output_section == bfd_abs_section_ptr)
7182 continue;
7183
7184 /* Look through the section relocs. */
7185 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7186 continue;
7187
6cdc0ccc 7188 local_syms = NULL;
0ffa91dd 7189 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7190
7191 /* Read the relocations. */
4ce794b7 7192 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7193 info->keep_memory);
1e2f5b6e 7194 if (relstart == NULL)
b34976b6 7195 return FALSE;
1e2f5b6e
AM
7196
7197 /* First run through the relocs to check they are sane, and to
7198 determine whether we need to edit this opd section. */
b34976b6 7199 need_edit = FALSE;
51aecdc5 7200 broken = FALSE;
3f764659 7201 need_pad = sec;
1e2f5b6e 7202 relend = relstart + sec->reloc_count;
50bc7936 7203 for (rel = relstart; rel < relend; )
1e2f5b6e 7204 {
04c9666a 7205 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7206 unsigned long r_symndx;
7207 asection *sym_sec;
7208 struct elf_link_hash_entry *h;
7209 Elf_Internal_Sym *sym;
51aecdc5 7210 bfd_vma offset;
1e2f5b6e 7211
51aecdc5 7212 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7213 only interested in the reloc pointing to a function entry
7214 point. */
51aecdc5
AM
7215 offset = rel->r_offset;
7216 if (rel + 1 == relend
7217 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7218 {
7219 /* If someone messes with .opd alignment then after a
7220 "ld -r" we might have padding in the middle of .opd.
7221 Also, there's nothing to prevent someone putting
7222 something silly in .opd with the assembler. No .opd
b34976b6 7223 optimization for them! */
3f764659 7224 broken_opd:
4eca0228 7225 _bfd_error_handler
871b3ab2 7226 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7227 broken = TRUE;
1e2f5b6e
AM
7228 break;
7229 }
7230
50bc7936
AM
7231 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7232 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7233 {
4eca0228 7234 _bfd_error_handler
695344c0 7235 /* xgettext:c-format */
871b3ab2 7236 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7237 ibfd, r_type);
51aecdc5 7238 broken = TRUE;
50bc7936
AM
7239 break;
7240 }
7241
1e2f5b6e 7242 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7243 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7244 r_symndx, ibfd))
50bc7936 7245 goto error_ret;
1e2f5b6e
AM
7246
7247 if (sym_sec == NULL || sym_sec->owner == NULL)
7248 {
411e1bfb
AM
7249 const char *sym_name;
7250 if (h != NULL)
7251 sym_name = h->root.root.string;
7252 else
26c61ae5
L
7253 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7254 sym_sec);
411e1bfb 7255
4eca0228 7256 _bfd_error_handler
695344c0 7257 /* xgettext:c-format */
871b3ab2 7258 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7259 ibfd, sym_name);
51aecdc5 7260 broken = TRUE;
1e2f5b6e
AM
7261 break;
7262 }
7263
51020317
AM
7264 /* opd entries are always for functions defined in the
7265 current input bfd. If the symbol isn't defined in the
7266 input bfd, then we won't be using the function in this
7267 bfd; It must be defined in a linkonce section in another
7268 bfd, or is weak. It's also possible that we are
7269 discarding the function due to a linker script /DISCARD/,
7270 which we test for via the output_section. */
7271 if (sym_sec->owner != ibfd
7272 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7273 need_edit = TRUE;
1e2f5b6e 7274
50bc7936 7275 rel += 2;
51aecdc5
AM
7276 if (rel + 1 == relend
7277 || (rel + 2 < relend
7278 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7279 ++rel;
7280
7281 if (rel == relend)
3f764659
JJ
7282 {
7283 if (sec->size == offset + 24)
7284 {
7285 need_pad = NULL;
7286 break;
7287 }
51aecdc5 7288 if (sec->size == offset + 16)
3f764659
JJ
7289 {
7290 cnt_16b++;
7291 break;
7292 }
7293 goto broken_opd;
7294 }
3f764659
JJ
7295 else if (rel + 1 < relend
7296 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7297 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7298 {
51aecdc5
AM
7299 if (rel[0].r_offset == offset + 16)
7300 cnt_16b++;
7301 else if (rel[0].r_offset != offset + 24)
7302 goto broken_opd;
3f764659
JJ
7303 }
7304 else
7305 goto broken_opd;
1e2f5b6e
AM
7306 }
7307
e7d1c40c 7308 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7309
51aecdc5 7310 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7311 {
7312 Elf_Internal_Rela *write_rel;
d4730f92 7313 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7314 bfd_byte *rptr, *wptr;
983bddc8 7315 bfd_byte *new_contents;
74f0fb50
AM
7316 bfd_size_type amt;
7317
983bddc8 7318 new_contents = NULL;
51aecdc5 7319 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7320 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7321 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7322 if (opd->adjust == NULL)
7323 return FALSE;
1e2f5b6e
AM
7324
7325 /* This seems a waste of time as input .opd sections are all
7326 zeros as generated by gcc, but I suppose there's no reason
7327 this will always be so. We might start putting something in
7328 the third word of .opd entries. */
7329 if ((sec->flags & SEC_IN_MEMORY) == 0)
7330 {
eea6121a
AM
7331 bfd_byte *loc;
7332 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7333 {
c9594989 7334 free (loc);
50bc7936 7335 error_ret:
c9594989 7336 if (symtab_hdr->contents != (unsigned char *) local_syms)
6cdc0ccc 7337 free (local_syms);
6cdc0ccc
AM
7338 if (elf_section_data (sec)->relocs != relstart)
7339 free (relstart);
b34976b6 7340 return FALSE;
6cdc0ccc 7341 }
1e2f5b6e
AM
7342 sec->contents = loc;
7343 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7344 }
7345
7346 elf_section_data (sec)->relocs = relstart;
7347
3f764659 7348 new_contents = sec->contents;
3f764659
JJ
7349 if (add_aux_fields)
7350 {
7351 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7352 if (new_contents == NULL)
7353 return FALSE;
51aecdc5 7354 need_pad = NULL;
3f764659 7355 }
b4f4e59f
AM
7356 wptr = new_contents;
7357 rptr = sec->contents;
1e2f5b6e 7358 write_rel = relstart;
51aecdc5 7359 for (rel = relstart; rel < relend; )
1e2f5b6e 7360 {
50bc7936
AM
7361 unsigned long r_symndx;
7362 asection *sym_sec;
7363 struct elf_link_hash_entry *h;
51aecdc5 7364 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7365 Elf_Internal_Sym *sym;
51aecdc5
AM
7366 long opd_ent_size;
7367 Elf_Internal_Rela *next_rel;
7368 bfd_boolean skip;
50bc7936
AM
7369
7370 r_symndx = ELF64_R_SYM (rel->r_info);
7371 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7372 r_symndx, ibfd))
50bc7936
AM
7373 goto error_ret;
7374
51aecdc5
AM
7375 next_rel = rel + 2;
7376 if (next_rel + 1 == relend
7377 || (next_rel + 2 < relend
7378 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7379 ++next_rel;
7380
7381 /* See if the .opd entry is full 24 byte or
7382 16 byte (with fd_aux entry overlapped with next
7383 fd_func). */
7384 opd_ent_size = 24;
7385 if (next_rel == relend)
1e2f5b6e 7386 {
51aecdc5 7387 if (sec->size == rel->r_offset + 16)
3f764659 7388 opd_ent_size = 16;
51aecdc5
AM
7389 }
7390 else if (next_rel->r_offset == rel->r_offset + 16)
7391 opd_ent_size = 16;
3f764659 7392
51aecdc5
AM
7393 if (h != NULL
7394 && h->root.root.string[0] == '.')
7395 {
ed7007c1 7396 fdh = ppc_elf_hash_entry (h)->oh;
8c5b4e52
AM
7397 if (fdh != NULL)
7398 {
7399 fdh = ppc_follow_link (fdh);
7400 if (fdh->elf.root.type != bfd_link_hash_defined
7401 && fdh->elf.root.type != bfd_link_hash_defweak)
7402 fdh = NULL;
7403 }
51aecdc5 7404 }
1e2f5b6e 7405
51aecdc5
AM
7406 skip = (sym_sec->owner != ibfd
7407 || sym_sec->output_section == bfd_abs_section_ptr);
7408 if (skip)
7409 {
7410 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7411 {
51aecdc5
AM
7412 /* Arrange for the function descriptor sym
7413 to be dropped. */
7414 fdh->elf.root.u.def.value = 0;
7415 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7416 }
51aecdc5 7417 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7418
0e1862bb 7419 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7420 rel = next_rel;
7421 else
7422 while (1)
7423 {
7424 if (!dec_dynrel_count (rel->r_info, sec, info,
7425 NULL, h, sym))
7426 goto error_ret;
754021d0 7427
51aecdc5
AM
7428 if (++rel == next_rel)
7429 break;
1e2f5b6e 7430
51aecdc5
AM
7431 r_symndx = ELF64_R_SYM (rel->r_info);
7432 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7433 r_symndx, ibfd))
7434 goto error_ret;
7435 }
50bc7936
AM
7436 }
7437 else
1e2f5b6e 7438 {
51aecdc5
AM
7439 /* We'll be keeping this opd entry. */
7440 long adjust;
7441
7442 if (fdh != NULL)
7443 {
7444 /* Redefine the function descriptor symbol to
7445 this location in the opd section. It is
7446 necessary to update the value here rather
7447 than using an array of adjustments as we do
7448 for local symbols, because various places
7449 in the generic ELF code use the value
7450 stored in u.def.value. */
7451 fdh->elf.root.u.def.value = wptr - new_contents;
7452 fdh->adjust_done = 1;
7453 }
7454
7455 /* Local syms are a bit tricky. We could
7456 tweak them as they can be cached, but
7457 we'd need to look through the local syms
7458 for the function descriptor sym which we
7459 don't have at the moment. So keep an
7460 array of adjustments. */
7461 adjust = (wptr - new_contents) - (rptr - sec->contents);
7462 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7463
7464 if (wptr != rptr)
7465 memcpy (wptr, rptr, opd_ent_size);
7466 wptr += opd_ent_size;
7467 if (add_aux_fields && opd_ent_size == 16)
7468 {
7469 memset (wptr, '\0', 8);
7470 wptr += 8;
7471 }
7472
50bc7936 7473 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7474 new opd entries. */
7475 for ( ; rel != next_rel; ++rel)
7476 {
7477 rel->r_offset += adjust;
7478 if (write_rel != rel)
7479 memcpy (write_rel, rel, sizeof (*rel));
7480 ++write_rel;
7481 }
1e2f5b6e 7482 }
51aecdc5
AM
7483
7484 rptr += opd_ent_size;
1e2f5b6e
AM
7485 }
7486
3f764659 7487 sec->size = wptr - new_contents;
1e2f5b6e 7488 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7489 if (add_aux_fields)
7490 {
7491 free (sec->contents);
7492 sec->contents = new_contents;
7493 }
7494
05bf9422 7495 /* Fudge the header size too, as this is used later in
cdcf6e38 7496 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7497 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7498 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7499 some_edited = TRUE;
1e2f5b6e 7500 }
6cdc0ccc 7501 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7502 free (relstart);
6cdc0ccc 7503
411e1bfb
AM
7504 if (local_syms != NULL
7505 && symtab_hdr->contents != (unsigned char *) local_syms)
7506 {
7507 if (!info->keep_memory)
7508 free (local_syms);
7509 else
7510 symtab_hdr->contents = (unsigned char *) local_syms;
7511 }
7512 }
7513
754021d0
AM
7514 if (some_edited)
7515 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7516
3f764659
JJ
7517 /* If we are doing a final link and the last .opd entry is just 16 byte
7518 long, add a 8 byte padding after it. */
0e1862bb 7519 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7520 {
7521 bfd_byte *p;
7522
7523 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7524 {
7525 BFD_ASSERT (need_pad->size > 0);
7526
7527 p = bfd_malloc (need_pad->size + 8);
7528 if (p == NULL)
7529 return FALSE;
699733f6 7530
2cdcc330
AM
7531 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7532 p, 0, need_pad->size))
3f764659
JJ
7533 return FALSE;
7534
7535 need_pad->contents = p;
7536 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7537 }
7538 else
7539 {
7540 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7541 if (p == NULL)
7542 return FALSE;
7543
7544 need_pad->contents = p;
7545 }
7546
7547 memset (need_pad->contents + need_pad->size, 0, 8);
7548 need_pad->size += 8;
7549 }
7550
411e1bfb
AM
7551 return TRUE;
7552}
7553
3e04d765
AM
7554/* Analyze inline PLT call relocations to see whether calls to locally
7555 defined functions can be converted to direct calls. */
7556
7557bfd_boolean
7558ppc64_elf_inline_plt (struct bfd_link_info *info)
7559{
7560 struct ppc_link_hash_table *htab;
7561 bfd *ibfd;
7562 asection *sec;
7563 bfd_vma low_vma, high_vma, limit;
7564
7565 htab = ppc_hash_table (info);
7566 if (htab == NULL)
7567 return FALSE;
7568
7569 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7570 reduced somewhat to cater for possible stubs that might be added
7571 between the call and its destination. */
7572 if (htab->params->group_size < 0)
7573 {
7574 limit = -htab->params->group_size;
7575 if (limit == 1)
7576 limit = 0x1e00000;
7577 }
7578 else
7579 {
7580 limit = htab->params->group_size;
7581 if (limit == 1)
7582 limit = 0x1c00000;
7583 }
7584
7585 low_vma = -1;
7586 high_vma = 0;
7587 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7588 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7589 {
7590 if (low_vma > sec->vma)
7591 low_vma = sec->vma;
7592 if (high_vma < sec->vma + sec->size)
7593 high_vma = sec->vma + sec->size;
7594 }
7595
7596 /* If a "bl" can reach anywhere in local code sections, then we can
7597 convert all inline PLT sequences to direct calls when the symbol
7598 is local. */
7599 if (high_vma - low_vma < limit)
7600 {
7601 htab->can_convert_all_inline_plt = 1;
7602 return TRUE;
7603 }
7604
7605 /* Otherwise, go looking through relocs for cases where a direct
7606 call won't reach. Mark the symbol on any such reloc to disable
7607 the optimization and keep the PLT entry as it seems likely that
7608 this will be better than creating trampolines. Note that this
7609 will disable the optimization for all inline PLT calls to a
7610 particular symbol, not just those that won't reach. The
7611 difficulty in doing a more precise optimization is that the
7612 linker needs to make a decision depending on whether a
7613 particular R_PPC64_PLTCALL insn can be turned into a direct
7614 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7615 the sequence, and there is nothing that ties those relocs
7616 together except their symbol. */
7617
7618 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7619 {
7620 Elf_Internal_Shdr *symtab_hdr;
7621 Elf_Internal_Sym *local_syms;
7622
7623 if (!is_ppc64_elf (ibfd))
7624 continue;
7625
7626 local_syms = NULL;
7627 symtab_hdr = &elf_symtab_hdr (ibfd);
7628
7629 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7630 if (ppc64_elf_section_data (sec)->has_pltcall
7631 && !bfd_is_abs_section (sec->output_section))
7632 {
7633 Elf_Internal_Rela *relstart, *rel, *relend;
7634
7635 /* Read the relocations. */
7636 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7637 info->keep_memory);
7638 if (relstart == NULL)
7639 return FALSE;
7640
7641 relend = relstart + sec->reloc_count;
435edf0b 7642 for (rel = relstart; rel < relend; rel++)
3e04d765
AM
7643 {
7644 enum elf_ppc64_reloc_type r_type;
7645 unsigned long r_symndx;
7646 asection *sym_sec;
7647 struct elf_link_hash_entry *h;
7648 Elf_Internal_Sym *sym;
7649 unsigned char *tls_maskp;
7650
7651 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7652 if (r_type != R_PPC64_PLTCALL
7653 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7654 continue;
7655
7656 r_symndx = ELF64_R_SYM (rel->r_info);
7657 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7658 r_symndx, ibfd))
7659 {
7660 if (elf_section_data (sec)->relocs != relstart)
7661 free (relstart);
c9594989 7662 if (symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765
AM
7663 free (local_syms);
7664 return FALSE;
7665 }
7666
7667 if (sym_sec != NULL && sym_sec->output_section != NULL)
7668 {
7669 bfd_vma from, to;
7670 if (h != NULL)
7671 to = h->root.u.def.value;
7672 else
7673 to = sym->st_value;
7674 to += (rel->r_addend
7675 + sym_sec->output_offset
7676 + sym_sec->output_section->vma);
7677 from = (rel->r_offset
7678 + sec->output_offset
7679 + sec->output_section->vma);
5663e321
AM
7680 if (to - from + limit < 2 * limit
7681 && !(r_type == R_PPC64_PLTCALL_NOTOC
7682 && (((h ? h->other : sym->st_other)
7683 & STO_PPC64_LOCAL_MASK)
4a4e7361 7684 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7685 *tls_maskp &= ~PLT_KEEP;
7686 }
7687 }
7688 if (elf_section_data (sec)->relocs != relstart)
7689 free (relstart);
7690 }
7691
7692 if (local_syms != NULL
7693 && symtab_hdr->contents != (unsigned char *) local_syms)
7694 {
7695 if (!info->keep_memory)
7696 free (local_syms);
7697 else
7698 symtab_hdr->contents = (unsigned char *) local_syms;
7699 }
7700 }
7701
7702 return TRUE;
7703}
7704
c9405344
AM
7705/* Set htab->tls_get_addr and various other info specific to TLS.
7706 This needs to run before dynamic symbols are processed in
7707 bfd_elf_size_dynamic_sections. */
411e1bfb 7708
c9405344 7709bfd_boolean
e7d1c40c 7710ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7711{
411e1bfb 7712 struct ppc_link_hash_table *htab;
9e7028aa 7713 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
411e1bfb 7714
411e1bfb 7715 htab = ppc_hash_table (info);
4dfe6ac6 7716 if (htab == NULL)
c9405344 7717 return FALSE;
4dfe6ac6 7718
ee67d69a
AM
7719 if (abiversion (info->output_bfd) == 1)
7720 htab->opd_abi = 1;
7721
e7d1c40c 7722 if (htab->params->no_multi_toc)
33c0ec9d
AM
7723 htab->do_multi_toc = 0;
7724 else if (!htab->do_multi_toc)
e7d1c40c 7725 htab->params->no_multi_toc = 1;
33c0ec9d 7726
8b5f1ed8
AM
7727 /* Default to --no-plt-localentry, as this option can cause problems
7728 with symbol interposition. For example, glibc libpthread.so and
7729 libc.so duplicate many pthread symbols, with a fallback
7730 implementation in libc.so. In some cases the fallback does more
7731 work than the pthread implementation. __pthread_condattr_destroy
7732 is one such symbol: the libpthread.so implementation is
7733 localentry:0 while the libc.so implementation is localentry:8.
7734 An app that "cleverly" uses dlopen to only load necessary
7735 libraries at runtime may omit loading libpthread.so when not
7736 running multi-threaded, which then results in the libc.so
7737 fallback symbols being used and ld.so complaining. Now there
7738 are workarounds in ld (see non_zero_localentry) to detect the
7739 pthread situation, but that may not be the only case where
7740 --plt-localentry can cause trouble. */
f378ab09 7741 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7742 htab->params->plt_localentry0 = 0;
3cd7c7d7
AM
7743 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7744 {
7745 /* The issue is that __glink_PLTresolve saves r2, which is done
7746 because glibc ld.so _dl_runtime_resolve restores r2 to support
7747 a glibc plt call optimisation where global entry code is
7748 skipped on calls that resolve to the same binary. The
7749 __glink_PLTresolve save of r2 is incompatible with code
7750 making tail calls, because the tail call might go via the
7751 resolver and thus overwrite the proper saved r2. */
7752 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7753 "power10 pc-relative code"));
7754 htab->params->plt_localentry0 = 0;
7755 }
d44c746a
AM
7756 if (htab->params->plt_localentry0
7757 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7758 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
7759 _bfd_error_handler
7760 (_("warning: --plt-localentry is especially dangerous without "
7761 "ld.so support to detect ABI violations"));
f378ab09 7762
9e7028aa
AM
7763 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7764 FALSE, FALSE, TRUE);
7765 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7766
a7f2871e 7767 /* Move dynamic linking info to the function descriptor sym. */
9e7028aa
AM
7768 if (tga != NULL)
7769 func_desc_adjust (tga, info);
7770 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7771 FALSE, FALSE, TRUE);
7772 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7773
7774 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
7775 FALSE, FALSE, TRUE);
7776 htab->tga_desc = ppc_elf_hash_entry (desc);
7777 if (desc != NULL)
7778 func_desc_adjust (desc, info);
7779 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
7780 FALSE, FALSE, TRUE);
7781 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7782
7c9cf415 7783 if (htab->params->tls_get_addr_opt)
a7f2871e 7784 {
9e7028aa 7785 struct elf_link_hash_entry *opt, *opt_fd;
a7f2871e
AM
7786
7787 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7788 FALSE, FALSE, TRUE);
7789 if (opt != NULL)
7790 func_desc_adjust (opt, info);
7791 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7792 FALSE, FALSE, TRUE);
7793 if (opt_fd != NULL
7794 && (opt_fd->root.type == bfd_link_hash_defined
7795 || opt_fd->root.type == bfd_link_hash_defweak))
7796 {
7797 /* If glibc supports an optimized __tls_get_addr call stub,
7798 signalled by the presence of __tls_get_addr_opt, and we'll
7799 be calling __tls_get_addr via a plt call stub, then
7800 make __tls_get_addr point to __tls_get_addr_opt. */
9e7028aa
AM
7801 if (!(htab->elf.dynamic_sections_created
7802 && tga_fd != NULL
7803 && (tga_fd->type == STT_FUNC
7804 || tga_fd->needs_plt)
7805 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7806 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
7807 tga_fd = NULL;
7808 if (!(htab->elf.dynamic_sections_created
7809 && desc_fd != NULL
7810 && (desc_fd->type == STT_FUNC
7811 || desc_fd->needs_plt)
7812 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
7813 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
7814 desc_fd = NULL;
7815
7816 if (tga_fd != NULL || desc_fd != NULL)
7817 {
7818 struct plt_entry *ent = NULL;
7819
7820 if (tga_fd != NULL)
7821 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7822 if (ent->plt.refcount > 0)
7823 break;
7824 if (ent == NULL && desc_fd != NULL)
7825 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
7826 if (ent->plt.refcount > 0)
7827 break;
a7f2871e
AM
7828 if (ent != NULL)
7829 {
9e7028aa
AM
7830 if (tga_fd != NULL)
7831 {
7832 tga_fd->root.type = bfd_link_hash_indirect;
7833 tga_fd->root.u.i.link = &opt_fd->root;
7834 tga_fd->root.u.i.warning = NULL;
7835 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7836 }
7837 if (desc_fd != NULL)
7838 {
7839 desc_fd->root.type = bfd_link_hash_indirect;
7840 desc_fd->root.u.i.link = &opt_fd->root;
7841 desc_fd->root.u.i.warning = NULL;
7842 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
7843 }
b531344c 7844 opt_fd->mark = 1;
a7f2871e
AM
7845 if (opt_fd->dynindx != -1)
7846 {
7847 /* Use __tls_get_addr_opt in dynamic relocations. */
7848 opt_fd->dynindx = -1;
7849 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7850 opt_fd->dynstr_index);
7851 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
c9405344 7852 return FALSE;
a7f2871e 7853 }
9e7028aa 7854 if (tga_fd != NULL)
a7f2871e 7855 {
9e7028aa 7856 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
9e51d549 7857 tga = (struct elf_link_hash_entry *) htab->tls_get_addr;
9e7028aa
AM
7858 if (opt != NULL && tga != NULL)
7859 {
7860 tga->root.type = bfd_link_hash_indirect;
7861 tga->root.u.i.link = &opt->root;
7862 tga->root.u.i.warning = NULL;
7863 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7864 opt->mark = 1;
7865 _bfd_elf_link_hash_hide_symbol (info, opt,
7866 tga->forced_local);
7867 htab->tls_get_addr = ppc_elf_hash_entry (opt);
7868 }
7869 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7870 htab->tls_get_addr_fd->is_func_descriptor = 1;
7871 if (htab->tls_get_addr != NULL)
7872 {
7873 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7874 htab->tls_get_addr->is_func = 1;
7875 }
a7f2871e 7876 }
9e7028aa 7877 if (desc_fd != NULL)
a7f2871e 7878 {
9e7028aa
AM
7879 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
7880 if (opt != NULL && desc != NULL)
7881 {
7882 desc->root.type = bfd_link_hash_indirect;
7883 desc->root.u.i.link = &opt->root;
7884 desc->root.u.i.warning = NULL;
7885 ppc64_elf_copy_indirect_symbol (info, opt, desc);
7886 opt->mark = 1;
7887 _bfd_elf_link_hash_hide_symbol (info, opt,
7888 desc->forced_local);
7889 htab->tga_desc = ppc_elf_hash_entry (opt);
7890 }
7891 htab->tga_desc_fd->oh = htab->tga_desc;
7892 htab->tga_desc_fd->is_func_descriptor = 1;
7893 if (htab->tga_desc != NULL)
7894 {
7895 htab->tga_desc->oh = htab->tga_desc_fd;
7896 htab->tga_desc->is_func = 1;
7897 }
a7f2871e
AM
7898 }
7899 }
7900 }
7901 }
7c9cf415
AM
7902 else if (htab->params->tls_get_addr_opt < 0)
7903 htab->params->tls_get_addr_opt = 0;
a7f2871e 7904 }
9e7028aa
AM
7905
7906 if (htab->tga_desc_fd != NULL
7907 && htab->params->tls_get_addr_opt
7908 && htab->params->no_tls_get_addr_regsave == -1)
7909 htab->params->no_tls_get_addr_regsave = 0;
7910
c9405344 7911 return TRUE;
3a71aa26 7912}
8387904d 7913
3a71aa26 7914/* Return TRUE iff REL is a branch reloc with a global symbol matching
9e7028aa 7915 any of HASH1, HASH2, HASH3, or HASH4. */
8387904d 7916
3a71aa26
AM
7917static bfd_boolean
7918branch_reloc_hash_match (const bfd *ibfd,
7919 const Elf_Internal_Rela *rel,
7920 const struct ppc_link_hash_entry *hash1,
9e7028aa
AM
7921 const struct ppc_link_hash_entry *hash2,
7922 const struct ppc_link_hash_entry *hash3,
7923 const struct ppc_link_hash_entry *hash4)
3a71aa26
AM
7924{
7925 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7926 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7927 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7928
e054468f 7929 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7930 {
3a71aa26
AM
7931 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7932 struct elf_link_hash_entry *h;
8387904d 7933
3a71aa26 7934 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7935 h = elf_follow_link (h);
9e51d549
AM
7936 if (h == (struct elf_link_hash_entry *) hash1
7937 || h == (struct elf_link_hash_entry *) hash2
7938 || h == (struct elf_link_hash_entry *) hash3
7939 || h == (struct elf_link_hash_entry *) hash4)
3a71aa26 7940 return TRUE;
a48ebf4d 7941 }
3a71aa26 7942 return FALSE;
951fd09b 7943}
411e1bfb 7944
951fd09b
AM
7945/* Run through all the TLS relocs looking for optimization
7946 opportunities. The linker has been hacked (see ppc64elf.em) to do
7947 a preliminary section layout so that we know the TLS segment
7948 offsets. We can't optimize earlier because some optimizations need
7949 to know the tp offset, and we need to optimize before allocating
7950 dynamic relocations. */
7951
7952bfd_boolean
33c0ec9d 7953ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7954{
7955 bfd *ibfd;
7956 asection *sec;
7957 struct ppc_link_hash_table *htab;
663a1470 7958 unsigned char *toc_ref;
102890f0 7959 int pass;
951fd09b 7960
3cbc1e5e 7961 if (!bfd_link_executable (info))
411e1bfb
AM
7962 return TRUE;
7963
951fd09b 7964 htab = ppc_hash_table (info);
4dfe6ac6
NC
7965 if (htab == NULL)
7966 return FALSE;
7967
252dcdf4
AM
7968 htab->do_tls_opt = 1;
7969
663a1470
AM
7970 /* Make two passes over the relocs. On the first pass, mark toc
7971 entries involved with tls relocs, and check that tls relocs
7972 involved in setting up a tls_get_addr call are indeed followed by
7973 such a call. If they are not, we can't do any tls optimization.
7974 On the second pass twiddle tls_mask flags to notify
7975 relocate_section that optimization can be done, and adjust got
7976 and plt refcounts. */
7977 toc_ref = NULL;
7978 for (pass = 0; pass < 2; ++pass)
c72f2fb2 7979 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
7980 {
7981 Elf_Internal_Sym *locsyms = NULL;
7982 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7983
102890f0
AM
7984 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7985 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7986 {
7987 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7988 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7989
102890f0
AM
7990 /* Read the relocations. */
7991 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7992 info->keep_memory);
7993 if (relstart == NULL)
2915c55b
JK
7994 {
7995 free (toc_ref);
7996 return FALSE;
7997 }
411e1bfb 7998
102890f0
AM
7999 relend = relstart + sec->reloc_count;
8000 for (rel = relstart; rel < relend; rel++)
8001 {
8002 enum elf_ppc64_reloc_type r_type;
8003 unsigned long r_symndx;
8004 struct elf_link_hash_entry *h;
8005 Elf_Internal_Sym *sym;
8006 asection *sym_sec;
f961d9dd 8007 unsigned char *tls_mask;
46e9995a 8008 unsigned int tls_set, tls_clear, tls_type = 0;
102890f0
AM
8009 bfd_vma value;
8010 bfd_boolean ok_tprel, is_local;
8011 long toc_ref_index = 0;
8012 int expecting_tls_get_addr = 0;
663a1470 8013 bfd_boolean ret = FALSE;
411e1bfb 8014
102890f0
AM
8015 r_symndx = ELF64_R_SYM (rel->r_info);
8016 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8017 r_symndx, ibfd))
8018 {
8019 err_free_rel:
8020 if (elf_section_data (sec)->relocs != relstart)
8021 free (relstart);
c9594989
AM
8022 free (toc_ref);
8023 if (elf_symtab_hdr (ibfd).contents
8024 != (unsigned char *) locsyms)
102890f0 8025 free (locsyms);
663a1470 8026 return ret;
102890f0 8027 }
411e1bfb 8028
102890f0
AM
8029 if (h != NULL)
8030 {
766bc656
AM
8031 if (h->root.type == bfd_link_hash_defined
8032 || h->root.type == bfd_link_hash_defweak)
8033 value = h->root.u.def.value;
8034 else if (h->root.type == bfd_link_hash_undefweak)
8035 value = 0;
8036 else
663a1470
AM
8037 {
8038 found_tls_get_addr_arg = 0;
8039 continue;
8040 }
102890f0
AM
8041 }
8042 else
8043 /* Symbols referenced by TLS relocs must be of type
8044 STT_TLS. So no need for .opd local sym adjust. */
8045 value = sym->st_value;
8046
8047 ok_tprel = FALSE;
f749f26e
AM
8048 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8049 if (is_local)
102890f0 8050 {
766bc656
AM
8051 if (h != NULL
8052 && h->root.type == bfd_link_hash_undefweak)
8053 ok_tprel = TRUE;
c27b8c2a
AM
8054 else if (sym_sec != NULL
8055 && sym_sec->output_section != NULL)
766bc656
AM
8056 {
8057 value += sym_sec->output_offset;
8058 value += sym_sec->output_section->vma;
0b147428 8059 value -= htab->elf.tls_sec->vma + TP_OFFSET;
c213164a
AM
8060 /* Note that even though the prefix insns
8061 allow a 1<<33 offset we use the same test
8062 as for addis;addi. There may be a mix of
8063 pcrel and non-pcrel code and the decision
8064 to optimise is per symbol, not per TLS
8065 sequence. */
0b147428 8066 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
766bc656 8067 }
102890f0 8068 }
951fd09b 8069
102890f0 8070 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8071 /* If this section has old-style __tls_get_addr calls
8072 without marker relocs, then check that each
8073 __tls_get_addr call reloc is preceded by a reloc
8074 that conceivably belongs to the __tls_get_addr arg
8075 setup insn. If we don't find matching arg setup
8076 relocs, don't do any tls optimization. */
8077 if (pass == 0
9737e8af 8078 && sec->nomark_tls_get_addr
663a1470 8079 && h != NULL
ed7007c1 8080 && is_tls_get_addr (h, htab)
663a1470
AM
8081 && !found_tls_get_addr_arg
8082 && is_branch_reloc (r_type))
8083 {
25f53a85 8084 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8085 "TLS optimization disabled\n"),
8086 ibfd, sec, rel->r_offset);
8087 ret = TRUE;
8088 goto err_free_rel;
8089 }
8090
8091 found_tls_get_addr_arg = 0;
102890f0
AM
8092 switch (r_type)
8093 {
8094 case R_PPC64_GOT_TLSLD16:
8095 case R_PPC64_GOT_TLSLD16_LO:
87c69f97 8096 case R_PPC64_GOT_TLSLD_PCREL34:
102890f0 8097 expecting_tls_get_addr = 1;
663a1470 8098 found_tls_get_addr_arg = 1;
1a0670f3 8099 /* Fall through. */
102890f0
AM
8100
8101 case R_PPC64_GOT_TLSLD16_HI:
8102 case R_PPC64_GOT_TLSLD16_HA:
8103 /* These relocs should never be against a symbol
8104 defined in a shared lib. Leave them alone if
8105 that turns out to be the case. */
8106 if (!is_local)
8107 continue;
411e1bfb 8108
102890f0 8109 /* LD -> LE */
411e1bfb 8110 tls_set = 0;
102890f0
AM
8111 tls_clear = TLS_LD;
8112 tls_type = TLS_TLS | TLS_LD;
8113 break;
411e1bfb 8114
102890f0
AM
8115 case R_PPC64_GOT_TLSGD16:
8116 case R_PPC64_GOT_TLSGD16_LO:
87c69f97 8117 case R_PPC64_GOT_TLSGD_PCREL34:
102890f0 8118 expecting_tls_get_addr = 1;
663a1470 8119 found_tls_get_addr_arg = 1;
1a0670f3 8120 /* Fall through. */
102890f0
AM
8121
8122 case R_PPC64_GOT_TLSGD16_HI:
8123 case R_PPC64_GOT_TLSGD16_HA:
8124 if (ok_tprel)
8125 /* GD -> LE */
411e1bfb 8126 tls_set = 0;
102890f0
AM
8127 else
8128 /* GD -> IE */
b00a0a86 8129 tls_set = TLS_TLS | TLS_GDIE;
102890f0
AM
8130 tls_clear = TLS_GD;
8131 tls_type = TLS_TLS | TLS_GD;
8132 break;
8133
87c69f97 8134 case R_PPC64_GOT_TPREL_PCREL34:
102890f0
AM
8135 case R_PPC64_GOT_TPREL16_DS:
8136 case R_PPC64_GOT_TPREL16_LO_DS:
8137 case R_PPC64_GOT_TPREL16_HI:
8138 case R_PPC64_GOT_TPREL16_HA:
8139 if (ok_tprel)
8140 {
8141 /* IE -> LE */
8142 tls_set = 0;
8143 tls_clear = TLS_TPREL;
8144 tls_type = TLS_TLS | TLS_TPREL;
8145 break;
8146 }
411e1bfb
AM
8147 continue;
8148
727fc41e 8149 case R_PPC64_TLSLD:
7d04a20a
AM
8150 if (!is_local)
8151 continue;
8152 /* Fall through. */
8153 case R_PPC64_TLSGD:
23cedd1d
AM
8154 if (rel + 1 < relend
8155 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8156 {
8157 if (pass != 0
2cdcc330 8158 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
8159 != R_PPC64_PLTSEQ)
8160 && (ELF64_R_TYPE (rel[1].r_info)
8161 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
8162 {
8163 r_symndx = ELF64_R_SYM (rel[1].r_info);
8164 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 8165 r_symndx, ibfd))
23cedd1d
AM
8166 goto err_free_rel;
8167 if (h != NULL)
8168 {
8169 struct plt_entry *ent = NULL;
8170
8171 for (ent = h->plt.plist;
8172 ent != NULL;
8173 ent = ent->next)
8174 if (ent->addend == rel[1].r_addend)
8175 break;
8176
8177 if (ent != NULL
8178 && ent->plt.refcount > 0)
8179 ent->plt.refcount -= 1;
8180 }
8181 }
8182 continue;
8183 }
663a1470 8184 found_tls_get_addr_arg = 1;
1a0670f3 8185 /* Fall through. */
663a1470
AM
8186
8187 case R_PPC64_TLS:
8188 case R_PPC64_TOC16:
8189 case R_PPC64_TOC16_LO:
102890f0
AM
8190 if (sym_sec == NULL || sym_sec != toc)
8191 continue;
8192
8193 /* Mark this toc entry as referenced by a TLS
8194 code sequence. We can do that now in the
8195 case of R_PPC64_TLS, and after checking for
8196 tls_get_addr for the TOC16 relocs. */
8197 if (toc_ref == NULL)
2cdcc330
AM
8198 toc_ref
8199 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
8200 if (toc_ref == NULL)
8201 goto err_free_rel;
8202
102890f0
AM
8203 if (h != NULL)
8204 value = h->root.u.def.value;
8205 else
8206 value = sym->st_value;
8207 value += rel->r_addend;
73242275
AM
8208 if (value % 8 != 0)
8209 continue;
8210 BFD_ASSERT (value < toc->size
8211 && toc->output_offset % 8 == 0);
663a1470 8212 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8213 if (r_type == R_PPC64_TLS
8214 || r_type == R_PPC64_TLSGD
8215 || r_type == R_PPC64_TLSLD)
102890f0
AM
8216 {
8217 toc_ref[toc_ref_index] = 1;
8218 continue;
8219 }
8220
8221 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8222 continue;
8223
8224 tls_set = 0;
8225 tls_clear = 0;
8226 expecting_tls_get_addr = 2;
8227 break;
8228
8229 case R_PPC64_TPREL64:
8230 if (pass == 0
8231 || sec != toc
8232 || toc_ref == NULL
663a1470 8233 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8234 continue;
8235 if (ok_tprel)
8236 {
8237 /* IE -> LE */
8238 tls_set = TLS_EXPLICIT;
8239 tls_clear = TLS_TPREL;
8240 break;
8241 }
8242 continue;
8243
8244 case R_PPC64_DTPMOD64:
8245 if (pass == 0
8246 || sec != toc
8247 || toc_ref == NULL
663a1470 8248 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8249 continue;
8250 if (rel + 1 < relend
8251 && (rel[1].r_info
8252 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8253 && rel[1].r_offset == rel->r_offset + 8)
8254 {
8255 if (ok_tprel)
8256 /* GD -> LE */
8257 tls_set = TLS_EXPLICIT | TLS_GD;
8258 else
8259 /* GD -> IE */
b00a0a86 8260 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
102890f0
AM
8261 tls_clear = TLS_GD;
8262 }
8263 else
8264 {
8265 if (!is_local)
8266 continue;
8267
8268 /* LD -> LE */
8269 tls_set = TLS_EXPLICIT;
8270 tls_clear = TLS_LD;
8271 }
8272 break;
8273
252dcdf4
AM
8274 case R_PPC64_TPREL16_HA:
8275 if (pass == 0)
8276 {
8277 unsigned char buf[4];
8278 unsigned int insn;
8279 bfd_vma off = rel->r_offset & ~3;
8280 if (!bfd_get_section_contents (ibfd, sec, buf,
8281 off, 4))
8282 goto err_free_rel;
8283 insn = bfd_get_32 (ibfd, buf);
8284 /* addis rt,13,imm */
8285 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8286 != ((15u << 26) | (13 << 16)))
8287 {
8288 /* xgettext:c-format */
8289 info->callbacks->minfo
8290 (_("%H: warning: %s unexpected insn %#x.\n"),
8291 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8292 htab->do_tls_opt = 0;
8293 }
8294 }
8295 continue;
8296
8297 case R_PPC64_TPREL16_HI:
8298 case R_PPC64_TPREL16_HIGH:
8299 case R_PPC64_TPREL16_HIGHA:
8300 case R_PPC64_TPREL16_HIGHER:
8301 case R_PPC64_TPREL16_HIGHERA:
8302 case R_PPC64_TPREL16_HIGHEST:
8303 case R_PPC64_TPREL16_HIGHESTA:
8304 /* These can all be used in sequences along with
8305 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8306 able to verify easily. */
8307 htab->do_tls_opt = 0;
8308 continue;
8309
102890f0
AM
8310 default:
8311 continue;
8312 }
8313
8314 if (pass == 0)
8315 {
727fc41e 8316 if (!expecting_tls_get_addr
9737e8af 8317 || !sec->nomark_tls_get_addr)
102890f0
AM
8318 continue;
8319
3a71aa26
AM
8320 if (rel + 1 < relend
8321 && branch_reloc_hash_match (ibfd, rel + 1,
9e7028aa
AM
8322 htab->tls_get_addr_fd,
8323 htab->tga_desc_fd,
3a71aa26 8324 htab->tls_get_addr,
9e7028aa 8325 htab->tga_desc))
102890f0 8326 {
3a71aa26 8327 if (expecting_tls_get_addr == 2)
102890f0 8328 {
3a71aa26 8329 /* Check for toc tls entries. */
f961d9dd 8330 unsigned char *toc_tls;
3a71aa26
AM
8331 int retval;
8332
8333 retval = get_tls_mask (&toc_tls, NULL, NULL,
8334 &locsyms,
8335 rel, ibfd);
8336 if (retval == 0)
8337 goto err_free_rel;
663a1470
AM
8338 if (toc_tls != NULL)
8339 {
37da22e5
AM
8340 if ((*toc_tls & TLS_TLS) != 0
8341 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8342 found_tls_get_addr_arg = 1;
8343 if (retval > 1)
8344 toc_ref[toc_ref_index] = 1;
8345 }
102890f0 8346 }
3a71aa26 8347 continue;
102890f0
AM
8348 }
8349
102890f0
AM
8350 /* Uh oh, we didn't find the expected call. We
8351 could just mark this symbol to exclude it
8352 from tls optimization but it's safer to skip
663a1470 8353 the entire optimization. */
695344c0 8354 /* xgettext:c-format */
25f53a85 8355 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8356 "TLS optimization disabled\n"),
8357 ibfd, sec, rel->r_offset);
8358 ret = TRUE;
8359 goto err_free_rel;
102890f0
AM
8360 }
8361
37da22e5
AM
8362 /* If we don't have old-style __tls_get_addr calls
8363 without TLSGD/TLSLD marker relocs, and we haven't
8364 found a new-style __tls_get_addr call with a
8365 marker for this symbol, then we either have a
8366 broken object file or an -mlongcall style
8367 indirect call to __tls_get_addr without a marker.
8368 Disable optimization in this case. */
8369 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8370 && (tls_set & TLS_EXPLICIT) == 0
9737e8af 8371 && !sec->nomark_tls_get_addr
37da22e5
AM
8372 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8373 != (TLS_TLS | TLS_MARK)))
8374 continue;
8375
7d04a20a 8376 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
102890f0 8377 {
23cedd1d
AM
8378 struct plt_entry *ent = NULL;
8379
9e7028aa
AM
8380 if (htab->tls_get_addr_fd != NULL)
8381 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8382 ent != NULL;
8383 ent = ent->next)
8384 if (ent->addend == 0)
8385 break;
8386
8387 if (ent == NULL && htab->tga_desc_fd != NULL)
8388 for (ent = htab->tga_desc_fd->elf.plt.plist;
8389 ent != NULL;
8390 ent = ent->next)
8391 if (ent->addend == 0)
8392 break;
8393
8394 if (ent == NULL && htab->tls_get_addr != NULL)
23cedd1d
AM
8395 for (ent = htab->tls_get_addr->elf.plt.plist;
8396 ent != NULL;
8397 ent = ent->next)
8398 if (ent->addend == 0)
102890f0 8399 break;
411e1bfb 8400
9e7028aa
AM
8401 if (ent == NULL && htab->tga_desc != NULL)
8402 for (ent = htab->tga_desc->elf.plt.plist;
23cedd1d
AM
8403 ent != NULL;
8404 ent = ent->next)
8405 if (ent->addend == 0)
102890f0 8406 break;
23cedd1d
AM
8407
8408 if (ent != NULL
8409 && ent->plt.refcount > 0)
8410 ent->plt.refcount -= 1;
102890f0 8411 }
411e1bfb 8412
102890f0 8413 if (tls_clear == 0)
30038c59
AM
8414 continue;
8415
102890f0
AM
8416 if ((tls_set & TLS_EXPLICIT) == 0)
8417 {
8418 struct got_entry *ent;
411e1bfb 8419
102890f0
AM
8420 /* Adjust got entry for this reloc. */
8421 if (h != NULL)
8422 ent = h->got.glist;
8423 else
8424 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8425
102890f0
AM
8426 for (; ent != NULL; ent = ent->next)
8427 if (ent->addend == rel->r_addend
8428 && ent->owner == ibfd
8429 && ent->tls_type == tls_type)
8430 break;
8431 if (ent == NULL)
8432 abort ();
411e1bfb 8433
102890f0
AM
8434 if (tls_set == 0)
8435 {
8436 /* We managed to get rid of a got entry. */
8437 if (ent->got.refcount > 0)
8438 ent->got.refcount -= 1;
8439 }
8440 }
8441 else
8442 {
8443 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8444 we'll lose one or two dyn relocs. */
8445 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8446 NULL, h, sym))
102890f0 8447 return FALSE;
411e1bfb 8448
102890f0
AM
8449 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8450 {
8451 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8452 NULL, h, sym))
102890f0
AM
8453 return FALSE;
8454 }
8455 }
411e1bfb 8456
46e9995a 8457 *tls_mask |= tls_set & 0xff;
102890f0
AM
8458 *tls_mask &= ~tls_clear;
8459 }
8c1d1bb8 8460
102890f0
AM
8461 if (elf_section_data (sec)->relocs != relstart)
8462 free (relstart);
8463 }
411e1bfb 8464
663a1470
AM
8465 if (locsyms != NULL
8466 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8467 {
8468 if (!info->keep_memory)
8469 free (locsyms);
8470 else
8471 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8472 }
8473 }
411e1bfb 8474
c9594989 8475 free (toc_ref);
b34976b6 8476 return TRUE;
1e2f5b6e 8477}
b34976b6 8478
c5614fa4
AM
8479/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8480 the values of any global symbols in a toc section that has been
8481 edited. Globals in toc sections should be a rarity, so this function
8482 sets a flag if any are found in toc sections other than the one just
de194d85 8483 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8484
8485struct adjust_toc_info
8486{
8487 asection *toc;
8488 unsigned long *skip;
8489 bfd_boolean global_toc_syms;
8490};
8491
ba761f19
AM
8492enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8493
c5614fa4
AM
8494static bfd_boolean
8495adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8496{
8497 struct ppc_link_hash_entry *eh;
8498 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8499 unsigned long i;
c5614fa4 8500
c5614fa4
AM
8501 if (h->root.type != bfd_link_hash_defined
8502 && h->root.type != bfd_link_hash_defweak)
8503 return TRUE;
8504
ed7007c1 8505 eh = ppc_elf_hash_entry (h);
c5614fa4
AM
8506 if (eh->adjust_done)
8507 return TRUE;
8508
8509 if (eh->elf.root.u.def.section == toc_inf->toc)
8510 {
854b41e7
AM
8511 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8512 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8513 else
854b41e7
AM
8514 i = eh->elf.root.u.def.value >> 3;
8515
ba761f19 8516 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8517 {
4eca0228 8518 _bfd_error_handler
854b41e7
AM
8519 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8520 do
8521 ++i;
ba761f19 8522 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8523 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8524 }
854b41e7
AM
8525
8526 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8527 eh->adjust_done = 1;
8528 }
8529 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8530 toc_inf->global_toc_syms = TRUE;
8531
8532 return TRUE;
8533}
8534
39eeab25
AM
8535/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8536 on a _LO variety toc/got reloc. */
560c8763
AM
8537
8538static bfd_boolean
39eeab25 8539ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8540{
2365f8d7
AM
8541 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8542 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8543 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8544 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8545 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8546 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8547 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8548 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8549 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8550 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8551 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8552 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8553 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8554 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8555 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8556 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8557 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
39eeab25
AM
8558 /* Exclude lfqu by testing reloc. If relocs are ever
8559 defined for the reduced D field in psq_lu then those
8560 will need testing too. */
8561 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8562 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
39eeab25 8563 && (insn & 1) == 0)
2365f8d7
AM
8564 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8565 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
39eeab25
AM
8566 /* Exclude stfqu. psq_stu as above for psq_lu. */
8567 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8568 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
39eeab25 8569 && (insn & 1) == 0));
560c8763
AM
8570}
8571
4a421c53
AM
8572/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8573 pld ra,symbol@got@pcrel
dd9b12c2 8574 load/store rt,off(ra)
4a421c53 8575 or
d4b87b1e 8576 pla ra,symbol@pcrel
dd9b12c2 8577 load/store rt,off(ra)
4a421c53 8578 may be translated to
dd9b12c2 8579 pload/pstore rt,symbol+off@pcrel
4a421c53
AM
8580 nop.
8581 This function returns true if the optimization is possible, placing
dd9b12c2 8582 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
4a421c53
AM
8583
8584 On entry to this function, the linker has already determined that
d4b87b1e 8585 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8586 while *PINSN2 is the second instruction. */
8587
8588static bfd_boolean
dd9b12c2 8589xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
4a421c53 8590{
77486630
AM
8591 uint64_t insn1 = *pinsn1;
8592 uint64_t insn2 = *pinsn2;
dd9b12c2 8593 bfd_signed_vma off;
4a421c53 8594
77486630
AM
8595 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8596 {
8597 /* Check that regs match. */
8598 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8599 return FALSE;
8600
8601 /* P8LS or PMLS form, non-pcrel. */
8602 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8603 return FALSE;
8604
8605 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8606 *pinsn2 = PNOP;
8607 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8608 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8609 return TRUE;
8610 }
8611
8612 insn2 >>= 32;
8613
4a421c53 8614 /* Check that regs match. */
77486630 8615 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
4a421c53
AM
8616 return FALSE;
8617
8618 switch ((insn2 >> 26) & 63)
8619 {
8620 default:
8621 return FALSE;
8622
8623 case 32: /* lwz */
8624 case 34: /* lbz */
8625 case 36: /* stw */
8626 case 38: /* stb */
8627 case 40: /* lhz */
8628 case 42: /* lha */
8629 case 44: /* sth */
8630 case 48: /* lfs */
8631 case 50: /* lfd */
8632 case 52: /* stfs */
8633 case 54: /* stfd */
8634 /* These are the PMLS cases, where we just need to tack a prefix
dd9b12c2 8635 on the insn. */
77486630 8636 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
4a421c53 8637 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8638 off = insn2 & 0xffff;
4a421c53
AM
8639 break;
8640
8641 case 58: /* lwa, ld */
dd9b12c2 8642 if ((insn2 & 1) != 0)
4a421c53 8643 return FALSE;
77486630 8644 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8645 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8646 | (insn2 & (31ULL << 21)));
dd9b12c2 8647 off = insn2 & 0xfffc;
4a421c53
AM
8648 break;
8649
8650 case 57: /* lxsd, lxssp */
dd9b12c2 8651 if ((insn2 & 3) < 2)
4a421c53 8652 return FALSE;
77486630 8653 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8654 | ((40ULL | (insn2 & 3)) << 26)
8655 | (insn2 & (31ULL << 21)));
dd9b12c2 8656 off = insn2 & 0xfffc;
4a421c53
AM
8657 break;
8658
8659 case 61: /* stxsd, stxssp, lxv, stxv */
8660 if ((insn2 & 3) == 0)
8661 return FALSE;
8662 else if ((insn2 & 3) >= 2)
8663 {
77486630 8664 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8665 | ((44ULL | (insn2 & 3)) << 26)
8666 | (insn2 & (31ULL << 21)));
dd9b12c2 8667 off = insn2 & 0xfffc;
4a421c53
AM
8668 }
8669 else
8670 {
77486630 8671 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8672 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8673 | (insn2 & (31ULL << 21)));
dd9b12c2 8674 off = insn2 & 0xfff0;
4a421c53
AM
8675 }
8676 break;
8677
8678 case 56: /* lq */
77486630 8679 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53 8680 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8681 off = insn2 & 0xffff;
4a421c53
AM
8682 break;
8683
94ba9882
AM
8684 case 6: /* lxvp, stxvp */
8685 if ((insn2 & 0xe) != 0)
8686 return FALSE;
8687 insn1 = ((1ULL << 58) | (1ULL << 52)
8688 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8689 | (insn2 & (31ULL << 21)));
8690 off = insn2 & 0xfff0;
8691 break;
8692
4a421c53 8693 case 62: /* std, stq */
dd9b12c2 8694 if ((insn2 & 1) != 0)
4a421c53 8695 return FALSE;
77486630 8696 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8697 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8698 | (insn2 & (31ULL << 21)));
dd9b12c2 8699 off = insn2 & 0xfffc;
4a421c53
AM
8700 break;
8701 }
8702
77486630 8703 *pinsn1 = insn1;
4a421c53 8704 *pinsn2 = (uint64_t) NOP << 32;
dd9b12c2 8705 *poff = (off ^ 0x8000) - 0x8000;
4a421c53
AM
8706 return TRUE;
8707}
8708
c5614fa4
AM
8709/* Examine all relocs referencing .toc sections in order to remove
8710 unused .toc entries. */
8711
8712bfd_boolean
33c0ec9d 8713ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8714{
8715 bfd *ibfd;
8716 struct adjust_toc_info toc_inf;
67f0cbdb 8717 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8718
67f0cbdb 8719 htab->do_toc_opt = 1;
c5614fa4 8720 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8721 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8722 {
8723 asection *toc, *sec;
8724 Elf_Internal_Shdr *symtab_hdr;
8725 Elf_Internal_Sym *local_syms;
425b145b 8726 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8727 unsigned long *skip, *drop;
8728 unsigned char *used;
8729 unsigned char *keep, last, some_unused;
8730
854b41e7
AM
8731 if (!is_ppc64_elf (ibfd))
8732 continue;
8733
c5614fa4
AM
8734 toc = bfd_get_section_by_name (ibfd, ".toc");
8735 if (toc == NULL
92b7a70f 8736 || toc->size == 0
dbaa2011
AM
8737 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8738 || discarded_section (toc))
c5614fa4
AM
8739 continue;
8740
425b145b 8741 toc_relocs = NULL;
c5614fa4 8742 local_syms = NULL;
0ffa91dd 8743 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8744
8745 /* Look at sections dropped from the final link. */
8746 skip = NULL;
8747 relstart = NULL;
8748 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8749 {
8750 if (sec->reloc_count == 0
dbaa2011 8751 || !discarded_section (sec)
c5614fa4
AM
8752 || get_opd_info (sec)
8753 || (sec->flags & SEC_ALLOC) == 0
8754 || (sec->flags & SEC_DEBUGGING) != 0)
8755 continue;
8756
8757 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8758 if (relstart == NULL)
8759 goto error_ret;
8760
8761 /* Run through the relocs to see which toc entries might be
8762 unused. */
8763 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8764 {
8765 enum elf_ppc64_reloc_type r_type;
8766 unsigned long r_symndx;
8767 asection *sym_sec;
8768 struct elf_link_hash_entry *h;
8769 Elf_Internal_Sym *sym;
8770 bfd_vma val;
8771
8772 r_type = ELF64_R_TYPE (rel->r_info);
8773 switch (r_type)
8774 {
8775 default:
8776 continue;
8777
8778 case R_PPC64_TOC16:
8779 case R_PPC64_TOC16_LO:
8780 case R_PPC64_TOC16_HI:
8781 case R_PPC64_TOC16_HA:
8782 case R_PPC64_TOC16_DS:
8783 case R_PPC64_TOC16_LO_DS:
8784 break;
8785 }
8786
8787 r_symndx = ELF64_R_SYM (rel->r_info);
8788 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8789 r_symndx, ibfd))
8790 goto error_ret;
8791
8792 if (sym_sec != toc)
8793 continue;
8794
8795 if (h != NULL)
8796 val = h->root.u.def.value;
8797 else
8798 val = sym->st_value;
8799 val += rel->r_addend;
8800
8801 if (val >= toc->size)
8802 continue;
8803
8804 /* Anything in the toc ought to be aligned to 8 bytes.
8805 If not, don't mark as unused. */
8806 if (val & 7)
8807 continue;
8808
8809 if (skip == NULL)
8810 {
854b41e7 8811 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8812 if (skip == NULL)
8813 goto error_ret;
8814 }
8815
ba761f19 8816 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8817 }
8818
8819 if (elf_section_data (sec)->relocs != relstart)
8820 free (relstart);
8821 }
8822
ba761f19
AM
8823 /* For largetoc loads of address constants, we can convert
8824 . addis rx,2,addr@got@ha
8825 . ld ry,addr@got@l(rx)
8826 to
8827 . addis rx,2,addr@toc@ha
8828 . addi ry,rx,addr@toc@l
8829 when addr is within 2G of the toc pointer. This then means
8830 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8831
ba761f19
AM
8832 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8833 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8834 && toc->reloc_count != 0)
8835 {
8836 /* Read toc relocs. */
425b145b
AM
8837 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8838 info->keep_memory);
8839 if (toc_relocs == NULL)
ba761f19
AM
8840 goto error_ret;
8841
425b145b 8842 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8843 {
8844 enum elf_ppc64_reloc_type r_type;
8845 unsigned long r_symndx;
8846 asection *sym_sec;
8847 struct elf_link_hash_entry *h;
8848 Elf_Internal_Sym *sym;
8849 bfd_vma val, addr;
8850
8851 r_type = ELF64_R_TYPE (rel->r_info);
8852 if (r_type != R_PPC64_ADDR64)
8853 continue;
8854
8855 r_symndx = ELF64_R_SYM (rel->r_info);
8856 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8857 r_symndx, ibfd))
8858 goto error_ret;
8859
425b145b 8860 if (sym_sec == NULL
c27b8c2a 8861 || sym_sec->output_section == NULL
dbaa2011 8862 || discarded_section (sym_sec))
425b145b
AM
8863 continue;
8864
afe397ea 8865 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8866 continue;
8867
8868 if (h != NULL)
bddc25c9
AM
8869 {
8870 if (h->type == STT_GNU_IFUNC)
8871 continue;
8872 val = h->root.u.def.value;
8873 }
ba761f19 8874 else
bddc25c9
AM
8875 {
8876 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8877 continue;
8878 val = sym->st_value;
8879 }
ba761f19
AM
8880 val += rel->r_addend;
8881 val += sym_sec->output_section->vma + sym_sec->output_offset;
8882
8883 /* We don't yet know the exact toc pointer value, but we
8884 know it will be somewhere in the toc section. Don't
8885 optimize if the difference from any possible toc
8886 pointer is outside [ff..f80008000, 7fff7fff]. */
8887 addr = toc->output_section->vma + TOC_BASE_OFF;
8888 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8889 continue;
8890
8891 addr = toc->output_section->vma + toc->output_section->rawsize;
8892 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8893 continue;
8894
8895 if (skip == NULL)
8896 {
8897 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8898 if (skip == NULL)
8899 goto error_ret;
8900 }
8901
8902 skip[rel->r_offset >> 3]
425b145b 8903 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8904 }
ba761f19
AM
8905 }
8906
c5614fa4
AM
8907 if (skip == NULL)
8908 continue;
8909
8910 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8911 if (used == NULL)
8912 {
8913 error_ret:
c9594989 8914 if (symtab_hdr->contents != (unsigned char *) local_syms)
c5614fa4
AM
8915 free (local_syms);
8916 if (sec != NULL
c5614fa4
AM
8917 && elf_section_data (sec)->relocs != relstart)
8918 free (relstart);
c9594989 8919 if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 8920 free (toc_relocs);
c9594989 8921 free (skip);
c5614fa4
AM
8922 return FALSE;
8923 }
8924
30038c59
AM
8925 /* Now check all kept sections that might reference the toc.
8926 Check the toc itself last. */
8927 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8928 : ibfd->sections);
c5614fa4 8929 sec != NULL;
c5614fa4 8930 sec = (sec == toc ? NULL
c5614fa4 8931 : sec->next == NULL ? toc
30038c59 8932 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8933 : sec->next))
8934 {
8935 int repeat;
8936
8937 if (sec->reloc_count == 0
dbaa2011 8938 || discarded_section (sec)
c5614fa4
AM
8939 || get_opd_info (sec)
8940 || (sec->flags & SEC_ALLOC) == 0
8941 || (sec->flags & SEC_DEBUGGING) != 0)
8942 continue;
8943
854b41e7
AM
8944 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8945 info->keep_memory);
c5614fa4 8946 if (relstart == NULL)
2915c55b
JK
8947 {
8948 free (used);
8949 goto error_ret;
8950 }
c5614fa4
AM
8951
8952 /* Mark toc entries referenced as used. */
c5614fa4 8953 do
d4f1ee75
AM
8954 {
8955 repeat = 0;
8956 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8957 {
8958 enum elf_ppc64_reloc_type r_type;
8959 unsigned long r_symndx;
8960 asection *sym_sec;
8961 struct elf_link_hash_entry *h;
8962 Elf_Internal_Sym *sym;
8963 bfd_vma val;
98528052 8964
d4f1ee75 8965 r_type = ELF64_R_TYPE (rel->r_info);
d4f1ee75
AM
8966 switch (r_type)
8967 {
8968 case R_PPC64_TOC16:
8969 case R_PPC64_TOC16_LO:
8970 case R_PPC64_TOC16_HI:
8971 case R_PPC64_TOC16_HA:
8972 case R_PPC64_TOC16_DS:
8973 case R_PPC64_TOC16_LO_DS:
8974 /* In case we're taking addresses of toc entries. */
8975 case R_PPC64_ADDR64:
8976 break;
c5614fa4 8977
d4f1ee75
AM
8978 default:
8979 continue;
8980 }
c5614fa4 8981
d4f1ee75
AM
8982 r_symndx = ELF64_R_SYM (rel->r_info);
8983 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8984 r_symndx, ibfd))
8985 {
8986 free (used);
8987 goto error_ret;
8988 }
c5614fa4 8989
d4f1ee75
AM
8990 if (sym_sec != toc)
8991 continue;
c5614fa4 8992
d4f1ee75
AM
8993 if (h != NULL)
8994 val = h->root.u.def.value;
8995 else
8996 val = sym->st_value;
8997 val += rel->r_addend;
ba761f19 8998
d4f1ee75
AM
8999 if (val >= toc->size)
9000 continue;
ba761f19 9001
d4f1ee75
AM
9002 if ((skip[val >> 3] & can_optimize) != 0)
9003 {
9004 bfd_vma off;
9005 unsigned char opc;
9006
9007 switch (r_type)
9008 {
9009 case R_PPC64_TOC16_HA:
ba761f19 9010 break;
ba761f19 9011
d4f1ee75
AM
9012 case R_PPC64_TOC16_LO_DS:
9013 off = rel->r_offset;
9014 off += (bfd_big_endian (ibfd) ? -2 : 3);
9015 if (!bfd_get_section_contents (ibfd, sec, &opc,
9016 off, 1))
9017 {
9018 free (used);
9019 goto error_ret;
9020 }
9021 if ((opc & (0x3f << 2)) == (58u << 2))
9022 break;
1a0670f3 9023 /* Fall through. */
ba761f19 9024
d4f1ee75
AM
9025 default:
9026 /* Wrong sort of reloc, or not a ld. We may
9027 as well clear ref_from_discarded too. */
9028 skip[val >> 3] = 0;
9029 }
9030 }
9031
9032 if (sec != toc)
9033 used[val >> 3] = 1;
9034 /* For the toc section, we only mark as used if this
9035 entry itself isn't unused. */
9036 else if ((used[rel->r_offset >> 3]
9037 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9038 && !used[val >> 3])
9039 {
9040 /* Do all the relocs again, to catch reference
9041 chains. */
9042 repeat = 1;
9043 used[val >> 3] = 1;
9044 }
9045 }
9046 }
c5614fa4 9047 while (repeat);
854b41e7
AM
9048
9049 if (elf_section_data (sec)->relocs != relstart)
9050 free (relstart);
c5614fa4
AM
9051 }
9052
9053 /* Merge the used and skip arrays. Assume that TOC
9054 doublewords not appearing as either used or unused belong
de194d85 9055 to an entry more than one doubleword in size. */
c5614fa4
AM
9056 for (drop = skip, keep = used, last = 0, some_unused = 0;
9057 drop < skip + (toc->size + 7) / 8;
9058 ++drop, ++keep)
9059 {
9060 if (*keep)
9061 {
ba761f19
AM
9062 *drop &= ~ref_from_discarded;
9063 if ((*drop & can_optimize) != 0)
9064 some_unused = 1;
c5614fa4
AM
9065 last = 0;
9066 }
b140b010 9067 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9068 {
9069 some_unused = 1;
ba761f19 9070 last = ref_from_discarded;
c5614fa4
AM
9071 }
9072 else
9073 *drop = last;
9074 }
9075
9076 free (used);
9077
9078 if (some_unused)
9079 {
9080 bfd_byte *contents, *src;
9081 unsigned long off;
d62b3684 9082 Elf_Internal_Sym *sym;
ba761f19 9083 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9084
9085 /* Shuffle the toc contents, and at the same time convert the
9086 skip array from booleans into offsets. */
9087 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9088 goto error_ret;
9089
9090 elf_section_data (toc)->this_hdr.contents = contents;
9091
9092 for (src = contents, off = 0, drop = skip;
9093 src < contents + toc->size;
9094 src += 8, ++drop)
9095 {
ba761f19
AM
9096 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9097 off += 8;
c5614fa4
AM
9098 else if (off != 0)
9099 {
9100 *drop = off;
9101 memcpy (src - off, src, 8);
9102 }
9103 }
854b41e7 9104 *drop = off;
c5614fa4
AM
9105 toc->rawsize = toc->size;
9106 toc->size = src - contents - off;
9107
ba761f19
AM
9108 /* Adjust addends for relocs against the toc section sym,
9109 and optimize any accesses we can. */
c5614fa4
AM
9110 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9111 {
9112 if (sec->reloc_count == 0
dbaa2011 9113 || discarded_section (sec))
c5614fa4
AM
9114 continue;
9115
9116 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9117 info->keep_memory);
c5614fa4
AM
9118 if (relstart == NULL)
9119 goto error_ret;
9120
9121 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9122 {
9123 enum elf_ppc64_reloc_type r_type;
9124 unsigned long r_symndx;
9125 asection *sym_sec;
9126 struct elf_link_hash_entry *h;
854b41e7 9127 bfd_vma val;
c5614fa4
AM
9128
9129 r_type = ELF64_R_TYPE (rel->r_info);
9130 switch (r_type)
9131 {
9132 default:
9133 continue;
9134
9135 case R_PPC64_TOC16:
9136 case R_PPC64_TOC16_LO:
9137 case R_PPC64_TOC16_HI:
9138 case R_PPC64_TOC16_HA:
9139 case R_PPC64_TOC16_DS:
9140 case R_PPC64_TOC16_LO_DS:
9141 case R_PPC64_ADDR64:
9142 break;
9143 }
9144
9145 r_symndx = ELF64_R_SYM (rel->r_info);
9146 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9147 r_symndx, ibfd))
9148 goto error_ret;
9149
ba761f19 9150 if (sym_sec != toc)
c5614fa4
AM
9151 continue;
9152
ba761f19
AM
9153 if (h != NULL)
9154 val = h->root.u.def.value;
9155 else
9156 {
9157 val = sym->st_value;
9158 if (val != 0)
9159 local_toc_syms = TRUE;
9160 }
9161
9162 val += rel->r_addend;
854b41e7
AM
9163
9164 if (val > toc->rawsize)
9165 val = toc->rawsize;
ba761f19
AM
9166 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9167 continue;
9168 else if ((skip[val >> 3] & can_optimize) != 0)
9169 {
9170 Elf_Internal_Rela *tocrel
425b145b 9171 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9172 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9173
9174 switch (r_type)
9175 {
9176 case R_PPC64_TOC16_HA:
9177 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9178 break;
9179
9180 case R_PPC64_TOC16_LO_DS:
9181 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9182 break;
9183
9184 default:
28942f62
AM
9185 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9186 ppc_howto_init ();
b140b010 9187 info->callbacks->einfo
695344c0 9188 /* xgettext:c-format */
174d0a74 9189 (_("%H: %s references "
b140b010
AM
9190 "optimized away TOC entry\n"),
9191 ibfd, sec, rel->r_offset,
9192 ppc64_elf_howto_table[r_type]->name);
9193 bfd_set_error (bfd_error_bad_value);
9194 goto error_ret;
ba761f19
AM
9195 }
9196 rel->r_addend = tocrel->r_addend;
9197 elf_section_data (sec)->relocs = relstart;
9198 continue;
9199 }
9200
9201 if (h != NULL || sym->st_value != 0)
9202 continue;
854b41e7
AM
9203
9204 rel->r_addend -= skip[val >> 3];
9205 elf_section_data (sec)->relocs = relstart;
c5614fa4 9206 }
854b41e7
AM
9207
9208 if (elf_section_data (sec)->relocs != relstart)
9209 free (relstart);
c5614fa4
AM
9210 }
9211
9212 /* We shouldn't have local or global symbols defined in the TOC,
9213 but handle them anyway. */
df22d223
AM
9214 if (local_syms != NULL)
9215 for (sym = local_syms;
9216 sym < local_syms + symtab_hdr->sh_info;
9217 ++sym)
9218 if (sym->st_value != 0
9219 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9220 {
9221 unsigned long i;
854b41e7 9222
df22d223
AM
9223 if (sym->st_value > toc->rawsize)
9224 i = toc->rawsize >> 3;
9225 else
9226 i = sym->st_value >> 3;
854b41e7 9227
df22d223
AM
9228 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9229 {
9230 if (local_toc_syms)
4eca0228 9231 _bfd_error_handler
df22d223
AM
9232 (_("%s defined on removed toc entry"),
9233 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9234 do
9235 ++i;
9236 while ((skip[i] & (ref_from_discarded | can_optimize)));
9237 sym->st_value = (bfd_vma) i << 3;
9238 }
d62b3684 9239
df22d223
AM
9240 sym->st_value -= skip[i];
9241 symtab_hdr->contents = (unsigned char *) local_syms;
9242 }
c5614fa4 9243
854b41e7 9244 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9245 if (toc_inf.global_toc_syms)
9246 {
9247 toc_inf.toc = toc;
9248 toc_inf.skip = skip;
9249 toc_inf.global_toc_syms = FALSE;
9250 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9251 &toc_inf);
9252 }
854b41e7
AM
9253
9254 if (toc->reloc_count != 0)
9255 {
d4730f92 9256 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9257 Elf_Internal_Rela *wrel;
9258 bfd_size_type sz;
9259
854b41e7 9260 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9261 if (toc_relocs == NULL)
9262 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9263 info->keep_memory);
9264 if (toc_relocs == NULL)
9265 goto error_ret;
9266
425b145b
AM
9267 wrel = toc_relocs;
9268 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9269 if ((skip[rel->r_offset >> 3]
9270 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9271 {
9272 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9273 wrel->r_info = rel->r_info;
9274 wrel->r_addend = rel->r_addend;
9275 ++wrel;
9276 }
9277 else if (!dec_dynrel_count (rel->r_info, toc, info,
9278 &local_syms, NULL, NULL))
9279 goto error_ret;
9280
425b145b
AM
9281 elf_section_data (toc)->relocs = toc_relocs;
9282 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9283 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9284 sz = rel_hdr->sh_entsize;
9285 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9286 }
c5614fa4 9287 }
c9594989 9288 else if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 9289 free (toc_relocs);
c5614fa4
AM
9290
9291 if (local_syms != NULL
9292 && symtab_hdr->contents != (unsigned char *) local_syms)
9293 {
9294 if (!info->keep_memory)
9295 free (local_syms);
9296 else
9297 symtab_hdr->contents = (unsigned char *) local_syms;
9298 }
9299 free (skip);
9300 }
9301
066f4018 9302 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
9303 a GOT relative or PC relative access, possibly reducing the
9304 number of GOT entries. */
066f4018
AM
9305 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9306 {
9307 asection *sec;
9308 Elf_Internal_Shdr *symtab_hdr;
9309 Elf_Internal_Sym *local_syms;
9310 Elf_Internal_Rela *relstart, *rel;
9311 bfd_vma got;
9312
9313 if (!is_ppc64_elf (ibfd))
9314 continue;
9315
903b777d 9316 if (!ppc64_elf_tdata (ibfd)->has_optrel)
066f4018
AM
9317 continue;
9318
9319 sec = ppc64_elf_tdata (ibfd)->got;
903b777d
AM
9320 got = 0;
9321 if (sec != NULL)
9322 got = sec->output_section->vma + sec->output_offset + 0x8000;
066f4018
AM
9323
9324 local_syms = NULL;
9325 symtab_hdr = &elf_symtab_hdr (ibfd);
9326
9327 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9328 {
9329 if (sec->reloc_count == 0
903b777d 9330 || !ppc64_elf_section_data (sec)->has_optrel
066f4018
AM
9331 || discarded_section (sec))
9332 continue;
9333
9334 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9335 info->keep_memory);
9336 if (relstart == NULL)
9337 {
9338 got_error_ret:
c9594989 9339 if (symtab_hdr->contents != (unsigned char *) local_syms)
066f4018
AM
9340 free (local_syms);
9341 if (sec != NULL
066f4018
AM
9342 && elf_section_data (sec)->relocs != relstart)
9343 free (relstart);
9344 return FALSE;
9345 }
9346
9347 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9348 {
9349 enum elf_ppc64_reloc_type r_type;
9350 unsigned long r_symndx;
9351 Elf_Internal_Sym *sym;
9352 asection *sym_sec;
9353 struct elf_link_hash_entry *h;
9354 struct got_entry *ent;
133a1f60 9355 bfd_vma val, pc;
4a421c53 9356 unsigned char buf[8];
066f4018 9357 unsigned int insn;
903b777d 9358 enum {no_check, check_lo, check_ha} insn_check;
066f4018
AM
9359
9360 r_type = ELF64_R_TYPE (rel->r_info);
903b777d
AM
9361 switch (r_type)
9362 {
9363 default:
9364 insn_check = no_check;
9365 break;
9366
9367 case R_PPC64_PLT16_HA:
9368 case R_PPC64_GOT_TLSLD16_HA:
9369 case R_PPC64_GOT_TLSGD16_HA:
9370 case R_PPC64_GOT_TPREL16_HA:
9371 case R_PPC64_GOT_DTPREL16_HA:
9372 case R_PPC64_GOT16_HA:
9373 case R_PPC64_TOC16_HA:
9374 insn_check = check_ha;
9375 break;
9376
9377 case R_PPC64_PLT16_LO:
9378 case R_PPC64_PLT16_LO_DS:
9379 case R_PPC64_GOT_TLSLD16_LO:
9380 case R_PPC64_GOT_TLSGD16_LO:
9381 case R_PPC64_GOT_TPREL16_LO_DS:
9382 case R_PPC64_GOT_DTPREL16_LO_DS:
9383 case R_PPC64_GOT16_LO:
9384 case R_PPC64_GOT16_LO_DS:
9385 case R_PPC64_TOC16_LO:
9386 case R_PPC64_TOC16_LO_DS:
9387 insn_check = check_lo;
9388 break;
9389 }
9390
9391 if (insn_check != no_check)
9392 {
9393 bfd_vma off = rel->r_offset & ~3;
9394
9395 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9396 goto got_error_ret;
9397
9398 insn = bfd_get_32 (ibfd, buf);
9399 if (insn_check == check_lo
9400 ? !ok_lo_toc_insn (insn, r_type)
2365f8d7 9401 : ((insn & ((0x3fu << 26) | 0x1f << 16))
903b777d
AM
9402 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9403 {
9404 char str[12];
9405
9406 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9407 sprintf (str, "%#08x", insn);
9408 info->callbacks->einfo
9409 /* xgettext:c-format */
9410 (_("%H: got/toc optimization is not supported for"
9411 " %s instruction\n"),
9412 ibfd, sec, rel->r_offset & ~3, str);
9413 continue;
9414 }
9415 }
9416
066f4018
AM
9417 switch (r_type)
9418 {
bb22a418
AM
9419 /* Note that we don't delete GOT entries for
9420 R_PPC64_GOT16_DS since we'd need a lot more
9421 analysis. For starters, the preliminary layout is
9422 before the GOT, PLT, dynamic sections and stubs are
9423 laid out. Then we'd need to allow for changes in
9424 distance between sections caused by alignment. */
066f4018
AM
9425 default:
9426 continue;
9427
066f4018
AM
9428 case R_PPC64_GOT16_HA:
9429 case R_PPC64_GOT16_LO_DS:
4a421c53 9430 case R_PPC64_GOT_PCREL34:
066f4018
AM
9431 break;
9432 }
9433
9434 r_symndx = ELF64_R_SYM (rel->r_info);
9435 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9436 r_symndx, ibfd))
9437 goto got_error_ret;
9438
6d5554a6
AM
9439 if (sym_sec == NULL
9440 || sym_sec->output_section == NULL
9441 || discarded_section (sym_sec))
9442 continue;
9443
06507dab
AM
9444 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9445 continue;
9446
066f4018
AM
9447 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9448 continue;
9449
9450 if (h != NULL)
9451 val = h->root.u.def.value;
9452 else
9453 val = sym->st_value;
133a1f60 9454 val += rel->r_addend;
066f4018
AM
9455 val += sym_sec->output_section->vma + sym_sec->output_offset;
9456
bb22a418
AM
9457/* Fudge factor to allow for the fact that the preliminary layout
9458 isn't exact. Reduce limits by this factor. */
9459#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9460
066f4018
AM
9461 switch (r_type)
9462 {
9463 default:
9464 continue;
9465
066f4018 9466 case R_PPC64_GOT16_HA:
bb22a418
AM
9467 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9468 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9469 continue;
9470
9471 if (!bfd_get_section_contents (ibfd, sec, buf,
9472 rel->r_offset & ~3, 4))
9473 goto got_error_ret;
9474 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9475 if (((insn & ((0x3fu << 26) | 0x1f << 16))
066f4018
AM
9476 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9477 continue;
9478 break;
9479
9480 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9481 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9482 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9483 continue;
9484 if (!bfd_get_section_contents (ibfd, sec, buf,
9485 rel->r_offset & ~3, 4))
9486 goto got_error_ret;
9487 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9488 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
066f4018
AM
9489 continue;
9490 break;
4a421c53
AM
9491
9492 case R_PPC64_GOT_PCREL34:
9493 pc = rel->r_offset;
9494 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9495 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9496 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9497 continue;
9498 if (!bfd_get_section_contents (ibfd, sec, buf,
9499 rel->r_offset & ~3, 8))
9500 goto got_error_ret;
9501 insn = bfd_get_32 (ibfd, buf);
9502 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9503 continue;
9504 insn = bfd_get_32 (ibfd, buf + 4);
2365f8d7 9505 if ((insn & (0x3fu << 26)) != 57u << 26)
4a421c53
AM
9506 continue;
9507 break;
066f4018 9508 }
bb22a418 9509#undef LIMIT_ADJUST
066f4018
AM
9510
9511 if (h != NULL)
9512 ent = h->got.glist;
9513 else
9514 {
9515 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9516 ent = local_got_ents[r_symndx];
9517 }
9518 for (; ent != NULL; ent = ent->next)
133a1f60 9519 if (ent->addend == rel->r_addend
066f4018
AM
9520 && ent->owner == ibfd
9521 && ent->tls_type == 0)
9522 break;
9523 BFD_ASSERT (ent && ent->got.refcount > 0);
9524 ent->got.refcount -= 1;
9525 }
9526
9527 if (elf_section_data (sec)->relocs != relstart)
9528 free (relstart);
9529 }
9530
9531 if (local_syms != NULL
9532 && symtab_hdr->contents != (unsigned char *) local_syms)
9533 {
9534 if (!info->keep_memory)
9535 free (local_syms);
9536 else
9537 symtab_hdr->contents = (unsigned char *) local_syms;
9538 }
9539 }
9540
c5614fa4
AM
9541 return TRUE;
9542}
9543
1bbe0902
AM
9544/* Return true iff input section I references the TOC using
9545 instructions limited to +/-32k offsets. */
9546
9547bfd_boolean
9548ppc64_elf_has_small_toc_reloc (asection *i)
9549{
9550 return (is_ppc64_elf (i->owner)
9551 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9552}
9553
927be08e
AM
9554/* Allocate space for one GOT entry. */
9555
9556static void
9557allocate_got (struct elf_link_hash_entry *h,
9558 struct bfd_link_info *info,
9559 struct got_entry *gent)
9560{
9561 struct ppc_link_hash_table *htab = ppc_hash_table (info);
ed7007c1 9562 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
927be08e
AM
9563 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9564 ? 16 : 8);
9565 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9566 ? 2 : 1) * sizeof (Elf64_External_Rela);
9567 asection *got = ppc64_elf_tdata (gent->owner)->got;
9568
9569 gent->got.offset = got->size;
9570 got->size += entsize;
9571
19e08130 9572 if (h->type == STT_GNU_IFUNC)
927be08e 9573 {
33e44f2e 9574 htab->elf.irelplt->size += rentsize;
19e08130 9575 htab->got_reli_size += rentsize;
927be08e 9576 }
f15d0b54 9577 else if (((bfd_link_pic (info)
f749f26e 9578 && !(gent->tls_type != 0
f15d0b54
AM
9579 && bfd_link_executable (info)
9580 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9581 || (htab->elf.dynamic_sections_created
9582 && h->dynindx != -1
9583 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9584 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9585 {
19e08130 9586 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9587 relgot->size += rentsize;
927be08e
AM
9588 }
9589}
9590
7865406b
AM
9591/* This function merges got entries in the same toc group. */
9592
9593static void
9594merge_got_entries (struct got_entry **pent)
9595{
9596 struct got_entry *ent, *ent2;
9597
9598 for (ent = *pent; ent != NULL; ent = ent->next)
9599 if (!ent->is_indirect)
9600 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9601 if (!ent2->is_indirect
9602 && ent2->addend == ent->addend
9603 && ent2->tls_type == ent->tls_type
9604 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9605 {
9606 ent2->is_indirect = TRUE;
9607 ent2->got.ent = ent;
9608 }
9609}
9610
46434633 9611/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9612
9613static bfd_boolean
46434633
AM
9614ensure_undef_dynamic (struct bfd_link_info *info,
9615 struct elf_link_hash_entry *h)
f0158f44
AM
9616{
9617 struct elf_link_hash_table *htab = elf_hash_table (info);
9618
9619 if (htab->dynamic_sections_created
46434633
AM
9620 && ((info->dynamic_undefined_weak != 0
9621 && h->root.type == bfd_link_hash_undefweak)
9622 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9623 && h->dynindx == -1
9624 && !h->forced_local
9625 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9626 return bfd_elf_link_record_dynamic_symbol (info, h);
9627 return TRUE;
9628}
9629
65f38f15
AM
9630/* Allocate space in .plt, .got and associated reloc sections for
9631 dynamic relocs. */
5bd4f169 9632
b34976b6 9633static bfd_boolean
4ce794b7 9634allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9635{
65f38f15
AM
9636 struct bfd_link_info *info;
9637 struct ppc_link_hash_table *htab;
5bd4f169 9638 asection *s;
65f38f15 9639 struct ppc_link_hash_entry *eh;
0b8bcf0d 9640 struct got_entry **pgent, *gent;
5bd4f169 9641
e92d460e 9642 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9643 return TRUE;
5bd4f169 9644
65f38f15
AM
9645 info = (struct bfd_link_info *) inf;
9646 htab = ppc_hash_table (info);
4dfe6ac6
NC
9647 if (htab == NULL)
9648 return FALSE;
5bd4f169 9649
ed7007c1 9650 eh = ppc_elf_hash_entry (h);
951fd09b
AM
9651 /* Run through the TLS GD got entries first if we're changing them
9652 to TPREL. */
b00a0a86 9653 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
951fd09b
AM
9654 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9655 if (gent->got.refcount > 0
9656 && (gent->tls_type & TLS_GD) != 0)
9657 {
9658 /* This was a GD entry that has been converted to TPREL. If
9659 there happens to be a TPREL entry we can use that one. */
9660 struct got_entry *ent;
9661 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9662 if (ent->got.refcount > 0
9663 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9664 && ent->addend == gent->addend
9665 && ent->owner == gent->owner)
951fd09b
AM
9666 {
9667 gent->got.refcount = 0;
9668 break;
9669 }
9670
9671 /* If not, then we'll be using our own TPREL entry. */
9672 if (gent->got.refcount != 0)
9673 gent->tls_type = TLS_TLS | TLS_TPREL;
9674 }
9675
7865406b
AM
9676 /* Remove any list entry that won't generate a word in the GOT before
9677 we call merge_got_entries. Otherwise we risk merging to empty
9678 entries. */
0b8bcf0d
AM
9679 pgent = &h->got.glist;
9680 while ((gent = *pgent) != NULL)
411e1bfb 9681 if (gent->got.refcount > 0)
7865406b
AM
9682 {
9683 if ((gent->tls_type & TLS_LD) != 0
f749f26e 9684 && SYMBOL_REFERENCES_LOCAL (info, h))
7865406b
AM
9685 {
9686 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9687 *pgent = gent->next;
9688 }
9689 else
9690 pgent = &gent->next;
9691 }
9692 else
9693 *pgent = gent->next;
9694
9695 if (!htab->do_multi_toc)
9696 merge_got_entries (&h->got.glist);
9697
9698 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9699 if (!gent->is_indirect)
411e1bfb 9700 {
ec73ddcd
AM
9701 /* Ensure we catch all the cases where this symbol should
9702 be made dynamic. */
46434633 9703 if (!ensure_undef_dynamic (info, h))
f0158f44 9704 return FALSE;
65f38f15 9705
0c8d6e5c 9706 if (!is_ppc64_elf (gent->owner))
927be08e 9707 abort ();
0ffa91dd 9708
927be08e 9709 allocate_got (h, info, gent);
411e1bfb 9710 }
65f38f15 9711
954b63d4
AM
9712 /* If no dynamic sections we can't have dynamic relocs, except for
9713 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9714 if (!htab->elf.dynamic_sections_created
9715 && h->type != STT_GNU_IFUNC)
190eb1dd 9716 h->dyn_relocs = NULL;
8a2058b5 9717
529fe20e
AM
9718 /* Discard relocs on undefined symbols that must be local. */
9719 else if (h->root.type == bfd_link_hash_undefined
9720 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
190eb1dd 9721 h->dyn_relocs = NULL;
529fe20e 9722
954b63d4
AM
9723 /* Also discard relocs on undefined weak syms with non-default
9724 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9725 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 9726 h->dyn_relocs = NULL;
954b63d4 9727
190eb1dd 9728 if (h->dyn_relocs != NULL)
65f38f15 9729 {
8a2058b5
AM
9730 struct elf_dyn_relocs *p, **pp;
9731
57e7d118
AM
9732 /* In the shared -Bsymbolic case, discard space allocated for
9733 dynamic pc-relative relocs against symbols which turn out to
9734 be defined in regular objects. For the normal shared case,
9735 discard space for relocs that have become local due to symbol
9736 visibility changes. */
57e7d118 9737 if (bfd_link_pic (info))
65f38f15 9738 {
57e7d118
AM
9739 /* Relocs that use pc_count are those that appear on a call
9740 insn, or certain REL relocs (see must_be_dyn_reloc) that
9741 can be generated via assembly. We want calls to
9742 protected symbols to resolve directly to the function
9743 rather than going via the plt. If people want function
9744 pointer comparisons to work as expected then they should
9745 avoid writing weird assembly. */
9746 if (SYMBOL_CALLS_LOCAL (info, h))
9747 {
190eb1dd 9748 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
57e7d118
AM
9749 {
9750 p->count -= p->pc_count;
9751 p->pc_count = 0;
9752 if (p->count == 0)
9753 *pp = p->next;
9754 else
9755 pp = &p->next;
9756 }
9757 }
65f38f15 9758
190eb1dd 9759 if (h->dyn_relocs != NULL)
5bd4f169 9760 {
ec73ddcd
AM
9761 /* Ensure we catch all the cases where this symbol
9762 should be made dynamic. */
46434633 9763 if (!ensure_undef_dynamic (info, h))
f0158f44 9764 return FALSE;
5bd4f169 9765 }
65f38f15 9766 }
ec73ddcd
AM
9767
9768 /* For a fixed position executable, discard space for
9769 relocs against symbols which are not dynamic. */
9770 else if (h->type != STT_GNU_IFUNC)
57e7d118 9771 {
529fe20e
AM
9772 if (h->dynamic_adjusted
9773 && !h->def_regular
9774 && !ELF_COMMON_DEF_P (h))
f0158f44 9775 {
ec73ddcd
AM
9776 /* Ensure we catch all the cases where this symbol
9777 should be made dynamic. */
46434633 9778 if (!ensure_undef_dynamic (info, h))
f0158f44 9779 return FALSE;
dfbb6ac9 9780
ec73ddcd 9781 /* But if that didn't work out, discard dynamic relocs. */
f0158f44 9782 if (h->dynindx == -1)
190eb1dd 9783 h->dyn_relocs = NULL;
f0158f44
AM
9784 }
9785 else
190eb1dd 9786 h->dyn_relocs = NULL;
57e7d118
AM
9787 }
9788
9789 /* Finally, allocate space. */
190eb1dd 9790 for (p = h->dyn_relocs; p != NULL; p = p->next)
57e7d118
AM
9791 {
9792 asection *sreloc = elf_section_data (p->sec)->sreloc;
9793 if (eh->elf.type == STT_GNU_IFUNC)
9794 sreloc = htab->elf.irelplt;
9795 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9796 }
65f38f15 9797 }
57e7d118 9798
2d7ad24e
AM
9799 /* We might need a PLT entry when the symbol
9800 a) is dynamic, or
9801 b) is an ifunc, or
9802 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9803 d) has plt16 relocs and we are linking statically. */
9804 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9805 || h->type == STT_GNU_IFUNC
9806 || (h->needs_plt && h->dynamic_adjusted)
9807 || (h->needs_plt
9808 && h->def_regular
9809 && !htab->elf.dynamic_sections_created
3e04d765 9810 && !htab->can_convert_all_inline_plt
ed7007c1 9811 && (ppc_elf_hash_entry (h)->tls_mask
2d7ad24e 9812 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 9813 {
57e7d118
AM
9814 struct plt_entry *pent;
9815 bfd_boolean doneone = FALSE;
9816 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9817 if (pent->plt.refcount > 0)
9818 {
9819 if (!htab->elf.dynamic_sections_created
9820 || h->dynindx == -1)
9821 {
2d7ad24e
AM
9822 if (h->type == STT_GNU_IFUNC)
9823 {
9824 s = htab->elf.iplt;
9825 pent->plt.offset = s->size;
9826 s->size += PLT_ENTRY_SIZE (htab);
9827 s = htab->elf.irelplt;
9828 }
9829 else
9830 {
9831 s = htab->pltlocal;
9832 pent->plt.offset = s->size;
9833 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9834 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9835 }
57e7d118
AM
9836 }
9837 else
9838 {
9839 /* If this is the first .plt entry, make room for the special
9840 first entry. */
9841 s = htab->elf.splt;
9842 if (s->size == 0)
9843 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9844
57e7d118 9845 pent->plt.offset = s->size;
65f38f15 9846
57e7d118
AM
9847 /* Make room for this entry. */
9848 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9849
57e7d118
AM
9850 /* Make room for the .glink code. */
9851 s = htab->glink;
9852 if (s->size == 0)
9e390558 9853 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9854 if (htab->opd_abi)
9855 {
9856 /* We need bigger stubs past index 32767. */
9e390558 9857 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9858 s->size += 4;
9859 s->size += 2*4;
9860 }
9861 else
9862 s->size += 4;
65f38f15 9863
57e7d118
AM
9864 /* We also need to make an entry in the .rela.plt section. */
9865 s = htab->elf.srelplt;
9866 }
2d7ad24e
AM
9867 if (s != NULL)
9868 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
9869 doneone = TRUE;
9870 }
9871 else
9872 pent->plt.offset = (bfd_vma) -1;
9873 if (!doneone)
9874 {
9875 h->plt.plist = NULL;
9876 h->needs_plt = 0;
9877 }
65f38f15 9878 }
57e7d118 9879 else
65f38f15 9880 {
57e7d118
AM
9881 h->plt.plist = NULL;
9882 h->needs_plt = 0;
65f38f15
AM
9883 }
9884
b34976b6 9885 return TRUE;
65f38f15
AM
9886}
9887
9e390558
AM
9888#define PPC_LO(v) ((v) & 0xffff)
9889#define PPC_HI(v) (((v) >> 16) & 0xffff)
9890#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
9891#define D34(v) \
9892 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9893#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 9894
a345bc8d
AM
9895/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9896 to set up space for global entry stubs. These are put in glink,
9897 after the branch table. */
65f38f15 9898
b34976b6 9899static bfd_boolean
a345bc8d 9900size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9901{
a345bc8d
AM
9902 struct bfd_link_info *info;
9903 struct ppc_link_hash_table *htab;
9904 struct plt_entry *pent;
9e390558 9905 asection *s, *plt;
65f38f15 9906
a345bc8d
AM
9907 if (h->root.type == bfd_link_hash_indirect)
9908 return TRUE;
65f38f15 9909
a345bc8d
AM
9910 if (!h->pointer_equality_needed)
9911 return TRUE;
65f38f15 9912
a345bc8d
AM
9913 if (h->def_regular)
9914 return TRUE;
65f38f15 9915
a345bc8d
AM
9916 info = inf;
9917 htab = ppc_hash_table (info);
9918 if (htab == NULL)
9919 return FALSE;
9920
9e390558
AM
9921 s = htab->global_entry;
9922 plt = htab->elf.splt;
a345bc8d
AM
9923 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9924 if (pent->plt.offset != (bfd_vma) -1
9925 && pent->addend == 0)
9926 {
afe397ea
AM
9927 /* For ELFv2, if this symbol is not defined in a regular file
9928 and we are not generating a shared library or pie, then we
9929 need to define the symbol in the executable on a call stub.
9930 This is to avoid text relocations. */
9e390558
AM
9931 bfd_vma off, stub_align, stub_off, stub_size;
9932 unsigned int align_power;
9933
9934 stub_size = 16;
9935 stub_off = s->size;
9936 if (htab->params->plt_stub_align >= 0)
9937 align_power = htab->params->plt_stub_align;
9938 else
9939 align_power = -htab->params->plt_stub_align;
9940 /* Setting section alignment is delayed until we know it is
9941 non-empty. Otherwise the .text output section will be
9942 aligned at least to plt_stub_align even when no global
9943 entry stubs are needed. */
9944 if (s->alignment_power < align_power)
9945 s->alignment_power = align_power;
9946 stub_align = (bfd_vma) 1 << align_power;
9947 if (htab->params->plt_stub_align >= 0
9948 || ((((stub_off + stub_size - 1) & -stub_align)
9949 - (stub_off & -stub_align))
9950 > ((stub_size - 1) & -stub_align)))
9951 stub_off = (stub_off + stub_align - 1) & -stub_align;
9952 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9953 off -= stub_off + s->output_offset + s->output_section->vma;
9954 /* Note that for --plt-stub-align negative we have a possible
9955 dependency between stub offset and size. Break that
9956 dependency by assuming the max stub size when calculating
9957 the stub offset. */
9958 if (PPC_HA (off) == 0)
9959 stub_size -= 4;
8a2058b5 9960 h->root.type = bfd_link_hash_defined;
afe397ea 9961 h->root.u.def.section = s;
9e390558
AM
9962 h->root.u.def.value = stub_off;
9963 s->size = stub_off + stub_size;
a345bc8d
AM
9964 break;
9965 }
9966 return TRUE;
9967}
9968
65f38f15
AM
9969/* Set the sizes of the dynamic sections. */
9970
b34976b6 9971static bfd_boolean
ee67d69a 9972ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9973 struct bfd_link_info *info)
65f38f15
AM
9974{
9975 struct ppc_link_hash_table *htab;
9976 bfd *dynobj;
9977 asection *s;
b34976b6 9978 bfd_boolean relocs;
65f38f15 9979 bfd *ibfd;
7865406b 9980 struct got_entry *first_tlsld;
65f38f15
AM
9981
9982 htab = ppc_hash_table (info);
4dfe6ac6
NC
9983 if (htab == NULL)
9984 return FALSE;
9985
65f38f15
AM
9986 dynobj = htab->elf.dynobj;
9987 if (dynobj == NULL)
9988 abort ();
9989
9990 if (htab->elf.dynamic_sections_created)
9991 {
9992 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9993 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9994 {
3d4d4302 9995 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9996 if (s == NULL)
9997 abort ();
eea6121a 9998 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9999 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10000 }
10001 }
10002
10003 /* Set up .got offsets for local syms, and space for local dynamic
10004 relocs. */
c72f2fb2 10005 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10006 {
411e1bfb
AM
10007 struct got_entry **lgot_ents;
10008 struct got_entry **end_lgot_ents;
e054468f
AM
10009 struct plt_entry **local_plt;
10010 struct plt_entry **end_local_plt;
f961d9dd 10011 unsigned char *lgot_masks;
65f38f15
AM
10012 bfd_size_type locsymcount;
10013 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10014
0c8d6e5c 10015 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10016 continue;
10017
10018 for (s = ibfd->sections; s != NULL; s = s->next)
10019 {
19e08130 10020 struct ppc_dyn_relocs *p;
65f38f15 10021
6edfbbad 10022 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10023 {
ec338859
AM
10024 if (!bfd_is_abs_section (p->sec)
10025 && bfd_is_abs_section (p->sec->output_section))
10026 {
10027 /* Input section has been discarded, either because
10028 it is a copy of a linkonce section or due to
10029 linker script /DISCARD/, so we'll be discarding
10030 the relocs too. */
10031 }
248866a8 10032 else if (p->count != 0)
ec338859 10033 {
19e08130
AM
10034 asection *srel = elf_section_data (p->sec)->sreloc;
10035 if (p->ifunc)
33e44f2e 10036 srel = htab->elf.irelplt;
eea6121a 10037 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10038 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10039 info->flags |= DF_TEXTREL;
ec338859 10040 }
65f38f15
AM
10041 }
10042 }
10043
411e1bfb
AM
10044 lgot_ents = elf_local_got_ents (ibfd);
10045 if (!lgot_ents)
65f38f15
AM
10046 continue;
10047
0ffa91dd 10048 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10049 locsymcount = symtab_hdr->sh_info;
411e1bfb 10050 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10051 local_plt = (struct plt_entry **) end_lgot_ents;
10052 end_local_plt = local_plt + locsymcount;
f961d9dd 10053 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10054 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10055 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10056 {
0b8bcf0d 10057 struct got_entry **pent, *ent;
411e1bfb 10058
0b8bcf0d
AM
10059 pent = lgot_ents;
10060 while ((ent = *pent) != NULL)
411e1bfb
AM
10061 if (ent->got.refcount > 0)
10062 {
e7b938ca 10063 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10064 {
927be08e 10065 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10066 *pent = ent->next;
411e1bfb
AM
10067 }
10068 else
10069 {
19e08130
AM
10070 unsigned int ent_size = 8;
10071 unsigned int rel_size = sizeof (Elf64_External_Rela);
10072
eea6121a 10073 ent->got.offset = s->size;
e7b938ca 10074 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10075 {
19e08130
AM
10076 ent_size *= 2;
10077 rel_size *= 2;
10078 }
10079 s->size += ent_size;
37da22e5 10080 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 10081 {
33e44f2e 10082 htab->elf.irelplt->size += rel_size;
19e08130
AM
10083 htab->got_reli_size += rel_size;
10084 }
93370e8e
AM
10085 else if (bfd_link_pic (info)
10086 && !(ent->tls_type != 0
10087 && bfd_link_executable (info)))
19e08130
AM
10088 {
10089 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10090 srel->size += rel_size;
927be08e 10091 }
0b8bcf0d 10092 pent = &ent->next;
411e1bfb
AM
10093 }
10094 }
10095 else
0b8bcf0d 10096 *pent = ent->next;
65f38f15 10097 }
e054468f 10098
2d7ad24e
AM
10099 /* Allocate space for plt calls to local syms. */
10100 lgot_masks = (unsigned char *) end_local_plt;
10101 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
10102 {
10103 struct plt_entry *ent;
10104
10105 for (ent = *local_plt; ent != NULL; ent = ent->next)
10106 if (ent->plt.refcount > 0)
10107 {
2d7ad24e
AM
10108 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10109 {
10110 s = htab->elf.iplt;
10111 ent->plt.offset = s->size;
10112 s->size += PLT_ENTRY_SIZE (htab);
10113 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10114 }
3e04d765
AM
10115 else if (htab->can_convert_all_inline_plt
10116 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
10117 ent->plt.offset = (bfd_vma) -1;
10118 else
10119 {
10120 s = htab->pltlocal;
10121 ent->plt.offset = s->size;
10122 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10123 if (bfd_link_pic (info))
10124 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10125 }
e054468f
AM
10126 }
10127 else
10128 ent->plt.offset = (bfd_vma) -1;
10129 }
65f38f15
AM
10130 }
10131
10132 /* Allocate global sym .plt and .got entries, and space for global
10133 sym dynamic relocs. */
4ce794b7 10134 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10135
0e1862bb 10136 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10137 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10138
7865406b 10139 first_tlsld = NULL;
c72f2fb2 10140 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10141 {
7865406b
AM
10142 struct got_entry *ent;
10143
0c8d6e5c 10144 if (!is_ppc64_elf (ibfd))
102890f0
AM
10145 continue;
10146
7865406b
AM
10147 ent = ppc64_tlsld_got (ibfd);
10148 if (ent->got.refcount > 0)
102890f0 10149 {
7865406b 10150 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10151 {
7865406b
AM
10152 ent->is_indirect = TRUE;
10153 ent->got.ent = first_tlsld;
10154 }
10155 else
10156 {
10157 if (first_tlsld == NULL)
10158 first_tlsld = ent;
10159 s = ppc64_elf_tdata (ibfd)->got;
10160 ent->got.offset = s->size;
10161 ent->owner = ibfd;
10162 s->size += 16;
f749f26e 10163 if (bfd_link_dll (info))
7865406b
AM
10164 {
10165 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10166 srel->size += sizeof (Elf64_External_Rela);
10167 }
102890f0
AM
10168 }
10169 }
10170 else
7865406b 10171 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10172 }
10173
65f38f15
AM
10174 /* We now have determined the sizes of the various dynamic sections.
10175 Allocate memory for them. */
b34976b6 10176 relocs = FALSE;
65f38f15
AM
10177 for (s = dynobj->sections; s != NULL; s = s->next)
10178 {
10179 if ((s->flags & SEC_LINKER_CREATED) == 0)
10180 continue;
10181
4ce794b7 10182 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10183 /* These haven't been allocated yet; don't strip. */
10184 continue;
33e44f2e
AM
10185 else if (s == htab->elf.sgot
10186 || s == htab->elf.splt
10187 || s == htab->elf.iplt
2d7ad24e 10188 || s == htab->pltlocal
c456f082 10189 || s == htab->glink
9e390558 10190 || s == htab->global_entry
5474d94f
AM
10191 || s == htab->elf.sdynbss
10192 || s == htab->elf.sdynrelro)
65f38f15
AM
10193 {
10194 /* Strip this section if we don't need it; see the
10195 comment below. */
5bd4f169 10196 }
58d180e8
AM
10197 else if (s == htab->glink_eh_frame)
10198 {
10199 if (!bfd_is_abs_section (s->output_section))
10200 /* Not sized yet. */
10201 continue;
10202 }
70cc837d 10203 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10204 {
c456f082 10205 if (s->size != 0)
5bd4f169 10206 {
33e44f2e 10207 if (s != htab->elf.srelplt)
b34976b6 10208 relocs = TRUE;
5bd4f169
AM
10209
10210 /* We use the reloc_count field as a counter if we need
10211 to copy relocs into the output file. */
10212 s->reloc_count = 0;
10213 }
10214 }
65f38f15 10215 else
5bd4f169
AM
10216 {
10217 /* It's not one of our sections, so don't allocate space. */
10218 continue;
10219 }
10220
eea6121a 10221 if (s->size == 0)
5bd4f169 10222 {
c456f082
AM
10223 /* If we don't need this section, strip it from the
10224 output file. This is mostly to handle .rela.bss and
10225 .rela.plt. We must create both sections in
10226 create_dynamic_sections, because they must be created
10227 before the linker maps input sections to output
10228 sections. The linker does that before
10229 adjust_dynamic_symbol is called, and it is that
10230 function which decides whether anything needs to go
10231 into these sections. */
8423293d 10232 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10233 continue;
10234 }
10235
06bcf541
AM
10236 if (bfd_is_abs_section (s->output_section))
10237 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10238 s->name);
10239
c456f082 10240 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10241 continue;
10242
65f38f15
AM
10243 /* Allocate memory for the section contents. We use bfd_zalloc
10244 here in case unused entries are not reclaimed before the
10245 section's contents are written out. This should not happen,
411e1bfb
AM
10246 but this way if it does we get a R_PPC64_NONE reloc in .rela
10247 sections instead of garbage.
10248 We also rely on the section contents being zero when writing
5474d94f 10249 the GOT and .dynrelro. */
eea6121a 10250 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10251 if (s->contents == NULL)
b34976b6 10252 return FALSE;
5bd4f169
AM
10253 }
10254
c72f2fb2 10255 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10256 {
0c8d6e5c 10257 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10258 continue;
10259
e717da7e 10260 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10261 if (s != NULL && s != htab->elf.sgot)
e717da7e 10262 {
eea6121a 10263 if (s->size == 0)
8423293d 10264 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10265 else
10266 {
eea6121a 10267 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10268 if (s->contents == NULL)
10269 return FALSE;
10270 }
10271 }
10272 s = ppc64_elf_tdata (ibfd)->relgot;
10273 if (s != NULL)
10274 {
eea6121a 10275 if (s->size == 0)
8423293d 10276 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10277 else
10278 {
eea6121a 10279 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10280 if (s->contents == NULL)
10281 return FALSE;
10282 relocs = TRUE;
10283 s->reloc_count = 0;
10284 }
10285 }
10286 }
10287
e86ce104 10288 if (htab->elf.dynamic_sections_created)
5bd4f169 10289 {
e8910a83
AM
10290 bfd_boolean tls_opt;
10291
5bd4f169
AM
10292 /* Add some entries to the .dynamic section. We fill in the
10293 values later, in ppc64_elf_finish_dynamic_sections, but we
10294 must add the entries now so that we get the correct size for
10295 the .dynamic section. The DT_DEBUG entry is filled in by the
10296 dynamic linker and used by the debugger. */
dc810e39 10297#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10298 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10299
0e1862bb 10300 if (bfd_link_executable (info))
5bd4f169 10301 {
dc810e39 10302 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10303 return FALSE;
5bd4f169
AM
10304 }
10305
33e44f2e 10306 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10307 {
dc810e39
AM
10308 if (!add_dynamic_entry (DT_PLTGOT, 0)
10309 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10310 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10311 || !add_dynamic_entry (DT_JMPREL, 0)
10312 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10313 return FALSE;
5bd4f169
AM
10314 }
10315
ee67d69a 10316 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10317 {
10318 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10319 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10320 return FALSE;
19397422
AM
10321 }
10322
7c9cf415 10323 tls_opt = (htab->params->tls_get_addr_opt
9e7028aa
AM
10324 && ((htab->tls_get_addr_fd != NULL
10325 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10326 || (htab->tga_desc_fd != NULL
10327 && htab->tga_desc_fd->elf.plt.plist != NULL)));
e8910a83
AM
10328 if (tls_opt || !htab->opd_abi)
10329 {
10330 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10331 return FALSE;
10332 }
a7f2871e 10333
5bd4f169
AM
10334 if (relocs)
10335 {
dc810e39
AM
10336 if (!add_dynamic_entry (DT_RELA, 0)
10337 || !add_dynamic_entry (DT_RELASZ, 0)
10338 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10339 return FALSE;
5bd4f169 10340
65f38f15
AM
10341 /* If any dynamic relocs apply to a read-only section,
10342 then we need a DT_TEXTREL entry. */
248866a8 10343 if ((info->flags & DF_TEXTREL) == 0)
d49e5065
L
10344 elf_link_hash_traverse (&htab->elf,
10345 _bfd_elf_maybe_set_textrel, info);
5bd4f169 10346
65f38f15 10347 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10348 {
65f38f15 10349 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10350 return FALSE;
5bd4f169 10351 }
5bd4f169 10352 }
5bd4f169 10353 }
65f38f15 10354#undef add_dynamic_entry
5bd4f169 10355
b34976b6 10356 return TRUE;
5bd4f169
AM
10357}
10358
a345bc8d
AM
10359/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10360
10361static bfd_boolean
10362ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10363{
10364 if (h->plt.plist != NULL
10365 && !h->def_regular
10366 && !h->pointer_equality_needed)
10367 return FALSE;
10368
10369 return _bfd_elf_hash_symbol (h);
10370}
10371
721956f4 10372/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10373
4ce794b7
AM
10374static inline enum ppc_stub_type
10375ppc_type_of_stub (asection *input_sec,
10376 const Elf_Internal_Rela *rel,
10377 struct ppc_link_hash_entry **hash,
e054468f 10378 struct plt_entry **plt_ent,
6911b7dc
AM
10379 bfd_vma destination,
10380 unsigned long local_off)
5bd4f169 10381{
721956f4
AM
10382 struct ppc_link_hash_entry *h = *hash;
10383 bfd_vma location;
10384 bfd_vma branch_offset;
10385 bfd_vma max_branch_offset;
4ce794b7 10386 enum elf_ppc64_reloc_type r_type;
5bd4f169 10387
721956f4
AM
10388 if (h != NULL)
10389 {
e054468f 10390 struct plt_entry *ent;
7fe2b9a6 10391 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10392 if (h->oh != NULL
10393 && h->oh->is_func_descriptor)
7b8f6675
AM
10394 {
10395 fdh = ppc_follow_link (h->oh);
10396 *hash = fdh;
10397 }
8387904d 10398
e054468f
AM
10399 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10400 if (ent->addend == rel->r_addend
10401 && ent->plt.offset != (bfd_vma) -1)
10402 {
e054468f
AM
10403 *plt_ent = ent;
10404 return ppc_stub_plt_call;
10405 }
5bd4f169 10406
7fe2b9a6
AM
10407 /* Here, we know we don't have a plt entry. If we don't have a
10408 either a defined function descriptor or a defined entry symbol
10409 in a regular object file, then it is pointless trying to make
10410 any other type of stub. */
854b41e7
AM
10411 if (!is_static_defined (&fdh->elf)
10412 && !is_static_defined (&h->elf))
721956f4 10413 return ppc_stub_none;
5d1634d7 10414 }
e054468f
AM
10415 else if (elf_local_got_ents (input_sec->owner) != NULL)
10416 {
10417 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10418 struct plt_entry **local_plt = (struct plt_entry **)
10419 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10420 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10421
10422 if (local_plt[r_symndx] != NULL)
10423 {
10424 struct plt_entry *ent;
10425
10426 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10427 if (ent->addend == rel->r_addend
10428 && ent->plt.offset != (bfd_vma) -1)
10429 {
10430 *plt_ent = ent;
10431 return ppc_stub_plt_call;
10432 }
10433 }
10434 }
5d1634d7 10435
721956f4
AM
10436 /* Determine where the call point is. */
10437 location = (input_sec->output_offset
10438 + input_sec->output_section->vma
10439 + rel->r_offset);
5d1634d7 10440
721956f4
AM
10441 branch_offset = destination - location;
10442 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10443
721956f4
AM
10444 /* Determine if a long branch stub is needed. */
10445 max_branch_offset = 1 << 25;
23cedd1d
AM
10446 if (r_type == R_PPC64_REL14
10447 || r_type == R_PPC64_REL14_BRTAKEN
10448 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10449 max_branch_offset = 1 << 15;
5d1634d7 10450
6911b7dc 10451 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10452 /* We need a stub. Figure out whether a long_branch or plt_branch
10453 is needed later. */
10454 return ppc_stub_long_branch;
5d1634d7 10455
721956f4 10456 return ppc_stub_none;
5d1634d7
AM
10457}
10458
f891966f
AM
10459/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10460 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10461 . mflr %r12
10462 . bcl 20,31,1f
10463 .1: mflr %r11
10464 . mtlr %r12
05d0e962 10465 . lis %r12,xxx-1b@highest
f891966f 10466 . ori %r12,%r12,xxx-1b@higher
05d0e962 10467 . sldi %r12,%r12,32
f891966f 10468 . oris %r12,%r12,xxx-1b@high
05d0e962 10469 . ori %r12,%r12,xxx-1b@l
f891966f 10470 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10471
10472static bfd_byte *
10473build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10474{
f891966f
AM
10475 bfd_put_32 (abfd, MFLR_R12, p);
10476 p += 4;
10477 bfd_put_32 (abfd, BCL_20_31, p);
10478 p += 4;
10479 bfd_put_32 (abfd, MFLR_R11, p);
10480 p += 4;
10481 bfd_put_32 (abfd, MTLR_R12, p);
10482 p += 4;
05d0e962
AM
10483 if (off + 0x8000 < 0x10000)
10484 {
10485 if (load)
10486 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10487 else
10488 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10489 p += 4;
10490 }
10491 else if (off + 0x80008000ULL < 0x100000000ULL)
10492 {
10493 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10494 p += 4;
10495 if (load)
10496 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10497 else
10498 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10499 p += 4;
10500 }
10501 else
10502 {
10503 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10504 {
10505 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10506 p += 4;
10507 }
10508 else
10509 {
10510 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10511 p += 4;
10512 if (((off >> 32) & 0xffff) != 0)
10513 {
10514 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10515 p += 4;
10516 }
10517 }
10518 if (((off >> 32) & 0xffffffffULL) != 0)
10519 {
10520 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10521 p += 4;
10522 }
10523 if (PPC_HI (off) != 0)
10524 {
10525 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10526 p += 4;
10527 }
10528 if (PPC_LO (off) != 0)
10529 {
10530 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10531 p += 4;
10532 }
10533 if (load)
10534 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10535 else
10536 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10537 p += 4;
10538 }
10539 return p;
10540}
10541
10542static unsigned int
10543size_offset (bfd_vma off)
10544{
10545 unsigned int size;
10546 if (off + 0x8000 < 0x10000)
10547 size = 4;
10548 else if (off + 0x80008000ULL < 0x100000000ULL)
10549 size = 8;
10550 else
10551 {
10552 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10553 size = 4;
10554 else
10555 {
10556 size = 4;
10557 if (((off >> 32) & 0xffff) != 0)
10558 size += 4;
10559 }
10560 if (((off >> 32) & 0xffffffffULL) != 0)
10561 size += 4;
10562 if (PPC_HI (off) != 0)
10563 size += 4;
10564 if (PPC_LO (off) != 0)
10565 size += 4;
10566 size += 4;
10567 }
f891966f 10568 return size + 16;
05d0e962
AM
10569}
10570
3d58e1fc
AM
10571static unsigned int
10572num_relocs_for_offset (bfd_vma off)
10573{
10574 unsigned int num_rel;
10575 if (off + 0x8000 < 0x10000)
10576 num_rel = 1;
10577 else if (off + 0x80008000ULL < 0x100000000ULL)
10578 num_rel = 2;
10579 else
10580 {
10581 num_rel = 1;
10582 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10583 && ((off >> 32) & 0xffff) != 0)
10584 num_rel += 1;
10585 if (PPC_HI (off) != 0)
10586 num_rel += 1;
10587 if (PPC_LO (off) != 0)
10588 num_rel += 1;
10589 }
10590 return num_rel;
10591}
10592
10593static Elf_Internal_Rela *
10594emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10595 bfd_vma roff, bfd_vma targ, bfd_vma off)
10596{
10597 bfd_vma relative_targ = targ - (roff - 8);
10598 if (bfd_big_endian (info->output_bfd))
10599 roff += 2;
10600 r->r_offset = roff;
10601 r->r_addend = relative_targ + roff;
10602 if (off + 0x8000 < 0x10000)
10603 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10604 else if (off + 0x80008000ULL < 0x100000000ULL)
10605 {
10606 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10607 ++r;
10608 roff += 4;
10609 r->r_offset = roff;
10610 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10611 r->r_addend = relative_targ + roff;
10612 }
10613 else
10614 {
10615 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10616 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10617 else
10618 {
10619 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10620 if (((off >> 32) & 0xffff) != 0)
10621 {
10622 ++r;
10623 roff += 4;
10624 r->r_offset = roff;
10625 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10626 r->r_addend = relative_targ + roff;
10627 }
10628 }
10629 if (((off >> 32) & 0xffffffffULL) != 0)
10630 roff += 4;
10631 if (PPC_HI (off) != 0)
10632 {
10633 ++r;
10634 roff += 4;
10635 r->r_offset = roff;
10636 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10637 r->r_addend = relative_targ + roff;
10638 }
10639 if (PPC_LO (off) != 0)
10640 {
10641 ++r;
10642 roff += 4;
10643 r->r_offset = roff;
10644 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10645 r->r_addend = relative_targ + roff;
10646 }
10647 }
10648 return r;
10649}
10650
04bdff6a 10651static bfd_byte *
7c1f4227 10652build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
04bdff6a
AM
10653 bfd_boolean load)
10654{
10655 uint64_t insn;
10656 if (off - odd + (1ULL << 33) < 1ULL << 34)
10657 {
10658 off -= odd;
10659 if (odd)
10660 {
10661 bfd_put_32 (abfd, NOP, p);
10662 p += 4;
10663 }
10664 if (load)
10665 insn = PLD_R12_PC;
10666 else
10667 insn = PADDI_R12_PC;
10668 insn |= D34 (off);
10669 bfd_put_32 (abfd, insn >> 32, p);
10670 p += 4;
10671 bfd_put_32 (abfd, insn, p);
10672 }
10673 /* The minimum value for paddi is -0x200000000. The minimum value
10674 for li is -0x8000, which when shifted by 34 and added gives a
10675 minimum value of -0x2000200000000. The maximum value is
10676 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10677 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10678 {
10679 off -= 8 - odd;
10680 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10681 p += 4;
10682 if (!odd)
10683 {
10684 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10685 p += 4;
10686 }
10687 insn = PADDI_R12_PC | D34 (off);
10688 bfd_put_32 (abfd, insn >> 32, p);
10689 p += 4;
10690 bfd_put_32 (abfd, insn, p);
10691 p += 4;
10692 if (odd)
10693 {
10694 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10695 p += 4;
10696 }
10697 if (load)
10698 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10699 else
10700 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10701 }
10702 else
10703 {
10704 off -= odd + 8;
10705 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10706 p += 4;
10707 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10708 p += 4;
10709 if (odd)
10710 {
10711 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10712 p += 4;
10713 }
10714 insn = PADDI_R12_PC | D34 (off);
10715 bfd_put_32 (abfd, insn >> 32, p);
10716 p += 4;
10717 bfd_put_32 (abfd, insn, p);
10718 p += 4;
10719 if (!odd)
10720 {
10721 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10722 p += 4;
10723 }
10724 if (load)
10725 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10726 else
10727 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10728 }
10729 p += 4;
10730 return p;
10731}
10732
10733static unsigned int
7c1f4227 10734size_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
10735{
10736 if (off - odd + (1ULL << 33) < 1ULL << 34)
10737 return odd + 8;
10738 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10739 return 20;
10740 else
10741 return 24;
10742}
10743
10744static unsigned int
7c1f4227 10745num_relocs_for_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
10746{
10747 if (off - odd + (1ULL << 33) < 1ULL << 34)
10748 return 1;
10749 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10750 return 2;
10751 else
10752 return 3;
10753}
10754
10755static Elf_Internal_Rela *
7c1f4227 10756emit_relocs_for_power10_offset (struct bfd_link_info *info,
04bdff6a
AM
10757 Elf_Internal_Rela *r, bfd_vma roff,
10758 bfd_vma targ, bfd_vma off, int odd)
10759{
10760 if (off - odd + (1ULL << 33) < 1ULL << 34)
10761 roff += odd;
10762 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10763 {
10764 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10765 r->r_offset = roff + d_offset;
10766 r->r_addend = targ + 8 - odd - d_offset;
10767 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10768 ++r;
10769 roff += 8 - odd;
10770 }
10771 else
10772 {
10773 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10774 r->r_offset = roff + d_offset;
10775 r->r_addend = targ + 8 + odd - d_offset;
10776 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10777 ++r;
10778 roff += 4;
10779 r->r_offset = roff + d_offset;
10780 r->r_addend = targ + 4 + odd - d_offset;
10781 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10782 ++r;
10783 roff += 4 + odd;
10784 }
10785 r->r_offset = roff;
10786 r->r_addend = targ;
10787 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10788 return r;
10789}
10790
df136d64
AM
10791/* Emit .eh_frame opcode to advance pc by DELTA. */
10792
10793static bfd_byte *
10794eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10795{
10796 delta /= 4;
10797 if (delta < 64)
10798 *eh++ = DW_CFA_advance_loc + delta;
10799 else if (delta < 256)
10800 {
10801 *eh++ = DW_CFA_advance_loc1;
10802 *eh++ = delta;
10803 }
10804 else if (delta < 65536)
10805 {
10806 *eh++ = DW_CFA_advance_loc2;
10807 bfd_put_16 (abfd, delta, eh);
10808 eh += 2;
10809 }
10810 else
10811 {
10812 *eh++ = DW_CFA_advance_loc4;
10813 bfd_put_32 (abfd, delta, eh);
10814 eh += 4;
10815 }
10816 return eh;
10817}
10818
10819/* Size of required .eh_frame opcode to advance pc by DELTA. */
10820
10821static unsigned int
10822eh_advance_size (unsigned int delta)
10823{
10824 if (delta < 64 * 4)
10825 /* DW_CFA_advance_loc+[1..63]. */
10826 return 1;
10827 if (delta < 256 * 4)
10828 /* DW_CFA_advance_loc1, byte. */
10829 return 2;
10830 if (delta < 65536 * 4)
10831 /* DW_CFA_advance_loc2, 2 bytes. */
10832 return 3;
10833 /* DW_CFA_advance_loc4, 4 bytes. */
10834 return 5;
10835}
10836
794e51c0
AM
10837/* With power7 weakly ordered memory model, it is possible for ld.so
10838 to update a plt entry in one thread and have another thread see a
10839 stale zero toc entry. To avoid this we need some sort of acquire
10840 barrier in the call stub. One solution is to make the load of the
10841 toc word seem to appear to depend on the load of the function entry
10842 word. Another solution is to test for r2 being zero, and branch to
10843 the appropriate glink entry if so.
10844
10845 . fake dep barrier compare
71a39c98
AM
10846 . ld 12,xxx(2) ld 12,xxx(2)
10847 . mtctr 12 mtctr 12
10848 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10849 . add 2,2,11 cmpldi 2,0
10850 . ld 2,xxx+8(2) bnectr+
10851 . bctr b <glink_entry>
10852
10853 The solution involving the compare turns out to be faster, so
10854 that's what we use unless the branch won't reach. */
10855
10856#define ALWAYS_USE_FAKE_DEP 0
10857#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10858
794e51c0
AM
10859static inline unsigned int
10860plt_stub_size (struct ppc_link_hash_table *htab,
10861 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
10862 bfd_vma off,
10863 unsigned int odd)
794e51c0 10864{
05d0e962 10865 unsigned size;
b9e5796b 10866
05d0e962
AM
10867 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10868 {
e10a07b3 10869 if (htab->params->power10_stubs != 0)
29433886 10870 size = 8 + size_power10_offset (off, odd);
04bdff6a
AM
10871 else
10872 size = 8 + size_offset (off - 8);
05d0e962
AM
10873 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10874 size += 4;
05d0e962 10875 }
29433886 10876 else
b9e5796b 10877 {
29433886
AM
10878 size = 12;
10879 if (ALWAYS_EMIT_R2SAVE
10880 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
b9e5796b 10881 size += 4;
29433886 10882 if (PPC_HA (off) != 0)
b9e5796b 10883 size += 4;
29433886
AM
10884 if (htab->opd_abi)
10885 {
10886 size += 4;
10887 if (htab->params->plt_static_chain)
10888 size += 4;
10889 if (htab->params->plt_thread_safe
10890 && htab->elf.dynamic_sections_created
10891 && stub_entry->h != NULL
10892 && stub_entry->h->elf.dynindx != -1)
10893 size += 8;
10894 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
10895 != PPC_HA (off))
10896 size += 4;
10897 }
b9e5796b 10898 }
794e51c0 10899 if (stub_entry->h != NULL
ed7007c1 10900 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 10901 && htab->params->tls_get_addr_opt)
f378ab09 10902 {
29433886 10903 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 10904 {
29433886
AM
10905 size += 30 * 4;
10906 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
10907 || stub_entry->stub_type == ppc_stub_plt_call_both)
10908 size += 4;
9e7028aa
AM
10909 }
10910 else
10911 {
29433886
AM
10912 size += 7 * 4;
10913 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
10914 || stub_entry->stub_type == ppc_stub_plt_call_both)
10915 size += 6 * 4;
9e7028aa 10916 }
f378ab09 10917 }
794e51c0
AM
10918 return size;
10919}
10920
2420fff6
AM
10921/* Depending on the sign of plt_stub_align:
10922 If positive, return the padding to align to a 2**plt_stub_align
10923 boundary.
10924 If negative, if this stub would cross fewer 2**plt_stub_align
10925 boundaries if we align, then return the padding needed to do so. */
10926
794e51c0
AM
10927static inline unsigned int
10928plt_stub_pad (struct ppc_link_hash_table *htab,
10929 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
10930 bfd_vma plt_off,
10931 unsigned int odd)
794e51c0 10932{
2420fff6 10933 int stub_align;
1aa42141 10934 unsigned stub_size;
6f20ed8a 10935 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10936
2420fff6
AM
10937 if (htab->params->plt_stub_align >= 0)
10938 {
10939 stub_align = 1 << htab->params->plt_stub_align;
10940 if ((stub_off & (stub_align - 1)) != 0)
10941 return stub_align - (stub_off & (stub_align - 1));
10942 return 0;
10943 }
10944
10945 stub_align = 1 << -htab->params->plt_stub_align;
29433886 10946 stub_size = plt_stub_size (htab, stub_entry, plt_off, odd);
794e51c0 10947 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10948 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10949 return stub_align - (stub_off & (stub_align - 1));
10950 return 0;
10951}
10952
10953/* Build a .plt call stub. */
10954
10955static inline bfd_byte *
10956build_plt_stub (struct ppc_link_hash_table *htab,
10957 struct ppc_stub_hash_entry *stub_entry,
10958 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10959{
e7d1c40c 10960 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10961 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10962 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10963 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10964 && htab->elf.dynamic_sections_created
10965 && stub_entry->h != NULL
10966 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10967 bfd_boolean use_fake_dep = plt_thread_safe;
10968 bfd_vma cmp_branch_off = 0;
10969
10970 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10971 && plt_load_toc
794e51c0 10972 && plt_thread_safe
9e51d549
AM
10973 && !(stub_entry->h != NULL
10974 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 10975 && htab->params->tls_get_addr_opt))
794e51c0
AM
10976 {
10977 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10978 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10979 / PLT_ENTRY_SIZE (htab));
9e390558 10980 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10981 bfd_vma to, from;
10982
68d62958
AM
10983 if (pltindex > 32768)
10984 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10985 to = (glinkoff
10986 + htab->glink->output_offset
10987 + htab->glink->output_section->vma);
6f20ed8a 10988 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10989 + 4 * (ALWAYS_EMIT_R2SAVE
10990 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10991 + 4 * (PPC_HA (offset) != 0)
10992 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10993 != PPC_HA (offset))
10994 + 4 * (plt_static_chain != 0)
10995 + 20
6f20ed8a
AM
10996 + stub_entry->group->stub_sec->output_offset
10997 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10998 cmp_branch_off = to - from;
10999 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11000 }
11001
ac2df442
AM
11002 if (PPC_HA (offset) != 0)
11003 {
176a0d42
AM
11004 if (r != NULL)
11005 {
794e51c0
AM
11006 if (ALWAYS_EMIT_R2SAVE
11007 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11008 r[0].r_offset += 4;
176a0d42 11009 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 11010 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
11011 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11012 r[1].r_addend = r[0].r_addend;
b9e5796b 11013 if (plt_load_toc)
176a0d42 11014 {
b9e5796b 11015 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11016 {
b9e5796b
AM
11017 r[2].r_offset = r[1].r_offset + 4;
11018 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11019 r[2].r_addend = r[0].r_addend;
11020 }
11021 else
11022 {
11023 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11024 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11025 r[2].r_addend = r[0].r_addend + 8;
11026 if (plt_static_chain)
11027 {
11028 r[3].r_offset = r[2].r_offset + 4;
11029 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11030 r[3].r_addend = r[0].r_addend + 16;
11031 }
c7131b65 11032 }
176a0d42
AM
11033 }
11034 }
794e51c0
AM
11035 if (ALWAYS_EMIT_R2SAVE
11036 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 11037 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
11038 if (plt_load_toc)
11039 {
11040 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11041 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11042 }
11043 else
11044 {
11045 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11046 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11047 }
b9e5796b
AM
11048 if (plt_load_toc
11049 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 11050 {
71a39c98 11051 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
11052 offset = 0;
11053 }
71a39c98 11054 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11055 if (plt_load_toc)
794e51c0 11056 {
b9e5796b
AM
11057 if (use_fake_dep)
11058 {
11059 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11060 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11061 }
11062 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11063 if (plt_static_chain)
11064 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 11065 }
ac2df442
AM
11066 }
11067 else
11068 {
176a0d42
AM
11069 if (r != NULL)
11070 {
794e51c0
AM
11071 if (ALWAYS_EMIT_R2SAVE
11072 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11073 r[0].r_offset += 4;
176a0d42 11074 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 11075 if (plt_load_toc)
176a0d42 11076 {
b9e5796b 11077 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11078 {
b9e5796b
AM
11079 r[1].r_offset = r[0].r_offset + 4;
11080 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11081 r[1].r_addend = r[0].r_addend;
11082 }
11083 else
11084 {
11085 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11086 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11087 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11088 if (plt_static_chain)
11089 {
11090 r[2].r_offset = r[1].r_offset + 4;
11091 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11092 r[2].r_addend = r[0].r_addend + 8;
11093 }
c7131b65 11094 }
176a0d42
AM
11095 }
11096 }
794e51c0
AM
11097 if (ALWAYS_EMIT_R2SAVE
11098 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 11099 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 11100 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
11101 if (plt_load_toc
11102 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
11103 {
11104 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11105 offset = 0;
11106 }
71a39c98 11107 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11108 if (plt_load_toc)
794e51c0 11109 {
b9e5796b
AM
11110 if (use_fake_dep)
11111 {
11112 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11113 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11114 }
11115 if (plt_static_chain)
11116 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11117 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 11118 }
ac2df442 11119 }
b9e5796b 11120 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
11121 {
11122 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11123 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 11124 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
11125 }
11126 else
407aa07c 11127 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
11128 return p;
11129}
11130
a7f2871e
AM
11131/* Build a special .plt call stub for __tls_get_addr. */
11132
b9ca1af6 11133#define LD_R0_0R3 0xe8030000
a7f2871e
AM
11134#define LD_R12_0R3 0xe9830000
11135#define MR_R0_R3 0x7c601b78
b9ca1af6 11136#define CMPDI_R0_0 0x2c200000
a7f2871e
AM
11137#define ADD_R3_R12_R13 0x7c6c6a14
11138#define BEQLR 0x4d820020
11139#define MR_R3_R0 0x7c030378
a7f2871e 11140#define BCTRL 0x4e800421
a7f2871e 11141
29433886
AM
11142static bfd_byte *
11143build_tls_get_addr_head (struct ppc_link_hash_table *htab,
794e51c0 11144 struct ppc_stub_hash_entry *stub_entry,
29433886 11145 bfd_byte *p)
a7f2871e 11146{
e7d1c40c 11147 bfd *obfd = htab->params->stub_bfd;
794e51c0 11148
b9ca1af6 11149 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
a7f2871e 11150 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
b9ca1af6 11151 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
a7f2871e 11152 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
a7f2871e
AM
11153 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11154 bfd_put_32 (obfd, BEQLR, p), p += 4;
11155 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9e7028aa 11156
29433886
AM
11157 if (!htab->params->no_tls_get_addr_regsave)
11158 p = tls_get_addr_prologue (obfd, p, htab);
11159 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11160 || stub_entry->stub_type == ppc_stub_plt_call_both)
11161 {
9e7028aa
AM
11162 bfd_put_32 (obfd, MFLR_R0, p);
11163 p += 4;
11164 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11165 p += 4;
29433886
AM
11166 }
11167 return p;
11168}
9e7028aa 11169
29433886
AM
11170static bfd_byte *
11171build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11172 struct ppc_stub_hash_entry *stub_entry,
11173 bfd_byte *p,
11174 bfd_byte *loc)
11175{
11176 bfd *obfd = htab->params->stub_bfd;
11177
11178 if (!htab->params->no_tls_get_addr_regsave)
11179 {
11180 bfd_put_32 (obfd, BCTRL, p - 4);
11181
11182 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11183 || stub_entry->stub_type == ppc_stub_plt_call_both)
11184 {
11185 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11186 p += 4;
11187 }
11188 p = tls_get_addr_epilogue (obfd, p, htab);
11189 }
11190 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11191 || stub_entry->stub_type == ppc_stub_plt_call_both)
11192 {
9e7028aa
AM
11193 bfd_put_32 (obfd, BCTRL, p - 4);
11194
11195 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11196 p += 4;
11197 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11198 p += 4;
11199 bfd_put_32 (obfd, MTLR_R0, p);
11200 p += 4;
11201 bfd_put_32 (obfd, BLR, p);
11202 p += 4;
11203 }
a7f2871e 11204
df136d64
AM
11205 if (htab->glink_eh_frame != NULL
11206 && htab->glink_eh_frame->size != 0)
11207 {
11208 bfd_byte *base, *eh;
df136d64
AM
11209
11210 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11211 eh = base + stub_entry->group->eh_size;
29433886
AM
11212
11213 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 11214 {
29433886
AM
11215 unsigned int cfa_updt, delta, i;
11216
9e7028aa
AM
11217 /* After the bctrl, lr has been modified so we need to emit
11218 .eh_frame info saying the return address is on the stack. In
11219 fact we must put the EH info at or before the call rather
11220 than after it, because the EH info for a call needs to be
11221 specified by that point.
11222 See libgcc/unwind-dw2.c execute_cfa_program.
11223 Any stack pointer update must be described immediately after
11224 the instruction making the change, and since the stdu occurs
11225 after saving regs we put all the reg saves and the cfa
11226 change there. */
11227 cfa_updt = stub_entry->stub_offset + 18 * 4;
11228 delta = cfa_updt - stub_entry->group->lr_restore;
11229 stub_entry->group->lr_restore
11230 = stub_entry->stub_offset + (p - loc) - 4;
11231 eh = eh_advance (htab->elf.dynobj, eh, delta);
11232 *eh++ = DW_CFA_def_cfa_offset;
11233 if (htab->opd_abi)
11234 {
11235 *eh++ = 128;
11236 *eh++ = 1;
11237 }
11238 else
11239 *eh++ = 96;
11240 *eh++ = DW_CFA_offset_extended_sf;
11241 *eh++ = 65;
11242 *eh++ = (-16 / 8) & 0x7f;
11243 for (i = 4; i < 12; i++)
11244 {
11245 *eh++ = DW_CFA_offset + i;
11246 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11247 }
11248 *eh++ = (DW_CFA_advance_loc
11249 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11250 *eh++ = DW_CFA_def_cfa_offset;
11251 *eh++ = 0;
11252 for (i = 4; i < 12; i++)
11253 *eh++ = DW_CFA_restore + i;
11254 *eh++ = DW_CFA_advance_loc + 2;
29433886
AM
11255 *eh++ = DW_CFA_restore_extended;
11256 *eh++ = 65;
11257 stub_entry->group->eh_size = eh - base;
11258 }
11259 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11260 || stub_entry->stub_type == ppc_stub_plt_call_both)
11261 {
11262 unsigned int lr_used, delta;
11263
11264 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11265 delta = lr_used - stub_entry->group->lr_restore;
11266 stub_entry->group->lr_restore = lr_used + 16;
11267 eh = eh_advance (htab->elf.dynobj, eh, delta);
11268 *eh++ = DW_CFA_offset_extended_sf;
11269 *eh++ = 65;
11270 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11271 *eh++ = DW_CFA_advance_loc + 4;
11272 *eh++ = DW_CFA_restore_extended;
11273 *eh++ = 65;
11274 stub_entry->group->eh_size = eh - base;
9e7028aa 11275 }
df136d64 11276 }
a7f2871e
AM
11277 return p;
11278}
11279
176a0d42
AM
11280static Elf_Internal_Rela *
11281get_relocs (asection *sec, int count)
11282{
11283 Elf_Internal_Rela *relocs;
11284 struct bfd_elf_section_data *elfsec_data;
11285
11286 elfsec_data = elf_section_data (sec);
11287 relocs = elfsec_data->relocs;
11288 if (relocs == NULL)
11289 {
11290 bfd_size_type relsize;
11291 relsize = sec->reloc_count * sizeof (*relocs);
11292 relocs = bfd_alloc (sec->owner, relsize);
11293 if (relocs == NULL)
11294 return NULL;
11295 elfsec_data->relocs = relocs;
d4730f92
BS
11296 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11297 sizeof (Elf_Internal_Shdr));
11298 if (elfsec_data->rela.hdr == NULL)
11299 return NULL;
11300 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11301 * sizeof (Elf64_External_Rela));
11302 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
11303 sec->reloc_count = 0;
11304 }
11305 relocs += sec->reloc_count;
11306 sec->reloc_count += count;
11307 return relocs;
11308}
11309
3d58e1fc
AM
11310/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11311 forms, to the equivalent relocs against the global symbol given by
11312 STUB_ENTRY->H. */
11313
11314static bfd_boolean
11315use_global_in_relocs (struct ppc_link_hash_table *htab,
11316 struct ppc_stub_hash_entry *stub_entry,
11317 Elf_Internal_Rela *r, unsigned int num_rel)
11318{
11319 struct elf_link_hash_entry **hashes;
11320 unsigned long symndx;
11321 struct ppc_link_hash_entry *h;
11322 bfd_vma symval;
11323
11324 /* Relocs are always against symbols in their own object file. Fake
11325 up global sym hashes for the stub bfd (which has no symbols). */
11326 hashes = elf_sym_hashes (htab->params->stub_bfd);
11327 if (hashes == NULL)
11328 {
11329 bfd_size_type hsize;
11330
11331 /* When called the first time, stub_globals will contain the
11332 total number of symbols seen during stub sizing. After
11333 allocating, stub_globals is used as an index to fill the
11334 hashes array. */
11335 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11336 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11337 if (hashes == NULL)
11338 return FALSE;
11339 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11340 htab->stub_globals = 1;
11341 }
11342 symndx = htab->stub_globals++;
11343 h = stub_entry->h;
11344 hashes[symndx] = &h->elf;
11345 if (h->oh != NULL && h->oh->is_func)
11346 h = ppc_follow_link (h->oh);
11347 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11348 || h->elf.root.type == bfd_link_hash_defweak);
ed7007c1 11349 symval = defined_sym_val (&h->elf);
3d58e1fc
AM
11350 while (num_rel-- != 0)
11351 {
11352 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11353 if (h->elf.root.u.def.section != stub_entry->target_section)
11354 {
11355 /* H is an opd symbol. The addend must be zero, and the
11356 branch reloc is the only one we can convert. */
11357 r->r_addend = 0;
11358 break;
11359 }
11360 else
11361 r->r_addend -= symval;
11362 --r;
11363 }
11364 return TRUE;
11365}
11366
aa374f67 11367static bfd_vma
25f53a85 11368get_r2off (struct bfd_link_info *info,
aa374f67
AM
11369 struct ppc_stub_hash_entry *stub_entry)
11370{
25f53a85 11371 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 11372 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
11373
11374 if (r2off == 0)
11375 {
11376 /* Support linking -R objects. Get the toc pointer from the
11377 opd entry. */
11378 char buf[8];
b9e5796b
AM
11379 if (!htab->opd_abi)
11380 return r2off;
aa374f67
AM
11381 asection *opd = stub_entry->h->elf.root.u.def.section;
11382 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11383
11384 if (strcmp (opd->name, ".opd") != 0
11385 || opd->reloc_count != 0)
11386 {
2cdcc330
AM
11387 info->callbacks->einfo
11388 (_("%P: cannot find opd entry toc for `%pT'\n"),
11389 stub_entry->h->elf.root.root.string);
aa374f67 11390 bfd_set_error (bfd_error_bad_value);
a7c49797 11391 return (bfd_vma) -1;
aa374f67
AM
11392 }
11393 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11394 return (bfd_vma) -1;
aa374f67 11395 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11396 r2off -= elf_gp (info->output_bfd);
aa374f67 11397 }
6f20ed8a 11398 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11399 return r2off;
11400}
11401
b34976b6 11402static bfd_boolean
4ce794b7 11403ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11404{
721956f4
AM
11405 struct ppc_stub_hash_entry *stub_entry;
11406 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11407 struct bfd_link_info *info;
11408 struct ppc_link_hash_table *htab;
29433886 11409 bfd *obfd;
721956f4 11410 bfd_byte *loc;
3d58e1fc 11411 bfd_byte *p, *relp;
1aa42141 11412 bfd_vma targ, off;
176a0d42 11413 Elf_Internal_Rela *r;
e054468f 11414 asection *plt;
3d58e1fc 11415 int num_rel;
04bdff6a 11416 int odd;
29433886 11417 bfd_boolean is_tga;
5d1634d7 11418
721956f4
AM
11419 /* Massage our args to the form they really have. */
11420 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11421 info = in_arg;
5d1634d7 11422
abf874aa
CL
11423 /* Fail if the target section could not be assigned to an output
11424 section. The user should fix his linker script. */
11425 if (stub_entry->target_section != NULL
11426 && stub_entry->target_section->output_section == NULL
11427 && info->non_contiguous_regions)
53215f21
CL
11428 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
11429 "Retry without --enable-non-contiguous-regions.\n"),
11430 stub_entry->target_section);
abf874aa
CL
11431
11432 /* Same for the group. */
11433 if (stub_entry->group->stub_sec != NULL
11434 && stub_entry->group->stub_sec->output_section == NULL
11435 && info->non_contiguous_regions)
53215f21
CL
11436 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11437 "output section. Retry without "
11438 "--enable-non-contiguous-regions.\n"),
11439 stub_entry->group->stub_sec,
11440 stub_entry->target_section);
abf874aa 11441
5d1634d7 11442 htab = ppc_hash_table (info);
4dfe6ac6
NC
11443 if (htab == NULL)
11444 return FALSE;
5d1634d7 11445
1aa42141 11446 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 11447 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11448
4ce794b7 11449 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 11450 switch (stub_entry->stub_type)
5d1634d7 11451 {
721956f4 11452 case ppc_stub_long_branch:
ad8e1ba5 11453 case ppc_stub_long_branch_r2off:
721956f4 11454 /* Branches are relative. This is where we are going to. */
1aa42141 11455 targ = (stub_entry->target_value
6911b7dc
AM
11456 + stub_entry->target_section->output_offset
11457 + stub_entry->target_section->output_section->vma);
1aa42141 11458 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11459
721956f4 11460 /* And this is where we are coming from. */
1aa42141
AM
11461 off = (stub_entry->stub_offset
11462 + stub_entry->group->stub_sec->output_offset
11463 + stub_entry->group->stub_sec->output_section->vma);
11464 off = targ - off;
e86ce104 11465
9e390558 11466 p = loc;
29433886 11467 obfd = htab->params->stub_bfd;
ac2df442 11468 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 11469 {
25f53a85 11470 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11471
a7c49797 11472 if (r2off == (bfd_vma) -1)
aa374f67
AM
11473 {
11474 htab->stub_error = TRUE;
11475 return FALSE;
11476 }
29433886 11477 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11478 p += 4;
ac2df442
AM
11479 if (PPC_HA (r2off) != 0)
11480 {
29433886 11481 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
9e390558 11482 p += 4;
a7c49797
AM
11483 }
11484 if (PPC_LO (r2off) != 0)
11485 {
29433886 11486 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
9e390558 11487 p += 4;
ac2df442 11488 }
9e390558 11489 off -= p - loc;
ad8e1ba5 11490 }
29433886 11491 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
9e390558 11492 p += 4;
ad8e1ba5 11493
5c3dead3
AM
11494 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11495 {
cf97bcb0
AM
11496 _bfd_error_handler
11497 (_("long branch stub `%s' offset overflow"),
bc30df16 11498 stub_entry->root.string);
5c3dead3
AM
11499 htab->stub_error = TRUE;
11500 return FALSE;
11501 }
ee75fd95
AM
11502
11503 if (info->emitrelocations)
11504 {
6f20ed8a 11505 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
11506 if (r == NULL)
11507 return FALSE;
9e390558 11508 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11509 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11510 r->r_addend = targ;
3d58e1fc
AM
11511 if (stub_entry->h != NULL
11512 && !use_global_in_relocs (htab, stub_entry, r, 1))
11513 return FALSE;
ee75fd95 11514 }
721956f4 11515 break;
e86ce104 11516
721956f4 11517 case ppc_stub_plt_branch:
ad8e1ba5 11518 case ppc_stub_plt_branch_r2off:
721956f4
AM
11519 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11520 stub_entry->root.string + 9,
b34976b6 11521 FALSE, FALSE);
721956f4
AM
11522 if (br_entry == NULL)
11523 {
cf97bcb0
AM
11524 _bfd_error_handler (_("can't find branch stub `%s'"),
11525 stub_entry->root.string);
b34976b6
AM
11526 htab->stub_error = TRUE;
11527 return FALSE;
721956f4
AM
11528 }
11529
1aa42141 11530 targ = (stub_entry->target_value
176a0d42
AM
11531 + stub_entry->target_section->output_offset
11532 + stub_entry->target_section->output_section->vma);
6911b7dc 11533 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 11534 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11535
1aa42141 11536 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11537 htab->brlt->contents + br_entry->offset);
721956f4 11538
f94498ff 11539 if (br_entry->iter == htab->stub_iteration)
721956f4 11540 {
f94498ff 11541 br_entry->iter = 0;
84f5d08e 11542
f94498ff 11543 if (htab->relbrlt != NULL)
84f5d08e 11544 {
f94498ff
AM
11545 /* Create a reloc for the branch lookup table entry. */
11546 Elf_Internal_Rela rela;
11547 bfd_byte *rl;
11548
11549 rela.r_offset = (br_entry->offset
11550 + htab->brlt->output_offset
11551 + htab->brlt->output_section->vma);
11552 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11553 rela.r_addend = targ;
f94498ff
AM
11554
11555 rl = htab->relbrlt->contents;
11556 rl += (htab->relbrlt->reloc_count++
11557 * sizeof (Elf64_External_Rela));
11558 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11559 }
11560 else if (info->emitrelocations)
11561 {
176a0d42
AM
11562 r = get_relocs (htab->brlt, 1);
11563 if (r == NULL)
11564 return FALSE;
11565 /* brlt, being SEC_LINKER_CREATED does not go through the
11566 normal reloc processing. Symbols and offsets are not
11567 translated from input file to output file form, so
11568 set up the offset per the output file. */
f94498ff
AM
11569 r->r_offset = (br_entry->offset
11570 + htab->brlt->output_offset
11571 + htab->brlt->output_section->vma);
11572 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11573 r->r_addend = targ;
84f5d08e 11574 }
84f5d08e 11575 }
721956f4 11576
1aa42141 11577 targ = (br_entry->offset
176a0d42
AM
11578 + htab->brlt->output_offset
11579 + htab->brlt->output_section->vma);
11580
1aa42141
AM
11581 off = (elf_gp (info->output_bfd)
11582 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11583 off = targ - off;
721956f4 11584
ad8e1ba5 11585 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11586 {
25f53a85 11587 info->callbacks->einfo
c1c8c1ef 11588 (_("%P: linkage table error against `%pT'\n"),
721956f4 11589 stub_entry->root.string);
5d1634d7 11590 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11591 htab->stub_error = TRUE;
11592 return FALSE;
5d1634d7 11593 }
41bd81ab 11594
176a0d42
AM
11595 if (info->emitrelocations)
11596 {
6f20ed8a 11597 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11598 if (r == NULL)
11599 return FALSE;
6f20ed8a 11600 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11601 if (bfd_big_endian (info->output_bfd))
11602 r[0].r_offset += 2;
00f412ee 11603 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11604 r[0].r_offset += 4;
11605 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11606 r[0].r_addend = targ;
176a0d42
AM
11607 if (PPC_HA (off) != 0)
11608 {
11609 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11610 r[1].r_offset = r[0].r_offset + 4;
11611 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11612 r[1].r_addend = r[0].r_addend;
11613 }
11614 }
11615
9e390558 11616 p = loc;
29433886 11617 obfd = htab->params->stub_bfd;
00f412ee 11618 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11619 {
176a0d42 11620 if (PPC_HA (off) != 0)
ac2df442 11621 {
29433886 11622 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11623 p += 4;
29433886 11624 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11625 }
11626 else
29433886 11627 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11628 }
11629 else
11630 {
25f53a85 11631 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11632
a7c49797 11633 if (r2off == (bfd_vma) -1)
aa374f67
AM
11634 {
11635 htab->stub_error = TRUE;
11636 return FALSE;
11637 }
ad8e1ba5 11638
29433886 11639 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11640 p += 4;
176a0d42 11641 if (PPC_HA (off) != 0)
ac2df442 11642 {
29433886 11643 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11644 p += 4;
29433886 11645 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11646 }
11647 else
29433886 11648 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11649
11650 if (PPC_HA (r2off) != 0)
11651 {
9e390558 11652 p += 4;
29433886 11653 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11654 }
11655 if (PPC_LO (r2off) != 0)
11656 {
9e390558 11657 p += 4;
29433886 11658 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11659 }
ad8e1ba5 11660 }
9e390558 11661 p += 4;
29433886 11662 bfd_put_32 (obfd, MTCTR_R12, p);
9e390558 11663 p += 4;
29433886 11664 bfd_put_32 (obfd, BCTR, p);
407aa07c 11665 p += 4;
721956f4 11666 break;
5d1634d7 11667
05d0e962
AM
11668 case ppc_stub_long_branch_notoc:
11669 case ppc_stub_long_branch_both:
11670 case ppc_stub_plt_branch_notoc:
11671 case ppc_stub_plt_branch_both:
11672 case ppc_stub_plt_call_notoc:
11673 case ppc_stub_plt_call_both:
11674 p = loc;
f891966f 11675 off = (stub_entry->stub_offset
05d0e962
AM
11676 + stub_entry->group->stub_sec->output_offset
11677 + stub_entry->group->stub_sec->output_section->vma);
29433886
AM
11678 obfd = htab->params->stub_bfd;
11679 is_tga = ((stub_entry->stub_type == ppc_stub_plt_call_notoc
11680 || stub_entry->stub_type == ppc_stub_plt_call_both)
12cf8b93 11681 && stub_entry->h != NULL
29433886
AM
11682 && is_tls_get_addr (&stub_entry->h->elf, htab)
11683 && htab->params->tls_get_addr_opt);
11684 if (is_tga)
11685 {
11686 p = build_tls_get_addr_head (htab, stub_entry, p);
11687 off += p - loc;
11688 }
05d0e962
AM
11689 if (stub_entry->stub_type == ppc_stub_long_branch_both
11690 || stub_entry->stub_type == ppc_stub_plt_branch_both
11691 || stub_entry->stub_type == ppc_stub_plt_call_both)
11692 {
11693 off += 4;
29433886 11694 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
05d0e962
AM
11695 p += 4;
11696 }
11697 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11698 {
11699 targ = stub_entry->plt_ent->plt.offset & ~1;
11700 if (targ >= (bfd_vma) -2)
11701 abort ();
11702
11703 plt = htab->elf.splt;
11704 if (!htab->elf.dynamic_sections_created
11705 || stub_entry->h == NULL
11706 || stub_entry->h->elf.dynindx == -1)
11707 {
11708 if (stub_entry->symtype == STT_GNU_IFUNC)
11709 plt = htab->elf.iplt;
11710 else
11711 plt = htab->pltlocal;
11712 }
11713 targ += plt->output_offset + plt->output_section->vma;
11714 }
11715 else
11716 targ = (stub_entry->target_value
11717 + stub_entry->target_section->output_offset
11718 + stub_entry->target_section->output_section->vma);
04bdff6a 11719 odd = off & 4;
05d0e962 11720 off = targ - off;
f891966f 11721
3d58e1fc
AM
11722 relp = p;
11723 num_rel = 0;
e10a07b3 11724 if (htab->params->power10_stubs != 0)
04bdff6a
AM
11725 {
11726 bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
29433886 11727 p = build_power10_offset (obfd, p, off, odd, load);
04bdff6a
AM
11728 }
11729 else
11730 {
29433886
AM
11731 if (htab->glink_eh_frame != NULL
11732 && htab->glink_eh_frame->size != 0)
11733 {
11734 bfd_byte *base, *eh;
11735 unsigned int lr_used, delta;
11736
11737 base = (htab->glink_eh_frame->contents
11738 + stub_entry->group->eh_base + 17);
11739 eh = base + stub_entry->group->eh_size;
11740 lr_used = stub_entry->stub_offset + (p - loc) + 8;
11741 delta = lr_used - stub_entry->group->lr_restore;
11742 stub_entry->group->lr_restore = lr_used + 8;
11743 eh = eh_advance (htab->elf.dynobj, eh, delta);
11744 *eh++ = DW_CFA_register;
11745 *eh++ = 65;
11746 *eh++ = 12;
11747 *eh++ = DW_CFA_advance_loc + 2;
11748 *eh++ = DW_CFA_restore_extended;
11749 *eh++ = 65;
11750 stub_entry->group->eh_size = eh - base;
11751 }
11752
04bdff6a
AM
11753 /* The notoc stubs calculate their target (either a PLT entry or
11754 the global entry point of a function) relative to the PC
11755 returned by the "bcl" two instructions past the start of the
11756 sequence emitted by build_offset. The offset is therefore 8
11757 less than calculated from the start of the sequence. */
11758 off -= 8;
29433886 11759 p = build_offset (obfd, p, off,
04bdff6a
AM
11760 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11761 }
11762
f891966f 11763 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
05d0e962 11764 {
f891966f 11765 bfd_vma from;
3d58e1fc 11766 num_rel = 1;
f891966f
AM
11767 from = (stub_entry->stub_offset
11768 + stub_entry->group->stub_sec->output_offset
11769 + stub_entry->group->stub_sec->output_section->vma
11770 + (p - loc));
29433886 11771 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
11772 }
11773 else
11774 {
29433886 11775 bfd_put_32 (obfd, MTCTR_R12, p);
05d0e962 11776 p += 4;
29433886 11777 bfd_put_32 (obfd, BCTR, p);
05d0e962
AM
11778 }
11779 p += 4;
df136d64 11780
29433886
AM
11781 if (is_tga)
11782 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
11783
3d58e1fc
AM
11784 if (info->emitrelocations)
11785 {
04bdff6a 11786 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
e10a07b3 11787 if (htab->params->power10_stubs != 0)
7c1f4227 11788 num_rel += num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
11789 else
11790 {
11791 num_rel += num_relocs_for_offset (off);
11792 roff += 16;
11793 }
3d58e1fc
AM
11794 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11795 if (r == NULL)
11796 return FALSE;
e10a07b3 11797 if (htab->params->power10_stubs != 0)
7c1f4227 11798 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
04bdff6a
AM
11799 else
11800 r = emit_relocs_for_offset (info, r, roff, targ, off);
3d58e1fc
AM
11801 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11802 || stub_entry->stub_type == ppc_stub_long_branch_both)
11803 {
11804 ++r;
11805 roff = p - 4 - stub_entry->group->stub_sec->contents;
11806 r->r_offset = roff;
11807 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11808 r->r_addend = targ;
11809 if (stub_entry->h != NULL
11810 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11811 return FALSE;
11812 }
11813 }
05d0e962
AM
11814 break;
11815
721956f4 11816 case ppc_stub_plt_call:
794e51c0 11817 case ppc_stub_plt_call_r2save:
e054468f 11818 if (stub_entry->h != NULL
b31867b6
AM
11819 && stub_entry->h->is_func_descriptor
11820 && stub_entry->h->oh != NULL)
c862ae31 11821 {
b31867b6
AM
11822 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11823
11824 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11825 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11826 if (fh->elf.root.type == bfd_link_hash_undefined
11827 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11828 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11829 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11830 }
11831
721956f4 11832 /* Now build the stub. */
1aa42141
AM
11833 targ = stub_entry->plt_ent->plt.offset & ~1;
11834 if (targ >= (bfd_vma) -2)
721956f4
AM
11835 abort ();
11836
33e44f2e 11837 plt = htab->elf.splt;
25f23106
AM
11838 if (!htab->elf.dynamic_sections_created
11839 || stub_entry->h == NULL
11840 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11841 {
11842 if (stub_entry->symtype == STT_GNU_IFUNC)
11843 plt = htab->elf.iplt;
11844 else
11845 plt = htab->pltlocal;
11846 }
1aa42141 11847 targ += plt->output_offset + plt->output_section->vma;
e054468f 11848
1aa42141
AM
11849 off = (elf_gp (info->output_bfd)
11850 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11851 off = targ - off;
721956f4 11852
ad8e1ba5 11853 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11854 {
25f53a85 11855 info->callbacks->einfo
695344c0 11856 /* xgettext:c-format */
c1c8c1ef 11857 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11858 stub_entry->h != NULL
11859 ? stub_entry->h->elf.root.root.string
11860 : "<local sym>");
721956f4 11861 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11862 htab->stub_error = TRUE;
11863 return FALSE;
721956f4
AM
11864 }
11865
176a0d42
AM
11866 r = NULL;
11867 if (info->emitrelocations)
11868 {
6f20ed8a 11869 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11870 ((PPC_HA (off) != 0)
11871 + (htab->opd_abi
e7d1c40c 11872 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11873 && PPC_HA (off + 16) == PPC_HA (off))
11874 : 1)));
176a0d42
AM
11875 if (r == NULL)
11876 return FALSE;
6f20ed8a 11877 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11878 if (bfd_big_endian (info->output_bfd))
11879 r[0].r_offset += 2;
1aa42141 11880 r[0].r_addend = targ;
176a0d42 11881 }
29433886
AM
11882 p = loc;
11883 obfd = htab->params->stub_bfd;
11884 is_tga = (stub_entry->h != NULL
11885 && is_tls_get_addr (&stub_entry->h->elf, htab)
11886 && htab->params->tls_get_addr_opt);
11887 if (is_tga)
11888 {
11889 p = build_tls_get_addr_head (htab, stub_entry, p);
11890 if (r != NULL)
11891 r[0].r_offset += p - loc;
11892 }
11893 p = build_plt_stub (htab, stub_entry, p, off, r);
11894 if (is_tga)
11895 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
721956f4
AM
11896 break;
11897
a4b6fadd
AM
11898 case ppc_stub_save_res:
11899 return TRUE;
11900
721956f4
AM
11901 default:
11902 BFD_FAIL ();
b34976b6 11903 return FALSE;
721956f4
AM
11904 }
11905
1aa42141 11906 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 11907
e7d1c40c 11908 if (htab->params->emit_stub_syms)
97b639ba
AM
11909 {
11910 struct elf_link_hash_entry *h;
ee75fd95
AM
11911 size_t len1, len2;
11912 char *name;
11913 const char *const stub_str[] = { "long_branch",
05d0e962
AM
11914 "long_branch",
11915 "long_branch",
3f6ff479
AM
11916 "long_branch",
11917 "plt_branch",
ee75fd95 11918 "plt_branch",
05d0e962
AM
11919 "plt_branch",
11920 "plt_branch",
11921 "plt_call",
11922 "plt_call",
794e51c0 11923 "plt_call",
ee75fd95
AM
11924 "plt_call" };
11925
11926 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11927 len2 = strlen (stub_entry->root.string);
11928 name = bfd_malloc (len1 + len2 + 2);
11929 if (name == NULL)
11930 return FALSE;
11931 memcpy (name, stub_entry->root.string, 9);
11932 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11933 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11934 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11935 if (h == NULL)
11936 return FALSE;
11937 if (h->root.type == bfd_link_hash_new)
11938 {
11939 h->root.type = bfd_link_hash_defined;
6f20ed8a 11940 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11941 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11942 h->ref_regular = 1;
11943 h->def_regular = 1;
11944 h->ref_regular_nonweak = 1;
11945 h->forced_local = 1;
11946 h->non_elf = 0;
2ec55de3 11947 h->root.linker_def = 1;
97b639ba
AM
11948 }
11949 }
11950
b34976b6 11951 return TRUE;
721956f4
AM
11952}
11953
11954/* As above, but don't actually build the stub. Just bump offset so
11955 we know stub section sizes, and select plt_branch stubs where
11956 long_branch stubs won't do. */
11957
b34976b6 11958static bfd_boolean
4ce794b7 11959ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11960{
11961 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11962 struct bfd_link_info *info;
721956f4 11963 struct ppc_link_hash_table *htab;
f891966f
AM
11964 asection *plt;
11965 bfd_vma targ, off, r2off;
04bdff6a 11966 unsigned int size, extra, lr_used, delta, odd;
721956f4
AM
11967
11968 /* Massage our args to the form they really have. */
11969 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11970 info = in_arg;
11971
11972 htab = ppc_hash_table (info);
4dfe6ac6
NC
11973 if (htab == NULL)
11974 return FALSE;
721956f4 11975
abf874aa
CL
11976 /* Fail if the target section could not be assigned to an output
11977 section. The user should fix his linker script. */
11978 if (stub_entry->target_section != NULL
11979 && stub_entry->target_section->output_section == NULL
11980 && info->non_contiguous_regions)
53215f21
CL
11981 info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
11982 "Retry without --enable-non-contiguous-regions.\n"),
11983 stub_entry->target_section);
abf874aa
CL
11984
11985 /* Same for the group. */
11986 if (stub_entry->group->stub_sec != NULL
11987 && stub_entry->group->stub_sec->output_section == NULL
11988 && info->non_contiguous_regions)
53215f21
CL
11989 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11990 "output section. Retry without "
11991 "--enable-non-contiguous-regions.\n"),
11992 stub_entry->group->stub_sec,
11993 stub_entry->target_section);
abf874aa 11994
1aa42141
AM
11995 /* Make a note of the offset within the stubs for this entry. */
11996 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11997
a4b6fadd
AM
11998 if (stub_entry->h != NULL
11999 && stub_entry->h->save_res
12000 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12001 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12002 {
12003 /* Don't make stubs to out-of-line register save/restore
12004 functions. Instead, emit copies of the functions. */
12005 stub_entry->group->needs_save_res = 1;
12006 stub_entry->stub_type = ppc_stub_save_res;
12007 return TRUE;
12008 }
12009
f891966f 12010 switch (stub_entry->stub_type)
721956f4 12011 {
f891966f
AM
12012 case ppc_stub_plt_branch:
12013 case ppc_stub_plt_branch_r2off:
12014 /* Reset the stub type from the plt branch variant in case we now
12015 can reach with a shorter stub. */
12016 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12017 /* Fall through. */
12018 case ppc_stub_long_branch:
12019 case ppc_stub_long_branch_r2off:
1aa42141
AM
12020 targ = (stub_entry->target_value
12021 + stub_entry->target_section->output_offset
12022 + stub_entry->target_section->output_section->vma);
f891966f 12023 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
1aa42141
AM
12024 off = (stub_entry->stub_offset
12025 + stub_entry->group->stub_sec->output_offset
12026 + stub_entry->group->stub_sec->output_section->vma);
721956f4 12027
ad8e1ba5 12028 size = 4;
f891966f 12029 r2off = 0;
ad8e1ba5
AM
12030 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
12031 {
25f53a85 12032 r2off = get_r2off (info, stub_entry);
a7c49797 12033 if (r2off == (bfd_vma) -1)
aa374f67
AM
12034 {
12035 htab->stub_error = TRUE;
12036 return FALSE;
12037 }
a7c49797 12038 size = 8;
ac2df442 12039 if (PPC_HA (r2off) != 0)
a7c49797
AM
12040 size += 4;
12041 if (PPC_LO (r2off) != 0)
12042 size += 4;
1aa42141 12043 off += size - 4;
ad8e1ba5 12044 }
1aa42141 12045 off = targ - off;
ad8e1ba5 12046
f891966f
AM
12047 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12048 Do the same for -R objects without function descriptors. */
12049 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
12050 && r2off == 0
12051 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12052 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 12053 {
f891966f 12054 struct ppc_branch_hash_entry *br_entry;
df136d64 12055
f891966f
AM
12056 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12057 stub_entry->root.string + 9,
12058 TRUE, FALSE);
12059 if (br_entry == NULL)
721956f4 12060 {
f891966f
AM
12061 _bfd_error_handler (_("can't build branch stub `%s'"),
12062 stub_entry->root.string);
12063 htab->stub_error = TRUE;
12064 return FALSE;
721956f4
AM
12065 }
12066
f891966f 12067 if (br_entry->iter != htab->stub_iteration)
721956f4 12068 {
f891966f
AM
12069 br_entry->iter = htab->stub_iteration;
12070 br_entry->offset = htab->brlt->size;
12071 htab->brlt->size += 8;
63bc6f6c 12072
f891966f
AM
12073 if (htab->relbrlt != NULL)
12074 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12075 else if (info->emitrelocations)
84f5d08e 12076 {
f891966f
AM
12077 htab->brlt->reloc_count += 1;
12078 htab->brlt->flags |= SEC_RELOC;
05d0e962 12079 }
f891966f 12080 }
ac2df442 12081
f891966f
AM
12082 targ = (br_entry->offset
12083 + htab->brlt->output_offset
12084 + htab->brlt->output_section->vma);
12085 off = (elf_gp (info->output_bfd)
12086 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12087 off = targ - off;
176a0d42 12088
f891966f
AM
12089 if (info->emitrelocations)
12090 {
12091 stub_entry->group->stub_sec->reloc_count
12092 += 1 + (PPC_HA (off) != 0);
12093 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12094 }
05d0e962 12095
f891966f
AM
12096 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
12097 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
12098 {
12099 size = 12;
12100 if (PPC_HA (off) != 0)
12101 size = 16;
ac2df442 12102 }
f891966f 12103 else
ac2df442 12104 {
f891966f
AM
12105 size = 16;
12106 if (PPC_HA (off) != 0)
12107 size += 4;
12108
12109 if (PPC_HA (r2off) != 0)
12110 size += 4;
12111 if (PPC_LO (r2off) != 0)
12112 size += 4;
ac2df442 12113 }
721956f4 12114 }
f891966f
AM
12115 else if (info->emitrelocations)
12116 {
12117 stub_entry->group->stub_sec->reloc_count += 1;
12118 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12119 }
12120 break;
12121
12122 case ppc_stub_plt_branch_notoc:
12123 case ppc_stub_plt_branch_both:
12124 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12125 /* Fall through. */
12126 case ppc_stub_long_branch_notoc:
12127 case ppc_stub_long_branch_both:
12128 off = (stub_entry->stub_offset
12129 + stub_entry->group->stub_sec->output_offset
12130 + stub_entry->group->stub_sec->output_section->vma);
12131 size = 0;
12132 if (stub_entry->stub_type == ppc_stub_long_branch_both)
12133 size = 4;
12134 off += size;
12135 targ = (stub_entry->target_value
12136 + stub_entry->target_section->output_offset
12137 + stub_entry->target_section->output_section->vma);
04bdff6a 12138 odd = off & 4;
f891966f
AM
12139 off = targ - off;
12140
3d58e1fc
AM
12141 if (info->emitrelocations)
12142 {
04bdff6a 12143 unsigned int num_rel;
e10a07b3 12144 if (htab->params->power10_stubs != 0)
7c1f4227 12145 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12146 else
12147 num_rel = num_relocs_for_offset (off - 8);
12148 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12149 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12150 }
12151
e10a07b3 12152 if (htab->params->power10_stubs != 0)
7c1f4227 12153 extra = size_power10_offset (off, odd);
04bdff6a
AM
12154 else
12155 extra = size_offset (off - 8);
f891966f
AM
12156 /* Include branch insn plus those in the offset sequence. */
12157 size += 4 + extra;
12158 /* The branch insn is at the end, or "extra" bytes along. So
12159 its offset will be "extra" bytes less that that already
12160 calculated. */
12161 off -= extra;
12162
e10a07b3 12163 if (htab->params->power10_stubs == 0)
04bdff6a
AM
12164 {
12165 /* After the bcl, lr has been modified so we need to emit
12166 .eh_frame info saying the return address is in r12. */
12167 lr_used = stub_entry->stub_offset + 8;
12168 if (stub_entry->stub_type == ppc_stub_long_branch_both)
12169 lr_used += 4;
12170 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12171 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12172 DW_CFA_restore_extended 65. */
12173 delta = lr_used - stub_entry->group->lr_restore;
12174 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12175 stub_entry->group->lr_restore = lr_used + 8;
12176 }
f891966f
AM
12177
12178 /* If the branch can't reach, use a plt_branch. */
12179 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12180 {
12181 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
12182 - ppc_stub_long_branch_notoc);
12183 size += 4;
12184 }
3d58e1fc
AM
12185 else if (info->emitrelocations)
12186 stub_entry->group->stub_sec->reloc_count +=1;
f891966f
AM
12187 break;
12188
12189 case ppc_stub_plt_call_notoc:
12190 case ppc_stub_plt_call_both:
29433886 12191 lr_used = 0;
12cf8b93
AM
12192 if (stub_entry->h != NULL
12193 && is_tls_get_addr (&stub_entry->h->elf, htab)
29433886
AM
12194 && htab->params->tls_get_addr_opt)
12195 {
12196 lr_used += 7 * 4;
12197 if (!htab->params->no_tls_get_addr_regsave)
12198 lr_used += 11 * 4;
12199 else if (stub_entry->stub_type == ppc_stub_plt_call_both)
12200 lr_used += 2 * 4;
12201 }
f891966f 12202 if (stub_entry->stub_type == ppc_stub_plt_call_both)
29433886 12203 lr_used += 4;
f891966f
AM
12204 targ = stub_entry->plt_ent->plt.offset & ~1;
12205 if (targ >= (bfd_vma) -2)
12206 abort ();
12207
12208 plt = htab->elf.splt;
12209 if (!htab->elf.dynamic_sections_created
12210 || stub_entry->h == NULL
12211 || stub_entry->h->elf.dynindx == -1)
12212 {
12213 if (stub_entry->symtype == STT_GNU_IFUNC)
12214 plt = htab->elf.iplt;
12215 else
12216 plt = htab->pltlocal;
12217 }
12218 targ += plt->output_offset + plt->output_section->vma;
29433886
AM
12219 off = (stub_entry->stub_offset
12220 + stub_entry->group->stub_sec->output_offset
12221 + stub_entry->group->stub_sec->output_section->vma
12222 + lr_used);
04bdff6a 12223 odd = off & 4;
f891966f
AM
12224 off = targ - off;
12225
12226 if (htab->params->plt_stub_align != 0)
12227 {
29433886 12228 unsigned pad = plt_stub_pad (htab, stub_entry, off, odd);
f891966f
AM
12229
12230 stub_entry->group->stub_sec->size += pad;
12231 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12232 off -= pad;
29433886 12233 odd ^= pad & 4;
f891966f
AM
12234 }
12235
3d58e1fc
AM
12236 if (info->emitrelocations)
12237 {
04bdff6a 12238 unsigned int num_rel;
e10a07b3 12239 if (htab->params->power10_stubs != 0)
7c1f4227 12240 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12241 else
12242 num_rel = num_relocs_for_offset (off - 8);
12243 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12244 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12245 }
12246
29433886 12247 size = plt_stub_size (htab, stub_entry, off, odd);
f891966f 12248
e10a07b3 12249 if (htab->params->power10_stubs == 0)
04bdff6a
AM
12250 {
12251 /* After the bcl, lr has been modified so we need to emit
12252 .eh_frame info saying the return address is in r12. */
29433886 12253 lr_used += stub_entry->stub_offset + 8;
04bdff6a
AM
12254 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12255 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12256 DW_CFA_restore_extended 65. */
12257 delta = lr_used - stub_entry->group->lr_restore;
12258 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12259 stub_entry->group->lr_restore = lr_used + 8;
12260 }
29433886
AM
12261 if ((stub_entry->stub_type == ppc_stub_plt_call_notoc
12262 || stub_entry->stub_type == ppc_stub_plt_call_both)
12cf8b93 12263 && stub_entry->h != NULL
29433886
AM
12264 && is_tls_get_addr (&stub_entry->h->elf, htab)
12265 && htab->params->tls_get_addr_opt)
12266 {
12267 if (!htab->params->no_tls_get_addr_regsave)
12268 {
12269 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12270 delta = cfa_updt - stub_entry->group->lr_restore;
12271 stub_entry->group->eh_size += eh_advance_size (delta);
12272 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12273 stub_entry->group->lr_restore
12274 = stub_entry->stub_offset + size - 4;
12275 }
12276 else if (stub_entry->stub_type == ppc_stub_plt_call_both)
12277 {
12278 lr_used = stub_entry->stub_offset + size - 20;
12279 delta = lr_used - stub_entry->group->lr_restore;
12280 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12281 stub_entry->group->lr_restore
12282 = stub_entry->stub_offset + size - 4;
12283 }
12284 }
f891966f
AM
12285 break;
12286
12287 case ppc_stub_plt_call:
12288 case ppc_stub_plt_call_r2save:
12289 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12290 if (targ >= (bfd_vma) -2)
12291 abort ();
12292 plt = htab->elf.splt;
12293 if (!htab->elf.dynamic_sections_created
12294 || stub_entry->h == NULL
12295 || stub_entry->h->elf.dynindx == -1)
12296 {
12297 if (stub_entry->symtype == STT_GNU_IFUNC)
12298 plt = htab->elf.iplt;
12299 else
12300 plt = htab->pltlocal;
12301 }
12302 targ += plt->output_offset + plt->output_section->vma;
12303
12304 off = (elf_gp (info->output_bfd)
12305 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12306 off = targ - off;
12307
12308 if (htab->params->plt_stub_align != 0)
12309 {
29433886 12310 unsigned pad = plt_stub_pad (htab, stub_entry, off, 0);
f891966f
AM
12311
12312 stub_entry->group->stub_sec->size += pad;
12313 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12314 }
12315
12316 if (info->emitrelocations)
12317 {
12318 stub_entry->group->stub_sec->reloc_count
12319 += ((PPC_HA (off) != 0)
12320 + (htab->opd_abi
12321 ? 2 + (htab->params->plt_static_chain
12322 && PPC_HA (off + 16) == PPC_HA (off))
12323 : 1));
12324 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12325 }
12326
29433886 12327 size = plt_stub_size (htab, stub_entry, off, 0);
f891966f
AM
12328
12329 if (stub_entry->h != NULL
ed7007c1 12330 && is_tls_get_addr (&stub_entry->h->elf, htab)
f891966f
AM
12331 && htab->params->tls_get_addr_opt
12332 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
12333 {
29433886
AM
12334 if (!htab->params->no_tls_get_addr_regsave)
12335 {
12336 /* Adjustments to r1 need to be described. */
12337 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12338 delta = cfa_updt - stub_entry->group->lr_restore;
12339 stub_entry->group->eh_size += eh_advance_size (delta);
12340 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12341 }
12342 else
9e7028aa
AM
12343 {
12344 lr_used = stub_entry->stub_offset + size - 20;
12345 /* The eh_frame info will consist of a DW_CFA_advance_loc
12346 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12347 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12348 delta = lr_used - stub_entry->group->lr_restore;
12349 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12350 }
29433886 12351 stub_entry->group->lr_restore = stub_entry->stub_offset + size - 4;
f891966f
AM
12352 }
12353 break;
12354
12355 default:
12356 BFD_FAIL ();
12357 return FALSE;
721956f4
AM
12358 }
12359
6f20ed8a 12360 stub_entry->group->stub_sec->size += size;
b34976b6 12361 return TRUE;
721956f4
AM
12362}
12363
12364/* Set up various things so that we can make a list of input sections
12365 for each output section included in the link. Returns -1 on error,
cedb70c5 12366 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
12367
12368int
e7d1c40c 12369ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 12370{
6f20ed8a 12371 unsigned int id;
986f0783 12372 size_t amt;
721956f4
AM
12373 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12374
4dfe6ac6
NC
12375 if (htab == NULL)
12376 return -1;
4c52953f 12377
7cf7fcc8 12378 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
12379 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12380 htab->sec_info = bfd_zmalloc (amt);
12381 if (htab->sec_info == NULL)
721956f4
AM
12382 return -1;
12383
3d6f9012
AM
12384 /* Set toc_off for com, und, abs and ind sections. */
12385 for (id = 0; id < 3; id++)
6f20ed8a 12386 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 12387
721956f4
AM
12388 return 1;
12389}
12390
927be08e
AM
12391/* Set up for first pass at multitoc partitioning. */
12392
12393void
12394ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12395{
12396 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12397
1c865ab2 12398 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
12399 htab->toc_bfd = NULL;
12400 htab->toc_first_sec = NULL;
12401}
12402
e717da7e
AM
12403/* The linker repeatedly calls this function for each TOC input section
12404 and linker generated GOT section. Group input bfds such that the toc
927be08e 12405 within a group is less than 64k in size. */
ad8e1ba5 12406
927be08e 12407bfd_boolean
4ce794b7 12408ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
12409{
12410 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 12411 bfd_vma addr, off, limit;
ad8e1ba5 12412
4dfe6ac6
NC
12413 if (htab == NULL)
12414 return FALSE;
12415
927be08e 12416 if (!htab->second_toc_pass)
4c52953f 12417 {
927be08e 12418 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
12419 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
12420
12421 if (new_bfd)
bf102f86
AM
12422 {
12423 htab->toc_bfd = isec->owner;
12424 htab->toc_first_sec = isec;
12425 }
927be08e 12426
bf102f86
AM
12427 addr = isec->output_offset + isec->output_section->vma;
12428 off = addr - htab->toc_curr;
d77c8a4b
AM
12429 limit = 0x80008000;
12430 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12431 limit = 0x10000;
12432 if (off + isec->size > limit)
bf102f86
AM
12433 {
12434 addr = (htab->toc_first_sec->output_offset
12435 + htab->toc_first_sec->output_section->vma);
12436 htab->toc_curr = addr;
a27e685f 12437 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 12438 }
99877b66 12439
927be08e
AM
12440 /* toc_curr is the base address of this toc group. Set elf_gp
12441 for the input section to be the offset relative to the
12442 output toc base plus 0x8000. Making the input elf_gp an
12443 offset allows us to move the toc as a whole without
12444 recalculating input elf_gp. */
06bcf541 12445 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
12446 off += TOC_BASE_OFF;
12447
12448 /* Die if someone uses a linker script that doesn't keep input
12449 file .toc and .got together. */
a4fd3de5
AM
12450 if (new_bfd
12451 && elf_gp (isec->owner) != 0
927be08e
AM
12452 && elf_gp (isec->owner) != off)
12453 return FALSE;
12454
12455 elf_gp (isec->owner) = off;
12456 return TRUE;
4c52953f 12457 }
927be08e
AM
12458
12459 /* During the second pass toc_first_sec points to the start of
12460 a toc group, and toc_curr is used to track the old elf_gp.
12461 We use toc_bfd to ensure we only look at each bfd once. */
12462 if (htab->toc_bfd == isec->owner)
12463 return TRUE;
12464 htab->toc_bfd = isec->owner;
12465
12466 if (htab->toc_first_sec == NULL
12467 || htab->toc_curr != elf_gp (isec->owner))
12468 {
12469 htab->toc_curr = elf_gp (isec->owner);
12470 htab->toc_first_sec = isec;
12471 }
12472 addr = (htab->toc_first_sec->output_offset
12473 + htab->toc_first_sec->output_section->vma);
06bcf541 12474 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
12475 elf_gp (isec->owner) = off;
12476
12477 return TRUE;
ad8e1ba5
AM
12478}
12479
927be08e
AM
12480/* Called via elf_link_hash_traverse to merge GOT entries for global
12481 symbol H. */
12482
12483static bfd_boolean
12484merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12485{
12486 if (h->root.type == bfd_link_hash_indirect)
12487 return TRUE;
12488
927be08e
AM
12489 merge_got_entries (&h->got.glist);
12490
12491 return TRUE;
12492}
12493
12494/* Called via elf_link_hash_traverse to allocate GOT entries for global
12495 symbol H. */
12496
12497static bfd_boolean
12498reallocate_got (struct elf_link_hash_entry *h, void *inf)
12499{
12500 struct got_entry *gent;
12501
12502 if (h->root.type == bfd_link_hash_indirect)
12503 return TRUE;
12504
927be08e
AM
12505 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12506 if (!gent->is_indirect)
12507 allocate_got (h, (struct bfd_link_info *) inf, gent);
12508 return TRUE;
12509}
12510
12511/* Called on the first multitoc pass after the last call to
12512 ppc64_elf_next_toc_section. This function removes duplicate GOT
12513 entries. */
12514
12515bfd_boolean
12516ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12517{
12518 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
12519 struct bfd *ibfd, *ibfd2;
12520 bfd_boolean done_something;
12521
12522 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12523
7865406b
AM
12524 if (!htab->do_multi_toc)
12525 return FALSE;
12526
d0fae19d 12527 /* Merge global sym got entries within a toc group. */
927be08e
AM
12528 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12529
12530 /* And tlsld_got. */
c72f2fb2 12531 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12532 {
12533 struct got_entry *ent, *ent2;
12534
12535 if (!is_ppc64_elf (ibfd))
12536 continue;
12537
12538 ent = ppc64_tlsld_got (ibfd);
12539 if (!ent->is_indirect
12540 && ent->got.offset != (bfd_vma) -1)
12541 {
c72f2fb2 12542 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12543 {
12544 if (!is_ppc64_elf (ibfd2))
12545 continue;
12546
12547 ent2 = ppc64_tlsld_got (ibfd2);
12548 if (!ent2->is_indirect
12549 && ent2->got.offset != (bfd_vma) -1
12550 && elf_gp (ibfd2) == elf_gp (ibfd))
12551 {
12552 ent2->is_indirect = TRUE;
12553 ent2->got.ent = ent;
12554 }
12555 }
12556 }
12557 }
12558
12559 /* Zap sizes of got sections. */
33e44f2e
AM
12560 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12561 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12562 htab->got_reli_size = 0;
12563
c72f2fb2 12564 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12565 {
12566 asection *got, *relgot;
12567
12568 if (!is_ppc64_elf (ibfd))
12569 continue;
12570
12571 got = ppc64_elf_tdata (ibfd)->got;
12572 if (got != NULL)
12573 {
12574 got->rawsize = got->size;
12575 got->size = 0;
12576 relgot = ppc64_elf_tdata (ibfd)->relgot;
12577 relgot->rawsize = relgot->size;
12578 relgot->size = 0;
12579 }
12580 }
12581
12582 /* Now reallocate the got, local syms first. We don't need to
12583 allocate section contents again since we never increase size. */
c72f2fb2 12584 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12585 {
12586 struct got_entry **lgot_ents;
12587 struct got_entry **end_lgot_ents;
12588 struct plt_entry **local_plt;
12589 struct plt_entry **end_local_plt;
f961d9dd 12590 unsigned char *lgot_masks;
927be08e
AM
12591 bfd_size_type locsymcount;
12592 Elf_Internal_Shdr *symtab_hdr;
19e08130 12593 asection *s;
927be08e
AM
12594
12595 if (!is_ppc64_elf (ibfd))
12596 continue;
12597
12598 lgot_ents = elf_local_got_ents (ibfd);
12599 if (!lgot_ents)
12600 continue;
12601
12602 symtab_hdr = &elf_symtab_hdr (ibfd);
12603 locsymcount = symtab_hdr->sh_info;
12604 end_lgot_ents = lgot_ents + locsymcount;
12605 local_plt = (struct plt_entry **) end_lgot_ents;
12606 end_local_plt = local_plt + locsymcount;
f961d9dd 12607 lgot_masks = (unsigned char *) end_local_plt;
927be08e 12608 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
12609 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12610 {
12611 struct got_entry *ent;
12612
12613 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12614 {
19e08130
AM
12615 unsigned int ent_size = 8;
12616 unsigned int rel_size = sizeof (Elf64_External_Rela);
12617
d0fae19d
AM
12618 ent->got.offset = s->size;
12619 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12620 {
19e08130
AM
12621 ent_size *= 2;
12622 rel_size *= 2;
12623 }
12624 s->size += ent_size;
37da22e5 12625 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12626 {
33e44f2e 12627 htab->elf.irelplt->size += rel_size;
19e08130
AM
12628 htab->got_reli_size += rel_size;
12629 }
6a3858a6 12630 else if (bfd_link_pic (info)
f749f26e 12631 && !(ent->tls_type != 0
6a3858a6 12632 && bfd_link_executable (info)))
19e08130
AM
12633 {
12634 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12635 srel->size += rel_size;
d0fae19d
AM
12636 }
12637 }
927be08e
AM
12638 }
12639 }
12640
12641 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12642
c72f2fb2 12643 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12644 {
12645 struct got_entry *ent;
12646
12647 if (!is_ppc64_elf (ibfd))
12648 continue;
12649
12650 ent = ppc64_tlsld_got (ibfd);
12651 if (!ent->is_indirect
12652 && ent->got.offset != (bfd_vma) -1)
12653 {
12654 asection *s = ppc64_elf_tdata (ibfd)->got;
12655 ent->got.offset = s->size;
12656 s->size += 16;
f749f26e 12657 if (bfd_link_dll (info))
927be08e
AM
12658 {
12659 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12660 srel->size += sizeof (Elf64_External_Rela);
12661 }
12662 }
12663 }
12664
33e44f2e 12665 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12666 if (!done_something)
c72f2fb2 12667 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12668 {
12669 asection *got;
12670
12671 if (!is_ppc64_elf (ibfd))
12672 continue;
12673
12674 got = ppc64_elf_tdata (ibfd)->got;
12675 if (got != NULL)
12676 {
12677 done_something = got->rawsize != got->size;
12678 if (done_something)
12679 break;
12680 }
12681 }
12682
12683 if (done_something)
e7d1c40c 12684 (*htab->params->layout_sections_again) ();
927be08e
AM
12685
12686 /* Set up for second pass over toc sections to recalculate elf_gp
12687 on input sections. */
12688 htab->toc_bfd = NULL;
12689 htab->toc_first_sec = NULL;
12690 htab->second_toc_pass = TRUE;
12691 return done_something;
12692}
12693
12694/* Called after second pass of multitoc partitioning. */
12695
12696void
12697ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12698{
12699 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12700
12701 /* After the second pass, toc_curr tracks the TOC offset used
12702 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12703 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12704}
12705
9b5ecbd0
AM
12706/* No toc references were found in ISEC. If the code in ISEC makes no
12707 calls, then there's no need to use toc adjusting stubs when branching
12708 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12709 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12710 needed, and 2 if a cyclical call-graph was found but no other reason
12711 for a stub was detected. If called from the top level, a return of
12712 2 means the same as a return of 0. */
9b5ecbd0
AM
12713
12714static int
4ce794b7 12715toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12716{
9b5ecbd0 12717 int ret;
70cc837d
AM
12718
12719 /* Mark this section as checked. */
12720 isec->call_check_done = 1;
9b5ecbd0 12721
772119ce
AM
12722 /* We know none of our code bearing sections will need toc stubs. */
12723 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12724 return 0;
12725
eea6121a 12726 if (isec->size == 0)
082c50f8
AM
12727 return 0;
12728
4c52953f
AM
12729 if (isec->output_section == NULL)
12730 return 0;
12731
4c52953f 12732 ret = 0;
70cc837d 12733 if (isec->reloc_count != 0)
9b5ecbd0 12734 {
70cc837d
AM
12735 Elf_Internal_Rela *relstart, *rel;
12736 Elf_Internal_Sym *local_syms;
12737 struct ppc_link_hash_table *htab;
2917689a 12738
70cc837d
AM
12739 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12740 info->keep_memory);
12741 if (relstart == NULL)
12742 return -1;
90aecf7a 12743
70cc837d
AM
12744 /* Look for branches to outside of this section. */
12745 local_syms = NULL;
12746 htab = ppc_hash_table (info);
12747 if (htab == NULL)
12748 return -1;
4c52953f 12749
70cc837d 12750 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12751 {
70cc837d
AM
12752 enum elf_ppc64_reloc_type r_type;
12753 unsigned long r_symndx;
12754 struct elf_link_hash_entry *h;
12755 struct ppc_link_hash_entry *eh;
12756 Elf_Internal_Sym *sym;
12757 asection *sym_sec;
12758 struct _opd_sec_data *opd;
12759 bfd_vma sym_value;
12760 bfd_vma dest;
12761
12762 r_type = ELF64_R_TYPE (rel->r_info);
12763 if (r_type != R_PPC64_REL24
05d0e962 12764 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
12765 && r_type != R_PPC64_REL14
12766 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 12767 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
12768 && r_type != R_PPC64_PLTCALL
12769 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 12770 continue;
4c52953f 12771
70cc837d
AM
12772 r_symndx = ELF64_R_SYM (rel->r_info);
12773 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12774 isec->owner))
4c52953f 12775 {
70cc837d
AM
12776 ret = -1;
12777 break;
12778 }
4c52953f 12779
70cc837d
AM
12780 /* Calls to dynamic lib functions go through a plt call stub
12781 that uses r2. */
ed7007c1 12782 eh = ppc_elf_hash_entry (h);
70cc837d
AM
12783 if (eh != NULL
12784 && (eh->elf.plt.plist != NULL
12785 || (eh->oh != NULL
12786 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12787 {
12788 ret = 1;
12789 break;
4c52953f
AM
12790 }
12791
70cc837d
AM
12792 if (sym_sec == NULL)
12793 /* Ignore other undefined symbols. */
4c52953f 12794 continue;
4c52953f 12795
70cc837d
AM
12796 /* Assume branches to other sections not included in the
12797 link need stubs too, to cover -R and absolute syms. */
12798 if (sym_sec->output_section == NULL)
12799 {
12800 ret = 1;
12801 break;
12802 }
4c52953f 12803
70cc837d
AM
12804 if (h == NULL)
12805 sym_value = sym->st_value;
12806 else
12807 {
12808 if (h->root.type != bfd_link_hash_defined
12809 && h->root.type != bfd_link_hash_defweak)
12810 abort ();
12811 sym_value = h->root.u.def.value;
12812 }
12813 sym_value += rel->r_addend;
4c52953f 12814
70cc837d
AM
12815 /* If this branch reloc uses an opd sym, find the code section. */
12816 opd = get_opd_info (sym_sec);
12817 if (opd != NULL)
12818 {
12819 if (h == NULL && opd->adjust != NULL)
12820 {
12821 long adjust;
4c52953f 12822
92a9c616 12823 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12824 if (adjust == -1)
12825 /* Assume deleted functions won't ever be called. */
12826 continue;
12827 sym_value += adjust;
12828 }
4c52953f 12829
aef36ac1
AM
12830 dest = opd_entry_value (sym_sec, sym_value,
12831 &sym_sec, NULL, FALSE);
70cc837d
AM
12832 if (dest == (bfd_vma) -1)
12833 continue;
12834 }
12835 else
12836 dest = (sym_value
12837 + sym_sec->output_offset
12838 + sym_sec->output_section->vma);
4c52953f 12839
70cc837d
AM
12840 /* Ignore branch to self. */
12841 if (sym_sec == isec)
12842 continue;
4c52953f 12843
70cc837d
AM
12844 /* If the called function uses the toc, we need a stub. */
12845 if (sym_sec->has_toc_reloc
12846 || sym_sec->makes_toc_func_call)
4c52953f 12847 {
70cc837d 12848 ret = 1;
4c52953f
AM
12849 break;
12850 }
70cc837d
AM
12851
12852 /* Assume any branch that needs a long branch stub might in fact
12853 need a plt_branch stub. A plt_branch stub uses r2. */
12854 else if (dest - (isec->output_offset
12855 + isec->output_section->vma
6911b7dc
AM
12856 + rel->r_offset) + (1 << 25)
12857 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12858 ? h->other
12859 : sym->st_other))
4c52953f 12860 {
70cc837d
AM
12861 ret = 1;
12862 break;
12863 }
12864
12865 /* If calling back to a section in the process of being
12866 tested, we can't say for sure that no toc adjusting stubs
12867 are needed, so don't return zero. */
12868 else if (sym_sec->call_check_in_progress)
12869 ret = 2;
12870
12871 /* Branches to another section that itself doesn't have any TOC
12872 references are OK. Recursively call ourselves to check. */
12873 else if (!sym_sec->call_check_done)
12874 {
12875 int recur;
12876
12877 /* Mark current section as indeterminate, so that other
12878 sections that call back to current won't be marked as
12879 known. */
12880 isec->call_check_in_progress = 1;
12881 recur = toc_adjusting_stub_needed (info, sym_sec);
12882 isec->call_check_in_progress = 0;
12883
4c52953f
AM
12884 if (recur != 0)
12885 {
70cc837d
AM
12886 ret = recur;
12887 if (recur != 2)
12888 break;
4c52953f
AM
12889 }
12890 }
4c52953f 12891 }
70cc837d 12892
c9594989
AM
12893 if (elf_symtab_hdr (isec->owner).contents
12894 != (unsigned char *) local_syms)
70cc837d
AM
12895 free (local_syms);
12896 if (elf_section_data (isec)->relocs != relstart)
12897 free (relstart);
9b5ecbd0
AM
12898 }
12899
70cc837d
AM
12900 if ((ret & 1) == 0
12901 && isec->map_head.s != NULL
12902 && (strcmp (isec->output_section->name, ".init") == 0
12903 || strcmp (isec->output_section->name, ".fini") == 0))
12904 {
12905 if (isec->map_head.s->has_toc_reloc
12906 || isec->map_head.s->makes_toc_func_call)
12907 ret = 1;
12908 else if (!isec->map_head.s->call_check_done)
12909 {
12910 int recur;
12911 isec->call_check_in_progress = 1;
12912 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12913 isec->call_check_in_progress = 0;
12914 if (recur != 0)
12915 ret = recur;
12916 }
12917 }
12918
12919 if (ret == 1)
12920 isec->makes_toc_func_call = 1;
4c52953f 12921
9b5ecbd0
AM
12922 return ret;
12923}
12924
721956f4
AM
12925/* The linker repeatedly calls this function for each input section,
12926 in the order that input sections are linked into output sections.
12927 Build lists of input sections to determine groupings between which
12928 we may insert linker stubs. */
12929
9b5ecbd0 12930bfd_boolean
4ce794b7 12931ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12932{
12933 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12934
4dfe6ac6
NC
12935 if (htab == NULL)
12936 return FALSE;
12937
734b6cf9 12938 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12939 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12940 {
3d6f9012
AM
12941 /* This happens to make the list in reverse order,
12942 which is what we want. */
6f20ed8a
AM
12943 htab->sec_info[isec->id].u.list
12944 = htab->sec_info[isec->output_section->id].u.list;
12945 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12946 }
ad8e1ba5 12947
4c52953f 12948 if (htab->multi_toc_needed)
9b5ecbd0 12949 {
8b974ba3
AM
12950 /* Analyse sections that aren't already flagged as needing a
12951 valid toc pointer. Exclude .fixup for the linux kernel.
12952 .fixup contains branches, but only back to the function that
12953 hit an exception. */
12954 if (!(isec->has_toc_reloc
12955 || (isec->flags & SEC_CODE) == 0
12956 || strcmp (isec->name, ".fixup") == 0
12957 || isec->call_check_done))
12958 {
12959 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12960 return FALSE;
8b974ba3
AM
12961 }
12962 /* Make all sections use the TOC assigned for this object file.
12963 This will be wrong for pasted sections; We fix that in
12964 check_pasted_section(). */
12965 if (elf_gp (isec->owner) != 0)
12966 htab->toc_curr = elf_gp (isec->owner);
12967 }
12968
6f20ed8a 12969 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12970 return TRUE;
721956f4
AM
12971}
12972
70cc837d
AM
12973/* Check that all .init and .fini sections use the same toc, if they
12974 have toc relocs. */
12975
12976static bfd_boolean
12977check_pasted_section (struct bfd_link_info *info, const char *name)
12978{
12979 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12980
12981 if (o != NULL)
12982 {
12983 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12984 bfd_vma toc_off = 0;
12985 asection *i;
12986
12987 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12988 if (i->has_toc_reloc)
12989 {
12990 if (toc_off == 0)
6f20ed8a
AM
12991 toc_off = htab->sec_info[i->id].toc_off;
12992 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12993 return FALSE;
12994 }
6683a28d
AM
12995
12996 if (toc_off == 0)
12997 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12998 if (i->makes_toc_func_call)
12999 {
6f20ed8a 13000 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
13001 break;
13002 }
13003
70cc837d
AM
13004 /* Make sure the whole pasted function uses the same toc offset. */
13005 if (toc_off != 0)
13006 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 13007 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
13008 }
13009 return TRUE;
13010}
13011
13012bfd_boolean
13013ppc64_elf_check_init_fini (struct bfd_link_info *info)
13014{
13015 return (check_pasted_section (info, ".init")
13016 & check_pasted_section (info, ".fini"));
13017}
13018
721956f4
AM
13019/* See whether we can group stub sections together. Grouping stub
13020 sections may result in fewer stubs. More importantly, we need to
13021 put all .init* and .fini* stubs at the beginning of the .init or
13022 .fini output sections respectively, because glibc splits the
13023 _init and _fini functions into multiple parts. Putting a stub in
13024 the middle of a function is not a good idea. */
13025
6f20ed8a
AM
13026static bfd_boolean
13027group_sections (struct bfd_link_info *info,
4ce794b7
AM
13028 bfd_size_type stub_group_size,
13029 bfd_boolean stubs_always_before_branch)
721956f4 13030{
6f20ed8a
AM
13031 struct ppc_link_hash_table *htab;
13032 asection *osec;
7c8fe5c4
AM
13033 bfd_boolean suppress_size_errors;
13034
6f20ed8a
AM
13035 htab = ppc_hash_table (info);
13036 if (htab == NULL)
13037 return FALSE;
13038
7c8fe5c4 13039 suppress_size_errors = FALSE;
7c8fe5c4
AM
13040 if (stub_group_size == 1)
13041 {
13042 /* Default values. */
13043 if (stubs_always_before_branch)
09f92717 13044 stub_group_size = 0x1e00000;
7c8fe5c4 13045 else
09f92717 13046 stub_group_size = 0x1c00000;
7c8fe5c4
AM
13047 suppress_size_errors = TRUE;
13048 }
13049
6f20ed8a 13050 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 13051 {
6f20ed8a
AM
13052 asection *tail;
13053
13054 if (osec->id >= htab->sec_info_arr_size)
13055 continue;
13056
13057 tail = htab->sec_info[osec->id].u.list;
734b6cf9 13058 while (tail != NULL)
721956f4 13059 {
734b6cf9
AM
13060 asection *curr;
13061 asection *prev;
13062 bfd_size_type total;
13063 bfd_boolean big_sec;
13064 bfd_vma curr_toc;
6f20ed8a 13065 struct map_stub *group;
09f92717 13066 bfd_size_type group_size;
734b6cf9
AM
13067
13068 curr = tail;
eea6121a 13069 total = tail->size;
09f92717
AM
13070 group_size = (ppc64_elf_section_data (tail) != NULL
13071 && ppc64_elf_section_data (tail)->has_14bit_branch
13072 ? stub_group_size >> 10 : stub_group_size);
13073
13074 big_sec = total > group_size;
7c8fe5c4 13075 if (big_sec && !suppress_size_errors)
695344c0 13076 /* xgettext:c-format */
871b3ab2 13077 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 13078 tail->owner, tail);
6f20ed8a 13079 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 13080
6f20ed8a 13081 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 13082 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
13083 < (ppc64_elf_section_data (prev) != NULL
13084 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 13085 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 13086 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13087 curr = prev;
13088
13089 /* OK, the size from the start of CURR to the end is less
09f92717 13090 than group_size and thus can be handled by one stub
734b6cf9 13091 section. (or the tail section is itself larger than
09f92717
AM
13092 group_size, in which case we may be toast.) We should
13093 really be keeping track of the total size of stubs added
13094 here, as stubs contribute to the final output section
13095 size. That's a little tricky, and this way will only
13096 break if stubs added make the total size more than 2^25,
13097 ie. for the default stub_group_size, if stubs total more
13098 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
13099 group = bfd_alloc (curr->owner, sizeof (*group));
13100 if (group == NULL)
13101 return FALSE;
13102 group->link_sec = curr;
13103 group->stub_sec = NULL;
a4b6fadd 13104 group->needs_save_res = 0;
df136d64
AM
13105 group->lr_restore = 0;
13106 group->eh_size = 0;
13107 group->eh_base = 0;
a4b6fadd
AM
13108 group->next = htab->group;
13109 htab->group = group;
734b6cf9 13110 do
721956f4 13111 {
6f20ed8a 13112 prev = htab->sec_info[tail->id].u.list;
734b6cf9 13113 /* Set up this stub group. */
6f20ed8a 13114 htab->sec_info[tail->id].u.group = group;
721956f4 13115 }
734b6cf9
AM
13116 while (tail != curr && (tail = prev) != NULL);
13117
09f92717 13118 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
13119 bytes before the stub section can be handled by it too.
13120 Don't do this if we have a really large section after the
13121 stubs, as adding more stubs increases the chance that
13122 branches may not reach into the stub section. */
13123 if (!stubs_always_before_branch && !big_sec)
13124 {
13125 total = 0;
13126 while (prev != NULL
13127 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
13128 < (ppc64_elf_section_data (prev) != NULL
13129 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
13130 ? (group_size = stub_group_size >> 10)
13131 : group_size))
6f20ed8a 13132 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13133 {
13134 tail = prev;
6f20ed8a
AM
13135 prev = htab->sec_info[tail->id].u.list;
13136 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
13137 }
13138 }
13139 tail = prev;
721956f4
AM
13140 }
13141 }
6f20ed8a 13142 return TRUE;
721956f4
AM
13143}
13144
58d180e8
AM
13145static const unsigned char glink_eh_frame_cie[] =
13146{
13147 0, 0, 0, 16, /* length. */
13148 0, 0, 0, 0, /* id. */
13149 1, /* CIE version. */
13150 'z', 'R', 0, /* Augmentation string. */
13151 4, /* Code alignment. */
13152 0x78, /* Data alignment. */
13153 65, /* RA reg. */
13154 1, /* Augmentation size. */
13155 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 13156 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
13157};
13158
d969d15f
AM
13159/* Stripping output sections is normally done before dynamic section
13160 symbols have been allocated. This function is called later, and
13161 handles cases like htab->brlt which is mapped to its own output
13162 section. */
13163
13164static void
13165maybe_strip_output (struct bfd_link_info *info, asection *isec)
13166{
13167 if (isec->size == 0
13168 && isec->output_section->size == 0
53d8967a 13169 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
13170 && !bfd_section_removed_from_list (info->output_bfd,
13171 isec->output_section)
13172 && elf_section_data (isec->output_section)->dynindx == 0)
13173 {
13174 isec->output_section->flags |= SEC_EXCLUDE;
13175 bfd_section_list_remove (info->output_bfd, isec->output_section);
13176 info->output_bfd->section_count--;
13177 }
13178}
13179
721956f4
AM
13180/* Determine and set the size of the stub section for a final link.
13181
13182 The basic idea here is to examine all the relocations looking for
13183 PC-relative calls to a target that is unreachable with a "bl"
13184 instruction. */
13185
b34976b6 13186bfd_boolean
e7d1c40c 13187ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
13188{
13189 bfd_size_type stub_group_size;
b34976b6 13190 bfd_boolean stubs_always_before_branch;
721956f4
AM
13191 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13192
4dfe6ac6
NC
13193 if (htab == NULL)
13194 return FALSE;
13195
e10a07b3
AM
13196 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13197 htab->params->power10_stubs = 0;
13198
0e1862bb 13199 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 13200 htab->params->plt_thread_safe = 1;
b9e5796b 13201 if (!htab->opd_abi)
e7d1c40c
AM
13202 htab->params->plt_thread_safe = 0;
13203 else if (htab->params->plt_thread_safe == -1)
794e51c0 13204 {
e2458743 13205 static const char *const thread_starter[] =
794e51c0
AM
13206 {
13207 "pthread_create",
13208 /* libstdc++ */
13209 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13210 /* librt */
13211 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13212 "mq_notify", "create_timer",
13213 /* libanl */
13214 "getaddrinfo_a",
13215 /* libgomp */
2300b5a1 13216 "GOMP_parallel",
794e51c0 13217 "GOMP_parallel_start",
2300b5a1 13218 "GOMP_parallel_loop_static",
794e51c0 13219 "GOMP_parallel_loop_static_start",
2300b5a1 13220 "GOMP_parallel_loop_dynamic",
794e51c0 13221 "GOMP_parallel_loop_dynamic_start",
2300b5a1 13222 "GOMP_parallel_loop_guided",
794e51c0 13223 "GOMP_parallel_loop_guided_start",
2300b5a1 13224 "GOMP_parallel_loop_runtime",
794e51c0 13225 "GOMP_parallel_loop_runtime_start",
2300b5a1 13226 "GOMP_parallel_sections",
68ffbac6 13227 "GOMP_parallel_sections_start",
f9dffbf0
AM
13228 /* libgo */
13229 "__go_go",
794e51c0
AM
13230 };
13231 unsigned i;
13232
a4b6fadd 13233 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
13234 {
13235 struct elf_link_hash_entry *h;
13236 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13237 FALSE, FALSE, TRUE);
e7d1c40c
AM
13238 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13239 if (htab->params->plt_thread_safe)
794e51c0
AM
13240 break;
13241 }
13242 }
e7d1c40c
AM
13243 stubs_always_before_branch = htab->params->group_size < 0;
13244 if (htab->params->group_size < 0)
13245 stub_group_size = -htab->params->group_size;
721956f4 13246 else
e7d1c40c 13247 stub_group_size = htab->params->group_size;
721956f4 13248
6f20ed8a
AM
13249 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13250 return FALSE;
721956f4 13251
a804e476
AM
13252 htab->tga_group = NULL;
13253 if (!htab->params->no_tls_get_addr_regsave
13254 && htab->tga_desc_fd != NULL
13255 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13256 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13257 && htab->tls_get_addr_fd != NULL
13258 && is_static_defined (&htab->tls_get_addr_fd->elf))
13259 {
13260 asection *sym_sec, *code_sec, *stub_sec;
13261 bfd_vma sym_value;
13262 struct _opd_sec_data *opd;
13263
13264 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13265 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13266 code_sec = sym_sec;
13267 opd = get_opd_info (sym_sec);
13268 if (opd != NULL)
13269 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, FALSE);
13270 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13271 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13272 htab->tga_group->link_sec);
13273 if (stub_sec == NULL)
13274 return FALSE;
13275 htab->tga_group->stub_sec = stub_sec;
13276
13277 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13278 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13279 htab->tga_desc_fd->elf.root.u.def.value = 0;
13280 htab->tga_desc_fd->elf.type = STT_FUNC;
13281 htab->tga_desc_fd->elf.def_regular = 1;
13282 htab->tga_desc_fd->elf.non_elf = 0;
13283 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, TRUE);
13284 }
13285
c9301e31
AM
13286#define STUB_SHRINK_ITER 20
13287 /* Loop until no stubs added. After iteration 20 of this loop we may
13288 exit on a stub section shrinking. This is to break out of a
13289 pathological case where adding stubs on one iteration decreases
13290 section gaps (perhaps due to alignment), which then requires
13291 fewer or smaller stubs on the next iteration. */
13292
721956f4
AM
13293 while (1)
13294 {
13295 bfd *input_bfd;
13296 unsigned int bfd_indx;
a4b6fadd 13297 struct map_stub *group;
721956f4
AM
13298
13299 htab->stub_iteration += 1;
721956f4
AM
13300
13301 for (input_bfd = info->input_bfds, bfd_indx = 0;
13302 input_bfd != NULL;
c72f2fb2 13303 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
13304 {
13305 Elf_Internal_Shdr *symtab_hdr;
13306 asection *section;
6cdc0ccc 13307 Elf_Internal_Sym *local_syms = NULL;
721956f4 13308
0c8d6e5c 13309 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
13310 continue;
13311
721956f4 13312 /* We'll need the symbol table in a second. */
0ffa91dd 13313 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
13314 if (symtab_hdr->sh_info == 0)
13315 continue;
13316
721956f4
AM
13317 /* Walk over each section attached to the input bfd. */
13318 for (section = input_bfd->sections;
13319 section != NULL;
13320 section = section->next)
13321 {
721956f4 13322 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
13323
13324 /* If there aren't any relocs, then there's nothing more
13325 to do. */
13326 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
13327 || (section->flags & SEC_ALLOC) == 0
13328 || (section->flags & SEC_LOAD) == 0
13329 || (section->flags & SEC_CODE) == 0
721956f4
AM
13330 || section->reloc_count == 0)
13331 continue;
13332
13333 /* If this section is a link-once section that will be
13334 discarded, then don't create any stubs. */
13335 if (section->output_section == NULL
927be08e 13336 || section->output_section->owner != info->output_bfd)
721956f4
AM
13337 continue;
13338
1e2f5b6e
AM
13339 /* Get the relocs. */
13340 internal_relocs
4ce794b7 13341 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 13342 info->keep_memory);
721956f4 13343 if (internal_relocs == NULL)
1e2f5b6e 13344 goto error_ret_free_local;
721956f4
AM
13345
13346 /* Now examine each relocation. */
13347 irela = internal_relocs;
13348 irelaend = irela + section->reloc_count;
13349 for (; irela < irelaend; irela++)
13350 {
4ce794b7
AM
13351 enum elf_ppc64_reloc_type r_type;
13352 unsigned int r_indx;
721956f4
AM
13353 enum ppc_stub_type stub_type;
13354 struct ppc_stub_hash_entry *stub_entry;
8387904d 13355 asection *sym_sec, *code_sec;
e054468f 13356 bfd_vma sym_value, code_value;
721956f4 13357 bfd_vma destination;
6911b7dc 13358 unsigned long local_off;
8843416a 13359 bfd_boolean ok_dest;
721956f4 13360 struct ppc_link_hash_entry *hash;
8387904d 13361 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
13362 struct elf_link_hash_entry *h;
13363 Elf_Internal_Sym *sym;
721956f4
AM
13364 char *stub_name;
13365 const asection *id_sec;
74f0fb50 13366 struct _opd_sec_data *opd;
e054468f 13367 struct plt_entry *plt_ent;
721956f4
AM
13368
13369 r_type = ELF64_R_TYPE (irela->r_info);
13370 r_indx = ELF64_R_SYM (irela->r_info);
13371
4ce794b7 13372 if (r_type >= R_PPC64_max)
721956f4
AM
13373 {
13374 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 13375 goto error_ret_free_internal;
721956f4
AM
13376 }
13377
13378 /* Only look for stubs on branch instructions. */
4ce794b7 13379 if (r_type != R_PPC64_REL24
05d0e962 13380 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
13381 && r_type != R_PPC64_REL14
13382 && r_type != R_PPC64_REL14_BRTAKEN
13383 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
13384 continue;
13385
13386 /* Now determine the call target, its name, value,
13387 section. */
411e1bfb
AM
13388 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13389 r_indx, input_bfd))
13390 goto error_ret_free_internal;
ed7007c1 13391 hash = ppc_elf_hash_entry (h);
411e1bfb 13392
8843416a 13393 ok_dest = FALSE;
8387904d 13394 fdh = NULL;
7fe2b9a6 13395 sym_value = 0;
411e1bfb 13396 if (hash == NULL)
721956f4 13397 {
411e1bfb 13398 sym_value = sym->st_value;
c27b8c2a
AM
13399 if (sym_sec != NULL
13400 && sym_sec->output_section != NULL)
13401 ok_dest = TRUE;
721956f4 13402 }
7fe2b9a6
AM
13403 else if (hash->elf.root.type == bfd_link_hash_defined
13404 || hash->elf.root.type == bfd_link_hash_defweak)
13405 {
13406 sym_value = hash->elf.root.u.def.value;
13407 if (sym_sec->output_section != NULL)
13408 ok_dest = TRUE;
13409 }
13410 else if (hash->elf.root.type == bfd_link_hash_undefweak
13411 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 13412 {
99877b66 13413 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
13414 use the func descriptor sym instead if it is
13415 defined. */
ceb1f1ef 13416 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 13417 && hash->oh != NULL)
8387904d 13418 {
8c5b4e52 13419 fdh = ppc_follow_link (hash->oh);
8387904d
AM
13420 if (fdh->elf.root.type == bfd_link_hash_defined
13421 || fdh->elf.root.type == bfd_link_hash_defweak)
13422 {
13423 sym_sec = fdh->elf.root.u.def.section;
13424 sym_value = fdh->elf.root.u.def.value;
13425 if (sym_sec->output_section != NULL)
13426 ok_dest = TRUE;
13427 }
99877b66
AM
13428 else
13429 fdh = NULL;
8387904d 13430 }
7fe2b9a6
AM
13431 }
13432 else
13433 {
13434 bfd_set_error (bfd_error_bad_value);
13435 goto error_ret_free_internal;
721956f4
AM
13436 }
13437
8843416a 13438 destination = 0;
6911b7dc 13439 local_off = 0;
8843416a
AM
13440 if (ok_dest)
13441 {
13442 sym_value += irela->r_addend;
13443 destination = (sym_value
13444 + sym_sec->output_offset
13445 + sym_sec->output_section->vma);
6911b7dc
AM
13446 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13447 ? hash->elf.other
13448 : sym->st_other);
8843416a
AM
13449 }
13450
8387904d 13451 code_sec = sym_sec;
e054468f 13452 code_value = sym_value;
74f0fb50
AM
13453 opd = get_opd_info (sym_sec);
13454 if (opd != NULL)
8387904d
AM
13455 {
13456 bfd_vma dest;
13457
74f0fb50 13458 if (hash == NULL && opd->adjust != NULL)
8387904d 13459 {
51aecdc5 13460 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
13461 if (adjust == -1)
13462 continue;
e054468f 13463 code_value += adjust;
8387904d
AM
13464 sym_value += adjust;
13465 }
13466 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 13467 &code_sec, &code_value, FALSE);
8387904d
AM
13468 if (dest != (bfd_vma) -1)
13469 {
13470 destination = dest;
13471 if (fdh != NULL)
13472 {
13473 /* Fixup old ABI sym to point at code
13474 entry. */
99877b66 13475 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 13476 hash->elf.root.u.def.section = code_sec;
e054468f 13477 hash->elf.root.u.def.value = code_value;
8387904d
AM
13478 }
13479 }
13480 }
13481
721956f4 13482 /* Determine what (if any) linker stub is needed. */
e054468f 13483 plt_ent = NULL;
721956f4 13484 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
13485 &plt_ent, destination,
13486 local_off);
ad8e1ba5 13487
05d0e962
AM
13488 if (r_type == R_PPC64_REL24_NOTOC)
13489 {
13490 if (stub_type == ppc_stub_plt_call)
13491 stub_type = ppc_stub_plt_call_notoc;
13492 else if (stub_type == ppc_stub_long_branch
13493 || (code_sec != NULL
13494 && code_sec->output_section != NULL
13495 && (((hash ? hash->elf.other : sym->st_other)
13496 & STO_PPC64_LOCAL_MASK)
4a4e7361 13497 > 1 << STO_PPC64_LOCAL_BIT)))
05d0e962
AM
13498 stub_type = ppc_stub_long_branch_notoc;
13499 }
13500 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
13501 {
13502 /* Check whether we need a TOC adjusting stub.
13503 Since the linker pastes together pieces from
13504 different object files when creating the
13505 _init and _fini functions, it may be that a
13506 call to what looks like a local sym is in
13507 fact a call needing a TOC adjustment. */
33cb30a1
AM
13508 if ((code_sec != NULL
13509 && code_sec->output_section != NULL
33cb30a1 13510 && (code_sec->has_toc_reloc
918dc783
AM
13511 || code_sec->makes_toc_func_call)
13512 && (htab->sec_info[code_sec->id].toc_off
13513 != htab->sec_info[section->id].toc_off))
33cb30a1
AM
13514 || (((hash ? hash->elf.other : sym->st_other)
13515 & STO_PPC64_LOCAL_MASK)
13516 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
13517 stub_type = ppc_stub_long_branch_r2off;
13518 }
13519
721956f4
AM
13520 if (stub_type == ppc_stub_none)
13521 continue;
13522
411e1bfb
AM
13523 /* __tls_get_addr calls might be eliminated. */
13524 if (stub_type != ppc_stub_plt_call
05d0e962 13525 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 13526 && hash != NULL
ed7007c1 13527 && is_tls_get_addr (&hash->elf, htab)
411e1bfb
AM
13528 && section->has_tls_reloc
13529 && irela != internal_relocs)
13530 {
13531 /* Get tls info. */
f961d9dd 13532 unsigned char *tls_mask;
411e1bfb 13533
3a71aa26 13534 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
13535 irela - 1, input_bfd))
13536 goto error_ret_free_internal;
abc489c6
AM
13537 if ((*tls_mask & TLS_TLS) != 0
13538 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
411e1bfb
AM
13539 continue;
13540 }
13541
f378ab09 13542 if (stub_type == ppc_stub_plt_call)
794e51c0 13543 {
6e1816be
AM
13544 if (!htab->opd_abi
13545 && htab->params->plt_localentry0 != 0
13546 && is_elfv2_localentry0 (&hash->elf))
13547 htab->has_plt_localentry0 = 1;
13548 else if (irela + 1 < irelaend
13549 && irela[1].r_offset == irela->r_offset + 4
13550 && (ELF64_R_TYPE (irela[1].r_info)
13551 == R_PPC64_TOCSAVE))
f378ab09
AM
13552 {
13553 if (!tocsave_find (htab, INSERT,
13554 &local_syms, irela + 1, input_bfd))
13555 goto error_ret_free_internal;
13556 }
f378ab09
AM
13557 else
13558 stub_type = ppc_stub_plt_call_r2save;
794e51c0 13559 }
3b421ab3 13560
721956f4 13561 /* Support for grouping stub sections. */
6f20ed8a 13562 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
13563
13564 /* Get the name of this stub. */
13565 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13566 if (!stub_name)
13567 goto error_ret_free_internal;
13568
13569 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 13570 stub_name, FALSE, FALSE);
721956f4
AM
13571 if (stub_entry != NULL)
13572 {
05d0e962 13573 enum ppc_stub_type old_type;
e10a07b3 13574
05d0e962
AM
13575 /* A stub has already been created, but it may
13576 not be the required type. We shouldn't be
13577 transitioning from plt_call to long_branch
13578 stubs or vice versa, but we might be
13579 upgrading from plt_call to plt_call_r2save or
13580 from long_branch to long_branch_r2off. */
721956f4 13581 free (stub_name);
e10a07b3
AM
13582 if (htab->params->power10_stubs == -1)
13583 {
13584 /* For --power10-stubs=auto, don't merge _notoc
13585 and other varieties of stubs. (The _both
13586 variety won't be created.) */
13587 bfd_boolean notoc = r_type == R_PPC64_REL24_NOTOC;
13588 struct ppc_stub_hash_entry *alt_stub
13589 = select_alt_stub (stub_entry, notoc);
13590
13591 if (alt_stub == NULL)
13592 {
13593 alt_stub = (struct ppc_stub_hash_entry *)
13594 stub_hash_newfunc (NULL,
13595 &htab->stub_hash_table,
13596 stub_entry->root.string);
13597 if (alt_stub == NULL)
13598 {
13599 /* xgettext:c-format */
13600 _bfd_error_handler
13601 (_("%pB: cannot create stub entry %s"),
13602 section->owner, stub_entry->root.string);
13603 goto error_ret_free_internal;
13604 }
13605 *alt_stub = *stub_entry;
13606 stub_entry->root.next = &alt_stub->root;
13607 if (notoc)
13608 /* Sort notoc stubs first, for no good
13609 reason. */
13610 alt_stub = stub_entry;
13611 alt_stub->stub_type = stub_type;
13612 }
13613 stub_entry = alt_stub;
13614 }
05d0e962
AM
13615 old_type = stub_entry->stub_type;
13616 switch (old_type)
13617 {
13618 default:
13619 abort ();
13620
13621 case ppc_stub_save_res:
13622 continue;
13623
13624 case ppc_stub_plt_call:
13625 case ppc_stub_plt_call_r2save:
13626 case ppc_stub_plt_call_notoc:
13627 case ppc_stub_plt_call_both:
13628 if (stub_type == ppc_stub_plt_call)
13629 continue;
13630 else if (stub_type == ppc_stub_plt_call_r2save)
13631 {
13632 if (old_type == ppc_stub_plt_call_notoc)
13633 stub_type = ppc_stub_plt_call_both;
13634 }
13635 else if (stub_type == ppc_stub_plt_call_notoc)
13636 {
13637 if (old_type == ppc_stub_plt_call_r2save)
13638 stub_type = ppc_stub_plt_call_both;
13639 }
13640 else
13641 abort ();
13642 break;
13643
13644 case ppc_stub_plt_branch:
13645 case ppc_stub_plt_branch_r2off:
13646 case ppc_stub_plt_branch_notoc:
13647 case ppc_stub_plt_branch_both:
13648 old_type += (ppc_stub_long_branch
13649 - ppc_stub_plt_branch);
13650 /* Fall through. */
13651 case ppc_stub_long_branch:
13652 case ppc_stub_long_branch_r2off:
13653 case ppc_stub_long_branch_notoc:
13654 case ppc_stub_long_branch_both:
13655 if (stub_type == ppc_stub_long_branch)
13656 continue;
13657 else if (stub_type == ppc_stub_long_branch_r2off)
13658 {
13659 if (old_type == ppc_stub_long_branch_notoc)
13660 stub_type = ppc_stub_long_branch_both;
13661 }
13662 else if (stub_type == ppc_stub_long_branch_notoc)
13663 {
13664 if (old_type == ppc_stub_long_branch_r2off)
13665 stub_type = ppc_stub_long_branch_both;
13666 }
13667 else
13668 abort ();
13669 break;
13670 }
13671 if (old_type < stub_type)
794e51c0 13672 stub_entry->stub_type = stub_type;
721956f4
AM
13673 continue;
13674 }
13675
25f53a85 13676 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
13677 if (stub_entry == NULL)
13678 {
13679 free (stub_name);
6cdc0ccc
AM
13680 error_ret_free_internal:
13681 if (elf_section_data (section)->relocs == NULL)
13682 free (internal_relocs);
13683 error_ret_free_local:
c9594989
AM
13684 if (symtab_hdr->contents
13685 != (unsigned char *) local_syms)
6cdc0ccc 13686 free (local_syms);
b34976b6 13687 return FALSE;
721956f4
AM
13688 }
13689
ad8e1ba5 13690 stub_entry->stub_type = stub_type;
05d0e962
AM
13691 if (stub_type >= ppc_stub_plt_call
13692 && stub_type <= ppc_stub_plt_call_both)
e054468f 13693 {
05d0e962
AM
13694 stub_entry->target_value = sym_value;
13695 stub_entry->target_section = sym_sec;
e054468f
AM
13696 }
13697 else
13698 {
05d0e962
AM
13699 stub_entry->target_value = code_value;
13700 stub_entry->target_section = code_sec;
e054468f 13701 }
721956f4 13702 stub_entry->h = hash;
e054468f 13703 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
13704 stub_entry->symtype
13705 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 13706 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 13707
3d58e1fc
AM
13708 if (hash != NULL
13709 && (hash->elf.root.type == bfd_link_hash_defined
13710 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 13711 htab->stub_globals += 1;
721956f4
AM
13712 }
13713
13714 /* We're done with the internal relocs, free them. */
6cdc0ccc 13715 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 13716 free (internal_relocs);
721956f4 13717 }
6cdc0ccc
AM
13718
13719 if (local_syms != NULL
13720 && symtab_hdr->contents != (unsigned char *) local_syms)
13721 {
13722 if (!info->keep_memory)
13723 free (local_syms);
13724 else
13725 symtab_hdr->contents = (unsigned char *) local_syms;
13726 }
721956f4
AM
13727 }
13728
5c3dead3 13729 /* We may have added some stubs. Find out the new size of the
721956f4 13730 stub sections. */
d4aaa2a0 13731 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13732 {
13733 group->lr_restore = 0;
13734 group->eh_size = 0;
13735 if (group->stub_sec != NULL)
13736 {
13737 asection *stub_sec = group->stub_sec;
13738
13739 if (htab->stub_iteration <= STUB_SHRINK_ITER
13740 || stub_sec->rawsize < stub_sec->size)
13741 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13742 stub_sec->rawsize = stub_sec->size;
13743 stub_sec->size = 0;
13744 stub_sec->reloc_count = 0;
13745 stub_sec->flags &= ~SEC_RELOC;
13746 }
13747 }
a804e476
AM
13748 if (htab->tga_group != NULL)
13749 {
13750 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
13751 htab->tga_group->eh_size
13752 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
13753 htab->tga_group->lr_restore = 23 * 4;
13754 htab->tga_group->stub_sec->size = 24 * 4;
13755 }
eea6121a 13756
ba21f564
AM
13757 if (htab->stub_iteration <= STUB_SHRINK_ITER
13758 || htab->brlt->rawsize < htab->brlt->size)
13759 htab->brlt->rawsize = htab->brlt->size;
eea6121a 13760 htab->brlt->size = 0;
84f5d08e
AM
13761 htab->brlt->reloc_count = 0;
13762 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 13763 if (htab->relbrlt != NULL)
eea6121a 13764 htab->relbrlt->size = 0;
721956f4 13765
63bc6f6c 13766 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 13767
a4b6fadd
AM
13768 for (group = htab->group; group != NULL; group = group->next)
13769 if (group->needs_save_res)
13770 group->stub_sec->size += htab->sfpr->size;
13771
176a0d42
AM
13772 if (info->emitrelocations
13773 && htab->glink != NULL && htab->glink->size != 0)
13774 {
13775 htab->glink->reloc_count = 1;
13776 htab->glink->flags |= SEC_RELOC;
13777 }
13778
58d180e8
AM
13779 if (htab->glink_eh_frame != NULL
13780 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 13781 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 13782 {
2e0ce1c8 13783 size_t size = 0, align = 4;
58d180e8 13784
d4aaa2a0 13785 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13786 if (group->eh_size != 0)
13787 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 13788 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 13789 size += (24 + align - 1) & -align;
58d180e8 13790 if (size != 0)
2e0ce1c8
AM
13791 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13792 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13793 size = (size + align - 1) & -align;
58d180e8
AM
13794 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13795 htab->glink_eh_frame->size = size;
13796 }
13797
e7d1c40c 13798 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13799 for (group = htab->group; group != NULL; group = group->next)
13800 if (group->stub_sec != NULL)
691d2e9a
AM
13801 {
13802 int align = abs (htab->params->plt_stub_align);
13803 group->stub_sec->size
13804 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13805 }
d4aaa2a0
AM
13806
13807 for (group = htab->group; group != NULL; group = group->next)
13808 if (group->stub_sec != NULL
13809 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 13810 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 13811 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
13812 break;
13813
d4aaa2a0 13814 if (group == NULL
ba21f564
AM
13815 && (htab->brlt->rawsize == htab->brlt->size
13816 || (htab->stub_iteration > STUB_SHRINK_ITER
13817 && htab->brlt->rawsize > htab->brlt->size))
58d180e8 13818 && (htab->glink_eh_frame == NULL
a804e476
AM
13819 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
13820 && (htab->tga_group == NULL
13821 || htab->stub_iteration > 1))
5c3dead3
AM
13822 break;
13823
721956f4 13824 /* Ask the linker to do its stuff. */
e7d1c40c 13825 (*htab->params->layout_sections_again) ();
721956f4
AM
13826 }
13827
da44f4e5
AM
13828 if (htab->glink_eh_frame != NULL
13829 && htab->glink_eh_frame->size != 0)
13830 {
13831 bfd_vma val;
13832 bfd_byte *p, *last_fde;
13833 size_t last_fde_len, size, align, pad;
d4aaa2a0 13834 struct map_stub *group;
da44f4e5 13835
df136d64
AM
13836 /* It is necessary to at least have a rough outline of the
13837 linker generated CIEs and FDEs written before
13838 bfd_elf_discard_info is run, in order for these FDEs to be
13839 indexed in .eh_frame_hdr. */
da44f4e5
AM
13840 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13841 if (p == NULL)
13842 return FALSE;
13843 htab->glink_eh_frame->contents = p;
13844 last_fde = p;
2e0ce1c8 13845 align = 4;
da44f4e5
AM
13846
13847 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13848 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 13849 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 13850 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 13851 p += last_fde_len + 4;
da44f4e5 13852
d4aaa2a0 13853 for (group = htab->group; group != NULL; group = group->next)
df136d64 13854 if (group->eh_size != 0)
da44f4e5 13855 {
df136d64 13856 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 13857 last_fde = p;
df136d64 13858 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 13859 /* FDE length. */
2e0ce1c8 13860 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13861 p += 4;
13862 /* CIE pointer. */
13863 val = p - htab->glink_eh_frame->contents;
13864 bfd_put_32 (htab->elf.dynobj, val, p);
13865 p += 4;
13866 /* Offset to stub section, written later. */
13867 p += 4;
13868 /* stub section size. */
d4aaa2a0 13869 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
13870 p += 4;
13871 /* Augmentation. */
13872 p += 1;
df136d64
AM
13873 /* Make sure we don't have all nops. This is enough for
13874 elf-eh-frame.c to detect the last non-nop opcode. */
13875 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 13876 p = last_fde + last_fde_len + 4;
da44f4e5
AM
13877 }
13878 if (htab->glink != NULL && htab->glink->size != 0)
13879 {
13880 last_fde = p;
2e0ce1c8 13881 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13882 /* FDE length. */
2e0ce1c8 13883 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13884 p += 4;
13885 /* CIE pointer. */
13886 val = p - htab->glink_eh_frame->contents;
13887 bfd_put_32 (htab->elf.dynobj, val, p);
13888 p += 4;
13889 /* Offset to .glink, written later. */
13890 p += 4;
13891 /* .glink size. */
13892 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13893 p += 4;
13894 /* Augmentation. */
13895 p += 1;
13896
3cd7c7d7 13897 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
da44f4e5
AM
13898 *p++ = DW_CFA_register;
13899 *p++ = 65;
9f08fa5c 13900 *p++ = htab->opd_abi ? 12 : 0;
3cd7c7d7 13901 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
da44f4e5
AM
13902 *p++ = DW_CFA_restore_extended;
13903 *p++ = 65;
2e0ce1c8 13904 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13905 }
13906 /* Subsume any padding into the last FDE if user .eh_frame
13907 sections are aligned more than glink_eh_frame. Otherwise any
13908 zero padding will be seen as a terminator. */
2e0ce1c8 13909 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13910 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13911 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13912 htab->glink_eh_frame->size = size + pad;
13913 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13914 }
13915
d969d15f 13916 maybe_strip_output (info, htab->brlt);
2efec98b
AM
13917 if (htab->relbrlt != NULL)
13918 maybe_strip_output (info, htab->relbrlt);
d969d15f
AM
13919 if (htab->glink_eh_frame != NULL)
13920 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13921
b34976b6 13922 return TRUE;
721956f4
AM
13923}
13924
13925/* Called after we have determined section placement. If sections
805fc799 13926 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13927
805fc799 13928bfd_vma
1c865ab2 13929ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13930{
805fc799 13931 asection *s;
a27e685f 13932 bfd_vma TOCstart, adjust;
721956f4 13933
43417696
AM
13934 if (info != NULL)
13935 {
13936 struct elf_link_hash_entry *h;
13937 struct elf_link_hash_table *htab = elf_hash_table (info);
13938
13939 if (is_elf_hash_table (htab)
13940 && htab->hgot != NULL)
13941 h = htab->hgot;
13942 else
13943 {
13944 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13945 if (is_elf_hash_table (htab))
13946 htab->hgot = h;
13947 }
13948 if (h != NULL
13949 && h->root.type == bfd_link_hash_defined
13950 && !h->root.linker_def
13951 && (!is_elf_hash_table (htab)
13952 || h->def_regular))
13953 {
ed7007c1 13954 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
43417696
AM
13955 _bfd_set_gp_value (obfd, TOCstart);
13956 return TOCstart;
13957 }
13958 }
13959
805fc799
AM
13960 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13961 order. The TOC starts where the first of these sections starts. */
13962 s = bfd_get_section_by_name (obfd, ".got");
e054468f 13963 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13964 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 13965 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13966 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 13967 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13968 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 13969 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
13970 {
13971 /* This may happen for
13972 o references to TOC base (SYM@toc / TOC[tc0]) without a
13973 .toc directive
13974 o bad linker script
13975 o --gc-sections and empty TOC sections
13976
13977 FIXME: Warn user? */
13978
13979 /* Look for a likely section. We probably won't even be
13980 using TOCstart. */
13981 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13982 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13983 | SEC_EXCLUDE))
805fc799
AM
13984 == (SEC_ALLOC | SEC_SMALL_DATA))
13985 break;
721956f4 13986 if (s == NULL)
805fc799 13987 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13988 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
13989 == (SEC_ALLOC | SEC_SMALL_DATA))
13990 break;
721956f4 13991 if (s == NULL)
805fc799 13992 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13993 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13994 == SEC_ALLOC)
805fc799 13995 break;
721956f4 13996 if (s == NULL)
805fc799 13997 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13998 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
13999 break;
14000 }
721956f4 14001
805fc799
AM
14002 TOCstart = 0;
14003 if (s != NULL)
14004 TOCstart = s->output_section->vma + s->output_offset;
721956f4 14005
a27e685f
AM
14006 /* Force alignment. */
14007 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14008 TOCstart -= adjust;
1c865ab2
AM
14009 _bfd_set_gp_value (obfd, TOCstart);
14010
810d4e75 14011 if (info != NULL && s != NULL)
1c865ab2
AM
14012 {
14013 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14014
810d4e75
AM
14015 if (htab != NULL)
14016 {
14017 if (htab->elf.hgot != NULL)
14018 {
a27e685f 14019 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
14020 htab->elf.hgot->root.u.def.section = s;
14021 }
14022 }
14023 else
1c865ab2 14024 {
810d4e75
AM
14025 struct bfd_link_hash_entry *bh = NULL;
14026 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
14027 s, TOC_BASE_OFF - adjust,
14028 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
14029 }
14030 }
805fc799 14031 return TOCstart;
721956f4
AM
14032}
14033
a345bc8d 14034/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 14035 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
14036
14037static bfd_boolean
49c09209 14038build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
14039{
14040 struct bfd_link_info *info;
14041 struct ppc_link_hash_table *htab;
49c09209 14042 struct plt_entry *ent;
a345bc8d
AM
14043 asection *s;
14044
14045 if (h->root.type == bfd_link_hash_indirect)
14046 return TRUE;
14047
49c09209
AM
14048 info = inf;
14049 htab = ppc_hash_table (info);
14050 if (htab == NULL)
14051 return FALSE;
14052
14053 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14054 if (ent->plt.offset != (bfd_vma) -1)
14055 {
14056 /* This symbol has an entry in the procedure linkage
14057 table. Set it up. */
14058 Elf_Internal_Rela rela;
2d7ad24e 14059 asection *plt, *relplt;
49c09209
AM
14060 bfd_byte *loc;
14061
14062 if (!htab->elf.dynamic_sections_created
14063 || h->dynindx == -1)
14064 {
14065 if (!(h->def_regular
14066 && (h->root.type == bfd_link_hash_defined
14067 || h->root.type == bfd_link_hash_defweak)))
14068 continue;
2d7ad24e
AM
14069 if (h->type == STT_GNU_IFUNC)
14070 {
14071 plt = htab->elf.iplt;
14072 relplt = htab->elf.irelplt;
cebd6b8a 14073 htab->elf.ifunc_resolvers = TRUE;
2d7ad24e
AM
14074 if (htab->opd_abi)
14075 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14076 else
14077 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14078 }
49c09209 14079 else
2d7ad24e
AM
14080 {
14081 plt = htab->pltlocal;
14082 if (bfd_link_pic (info))
14083 {
14084 relplt = htab->relpltlocal;
14085 if (htab->opd_abi)
14086 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14087 else
14088 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14089 }
14090 else
14091 relplt = NULL;
14092 }
ed7007c1 14093 rela.r_addend = defined_sym_val (h) + ent->addend;
2d7ad24e
AM
14094
14095 if (relplt == NULL)
14096 {
14097 loc = plt->contents + ent->plt.offset;
14098 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14099 if (htab->opd_abi)
14100 {
14101 bfd_vma toc = elf_gp (info->output_bfd);
14102 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14103 bfd_put_64 (info->output_bfd, toc, loc + 8);
14104 }
14105 }
14106 else
14107 {
14108 rela.r_offset = (plt->output_section->vma
14109 + plt->output_offset
14110 + ent->plt.offset);
14111 loc = relplt->contents + (relplt->reloc_count++
14112 * sizeof (Elf64_External_Rela));
14113 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14114 }
49c09209
AM
14115 }
14116 else
14117 {
14118 rela.r_offset = (htab->elf.splt->output_section->vma
14119 + htab->elf.splt->output_offset
14120 + ent->plt.offset);
14121 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14122 rela.r_addend = ent->addend;
14123 loc = (htab->elf.srelplt->contents
14124 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14125 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14126 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
cebd6b8a 14127 htab->elf.ifunc_resolvers = TRUE;
2d7ad24e 14128 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 14129 }
49c09209
AM
14130 }
14131
a345bc8d
AM
14132 if (!h->pointer_equality_needed)
14133 return TRUE;
14134
14135 if (h->def_regular)
14136 return TRUE;
14137
9e390558 14138 s = htab->global_entry;
49c09209
AM
14139 if (s == NULL || s->size == 0)
14140 return TRUE;
14141
14142 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14143 if (ent->plt.offset != (bfd_vma) -1
14144 && ent->addend == 0)
a345bc8d
AM
14145 {
14146 bfd_byte *p;
14147 asection *plt;
14148 bfd_vma off;
14149
a345bc8d 14150 p = s->contents + h->root.u.def.value;
33e44f2e 14151 plt = htab->elf.splt;
a345bc8d
AM
14152 if (!htab->elf.dynamic_sections_created
14153 || h->dynindx == -1)
2d7ad24e
AM
14154 {
14155 if (h->type == STT_GNU_IFUNC)
14156 plt = htab->elf.iplt;
14157 else
14158 plt = htab->pltlocal;
14159 }
49c09209 14160 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
14161 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14162
14163 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14164 {
14165 info->callbacks->einfo
c1c8c1ef 14166 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
14167 h->root.root.string);
14168 bfd_set_error (bfd_error_bad_value);
14169 htab->stub_error = TRUE;
14170 }
14171
7341d5e2
AM
14172 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14173 if (htab->params->emit_stub_syms)
14174 {
14175 size_t len = strlen (h->root.root.string);
14176 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14177
14178 if (name == NULL)
14179 return FALSE;
14180
14181 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14182 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
14183 if (h == NULL)
14184 return FALSE;
14185 if (h->root.type == bfd_link_hash_new)
14186 {
14187 h->root.type = bfd_link_hash_defined;
14188 h->root.u.def.section = s;
14189 h->root.u.def.value = p - s->contents;
14190 h->ref_regular = 1;
14191 h->def_regular = 1;
14192 h->ref_regular_nonweak = 1;
14193 h->forced_local = 1;
14194 h->non_elf = 0;
2ec55de3 14195 h->root.linker_def = 1;
7341d5e2
AM
14196 }
14197 }
14198
a345bc8d
AM
14199 if (PPC_HA (off) != 0)
14200 {
14201 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14202 p += 4;
14203 }
14204 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14205 p += 4;
14206 bfd_put_32 (s->owner, MTCTR_R12, p);
14207 p += 4;
407aa07c 14208 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
14209 break;
14210 }
14211 return TRUE;
14212}
14213
49c09209
AM
14214/* Write PLT relocs for locals. */
14215
14216static bfd_boolean
14217write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14218{
14219 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14220 bfd *ibfd;
14221
14222 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14223 {
14224 struct got_entry **lgot_ents, **end_lgot_ents;
14225 struct plt_entry **local_plt, **lplt, **end_local_plt;
14226 Elf_Internal_Shdr *symtab_hdr;
14227 bfd_size_type locsymcount;
14228 Elf_Internal_Sym *local_syms = NULL;
14229 struct plt_entry *ent;
14230
14231 if (!is_ppc64_elf (ibfd))
14232 continue;
14233
14234 lgot_ents = elf_local_got_ents (ibfd);
14235 if (!lgot_ents)
14236 continue;
14237
14238 symtab_hdr = &elf_symtab_hdr (ibfd);
14239 locsymcount = symtab_hdr->sh_info;
14240 end_lgot_ents = lgot_ents + locsymcount;
14241 local_plt = (struct plt_entry **) end_lgot_ents;
14242 end_local_plt = local_plt + locsymcount;
14243 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14244 for (ent = *lplt; ent != NULL; ent = ent->next)
14245 if (ent->plt.offset != (bfd_vma) -1)
14246 {
14247 Elf_Internal_Sym *sym;
14248 asection *sym_sec;
14249 asection *plt, *relplt;
14250 bfd_byte *loc;
14251 bfd_vma val;
49c09209
AM
14252
14253 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14254 lplt - local_plt, ibfd))
14255 {
c9594989 14256 if (symtab_hdr->contents != (unsigned char *) local_syms)
49c09209
AM
14257 free (local_syms);
14258 return FALSE;
14259 }
14260
14261 val = sym->st_value + ent->addend;
49c09209
AM
14262 if (sym_sec != NULL && sym_sec->output_section != NULL)
14263 val += sym_sec->output_offset + sym_sec->output_section->vma;
14264
2d7ad24e
AM
14265 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14266 {
cebd6b8a 14267 htab->elf.ifunc_resolvers = TRUE;
2d7ad24e
AM
14268 plt = htab->elf.iplt;
14269 relplt = htab->elf.irelplt;
14270 }
14271 else
14272 {
14273 plt = htab->pltlocal;
14274 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
14275 }
49c09209 14276
2d7ad24e
AM
14277 if (relplt == NULL)
14278 {
14279 loc = plt->contents + ent->plt.offset;
14280 bfd_put_64 (info->output_bfd, val, loc);
14281 if (htab->opd_abi)
14282 {
14283 bfd_vma toc = elf_gp (ibfd);
14284 bfd_put_64 (info->output_bfd, toc, loc + 8);
14285 }
14286 }
49c09209 14287 else
2d7ad24e
AM
14288 {
14289 Elf_Internal_Rela rela;
14290 rela.r_offset = (ent->plt.offset
14291 + plt->output_offset
14292 + plt->output_section->vma);
14293 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14294 {
14295 if (htab->opd_abi)
14296 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14297 else
14298 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14299 }
14300 else
14301 {
14302 if (htab->opd_abi)
14303 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14304 else
14305 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14306 }
14307 rela.r_addend = val;
14308 loc = relplt->contents + (relplt->reloc_count++
14309 * sizeof (Elf64_External_Rela));
14310 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14311 }
49c09209
AM
14312 }
14313
14314 if (local_syms != NULL
14315 && symtab_hdr->contents != (unsigned char *) local_syms)
14316 {
14317 if (!info->keep_memory)
14318 free (local_syms);
14319 else
14320 symtab_hdr->contents = (unsigned char *) local_syms;
14321 }
14322 }
14323 return TRUE;
14324}
14325
a804e476
AM
14326/* Emit the static wrapper function preserving registers around a
14327 __tls_get_addr_opt call. */
14328
14329static bfd_boolean
14330emit_tga_desc (struct ppc_link_hash_table *htab)
14331{
14332 asection *stub_sec = htab->tga_group->stub_sec;
14333 unsigned int cfa_updt = 11 * 4;
14334 bfd_byte *p;
14335 bfd_vma to, from, delta;
14336
14337 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14338 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14339 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14340 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14341 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14342 delta = to - from;
14343 if (delta + (1 << 25) >= 1 << 26)
14344 {
14345 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14346 htab->stub_error = TRUE;
14347 return FALSE;
14348 }
14349
14350 p = stub_sec->contents;
14351 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14352 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14353 p += 4;
14354 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14355 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14356}
14357
14358/* Emit eh_frame describing the static wrapper function. */
14359
14360static bfd_byte *
14361emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14362{
14363 unsigned int cfa_updt = 11 * 4;
14364 unsigned int i;
14365
14366 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14367 *p++ = DW_CFA_def_cfa_offset;
14368 if (htab->opd_abi)
14369 {
14370 *p++ = 128;
14371 *p++ = 1;
14372 }
14373 else
14374 *p++ = 96;
14375 *p++ = DW_CFA_offset_extended_sf;
14376 *p++ = 65;
14377 *p++ = (-16 / 8) & 0x7f;
14378 for (i = 4; i < 12; i++)
14379 {
14380 *p++ = DW_CFA_offset + i;
14381 *p++ = (htab->opd_abi ? 13 : 12) - i;
14382 }
14383 *p++ = DW_CFA_advance_loc + 10;
14384 *p++ = DW_CFA_def_cfa_offset;
14385 *p++ = 0;
14386 for (i = 4; i < 12; i++)
14387 *p++ = DW_CFA_restore + i;
14388 *p++ = DW_CFA_advance_loc + 2;
14389 *p++ = DW_CFA_restore_extended;
14390 *p++ = 65;
14391 return p;
14392}
14393
721956f4
AM
14394/* Build all the stubs associated with the current output file.
14395 The stubs are kept in a hash table attached to the main linker
14396 hash table. This function is called via gldelf64ppc_finish. */
14397
b34976b6 14398bfd_boolean
e7d1c40c 14399ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 14400 char **stats)
5d1634d7
AM
14401{
14402 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 14403 struct map_stub *group;
721956f4 14404 asection *stub_sec;
5d1634d7 14405 bfd_byte *p;
e717da7e 14406 int stub_sec_count = 0;
5d1634d7 14407
4dfe6ac6
NC
14408 if (htab == NULL)
14409 return FALSE;
14410
eea6121a 14411 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 14412 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14413 {
14414 group->eh_size = 0;
14415 group->lr_restore = 0;
14416 if ((stub_sec = group->stub_sec) != NULL
14417 && stub_sec->size != 0)
14418 {
14419 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14420 stub_sec->size);
14421 if (stub_sec->contents == NULL)
14422 return FALSE;
14423 stub_sec->size = 0;
14424 }
14425 }
5d1634d7 14426
23eb7e01 14427 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 14428 {
9f951329 14429 unsigned int indx;
ad8e1ba5 14430 bfd_vma plt0;
9f951329 14431
721956f4 14432 /* Build the .glink plt call stub. */
e7d1c40c 14433 if (htab->params->emit_stub_syms)
97b639ba
AM
14434 {
14435 struct elf_link_hash_entry *h;
468392fb
AM
14436 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14437 TRUE, FALSE, FALSE);
97b639ba
AM
14438 if (h == NULL)
14439 return FALSE;
14440 if (h->root.type == bfd_link_hash_new)
14441 {
14442 h->root.type = bfd_link_hash_defined;
14443 h->root.u.def.section = htab->glink;
ee4bf8d2 14444 h->root.u.def.value = 8;
f5385ebf
AM
14445 h->ref_regular = 1;
14446 h->def_regular = 1;
14447 h->ref_regular_nonweak = 1;
14448 h->forced_local = 1;
14449 h->non_elf = 0;
2ec55de3 14450 h->root.linker_def = 1;
97b639ba
AM
14451 }
14452 }
33e44f2e
AM
14453 plt0 = (htab->elf.splt->output_section->vma
14454 + htab->elf.splt->output_offset
14455 - 16);
176a0d42
AM
14456 if (info->emitrelocations)
14457 {
14458 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14459 if (r == NULL)
14460 return FALSE;
14461 r->r_offset = (htab->glink->output_offset
14462 + htab->glink->output_section->vma);
14463 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14464 r->r_addend = plt0;
14465 }
4ce794b7 14466 p = htab->glink->contents;
176a0d42 14467 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
14468 bfd_put_64 (htab->glink->owner, plt0, p);
14469 p += 8;
b9e5796b
AM
14470 if (htab->opd_abi)
14471 {
14472 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14473 p += 4;
14474 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14475 p += 4;
14476 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14477 p += 4;
14478 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14479 p += 4;
14480 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14481 p += 4;
14482 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14483 p += 4;
14484 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14485 p += 4;
14486 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14487 p += 4;
14488 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14489 p += 4;
14490 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14491 p += 4;
14492 }
14493 else
14494 {
3cd7c7d7
AM
14495 unsigned int insn;
14496
14497 /* 0:
14498 . .quad plt0-1f # plt0 entry relative to 1:
14499 #
14500 # We get here with r12 initially @ a glink branch
14501 # Load the address of _dl_runtime_resolve from plt0 and
14502 # jump to it, with r0 set to the index of the PLT entry
14503 # to be resolved and r11 the link map.
14504 __glink_PLTresolve:
14505 . std %r2,24(%r1) # optional
14506 . mflr %r0
14507 . bcl 20,31,1f
14508 1:
14509 . mflr %r11
14510 . mtlr %r0
14511 . ld %r0,(0b-1b)(%r11)
14512 . sub %r12,%r12,%r11
14513 . add %r11,%r0,%r11
14514 . addi %r0,%r12,1b-2f
14515 . ld %r12,0(%r11)
14516 . srdi %r0,%r0,2
14517 . mtctr %r12
14518 . ld %r11,8(%r11)
14519 . bctr
14520 2:
14521 . b __glink_PLTresolve
14522 . ...
14523 . b __glink_PLTresolve */
14524
14525 if (htab->has_plt_localentry0)
14526 {
14527 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14528 p += 4;
14529 }
b9e5796b
AM
14530 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14531 p += 4;
14532 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14533 p += 4;
14534 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14535 p += 4;
b9e5796b
AM
14536 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14537 p += 4;
3cd7c7d7
AM
14538 if (htab->has_plt_localentry0)
14539 insn = LD_R0_0R11 | (-20 & 0xfffc);
14540 else
14541 insn = LD_R0_0R11 | (-16 & 0xfffc);
14542 bfd_put_32 (htab->glink->owner, insn, p);
14543 p += 4;
b9e5796b
AM
14544 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14545 p += 4;
3cd7c7d7 14546 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
b9e5796b 14547 p += 4;
3cd7c7d7 14548 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
b9e5796b
AM
14549 p += 4;
14550 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14551 p += 4;
14552 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14553 p += 4;
14554 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14555 p += 4;
14556 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14557 p += 4;
14558 }
407aa07c
AM
14559 bfd_put_32 (htab->glink->owner, BCTR, p);
14560 p += 4;
c75bc4f7 14561 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 14562
9f951329
AM
14563 /* Build the .glink lazy link call stubs. */
14564 indx = 0;
9e390558 14565 while (p < htab->glink->contents + htab->glink->size)
9f951329 14566 {
b9e5796b 14567 if (htab->opd_abi)
9f951329 14568 {
b9e5796b
AM
14569 if (indx < 0x8000)
14570 {
14571 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14572 p += 4;
14573 }
14574 else
14575 {
14576 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14577 p += 4;
14578 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14579 p);
14580 p += 4;
14581 }
9f951329 14582 }
4ce794b7 14583 bfd_put_32 (htab->glink->owner,
ee4bf8d2 14584 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 14585 indx++;
9f951329
AM
14586 p += 4;
14587 }
5d1634d7 14588 }
5d1634d7 14589
a804e476
AM
14590 if (htab->tga_group != NULL)
14591 {
14592 htab->tga_group->lr_restore = 23 * 4;
14593 htab->tga_group->stub_sec->size = 24 * 4;
14594 if (!emit_tga_desc (htab))
14595 return FALSE;
14596 if (htab->glink_eh_frame != NULL
14597 && htab->glink_eh_frame->size != 0)
14598 {
14599 size_t align = 4;
14600
14601 p = htab->glink_eh_frame->contents;
14602 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14603 p += 17;
14604 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
14605 }
14606 }
14607
49c09209
AM
14608 /* Build .glink global entry stubs, and PLT relocs for globals. */
14609 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
14610
14611 if (!write_plt_relocs_for_local_syms (info))
14612 return FALSE;
9e390558 14613
7341d5e2 14614 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 14615 {
4ce794b7 14616 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 14617 htab->brlt->size);
4ce794b7 14618 if (htab->brlt->contents == NULL)
b34976b6 14619 return FALSE;
721956f4 14620 }
ee75fd95 14621 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
14622 {
14623 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 14624 htab->relbrlt->size);
63bc6f6c
AM
14625 if (htab->relbrlt->contents == NULL)
14626 return FALSE;
14627 }
5d1634d7 14628
721956f4
AM
14629 /* Build the stubs as directed by the stub hash table. */
14630 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 14631
a4b6fadd
AM
14632 for (group = htab->group; group != NULL; group = group->next)
14633 if (group->needs_save_res)
7dda8d3c 14634 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 14635
aa8a7074
AM
14636 if (htab->relbrlt != NULL)
14637 htab->relbrlt->reloc_count = 0;
14638
e7d1c40c 14639 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
14640 for (group = htab->group; group != NULL; group = group->next)
14641 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
14642 {
14643 int align = abs (htab->params->plt_stub_align);
14644 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14645 }
794e51c0 14646
7dda8d3c
AM
14647 for (group = htab->group; group != NULL; group = group->next)
14648 if (group->needs_save_res)
14649 {
14650 stub_sec = group->stub_sec;
14651 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14652 htab->sfpr->contents, htab->sfpr->size);
14653 if (htab->params->emit_stub_syms)
14654 {
14655 unsigned int i;
14656
14657 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14658 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14659 return FALSE;
14660 }
14661 }
14662
df136d64
AM
14663 if (htab->glink_eh_frame != NULL
14664 && htab->glink_eh_frame->size != 0)
14665 {
14666 bfd_vma val;
14667 size_t align = 4;
14668
14669 p = htab->glink_eh_frame->contents;
14670 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14671
14672 for (group = htab->group; group != NULL; group = group->next)
14673 if (group->eh_size != 0)
14674 {
14675 /* Offset to stub section. */
14676 val = (group->stub_sec->output_section->vma
14677 + group->stub_sec->output_offset);
14678 val -= (htab->glink_eh_frame->output_section->vma
14679 + htab->glink_eh_frame->output_offset
14680 + (p + 8 - htab->glink_eh_frame->contents));
14681 if (val + 0x80000000 > 0xffffffff)
14682 {
14683 _bfd_error_handler
14684 (_("%s offset too large for .eh_frame sdata4 encoding"),
14685 group->stub_sec->name);
14686 return FALSE;
14687 }
14688 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14689 p += (group->eh_size + 17 + 3) & -4;
14690 }
14691 if (htab->glink != NULL && htab->glink->size != 0)
14692 {
14693 /* Offset to .glink. */
14694 val = (htab->glink->output_section->vma
14695 + htab->glink->output_offset
14696 + 8);
14697 val -= (htab->glink_eh_frame->output_section->vma
14698 + htab->glink_eh_frame->output_offset
14699 + (p + 8 - htab->glink_eh_frame->contents));
14700 if (val + 0x80000000 > 0xffffffff)
14701 {
14702 _bfd_error_handler
14703 (_("%s offset too large for .eh_frame sdata4 encoding"),
14704 htab->glink->name);
14705 return FALSE;
14706 }
14707 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14708 p += (24 + align - 1) & -align;
14709 }
14710 }
14711
d4aaa2a0
AM
14712 for (group = htab->group; group != NULL; group = group->next)
14713 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
14714 {
14715 stub_sec_count += 1;
c9301e31
AM
14716 if (stub_sec->rawsize != stub_sec->size
14717 && (htab->stub_iteration <= STUB_SHRINK_ITER
14718 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
14719 break;
14720 }
5d1634d7 14721
25516cc5 14722 if (group != NULL)
5d1634d7 14723 {
b34976b6 14724 htab->stub_error = TRUE;
cf97bcb0 14725 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 14726 }
721956f4 14727
d2a300cf
AM
14728 if (htab->stub_error)
14729 return FALSE;
14730
14731 if (stats != NULL)
14732 {
988b7300
AM
14733 char *groupmsg;
14734 if (asprintf (&groupmsg,
14735 ngettext ("linker stubs in %u group\n",
14736 "linker stubs in %u groups\n",
14737 stub_sec_count),
14738 stub_sec_count) < 0)
14739 *stats = NULL;
14740 else
14741 {
14742 if (asprintf (stats, _("%s"
14743 " branch %lu\n"
14744 " branch toc adj %lu\n"
14745 " branch notoc %lu\n"
14746 " branch both %lu\n"
14747 " long branch %lu\n"
14748 " long toc adj %lu\n"
14749 " long notoc %lu\n"
14750 " long both %lu\n"
14751 " plt call %lu\n"
14752 " plt call save %lu\n"
14753 " plt call notoc %lu\n"
14754 " plt call both %lu\n"
14755 " global entry %lu"),
14756 groupmsg,
14757 htab->stub_count[ppc_stub_long_branch - 1],
14758 htab->stub_count[ppc_stub_long_branch_r2off - 1],
14759 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14760 htab->stub_count[ppc_stub_long_branch_both - 1],
14761 htab->stub_count[ppc_stub_plt_branch - 1],
14762 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14763 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14764 htab->stub_count[ppc_stub_plt_branch_both - 1],
14765 htab->stub_count[ppc_stub_plt_call - 1],
14766 htab->stub_count[ppc_stub_plt_call_r2save - 1],
14767 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14768 htab->stub_count[ppc_stub_plt_call_both - 1],
14769 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
14770 *stats = NULL;
14771 free (groupmsg);
14772 }
d2a300cf
AM
14773 }
14774 return TRUE;
5bd4f169
AM
14775}
14776
60124e18
AM
14777/* What to do when ld finds relocations against symbols defined in
14778 discarded sections. */
14779
14780static unsigned int
14781ppc64_elf_action_discarded (asection *sec)
14782{
14783 if (strcmp (".opd", sec->name) == 0)
14784 return 0;
14785
14786 if (strcmp (".toc", sec->name) == 0)
14787 return 0;
14788
bce50a28
JJ
14789 if (strcmp (".toc1", sec->name) == 0)
14790 return 0;
14791
60124e18
AM
14792 return _bfd_elf_default_action_discarded (sec);
14793}
14794
e59a1001
AM
14795/* These are the dynamic relocations supported by glibc. */
14796
14797static bfd_boolean
14798ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
14799{
14800 switch (r_type)
14801 {
14802 case R_PPC64_RELATIVE:
14803 case R_PPC64_NONE:
14804 case R_PPC64_ADDR64:
14805 case R_PPC64_GLOB_DAT:
14806 case R_PPC64_IRELATIVE:
14807 case R_PPC64_JMP_IREL:
14808 case R_PPC64_JMP_SLOT:
14809 case R_PPC64_DTPMOD64:
14810 case R_PPC64_DTPREL64:
14811 case R_PPC64_TPREL64:
14812 case R_PPC64_TPREL16_LO_DS:
14813 case R_PPC64_TPREL16_DS:
14814 case R_PPC64_TPREL16:
14815 case R_PPC64_TPREL16_LO:
14816 case R_PPC64_TPREL16_HI:
14817 case R_PPC64_TPREL16_HIGH:
14818 case R_PPC64_TPREL16_HA:
14819 case R_PPC64_TPREL16_HIGHA:
14820 case R_PPC64_TPREL16_HIGHER:
14821 case R_PPC64_TPREL16_HIGHEST:
14822 case R_PPC64_TPREL16_HIGHERA:
14823 case R_PPC64_TPREL16_HIGHESTA:
14824 case R_PPC64_ADDR16_LO_DS:
14825 case R_PPC64_ADDR16_LO:
14826 case R_PPC64_ADDR16_HI:
14827 case R_PPC64_ADDR16_HIGH:
14828 case R_PPC64_ADDR16_HA:
14829 case R_PPC64_ADDR16_HIGHA:
14830 case R_PPC64_REL30:
14831 case R_PPC64_COPY:
14832 case R_PPC64_UADDR64:
14833 case R_PPC64_UADDR32:
14834 case R_PPC64_ADDR32:
14835 case R_PPC64_ADDR24:
14836 case R_PPC64_ADDR16:
14837 case R_PPC64_UADDR16:
14838 case R_PPC64_ADDR16_DS:
14839 case R_PPC64_ADDR16_HIGHER:
14840 case R_PPC64_ADDR16_HIGHEST:
14841 case R_PPC64_ADDR16_HIGHERA:
14842 case R_PPC64_ADDR16_HIGHESTA:
14843 case R_PPC64_ADDR14:
14844 case R_PPC64_ADDR14_BRTAKEN:
14845 case R_PPC64_ADDR14_BRNTAKEN:
14846 case R_PPC64_REL32:
14847 case R_PPC64_REL64:
14848 return TRUE;
14849
14850 default:
14851 return FALSE;
14852 }
14853}
14854
5bd4f169
AM
14855/* The RELOCATE_SECTION function is called by the ELF backend linker
14856 to handle the relocations for a section.
14857
14858 The relocs are always passed as Rela structures; if the section
14859 actually uses Rel structures, the r_addend field will always be
14860 zero.
14861
14862 This function is responsible for adjust the section contents as
14863 necessary, and (if using Rela relocs and generating a
1049f94e 14864 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
14865 necessary.
14866
14867 This function does not have to worry about setting the reloc
14868 address or the reloc symbol index.
14869
14870 LOCAL_SYMS is a pointer to the swapped in local symbols.
14871
14872 LOCAL_SECTIONS is an array giving the section in the input file
14873 corresponding to the st_shndx field of each local symbol.
14874
14875 The global hash table entry for the global symbols can be found
14876 via elf_sym_hashes (input_bfd).
14877
1049f94e 14878 When generating relocatable output, this function must handle
5bd4f169
AM
14879 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14880 going to be the section symbol corresponding to the output
14881 section, which means that the addend must be adjusted
14882 accordingly. */
14883
b34976b6 14884static bfd_boolean
4ce794b7
AM
14885ppc64_elf_relocate_section (bfd *output_bfd,
14886 struct bfd_link_info *info,
14887 bfd *input_bfd,
14888 asection *input_section,
14889 bfd_byte *contents,
14890 Elf_Internal_Rela *relocs,
14891 Elf_Internal_Sym *local_syms,
14892 asection **local_sections)
5bd4f169 14893{
65f38f15 14894 struct ppc_link_hash_table *htab;
5bd4f169
AM
14895 Elf_Internal_Shdr *symtab_hdr;
14896 struct elf_link_hash_entry **sym_hashes;
5bd4f169 14897 Elf_Internal_Rela *rel;
c316a17c 14898 Elf_Internal_Rela *wrel;
5bd4f169 14899 Elf_Internal_Rela *relend;
411e1bfb
AM
14900 Elf_Internal_Rela outrel;
14901 bfd_byte *loc;
411e1bfb 14902 struct got_entry **local_got_ents;
5bd4f169 14903 bfd_vma TOCstart;
b34976b6
AM
14904 bfd_boolean ret = TRUE;
14905 bfd_boolean is_opd;
794e51c0
AM
14906 /* Assume 'at' branch hints. */
14907 bfd_boolean is_isa_v2 = TRUE;
e59a1001 14908 bfd_boolean warned_dynamic = FALSE;
95f0d0d2 14909 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 14910
65f38f15 14911 /* Initialize howto table if needed. */
5bd4f169 14912 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
14913 ppc_howto_init ();
14914
65f38f15 14915 htab = ppc_hash_table (info);
4dfe6ac6
NC
14916 if (htab == NULL)
14917 return FALSE;
ee75fd95
AM
14918
14919 /* Don't relocate stub sections. */
e7d1c40c 14920 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
14921 return TRUE;
14922
7af5d5c4
AM
14923 if (!is_ppc64_elf (input_bfd))
14924 {
14925 bfd_set_error (bfd_error_wrong_format);
14926 return FALSE;
14927 }
0ffa91dd 14928
411e1bfb 14929 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 14930 TOCstart = elf_gp (output_bfd);
0ffa91dd 14931 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 14932 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 14933 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 14934
c316a17c 14935 rel = wrel = relocs;
5bd4f169 14936 relend = relocs + input_section->reloc_count;
c316a17c 14937 for (; rel < relend; wrel++, rel++)
5bd4f169 14938 {
04c9666a 14939 enum elf_ppc64_reloc_type r_type;
31c76678 14940 bfd_vma addend;
5bd4f169
AM
14941 bfd_reloc_status_type r;
14942 Elf_Internal_Sym *sym;
14943 asection *sec;
039b3fef
AM
14944 struct elf_link_hash_entry *h_elf;
14945 struct ppc_link_hash_entry *h;
14946 struct ppc_link_hash_entry *fdh;
5bd4f169 14947 const char *sym_name;
0d4792f7 14948 unsigned long r_symndx, toc_symndx;
3a71aa26 14949 bfd_vma toc_addend;
f961d9dd
AM
14950 unsigned char tls_mask, tls_gd, tls_type;
14951 unsigned char sym_type;
5bd4f169 14952 bfd_vma relocation;
23cedd1d 14953 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 14954 bfd_boolean warned;
bc30df16 14955 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 14956 unsigned int insn;
e11840f9 14957 unsigned int mask;
721956f4
AM
14958 struct ppc_stub_hash_entry *stub_entry;
14959 bfd_vma max_br_offset;
14960 bfd_vma from;
c316a17c 14961 Elf_Internal_Rela orig_rel;
b80eed39
AM
14962 reloc_howto_type *howto;
14963 struct reloc_howto_struct alt_howto;
4a421c53
AM
14964 uint64_t pinsn;
14965 bfd_vma offset;
5bd4f169 14966
c316a17c
AM
14967 again:
14968 orig_rel = *rel;
14969
4ce794b7 14970 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 14971 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
14972
14973 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14974 symbol of the previous ADDR64 reloc. The symbol gives us the
14975 proper TOC base to use. */
14976 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
14977 && wrel != relocs
14978 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 14979 && is_opd)
c316a17c 14980 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 14981
4ce794b7
AM
14982 sym = NULL;
14983 sec = NULL;
039b3fef 14984 h_elf = NULL;
4ce794b7 14985 sym_name = NULL;
b34976b6
AM
14986 unresolved_reloc = FALSE;
14987 warned = FALSE;
65f38f15 14988
0b13192e 14989 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
14990 {
14991 /* It's a local symbol. */
74f0fb50 14992 struct _opd_sec_data *opd;
4025353c 14993
5bd4f169
AM
14994 sym = local_syms + r_symndx;
14995 sec = local_sections[r_symndx];
26c61ae5 14996 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 14997 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 14998 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
14999 opd = get_opd_info (sec);
15000 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 15001 {
51aecdc5
AM
15002 long adjust = opd->adjust[OPD_NDX (sym->st_value
15003 + rel->r_addend)];
4025353c
AM
15004 if (adjust == -1)
15005 relocation = 0;
15006 else
4cc603a5
AM
15007 {
15008 /* If this is a relocation against the opd section sym
15009 and we have edited .opd, adjust the reloc addend so
15010 that ld -r and ld --emit-relocs output is correct.
15011 If it is a reloc against some other .opd symbol,
15012 then the symbol value will be adjusted later. */
15013 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15014 rel->r_addend += adjust;
15015 else
15016 relocation += adjust;
15017 }
1e2f5b6e 15018 }
5bd4f169
AM
15019 }
15020 else
15021 {
62d887d4
L
15022 bfd_boolean ignored;
15023
b2a8e766
AM
15024 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15025 r_symndx, symtab_hdr, sym_hashes,
039b3fef 15026 h_elf, sec, relocation,
62d887d4 15027 unresolved_reloc, warned, ignored);
039b3fef
AM
15028 sym_name = h_elf->root.root.string;
15029 sym_type = h_elf->type;
b69fdb4e
AM
15030 if (sec != NULL
15031 && sec->owner == output_bfd
15032 && strcmp (sec->name, ".opd") == 0)
15033 {
15034 /* This is a symbol defined in a linker script. All
15035 such are defined in output sections, even those
15036 defined by simple assignment from a symbol defined in
15037 an input section. Transfer the symbol to an
15038 appropriate input .opd section, so that a branch to
15039 this symbol will be mapped to the location specified
15040 by the opd entry. */
15041 struct bfd_link_order *lo;
15042 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15043 if (lo->type == bfd_indirect_link_order)
15044 {
15045 asection *isec = lo->u.indirect.section;
15046 if (h_elf->root.u.def.value >= isec->output_offset
15047 && h_elf->root.u.def.value < (isec->output_offset
15048 + isec->size))
15049 {
15050 h_elf->root.u.def.value -= isec->output_offset;
15051 h_elf->root.u.def.section = isec;
15052 sec = isec;
15053 break;
15054 }
15055 }
15056 }
5bd4f169 15057 }
ed7007c1 15058 h = ppc_elf_hash_entry (h_elf);
5bd4f169 15059
dbaa2011 15060 if (sec != NULL && discarded_section (sec))
c316a17c
AM
15061 {
15062 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15063 input_bfd, input_section,
0930cb30 15064 contents, rel->r_offset);
c316a17c
AM
15065 wrel->r_offset = rel->r_offset;
15066 wrel->r_info = 0;
15067 wrel->r_addend = 0;
15068
15069 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 15070 symbols defined in discarded sections. Not done for
c316a17c
AM
15071 non-debug to preserve relocs in .eh_frame which the
15072 eh_frame editing code expects to be present. */
15073 if (bfd_link_relocatable (info)
15074 && (input_section->flags & SEC_DEBUGGING))
15075 wrel--;
15076
15077 continue;
15078 }
ab96bf03 15079
0e1862bb 15080 if (bfd_link_relocatable (info))
c316a17c 15081 goto copy_reloc;
ab96bf03 15082
f40da81b
AM
15083 if (h != NULL && &h->elf == htab->elf.hgot)
15084 {
6f20ed8a 15085 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
15086 sec = bfd_abs_section_ptr;
15087 unresolved_reloc = FALSE;
15088 }
15089
951fd09b
AM
15090 /* TLS optimizations. Replace instruction sequences and relocs
15091 based on information we collected in tls_optimize. We edit
15092 RELOCS so that --emit-relocs will output something sensible
15093 for the final instruction stream. */
15094 tls_mask = 0;
15095 tls_gd = 0;
0d4792f7 15096 toc_symndx = 0;
727fc41e
AM
15097 if (h != NULL)
15098 tls_mask = h->tls_mask;
15099 else if (local_got_ents != NULL)
411e1bfb 15100 {
e054468f
AM
15101 struct plt_entry **local_plt = (struct plt_entry **)
15102 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 15103 unsigned char *lgot_masks = (unsigned char *)
e054468f 15104 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
15105 tls_mask = lgot_masks[r_symndx];
15106 }
37da22e5 15107 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
15108 && (r_type == R_PPC64_TLS
15109 || r_type == R_PPC64_TLSGD
15110 || r_type == R_PPC64_TLSLD))
15111 {
15112 /* Check for toc tls entries. */
f961d9dd 15113 unsigned char *toc_tls;
0d4792f7 15114
727fc41e
AM
15115 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15116 &local_syms, rel, input_bfd))
15117 return FALSE;
0d4792f7 15118
727fc41e
AM
15119 if (toc_tls)
15120 tls_mask = *toc_tls;
0d4792f7
AM
15121 }
15122
15123 /* Check that tls relocs are used with tls syms, and non-tls
15124 relocs are used with non-tls syms. */
cf35638d 15125 if (r_symndx != STN_UNDEF
0d4792f7
AM
15126 && r_type != R_PPC64_NONE
15127 && (h == NULL
039b3fef
AM
15128 || h->elf.root.type == bfd_link_hash_defined
15129 || h->elf.root.type == bfd_link_hash_defweak)
71c4e95a 15130 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
0d4792f7 15131 {
37da22e5 15132 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
15133 && (r_type == R_PPC64_TLS
15134 || r_type == R_PPC64_TLSGD
15135 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
15136 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15137 ;
15138 else
25f53a85 15139 info->callbacks->einfo
1d483afe 15140 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 15141 /* xgettext:c-format */
c1c8c1ef 15142 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 15143 /* xgettext:c-format */
c1c8c1ef 15144 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 15145 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
15146 ppc64_elf_howto_table[r_type]->name,
15147 sym_name);
411e1bfb
AM
15148 }
15149
15150 /* Ensure reloc mapping code below stays sane. */
15151 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15152 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15153 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15154 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15155 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15156 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15157 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15158 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15159 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15160 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15161 abort ();
0d4792f7 15162
411e1bfb
AM
15163 switch (r_type)
15164 {
15165 default:
411e1bfb
AM
15166 break;
15167
ba761f19 15168 case R_PPC64_LO_DS_OPT:
95f0d0d2 15169 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
2365f8d7 15170 if ((insn & (0x3fu << 26)) != 58u << 26)
ba761f19
AM
15171 abort ();
15172 insn += (14u << 26) - (58u << 26);
95f0d0d2 15173 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
15174 r_type = R_PPC64_TOC16_LO;
15175 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15176 break;
15177
411e1bfb
AM
15178 case R_PPC64_TOC16:
15179 case R_PPC64_TOC16_LO:
15180 case R_PPC64_TOC16_DS:
15181 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
15182 {
15183 /* Check for toc tls entries. */
f961d9dd 15184 unsigned char *toc_tls;
951fd09b 15185 int retval;
411e1bfb 15186
3a71aa26
AM
15187 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15188 &local_syms, rel, input_bfd);
951fd09b 15189 if (retval == 0)
411e1bfb
AM
15190 return FALSE;
15191
15192 if (toc_tls)
15193 {
951fd09b 15194 tls_mask = *toc_tls;
411e1bfb
AM
15195 if (r_type == R_PPC64_TOC16_DS
15196 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 15197 {
37da22e5 15198 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
15199 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15200 goto toctprel;
15201 }
411e1bfb 15202 else
951fd09b
AM
15203 {
15204 /* If we found a GD reloc pair, then we might be
15205 doing a GD->IE transition. */
15206 if (retval == 2)
15207 {
b00a0a86 15208 tls_gd = TLS_GDIE;
37da22e5
AM
15209 if ((tls_mask & TLS_TLS) != 0
15210 && (tls_mask & TLS_GD) == 0)
102890f0 15211 goto tls_ldgd_opt;
951fd09b
AM
15212 }
15213 else if (retval == 3)
15214 {
37da22e5
AM
15215 if ((tls_mask & TLS_TLS) != 0
15216 && (tls_mask & TLS_LD) == 0)
102890f0 15217 goto tls_ldgd_opt;
951fd09b
AM
15218 }
15219 }
411e1bfb
AM
15220 }
15221 }
15222 break;
15223
9d6ded02
AM
15224 case R_PPC64_GOT_TPREL16_HI:
15225 case R_PPC64_GOT_TPREL16_HA:
37da22e5 15226 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
15227 && (tls_mask & TLS_TPREL) == 0)
15228 {
15229 rel->r_offset -= d_offset;
95f0d0d2 15230 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
15231 r_type = R_PPC64_NONE;
15232 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15233 }
15234 break;
15235
411e1bfb
AM
15236 case R_PPC64_GOT_TPREL16_DS:
15237 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 15238 if ((tls_mask & TLS_TLS) != 0
951fd09b 15239 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 15240 {
81407a69 15241 toctprel:
95f0d0d2 15242 insn = bfd_get_32 (input_bfd,
c316a17c 15243 contents + rel->r_offset - d_offset);
411e1bfb
AM
15244 insn &= 31 << 21;
15245 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 15246 bfd_put_32 (input_bfd, insn,
c316a17c 15247 contents + rel->r_offset - d_offset);
411e1bfb 15248 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
15249 if (toc_symndx != 0)
15250 {
15251 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 15252 rel->r_addend = toc_addend;
0d4792f7
AM
15253 /* We changed the symbol. Start over in order to
15254 get h, sym, sec etc. right. */
c316a17c 15255 goto again;
0d4792f7
AM
15256 }
15257 else
15258 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15259 }
15260 break;
15261
87c69f97 15262 case R_PPC64_GOT_TPREL_PCREL34:
c213164a
AM
15263 if ((tls_mask & TLS_TLS) != 0
15264 && (tls_mask & TLS_TPREL) == 0)
15265 {
15266 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15267 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15268 pinsn <<= 32;
15269 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15270 pinsn += ((2ULL << 56) + (-1ULL << 52)
15271 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15272 bfd_put_32 (input_bfd, pinsn >> 32,
15273 contents + rel->r_offset);
15274 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15275 contents + rel->r_offset + 4);
15276 r_type = R_PPC64_TPREL34;
15277 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15278 }
15279 break;
15280
411e1bfb 15281 case R_PPC64_TLS:
37da22e5 15282 if ((tls_mask & TLS_TLS) != 0
951fd09b 15283 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 15284 {
c213164a 15285 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2d0f3896
AM
15286 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15287 if (insn == 0)
c213164a
AM
15288 break;
15289 if ((rel->r_offset & 3) == 0)
0d4792f7 15290 {
c213164a
AM
15291 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15292 /* Was PPC64_TLS which sits on insn boundary, now
15293 PPC64_TPREL16_LO which is at low-order half-word. */
15294 rel->r_offset += d_offset;
15295 r_type = R_PPC64_TPREL16_LO;
15296 if (toc_symndx != 0)
15297 {
15298 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15299 rel->r_addend = toc_addend;
15300 /* We changed the symbol. Start over in order to
15301 get h, sym, sec etc. right. */
15302 goto again;
15303 }
15304 else
15305 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15306 }
15307 else if ((rel->r_offset & 3) == 1)
15308 {
15309 /* For pcrel IE to LE we already have the full
15310 offset and thus don't need an addi here. A nop
15311 or mr will do. */
2365f8d7 15312 if ((insn & (0x3fu << 26)) == 14 << 26)
c213164a
AM
15313 {
15314 /* Extract regs from addi rt,ra,si. */
15315 unsigned int rt = (insn >> 21) & 0x1f;
15316 unsigned int ra = (insn >> 16) & 0x1f;
15317 if (rt == ra)
15318 insn = NOP;
15319 else
15320 {
15321 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15322 insn = (rt << 16) | (ra << 21) | (ra << 11);
15323 insn |= (31u << 26) | (444u << 1);
15324 }
15325 }
15326 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
0d4792f7 15327 }
411e1bfb
AM
15328 }
15329 break;
15330
411e1bfb
AM
15331 case R_PPC64_GOT_TLSGD16_HI:
15332 case R_PPC64_GOT_TLSGD16_HA:
b00a0a86 15333 tls_gd = TLS_GDIE;
37da22e5 15334 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
15335 goto tls_gdld_hi;
15336 break;
15337
411e1bfb
AM
15338 case R_PPC64_GOT_TLSLD16_HI:
15339 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 15340 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 15341 {
951fd09b
AM
15342 tls_gdld_hi:
15343 if ((tls_mask & tls_gd) != 0)
15344 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15345 + R_PPC64_GOT_TPREL16_DS);
15346 else
411e1bfb 15347 {
4fe5ca5b 15348 rel->r_offset -= d_offset;
95f0d0d2 15349 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 15350 r_type = R_PPC64_NONE;
411e1bfb 15351 }
951fd09b 15352 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15353 }
15354 break;
15355
951fd09b
AM
15356 case R_PPC64_GOT_TLSGD16:
15357 case R_PPC64_GOT_TLSGD16_LO:
b00a0a86 15358 tls_gd = TLS_GDIE;
37da22e5 15359 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 15360 goto tls_ldgd_opt;
951fd09b 15361 break;
411e1bfb 15362
951fd09b
AM
15363 case R_PPC64_GOT_TLSLD16:
15364 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 15365 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 15366 {
b9f04fe0 15367 unsigned int insn1, insn2;
102890f0
AM
15368
15369 tls_ldgd_opt:
727fc41e
AM
15370 offset = (bfd_vma) -1;
15371 /* If not using the newer R_PPC64_TLSGD/LD to mark
15372 __tls_get_addr calls, we must trust that the call
15373 stays with its arg setup insns, ie. that the next
15374 reloc is the __tls_get_addr call associated with
15375 the current reloc. Edit both insns. */
9737e8af 15376 if (input_section->nomark_tls_get_addr
727fc41e
AM
15377 && rel + 1 < relend
15378 && branch_reloc_hash_match (input_bfd, rel + 1,
9e7028aa
AM
15379 htab->tls_get_addr_fd,
15380 htab->tga_desc_fd,
727fc41e 15381 htab->tls_get_addr,
9e7028aa 15382 htab->tga_desc))
727fc41e 15383 offset = rel[1].r_offset;
b86ac8e3
AM
15384 /* We read the low GOT_TLS (or TOC16) insn because we
15385 need to keep the destination reg. It may be
15386 something other than the usual r3, and moved to r3
15387 before the call by intervening code. */
95f0d0d2 15388 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 15389 contents + rel->r_offset - d_offset);
102890f0 15390 if ((tls_mask & tls_gd) != 0)
411e1bfb 15391 {
102890f0 15392 /* IE */
b86ac8e3 15393 insn1 &= (0x1f << 21) | (0x1f << 16);
2365f8d7 15394 insn1 |= 58u << 26; /* ld */
102890f0 15395 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 15396 if (offset != (bfd_vma) -1)
f58d5a2d 15397 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
46e9995a
AM
15398 if (r_type == R_PPC64_TOC16
15399 || r_type == R_PPC64_TOC16_LO)
102890f0 15400 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
46e9995a
AM
15401 else
15402 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15403 + R_PPC64_GOT_TPREL16_DS);
102890f0
AM
15404 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15405 }
15406 else
15407 {
15408 /* LE */
b86ac8e3
AM
15409 insn1 &= 0x1f << 21;
15410 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
15411 insn2 = 0x38630000; /* addi 3,3,0 */
15412 if (tls_gd == 0)
951fd09b 15413 {
102890f0 15414 /* Was an LD reloc. */
71c4e95a 15415 r_symndx = STN_UNDEF;
102890f0 15416 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 15417 }
102890f0 15418 else if (toc_symndx != 0)
3a71aa26
AM
15419 {
15420 r_symndx = toc_symndx;
15421 rel->r_addend = toc_addend;
15422 }
102890f0
AM
15423 r_type = R_PPC64_TPREL16_HA;
15424 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
15425 if (offset != (bfd_vma) -1)
15426 {
15427 rel[1].r_info = ELF64_R_INFO (r_symndx,
15428 R_PPC64_TPREL16_LO);
15429 rel[1].r_offset = offset + d_offset;
15430 rel[1].r_addend = rel->r_addend;
15431 }
102890f0 15432 }
95f0d0d2 15433 bfd_put_32 (input_bfd, insn1,
3a71aa26 15434 contents + rel->r_offset - d_offset);
727fc41e 15435 if (offset != (bfd_vma) -1)
c96e0573
AM
15436 {
15437 bfd_put_32 (input_bfd, insn2, contents + offset);
15438 if (offset + 8 <= input_section->size)
15439 {
15440 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15441 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15442 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15443 }
15444 }
727fc41e
AM
15445 if ((tls_mask & tls_gd) == 0
15446 && (tls_gd == 0 || toc_symndx != 0))
15447 {
15448 /* We changed the symbol. Start over in order
15449 to get h, sym, sec etc. right. */
c316a17c 15450 goto again;
727fc41e
AM
15451 }
15452 }
15453 break;
15454
87c69f97 15455 case R_PPC64_GOT_TLSGD_PCREL34:
c213164a
AM
15456 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
15457 {
15458 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15459 pinsn <<= 32;
15460 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15461 if ((tls_mask & TLS_GDIE) != 0)
15462 {
15463 /* IE, pla -> pld */
15464 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
87c69f97 15465 r_type = R_PPC64_GOT_TPREL_PCREL34;
c213164a
AM
15466 }
15467 else
15468 {
15469 /* LE, pla pcrel -> paddi r13 */
15470 pinsn += (-1ULL << 52) + (13ULL << 16);
15471 r_type = R_PPC64_TPREL34;
15472 }
15473 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15474 bfd_put_32 (input_bfd, pinsn >> 32,
15475 contents + rel->r_offset);
15476 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15477 contents + rel->r_offset + 4);
15478 }
15479 break;
15480
87c69f97 15481 case R_PPC64_GOT_TLSLD_PCREL34:
c213164a
AM
15482 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
15483 {
15484 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15485 pinsn <<= 32;
15486 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15487 pinsn += (-1ULL << 52) + (13ULL << 16);
15488 bfd_put_32 (input_bfd, pinsn >> 32,
15489 contents + rel->r_offset);
15490 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15491 contents + rel->r_offset + 4);
15492 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15493 r_symndx = STN_UNDEF;
15494 r_type = R_PPC64_TPREL34;
15495 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15496 goto again;
15497 }
15498 break;
15499
727fc41e 15500 case R_PPC64_TLSGD:
37da22e5 15501 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 15502 && rel + 1 < relend)
727fc41e 15503 {
b9f04fe0 15504 unsigned int insn2;
5663e321 15505 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 15506
4a421c53 15507 offset = rel->r_offset;
5663e321 15508 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
15509 {
15510 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
15511 if (r_type1 == R_PPC64_PLT_PCREL34
15512 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15513 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
15514 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15515 break;
15516 }
15517
15518 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15519 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15520
b00a0a86 15521 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
15522 {
15523 /* IE */
15524 r_type = R_PPC64_NONE;
15525 insn2 = 0x7c636a14; /* add 3,3,13 */
15526 }
15527 else
15528 {
15529 /* LE */
15530 if (toc_symndx != 0)
15531 {
15532 r_symndx = toc_symndx;
15533 rel->r_addend = toc_addend;
15534 }
c213164a
AM
15535 if (r_type1 == R_PPC64_REL24_NOTOC
15536 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15537 {
15538 r_type = R_PPC64_NONE;
15539 insn2 = NOP;
15540 }
15541 else
15542 {
15543 rel->r_offset = offset + d_offset;
15544 r_type = R_PPC64_TPREL16_LO;
15545 insn2 = 0x38630000; /* addi 3,3,0 */
15546 }
727fc41e
AM
15547 }
15548 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15549 /* Zap the reloc on the _tls_get_addr call too. */
15550 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 15551 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 15552 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
15553 if ((tls_mask & TLS_GDIE) == 0
15554 && toc_symndx != 0
15555 && r_type != R_PPC64_NONE)
c316a17c 15556 goto again;
411e1bfb 15557 }
411e1bfb
AM
15558 break;
15559
727fc41e 15560 case R_PPC64_TLSLD:
37da22e5 15561 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 15562 && rel + 1 < relend)
727fc41e 15563 {
b9f04fe0 15564 unsigned int insn2;
5663e321 15565 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 15566
4a421c53 15567 offset = rel->r_offset;
5663e321 15568 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
15569 {
15570 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
15571 if (r_type1 == R_PPC64_PLT_PCREL34
15572 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15573 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
15574 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15575 break;
15576 }
15577
15578 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15579 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15580
c213164a
AM
15581 if (r_type1 == R_PPC64_REL24_NOTOC
15582 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15583 {
15584 r_type = R_PPC64_NONE;
15585 insn2 = NOP;
15586 }
15587 else
15588 {
15589 rel->r_offset = offset + d_offset;
15590 r_symndx = STN_UNDEF;
15591 r_type = R_PPC64_TPREL16_LO;
15592 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15593 insn2 = 0x38630000; /* addi 3,3,0 */
15594 }
727fc41e 15595 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
15596 /* Zap the reloc on the _tls_get_addr call too. */
15597 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 15598 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 15599 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
15600 if (r_type != R_PPC64_NONE)
15601 goto again;
727fc41e
AM
15602 }
15603 break;
15604
411e1bfb 15605 case R_PPC64_DTPMOD64:
951fd09b
AM
15606 if (rel + 1 < relend
15607 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
15608 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 15609 {
951fd09b
AM
15610 if ((tls_mask & TLS_GD) == 0)
15611 {
15612 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
b00a0a86 15613 if ((tls_mask & TLS_GDIE) != 0)
951fd09b
AM
15614 r_type = R_PPC64_TPREL64;
15615 else
15616 {
4ce794b7 15617 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
15618 r_type = R_PPC64_NONE;
15619 }
15620 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15621 }
15622 }
15623 else
15624 {
15625 if ((tls_mask & TLS_LD) == 0)
411e1bfb 15626 {
4ce794b7 15627 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 15628 r_type = R_PPC64_NONE;
951fd09b 15629 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 15630 }
411e1bfb
AM
15631 }
15632 break;
15633
15634 case R_PPC64_TPREL64:
951fd09b 15635 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
15636 {
15637 r_type = R_PPC64_NONE;
15638 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15639 }
15640 break;
52a82034 15641
006589cf
AM
15642 case R_PPC64_ENTRY:
15643 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15644 if (!bfd_link_pic (info)
15645 && !info->traditional_format
15646 && relocation + 0x80008000 <= 0xffffffff)
15647 {
15648 unsigned int insn1, insn2;
15649
15650 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15651 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15652 if ((insn1 & ~0xfffc) == LD_R2_0R12
15653 && insn2 == ADD_R2_R2_R12)
15654 {
95f0d0d2 15655 bfd_put_32 (input_bfd,
006589cf
AM
15656 LIS_R2 + PPC_HA (relocation),
15657 contents + rel->r_offset);
95f0d0d2 15658 bfd_put_32 (input_bfd,
006589cf
AM
15659 ADDI_R2_R2 + PPC_LO (relocation),
15660 contents + rel->r_offset + 4);
15661 }
15662 }
15663 else
15664 {
15665 relocation -= (rel->r_offset
15666 + input_section->output_offset
15667 + input_section->output_section->vma);
15668 if (relocation + 0x80008000 <= 0xffffffff)
15669 {
15670 unsigned int insn1, insn2;
15671
15672 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15673 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15674 if ((insn1 & ~0xfffc) == LD_R2_0R12
15675 && insn2 == ADD_R2_R2_R12)
15676 {
95f0d0d2 15677 bfd_put_32 (input_bfd,
006589cf
AM
15678 ADDIS_R2_R12 + PPC_HA (relocation),
15679 contents + rel->r_offset);
95f0d0d2 15680 bfd_put_32 (input_bfd,
006589cf
AM
15681 ADDI_R2_R2 + PPC_LO (relocation),
15682 contents + rel->r_offset + 4);
15683 }
15684 }
15685 }
15686 break;
15687
52a82034
AM
15688 case R_PPC64_REL16_HA:
15689 /* If we are generating a non-PIC executable, edit
15690 . 0: addis 2,12,.TOC.-0b@ha
15691 . addi 2,2,.TOC.-0b@l
15692 used by ELFv2 global entry points to set up r2, to
15693 . lis 2,.TOC.@ha
15694 . addi 2,2,.TOC.@l
15695 if .TOC. is in range. */
0e1862bb 15696 if (!bfd_link_pic (info)
810d4e75 15697 && !info->traditional_format
006589cf 15698 && !htab->opd_abi
4f038ee5 15699 && rel->r_addend == d_offset
52a82034
AM
15700 && h != NULL && &h->elf == htab->elf.hgot
15701 && rel + 1 < relend
15702 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15703 && rel[1].r_offset == rel->r_offset + 4
15704 && rel[1].r_addend == rel->r_addend + 4
15705 && relocation + 0x80008000 <= 0xffffffff)
15706 {
15707 unsigned int insn1, insn2;
4a421c53 15708 offset = rel->r_offset - d_offset;
95f0d0d2
AM
15709 insn1 = bfd_get_32 (input_bfd, contents + offset);
15710 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
15711 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15712 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
15713 {
15714 r_type = R_PPC64_ADDR16_HA;
15715 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15716 rel->r_addend -= d_offset;
15717 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15718 rel[1].r_addend -= d_offset + 4;
95f0d0d2 15719 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
15720 }
15721 }
15722 break;
411e1bfb
AM
15723 }
15724
15725 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 15726 insn = 0;
b25116a9
AM
15727 max_br_offset = 1 << 25;
15728 addend = rel->r_addend;
bc30df16 15729 reloc_dest = DEST_NORMAL;
65f38f15 15730 switch (r_type)
5bd4f169
AM
15731 {
15732 default:
65f38f15 15733 break;
5bd4f169 15734
3b421ab3
AM
15735 case R_PPC64_TOCSAVE:
15736 if (relocation + addend == (rel->r_offset
15737 + input_section->output_offset
15738 + input_section->output_section->vma)
15739 && tocsave_find (htab, NO_INSERT,
15740 &local_syms, rel, input_bfd))
15741 {
15742 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15743 if (insn == NOP
15744 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
15745 bfd_put_32 (input_bfd,
15746 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
15747 contents + rel->r_offset);
15748 }
15749 break;
15750
65f38f15
AM
15751 /* Branch taken prediction relocations. */
15752 case R_PPC64_ADDR14_BRTAKEN:
15753 case R_PPC64_REL14_BRTAKEN:
cedb70c5 15754 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 15755 /* Fall through. */
65f38f15 15756
86c76c7b 15757 /* Branch not taken prediction relocations. */
65f38f15
AM
15758 case R_PPC64_ADDR14_BRNTAKEN:
15759 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 15760 insn |= bfd_get_32 (input_bfd,
411e1bfb 15761 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 15762 /* Fall through. */
86c76c7b 15763
b25116a9
AM
15764 case R_PPC64_REL14:
15765 max_br_offset = 1 << 15;
1a0670f3 15766 /* Fall through. */
5bd4f169 15767
65f38f15 15768 case R_PPC64_REL24:
05d0e962 15769 case R_PPC64_REL24_NOTOC:
23cedd1d 15770 case R_PPC64_PLTCALL:
5663e321 15771 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
15772 /* Calls to functions with a different TOC, such as calls to
15773 shared objects, need to alter the TOC pointer. This is
15774 done using a linkage stub. A REL24 branching to these
15775 linkage stubs needs to be followed by a nop, as the nop
15776 will be replaced with an instruction to restore the TOC
15777 base pointer. */
8387904d 15778 fdh = h;
b31867b6
AM
15779 if (h != NULL
15780 && h->oh != NULL
15781 && h->oh->is_func_descriptor)
15782 fdh = ppc_follow_link (h->oh);
31c76678
DK
15783 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15784 htab);
5663e321
AM
15785 if ((r_type == R_PPC64_PLTCALL
15786 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 15787 && stub_entry != NULL
05d0e962
AM
15788 && stub_entry->stub_type >= ppc_stub_plt_call
15789 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
15790 stub_entry = NULL;
15791
6abec6bc 15792 if (stub_entry != NULL
d0abeec8
AM
15793 && ((stub_entry->stub_type >= ppc_stub_plt_call
15794 && stub_entry->stub_type <= ppc_stub_plt_call_both)
ad8e1ba5 15795 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
15796 || stub_entry->stub_type == ppc_stub_plt_branch_both
15797 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15798 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 15799 {
b25116a9 15800 bfd_boolean can_plt_call = FALSE;
721956f4 15801
6e1816be
AM
15802 if (stub_entry->stub_type == ppc_stub_plt_call
15803 && !htab->opd_abi
15804 && htab->params->plt_localentry0 != 0
9e51d549 15805 && h != NULL
6e1816be
AM
15806 && is_elfv2_localentry0 (&h->elf))
15807 {
15808 /* The function doesn't use or change r2. */
15809 can_plt_call = TRUE;
15810 }
05d0e962
AM
15811 else if (r_type == R_PPC64_REL24_NOTOC)
15812 {
15813 /* NOTOC calls don't need to restore r2. */
15814 can_plt_call = TRUE;
15815 }
6e1816be 15816
f378ab09 15817 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
15818 branch and link followed by a nop. The nop is
15819 replaced by an insn to restore r2. */
6e1816be 15820 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 15821 {
ba8ca3e7
AM
15822 unsigned long br;
15823
15824 br = bfd_get_32 (input_bfd,
15825 contents + rel->r_offset);
15826 if ((br & 1) != 0)
41bd81ab 15827 {
ba8ca3e7
AM
15828 unsigned long nop;
15829
15830 nop = bfd_get_32 (input_bfd,
15831 contents + rel->r_offset + 4);
23cedd1d
AM
15832 if (nop == LD_R2_0R1 + STK_TOC (htab))
15833 can_plt_call = TRUE;
15834 else if (nop == NOP
15835 || nop == CROR_151515
15836 || nop == CROR_313131)
a7f2871e 15837 {
ba8ca3e7 15838 if (h != NULL
ed7007c1 15839 && is_tls_get_addr (&h->elf, htab)
7c9cf415 15840 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
15841 {
15842 /* Special stub used, leave nop alone. */
15843 }
15844 else
a078d95a
AM
15845 bfd_put_32 (input_bfd,
15846 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
15847 contents + rel->r_offset + 4);
15848 can_plt_call = TRUE;
a7f2871e 15849 }
41bd81ab 15850 }
5bd4f169 15851 }
721956f4 15852
ba8ca3e7 15853 if (!can_plt_call && h != NULL)
721956f4 15854 {
ba8ca3e7
AM
15855 const char *name = h->elf.root.root.string;
15856
15857 if (*name == '.')
15858 ++name;
15859
15860 if (strncmp (name, "__libc_start_main", 17) == 0
15861 && (name[17] == 0 || name[17] == '@'))
6ab189d5 15862 {
ba8ca3e7
AM
15863 /* Allow crt1 branch to go via a toc adjusting
15864 stub. Other calls that never return could do
15865 the same, if we could detect such. */
b25116a9 15866 can_plt_call = TRUE;
6ab189d5 15867 }
ba8ca3e7
AM
15868 }
15869
15870 if (!can_plt_call)
15871 {
15872 /* g++ as of 20130507 emits self-calls without a
15873 following nop. This is arguably wrong since we
15874 have conflicting information. On the one hand a
15875 global symbol and on the other a local call
15876 sequence, but don't error for this special case.
15877 It isn't possible to cheaply verify we have
15878 exactly such a call. Allow all calls to the same
15879 section. */
15880 asection *code_sec = sec;
15881
15882 if (get_opd_info (sec) != NULL)
ad8e1ba5 15883 {
ba8ca3e7
AM
15884 bfd_vma off = (relocation + addend
15885 - sec->output_section->vma
15886 - sec->output_offset);
bc30df16 15887
ba8ca3e7 15888 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 15889 }
ba8ca3e7
AM
15890 if (code_sec == input_section)
15891 can_plt_call = TRUE;
15892 }
15893
15894 if (!can_plt_call)
15895 {
05d0e962
AM
15896 if (stub_entry->stub_type >= ppc_stub_plt_call
15897 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 15898 info->callbacks->einfo
695344c0 15899 /* xgettext:c-format */
c1c8c1ef 15900 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15901 "(plt call stub)\n"),
4805fc55
AM
15902 input_bfd, input_section, rel->r_offset, sym_name);
15903 else
15904 info->callbacks->einfo
695344c0 15905 /* xgettext:c-format */
c1c8c1ef 15906 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15907 "(toc save/adjust stub)\n"),
4805fc55 15908 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
15909
15910 bfd_set_error (bfd_error_bad_value);
15911 ret = FALSE;
721956f4
AM
15912 }
15913
b25116a9 15914 if (can_plt_call
05d0e962
AM
15915 && stub_entry->stub_type >= ppc_stub_plt_call
15916 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
15917 unresolved_reloc = FALSE;
15918 }
15919
6abec6bc
AM
15920 if ((stub_entry == NULL
15921 || stub_entry->stub_type == ppc_stub_long_branch
15922 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
15923 && get_opd_info (sec) != NULL)
15924 {
15925 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
15926 bfd_vma off = (relocation + addend
15927 - sec->output_section->vma
15928 - sec->output_offset);
aef36ac1 15929 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
15930 if (dest != (bfd_vma) -1)
15931 {
15932 relocation = dest;
15933 addend = 0;
bc30df16 15934 reloc_dest = DEST_OPD;
8387904d
AM
15935 }
15936 }
15937
b25116a9
AM
15938 /* If the branch is out of reach we ought to have a long
15939 branch stub. */
15940 from = (rel->r_offset
15941 + input_section->output_offset
15942 + input_section->output_section->vma);
15943
6911b7dc
AM
15944 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15945 ? fdh->elf.other
15946 : sym->st_other);
15947
6abec6bc
AM
15948 if (stub_entry != NULL
15949 && (stub_entry->stub_type == ppc_stub_long_branch
15950 || stub_entry->stub_type == ppc_stub_plt_branch)
15951 && (r_type == R_PPC64_ADDR14_BRTAKEN
15952 || r_type == R_PPC64_ADDR14_BRNTAKEN
15953 || (relocation + addend - from + max_br_offset
15954 < 2 * max_br_offset)))
15955 /* Don't use the stub if this branch is in range. */
15956 stub_entry = NULL;
b25116a9 15957
05d0e962
AM
15958 if (stub_entry != NULL
15959 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15960 || stub_entry->stub_type == ppc_stub_long_branch_both
15961 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15962 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15963 && (r_type != R_PPC64_REL24_NOTOC
15964 || ((fdh ? fdh->elf.other : sym->st_other)
4a4e7361 15965 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
05d0e962
AM
15966 && (relocation + addend - from + max_br_offset
15967 < 2 * max_br_offset))
15968 stub_entry = NULL;
15969
15970 if (stub_entry != NULL
15971 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15972 || stub_entry->stub_type == ppc_stub_long_branch_both
15973 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15974 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15975 && r_type == R_PPC64_REL24_NOTOC
15976 && (relocation + addend - from + max_br_offset
15977 < 2 * max_br_offset))
15978 stub_entry = NULL;
15979
b25116a9
AM
15980 if (stub_entry != NULL)
15981 {
15982 /* Munge up the value and addend so that we call the stub
15983 rather than the procedure directly. */
a4b6fadd
AM
15984 asection *stub_sec = stub_entry->group->stub_sec;
15985
15986 if (stub_entry->stub_type == ppc_stub_save_res)
15987 relocation += (stub_sec->output_offset
15988 + stub_sec->output_section->vma
15989 + stub_sec->size - htab->sfpr->size
15990 - htab->sfpr->output_offset
15991 - htab->sfpr->output_section->vma);
15992 else
15993 relocation = (stub_entry->stub_offset
15994 + stub_sec->output_offset
15995 + stub_sec->output_section->vma);
b25116a9 15996 addend = 0;
bc30df16 15997 reloc_dest = DEST_STUB;
3b421ab3 15998
29433886
AM
15999 if ((((stub_entry->stub_type == ppc_stub_plt_call
16000 && ALWAYS_EMIT_R2SAVE)
16001 || stub_entry->stub_type == ppc_stub_plt_call_r2save
16002 || stub_entry->stub_type == ppc_stub_plt_call_both)
16003 && rel + 1 < relend
16004 && rel[1].r_offset == rel->r_offset + 4
16005 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
16006 || ((stub_entry->stub_type == ppc_stub_long_branch_both
16007 || stub_entry->stub_type == ppc_stub_plt_branch_both
16008 || stub_entry->stub_type == ppc_stub_plt_call_both)
16009 && r_type == R_PPC64_REL24_NOTOC))
16010 {
16011 /* Skip over the r2 store at the start of the stub. */
16012 if (!(stub_entry->stub_type >= ppc_stub_plt_call
16013 && htab->params->tls_get_addr_opt
16014 && h != NULL
16015 && is_tls_get_addr (&h->elf, htab)))
16016 relocation += 4;
16017 }
5663e321
AM
16018
16019 if (r_type == R_PPC64_REL24_NOTOC
16020 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
16021 || stub_entry->stub_type == ppc_stub_plt_call_both))
16022 htab->notoc_plt = 1;
b25116a9
AM
16023 }
16024
16025 if (insn != 0)
16026 {
794e51c0 16027 if (is_isa_v2)
721956f4 16028 {
b25116a9
AM
16029 /* Set 'a' bit. This is 0b00010 in BO field for branch
16030 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16031 for branch on CTR insns (BO == 1a00t or 1a01t). */
16032 if ((insn & (0x14 << 21)) == (0x04 << 21))
16033 insn |= 0x02 << 21;
16034 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16035 insn |= 0x08 << 21;
16036 else
16037 break;
16038 }
16039 else
16040 {
16041 /* Invert 'y' bit if not the default. */
4cc603a5 16042 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 16043 insn ^= 0x01 << 21;
721956f4 16044 }
b25116a9 16045
95f0d0d2 16046 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 16047 }
e86ce104 16048
06da1e8e
AM
16049 /* NOP out calls to undefined weak functions.
16050 We can thus call a weak function without first
16051 checking whether the function is defined. */
b25116a9 16052 else if (h != NULL
039b3fef 16053 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 16054 && h->elf.dynindx == -1
05d0e962
AM
16055 && (r_type == R_PPC64_REL24
16056 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 16057 && relocation == 0
4cc603a5 16058 && addend == 0)
e86ce104 16059 {
95f0d0d2 16060 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 16061 goto copy_reloc;
e86ce104 16062 }
65f38f15 16063 break;
066f4018
AM
16064
16065 case R_PPC64_GOT16_DS:
6738c8a7
AM
16066 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16067 || !htab->do_toc_opt)
06507dab 16068 break;
066f4018
AM
16069 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16070 if (relocation + addend - from + 0x8000 < 0x10000
9e51d549 16071 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
066f4018
AM
16072 {
16073 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 16074 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16075 {
16076 insn += (14u << 26) - (58u << 26);
16077 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16078 r_type = R_PPC64_TOC16;
16079 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16080 }
16081 }
16082 break;
16083
16084 case R_PPC64_GOT16_LO_DS:
16085 case R_PPC64_GOT16_HA:
6738c8a7
AM
16086 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16087 || !htab->do_toc_opt)
06507dab 16088 break;
066f4018
AM
16089 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16090 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
9e51d549 16091 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
066f4018
AM
16092 {
16093 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
fba8689a
AM
16094 if (r_type == R_PPC64_GOT16_LO_DS
16095 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16096 {
16097 insn += (14u << 26) - (58u << 26);
16098 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16099 r_type = R_PPC64_TOC16_LO;
16100 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16101 }
fba8689a
AM
16102 else if (r_type == R_PPC64_GOT16_HA
16103 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
066f4018
AM
16104 {
16105 r_type = R_PPC64_TOC16_HA;
16106 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16107 }
16108 }
16109 break;
4a421c53
AM
16110
16111 case R_PPC64_GOT_PCREL34:
6738c8a7
AM
16112 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16113 || !htab->do_toc_opt)
06507dab 16114 break;
4a421c53
AM
16115 from = (rel->r_offset
16116 + input_section->output_section->vma
16117 + input_section->output_offset);
6738c8a7 16118 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
9e51d549 16119 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
6738c8a7
AM
16120 break;
16121
16122 offset = rel->r_offset;
16123 pinsn = bfd_get_32 (input_bfd, contents + offset);
16124 pinsn <<= 32;
16125 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16126 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16127 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16128 break;
16129
16130 /* Replace with paddi. */
16131 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16132 r_type = R_PPC64_PCREL34;
16133 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16134 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16135 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16136 /* Fall through. */
4a421c53
AM
16137
16138 case R_PPC64_PCREL34:
6738c8a7
AM
16139 if (!htab->params->no_pcrel_opt
16140 && rel + 1 < relend
16141 && rel[1].r_offset == rel->r_offset
16142 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
9e51d549 16143 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
4a421c53
AM
16144 {
16145 offset = rel->r_offset;
16146 pinsn = bfd_get_32 (input_bfd, contents + offset);
16147 pinsn <<= 32;
16148 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16149 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16150 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16151 | (14ULL << 26) /* paddi */))
16152 {
6738c8a7
AM
16153 bfd_vma off2 = rel[1].r_addend;
16154 if (off2 == 0)
16155 /* zero means next insn. */
16156 off2 = 8;
16157 off2 += offset;
16158 if (off2 + 4 <= input_section->size)
4a421c53 16159 {
6738c8a7
AM
16160 uint64_t pinsn2;
16161 bfd_signed_vma addend_off;
16162 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16163 pinsn2 <<= 32;
16164 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
4a421c53 16165 {
6738c8a7
AM
16166 if (off2 + 8 > input_section->size)
16167 break;
16168 pinsn2 |= bfd_get_32 (input_bfd,
16169 contents + off2 + 4);
16170 }
16171 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16172 {
16173 addend += addend_off;
16174 rel->r_addend = addend;
16175 bfd_put_32 (input_bfd, pinsn >> 32,
16176 contents + offset);
16177 bfd_put_32 (input_bfd, pinsn,
16178 contents + offset + 4);
16179 bfd_put_32 (input_bfd, pinsn2 >> 32,
16180 contents + off2);
4a421c53 16181 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
6738c8a7
AM
16182 bfd_put_32 (input_bfd, pinsn2,
16183 contents + off2 + 4);
4a421c53
AM
16184 }
16185 }
16186 }
16187 }
16188 break;
65f38f15 16189 }
5bd4f169 16190
411e1bfb 16191 tls_type = 0;
23cedd1d 16192 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
16193 switch (r_type)
16194 {
16195 default:
cf97bcb0
AM
16196 /* xgettext:c-format */
16197 _bfd_error_handler (_("%pB: %s unsupported"),
16198 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 16199
65f38f15 16200 bfd_set_error (bfd_error_bad_value);
b34976b6 16201 ret = FALSE;
c316a17c 16202 goto copy_reloc;
5bd4f169 16203
65f38f15 16204 case R_PPC64_NONE:
411e1bfb 16205 case R_PPC64_TLS:
727fc41e
AM
16206 case R_PPC64_TLSGD:
16207 case R_PPC64_TLSLD:
3b421ab3 16208 case R_PPC64_TOCSAVE:
04c9666a
AM
16209 case R_PPC64_GNU_VTINHERIT:
16210 case R_PPC64_GNU_VTENTRY:
006589cf 16211 case R_PPC64_ENTRY:
4a421c53 16212 case R_PPC64_PCREL_OPT:
c316a17c 16213 goto copy_reloc;
5bd4f169
AM
16214
16215 /* GOT16 relocations. Like an ADDR16 using the symbol's
16216 address in the GOT as relocation value instead of the
411e1bfb 16217 symbol's value itself. Also, create a GOT entry for the
5bd4f169 16218 symbol and put the symbol value there. */
411e1bfb
AM
16219 case R_PPC64_GOT_TLSGD16:
16220 case R_PPC64_GOT_TLSGD16_LO:
16221 case R_PPC64_GOT_TLSGD16_HI:
16222 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 16223 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 16224 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
16225 goto dogot;
16226
16227 case R_PPC64_GOT_TLSLD16:
16228 case R_PPC64_GOT_TLSLD16_LO:
16229 case R_PPC64_GOT_TLSLD16_HI:
16230 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 16231 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 16232 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
16233 goto dogot;
16234
16235 case R_PPC64_GOT_TPREL16_DS:
16236 case R_PPC64_GOT_TPREL16_LO_DS:
16237 case R_PPC64_GOT_TPREL16_HI:
16238 case R_PPC64_GOT_TPREL16_HA:
87c69f97 16239 case R_PPC64_GOT_TPREL_PCREL34:
411e1bfb
AM
16240 tls_type = TLS_TLS | TLS_TPREL;
16241 goto dogot;
16242
16243 case R_PPC64_GOT_DTPREL16_DS:
16244 case R_PPC64_GOT_DTPREL16_LO_DS:
16245 case R_PPC64_GOT_DTPREL16_HI:
16246 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 16247 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
16248 tls_type = TLS_TLS | TLS_DTPREL;
16249 goto dogot;
16250
65f38f15
AM
16251 case R_PPC64_GOT16:
16252 case R_PPC64_GOT16_LO:
16253 case R_PPC64_GOT16_HI:
16254 case R_PPC64_GOT16_HA:
16255 case R_PPC64_GOT16_DS:
16256 case R_PPC64_GOT16_LO_DS:
5663e321 16257 case R_PPC64_GOT_PCREL34:
411e1bfb 16258 dogot:
5bd4f169
AM
16259 {
16260 /* Relocation is to the entry for this symbol in the global
16261 offset table. */
e717da7e 16262 asection *got;
d881513a 16263 bfd_vma *offp;
5bd4f169 16264 bfd_vma off;
d881513a 16265 unsigned long indx = 0;
927be08e 16266 struct got_entry *ent;
65f38f15 16267
d881513a 16268 if (tls_type == (TLS_TLS | TLS_LD)
9e51d549 16269 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
927be08e 16270 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 16271 else
5bd4f169 16272 {
d881513a
AM
16273 if (h != NULL)
16274 {
f0158f44
AM
16275 if (!htab->elf.dynamic_sections_created
16276 || h->elf.dynindx == -1
16277 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 16278 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
16279 /* This is actually a static link, or it is a
16280 -Bsymbolic link and the symbol is defined
16281 locally, or the symbol was forced to be local
16282 because of a version file. */
16283 ;
16284 else
16285 {
039b3fef 16286 indx = h->elf.dynindx;
d881513a
AM
16287 unresolved_reloc = FALSE;
16288 }
039b3fef 16289 ent = h->elf.got.glist;
d881513a 16290 }
411e1bfb 16291 else
5bd4f169 16292 {
d881513a
AM
16293 if (local_got_ents == NULL)
16294 abort ();
16295 ent = local_got_ents[r_symndx];
5bd4f169 16296 }
d881513a
AM
16297
16298 for (; ent != NULL; ent = ent->next)
133a1f60 16299 if (ent->addend == orig_rel.r_addend
e717da7e 16300 && ent->owner == input_bfd
d881513a
AM
16301 && ent->tls_type == tls_type)
16302 break;
5bd4f169 16303 }
411e1bfb 16304
927be08e
AM
16305 if (ent == NULL)
16306 abort ();
16307 if (ent->is_indirect)
16308 ent = ent->got.ent;
16309 offp = &ent->got.offset;
16310 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
16311 if (got == NULL)
16312 abort ();
16313
411e1bfb
AM
16314 /* The offset must always be a multiple of 8. We use the
16315 least significant bit to record whether we have already
16316 processed this entry. */
d881513a 16317 off = *offp;
411e1bfb
AM
16318 if ((off & 1) != 0)
16319 off &= ~1;
5bd4f169
AM
16320 else
16321 {
411e1bfb
AM
16322 /* Generate relocs for the dynamic linker, except in
16323 the case of TLSLD where we'll use one entry per
16324 module. */
25f23106
AM
16325 asection *relgot;
16326 bfd_boolean ifunc;
e717da7e 16327
d881513a 16328 *offp = off | 1;
25f23106
AM
16329 relgot = NULL;
16330 ifunc = (h != NULL
16331 ? h->elf.type == STT_GNU_IFUNC
16332 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 16333 if (ifunc)
82e66161
AM
16334 {
16335 relgot = htab->elf.irelplt;
efb2a7b4 16336 if (indx == 0 || is_static_defined (&h->elf))
cebd6b8a 16337 htab->elf.ifunc_resolvers = TRUE;
82e66161 16338 }
f0158f44
AM
16339 else if (indx != 0
16340 || (bfd_link_pic (info)
16341 && (h == NULL
f749f26e
AM
16342 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16343 && !(tls_type != 0
f15d0b54 16344 && bfd_link_executable (info)
9e51d549
AM
16345 && (h == NULL
16346 || SYMBOL_REFERENCES_LOCAL (info,
16347 &h->elf)))))
19e08130 16348 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 16349 if (relgot != NULL)
5bd4f169 16350 {
e717da7e
AM
16351 outrel.r_offset = (got->output_section->vma
16352 + got->output_offset
411e1bfb 16353 + off);
133a1f60 16354 outrel.r_addend = orig_rel.r_addend;
d881513a 16355 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 16356 {
411e1bfb 16357 outrel.r_addend = 0;
e515b051 16358 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
16359 if (tls_type == (TLS_TLS | TLS_GD))
16360 {
e717da7e
AM
16361 loc = relgot->contents;
16362 loc += (relgot->reloc_count++
d881513a
AM
16363 * sizeof (Elf64_External_Rela));
16364 bfd_elf64_swap_reloca_out (output_bfd,
16365 &outrel, loc);
e515b051 16366 outrel.r_offset += 8;
133a1f60 16367 outrel.r_addend = orig_rel.r_addend;
d881513a
AM
16368 outrel.r_info
16369 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 16370 }
411e1bfb 16371 }
951fd09b 16372 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 16373 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 16374 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 16375 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
16376 else if (indx != 0)
16377 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16378 else
81407a69 16379 {
25f23106
AM
16380 if (ifunc)
16381 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16382 else
16383 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
16384
16385 /* Write the .got section contents for the sake
16386 of prelink. */
e717da7e 16387 loc = got->contents + off;
23fbd6fa
JJ
16388 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16389 loc);
81407a69 16390 }
81407a69
AM
16391
16392 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
16393 {
16394 outrel.r_addend += relocation;
16395 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
16396 {
16397 if (htab->elf.tls_sec == NULL)
16398 outrel.r_addend = 0;
16399 else
16400 outrel.r_addend -= htab->elf.tls_sec->vma;
16401 }
e515b051 16402 }
e717da7e
AM
16403 loc = relgot->contents;
16404 loc += (relgot->reloc_count++
411e1bfb
AM
16405 * sizeof (Elf64_External_Rela));
16406 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16407 }
16408
ad8e1ba5 16409 /* Init the .got section contents here if we're not
81407a69 16410 emitting a reloc. */
d881513a 16411 else
411e1bfb 16412 {
133a1f60 16413 relocation += orig_rel.r_addend;
f0158f44 16414 if (tls_type != 0)
411e1bfb 16415 {
989f9879
AM
16416 if (htab->elf.tls_sec == NULL)
16417 relocation = 0;
16418 else
16419 {
f0158f44
AM
16420 if (tls_type & TLS_LD)
16421 relocation = 0;
16422 else
16423 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 16424 if (tls_type & TLS_TPREL)
989f9879
AM
16425 relocation += DTP_OFFSET - TP_OFFSET;
16426 }
5bd4f169 16427
f0158f44 16428 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
16429 {
16430 bfd_put_64 (output_bfd, relocation,
e717da7e 16431 got->contents + off + 8);
676ee2b5 16432 relocation = 1;
7b609f53 16433 }
411e1bfb
AM
16434 }
16435 bfd_put_64 (output_bfd, relocation,
e717da7e 16436 got->contents + off);
5bd4f169
AM
16437 }
16438 }
16439
65f38f15
AM
16440 if (off >= (bfd_vma) -2)
16441 abort ();
16442
bf102f86 16443 relocation = got->output_section->vma + got->output_offset + off;
133a1f60 16444 addend = 0;
c213164a 16445 if (!(r_type == R_PPC64_GOT_PCREL34
87c69f97
AM
16446 || r_type == R_PPC64_GOT_TLSGD_PCREL34
16447 || r_type == R_PPC64_GOT_TLSLD_PCREL34
16448 || r_type == R_PPC64_GOT_TPREL_PCREL34
16449 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
5663e321 16450 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 16451 }
65f38f15
AM
16452 break;
16453
16454 case R_PPC64_PLT16_HA:
16455 case R_PPC64_PLT16_HI:
16456 case R_PPC64_PLT16_LO:
08be3224 16457 case R_PPC64_PLT16_LO_DS:
5663e321
AM
16458 case R_PPC64_PLT_PCREL34:
16459 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
16460 case R_PPC64_PLT32:
16461 case R_PPC64_PLT64:
23cedd1d 16462 case R_PPC64_PLTSEQ:
5663e321 16463 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 16464 case R_PPC64_PLTCALL:
5663e321 16465 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
16466 /* Relocation is to the entry for this symbol in the
16467 procedure linkage table. */
23cedd1d 16468 unresolved_reloc = TRUE;
cbf95972
AM
16469 {
16470 struct plt_entry **plt_list = NULL;
16471 if (h != NULL)
16472 plt_list = &h->elf.plt.plist;
16473 else if (local_got_ents != NULL)
16474 {
16475 struct plt_entry **local_plt = (struct plt_entry **)
16476 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 16477 plt_list = local_plt + r_symndx;
cbf95972
AM
16478 }
16479 if (plt_list)
16480 {
16481 struct plt_entry *ent;
65f38f15 16482
cbf95972
AM
16483 for (ent = *plt_list; ent != NULL; ent = ent->next)
16484 if (ent->plt.offset != (bfd_vma) -1
133a1f60 16485 && ent->addend == orig_rel.r_addend)
cbf95972
AM
16486 {
16487 asection *plt;
08be3224 16488 bfd_vma got;
cbf95972
AM
16489
16490 plt = htab->elf.splt;
16491 if (!htab->elf.dynamic_sections_created
16492 || h == NULL
16493 || h->elf.dynindx == -1)
2d7ad24e
AM
16494 {
16495 if (h != NULL
16496 ? h->elf.type == STT_GNU_IFUNC
16497 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16498 plt = htab->elf.iplt;
16499 else
16500 plt = htab->pltlocal;
16501 }
16502 relocation = (plt->output_section->vma
16503 + plt->output_offset
16504 + ent->plt.offset);
08be3224 16505 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
16506 || r_type == R_PPC64_PLT16_HI
16507 || r_type == R_PPC64_PLT16_LO
16508 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
16509 {
16510 got = (elf_gp (output_bfd)
16511 + htab->sec_info[input_section->id].toc_off);
16512 relocation -= got;
16513 }
133a1f60 16514 addend = 0;
cbf95972
AM
16515 unresolved_reloc = FALSE;
16516 break;
16517 }
16518 }
16519 }
65f38f15 16520 break;
5bd4f169 16521
0b13192e
AM
16522 case R_PPC64_TOC:
16523 /* Relocation value is TOC base. */
16524 relocation = TOCstart;
cf35638d 16525 if (r_symndx == STN_UNDEF)
6f20ed8a 16526 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
16527 else if (unresolved_reloc)
16528 ;
6f20ed8a
AM
16529 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
16530 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
16531 else
16532 unresolved_reloc = TRUE;
ab96bf03 16533 goto dodyn;
0b13192e 16534
5bd4f169
AM
16535 /* TOC16 relocs. We want the offset relative to the TOC base,
16536 which is the address of the start of the TOC plus 0x8000.
16537 The TOC consists of sections .got, .toc, .tocbss, and .plt,
16538 in this order. */
65f38f15
AM
16539 case R_PPC64_TOC16:
16540 case R_PPC64_TOC16_LO:
16541 case R_PPC64_TOC16_HI:
16542 case R_PPC64_TOC16_DS:
16543 case R_PPC64_TOC16_LO_DS:
16544 case R_PPC64_TOC16_HA:
6f20ed8a 16545 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
1bdd8fac
AM
16546 if (h != NULL)
16547 goto dodyn;
5bd4f169
AM
16548 break;
16549
16550 /* Relocate against the beginning of the section. */
65f38f15
AM
16551 case R_PPC64_SECTOFF:
16552 case R_PPC64_SECTOFF_LO:
16553 case R_PPC64_SECTOFF_HI:
16554 case R_PPC64_SECTOFF_DS:
16555 case R_PPC64_SECTOFF_LO_DS:
16556 case R_PPC64_SECTOFF_HA:
4ce794b7 16557 if (sec != NULL)
65f38f15 16558 addend -= sec->output_section->vma;
5bd4f169
AM
16559 break;
16560
25f23106
AM
16561 case R_PPC64_REL16:
16562 case R_PPC64_REL16_LO:
16563 case R_PPC64_REL16_HI:
16564 case R_PPC64_REL16_HA:
4a969973
AM
16565 case R_PPC64_REL16_HIGH:
16566 case R_PPC64_REL16_HIGHA:
16567 case R_PPC64_REL16_HIGHER:
16568 case R_PPC64_REL16_HIGHERA:
16569 case R_PPC64_REL16_HIGHEST:
16570 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
16571 case R_PPC64_REL16_HIGHER34:
16572 case R_PPC64_REL16_HIGHERA34:
16573 case R_PPC64_REL16_HIGHEST34:
16574 case R_PPC64_REL16_HIGHESTA34:
a680de9a 16575 case R_PPC64_REL16DX_HA:
721956f4
AM
16576 case R_PPC64_REL14:
16577 case R_PPC64_REL14_BRNTAKEN:
16578 case R_PPC64_REL14_BRTAKEN:
5d1634d7 16579 case R_PPC64_REL24:
05d0e962 16580 case R_PPC64_REL24_NOTOC:
5663e321
AM
16581 case R_PPC64_PCREL34:
16582 case R_PPC64_PCREL28:
5d1634d7
AM
16583 break;
16584
411e1bfb
AM
16585 case R_PPC64_TPREL16:
16586 case R_PPC64_TPREL16_LO:
16587 case R_PPC64_TPREL16_HI:
16588 case R_PPC64_TPREL16_HA:
16589 case R_PPC64_TPREL16_DS:
16590 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
16591 case R_PPC64_TPREL16_HIGH:
16592 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
16593 case R_PPC64_TPREL16_HIGHER:
16594 case R_PPC64_TPREL16_HIGHERA:
16595 case R_PPC64_TPREL16_HIGHEST:
16596 case R_PPC64_TPREL16_HIGHESTA:
c213164a 16597 case R_PPC64_TPREL34:
766bc656
AM
16598 if (h != NULL
16599 && h->elf.root.type == bfd_link_hash_undefweak
16600 && h->elf.dynindx == -1)
16601 {
16602 /* Make this relocation against an undefined weak symbol
16603 resolve to zero. This is really just a tweak, since
16604 code using weak externs ought to check that they are
16605 defined before using them. */
16606 bfd_byte *p = contents + rel->r_offset - d_offset;
16607
95f0d0d2 16608 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
16609 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
16610 if (insn != 0)
95f0d0d2 16611 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
16612 break;
16613 }
989f9879
AM
16614 if (htab->elf.tls_sec != NULL)
16615 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
16616 /* The TPREL16 relocs shouldn't really be used in shared
16617 libs or with non-local symbols as that will result in
16618 DT_TEXTREL being set, but support them anyway. */
16619 goto dodyn;
411e1bfb
AM
16620
16621 case R_PPC64_DTPREL16:
16622 case R_PPC64_DTPREL16_LO:
16623 case R_PPC64_DTPREL16_HI:
16624 case R_PPC64_DTPREL16_HA:
16625 case R_PPC64_DTPREL16_DS:
16626 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
16627 case R_PPC64_DTPREL16_HIGH:
16628 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
16629 case R_PPC64_DTPREL16_HIGHER:
16630 case R_PPC64_DTPREL16_HIGHERA:
16631 case R_PPC64_DTPREL16_HIGHEST:
16632 case R_PPC64_DTPREL16_HIGHESTA:
c213164a 16633 case R_PPC64_DTPREL34:
989f9879
AM
16634 if (htab->elf.tls_sec != NULL)
16635 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
16636 break;
16637
45965137
AM
16638 case R_PPC64_ADDR64_LOCAL:
16639 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
16640 ? h->elf.other
16641 : sym->st_other);
16642 break;
16643
e515b051
AM
16644 case R_PPC64_DTPMOD64:
16645 relocation = 1;
16646 addend = 0;
16647 goto dodyn;
16648
411e1bfb 16649 case R_PPC64_TPREL64:
989f9879
AM
16650 if (htab->elf.tls_sec != NULL)
16651 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
16652 goto dodyn;
16653
16654 case R_PPC64_DTPREL64:
989f9879
AM
16655 if (htab->elf.tls_sec != NULL)
16656 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 16657 /* Fall through. */
411e1bfb 16658
65f38f15
AM
16659 /* Relocations that may need to be propagated if this is a
16660 dynamic object. */
04c9666a 16661 case R_PPC64_REL30:
65f38f15
AM
16662 case R_PPC64_REL32:
16663 case R_PPC64_REL64:
16664 case R_PPC64_ADDR14:
16665 case R_PPC64_ADDR14_BRNTAKEN:
16666 case R_PPC64_ADDR14_BRTAKEN:
16667 case R_PPC64_ADDR16:
16668 case R_PPC64_ADDR16_DS:
16669 case R_PPC64_ADDR16_HA:
16670 case R_PPC64_ADDR16_HI:
f9c6b907
AM
16671 case R_PPC64_ADDR16_HIGH:
16672 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
16673 case R_PPC64_ADDR16_HIGHER:
16674 case R_PPC64_ADDR16_HIGHERA:
16675 case R_PPC64_ADDR16_HIGHEST:
16676 case R_PPC64_ADDR16_HIGHESTA:
16677 case R_PPC64_ADDR16_LO:
16678 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
16679 case R_PPC64_ADDR16_HIGHER34:
16680 case R_PPC64_ADDR16_HIGHERA34:
16681 case R_PPC64_ADDR16_HIGHEST34:
16682 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 16683 case R_PPC64_ADDR24:
65f38f15
AM
16684 case R_PPC64_ADDR32:
16685 case R_PPC64_ADDR64:
16686 case R_PPC64_UADDR16:
16687 case R_PPC64_UADDR32:
16688 case R_PPC64_UADDR64:
5663e321
AM
16689 case R_PPC64_D34:
16690 case R_PPC64_D34_LO:
16691 case R_PPC64_D34_HI30:
16692 case R_PPC64_D34_HA30:
16693 case R_PPC64_D28:
411e1bfb 16694 dodyn:
5d1634d7 16695 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
16696 break;
16697
41bd81ab
AM
16698 if (NO_OPD_RELOCS && is_opd)
16699 break;
16700
8a9e8e72 16701 if (bfd_link_pic (info)
b1b07054 16702 ? ((h == NULL
190eb1dd 16703 || h->elf.dyn_relocs != NULL)
b1b07054
AM
16704 && ((h != NULL && pc_dynrelocs (h))
16705 || must_be_dyn_reloc (info, r_type)))
8a9e8e72 16706 : (h != NULL
190eb1dd 16707 ? h->elf.dyn_relocs != NULL
d311bc8b 16708 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 16709 {
b34976b6 16710 bfd_boolean skip, relocate;
65f38f15 16711 asection *sreloc;
1cf1f670 16712 bfd_vma out_off;
82e66161 16713 long indx = 0;
65f38f15
AM
16714
16715 /* When generating a dynamic object, these relocations
16716 are copied into the output file to be resolved at run
16717 time. */
16718
b34976b6
AM
16719 skip = FALSE;
16720 relocate = FALSE;
65f38f15 16721
1cf1f670
AM
16722 out_off = _bfd_elf_section_offset (output_bfd, info,
16723 input_section, rel->r_offset);
16724 if (out_off == (bfd_vma) -1)
b34976b6 16725 skip = TRUE;
1cf1f670 16726 else if (out_off == (bfd_vma) -2)
b34976b6 16727 skip = TRUE, relocate = TRUE;
1cf1f670
AM
16728 out_off += (input_section->output_section->vma
16729 + input_section->output_offset);
16730 outrel.r_offset = out_off;
411e1bfb 16731 outrel.r_addend = rel->r_addend;
65f38f15 16732
1cf1f670
AM
16733 /* Optimize unaligned reloc use. */
16734 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
16735 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
16736 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
16737 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
16738 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
16739 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
16740 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16741 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16742 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16743
65f38f15 16744 if (skip)
0bb2d96a 16745 memset (&outrel, 0, sizeof outrel);
9e51d549
AM
16746 else if (h != NULL
16747 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
16748 && !is_opd
16749 && r_type != R_PPC64_TOC)
14acf4dc 16750 {
82e66161
AM
16751 indx = h->elf.dynindx;
16752 BFD_ASSERT (indx != -1);
16753 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 16754 }
65f38f15
AM
16755 else
16756 {
41bd81ab
AM
16757 /* This symbol is local, or marked to become local,
16758 or this is an opd section reloc which must point
16759 at a local function. */
65f38f15 16760 outrel.r_addend += relocation;
e86ce104 16761 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 16762 {
3fad3c7c 16763 if (is_opd && h != NULL)
afbe61cf
AM
16764 {
16765 /* Lie about opd entries. This case occurs
16766 when building shared libraries and we
16767 reference a function in another shared
3fad3c7c
AM
16768 lib. The same thing happens for a weak
16769 definition in an application that's
16770 overridden by a strong definition in a
16771 shared lib. (I believe this is a generic
16772 bug in binutils handling of weak syms.)
16773 In these cases we won't use the opd
1e2f5b6e 16774 entry in this lib. */
b34976b6 16775 unresolved_reloc = FALSE;
afbe61cf 16776 }
25f23106
AM
16777 if (!is_opd
16778 && r_type == R_PPC64_ADDR64
16779 && (h != NULL
16780 ? h->elf.type == STT_GNU_IFUNC
16781 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16782 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16783 else
16784 {
16785 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 16786
25f23106
AM
16787 /* We need to relocate .opd contents for ld.so.
16788 Prelink also wants simple and consistent rules
16789 for relocs. This make all RELATIVE relocs have
16790 *r_offset equal to r_addend. */
16791 relocate = TRUE;
16792 }
65f38f15
AM
16793 }
16794 else
16795 {
25f23106
AM
16796 if (h != NULL
16797 ? h->elf.type == STT_GNU_IFUNC
16798 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16799 {
25f53a85 16800 info->callbacks->einfo
695344c0 16801 /* xgettext:c-format */
174d0a74 16802 (_("%H: %s for indirect "
c1c8c1ef 16803 "function `%pT' unsupported\n"),
25f53a85 16804 input_bfd, input_section, rel->r_offset,
25f23106
AM
16805 ppc64_elf_howto_table[r_type]->name,
16806 sym_name);
16807 ret = FALSE;
16808 }
cf35638d 16809 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
16810 ;
16811 else if (sec == NULL || sec->owner == NULL)
16812 {
16813 bfd_set_error (bfd_error_bad_value);
b34976b6 16814 return FALSE;
65f38f15
AM
16815 }
16816 else
16817 {
f26a3287 16818 asection *osec = sec->output_section;
65f38f15 16819
f26a3287
AM
16820 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16821 {
16822 /* TLS symbol values are relative to the
16823 TLS segment. Dynamic relocations for
16824 local TLS symbols therefore can't be
16825 reduced to a relocation against their
16826 section symbol because it holds the
16827 address of the section, not a value
16828 relative to the TLS segment. We could
16829 change the .tdata dynamic section symbol
16830 to be zero value but STN_UNDEF works
16831 and is used elsewhere, eg. for TPREL64
16832 GOT relocs against local TLS symbols. */
16833 osec = htab->elf.tls_sec;
16834 indx = 0;
16835 }
16836 else
74541ad4 16837 {
74541ad4 16838 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
16839 if (indx == 0)
16840 {
16841 if ((osec->flags & SEC_READONLY) == 0
16842 && htab->elf.data_index_section != NULL)
16843 osec = htab->elf.data_index_section;
16844 else
16845 osec = htab->elf.text_index_section;
16846 indx = elf_section_data (osec)->dynindx;
16847 }
16848 BFD_ASSERT (indx != 0);
74541ad4 16849 }
74541ad4 16850
65f38f15
AM
16851 /* We are turning this relocation into one
16852 against a section symbol, so subtract out
16853 the output section's address but not the
16854 offset of the input section in the output
16855 section. */
16856 outrel.r_addend -= osec->vma;
16857 }
16858
16859 outrel.r_info = ELF64_R_INFO (indx, r_type);
16860 }
16861 }
16862
16863 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
16864 if (h != NULL
16865 ? h->elf.type == STT_GNU_IFUNC
16866 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
16867 {
16868 sreloc = htab->elf.irelplt;
efb2a7b4 16869 if (indx == 0 || is_static_defined (&h->elf))
cebd6b8a 16870 htab->elf.ifunc_resolvers = TRUE;
82e66161 16871 }
65f38f15
AM
16872 if (sreloc == NULL)
16873 abort ();
16874
dfbb6ac9
AM
16875 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16876 >= sreloc->size)
16877 abort ();
947216bf
AM
16878 loc = sreloc->contents;
16879 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
16880 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16881
e59a1001
AM
16882 if (!warned_dynamic
16883 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
16884 {
16885 info->callbacks->einfo
16886 /* xgettext:c-format */
16887 (_("%X%P: %pB: %s against %pT "
16888 "is not supported by glibc as a dynamic relocation\n"),
16889 input_bfd,
16890 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
16891 sym_name);
16892 warned_dynamic = TRUE;
16893 }
16894
65f38f15
AM
16895 /* If this reloc is against an external symbol, it will
16896 be computed at runtime, so there's no need to do
81407a69
AM
16897 anything now. However, for the sake of prelink ensure
16898 that the section contents are a known value. */
2cdcc330 16899 if (!relocate)
81407a69
AM
16900 {
16901 unresolved_reloc = FALSE;
16902 /* The value chosen here is quite arbitrary as ld.so
16903 ignores section contents except for the special
16904 case of .opd where the contents might be accessed
16905 before relocation. Choose zero, as that won't
16906 cause reloc overflow. */
16907 relocation = 0;
16908 addend = 0;
16909 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16910 to improve backward compatibility with older
16911 versions of ld. */
16912 if (r_type == R_PPC64_ADDR64)
16913 addend = outrel.r_addend;
16914 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 16915 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 16916 addend = outrel.r_offset;
81407a69 16917 }
65f38f15 16918 }
5bd4f169
AM
16919 break;
16920
65f38f15
AM
16921 case R_PPC64_COPY:
16922 case R_PPC64_GLOB_DAT:
16923 case R_PPC64_JMP_SLOT:
25f23106 16924 case R_PPC64_JMP_IREL:
65f38f15
AM
16925 case R_PPC64_RELATIVE:
16926 /* We shouldn't ever see these dynamic relocs in relocatable
16927 files. */
ae9a127f 16928 /* Fall through. */
65f38f15
AM
16929
16930 case R_PPC64_PLTGOT16:
16931 case R_PPC64_PLTGOT16_DS:
16932 case R_PPC64_PLTGOT16_HA:
16933 case R_PPC64_PLTGOT16_HI:
16934 case R_PPC64_PLTGOT16_LO:
16935 case R_PPC64_PLTGOT16_LO_DS:
16936 case R_PPC64_PLTREL32:
16937 case R_PPC64_PLTREL64:
16938 /* These ones haven't been implemented yet. */
16939
25f53a85 16940 info->callbacks->einfo
695344c0 16941 /* xgettext:c-format */
c1c8c1ef 16942 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 16943 input_bfd,
4ce794b7 16944 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
16945
16946 bfd_set_error (bfd_error_invalid_operation);
b34976b6 16947 ret = FALSE;
c316a17c 16948 goto copy_reloc;
65f38f15 16949 }
5bd4f169 16950
67f0cbdb
AM
16951 /* Multi-instruction sequences that access the TOC can be
16952 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 16953 to nop; addi rb,r2,x; */
67f0cbdb
AM
16954 switch (r_type)
16955 {
16956 default:
16957 break;
16958
16959 case R_PPC64_GOT_TLSLD16_HI:
16960 case R_PPC64_GOT_TLSGD16_HI:
16961 case R_PPC64_GOT_TPREL16_HI:
16962 case R_PPC64_GOT_DTPREL16_HI:
16963 case R_PPC64_GOT16_HI:
16964 case R_PPC64_TOC16_HI:
16965 /* These relocs would only be useful if building up an
16966 offset to later add to r2, perhaps in an indexed
16967 addressing mode instruction. Don't try to optimize.
16968 Unfortunately, the possibility of someone building up an
16969 offset like this or even with the HA relocs, means that
16970 we need to check the high insn when optimizing the low
16971 insn. */
16972 break;
16973
5663e321
AM
16974 case R_PPC64_PLTCALL_NOTOC:
16975 if (!unresolved_reloc)
16976 htab->notoc_plt = 1;
16977 /* Fall through. */
23cedd1d
AM
16978 case R_PPC64_PLTCALL:
16979 if (unresolved_reloc)
16980 {
16981 /* No plt entry. Make this into a direct call. */
16982 bfd_byte *p = contents + rel->r_offset;
16983 insn = bfd_get_32 (input_bfd, p);
16984 insn &= 1;
16985 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
16986 if (r_type == R_PPC64_PLTCALL)
16987 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
16988 unresolved_reloc = save_unresolved_reloc;
16989 r_type = R_PPC64_REL24;
16990 }
16991 break;
16992
5663e321 16993 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
16994 case R_PPC64_PLTSEQ:
16995 if (unresolved_reloc)
16996 {
16997 unresolved_reloc = FALSE;
16998 goto nop_it;
16999 }
17000 break;
17001
5663e321
AM
17002 case R_PPC64_PLT_PCREL34_NOTOC:
17003 if (!unresolved_reloc)
17004 htab->notoc_plt = 1;
17005 /* Fall through. */
17006 case R_PPC64_PLT_PCREL34:
17007 if (unresolved_reloc)
17008 {
17009 bfd_byte *p = contents + rel->r_offset;
17010 bfd_put_32 (input_bfd, PNOP >> 32, p);
17011 bfd_put_32 (input_bfd, PNOP, p + 4);
17012 unresolved_reloc = FALSE;
17013 goto copy_reloc;
17014 }
17015 break;
17016
23cedd1d
AM
17017 case R_PPC64_PLT16_HA:
17018 if (unresolved_reloc)
17019 {
17020 unresolved_reloc = FALSE;
17021 goto nop_it;
17022 }
17023 /* Fall through. */
67f0cbdb
AM
17024 case R_PPC64_GOT_TLSLD16_HA:
17025 case R_PPC64_GOT_TLSGD16_HA:
17026 case R_PPC64_GOT_TPREL16_HA:
17027 case R_PPC64_GOT_DTPREL16_HA:
17028 case R_PPC64_GOT16_HA:
17029 case R_PPC64_TOC16_HA:
98528052 17030 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 17031 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 17032 {
23cedd1d
AM
17033 bfd_byte *p;
17034 nop_it:
17035 p = contents + (rel->r_offset & ~3);
98528052 17036 bfd_put_32 (input_bfd, NOP, p);
d830549d 17037 goto copy_reloc;
98528052 17038 }
67f0cbdb
AM
17039 break;
17040
23cedd1d
AM
17041 case R_PPC64_PLT16_LO:
17042 case R_PPC64_PLT16_LO_DS:
17043 if (unresolved_reloc)
17044 {
17045 unresolved_reloc = FALSE;
17046 goto nop_it;
17047 }
17048 /* Fall through. */
67f0cbdb
AM
17049 case R_PPC64_GOT_TLSLD16_LO:
17050 case R_PPC64_GOT_TLSGD16_LO:
17051 case R_PPC64_GOT_TPREL16_LO_DS:
17052 case R_PPC64_GOT_DTPREL16_LO_DS:
17053 case R_PPC64_GOT16_LO:
17054 case R_PPC64_GOT16_LO_DS:
17055 case R_PPC64_TOC16_LO:
17056 case R_PPC64_TOC16_LO_DS:
98528052 17057 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 17058 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
17059 {
17060 bfd_byte *p = contents + (rel->r_offset & ~3);
17061 insn = bfd_get_32 (input_bfd, p);
2365f8d7 17062 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
560c8763
AM
17063 {
17064 /* Transform addic to addi when we change reg. */
2365f8d7 17065 insn &= ~((0x3fu << 26) | (0x1f << 16));
560c8763
AM
17066 insn |= (14u << 26) | (2 << 16);
17067 }
17068 else
67f0cbdb 17069 {
98528052
AM
17070 insn &= ~(0x1f << 16);
17071 insn |= 2 << 16;
67f0cbdb 17072 }
560c8763 17073 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
17074 }
17075 break;
9a23f96e
AM
17076
17077 case R_PPC64_TPREL16_HA:
17078 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
17079 {
17080 bfd_byte *p = contents + (rel->r_offset & ~3);
252dcdf4
AM
17081 bfd_put_32 (input_bfd, NOP, p);
17082 goto copy_reloc;
9a23f96e
AM
17083 }
17084 break;
17085
17086 case R_PPC64_TPREL16_LO:
17087 case R_PPC64_TPREL16_LO_DS:
17088 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
17089 {
17090 bfd_byte *p = contents + (rel->r_offset & ~3);
17091 insn = bfd_get_32 (input_bfd, p);
17092 insn &= ~(0x1f << 16);
17093 insn |= 13 << 16;
17094 bfd_put_32 (input_bfd, insn, p);
17095 }
17096 break;
67f0cbdb
AM
17097 }
17098
65f38f15
AM
17099 /* Do any further special processing. */
17100 switch (r_type)
17101 {
17102 default:
17103 break;
17104
25f23106 17105 case R_PPC64_REL16_HA:
4a969973
AM
17106 case R_PPC64_REL16_HIGHA:
17107 case R_PPC64_REL16_HIGHERA:
17108 case R_PPC64_REL16_HIGHESTA:
a680de9a 17109 case R_PPC64_REL16DX_HA:
f9c6b907
AM
17110 case R_PPC64_ADDR16_HA:
17111 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
17112 case R_PPC64_ADDR16_HIGHERA:
17113 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
17114 case R_PPC64_TOC16_HA:
17115 case R_PPC64_SECTOFF_HA:
411e1bfb 17116 case R_PPC64_TPREL16_HA:
f9c6b907 17117 case R_PPC64_TPREL16_HIGHA:
411e1bfb 17118 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 17119 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
17120 case R_PPC64_DTPREL16_HA:
17121 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 17122 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 17123 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
17124 /* It's just possible that this symbol is a weak symbol
17125 that's not actually defined anywhere. In that case,
17126 'sec' would be NULL, and we should leave the symbol
17127 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
17128 if (sec == NULL)
17129 break;
1a0670f3 17130 /* Fall through. */
5c5f6e17
AM
17131
17132 case R_PPC64_GOT16_HA:
17133 case R_PPC64_PLTGOT16_HA:
17134 case R_PPC64_PLT16_HA:
17135 case R_PPC64_GOT_TLSGD16_HA:
17136 case R_PPC64_GOT_TLSLD16_HA:
17137 case R_PPC64_GOT_TPREL16_HA:
17138 case R_PPC64_GOT_DTPREL16_HA:
17139 /* Add 0x10000 if sign bit in 0:15 is set.
17140 Bits 0:15 are not used. */
17141 addend += 0x8000;
65f38f15
AM
17142 break;
17143
5663e321
AM
17144 case R_PPC64_D34_HA30:
17145 case R_PPC64_ADDR16_HIGHERA34:
17146 case R_PPC64_ADDR16_HIGHESTA34:
17147 case R_PPC64_REL16_HIGHERA34:
17148 case R_PPC64_REL16_HIGHESTA34:
17149 if (sec != NULL)
17150 addend += 1ULL << 33;
17151 break;
17152
65f38f15
AM
17153 case R_PPC64_ADDR16_DS:
17154 case R_PPC64_ADDR16_LO_DS:
17155 case R_PPC64_GOT16_DS:
17156 case R_PPC64_GOT16_LO_DS:
17157 case R_PPC64_PLT16_LO_DS:
17158 case R_PPC64_SECTOFF_DS:
17159 case R_PPC64_SECTOFF_LO_DS:
17160 case R_PPC64_TOC16_DS:
17161 case R_PPC64_TOC16_LO_DS:
17162 case R_PPC64_PLTGOT16_DS:
17163 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
17164 case R_PPC64_GOT_TPREL16_DS:
17165 case R_PPC64_GOT_TPREL16_LO_DS:
17166 case R_PPC64_GOT_DTPREL16_DS:
17167 case R_PPC64_GOT_DTPREL16_LO_DS:
17168 case R_PPC64_TPREL16_DS:
17169 case R_PPC64_TPREL16_LO_DS:
17170 case R_PPC64_DTPREL16_DS:
17171 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
17172 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17173 mask = 3;
a680de9a
PB
17174 /* If this reloc is against an lq, lxv, or stxv insn, then
17175 the value must be a multiple of 16. This is somewhat of
17176 a hack, but the "correct" way to do this by defining _DQ
17177 forms of all the _DS relocs bloats all reloc switches in
17178 this file. It doesn't make much sense to use these
17179 relocs in data, so testing the insn should be safe. */
2365f8d7
AM
17180 if ((insn & (0x3fu << 26)) == (56u << 26)
17181 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 17182 mask = 15;
a680de9a
PB
17183 relocation += addend;
17184 addend = insn & (mask ^ 3);
17185 if ((relocation & mask) != 0)
65f38f15 17186 {
a680de9a 17187 relocation ^= relocation & mask;
25f53a85 17188 info->callbacks->einfo
695344c0 17189 /* xgettext:c-format */
174d0a74 17190 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 17191 input_bfd, input_section, rel->r_offset,
d830549d 17192 ppc64_elf_howto_table[r_type]->name,
adadcc0c 17193 mask + 1);
65f38f15 17194 bfd_set_error (bfd_error_bad_value);
b34976b6 17195 ret = FALSE;
c316a17c 17196 goto copy_reloc;
65f38f15
AM
17197 }
17198 break;
5bd4f169
AM
17199 }
17200
239e1f3a
AM
17201 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17202 because such sections are not SEC_ALLOC and thus ld.so will
17203 not process them. */
d830549d 17204 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 17205 if (unresolved_reloc
239e1f3a 17206 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
17207 && h->elf.def_dynamic)
17208 && _bfd_elf_section_offset (output_bfd, info, input_section,
17209 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 17210 {
25f53a85 17211 info->callbacks->einfo
695344c0 17212 /* xgettext:c-format */
c1c8c1ef 17213 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 17214 input_bfd, input_section, rel->r_offset,
b80eed39 17215 howto->name,
039b3fef 17216 h->elf.root.root.string);
b34976b6 17217 ret = FALSE;
9c07fe7c 17218 }
5bd4f169 17219
b80eed39
AM
17220 /* 16-bit fields in insns mostly have signed values, but a
17221 few insns have 16-bit unsigned values. Really, we should
17222 have different reloc types. */
17223 if (howto->complain_on_overflow != complain_overflow_dont
17224 && howto->dst_mask == 0xffff
17225 && (input_section->flags & SEC_CODE) != 0)
17226 {
17227 enum complain_overflow complain = complain_overflow_signed;
17228
17229 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 17230 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
a47622ac
AM
17231 complain = complain_overflow_bitfield;
17232 else if (howto->rightshift == 0
2365f8d7
AM
17233 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17234 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17235 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17236 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17237 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17238 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
17239 complain = complain_overflow_unsigned;
17240 if (howto->complain_on_overflow != complain)
17241 {
17242 alt_howto = *howto;
17243 alt_howto.complain_on_overflow = complain;
17244 howto = &alt_howto;
17245 }
17246 }
17247
5663e321 17248 switch (r_type)
a680de9a 17249 {
5663e321
AM
17250 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17251 case R_PPC64_D34:
17252 case R_PPC64_D34_LO:
17253 case R_PPC64_D34_HI30:
17254 case R_PPC64_D34_HA30:
17255 case R_PPC64_PCREL34:
17256 case R_PPC64_GOT_PCREL34:
c213164a
AM
17257 case R_PPC64_TPREL34:
17258 case R_PPC64_DTPREL34:
87c69f97
AM
17259 case R_PPC64_GOT_TLSGD_PCREL34:
17260 case R_PPC64_GOT_TLSLD_PCREL34:
17261 case R_PPC64_GOT_TPREL_PCREL34:
17262 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
17263 case R_PPC64_PLT_PCREL34:
17264 case R_PPC64_PLT_PCREL34_NOTOC:
17265 case R_PPC64_D28:
17266 case R_PPC64_PCREL28:
17267 if (rel->r_offset + 8 > input_section->size)
17268 r = bfd_reloc_outofrange;
17269 else
17270 {
5663e321
AM
17271 relocation += addend;
17272 if (howto->pc_relative)
17273 relocation -= (rel->r_offset
17274 + input_section->output_offset
17275 + input_section->output_section->vma);
17276 relocation >>= howto->rightshift;
17277
17278 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17279 pinsn <<= 32;
17280 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17281
17282 pinsn &= ~howto->dst_mask;
17283 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17284 & howto->dst_mask);
17285 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17286 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17287 r = bfd_reloc_ok;
17288 if (howto->complain_on_overflow == complain_overflow_signed
17289 && (relocation + (1ULL << (howto->bitsize - 1))
17290 >= 1ULL << howto->bitsize))
17291 r = bfd_reloc_overflow;
17292 }
17293 break;
17294
17295 case R_PPC64_REL16DX_HA:
a680de9a
PB
17296 if (rel->r_offset + 4 > input_section->size)
17297 r = bfd_reloc_outofrange;
17298 else
17299 {
17300 relocation += addend;
17301 relocation -= (rel->r_offset
17302 + input_section->output_offset
17303 + input_section->output_section->vma);
3de43e7b 17304 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
17305 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17306 insn &= ~0x1fffc1;
3de43e7b 17307 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
17308 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17309 r = bfd_reloc_ok;
3de43e7b 17310 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
17311 r = bfd_reloc_overflow;
17312 }
5663e321
AM
17313 break;
17314
17315 default:
17316 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17317 contents, rel->r_offset,
17318 relocation, addend);
a680de9a 17319 }
5bd4f169 17320
ef60b7ff 17321 if (r != bfd_reloc_ok)
5bd4f169 17322 {
bc30df16 17323 char *more_info = NULL;
b80eed39 17324 const char *reloc_name = howto->name;
bc30df16
AM
17325
17326 if (reloc_dest != DEST_NORMAL)
17327 {
17328 more_info = bfd_malloc (strlen (reloc_name) + 8);
17329 if (more_info != NULL)
17330 {
17331 strcpy (more_info, reloc_name);
17332 strcat (more_info, (reloc_dest == DEST_OPD
17333 ? " (OPD)" : " (stub)"));
17334 reloc_name = more_info;
17335 }
17336 }
17337
cd27b276 17338 if (r == bfd_reloc_overflow)
5bd4f169 17339 {
8131c122
AM
17340 /* On code like "if (foo) foo();" don't report overflow
17341 on a branch to zero when foo is undefined. */
17342 if (!warned
17343 && (reloc_dest == DEST_STUB
17344 || !(h != NULL
17345 && (h->elf.root.type == bfd_link_hash_undefweak
17346 || h->elf.root.type == bfd_link_hash_undefined)
17347 && is_branch_reloc (r_type))))
9e51d549
AM
17348 info->callbacks->reloc_overflow
17349 (info, (struct bfd_link_hash_entry *) h, sym_name,
17350 reloc_name, orig_rel.r_addend, input_bfd, input_section,
17351 rel->r_offset);
ef60b7ff
AM
17352 }
17353 else
17354 {
25f53a85 17355 info->callbacks->einfo
695344c0 17356 /* xgettext:c-format */
c1c8c1ef 17357 (_("%H: %s against `%pT': error %d\n"),
25f53a85 17358 input_bfd, input_section, rel->r_offset,
bc30df16 17359 reloc_name, sym_name, (int) r);
b34976b6 17360 ret = FALSE;
ef60b7ff 17361 }
c9594989 17362 free (more_info);
5bd4f169 17363 }
c316a17c
AM
17364 copy_reloc:
17365 if (wrel != rel)
17366 *wrel = *rel;
17367 }
17368
17369 if (wrel != rel)
17370 {
17371 Elf_Internal_Shdr *rel_hdr;
17372 size_t deleted = rel - wrel;
17373
17374 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17375 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17376 if (rel_hdr->sh_size == 0)
17377 {
17378 /* It is too late to remove an empty reloc section. Leave
17379 one NONE reloc.
17380 ??? What is wrong with an empty section??? */
17381 rel_hdr->sh_size = rel_hdr->sh_entsize;
17382 deleted -= 1;
17383 }
17384 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
17385 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17386 input_section->reloc_count -= deleted;
5bd4f169
AM
17387 }
17388
645ea6a9
AM
17389 /* If we're emitting relocations, then shortly after this function
17390 returns, reloc offsets and addends for this section will be
17391 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
17392 file rather than the input. Save a copy of the relocs for
17393 opd_entry_value. */
0e1862bb 17394 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
17395 {
17396 bfd_size_type amt;
17397 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
17398 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
17399 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
17400 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
17401 if (rel == NULL)
17402 return FALSE;
17403 memcpy (rel, relocs, amt);
17404 }
5bd4f169
AM
17405 return ret;
17406}
17407
754021d0
AM
17408/* Adjust the value of any local symbols in opd sections. */
17409
6e0b88f1 17410static int
754021d0
AM
17411ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
17412 const char *name ATTRIBUTE_UNUSED,
17413 Elf_Internal_Sym *elfsym,
17414 asection *input_sec,
17415 struct elf_link_hash_entry *h)
17416{
74f0fb50
AM
17417 struct _opd_sec_data *opd;
17418 long adjust;
754021d0
AM
17419 bfd_vma value;
17420
4025353c 17421 if (h != NULL)
6e0b88f1 17422 return 1;
4025353c 17423
74f0fb50
AM
17424 opd = get_opd_info (input_sec);
17425 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 17426 return 1;
754021d0
AM
17427
17428 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 17429 if (!bfd_link_relocatable (info))
754021d0
AM
17430 value -= input_sec->output_section->vma;
17431
51aecdc5 17432 adjust = opd->adjust[OPD_NDX (value)];
4025353c 17433 if (adjust == -1)
6e0b88f1
AM
17434 return 2;
17435
17436 elfsym->st_value += adjust;
17437 return 1;
754021d0
AM
17438}
17439
5bd4f169
AM
17440/* Finish up dynamic symbol handling. We set the contents of various
17441 dynamic sections here. */
17442
b34976b6 17443static bfd_boolean
4ce794b7
AM
17444ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
17445 struct bfd_link_info *info,
17446 struct elf_link_hash_entry *h,
4aef7643 17447 Elf_Internal_Sym *sym)
5bd4f169 17448{
65f38f15 17449 struct ppc_link_hash_table *htab;
8387904d 17450 struct plt_entry *ent;
5bd4f169 17451
65f38f15 17452 htab = ppc_hash_table (info);
4dfe6ac6
NC
17453 if (htab == NULL)
17454 return FALSE;
5bd4f169 17455
49c09209
AM
17456 if (!htab->opd_abi && !h->def_regular)
17457 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
17458 if (ent->plt.offset != (bfd_vma) -1)
17459 {
17460 /* Mark the symbol as undefined, rather than as
17461 defined in glink. Leave the value if there were
17462 any relocations where pointer equality matters
17463 (this is a clue for the dynamic linker, to make
17464 function pointer comparisons work between an
17465 application and shared library), otherwise set it
17466 to zero. */
17467 sym->st_shndx = SHN_UNDEF;
17468 if (!h->pointer_equality_needed)
17469 sym->st_value = 0;
17470 else if (!h->ref_regular_nonweak)
17471 {
17472 /* This breaks function pointer comparisons, but
17473 that is better than breaking tests for a NULL
17474 function pointer. */
17475 sym->st_value = 0;
17476 }
17477 break;
17478 }
5bd4f169 17479
1bdd8fac
AM
17480 if (h->needs_copy
17481 && (h->root.type == bfd_link_hash_defined
17482 || h->root.type == bfd_link_hash_defweak)
17483 && (h->root.u.def.section == htab->elf.sdynbss
17484 || h->root.u.def.section == htab->elf.sdynrelro))
5bd4f169 17485 {
65f38f15 17486 /* This symbol needs a copy reloc. Set it up. */
49c09209 17487 Elf_Internal_Rela rela;
5474d94f 17488 asection *srel;
49c09209 17489 bfd_byte *loc;
5bd4f169 17490
1bdd8fac 17491 if (h->dynindx == -1)
65f38f15 17492 abort ();
5bd4f169 17493
ed7007c1 17494 rela.r_offset = defined_sym_val (h);
5bd4f169
AM
17495 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
17496 rela.r_addend = 0;
afbf7e8e 17497 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
17498 srel = htab->elf.sreldynrelro;
17499 else
17500 srel = htab->elf.srelbss;
17501 loc = srel->contents;
17502 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 17503 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
17504 }
17505
b34976b6 17506 return TRUE;
5bd4f169
AM
17507}
17508
65f38f15
AM
17509/* Used to decide how to sort relocs in an optimal manner for the
17510 dynamic linker, before writing them out. */
17511
17512static enum elf_reloc_type_class
7e612e98
AM
17513ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
17514 const asection *rel_sec,
17515 const Elf_Internal_Rela *rela)
65f38f15 17516{
04c9666a 17517 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
17518 struct ppc_link_hash_table *htab = ppc_hash_table (info);
17519
33e44f2e 17520 if (rel_sec == htab->elf.irelplt)
7e612e98 17521 return reloc_class_ifunc;
a33d1f77 17522
4ce794b7 17523 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 17524 switch (r_type)
65f38f15
AM
17525 {
17526 case R_PPC64_RELATIVE:
17527 return reloc_class_relative;
17528 case R_PPC64_JMP_SLOT:
17529 return reloc_class_plt;
17530 case R_PPC64_COPY:
17531 return reloc_class_copy;
17532 default:
17533 return reloc_class_normal;
17534 }
17535}
17536
5bd4f169
AM
17537/* Finish up the dynamic sections. */
17538
b34976b6 17539static bfd_boolean
4ce794b7
AM
17540ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
17541 struct bfd_link_info *info)
5bd4f169 17542{
65f38f15
AM
17543 struct ppc_link_hash_table *htab;
17544 bfd *dynobj;
5bd4f169 17545 asection *sdyn;
5bd4f169 17546
65f38f15 17547 htab = ppc_hash_table (info);
4dfe6ac6
NC
17548 if (htab == NULL)
17549 return FALSE;
17550
65f38f15 17551 dynobj = htab->elf.dynobj;
3d4d4302 17552 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 17553
65f38f15 17554 if (htab->elf.dynamic_sections_created)
5bd4f169 17555 {
5bd4f169
AM
17556 Elf64_External_Dyn *dyncon, *dynconend;
17557
33e44f2e 17558 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 17559 abort ();
5bd4f169
AM
17560
17561 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 17562 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
17563 for (; dyncon < dynconend; dyncon++)
17564 {
17565 Elf_Internal_Dyn dyn;
19397422 17566 asection *s;
5bd4f169
AM
17567
17568 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
17569
17570 switch (dyn.d_tag)
17571 {
65f38f15
AM
17572 default:
17573 continue;
5bd4f169 17574
5d1634d7 17575 case DT_PPC64_GLINK:
4ce794b7 17576 s = htab->glink;
6348e046 17577 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
17578 /* We stupidly defined DT_PPC64_GLINK to be the start
17579 of glink rather than the first entry point, which is
17580 what ld.so needs, and now have a bigger stub to
17581 support automatic multiple TOCs. */
9e390558 17582 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
17583 break;
17584
19397422
AM
17585 case DT_PPC64_OPD:
17586 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
17587 if (s == NULL)
17588 continue;
17589 dyn.d_un.d_ptr = s->vma;
19397422
AM
17590 break;
17591
e8910a83 17592 case DT_PPC64_OPT:
5663e321
AM
17593 if ((htab->do_multi_toc && htab->multi_toc_needed)
17594 || htab->notoc_plt)
e8910a83 17595 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
17596 if (htab->has_plt_localentry0)
17597 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
17598 break;
17599
19397422
AM
17600 case DT_PPC64_OPDSZ:
17601 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
17602 if (s == NULL)
17603 continue;
eea6121a 17604 dyn.d_un.d_val = s->size;
19397422
AM
17605 break;
17606
65f38f15 17607 case DT_PLTGOT:
33e44f2e 17608 s = htab->elf.splt;
6348e046 17609 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
17610 break;
17611
17612 case DT_JMPREL:
33e44f2e 17613 s = htab->elf.srelplt;
6348e046 17614 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 17615 break;
5bd4f169 17616
65f38f15 17617 case DT_PLTRELSZ:
33e44f2e 17618 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 17619 break;
82e66161
AM
17620
17621 case DT_TEXTREL:
cebd6b8a 17622 if (htab->elf.ifunc_resolvers)
82e66161
AM
17623 info->callbacks->einfo
17624 (_("%P: warning: text relocations and GNU indirect "
17625 "functions may result in a segfault at runtime\n"));
17626 continue;
5bd4f169 17627 }
5bd4f169 17628
65f38f15 17629 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 17630 }
5bd4f169
AM
17631 }
17632
6528b6eb
AM
17633 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
17634 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
17635 {
17636 /* Fill in the first entry in the global offset table.
17637 We use it to hold the link-time TOCbase. */
17638 bfd_put_64 (output_bfd,
60ee0d4a 17639 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 17640 htab->elf.sgot->contents);
5d1634d7
AM
17641
17642 /* Set .got entry size. */
2cdcc330
AM
17643 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
17644 = 8;
5d1634d7
AM
17645 }
17646
6528b6eb
AM
17647 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
17648 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
17649 {
17650 /* Set .plt entry size. */
33e44f2e 17651 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 17652 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
17653 }
17654
84f5d08e
AM
17655 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
17656 brlt ourselves if emitrelocations. */
17657 if (htab->brlt != NULL
17658 && htab->brlt->reloc_count != 0
17659 && !_bfd_elf_link_output_relocs (output_bfd,
17660 htab->brlt,
d4730f92 17661 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
17662 elf_section_data (htab->brlt)->relocs,
17663 NULL))
17664 return FALSE;
17665
176a0d42
AM
17666 if (htab->glink != NULL
17667 && htab->glink->reloc_count != 0
17668 && !_bfd_elf_link_output_relocs (output_bfd,
17669 htab->glink,
d4730f92 17670 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
17671 elf_section_data (htab->glink)->relocs,
17672 NULL))
17673 return FALSE;
17674
da44f4e5 17675
df136d64
AM
17676 if (htab->glink_eh_frame != NULL
17677 && htab->glink_eh_frame->size != 0
17678 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
17679 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
17680 htab->glink_eh_frame,
17681 htab->glink_eh_frame->contents))
17682 return FALSE;
58d180e8 17683
e717da7e 17684 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
17685 since we didn't add them to DYNOBJ. We know dynobj is the first
17686 bfd. */
c72f2fb2 17687 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
17688 {
17689 asection *s;
7b53ace3 17690
0c8d6e5c 17691 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
17692 continue;
17693
e717da7e
AM
17694 s = ppc64_elf_tdata (dynobj)->got;
17695 if (s != NULL
eea6121a 17696 && s->size != 0
e717da7e
AM
17697 && s->output_section != bfd_abs_section_ptr
17698 && !bfd_set_section_contents (output_bfd, s->output_section,
17699 s->contents, s->output_offset,
eea6121a 17700 s->size))
e717da7e
AM
17701 return FALSE;
17702 s = ppc64_elf_tdata (dynobj)->relgot;
17703 if (s != NULL
eea6121a 17704 && s->size != 0
e717da7e
AM
17705 && s->output_section != bfd_abs_section_ptr
17706 && !bfd_set_section_contents (output_bfd, s->output_section,
17707 s->contents, s->output_offset,
eea6121a 17708 s->size))
e717da7e
AM
17709 return FALSE;
17710 }
f6c52c13 17711
b34976b6 17712 return TRUE;
5bd4f169
AM
17713}
17714
5bd4f169 17715#include "elf64-target.h"
7b8e7dad
AM
17716
17717/* FreeBSD support */
17718
17719#undef TARGET_LITTLE_SYM
6afcdeb3 17720#define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
7b8e7dad 17721#undef TARGET_LITTLE_NAME
6afcdeb3 17722#define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
7b8e7dad
AM
17723
17724#undef TARGET_BIG_SYM
6d00b590 17725#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
17726#undef TARGET_BIG_NAME
17727#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
17728
17729#undef ELF_OSABI
17730#define ELF_OSABI ELFOSABI_FREEBSD
17731
17732#undef elf64_bed
17733#define elf64_bed elf64_powerpc_fbsd_bed
17734
17735#include "elf64-target.h"
This page took 3.994498 seconds and 4 git commands to generate.