x86: Define GNU_PROPERTY_X86_ISA_1_AVX512_BF16
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
82704155 2 Copyright (C) 1999-2019 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
805fc799 38static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
40static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 42static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 44static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 46static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 48static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 54static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 56static bfd_vma opd_entry_value
aef36ac1 57 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
5bd4f169 58
6d00b590 59#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
ad8e1ba5 60#define TARGET_LITTLE_NAME "elf64-powerpcle"
6d00b590 61#define TARGET_BIG_SYM powerpc_elf64_vec
ad8e1ba5
AM
62#define TARGET_BIG_NAME "elf64-powerpc"
63#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 64#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
65#define ELF_MACHINE_CODE EM_PPC64
66#define ELF_MAXPAGESIZE 0x10000
702d1671
AM
67#define ELF_COMMONPAGESIZE 0x1000
68#define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
ad8e1ba5
AM
69#define elf_info_to_howto ppc64_elf_info_to_howto
70
71#define elf_backend_want_got_sym 0
72#define elf_backend_want_plt_sym 0
73#define elf_backend_plt_alignment 3
74#define elf_backend_plt_not_loaded 1
ad8e1ba5 75#define elf_backend_got_header_size 8
5474d94f 76#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
77#define elf_backend_can_gc_sections 1
78#define elf_backend_can_refcount 1
79#define elf_backend_rela_normal 1
64f52338 80#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 81#define elf_backend_default_execstack 0
ad8e1ba5 82
e717da7e 83#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 84#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 85#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
86#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
87#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
88#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
89#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 90#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 91#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 92#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
93
94#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
95#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
96#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 97#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 98#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 99#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 100#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 101#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 102#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 103#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 104#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 105#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 106#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 107#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 108#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
109#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
110#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 111#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
ad8e1ba5
AM
112#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
113#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 114#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 115#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 116#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
117#define elf_backend_relocate_section ppc64_elf_relocate_section
118#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
119#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
120#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 121#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 122#define elf_backend_special_sections ppc64_elf_special_sections
6911b7dc 123#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 124#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 125#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 126
5bd4f169
AM
127/* The name of the dynamic interpreter. This is put in the .interp
128 section. */
129#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
130
131/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 132#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
2d7ad24e 133#define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
5bd4f169
AM
134
135/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 136#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 137
a078d95a
AM
138/* Offsets to some stack save slots. */
139#define STK_LR 16
140#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 141/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
142 CR save slot. Used only by optimised __tls_get_addr call stub,
143 relying on __tls_get_addr_opt not saving CR.. */
144#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
145
5bd4f169 146/* TOC base pointers offset from start of TOC. */
411e1bfb 147#define TOC_BASE_OFF 0x8000
a27e685f
AM
148/* TOC base alignment. */
149#define TOC_BASE_ALIGN 256
411e1bfb
AM
150
151/* Offset of tp and dtp pointers from start of TLS block. */
152#define TP_OFFSET 0x7000
153#define DTP_OFFSET 0x8000
5bd4f169 154
ad8e1ba5
AM
155/* .plt call stub instructions. The normal stub is like this, but
156 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 157 insert an addi to adjust r11. */
a078d95a 158#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
159#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
160#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
161#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
162#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
163#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
164#define BCTR 0x4e800420 /* bctr */
165
07d6d2b8 166#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
05d0e962
AM
167#define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
168#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
07d6d2b8
AM
169#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
170#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
171
172#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
173#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
174#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
175#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
176#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
177#define BNECTR 0x4ca20420 /* bnectr+ */
178#define BNECTR_P4 0x4ce20420 /* bnectr+ */
794e51c0 179
71a39c98 180#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 181#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 182#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 183
07d6d2b8
AM
184#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
185#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
186#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 187
07d6d2b8 188#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
05d0e962 189#define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
006589cf
AM
190#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
191#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
05d0e962 192#define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
a345bc8d 193#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
05d0e962
AM
194#define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
195#define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
07d6d2b8 196#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
05d0e962
AM
197#define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
198#define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
199#define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
a345bc8d 200
9e390558
AM
201/* __glink_PLTresolve stub instructions. We enter with the index in R0. */
202#define GLINK_PLTRESOLVE_SIZE(htab) \
407aa07c 203 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
ee4bf8d2
AM
204 /* 0: */
205 /* .quad plt0-1f */
206 /* __glink: */
207#define MFLR_R12 0x7d8802a6 /* mflr %12 */
208#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
209 /* 1: */
210#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 211 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 212#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
213#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
214 /* ld %12,0(%11) */
215 /* ld %2,8(%11) */
216 /* mtctr %12 */
217 /* ld %11,16(%11) */
ee4bf8d2 218 /* bctr */
b9e5796b
AM
219#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
220#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
221#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
222#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
223#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
224
225/* Pad with this. */
226#define NOP 0x60000000
227
721956f4
AM
228/* Some other nops. */
229#define CROR_151515 0x4def7b82
230#define CROR_313131 0x4ffffb82
231
cedb70c5 232/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
233#define LI_R0_0 0x38000000 /* li %r0,0 */
234#define B_DOT 0x48000000 /* b . */
235
236/* After that, we need two instructions to load the index, followed by
237 a branch. */
238#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 239#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 240
deb0e272
AM
241/* Instructions used by the save and restore reg functions. */
242#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
243#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
244#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
245#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
246#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
247#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
248#define LI_R12_0 0x39800000 /* li %r12,0 */
249#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
250#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
251#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
252#define BLR 0x4e800020 /* blr */
253
41bd81ab
AM
254/* Since .opd is an array of descriptors and each entry will end up
255 with identical R_PPC64_RELATIVE relocs, there is really no need to
256 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 257 relocate .opd without reloc entries. */
41bd81ab
AM
258#ifndef NO_OPD_RELOCS
259#define NO_OPD_RELOCS 0
260#endif
810d4e75 261
a4b6fadd
AM
262#ifndef ARRAY_SIZE
263#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
264#endif
265
810d4e75
AM
266static inline int
267abiversion (bfd *abfd)
268{
269 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
270}
271
272static inline void
273set_abiversion (bfd *abfd, int ver)
274{
275 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
276 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
277}
5bd4f169
AM
278\f
279/* Relocation HOWTO's. */
46807bf4
AM
280/* Like other ELF RELA targets that don't apply multiple
281 field-altering relocations to the same localation, src_mask is
282 always zero and pcrel_offset is the same as pc_relative.
283 PowerPC can always use a zero bitpos, even when the field is not at
284 the LSB. For example, a REL24 could use rightshift=2, bisize=24
285 and bitpos=2 which matches the ABI description, or as we do here,
286 rightshift=0, bitsize=26 and bitpos=0. */
287#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
288 complain, special_func) \
289 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
290 complain_overflow_ ## complain, special_func, \
291 #type, FALSE, 0, mask, pc_relative)
292
04c9666a 293static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 294
f3185997
NC
295static reloc_howto_type ppc64_elf_howto_raw[] =
296{
5bd4f169 297 /* This reloc does nothing. */
46807bf4
AM
298 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
299 bfd_elf_generic_reloc),
5bd4f169
AM
300
301 /* A standard 32 bit relocation. */
46807bf4
AM
302 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
303 bfd_elf_generic_reloc),
5bd4f169
AM
304
305 /* An absolute 26 bit branch; the lower two bits must be zero.
306 FIXME: we don't check that, we just clear them. */
46807bf4
AM
307 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
308 bfd_elf_generic_reloc),
5bd4f169
AM
309
310 /* A standard 16 bit relocation. */
46807bf4
AM
311 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
312 bfd_elf_generic_reloc),
5bd4f169
AM
313
314 /* A 16 bit relocation without overflow. */
46807bf4
AM
315 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
316 bfd_elf_generic_reloc),
5bd4f169
AM
317
318 /* Bits 16-31 of an address. */
46807bf4
AM
319 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
320 bfd_elf_generic_reloc),
5bd4f169
AM
321
322 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
323 bits, treated as a signed number, is negative. */
46807bf4
AM
324 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
325 ppc64_elf_ha_reloc),
5bd4f169
AM
326
327 /* An absolute 16 bit branch; the lower two bits must be zero.
328 FIXME: we don't check that, we just clear them. */
46807bf4
AM
329 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
330 ppc64_elf_branch_reloc),
5bd4f169
AM
331
332 /* An absolute 16 bit branch, for which bit 10 should be set to
333 indicate that the branch is expected to be taken. The lower two
334 bits must be zero. */
46807bf4
AM
335 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
336 ppc64_elf_brtaken_reloc),
5bd4f169
AM
337
338 /* An absolute 16 bit branch, for which bit 10 should be set to
339 indicate that the branch is not expected to be taken. The lower
340 two bits must be zero. */
46807bf4
AM
341 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
342 ppc64_elf_brtaken_reloc),
5bd4f169
AM
343
344 /* A relative 26 bit branch; the lower two bits must be zero. */
46807bf4
AM
345 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
346 ppc64_elf_branch_reloc),
5bd4f169 347
05d0e962 348 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
46807bf4
AM
349 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
350 ppc64_elf_branch_reloc),
05d0e962 351
5bd4f169 352 /* A relative 16 bit branch; the lower two bits must be zero. */
46807bf4
AM
353 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
354 ppc64_elf_branch_reloc),
5bd4f169
AM
355
356 /* A relative 16 bit branch. Bit 10 should be set to indicate that
357 the branch is expected to be taken. The lower two bits must be
358 zero. */
46807bf4
AM
359 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
360 ppc64_elf_brtaken_reloc),
5bd4f169
AM
361
362 /* A relative 16 bit branch. Bit 10 should be set to indicate that
363 the branch is not expected to be taken. The lower two bits must
364 be zero. */
46807bf4
AM
365 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
366 ppc64_elf_brtaken_reloc),
5bd4f169
AM
367
368 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
369 symbol. */
46807bf4
AM
370 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
371 ppc64_elf_unhandled_reloc),
5bd4f169
AM
372
373 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
374 the symbol. */
46807bf4
AM
375 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
376 ppc64_elf_unhandled_reloc),
5bd4f169
AM
377
378 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
379 the symbol. */
46807bf4
AM
380 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
381 ppc64_elf_unhandled_reloc),
5bd4f169
AM
382
383 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
384 the symbol. */
46807bf4
AM
385 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
386 ppc64_elf_unhandled_reloc),
5bd4f169
AM
387
388 /* This is used only by the dynamic linker. The symbol should exist
389 both in the object being run and in some shared library. The
390 dynamic linker copies the data addressed by the symbol from the
391 shared library into the object, because the object being
392 run has to have the data at some particular address. */
46807bf4
AM
393 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
394 ppc64_elf_unhandled_reloc),
5bd4f169
AM
395
396 /* Like R_PPC64_ADDR64, but used when setting global offset table
397 entries. */
46807bf4
AM
398 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
399 ppc64_elf_unhandled_reloc),
5bd4f169
AM
400
401 /* Created by the link editor. Marks a procedure linkage table
402 entry for a symbol. */
46807bf4
AM
403 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
404 ppc64_elf_unhandled_reloc),
5bd4f169
AM
405
406 /* Used only by the dynamic linker. When the object is run, this
407 doubleword64 is set to the load address of the object, plus the
408 addend. */
46807bf4
AM
409 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
410 bfd_elf_generic_reloc),
5bd4f169
AM
411
412 /* Like R_PPC64_ADDR32, but may be unaligned. */
46807bf4
AM
413 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
414 bfd_elf_generic_reloc),
5bd4f169
AM
415
416 /* Like R_PPC64_ADDR16, but may be unaligned. */
46807bf4
AM
417 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
418 bfd_elf_generic_reloc),
5bd4f169
AM
419
420 /* 32-bit PC relative. */
46807bf4
AM
421 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
422 bfd_elf_generic_reloc),
5bd4f169 423
10ed1bba 424 /* 32-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
425 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
426 ppc64_elf_unhandled_reloc),
5bd4f169
AM
427
428 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
429 FIXME: R_PPC64_PLTREL32 not supported. */
46807bf4
AM
430 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
431 ppc64_elf_unhandled_reloc),
5bd4f169
AM
432
433 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
434 the symbol. */
46807bf4
AM
435 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
436 ppc64_elf_unhandled_reloc),
5bd4f169
AM
437
438 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
439 the symbol. */
46807bf4
AM
440 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
441 ppc64_elf_unhandled_reloc),
5bd4f169
AM
442
443 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
444 the symbol. */
46807bf4
AM
445 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
446 ppc64_elf_unhandled_reloc),
5bd4f169 447
c061c2d8 448 /* 16-bit section relative relocation. */
46807bf4
AM
449 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
450 ppc64_elf_sectoff_reloc),
5bd4f169 451
c061c2d8 452 /* Like R_PPC64_SECTOFF, but no overflow warning. */
46807bf4
AM
453 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
454 ppc64_elf_sectoff_reloc),
5bd4f169
AM
455
456 /* 16-bit upper half section relative relocation. */
46807bf4
AM
457 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
458 ppc64_elf_sectoff_reloc),
5bd4f169
AM
459
460 /* 16-bit upper half adjusted section relative relocation. */
46807bf4
AM
461 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
462 ppc64_elf_sectoff_ha_reloc),
5bd4f169 463
04c9666a 464 /* Like R_PPC64_REL24 without touching the two least significant bits. */
46807bf4
AM
465 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
466 bfd_elf_generic_reloc),
5bd4f169
AM
467
468 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
469
470 /* A standard 64-bit relocation. */
46807bf4
AM
471 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
472 bfd_elf_generic_reloc),
5bd4f169
AM
473
474 /* The bits 32-47 of an address. */
46807bf4
AM
475 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
476 bfd_elf_generic_reloc),
5bd4f169
AM
477
478 /* The bits 32-47 of an address, plus 1 if the contents of the low
479 16 bits, treated as a signed number, is negative. */
46807bf4
AM
480 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
481 ppc64_elf_ha_reloc),
5bd4f169
AM
482
483 /* The bits 48-63 of an address. */
46807bf4
AM
484 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
485 bfd_elf_generic_reloc),
5bd4f169
AM
486
487 /* The bits 48-63 of an address, plus 1 if the contents of the low
488 16 bits, treated as a signed number, is negative. */
46807bf4
AM
489 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
490 ppc64_elf_ha_reloc),
5bd4f169
AM
491
492 /* Like ADDR64, but may be unaligned. */
46807bf4
AM
493 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
494 bfd_elf_generic_reloc),
5bd4f169
AM
495
496 /* 64-bit relative relocation. */
46807bf4
AM
497 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
498 bfd_elf_generic_reloc),
5bd4f169 499
cedb70c5 500 /* 64-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
501 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
502 ppc64_elf_unhandled_reloc),
5bd4f169
AM
503
504 /* 64-bit PC relative relocation to the symbol's procedure linkage
505 table. */
506 /* FIXME: R_PPC64_PLTREL64 not supported. */
46807bf4
AM
507 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
508 ppc64_elf_unhandled_reloc),
5bd4f169
AM
509
510 /* 16 bit TOC-relative relocation. */
5bd4f169 511 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
46807bf4
AM
512 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
513 ppc64_elf_toc_reloc),
5bd4f169
AM
514
515 /* 16 bit TOC-relative relocation without overflow. */
5bd4f169 516 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
46807bf4
AM
517 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
518 ppc64_elf_toc_reloc),
5bd4f169
AM
519
520 /* 16 bit TOC-relative relocation, high 16 bits. */
5bd4f169 521 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
46807bf4
AM
522 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
523 ppc64_elf_toc_reloc),
5bd4f169
AM
524
525 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
526 contents of the low 16 bits, treated as a signed number, is
527 negative. */
5bd4f169 528 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
46807bf4
AM
529 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
530 ppc64_elf_toc_ha_reloc),
5bd4f169
AM
531
532 /* 64-bit relocation; insert value of TOC base (.TOC.). */
5bd4f169 533 /* R_PPC64_TOC 51 doubleword64 .TOC. */
46807bf4
AM
534 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
535 ppc64_elf_toc64_reloc),
5bd4f169
AM
536
537 /* Like R_PPC64_GOT16, but also informs the link editor that the
538 value to relocate may (!) refer to a PLT entry which the link
539 editor (a) may replace with the symbol value. If the link editor
540 is unable to fully resolve the symbol, it may (b) create a PLT
541 entry and store the address to the new PLT entry in the GOT.
542 This permits lazy resolution of function symbols at run time.
543 The link editor may also skip all of this and just (c) emit a
544 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
545 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
46807bf4
AM
546 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
547 ppc64_elf_unhandled_reloc),
411e1bfb
AM
548
549 /* Like R_PPC64_PLTGOT16, but without overflow. */
550 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
551 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
552 ppc64_elf_unhandled_reloc),
411e1bfb
AM
553
554 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
555 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
46807bf4
AM
556 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
557 ppc64_elf_unhandled_reloc),
411e1bfb
AM
558
559 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
560 1 if the contents of the low 16 bits, treated as a signed number,
561 is negative. */
562 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
46807bf4
AM
563 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
564 ppc64_elf_unhandled_reloc),
411e1bfb
AM
565
566 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
46807bf4
AM
567 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
568 bfd_elf_generic_reloc),
411e1bfb
AM
569
570 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
46807bf4
AM
571 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
572 bfd_elf_generic_reloc),
411e1bfb
AM
573
574 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
46807bf4
AM
575 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
576 ppc64_elf_unhandled_reloc),
411e1bfb
AM
577
578 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
46807bf4
AM
579 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
580 ppc64_elf_unhandled_reloc),
411e1bfb
AM
581
582 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
46807bf4
AM
583 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
584 ppc64_elf_unhandled_reloc),
411e1bfb
AM
585
586 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
46807bf4
AM
587 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
588 ppc64_elf_sectoff_reloc),
411e1bfb
AM
589
590 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
46807bf4
AM
591 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
592 ppc64_elf_sectoff_reloc),
411e1bfb
AM
593
594 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
46807bf4
AM
595 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
596 ppc64_elf_toc_reloc),
411e1bfb
AM
597
598 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
46807bf4
AM
599 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
600 ppc64_elf_toc_reloc),
411e1bfb
AM
601
602 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
603 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
46807bf4
AM
604 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
605 ppc64_elf_unhandled_reloc),
411e1bfb
AM
606
607 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
608 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
609 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
610 ppc64_elf_unhandled_reloc),
411e1bfb 611
727fc41e 612 /* Marker relocs for TLS. */
46807bf4
AM
613 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
614 bfd_elf_generic_reloc),
615
616 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
617 bfd_elf_generic_reloc),
618
619 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
620 bfd_elf_generic_reloc),
727fc41e 621
23cedd1d
AM
622 /* Marker reloc for optimizing r2 save in prologue rather than on
623 each plt call stub. */
46807bf4
AM
624 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
625 bfd_elf_generic_reloc),
3b421ab3 626
23cedd1d 627 /* Marker relocs on inline plt call instructions. */
46807bf4
AM
628 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
629 bfd_elf_generic_reloc),
630
631 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
632 bfd_elf_generic_reloc),
23cedd1d 633
411e1bfb
AM
634 /* Computes the load module index of the load module that contains the
635 definition of its TLS sym. */
46807bf4
AM
636 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
637 ppc64_elf_unhandled_reloc),
411e1bfb
AM
638
639 /* Computes a dtv-relative displacement, the difference between the value
640 of sym+add and the base address of the thread-local storage block that
641 contains the definition of sym, minus 0x8000. */
46807bf4
AM
642 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
643 ppc64_elf_unhandled_reloc),
411e1bfb
AM
644
645 /* A 16 bit dtprel reloc. */
46807bf4
AM
646 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
647 ppc64_elf_unhandled_reloc),
411e1bfb
AM
648
649 /* Like DTPREL16, but no overflow. */
46807bf4
AM
650 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
651 ppc64_elf_unhandled_reloc),
411e1bfb
AM
652
653 /* Like DTPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
654 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
655 ppc64_elf_unhandled_reloc),
411e1bfb
AM
656
657 /* Like DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
658 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
659 ppc64_elf_unhandled_reloc),
411e1bfb
AM
660
661 /* Like DTPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
662 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
663 ppc64_elf_unhandled_reloc),
411e1bfb
AM
664
665 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
666 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
667 ppc64_elf_unhandled_reloc),
411e1bfb
AM
668
669 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
670 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
671 ppc64_elf_unhandled_reloc),
411e1bfb
AM
672
673 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
674 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
675 ppc64_elf_unhandled_reloc),
411e1bfb
AM
676
677 /* Like DTPREL16, but for insns with a DS field. */
46807bf4
AM
678 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
679 ppc64_elf_unhandled_reloc),
411e1bfb
AM
680
681 /* Like DTPREL16_DS, but no overflow. */
46807bf4
AM
682 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
683 ppc64_elf_unhandled_reloc),
411e1bfb
AM
684
685 /* Computes a tp-relative displacement, the difference between the value of
686 sym+add and the value of the thread pointer (r13). */
46807bf4
AM
687 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
688 ppc64_elf_unhandled_reloc),
411e1bfb
AM
689
690 /* A 16 bit tprel reloc. */
46807bf4
AM
691 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
692 ppc64_elf_unhandled_reloc),
411e1bfb
AM
693
694 /* Like TPREL16, but no overflow. */
46807bf4
AM
695 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
696 ppc64_elf_unhandled_reloc),
411e1bfb
AM
697
698 /* Like TPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
699 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
700 ppc64_elf_unhandled_reloc),
411e1bfb
AM
701
702 /* Like TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
703 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
704 ppc64_elf_unhandled_reloc),
411e1bfb
AM
705
706 /* Like TPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
707 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
708 ppc64_elf_unhandled_reloc),
411e1bfb
AM
709
710 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
711 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
712 ppc64_elf_unhandled_reloc),
411e1bfb
AM
713
714 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
715 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
716 ppc64_elf_unhandled_reloc),
411e1bfb
AM
717
718 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
719 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
720 ppc64_elf_unhandled_reloc),
411e1bfb
AM
721
722 /* Like TPREL16, but for insns with a DS field. */
46807bf4
AM
723 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
724 ppc64_elf_unhandled_reloc),
411e1bfb
AM
725
726 /* Like TPREL16_DS, but no overflow. */
46807bf4
AM
727 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
728 ppc64_elf_unhandled_reloc),
411e1bfb
AM
729
730 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
731 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
732 to the first entry relative to the TOC base (r2). */
46807bf4
AM
733 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
734 ppc64_elf_unhandled_reloc),
5bd4f169 735
411e1bfb 736 /* Like GOT_TLSGD16, but no overflow. */
46807bf4
AM
737 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
738 ppc64_elf_unhandled_reloc),
5bd4f169 739
411e1bfb 740 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
46807bf4
AM
741 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
742 ppc64_elf_unhandled_reloc),
5bd4f169 743
411e1bfb 744 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
46807bf4
AM
745 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
746 ppc64_elf_unhandled_reloc),
5bd4f169 747
411e1bfb
AM
748 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
749 with values (sym+add)@dtpmod and zero, and computes the offset to the
750 first entry relative to the TOC base (r2). */
46807bf4
AM
751 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
752 ppc64_elf_unhandled_reloc),
5bd4f169 753
411e1bfb 754 /* Like GOT_TLSLD16, but no overflow. */
46807bf4
AM
755 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
756 ppc64_elf_unhandled_reloc),
5bd4f169 757
411e1bfb 758 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
46807bf4
AM
759 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
760 ppc64_elf_unhandled_reloc),
5bd4f169 761
411e1bfb 762 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
46807bf4
AM
763 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
764 ppc64_elf_unhandled_reloc),
5bd4f169 765
411e1bfb
AM
766 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
767 the offset to the entry relative to the TOC base (r2). */
46807bf4
AM
768 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
769 ppc64_elf_unhandled_reloc),
5bd4f169 770
411e1bfb 771 /* Like GOT_DTPREL16_DS, but no overflow. */
46807bf4
AM
772 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
773 ppc64_elf_unhandled_reloc),
5bd4f169 774
411e1bfb 775 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
776 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
777 ppc64_elf_unhandled_reloc),
5bd4f169 778
411e1bfb 779 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
780 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
781 ppc64_elf_unhandled_reloc),
411e1bfb
AM
782
783 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
784 offset to the entry relative to the TOC base (r2). */
46807bf4
AM
785 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
786 ppc64_elf_unhandled_reloc),
5bd4f169 787
411e1bfb 788 /* Like GOT_TPREL16_DS, but no overflow. */
46807bf4
AM
789 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
790 ppc64_elf_unhandled_reloc),
5bd4f169 791
411e1bfb 792 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
793 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
794 ppc64_elf_unhandled_reloc),
5bd4f169 795
411e1bfb 796 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
797 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
798 ppc64_elf_unhandled_reloc),
799
800 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
801 ppc64_elf_unhandled_reloc),
802
803 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
804 bfd_elf_generic_reloc),
e054468f 805
25f23106 806 /* A 16 bit relative relocation. */
46807bf4
AM
807 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
808 bfd_elf_generic_reloc),
25f23106
AM
809
810 /* A 16 bit relative relocation without overflow. */
46807bf4
AM
811 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
812 bfd_elf_generic_reloc),
25f23106
AM
813
814 /* The high order 16 bits of a relative address. */
46807bf4
AM
815 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
816 bfd_elf_generic_reloc),
25f23106
AM
817
818 /* The high order 16 bits of a relative address, plus 1 if the contents of
819 the low 16 bits, treated as a signed number, is negative. */
46807bf4
AM
820 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
821 ppc64_elf_ha_reloc),
25f23106 822
4a969973
AM
823 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
824 bfd_elf_generic_reloc),
825
826 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
827 ppc64_elf_ha_reloc),
828
829 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
830 bfd_elf_generic_reloc),
831
832 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
833 ppc64_elf_ha_reloc),
834
835 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
836 bfd_elf_generic_reloc),
837
838 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
839 ppc64_elf_ha_reloc),
840
a680de9a 841 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
46807bf4
AM
842 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
843 ppc64_elf_ha_reloc),
a680de9a 844
7ba71655 845 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
46807bf4
AM
846 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
847 ppc64_elf_ha_reloc),
7ba71655 848
f9c6b907 849 /* Like R_PPC64_ADDR16_HI, but no overflow. */
46807bf4
AM
850 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
851 bfd_elf_generic_reloc),
f9c6b907
AM
852
853 /* Like R_PPC64_ADDR16_HA, but no overflow. */
46807bf4
AM
854 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
855 ppc64_elf_ha_reloc),
f9c6b907
AM
856
857 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
46807bf4
AM
858 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
859 ppc64_elf_unhandled_reloc),
f9c6b907
AM
860
861 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
46807bf4
AM
862 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
863 ppc64_elf_unhandled_reloc),
f9c6b907
AM
864
865 /* Like R_PPC64_TPREL16_HI, but no overflow. */
46807bf4
AM
866 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
867 ppc64_elf_unhandled_reloc),
f9c6b907
AM
868
869 /* Like R_PPC64_TPREL16_HA, but no overflow. */
46807bf4
AM
870 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
871 ppc64_elf_unhandled_reloc),
f9c6b907 872
006589cf 873 /* Marker reloc on ELFv2 large-model function entry. */
46807bf4
AM
874 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
875 bfd_elf_generic_reloc),
006589cf 876
45965137 877 /* Like ADDR64, but use local entry point of function. */
46807bf4
AM
878 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
879 bfd_elf_generic_reloc),
45965137 880
5bd4f169 881 /* GNU extension to record C++ vtable hierarchy. */
46807bf4
AM
882 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
883 NULL),
5bd4f169
AM
884
885 /* GNU extension to record C++ vtable member usage. */
46807bf4
AM
886 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
887 NULL),
5bd4f169
AM
888};
889
890\f
891/* Initialize the ppc64_elf_howto_table, so that linear accesses can
892 be done. */
893
894static void
4ce794b7 895ppc_howto_init (void)
5bd4f169
AM
896{
897 unsigned int i, type;
898
a4b6fadd 899 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
900 {
901 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 902 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
903 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
904 }
905}
906
907static reloc_howto_type *
f3185997 908ppc64_elf_reloc_type_lookup (bfd *abfd,
4ce794b7 909 bfd_reloc_code_real_type code)
5bd4f169 910{
411e1bfb 911 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
912
913 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
914 /* Initialize howto table if needed. */
915 ppc_howto_init ();
916
4ce794b7 917 switch (code)
5bd4f169
AM
918 {
919 default:
f3185997 920 /* xgettext:c-format */
2cdcc330
AM
921 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
922 (int) code);
f3185997 923 bfd_set_error (bfd_error_bad_value);
4ce794b7 924 return NULL;
5bd4f169 925
411e1bfb
AM
926 case BFD_RELOC_NONE: r = R_PPC64_NONE;
927 break;
928 case BFD_RELOC_32: r = R_PPC64_ADDR32;
929 break;
930 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
931 break;
932 case BFD_RELOC_16: r = R_PPC64_ADDR16;
933 break;
934 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
935 break;
936 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
937 break;
f9c6b907
AM
938 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
939 break;
411e1bfb 940 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 941 break;
f9c6b907
AM
942 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
943 break;
411e1bfb 944 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 945 break;
411e1bfb 946 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 947 break;
411e1bfb 948 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 949 break;
411e1bfb 950 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 951 break;
05d0e962
AM
952 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
953 break;
411e1bfb 954 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 955 break;
411e1bfb 956 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 957 break;
411e1bfb 958 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 959 break;
411e1bfb 960 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 961 break;
411e1bfb 962 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 963 break;
411e1bfb 964 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 965 break;
411e1bfb 966 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 967 break;
411e1bfb 968 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 969 break;
411e1bfb 970 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 971 break;
411e1bfb 972 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 973 break;
411e1bfb 974 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 975 break;
411e1bfb 976 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 977 break;
411e1bfb 978 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 979 break;
411e1bfb 980 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 981 break;
411e1bfb 982 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 983 break;
411e1bfb 984 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 985 break;
411e1bfb 986 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 987 break;
411e1bfb 988 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 989 break;
411e1bfb 990 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 991 break;
411e1bfb 992 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 993 break;
411e1bfb 994 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 995 break;
411e1bfb 996 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 997 break;
411e1bfb 998 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 999 break;
411e1bfb 1000 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1001 break;
411e1bfb 1002 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1003 break;
411e1bfb 1004 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1005 break;
411e1bfb 1006 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1007 break;
411e1bfb 1008 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1009 break;
411e1bfb 1010 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1011 break;
411e1bfb 1012 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1013 break;
411e1bfb 1014 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1015 break;
411e1bfb 1016 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1017 break;
411e1bfb 1018 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1019 break;
411e1bfb 1020 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1021 break;
411e1bfb 1022 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1023 break;
411e1bfb 1024 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1025 break;
411e1bfb 1026 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1027 break;
411e1bfb 1028 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1029 break;
411e1bfb 1030 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1031 break;
411e1bfb 1032 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1033 break;
411e1bfb 1034 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1035 break;
411e1bfb 1036 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1037 break;
411e1bfb 1038 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1039 break;
411e1bfb 1040 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1041 break;
411e1bfb 1042 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1043 break;
411e1bfb 1044 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1045 break;
411e1bfb 1046 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1047 break;
411e1bfb 1048 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1049 break;
411e1bfb 1050 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1051 break;
727fc41e
AM
1052 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1053 break;
1054 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1055 break;
411e1bfb 1056 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1057 break;
411e1bfb 1058 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1059 break;
411e1bfb 1060 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1061 break;
411e1bfb 1062 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1063 break;
f9c6b907
AM
1064 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1065 break;
411e1bfb 1066 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1067 break;
f9c6b907
AM
1068 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1069 break;
411e1bfb 1070 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1071 break;
411e1bfb
AM
1072 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1073 break;
1074 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1075 break;
1076 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1077 break;
f9c6b907
AM
1078 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1079 break;
411e1bfb
AM
1080 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1081 break;
f9c6b907
AM
1082 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1083 break;
411e1bfb
AM
1084 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1085 break;
1086 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1087 break;
1088 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1089 break;
1090 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1091 break;
1092 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1093 break;
1094 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1095 break;
1096 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1097 break;
1098 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1099 break;
1100 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1101 break;
1102 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1103 break;
1104 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1105 break;
1106 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1107 break;
1108 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1109 break;
1110 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1111 break;
1112 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1113 break;
1114 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1115 break;
1116 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1117 break;
1118 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1119 break;
1120 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1121 break;
1122 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1123 break;
1124 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1125 break;
1126 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1127 break;
1128 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1129 break;
1130 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1131 break;
1132 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1133 break;
1134 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1135 break;
1136 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1137 break;
1138 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1139 break;
1140 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1141 break;
25f23106
AM
1142 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1143 break;
1144 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1145 break;
1146 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1147 break;
1148 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1149 break;
4a969973
AM
1150 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1151 break;
1152 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1153 break;
1154 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1155 break;
1156 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1157 break;
1158 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1159 break;
1160 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1161 break;
7ba71655
AM
1162 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1163 break;
a680de9a
PB
1164 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1165 break;
006589cf
AM
1166 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1167 break;
45965137
AM
1168 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1169 break;
411e1bfb
AM
1170 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1171 break;
1172 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
1173 break;
1174 }
1175
4ce794b7 1176 return ppc64_elf_howto_table[r];
5bd4f169
AM
1177};
1178
157090f7
AM
1179static reloc_howto_type *
1180ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1181 const char *r_name)
1182{
1183 unsigned int i;
1184
a4b6fadd 1185 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
1186 if (ppc64_elf_howto_raw[i].name != NULL
1187 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1188 return &ppc64_elf_howto_raw[i];
1189
1190 return NULL;
1191}
1192
5bd4f169
AM
1193/* Set the howto pointer for a PowerPC ELF reloc. */
1194
f3185997 1195static bfd_boolean
4aef7643 1196ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 1197 Elf_Internal_Rela *dst)
5bd4f169 1198{
65f38f15
AM
1199 unsigned int type;
1200
ef60b7ff 1201 /* Initialize howto table if needed. */
5bd4f169 1202 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
1203 ppc_howto_init ();
1204
65f38f15 1205 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 1206 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 1207 {
695344c0 1208 /* xgettext:c-format */
0aa13fee 1209 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 1210 abfd, type);
f3185997
NC
1211 bfd_set_error (bfd_error_bad_value);
1212 return FALSE;
d0fb9a8d 1213 }
65f38f15 1214 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
1215 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1216 {
1217 /* xgettext:c-format */
1218 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1219 abfd, type);
1220 bfd_set_error (bfd_error_bad_value);
1221 return FALSE;
1222 }
2cdcc330 1223
f3185997 1224 return TRUE;
5bd4f169
AM
1225}
1226
04c9666a 1227/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
1228
1229static bfd_reloc_status_type
4ce794b7
AM
1230ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1231 void *data, asection *input_section,
1232 bfd *output_bfd, char **error_message)
5bd4f169 1233{
a680de9a
PB
1234 enum elf_ppc64_reloc_type r_type;
1235 long insn;
1236 bfd_size_type octets;
3de43e7b 1237 bfd_vma value;
a680de9a 1238
805fc799
AM
1239 /* If this is a relocatable link (output_bfd test tells us), just
1240 call the generic function. Any adjustment will be done at final
1241 link time. */
1242 if (output_bfd != NULL)
cedb70c5 1243 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1244 input_section, output_bfd, error_message);
1245
1246 /* Adjust the addend for sign extension of the low 16 bits.
1247 We won't actually be using the low 16 bits, so trashing them
1248 doesn't matter. */
1249 reloc_entry->addend += 0x8000;
a680de9a
PB
1250 r_type = reloc_entry->howto->type;
1251 if (r_type != R_PPC64_REL16DX_HA)
1252 return bfd_reloc_continue;
1253
1254 value = 0;
1255 if (!bfd_is_com_section (symbol->section))
1256 value = symbol->value;
1257 value += (reloc_entry->addend
1258 + symbol->section->output_offset
1259 + symbol->section->output_section->vma);
1260 value -= (reloc_entry->address
1261 + input_section->output_offset
1262 + input_section->output_section->vma);
3de43e7b 1263 value = (bfd_signed_vma) value >> 16;
a680de9a
PB
1264
1265 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1266 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1267 insn &= ~0x1fffc1;
3de43e7b 1268 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 1269 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 1270 if (value + 0x8000 > 0xffff)
a680de9a
PB
1271 return bfd_reloc_overflow;
1272 return bfd_reloc_ok;
805fc799 1273}
5bd4f169 1274
2441e016
AM
1275static bfd_reloc_status_type
1276ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1277 void *data, asection *input_section,
1278 bfd *output_bfd, char **error_message)
1279{
1280 if (output_bfd != NULL)
1281 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1282 input_section, output_bfd, error_message);
1283
699733f6
AM
1284 if (strcmp (symbol->section->name, ".opd") == 0
1285 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
1286 {
1287 bfd_vma dest = opd_entry_value (symbol->section,
1288 symbol->value + reloc_entry->addend,
aef36ac1 1289 NULL, NULL, FALSE);
2441e016
AM
1290 if (dest != (bfd_vma) -1)
1291 reloc_entry->addend = dest - (symbol->value
1292 + symbol->section->output_section->vma
1293 + symbol->section->output_offset);
1294 }
810d4e75
AM
1295 else
1296 {
1297 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1298
1299 if (symbol->section->owner != abfd
9f284bf9 1300 && symbol->section->owner != NULL
810d4e75
AM
1301 && abiversion (symbol->section->owner) >= 2)
1302 {
1303 unsigned int i;
1304
1305 for (i = 0; i < symbol->section->owner->symcount; ++i)
1306 {
1307 asymbol *symdef = symbol->section->owner->outsymbols[i];
1308
1309 if (strcmp (symdef->name, symbol->name) == 0)
1310 {
1311 elfsym = (elf_symbol_type *) symdef;
1312 break;
1313 }
1314 }
1315 }
1316 reloc_entry->addend
1317 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1318 }
2441e016
AM
1319 return bfd_reloc_continue;
1320}
1321
805fc799 1322static bfd_reloc_status_type
4ce794b7
AM
1323ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1324 void *data, asection *input_section,
1325 bfd *output_bfd, char **error_message)
805fc799
AM
1326{
1327 long insn;
04c9666a 1328 enum elf_ppc64_reloc_type r_type;
805fc799 1329 bfd_size_type octets;
794e51c0
AM
1330 /* Assume 'at' branch hints. */
1331 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
1332
1333 /* If this is a relocatable link (output_bfd test tells us), just
1334 call the generic function. Any adjustment will be done at final
1335 link time. */
5bd4f169 1336 if (output_bfd != NULL)
cedb70c5 1337 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1338 input_section, output_bfd, error_message);
1339
1340 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1341 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1342 insn &= ~(0x01 << 21);
4ce794b7 1343 r_type = reloc_entry->howto->type;
805fc799
AM
1344 if (r_type == R_PPC64_ADDR14_BRTAKEN
1345 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 1346 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 1347
794e51c0 1348 if (is_isa_v2)
5bd4f169 1349 {
805fc799
AM
1350 /* Set 'a' bit. This is 0b00010 in BO field for branch
1351 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1352 for branch on CTR insns (BO == 1a00t or 1a01t). */
1353 if ((insn & (0x14 << 21)) == (0x04 << 21))
1354 insn |= 0x02 << 21;
1355 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1356 insn |= 0x08 << 21;
1357 else
2441e016 1358 goto out;
5bd4f169 1359 }
805fc799
AM
1360 else
1361 {
1362 bfd_vma target = 0;
1363 bfd_vma from;
5bd4f169 1364
805fc799
AM
1365 if (!bfd_is_com_section (symbol->section))
1366 target = symbol->value;
1367 target += symbol->section->output_section->vma;
1368 target += symbol->section->output_offset;
1369 target += reloc_entry->addend;
5bd4f169 1370
805fc799
AM
1371 from = (reloc_entry->address
1372 + input_section->output_offset
1373 + input_section->output_section->vma);
5bd4f169 1374
805fc799
AM
1375 /* Invert 'y' bit if not the default. */
1376 if ((bfd_signed_vma) (target - from) < 0)
1377 insn ^= 0x01 << 21;
1378 }
4ce794b7 1379 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
1380 out:
1381 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1382 input_section, output_bfd, error_message);
805fc799 1383}
5bd4f169 1384
805fc799 1385static bfd_reloc_status_type
4ce794b7
AM
1386ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1387 void *data, asection *input_section,
1388 bfd *output_bfd, char **error_message)
805fc799
AM
1389{
1390 /* If this is a relocatable link (output_bfd test tells us), just
1391 call the generic function. Any adjustment will be done at final
1392 link time. */
1393 if (output_bfd != NULL)
cedb70c5 1394 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 1395 input_section, output_bfd, error_message);
5bd4f169 1396
805fc799
AM
1397 /* Subtract the symbol section base address. */
1398 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
1399 return bfd_reloc_continue;
1400}
1401
805fc799 1402static bfd_reloc_status_type
4ce794b7
AM
1403ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1404 void *data, asection *input_section,
1405 bfd *output_bfd, char **error_message)
805fc799
AM
1406{
1407 /* If this is a relocatable link (output_bfd test tells us), just
1408 call the generic function. Any adjustment will be done at final
1409 link time. */
1410 if (output_bfd != NULL)
cedb70c5 1411 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1412 input_section, output_bfd, error_message);
1413
1414 /* Subtract the symbol section base address. */
1415 reloc_entry->addend -= symbol->section->output_section->vma;
1416
1417 /* Adjust the addend for sign extension of the low 16 bits. */
1418 reloc_entry->addend += 0x8000;
1419 return bfd_reloc_continue;
1420}
1421
1422static bfd_reloc_status_type
4ce794b7
AM
1423ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1424 void *data, asection *input_section,
1425 bfd *output_bfd, char **error_message)
805fc799
AM
1426{
1427 bfd_vma TOCstart;
1428
1429 /* If this is a relocatable link (output_bfd test tells us), just
1430 call the generic function. Any adjustment will be done at final
1431 link time. */
1432 if (output_bfd != NULL)
cedb70c5 1433 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1434 input_section, output_bfd, error_message);
1435
1436 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1437 if (TOCstart == 0)
1c865ab2 1438 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1439
1440 /* Subtract the TOC base address. */
1441 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1442 return bfd_reloc_continue;
1443}
1444
1445static bfd_reloc_status_type
4ce794b7
AM
1446ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1447 void *data, asection *input_section,
1448 bfd *output_bfd, char **error_message)
805fc799
AM
1449{
1450 bfd_vma TOCstart;
1451
1452 /* If this is a relocatable link (output_bfd test tells us), just
1453 call the generic function. Any adjustment will be done at final
1454 link time. */
1455 if (output_bfd != NULL)
cedb70c5 1456 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1457 input_section, output_bfd, error_message);
1458
1459 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1460 if (TOCstart == 0)
1c865ab2 1461 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1462
1463 /* Subtract the TOC base address. */
1464 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1465
1466 /* Adjust the addend for sign extension of the low 16 bits. */
1467 reloc_entry->addend += 0x8000;
1468 return bfd_reloc_continue;
1469}
1470
1471static bfd_reloc_status_type
4ce794b7
AM
1472ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1473 void *data, asection *input_section,
1474 bfd *output_bfd, char **error_message)
805fc799
AM
1475{
1476 bfd_vma TOCstart;
1477 bfd_size_type octets;
1478
1479 /* If this is a relocatable link (output_bfd test tells us), just
1480 call the generic function. Any adjustment will be done at final
1481 link time. */
1482 if (output_bfd != NULL)
cedb70c5 1483 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1484 input_section, output_bfd, error_message);
1485
1486 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1487 if (TOCstart == 0)
1c865ab2 1488 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1489
1490 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1491 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1492 return bfd_reloc_ok;
1493}
1494
1495static bfd_reloc_status_type
4ce794b7
AM
1496ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1497 void *data, asection *input_section,
1498 bfd *output_bfd, char **error_message)
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. */
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
1507 if (error_message != NULL)
1508 {
1509 static char buf[60];
1510 sprintf (buf, "generic linker can't handle %s",
1511 reloc_entry->howto->name);
1512 *error_message = buf;
1513 }
1514 return bfd_reloc_dangerous;
1515}
1516
927be08e
AM
1517/* Track GOT entries needed for a given symbol. We might need more
1518 than one got entry per symbol. */
1519struct got_entry
1520{
1521 struct got_entry *next;
1522
1523 /* The symbol addend that we'll be placing in the GOT. */
1524 bfd_vma addend;
1525
1526 /* Unlike other ELF targets, we use separate GOT entries for the same
1527 symbol referenced from different input files. This is to support
1528 automatic multiple TOC/GOT sections, where the TOC base can vary
1529 from one input file to another. After partitioning into TOC groups
1530 we merge entries within the group.
1531
1532 Point to the BFD owning this GOT entry. */
1533 bfd *owner;
1534
1535 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1536 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1537 unsigned char tls_type;
927be08e
AM
1538
1539 /* Non-zero if got.ent points to real entry. */
f961d9dd 1540 unsigned char is_indirect;
927be08e
AM
1541
1542 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1543 union
2cdcc330
AM
1544 {
1545 bfd_signed_vma refcount;
1546 bfd_vma offset;
1547 struct got_entry *ent;
1548 } got;
927be08e
AM
1549};
1550
1551/* The same for PLT. */
1552struct plt_entry
1553{
1554 struct plt_entry *next;
1555
1556 bfd_vma addend;
1557
1558 union
2cdcc330
AM
1559 {
1560 bfd_signed_vma refcount;
1561 bfd_vma offset;
1562 } plt;
927be08e
AM
1563};
1564
e717da7e
AM
1565struct ppc64_elf_obj_tdata
1566{
1567 struct elf_obj_tdata elf;
1568
1569 /* Shortcuts to dynamic linker sections. */
1570 asection *got;
1571 asection *relgot;
1572
b3fac117
AM
1573 /* Used during garbage collection. We attach global symbols defined
1574 on removed .opd entries to this section so that the sym is removed. */
1575 asection *deleted_section;
81688140 1576
927be08e 1577 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1578 sections means we potentially need one of these for each input bfd. */
927be08e 1579 struct got_entry tlsld_got;
8860955f 1580
2cdcc330
AM
1581 union
1582 {
729eabd5
AM
1583 /* A copy of relocs before they are modified for --emit-relocs. */
1584 Elf_Internal_Rela *relocs;
1585
1586 /* Section contents. */
1587 bfd_byte *contents;
1588 } opd;
d77c8a4b
AM
1589
1590 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1591 the reloc to be in the range -32768 to 32767. */
98528052
AM
1592 unsigned int has_small_toc_reloc : 1;
1593
560c8763
AM
1594 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1595 instruction not one we handle. */
1596 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
1597};
1598
1599#define ppc64_elf_tdata(bfd) \
1600 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1601
1602#define ppc64_tlsld_got(bfd) \
1603 (&ppc64_elf_tdata (bfd)->tlsld_got)
1604
0c8d6e5c
AM
1605#define is_ppc64_elf(bfd) \
1606 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1607 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1608
e717da7e
AM
1609/* Override the generic function because we store some extras. */
1610
1611static bfd_boolean
1612ppc64_elf_mkobject (bfd *abfd)
1613{
0ffa91dd 1614 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1615 PPC64_ELF_DATA);
e717da7e
AM
1616}
1617
feee612b 1618/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1619 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1620
b34976b6 1621static bfd_boolean
4ce794b7 1622ppc64_elf_object_p (bfd *abfd)
feee612b 1623{
14b57c7c
AM
1624 if (!abfd->arch_info->the_default)
1625 return TRUE;
1626
1627 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1628 {
1629 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1630
1631 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1632 {
1633 /* Relies on arch after 32 bit default being 64 bit default. */
1634 abfd->arch_info = abfd->arch_info->next;
1635 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1636 }
1637 }
14b57c7c 1638 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1639}
1640
d37c89e5
AM
1641/* Support for core dump NOTE sections. */
1642
1643static bfd_boolean
1644ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1645{
eea6121a 1646 size_t offset, size;
d37c89e5
AM
1647
1648 if (note->descsz != 504)
1649 return FALSE;
1650
1651 /* pr_cursig */
228e534f 1652 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1653
1654 /* pr_pid */
228e534f 1655 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1656
1657 /* pr_reg */
1658 offset = 112;
eea6121a 1659 size = 384;
d37c89e5
AM
1660
1661 /* Make a ".reg/999" section. */
1662 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1663 size, note->descpos + offset);
d37c89e5
AM
1664}
1665
1666static bfd_boolean
1667ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1668{
1669 if (note->descsz != 136)
1670 return FALSE;
1671
228e534f 1672 elf_tdata (abfd)->core->pid
bc989cdc 1673 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1674 elf_tdata (abfd)->core->program
d37c89e5 1675 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1676 elf_tdata (abfd)->core->command
d37c89e5
AM
1677 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1678
1679 return TRUE;
1680}
1681
183e98be
AM
1682static char *
1683ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1684 ...)
1685{
1686 switch (note_type)
1687 {
1688 default:
1689 return NULL;
1690
1691 case NT_PRPSINFO:
1692 {
9ef6d1e3 1693 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1694 va_list ap;
1695
1696 va_start (ap, note_type);
75cd47ed 1697 memset (data, 0, sizeof (data));
183e98be 1698 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1699#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1700 DIAGNOSTIC_PUSH;
be3e27bb 1701 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1702 -Wstringop-truncation:
1703 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1704 */
95da9854
L
1705 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1706#endif
183e98be 1707 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1708#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1709 DIAGNOSTIC_POP;
fe75810f 1710#endif
183e98be
AM
1711 va_end (ap);
1712 return elfcore_write_note (abfd, buf, bufsiz,
1713 "CORE", note_type, data, sizeof (data));
1714 }
1715
1716 case NT_PRSTATUS:
1717 {
1718 char data[504];
1719 va_list ap;
1720 long pid;
1721 int cursig;
1722 const void *greg;
1723
1724 va_start (ap, note_type);
1725 memset (data, 0, 112);
1726 pid = va_arg (ap, long);
1727 bfd_put_32 (abfd, pid, data + 32);
1728 cursig = va_arg (ap, int);
1729 bfd_put_16 (abfd, cursig, data + 12);
1730 greg = va_arg (ap, const void *);
1731 memcpy (data + 112, greg, 384);
1732 memset (data + 496, 0, 8);
1733 va_end (ap);
1734 return elfcore_write_note (abfd, buf, bufsiz,
1735 "CORE", note_type, data, sizeof (data));
1736 }
1737 }
1738}
1739
5d35169e
AM
1740/* Add extra PPC sections. */
1741
2cdcc330 1742static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1743{
07d6d2b8
AM
1744 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1745 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1746 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1747 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1748 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1749 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1750 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1751};
1752
7c8fe5c4
AM
1753enum _ppc64_sec_type {
1754 sec_normal = 0,
1755 sec_opd = 1,
1756 sec_toc = 2
1757};
1758
f0abc2a1
AM
1759struct _ppc64_elf_section_data
1760{
1761 struct bfd_elf_section_data elf;
411e1bfb 1762
f0abc2a1
AM
1763 union
1764 {
51aecdc5
AM
1765 /* An array with one entry for each opd function descriptor,
1766 and some spares since opd entries may be either 16 or 24 bytes. */
1767#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
1768 struct _opd_sec_data
1769 {
1770 /* Points to the function code section for local opd entries. */
1771 asection **func_sec;
1772
1773 /* After editing .opd, adjust references to opd local syms. */
1774 long *adjust;
1775 } opd;
7c8fe5c4 1776
3a71aa26
AM
1777 /* An array for toc sections, indexed by offset/8. */
1778 struct _toc_sec_data
1779 {
1780 /* Specifies the relocation symbol index used at a given toc offset. */
1781 unsigned *symndx;
1782
1783 /* And the relocation addend. */
1784 bfd_vma *add;
1785 } toc;
7c8fe5c4
AM
1786 } u;
1787
1788 enum _ppc64_sec_type sec_type:2;
411e1bfb 1789
7c8fe5c4
AM
1790 /* Flag set when small branches are detected. Used to
1791 select suitable defaults for the stub group size. */
1792 unsigned int has_14bit_branch:1;
3e04d765
AM
1793
1794 /* Flag set when PLTCALL relocs are detected. */
1795 unsigned int has_pltcall:1;
f0abc2a1
AM
1796};
1797
1798#define ppc64_elf_section_data(sec) \
411e1bfb 1799 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
1800
1801static bfd_boolean
4ce794b7 1802ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1803{
f592407e
AM
1804 if (!sec->used_by_bfd)
1805 {
1806 struct _ppc64_elf_section_data *sdata;
1807 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1808
f592407e
AM
1809 sdata = bfd_zalloc (abfd, amt);
1810 if (sdata == NULL)
1811 return FALSE;
1812 sec->used_by_bfd = sdata;
1813 }
f0abc2a1
AM
1814
1815 return _bfd_elf_new_section_hook (abfd, sec);
1816}
4025353c 1817
74f0fb50 1818static struct _opd_sec_data *
4025353c
AM
1819get_opd_info (asection * sec)
1820{
1821 if (sec != NULL
1822 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 1823 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 1824 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
1825 return NULL;
1826}
90e3cdf2
JJ
1827\f
1828/* Parameters for the qsort hook. */
90e3cdf2 1829static bfd_boolean synthetic_relocatable;
cd285db5 1830static asection *synthetic_opd;
90e3cdf2 1831
699733f6 1832/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
1833
1834static int
1835compare_symbols (const void *ap, const void *bp)
1836{
2cdcc330
AM
1837 const asymbol *a = *(const asymbol **) ap;
1838 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 1839
699733f6
AM
1840 /* Section symbols first. */
1841 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 1842 return -1;
699733f6 1843 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
1844 return 1;
1845
699733f6 1846 /* then .opd symbols. */
cd285db5
AM
1847 if (synthetic_opd != NULL)
1848 {
1849 if (strcmp (a->section->name, ".opd") == 0
1850 && strcmp (b->section->name, ".opd") != 0)
1851 return -1;
1852 if (strcmp (a->section->name, ".opd") != 0
1853 && strcmp (b->section->name, ".opd") == 0)
1854 return 1;
1855 }
90e3cdf2 1856
699733f6 1857 /* then other code symbols. */
2cdcc330
AM
1858 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1859 == (SEC_CODE | SEC_ALLOC))
1860 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1861 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
1862 return -1;
1863
2cdcc330
AM
1864 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1865 != (SEC_CODE | SEC_ALLOC))
1866 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1867 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
1868 return 1;
1869
1870 if (synthetic_relocatable)
1871 {
1872 if (a->section->id < b->section->id)
1873 return -1;
1874
1875 if (a->section->id > b->section->id)
1876 return 1;
1877 }
1878
1879 if (a->value + a->section->vma < b->value + b->section->vma)
1880 return -1;
1881
1882 if (a->value + a->section->vma > b->value + b->section->vma)
1883 return 1;
1884
4d35a0aa
AM
1885 /* For syms with the same value, prefer strong dynamic global function
1886 syms over other syms. */
1887 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
1888 return -1;
1889
1890 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
1891 return 1;
1892
1893 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
1894 return -1;
1895
1896 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
1897 return 1;
1898
1899 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
1900 return -1;
1901
1902 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
1903 return 1;
1904
1905 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
1906 return -1;
1907
1908 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
1909 return 1;
1910
aaed6f5b 1911 return a > b;
90e3cdf2
JJ
1912}
1913
699733f6 1914/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 1915
699733f6 1916static asymbol *
7292b3ac 1917sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 1918{
699733f6 1919 long mid;
90e3cdf2 1920
7292b3ac 1921 if (id == (unsigned) -1)
699733f6
AM
1922 {
1923 while (lo < hi)
1924 {
1925 mid = (lo + hi) >> 1;
1926 if (syms[mid]->value + syms[mid]->section->vma < value)
1927 lo = mid + 1;
1928 else if (syms[mid]->value + syms[mid]->section->vma > value)
1929 hi = mid;
1930 else
1931 return syms[mid];
1932 }
1933 }
1934 else
1935 {
1936 while (lo < hi)
1937 {
1938 mid = (lo + hi) >> 1;
1939 if (syms[mid]->section->id < id)
1940 lo = mid + 1;
1941 else if (syms[mid]->section->id > id)
1942 hi = mid;
1943 else if (syms[mid]->value < value)
1944 lo = mid + 1;
1945 else if (syms[mid]->value > value)
1946 hi = mid;
1947 else
1948 return syms[mid];
1949 }
1950 }
1951 return NULL;
90e3cdf2
JJ
1952}
1953
468392fb
AM
1954static bfd_boolean
1955section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
1956{
1957 bfd_vma vma = *(bfd_vma *) ptr;
1958 return ((section->flags & SEC_ALLOC) != 0
1959 && section->vma <= vma
1960 && vma < section->vma + section->size);
1961}
1962
699733f6 1963/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
1964 entry syms. Also generate @plt symbols for the glink branch table.
1965 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
1966
1967static long
a7535cf3
AM
1968ppc64_elf_get_synthetic_symtab (bfd *abfd,
1969 long static_count, asymbol **static_syms,
1970 long dyn_count, asymbol **dyn_syms,
c9727e01 1971 asymbol **ret)
90e3cdf2
JJ
1972{
1973 asymbol *s;
0ccf57bd 1974 size_t i, j, count;
90e3cdf2 1975 char *names;
0ccf57bd 1976 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 1977 asection *opd = NULL;
90e3cdf2 1978 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 1979 asymbol **syms;
ee67d69a 1980 int abi = abiversion (abfd);
90e3cdf2
JJ
1981
1982 *ret = NULL;
1983
ee67d69a
AM
1984 if (abi < 2)
1985 {
1986 opd = bfd_get_section_by_name (abfd, ".opd");
1987 if (opd == NULL && abi == 1)
1988 return 0;
1989 }
90e3cdf2 1990
a5259595
AM
1991 syms = NULL;
1992 codesecsym = 0;
1993 codesecsymend = 0;
1994 secsymend = 0;
1995 opdsymend = 0;
1996 symcount = 0;
1997 if (opd != NULL)
c9727e01 1998 {
a5259595
AM
1999 symcount = static_count;
2000 if (!relocatable)
2001 symcount += dyn_count;
2002 if (symcount == 0)
2003 return 0;
c9727e01 2004
a5259595
AM
2005 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2006 if (syms == NULL)
2007 return -1;
90e3cdf2 2008
a5259595
AM
2009 if (!relocatable && static_count != 0 && dyn_count != 0)
2010 {
2011 /* Use both symbol tables. */
2012 memcpy (syms, static_syms, static_count * sizeof (*syms));
2013 memcpy (syms + static_count, dyn_syms,
2014 (dyn_count + 1) * sizeof (*syms));
2015 }
2016 else if (!relocatable && static_count == 0)
2017 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2018 else
2019 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2020
0ccf57bd
AM
2021 /* Trim uninteresting symbols. Interesting symbols are section,
2022 function, and notype symbols. */
2023 for (i = 0, j = 0; i < symcount; ++i)
2024 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2025 | BSF_RELC | BSF_SRELC)) == 0)
2026 syms[j++] = syms[i];
2027 symcount = j;
2028
a5259595
AM
2029 synthetic_relocatable = relocatable;
2030 synthetic_opd = opd;
2031 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2032
a5259595
AM
2033 if (!relocatable && symcount > 1)
2034 {
bfa5bd2a
PA
2035 /* Trim duplicate syms, since we may have merged the normal
2036 and dynamic symbols. Actually, we only care about syms
2037 that have different values, so trim any with the same
2038 value. Don't consider ifunc and ifunc resolver symbols
2039 duplicates however, because GDB wants to know whether a
2040 text symbol is an ifunc resolver. */
a5259595 2041 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2042 {
2043 const asymbol *s0 = syms[i - 1];
2044 const asymbol *s1 = syms[i];
2045
2046 if ((s0->value + s0->section->vma
2047 != s1->value + s1->section->vma)
2048 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2049 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2050 syms[j++] = syms[i];
2051 }
a5259595
AM
2052 symcount = j;
2053 }
699733f6 2054
a5259595
AM
2055 i = 0;
2056 /* Note that here and in compare_symbols we can't compare opd and
2057 sym->section directly. With separate debug info files, the
2058 symbols will be extracted from the debug file while abfd passed
2059 to this function is the real binary. */
0ccf57bd 2060 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2061 ++i;
2062 codesecsym = i;
2063
2064 for (; i < symcount; ++i)
2065 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2066 | SEC_THREAD_LOCAL))
2067 != (SEC_CODE | SEC_ALLOC))
2068 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2069 break;
2070 codesecsymend = i;
2071
2072 for (; i < symcount; ++i)
2073 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2074 break;
2075 secsymend = i;
2076
2077 for (; i < symcount; ++i)
2078 if (strcmp (syms[i]->section->name, ".opd") != 0)
2079 break;
2080 opdsymend = i;
2081
2082 for (; i < symcount; ++i)
2cdcc330
AM
2083 if (((syms[i]->section->flags
2084 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2085 != (SEC_CODE | SEC_ALLOC))
2086 break;
2087 symcount = i;
2088 }
c9727e01 2089 count = 0;
90e3cdf2 2090
699733f6 2091 if (relocatable)
90e3cdf2 2092 {
699733f6
AM
2093 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2094 arelent *r;
2095 size_t size;
0ccf57bd 2096 size_t relcount;
90e3cdf2 2097
468392fb
AM
2098 if (opdsymend == secsymend)
2099 goto done;
2100
699733f6 2101 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2102 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2103 if (relcount == 0)
c9727e01 2104 goto done;
90e3cdf2 2105
7356fed5
AM
2106 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2107 {
2108 count = -1;
2109 goto done;
2110 }
2111
699733f6 2112 size = 0;
595da8c5 2113 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2114 {
2115 asymbol *sym;
90e3cdf2 2116
595da8c5 2117 while (r < opd->relocation + relcount
699733f6
AM
2118 && r->address < syms[i]->value + opd->vma)
2119 ++r;
90e3cdf2 2120
595da8c5 2121 if (r == opd->relocation + relcount)
699733f6 2122 break;
90e3cdf2 2123
699733f6
AM
2124 if (r->address != syms[i]->value + opd->vma)
2125 continue;
90e3cdf2 2126
699733f6
AM
2127 if (r->howto->type != R_PPC64_ADDR64)
2128 continue;
90e3cdf2 2129
699733f6
AM
2130 sym = *r->sym_ptr_ptr;
2131 if (!sym_exists_at (syms, opdsymend, symcount,
2132 sym->section->id, sym->value + r->addend))
2133 {
2134 ++count;
2135 size += sizeof (asymbol);
2136 size += strlen (syms[i]->name) + 2;
2137 }
2138 }
90e3cdf2 2139
c4b0b099
AM
2140 if (size == 0)
2141 goto done;
699733f6
AM
2142 s = *ret = bfd_malloc (size);
2143 if (s == NULL)
2144 {
7356fed5 2145 count = -1;
c9727e01 2146 goto done;
699733f6 2147 }
90e3cdf2 2148
699733f6 2149 names = (char *) (s + count);
90e3cdf2 2150
595da8c5 2151 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2152 {
699733f6 2153 asymbol *sym;
90e3cdf2 2154
595da8c5 2155 while (r < opd->relocation + relcount
699733f6
AM
2156 && r->address < syms[i]->value + opd->vma)
2157 ++r;
90e3cdf2 2158
595da8c5 2159 if (r == opd->relocation + relcount)
699733f6
AM
2160 break;
2161
2162 if (r->address != syms[i]->value + opd->vma)
2163 continue;
2164
2165 if (r->howto->type != R_PPC64_ADDR64)
2166 continue;
90e3cdf2 2167
699733f6
AM
2168 sym = *r->sym_ptr_ptr;
2169 if (!sym_exists_at (syms, opdsymend, symcount,
2170 sym->section->id, sym->value + r->addend))
2171 {
2172 size_t len;
2173
2174 *s = *syms[i];
6ba2a415 2175 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2176 s->section = sym->section;
2177 s->value = sym->value + r->addend;
2178 s->name = names;
2179 *names++ = '.';
2180 len = strlen (syms[i]->name);
2181 memcpy (names, syms[i]->name, len + 1);
2182 names += len + 1;
6f610d07
UW
2183 /* Have udata.p point back to the original symbol this
2184 synthetic symbol was derived from. */
2185 s->udata.p = syms[i];
699733f6
AM
2186 s++;
2187 }
2188 }
2189 }
2190 else
90e3cdf2 2191 {
468392fb 2192 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 2193 bfd_byte *contents = NULL;
699733f6 2194 size_t size;
0ccf57bd 2195 size_t plt_count = 0;
468392fb
AM
2196 bfd_vma glink_vma = 0, resolv_vma = 0;
2197 asection *dynamic, *glink = NULL, *relplt = NULL;
2198 arelent *p;
90e3cdf2 2199
ee67d69a 2200 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2201 {
c4b0b099
AM
2202 free_contents_and_exit_err:
2203 count = -1;
ee67d69a 2204 free_contents_and_exit:
699733f6 2205 if (contents)
ee67d69a 2206 free (contents);
c9727e01 2207 goto done;
699733f6 2208 }
90e3cdf2 2209
699733f6
AM
2210 size = 0;
2211 for (i = secsymend; i < opdsymend; ++i)
2212 {
2213 bfd_vma ent;
90e3cdf2 2214
5ef11c02
AM
2215 /* Ignore bogus symbols. */
2216 if (syms[i]->value > opd->size - 8)
2217 continue;
2218
699733f6
AM
2219 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2220 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2221 {
2222 ++count;
2223 size += sizeof (asymbol);
2224 size += strlen (syms[i]->name) + 2;
2225 }
2226 }
90e3cdf2 2227
468392fb 2228 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2229 if (dyn_count != 0
2230 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2231 {
2232 bfd_byte *dynbuf, *extdyn, *extdynend;
2233 size_t extdynsize;
2234 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2235
2236 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2237 goto free_contents_and_exit_err;
468392fb
AM
2238
2239 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2240 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2241
2242 extdyn = dynbuf;
2243 extdynend = extdyn + dynamic->size;
2244 for (; extdyn < extdynend; extdyn += extdynsize)
2245 {
2246 Elf_Internal_Dyn dyn;
2247 (*swap_dyn_in) (abfd, extdyn, &dyn);
2248
2249 if (dyn.d_tag == DT_NULL)
2250 break;
2251
2252 if (dyn.d_tag == DT_PPC64_GLINK)
2253 {
9e390558
AM
2254 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2255 See comment in ppc64_elf_finish_dynamic_sections. */
2256 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2257 /* The .glink section usually does not survive the final
2258 link; search for the section (usually .text) where the
2259 glink stubs now reside. */
2260 glink = bfd_sections_find_if (abfd, section_covers_vma,
2261 &glink_vma);
2262 break;
2263 }
2264 }
2265
2266 free (dynbuf);
2267 }
2268
2269 if (glink != NULL)
2270 {
2271 /* Determine __glink trampoline by reading the relative branch
2272 from the first glink stub. */
2273 bfd_byte buf[4];
b9e5796b
AM
2274 unsigned int off = 0;
2275
2276 while (bfd_get_section_contents (abfd, glink, buf,
2277 glink_vma + off - glink->vma, 4))
468392fb
AM
2278 {
2279 unsigned int insn = bfd_get_32 (abfd, buf);
2280 insn ^= B_DOT;
2281 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2282 {
2cdcc330
AM
2283 resolv_vma
2284 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2285 break;
2286 }
2287 off += 4;
2288 if (off > 4)
2289 break;
468392fb
AM
2290 }
2291
2292 if (resolv_vma)
2293 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2294
066ee829
AM
2295 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2296 if (relplt != NULL)
2297 {
2298 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2cdcc330 2299 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 2300 goto free_contents_and_exit_err;
68ffbac6 2301
066ee829
AM
2302 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2303 size += plt_count * sizeof (asymbol);
468392fb 2304
066ee829
AM
2305 p = relplt->relocation;
2306 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2307 {
2308 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2309 if (p->addend != 0)
2310 size += sizeof ("+0x") - 1 + 16;
2311 }
066ee829 2312 }
468392fb
AM
2313 }
2314
c4b0b099
AM
2315 if (size == 0)
2316 goto free_contents_and_exit;
699733f6
AM
2317 s = *ret = bfd_malloc (size);
2318 if (s == NULL)
c4b0b099 2319 goto free_contents_and_exit_err;
90e3cdf2 2320
468392fb 2321 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2322
699733f6 2323 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2324 {
699733f6 2325 bfd_vma ent;
90e3cdf2 2326
5ef11c02
AM
2327 if (syms[i]->value > opd->size - 8)
2328 continue;
2329
699733f6
AM
2330 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2331 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2332 {
0ccf57bd 2333 size_t lo, hi;
699733f6 2334 size_t len;
c9727e01 2335 asection *sec = abfd->sections;
90e3cdf2 2336
699733f6
AM
2337 *s = *syms[i];
2338 lo = codesecsym;
2339 hi = codesecsymend;
2340 while (lo < hi)
2341 {
0ccf57bd 2342 size_t mid = (lo + hi) >> 1;
699733f6
AM
2343 if (syms[mid]->section->vma < ent)
2344 lo = mid + 1;
2345 else if (syms[mid]->section->vma > ent)
2346 hi = mid;
2347 else
c9727e01
AM
2348 {
2349 sec = syms[mid]->section;
2350 break;
2351 }
699733f6
AM
2352 }
2353
c9727e01 2354 if (lo >= hi && lo > codesecsym)
699733f6 2355 sec = syms[lo - 1]->section;
699733f6
AM
2356
2357 for (; sec != NULL; sec = sec->next)
2358 {
2359 if (sec->vma > ent)
2360 break;
63524580
JK
2361 /* SEC_LOAD may not be set if SEC is from a separate debug
2362 info file. */
2363 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2364 break;
2365 if ((sec->flags & SEC_CODE) != 0)
2366 s->section = sec;
2367 }
6ba2a415 2368 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2369 s->value = ent - s->section->vma;
2370 s->name = names;
2371 *names++ = '.';
2372 len = strlen (syms[i]->name);
2373 memcpy (names, syms[i]->name, len + 1);
2374 names += len + 1;
6f610d07
UW
2375 /* Have udata.p point back to the original symbol this
2376 synthetic symbol was derived from. */
2377 s->udata.p = syms[i];
699733f6 2378 s++;
90e3cdf2 2379 }
90e3cdf2 2380 }
699733f6 2381 free (contents);
468392fb
AM
2382
2383 if (glink != NULL && relplt != NULL)
2384 {
2385 if (resolv_vma)
2386 {
2387 /* Add a symbol for the main glink trampoline. */
86a4952b 2388 memset (s, 0, sizeof *s);
468392fb 2389 s->the_bfd = abfd;
6ba2a415 2390 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2391 s->section = glink;
2392 s->value = resolv_vma - glink->vma;
2393 s->name = names;
2cdcc330
AM
2394 memcpy (names, "__glink_PLTresolve",
2395 sizeof ("__glink_PLTresolve"));
468392fb
AM
2396 names += sizeof ("__glink_PLTresolve");
2397 s++;
2398 count++;
2399 }
2400
2401 /* FIXME: It would be very much nicer to put sym@plt on the
2402 stub rather than on the glink branch table entry. The
2403 objdump disassembler would then use a sensible symbol
2404 name on plt calls. The difficulty in doing so is
2405 a) finding the stubs, and,
2406 b) matching stubs against plt entries, and,
2407 c) there can be multiple stubs for a given plt entry.
2408
2409 Solving (a) could be done by code scanning, but older
2410 ppc64 binaries used different stubs to current code.
2411 (b) is the tricky one since you need to known the toc
2412 pointer for at least one function that uses a pic stub to
2413 be able to calculate the plt address referenced.
2414 (c) means gdb would need to set multiple breakpoints (or
2415 find the glink branch itself) when setting breakpoints
2416 for pending shared library loads. */
2417 p = relplt->relocation;
2418 for (i = 0; i < plt_count; i++, p++)
2419 {
2420 size_t len;
2421
2422 *s = **p->sym_ptr_ptr;
2423 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2424 we are defining a symbol, ensure one of them is set. */
2425 if ((s->flags & BSF_LOCAL) == 0)
2426 s->flags |= BSF_GLOBAL;
6ba2a415 2427 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2428 s->section = glink;
2429 s->value = glink_vma - glink->vma;
2430 s->name = names;
2431 s->udata.p = NULL;
2432 len = strlen ((*p->sym_ptr_ptr)->name);
2433 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2434 names += len;
e054468f
AM
2435 if (p->addend != 0)
2436 {
2437 memcpy (names, "+0x", sizeof ("+0x") - 1);
2438 names += sizeof ("+0x") - 1;
2439 bfd_sprintf_vma (abfd, names, p->addend);
2440 names += strlen (names);
2441 }
468392fb
AM
2442 memcpy (names, "@plt", sizeof ("@plt"));
2443 names += sizeof ("@plt");
2444 s++;
b9e5796b
AM
2445 if (abi < 2)
2446 {
2447 glink_vma += 8;
2448 if (i >= 0x8000)
2449 glink_vma += 4;
2450 }
2451 else
468392fb
AM
2452 glink_vma += 4;
2453 }
2454 count += plt_count;
2455 }
90e3cdf2
JJ
2456 }
2457
c9727e01 2458 done:
a7535cf3 2459 free (syms);
90e3cdf2
JJ
2460 return count;
2461}
5bd4f169 2462\f
65f38f15
AM
2463/* The following functions are specific to the ELF linker, while
2464 functions above are used generally. Those named ppc64_elf_* are
2465 called by the main ELF linker code. They appear in this file more
2466 or less in the order in which they are called. eg.
2467 ppc64_elf_check_relocs is called early in the link process,
2468 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2469 called.
2470
2471 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2472 functions have both a function code symbol and a function descriptor
2473 symbol. A call to foo in a relocatable object file looks like:
2474
2475 . .text
2476 . x:
2477 . bl .foo
2478 . nop
2479
2480 The function definition in another object file might be:
2481
2482 . .section .opd
2483 . foo: .quad .foo
2484 . .quad .TOC.@tocbase
2485 . .quad 0
2486 .
2487 . .text
2488 . .foo: blr
2489
2490 When the linker resolves the call during a static link, the branch
2491 unsurprisingly just goes to .foo and the .opd information is unused.
2492 If the function definition is in a shared library, things are a little
2493 different: The call goes via a plt call stub, the opd information gets
2494 copied to the plt, and the linker patches the nop.
2495
2496 . x:
2497 . bl .foo_stub
2498 . ld 2,40(1)
2499 .
2500 .
2501 . .foo_stub:
71a39c98
AM
2502 . std 2,40(1) # in practice, the call stub
2503 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2504 . addi 11,11,Lfoo@toc@l # this is the general idea
2505 . ld 12,0(11)
2506 . ld 2,8(11)
2507 . mtctr 12
2508 . ld 11,16(11)
e86ce104
AM
2509 . bctr
2510 .
2511 . .section .plt
2512 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2513
2514 The "reloc ()" notation is supposed to indicate that the linker emits
2515 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2516 copying.
2517
2518 What are the difficulties here? Well, firstly, the relocations
2519 examined by the linker in check_relocs are against the function code
2520 sym .foo, while the dynamic relocation in the plt is emitted against
2521 the function descriptor symbol, foo. Somewhere along the line, we need
2522 to carefully copy dynamic link information from one symbol to the other.
2523 Secondly, the generic part of the elf linker will make .foo a dynamic
2524 symbol as is normal for most other backends. We need foo dynamic
2525 instead, at least for an application final link. However, when
2526 creating a shared library containing foo, we need to have both symbols
2527 dynamic so that references to .foo are satisfied during the early
2528 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2529 definition from some other object, eg. a static library.
2530
2531 Update: As of August 2004, we support a new convention. Function
2532 calls may use the function descriptor symbol, ie. "bl foo". This
2533 behaves exactly as "bl .foo". */
65f38f15 2534
7c8bbca5
AM
2535/* Of those relocs that might be copied as dynamic relocs, this
2536 function selects those that must be copied when linking a shared
2537 library or PIE, even when the symbol is local. */
65f38f15 2538
1d483afe
AM
2539static int
2540must_be_dyn_reloc (struct bfd_link_info *info,
2541 enum elf_ppc64_reloc_type r_type)
2542{
2543 switch (r_type)
2544 {
2545 default:
7c8bbca5
AM
2546 /* Only relative relocs can be resolved when the object load
2547 address isn't fixed. DTPREL64 is excluded because the
2548 dynamic linker needs to differentiate global dynamic from
2549 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2550 return 1;
2551
2552 case R_PPC64_REL32:
2553 case R_PPC64_REL64:
2554 case R_PPC64_REL30:
2555 return 0;
2556
2557 case R_PPC64_TPREL16:
2558 case R_PPC64_TPREL16_LO:
2559 case R_PPC64_TPREL16_HI:
2560 case R_PPC64_TPREL16_HA:
2561 case R_PPC64_TPREL16_DS:
2562 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2563 case R_PPC64_TPREL16_HIGH:
2564 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2565 case R_PPC64_TPREL16_HIGHER:
2566 case R_PPC64_TPREL16_HIGHERA:
2567 case R_PPC64_TPREL16_HIGHEST:
2568 case R_PPC64_TPREL16_HIGHESTA:
2569 case R_PPC64_TPREL64:
7c8bbca5
AM
2570 /* These relocations are relative but in a shared library the
2571 linker doesn't know the thread pointer base. */
2572 return bfd_link_dll (info);
1d483afe
AM
2573 }
2574}
65f38f15 2575
f4656909
AM
2576/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2577 copying dynamic variables from a shared lib into an app's dynbss
2578 section, and instead use a dynamic relocation to point into the
5d35169e
AM
2579 shared lib. With code that gcc generates, it's vital that this be
2580 enabled; In the PowerPC64 ABI, the address of a function is actually
2581 the address of a function descriptor, which resides in the .opd
2582 section. gcc uses the descriptor directly rather than going via the
2583 GOT as some other ABI's do, which means that initialized function
2584 pointers must reference the descriptor. Thus, a function pointer
2585 initialized to the address of a function in a shared library will
2586 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 2587 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
2588 presents a problem as a plt entry for that function is also
2589 initialized from the function descriptor symbol and the copy reloc
2590 may not be initialized first. */
a23b6845 2591#define ELIMINATE_COPY_RELOCS 1
f4656909 2592
721956f4 2593/* Section name for stubs is the associated section name plus this
29942be8
NC
2594 string. */
2595#define STUB_SUFFIX ".stub"
721956f4
AM
2596
2597/* Linker stubs.
2598 ppc_stub_long_branch:
2599 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2600 destination, but a 24 bit branch in a stub section will reach.
2601 . b dest
2602
2603 ppc_stub_plt_branch:
2604 Similar to the above, but a 24 bit branch in the stub section won't
2605 reach its destination.
71a39c98
AM
2606 . addis %r11,%r2,xxx@toc@ha
2607 . ld %r12,xxx@toc@l(%r11)
2608 . mtctr %r12
721956f4
AM
2609 . bctr
2610
2611 ppc_stub_plt_call:
2c66dc6c
AM
2612 Used to call a function in a shared library. If it so happens that
2613 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2614 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 2615 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
2616 . addis %r11,%r2,xxx@toc@ha
2617 . ld %r12,xxx+0@toc@l(%r11)
2618 . mtctr %r12
2619 . ld %r2,xxx+8@toc@l(%r11)
2620 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2621 . bctr
ad8e1ba5
AM
2622
2623 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2624 code to adjust the value and save r2 to support multiple toc sections.
2625 A ppc_stub_long_branch with an r2 offset looks like:
2626 . std %r2,40(%r1)
2627 . addis %r2,%r2,off@ha
2628 . addi %r2,%r2,off@l
2629 . b dest
2630
2631 A ppc_stub_plt_branch with an r2 offset looks like:
2632 . std %r2,40(%r1)
71a39c98
AM
2633 . addis %r11,%r2,xxx@toc@ha
2634 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
2635 . addis %r2,%r2,off@ha
2636 . addi %r2,%r2,off@l
71a39c98 2637 . mtctr %r12
ad8e1ba5 2638 . bctr
ac2df442 2639
05d0e962
AM
2640 All of the above stubs are shown as their ELFv1 variants. ELFv2
2641 variants exist too, simpler for plt calls since a new toc pointer
2642 and static chain are not loaded by the stub. In addition, ELFv2
2643 has some more complex stubs to handle calls marked with NOTOC
2644 relocs from functions where r2 is not a valid toc pointer. These
2645 come in two flavours, the ones shown below, and _both variants that
2646 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2647 one call is from a function where r2 is used as the toc pointer but
2648 needs a toc adjusting stub for small-model multi-toc, and another
2649 call is from a function where r2 is not valid.
2650 ppc_stub_long_branch_notoc:
2651 . mflr %r12
2652 . bcl 20,31,1f
2653 . 1:
2654 . mflr %r11
2655 . mtlr %r12
2cdcc330
AM
2656 . addis %r12,%r11,dest-1b@ha
2657 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2658 . b dest
2659
2660 ppc_stub_plt_branch_notoc:
2661 . mflr %r12
2662 . bcl 20,31,1f
2663 . 1:
2664 . mflr %r11
2665 . mtlr %r12
2666 . lis %r12,xxx-1b@highest
f891966f 2667 . ori %r12,%r12,xxx-1b@higher
05d0e962 2668 . sldi %r12,%r12,32
f891966f 2669 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2670 . ori %r12,%r12,xxx-1b@l
2671 . add %r12,%r11,%r12
2672 . mtctr %r12
2673 . bctr
2674
2675 ppc_stub_plt_call_notoc:
2676 . mflr %r12
2677 . bcl 20,31,1f
2678 . 1:
2679 . mflr %r11
2680 . mtlr %r12
2681 . lis %r12,xxx-1b@highest
f891966f 2682 . ori %r12,%r12,xxx-1b@higher
05d0e962 2683 . sldi %r12,%r12,32
f891966f 2684 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2685 . ori %r12,%r12,xxx-1b@l
2686 . ldx %r12,%r11,%r12
2687 . mtctr %r12
2688 . bctr
2689
2690 In cases where the high instructions would add zero, they are
2691 omitted and following instructions modified in some cases.
2692
2693 For a given stub group (a set of sections all using the same toc
2694 pointer value) there will be just one stub type used for any
2695 particular function symbol. For example, if printf is called from
2696 code with the tocsave optimization (ie. r2 saved in function
2697 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2698 and from other code without the tocsave optimization requiring a
2699 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2700 type will be created. Calls with the tocsave optimization will
2701 enter this stub after the instruction saving r2. A similar
2702 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2703 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2704 to call an external function like printf. If other calls to printf
2705 require a ppc_stub_plt_call linkage stub then a single
2706 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2707 call. If other calls to printf require a ppc_stub_plt_call_r2save
2708 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2709 be created and calls not requiring r2 to be saved will enter the
2710 stub after the r2 save instruction. There is an analogous
2711 hierarchy of long branch and plt branch stubs for local call
2712 linkage. */
721956f4 2713
2cdcc330
AM
2714enum ppc_stub_type
2715{
721956f4
AM
2716 ppc_stub_none,
2717 ppc_stub_long_branch,
ad8e1ba5 2718 ppc_stub_long_branch_r2off,
05d0e962
AM
2719 ppc_stub_long_branch_notoc,
2720 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 2721 ppc_stub_plt_branch,
ad8e1ba5 2722 ppc_stub_plt_branch_r2off,
05d0e962
AM
2723 ppc_stub_plt_branch_notoc,
2724 ppc_stub_plt_branch_both,
794e51c0 2725 ppc_stub_plt_call,
7341d5e2 2726 ppc_stub_plt_call_r2save,
05d0e962
AM
2727 ppc_stub_plt_call_notoc,
2728 ppc_stub_plt_call_both,
a4b6fadd
AM
2729 ppc_stub_global_entry,
2730 ppc_stub_save_res
721956f4
AM
2731};
2732
6f20ed8a
AM
2733/* Information on stub grouping. */
2734struct map_stub
2735{
2736 /* The stub section. */
2737 asection *stub_sec;
2738 /* This is the section to which stubs in the group will be attached. */
2739 asection *link_sec;
a4b6fadd
AM
2740 /* Next group. */
2741 struct map_stub *next;
2742 /* Whether to emit a copy of register save/restore functions in this
2743 group. */
2744 int needs_save_res;
df136d64
AM
2745 /* Current offset within stubs after the insn restoring lr in a
2746 _notoc or _both stub using bcl for pc-relative addressing, or
2747 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2748 unsigned int lr_restore;
2749 /* Accumulated size of EH info emitted to describe return address
2750 if stubs modify lr. Does not include 17 byte FDE header. */
2751 unsigned int eh_size;
2752 /* Offset in glink_eh_frame to the start of EH info for this group. */
2753 unsigned int eh_base;
6f20ed8a
AM
2754};
2755
2cdcc330
AM
2756struct ppc_stub_hash_entry
2757{
721956f4
AM
2758 /* Base hash table entry structure. */
2759 struct bfd_hash_entry root;
2760
ad8e1ba5
AM
2761 enum ppc_stub_type stub_type;
2762
6f20ed8a
AM
2763 /* Group information. */
2764 struct map_stub *group;
721956f4
AM
2765
2766 /* Offset within stub_sec of the beginning of this stub. */
2767 bfd_vma stub_offset;
2768
2769 /* Given the symbol's value and its section we can determine its final
2770 value when building the stubs (so the stub knows where to jump. */
2771 bfd_vma target_value;
2772 asection *target_section;
2773
721956f4
AM
2774 /* The symbol table entry, if any, that this was derived from. */
2775 struct ppc_link_hash_entry *h;
e054468f 2776 struct plt_entry *plt_ent;
721956f4 2777
2d7ad24e
AM
2778 /* Symbol type. */
2779 unsigned char symtype;
2780
6911b7dc
AM
2781 /* Symbol st_other. */
2782 unsigned char other;
721956f4
AM
2783};
2784
2cdcc330
AM
2785struct ppc_branch_hash_entry
2786{
721956f4
AM
2787 /* Base hash table entry structure. */
2788 struct bfd_hash_entry root;
2789
c456f082 2790 /* Offset within branch lookup table. */
721956f4
AM
2791 unsigned int offset;
2792
2793 /* Generation marker. */
2794 unsigned int iter;
2795};
65f38f15 2796
19e08130
AM
2797/* Used to track dynamic relocations for local symbols. */
2798struct ppc_dyn_relocs
2799{
2800 struct ppc_dyn_relocs *next;
2801
2802 /* The input section of the reloc. */
2803 asection *sec;
2804
2805 /* Total number of relocs copied for the input section. */
2806 unsigned int count : 31;
2807
2808 /* Whether this entry is for STT_GNU_IFUNC symbols. */
2809 unsigned int ifunc : 1;
2810};
2811
65f38f15
AM
2812struct ppc_link_hash_entry
2813{
2814 struct elf_link_hash_entry elf;
2815
2cdcc330
AM
2816 union
2817 {
b3fac117
AM
2818 /* A pointer to the most recently used stub hash entry against this
2819 symbol. */
2820 struct ppc_stub_hash_entry *stub_cache;
2821
2822 /* A pointer to the next symbol starting with a '.' */
2823 struct ppc_link_hash_entry *next_dot_sym;
2824 } u;
721956f4 2825
65f38f15 2826 /* Track dynamic relocs copied for this symbol. */
6061a67d 2827 struct elf_dyn_relocs *dyn_relocs;
e86ce104 2828
721956f4 2829 /* Link between function code and descriptor symbols. */
34814b9f 2830 struct ppc_link_hash_entry *oh;
721956f4 2831
e86ce104
AM
2832 /* Flag function code and descriptor symbols. */
2833 unsigned int is_func:1;
2834 unsigned int is_func_descriptor:1;
908b32fc 2835 unsigned int fake:1;
411e1bfb 2836
c5614fa4
AM
2837 /* Whether global opd/toc sym has been adjusted or not.
2838 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
2839 should be set for all globals defined in any opd/toc section. */
754021d0
AM
2840 unsigned int adjust_done:1;
2841
a4b6fadd
AM
2842 /* Set if this is an out-of-line register save/restore function,
2843 with non-standard calling convention. */
2844 unsigned int save_res:1;
2845
8b5f1ed8
AM
2846 /* Set if a duplicate symbol with non-zero localentry is detected,
2847 even when the duplicate symbol does not provide a definition. */
2848 unsigned int non_zero_localentry:1;
2849
411e1bfb 2850 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
2851 Bits are or'd into the mask as the corresponding relocs are
2852 encountered during check_relocs, with TLS_TLS being set when any
2853 of the other TLS bits are set. tls_optimize clears bits when
2854 optimizing to indicate the corresponding GOT entry type is not
2855 needed. If set, TLS_TLS is never cleared. tls_optimize may also
2856 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
2857 separate flag rather than setting TPREL just for convenience in
2858 distinguishing the two cases.
2859 These flags are also kept for local symbols. */
2860#define TLS_TLS 1 /* Any TLS reloc. */
2861#define TLS_GD 2 /* GD reloc. */
2862#define TLS_LD 4 /* LD reloc. */
2863#define TLS_TPREL 8 /* TPREL reloc, => IE. */
2864#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
2865#define TLS_MARK 32 /* __tls_get_addr call marked. */
e7b938ca 2866#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
37da22e5 2867#define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
f961d9dd 2868 unsigned char tls_mask;
37da22e5
AM
2869
2870 /* The above field is also used to mark function symbols. In which
2871 case TLS_TLS will be 0. */
2872#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 2873#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 2874#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
2875};
2876
2877/* ppc64 ELF linker hash table. */
2878
2879struct ppc_link_hash_table
2880{
2881 struct elf_link_hash_table elf;
2882
721956f4
AM
2883 /* The stub hash table. */
2884 struct bfd_hash_table stub_hash_table;
2885
2886 /* Another hash table for plt_branch stubs. */
2887 struct bfd_hash_table branch_hash_table;
2888
3b421ab3
AM
2889 /* Hash table for function prologue tocsave. */
2890 htab_t tocsave_htab;
2891
e7d1c40c
AM
2892 /* Various options and other info passed from the linker. */
2893 struct ppc64_elf_params *params;
721956f4 2894
6f20ed8a
AM
2895 /* The size of sec_info below. */
2896 unsigned int sec_info_arr_size;
2897
2898 /* Per-section array of extra section info. Done this way rather
2899 than as part of ppc64_elf_section_data so we have the info for
2900 non-ppc64 sections. */
2901 struct
2902 {
2903 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 2904 bfd_vma toc_off;
6f20ed8a
AM
2905
2906 union
2907 {
2908 /* The section group that this section belongs to. */
2909 struct map_stub *group;
2910 /* A temp section list pointer. */
2911 asection *list;
2912 } u;
2913 } *sec_info;
721956f4 2914
a4b6fadd
AM
2915 /* Linked list of groups. */
2916 struct map_stub *group;
2917
ad8e1ba5
AM
2918 /* Temp used when calculating TOC pointers. */
2919 bfd_vma toc_curr;
bf102f86
AM
2920 bfd *toc_bfd;
2921 asection *toc_first_sec;
ad8e1ba5 2922
b3fac117
AM
2923 /* Used when adding symbols. */
2924 struct ppc_link_hash_entry *dot_syms;
2925
33e44f2e 2926 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 2927 asection *glink;
9e390558 2928 asection *global_entry;
82bd7b59 2929 asection *sfpr;
2d7ad24e
AM
2930 asection *pltlocal;
2931 asection *relpltlocal;
4ce794b7
AM
2932 asection *brlt;
2933 asection *relbrlt;
58d180e8 2934 asection *glink_eh_frame;
ec338859 2935
8387904d
AM
2936 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
2937 struct ppc_link_hash_entry *tls_get_addr;
2938 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 2939
927be08e
AM
2940 /* The size of reliplt used by got entry relocs. */
2941 bfd_size_type got_reli_size;
2942
9b5ecbd0 2943 /* Statistics. */
7341d5e2 2944 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 2945
ee75fd95
AM
2946 /* Number of stubs against global syms. */
2947 unsigned long stub_globals;
2948
ee67d69a
AM
2949 /* Set if we're linking code with function descriptors. */
2950 unsigned int opd_abi:1;
2951
4c52953f 2952 /* Support for multiple toc sections. */
33c0ec9d 2953 unsigned int do_multi_toc:1;
4c52953f 2954 unsigned int multi_toc_needed:1;
927be08e 2955 unsigned int second_toc_pass:1;
67f0cbdb 2956 unsigned int do_toc_opt:1;
4c52953f 2957
9a23f96e
AM
2958 /* Set if tls optimization is enabled. */
2959 unsigned int do_tls_opt:1;
2960
3e04d765
AM
2961 /* Set if inline plt calls should be converted to direct calls. */
2962 unsigned int can_convert_all_inline_plt:1;
2963
5d1634d7 2964 /* Set on error. */
99877b66 2965 unsigned int stub_error:1;
721956f4 2966
8c5b4e52
AM
2967 /* Whether func_desc_adjust needs to be run over symbols. */
2968 unsigned int need_func_desc_adj:1;
721956f4 2969
82e66161
AM
2970 /* Whether there exist local gnu indirect function resolvers,
2971 referenced by dynamic relocations. */
2972 unsigned int local_ifunc_resolver:1;
2973 unsigned int maybe_local_ifunc_resolver:1;
2974
f378ab09
AM
2975 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
2976 unsigned int has_plt_localentry0:1;
2977
721956f4
AM
2978 /* Incremented every time we size stubs. */
2979 unsigned int stub_iteration;
5d1634d7 2980
87d72d41
AM
2981 /* Small local sym cache. */
2982 struct sym_cache sym_cache;
65f38f15
AM
2983};
2984
4c52953f
AM
2985/* Rename some of the generic section flags to better document how they
2986 are used here. */
b0dddeec
AM
2987
2988/* Nonzero if this section has TLS related relocations. */
2989#define has_tls_reloc sec_flg0
2990
23cedd1d 2991/* Nonzero if this section has an old-style call to __tls_get_addr. */
b0dddeec
AM
2992#define has_tls_get_addr_call sec_flg1
2993
2994/* Nonzero if this section has any toc or got relocs. */
2995#define has_toc_reloc sec_flg2
2996
2997/* Nonzero if this section has a call to another section that uses
2998 the toc or got. */
d77c8a4b 2999#define makes_toc_func_call sec_flg3
b0dddeec
AM
3000
3001/* Recursion protection when determining above flag. */
d77c8a4b 3002#define call_check_in_progress sec_flg4
70cc837d 3003#define call_check_done sec_flg5
4c52953f 3004
65f38f15
AM
3005/* Get the ppc64 ELF linker hash table from a link_info structure. */
3006
3007#define ppc_hash_table(p) \
4dfe6ac6
NC
3008 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3009 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3010
721956f4
AM
3011#define ppc_stub_hash_lookup(table, string, create, copy) \
3012 ((struct ppc_stub_hash_entry *) \
3013 bfd_hash_lookup ((table), (string), (create), (copy)))
3014
3015#define ppc_branch_hash_lookup(table, string, create, copy) \
3016 ((struct ppc_branch_hash_entry *) \
3017 bfd_hash_lookup ((table), (string), (create), (copy)))
3018
3019/* Create an entry in the stub hash table. */
3020
3021static struct bfd_hash_entry *
4ce794b7
AM
3022stub_hash_newfunc (struct bfd_hash_entry *entry,
3023 struct bfd_hash_table *table,
3024 const char *string)
721956f4
AM
3025{
3026 /* Allocate the structure if it has not already been allocated by a
3027 subclass. */
3028 if (entry == NULL)
3029 {
3030 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3031 if (entry == NULL)
3032 return entry;
3033 }
3034
3035 /* Call the allocation method of the superclass. */
3036 entry = bfd_hash_newfunc (entry, table, string);
3037 if (entry != NULL)
3038 {
3039 struct ppc_stub_hash_entry *eh;
3040
3041 /* Initialize the local fields. */
3042 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3043 eh->stub_type = ppc_stub_none;
6f20ed8a 3044 eh->group = NULL;
721956f4
AM
3045 eh->stub_offset = 0;
3046 eh->target_value = 0;
3047 eh->target_section = NULL;
721956f4 3048 eh->h = NULL;
6911b7dc 3049 eh->plt_ent = NULL;
6911b7dc 3050 eh->other = 0;
721956f4
AM
3051 }
3052
3053 return entry;
3054}
3055
3056/* Create an entry in the branch hash table. */
3057
3058static struct bfd_hash_entry *
4ce794b7
AM
3059branch_hash_newfunc (struct bfd_hash_entry *entry,
3060 struct bfd_hash_table *table,
3061 const char *string)
721956f4
AM
3062{
3063 /* Allocate the structure if it has not already been allocated by a
3064 subclass. */
3065 if (entry == NULL)
3066 {
3067 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3068 if (entry == NULL)
3069 return entry;
3070 }
3071
3072 /* Call the allocation method of the superclass. */
3073 entry = bfd_hash_newfunc (entry, table, string);
3074 if (entry != NULL)
3075 {
3076 struct ppc_branch_hash_entry *eh;
3077
3078 /* Initialize the local fields. */
3079 eh = (struct ppc_branch_hash_entry *) entry;
3080 eh->offset = 0;
3081 eh->iter = 0;
3082 }
3083
3084 return entry;
3085}
3086
65f38f15
AM
3087/* Create an entry in a ppc64 ELF linker hash table. */
3088
3089static struct bfd_hash_entry *
4ce794b7
AM
3090link_hash_newfunc (struct bfd_hash_entry *entry,
3091 struct bfd_hash_table *table,
3092 const char *string)
65f38f15
AM
3093{
3094 /* Allocate the structure if it has not already been allocated by a
3095 subclass. */
3096 if (entry == NULL)
3097 {
3098 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3099 if (entry == NULL)
3100 return entry;
3101 }
3102
3103 /* Call the allocation method of the superclass. */
3104 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3105 if (entry != NULL)
3106 {
3107 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3108
b3fac117 3109 memset (&eh->u.stub_cache, 0,
908b32fc 3110 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3111 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3112
3113 /* When making function calls, old ABI code references function entry
3114 points (dot symbols), while new ABI code references the function
3115 descriptor symbol. We need to make any combination of reference and
3116 definition work together, without breaking archive linking.
3117
3118 For a defined function "foo" and an undefined call to "bar":
3119 An old object defines "foo" and ".foo", references ".bar" (possibly
3120 "bar" too).
3121 A new object defines "foo" and references "bar".
3122
3123 A new object thus has no problem with its undefined symbols being
3124 satisfied by definitions in an old object. On the other hand, the
3125 old object won't have ".bar" satisfied by a new object.
3126
3127 Keep a list of newly added dot-symbols. */
3128
3129 if (string[0] == '.')
3130 {
3131 struct ppc_link_hash_table *htab;
3132
3133 htab = (struct ppc_link_hash_table *) table;
3134 eh->u.next_dot_sym = htab->dot_syms;
3135 htab->dot_syms = eh;
3136 }
65f38f15
AM
3137 }
3138
3139 return entry;
3140}
3141
2cdcc330
AM
3142struct tocsave_entry
3143{
3b421ab3
AM
3144 asection *sec;
3145 bfd_vma offset;
3146};
3147
3148static hashval_t
3149tocsave_htab_hash (const void *p)
3150{
3151 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3152 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3153}
3154
3155static int
3156tocsave_htab_eq (const void *p1, const void *p2)
3157{
3158 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3159 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3160 return e1->sec == e2->sec && e1->offset == e2->offset;
3161}
3162
68faa637
AM
3163/* Destroy a ppc64 ELF linker hash table. */
3164
3165static void
d495ab0d 3166ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3167{
d495ab0d 3168 struct ppc_link_hash_table *htab;
68faa637 3169
d495ab0d 3170 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3171 if (htab->tocsave_htab)
3172 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3173 bfd_hash_table_free (&htab->branch_hash_table);
3174 bfd_hash_table_free (&htab->stub_hash_table);
3175 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3176}
3177
65f38f15
AM
3178/* Create a ppc64 ELF linker hash table. */
3179
3180static struct bfd_link_hash_table *
4ce794b7 3181ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3182{
3183 struct ppc_link_hash_table *htab;
3184 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3185
4ce794b7 3186 htab = bfd_zmalloc (amt);
65f38f15
AM
3187 if (htab == NULL)
3188 return NULL;
3189
66eb6687 3190 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3191 sizeof (struct ppc_link_hash_entry),
3192 PPC64_ELF_DATA))
65f38f15 3193 {
e2d34d7d 3194 free (htab);
65f38f15
AM
3195 return NULL;
3196 }
3197
721956f4 3198 /* Init the stub hash table too. */
66eb6687
AM
3199 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3200 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3201 {
d495ab0d 3202 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3203 return NULL;
3204 }
721956f4
AM
3205
3206 /* And the branch hash table. */
66eb6687
AM
3207 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3208 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3209 {
3210 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3211 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3212 return NULL;
3213 }
721956f4 3214
3b421ab3
AM
3215 htab->tocsave_htab = htab_try_create (1024,
3216 tocsave_htab_hash,
3217 tocsave_htab_eq,
3218 NULL);
3219 if (htab->tocsave_htab == NULL)
2915c55b 3220 {
d495ab0d 3221 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3222 return NULL;
3223 }
d495ab0d 3224 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3225
3254fd24
AM
3226 /* Initializing two fields of the union is just cosmetic. We really
3227 only care about glist, but when compiled on a 32-bit host the
3228 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3229 debugger inspection of these fields look nicer. */
a6aa5195
AM
3230 htab->elf.init_got_refcount.refcount = 0;
3231 htab->elf.init_got_refcount.glist = NULL;
3232 htab->elf.init_plt_refcount.refcount = 0;
3233 htab->elf.init_plt_refcount.glist = NULL;
3234 htab->elf.init_got_offset.offset = 0;
3235 htab->elf.init_got_offset.glist = NULL;
3236 htab->elf.init_plt_offset.offset = 0;
3237 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3238
65f38f15
AM
3239 return &htab->elf.root;
3240}
3241
bfeb4a28
AM
3242/* Create sections for linker generated code. */
3243
3244static bfd_boolean
3245create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3246{
3247 struct ppc_link_hash_table *htab;
3248 flagword flags;
3249
3250 htab = ppc_hash_table (info);
3251
bfeb4a28
AM
3252 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3253 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3254 if (htab->params->save_restore_funcs)
3255 {
3256 /* Create .sfpr for code to save and restore fp regs. */
3257 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3258 flags);
3259 if (htab->sfpr == NULL
2cdcc330 3260 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b32547cd
AM
3261 return FALSE;
3262 }
3263
3264 if (bfd_link_relocatable (info))
3265 return TRUE;
bfeb4a28
AM
3266
3267 /* Create .glink for lazy dynamic linking support. */
3268 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3269 flags);
3270 if (htab->glink == NULL
2cdcc330 3271 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
bfeb4a28
AM
3272 return FALSE;
3273
9e390558
AM
3274 /* The part of .glink used by global entry stubs, separate so that
3275 it can be aligned appropriately without affecting htab->glink. */
3276 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3277 flags);
3278 if (htab->global_entry == NULL
2cdcc330 3279 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
9e390558
AM
3280 return FALSE;
3281
bfeb4a28
AM
3282 if (!info->no_ld_generated_unwind_info)
3283 {
3284 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3285 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3286 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3287 ".eh_frame",
3288 flags);
3289 if (htab->glink_eh_frame == NULL
3290 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3291 return FALSE;
3292 }
3293
3294 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3295 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3296 if (htab->elf.iplt == NULL
2cdcc330 3297 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
3298 return FALSE;
3299
3300 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3301 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3302 htab->elf.irelplt
3303 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3304 if (htab->elf.irelplt == NULL
2cdcc330 3305 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
3306 return FALSE;
3307
3308 /* Create branch lookup table for plt_branch stubs. */
3309 flags = (SEC_ALLOC | SEC_LOAD
3310 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3311 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3312 flags);
3313 if (htab->brlt == NULL
2cdcc330 3314 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
bfeb4a28
AM
3315 return FALSE;
3316
2d7ad24e
AM
3317 /* Local plt entries, put in .branch_lt but a separate section for
3318 convenience. */
3319 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3320 flags);
3321 if (htab->pltlocal == NULL
2cdcc330 3322 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
2d7ad24e
AM
3323 return FALSE;
3324
0e1862bb 3325 if (!bfd_link_pic (info))
bfeb4a28
AM
3326 return TRUE;
3327
3328 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3329 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3330 htab->relbrlt
3331 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3332 if (htab->relbrlt == NULL
2cdcc330 3333 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
bfeb4a28
AM
3334 return FALSE;
3335
2d7ad24e
AM
3336 htab->relpltlocal
3337 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3338 if (htab->relpltlocal == NULL
2cdcc330 3339 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
2d7ad24e
AM
3340 return FALSE;
3341
bfeb4a28
AM
3342 return TRUE;
3343}
3344
e717da7e
AM
3345/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3346
bfeb4a28 3347bfd_boolean
e7d1c40c
AM
3348ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3349 struct ppc64_elf_params *params)
e717da7e
AM
3350{
3351 struct ppc_link_hash_table *htab;
3352
e7d1c40c 3353 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3354
3355/* Always hook our dynamic sections into the first bfd, which is the
3356 linker created stub bfd. This ensures that the GOT header is at
3357 the start of the output TOC section. */
3358 htab = ppc_hash_table (info);
e7d1c40c
AM
3359 htab->elf.dynobj = params->stub_bfd;
3360 htab->params = params;
bfeb4a28 3361
bfeb4a28 3362 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3363}
3364
721956f4
AM
3365/* Build a name for an entry in the stub hash table. */
3366
3367static char *
4ce794b7
AM
3368ppc_stub_name (const asection *input_section,
3369 const asection *sym_sec,
3370 const struct ppc_link_hash_entry *h,
3371 const Elf_Internal_Rela *rel)
721956f4
AM
3372{
3373 char *stub_name;
bcaa2f82 3374 ssize_t len;
721956f4
AM
3375
3376 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3377 offsets from a sym as a branch target? In fact, we could
3378 probably assume the addend is always zero. */
3379 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3380
3381 if (h)
3382 {
3383 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3384 stub_name = bfd_malloc (len);
46de2a7c
AM
3385 if (stub_name == NULL)
3386 return stub_name;
3387
bcaa2f82
AM
3388 len = sprintf (stub_name, "%08x.%s+%x",
3389 input_section->id & 0xffffffff,
3390 h->elf.root.root.string,
3391 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3392 }
3393 else
3394 {
ad8e1ba5 3395 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3396 stub_name = bfd_malloc (len);
46de2a7c
AM
3397 if (stub_name == NULL)
3398 return stub_name;
3399
bcaa2f82
AM
3400 len = sprintf (stub_name, "%08x.%x:%x+%x",
3401 input_section->id & 0xffffffff,
3402 sym_sec->id & 0xffffffff,
3403 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3404 (int) rel->r_addend & 0xffffffff);
721956f4 3405 }
bcaa2f82 3406 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3407 stub_name[len - 2] = 0;
721956f4
AM
3408 return stub_name;
3409}
3410
3411/* Look up an entry in the stub hash. Stub entries are cached because
3412 creating the stub name takes a bit of time. */
3413
3414static struct ppc_stub_hash_entry *
4ce794b7
AM
3415ppc_get_stub_entry (const asection *input_section,
3416 const asection *sym_sec,
039b3fef 3417 struct ppc_link_hash_entry *h,
4ce794b7
AM
3418 const Elf_Internal_Rela *rel,
3419 struct ppc_link_hash_table *htab)
721956f4
AM
3420{
3421 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3422 struct map_stub *group;
721956f4
AM
3423
3424 /* If this input section is part of a group of sections sharing one
3425 stub section, then use the id of the first section in the group.
3426 Stub names need to include a section id, as there may well be
3427 more than one stub used to reach say, printf, and we need to
3428 distinguish between them. */
6f20ed8a 3429 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3430 if (group == NULL)
3431 return NULL;
721956f4 3432
b3fac117
AM
3433 if (h != NULL && h->u.stub_cache != NULL
3434 && h->u.stub_cache->h == h
6f20ed8a 3435 && h->u.stub_cache->group == group)
721956f4 3436 {
b3fac117 3437 stub_entry = h->u.stub_cache;
721956f4
AM
3438 }
3439 else
3440 {
3441 char *stub_name;
3442
6f20ed8a 3443 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3444 if (stub_name == NULL)
3445 return NULL;
3446
3447 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3448 stub_name, FALSE, FALSE);
721956f4 3449 if (h != NULL)
b3fac117 3450 h->u.stub_cache = stub_entry;
721956f4
AM
3451
3452 free (stub_name);
3453 }
3454
3455 return stub_entry;
3456}
3457
3458/* Add a new stub entry to the stub hash. Not all fields of the new
3459 stub entry are initialised. */
3460
3461static struct ppc_stub_hash_entry *
4ce794b7
AM
3462ppc_add_stub (const char *stub_name,
3463 asection *section,
25f53a85 3464 struct bfd_link_info *info)
721956f4 3465{
25f53a85 3466 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3467 struct map_stub *group;
721956f4
AM
3468 asection *link_sec;
3469 asection *stub_sec;
3470 struct ppc_stub_hash_entry *stub_entry;
3471
6f20ed8a
AM
3472 group = htab->sec_info[section->id].u.group;
3473 link_sec = group->link_sec;
3474 stub_sec = group->stub_sec;
721956f4
AM
3475 if (stub_sec == NULL)
3476 {
6f20ed8a
AM
3477 size_t namelen;
3478 bfd_size_type len;
3479 char *s_name;
721956f4 3480
6f20ed8a
AM
3481 namelen = strlen (link_sec->name);
3482 len = namelen + sizeof (STUB_SUFFIX);
3483 s_name = bfd_alloc (htab->params->stub_bfd, len);
3484 if (s_name == NULL)
3485 return NULL;
721956f4 3486
6f20ed8a
AM
3487 memcpy (s_name, link_sec->name, namelen);
3488 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3489 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3490 if (stub_sec == NULL)
3491 return NULL;
3492 group->stub_sec = stub_sec;
721956f4
AM
3493 }
3494
3495 /* Enter this entry into the linker stub hash table. */
3496 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3497 TRUE, FALSE);
721956f4
AM
3498 if (stub_entry == NULL)
3499 {
695344c0 3500 /* xgettext:c-format */
cf97bcb0
AM
3501 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3502 section->owner, stub_name);
721956f4
AM
3503 return NULL;
3504 }
3505
6f20ed8a 3506 stub_entry->group = group;
721956f4 3507 stub_entry->stub_offset = 0;
721956f4
AM
3508 return stub_entry;
3509}
3510
e717da7e
AM
3511/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3512 not already done. */
65f38f15 3513
b34976b6 3514static bfd_boolean
e717da7e 3515create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3516{
e717da7e
AM
3517 asection *got, *relgot;
3518 flagword flags;
3519 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3520
0c8d6e5c 3521 if (!is_ppc64_elf (abfd))
0ffa91dd 3522 return FALSE;
4dfe6ac6
NC
3523 if (htab == NULL)
3524 return FALSE;
0ffa91dd 3525
33e44f2e
AM
3526 if (!htab->elf.sgot
3527 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3528 return FALSE;
e717da7e
AM
3529
3530 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3531 | SEC_LINKER_CREATED);
3532
c456f082 3533 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3534 if (!got
e717da7e
AM
3535 || !bfd_set_section_alignment (abfd, got, 3))
3536 return FALSE;
65f38f15 3537
c456f082
AM
3538 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3539 flags | SEC_READONLY);
e717da7e 3540 if (!relgot
2cdcc330 3541 || !bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 3542 return FALSE;
e717da7e
AM
3543
3544 ppc64_elf_tdata (abfd)->got = got;
3545 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 3546 return TRUE;
65f38f15 3547}
5bd4f169 3548
b31867b6
AM
3549/* Follow indirect and warning symbol links. */
3550
3551static inline struct bfd_link_hash_entry *
3552follow_link (struct bfd_link_hash_entry *h)
3553{
3554 while (h->type == bfd_link_hash_indirect
3555 || h->type == bfd_link_hash_warning)
3556 h = h->u.i.link;
3557 return h;
3558}
3559
3560static inline struct elf_link_hash_entry *
3561elf_follow_link (struct elf_link_hash_entry *h)
3562{
3563 return (struct elf_link_hash_entry *) follow_link (&h->root);
3564}
3565
3566static inline struct ppc_link_hash_entry *
3567ppc_follow_link (struct ppc_link_hash_entry *h)
3568{
3569 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3570}
3571
40d16e0b
AM
3572/* Merge PLT info on FROM with that on TO. */
3573
3574static void
3575move_plt_plist (struct ppc_link_hash_entry *from,
3576 struct ppc_link_hash_entry *to)
3577{
3578 if (from->elf.plt.plist != NULL)
3579 {
3580 if (to->elf.plt.plist != NULL)
3581 {
3582 struct plt_entry **entp;
3583 struct plt_entry *ent;
3584
3585 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3586 {
3587 struct plt_entry *dent;
3588
3589 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3590 if (dent->addend == ent->addend)
3591 {
3592 dent->plt.refcount += ent->plt.refcount;
3593 *entp = ent->next;
3594 break;
3595 }
3596 if (dent == NULL)
3597 entp = &ent->next;
3598 }
3599 *entp = to->elf.plt.plist;
3600 }
3601
3602 to->elf.plt.plist = from->elf.plt.plist;
3603 from->elf.plt.plist = NULL;
3604 }
3605}
3606
65f38f15
AM
3607/* Copy the extra info we tack onto an elf_link_hash_entry. */
3608
3609static void
fcfa13d2
AM
3610ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3611 struct elf_link_hash_entry *dir,
3612 struct elf_link_hash_entry *ind)
65f38f15
AM
3613{
3614 struct ppc_link_hash_entry *edir, *eind;
3615
3616 edir = (struct ppc_link_hash_entry *) dir;
3617 eind = (struct ppc_link_hash_entry *) ind;
3618
c79d6685
AM
3619 edir->is_func |= eind->is_func;
3620 edir->is_func_descriptor |= eind->is_func_descriptor;
3621 edir->tls_mask |= eind->tls_mask;
3622 if (eind->oh != NULL)
3623 edir->oh = ppc_follow_link (eind->oh);
3624
474436e6 3625 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
3626 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3627 edir->elf.ref_regular |= eind->elf.ref_regular;
3628 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 3629 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
3630 edir->elf.needs_plt |= eind->elf.needs_plt;
3631 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 3632
d311bc8b
AM
3633 /* If we were called to copy over info for a weak sym, don't copy
3634 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3635 in order to simplify readonly_dynrelocs and save a field in the
3636 symbol hash entry, but that means dyn_relocs can't be used in any
3637 tests about a specific symbol, or affect other symbol flags which
ab2477e1 3638 are then tested. */
d311bc8b 3639 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 3640 return;
d311bc8b 3641
411e1bfb 3642 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 3643 if (eind->dyn_relocs != NULL)
65f38f15 3644 {
bbd7ec4a
AM
3645 if (edir->dyn_relocs != NULL)
3646 {
6061a67d
AM
3647 struct elf_dyn_relocs **pp;
3648 struct elf_dyn_relocs *p;
bbd7ec4a 3649
fcfa13d2 3650 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
3651 list. Merge any entries against the same section. */
3652 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3653 {
6061a67d 3654 struct elf_dyn_relocs *q;
bbd7ec4a
AM
3655
3656 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3657 if (q->sec == p->sec)
3658 {
3659 q->pc_count += p->pc_count;
3660 q->count += p->count;
3661 *pp = p->next;
3662 break;
3663 }
3664 if (q == NULL)
3665 pp = &p->next;
3666 }
3667 *pp = edir->dyn_relocs;
3668 }
3669
65f38f15
AM
3670 edir->dyn_relocs = eind->dyn_relocs;
3671 eind->dyn_relocs = NULL;
3672 }
65f38f15 3673
81848ca0
AM
3674 /* Copy over got entries that we may have already seen to the
3675 symbol which just became indirect. */
411e1bfb
AM
3676 if (eind->elf.got.glist != NULL)
3677 {
3678 if (edir->elf.got.glist != NULL)
3679 {
3680 struct got_entry **entp;
3681 struct got_entry *ent;
3682
3683 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3684 {
3685 struct got_entry *dent;
3686
3687 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3688 if (dent->addend == ent->addend
e717da7e 3689 && dent->owner == ent->owner
411e1bfb
AM
3690 && dent->tls_type == ent->tls_type)
3691 {
3692 dent->got.refcount += ent->got.refcount;
3693 *entp = ent->next;
3694 break;
3695 }
3696 if (dent == NULL)
3697 entp = &ent->next;
3698 }
3699 *entp = edir->elf.got.glist;
3700 }
3701
3702 edir->elf.got.glist = eind->elf.got.glist;
3703 eind->elf.got.glist = NULL;
3704 }
3705
3706 /* And plt entries. */
40d16e0b 3707 move_plt_plist (eind, edir);
411e1bfb 3708
fcfa13d2 3709 if (eind->elf.dynindx != -1)
411e1bfb 3710 {
fcfa13d2
AM
3711 if (edir->elf.dynindx != -1)
3712 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3713 edir->elf.dynstr_index);
411e1bfb
AM
3714 edir->elf.dynindx = eind->elf.dynindx;
3715 edir->elf.dynstr_index = eind->elf.dynstr_index;
3716 eind->elf.dynindx = -1;
3717 eind->elf.dynstr_index = 0;
3718 }
411e1bfb
AM
3719}
3720
8387904d
AM
3721/* Find the function descriptor hash entry from the given function code
3722 hash entry FH. Link the entries via their OH fields. */
3723
3724static struct ppc_link_hash_entry *
b31867b6 3725lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
3726{
3727 struct ppc_link_hash_entry *fdh = fh->oh;
3728
3729 if (fdh == NULL)
3730 {
3731 const char *fd_name = fh->elf.root.root.string + 1;
3732
3733 fdh = (struct ppc_link_hash_entry *)
3734 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
3735 if (fdh == NULL)
3736 return fdh;
3737
3738 fdh->is_func_descriptor = 1;
3739 fdh->oh = fh;
3740 fh->is_func = 1;
3741 fh->oh = fdh;
8387904d
AM
3742 }
3743
8c5b4e52
AM
3744 fdh = ppc_follow_link (fdh);
3745 fdh->is_func_descriptor = 1;
3746 fdh->oh = fh;
3747 return fdh;
8387904d
AM
3748}
3749
8c5b4e52 3750/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
3751
3752static struct ppc_link_hash_entry *
3753make_fdh (struct bfd_link_info *info,
908b32fc 3754 struct ppc_link_hash_entry *fh)
bb700d78 3755{
8c5b4e52
AM
3756 bfd *abfd = fh->elf.root.u.undef.abfd;
3757 struct bfd_link_hash_entry *bh = NULL;
bb700d78 3758 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
3759 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3760 ? BSF_WEAK
3761 : BSF_GLOBAL);
3762
3763 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3764 fh->elf.root.root.string + 1,
3765 flags, bfd_und_section_ptr, 0,
3766 NULL, FALSE, FALSE, &bh))
bb700d78
AM
3767 return NULL;
3768
3769 fdh = (struct ppc_link_hash_entry *) bh;
3770 fdh->elf.non_elf = 0;
908b32fc
AM
3771 fdh->fake = 1;
3772 fdh->is_func_descriptor = 1;
3773 fdh->oh = fh;
3774 fh->is_func = 1;
3775 fh->oh = fdh;
bb700d78
AM
3776 return fdh;
3777}
3778
8387904d
AM
3779/* Fix function descriptor symbols defined in .opd sections to be
3780 function type. */
555cd476
AM
3781
3782static bfd_boolean
c16153ae 3783ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 3784 struct bfd_link_info *info,
555cd476 3785 Elf_Internal_Sym *isym,
6911b7dc 3786 const char **name,
555cd476
AM
3787 flagword *flags ATTRIBUTE_UNUSED,
3788 asection **sec,
b53dfeb2 3789 bfd_vma *value)
555cd476 3790{
b53dfeb2 3791 if (*sec != NULL
f1885d1e 3792 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
3793 {
3794 asection *code_sec;
3795
3796 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3797 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
3798 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
3799
3800 /* If the symbol is a function defined in .opd, and the function
3801 code is in a discarded group, let it appear to be undefined. */
0e1862bb 3802 if (!bfd_link_relocatable (info)
b53dfeb2
AM
3803 && (*sec)->reloc_count != 0
3804 && opd_entry_value (*sec, *value, &code_sec, NULL,
3805 FALSE) != (bfd_vma) -1
3806 && discarded_section (code_sec))
3807 {
3808 *sec = bfd_und_section_ptr;
3809 isym->st_shndx = SHN_UNDEF;
3810 }
3811 }
dbd1e97e
AM
3812 else if (*sec != NULL
3813 && strcmp ((*sec)->name, ".toc") == 0
3814 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
3815 {
3816 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3817 if (htab != NULL)
3818 htab->params->object_in_toc = 1;
3819 }
433817dd 3820
6911b7dc
AM
3821 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3822 {
3823 if (abiversion (ibfd) == 0)
3824 set_abiversion (ibfd, 2);
3825 else if (abiversion (ibfd) == 1)
3826 {
cf97bcb0
AM
3827 _bfd_error_handler (_("symbol '%s' has invalid st_other"
3828 " for ABI version 1"), *name);
6911b7dc
AM
3829 bfd_set_error (bfd_error_bad_value);
3830 return FALSE;
3831 }
3832 }
3833
555cd476
AM
3834 return TRUE;
3835}
3836
6911b7dc
AM
3837/* Merge non-visibility st_other attributes: local entry point. */
3838
3839static void
3840ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
3841 const Elf_Internal_Sym *isym,
3842 bfd_boolean definition,
3843 bfd_boolean dynamic)
3844{
f378ab09 3845 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
3846 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
3847 | ELF_ST_VISIBILITY (h->other));
3848}
3849
8c5b4e52
AM
3850/* Hook called on merging a symbol. We use this to clear "fake" since
3851 we now have a real symbol. */
3852
3853static bfd_boolean
3854ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 3855 const Elf_Internal_Sym *isym,
8c5b4e52
AM
3856 asection **psec ATTRIBUTE_UNUSED,
3857 bfd_boolean newdef ATTRIBUTE_UNUSED,
3858 bfd_boolean olddef ATTRIBUTE_UNUSED,
3859 bfd *oldbfd ATTRIBUTE_UNUSED,
3860 const asection *oldsec ATTRIBUTE_UNUSED)
3861{
3862 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
3863 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3864 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
3865 return TRUE;
3866}
3867
8387904d 3868/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
3869 inclusion of a new ABI object archive that defines "bar".
3870 NAME is a symbol defined in an archive. Return a symbol in the hash
3871 table that might be satisfied by the archive symbols. */
8387904d
AM
3872
3873static struct elf_link_hash_entry *
3874ppc64_elf_archive_symbol_lookup (bfd *abfd,
3875 struct bfd_link_info *info,
3876 const char *name)
3877{
3878 struct elf_link_hash_entry *h;
3879 char *dot_name;
3880 size_t len;
3881
3882 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
3883 if (h != NULL
3884 /* Don't return this sym if it is a fake function descriptor
3885 created by add_symbol_adjust. */
8c5b4e52 3886 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
3887 return h;
3888
3889 if (name[0] == '.')
3890 return h;
3891
3892 len = strlen (name);
3893 dot_name = bfd_alloc (abfd, len + 2);
3894 if (dot_name == NULL)
e99955cd 3895 return (struct elf_link_hash_entry *) -1;
8387904d
AM
3896 dot_name[0] = '.';
3897 memcpy (dot_name + 1, name, len + 1);
3898 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
3899 bfd_release (abfd, dot_name);
3900 return h;
3901}
3902
3903/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
3904 new ABI object defines "bar". Well, at least, undefined dot symbols
3905 are made weak. This stops later archive searches from including an
3906 object if we already have a function descriptor definition. It also
35b0ce59
AM
3907 prevents the linker complaining about undefined symbols.
3908 We also check and correct mismatched symbol visibility here. The
3909 most restrictive visibility of the function descriptor and the
3910 function entry symbol is used. */
8387904d
AM
3911
3912static bfd_boolean
b3fac117 3913add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 3914{
8387904d
AM
3915 struct ppc_link_hash_table *htab;
3916 struct ppc_link_hash_entry *fdh;
3917
b3fac117
AM
3918 if (eh->elf.root.type == bfd_link_hash_warning)
3919 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 3920
8400d40d
AM
3921 if (eh->elf.root.type == bfd_link_hash_indirect)
3922 return TRUE;
3923
b3fac117
AM
3924 if (eh->elf.root.root.string[0] != '.')
3925 abort ();
8387904d 3926
b3fac117 3927 htab = ppc_hash_table (info);
4dfe6ac6
NC
3928 if (htab == NULL)
3929 return FALSE;
3930
b31867b6 3931 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
3932 if (fdh == NULL
3933 && !bfd_link_relocatable (info)
3934 && (eh->elf.root.type == bfd_link_hash_undefined
3935 || eh->elf.root.type == bfd_link_hash_undefweak)
3936 && eh->elf.ref_regular)
3937 {
3938 /* Make an undefined function descriptor sym, in order to
3939 pull in an --as-needed shared lib. Archives are handled
3940 elsewhere. */
3941 fdh = make_fdh (info, eh);
3942 if (fdh == NULL)
3943 return FALSE;
bb700d78 3944 }
8c5b4e52
AM
3945
3946 if (fdh != NULL)
8387904d 3947 {
35b0ce59
AM
3948 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
3949 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
3950
3951 /* Make both descriptor and entry symbol have the most
3952 constraining visibility of either symbol. */
35b0ce59
AM
3953 if (entry_vis < descr_vis)
3954 fdh->elf.other += entry_vis - descr_vis;
3955 else if (entry_vis > descr_vis)
3956 eh->elf.other += descr_vis - entry_vis;
3957
8c5b4e52
AM
3958 /* Propagate reference flags from entry symbol to function
3959 descriptor symbol. */
bc4e12de 3960 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 3961 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
3962 fdh->elf.ref_regular |= eh->elf.ref_regular;
3963 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
3964
3965 if (!fdh->elf.forced_local
3966 && fdh->elf.dynindx == -1
3967 && fdh->elf.versioned != versioned_hidden
3968 && (bfd_link_dll (info)
3969 || fdh->elf.def_dynamic
3970 || fdh->elf.ref_dynamic)
3971 && (eh->elf.ref_regular
3972 || eh->elf.def_regular))
3973 {
2cdcc330 3974 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
8c5b4e52 3975 return FALSE;
35b0ce59 3976 }
8387904d 3977 }
99877b66 3978
8387904d
AM
3979 return TRUE;
3980}
3981
f6c7c3e8
AM
3982/* Set up opd section info and abiversion for IBFD, and process list
3983 of dot-symbols we made in link_hash_newfunc. */
b3fac117 3984
8387904d 3985static bfd_boolean
f6c7c3e8 3986ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 3987{
99877b66 3988 struct ppc_link_hash_table *htab;
b3fac117 3989 struct ppc_link_hash_entry **p, *eh;
459609d6 3990 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 3991
459609d6 3992 if (opd != NULL && opd->size != 0)
b3fac117 3993 {
b9399fcf
AM
3994 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
3995 ppc64_elf_section_data (opd)->sec_type = sec_opd;
3996
459609d6
AM
3997 if (abiversion (ibfd) == 0)
3998 set_abiversion (ibfd, 1);
8a2058b5 3999 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4000 {
695344c0 4001 /* xgettext:c-format */
cf97bcb0
AM
4002 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4003 ibfd, abiversion (ibfd));
459609d6
AM
4004 bfd_set_error (bfd_error_bad_value);
4005 return FALSE;
f6c7c3e8 4006 }
b9399fcf 4007 }
f6c7c3e8 4008
b9399fcf
AM
4009 if (is_ppc64_elf (info->output_bfd))
4010 {
4011 /* For input files without an explicit abiversion in e_flags
4012 we should have flagged any with symbol st_other bits set
4013 as ELFv1 and above flagged those with .opd as ELFv2.
4014 Set the output abiversion if not yet set, and for any input
4015 still ambiguous, take its abiversion from the output.
4016 Differences in ABI are reported later. */
4017 if (abiversion (info->output_bfd) == 0)
4018 set_abiversion (info->output_bfd, abiversion (ibfd));
4019 else if (abiversion (ibfd) == 0)
4020 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4021 }
4022
459609d6
AM
4023 htab = ppc_hash_table (info);
4024 if (htab == NULL)
b9399fcf 4025 return TRUE;
459609d6 4026
b9399fcf
AM
4027 if (opd != NULL && opd->size != 0
4028 && (ibfd->flags & DYNAMIC) == 0
4029 && (opd->flags & SEC_RELOC) != 0
4030 && opd->reloc_count != 0
4031 && !bfd_is_abs_section (opd->output_section)
4032 && info->gc_sections)
4033 {
4034 /* Garbage collection needs some extra help with .opd sections.
4035 We don't want to necessarily keep everything referenced by
4036 relocs in .opd, as that would keep all functions. Instead,
4037 if we reference an .opd symbol (a function descriptor), we
4038 want to keep the function code symbol's section. This is
4039 easy for global symbols, but for local syms we need to keep
4040 information about the associated function section. */
4041 bfd_size_type amt;
4042 asection **opd_sym_map;
4043 Elf_Internal_Shdr *symtab_hdr;
4044 Elf_Internal_Rela *relocs, *rel_end, *rel;
4045
4046 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4047 opd_sym_map = bfd_zalloc (ibfd, amt);
4048 if (opd_sym_map == NULL)
4049 return FALSE;
4050 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4051 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4052 info->keep_memory);
4053 if (relocs == NULL)
4054 return FALSE;
4055 symtab_hdr = &elf_symtab_hdr (ibfd);
4056 rel_end = relocs + opd->reloc_count - 1;
4057 for (rel = relocs; rel < rel_end; rel++)
4058 {
4059 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4060 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4061
4062 if (r_type == R_PPC64_ADDR64
4063 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4064 && r_symndx < symtab_hdr->sh_info)
4065 {
4066 Elf_Internal_Sym *isym;
4067 asection *s;
4068
4069 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4070 if (isym == NULL)
4071 {
4072 if (elf_section_data (opd)->relocs != relocs)
4073 free (relocs);
4074 return FALSE;
4075 }
4076
4077 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4078 if (s != NULL && s != opd)
4079 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4080 }
4081 }
4082 if (elf_section_data (opd)->relocs != relocs)
4083 free (relocs);
4084 }
459609d6
AM
4085
4086 p = &htab->dot_syms;
4087 while ((eh = *p) != NULL)
4088 {
4089 *p = NULL;
4090 if (&eh->elf == htab->elf.hgot)
4091 ;
4092 else if (htab->elf.hgot == NULL
4093 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4094 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4095 else if (abiversion (ibfd) <= 1)
4096 {
4097 htab->need_func_desc_adj = 1;
4098 if (!add_symbol_adjust (eh, info))
4099 return FALSE;
4100 }
459609d6
AM
4101 p = &eh->u.next_dot_sym;
4102 }
b3fac117 4103 return TRUE;
8387904d
AM
4104}
4105
97fed1c9
JJ
4106/* Undo hash table changes when an --as-needed input file is determined
4107 not to be needed. */
4108
4109static bfd_boolean
e5034e59
AM
4110ppc64_elf_notice_as_needed (bfd *ibfd,
4111 struct bfd_link_info *info,
4112 enum notice_asneeded_action act)
97fed1c9 4113{
e5034e59
AM
4114 if (act == notice_not_needed)
4115 {
4116 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4117
e5034e59
AM
4118 if (htab == NULL)
4119 return FALSE;
4dfe6ac6 4120
e5034e59
AM
4121 htab->dot_syms = NULL;
4122 }
4123 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4124}
4125
aa374f67
AM
4126/* If --just-symbols against a final linked binary, then assume we need
4127 toc adjusting stubs when calling functions defined there. */
4128
4129static void
4130ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4131{
4132 if ((sec->flags & SEC_CODE) != 0
4133 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4134 && is_ppc64_elf (sec->owner))
4135 {
2c3f079f
AM
4136 if (abiversion (sec->owner) >= 2
4137 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4138 sec->has_toc_reloc = 1;
4139 }
4140 _bfd_elf_link_just_syms (sec, info);
4141}
4142
e054468f 4143static struct plt_entry **
4ce794b7
AM
4144update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4145 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4146{
4147 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4148 struct plt_entry **local_plt;
f961d9dd 4149 unsigned char *local_got_tls_masks;
411e1bfb
AM
4150
4151 if (local_got_ents == NULL)
4152 {
4153 bfd_size_type size = symtab_hdr->sh_info;
4154
e054468f
AM
4155 size *= (sizeof (*local_got_ents)
4156 + sizeof (*local_plt)
4157 + sizeof (*local_got_tls_masks));
4ce794b7 4158 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4159 if (local_got_ents == NULL)
e054468f 4160 return NULL;
411e1bfb
AM
4161 elf_local_got_ents (abfd) = local_got_ents;
4162 }
4163
37da22e5 4164 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4165 {
4166 struct got_entry *ent;
4167
4168 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4169 if (ent->addend == r_addend
4170 && ent->owner == abfd
4171 && ent->tls_type == tls_type)
411e1bfb
AM
4172 break;
4173 if (ent == NULL)
4174 {
4175 bfd_size_type amt = sizeof (*ent);
4ce794b7 4176 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4177 if (ent == NULL)
4178 return FALSE;
4179 ent->next = local_got_ents[r_symndx];
4180 ent->addend = r_addend;
e717da7e 4181 ent->owner = abfd;
411e1bfb 4182 ent->tls_type = tls_type;
927be08e 4183 ent->is_indirect = FALSE;
411e1bfb
AM
4184 ent->got.refcount = 0;
4185 local_got_ents[r_symndx] = ent;
4186 }
4187 ent->got.refcount += 1;
4188 }
4189
e054468f 4190 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4191 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4192 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4193
4194 return local_plt + r_symndx;
65f38f15
AM
4195}
4196
411e1bfb 4197static bfd_boolean
e054468f 4198update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4199{
411e1bfb 4200 struct plt_entry *ent;
1e2f5b6e 4201
e054468f 4202 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4203 if (ent->addend == addend)
4204 break;
4205 if (ent == NULL)
1e2f5b6e 4206 {
411e1bfb 4207 bfd_size_type amt = sizeof (*ent);
4ce794b7 4208 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4209 if (ent == NULL)
4210 return FALSE;
e054468f 4211 ent->next = *plist;
411e1bfb
AM
4212 ent->addend = addend;
4213 ent->plt.refcount = 0;
e054468f 4214 *plist = ent;
1e2f5b6e 4215 }
411e1bfb 4216 ent->plt.refcount += 1;
b34976b6 4217 return TRUE;
1e2f5b6e
AM
4218}
4219
e054468f
AM
4220static bfd_boolean
4221is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4222{
4223 return (r_type == R_PPC64_REL24
05d0e962 4224 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
4225 || r_type == R_PPC64_REL14
4226 || r_type == R_PPC64_REL14_BRTAKEN
4227 || r_type == R_PPC64_REL14_BRNTAKEN
4228 || r_type == R_PPC64_ADDR24
4229 || r_type == R_PPC64_ADDR14
4230 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d
AM
4231 || r_type == R_PPC64_ADDR14_BRNTAKEN
4232 || r_type == R_PPC64_PLTCALL);
4233}
4234
4235/* Relocs on inline plt call sequence insns prior to the call. */
4236
4237static bfd_boolean
4238is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4239{
4240 return (r_type == R_PPC64_PLT16_HA
4241 || r_type == R_PPC64_PLT16_HI
4242 || r_type == R_PPC64_PLT16_LO
4243 || r_type == R_PPC64_PLT16_LO_DS
4244 || r_type == R_PPC64_PLTSEQ);
e054468f
AM
4245}
4246
5bd4f169 4247/* Look through the relocs for a section during the first phase, and
65f38f15 4248 calculate needed space in the global offset table, procedure
5d1634d7 4249 linkage table, and dynamic reloc sections. */
5bd4f169 4250
b34976b6 4251static bfd_boolean
4ce794b7
AM
4252ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4253 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4254{
65f38f15 4255 struct ppc_link_hash_table *htab;
5bd4f169 4256 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4257 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4258 const Elf_Internal_Rela *rel;
4259 const Elf_Internal_Rela *rel_end;
5bd4f169 4260 asection *sreloc;
3a71aa26 4261 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 4262 bfd_boolean is_opd;
5bd4f169 4263
0e1862bb 4264 if (bfd_link_relocatable (info))
b34976b6 4265 return TRUE;
5bd4f169 4266
680a3378
AM
4267 /* Don't do anything special with non-loaded, non-alloced sections.
4268 In particular, any relocs in such sections should not affect GOT
4269 and PLT reference counting (ie. we don't allow them to create GOT
4270 or PLT entries), there's no possibility or desire to optimize TLS
4271 relocs, and there's not much point in propagating relocs to shared
4272 libs that the dynamic linker won't relocate. */
4273 if ((sec->flags & SEC_ALLOC) == 0)
4274 return TRUE;
4275
0c8d6e5c 4276 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4277
65f38f15 4278 htab = ppc_hash_table (info);
4dfe6ac6
NC
4279 if (htab == NULL)
4280 return FALSE;
4281
3a71aa26
AM
4282 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4283 FALSE, FALSE, TRUE);
4284 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4285 FALSE, FALSE, TRUE);
0ffa91dd 4286 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4287 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4288 sreloc = NULL;
b9399fcf 4289 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4290 rel_end = relocs + sec->reloc_count;
4291 for (rel = relocs; rel < rel_end; rel++)
4292 {
4293 unsigned long r_symndx;
4294 struct elf_link_hash_entry *h;
04c9666a 4295 enum elf_ppc64_reloc_type r_type;
727fc41e 4296 int tls_type;
7c8fe5c4 4297 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4298 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
4299
4300 r_symndx = ELF64_R_SYM (rel->r_info);
4301 if (r_symndx < symtab_hdr->sh_info)
4302 h = NULL;
4303 else
973a3492
L
4304 {
4305 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4306 h = elf_follow_link (h);
1c865ab2
AM
4307
4308 if (h == htab->elf.hgot)
4309 sec->has_toc_reloc = 1;
973a3492 4310 }
5bd4f169 4311
727fc41e 4312 tls_type = 0;
e054468f 4313 ifunc = NULL;
25f23106
AM
4314 if (h != NULL)
4315 {
4316 if (h->type == STT_GNU_IFUNC)
4317 {
4318 h->needs_plt = 1;
4319 ifunc = &h->plt.plist;
4320 }
4321 }
4322 else
4323 {
4324 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4325 abfd, r_symndx);
4326 if (isym == NULL)
4327 return FALSE;
4328
4329 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4330 {
4331 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
37da22e5
AM
4332 rel->r_addend,
4333 NON_GOT | PLT_IFUNC);
25f23106
AM
4334 if (ifunc == NULL)
4335 return FALSE;
4336 }
4337 }
727fc41e 4338
cbf95972 4339 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 4340 switch (r_type)
5bd4f169 4341 {
727fc41e
AM
4342 case R_PPC64_TLSGD:
4343 case R_PPC64_TLSLD:
4344 /* These special tls relocs tie a call to __tls_get_addr with
4345 its parameter symbol. */
37da22e5
AM
4346 if (h != NULL)
4347 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4348 else
4349 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4350 rel->r_addend,
4351 NON_GOT | TLS_TLS | TLS_MARK))
4352 return FALSE;
4353 sec->has_tls_reloc = 1;
727fc41e
AM
4354 break;
4355
411e1bfb
AM
4356 case R_PPC64_GOT_TLSLD16:
4357 case R_PPC64_GOT_TLSLD16_LO:
4358 case R_PPC64_GOT_TLSLD16_HI:
4359 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 4360 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4361 goto dogottls;
4362
4363 case R_PPC64_GOT_TLSGD16:
4364 case R_PPC64_GOT_TLSGD16_LO:
4365 case R_PPC64_GOT_TLSGD16_HI:
4366 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 4367 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4368 goto dogottls;
4369
4370 case R_PPC64_GOT_TPREL16_DS:
4371 case R_PPC64_GOT_TPREL16_LO_DS:
4372 case R_PPC64_GOT_TPREL16_HI:
4373 case R_PPC64_GOT_TPREL16_HA:
7c8bbca5 4374 if (bfd_link_dll (info))
411e1bfb
AM
4375 info->flags |= DF_STATIC_TLS;
4376 tls_type = TLS_TLS | TLS_TPREL;
4377 goto dogottls;
4378
4379 case R_PPC64_GOT_DTPREL16_DS:
4380 case R_PPC64_GOT_DTPREL16_LO_DS:
4381 case R_PPC64_GOT_DTPREL16_HI:
4382 case R_PPC64_GOT_DTPREL16_HA:
4383 tls_type = TLS_TLS | TLS_DTPREL;
4384 dogottls:
4385 sec->has_tls_reloc = 1;
1a0670f3 4386 /* Fall through */
411e1bfb 4387
5bd4f169 4388 case R_PPC64_GOT16:
5bd4f169 4389 case R_PPC64_GOT16_DS:
65f38f15
AM
4390 case R_PPC64_GOT16_HA:
4391 case R_PPC64_GOT16_HI:
4392 case R_PPC64_GOT16_LO:
5bd4f169 4393 case R_PPC64_GOT16_LO_DS:
65f38f15 4394 /* This symbol requires a global offset table entry. */
4c52953f 4395 sec->has_toc_reloc = 1;
33c0ec9d
AM
4396 if (r_type == R_PPC64_GOT_TLSLD16
4397 || r_type == R_PPC64_GOT_TLSGD16
4398 || r_type == R_PPC64_GOT_TPREL16_DS
4399 || r_type == R_PPC64_GOT_DTPREL16_DS
4400 || r_type == R_PPC64_GOT16
4401 || r_type == R_PPC64_GOT16_DS)
4402 {
4403 htab->do_multi_toc = 1;
d77c8a4b 4404 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4405 }
4406
e717da7e
AM
4407 if (ppc64_elf_tdata (abfd)->got == NULL
4408 && !create_got_section (abfd, info))
b34976b6 4409 return FALSE;
5bd4f169
AM
4410
4411 if (h != NULL)
4412 {
411e1bfb
AM
4413 struct ppc_link_hash_entry *eh;
4414 struct got_entry *ent;
65f38f15 4415
411e1bfb
AM
4416 eh = (struct ppc_link_hash_entry *) h;
4417 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4418 if (ent->addend == rel->r_addend
e717da7e 4419 && ent->owner == abfd
411e1bfb
AM
4420 && ent->tls_type == tls_type)
4421 break;
4422 if (ent == NULL)
5bd4f169 4423 {
411e1bfb 4424 bfd_size_type amt = sizeof (*ent);
4ce794b7 4425 ent = bfd_alloc (abfd, amt);
411e1bfb 4426 if (ent == NULL)
b34976b6 4427 return FALSE;
411e1bfb
AM
4428 ent->next = eh->elf.got.glist;
4429 ent->addend = rel->r_addend;
e717da7e 4430 ent->owner = abfd;
411e1bfb 4431 ent->tls_type = tls_type;
927be08e 4432 ent->is_indirect = FALSE;
411e1bfb
AM
4433 ent->got.refcount = 0;
4434 eh->elf.got.glist = ent;
5bd4f169 4435 }
411e1bfb 4436 ent->got.refcount += 1;
e7b938ca 4437 eh->tls_mask |= tls_type;
5bd4f169 4438 }
411e1bfb
AM
4439 else
4440 /* This is a global offset table entry for a local symbol. */
4441 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4442 rel->r_addend, tls_type))
4443 return FALSE;
a345bc8d
AM
4444
4445 /* We may also need a plt entry if the symbol turns out to be
4446 an ifunc. */
0e1862bb 4447 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
4448 {
4449 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4450 return FALSE;
4451 }
5bd4f169
AM
4452 break;
4453
5bd4f169 4454 case R_PPC64_PLT16_HA:
65f38f15
AM
4455 case R_PPC64_PLT16_HI:
4456 case R_PPC64_PLT16_LO:
08be3224 4457 case R_PPC64_PLT16_LO_DS:
65f38f15
AM
4458 case R_PPC64_PLT32:
4459 case R_PPC64_PLT64:
cbf95972
AM
4460 /* This symbol requires a procedure linkage table entry. */
4461 plt_list = ifunc;
4462 if (h != NULL)
e054468f 4463 {
e054468f
AM
4464 h->needs_plt = 1;
4465 if (h->root.root.string[0] == '.'
4466 && h->root.root.string[1] != '\0')
4467 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2d7ad24e 4468 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4469 plt_list = &h->plt.plist;
4470 }
4471 if (plt_list == NULL)
2d7ad24e
AM
4472 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4473 rel->r_addend,
4474 NON_GOT | PLT_KEEP);
cbf95972
AM
4475 if (!update_plt_info (abfd, plt_list, rel->r_addend))
4476 return FALSE;
5bd4f169
AM
4477 break;
4478
4479 /* The following relocations don't need to propagate the
4480 relocation if linking a shared object since they are
4481 section relative. */
4482 case R_PPC64_SECTOFF:
4483 case R_PPC64_SECTOFF_LO:
4484 case R_PPC64_SECTOFF_HI:
4485 case R_PPC64_SECTOFF_HA:
4486 case R_PPC64_SECTOFF_DS:
4487 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4488 case R_PPC64_DTPREL16:
4489 case R_PPC64_DTPREL16_LO:
4490 case R_PPC64_DTPREL16_HI:
4491 case R_PPC64_DTPREL16_HA:
4492 case R_PPC64_DTPREL16_DS:
4493 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
4494 case R_PPC64_DTPREL16_HIGH:
4495 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
4496 case R_PPC64_DTPREL16_HIGHER:
4497 case R_PPC64_DTPREL16_HIGHERA:
4498 case R_PPC64_DTPREL16_HIGHEST:
4499 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4500 break;
4501
ad8e1ba5 4502 /* Nor do these. */
25f23106
AM
4503 case R_PPC64_REL16:
4504 case R_PPC64_REL16_LO:
4505 case R_PPC64_REL16_HI:
4506 case R_PPC64_REL16_HA:
4a969973
AM
4507 case R_PPC64_REL16_HIGH:
4508 case R_PPC64_REL16_HIGHA:
4509 case R_PPC64_REL16_HIGHER:
4510 case R_PPC64_REL16_HIGHERA:
4511 case R_PPC64_REL16_HIGHEST:
4512 case R_PPC64_REL16_HIGHESTA:
a680de9a 4513 case R_PPC64_REL16DX_HA:
25f23106
AM
4514 break;
4515
45965137
AM
4516 /* Not supported as a dynamic relocation. */
4517 case R_PPC64_ADDR64_LOCAL:
0e1862bb 4518 if (bfd_link_pic (info))
45965137
AM
4519 {
4520 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4521 ppc_howto_init ();
695344c0 4522 /* xgettext:c-format */
174d0a74 4523 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 4524 "in shared libraries and PIEs\n"),
45965137
AM
4525 abfd, sec, rel->r_offset,
4526 ppc64_elf_howto_table[r_type]->name);
4527 bfd_set_error (bfd_error_bad_value);
4528 return FALSE;
4529 }
4530 break;
4531
ad8e1ba5 4532 case R_PPC64_TOC16:
33c0ec9d
AM
4533 case R_PPC64_TOC16_DS:
4534 htab->do_multi_toc = 1;
d77c8a4b 4535 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 4536 /* Fall through. */
ad8e1ba5
AM
4537 case R_PPC64_TOC16_LO:
4538 case R_PPC64_TOC16_HI:
4539 case R_PPC64_TOC16_HA:
ad8e1ba5 4540 case R_PPC64_TOC16_LO_DS:
4c52953f 4541 sec->has_toc_reloc = 1;
ad8e1ba5
AM
4542 break;
4543
006589cf
AM
4544 /* Marker reloc. */
4545 case R_PPC64_ENTRY:
4546 break;
4547
5bd4f169
AM
4548 /* This relocation describes the C++ object vtable hierarchy.
4549 Reconstruct it for later use during GC. */
4550 case R_PPC64_GNU_VTINHERIT:
c152c796 4551 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4552 return FALSE;
5bd4f169
AM
4553 break;
4554
4555 /* This relocation describes which C++ vtable entries are actually
4556 used. Record for later use during GC. */
4557 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
4558 BFD_ASSERT (h != NULL);
4559 if (h != NULL
4560 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4561 return FALSE;
5bd4f169
AM
4562 break;
4563
721956f4
AM
4564 case R_PPC64_REL14:
4565 case R_PPC64_REL14_BRTAKEN:
4566 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4567 {
4568 asection *dest = NULL;
4569
4570 /* Heuristic: If jumping outside our section, chances are
4571 we are going to need a stub. */
4572 if (h != NULL)
4573 {
4574 /* If the sym is weak it may be overridden later, so
4575 don't assume we know where a weak sym lives. */
4576 if (h->root.type == bfd_link_hash_defined)
4577 dest = h->root.u.def.section;
4578 }
4579 else
87d72d41
AM
4580 {
4581 Elf_Internal_Sym *isym;
4582
4583 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4584 abfd, r_symndx);
4585 if (isym == NULL)
4586 return FALSE;
4587
4588 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4589 }
4590
220c76dd 4591 if (dest != sec)
7c8fe5c4 4592 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4593 }
3e04d765
AM
4594 goto rel24;
4595
4596 case R_PPC64_PLTCALL:
4597 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
4598 /* Fall through. */
4599
5d1634d7 4600 case R_PPC64_REL24:
05d0e962 4601 case R_PPC64_REL24_NOTOC:
3e04d765 4602 rel24:
cbf95972
AM
4603 plt_list = ifunc;
4604 if (h != NULL)
5d1634d7 4605 {
e054468f
AM
4606 h->needs_plt = 1;
4607 if (h->root.root.string[0] == '.'
4608 && h->root.root.string[1] != '\0')
4609 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 4610
3a71aa26 4611 if (h == tga || h == dottga)
cbf95972
AM
4612 {
4613 sec->has_tls_reloc = 1;
4614 if (rel != relocs
4615 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4616 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4617 /* We have a new-style __tls_get_addr call with
4618 a marker reloc. */
4619 ;
4620 else
4621 /* Mark this section as having an old-style call. */
4622 sec->has_tls_get_addr_call = 1;
4623 }
4624 plt_list = &h->plt.plist;
411e1bfb 4625 }
cbf95972
AM
4626
4627 /* We may need a .plt entry if the function this reloc
4628 refers to is in a shared lib. */
4629 if (plt_list
4630 && !update_plt_info (abfd, plt_list, rel->r_addend))
4631 return FALSE;
411e1bfb
AM
4632 break;
4633
cbf95972
AM
4634 case R_PPC64_ADDR14:
4635 case R_PPC64_ADDR14_BRNTAKEN:
4636 case R_PPC64_ADDR14_BRTAKEN:
4637 case R_PPC64_ADDR24:
4638 goto dodyn;
4639
411e1bfb
AM
4640 case R_PPC64_TPREL64:
4641 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 4642 if (bfd_link_dll (info))
411e1bfb
AM
4643 info->flags |= DF_STATIC_TLS;
4644 goto dotlstoc;
4645
4646 case R_PPC64_DTPMOD64:
4647 if (rel + 1 < rel_end
4648 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4649 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 4650 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 4651 else
951fd09b 4652 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
4653 goto dotlstoc;
4654
4655 case R_PPC64_DTPREL64:
4656 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4657 if (rel != relocs
4658 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4659 && rel[-1].r_offset == rel->r_offset - 8)
4660 /* This is the second reloc of a dtpmod, dtprel pair.
4661 Don't mark with TLS_DTPREL. */
4662 goto dodyn;
4663
4664 dotlstoc:
4665 sec->has_tls_reloc = 1;
4666 if (h != NULL)
4667 {
4668 struct ppc_link_hash_entry *eh;
4669 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 4670 eh->tls_mask |= tls_type;
411e1bfb
AM
4671 }
4672 else
4673 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4674 rel->r_addend, tls_type))
4675 return FALSE;
4676
7c8fe5c4
AM
4677 ppc64_sec = ppc64_elf_section_data (sec);
4678 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 4679 {
3a71aa26
AM
4680 bfd_size_type amt;
4681
e7b938ca 4682 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
4683 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4684 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4685 if (ppc64_sec->u.toc.symndx == NULL)
4686 return FALSE;
4687 amt = sec->size * sizeof (bfd_vma) / 8;
4688 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4689 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 4690 return FALSE;
7c8fe5c4
AM
4691 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4692 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
4693 }
4694 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
4695 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
4696 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
4697
4698 /* Mark the second slot of a GD or LD entry.
4699 -1 to indicate GD and -2 to indicate LD. */
4700 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 4701 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 4702 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 4703 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
4704 goto dodyn;
4705
4706 case R_PPC64_TPREL16:
4707 case R_PPC64_TPREL16_LO:
4708 case R_PPC64_TPREL16_HI:
4709 case R_PPC64_TPREL16_HA:
4710 case R_PPC64_TPREL16_DS:
4711 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
4712 case R_PPC64_TPREL16_HIGH:
4713 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
4714 case R_PPC64_TPREL16_HIGHER:
4715 case R_PPC64_TPREL16_HIGHERA:
4716 case R_PPC64_TPREL16_HIGHEST:
4717 case R_PPC64_TPREL16_HIGHESTA:
7c8bbca5
AM
4718 if (bfd_link_dll (info))
4719 info->flags |= DF_STATIC_TLS;
4720 goto dodyn;
5d1634d7 4721
e86ce104 4722 case R_PPC64_ADDR64:
b9399fcf 4723 if (is_opd
1e2f5b6e 4724 && rel + 1 < rel_end
4ce794b7 4725 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 4726 {
8387904d 4727 if (h != NULL)
8c5b4e52 4728 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 4729 }
e86ce104
AM
4730 /* Fall through. */
4731
65f38f15
AM
4732 case R_PPC64_ADDR16:
4733 case R_PPC64_ADDR16_DS:
4734 case R_PPC64_ADDR16_HA:
4735 case R_PPC64_ADDR16_HI:
f9c6b907
AM
4736 case R_PPC64_ADDR16_HIGH:
4737 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
4738 case R_PPC64_ADDR16_HIGHER:
4739 case R_PPC64_ADDR16_HIGHERA:
4740 case R_PPC64_ADDR16_HIGHEST:
4741 case R_PPC64_ADDR16_HIGHESTA:
4742 case R_PPC64_ADDR16_LO:
4743 case R_PPC64_ADDR16_LO_DS:
0e1862bb 4744 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
4745 && rel->r_addend == 0)
4746 {
4747 /* We may need a .plt entry if this reloc refers to a
4748 function in a shared lib. */
4749 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4750 return FALSE;
4751 h->pointer_equality_needed = 1;
4752 }
4753 /* Fall through. */
4754
4755 case R_PPC64_REL30:
4756 case R_PPC64_REL32:
4757 case R_PPC64_REL64:
65f38f15 4758 case R_PPC64_ADDR32:
65f38f15
AM
4759 case R_PPC64_UADDR16:
4760 case R_PPC64_UADDR32:
4761 case R_PPC64_UADDR64:
5bd4f169 4762 case R_PPC64_TOC:
0e1862bb 4763 if (h != NULL && !bfd_link_pic (info))
81848ca0 4764 /* We may need a copy reloc. */
f5385ebf 4765 h->non_got_ref = 1;
81848ca0 4766
41bd81ab 4767 /* Don't propagate .opd relocs. */
b9399fcf 4768 if (NO_OPD_RELOCS && is_opd)
e86ce104 4769 break;
e86ce104 4770
65f38f15
AM
4771 /* If we are creating a shared library, and this is a reloc
4772 against a global symbol, or a non PC relative reloc
4773 against a local symbol, then we need to copy the reloc
4774 into the shared library. However, if we are linking with
4775 -Bsymbolic, we do not need to copy a reloc against a
4776 global symbol which is defined in an object we are
4777 including in the link (i.e., DEF_REGULAR is set). At
4778 this point we have not seen all the input files, so it is
4779 possible that DEF_REGULAR is not set now but will be set
4780 later (it is never cleared). In case of a weak definition,
4781 DEF_REGULAR may be cleared later by a strong definition in
4782 a shared library. We account for that possibility below by
f4656909 4783 storing information in the dyn_relocs field of the hash
65f38f15
AM
4784 table entry. A similar situation occurs when creating
4785 shared libraries and symbol visibility changes render the
4786 symbol local.
4787
4788 If on the other hand, we are creating an executable, we
4789 may need to keep relocations for symbols satisfied by a
4790 dynamic library if we manage to avoid copy relocs for the
4791 symbol. */
411e1bfb 4792 dodyn:
0e1862bb 4793 if ((bfd_link_pic (info)
1d483afe 4794 && (must_be_dyn_reloc (info, r_type)
65f38f15 4795 || (h != NULL
198f1157 4796 && (!SYMBOLIC_BIND (info, h)
65f38f15 4797 || h->root.type == bfd_link_hash_defweak
f5385ebf 4798 || !h->def_regular))))
f4656909 4799 || (ELIMINATE_COPY_RELOCS
0e1862bb 4800 && !bfd_link_pic (info)
65f38f15
AM
4801 && h != NULL
4802 && (h->root.type == bfd_link_hash_defweak
25f23106 4803 || !h->def_regular))
0e1862bb 4804 || (!bfd_link_pic (info)
25f23106 4805 && ifunc != NULL))
5bd4f169 4806 {
65f38f15
AM
4807 /* We must copy these reloc types into the output file.
4808 Create a reloc section in dynobj and make room for
4809 this reloc. */
5bd4f169
AM
4810 if (sreloc == NULL)
4811 {
83bac4b0
NC
4812 sreloc = _bfd_elf_make_dynamic_reloc_section
4813 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 4814
5bd4f169 4815 if (sreloc == NULL)
83bac4b0 4816 return FALSE;
5bd4f169
AM
4817 }
4818
65f38f15
AM
4819 /* If this is a global symbol, we count the number of
4820 relocations we need for this symbol. */
4821 if (h != NULL)
4822 {
19e08130
AM
4823 struct elf_dyn_relocs *p;
4824 struct elf_dyn_relocs **head;
4825
ec338859 4826 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
4827 p = *head;
4828 if (p == NULL || p->sec != sec)
4829 {
4830 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4831 if (p == NULL)
4832 return FALSE;
4833 p->next = *head;
4834 *head = p;
4835 p->sec = sec;
4836 p->count = 0;
4837 p->pc_count = 0;
4838 }
4839 p->count += 1;
4840 if (!must_be_dyn_reloc (info, r_type))
4841 p->pc_count += 1;
65f38f15
AM
4842 }
4843 else
4844 {
ec338859
AM
4845 /* Track dynamic relocs needed for local syms too.
4846 We really need local syms available to do this
4847 easily. Oh well. */
19e08130
AM
4848 struct ppc_dyn_relocs *p;
4849 struct ppc_dyn_relocs **head;
4850 bfd_boolean is_ifunc;
ec338859 4851 asection *s;
6edfbbad 4852 void *vpp;
87d72d41 4853 Elf_Internal_Sym *isym;
6edfbbad 4854
87d72d41
AM
4855 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4856 abfd, r_symndx);
4857 if (isym == NULL)
b34976b6 4858 return FALSE;
ec338859 4859
87d72d41
AM
4860 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4861 if (s == NULL)
4862 s = sec;
4863
6edfbbad 4864 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
4865 head = (struct ppc_dyn_relocs **) vpp;
4866 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
4867 p = *head;
4868 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
4869 p = p->next;
4870 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
4871 {
4872 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4873 if (p == NULL)
4874 return FALSE;
4875 p->next = *head;
4876 *head = p;
4877 p->sec = sec;
4878 p->ifunc = is_ifunc;
4879 p->count = 0;
4880 }
4881 p->count += 1;
ec338859 4882 }
65f38f15 4883 }
5bd4f169 4884 break;
65f38f15
AM
4885
4886 default:
96e0dda4 4887 break;
5bd4f169
AM
4888 }
4889 }
4890
b34976b6 4891 return TRUE;
5bd4f169
AM
4892}
4893
ee67d69a
AM
4894/* Merge backend specific data from an object file to the output
4895 object file when linking. */
4896
4897static bfd_boolean
50e03d47 4898ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 4899{
50e03d47 4900 bfd *obfd = info->output_bfd;
ee67d69a
AM
4901 unsigned long iflags, oflags;
4902
4903 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
4904 return TRUE;
4905
4906 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
4907 return TRUE;
4908
50e03d47 4909 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
4910 return FALSE;
4911
4912 iflags = elf_elfheader (ibfd)->e_flags;
4913 oflags = elf_elfheader (obfd)->e_flags;
4914
f6c7c3e8 4915 if (iflags & ~EF_PPC64_ABI)
ee67d69a 4916 {
4eca0228 4917 _bfd_error_handler
695344c0 4918 /* xgettext:c-format */
871b3ab2 4919 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
4920 bfd_set_error (bfd_error_bad_value);
4921 return FALSE;
4922 }
f6c7c3e8 4923 else if (iflags != oflags && iflags != 0)
ee67d69a 4924 {
4eca0228 4925 _bfd_error_handler
695344c0 4926 /* xgettext:c-format */
871b3ab2 4927 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
4928 ibfd, iflags, oflags);
4929 bfd_set_error (bfd_error_bad_value);
4930 return FALSE;
4931 }
4932
4a91d0ba
AM
4933 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
4934 return FALSE;
005d79fd 4935
ee67d69a 4936 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 4937 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
4938}
4939
4940static bfd_boolean
4941ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
4942{
4943 /* Print normal ELF private data. */
4944 _bfd_elf_print_private_bfd_data (abfd, ptr);
4945
4946 if (elf_elfheader (abfd)->e_flags != 0)
4947 {
4948 FILE *file = ptr;
4949
ee67d69a
AM
4950 fprintf (file, _("private flags = 0x%lx:"),
4951 elf_elfheader (abfd)->e_flags);
4952
4953 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
4954 fprintf (file, _(" [abiv%ld]"),
4955 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
4956 fputc ('\n', file);
4957 }
4958
4959 return TRUE;
4960}
4961
8387904d 4962/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
4963 of the code entry point, and its section, which must be in the same
4964 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
4965
4966static bfd_vma
4967opd_entry_value (asection *opd_sec,
4968 bfd_vma offset,
4969 asection **code_sec,
aef36ac1
AM
4970 bfd_vma *code_off,
4971 bfd_boolean in_code_sec)
8387904d
AM
4972{
4973 bfd *opd_bfd = opd_sec->owner;
8860955f 4974 Elf_Internal_Rela *relocs;
8387904d 4975 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 4976 bfd_vma val;
8387904d 4977
9f296da3
AM
4978 /* No relocs implies we are linking a --just-symbols object, or looking
4979 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
4980 if (opd_sec->reloc_count == 0)
4981 {
729eabd5 4982 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 4983
729eabd5
AM
4984 if (contents == NULL)
4985 {
4986 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
4987 return (bfd_vma) -1;
4988 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
4989 }
ee1e4ede 4990
dbb3fbbb 4991 /* PR 17512: file: 64b9dfbb. */
451dfd38 4992 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
4993 return (bfd_vma) -1;
4994
729eabd5 4995 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
4996 if (code_sec != NULL)
4997 {
4998 asection *sec, *likely = NULL;
ee1e4ede 4999
aef36ac1 5000 if (in_code_sec)
4b85d634 5001 {
aef36ac1
AM
5002 sec = *code_sec;
5003 if (sec->vma <= val
5004 && val < sec->vma + sec->size)
5005 likely = sec;
5006 else
5007 val = -1;
5008 }
5009 else
5010 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5011 if (sec->vma <= val
5012 && (sec->flags & SEC_LOAD) != 0
5013 && (sec->flags & SEC_ALLOC) != 0)
5014 likely = sec;
5015 if (likely != NULL)
5016 {
5017 *code_sec = likely;
5018 if (code_off != NULL)
5019 *code_off = val - likely->vma;
4b85d634
AM
5020 }
5021 }
aef36ac1 5022 return val;
4b85d634
AM
5023 }
5024
0c8d6e5c 5025 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5026
729eabd5 5027 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5028 if (relocs == NULL)
5029 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
5030 /* PR 17512: file: df8e1fd6. */
5031 if (relocs == NULL)
5032 return (bfd_vma) -1;
645ea6a9 5033
8387904d 5034 /* Go find the opd reloc at the sym address. */
8860955f 5035 lo = relocs;
8387904d 5036 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5037 val = (bfd_vma) -1;
8387904d
AM
5038 while (lo < hi)
5039 {
5040 look = lo + (hi - lo) / 2;
5041 if (look->r_offset < offset)
5042 lo = look + 1;
5043 else if (look->r_offset > offset)
5044 hi = look;
5045 else
5046 {
0ffa91dd
NC
5047 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5048
8387904d
AM
5049 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5050 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5051 {
5052 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5053 asection *sec = NULL;
8387904d 5054
b53dfeb2
AM
5055 if (symndx >= symtab_hdr->sh_info
5056 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5057 {
5058 struct elf_link_hash_entry **sym_hashes;
5059 struct elf_link_hash_entry *rh;
5060
5061 sym_hashes = elf_sym_hashes (opd_bfd);
5062 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5063 if (rh != NULL)
5064 {
5065 rh = elf_follow_link (rh);
bb854a36
AM
5066 if (rh->root.type != bfd_link_hash_defined
5067 && rh->root.type != bfd_link_hash_defweak)
5068 break;
5069 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5070 {
bb854a36
AM
5071 val = rh->root.u.def.value;
5072 sec = rh->root.u.def.section;
b53dfeb2
AM
5073 }
5074 }
5075 }
5076
5077 if (sec == NULL)
5078 {
5079 Elf_Internal_Sym *sym;
5080
5081 if (symndx < symtab_hdr->sh_info)
5082 {
5083 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5084 if (sym == NULL)
5085 {
5086 size_t symcnt = symtab_hdr->sh_info;
5087 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5088 symcnt, 0,
5089 NULL, NULL, NULL);
5090 if (sym == NULL)
5091 break;
5092 symtab_hdr->contents = (bfd_byte *) sym;
5093 }
5094 sym += symndx;
128205bb
AM
5095 }
5096 else
5097 {
b53dfeb2
AM
5098 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5099 1, symndx,
5100 NULL, NULL, NULL);
128205bb
AM
5101 if (sym == NULL)
5102 break;
128205bb 5103 }
b53dfeb2
AM
5104 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5105 if (sec == NULL)
5106 break;
5107 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5108 val = sym->st_value;
8387904d 5109 }
b53dfeb2 5110
8387904d
AM
5111 val += look->r_addend;
5112 if (code_off != NULL)
5113 *code_off = val;
5114 if (code_sec != NULL)
aef36ac1
AM
5115 {
5116 if (in_code_sec && *code_sec != sec)
5117 return -1;
5118 else
5119 *code_sec = sec;
5120 }
b53dfeb2 5121 if (sec->output_section != NULL)
8387904d 5122 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5123 }
5124 break;
5125 }
5126 }
645ea6a9 5127
645ea6a9 5128 return val;
8387904d
AM
5129}
5130
aef36ac1
AM
5131/* If the ELF symbol SYM might be a function in SEC, return the
5132 function size and set *CODE_OFF to the function's entry point,
5133 otherwise return zero. */
9f296da3 5134
aef36ac1
AM
5135static bfd_size_type
5136ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5137 bfd_vma *code_off)
9f296da3 5138{
aef36ac1
AM
5139 bfd_size_type size;
5140
5141 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5142 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5143 return 0;
5144
5145 size = 0;
5146 if (!(sym->flags & BSF_SYNTHETIC))
5147 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5148
5149 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5150 {
b07bca4e
AM
5151 struct _opd_sec_data *opd = get_opd_info (sym->section);
5152 bfd_vma symval = sym->value;
5153
5154 if (opd != NULL
5155 && opd->adjust != NULL
5156 && elf_section_data (sym->section)->relocs != NULL)
5157 {
5158 /* opd_entry_value will use cached relocs that have been
5159 adjusted, but with raw symbols. That means both local
5160 and global symbols need adjusting. */
5161 long adjust = opd->adjust[OPD_NDX (symval)];
5162 if (adjust == -1)
5163 return 0;
5164 symval += adjust;
5165 }
5166
5167 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
5168 &sec, code_off, TRUE) == (bfd_vma) -1)
5169 return 0;
5170 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5171 symbol. This size has nothing to do with the code size of the
5172 function, which is what we're supposed to return, but the
5173 code size isn't available without looking up the dot-sym.
5174 However, doing that would be a waste of time particularly
5175 since elf_find_function will look at the dot-sym anyway.
5176 Now, elf_find_function will keep the largest size of any
5177 function sym found at the code address of interest, so return
5178 1 here to avoid it incorrectly caching a larger function size
5179 for a small function. This does mean we return the wrong
5180 size for a new-ABI function of size 24, but all that does is
5181 disable caching for such functions. */
5182 if (size == 24)
5183 size = 1;
9f296da3 5184 }
aef36ac1
AM
5185 else
5186 {
5187 if (sym->section != sec)
5188 return 0;
5189 *code_off = sym->value;
5190 }
5191 if (size == 0)
5192 size = 1;
5193 return size;
9f296da3
AM
5194}
5195
f378ab09
AM
5196/* Return true if symbol is a strong function defined in an ELFv2
5197 object with st_other localentry bits of zero, ie. its local entry
5198 point coincides with its global entry point. */
5199
5200static bfd_boolean
5201is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5202{
5203 return (h != NULL
5204 && h->type == STT_FUNC
5205 && h->root.type == bfd_link_hash_defined
5206 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 5207 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
5208 && is_ppc64_elf (h->root.u.def.section->owner)
5209 && abiversion (h->root.u.def.section->owner) >= 2);
5210}
5211
854b41e7
AM
5212/* Return true if symbol is defined in a regular object file. */
5213
5214static bfd_boolean
5215is_static_defined (struct elf_link_hash_entry *h)
5216{
5217 return ((h->root.type == bfd_link_hash_defined
5218 || h->root.type == bfd_link_hash_defweak)
5219 && h->root.u.def.section != NULL
5220 && h->root.u.def.section->output_section != NULL);
5221}
5222
b31867b6
AM
5223/* If FDH is a function descriptor symbol, return the associated code
5224 entry symbol if it is defined. Return NULL otherwise. */
5225
5226static struct ppc_link_hash_entry *
5227defined_code_entry (struct ppc_link_hash_entry *fdh)
5228{
5229 if (fdh->is_func_descriptor)
5230 {
5231 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5232 if (fh->elf.root.type == bfd_link_hash_defined
5233 || fh->elf.root.type == bfd_link_hash_defweak)
5234 return fh;
5235 }
5236 return NULL;
5237}
5238
5239/* If FH is a function code entry symbol, return the associated
5240 function descriptor symbol if it is defined. Return NULL otherwise. */
5241
5242static struct ppc_link_hash_entry *
5243defined_func_desc (struct ppc_link_hash_entry *fh)
5244{
5245 if (fh->oh != NULL
5246 && fh->oh->is_func_descriptor)
5247 {
5248 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5249 if (fdh->elf.root.type == bfd_link_hash_defined
5250 || fdh->elf.root.type == bfd_link_hash_defweak)
5251 return fdh;
5252 }
5253 return NULL;
5254}
5255
8c5b4e52
AM
5256static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5257
5258/* Garbage collect sections, after first dealing with dot-symbols. */
5259
5260static bfd_boolean
5261ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5262{
5263 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5264
5265 if (htab != NULL && htab->need_func_desc_adj)
5266 {
5267 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5268 htab->need_func_desc_adj = 0;
5269 }
5270 return bfd_elf_gc_sections (abfd, info);
5271}
5272
74f0fb50
AM
5273/* Mark all our entry sym sections, both opd and code section. */
5274
5275static void
5276ppc64_elf_gc_keep (struct bfd_link_info *info)
5277{
5278 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5279 struct bfd_sym_chain *sym;
5280
4dfe6ac6
NC
5281 if (htab == NULL)
5282 return;
5283
74f0fb50
AM
5284 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5285 {
b31867b6 5286 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5287 asection *sec;
5288
5289 eh = (struct ppc_link_hash_entry *)
b31867b6 5290 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5291 if (eh == NULL)
5292 continue;
5293 if (eh->elf.root.type != bfd_link_hash_defined
5294 && eh->elf.root.type != bfd_link_hash_defweak)
5295 continue;
5296
b31867b6
AM
5297 fh = defined_code_entry (eh);
5298 if (fh != NULL)
74f0fb50 5299 {
b31867b6 5300 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5301 sec->flags |= SEC_KEEP;
5302 }
5303 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5304 && opd_entry_value (eh->elf.root.u.def.section,
5305 eh->elf.root.u.def.value,
aef36ac1 5306 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
5307 sec->flags |= SEC_KEEP;
5308
5309 sec = eh->elf.root.u.def.section;
5310 sec->flags |= SEC_KEEP;
5311 }
5312}
5313
64d03ab5
AM
5314/* Mark sections containing dynamically referenced symbols. When
5315 building shared libraries, we must assume that any visible symbol is
5316 referenced. */
5317
5318static bfd_boolean
5319ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5320{
5321 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5322 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5323 struct ppc_link_hash_entry *fdh;
b407645f 5324 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5325
64d03ab5 5326 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5327 fdh = defined_func_desc (eh);
5328 if (fdh != NULL)
5329 eh = fdh;
64d03ab5
AM
5330
5331 if ((eh->elf.root.type == bfd_link_hash_defined
5332 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 5333 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5334 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5335 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5336 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5337 && (!bfd_link_executable (info)
e278ae05 5338 || info->gc_keep_exported
b407645f
AM
5339 || info->export_dynamic
5340 || (eh->elf.dynamic
5341 && d != NULL
2cdcc330
AM
5342 && (*d->match) (&d->head, NULL,
5343 eh->elf.root.root.string)))
e278ae05 5344 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5345 || !bfd_hide_sym_by_version (info->version_info,
5346 eh->elf.root.root.string)))))
64d03ab5
AM
5347 {
5348 asection *code_sec;
b31867b6 5349 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5350
5351 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5352
5353 /* Function descriptor syms cause the associated
5354 function code sym section to be marked. */
b31867b6
AM
5355 fh = defined_code_entry (eh);
5356 if (fh != NULL)
5357 {
5358 code_sec = fh->elf.root.u.def.section;
5359 code_sec->flags |= SEC_KEEP;
5360 }
64d03ab5
AM
5361 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5362 && opd_entry_value (eh->elf.root.u.def.section,
5363 eh->elf.root.u.def.value,
aef36ac1 5364 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
5365 code_sec->flags |= SEC_KEEP;
5366 }
5367
5368 return TRUE;
5369}
5370
5bd4f169
AM
5371/* Return the section that should be marked against GC for a given
5372 relocation. */
5373
5374static asection *
4ce794b7 5375ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5376 struct bfd_link_info *info,
4ce794b7
AM
5377 Elf_Internal_Rela *rel,
5378 struct elf_link_hash_entry *h,
5379 Elf_Internal_Sym *sym)
5bd4f169 5380{
ccfa59ea
AM
5381 asection *rsec;
5382
ccfa59ea
AM
5383 /* Syms return NULL if we're marking .opd, so we avoid marking all
5384 function sections, as all functions are referenced in .opd. */
5385 rsec = NULL;
5386 if (get_opd_info (sec) != NULL)
5387 return rsec;
1e2f5b6e 5388
5bd4f169
AM
5389 if (h != NULL)
5390 {
04c9666a 5391 enum elf_ppc64_reloc_type r_type;
b31867b6 5392 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5393
4ce794b7 5394 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5395 switch (r_type)
5bd4f169
AM
5396 {
5397 case R_PPC64_GNU_VTINHERIT:
5398 case R_PPC64_GNU_VTENTRY:
5399 break;
5400
5401 default:
5402 switch (h->root.type)
5403 {
5404 case bfd_link_hash_defined:
5405 case bfd_link_hash_defweak:
ccfa59ea 5406 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5407 fdh = defined_func_desc (eh);
5408 if (fdh != NULL)
8c5b4e52
AM
5409 {
5410 /* -mcall-aixdesc code references the dot-symbol on
5411 a call reloc. Mark the function descriptor too
5412 against garbage collection. */
5413 fdh->elf.mark = 1;
60d67dc8
AM
5414 if (fdh->elf.is_weakalias)
5415 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
5416 eh = fdh;
5417 }
1e2f5b6e
AM
5418
5419 /* Function descriptor syms cause the associated
5420 function code sym section to be marked. */
b31867b6
AM
5421 fh = defined_code_entry (eh);
5422 if (fh != NULL)
ccfa59ea
AM
5423 {
5424 /* They also mark their opd section. */
74f0fb50 5425 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5426
b31867b6 5427 rsec = fh->elf.root.u.def.section;
ccfa59ea 5428 }
8387904d
AM
5429 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5430 && opd_entry_value (eh->elf.root.u.def.section,
5431 eh->elf.root.u.def.value,
aef36ac1 5432 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 5433 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5434 else
1e2f5b6e
AM
5435 rsec = h->root.u.def.section;
5436 break;
5bd4f169
AM
5437
5438 case bfd_link_hash_common:
1e2f5b6e
AM
5439 rsec = h->root.u.c.p->section;
5440 break;
5bd4f169
AM
5441
5442 default:
fb34365b 5443 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5444 }
5445 }
5446 }
5447 else
5448 {
74f0fb50 5449 struct _opd_sec_data *opd;
1e2f5b6e
AM
5450
5451 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5452 opd = get_opd_info (rsec);
5453 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5454 {
74f0fb50 5455 rsec->gc_mark = 1;
ccfa59ea 5456
51aecdc5 5457 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 5458 }
5bd4f169
AM
5459 }
5460
1e2f5b6e 5461 return rsec;
5bd4f169
AM
5462}
5463
deb0e272
AM
5464/* The maximum size of .sfpr. */
5465#define SFPR_MAX (218*4)
5466
5467struct sfpr_def_parms
5468{
699733f6
AM
5469 const char name[12];
5470 unsigned char lo, hi;
2cdcc330
AM
5471 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5472 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
5473};
5474
a4b6fadd
AM
5475/* Auto-generate _save*, _rest* functions in .sfpr.
5476 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5477 instead. */
deb0e272 5478
4dfe6ac6 5479static bfd_boolean
a4b6fadd
AM
5480sfpr_define (struct bfd_link_info *info,
5481 const struct sfpr_def_parms *parm,
5482 asection *stub_sec)
deb0e272
AM
5483{
5484 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5485 unsigned int i;
5486 size_t len = strlen (parm->name);
5487 bfd_boolean writing = FALSE;
699733f6 5488 char sym[16];
deb0e272 5489
4dfe6ac6
NC
5490 if (htab == NULL)
5491 return FALSE;
5492
deb0e272
AM
5493 memcpy (sym, parm->name, len);
5494 sym[len + 2] = 0;
5495
5496 for (i = parm->lo; i <= parm->hi; i++)
5497 {
a4b6fadd 5498 struct ppc_link_hash_entry *h;
deb0e272
AM
5499
5500 sym[len + 0] = i / 10 + '0';
5501 sym[len + 1] = i % 10 + '0';
a4b6fadd 5502 h = (struct ppc_link_hash_entry *)
b32547cd 5503 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 5504 if (stub_sec != NULL)
deb0e272 5505 {
a4b6fadd
AM
5506 if (h != NULL
5507 && h->elf.root.type == bfd_link_hash_defined
5508 && h->elf.root.u.def.section == htab->sfpr)
5509 {
5510 struct elf_link_hash_entry *s;
5511 char buf[32];
5512 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5513 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5514 if (s == NULL)
5515 return FALSE;
5516 if (s->root.type == bfd_link_hash_new
5517 || (s->root.type = bfd_link_hash_defined
5518 && s->root.u.def.section == stub_sec))
5519 {
5520 s->root.type = bfd_link_hash_defined;
5521 s->root.u.def.section = stub_sec;
7dda8d3c 5522 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
5523 + h->elf.root.u.def.value);
5524 s->ref_regular = 1;
5525 s->def_regular = 1;
5526 s->ref_regular_nonweak = 1;
5527 s->forced_local = 1;
5528 s->non_elf = 0;
5529 s->root.linker_def = 1;
5530 }
5531 }
5532 continue;
5533 }
5534 if (h != NULL)
5535 {
5536 h->save_res = 1;
5537 if (!h->elf.def_regular)
deb0e272 5538 {
a4b6fadd
AM
5539 h->elf.root.type = bfd_link_hash_defined;
5540 h->elf.root.u.def.section = htab->sfpr;
5541 h->elf.root.u.def.value = htab->sfpr->size;
5542 h->elf.type = STT_FUNC;
5543 h->elf.def_regular = 1;
b32547cd 5544 h->elf.non_elf = 0;
a4b6fadd
AM
5545 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5546 writing = TRUE;
deb0e272 5547 if (htab->sfpr->contents == NULL)
a4b6fadd 5548 {
2cdcc330
AM
5549 htab->sfpr->contents
5550 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd
AM
5551 if (htab->sfpr->contents == NULL)
5552 return FALSE;
5553 }
deb0e272
AM
5554 }
5555 }
5556 if (writing)
5557 {
5558 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5559 if (i != parm->hi)
5560 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5561 else
5562 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5563 htab->sfpr->size = p - htab->sfpr->contents;
5564 }
5565 }
5566
5567 return TRUE;
5568}
5569
5570static bfd_byte *
5571savegpr0 (bfd *abfd, bfd_byte *p, int r)
5572{
5573 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5574 return p + 4;
5575}
5576
5577static bfd_byte *
5578savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5579{
5580 p = savegpr0 (abfd, p, r);
a078d95a 5581 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5582 p = p + 4;
5583 bfd_put_32 (abfd, BLR, p);
5584 return p + 4;
5585}
5586
5587static bfd_byte *
5588restgpr0 (bfd *abfd, bfd_byte *p, int r)
5589{
5590 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5591 return p + 4;
5592}
5593
5594static bfd_byte *
5595restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5596{
a078d95a 5597 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5598 p = p + 4;
5599 p = restgpr0 (abfd, p, r);
5600 bfd_put_32 (abfd, MTLR_R0, p);
5601 p = p + 4;
5602 if (r == 29)
5603 {
5604 p = restgpr0 (abfd, p, 30);
5605 p = restgpr0 (abfd, p, 31);
5606 }
5607 bfd_put_32 (abfd, BLR, p);
5608 return p + 4;
5609}
5610
5611static bfd_byte *
5612savegpr1 (bfd *abfd, bfd_byte *p, int r)
5613{
5614 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5615 return p + 4;
5616}
5617
5618static bfd_byte *
5619savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5620{
5621 p = savegpr1 (abfd, p, r);
5622 bfd_put_32 (abfd, BLR, p);
5623 return p + 4;
5624}
5625
5626static bfd_byte *
5627restgpr1 (bfd *abfd, bfd_byte *p, int r)
5628{
5629 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5630 return p + 4;
5631}
5632
5633static bfd_byte *
5634restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5635{
5636 p = restgpr1 (abfd, p, r);
5637 bfd_put_32 (abfd, BLR, p);
5638 return p + 4;
5639}
5640
5641static bfd_byte *
5642savefpr (bfd *abfd, bfd_byte *p, int r)
5643{
5644 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5645 return p + 4;
5646}
5647
5648static bfd_byte *
5649savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5650{
5651 p = savefpr (abfd, p, r);
a078d95a 5652 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5653 p = p + 4;
5654 bfd_put_32 (abfd, BLR, p);
5655 return p + 4;
5656}
5657
5658static bfd_byte *
5659restfpr (bfd *abfd, bfd_byte *p, int r)
5660{
5661 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5662 return p + 4;
5663}
5664
5665static bfd_byte *
5666restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5667{
a078d95a 5668 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5669 p = p + 4;
5670 p = restfpr (abfd, p, r);
5671 bfd_put_32 (abfd, MTLR_R0, p);
5672 p = p + 4;
5673 if (r == 29)
5674 {
5675 p = restfpr (abfd, p, 30);
5676 p = restfpr (abfd, p, 31);
5677 }
5678 bfd_put_32 (abfd, BLR, p);
5679 return p + 4;
5680}
5681
5682static bfd_byte *
5683savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5684{
5685 p = savefpr (abfd, p, r);
5686 bfd_put_32 (abfd, BLR, p);
5687 return p + 4;
5688}
5689
5690static bfd_byte *
5691restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5692{
5693 p = restfpr (abfd, p, r);
5694 bfd_put_32 (abfd, BLR, p);
5695 return p + 4;
5696}
5697
5698static bfd_byte *
5699savevr (bfd *abfd, bfd_byte *p, int r)
5700{
5701 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5702 p = p + 4;
5703 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5704 return p + 4;
5705}
5706
5707static bfd_byte *
5708savevr_tail (bfd *abfd, bfd_byte *p, int r)
5709{
5710 p = savevr (abfd, p, r);
5711 bfd_put_32 (abfd, BLR, p);
5712 return p + 4;
5713}
5714
5715static bfd_byte *
5716restvr (bfd *abfd, bfd_byte *p, int r)
5717{
5718 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5719 p = p + 4;
5720 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5721 return p + 4;
5722}
5723
5724static bfd_byte *
5725restvr_tail (bfd *abfd, bfd_byte *p, int r)
5726{
5727 p = restvr (abfd, p, r);
5728 bfd_put_32 (abfd, BLR, p);
5729 return p + 4;
5730}
5731
e86ce104
AM
5732/* Called via elf_link_hash_traverse to transfer dynamic linking
5733 information on function code symbol entries to their corresponding
5734 function descriptor symbol entries. */
deb0e272 5735
b34976b6 5736static bfd_boolean
4ce794b7 5737func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 5738{
e86ce104 5739 struct bfd_link_info *info;
65f38f15 5740 struct ppc_link_hash_table *htab;
50bc7936
AM
5741 struct ppc_link_hash_entry *fh;
5742 struct ppc_link_hash_entry *fdh;
5743 bfd_boolean force_local;
5bd4f169 5744
50bc7936
AM
5745 fh = (struct ppc_link_hash_entry *) h;
5746 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 5747 return TRUE;
e86ce104 5748
8c5b4e52
AM
5749 if (!fh->is_func)
5750 return TRUE;
5751
5752 if (fh->elf.root.root.string[0] != '.'
5753 || fh->elf.root.root.string[1] == '\0')
5754 return TRUE;
5755
4ce794b7 5756 info = inf;
65f38f15 5757 htab = ppc_hash_table (info);
4dfe6ac6
NC
5758 if (htab == NULL)
5759 return FALSE;
5bd4f169 5760
8c5b4e52
AM
5761 /* Find the corresponding function descriptor symbol. */
5762 fdh = lookup_fdh (fh, htab);
5763
c09bdfe5
AM
5764 /* Resolve undefined references to dot-symbols as the value
5765 in the function descriptor, if we have one in a regular object.
5766 This is to satisfy cases like ".quad .foo". Calls to functions
5767 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
5768 if ((fh->elf.root.type == bfd_link_hash_undefined
5769 || fh->elf.root.type == bfd_link_hash_undefweak)
5770 && (fdh->elf.root.type == bfd_link_hash_defined
5771 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
5772 && get_opd_info (fdh->elf.root.u.def.section) != NULL
5773 && opd_entry_value (fdh->elf.root.u.def.section,
5774 fdh->elf.root.u.def.value,
c09bdfe5 5775 &fh->elf.root.u.def.section,
aef36ac1 5776 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 5777 {
b31867b6 5778 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 5779 fh->elf.forced_local = 1;
b31867b6
AM
5780 fh->elf.def_regular = fdh->elf.def_regular;
5781 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
5782 }
5783
8c5b4e52
AM
5784 if (!fh->elf.dynamic)
5785 {
5786 struct plt_entry *ent;
5bd4f169 5787
8c5b4e52
AM
5788 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5789 if (ent->plt.refcount > 0)
5790 break;
5791 if (ent == NULL)
5792 return TRUE;
5793 }
5bd4f169 5794
8c5b4e52 5795 /* Create a descriptor as undefined if necessary. */
50bc7936 5796 if (fdh == NULL
0e1862bb 5797 && !bfd_link_executable (info)
50bc7936
AM
5798 && (fh->elf.root.type == bfd_link_hash_undefined
5799 || fh->elf.root.type == bfd_link_hash_undefweak))
5800 {
908b32fc 5801 fdh = make_fdh (info, fh);
bb700d78
AM
5802 if (fdh == NULL)
5803 return FALSE;
50bc7936 5804 }
648cca2c 5805
8c5b4e52 5806 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
5807 if (fdh != NULL
5808 && fdh->fake
8c5b4e52
AM
5809 && (fh->elf.root.type == bfd_link_hash_defined
5810 || fh->elf.root.type == bfd_link_hash_defweak))
5811 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 5812
8c5b4e52
AM
5813 /* Transfer dynamic linking information to the function descriptor. */
5814 if (fdh != NULL)
5815 {
f5385ebf
AM
5816 fdh->elf.ref_regular |= fh->elf.ref_regular;
5817 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5818 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5819 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
5820 fdh->elf.dynamic |= fh->elf.dynamic;
5821 fdh->elf.needs_plt |= (fh->elf.needs_plt
5822 || fh->elf.type == STT_FUNC
5823 || fh->elf.type == STT_GNU_IFUNC);
5824 move_plt_plist (fh, fdh);
5825
5826 if (!fdh->elf.forced_local
5827 && fh->elf.dynindx != -1)
5828 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5829 return FALSE;
e86ce104
AM
5830 }
5831
50bc7936
AM
5832 /* Now that the info is on the function descriptor, clear the
5833 function code sym info. Any function code syms for which we
5834 don't have a definition in a regular file, we force local.
5835 This prevents a shared library from exporting syms that have
5836 been imported from another library. Function code syms that
5837 are really in the library we must leave global to prevent the
5838 linker dragging in a definition from a static library. */
93f3fa99
AM
5839 force_local = (!fh->elf.def_regular
5840 || fdh == NULL
5841 || !fdh->elf.def_regular
5842 || fdh->elf.forced_local);
50bc7936
AM
5843 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5844
b34976b6 5845 return TRUE;
e86ce104 5846}
40b8271b 5847
a4b6fadd
AM
5848static const struct sfpr_def_parms save_res_funcs[] =
5849 {
5850 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5851 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5852 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5853 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5854 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5855 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5856 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5857 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5858 { "._savef", 14, 31, savefpr, savefpr1_tail },
5859 { "._restf", 14, 31, restfpr, restfpr1_tail },
5860 { "_savevr_", 20, 31, savevr, savevr_tail },
5861 { "_restvr_", 20, 31, restvr, restvr_tail }
5862 };
5863
e86ce104 5864/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
5865 this hook to a) provide some gcc support functions, and b) transfer
5866 dynamic linking information gathered so far on function code symbol
5867 entries, to their corresponding function descriptor symbol entries. */
deb0e272 5868
b34976b6 5869static bfd_boolean
4ce794b7
AM
5870ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5871 struct bfd_link_info *info)
e86ce104
AM
5872{
5873 struct ppc_link_hash_table *htab;
5874
5875 htab = ppc_hash_table (info);
4dfe6ac6
NC
5876 if (htab == NULL)
5877 return FALSE;
5878
b32547cd
AM
5879 /* Provide any missing _save* and _rest* functions. */
5880 if (htab->sfpr != NULL)
5881 {
5882 unsigned int i;
5883
5884 htab->sfpr->size = 0;
5885 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
5886 if (!sfpr_define (info, &save_res_funcs[i], NULL))
5887 return FALSE;
5888 if (htab->sfpr->size == 0)
5889 htab->sfpr->flags |= SEC_EXCLUDE;
5890 }
5891
5892 if (bfd_link_relocatable (info))
5893 return TRUE;
5894
5895 if (htab->elf.hgot != NULL)
dba6fa9b
AM
5896 {
5897 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
5898 /* Make .TOC. defined so as to prevent it being made dynamic.
5899 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
5900 if (!htab->elf.hgot->def_regular
5901 || htab->elf.hgot->root.type != bfd_link_hash_defined)
5902 {
5903 htab->elf.hgot->root.type = bfd_link_hash_defined;
5904 htab->elf.hgot->root.u.def.value = 0;
5905 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
5906 htab->elf.hgot->def_regular = 1;
5907 htab->elf.hgot->root.linker_def = 1;
5908 }
dba6fa9b 5909 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
5910 htab->elf.hgot->other
5911 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 5912 }
c66bb0ee 5913
8c5b4e52
AM
5914 if (htab->need_func_desc_adj)
5915 {
5916 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5917 htab->need_func_desc_adj = 0;
5918 }
805fc799 5919
b34976b6 5920 return TRUE;
e86ce104
AM
5921}
5922
98bbb1b8 5923/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 5924
98bbb1b8 5925static asection *
a345bc8d
AM
5926readonly_dynrelocs (struct elf_link_hash_entry *h)
5927{
5928 struct ppc_link_hash_entry *eh;
5929 struct elf_dyn_relocs *p;
5930
5931 eh = (struct ppc_link_hash_entry *) h;
5932 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5933 {
5934 asection *s = p->sec->output_section;
5935
5936 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 5937 return p->sec;
a345bc8d 5938 }
98bbb1b8 5939 return NULL;
a345bc8d
AM
5940}
5941
d311bc8b 5942/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
5943 aliases, that apply to read-only sections. Cannot be used after
5944 size_dynamic_sections. */
d311bc8b
AM
5945
5946static bfd_boolean
5947alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
5948{
5949 struct ppc_link_hash_entry *eh;
5950
5951 eh = (struct ppc_link_hash_entry *) h;
5952 do
5953 {
5954 if (readonly_dynrelocs (&eh->elf))
5955 return TRUE;
ab2477e1 5956 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
2cdcc330
AM
5957 }
5958 while (eh != NULL && &eh->elf != h);
d311bc8b
AM
5959
5960 return FALSE;
5961}
8a2058b5 5962
8a9e8e72
AM
5963/* Return whether EH has pc-relative dynamic relocs. */
5964
5965static bfd_boolean
5966pc_dynrelocs (struct ppc_link_hash_entry *eh)
5967{
5968 struct elf_dyn_relocs *p;
5969
5970 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5971 if (p->pc_count != 0)
5972 return TRUE;
5973 return FALSE;
5974}
5975
8a2058b5
AM
5976/* Return true if a global entry stub will be created for H. Valid
5977 for ELFv2 before plt entries have been allocated. */
5978
5979static bfd_boolean
5980global_entry_stub (struct elf_link_hash_entry *h)
5981{
5982 struct plt_entry *pent;
5983
5984 if (!h->pointer_equality_needed
5985 || h->def_regular)
5986 return FALSE;
5987
5988 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
5989 if (pent->plt.refcount > 0
5990 && pent->addend == 0)
5991 return TRUE;
5992
5993 return FALSE;
5994}
5995
e86ce104
AM
5996/* Adjust a symbol defined by a dynamic object and referenced by a
5997 regular object. The current definition is in some section of the
5998 dynamic object, but we're not including those sections. We have to
5999 change the definition to something the rest of the link can
6000 understand. */
6001
b34976b6 6002static bfd_boolean
4ce794b7
AM
6003ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6004 struct elf_link_hash_entry *h)
e86ce104
AM
6005{
6006 struct ppc_link_hash_table *htab;
5474d94f 6007 asection *s, *srel;
e86ce104
AM
6008
6009 htab = ppc_hash_table (info);
4dfe6ac6
NC
6010 if (htab == NULL)
6011 return FALSE;
e86ce104
AM
6012
6013 /* Deal with function syms. */
6014 if (h->type == STT_FUNC
e054468f 6015 || h->type == STT_GNU_IFUNC
f5385ebf 6016 || h->needs_plt)
e86ce104 6017 {
529fe20e
AM
6018 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6019 || SYMBOL_CALLS_LOCAL (info, h)
6020 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6021 /* Discard dyn_relocs when non-pic if we've decided that a
6022 function symbol is local and not an ifunc. We keep dynamic
6023 relocs for ifuncs when local rather than always emitting a
6024 plt call stub for them and defining the symbol on the call
6025 stub. We can't do that for ELFv1 anyway (a function symbol
6026 is defined on a descriptor, not code) and it can be faster at
6027 run-time due to not needing to bounce through a stub. The
6028 dyn_relocs for ifuncs will be applied even in a static
6029 executable. */
6030 if (!bfd_link_pic (info)
6031 && h->type != STT_GNU_IFUNC
6032 && local)
6033 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6034
e86ce104
AM
6035 /* Clear procedure linkage table information for any symbol that
6036 won't need a .plt entry. */
411e1bfb
AM
6037 struct plt_entry *ent;
6038 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6039 if (ent->plt.refcount > 0)
6040 break;
8387904d 6041 if (ent == NULL
2d7ad24e
AM
6042 || (h->type != STT_GNU_IFUNC
6043 && local
3e04d765
AM
6044 && (htab->can_convert_all_inline_plt
6045 || (((struct ppc_link_hash_entry *) h)->tls_mask
6046 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6047 {
411e1bfb 6048 h->plt.plist = NULL;
f5385ebf 6049 h->needs_plt = 0;
d1eca1e4 6050 h->pointer_equality_needed = 0;
40b8271b 6051 }
8a2058b5 6052 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6053 {
d1eca1e4
AM
6054 /* Taking a function's address in a read/write section
6055 doesn't require us to define the function symbol in the
6056 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6057 be used instead. The reason we prefer a few more dynamic
6058 relocs is that calling via a global entry stub costs a
6059 few more instructions, and pointer_equality_needed causes
6060 extra work in ld.so when resolving these symbols. */
529fe20e 6061 if (global_entry_stub (h))
d1eca1e4 6062 {
ab2477e1 6063 if (!readonly_dynrelocs (h))
529fe20e
AM
6064 {
6065 h->pointer_equality_needed = 0;
04383fd1
AM
6066 /* If we haven't seen a branch reloc and the symbol
6067 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6068 if (!h->needs_plt)
6069 h->plt.plist = NULL;
6070 }
6071 else if (!bfd_link_pic (info))
6072 /* We are going to be defining the function symbol on the
6073 plt stub, so no dyn_relocs needed when non-pic. */
6074 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
6075 }
6076
3988aed5
AM
6077 /* ELFv2 function symbols can't have copy relocs. */
6078 return TRUE;
6079 }
6080 else if (!h->needs_plt
ab2477e1 6081 && !readonly_dynrelocs (h))
3988aed5 6082 {
04383fd1
AM
6083 /* If we haven't seen a branch reloc and the symbol isn't an
6084 ifunc then we don't need a plt entry. */
3988aed5
AM
6085 h->plt.plist = NULL;
6086 h->pointer_equality_needed = 0;
a345bc8d
AM
6087 return TRUE;
6088 }
5bd4f169 6089 }
bbd7ec4a 6090 else
411e1bfb 6091 h->plt.plist = NULL;
5bd4f169
AM
6092
6093 /* If this is a weak symbol, and there is a real definition, the
6094 processor independent code will have arranged for us to see the
6095 real definition first, and we can just use the same value. */
60d67dc8 6096 if (h->is_weakalias)
5bd4f169 6097 {
60d67dc8
AM
6098 struct elf_link_hash_entry *def = weakdef (h);
6099 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6100 h->root.u.def.section = def->root.u.def.section;
6101 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6102 if (def->root.u.def.section == htab->elf.sdynbss
6103 || def->root.u.def.section == htab->elf.sdynrelro)
6104 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 6105 return TRUE;
5bd4f169
AM
6106 }
6107
5bd4f169
AM
6108 /* If we are creating a shared library, we must presume that the
6109 only references to the symbol are via the global offset table.
6110 For such cases we need not do anything here; the relocations will
6111 be handled correctly by relocate_section. */
0e1862bb 6112 if (bfd_link_pic (info))
b34976b6 6113 return TRUE;
5bd4f169 6114
65f38f15
AM
6115 /* If there are no references to this symbol that do not use the
6116 GOT, we don't need to generate a copy reloc. */
f5385ebf 6117 if (!h->non_got_ref)
b34976b6 6118 return TRUE;
65f38f15 6119
b186458a 6120 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6121 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6122
d93d1c80
AM
6123 /* If -z nocopyreloc was given, don't generate them either. */
6124 || info->nocopyreloc
a127494f 6125
dce2246a 6126 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6127 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 6128 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 6129
d93d1c80
AM
6130 /* Protected variables do not work with .dynbss. The copy in
6131 .dynbss won't be used by the shared library with the protected
6132 definition for the variable. Text relocations are preferable
6133 to an incorrect program. */
6134 || h->protected_def)
529fe20e 6135 return TRUE;
a127494f 6136
5d35169e 6137 if (h->plt.plist != NULL)
97b639ba
AM
6138 {
6139 /* We should never get here, but unfortunately there are versions
6140 of gcc out there that improperly (for this ABI) put initialized
6141 function pointers, vtable refs and suchlike in read-only
6142 sections. Allow them to proceed, but warn that this might
6143 break at runtime. */
25f53a85 6144 info->callbacks->einfo
c1c8c1ef 6145 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6146 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6147 h->root.root.string);
6148 }
5d35169e
AM
6149
6150 /* This is a reference to a symbol defined by a dynamic object which
6151 is not a function. */
6152
5bd4f169
AM
6153 /* We must allocate the symbol in our .dynbss section, which will
6154 become part of the .bss section of the executable. There will be
6155 an entry for this symbol in the .dynsym section. The dynamic
6156 object will contain position independent code, so all references
6157 from the dynamic object to this symbol will go through the global
6158 offset table. The dynamic linker will use the .dynsym entry to
6159 determine the address it must put in the global offset table, so
6160 both the dynamic object and the regular object will refer to the
6161 same memory location for the variable. */
5474d94f
AM
6162 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6163 {
6164 s = htab->elf.sdynrelro;
6165 srel = htab->elf.sreldynrelro;
6166 }
6167 else
6168 {
6169 s = htab->elf.sdynbss;
6170 srel = htab->elf.srelbss;
6171 }
1d7e9d18 6172 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6173 {
4a7e5234
AM
6174 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6175 linker to copy the initial value out of the dynamic object
6176 and into the runtime process image. */
5474d94f 6177 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6178 h->needs_copy = 1;
5bd4f169
AM
6179 }
6180
529fe20e
AM
6181 /* We no longer want dyn_relocs. */
6182 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 6183 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6184}
6185
e86ce104
AM
6186/* If given a function descriptor symbol, hide both the function code
6187 sym and the descriptor. */
6188static void
4ce794b7
AM
6189ppc64_elf_hide_symbol (struct bfd_link_info *info,
6190 struct elf_link_hash_entry *h,
6191 bfd_boolean force_local)
e86ce104 6192{
34814b9f 6193 struct ppc_link_hash_entry *eh;
e86ce104
AM
6194 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6195
87469ba2
AM
6196 if (ppc_hash_table (info) == NULL)
6197 return;
6198
34814b9f
AM
6199 eh = (struct ppc_link_hash_entry *) h;
6200 if (eh->is_func_descriptor)
e86ce104 6201 {
34814b9f 6202 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6203
721956f4 6204 if (fh == NULL)
d1329ca3
AM
6205 {
6206 const char *p, *q;
b8ac2841 6207 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6208 char save;
6209
6210 /* We aren't supposed to use alloca in BFD because on
6211 systems which do not have alloca the version in libiberty
6212 calls xmalloc, which might cause the program to crash
6213 when it runs out of memory. This function doesn't have a
6214 return status, so there's no way to gracefully return an
6215 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6216 accessed; It's either a string in an ELF string table,
6217 or allocated in an objalloc structure. */
d1329ca3 6218
34814b9f 6219 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6220 save = *p;
6221 *(char *) p = '.';
34814b9f 6222 fh = (struct ppc_link_hash_entry *)
b8ac2841 6223 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6224 *(char *) p = save;
6225
6226 /* Unfortunately, if it so happens that the string we were
6227 looking for was allocated immediately before this string,
6228 then we overwrote the string terminator. That's the only
6229 reason the lookup should fail. */
6230 if (fh == NULL)
6231 {
34814b9f
AM
6232 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6233 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6234 --q, --p;
34814b9f
AM
6235 if (q < eh->elf.root.root.string && *p == '.')
6236 fh = (struct ppc_link_hash_entry *)
b8ac2841 6237 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6238 }
6239 if (fh != NULL)
6240 {
34814b9f
AM
6241 eh->oh = fh;
6242 fh->oh = eh;
d1329ca3
AM
6243 }
6244 }
e86ce104 6245 if (fh != NULL)
34814b9f 6246 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6247 }
6248}
6249
411e1bfb 6250static bfd_boolean
8843416a
AM
6251get_sym_h (struct elf_link_hash_entry **hp,
6252 Elf_Internal_Sym **symp,
6253 asection **symsecp,
f961d9dd 6254 unsigned char **tls_maskp,
8843416a
AM
6255 Elf_Internal_Sym **locsymsp,
6256 unsigned long r_symndx,
6257 bfd *ibfd)
411e1bfb 6258{
0ffa91dd 6259 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6260
6261 if (r_symndx >= symtab_hdr->sh_info)
6262 {
6263 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6264 struct elf_link_hash_entry *h;
6265
6266 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6267 h = elf_follow_link (h);
411e1bfb
AM
6268
6269 if (hp != NULL)
6270 *hp = h;
6271
6272 if (symp != NULL)
6273 *symp = NULL;
6274
6275 if (symsecp != NULL)
6276 {
6277 asection *symsec = NULL;
6278 if (h->root.type == bfd_link_hash_defined
6279 || h->root.type == bfd_link_hash_defweak)
6280 symsec = h->root.u.def.section;
6281 *symsecp = symsec;
6282 }
6283
e7b938ca 6284 if (tls_maskp != NULL)
411e1bfb
AM
6285 {
6286 struct ppc_link_hash_entry *eh;
6287
6288 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6289 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6290 }
6291 }
6292 else
6293 {
6294 Elf_Internal_Sym *sym;
6295 Elf_Internal_Sym *locsyms = *locsymsp;
6296
6297 if (locsyms == NULL)
6298 {
6299 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6300 if (locsyms == NULL)
6301 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6302 symtab_hdr->sh_info,
6303 0, NULL, NULL, NULL);
6304 if (locsyms == NULL)
6305 return FALSE;
6306 *locsymsp = locsyms;
6307 }
6308 sym = locsyms + r_symndx;
6309
6310 if (hp != NULL)
6311 *hp = NULL;
6312
6313 if (symp != NULL)
6314 *symp = sym;
6315
6316 if (symsecp != NULL)
cb33740c 6317 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6318
e7b938ca 6319 if (tls_maskp != NULL)
411e1bfb
AM
6320 {
6321 struct got_entry **lgot_ents;
f961d9dd 6322 unsigned char *tls_mask;
411e1bfb 6323
e7b938ca 6324 tls_mask = NULL;
411e1bfb
AM
6325 lgot_ents = elf_local_got_ents (ibfd);
6326 if (lgot_ents != NULL)
6327 {
e054468f
AM
6328 struct plt_entry **local_plt = (struct plt_entry **)
6329 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6330 unsigned char *lgot_masks = (unsigned char *)
e054468f 6331 (local_plt + symtab_hdr->sh_info);
e7b938ca 6332 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6333 }
e7b938ca 6334 *tls_maskp = tls_mask;
411e1bfb
AM
6335 }
6336 }
6337 return TRUE;
6338}
6339
e7b938ca 6340/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6341 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6342 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6343
6344static int
f961d9dd 6345get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6346 unsigned long *toc_symndx,
6347 bfd_vma *toc_addend,
0d4792f7 6348 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6349 const Elf_Internal_Rela *rel,
6350 bfd *ibfd)
411e1bfb
AM
6351{
6352 unsigned long r_symndx;
0d4792f7 6353 int next_r;
411e1bfb
AM
6354 struct elf_link_hash_entry *h;
6355 Elf_Internal_Sym *sym;
6356 asection *sec;
6357 bfd_vma off;
6358
6359 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6360 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6361 return 0;
411e1bfb 6362
37da22e5
AM
6363 if ((*tls_maskp != NULL
6364 && (**tls_maskp & TLS_TLS) != 0
6365 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 6366 || sec == NULL
6bee8834 6367 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6368 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6369 return 1;
411e1bfb
AM
6370
6371 /* Look inside a TOC section too. */
6372 if (h != NULL)
6373 {
6374 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6375 off = h->root.u.def.value;
6376 }
6377 else
6378 off = sym->st_value;
6379 off += rel->r_addend;
6380 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6381 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6382 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6383 if (toc_symndx != NULL)
6384 *toc_symndx = r_symndx;
3a71aa26
AM
6385 if (toc_addend != NULL)
6386 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6387 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6388 return 0;
854b41e7 6389 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6390 && (next_r == -1 || next_r == -2))
6391 return 1 - next_r;
951fd09b 6392 return 1;
411e1bfb
AM
6393}
6394
3b421ab3
AM
6395/* Find (or create) an entry in the tocsave hash table. */
6396
6397static struct tocsave_entry *
6398tocsave_find (struct ppc_link_hash_table *htab,
6399 enum insert_option insert,
6400 Elf_Internal_Sym **local_syms,
6401 const Elf_Internal_Rela *irela,
6402 bfd *ibfd)
6403{
6404 unsigned long r_indx;
6405 struct elf_link_hash_entry *h;
6406 Elf_Internal_Sym *sym;
6407 struct tocsave_entry ent, *p;
6408 hashval_t hash;
6409 struct tocsave_entry **slot;
6410
6411 r_indx = ELF64_R_SYM (irela->r_info);
6412 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6413 return NULL;
6414 if (ent.sec == NULL || ent.sec->output_section == NULL)
6415 {
4eca0228 6416 _bfd_error_handler
871b3ab2 6417 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
6418 return NULL;
6419 }
6420
6421 if (h != NULL)
6422 ent.offset = h->root.u.def.value;
6423 else
6424 ent.offset = sym->st_value;
6425 ent.offset += irela->r_addend;
6426
6427 hash = tocsave_htab_hash (&ent);
6428 slot = ((struct tocsave_entry **)
6429 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6430 if (slot == NULL)
6431 return NULL;
6432
6433 if (*slot == NULL)
6434 {
6435 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6436 if (p == NULL)
6437 return NULL;
6438 *p = ent;
6439 *slot = p;
6440 }
6441 return *slot;
6442}
6443
754021d0 6444/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6445 code for the old ABI, these will already have been done. */
754021d0
AM
6446
6447static bfd_boolean
6448adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6449{
6450 struct ppc_link_hash_entry *eh;
6451 asection *sym_sec;
74f0fb50 6452 struct _opd_sec_data *opd;
754021d0
AM
6453
6454 if (h->root.type == bfd_link_hash_indirect)
6455 return TRUE;
6456
754021d0
AM
6457 if (h->root.type != bfd_link_hash_defined
6458 && h->root.type != bfd_link_hash_defweak)
6459 return TRUE;
6460
6461 eh = (struct ppc_link_hash_entry *) h;
6462 if (eh->adjust_done)
6463 return TRUE;
6464
6465 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6466 opd = get_opd_info (sym_sec);
6467 if (opd != NULL && opd->adjust != NULL)
754021d0 6468 {
51aecdc5 6469 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
6470 if (adjust == -1)
6471 {
6472 /* This entry has been deleted. */
b3fac117 6473 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6474 if (dsec == NULL)
6475 {
6476 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 6477 if (discarded_section (dsec))
81688140 6478 {
b3fac117 6479 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6480 break;
6481 }
6482 }
4025353c 6483 eh->elf.root.u.def.value = 0;
81688140 6484 eh->elf.root.u.def.section = dsec;
4025353c
AM
6485 }
6486 else
6487 eh->elf.root.u.def.value += adjust;
754021d0
AM
6488 eh->adjust_done = 1;
6489 }
6490 return TRUE;
6491}
6492
8c1d1bb8 6493/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 6494 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
6495 have already been determined. */
6496
6497static bfd_boolean
6498dec_dynrel_count (bfd_vma r_info,
6499 asection *sec,
6500 struct bfd_link_info *info,
6501 Elf_Internal_Sym **local_syms,
6502 struct elf_link_hash_entry *h,
19e08130 6503 Elf_Internal_Sym *sym)
8c1d1bb8
AM
6504{
6505 enum elf_ppc64_reloc_type r_type;
19e08130 6506 asection *sym_sec = NULL;
8c1d1bb8
AM
6507
6508 /* Can this reloc be dynamic? This switch, and later tests here
6509 should be kept in sync with the code in check_relocs. */
6510 r_type = ELF64_R_TYPE (r_info);
6511 switch (r_type)
6512 {
6513 default:
6514 return TRUE;
6515
6516 case R_PPC64_TPREL16:
6517 case R_PPC64_TPREL16_LO:
6518 case R_PPC64_TPREL16_HI:
6519 case R_PPC64_TPREL16_HA:
6520 case R_PPC64_TPREL16_DS:
6521 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
6522 case R_PPC64_TPREL16_HIGH:
6523 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
6524 case R_PPC64_TPREL16_HIGHER:
6525 case R_PPC64_TPREL16_HIGHERA:
6526 case R_PPC64_TPREL16_HIGHEST:
6527 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8
AM
6528 case R_PPC64_TPREL64:
6529 case R_PPC64_DTPMOD64:
6530 case R_PPC64_DTPREL64:
6531 case R_PPC64_ADDR64:
6532 case R_PPC64_REL30:
6533 case R_PPC64_REL32:
6534 case R_PPC64_REL64:
6535 case R_PPC64_ADDR14:
6536 case R_PPC64_ADDR14_BRNTAKEN:
6537 case R_PPC64_ADDR14_BRTAKEN:
6538 case R_PPC64_ADDR16:
6539 case R_PPC64_ADDR16_DS:
6540 case R_PPC64_ADDR16_HA:
6541 case R_PPC64_ADDR16_HI:
f9c6b907
AM
6542 case R_PPC64_ADDR16_HIGH:
6543 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
6544 case R_PPC64_ADDR16_HIGHER:
6545 case R_PPC64_ADDR16_HIGHERA:
6546 case R_PPC64_ADDR16_HIGHEST:
6547 case R_PPC64_ADDR16_HIGHESTA:
6548 case R_PPC64_ADDR16_LO:
6549 case R_PPC64_ADDR16_LO_DS:
6550 case R_PPC64_ADDR24:
6551 case R_PPC64_ADDR32:
6552 case R_PPC64_UADDR16:
6553 case R_PPC64_UADDR32:
6554 case R_PPC64_UADDR64:
6555 case R_PPC64_TOC:
6556 break;
6557 }
6558
6559 if (local_syms != NULL)
6560 {
6561 unsigned long r_symndx;
8c1d1bb8
AM
6562 bfd *ibfd = sec->owner;
6563
6564 r_symndx = ELF64_R_SYM (r_info);
6565 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6566 return FALSE;
6567 }
6568
0e1862bb 6569 if ((bfd_link_pic (info)
1d483afe 6570 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 6571 || (h != NULL
198f1157 6572 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
6573 || h->root.type == bfd_link_hash_defweak
6574 || !h->def_regular))))
6575 || (ELIMINATE_COPY_RELOCS
0e1862bb 6576 && !bfd_link_pic (info)
8c1d1bb8
AM
6577 && h != NULL
6578 && (h->root.type == bfd_link_hash_defweak
6579 || !h->def_regular)))
6580 ;
6581 else
6582 return TRUE;
6583
6584 if (h != NULL)
6edfbbad 6585 {
19e08130
AM
6586 struct elf_dyn_relocs *p;
6587 struct elf_dyn_relocs **pp;
6588 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6589
6590 /* elf_gc_sweep may have already removed all dyn relocs associated
6591 with local syms for a given section. Also, symbol flags are
6592 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6593 report a dynreloc miscount. */
6594 if (*pp == NULL && info->gc_sections)
6595 return TRUE;
6596
6597 while ((p = *pp) != NULL)
60124e18 6598 {
19e08130
AM
6599 if (p->sec == sec)
6600 {
6601 if (!must_be_dyn_reloc (info, r_type))
6602 p->pc_count -= 1;
6603 p->count -= 1;
6604 if (p->count == 0)
6605 *pp = p->next;
6606 return TRUE;
6607 }
6608 pp = &p->next;
60124e18 6609 }
6edfbbad 6610 }
19e08130
AM
6611 else
6612 {
6613 struct ppc_dyn_relocs *p;
6614 struct ppc_dyn_relocs **pp;
6615 void *vpp;
6616 bfd_boolean is_ifunc;
8c1d1bb8 6617
19e08130
AM
6618 if (local_syms == NULL)
6619 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6620 if (sym_sec == NULL)
6621 sym_sec = sec;
c57da1a7 6622
19e08130
AM
6623 vpp = &elf_section_data (sym_sec)->local_dynrel;
6624 pp = (struct ppc_dyn_relocs **) vpp;
6625
6626 if (*pp == NULL && info->gc_sections)
6627 return TRUE;
6628
6629 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6630 while ((p = *pp) != NULL)
8c1d1bb8 6631 {
19e08130
AM
6632 if (p->sec == sec && p->ifunc == is_ifunc)
6633 {
6634 p->count -= 1;
6635 if (p->count == 0)
6636 *pp = p->next;
6637 return TRUE;
6638 }
6639 pp = &p->next;
8c1d1bb8 6640 }
8c1d1bb8
AM
6641 }
6642
695344c0 6643 /* xgettext:c-format */
cf97bcb0
AM
6644 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6645 sec->owner, sec);
8c1d1bb8
AM
6646 bfd_set_error (bfd_error_bad_value);
6647 return FALSE;
6648}
6649
754021d0
AM
6650/* Remove unused Official Procedure Descriptor entries. Currently we
6651 only remove those associated with functions in discarded link-once
6652 sections, or weakly defined functions that have been overridden. It
6653 would be possible to remove many more entries for statically linked
6654 applications. */
6655
b34976b6 6656bfd_boolean
e7d1c40c 6657ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
6658{
6659 bfd *ibfd;
754021d0 6660 bfd_boolean some_edited = FALSE;
3f764659 6661 asection *need_pad = NULL;
e7d1c40c
AM
6662 struct ppc_link_hash_table *htab;
6663
6664 htab = ppc_hash_table (info);
6665 if (htab == NULL)
6666 return FALSE;
1e2f5b6e 6667
c72f2fb2 6668 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
6669 {
6670 asection *sec;
6671 Elf_Internal_Rela *relstart, *rel, *relend;
6672 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 6673 Elf_Internal_Sym *local_syms;
74f0fb50 6674 struct _opd_sec_data *opd;
51aecdc5 6675 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 6676 bfd_size_type cnt_16b = 0;
1e2f5b6e 6677
854b41e7
AM
6678 if (!is_ppc64_elf (ibfd))
6679 continue;
6680
1e2f5b6e 6681 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 6682 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
6683 continue;
6684
dbaa2011 6685 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
6686 continue;
6687
1e2f5b6e
AM
6688 if (sec->output_section == bfd_abs_section_ptr)
6689 continue;
6690
6691 /* Look through the section relocs. */
6692 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6693 continue;
6694
6cdc0ccc 6695 local_syms = NULL;
0ffa91dd 6696 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
6697
6698 /* Read the relocations. */
4ce794b7 6699 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 6700 info->keep_memory);
1e2f5b6e 6701 if (relstart == NULL)
b34976b6 6702 return FALSE;
1e2f5b6e
AM
6703
6704 /* First run through the relocs to check they are sane, and to
6705 determine whether we need to edit this opd section. */
b34976b6 6706 need_edit = FALSE;
51aecdc5 6707 broken = FALSE;
3f764659 6708 need_pad = sec;
1e2f5b6e 6709 relend = relstart + sec->reloc_count;
50bc7936 6710 for (rel = relstart; rel < relend; )
1e2f5b6e 6711 {
04c9666a 6712 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
6713 unsigned long r_symndx;
6714 asection *sym_sec;
6715 struct elf_link_hash_entry *h;
6716 Elf_Internal_Sym *sym;
51aecdc5 6717 bfd_vma offset;
1e2f5b6e 6718
51aecdc5 6719 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
6720 only interested in the reloc pointing to a function entry
6721 point. */
51aecdc5
AM
6722 offset = rel->r_offset;
6723 if (rel + 1 == relend
6724 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
6725 {
6726 /* If someone messes with .opd alignment then after a
6727 "ld -r" we might have padding in the middle of .opd.
6728 Also, there's nothing to prevent someone putting
6729 something silly in .opd with the assembler. No .opd
b34976b6 6730 optimization for them! */
3f764659 6731 broken_opd:
4eca0228 6732 _bfd_error_handler
871b3ab2 6733 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 6734 broken = TRUE;
1e2f5b6e
AM
6735 break;
6736 }
6737
50bc7936
AM
6738 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6739 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6740 {
4eca0228 6741 _bfd_error_handler
695344c0 6742 /* xgettext:c-format */
871b3ab2 6743 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 6744 ibfd, r_type);
51aecdc5 6745 broken = TRUE;
50bc7936
AM
6746 break;
6747 }
6748
1e2f5b6e 6749 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
6750 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6751 r_symndx, ibfd))
50bc7936 6752 goto error_ret;
1e2f5b6e
AM
6753
6754 if (sym_sec == NULL || sym_sec->owner == NULL)
6755 {
411e1bfb
AM
6756 const char *sym_name;
6757 if (h != NULL)
6758 sym_name = h->root.root.string;
6759 else
26c61ae5
L
6760 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6761 sym_sec);
411e1bfb 6762
4eca0228 6763 _bfd_error_handler
695344c0 6764 /* xgettext:c-format */
871b3ab2 6765 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 6766 ibfd, sym_name);
51aecdc5 6767 broken = TRUE;
1e2f5b6e
AM
6768 break;
6769 }
6770
51020317
AM
6771 /* opd entries are always for functions defined in the
6772 current input bfd. If the symbol isn't defined in the
6773 input bfd, then we won't be using the function in this
6774 bfd; It must be defined in a linkonce section in another
6775 bfd, or is weak. It's also possible that we are
6776 discarding the function due to a linker script /DISCARD/,
6777 which we test for via the output_section. */
6778 if (sym_sec->owner != ibfd
6779 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 6780 need_edit = TRUE;
1e2f5b6e 6781
50bc7936 6782 rel += 2;
51aecdc5
AM
6783 if (rel + 1 == relend
6784 || (rel + 2 < relend
6785 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
6786 ++rel;
6787
6788 if (rel == relend)
3f764659
JJ
6789 {
6790 if (sec->size == offset + 24)
6791 {
6792 need_pad = NULL;
6793 break;
6794 }
51aecdc5 6795 if (sec->size == offset + 16)
3f764659
JJ
6796 {
6797 cnt_16b++;
6798 break;
6799 }
6800 goto broken_opd;
6801 }
3f764659
JJ
6802 else if (rel + 1 < relend
6803 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6804 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6805 {
51aecdc5
AM
6806 if (rel[0].r_offset == offset + 16)
6807 cnt_16b++;
6808 else if (rel[0].r_offset != offset + 24)
6809 goto broken_opd;
3f764659
JJ
6810 }
6811 else
6812 goto broken_opd;
1e2f5b6e
AM
6813 }
6814
e7d1c40c 6815 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 6816
51aecdc5 6817 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
6818 {
6819 Elf_Internal_Rela *write_rel;
d4730f92 6820 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 6821 bfd_byte *rptr, *wptr;
983bddc8 6822 bfd_byte *new_contents;
74f0fb50
AM
6823 bfd_size_type amt;
6824
983bddc8 6825 new_contents = NULL;
51aecdc5 6826 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 6827 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 6828 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
6829 if (opd->adjust == NULL)
6830 return FALSE;
1e2f5b6e
AM
6831
6832 /* This seems a waste of time as input .opd sections are all
6833 zeros as generated by gcc, but I suppose there's no reason
6834 this will always be so. We might start putting something in
6835 the third word of .opd entries. */
6836 if ((sec->flags & SEC_IN_MEMORY) == 0)
6837 {
eea6121a
AM
6838 bfd_byte *loc;
6839 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 6840 {
eea6121a
AM
6841 if (loc != NULL)
6842 free (loc);
50bc7936 6843 error_ret:
6cdc0ccc
AM
6844 if (local_syms != NULL
6845 && symtab_hdr->contents != (unsigned char *) local_syms)
6846 free (local_syms);
6cdc0ccc
AM
6847 if (elf_section_data (sec)->relocs != relstart)
6848 free (relstart);
b34976b6 6849 return FALSE;
6cdc0ccc 6850 }
1e2f5b6e
AM
6851 sec->contents = loc;
6852 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6853 }
6854
6855 elf_section_data (sec)->relocs = relstart;
6856
3f764659 6857 new_contents = sec->contents;
3f764659
JJ
6858 if (add_aux_fields)
6859 {
6860 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6861 if (new_contents == NULL)
6862 return FALSE;
51aecdc5 6863 need_pad = NULL;
3f764659 6864 }
b4f4e59f
AM
6865 wptr = new_contents;
6866 rptr = sec->contents;
1e2f5b6e 6867 write_rel = relstart;
51aecdc5 6868 for (rel = relstart; rel < relend; )
1e2f5b6e 6869 {
50bc7936
AM
6870 unsigned long r_symndx;
6871 asection *sym_sec;
6872 struct elf_link_hash_entry *h;
51aecdc5 6873 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 6874 Elf_Internal_Sym *sym;
51aecdc5
AM
6875 long opd_ent_size;
6876 Elf_Internal_Rela *next_rel;
6877 bfd_boolean skip;
50bc7936
AM
6878
6879 r_symndx = ELF64_R_SYM (rel->r_info);
6880 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 6881 r_symndx, ibfd))
50bc7936
AM
6882 goto error_ret;
6883
51aecdc5
AM
6884 next_rel = rel + 2;
6885 if (next_rel + 1 == relend
6886 || (next_rel + 2 < relend
6887 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
6888 ++next_rel;
6889
6890 /* See if the .opd entry is full 24 byte or
6891 16 byte (with fd_aux entry overlapped with next
6892 fd_func). */
6893 opd_ent_size = 24;
6894 if (next_rel == relend)
1e2f5b6e 6895 {
51aecdc5 6896 if (sec->size == rel->r_offset + 16)
3f764659 6897 opd_ent_size = 16;
51aecdc5
AM
6898 }
6899 else if (next_rel->r_offset == rel->r_offset + 16)
6900 opd_ent_size = 16;
3f764659 6901
51aecdc5
AM
6902 if (h != NULL
6903 && h->root.root.string[0] == '.')
6904 {
8c5b4e52
AM
6905 fdh = ((struct ppc_link_hash_entry *) h)->oh;
6906 if (fdh != NULL)
6907 {
6908 fdh = ppc_follow_link (fdh);
6909 if (fdh->elf.root.type != bfd_link_hash_defined
6910 && fdh->elf.root.type != bfd_link_hash_defweak)
6911 fdh = NULL;
6912 }
51aecdc5 6913 }
1e2f5b6e 6914
51aecdc5
AM
6915 skip = (sym_sec->owner != ibfd
6916 || sym_sec->output_section == bfd_abs_section_ptr);
6917 if (skip)
6918 {
6919 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 6920 {
51aecdc5
AM
6921 /* Arrange for the function descriptor sym
6922 to be dropped. */
6923 fdh->elf.root.u.def.value = 0;
6924 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 6925 }
51aecdc5 6926 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 6927
0e1862bb 6928 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
6929 rel = next_rel;
6930 else
6931 while (1)
6932 {
6933 if (!dec_dynrel_count (rel->r_info, sec, info,
6934 NULL, h, sym))
6935 goto error_ret;
754021d0 6936
51aecdc5
AM
6937 if (++rel == next_rel)
6938 break;
1e2f5b6e 6939
51aecdc5
AM
6940 r_symndx = ELF64_R_SYM (rel->r_info);
6941 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6942 r_symndx, ibfd))
6943 goto error_ret;
6944 }
50bc7936
AM
6945 }
6946 else
1e2f5b6e 6947 {
51aecdc5
AM
6948 /* We'll be keeping this opd entry. */
6949 long adjust;
6950
6951 if (fdh != NULL)
6952 {
6953 /* Redefine the function descriptor symbol to
6954 this location in the opd section. It is
6955 necessary to update the value here rather
6956 than using an array of adjustments as we do
6957 for local symbols, because various places
6958 in the generic ELF code use the value
6959 stored in u.def.value. */
6960 fdh->elf.root.u.def.value = wptr - new_contents;
6961 fdh->adjust_done = 1;
6962 }
6963
6964 /* Local syms are a bit tricky. We could
6965 tweak them as they can be cached, but
6966 we'd need to look through the local syms
6967 for the function descriptor sym which we
6968 don't have at the moment. So keep an
6969 array of adjustments. */
6970 adjust = (wptr - new_contents) - (rptr - sec->contents);
6971 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
6972
6973 if (wptr != rptr)
6974 memcpy (wptr, rptr, opd_ent_size);
6975 wptr += opd_ent_size;
6976 if (add_aux_fields && opd_ent_size == 16)
6977 {
6978 memset (wptr, '\0', 8);
6979 wptr += 8;
6980 }
6981
50bc7936 6982 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
6983 new opd entries. */
6984 for ( ; rel != next_rel; ++rel)
6985 {
6986 rel->r_offset += adjust;
6987 if (write_rel != rel)
6988 memcpy (write_rel, rel, sizeof (*rel));
6989 ++write_rel;
6990 }
1e2f5b6e 6991 }
51aecdc5
AM
6992
6993 rptr += opd_ent_size;
1e2f5b6e
AM
6994 }
6995
3f764659 6996 sec->size = wptr - new_contents;
1e2f5b6e 6997 sec->reloc_count = write_rel - relstart;
3f764659
JJ
6998 if (add_aux_fields)
6999 {
7000 free (sec->contents);
7001 sec->contents = new_contents;
7002 }
7003
05bf9422 7004 /* Fudge the header size too, as this is used later in
cdcf6e38 7005 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7006 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7007 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7008 some_edited = TRUE;
1e2f5b6e 7009 }
6cdc0ccc 7010 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7011 free (relstart);
6cdc0ccc 7012
411e1bfb
AM
7013 if (local_syms != NULL
7014 && symtab_hdr->contents != (unsigned char *) local_syms)
7015 {
7016 if (!info->keep_memory)
7017 free (local_syms);
7018 else
7019 symtab_hdr->contents = (unsigned char *) local_syms;
7020 }
7021 }
7022
754021d0
AM
7023 if (some_edited)
7024 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7025
3f764659
JJ
7026 /* If we are doing a final link and the last .opd entry is just 16 byte
7027 long, add a 8 byte padding after it. */
0e1862bb 7028 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7029 {
7030 bfd_byte *p;
7031
7032 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7033 {
7034 BFD_ASSERT (need_pad->size > 0);
7035
7036 p = bfd_malloc (need_pad->size + 8);
7037 if (p == NULL)
7038 return FALSE;
699733f6 7039
2cdcc330
AM
7040 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7041 p, 0, need_pad->size))
3f764659
JJ
7042 return FALSE;
7043
7044 need_pad->contents = p;
7045 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7046 }
7047 else
7048 {
7049 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7050 if (p == NULL)
7051 return FALSE;
7052
7053 need_pad->contents = p;
7054 }
7055
7056 memset (need_pad->contents + need_pad->size, 0, 8);
7057 need_pad->size += 8;
7058 }
7059
411e1bfb
AM
7060 return TRUE;
7061}
7062
3e04d765
AM
7063/* Analyze inline PLT call relocations to see whether calls to locally
7064 defined functions can be converted to direct calls. */
7065
7066bfd_boolean
7067ppc64_elf_inline_plt (struct bfd_link_info *info)
7068{
7069 struct ppc_link_hash_table *htab;
7070 bfd *ibfd;
7071 asection *sec;
7072 bfd_vma low_vma, high_vma, limit;
7073
7074 htab = ppc_hash_table (info);
7075 if (htab == NULL)
7076 return FALSE;
7077
7078 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7079 reduced somewhat to cater for possible stubs that might be added
7080 between the call and its destination. */
7081 if (htab->params->group_size < 0)
7082 {
7083 limit = -htab->params->group_size;
7084 if (limit == 1)
7085 limit = 0x1e00000;
7086 }
7087 else
7088 {
7089 limit = htab->params->group_size;
7090 if (limit == 1)
7091 limit = 0x1c00000;
7092 }
7093
7094 low_vma = -1;
7095 high_vma = 0;
7096 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7097 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7098 {
7099 if (low_vma > sec->vma)
7100 low_vma = sec->vma;
7101 if (high_vma < sec->vma + sec->size)
7102 high_vma = sec->vma + sec->size;
7103 }
7104
7105 /* If a "bl" can reach anywhere in local code sections, then we can
7106 convert all inline PLT sequences to direct calls when the symbol
7107 is local. */
7108 if (high_vma - low_vma < limit)
7109 {
7110 htab->can_convert_all_inline_plt = 1;
7111 return TRUE;
7112 }
7113
7114 /* Otherwise, go looking through relocs for cases where a direct
7115 call won't reach. Mark the symbol on any such reloc to disable
7116 the optimization and keep the PLT entry as it seems likely that
7117 this will be better than creating trampolines. Note that this
7118 will disable the optimization for all inline PLT calls to a
7119 particular symbol, not just those that won't reach. The
7120 difficulty in doing a more precise optimization is that the
7121 linker needs to make a decision depending on whether a
7122 particular R_PPC64_PLTCALL insn can be turned into a direct
7123 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7124 the sequence, and there is nothing that ties those relocs
7125 together except their symbol. */
7126
7127 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7128 {
7129 Elf_Internal_Shdr *symtab_hdr;
7130 Elf_Internal_Sym *local_syms;
7131
7132 if (!is_ppc64_elf (ibfd))
7133 continue;
7134
7135 local_syms = NULL;
7136 symtab_hdr = &elf_symtab_hdr (ibfd);
7137
7138 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7139 if (ppc64_elf_section_data (sec)->has_pltcall
7140 && !bfd_is_abs_section (sec->output_section))
7141 {
7142 Elf_Internal_Rela *relstart, *rel, *relend;
7143
7144 /* Read the relocations. */
7145 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7146 info->keep_memory);
7147 if (relstart == NULL)
7148 return FALSE;
7149
7150 relend = relstart + sec->reloc_count;
7151 for (rel = relstart; rel < relend; )
7152 {
7153 enum elf_ppc64_reloc_type r_type;
7154 unsigned long r_symndx;
7155 asection *sym_sec;
7156 struct elf_link_hash_entry *h;
7157 Elf_Internal_Sym *sym;
7158 unsigned char *tls_maskp;
7159
7160 r_type = ELF64_R_TYPE (rel->r_info);
7161 if (r_type != R_PPC64_PLTCALL)
7162 continue;
7163
7164 r_symndx = ELF64_R_SYM (rel->r_info);
7165 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7166 r_symndx, ibfd))
7167 {
7168 if (elf_section_data (sec)->relocs != relstart)
7169 free (relstart);
7170 if (local_syms != NULL
2cdcc330 7171 && symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765
AM
7172 free (local_syms);
7173 return FALSE;
7174 }
7175
7176 if (sym_sec != NULL && sym_sec->output_section != NULL)
7177 {
7178 bfd_vma from, to;
7179 if (h != NULL)
7180 to = h->root.u.def.value;
7181 else
7182 to = sym->st_value;
7183 to += (rel->r_addend
7184 + sym_sec->output_offset
7185 + sym_sec->output_section->vma);
7186 from = (rel->r_offset
7187 + sec->output_offset
7188 + sec->output_section->vma);
7189 if (to - from + limit < 2 * limit)
7190 *tls_maskp &= ~PLT_KEEP;
7191 }
7192 }
7193 if (elf_section_data (sec)->relocs != relstart)
7194 free (relstart);
7195 }
7196
7197 if (local_syms != NULL
7198 && symtab_hdr->contents != (unsigned char *) local_syms)
7199 {
7200 if (!info->keep_memory)
7201 free (local_syms);
7202 else
7203 symtab_hdr->contents = (unsigned char *) local_syms;
7204 }
7205 }
7206
7207 return TRUE;
7208}
7209
e1918d23 7210/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7211
e1918d23 7212asection *
e7d1c40c 7213ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7214{
411e1bfb
AM
7215 struct ppc_link_hash_table *htab;
7216
411e1bfb 7217 htab = ppc_hash_table (info);
4dfe6ac6
NC
7218 if (htab == NULL)
7219 return NULL;
7220
ee67d69a
AM
7221 if (abiversion (info->output_bfd) == 1)
7222 htab->opd_abi = 1;
7223
e7d1c40c 7224 if (htab->params->no_multi_toc)
33c0ec9d
AM
7225 htab->do_multi_toc = 0;
7226 else if (!htab->do_multi_toc)
e7d1c40c 7227 htab->params->no_multi_toc = 1;
33c0ec9d 7228
8b5f1ed8
AM
7229 /* Default to --no-plt-localentry, as this option can cause problems
7230 with symbol interposition. For example, glibc libpthread.so and
7231 libc.so duplicate many pthread symbols, with a fallback
7232 implementation in libc.so. In some cases the fallback does more
7233 work than the pthread implementation. __pthread_condattr_destroy
7234 is one such symbol: the libpthread.so implementation is
7235 localentry:0 while the libc.so implementation is localentry:8.
7236 An app that "cleverly" uses dlopen to only load necessary
7237 libraries at runtime may omit loading libpthread.so when not
7238 running multi-threaded, which then results in the libc.so
7239 fallback symbols being used and ld.so complaining. Now there
7240 are workarounds in ld (see non_zero_localentry) to detect the
7241 pthread situation, but that may not be the only case where
7242 --plt-localentry can cause trouble. */
f378ab09 7243 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7244 htab->params->plt_localentry0 = 0;
d44c746a
AM
7245 if (htab->params->plt_localentry0
7246 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7247 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
7248 _bfd_error_handler
7249 (_("warning: --plt-localentry is especially dangerous without "
7250 "ld.so support to detect ABI violations"));
f378ab09 7251
3a71aa26
AM
7252 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7253 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7254 FALSE, FALSE, TRUE));
a7f2871e
AM
7255 /* Move dynamic linking info to the function descriptor sym. */
7256 if (htab->tls_get_addr != NULL)
7257 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7258 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7259 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7260 FALSE, FALSE, TRUE));
7c9cf415 7261 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
7262 {
7263 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7264
7265 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7266 FALSE, FALSE, TRUE);
7267 if (opt != NULL)
7268 func_desc_adjust (opt, info);
7269 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7270 FALSE, FALSE, TRUE);
7271 if (opt_fd != NULL
7272 && (opt_fd->root.type == bfd_link_hash_defined
7273 || opt_fd->root.type == bfd_link_hash_defweak))
7274 {
7275 /* If glibc supports an optimized __tls_get_addr call stub,
7276 signalled by the presence of __tls_get_addr_opt, and we'll
7277 be calling __tls_get_addr via a plt call stub, then
7278 make __tls_get_addr point to __tls_get_addr_opt. */
7279 tga_fd = &htab->tls_get_addr_fd->elf;
7280 if (htab->elf.dynamic_sections_created
7281 && tga_fd != NULL
7282 && (tga_fd->type == STT_FUNC
7283 || tga_fd->needs_plt)
7284 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 7285 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
7286 {
7287 struct plt_entry *ent;
7288
7289 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7290 if (ent->plt.refcount > 0)
7291 break;
7292 if (ent != NULL)
7293 {
7294 tga_fd->root.type = bfd_link_hash_indirect;
7295 tga_fd->root.u.i.link = &opt_fd->root;
7296 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 7297 opt_fd->mark = 1;
a7f2871e
AM
7298 if (opt_fd->dynindx != -1)
7299 {
7300 /* Use __tls_get_addr_opt in dynamic relocations. */
7301 opt_fd->dynindx = -1;
7302 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7303 opt_fd->dynstr_index);
7304 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7305 return NULL;
a7f2871e 7306 }
2cdcc330
AM
7307 htab->tls_get_addr_fd
7308 = (struct ppc_link_hash_entry *) opt_fd;
a7f2871e
AM
7309 tga = &htab->tls_get_addr->elf;
7310 if (opt != NULL && tga != NULL)
7311 {
7312 tga->root.type = bfd_link_hash_indirect;
7313 tga->root.u.i.link = &opt->root;
7314 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 7315 opt->mark = 1;
a7f2871e
AM
7316 _bfd_elf_link_hash_hide_symbol (info, opt,
7317 tga->forced_local);
7318 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7319 }
7320 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7321 htab->tls_get_addr_fd->is_func_descriptor = 1;
7322 if (htab->tls_get_addr != NULL)
7323 {
7324 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7325 htab->tls_get_addr->is_func = 1;
7326 }
7327 }
7328 }
7329 }
7c9cf415
AM
7330 else if (htab->params->tls_get_addr_opt < 0)
7331 htab->params->tls_get_addr_opt = 0;
a7f2871e 7332 }
33c0ec9d 7333 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7334}
8387904d 7335
3a71aa26
AM
7336/* Return TRUE iff REL is a branch reloc with a global symbol matching
7337 HASH1 or HASH2. */
8387904d 7338
3a71aa26
AM
7339static bfd_boolean
7340branch_reloc_hash_match (const bfd *ibfd,
7341 const Elf_Internal_Rela *rel,
7342 const struct ppc_link_hash_entry *hash1,
7343 const struct ppc_link_hash_entry *hash2)
7344{
7345 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7346 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7347 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7348
e054468f 7349 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7350 {
3a71aa26
AM
7351 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7352 struct elf_link_hash_entry *h;
8387904d 7353
3a71aa26 7354 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7355 h = elf_follow_link (h);
3a71aa26
AM
7356 if (h == &hash1->elf || h == &hash2->elf)
7357 return TRUE;
a48ebf4d 7358 }
3a71aa26 7359 return FALSE;
951fd09b 7360}
411e1bfb 7361
951fd09b
AM
7362/* Run through all the TLS relocs looking for optimization
7363 opportunities. The linker has been hacked (see ppc64elf.em) to do
7364 a preliminary section layout so that we know the TLS segment
7365 offsets. We can't optimize earlier because some optimizations need
7366 to know the tp offset, and we need to optimize before allocating
7367 dynamic relocations. */
7368
7369bfd_boolean
33c0ec9d 7370ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7371{
7372 bfd *ibfd;
7373 asection *sec;
7374 struct ppc_link_hash_table *htab;
663a1470 7375 unsigned char *toc_ref;
102890f0 7376 int pass;
951fd09b 7377
3cbc1e5e 7378 if (!bfd_link_executable (info))
411e1bfb
AM
7379 return TRUE;
7380
951fd09b 7381 htab = ppc_hash_table (info);
4dfe6ac6
NC
7382 if (htab == NULL)
7383 return FALSE;
7384
663a1470
AM
7385 /* Make two passes over the relocs. On the first pass, mark toc
7386 entries involved with tls relocs, and check that tls relocs
7387 involved in setting up a tls_get_addr call are indeed followed by
7388 such a call. If they are not, we can't do any tls optimization.
7389 On the second pass twiddle tls_mask flags to notify
7390 relocate_section that optimization can be done, and adjust got
7391 and plt refcounts. */
7392 toc_ref = NULL;
7393 for (pass = 0; pass < 2; ++pass)
c72f2fb2 7394 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
7395 {
7396 Elf_Internal_Sym *locsyms = NULL;
7397 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7398
102890f0
AM
7399 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7400 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7401 {
7402 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7403 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7404
102890f0
AM
7405 /* Read the relocations. */
7406 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7407 info->keep_memory);
7408 if (relstart == NULL)
2915c55b
JK
7409 {
7410 free (toc_ref);
7411 return FALSE;
7412 }
411e1bfb 7413
102890f0
AM
7414 relend = relstart + sec->reloc_count;
7415 for (rel = relstart; rel < relend; rel++)
7416 {
7417 enum elf_ppc64_reloc_type r_type;
7418 unsigned long r_symndx;
7419 struct elf_link_hash_entry *h;
7420 Elf_Internal_Sym *sym;
7421 asection *sym_sec;
f961d9dd
AM
7422 unsigned char *tls_mask;
7423 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
7424 bfd_vma value;
7425 bfd_boolean ok_tprel, is_local;
7426 long toc_ref_index = 0;
7427 int expecting_tls_get_addr = 0;
663a1470 7428 bfd_boolean ret = FALSE;
411e1bfb 7429
102890f0
AM
7430 r_symndx = ELF64_R_SYM (rel->r_info);
7431 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7432 r_symndx, ibfd))
7433 {
7434 err_free_rel:
7435 if (elf_section_data (sec)->relocs != relstart)
7436 free (relstart);
7437 if (toc_ref != NULL)
7438 free (toc_ref);
7439 if (locsyms != NULL
0ffa91dd 7440 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7441 != (unsigned char *) locsyms))
7442 free (locsyms);
663a1470 7443 return ret;
102890f0 7444 }
411e1bfb 7445
102890f0
AM
7446 if (h != NULL)
7447 {
766bc656
AM
7448 if (h->root.type == bfd_link_hash_defined
7449 || h->root.type == bfd_link_hash_defweak)
7450 value = h->root.u.def.value;
7451 else if (h->root.type == bfd_link_hash_undefweak)
7452 value = 0;
7453 else
663a1470
AM
7454 {
7455 found_tls_get_addr_arg = 0;
7456 continue;
7457 }
102890f0
AM
7458 }
7459 else
7460 /* Symbols referenced by TLS relocs must be of type
7461 STT_TLS. So no need for .opd local sym adjust. */
7462 value = sym->st_value;
7463
7464 ok_tprel = FALSE;
7465 is_local = FALSE;
7466 if (h == NULL
7467 || !h->def_dynamic)
7468 {
7469 is_local = TRUE;
766bc656
AM
7470 if (h != NULL
7471 && h->root.type == bfd_link_hash_undefweak)
7472 ok_tprel = TRUE;
c27b8c2a
AM
7473 else if (sym_sec != NULL
7474 && sym_sec->output_section != NULL)
766bc656
AM
7475 {
7476 value += sym_sec->output_offset;
7477 value += sym_sec->output_section->vma;
7478 value -= htab->elf.tls_sec->vma;
7479 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7480 < (bfd_vma) 1 << 32);
7481 }
102890f0 7482 }
951fd09b 7483
102890f0 7484 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7485 /* If this section has old-style __tls_get_addr calls
7486 without marker relocs, then check that each
7487 __tls_get_addr call reloc is preceded by a reloc
7488 that conceivably belongs to the __tls_get_addr arg
7489 setup insn. If we don't find matching arg setup
7490 relocs, don't do any tls optimization. */
7491 if (pass == 0
7492 && sec->has_tls_get_addr_call
7493 && h != NULL
7494 && (h == &htab->tls_get_addr->elf
7495 || h == &htab->tls_get_addr_fd->elf)
7496 && !found_tls_get_addr_arg
7497 && is_branch_reloc (r_type))
7498 {
25f53a85 7499 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
7500 "TLS optimization disabled\n"),
7501 ibfd, sec, rel->r_offset);
7502 ret = TRUE;
7503 goto err_free_rel;
7504 }
7505
7506 found_tls_get_addr_arg = 0;
102890f0
AM
7507 switch (r_type)
7508 {
7509 case R_PPC64_GOT_TLSLD16:
7510 case R_PPC64_GOT_TLSLD16_LO:
7511 expecting_tls_get_addr = 1;
663a1470 7512 found_tls_get_addr_arg = 1;
1a0670f3 7513 /* Fall through. */
102890f0
AM
7514
7515 case R_PPC64_GOT_TLSLD16_HI:
7516 case R_PPC64_GOT_TLSLD16_HA:
7517 /* These relocs should never be against a symbol
7518 defined in a shared lib. Leave them alone if
7519 that turns out to be the case. */
7520 if (!is_local)
7521 continue;
411e1bfb 7522
102890f0 7523 /* LD -> LE */
411e1bfb 7524 tls_set = 0;
102890f0
AM
7525 tls_clear = TLS_LD;
7526 tls_type = TLS_TLS | TLS_LD;
7527 break;
411e1bfb 7528
102890f0
AM
7529 case R_PPC64_GOT_TLSGD16:
7530 case R_PPC64_GOT_TLSGD16_LO:
7531 expecting_tls_get_addr = 1;
663a1470 7532 found_tls_get_addr_arg = 1;
1a0670f3 7533 /* Fall through. */
102890f0
AM
7534
7535 case R_PPC64_GOT_TLSGD16_HI:
7536 case R_PPC64_GOT_TLSGD16_HA:
7537 if (ok_tprel)
7538 /* GD -> LE */
411e1bfb 7539 tls_set = 0;
102890f0
AM
7540 else
7541 /* GD -> IE */
7542 tls_set = TLS_TLS | TLS_TPRELGD;
7543 tls_clear = TLS_GD;
7544 tls_type = TLS_TLS | TLS_GD;
7545 break;
7546
7547 case R_PPC64_GOT_TPREL16_DS:
7548 case R_PPC64_GOT_TPREL16_LO_DS:
7549 case R_PPC64_GOT_TPREL16_HI:
7550 case R_PPC64_GOT_TPREL16_HA:
7551 if (ok_tprel)
7552 {
7553 /* IE -> LE */
7554 tls_set = 0;
7555 tls_clear = TLS_TPREL;
7556 tls_type = TLS_TLS | TLS_TPREL;
7557 break;
7558 }
411e1bfb
AM
7559 continue;
7560
727fc41e
AM
7561 case R_PPC64_TLSGD:
7562 case R_PPC64_TLSLD:
23cedd1d
AM
7563 if (rel + 1 < relend
7564 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7565 {
7566 if (pass != 0
2cdcc330
AM
7567 && (ELF64_R_TYPE (rel[1].r_info)
7568 != R_PPC64_PLTSEQ))
23cedd1d
AM
7569 {
7570 r_symndx = ELF64_R_SYM (rel[1].r_info);
7571 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 7572 r_symndx, ibfd))
23cedd1d
AM
7573 goto err_free_rel;
7574 if (h != NULL)
7575 {
7576 struct plt_entry *ent = NULL;
7577
7578 for (ent = h->plt.plist;
7579 ent != NULL;
7580 ent = ent->next)
7581 if (ent->addend == rel[1].r_addend)
7582 break;
7583
7584 if (ent != NULL
7585 && ent->plt.refcount > 0)
7586 ent->plt.refcount -= 1;
7587 }
7588 }
7589 continue;
7590 }
663a1470 7591 found_tls_get_addr_arg = 1;
1a0670f3 7592 /* Fall through. */
663a1470
AM
7593
7594 case R_PPC64_TLS:
7595 case R_PPC64_TOC16:
7596 case R_PPC64_TOC16_LO:
102890f0
AM
7597 if (sym_sec == NULL || sym_sec != toc)
7598 continue;
7599
7600 /* Mark this toc entry as referenced by a TLS
7601 code sequence. We can do that now in the
7602 case of R_PPC64_TLS, and after checking for
7603 tls_get_addr for the TOC16 relocs. */
7604 if (toc_ref == NULL)
2cdcc330
AM
7605 toc_ref
7606 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
7607 if (toc_ref == NULL)
7608 goto err_free_rel;
7609
102890f0
AM
7610 if (h != NULL)
7611 value = h->root.u.def.value;
7612 else
7613 value = sym->st_value;
7614 value += rel->r_addend;
73242275
AM
7615 if (value % 8 != 0)
7616 continue;
7617 BFD_ASSERT (value < toc->size
7618 && toc->output_offset % 8 == 0);
663a1470 7619 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7620 if (r_type == R_PPC64_TLS
7621 || r_type == R_PPC64_TLSGD
7622 || r_type == R_PPC64_TLSLD)
102890f0
AM
7623 {
7624 toc_ref[toc_ref_index] = 1;
7625 continue;
7626 }
7627
7628 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7629 continue;
7630
7631 tls_set = 0;
7632 tls_clear = 0;
7633 expecting_tls_get_addr = 2;
7634 break;
7635
7636 case R_PPC64_TPREL64:
7637 if (pass == 0
7638 || sec != toc
7639 || toc_ref == NULL
663a1470 7640 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7641 continue;
7642 if (ok_tprel)
7643 {
7644 /* IE -> LE */
7645 tls_set = TLS_EXPLICIT;
7646 tls_clear = TLS_TPREL;
7647 break;
7648 }
7649 continue;
7650
7651 case R_PPC64_DTPMOD64:
7652 if (pass == 0
7653 || sec != toc
7654 || toc_ref == NULL
663a1470 7655 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7656 continue;
7657 if (rel + 1 < relend
7658 && (rel[1].r_info
7659 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7660 && rel[1].r_offset == rel->r_offset + 8)
7661 {
7662 if (ok_tprel)
7663 /* GD -> LE */
7664 tls_set = TLS_EXPLICIT | TLS_GD;
7665 else
7666 /* GD -> IE */
7667 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7668 tls_clear = TLS_GD;
7669 }
7670 else
7671 {
7672 if (!is_local)
7673 continue;
7674
7675 /* LD -> LE */
7676 tls_set = TLS_EXPLICIT;
7677 tls_clear = TLS_LD;
7678 }
7679 break;
7680
7681 default:
7682 continue;
7683 }
7684
7685 if (pass == 0)
7686 {
727fc41e
AM
7687 if (!expecting_tls_get_addr
7688 || !sec->has_tls_get_addr_call)
102890f0
AM
7689 continue;
7690
3a71aa26
AM
7691 if (rel + 1 < relend
7692 && branch_reloc_hash_match (ibfd, rel + 1,
7693 htab->tls_get_addr,
7694 htab->tls_get_addr_fd))
102890f0 7695 {
3a71aa26 7696 if (expecting_tls_get_addr == 2)
102890f0 7697 {
3a71aa26 7698 /* Check for toc tls entries. */
f961d9dd 7699 unsigned char *toc_tls;
3a71aa26
AM
7700 int retval;
7701
7702 retval = get_tls_mask (&toc_tls, NULL, NULL,
7703 &locsyms,
7704 rel, ibfd);
7705 if (retval == 0)
7706 goto err_free_rel;
663a1470
AM
7707 if (toc_tls != NULL)
7708 {
37da22e5
AM
7709 if ((*toc_tls & TLS_TLS) != 0
7710 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
7711 found_tls_get_addr_arg = 1;
7712 if (retval > 1)
7713 toc_ref[toc_ref_index] = 1;
7714 }
102890f0 7715 }
3a71aa26 7716 continue;
102890f0
AM
7717 }
7718
102890f0
AM
7719 /* Uh oh, we didn't find the expected call. We
7720 could just mark this symbol to exclude it
7721 from tls optimization but it's safer to skip
663a1470 7722 the entire optimization. */
695344c0 7723 /* xgettext:c-format */
25f53a85 7724 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
7725 "TLS optimization disabled\n"),
7726 ibfd, sec, rel->r_offset);
7727 ret = TRUE;
7728 goto err_free_rel;
102890f0
AM
7729 }
7730
37da22e5
AM
7731 /* If we don't have old-style __tls_get_addr calls
7732 without TLSGD/TLSLD marker relocs, and we haven't
7733 found a new-style __tls_get_addr call with a
7734 marker for this symbol, then we either have a
7735 broken object file or an -mlongcall style
7736 indirect call to __tls_get_addr without a marker.
7737 Disable optimization in this case. */
7738 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
7739 && (tls_set & TLS_EXPLICIT) == 0
7740 && !sec->has_tls_get_addr_call
7741 && ((*tls_mask & (TLS_TLS | TLS_MARK))
7742 != (TLS_TLS | TLS_MARK)))
7743 continue;
7744
23cedd1d 7745 if (expecting_tls_get_addr)
102890f0 7746 {
23cedd1d
AM
7747 struct plt_entry *ent = NULL;
7748
7749 if (htab->tls_get_addr != NULL)
7750 for (ent = htab->tls_get_addr->elf.plt.plist;
7751 ent != NULL;
7752 ent = ent->next)
7753 if (ent->addend == 0)
102890f0 7754 break;
411e1bfb 7755
23cedd1d
AM
7756 if (ent == NULL && htab->tls_get_addr_fd != NULL)
7757 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7758 ent != NULL;
7759 ent = ent->next)
7760 if (ent->addend == 0)
102890f0 7761 break;
23cedd1d
AM
7762
7763 if (ent != NULL
7764 && ent->plt.refcount > 0)
7765 ent->plt.refcount -= 1;
102890f0 7766 }
411e1bfb 7767
102890f0 7768 if (tls_clear == 0)
30038c59
AM
7769 continue;
7770
102890f0
AM
7771 if ((tls_set & TLS_EXPLICIT) == 0)
7772 {
7773 struct got_entry *ent;
411e1bfb 7774
102890f0
AM
7775 /* Adjust got entry for this reloc. */
7776 if (h != NULL)
7777 ent = h->got.glist;
7778 else
7779 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 7780
102890f0
AM
7781 for (; ent != NULL; ent = ent->next)
7782 if (ent->addend == rel->r_addend
7783 && ent->owner == ibfd
7784 && ent->tls_type == tls_type)
7785 break;
7786 if (ent == NULL)
7787 abort ();
411e1bfb 7788
102890f0
AM
7789 if (tls_set == 0)
7790 {
7791 /* We managed to get rid of a got entry. */
7792 if (ent->got.refcount > 0)
7793 ent->got.refcount -= 1;
7794 }
7795 }
7796 else
7797 {
7798 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7799 we'll lose one or two dyn relocs. */
7800 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 7801 NULL, h, sym))
102890f0 7802 return FALSE;
411e1bfb 7803
102890f0
AM
7804 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7805 {
7806 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 7807 NULL, h, sym))
102890f0
AM
7808 return FALSE;
7809 }
7810 }
411e1bfb 7811
102890f0
AM
7812 *tls_mask |= tls_set;
7813 *tls_mask &= ~tls_clear;
7814 }
8c1d1bb8 7815
102890f0
AM
7816 if (elf_section_data (sec)->relocs != relstart)
7817 free (relstart);
7818 }
411e1bfb 7819
663a1470
AM
7820 if (locsyms != NULL
7821 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7822 {
7823 if (!info->keep_memory)
7824 free (locsyms);
7825 else
7826 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7827 }
7828 }
411e1bfb 7829
663a1470
AM
7830 if (toc_ref != NULL)
7831 free (toc_ref);
9a23f96e 7832 htab->do_tls_opt = 1;
b34976b6 7833 return TRUE;
1e2f5b6e 7834}
b34976b6 7835
c5614fa4
AM
7836/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7837 the values of any global symbols in a toc section that has been
7838 edited. Globals in toc sections should be a rarity, so this function
7839 sets a flag if any are found in toc sections other than the one just
de194d85 7840 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
7841
7842struct adjust_toc_info
7843{
7844 asection *toc;
7845 unsigned long *skip;
7846 bfd_boolean global_toc_syms;
7847};
7848
ba761f19
AM
7849enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7850
c5614fa4
AM
7851static bfd_boolean
7852adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7853{
7854 struct ppc_link_hash_entry *eh;
7855 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 7856 unsigned long i;
c5614fa4 7857
c5614fa4
AM
7858 if (h->root.type != bfd_link_hash_defined
7859 && h->root.type != bfd_link_hash_defweak)
7860 return TRUE;
7861
7862 eh = (struct ppc_link_hash_entry *) h;
7863 if (eh->adjust_done)
7864 return TRUE;
7865
7866 if (eh->elf.root.u.def.section == toc_inf->toc)
7867 {
854b41e7
AM
7868 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7869 i = toc_inf->toc->rawsize >> 3;
c5614fa4 7870 else
854b41e7
AM
7871 i = eh->elf.root.u.def.value >> 3;
7872
ba761f19 7873 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 7874 {
4eca0228 7875 _bfd_error_handler
854b41e7
AM
7876 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7877 do
7878 ++i;
ba761f19 7879 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 7880 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 7881 }
854b41e7
AM
7882
7883 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
7884 eh->adjust_done = 1;
7885 }
7886 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7887 toc_inf->global_toc_syms = TRUE;
7888
7889 return TRUE;
7890}
7891
39eeab25
AM
7892/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
7893 on a _LO variety toc/got reloc. */
560c8763
AM
7894
7895static bfd_boolean
39eeab25 7896ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 7897{
39eeab25
AM
7898 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7899 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
7900 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7901 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7902 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7903 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7904 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7905 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7906 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7907 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7908 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7909 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7910 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7911 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7912 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
7913 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7914 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7915 /* Exclude lfqu by testing reloc. If relocs are ever
7916 defined for the reduced D field in psq_lu then those
7917 will need testing too. */
7918 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7919 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7920 && (insn & 1) == 0)
7921 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7922 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7923 /* Exclude stfqu. psq_stu as above for psq_lu. */
7924 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7925 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7926 && (insn & 1) == 0));
560c8763
AM
7927}
7928
c5614fa4
AM
7929/* Examine all relocs referencing .toc sections in order to remove
7930 unused .toc entries. */
7931
7932bfd_boolean
33c0ec9d 7933ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
7934{
7935 bfd *ibfd;
7936 struct adjust_toc_info toc_inf;
67f0cbdb 7937 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 7938
67f0cbdb 7939 htab->do_toc_opt = 1;
c5614fa4 7940 toc_inf.global_toc_syms = TRUE;
c72f2fb2 7941 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
7942 {
7943 asection *toc, *sec;
7944 Elf_Internal_Shdr *symtab_hdr;
7945 Elf_Internal_Sym *local_syms;
425b145b 7946 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
7947 unsigned long *skip, *drop;
7948 unsigned char *used;
7949 unsigned char *keep, last, some_unused;
7950
854b41e7
AM
7951 if (!is_ppc64_elf (ibfd))
7952 continue;
7953
c5614fa4
AM
7954 toc = bfd_get_section_by_name (ibfd, ".toc");
7955 if (toc == NULL
92b7a70f 7956 || toc->size == 0
dbaa2011
AM
7957 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
7958 || discarded_section (toc))
c5614fa4
AM
7959 continue;
7960
425b145b 7961 toc_relocs = NULL;
c5614fa4 7962 local_syms = NULL;
0ffa91dd 7963 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
7964
7965 /* Look at sections dropped from the final link. */
7966 skip = NULL;
7967 relstart = NULL;
7968 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7969 {
7970 if (sec->reloc_count == 0
dbaa2011 7971 || !discarded_section (sec)
c5614fa4
AM
7972 || get_opd_info (sec)
7973 || (sec->flags & SEC_ALLOC) == 0
7974 || (sec->flags & SEC_DEBUGGING) != 0)
7975 continue;
7976
7977 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7978 if (relstart == NULL)
7979 goto error_ret;
7980
7981 /* Run through the relocs to see which toc entries might be
7982 unused. */
7983 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7984 {
7985 enum elf_ppc64_reloc_type r_type;
7986 unsigned long r_symndx;
7987 asection *sym_sec;
7988 struct elf_link_hash_entry *h;
7989 Elf_Internal_Sym *sym;
7990 bfd_vma val;
7991
7992 r_type = ELF64_R_TYPE (rel->r_info);
7993 switch (r_type)
7994 {
7995 default:
7996 continue;
7997
7998 case R_PPC64_TOC16:
7999 case R_PPC64_TOC16_LO:
8000 case R_PPC64_TOC16_HI:
8001 case R_PPC64_TOC16_HA:
8002 case R_PPC64_TOC16_DS:
8003 case R_PPC64_TOC16_LO_DS:
8004 break;
8005 }
8006
8007 r_symndx = ELF64_R_SYM (rel->r_info);
8008 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8009 r_symndx, ibfd))
8010 goto error_ret;
8011
8012 if (sym_sec != toc)
8013 continue;
8014
8015 if (h != NULL)
8016 val = h->root.u.def.value;
8017 else
8018 val = sym->st_value;
8019 val += rel->r_addend;
8020
8021 if (val >= toc->size)
8022 continue;
8023
8024 /* Anything in the toc ought to be aligned to 8 bytes.
8025 If not, don't mark as unused. */
8026 if (val & 7)
8027 continue;
8028
8029 if (skip == NULL)
8030 {
854b41e7 8031 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8032 if (skip == NULL)
8033 goto error_ret;
8034 }
8035
ba761f19 8036 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8037 }
8038
8039 if (elf_section_data (sec)->relocs != relstart)
8040 free (relstart);
8041 }
8042
ba761f19
AM
8043 /* For largetoc loads of address constants, we can convert
8044 . addis rx,2,addr@got@ha
8045 . ld ry,addr@got@l(rx)
8046 to
8047 . addis rx,2,addr@toc@ha
8048 . addi ry,rx,addr@toc@l
8049 when addr is within 2G of the toc pointer. This then means
8050 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8051
ba761f19
AM
8052 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8053 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8054 && toc->reloc_count != 0)
8055 {
8056 /* Read toc relocs. */
425b145b
AM
8057 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8058 info->keep_memory);
8059 if (toc_relocs == NULL)
ba761f19
AM
8060 goto error_ret;
8061
425b145b 8062 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8063 {
8064 enum elf_ppc64_reloc_type r_type;
8065 unsigned long r_symndx;
8066 asection *sym_sec;
8067 struct elf_link_hash_entry *h;
8068 Elf_Internal_Sym *sym;
8069 bfd_vma val, addr;
8070
8071 r_type = ELF64_R_TYPE (rel->r_info);
8072 if (r_type != R_PPC64_ADDR64)
8073 continue;
8074
8075 r_symndx = ELF64_R_SYM (rel->r_info);
8076 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8077 r_symndx, ibfd))
8078 goto error_ret;
8079
425b145b 8080 if (sym_sec == NULL
c27b8c2a 8081 || sym_sec->output_section == NULL
dbaa2011 8082 || discarded_section (sym_sec))
425b145b
AM
8083 continue;
8084
afe397ea 8085 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8086 continue;
8087
8088 if (h != NULL)
bddc25c9
AM
8089 {
8090 if (h->type == STT_GNU_IFUNC)
8091 continue;
8092 val = h->root.u.def.value;
8093 }
ba761f19 8094 else
bddc25c9
AM
8095 {
8096 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8097 continue;
8098 val = sym->st_value;
8099 }
ba761f19
AM
8100 val += rel->r_addend;
8101 val += sym_sec->output_section->vma + sym_sec->output_offset;
8102
8103 /* We don't yet know the exact toc pointer value, but we
8104 know it will be somewhere in the toc section. Don't
8105 optimize if the difference from any possible toc
8106 pointer is outside [ff..f80008000, 7fff7fff]. */
8107 addr = toc->output_section->vma + TOC_BASE_OFF;
8108 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8109 continue;
8110
8111 addr = toc->output_section->vma + toc->output_section->rawsize;
8112 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8113 continue;
8114
8115 if (skip == NULL)
8116 {
8117 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8118 if (skip == NULL)
8119 goto error_ret;
8120 }
8121
8122 skip[rel->r_offset >> 3]
425b145b 8123 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8124 }
ba761f19
AM
8125 }
8126
c5614fa4
AM
8127 if (skip == NULL)
8128 continue;
8129
8130 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8131 if (used == NULL)
8132 {
8133 error_ret:
8134 if (local_syms != NULL
8135 && symtab_hdr->contents != (unsigned char *) local_syms)
8136 free (local_syms);
8137 if (sec != NULL
8138 && relstart != NULL
8139 && elf_section_data (sec)->relocs != relstart)
8140 free (relstart);
425b145b
AM
8141 if (toc_relocs != NULL
8142 && elf_section_data (toc)->relocs != toc_relocs)
8143 free (toc_relocs);
c5614fa4
AM
8144 if (skip != NULL)
8145 free (skip);
8146 return FALSE;
8147 }
8148
30038c59
AM
8149 /* Now check all kept sections that might reference the toc.
8150 Check the toc itself last. */
8151 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8152 : ibfd->sections);
c5614fa4 8153 sec != NULL;
c5614fa4 8154 sec = (sec == toc ? NULL
c5614fa4 8155 : sec->next == NULL ? toc
30038c59 8156 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8157 : sec->next))
8158 {
8159 int repeat;
8160
8161 if (sec->reloc_count == 0
dbaa2011 8162 || discarded_section (sec)
c5614fa4
AM
8163 || get_opd_info (sec)
8164 || (sec->flags & SEC_ALLOC) == 0
8165 || (sec->flags & SEC_DEBUGGING) != 0)
8166 continue;
8167
854b41e7
AM
8168 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8169 info->keep_memory);
c5614fa4 8170 if (relstart == NULL)
2915c55b
JK
8171 {
8172 free (used);
8173 goto error_ret;
8174 }
c5614fa4
AM
8175
8176 /* Mark toc entries referenced as used. */
c5614fa4 8177 do
d4f1ee75
AM
8178 {
8179 repeat = 0;
8180 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8181 {
8182 enum elf_ppc64_reloc_type r_type;
8183 unsigned long r_symndx;
8184 asection *sym_sec;
8185 struct elf_link_hash_entry *h;
8186 Elf_Internal_Sym *sym;
8187 bfd_vma val;
8188 enum {no_check, check_lo, check_ha} insn_check;
98528052 8189
d4f1ee75
AM
8190 r_type = ELF64_R_TYPE (rel->r_info);
8191 switch (r_type)
8192 {
8193 default:
8194 insn_check = no_check;
8195 break;
98528052 8196
d4f1ee75
AM
8197 case R_PPC64_GOT_TLSLD16_HA:
8198 case R_PPC64_GOT_TLSGD16_HA:
8199 case R_PPC64_GOT_TPREL16_HA:
8200 case R_PPC64_GOT_DTPREL16_HA:
8201 case R_PPC64_GOT16_HA:
8202 case R_PPC64_TOC16_HA:
8203 insn_check = check_ha;
8204 break;
98528052 8205
d4f1ee75
AM
8206 case R_PPC64_GOT_TLSLD16_LO:
8207 case R_PPC64_GOT_TLSGD16_LO:
8208 case R_PPC64_GOT_TPREL16_LO_DS:
8209 case R_PPC64_GOT_DTPREL16_LO_DS:
8210 case R_PPC64_GOT16_LO:
8211 case R_PPC64_GOT16_LO_DS:
8212 case R_PPC64_TOC16_LO:
8213 case R_PPC64_TOC16_LO_DS:
8214 insn_check = check_lo;
8215 break;
8216 }
560c8763 8217
d4f1ee75
AM
8218 if (insn_check != no_check)
8219 {
8220 bfd_vma off = rel->r_offset & ~3;
8221 unsigned char buf[4];
8222 unsigned int insn;
c5614fa4 8223
d4f1ee75
AM
8224 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8225 {
8226 free (used);
8227 goto error_ret;
8228 }
8229 insn = bfd_get_32 (ibfd, buf);
8230 if (insn_check == check_lo
39eeab25 8231 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
8232 : ((insn & ((0x3f << 26) | 0x1f << 16))
8233 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8234 {
8235 char str[12];
8236
8237 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8238 sprintf (str, "%#08x", insn);
8239 info->callbacks->einfo
695344c0 8240 /* xgettext:c-format */
174d0a74 8241 (_("%H: toc optimization is not supported for"
cf97bcb0 8242 " %s instruction\n"),
d4f1ee75
AM
8243 ibfd, sec, rel->r_offset & ~3, str);
8244 }
8245 }
c5614fa4 8246
d4f1ee75
AM
8247 switch (r_type)
8248 {
8249 case R_PPC64_TOC16:
8250 case R_PPC64_TOC16_LO:
8251 case R_PPC64_TOC16_HI:
8252 case R_PPC64_TOC16_HA:
8253 case R_PPC64_TOC16_DS:
8254 case R_PPC64_TOC16_LO_DS:
8255 /* In case we're taking addresses of toc entries. */
8256 case R_PPC64_ADDR64:
8257 break;
c5614fa4 8258
d4f1ee75
AM
8259 default:
8260 continue;
8261 }
c5614fa4 8262
d4f1ee75
AM
8263 r_symndx = ELF64_R_SYM (rel->r_info);
8264 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8265 r_symndx, ibfd))
8266 {
8267 free (used);
8268 goto error_ret;
8269 }
c5614fa4 8270
d4f1ee75
AM
8271 if (sym_sec != toc)
8272 continue;
c5614fa4 8273
d4f1ee75
AM
8274 if (h != NULL)
8275 val = h->root.u.def.value;
8276 else
8277 val = sym->st_value;
8278 val += rel->r_addend;
ba761f19 8279
d4f1ee75
AM
8280 if (val >= toc->size)
8281 continue;
ba761f19 8282
d4f1ee75
AM
8283 if ((skip[val >> 3] & can_optimize) != 0)
8284 {
8285 bfd_vma off;
8286 unsigned char opc;
8287
8288 switch (r_type)
8289 {
8290 case R_PPC64_TOC16_HA:
ba761f19 8291 break;
ba761f19 8292
d4f1ee75
AM
8293 case R_PPC64_TOC16_LO_DS:
8294 off = rel->r_offset;
8295 off += (bfd_big_endian (ibfd) ? -2 : 3);
8296 if (!bfd_get_section_contents (ibfd, sec, &opc,
8297 off, 1))
8298 {
8299 free (used);
8300 goto error_ret;
8301 }
8302 if ((opc & (0x3f << 2)) == (58u << 2))
8303 break;
1a0670f3 8304 /* Fall through. */
ba761f19 8305
d4f1ee75
AM
8306 default:
8307 /* Wrong sort of reloc, or not a ld. We may
8308 as well clear ref_from_discarded too. */
8309 skip[val >> 3] = 0;
8310 }
8311 }
8312
8313 if (sec != toc)
8314 used[val >> 3] = 1;
8315 /* For the toc section, we only mark as used if this
8316 entry itself isn't unused. */
8317 else if ((used[rel->r_offset >> 3]
8318 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8319 && !used[val >> 3])
8320 {
8321 /* Do all the relocs again, to catch reference
8322 chains. */
8323 repeat = 1;
8324 used[val >> 3] = 1;
8325 }
8326 }
8327 }
c5614fa4 8328 while (repeat);
854b41e7
AM
8329
8330 if (elf_section_data (sec)->relocs != relstart)
8331 free (relstart);
c5614fa4
AM
8332 }
8333
8334 /* Merge the used and skip arrays. Assume that TOC
8335 doublewords not appearing as either used or unused belong
de194d85 8336 to an entry more than one doubleword in size. */
c5614fa4
AM
8337 for (drop = skip, keep = used, last = 0, some_unused = 0;
8338 drop < skip + (toc->size + 7) / 8;
8339 ++drop, ++keep)
8340 {
8341 if (*keep)
8342 {
ba761f19
AM
8343 *drop &= ~ref_from_discarded;
8344 if ((*drop & can_optimize) != 0)
8345 some_unused = 1;
c5614fa4
AM
8346 last = 0;
8347 }
b140b010 8348 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8349 {
8350 some_unused = 1;
ba761f19 8351 last = ref_from_discarded;
c5614fa4
AM
8352 }
8353 else
8354 *drop = last;
8355 }
8356
8357 free (used);
8358
8359 if (some_unused)
8360 {
8361 bfd_byte *contents, *src;
8362 unsigned long off;
d62b3684 8363 Elf_Internal_Sym *sym;
ba761f19 8364 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8365
8366 /* Shuffle the toc contents, and at the same time convert the
8367 skip array from booleans into offsets. */
8368 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8369 goto error_ret;
8370
8371 elf_section_data (toc)->this_hdr.contents = contents;
8372
8373 for (src = contents, off = 0, drop = skip;
8374 src < contents + toc->size;
8375 src += 8, ++drop)
8376 {
ba761f19
AM
8377 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8378 off += 8;
c5614fa4
AM
8379 else if (off != 0)
8380 {
8381 *drop = off;
8382 memcpy (src - off, src, 8);
8383 }
8384 }
854b41e7 8385 *drop = off;
c5614fa4
AM
8386 toc->rawsize = toc->size;
8387 toc->size = src - contents - off;
8388
ba761f19
AM
8389 /* Adjust addends for relocs against the toc section sym,
8390 and optimize any accesses we can. */
c5614fa4
AM
8391 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8392 {
8393 if (sec->reloc_count == 0
dbaa2011 8394 || discarded_section (sec))
c5614fa4
AM
8395 continue;
8396
8397 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8398 info->keep_memory);
c5614fa4
AM
8399 if (relstart == NULL)
8400 goto error_ret;
8401
8402 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8403 {
8404 enum elf_ppc64_reloc_type r_type;
8405 unsigned long r_symndx;
8406 asection *sym_sec;
8407 struct elf_link_hash_entry *h;
854b41e7 8408 bfd_vma val;
c5614fa4
AM
8409
8410 r_type = ELF64_R_TYPE (rel->r_info);
8411 switch (r_type)
8412 {
8413 default:
8414 continue;
8415
8416 case R_PPC64_TOC16:
8417 case R_PPC64_TOC16_LO:
8418 case R_PPC64_TOC16_HI:
8419 case R_PPC64_TOC16_HA:
8420 case R_PPC64_TOC16_DS:
8421 case R_PPC64_TOC16_LO_DS:
8422 case R_PPC64_ADDR64:
8423 break;
8424 }
8425
8426 r_symndx = ELF64_R_SYM (rel->r_info);
8427 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8428 r_symndx, ibfd))
8429 goto error_ret;
8430
ba761f19 8431 if (sym_sec != toc)
c5614fa4
AM
8432 continue;
8433
ba761f19
AM
8434 if (h != NULL)
8435 val = h->root.u.def.value;
8436 else
8437 {
8438 val = sym->st_value;
8439 if (val != 0)
8440 local_toc_syms = TRUE;
8441 }
8442
8443 val += rel->r_addend;
854b41e7
AM
8444
8445 if (val > toc->rawsize)
8446 val = toc->rawsize;
ba761f19
AM
8447 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8448 continue;
8449 else if ((skip[val >> 3] & can_optimize) != 0)
8450 {
8451 Elf_Internal_Rela *tocrel
425b145b 8452 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8453 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8454
8455 switch (r_type)
8456 {
8457 case R_PPC64_TOC16_HA:
8458 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8459 break;
8460
8461 case R_PPC64_TOC16_LO_DS:
8462 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8463 break;
8464
8465 default:
28942f62
AM
8466 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8467 ppc_howto_init ();
b140b010 8468 info->callbacks->einfo
695344c0 8469 /* xgettext:c-format */
174d0a74 8470 (_("%H: %s references "
b140b010
AM
8471 "optimized away TOC entry\n"),
8472 ibfd, sec, rel->r_offset,
8473 ppc64_elf_howto_table[r_type]->name);
8474 bfd_set_error (bfd_error_bad_value);
8475 goto error_ret;
ba761f19
AM
8476 }
8477 rel->r_addend = tocrel->r_addend;
8478 elf_section_data (sec)->relocs = relstart;
8479 continue;
8480 }
8481
8482 if (h != NULL || sym->st_value != 0)
8483 continue;
854b41e7
AM
8484
8485 rel->r_addend -= skip[val >> 3];
8486 elf_section_data (sec)->relocs = relstart;
c5614fa4 8487 }
854b41e7
AM
8488
8489 if (elf_section_data (sec)->relocs != relstart)
8490 free (relstart);
c5614fa4
AM
8491 }
8492
8493 /* We shouldn't have local or global symbols defined in the TOC,
8494 but handle them anyway. */
df22d223
AM
8495 if (local_syms != NULL)
8496 for (sym = local_syms;
8497 sym < local_syms + symtab_hdr->sh_info;
8498 ++sym)
8499 if (sym->st_value != 0
8500 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8501 {
8502 unsigned long i;
854b41e7 8503
df22d223
AM
8504 if (sym->st_value > toc->rawsize)
8505 i = toc->rawsize >> 3;
8506 else
8507 i = sym->st_value >> 3;
854b41e7 8508
df22d223
AM
8509 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8510 {
8511 if (local_toc_syms)
4eca0228 8512 _bfd_error_handler
df22d223
AM
8513 (_("%s defined on removed toc entry"),
8514 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8515 do
8516 ++i;
8517 while ((skip[i] & (ref_from_discarded | can_optimize)));
8518 sym->st_value = (bfd_vma) i << 3;
8519 }
d62b3684 8520
df22d223
AM
8521 sym->st_value -= skip[i];
8522 symtab_hdr->contents = (unsigned char *) local_syms;
8523 }
c5614fa4 8524
854b41e7 8525 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8526 if (toc_inf.global_toc_syms)
8527 {
8528 toc_inf.toc = toc;
8529 toc_inf.skip = skip;
8530 toc_inf.global_toc_syms = FALSE;
8531 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8532 &toc_inf);
8533 }
854b41e7
AM
8534
8535 if (toc->reloc_count != 0)
8536 {
d4730f92 8537 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8538 Elf_Internal_Rela *wrel;
8539 bfd_size_type sz;
8540
854b41e7 8541 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8542 if (toc_relocs == NULL)
8543 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8544 info->keep_memory);
8545 if (toc_relocs == NULL)
8546 goto error_ret;
8547
425b145b
AM
8548 wrel = toc_relocs;
8549 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8550 if ((skip[rel->r_offset >> 3]
8551 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8552 {
8553 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8554 wrel->r_info = rel->r_info;
8555 wrel->r_addend = rel->r_addend;
8556 ++wrel;
8557 }
8558 else if (!dec_dynrel_count (rel->r_info, toc, info,
8559 &local_syms, NULL, NULL))
8560 goto error_ret;
8561
425b145b
AM
8562 elf_section_data (toc)->relocs = toc_relocs;
8563 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8564 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8565 sz = rel_hdr->sh_entsize;
8566 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8567 }
c5614fa4 8568 }
28be611c
AM
8569 else if (toc_relocs != NULL
8570 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8571 free (toc_relocs);
c5614fa4
AM
8572
8573 if (local_syms != NULL
8574 && symtab_hdr->contents != (unsigned char *) local_syms)
8575 {
8576 if (!info->keep_memory)
8577 free (local_syms);
8578 else
8579 symtab_hdr->contents = (unsigned char *) local_syms;
8580 }
8581 free (skip);
8582 }
8583
8584 return TRUE;
8585}
8586
1bbe0902
AM
8587/* Return true iff input section I references the TOC using
8588 instructions limited to +/-32k offsets. */
8589
8590bfd_boolean
8591ppc64_elf_has_small_toc_reloc (asection *i)
8592{
8593 return (is_ppc64_elf (i->owner)
8594 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8595}
8596
927be08e
AM
8597/* Allocate space for one GOT entry. */
8598
8599static void
8600allocate_got (struct elf_link_hash_entry *h,
8601 struct bfd_link_info *info,
8602 struct got_entry *gent)
8603{
8604 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
8605 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8606 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8607 ? 16 : 8);
8608 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8609 ? 2 : 1) * sizeof (Elf64_External_Rela);
8610 asection *got = ppc64_elf_tdata (gent->owner)->got;
8611
8612 gent->got.offset = got->size;
8613 got->size += entsize;
8614
19e08130 8615 if (h->type == STT_GNU_IFUNC)
927be08e 8616 {
33e44f2e 8617 htab->elf.irelplt->size += rentsize;
19e08130 8618 htab->got_reli_size += rentsize;
927be08e 8619 }
f15d0b54
AM
8620 else if (((bfd_link_pic (info)
8621 && !((gent->tls_type & TLS_TPREL) != 0
8622 && bfd_link_executable (info)
8623 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
8624 || (htab->elf.dynamic_sections_created
8625 && h->dynindx != -1
8626 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 8627 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 8628 {
19e08130 8629 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 8630 relgot->size += rentsize;
927be08e
AM
8631 }
8632}
8633
7865406b
AM
8634/* This function merges got entries in the same toc group. */
8635
8636static void
8637merge_got_entries (struct got_entry **pent)
8638{
8639 struct got_entry *ent, *ent2;
8640
8641 for (ent = *pent; ent != NULL; ent = ent->next)
8642 if (!ent->is_indirect)
8643 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8644 if (!ent2->is_indirect
8645 && ent2->addend == ent->addend
8646 && ent2->tls_type == ent->tls_type
8647 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8648 {
8649 ent2->is_indirect = TRUE;
8650 ent2->got.ent = ent;
8651 }
8652}
8653
46434633 8654/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
8655
8656static bfd_boolean
46434633
AM
8657ensure_undef_dynamic (struct bfd_link_info *info,
8658 struct elf_link_hash_entry *h)
f0158f44
AM
8659{
8660 struct elf_link_hash_table *htab = elf_hash_table (info);
8661
8662 if (htab->dynamic_sections_created
46434633
AM
8663 && ((info->dynamic_undefined_weak != 0
8664 && h->root.type == bfd_link_hash_undefweak)
8665 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
8666 && h->dynindx == -1
8667 && !h->forced_local
8668 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
8669 return bfd_elf_link_record_dynamic_symbol (info, h);
8670 return TRUE;
8671}
8672
65f38f15
AM
8673/* Allocate space in .plt, .got and associated reloc sections for
8674 dynamic relocs. */
5bd4f169 8675
b34976b6 8676static bfd_boolean
4ce794b7 8677allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 8678{
65f38f15
AM
8679 struct bfd_link_info *info;
8680 struct ppc_link_hash_table *htab;
5bd4f169 8681 asection *s;
65f38f15 8682 struct ppc_link_hash_entry *eh;
0b8bcf0d 8683 struct got_entry **pgent, *gent;
5bd4f169 8684
e92d460e 8685 if (h->root.type == bfd_link_hash_indirect)
b34976b6 8686 return TRUE;
5bd4f169 8687
65f38f15
AM
8688 info = (struct bfd_link_info *) inf;
8689 htab = ppc_hash_table (info);
4dfe6ac6
NC
8690 if (htab == NULL)
8691 return FALSE;
5bd4f169 8692
951fd09b
AM
8693 eh = (struct ppc_link_hash_entry *) h;
8694 /* Run through the TLS GD got entries first if we're changing them
8695 to TPREL. */
37da22e5 8696 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
951fd09b
AM
8697 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8698 if (gent->got.refcount > 0
8699 && (gent->tls_type & TLS_GD) != 0)
8700 {
8701 /* This was a GD entry that has been converted to TPREL. If
8702 there happens to be a TPREL entry we can use that one. */
8703 struct got_entry *ent;
8704 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8705 if (ent->got.refcount > 0
8706 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
8707 && ent->addend == gent->addend
8708 && ent->owner == gent->owner)
951fd09b
AM
8709 {
8710 gent->got.refcount = 0;
8711 break;
8712 }
8713
8714 /* If not, then we'll be using our own TPREL entry. */
8715 if (gent->got.refcount != 0)
8716 gent->tls_type = TLS_TLS | TLS_TPREL;
8717 }
8718
7865406b
AM
8719 /* Remove any list entry that won't generate a word in the GOT before
8720 we call merge_got_entries. Otherwise we risk merging to empty
8721 entries. */
0b8bcf0d
AM
8722 pgent = &h->got.glist;
8723 while ((gent = *pgent) != NULL)
411e1bfb 8724 if (gent->got.refcount > 0)
7865406b
AM
8725 {
8726 if ((gent->tls_type & TLS_LD) != 0
8727 && !h->def_dynamic)
8728 {
8729 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8730 *pgent = gent->next;
8731 }
8732 else
8733 pgent = &gent->next;
8734 }
8735 else
8736 *pgent = gent->next;
8737
8738 if (!htab->do_multi_toc)
8739 merge_got_entries (&h->got.glist);
8740
8741 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8742 if (!gent->is_indirect)
411e1bfb 8743 {
46434633
AM
8744 /* Make sure this symbol is output as a dynamic symbol. */
8745 if (!ensure_undef_dynamic (info, h))
f0158f44 8746 return FALSE;
65f38f15 8747
0c8d6e5c 8748 if (!is_ppc64_elf (gent->owner))
927be08e 8749 abort ();
0ffa91dd 8750
927be08e 8751 allocate_got (h, info, gent);
411e1bfb 8752 }
65f38f15 8753
954b63d4
AM
8754 /* If no dynamic sections we can't have dynamic relocs, except for
8755 IFUNCs which are handled even in static executables. */
8a2058b5
AM
8756 if (!htab->elf.dynamic_sections_created
8757 && h->type != STT_GNU_IFUNC)
8758 eh->dyn_relocs = NULL;
8759
529fe20e
AM
8760 /* Discard relocs on undefined symbols that must be local. */
8761 else if (h->root.type == bfd_link_hash_undefined
8762 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8763 eh->dyn_relocs = NULL;
8764
954b63d4
AM
8765 /* Also discard relocs on undefined weak syms with non-default
8766 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 8767 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
8768 eh->dyn_relocs = NULL;
8769
8a2058b5 8770 if (eh->dyn_relocs != NULL)
65f38f15 8771 {
8a2058b5
AM
8772 struct elf_dyn_relocs *p, **pp;
8773
57e7d118
AM
8774 /* In the shared -Bsymbolic case, discard space allocated for
8775 dynamic pc-relative relocs against symbols which turn out to
8776 be defined in regular objects. For the normal shared case,
8777 discard space for relocs that have become local due to symbol
8778 visibility changes. */
8779
8780 if (bfd_link_pic (info))
65f38f15 8781 {
57e7d118
AM
8782 /* Relocs that use pc_count are those that appear on a call
8783 insn, or certain REL relocs (see must_be_dyn_reloc) that
8784 can be generated via assembly. We want calls to
8785 protected symbols to resolve directly to the function
8786 rather than going via the plt. If people want function
8787 pointer comparisons to work as expected then they should
8788 avoid writing weird assembly. */
8789 if (SYMBOL_CALLS_LOCAL (info, h))
8790 {
57e7d118
AM
8791 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8792 {
8793 p->count -= p->pc_count;
8794 p->pc_count = 0;
8795 if (p->count == 0)
8796 *pp = p->next;
8797 else
8798 pp = &p->next;
8799 }
8800 }
65f38f15 8801
954b63d4 8802 if (eh->dyn_relocs != NULL)
5bd4f169 8803 {
46434633
AM
8804 /* Make sure this symbol is output as a dynamic symbol. */
8805 if (!ensure_undef_dynamic (info, h))
f0158f44 8806 return FALSE;
5bd4f169 8807 }
65f38f15 8808 }
529fe20e 8809 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 8810 {
8a2058b5 8811 /* For the non-pic case, discard space for relocs against
57e7d118
AM
8812 symbols which turn out to need copy relocs or are not
8813 dynamic. */
529fe20e
AM
8814 if (h->dynamic_adjusted
8815 && !h->def_regular
8816 && !ELF_COMMON_DEF_P (h))
f0158f44 8817 {
46434633
AM
8818 /* Make sure this symbol is output as a dynamic symbol. */
8819 if (!ensure_undef_dynamic (info, h))
f0158f44 8820 return FALSE;
dfbb6ac9 8821
f0158f44
AM
8822 if (h->dynindx == -1)
8823 eh->dyn_relocs = NULL;
8824 }
8825 else
8a2058b5 8826 eh->dyn_relocs = NULL;
57e7d118
AM
8827 }
8828
8829 /* Finally, allocate space. */
8830 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8831 {
8832 asection *sreloc = elf_section_data (p->sec)->sreloc;
8833 if (eh->elf.type == STT_GNU_IFUNC)
8834 sreloc = htab->elf.irelplt;
8835 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 8836 }
65f38f15 8837 }
57e7d118 8838
2d7ad24e
AM
8839 /* We might need a PLT entry when the symbol
8840 a) is dynamic, or
8841 b) is an ifunc, or
8842 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
8843 d) has plt16 relocs and we are linking statically. */
8844 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
8845 || h->type == STT_GNU_IFUNC
8846 || (h->needs_plt && h->dynamic_adjusted)
8847 || (h->needs_plt
8848 && h->def_regular
8849 && !htab->elf.dynamic_sections_created
3e04d765 8850 && !htab->can_convert_all_inline_plt
2d7ad24e
AM
8851 && (((struct ppc_link_hash_entry *) h)->tls_mask
8852 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 8853 {
57e7d118
AM
8854 struct plt_entry *pent;
8855 bfd_boolean doneone = FALSE;
8856 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8857 if (pent->plt.refcount > 0)
8858 {
8859 if (!htab->elf.dynamic_sections_created
8860 || h->dynindx == -1)
8861 {
2d7ad24e
AM
8862 if (h->type == STT_GNU_IFUNC)
8863 {
8864 s = htab->elf.iplt;
8865 pent->plt.offset = s->size;
8866 s->size += PLT_ENTRY_SIZE (htab);
8867 s = htab->elf.irelplt;
8868 }
8869 else
8870 {
8871 s = htab->pltlocal;
8872 pent->plt.offset = s->size;
8873 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
8874 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
8875 }
57e7d118
AM
8876 }
8877 else
8878 {
8879 /* If this is the first .plt entry, make room for the special
8880 first entry. */
8881 s = htab->elf.splt;
8882 if (s->size == 0)
8883 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 8884
57e7d118 8885 pent->plt.offset = s->size;
65f38f15 8886
57e7d118
AM
8887 /* Make room for this entry. */
8888 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 8889
57e7d118
AM
8890 /* Make room for the .glink code. */
8891 s = htab->glink;
8892 if (s->size == 0)
9e390558 8893 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
8894 if (htab->opd_abi)
8895 {
8896 /* We need bigger stubs past index 32767. */
9e390558 8897 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
8898 s->size += 4;
8899 s->size += 2*4;
8900 }
8901 else
8902 s->size += 4;
65f38f15 8903
57e7d118
AM
8904 /* We also need to make an entry in the .rela.plt section. */
8905 s = htab->elf.srelplt;
8906 }
2d7ad24e
AM
8907 if (s != NULL)
8908 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
8909 doneone = TRUE;
8910 }
8911 else
8912 pent->plt.offset = (bfd_vma) -1;
8913 if (!doneone)
8914 {
8915 h->plt.plist = NULL;
8916 h->needs_plt = 0;
8917 }
65f38f15 8918 }
57e7d118 8919 else
65f38f15 8920 {
57e7d118
AM
8921 h->plt.plist = NULL;
8922 h->needs_plt = 0;
65f38f15
AM
8923 }
8924
b34976b6 8925 return TRUE;
65f38f15
AM
8926}
8927
9e390558
AM
8928#define PPC_LO(v) ((v) & 0xffff)
8929#define PPC_HI(v) (((v) >> 16) & 0xffff)
8930#define PPC_HA(v) PPC_HI ((v) + 0x8000)
8931
a345bc8d
AM
8932/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
8933 to set up space for global entry stubs. These are put in glink,
8934 after the branch table. */
65f38f15 8935
b34976b6 8936static bfd_boolean
a345bc8d 8937size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 8938{
a345bc8d
AM
8939 struct bfd_link_info *info;
8940 struct ppc_link_hash_table *htab;
8941 struct plt_entry *pent;
9e390558 8942 asection *s, *plt;
65f38f15 8943
a345bc8d
AM
8944 if (h->root.type == bfd_link_hash_indirect)
8945 return TRUE;
65f38f15 8946
a345bc8d
AM
8947 if (!h->pointer_equality_needed)
8948 return TRUE;
65f38f15 8949
a345bc8d
AM
8950 if (h->def_regular)
8951 return TRUE;
65f38f15 8952
a345bc8d
AM
8953 info = inf;
8954 htab = ppc_hash_table (info);
8955 if (htab == NULL)
8956 return FALSE;
8957
9e390558
AM
8958 s = htab->global_entry;
8959 plt = htab->elf.splt;
a345bc8d
AM
8960 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8961 if (pent->plt.offset != (bfd_vma) -1
8962 && pent->addend == 0)
8963 {
afe397ea
AM
8964 /* For ELFv2, if this symbol is not defined in a regular file
8965 and we are not generating a shared library or pie, then we
8966 need to define the symbol in the executable on a call stub.
8967 This is to avoid text relocations. */
9e390558
AM
8968 bfd_vma off, stub_align, stub_off, stub_size;
8969 unsigned int align_power;
8970
8971 stub_size = 16;
8972 stub_off = s->size;
8973 if (htab->params->plt_stub_align >= 0)
8974 align_power = htab->params->plt_stub_align;
8975 else
8976 align_power = -htab->params->plt_stub_align;
8977 /* Setting section alignment is delayed until we know it is
8978 non-empty. Otherwise the .text output section will be
8979 aligned at least to plt_stub_align even when no global
8980 entry stubs are needed. */
8981 if (s->alignment_power < align_power)
8982 s->alignment_power = align_power;
8983 stub_align = (bfd_vma) 1 << align_power;
8984 if (htab->params->plt_stub_align >= 0
8985 || ((((stub_off + stub_size - 1) & -stub_align)
8986 - (stub_off & -stub_align))
8987 > ((stub_size - 1) & -stub_align)))
8988 stub_off = (stub_off + stub_align - 1) & -stub_align;
8989 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
8990 off -= stub_off + s->output_offset + s->output_section->vma;
8991 /* Note that for --plt-stub-align negative we have a possible
8992 dependency between stub offset and size. Break that
8993 dependency by assuming the max stub size when calculating
8994 the stub offset. */
8995 if (PPC_HA (off) == 0)
8996 stub_size -= 4;
8a2058b5 8997 h->root.type = bfd_link_hash_defined;
afe397ea 8998 h->root.u.def.section = s;
9e390558
AM
8999 h->root.u.def.value = stub_off;
9000 s->size = stub_off + stub_size;
a345bc8d
AM
9001 break;
9002 }
9003 return TRUE;
9004}
9005
9006/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9007 read-only sections. */
9008
9009static bfd_boolean
98bbb1b8 9010maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 9011{
98bbb1b8
AM
9012 asection *sec;
9013
a345bc8d
AM
9014 if (h->root.type == bfd_link_hash_indirect)
9015 return TRUE;
9016
98bbb1b8
AM
9017 sec = readonly_dynrelocs (h);
9018 if (sec != NULL)
a345bc8d 9019 {
98bbb1b8
AM
9020 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9021
9022 info->flags |= DF_TEXTREL;
2cdcc330
AM
9023 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9024 " in read-only section `%pA'\n"),
9025 sec->owner, h->root.root.string, sec);
a345bc8d
AM
9026
9027 /* Not an error, just cut short the traversal. */
9028 return FALSE;
65f38f15 9029 }
b34976b6 9030 return TRUE;
65f38f15
AM
9031}
9032
9033/* Set the sizes of the dynamic sections. */
9034
b34976b6 9035static bfd_boolean
ee67d69a 9036ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9037 struct bfd_link_info *info)
65f38f15
AM
9038{
9039 struct ppc_link_hash_table *htab;
9040 bfd *dynobj;
9041 asection *s;
b34976b6 9042 bfd_boolean relocs;
65f38f15 9043 bfd *ibfd;
7865406b 9044 struct got_entry *first_tlsld;
65f38f15
AM
9045
9046 htab = ppc_hash_table (info);
4dfe6ac6
NC
9047 if (htab == NULL)
9048 return FALSE;
9049
65f38f15
AM
9050 dynobj = htab->elf.dynobj;
9051 if (dynobj == NULL)
9052 abort ();
9053
9054 if (htab->elf.dynamic_sections_created)
9055 {
9056 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9057 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9058 {
3d4d4302 9059 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9060 if (s == NULL)
9061 abort ();
eea6121a 9062 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9063 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9064 }
9065 }
9066
9067 /* Set up .got offsets for local syms, and space for local dynamic
9068 relocs. */
c72f2fb2 9069 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9070 {
411e1bfb
AM
9071 struct got_entry **lgot_ents;
9072 struct got_entry **end_lgot_ents;
e054468f
AM
9073 struct plt_entry **local_plt;
9074 struct plt_entry **end_local_plt;
f961d9dd 9075 unsigned char *lgot_masks;
65f38f15
AM
9076 bfd_size_type locsymcount;
9077 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9078
0c8d6e5c 9079 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9080 continue;
9081
9082 for (s = ibfd->sections; s != NULL; s = s->next)
9083 {
19e08130 9084 struct ppc_dyn_relocs *p;
65f38f15 9085
6edfbbad 9086 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9087 {
ec338859
AM
9088 if (!bfd_is_abs_section (p->sec)
9089 && bfd_is_abs_section (p->sec->output_section))
9090 {
9091 /* Input section has been discarded, either because
9092 it is a copy of a linkonce section or due to
9093 linker script /DISCARD/, so we'll be discarding
9094 the relocs too. */
9095 }
248866a8 9096 else if (p->count != 0)
ec338859 9097 {
19e08130
AM
9098 asection *srel = elf_section_data (p->sec)->sreloc;
9099 if (p->ifunc)
33e44f2e 9100 srel = htab->elf.irelplt;
eea6121a 9101 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9102 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9103 info->flags |= DF_TEXTREL;
ec338859 9104 }
65f38f15
AM
9105 }
9106 }
9107
411e1bfb
AM
9108 lgot_ents = elf_local_got_ents (ibfd);
9109 if (!lgot_ents)
65f38f15
AM
9110 continue;
9111
0ffa91dd 9112 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9113 locsymcount = symtab_hdr->sh_info;
411e1bfb 9114 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9115 local_plt = (struct plt_entry **) end_lgot_ents;
9116 end_local_plt = local_plt + locsymcount;
f961d9dd 9117 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9118 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9119 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9120 {
0b8bcf0d 9121 struct got_entry **pent, *ent;
411e1bfb 9122
0b8bcf0d
AM
9123 pent = lgot_ents;
9124 while ((ent = *pent) != NULL)
411e1bfb
AM
9125 if (ent->got.refcount > 0)
9126 {
e7b938ca 9127 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9128 {
927be08e 9129 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9130 *pent = ent->next;
411e1bfb
AM
9131 }
9132 else
9133 {
19e08130
AM
9134 unsigned int ent_size = 8;
9135 unsigned int rel_size = sizeof (Elf64_External_Rela);
9136
eea6121a 9137 ent->got.offset = s->size;
e7b938ca 9138 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9139 {
19e08130
AM
9140 ent_size *= 2;
9141 rel_size *= 2;
9142 }
9143 s->size += ent_size;
37da22e5 9144 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 9145 {
33e44f2e 9146 htab->elf.irelplt->size += rel_size;
19e08130
AM
9147 htab->got_reli_size += rel_size;
9148 }
f15d0b54
AM
9149 else if (bfd_link_pic (info)
9150 && !((ent->tls_type & TLS_TPREL) != 0
9151 && bfd_link_executable (info)))
19e08130
AM
9152 {
9153 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9154 srel->size += rel_size;
927be08e 9155 }
0b8bcf0d 9156 pent = &ent->next;
411e1bfb
AM
9157 }
9158 }
9159 else
0b8bcf0d 9160 *pent = ent->next;
65f38f15 9161 }
e054468f 9162
2d7ad24e
AM
9163 /* Allocate space for plt calls to local syms. */
9164 lgot_masks = (unsigned char *) end_local_plt;
9165 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
9166 {
9167 struct plt_entry *ent;
9168
9169 for (ent = *local_plt; ent != NULL; ent = ent->next)
9170 if (ent->plt.refcount > 0)
9171 {
2d7ad24e
AM
9172 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9173 {
9174 s = htab->elf.iplt;
9175 ent->plt.offset = s->size;
9176 s->size += PLT_ENTRY_SIZE (htab);
9177 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9178 }
3e04d765
AM
9179 else if (htab->can_convert_all_inline_plt
9180 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
9181 ent->plt.offset = (bfd_vma) -1;
9182 else
9183 {
9184 s = htab->pltlocal;
9185 ent->plt.offset = s->size;
9186 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9187 if (bfd_link_pic (info))
9188 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9189 }
e054468f
AM
9190 }
9191 else
9192 ent->plt.offset = (bfd_vma) -1;
9193 }
65f38f15
AM
9194 }
9195
9196 /* Allocate global sym .plt and .got entries, and space for global
9197 sym dynamic relocs. */
4ce794b7 9198 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 9199
0e1862bb 9200 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 9201 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 9202
7865406b 9203 first_tlsld = NULL;
c72f2fb2 9204 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 9205 {
7865406b
AM
9206 struct got_entry *ent;
9207
0c8d6e5c 9208 if (!is_ppc64_elf (ibfd))
102890f0
AM
9209 continue;
9210
7865406b
AM
9211 ent = ppc64_tlsld_got (ibfd);
9212 if (ent->got.refcount > 0)
102890f0 9213 {
7865406b 9214 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9215 {
7865406b
AM
9216 ent->is_indirect = TRUE;
9217 ent->got.ent = first_tlsld;
9218 }
9219 else
9220 {
9221 if (first_tlsld == NULL)
9222 first_tlsld = ent;
9223 s = ppc64_elf_tdata (ibfd)->got;
9224 ent->got.offset = s->size;
9225 ent->owner = ibfd;
9226 s->size += 16;
0e1862bb 9227 if (bfd_link_pic (info))
7865406b
AM
9228 {
9229 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9230 srel->size += sizeof (Elf64_External_Rela);
9231 }
102890f0
AM
9232 }
9233 }
9234 else
7865406b 9235 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9236 }
9237
65f38f15
AM
9238 /* We now have determined the sizes of the various dynamic sections.
9239 Allocate memory for them. */
b34976b6 9240 relocs = FALSE;
65f38f15
AM
9241 for (s = dynobj->sections; s != NULL; s = s->next)
9242 {
9243 if ((s->flags & SEC_LINKER_CREATED) == 0)
9244 continue;
9245
4ce794b7 9246 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9247 /* These haven't been allocated yet; don't strip. */
9248 continue;
33e44f2e
AM
9249 else if (s == htab->elf.sgot
9250 || s == htab->elf.splt
9251 || s == htab->elf.iplt
2d7ad24e 9252 || s == htab->pltlocal
c456f082 9253 || s == htab->glink
9e390558 9254 || s == htab->global_entry
5474d94f
AM
9255 || s == htab->elf.sdynbss
9256 || s == htab->elf.sdynrelro)
65f38f15
AM
9257 {
9258 /* Strip this section if we don't need it; see the
9259 comment below. */
5bd4f169 9260 }
58d180e8
AM
9261 else if (s == htab->glink_eh_frame)
9262 {
9263 if (!bfd_is_abs_section (s->output_section))
9264 /* Not sized yet. */
9265 continue;
9266 }
70cc837d 9267 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9268 {
c456f082 9269 if (s->size != 0)
5bd4f169 9270 {
33e44f2e 9271 if (s != htab->elf.srelplt)
b34976b6 9272 relocs = TRUE;
5bd4f169
AM
9273
9274 /* We use the reloc_count field as a counter if we need
9275 to copy relocs into the output file. */
9276 s->reloc_count = 0;
9277 }
9278 }
65f38f15 9279 else
5bd4f169
AM
9280 {
9281 /* It's not one of our sections, so don't allocate space. */
9282 continue;
9283 }
9284
eea6121a 9285 if (s->size == 0)
5bd4f169 9286 {
c456f082
AM
9287 /* If we don't need this section, strip it from the
9288 output file. This is mostly to handle .rela.bss and
9289 .rela.plt. We must create both sections in
9290 create_dynamic_sections, because they must be created
9291 before the linker maps input sections to output
9292 sections. The linker does that before
9293 adjust_dynamic_symbol is called, and it is that
9294 function which decides whether anything needs to go
9295 into these sections. */
8423293d 9296 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9297 continue;
9298 }
9299
06bcf541
AM
9300 if (bfd_is_abs_section (s->output_section))
9301 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9302 s->name);
9303
c456f082 9304 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9305 continue;
9306
65f38f15
AM
9307 /* Allocate memory for the section contents. We use bfd_zalloc
9308 here in case unused entries are not reclaimed before the
9309 section's contents are written out. This should not happen,
411e1bfb
AM
9310 but this way if it does we get a R_PPC64_NONE reloc in .rela
9311 sections instead of garbage.
9312 We also rely on the section contents being zero when writing
5474d94f 9313 the GOT and .dynrelro. */
eea6121a 9314 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9315 if (s->contents == NULL)
b34976b6 9316 return FALSE;
5bd4f169
AM
9317 }
9318
c72f2fb2 9319 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 9320 {
0c8d6e5c 9321 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9322 continue;
9323
e717da7e 9324 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 9325 if (s != NULL && s != htab->elf.sgot)
e717da7e 9326 {
eea6121a 9327 if (s->size == 0)
8423293d 9328 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9329 else
9330 {
eea6121a 9331 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9332 if (s->contents == NULL)
9333 return FALSE;
9334 }
9335 }
9336 s = ppc64_elf_tdata (ibfd)->relgot;
9337 if (s != NULL)
9338 {
eea6121a 9339 if (s->size == 0)
8423293d 9340 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9341 else
9342 {
eea6121a 9343 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9344 if (s->contents == NULL)
9345 return FALSE;
9346 relocs = TRUE;
9347 s->reloc_count = 0;
9348 }
9349 }
9350 }
9351
e86ce104 9352 if (htab->elf.dynamic_sections_created)
5bd4f169 9353 {
e8910a83
AM
9354 bfd_boolean tls_opt;
9355
5bd4f169
AM
9356 /* Add some entries to the .dynamic section. We fill in the
9357 values later, in ppc64_elf_finish_dynamic_sections, but we
9358 must add the entries now so that we get the correct size for
9359 the .dynamic section. The DT_DEBUG entry is filled in by the
9360 dynamic linker and used by the debugger. */
dc810e39 9361#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9362 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9363
0e1862bb 9364 if (bfd_link_executable (info))
5bd4f169 9365 {
dc810e39 9366 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9367 return FALSE;
5bd4f169
AM
9368 }
9369
33e44f2e 9370 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 9371 {
dc810e39
AM
9372 if (!add_dynamic_entry (DT_PLTGOT, 0)
9373 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9374 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9375 || !add_dynamic_entry (DT_JMPREL, 0)
9376 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9377 return FALSE;
5bd4f169
AM
9378 }
9379
ee67d69a 9380 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
9381 {
9382 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9383 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9384 return FALSE;
19397422
AM
9385 }
9386
7c9cf415 9387 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
9388 && htab->tls_get_addr_fd != NULL
9389 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9390 if (tls_opt || !htab->opd_abi)
9391 {
9392 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9393 return FALSE;
9394 }
a7f2871e 9395
5bd4f169
AM
9396 if (relocs)
9397 {
dc810e39
AM
9398 if (!add_dynamic_entry (DT_RELA, 0)
9399 || !add_dynamic_entry (DT_RELASZ, 0)
9400 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9401 return FALSE;
5bd4f169 9402
65f38f15
AM
9403 /* If any dynamic relocs apply to a read-only section,
9404 then we need a DT_TEXTREL entry. */
248866a8 9405 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 9406 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 9407
65f38f15 9408 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9409 {
65f38f15 9410 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9411 return FALSE;
5bd4f169 9412 }
5bd4f169 9413 }
5bd4f169 9414 }
65f38f15 9415#undef add_dynamic_entry
5bd4f169 9416
b34976b6 9417 return TRUE;
5bd4f169
AM
9418}
9419
a345bc8d
AM
9420/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
9421
9422static bfd_boolean
9423ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9424{
9425 if (h->plt.plist != NULL
9426 && !h->def_regular
9427 && !h->pointer_equality_needed)
9428 return FALSE;
9429
9430 return _bfd_elf_hash_symbol (h);
9431}
9432
721956f4 9433/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9434
4ce794b7
AM
9435static inline enum ppc_stub_type
9436ppc_type_of_stub (asection *input_sec,
9437 const Elf_Internal_Rela *rel,
9438 struct ppc_link_hash_entry **hash,
e054468f 9439 struct plt_entry **plt_ent,
6911b7dc
AM
9440 bfd_vma destination,
9441 unsigned long local_off)
5bd4f169 9442{
721956f4
AM
9443 struct ppc_link_hash_entry *h = *hash;
9444 bfd_vma location;
9445 bfd_vma branch_offset;
9446 bfd_vma max_branch_offset;
4ce794b7 9447 enum elf_ppc64_reloc_type r_type;
5bd4f169 9448
721956f4
AM
9449 if (h != NULL)
9450 {
e054468f 9451 struct plt_entry *ent;
7fe2b9a6 9452 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
9453 if (h->oh != NULL
9454 && h->oh->is_func_descriptor)
7b8f6675
AM
9455 {
9456 fdh = ppc_follow_link (h->oh);
9457 *hash = fdh;
9458 }
8387904d 9459
e054468f
AM
9460 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9461 if (ent->addend == rel->r_addend
9462 && ent->plt.offset != (bfd_vma) -1)
9463 {
e054468f
AM
9464 *plt_ent = ent;
9465 return ppc_stub_plt_call;
9466 }
5bd4f169 9467
7fe2b9a6
AM
9468 /* Here, we know we don't have a plt entry. If we don't have a
9469 either a defined function descriptor or a defined entry symbol
9470 in a regular object file, then it is pointless trying to make
9471 any other type of stub. */
854b41e7
AM
9472 if (!is_static_defined (&fdh->elf)
9473 && !is_static_defined (&h->elf))
721956f4 9474 return ppc_stub_none;
5d1634d7 9475 }
e054468f
AM
9476 else if (elf_local_got_ents (input_sec->owner) != NULL)
9477 {
9478 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9479 struct plt_entry **local_plt = (struct plt_entry **)
9480 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9481 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9482
9483 if (local_plt[r_symndx] != NULL)
9484 {
9485 struct plt_entry *ent;
9486
9487 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9488 if (ent->addend == rel->r_addend
9489 && ent->plt.offset != (bfd_vma) -1)
9490 {
9491 *plt_ent = ent;
9492 return ppc_stub_plt_call;
9493 }
9494 }
9495 }
5d1634d7 9496
721956f4
AM
9497 /* Determine where the call point is. */
9498 location = (input_sec->output_offset
9499 + input_sec->output_section->vma
9500 + rel->r_offset);
5d1634d7 9501
721956f4
AM
9502 branch_offset = destination - location;
9503 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 9504
721956f4
AM
9505 /* Determine if a long branch stub is needed. */
9506 max_branch_offset = 1 << 25;
23cedd1d
AM
9507 if (r_type == R_PPC64_REL14
9508 || r_type == R_PPC64_REL14_BRTAKEN
9509 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 9510 max_branch_offset = 1 << 15;
5d1634d7 9511
6911b7dc 9512 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
9513 /* We need a stub. Figure out whether a long_branch or plt_branch
9514 is needed later. */
9515 return ppc_stub_long_branch;
5d1634d7 9516
721956f4 9517 return ppc_stub_none;
5d1634d7
AM
9518}
9519
f891966f
AM
9520/* Gets the address of a label (1:) in r11 and builds an offset in r12,
9521 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
9522 . mflr %r12
9523 . bcl 20,31,1f
9524 .1: mflr %r11
9525 . mtlr %r12
05d0e962 9526 . lis %r12,xxx-1b@highest
f891966f 9527 . ori %r12,%r12,xxx-1b@higher
05d0e962 9528 . sldi %r12,%r12,32
f891966f 9529 . oris %r12,%r12,xxx-1b@high
05d0e962 9530 . ori %r12,%r12,xxx-1b@l
f891966f 9531 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
9532
9533static bfd_byte *
9534build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
9535{
f891966f
AM
9536 bfd_put_32 (abfd, MFLR_R12, p);
9537 p += 4;
9538 bfd_put_32 (abfd, BCL_20_31, p);
9539 p += 4;
9540 bfd_put_32 (abfd, MFLR_R11, p);
9541 p += 4;
9542 bfd_put_32 (abfd, MTLR_R12, p);
9543 p += 4;
05d0e962
AM
9544 if (off + 0x8000 < 0x10000)
9545 {
9546 if (load)
9547 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
9548 else
9549 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
9550 p += 4;
9551 }
9552 else if (off + 0x80008000ULL < 0x100000000ULL)
9553 {
9554 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
9555 p += 4;
9556 if (load)
9557 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
9558 else
9559 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
9560 p += 4;
9561 }
9562 else
9563 {
9564 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9565 {
9566 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
9567 p += 4;
9568 }
9569 else
9570 {
9571 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
9572 p += 4;
9573 if (((off >> 32) & 0xffff) != 0)
9574 {
9575 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
9576 p += 4;
9577 }
9578 }
9579 if (((off >> 32) & 0xffffffffULL) != 0)
9580 {
9581 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
9582 p += 4;
9583 }
9584 if (PPC_HI (off) != 0)
9585 {
9586 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
9587 p += 4;
9588 }
9589 if (PPC_LO (off) != 0)
9590 {
9591 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
9592 p += 4;
9593 }
9594 if (load)
9595 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
9596 else
9597 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
9598 p += 4;
9599 }
9600 return p;
9601}
9602
9603static unsigned int
9604size_offset (bfd_vma off)
9605{
9606 unsigned int size;
9607 if (off + 0x8000 < 0x10000)
9608 size = 4;
9609 else if (off + 0x80008000ULL < 0x100000000ULL)
9610 size = 8;
9611 else
9612 {
9613 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9614 size = 4;
9615 else
9616 {
9617 size = 4;
9618 if (((off >> 32) & 0xffff) != 0)
9619 size += 4;
9620 }
9621 if (((off >> 32) & 0xffffffffULL) != 0)
9622 size += 4;
9623 if (PPC_HI (off) != 0)
9624 size += 4;
9625 if (PPC_LO (off) != 0)
9626 size += 4;
9627 size += 4;
9628 }
f891966f 9629 return size + 16;
05d0e962
AM
9630}
9631
3d58e1fc
AM
9632static unsigned int
9633num_relocs_for_offset (bfd_vma off)
9634{
9635 unsigned int num_rel;
9636 if (off + 0x8000 < 0x10000)
9637 num_rel = 1;
9638 else if (off + 0x80008000ULL < 0x100000000ULL)
9639 num_rel = 2;
9640 else
9641 {
9642 num_rel = 1;
9643 if (off + 0x800000000000ULL >= 0x1000000000000ULL
9644 && ((off >> 32) & 0xffff) != 0)
9645 num_rel += 1;
9646 if (PPC_HI (off) != 0)
9647 num_rel += 1;
9648 if (PPC_LO (off) != 0)
9649 num_rel += 1;
9650 }
9651 return num_rel;
9652}
9653
9654static Elf_Internal_Rela *
9655emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
9656 bfd_vma roff, bfd_vma targ, bfd_vma off)
9657{
9658 bfd_vma relative_targ = targ - (roff - 8);
9659 if (bfd_big_endian (info->output_bfd))
9660 roff += 2;
9661 r->r_offset = roff;
9662 r->r_addend = relative_targ + roff;
9663 if (off + 0x8000 < 0x10000)
9664 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
9665 else if (off + 0x80008000ULL < 0x100000000ULL)
9666 {
9667 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
9668 ++r;
9669 roff += 4;
9670 r->r_offset = roff;
9671 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
9672 r->r_addend = relative_targ + roff;
9673 }
9674 else
9675 {
9676 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9677 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
9678 else
9679 {
9680 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
9681 if (((off >> 32) & 0xffff) != 0)
9682 {
9683 ++r;
9684 roff += 4;
9685 r->r_offset = roff;
9686 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
9687 r->r_addend = relative_targ + roff;
9688 }
9689 }
9690 if (((off >> 32) & 0xffffffffULL) != 0)
9691 roff += 4;
9692 if (PPC_HI (off) != 0)
9693 {
9694 ++r;
9695 roff += 4;
9696 r->r_offset = roff;
9697 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
9698 r->r_addend = relative_targ + roff;
9699 }
9700 if (PPC_LO (off) != 0)
9701 {
9702 ++r;
9703 roff += 4;
9704 r->r_offset = roff;
9705 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
9706 r->r_addend = relative_targ + roff;
9707 }
9708 }
9709 return r;
9710}
9711
df136d64
AM
9712/* Emit .eh_frame opcode to advance pc by DELTA. */
9713
9714static bfd_byte *
9715eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
9716{
9717 delta /= 4;
9718 if (delta < 64)
9719 *eh++ = DW_CFA_advance_loc + delta;
9720 else if (delta < 256)
9721 {
9722 *eh++ = DW_CFA_advance_loc1;
9723 *eh++ = delta;
9724 }
9725 else if (delta < 65536)
9726 {
9727 *eh++ = DW_CFA_advance_loc2;
9728 bfd_put_16 (abfd, delta, eh);
9729 eh += 2;
9730 }
9731 else
9732 {
9733 *eh++ = DW_CFA_advance_loc4;
9734 bfd_put_32 (abfd, delta, eh);
9735 eh += 4;
9736 }
9737 return eh;
9738}
9739
9740/* Size of required .eh_frame opcode to advance pc by DELTA. */
9741
9742static unsigned int
9743eh_advance_size (unsigned int delta)
9744{
9745 if (delta < 64 * 4)
9746 /* DW_CFA_advance_loc+[1..63]. */
9747 return 1;
9748 if (delta < 256 * 4)
9749 /* DW_CFA_advance_loc1, byte. */
9750 return 2;
9751 if (delta < 65536 * 4)
9752 /* DW_CFA_advance_loc2, 2 bytes. */
9753 return 3;
9754 /* DW_CFA_advance_loc4, 4 bytes. */
9755 return 5;
9756}
9757
794e51c0
AM
9758/* With power7 weakly ordered memory model, it is possible for ld.so
9759 to update a plt entry in one thread and have another thread see a
9760 stale zero toc entry. To avoid this we need some sort of acquire
9761 barrier in the call stub. One solution is to make the load of the
9762 toc word seem to appear to depend on the load of the function entry
9763 word. Another solution is to test for r2 being zero, and branch to
9764 the appropriate glink entry if so.
9765
9766 . fake dep barrier compare
71a39c98
AM
9767 . ld 12,xxx(2) ld 12,xxx(2)
9768 . mtctr 12 mtctr 12
9769 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
9770 . add 2,2,11 cmpldi 2,0
9771 . ld 2,xxx+8(2) bnectr+
9772 . bctr b <glink_entry>
9773
9774 The solution involving the compare turns out to be faster, so
9775 that's what we use unless the branch won't reach. */
9776
9777#define ALWAYS_USE_FAKE_DEP 0
9778#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 9779
794e51c0
AM
9780static inline unsigned int
9781plt_stub_size (struct ppc_link_hash_table *htab,
9782 struct ppc_stub_hash_entry *stub_entry,
9783 bfd_vma off)
9784{
05d0e962 9785 unsigned size;
b9e5796b 9786
05d0e962
AM
9787 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
9788 {
f891966f 9789 size = 8 + size_offset (off - 8);
05d0e962
AM
9790 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
9791 size += 4;
9792 return size;
9793 }
9794
9795 size = 12;
b9e5796b
AM
9796 if (ALWAYS_EMIT_R2SAVE
9797 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9798 size += 4;
9799 if (PPC_HA (off) != 0)
794e51c0 9800 size += 4;
b9e5796b
AM
9801 if (htab->opd_abi)
9802 {
9803 size += 4;
e7d1c40c 9804 if (htab->params->plt_static_chain)
b9e5796b 9805 size += 4;
bd4d2eaa
AM
9806 if (htab->params->plt_thread_safe
9807 && htab->elf.dynamic_sections_created
9808 && stub_entry->h != NULL
9809 && stub_entry->h->elf.dynindx != -1)
b9e5796b 9810 size += 8;
e7d1c40c 9811 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
9812 size += 4;
9813 }
794e51c0
AM
9814 if (stub_entry->h != NULL
9815 && (stub_entry->h == htab->tls_get_addr_fd
9816 || stub_entry->h == htab->tls_get_addr)
7c9cf415 9817 && htab->params->tls_get_addr_opt)
f378ab09
AM
9818 {
9819 size += 7 * 4;
e81b4c93 9820 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 9821 size += 6 * 4;
f378ab09 9822 }
794e51c0
AM
9823 return size;
9824}
9825
2420fff6
AM
9826/* Depending on the sign of plt_stub_align:
9827 If positive, return the padding to align to a 2**plt_stub_align
9828 boundary.
9829 If negative, if this stub would cross fewer 2**plt_stub_align
9830 boundaries if we align, then return the padding needed to do so. */
9831
794e51c0
AM
9832static inline unsigned int
9833plt_stub_pad (struct ppc_link_hash_table *htab,
9834 struct ppc_stub_hash_entry *stub_entry,
9835 bfd_vma plt_off)
9836{
2420fff6 9837 int stub_align;
1aa42141 9838 unsigned stub_size;
6f20ed8a 9839 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 9840
2420fff6
AM
9841 if (htab->params->plt_stub_align >= 0)
9842 {
9843 stub_align = 1 << htab->params->plt_stub_align;
9844 if ((stub_off & (stub_align - 1)) != 0)
9845 return stub_align - (stub_off & (stub_align - 1));
9846 return 0;
9847 }
9848
9849 stub_align = 1 << -htab->params->plt_stub_align;
1aa42141 9850 stub_size = plt_stub_size (htab, stub_entry, plt_off);
794e51c0 9851 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 9852 > ((stub_size - 1) & -stub_align))
794e51c0
AM
9853 return stub_align - (stub_off & (stub_align - 1));
9854 return 0;
9855}
9856
9857/* Build a .plt call stub. */
9858
9859static inline bfd_byte *
9860build_plt_stub (struct ppc_link_hash_table *htab,
9861 struct ppc_stub_hash_entry *stub_entry,
9862 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9863{
e7d1c40c 9864 bfd *obfd = htab->params->stub_bfd;
b9e5796b 9865 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 9866 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
9867 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
9868 && htab->elf.dynamic_sections_created
9869 && stub_entry->h != NULL
9870 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
9871 bfd_boolean use_fake_dep = plt_thread_safe;
9872 bfd_vma cmp_branch_off = 0;
9873
9874 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 9875 && plt_load_toc
794e51c0 9876 && plt_thread_safe
bd4d2eaa
AM
9877 && !((stub_entry->h == htab->tls_get_addr_fd
9878 || stub_entry->h == htab->tls_get_addr)
7c9cf415 9879 && htab->params->tls_get_addr_opt))
794e51c0
AM
9880 {
9881 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
9882 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
9883 / PLT_ENTRY_SIZE (htab));
9e390558 9884 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
9885 bfd_vma to, from;
9886
68d62958
AM
9887 if (pltindex > 32768)
9888 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
9889 to = (glinkoff
9890 + htab->glink->output_offset
9891 + htab->glink->output_section->vma);
6f20ed8a 9892 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
9893 + 4 * (ALWAYS_EMIT_R2SAVE
9894 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9895 + 4 * (PPC_HA (offset) != 0)
9896 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
9897 != PPC_HA (offset))
9898 + 4 * (plt_static_chain != 0)
9899 + 20
6f20ed8a
AM
9900 + stub_entry->group->stub_sec->output_offset
9901 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
9902 cmp_branch_off = to - from;
9903 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
9904 }
9905
ac2df442
AM
9906 if (PPC_HA (offset) != 0)
9907 {
176a0d42
AM
9908 if (r != NULL)
9909 {
794e51c0
AM
9910 if (ALWAYS_EMIT_R2SAVE
9911 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9912 r[0].r_offset += 4;
176a0d42 9913 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 9914 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
9915 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9916 r[1].r_addend = r[0].r_addend;
b9e5796b 9917 if (plt_load_toc)
176a0d42 9918 {
b9e5796b 9919 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 9920 {
b9e5796b
AM
9921 r[2].r_offset = r[1].r_offset + 4;
9922 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9923 r[2].r_addend = r[0].r_addend;
9924 }
9925 else
9926 {
9927 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
9928 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9929 r[2].r_addend = r[0].r_addend + 8;
9930 if (plt_static_chain)
9931 {
9932 r[3].r_offset = r[2].r_offset + 4;
9933 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9934 r[3].r_addend = r[0].r_addend + 16;
9935 }
c7131b65 9936 }
176a0d42
AM
9937 }
9938 }
794e51c0
AM
9939 if (ALWAYS_EMIT_R2SAVE
9940 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 9941 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
9942 if (plt_load_toc)
9943 {
9944 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
9945 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
9946 }
9947 else
9948 {
9949 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9950 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
9951 }
b9e5796b
AM
9952 if (plt_load_toc
9953 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 9954 {
71a39c98 9955 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
9956 offset = 0;
9957 }
71a39c98 9958 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 9959 if (plt_load_toc)
794e51c0 9960 {
b9e5796b
AM
9961 if (use_fake_dep)
9962 {
9963 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
9964 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
9965 }
9966 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
9967 if (plt_static_chain)
9968 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 9969 }
ac2df442
AM
9970 }
9971 else
9972 {
176a0d42
AM
9973 if (r != NULL)
9974 {
794e51c0
AM
9975 if (ALWAYS_EMIT_R2SAVE
9976 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9977 r[0].r_offset += 4;
176a0d42 9978 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 9979 if (plt_load_toc)
176a0d42 9980 {
b9e5796b 9981 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 9982 {
b9e5796b
AM
9983 r[1].r_offset = r[0].r_offset + 4;
9984 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9985 r[1].r_addend = r[0].r_addend;
9986 }
9987 else
9988 {
9989 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
9990 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9991 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
9992 if (plt_static_chain)
9993 {
9994 r[2].r_offset = r[1].r_offset + 4;
9995 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9996 r[2].r_addend = r[0].r_addend + 8;
9997 }
c7131b65 9998 }
176a0d42
AM
9999 }
10000 }
794e51c0
AM
10001 if (ALWAYS_EMIT_R2SAVE
10002 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10003 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10004 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10005 if (plt_load_toc
10006 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10007 {
10008 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10009 offset = 0;
10010 }
71a39c98 10011 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10012 if (plt_load_toc)
794e51c0 10013 {
b9e5796b
AM
10014 if (use_fake_dep)
10015 {
10016 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10017 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10018 }
10019 if (plt_static_chain)
10020 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10021 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10022 }
ac2df442 10023 }
b9e5796b 10024 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10025 {
10026 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10027 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10028 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10029 }
10030 else
407aa07c 10031 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10032 return p;
10033}
10034
a7f2871e
AM
10035/* Build a special .plt call stub for __tls_get_addr. */
10036
10037#define LD_R11_0R3 0xe9630000
10038#define LD_R12_0R3 0xe9830000
10039#define MR_R0_R3 0x7c601b78
10040#define CMPDI_R11_0 0x2c2b0000
10041#define ADD_R3_R12_R13 0x7c6c6a14
10042#define BEQLR 0x4d820020
10043#define MR_R3_R0 0x7c030378
a7f2871e
AM
10044#define STD_R11_0R1 0xf9610000
10045#define BCTRL 0x4e800421
10046#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10047#define MTLR_R11 0x7d6803a6
10048
10049static inline bfd_byte *
794e51c0
AM
10050build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10051 struct ppc_stub_hash_entry *stub_entry,
10052 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10053{
e7d1c40c 10054 bfd *obfd = htab->params->stub_bfd;
df136d64 10055 bfd_byte *loc = p;
794e51c0 10056
a7f2871e
AM
10057 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10058 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10059 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10060 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10061 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10062 bfd_put_32 (obfd, BEQLR, p), p += 4;
10063 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
10064 if (r != NULL)
10065 r[0].r_offset += 7 * 4;
e81b4c93 10066 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
f378ab09
AM
10067 return build_plt_stub (htab, stub_entry, p, offset, r);
10068
a7f2871e 10069 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10070 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10071
10072 if (r != NULL)
f378ab09 10073 r[0].r_offset += 2 * 4;
794e51c0 10074 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 10075 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 10076
a078d95a 10077 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10078 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10079 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10080 bfd_put_32 (obfd, BLR, p), p += 4;
10081
df136d64
AM
10082 if (htab->glink_eh_frame != NULL
10083 && htab->glink_eh_frame->size != 0)
10084 {
10085 bfd_byte *base, *eh;
10086 unsigned int lr_used, delta;
10087
10088 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10089 eh = base + stub_entry->group->eh_size;
10090 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10091 delta = lr_used - stub_entry->group->lr_restore;
10092 stub_entry->group->lr_restore = lr_used + 16;
10093 eh = eh_advance (htab->elf.dynobj, eh, delta);
10094 *eh++ = DW_CFA_offset_extended_sf;
10095 *eh++ = 65;
10096 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10097 *eh++ = DW_CFA_advance_loc + 4;
10098 *eh++ = DW_CFA_restore_extended;
10099 *eh++ = 65;
10100 stub_entry->group->eh_size = eh - base;
10101 }
a7f2871e
AM
10102 return p;
10103}
10104
176a0d42
AM
10105static Elf_Internal_Rela *
10106get_relocs (asection *sec, int count)
10107{
10108 Elf_Internal_Rela *relocs;
10109 struct bfd_elf_section_data *elfsec_data;
10110
10111 elfsec_data = elf_section_data (sec);
10112 relocs = elfsec_data->relocs;
10113 if (relocs == NULL)
10114 {
10115 bfd_size_type relsize;
10116 relsize = sec->reloc_count * sizeof (*relocs);
10117 relocs = bfd_alloc (sec->owner, relsize);
10118 if (relocs == NULL)
10119 return NULL;
10120 elfsec_data->relocs = relocs;
d4730f92
BS
10121 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10122 sizeof (Elf_Internal_Shdr));
10123 if (elfsec_data->rela.hdr == NULL)
10124 return NULL;
10125 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10126 * sizeof (Elf64_External_Rela));
10127 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10128 sec->reloc_count = 0;
10129 }
10130 relocs += sec->reloc_count;
10131 sec->reloc_count += count;
10132 return relocs;
10133}
10134
3d58e1fc
AM
10135/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10136 forms, to the equivalent relocs against the global symbol given by
10137 STUB_ENTRY->H. */
10138
10139static bfd_boolean
10140use_global_in_relocs (struct ppc_link_hash_table *htab,
10141 struct ppc_stub_hash_entry *stub_entry,
10142 Elf_Internal_Rela *r, unsigned int num_rel)
10143{
10144 struct elf_link_hash_entry **hashes;
10145 unsigned long symndx;
10146 struct ppc_link_hash_entry *h;
10147 bfd_vma symval;
10148
10149 /* Relocs are always against symbols in their own object file. Fake
10150 up global sym hashes for the stub bfd (which has no symbols). */
10151 hashes = elf_sym_hashes (htab->params->stub_bfd);
10152 if (hashes == NULL)
10153 {
10154 bfd_size_type hsize;
10155
10156 /* When called the first time, stub_globals will contain the
10157 total number of symbols seen during stub sizing. After
10158 allocating, stub_globals is used as an index to fill the
10159 hashes array. */
10160 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10161 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10162 if (hashes == NULL)
10163 return FALSE;
10164 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10165 htab->stub_globals = 1;
10166 }
10167 symndx = htab->stub_globals++;
10168 h = stub_entry->h;
10169 hashes[symndx] = &h->elf;
10170 if (h->oh != NULL && h->oh->is_func)
10171 h = ppc_follow_link (h->oh);
10172 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10173 || h->elf.root.type == bfd_link_hash_defweak);
10174 symval = (h->elf.root.u.def.value
10175 + h->elf.root.u.def.section->output_offset
10176 + h->elf.root.u.def.section->output_section->vma);
10177 while (num_rel-- != 0)
10178 {
10179 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10180 if (h->elf.root.u.def.section != stub_entry->target_section)
10181 {
10182 /* H is an opd symbol. The addend must be zero, and the
10183 branch reloc is the only one we can convert. */
10184 r->r_addend = 0;
10185 break;
10186 }
10187 else
10188 r->r_addend -= symval;
10189 --r;
10190 }
10191 return TRUE;
10192}
10193
aa374f67 10194static bfd_vma
25f53a85 10195get_r2off (struct bfd_link_info *info,
aa374f67
AM
10196 struct ppc_stub_hash_entry *stub_entry)
10197{
25f53a85 10198 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10199 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10200
10201 if (r2off == 0)
10202 {
10203 /* Support linking -R objects. Get the toc pointer from the
10204 opd entry. */
10205 char buf[8];
b9e5796b
AM
10206 if (!htab->opd_abi)
10207 return r2off;
aa374f67
AM
10208 asection *opd = stub_entry->h->elf.root.u.def.section;
10209 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10210
10211 if (strcmp (opd->name, ".opd") != 0
10212 || opd->reloc_count != 0)
10213 {
2cdcc330
AM
10214 info->callbacks->einfo
10215 (_("%P: cannot find opd entry toc for `%pT'\n"),
10216 stub_entry->h->elf.root.root.string);
aa374f67 10217 bfd_set_error (bfd_error_bad_value);
a7c49797 10218 return (bfd_vma) -1;
aa374f67
AM
10219 }
10220 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10221 return (bfd_vma) -1;
aa374f67 10222 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10223 r2off -= elf_gp (info->output_bfd);
aa374f67 10224 }
6f20ed8a 10225 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10226 return r2off;
10227}
10228
b34976b6 10229static bfd_boolean
4ce794b7 10230ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10231{
721956f4
AM
10232 struct ppc_stub_hash_entry *stub_entry;
10233 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10234 struct bfd_link_info *info;
10235 struct ppc_link_hash_table *htab;
721956f4 10236 bfd_byte *loc;
3d58e1fc 10237 bfd_byte *p, *relp;
1aa42141 10238 bfd_vma targ, off;
176a0d42 10239 Elf_Internal_Rela *r;
e054468f 10240 asection *plt;
3d58e1fc 10241 int num_rel;
5d1634d7 10242
721956f4
AM
10243 /* Massage our args to the form they really have. */
10244 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10245 info = in_arg;
5d1634d7 10246
5d1634d7 10247 htab = ppc_hash_table (info);
4dfe6ac6
NC
10248 if (htab == NULL)
10249 return FALSE;
5d1634d7 10250
1aa42141 10251 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 10252 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10253
4ce794b7 10254 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10255 switch (stub_entry->stub_type)
5d1634d7 10256 {
721956f4 10257 case ppc_stub_long_branch:
ad8e1ba5 10258 case ppc_stub_long_branch_r2off:
721956f4 10259 /* Branches are relative. This is where we are going to. */
1aa42141 10260 targ = (stub_entry->target_value
6911b7dc
AM
10261 + stub_entry->target_section->output_offset
10262 + stub_entry->target_section->output_section->vma);
1aa42141 10263 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 10264
721956f4 10265 /* And this is where we are coming from. */
1aa42141
AM
10266 off = (stub_entry->stub_offset
10267 + stub_entry->group->stub_sec->output_offset
10268 + stub_entry->group->stub_sec->output_section->vma);
10269 off = targ - off;
e86ce104 10270
9e390558 10271 p = loc;
ac2df442 10272 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10273 {
25f53a85 10274 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10275
a7c49797 10276 if (r2off == (bfd_vma) -1)
aa374f67
AM
10277 {
10278 htab->stub_error = TRUE;
10279 return FALSE;
10280 }
9e390558
AM
10281 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10282 p += 4;
ac2df442
AM
10283 if (PPC_HA (r2off) != 0)
10284 {
e7d1c40c 10285 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10286 ADDIS_R2_R2 | PPC_HA (r2off), p);
10287 p += 4;
a7c49797
AM
10288 }
10289 if (PPC_LO (r2off) != 0)
10290 {
10291 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10292 ADDI_R2_R2 | PPC_LO (r2off), p);
10293 p += 4;
ac2df442 10294 }
9e390558 10295 off -= p - loc;
ad8e1ba5 10296 }
9e390558
AM
10297 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10298 p += 4;
ad8e1ba5 10299
5c3dead3
AM
10300 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10301 {
cf97bcb0
AM
10302 _bfd_error_handler
10303 (_("long branch stub `%s' offset overflow"),
bc30df16 10304 stub_entry->root.string);
5c3dead3
AM
10305 htab->stub_error = TRUE;
10306 return FALSE;
10307 }
ee75fd95
AM
10308
10309 if (info->emitrelocations)
10310 {
6f20ed8a 10311 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
10312 if (r == NULL)
10313 return FALSE;
9e390558 10314 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 10315 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 10316 r->r_addend = targ;
3d58e1fc
AM
10317 if (stub_entry->h != NULL
10318 && !use_global_in_relocs (htab, stub_entry, r, 1))
10319 return FALSE;
ee75fd95 10320 }
721956f4 10321 break;
e86ce104 10322
721956f4 10323 case ppc_stub_plt_branch:
ad8e1ba5 10324 case ppc_stub_plt_branch_r2off:
721956f4
AM
10325 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10326 stub_entry->root.string + 9,
b34976b6 10327 FALSE, FALSE);
721956f4
AM
10328 if (br_entry == NULL)
10329 {
cf97bcb0
AM
10330 _bfd_error_handler (_("can't find branch stub `%s'"),
10331 stub_entry->root.string);
b34976b6
AM
10332 htab->stub_error = TRUE;
10333 return FALSE;
721956f4
AM
10334 }
10335
1aa42141 10336 targ = (stub_entry->target_value
176a0d42
AM
10337 + stub_entry->target_section->output_offset
10338 + stub_entry->target_section->output_section->vma);
6911b7dc 10339 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 10340 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10341
1aa42141 10342 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 10343 htab->brlt->contents + br_entry->offset);
721956f4 10344
f94498ff 10345 if (br_entry->iter == htab->stub_iteration)
721956f4 10346 {
f94498ff 10347 br_entry->iter = 0;
84f5d08e 10348
f94498ff 10349 if (htab->relbrlt != NULL)
84f5d08e 10350 {
f94498ff
AM
10351 /* Create a reloc for the branch lookup table entry. */
10352 Elf_Internal_Rela rela;
10353 bfd_byte *rl;
10354
10355 rela.r_offset = (br_entry->offset
10356 + htab->brlt->output_offset
10357 + htab->brlt->output_section->vma);
10358 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 10359 rela.r_addend = targ;
f94498ff
AM
10360
10361 rl = htab->relbrlt->contents;
10362 rl += (htab->relbrlt->reloc_count++
10363 * sizeof (Elf64_External_Rela));
10364 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10365 }
10366 else if (info->emitrelocations)
10367 {
176a0d42
AM
10368 r = get_relocs (htab->brlt, 1);
10369 if (r == NULL)
10370 return FALSE;
10371 /* brlt, being SEC_LINKER_CREATED does not go through the
10372 normal reloc processing. Symbols and offsets are not
10373 translated from input file to output file form, so
10374 set up the offset per the output file. */
f94498ff
AM
10375 r->r_offset = (br_entry->offset
10376 + htab->brlt->output_offset
10377 + htab->brlt->output_section->vma);
10378 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 10379 r->r_addend = targ;
84f5d08e 10380 }
84f5d08e 10381 }
721956f4 10382
1aa42141 10383 targ = (br_entry->offset
176a0d42
AM
10384 + htab->brlt->output_offset
10385 + htab->brlt->output_section->vma);
10386
1aa42141
AM
10387 off = (elf_gp (info->output_bfd)
10388 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10389 off = targ - off;
721956f4 10390
ad8e1ba5 10391 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 10392 {
25f53a85 10393 info->callbacks->einfo
c1c8c1ef 10394 (_("%P: linkage table error against `%pT'\n"),
721956f4 10395 stub_entry->root.string);
5d1634d7 10396 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10397 htab->stub_error = TRUE;
10398 return FALSE;
5d1634d7 10399 }
41bd81ab 10400
176a0d42
AM
10401 if (info->emitrelocations)
10402 {
6f20ed8a 10403 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
10404 if (r == NULL)
10405 return FALSE;
6f20ed8a 10406 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
10407 if (bfd_big_endian (info->output_bfd))
10408 r[0].r_offset += 2;
00f412ee 10409 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
10410 r[0].r_offset += 4;
10411 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 10412 r[0].r_addend = targ;
176a0d42
AM
10413 if (PPC_HA (off) != 0)
10414 {
10415 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10416 r[1].r_offset = r[0].r_offset + 4;
10417 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10418 r[1].r_addend = r[0].r_addend;
10419 }
10420 }
10421
9e390558 10422 p = loc;
00f412ee 10423 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 10424 {
176a0d42 10425 if (PPC_HA (off) != 0)
ac2df442 10426 {
e7d1c40c 10427 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10428 ADDIS_R12_R2 | PPC_HA (off), p);
10429 p += 4;
e7d1c40c 10430 bfd_put_32 (htab->params->stub_bfd,
9e390558 10431 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
10432 }
10433 else
9e390558
AM
10434 bfd_put_32 (htab->params->stub_bfd,
10435 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
10436 }
10437 else
10438 {
25f53a85 10439 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 10440
a7c49797 10441 if (r2off == (bfd_vma) -1)
aa374f67
AM
10442 {
10443 htab->stub_error = TRUE;
10444 return FALSE;
10445 }
ad8e1ba5 10446
9e390558
AM
10447 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10448 p += 4;
176a0d42 10449 if (PPC_HA (off) != 0)
ac2df442 10450 {
e7d1c40c 10451 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10452 ADDIS_R12_R2 | PPC_HA (off), p);
10453 p += 4;
e7d1c40c 10454 bfd_put_32 (htab->params->stub_bfd,
9e390558 10455 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
10456 }
10457 else
9e390558 10458 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
10459
10460 if (PPC_HA (r2off) != 0)
10461 {
9e390558 10462 p += 4;
e7d1c40c 10463 bfd_put_32 (htab->params->stub_bfd,
9e390558 10464 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
10465 }
10466 if (PPC_LO (r2off) != 0)
10467 {
9e390558 10468 p += 4;
e7d1c40c 10469 bfd_put_32 (htab->params->stub_bfd,
9e390558 10470 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 10471 }
ad8e1ba5 10472 }
9e390558
AM
10473 p += 4;
10474 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10475 p += 4;
407aa07c
AM
10476 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10477 p += 4;
721956f4 10478 break;
5d1634d7 10479
05d0e962
AM
10480 case ppc_stub_long_branch_notoc:
10481 case ppc_stub_long_branch_both:
10482 case ppc_stub_plt_branch_notoc:
10483 case ppc_stub_plt_branch_both:
10484 case ppc_stub_plt_call_notoc:
10485 case ppc_stub_plt_call_both:
10486 p = loc;
f891966f 10487 off = (stub_entry->stub_offset
05d0e962
AM
10488 + stub_entry->group->stub_sec->output_offset
10489 + stub_entry->group->stub_sec->output_section->vma);
10490 if (stub_entry->stub_type == ppc_stub_long_branch_both
10491 || stub_entry->stub_type == ppc_stub_plt_branch_both
10492 || stub_entry->stub_type == ppc_stub_plt_call_both)
10493 {
10494 off += 4;
10495 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10496 p += 4;
10497 }
10498 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10499 {
10500 targ = stub_entry->plt_ent->plt.offset & ~1;
10501 if (targ >= (bfd_vma) -2)
10502 abort ();
10503
10504 plt = htab->elf.splt;
10505 if (!htab->elf.dynamic_sections_created
10506 || stub_entry->h == NULL
10507 || stub_entry->h->elf.dynindx == -1)
10508 {
10509 if (stub_entry->symtype == STT_GNU_IFUNC)
10510 plt = htab->elf.iplt;
10511 else
10512 plt = htab->pltlocal;
10513 }
10514 targ += plt->output_offset + plt->output_section->vma;
10515 }
10516 else
10517 targ = (stub_entry->target_value
10518 + stub_entry->target_section->output_offset
10519 + stub_entry->target_section->output_section->vma);
10520 off = targ - off;
f891966f 10521
3d58e1fc
AM
10522 relp = p;
10523 num_rel = 0;
f891966f
AM
10524 /* The notoc stubs calculate their target (either a PLT entry or
10525 the global entry point of a function) relative to the PC
10526 returned by the "bcl" two instructions past the start of the
10527 sequence emitted by build_offset. The offset is therefore 8
10528 less than calculated from the start of the sequence. */
10529 off -= 8;
05d0e962
AM
10530 p = build_offset (htab->params->stub_bfd, p, off,
10531 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
f891966f 10532 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
05d0e962 10533 {
f891966f 10534 bfd_vma from;
3d58e1fc 10535 num_rel = 1;
f891966f
AM
10536 from = (stub_entry->stub_offset
10537 + stub_entry->group->stub_sec->output_offset
10538 + stub_entry->group->stub_sec->output_section->vma
10539 + (p - loc));
05d0e962 10540 bfd_put_32 (htab->params->stub_bfd,
f891966f 10541 B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
10542 }
10543 else
10544 {
10545 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10546 p += 4;
10547 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10548 }
10549 p += 4;
df136d64 10550
3d58e1fc
AM
10551 if (info->emitrelocations)
10552 {
10553 bfd_vma roff;
10554 num_rel += num_relocs_for_offset (off);
10555 r = get_relocs (stub_entry->group->stub_sec, num_rel);
10556 if (r == NULL)
10557 return FALSE;
10558 roff = relp + 16 - stub_entry->group->stub_sec->contents;
10559 r = emit_relocs_for_offset (info, r, roff, targ, off);
10560 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
10561 || stub_entry->stub_type == ppc_stub_long_branch_both)
10562 {
10563 ++r;
10564 roff = p - 4 - stub_entry->group->stub_sec->contents;
10565 r->r_offset = roff;
10566 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10567 r->r_addend = targ;
10568 if (stub_entry->h != NULL
10569 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
10570 return FALSE;
10571 }
10572 }
10573
df136d64
AM
10574 if (htab->glink_eh_frame != NULL
10575 && htab->glink_eh_frame->size != 0)
10576 {
10577 bfd_byte *base, *eh;
10578 unsigned int lr_used, delta;
10579
10580 base = (htab->glink_eh_frame->contents
10581 + stub_entry->group->eh_base + 17);
10582 eh = base + stub_entry->group->eh_size;
10583 lr_used = stub_entry->stub_offset + 8;
10584 if (stub_entry->stub_type == ppc_stub_long_branch_both
10585 || stub_entry->stub_type == ppc_stub_plt_branch_both
10586 || stub_entry->stub_type == ppc_stub_plt_call_both)
10587 lr_used += 4;
10588 delta = lr_used - stub_entry->group->lr_restore;
10589 stub_entry->group->lr_restore = lr_used + 8;
10590 eh = eh_advance (htab->elf.dynobj, eh, delta);
10591 *eh++ = DW_CFA_register;
10592 *eh++ = 65;
10593 *eh++ = 12;
10594 *eh++ = DW_CFA_advance_loc + 2;
10595 *eh++ = DW_CFA_restore_extended;
10596 *eh++ = 65;
10597 stub_entry->group->eh_size = eh - base;
10598 }
05d0e962
AM
10599 break;
10600
721956f4 10601 case ppc_stub_plt_call:
794e51c0 10602 case ppc_stub_plt_call_r2save:
e054468f 10603 if (stub_entry->h != NULL
b31867b6
AM
10604 && stub_entry->h->is_func_descriptor
10605 && stub_entry->h->oh != NULL)
c862ae31 10606 {
b31867b6
AM
10607 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10608
10609 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 10610 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
10611 if (fh->elf.root.type == bfd_link_hash_undefined
10612 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
10613 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 10614 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
10615 }
10616
721956f4 10617 /* Now build the stub. */
1aa42141
AM
10618 targ = stub_entry->plt_ent->plt.offset & ~1;
10619 if (targ >= (bfd_vma) -2)
721956f4
AM
10620 abort ();
10621
33e44f2e 10622 plt = htab->elf.splt;
25f23106
AM
10623 if (!htab->elf.dynamic_sections_created
10624 || stub_entry->h == NULL
10625 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
10626 {
10627 if (stub_entry->symtype == STT_GNU_IFUNC)
10628 plt = htab->elf.iplt;
10629 else
10630 plt = htab->pltlocal;
10631 }
1aa42141 10632 targ += plt->output_offset + plt->output_section->vma;
e054468f 10633
1aa42141
AM
10634 off = (elf_gp (info->output_bfd)
10635 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10636 off = targ - off;
721956f4 10637
ad8e1ba5 10638 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 10639 {
25f53a85 10640 info->callbacks->einfo
695344c0 10641 /* xgettext:c-format */
c1c8c1ef 10642 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
10643 stub_entry->h != NULL
10644 ? stub_entry->h->elf.root.root.string
10645 : "<local sym>");
721956f4 10646 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10647 htab->stub_error = TRUE;
10648 return FALSE;
721956f4
AM
10649 }
10650
176a0d42
AM
10651 r = NULL;
10652 if (info->emitrelocations)
10653 {
6f20ed8a 10654 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
10655 ((PPC_HA (off) != 0)
10656 + (htab->opd_abi
e7d1c40c 10657 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
10658 && PPC_HA (off + 16) == PPC_HA (off))
10659 : 1)));
176a0d42
AM
10660 if (r == NULL)
10661 return FALSE;
6f20ed8a 10662 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
10663 if (bfd_big_endian (info->output_bfd))
10664 r[0].r_offset += 2;
1aa42141 10665 r[0].r_addend = targ;
176a0d42 10666 }
a7f2871e
AM
10667 if (stub_entry->h != NULL
10668 && (stub_entry->h == htab->tls_get_addr_fd
10669 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10670 && htab->params->tls_get_addr_opt)
794e51c0 10671 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 10672 else
794e51c0 10673 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
10674 break;
10675
a4b6fadd
AM
10676 case ppc_stub_save_res:
10677 return TRUE;
10678
721956f4
AM
10679 default:
10680 BFD_FAIL ();
b34976b6 10681 return FALSE;
721956f4
AM
10682 }
10683
1aa42141 10684 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 10685
e7d1c40c 10686 if (htab->params->emit_stub_syms)
97b639ba
AM
10687 {
10688 struct elf_link_hash_entry *h;
ee75fd95
AM
10689 size_t len1, len2;
10690 char *name;
10691 const char *const stub_str[] = { "long_branch",
05d0e962
AM
10692 "long_branch",
10693 "long_branch",
3f6ff479
AM
10694 "long_branch",
10695 "plt_branch",
ee75fd95 10696 "plt_branch",
05d0e962
AM
10697 "plt_branch",
10698 "plt_branch",
10699 "plt_call",
10700 "plt_call",
794e51c0 10701 "plt_call",
ee75fd95
AM
10702 "plt_call" };
10703
10704 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10705 len2 = strlen (stub_entry->root.string);
10706 name = bfd_malloc (len1 + len2 + 2);
10707 if (name == NULL)
10708 return FALSE;
10709 memcpy (name, stub_entry->root.string, 9);
10710 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10711 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10712 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
10713 if (h == NULL)
10714 return FALSE;
10715 if (h->root.type == bfd_link_hash_new)
10716 {
10717 h->root.type = bfd_link_hash_defined;
6f20ed8a 10718 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 10719 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
10720 h->ref_regular = 1;
10721 h->def_regular = 1;
10722 h->ref_regular_nonweak = 1;
10723 h->forced_local = 1;
10724 h->non_elf = 0;
2ec55de3 10725 h->root.linker_def = 1;
97b639ba
AM
10726 }
10727 }
10728
b34976b6 10729 return TRUE;
721956f4
AM
10730}
10731
10732/* As above, but don't actually build the stub. Just bump offset so
10733 we know stub section sizes, and select plt_branch stubs where
10734 long_branch stubs won't do. */
10735
b34976b6 10736static bfd_boolean
4ce794b7 10737ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
10738{
10739 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 10740 struct bfd_link_info *info;
721956f4 10741 struct ppc_link_hash_table *htab;
f891966f
AM
10742 asection *plt;
10743 bfd_vma targ, off, r2off;
10744 unsigned int size, extra, lr_used, delta;
721956f4
AM
10745
10746 /* Massage our args to the form they really have. */
10747 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
10748 info = in_arg;
10749
10750 htab = ppc_hash_table (info);
4dfe6ac6
NC
10751 if (htab == NULL)
10752 return FALSE;
721956f4 10753
1aa42141
AM
10754 /* Make a note of the offset within the stubs for this entry. */
10755 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10756
a4b6fadd
AM
10757 if (stub_entry->h != NULL
10758 && stub_entry->h->save_res
10759 && stub_entry->h->elf.root.type == bfd_link_hash_defined
10760 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
10761 {
10762 /* Don't make stubs to out-of-line register save/restore
10763 functions. Instead, emit copies of the functions. */
10764 stub_entry->group->needs_save_res = 1;
10765 stub_entry->stub_type = ppc_stub_save_res;
10766 return TRUE;
10767 }
10768
f891966f 10769 switch (stub_entry->stub_type)
721956f4 10770 {
f891966f
AM
10771 case ppc_stub_plt_branch:
10772 case ppc_stub_plt_branch_r2off:
10773 /* Reset the stub type from the plt branch variant in case we now
10774 can reach with a shorter stub. */
10775 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10776 /* Fall through. */
10777 case ppc_stub_long_branch:
10778 case ppc_stub_long_branch_r2off:
1aa42141
AM
10779 targ = (stub_entry->target_value
10780 + stub_entry->target_section->output_offset
10781 + stub_entry->target_section->output_section->vma);
f891966f 10782 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
1aa42141
AM
10783 off = (stub_entry->stub_offset
10784 + stub_entry->group->stub_sec->output_offset
10785 + stub_entry->group->stub_sec->output_section->vma);
721956f4 10786
ad8e1ba5 10787 size = 4;
f891966f 10788 r2off = 0;
ad8e1ba5
AM
10789 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10790 {
25f53a85 10791 r2off = get_r2off (info, stub_entry);
a7c49797 10792 if (r2off == (bfd_vma) -1)
aa374f67
AM
10793 {
10794 htab->stub_error = TRUE;
10795 return FALSE;
10796 }
a7c49797 10797 size = 8;
ac2df442 10798 if (PPC_HA (r2off) != 0)
a7c49797
AM
10799 size += 4;
10800 if (PPC_LO (r2off) != 0)
10801 size += 4;
1aa42141 10802 off += size - 4;
ad8e1ba5 10803 }
1aa42141 10804 off = targ - off;
ad8e1ba5 10805
f891966f
AM
10806 /* If the branch offset is too big, use a ppc_stub_plt_branch.
10807 Do the same for -R objects without function descriptors. */
10808 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
10809 && r2off == 0
10810 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
10811 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 10812 {
f891966f 10813 struct ppc_branch_hash_entry *br_entry;
df136d64 10814
f891966f
AM
10815 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10816 stub_entry->root.string + 9,
10817 TRUE, FALSE);
10818 if (br_entry == NULL)
721956f4 10819 {
f891966f
AM
10820 _bfd_error_handler (_("can't build branch stub `%s'"),
10821 stub_entry->root.string);
10822 htab->stub_error = TRUE;
10823 return FALSE;
721956f4
AM
10824 }
10825
f891966f 10826 if (br_entry->iter != htab->stub_iteration)
721956f4 10827 {
f891966f
AM
10828 br_entry->iter = htab->stub_iteration;
10829 br_entry->offset = htab->brlt->size;
10830 htab->brlt->size += 8;
63bc6f6c 10831
f891966f
AM
10832 if (htab->relbrlt != NULL)
10833 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10834 else if (info->emitrelocations)
84f5d08e 10835 {
f891966f
AM
10836 htab->brlt->reloc_count += 1;
10837 htab->brlt->flags |= SEC_RELOC;
05d0e962 10838 }
f891966f 10839 }
ac2df442 10840
f891966f
AM
10841 targ = (br_entry->offset
10842 + htab->brlt->output_offset
10843 + htab->brlt->output_section->vma);
10844 off = (elf_gp (info->output_bfd)
10845 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10846 off = targ - off;
176a0d42 10847
f891966f
AM
10848 if (info->emitrelocations)
10849 {
10850 stub_entry->group->stub_sec->reloc_count
10851 += 1 + (PPC_HA (off) != 0);
10852 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10853 }
05d0e962 10854
f891966f
AM
10855 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
10856 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10857 {
10858 size = 12;
10859 if (PPC_HA (off) != 0)
10860 size = 16;
ac2df442 10861 }
f891966f 10862 else
ac2df442 10863 {
f891966f
AM
10864 size = 16;
10865 if (PPC_HA (off) != 0)
10866 size += 4;
10867
10868 if (PPC_HA (r2off) != 0)
10869 size += 4;
10870 if (PPC_LO (r2off) != 0)
10871 size += 4;
ac2df442 10872 }
721956f4 10873 }
f891966f
AM
10874 else if (info->emitrelocations)
10875 {
10876 stub_entry->group->stub_sec->reloc_count += 1;
10877 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10878 }
10879 break;
10880
10881 case ppc_stub_plt_branch_notoc:
10882 case ppc_stub_plt_branch_both:
10883 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10884 /* Fall through. */
10885 case ppc_stub_long_branch_notoc:
10886 case ppc_stub_long_branch_both:
10887 off = (stub_entry->stub_offset
10888 + stub_entry->group->stub_sec->output_offset
10889 + stub_entry->group->stub_sec->output_section->vma);
10890 size = 0;
10891 if (stub_entry->stub_type == ppc_stub_long_branch_both)
10892 size = 4;
10893 off += size;
10894 targ = (stub_entry->target_value
10895 + stub_entry->target_section->output_offset
10896 + stub_entry->target_section->output_section->vma);
10897 off = targ - off;
10898
3d58e1fc
AM
10899 if (info->emitrelocations)
10900 {
10901 stub_entry->group->stub_sec->reloc_count
10902 += num_relocs_for_offset (off);
10903 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10904 }
10905
f891966f
AM
10906 extra = size_offset (off - 8);
10907 /* Include branch insn plus those in the offset sequence. */
10908 size += 4 + extra;
10909 /* The branch insn is at the end, or "extra" bytes along. So
10910 its offset will be "extra" bytes less that that already
10911 calculated. */
10912 off -= extra;
10913
10914 /* After the bcl, lr has been modified so we need to emit
10915 .eh_frame info saying the return address is in r12. */
10916 lr_used = stub_entry->stub_offset + 8;
10917 if (stub_entry->stub_type == ppc_stub_long_branch_both)
10918 lr_used += 4;
10919 /* The eh_frame info will consist of a DW_CFA_advance_loc or
10920 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
10921 DW_CFA_restore_extended 65. */
10922 delta = lr_used - stub_entry->group->lr_restore;
10923 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10924 stub_entry->group->lr_restore = lr_used + 8;
10925
10926 /* If the branch can't reach, use a plt_branch. */
10927 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10928 {
10929 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
10930 - ppc_stub_long_branch_notoc);
10931 size += 4;
10932 }
3d58e1fc
AM
10933 else if (info->emitrelocations)
10934 stub_entry->group->stub_sec->reloc_count +=1;
f891966f
AM
10935 break;
10936
10937 case ppc_stub_plt_call_notoc:
10938 case ppc_stub_plt_call_both:
10939 off = (stub_entry->stub_offset
10940 + stub_entry->group->stub_sec->output_offset
10941 + stub_entry->group->stub_sec->output_section->vma);
10942 if (stub_entry->stub_type == ppc_stub_plt_call_both)
10943 off += 4;
10944 targ = stub_entry->plt_ent->plt.offset & ~1;
10945 if (targ >= (bfd_vma) -2)
10946 abort ();
10947
10948 plt = htab->elf.splt;
10949 if (!htab->elf.dynamic_sections_created
10950 || stub_entry->h == NULL
10951 || stub_entry->h->elf.dynindx == -1)
10952 {
10953 if (stub_entry->symtype == STT_GNU_IFUNC)
10954 plt = htab->elf.iplt;
10955 else
10956 plt = htab->pltlocal;
10957 }
10958 targ += plt->output_offset + plt->output_section->vma;
10959 off = targ - off;
10960
10961 if (htab->params->plt_stub_align != 0)
10962 {
10963 unsigned pad = plt_stub_pad (htab, stub_entry, off);
10964
10965 stub_entry->group->stub_sec->size += pad;
10966 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10967 off -= pad;
10968 }
10969
3d58e1fc
AM
10970 if (info->emitrelocations)
10971 {
10972 stub_entry->group->stub_sec->reloc_count
10973 += num_relocs_for_offset (off - 8);
10974 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10975 }
10976
f891966f
AM
10977 size = plt_stub_size (htab, stub_entry, off);
10978
10979 /* After the bcl, lr has been modified so we need to emit
10980 .eh_frame info saying the return address is in r12. */
10981 lr_used = stub_entry->stub_offset + 8;
10982 if (stub_entry->stub_type == ppc_stub_plt_call_both)
10983 lr_used += 4;
10984 /* The eh_frame info will consist of a DW_CFA_advance_loc or
10985 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
10986 DW_CFA_restore_extended 65. */
10987 delta = lr_used - stub_entry->group->lr_restore;
10988 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10989 stub_entry->group->lr_restore = lr_used + 8;
10990 break;
10991
10992 case ppc_stub_plt_call:
10993 case ppc_stub_plt_call_r2save:
10994 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
10995 if (targ >= (bfd_vma) -2)
10996 abort ();
10997 plt = htab->elf.splt;
10998 if (!htab->elf.dynamic_sections_created
10999 || stub_entry->h == NULL
11000 || stub_entry->h->elf.dynindx == -1)
11001 {
11002 if (stub_entry->symtype == STT_GNU_IFUNC)
11003 plt = htab->elf.iplt;
11004 else
11005 plt = htab->pltlocal;
11006 }
11007 targ += plt->output_offset + plt->output_section->vma;
11008
11009 off = (elf_gp (info->output_bfd)
11010 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11011 off = targ - off;
11012
11013 if (htab->params->plt_stub_align != 0)
11014 {
11015 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11016
11017 stub_entry->group->stub_sec->size += pad;
11018 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11019 }
11020
11021 if (info->emitrelocations)
11022 {
11023 stub_entry->group->stub_sec->reloc_count
11024 += ((PPC_HA (off) != 0)
11025 + (htab->opd_abi
11026 ? 2 + (htab->params->plt_static_chain
11027 && PPC_HA (off + 16) == PPC_HA (off))
11028 : 1));
11029 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11030 }
11031
11032 size = plt_stub_size (htab, stub_entry, off);
11033
11034 if (stub_entry->h != NULL
11035 && (stub_entry->h == htab->tls_get_addr_fd
11036 || stub_entry->h == htab->tls_get_addr)
11037 && htab->params->tls_get_addr_opt
11038 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11039 {
11040 /* After the bctrl, lr has been modified so we need to
11041 emit .eh_frame info saying the return address is
11042 on the stack. In fact we put the EH info specifying
11043 that the return address is on the stack *at* the
11044 call rather than after it, because the EH info for a
11045 call needs to be specified by that point.
11046 See libgcc/unwind-dw2.c execute_cfa_program. */
11047 lr_used = stub_entry->stub_offset + size - 20;
11048 /* The eh_frame info will consist of a DW_CFA_advance_loc
11049 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11050 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11051 delta = lr_used - stub_entry->group->lr_restore;
11052 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11053 stub_entry->group->lr_restore = size - 4;
11054 }
11055 break;
11056
11057 default:
11058 BFD_FAIL ();
11059 return FALSE;
721956f4
AM
11060 }
11061
6f20ed8a 11062 stub_entry->group->stub_sec->size += size;
b34976b6 11063 return TRUE;
721956f4
AM
11064}
11065
11066/* Set up various things so that we can make a list of input sections
11067 for each output section included in the link. Returns -1 on error,
cedb70c5 11068 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11069
11070int
e7d1c40c 11071ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11072{
6f20ed8a 11073 unsigned int id;
721956f4
AM
11074 bfd_size_type amt;
11075 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11076
4dfe6ac6
NC
11077 if (htab == NULL)
11078 return -1;
4c52953f 11079
7cf7fcc8 11080 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
11081 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11082 htab->sec_info = bfd_zmalloc (amt);
11083 if (htab->sec_info == NULL)
721956f4
AM
11084 return -1;
11085
3d6f9012
AM
11086 /* Set toc_off for com, und, abs and ind sections. */
11087 for (id = 0; id < 3; id++)
6f20ed8a 11088 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11089
721956f4
AM
11090 return 1;
11091}
11092
927be08e
AM
11093/* Set up for first pass at multitoc partitioning. */
11094
11095void
11096ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11097{
11098 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11099
1c865ab2 11100 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11101 htab->toc_bfd = NULL;
11102 htab->toc_first_sec = NULL;
11103}
11104
e717da7e
AM
11105/* The linker repeatedly calls this function for each TOC input section
11106 and linker generated GOT section. Group input bfds such that the toc
927be08e 11107 within a group is less than 64k in size. */
ad8e1ba5 11108
927be08e 11109bfd_boolean
4ce794b7 11110ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11111{
11112 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11113 bfd_vma addr, off, limit;
ad8e1ba5 11114
4dfe6ac6
NC
11115 if (htab == NULL)
11116 return FALSE;
11117
927be08e 11118 if (!htab->second_toc_pass)
4c52953f 11119 {
927be08e 11120 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11121 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11122
11123 if (new_bfd)
bf102f86
AM
11124 {
11125 htab->toc_bfd = isec->owner;
11126 htab->toc_first_sec = isec;
11127 }
927be08e 11128
bf102f86
AM
11129 addr = isec->output_offset + isec->output_section->vma;
11130 off = addr - htab->toc_curr;
d77c8a4b
AM
11131 limit = 0x80008000;
11132 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11133 limit = 0x10000;
11134 if (off + isec->size > limit)
bf102f86
AM
11135 {
11136 addr = (htab->toc_first_sec->output_offset
11137 + htab->toc_first_sec->output_section->vma);
11138 htab->toc_curr = addr;
a27e685f 11139 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11140 }
99877b66 11141
927be08e
AM
11142 /* toc_curr is the base address of this toc group. Set elf_gp
11143 for the input section to be the offset relative to the
11144 output toc base plus 0x8000. Making the input elf_gp an
11145 offset allows us to move the toc as a whole without
11146 recalculating input elf_gp. */
06bcf541 11147 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
11148 off += TOC_BASE_OFF;
11149
11150 /* Die if someone uses a linker script that doesn't keep input
11151 file .toc and .got together. */
a4fd3de5
AM
11152 if (new_bfd
11153 && elf_gp (isec->owner) != 0
927be08e
AM
11154 && elf_gp (isec->owner) != off)
11155 return FALSE;
11156
11157 elf_gp (isec->owner) = off;
11158 return TRUE;
4c52953f 11159 }
927be08e
AM
11160
11161 /* During the second pass toc_first_sec points to the start of
11162 a toc group, and toc_curr is used to track the old elf_gp.
11163 We use toc_bfd to ensure we only look at each bfd once. */
11164 if (htab->toc_bfd == isec->owner)
11165 return TRUE;
11166 htab->toc_bfd = isec->owner;
11167
11168 if (htab->toc_first_sec == NULL
11169 || htab->toc_curr != elf_gp (isec->owner))
11170 {
11171 htab->toc_curr = elf_gp (isec->owner);
11172 htab->toc_first_sec = isec;
11173 }
11174 addr = (htab->toc_first_sec->output_offset
11175 + htab->toc_first_sec->output_section->vma);
06bcf541 11176 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
11177 elf_gp (isec->owner) = off;
11178
11179 return TRUE;
ad8e1ba5
AM
11180}
11181
927be08e
AM
11182/* Called via elf_link_hash_traverse to merge GOT entries for global
11183 symbol H. */
11184
11185static bfd_boolean
11186merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11187{
11188 if (h->root.type == bfd_link_hash_indirect)
11189 return TRUE;
11190
927be08e
AM
11191 merge_got_entries (&h->got.glist);
11192
11193 return TRUE;
11194}
11195
11196/* Called via elf_link_hash_traverse to allocate GOT entries for global
11197 symbol H. */
11198
11199static bfd_boolean
11200reallocate_got (struct elf_link_hash_entry *h, void *inf)
11201{
11202 struct got_entry *gent;
11203
11204 if (h->root.type == bfd_link_hash_indirect)
11205 return TRUE;
11206
927be08e
AM
11207 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11208 if (!gent->is_indirect)
11209 allocate_got (h, (struct bfd_link_info *) inf, gent);
11210 return TRUE;
11211}
11212
11213/* Called on the first multitoc pass after the last call to
11214 ppc64_elf_next_toc_section. This function removes duplicate GOT
11215 entries. */
11216
11217bfd_boolean
11218ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11219{
11220 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11221 struct bfd *ibfd, *ibfd2;
11222 bfd_boolean done_something;
11223
11224 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11225
7865406b
AM
11226 if (!htab->do_multi_toc)
11227 return FALSE;
11228
d0fae19d 11229 /* Merge global sym got entries within a toc group. */
927be08e
AM
11230 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11231
11232 /* And tlsld_got. */
c72f2fb2 11233 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11234 {
11235 struct got_entry *ent, *ent2;
11236
11237 if (!is_ppc64_elf (ibfd))
11238 continue;
11239
11240 ent = ppc64_tlsld_got (ibfd);
11241 if (!ent->is_indirect
11242 && ent->got.offset != (bfd_vma) -1)
11243 {
c72f2fb2 11244 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11245 {
11246 if (!is_ppc64_elf (ibfd2))
11247 continue;
11248
11249 ent2 = ppc64_tlsld_got (ibfd2);
11250 if (!ent2->is_indirect
11251 && ent2->got.offset != (bfd_vma) -1
11252 && elf_gp (ibfd2) == elf_gp (ibfd))
11253 {
11254 ent2->is_indirect = TRUE;
11255 ent2->got.ent = ent;
11256 }
11257 }
11258 }
11259 }
11260
11261 /* Zap sizes of got sections. */
33e44f2e
AM
11262 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11263 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11264 htab->got_reli_size = 0;
11265
c72f2fb2 11266 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11267 {
11268 asection *got, *relgot;
11269
11270 if (!is_ppc64_elf (ibfd))
11271 continue;
11272
11273 got = ppc64_elf_tdata (ibfd)->got;
11274 if (got != NULL)
11275 {
11276 got->rawsize = got->size;
11277 got->size = 0;
11278 relgot = ppc64_elf_tdata (ibfd)->relgot;
11279 relgot->rawsize = relgot->size;
11280 relgot->size = 0;
11281 }
11282 }
11283
11284 /* Now reallocate the got, local syms first. We don't need to
11285 allocate section contents again since we never increase size. */
c72f2fb2 11286 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11287 {
11288 struct got_entry **lgot_ents;
11289 struct got_entry **end_lgot_ents;
11290 struct plt_entry **local_plt;
11291 struct plt_entry **end_local_plt;
f961d9dd 11292 unsigned char *lgot_masks;
927be08e
AM
11293 bfd_size_type locsymcount;
11294 Elf_Internal_Shdr *symtab_hdr;
19e08130 11295 asection *s;
927be08e
AM
11296
11297 if (!is_ppc64_elf (ibfd))
11298 continue;
11299
11300 lgot_ents = elf_local_got_ents (ibfd);
11301 if (!lgot_ents)
11302 continue;
11303
11304 symtab_hdr = &elf_symtab_hdr (ibfd);
11305 locsymcount = symtab_hdr->sh_info;
11306 end_lgot_ents = lgot_ents + locsymcount;
11307 local_plt = (struct plt_entry **) end_lgot_ents;
11308 end_local_plt = local_plt + locsymcount;
f961d9dd 11309 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11310 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11311 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11312 {
11313 struct got_entry *ent;
11314
11315 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11316 {
19e08130
AM
11317 unsigned int ent_size = 8;
11318 unsigned int rel_size = sizeof (Elf64_External_Rela);
11319
d0fae19d
AM
11320 ent->got.offset = s->size;
11321 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11322 {
19e08130
AM
11323 ent_size *= 2;
11324 rel_size *= 2;
11325 }
11326 s->size += ent_size;
37da22e5 11327 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 11328 {
33e44f2e 11329 htab->elf.irelplt->size += rel_size;
19e08130
AM
11330 htab->got_reli_size += rel_size;
11331 }
6a3858a6
AM
11332 else if (bfd_link_pic (info)
11333 && !((ent->tls_type & TLS_TPREL) != 0
11334 && bfd_link_executable (info)))
19e08130
AM
11335 {
11336 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11337 srel->size += rel_size;
d0fae19d
AM
11338 }
11339 }
927be08e
AM
11340 }
11341 }
11342
11343 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11344
c72f2fb2 11345 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11346 {
11347 struct got_entry *ent;
11348
11349 if (!is_ppc64_elf (ibfd))
11350 continue;
11351
11352 ent = ppc64_tlsld_got (ibfd);
11353 if (!ent->is_indirect
11354 && ent->got.offset != (bfd_vma) -1)
11355 {
11356 asection *s = ppc64_elf_tdata (ibfd)->got;
11357 ent->got.offset = s->size;
11358 s->size += 16;
0e1862bb 11359 if (bfd_link_pic (info))
927be08e
AM
11360 {
11361 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11362 srel->size += sizeof (Elf64_External_Rela);
11363 }
11364 }
11365 }
11366
33e44f2e 11367 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11368 if (!done_something)
c72f2fb2 11369 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11370 {
11371 asection *got;
11372
11373 if (!is_ppc64_elf (ibfd))
11374 continue;
11375
11376 got = ppc64_elf_tdata (ibfd)->got;
11377 if (got != NULL)
11378 {
11379 done_something = got->rawsize != got->size;
11380 if (done_something)
11381 break;
11382 }
11383 }
11384
11385 if (done_something)
e7d1c40c 11386 (*htab->params->layout_sections_again) ();
927be08e
AM
11387
11388 /* Set up for second pass over toc sections to recalculate elf_gp
11389 on input sections. */
11390 htab->toc_bfd = NULL;
11391 htab->toc_first_sec = NULL;
11392 htab->second_toc_pass = TRUE;
11393 return done_something;
11394}
11395
11396/* Called after second pass of multitoc partitioning. */
11397
11398void
11399ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11400{
11401 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11402
11403 /* After the second pass, toc_curr tracks the TOC offset used
11404 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11405 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11406}
11407
9b5ecbd0
AM
11408/* No toc references were found in ISEC. If the code in ISEC makes no
11409 calls, then there's no need to use toc adjusting stubs when branching
11410 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11411 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11412 needed, and 2 if a cyclical call-graph was found but no other reason
11413 for a stub was detected. If called from the top level, a return of
11414 2 means the same as a return of 0. */
9b5ecbd0
AM
11415
11416static int
4ce794b7 11417toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11418{
9b5ecbd0 11419 int ret;
70cc837d
AM
11420
11421 /* Mark this section as checked. */
11422 isec->call_check_done = 1;
9b5ecbd0 11423
772119ce
AM
11424 /* We know none of our code bearing sections will need toc stubs. */
11425 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11426 return 0;
11427
eea6121a 11428 if (isec->size == 0)
082c50f8
AM
11429 return 0;
11430
4c52953f
AM
11431 if (isec->output_section == NULL)
11432 return 0;
11433
4c52953f 11434 ret = 0;
70cc837d 11435 if (isec->reloc_count != 0)
9b5ecbd0 11436 {
70cc837d
AM
11437 Elf_Internal_Rela *relstart, *rel;
11438 Elf_Internal_Sym *local_syms;
11439 struct ppc_link_hash_table *htab;
2917689a 11440
70cc837d
AM
11441 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11442 info->keep_memory);
11443 if (relstart == NULL)
11444 return -1;
90aecf7a 11445
70cc837d
AM
11446 /* Look for branches to outside of this section. */
11447 local_syms = NULL;
11448 htab = ppc_hash_table (info);
11449 if (htab == NULL)
11450 return -1;
4c52953f 11451
70cc837d 11452 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11453 {
70cc837d
AM
11454 enum elf_ppc64_reloc_type r_type;
11455 unsigned long r_symndx;
11456 struct elf_link_hash_entry *h;
11457 struct ppc_link_hash_entry *eh;
11458 Elf_Internal_Sym *sym;
11459 asection *sym_sec;
11460 struct _opd_sec_data *opd;
11461 bfd_vma sym_value;
11462 bfd_vma dest;
11463
11464 r_type = ELF64_R_TYPE (rel->r_info);
11465 if (r_type != R_PPC64_REL24
05d0e962 11466 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
11467 && r_type != R_PPC64_REL14
11468 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d
AM
11469 && r_type != R_PPC64_REL14_BRNTAKEN
11470 && r_type != R_PPC64_PLTCALL)
70cc837d 11471 continue;
4c52953f 11472
70cc837d
AM
11473 r_symndx = ELF64_R_SYM (rel->r_info);
11474 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11475 isec->owner))
4c52953f 11476 {
70cc837d
AM
11477 ret = -1;
11478 break;
11479 }
4c52953f 11480
70cc837d
AM
11481 /* Calls to dynamic lib functions go through a plt call stub
11482 that uses r2. */
11483 eh = (struct ppc_link_hash_entry *) h;
11484 if (eh != NULL
11485 && (eh->elf.plt.plist != NULL
11486 || (eh->oh != NULL
11487 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11488 {
11489 ret = 1;
11490 break;
4c52953f
AM
11491 }
11492
70cc837d
AM
11493 if (sym_sec == NULL)
11494 /* Ignore other undefined symbols. */
4c52953f 11495 continue;
4c52953f 11496
70cc837d
AM
11497 /* Assume branches to other sections not included in the
11498 link need stubs too, to cover -R and absolute syms. */
11499 if (sym_sec->output_section == NULL)
11500 {
11501 ret = 1;
11502 break;
11503 }
4c52953f 11504
70cc837d
AM
11505 if (h == NULL)
11506 sym_value = sym->st_value;
11507 else
11508 {
11509 if (h->root.type != bfd_link_hash_defined
11510 && h->root.type != bfd_link_hash_defweak)
11511 abort ();
11512 sym_value = h->root.u.def.value;
11513 }
11514 sym_value += rel->r_addend;
4c52953f 11515
70cc837d
AM
11516 /* If this branch reloc uses an opd sym, find the code section. */
11517 opd = get_opd_info (sym_sec);
11518 if (opd != NULL)
11519 {
11520 if (h == NULL && opd->adjust != NULL)
11521 {
11522 long adjust;
4c52953f 11523
92a9c616 11524 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
11525 if (adjust == -1)
11526 /* Assume deleted functions won't ever be called. */
11527 continue;
11528 sym_value += adjust;
11529 }
4c52953f 11530
aef36ac1
AM
11531 dest = opd_entry_value (sym_sec, sym_value,
11532 &sym_sec, NULL, FALSE);
70cc837d
AM
11533 if (dest == (bfd_vma) -1)
11534 continue;
11535 }
11536 else
11537 dest = (sym_value
11538 + sym_sec->output_offset
11539 + sym_sec->output_section->vma);
4c52953f 11540
70cc837d
AM
11541 /* Ignore branch to self. */
11542 if (sym_sec == isec)
11543 continue;
4c52953f 11544
70cc837d
AM
11545 /* If the called function uses the toc, we need a stub. */
11546 if (sym_sec->has_toc_reloc
11547 || sym_sec->makes_toc_func_call)
4c52953f 11548 {
70cc837d 11549 ret = 1;
4c52953f
AM
11550 break;
11551 }
70cc837d
AM
11552
11553 /* Assume any branch that needs a long branch stub might in fact
11554 need a plt_branch stub. A plt_branch stub uses r2. */
11555 else if (dest - (isec->output_offset
11556 + isec->output_section->vma
6911b7dc
AM
11557 + rel->r_offset) + (1 << 25)
11558 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11559 ? h->other
11560 : sym->st_other))
4c52953f 11561 {
70cc837d
AM
11562 ret = 1;
11563 break;
11564 }
11565
11566 /* If calling back to a section in the process of being
11567 tested, we can't say for sure that no toc adjusting stubs
11568 are needed, so don't return zero. */
11569 else if (sym_sec->call_check_in_progress)
11570 ret = 2;
11571
11572 /* Branches to another section that itself doesn't have any TOC
11573 references are OK. Recursively call ourselves to check. */
11574 else if (!sym_sec->call_check_done)
11575 {
11576 int recur;
11577
11578 /* Mark current section as indeterminate, so that other
11579 sections that call back to current won't be marked as
11580 known. */
11581 isec->call_check_in_progress = 1;
11582 recur = toc_adjusting_stub_needed (info, sym_sec);
11583 isec->call_check_in_progress = 0;
11584
4c52953f
AM
11585 if (recur != 0)
11586 {
70cc837d
AM
11587 ret = recur;
11588 if (recur != 2)
11589 break;
4c52953f
AM
11590 }
11591 }
4c52953f 11592 }
70cc837d
AM
11593
11594 if (local_syms != NULL
11595 && (elf_symtab_hdr (isec->owner).contents
11596 != (unsigned char *) local_syms))
11597 free (local_syms);
11598 if (elf_section_data (isec)->relocs != relstart)
11599 free (relstart);
9b5ecbd0
AM
11600 }
11601
70cc837d
AM
11602 if ((ret & 1) == 0
11603 && isec->map_head.s != NULL
11604 && (strcmp (isec->output_section->name, ".init") == 0
11605 || strcmp (isec->output_section->name, ".fini") == 0))
11606 {
11607 if (isec->map_head.s->has_toc_reloc
11608 || isec->map_head.s->makes_toc_func_call)
11609 ret = 1;
11610 else if (!isec->map_head.s->call_check_done)
11611 {
11612 int recur;
11613 isec->call_check_in_progress = 1;
11614 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11615 isec->call_check_in_progress = 0;
11616 if (recur != 0)
11617 ret = recur;
11618 }
11619 }
11620
11621 if (ret == 1)
11622 isec->makes_toc_func_call = 1;
4c52953f 11623
9b5ecbd0
AM
11624 return ret;
11625}
11626
721956f4
AM
11627/* The linker repeatedly calls this function for each input section,
11628 in the order that input sections are linked into output sections.
11629 Build lists of input sections to determine groupings between which
11630 we may insert linker stubs. */
11631
9b5ecbd0 11632bfd_boolean
4ce794b7 11633ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
11634{
11635 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11636
4dfe6ac6
NC
11637 if (htab == NULL)
11638 return FALSE;
11639
734b6cf9 11640 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 11641 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 11642 {
3d6f9012
AM
11643 /* This happens to make the list in reverse order,
11644 which is what we want. */
6f20ed8a
AM
11645 htab->sec_info[isec->id].u.list
11646 = htab->sec_info[isec->output_section->id].u.list;
11647 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 11648 }
ad8e1ba5 11649
4c52953f 11650 if (htab->multi_toc_needed)
9b5ecbd0 11651 {
8b974ba3
AM
11652 /* Analyse sections that aren't already flagged as needing a
11653 valid toc pointer. Exclude .fixup for the linux kernel.
11654 .fixup contains branches, but only back to the function that
11655 hit an exception. */
11656 if (!(isec->has_toc_reloc
11657 || (isec->flags & SEC_CODE) == 0
11658 || strcmp (isec->name, ".fixup") == 0
11659 || isec->call_check_done))
11660 {
11661 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 11662 return FALSE;
8b974ba3
AM
11663 }
11664 /* Make all sections use the TOC assigned for this object file.
11665 This will be wrong for pasted sections; We fix that in
11666 check_pasted_section(). */
11667 if (elf_gp (isec->owner) != 0)
11668 htab->toc_curr = elf_gp (isec->owner);
11669 }
11670
6f20ed8a 11671 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 11672 return TRUE;
721956f4
AM
11673}
11674
70cc837d
AM
11675/* Check that all .init and .fini sections use the same toc, if they
11676 have toc relocs. */
11677
11678static bfd_boolean
11679check_pasted_section (struct bfd_link_info *info, const char *name)
11680{
11681 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11682
11683 if (o != NULL)
11684 {
11685 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11686 bfd_vma toc_off = 0;
11687 asection *i;
11688
11689 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11690 if (i->has_toc_reloc)
11691 {
11692 if (toc_off == 0)
6f20ed8a
AM
11693 toc_off = htab->sec_info[i->id].toc_off;
11694 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
11695 return FALSE;
11696 }
6683a28d
AM
11697
11698 if (toc_off == 0)
11699 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11700 if (i->makes_toc_func_call)
11701 {
6f20ed8a 11702 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
11703 break;
11704 }
11705
70cc837d
AM
11706 /* Make sure the whole pasted function uses the same toc offset. */
11707 if (toc_off != 0)
11708 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 11709 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
11710 }
11711 return TRUE;
11712}
11713
11714bfd_boolean
11715ppc64_elf_check_init_fini (struct bfd_link_info *info)
11716{
11717 return (check_pasted_section (info, ".init")
11718 & check_pasted_section (info, ".fini"));
11719}
11720
721956f4
AM
11721/* See whether we can group stub sections together. Grouping stub
11722 sections may result in fewer stubs. More importantly, we need to
11723 put all .init* and .fini* stubs at the beginning of the .init or
11724 .fini output sections respectively, because glibc splits the
11725 _init and _fini functions into multiple parts. Putting a stub in
11726 the middle of a function is not a good idea. */
11727
6f20ed8a
AM
11728static bfd_boolean
11729group_sections (struct bfd_link_info *info,
4ce794b7
AM
11730 bfd_size_type stub_group_size,
11731 bfd_boolean stubs_always_before_branch)
721956f4 11732{
6f20ed8a
AM
11733 struct ppc_link_hash_table *htab;
11734 asection *osec;
7c8fe5c4
AM
11735 bfd_boolean suppress_size_errors;
11736
6f20ed8a
AM
11737 htab = ppc_hash_table (info);
11738 if (htab == NULL)
11739 return FALSE;
11740
7c8fe5c4 11741 suppress_size_errors = FALSE;
7c8fe5c4
AM
11742 if (stub_group_size == 1)
11743 {
11744 /* Default values. */
11745 if (stubs_always_before_branch)
09f92717 11746 stub_group_size = 0x1e00000;
7c8fe5c4 11747 else
09f92717 11748 stub_group_size = 0x1c00000;
7c8fe5c4
AM
11749 suppress_size_errors = TRUE;
11750 }
11751
6f20ed8a 11752 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 11753 {
6f20ed8a
AM
11754 asection *tail;
11755
11756 if (osec->id >= htab->sec_info_arr_size)
11757 continue;
11758
11759 tail = htab->sec_info[osec->id].u.list;
734b6cf9 11760 while (tail != NULL)
721956f4 11761 {
734b6cf9
AM
11762 asection *curr;
11763 asection *prev;
11764 bfd_size_type total;
11765 bfd_boolean big_sec;
11766 bfd_vma curr_toc;
6f20ed8a 11767 struct map_stub *group;
09f92717 11768 bfd_size_type group_size;
734b6cf9
AM
11769
11770 curr = tail;
eea6121a 11771 total = tail->size;
09f92717
AM
11772 group_size = (ppc64_elf_section_data (tail) != NULL
11773 && ppc64_elf_section_data (tail)->has_14bit_branch
11774 ? stub_group_size >> 10 : stub_group_size);
11775
11776 big_sec = total > group_size;
7c8fe5c4 11777 if (big_sec && !suppress_size_errors)
695344c0 11778 /* xgettext:c-format */
871b3ab2 11779 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 11780 tail->owner, tail);
6f20ed8a 11781 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 11782
6f20ed8a 11783 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 11784 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
11785 < (ppc64_elf_section_data (prev) != NULL
11786 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 11787 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 11788 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
11789 curr = prev;
11790
11791 /* OK, the size from the start of CURR to the end is less
09f92717 11792 than group_size and thus can be handled by one stub
734b6cf9 11793 section. (or the tail section is itself larger than
09f92717
AM
11794 group_size, in which case we may be toast.) We should
11795 really be keeping track of the total size of stubs added
11796 here, as stubs contribute to the final output section
11797 size. That's a little tricky, and this way will only
11798 break if stubs added make the total size more than 2^25,
11799 ie. for the default stub_group_size, if stubs total more
11800 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
11801 group = bfd_alloc (curr->owner, sizeof (*group));
11802 if (group == NULL)
11803 return FALSE;
11804 group->link_sec = curr;
11805 group->stub_sec = NULL;
a4b6fadd 11806 group->needs_save_res = 0;
df136d64
AM
11807 group->lr_restore = 0;
11808 group->eh_size = 0;
11809 group->eh_base = 0;
a4b6fadd
AM
11810 group->next = htab->group;
11811 htab->group = group;
734b6cf9 11812 do
721956f4 11813 {
6f20ed8a 11814 prev = htab->sec_info[tail->id].u.list;
734b6cf9 11815 /* Set up this stub group. */
6f20ed8a 11816 htab->sec_info[tail->id].u.group = group;
721956f4 11817 }
734b6cf9
AM
11818 while (tail != curr && (tail = prev) != NULL);
11819
09f92717 11820 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
11821 bytes before the stub section can be handled by it too.
11822 Don't do this if we have a really large section after the
11823 stubs, as adding more stubs increases the chance that
11824 branches may not reach into the stub section. */
11825 if (!stubs_always_before_branch && !big_sec)
11826 {
11827 total = 0;
11828 while (prev != NULL
11829 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
11830 < (ppc64_elf_section_data (prev) != NULL
11831 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
11832 ? (group_size = stub_group_size >> 10)
11833 : group_size))
6f20ed8a 11834 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
11835 {
11836 tail = prev;
6f20ed8a
AM
11837 prev = htab->sec_info[tail->id].u.list;
11838 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
11839 }
11840 }
11841 tail = prev;
721956f4
AM
11842 }
11843 }
6f20ed8a 11844 return TRUE;
721956f4
AM
11845}
11846
58d180e8
AM
11847static const unsigned char glink_eh_frame_cie[] =
11848{
11849 0, 0, 0, 16, /* length. */
11850 0, 0, 0, 0, /* id. */
11851 1, /* CIE version. */
11852 'z', 'R', 0, /* Augmentation string. */
11853 4, /* Code alignment. */
11854 0x78, /* Data alignment. */
11855 65, /* RA reg. */
11856 1, /* Augmentation size. */
11857 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 11858 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
11859};
11860
d969d15f
AM
11861/* Stripping output sections is normally done before dynamic section
11862 symbols have been allocated. This function is called later, and
11863 handles cases like htab->brlt which is mapped to its own output
11864 section. */
11865
11866static void
11867maybe_strip_output (struct bfd_link_info *info, asection *isec)
11868{
11869 if (isec->size == 0
11870 && isec->output_section->size == 0
53d8967a 11871 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
11872 && !bfd_section_removed_from_list (info->output_bfd,
11873 isec->output_section)
11874 && elf_section_data (isec->output_section)->dynindx == 0)
11875 {
11876 isec->output_section->flags |= SEC_EXCLUDE;
11877 bfd_section_list_remove (info->output_bfd, isec->output_section);
11878 info->output_bfd->section_count--;
11879 }
11880}
11881
721956f4
AM
11882/* Determine and set the size of the stub section for a final link.
11883
11884 The basic idea here is to examine all the relocations looking for
11885 PC-relative calls to a target that is unreachable with a "bl"
11886 instruction. */
11887
b34976b6 11888bfd_boolean
e7d1c40c 11889ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
11890{
11891 bfd_size_type stub_group_size;
b34976b6 11892 bfd_boolean stubs_always_before_branch;
721956f4
AM
11893 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11894
4dfe6ac6
NC
11895 if (htab == NULL)
11896 return FALSE;
11897
0e1862bb 11898 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 11899 htab->params->plt_thread_safe = 1;
b9e5796b 11900 if (!htab->opd_abi)
e7d1c40c
AM
11901 htab->params->plt_thread_safe = 0;
11902 else if (htab->params->plt_thread_safe == -1)
794e51c0 11903 {
e2458743 11904 static const char *const thread_starter[] =
794e51c0
AM
11905 {
11906 "pthread_create",
11907 /* libstdc++ */
11908 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11909 /* librt */
11910 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11911 "mq_notify", "create_timer",
11912 /* libanl */
11913 "getaddrinfo_a",
11914 /* libgomp */
2300b5a1 11915 "GOMP_parallel",
794e51c0 11916 "GOMP_parallel_start",
2300b5a1 11917 "GOMP_parallel_loop_static",
794e51c0 11918 "GOMP_parallel_loop_static_start",
2300b5a1 11919 "GOMP_parallel_loop_dynamic",
794e51c0 11920 "GOMP_parallel_loop_dynamic_start",
2300b5a1 11921 "GOMP_parallel_loop_guided",
794e51c0 11922 "GOMP_parallel_loop_guided_start",
2300b5a1 11923 "GOMP_parallel_loop_runtime",
794e51c0 11924 "GOMP_parallel_loop_runtime_start",
2300b5a1 11925 "GOMP_parallel_sections",
68ffbac6 11926 "GOMP_parallel_sections_start",
f9dffbf0
AM
11927 /* libgo */
11928 "__go_go",
794e51c0
AM
11929 };
11930 unsigned i;
11931
a4b6fadd 11932 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
11933 {
11934 struct elf_link_hash_entry *h;
11935 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11936 FALSE, FALSE, TRUE);
e7d1c40c
AM
11937 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
11938 if (htab->params->plt_thread_safe)
794e51c0
AM
11939 break;
11940 }
11941 }
e7d1c40c
AM
11942 stubs_always_before_branch = htab->params->group_size < 0;
11943 if (htab->params->group_size < 0)
11944 stub_group_size = -htab->params->group_size;
721956f4 11945 else
e7d1c40c 11946 stub_group_size = htab->params->group_size;
721956f4 11947
6f20ed8a
AM
11948 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
11949 return FALSE;
721956f4 11950
c9301e31
AM
11951#define STUB_SHRINK_ITER 20
11952 /* Loop until no stubs added. After iteration 20 of this loop we may
11953 exit on a stub section shrinking. This is to break out of a
11954 pathological case where adding stubs on one iteration decreases
11955 section gaps (perhaps due to alignment), which then requires
11956 fewer or smaller stubs on the next iteration. */
11957
721956f4
AM
11958 while (1)
11959 {
11960 bfd *input_bfd;
11961 unsigned int bfd_indx;
a4b6fadd 11962 struct map_stub *group;
721956f4
AM
11963
11964 htab->stub_iteration += 1;
721956f4
AM
11965
11966 for (input_bfd = info->input_bfds, bfd_indx = 0;
11967 input_bfd != NULL;
c72f2fb2 11968 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
11969 {
11970 Elf_Internal_Shdr *symtab_hdr;
11971 asection *section;
6cdc0ccc 11972 Elf_Internal_Sym *local_syms = NULL;
721956f4 11973
0c8d6e5c 11974 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
11975 continue;
11976
721956f4 11977 /* We'll need the symbol table in a second. */
0ffa91dd 11978 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
11979 if (symtab_hdr->sh_info == 0)
11980 continue;
11981
721956f4
AM
11982 /* Walk over each section attached to the input bfd. */
11983 for (section = input_bfd->sections;
11984 section != NULL;
11985 section = section->next)
11986 {
721956f4 11987 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
11988
11989 /* If there aren't any relocs, then there's nothing more
11990 to do. */
11991 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
11992 || (section->flags & SEC_ALLOC) == 0
11993 || (section->flags & SEC_LOAD) == 0
11994 || (section->flags & SEC_CODE) == 0
721956f4
AM
11995 || section->reloc_count == 0)
11996 continue;
11997
11998 /* If this section is a link-once section that will be
11999 discarded, then don't create any stubs. */
12000 if (section->output_section == NULL
927be08e 12001 || section->output_section->owner != info->output_bfd)
721956f4
AM
12002 continue;
12003
1e2f5b6e
AM
12004 /* Get the relocs. */
12005 internal_relocs
4ce794b7 12006 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12007 info->keep_memory);
721956f4 12008 if (internal_relocs == NULL)
1e2f5b6e 12009 goto error_ret_free_local;
721956f4
AM
12010
12011 /* Now examine each relocation. */
12012 irela = internal_relocs;
12013 irelaend = irela + section->reloc_count;
12014 for (; irela < irelaend; irela++)
12015 {
4ce794b7
AM
12016 enum elf_ppc64_reloc_type r_type;
12017 unsigned int r_indx;
721956f4
AM
12018 enum ppc_stub_type stub_type;
12019 struct ppc_stub_hash_entry *stub_entry;
8387904d 12020 asection *sym_sec, *code_sec;
e054468f 12021 bfd_vma sym_value, code_value;
721956f4 12022 bfd_vma destination;
6911b7dc 12023 unsigned long local_off;
8843416a 12024 bfd_boolean ok_dest;
721956f4 12025 struct ppc_link_hash_entry *hash;
8387904d 12026 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12027 struct elf_link_hash_entry *h;
12028 Elf_Internal_Sym *sym;
721956f4
AM
12029 char *stub_name;
12030 const asection *id_sec;
74f0fb50 12031 struct _opd_sec_data *opd;
e054468f 12032 struct plt_entry *plt_ent;
721956f4
AM
12033
12034 r_type = ELF64_R_TYPE (irela->r_info);
12035 r_indx = ELF64_R_SYM (irela->r_info);
12036
4ce794b7 12037 if (r_type >= R_PPC64_max)
721956f4
AM
12038 {
12039 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12040 goto error_ret_free_internal;
721956f4
AM
12041 }
12042
12043 /* Only look for stubs on branch instructions. */
4ce794b7 12044 if (r_type != R_PPC64_REL24
05d0e962 12045 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
12046 && r_type != R_PPC64_REL14
12047 && r_type != R_PPC64_REL14_BRTAKEN
12048 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12049 continue;
12050
12051 /* Now determine the call target, its name, value,
12052 section. */
411e1bfb
AM
12053 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12054 r_indx, input_bfd))
12055 goto error_ret_free_internal;
12056 hash = (struct ppc_link_hash_entry *) h;
12057
8843416a 12058 ok_dest = FALSE;
8387904d 12059 fdh = NULL;
7fe2b9a6 12060 sym_value = 0;
411e1bfb 12061 if (hash == NULL)
721956f4 12062 {
411e1bfb 12063 sym_value = sym->st_value;
c27b8c2a
AM
12064 if (sym_sec != NULL
12065 && sym_sec->output_section != NULL)
12066 ok_dest = TRUE;
721956f4 12067 }
7fe2b9a6
AM
12068 else if (hash->elf.root.type == bfd_link_hash_defined
12069 || hash->elf.root.type == bfd_link_hash_defweak)
12070 {
12071 sym_value = hash->elf.root.u.def.value;
12072 if (sym_sec->output_section != NULL)
12073 ok_dest = TRUE;
12074 }
12075 else if (hash->elf.root.type == bfd_link_hash_undefweak
12076 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12077 {
99877b66 12078 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12079 use the func descriptor sym instead if it is
12080 defined. */
ceb1f1ef 12081 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12082 && hash->oh != NULL)
8387904d 12083 {
8c5b4e52 12084 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12085 if (fdh->elf.root.type == bfd_link_hash_defined
12086 || fdh->elf.root.type == bfd_link_hash_defweak)
12087 {
12088 sym_sec = fdh->elf.root.u.def.section;
12089 sym_value = fdh->elf.root.u.def.value;
12090 if (sym_sec->output_section != NULL)
12091 ok_dest = TRUE;
12092 }
99877b66
AM
12093 else
12094 fdh = NULL;
8387904d 12095 }
7fe2b9a6
AM
12096 }
12097 else
12098 {
12099 bfd_set_error (bfd_error_bad_value);
12100 goto error_ret_free_internal;
721956f4
AM
12101 }
12102
8843416a 12103 destination = 0;
6911b7dc 12104 local_off = 0;
8843416a
AM
12105 if (ok_dest)
12106 {
12107 sym_value += irela->r_addend;
12108 destination = (sym_value
12109 + sym_sec->output_offset
12110 + sym_sec->output_section->vma);
6911b7dc
AM
12111 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12112 ? hash->elf.other
12113 : sym->st_other);
8843416a
AM
12114 }
12115
8387904d 12116 code_sec = sym_sec;
e054468f 12117 code_value = sym_value;
74f0fb50
AM
12118 opd = get_opd_info (sym_sec);
12119 if (opd != NULL)
8387904d
AM
12120 {
12121 bfd_vma dest;
12122
74f0fb50 12123 if (hash == NULL && opd->adjust != NULL)
8387904d 12124 {
51aecdc5 12125 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12126 if (adjust == -1)
12127 continue;
e054468f 12128 code_value += adjust;
8387904d
AM
12129 sym_value += adjust;
12130 }
12131 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12132 &code_sec, &code_value, FALSE);
8387904d
AM
12133 if (dest != (bfd_vma) -1)
12134 {
12135 destination = dest;
12136 if (fdh != NULL)
12137 {
12138 /* Fixup old ABI sym to point at code
12139 entry. */
99877b66 12140 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12141 hash->elf.root.u.def.section = code_sec;
e054468f 12142 hash->elf.root.u.def.value = code_value;
8387904d
AM
12143 }
12144 }
12145 }
12146
721956f4 12147 /* Determine what (if any) linker stub is needed. */
e054468f 12148 plt_ent = NULL;
721956f4 12149 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12150 &plt_ent, destination,
12151 local_off);
ad8e1ba5 12152
05d0e962
AM
12153 if (r_type == R_PPC64_REL24_NOTOC)
12154 {
12155 if (stub_type == ppc_stub_plt_call)
12156 stub_type = ppc_stub_plt_call_notoc;
12157 else if (stub_type == ppc_stub_long_branch
12158 || (code_sec != NULL
12159 && code_sec->output_section != NULL
12160 && (((hash ? hash->elf.other : sym->st_other)
12161 & STO_PPC64_LOCAL_MASK)
12162 != 1 << STO_PPC64_LOCAL_BIT)))
12163 stub_type = ppc_stub_long_branch_notoc;
12164 }
12165 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
12166 {
12167 /* Check whether we need a TOC adjusting stub.
12168 Since the linker pastes together pieces from
12169 different object files when creating the
12170 _init and _fini functions, it may be that a
12171 call to what looks like a local sym is in
12172 fact a call needing a TOC adjustment. */
33cb30a1
AM
12173 if ((code_sec != NULL
12174 && code_sec->output_section != NULL
12175 && (htab->sec_info[code_sec->id].toc_off
12176 != htab->sec_info[section->id].toc_off)
12177 && (code_sec->has_toc_reloc
12178 || code_sec->makes_toc_func_call))
12179 || (((hash ? hash->elf.other : sym->st_other)
12180 & STO_PPC64_LOCAL_MASK)
12181 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
12182 stub_type = ppc_stub_long_branch_r2off;
12183 }
12184
721956f4
AM
12185 if (stub_type == ppc_stub_none)
12186 continue;
12187
411e1bfb
AM
12188 /* __tls_get_addr calls might be eliminated. */
12189 if (stub_type != ppc_stub_plt_call
05d0e962 12190 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 12191 && hash != NULL
8387904d
AM
12192 && (hash == htab->tls_get_addr
12193 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12194 && section->has_tls_reloc
12195 && irela != internal_relocs)
12196 {
12197 /* Get tls info. */
f961d9dd 12198 unsigned char *tls_mask;
411e1bfb 12199
3a71aa26 12200 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12201 irela - 1, input_bfd))
12202 goto error_ret_free_internal;
37da22e5 12203 if ((*tls_mask & TLS_TLS) != 0)
411e1bfb
AM
12204 continue;
12205 }
12206
f378ab09 12207 if (stub_type == ppc_stub_plt_call)
794e51c0 12208 {
6e1816be
AM
12209 if (!htab->opd_abi
12210 && htab->params->plt_localentry0 != 0
12211 && is_elfv2_localentry0 (&hash->elf))
12212 htab->has_plt_localentry0 = 1;
12213 else if (irela + 1 < irelaend
12214 && irela[1].r_offset == irela->r_offset + 4
12215 && (ELF64_R_TYPE (irela[1].r_info)
12216 == R_PPC64_TOCSAVE))
f378ab09
AM
12217 {
12218 if (!tocsave_find (htab, INSERT,
12219 &local_syms, irela + 1, input_bfd))
12220 goto error_ret_free_internal;
12221 }
f378ab09
AM
12222 else
12223 stub_type = ppc_stub_plt_call_r2save;
794e51c0 12224 }
3b421ab3 12225
721956f4 12226 /* Support for grouping stub sections. */
6f20ed8a 12227 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12228
12229 /* Get the name of this stub. */
12230 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12231 if (!stub_name)
12232 goto error_ret_free_internal;
12233
12234 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12235 stub_name, FALSE, FALSE);
721956f4
AM
12236 if (stub_entry != NULL)
12237 {
05d0e962
AM
12238 enum ppc_stub_type old_type;
12239 /* A stub has already been created, but it may
12240 not be the required type. We shouldn't be
12241 transitioning from plt_call to long_branch
12242 stubs or vice versa, but we might be
12243 upgrading from plt_call to plt_call_r2save or
12244 from long_branch to long_branch_r2off. */
721956f4 12245 free (stub_name);
05d0e962
AM
12246 old_type = stub_entry->stub_type;
12247 switch (old_type)
12248 {
12249 default:
12250 abort ();
12251
12252 case ppc_stub_save_res:
12253 continue;
12254
12255 case ppc_stub_plt_call:
12256 case ppc_stub_plt_call_r2save:
12257 case ppc_stub_plt_call_notoc:
12258 case ppc_stub_plt_call_both:
12259 if (stub_type == ppc_stub_plt_call)
12260 continue;
12261 else if (stub_type == ppc_stub_plt_call_r2save)
12262 {
12263 if (old_type == ppc_stub_plt_call_notoc)
12264 stub_type = ppc_stub_plt_call_both;
12265 }
12266 else if (stub_type == ppc_stub_plt_call_notoc)
12267 {
12268 if (old_type == ppc_stub_plt_call_r2save)
12269 stub_type = ppc_stub_plt_call_both;
12270 }
12271 else
12272 abort ();
12273 break;
12274
12275 case ppc_stub_plt_branch:
12276 case ppc_stub_plt_branch_r2off:
12277 case ppc_stub_plt_branch_notoc:
12278 case ppc_stub_plt_branch_both:
12279 old_type += (ppc_stub_long_branch
12280 - ppc_stub_plt_branch);
12281 /* Fall through. */
12282 case ppc_stub_long_branch:
12283 case ppc_stub_long_branch_r2off:
12284 case ppc_stub_long_branch_notoc:
12285 case ppc_stub_long_branch_both:
12286 if (stub_type == ppc_stub_long_branch)
12287 continue;
12288 else if (stub_type == ppc_stub_long_branch_r2off)
12289 {
12290 if (old_type == ppc_stub_long_branch_notoc)
12291 stub_type = ppc_stub_long_branch_both;
12292 }
12293 else if (stub_type == ppc_stub_long_branch_notoc)
12294 {
12295 if (old_type == ppc_stub_long_branch_r2off)
12296 stub_type = ppc_stub_long_branch_both;
12297 }
12298 else
12299 abort ();
12300 break;
12301 }
12302 if (old_type < stub_type)
794e51c0 12303 stub_entry->stub_type = stub_type;
721956f4
AM
12304 continue;
12305 }
12306
25f53a85 12307 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12308 if (stub_entry == NULL)
12309 {
12310 free (stub_name);
6cdc0ccc
AM
12311 error_ret_free_internal:
12312 if (elf_section_data (section)->relocs == NULL)
12313 free (internal_relocs);
12314 error_ret_free_local:
12315 if (local_syms != NULL
12316 && (symtab_hdr->contents
12317 != (unsigned char *) local_syms))
12318 free (local_syms);
b34976b6 12319 return FALSE;
721956f4
AM
12320 }
12321
ad8e1ba5 12322 stub_entry->stub_type = stub_type;
05d0e962
AM
12323 if (stub_type >= ppc_stub_plt_call
12324 && stub_type <= ppc_stub_plt_call_both)
e054468f 12325 {
05d0e962
AM
12326 stub_entry->target_value = sym_value;
12327 stub_entry->target_section = sym_sec;
e054468f
AM
12328 }
12329 else
12330 {
05d0e962
AM
12331 stub_entry->target_value = code_value;
12332 stub_entry->target_section = code_sec;
e054468f 12333 }
721956f4 12334 stub_entry->h = hash;
e054468f 12335 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
12336 stub_entry->symtype
12337 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 12338 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 12339
3d58e1fc
AM
12340 if (hash != NULL
12341 && (hash->elf.root.type == bfd_link_hash_defined
12342 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 12343 htab->stub_globals += 1;
721956f4
AM
12344 }
12345
12346 /* We're done with the internal relocs, free them. */
6cdc0ccc 12347 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12348 free (internal_relocs);
721956f4 12349 }
6cdc0ccc
AM
12350
12351 if (local_syms != NULL
12352 && symtab_hdr->contents != (unsigned char *) local_syms)
12353 {
12354 if (!info->keep_memory)
12355 free (local_syms);
12356 else
12357 symtab_hdr->contents = (unsigned char *) local_syms;
12358 }
721956f4
AM
12359 }
12360
5c3dead3 12361 /* We may have added some stubs. Find out the new size of the
721956f4 12362 stub sections. */
d4aaa2a0 12363 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
12364 {
12365 group->lr_restore = 0;
12366 group->eh_size = 0;
12367 if (group->stub_sec != NULL)
12368 {
12369 asection *stub_sec = group->stub_sec;
12370
12371 if (htab->stub_iteration <= STUB_SHRINK_ITER
12372 || stub_sec->rawsize < stub_sec->size)
12373 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12374 stub_sec->rawsize = stub_sec->size;
12375 stub_sec->size = 0;
12376 stub_sec->reloc_count = 0;
12377 stub_sec->flags &= ~SEC_RELOC;
12378 }
12379 }
eea6121a 12380
ba21f564
AM
12381 if (htab->stub_iteration <= STUB_SHRINK_ITER
12382 || htab->brlt->rawsize < htab->brlt->size)
12383 htab->brlt->rawsize = htab->brlt->size;
eea6121a 12384 htab->brlt->size = 0;
84f5d08e
AM
12385 htab->brlt->reloc_count = 0;
12386 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12387 if (htab->relbrlt != NULL)
eea6121a 12388 htab->relbrlt->size = 0;
721956f4 12389
63bc6f6c 12390 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12391
a4b6fadd
AM
12392 for (group = htab->group; group != NULL; group = group->next)
12393 if (group->needs_save_res)
12394 group->stub_sec->size += htab->sfpr->size;
12395
176a0d42
AM
12396 if (info->emitrelocations
12397 && htab->glink != NULL && htab->glink->size != 0)
12398 {
12399 htab->glink->reloc_count = 1;
12400 htab->glink->flags |= SEC_RELOC;
12401 }
12402
58d180e8
AM
12403 if (htab->glink_eh_frame != NULL
12404 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 12405 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 12406 {
2e0ce1c8 12407 size_t size = 0, align = 4;
58d180e8 12408
d4aaa2a0 12409 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
12410 if (group->eh_size != 0)
12411 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 12412 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 12413 size += (24 + align - 1) & -align;
58d180e8 12414 if (size != 0)
2e0ce1c8
AM
12415 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12416 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12417 size = (size + align - 1) & -align;
58d180e8
AM
12418 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12419 htab->glink_eh_frame->size = size;
12420 }
12421
e7d1c40c 12422 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
12423 for (group = htab->group; group != NULL; group = group->next)
12424 if (group->stub_sec != NULL)
691d2e9a
AM
12425 {
12426 int align = abs (htab->params->plt_stub_align);
12427 group->stub_sec->size
12428 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12429 }
d4aaa2a0
AM
12430
12431 for (group = htab->group; group != NULL; group = group->next)
12432 if (group->stub_sec != NULL
12433 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 12434 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 12435 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
12436 break;
12437
d4aaa2a0 12438 if (group == NULL
ba21f564
AM
12439 && (htab->brlt->rawsize == htab->brlt->size
12440 || (htab->stub_iteration > STUB_SHRINK_ITER
12441 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
12442 && (htab->glink_eh_frame == NULL
12443 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12444 break;
12445
721956f4 12446 /* Ask the linker to do its stuff. */
e7d1c40c 12447 (*htab->params->layout_sections_again) ();
721956f4
AM
12448 }
12449
da44f4e5
AM
12450 if (htab->glink_eh_frame != NULL
12451 && htab->glink_eh_frame->size != 0)
12452 {
12453 bfd_vma val;
12454 bfd_byte *p, *last_fde;
12455 size_t last_fde_len, size, align, pad;
d4aaa2a0 12456 struct map_stub *group;
da44f4e5 12457
df136d64
AM
12458 /* It is necessary to at least have a rough outline of the
12459 linker generated CIEs and FDEs written before
12460 bfd_elf_discard_info is run, in order for these FDEs to be
12461 indexed in .eh_frame_hdr. */
da44f4e5
AM
12462 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12463 if (p == NULL)
12464 return FALSE;
12465 htab->glink_eh_frame->contents = p;
12466 last_fde = p;
2e0ce1c8 12467 align = 4;
da44f4e5
AM
12468
12469 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12470 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 12471 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 12472 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 12473 p += last_fde_len + 4;
da44f4e5 12474
d4aaa2a0 12475 for (group = htab->group; group != NULL; group = group->next)
df136d64 12476 if (group->eh_size != 0)
da44f4e5 12477 {
df136d64 12478 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 12479 last_fde = p;
df136d64 12480 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 12481 /* FDE length. */
2e0ce1c8 12482 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12483 p += 4;
12484 /* CIE pointer. */
12485 val = p - htab->glink_eh_frame->contents;
12486 bfd_put_32 (htab->elf.dynobj, val, p);
12487 p += 4;
12488 /* Offset to stub section, written later. */
12489 p += 4;
12490 /* stub section size. */
d4aaa2a0 12491 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
12492 p += 4;
12493 /* Augmentation. */
12494 p += 1;
df136d64
AM
12495 /* Make sure we don't have all nops. This is enough for
12496 elf-eh-frame.c to detect the last non-nop opcode. */
12497 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 12498 p = last_fde + last_fde_len + 4;
da44f4e5
AM
12499 }
12500 if (htab->glink != NULL && htab->glink->size != 0)
12501 {
12502 last_fde = p;
2e0ce1c8 12503 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 12504 /* FDE length. */
2e0ce1c8 12505 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12506 p += 4;
12507 /* CIE pointer. */
12508 val = p - htab->glink_eh_frame->contents;
12509 bfd_put_32 (htab->elf.dynobj, val, p);
12510 p += 4;
12511 /* Offset to .glink, written later. */
12512 p += 4;
12513 /* .glink size. */
12514 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12515 p += 4;
12516 /* Augmentation. */
12517 p += 1;
12518
12519 *p++ = DW_CFA_advance_loc + 1;
12520 *p++ = DW_CFA_register;
12521 *p++ = 65;
9f08fa5c 12522 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 12523 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
12524 *p++ = DW_CFA_restore_extended;
12525 *p++ = 65;
2e0ce1c8 12526 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
12527 }
12528 /* Subsume any padding into the last FDE if user .eh_frame
12529 sections are aligned more than glink_eh_frame. Otherwise any
12530 zero padding will be seen as a terminator. */
2e0ce1c8 12531 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 12532 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 12533 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
12534 htab->glink_eh_frame->size = size + pad;
12535 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12536 }
12537
d969d15f
AM
12538 maybe_strip_output (info, htab->brlt);
12539 if (htab->glink_eh_frame != NULL)
12540 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12541
b34976b6 12542 return TRUE;
721956f4
AM
12543}
12544
12545/* Called after we have determined section placement. If sections
805fc799 12546 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12547
805fc799 12548bfd_vma
1c865ab2 12549ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12550{
805fc799 12551 asection *s;
a27e685f 12552 bfd_vma TOCstart, adjust;
721956f4 12553
43417696
AM
12554 if (info != NULL)
12555 {
12556 struct elf_link_hash_entry *h;
12557 struct elf_link_hash_table *htab = elf_hash_table (info);
12558
12559 if (is_elf_hash_table (htab)
12560 && htab->hgot != NULL)
12561 h = htab->hgot;
12562 else
12563 {
12564 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12565 if (is_elf_hash_table (htab))
12566 htab->hgot = h;
12567 }
12568 if (h != NULL
12569 && h->root.type == bfd_link_hash_defined
12570 && !h->root.linker_def
12571 && (!is_elf_hash_table (htab)
12572 || h->def_regular))
12573 {
12574 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12575 + h->root.u.def.section->output_offset
12576 + h->root.u.def.section->output_section->vma);
12577 _bfd_set_gp_value (obfd, TOCstart);
12578 return TOCstart;
12579 }
12580 }
12581
805fc799
AM
12582 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12583 order. The TOC starts where the first of these sections starts. */
12584 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12585 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12586 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12587 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12588 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12589 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12590 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12591 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12592 {
12593 /* This may happen for
12594 o references to TOC base (SYM@toc / TOC[tc0]) without a
12595 .toc directive
12596 o bad linker script
12597 o --gc-sections and empty TOC sections
12598
12599 FIXME: Warn user? */
12600
12601 /* Look for a likely section. We probably won't even be
12602 using TOCstart. */
12603 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12604 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12605 | SEC_EXCLUDE))
805fc799
AM
12606 == (SEC_ALLOC | SEC_SMALL_DATA))
12607 break;
721956f4 12608 if (s == NULL)
805fc799 12609 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12610 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12611 == (SEC_ALLOC | SEC_SMALL_DATA))
12612 break;
721956f4 12613 if (s == NULL)
805fc799 12614 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12615 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12616 == SEC_ALLOC)
805fc799 12617 break;
721956f4 12618 if (s == NULL)
805fc799 12619 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12620 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12621 break;
12622 }
721956f4 12623
805fc799
AM
12624 TOCstart = 0;
12625 if (s != NULL)
12626 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12627
a27e685f
AM
12628 /* Force alignment. */
12629 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12630 TOCstart -= adjust;
1c865ab2
AM
12631 _bfd_set_gp_value (obfd, TOCstart);
12632
810d4e75 12633 if (info != NULL && s != NULL)
1c865ab2
AM
12634 {
12635 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12636
810d4e75
AM
12637 if (htab != NULL)
12638 {
12639 if (htab->elf.hgot != NULL)
12640 {
a27e685f 12641 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
12642 htab->elf.hgot->root.u.def.section = s;
12643 }
12644 }
12645 else
1c865ab2 12646 {
810d4e75
AM
12647 struct bfd_link_hash_entry *bh = NULL;
12648 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
12649 s, TOC_BASE_OFF - adjust,
12650 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
12651 }
12652 }
805fc799 12653 return TOCstart;
721956f4
AM
12654}
12655
a345bc8d 12656/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 12657 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
12658
12659static bfd_boolean
49c09209 12660build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
12661{
12662 struct bfd_link_info *info;
12663 struct ppc_link_hash_table *htab;
49c09209 12664 struct plt_entry *ent;
a345bc8d
AM
12665 asection *s;
12666
12667 if (h->root.type == bfd_link_hash_indirect)
12668 return TRUE;
12669
49c09209
AM
12670 info = inf;
12671 htab = ppc_hash_table (info);
12672 if (htab == NULL)
12673 return FALSE;
12674
12675 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12676 if (ent->plt.offset != (bfd_vma) -1)
12677 {
12678 /* This symbol has an entry in the procedure linkage
12679 table. Set it up. */
12680 Elf_Internal_Rela rela;
2d7ad24e 12681 asection *plt, *relplt;
49c09209
AM
12682 bfd_byte *loc;
12683
12684 if (!htab->elf.dynamic_sections_created
12685 || h->dynindx == -1)
12686 {
12687 if (!(h->def_regular
12688 && (h->root.type == bfd_link_hash_defined
12689 || h->root.type == bfd_link_hash_defweak)))
12690 continue;
2d7ad24e
AM
12691 if (h->type == STT_GNU_IFUNC)
12692 {
12693 plt = htab->elf.iplt;
12694 relplt = htab->elf.irelplt;
12695 htab->local_ifunc_resolver = 1;
12696 if (htab->opd_abi)
12697 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
12698 else
12699 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12700 }
49c09209 12701 else
2d7ad24e
AM
12702 {
12703 plt = htab->pltlocal;
12704 if (bfd_link_pic (info))
12705 {
12706 relplt = htab->relpltlocal;
12707 if (htab->opd_abi)
12708 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
12709 else
12710 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12711 }
12712 else
12713 relplt = NULL;
12714 }
49c09209
AM
12715 rela.r_addend = (h->root.u.def.value
12716 + h->root.u.def.section->output_offset
12717 + h->root.u.def.section->output_section->vma
12718 + ent->addend);
2d7ad24e
AM
12719
12720 if (relplt == NULL)
12721 {
12722 loc = plt->contents + ent->plt.offset;
12723 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
12724 if (htab->opd_abi)
12725 {
12726 bfd_vma toc = elf_gp (info->output_bfd);
12727 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
12728 bfd_put_64 (info->output_bfd, toc, loc + 8);
12729 }
12730 }
12731 else
12732 {
12733 rela.r_offset = (plt->output_section->vma
12734 + plt->output_offset
12735 + ent->plt.offset);
12736 loc = relplt->contents + (relplt->reloc_count++
12737 * sizeof (Elf64_External_Rela));
12738 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12739 }
49c09209
AM
12740 }
12741 else
12742 {
12743 rela.r_offset = (htab->elf.splt->output_section->vma
12744 + htab->elf.splt->output_offset
12745 + ent->plt.offset);
12746 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
12747 rela.r_addend = ent->addend;
12748 loc = (htab->elf.srelplt->contents
12749 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
12750 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
12751 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
12752 htab->maybe_local_ifunc_resolver = 1;
2d7ad24e 12753 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 12754 }
49c09209
AM
12755 }
12756
a345bc8d
AM
12757 if (!h->pointer_equality_needed)
12758 return TRUE;
12759
12760 if (h->def_regular)
12761 return TRUE;
12762
9e390558 12763 s = htab->global_entry;
49c09209
AM
12764 if (s == NULL || s->size == 0)
12765 return TRUE;
12766
12767 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12768 if (ent->plt.offset != (bfd_vma) -1
12769 && ent->addend == 0)
a345bc8d
AM
12770 {
12771 bfd_byte *p;
12772 asection *plt;
12773 bfd_vma off;
12774
a345bc8d 12775 p = s->contents + h->root.u.def.value;
33e44f2e 12776 plt = htab->elf.splt;
a345bc8d
AM
12777 if (!htab->elf.dynamic_sections_created
12778 || h->dynindx == -1)
2d7ad24e
AM
12779 {
12780 if (h->type == STT_GNU_IFUNC)
12781 plt = htab->elf.iplt;
12782 else
12783 plt = htab->pltlocal;
12784 }
49c09209 12785 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
12786 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12787
12788 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12789 {
12790 info->callbacks->einfo
c1c8c1ef 12791 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
12792 h->root.root.string);
12793 bfd_set_error (bfd_error_bad_value);
12794 htab->stub_error = TRUE;
12795 }
12796
7341d5e2
AM
12797 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12798 if (htab->params->emit_stub_syms)
12799 {
12800 size_t len = strlen (h->root.root.string);
12801 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12802
12803 if (name == NULL)
12804 return FALSE;
12805
12806 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12807 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12808 if (h == NULL)
12809 return FALSE;
12810 if (h->root.type == bfd_link_hash_new)
12811 {
12812 h->root.type = bfd_link_hash_defined;
12813 h->root.u.def.section = s;
12814 h->root.u.def.value = p - s->contents;
12815 h->ref_regular = 1;
12816 h->def_regular = 1;
12817 h->ref_regular_nonweak = 1;
12818 h->forced_local = 1;
12819 h->non_elf = 0;
2ec55de3 12820 h->root.linker_def = 1;
7341d5e2
AM
12821 }
12822 }
12823
a345bc8d
AM
12824 if (PPC_HA (off) != 0)
12825 {
12826 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12827 p += 4;
12828 }
12829 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12830 p += 4;
12831 bfd_put_32 (s->owner, MTCTR_R12, p);
12832 p += 4;
407aa07c 12833 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
12834 break;
12835 }
12836 return TRUE;
12837}
12838
49c09209
AM
12839/* Write PLT relocs for locals. */
12840
12841static bfd_boolean
12842write_plt_relocs_for_local_syms (struct bfd_link_info *info)
12843{
12844 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12845 bfd *ibfd;
12846
12847 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12848 {
12849 struct got_entry **lgot_ents, **end_lgot_ents;
12850 struct plt_entry **local_plt, **lplt, **end_local_plt;
12851 Elf_Internal_Shdr *symtab_hdr;
12852 bfd_size_type locsymcount;
12853 Elf_Internal_Sym *local_syms = NULL;
12854 struct plt_entry *ent;
12855
12856 if (!is_ppc64_elf (ibfd))
12857 continue;
12858
12859 lgot_ents = elf_local_got_ents (ibfd);
12860 if (!lgot_ents)
12861 continue;
12862
12863 symtab_hdr = &elf_symtab_hdr (ibfd);
12864 locsymcount = symtab_hdr->sh_info;
12865 end_lgot_ents = lgot_ents + locsymcount;
12866 local_plt = (struct plt_entry **) end_lgot_ents;
12867 end_local_plt = local_plt + locsymcount;
12868 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
12869 for (ent = *lplt; ent != NULL; ent = ent->next)
12870 if (ent->plt.offset != (bfd_vma) -1)
12871 {
12872 Elf_Internal_Sym *sym;
12873 asection *sym_sec;
12874 asection *plt, *relplt;
12875 bfd_byte *loc;
12876 bfd_vma val;
49c09209
AM
12877
12878 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
12879 lplt - local_plt, ibfd))
12880 {
12881 if (local_syms != NULL
12882 && symtab_hdr->contents != (unsigned char *) local_syms)
12883 free (local_syms);
12884 return FALSE;
12885 }
12886
12887 val = sym->st_value + ent->addend;
ba85c15d
AM
12888 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
12889 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
49c09209
AM
12890 if (sym_sec != NULL && sym_sec->output_section != NULL)
12891 val += sym_sec->output_offset + sym_sec->output_section->vma;
12892
2d7ad24e
AM
12893 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12894 {
12895 htab->local_ifunc_resolver = 1;
12896 plt = htab->elf.iplt;
12897 relplt = htab->elf.irelplt;
12898 }
12899 else
12900 {
12901 plt = htab->pltlocal;
12902 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
12903 }
49c09209 12904
2d7ad24e
AM
12905 if (relplt == NULL)
12906 {
12907 loc = plt->contents + ent->plt.offset;
12908 bfd_put_64 (info->output_bfd, val, loc);
12909 if (htab->opd_abi)
12910 {
12911 bfd_vma toc = elf_gp (ibfd);
12912 bfd_put_64 (info->output_bfd, toc, loc + 8);
12913 }
12914 }
49c09209 12915 else
2d7ad24e
AM
12916 {
12917 Elf_Internal_Rela rela;
12918 rela.r_offset = (ent->plt.offset
12919 + plt->output_offset
12920 + plt->output_section->vma);
12921 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12922 {
12923 if (htab->opd_abi)
12924 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
12925 else
12926 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12927 }
12928 else
12929 {
12930 if (htab->opd_abi)
12931 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
12932 else
12933 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12934 }
12935 rela.r_addend = val;
12936 loc = relplt->contents + (relplt->reloc_count++
12937 * sizeof (Elf64_External_Rela));
12938 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12939 }
49c09209
AM
12940 }
12941
12942 if (local_syms != NULL
12943 && symtab_hdr->contents != (unsigned char *) local_syms)
12944 {
12945 if (!info->keep_memory)
12946 free (local_syms);
12947 else
12948 symtab_hdr->contents = (unsigned char *) local_syms;
12949 }
12950 }
12951 return TRUE;
12952}
12953
721956f4
AM
12954/* Build all the stubs associated with the current output file.
12955 The stubs are kept in a hash table attached to the main linker
12956 hash table. This function is called via gldelf64ppc_finish. */
12957
b34976b6 12958bfd_boolean
e7d1c40c 12959ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 12960 char **stats)
5d1634d7
AM
12961{
12962 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 12963 struct map_stub *group;
721956f4 12964 asection *stub_sec;
5d1634d7 12965 bfd_byte *p;
e717da7e 12966 int stub_sec_count = 0;
5d1634d7 12967
4dfe6ac6
NC
12968 if (htab == NULL)
12969 return FALSE;
12970
eea6121a 12971 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 12972 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
12973 {
12974 group->eh_size = 0;
12975 group->lr_restore = 0;
12976 if ((stub_sec = group->stub_sec) != NULL
12977 && stub_sec->size != 0)
12978 {
12979 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
12980 stub_sec->size);
12981 if (stub_sec->contents == NULL)
12982 return FALSE;
12983 stub_sec->size = 0;
12984 }
12985 }
5d1634d7 12986
23eb7e01 12987 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 12988 {
9f951329 12989 unsigned int indx;
ad8e1ba5 12990 bfd_vma plt0;
9f951329 12991
721956f4 12992 /* Build the .glink plt call stub. */
e7d1c40c 12993 if (htab->params->emit_stub_syms)
97b639ba
AM
12994 {
12995 struct elf_link_hash_entry *h;
468392fb
AM
12996 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12997 TRUE, FALSE, FALSE);
97b639ba
AM
12998 if (h == NULL)
12999 return FALSE;
13000 if (h->root.type == bfd_link_hash_new)
13001 {
13002 h->root.type = bfd_link_hash_defined;
13003 h->root.u.def.section = htab->glink;
ee4bf8d2 13004 h->root.u.def.value = 8;
f5385ebf
AM
13005 h->ref_regular = 1;
13006 h->def_regular = 1;
13007 h->ref_regular_nonweak = 1;
13008 h->forced_local = 1;
13009 h->non_elf = 0;
2ec55de3 13010 h->root.linker_def = 1;
97b639ba
AM
13011 }
13012 }
33e44f2e
AM
13013 plt0 = (htab->elf.splt->output_section->vma
13014 + htab->elf.splt->output_offset
13015 - 16);
176a0d42
AM
13016 if (info->emitrelocations)
13017 {
13018 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13019 if (r == NULL)
13020 return FALSE;
13021 r->r_offset = (htab->glink->output_offset
13022 + htab->glink->output_section->vma);
13023 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13024 r->r_addend = plt0;
13025 }
4ce794b7 13026 p = htab->glink->contents;
176a0d42 13027 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13028 bfd_put_64 (htab->glink->owner, plt0, p);
13029 p += 8;
b9e5796b
AM
13030 if (htab->opd_abi)
13031 {
13032 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13033 p += 4;
13034 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13035 p += 4;
13036 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13037 p += 4;
13038 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13039 p += 4;
13040 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13041 p += 4;
13042 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13043 p += 4;
13044 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13045 p += 4;
13046 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13047 p += 4;
13048 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13049 p += 4;
13050 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13051 p += 4;
13052 }
13053 else
13054 {
13055 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13056 p += 4;
13057 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13058 p += 4;
13059 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13060 p += 4;
f378ab09
AM
13061 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13062 p += 4;
b9e5796b
AM
13063 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13064 p += 4;
13065 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13066 p += 4;
13067 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13068 p += 4;
13069 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13070 p += 4;
13071 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13072 p += 4;
13073 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13074 p += 4;
13075 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13076 p += 4;
13077 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13078 p += 4;
13079 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13080 p += 4;
13081 }
407aa07c
AM
13082 bfd_put_32 (htab->glink->owner, BCTR, p);
13083 p += 4;
c75bc4f7 13084 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 13085
9f951329
AM
13086 /* Build the .glink lazy link call stubs. */
13087 indx = 0;
9e390558 13088 while (p < htab->glink->contents + htab->glink->size)
9f951329 13089 {
b9e5796b 13090 if (htab->opd_abi)
9f951329 13091 {
b9e5796b
AM
13092 if (indx < 0x8000)
13093 {
13094 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13095 p += 4;
13096 }
13097 else
13098 {
13099 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13100 p += 4;
13101 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13102 p);
13103 p += 4;
13104 }
9f951329 13105 }
4ce794b7 13106 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13107 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13108 indx++;
9f951329
AM
13109 p += 4;
13110 }
5d1634d7 13111 }
5d1634d7 13112
49c09209
AM
13113 /* Build .glink global entry stubs, and PLT relocs for globals. */
13114 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13115
13116 if (!write_plt_relocs_for_local_syms (info))
13117 return FALSE;
9e390558 13118
7341d5e2 13119 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13120 {
4ce794b7 13121 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13122 htab->brlt->size);
4ce794b7 13123 if (htab->brlt->contents == NULL)
b34976b6 13124 return FALSE;
721956f4 13125 }
ee75fd95 13126 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13127 {
13128 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13129 htab->relbrlt->size);
63bc6f6c
AM
13130 if (htab->relbrlt->contents == NULL)
13131 return FALSE;
13132 }
5d1634d7 13133
721956f4
AM
13134 /* Build the stubs as directed by the stub hash table. */
13135 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13136
a4b6fadd
AM
13137 for (group = htab->group; group != NULL; group = group->next)
13138 if (group->needs_save_res)
7dda8d3c 13139 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 13140
aa8a7074
AM
13141 if (htab->relbrlt != NULL)
13142 htab->relbrlt->reloc_count = 0;
13143
e7d1c40c 13144 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13145 for (group = htab->group; group != NULL; group = group->next)
13146 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
13147 {
13148 int align = abs (htab->params->plt_stub_align);
13149 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13150 }
794e51c0 13151
7dda8d3c
AM
13152 for (group = htab->group; group != NULL; group = group->next)
13153 if (group->needs_save_res)
13154 {
13155 stub_sec = group->stub_sec;
13156 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13157 htab->sfpr->contents, htab->sfpr->size);
13158 if (htab->params->emit_stub_syms)
13159 {
13160 unsigned int i;
13161
13162 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13163 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13164 return FALSE;
13165 }
13166 }
13167
df136d64
AM
13168 if (htab->glink_eh_frame != NULL
13169 && htab->glink_eh_frame->size != 0)
13170 {
13171 bfd_vma val;
13172 size_t align = 4;
13173
13174 p = htab->glink_eh_frame->contents;
13175 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13176
13177 for (group = htab->group; group != NULL; group = group->next)
13178 if (group->eh_size != 0)
13179 {
13180 /* Offset to stub section. */
13181 val = (group->stub_sec->output_section->vma
13182 + group->stub_sec->output_offset);
13183 val -= (htab->glink_eh_frame->output_section->vma
13184 + htab->glink_eh_frame->output_offset
13185 + (p + 8 - htab->glink_eh_frame->contents));
13186 if (val + 0x80000000 > 0xffffffff)
13187 {
13188 _bfd_error_handler
13189 (_("%s offset too large for .eh_frame sdata4 encoding"),
13190 group->stub_sec->name);
13191 return FALSE;
13192 }
13193 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13194 p += (group->eh_size + 17 + 3) & -4;
13195 }
13196 if (htab->glink != NULL && htab->glink->size != 0)
13197 {
13198 /* Offset to .glink. */
13199 val = (htab->glink->output_section->vma
13200 + htab->glink->output_offset
13201 + 8);
13202 val -= (htab->glink_eh_frame->output_section->vma
13203 + htab->glink_eh_frame->output_offset
13204 + (p + 8 - htab->glink_eh_frame->contents));
13205 if (val + 0x80000000 > 0xffffffff)
13206 {
13207 _bfd_error_handler
13208 (_("%s offset too large for .eh_frame sdata4 encoding"),
13209 htab->glink->name);
13210 return FALSE;
13211 }
13212 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13213 p += (24 + align - 1) & -align;
13214 }
13215 }
13216
d4aaa2a0
AM
13217 for (group = htab->group; group != NULL; group = group->next)
13218 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
13219 {
13220 stub_sec_count += 1;
c9301e31
AM
13221 if (stub_sec->rawsize != stub_sec->size
13222 && (htab->stub_iteration <= STUB_SHRINK_ITER
13223 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13224 break;
13225 }
5d1634d7 13226
25516cc5 13227 if (group != NULL)
5d1634d7 13228 {
b34976b6 13229 htab->stub_error = TRUE;
cf97bcb0 13230 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 13231 }
721956f4 13232
d2a300cf
AM
13233 if (htab->stub_error)
13234 return FALSE;
13235
13236 if (stats != NULL)
13237 {
db84fff3 13238 size_t len;
d2a300cf
AM
13239 *stats = bfd_malloc (500);
13240 if (*stats == NULL)
13241 return FALSE;
13242
db84fff3
AM
13243 len = sprintf (*stats,
13244 ngettext ("linker stubs in %u group\n",
13245 "linker stubs in %u groups\n",
13246 stub_sec_count),
13247 stub_sec_count);
05d0e962
AM
13248 sprintf (*stats + len, _(" branch %lu\n"
13249 " branch toc adj %lu\n"
13250 " branch notoc %lu\n"
13251 " branch both %lu\n"
13252 " long branch %lu\n"
13253 " long toc adj %lu\n"
13254 " long notoc %lu\n"
13255 " long both %lu\n"
13256 " plt call %lu\n"
13257 " plt call save %lu\n"
13258 " plt call notoc %lu\n"
13259 " plt call both %lu\n"
13260 " global entry %lu"),
4ce794b7
AM
13261 htab->stub_count[ppc_stub_long_branch - 1],
13262 htab->stub_count[ppc_stub_long_branch_r2off - 1],
05d0e962
AM
13263 htab->stub_count[ppc_stub_long_branch_notoc - 1],
13264 htab->stub_count[ppc_stub_long_branch_both - 1],
4ce794b7
AM
13265 htab->stub_count[ppc_stub_plt_branch - 1],
13266 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
05d0e962
AM
13267 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
13268 htab->stub_count[ppc_stub_plt_branch_both - 1],
794e51c0 13269 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2 13270 htab->stub_count[ppc_stub_plt_call_r2save - 1],
05d0e962
AM
13271 htab->stub_count[ppc_stub_plt_call_notoc - 1],
13272 htab->stub_count[ppc_stub_plt_call_both - 1],
7341d5e2 13273 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13274 }
13275 return TRUE;
5bd4f169
AM
13276}
13277
60124e18
AM
13278/* What to do when ld finds relocations against symbols defined in
13279 discarded sections. */
13280
13281static unsigned int
13282ppc64_elf_action_discarded (asection *sec)
13283{
13284 if (strcmp (".opd", sec->name) == 0)
13285 return 0;
13286
13287 if (strcmp (".toc", sec->name) == 0)
13288 return 0;
13289
bce50a28
JJ
13290 if (strcmp (".toc1", sec->name) == 0)
13291 return 0;
13292
60124e18
AM
13293 return _bfd_elf_default_action_discarded (sec);
13294}
13295
5bd4f169
AM
13296/* The RELOCATE_SECTION function is called by the ELF backend linker
13297 to handle the relocations for a section.
13298
13299 The relocs are always passed as Rela structures; if the section
13300 actually uses Rel structures, the r_addend field will always be
13301 zero.
13302
13303 This function is responsible for adjust the section contents as
13304 necessary, and (if using Rela relocs and generating a
1049f94e 13305 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13306 necessary.
13307
13308 This function does not have to worry about setting the reloc
13309 address or the reloc symbol index.
13310
13311 LOCAL_SYMS is a pointer to the swapped in local symbols.
13312
13313 LOCAL_SECTIONS is an array giving the section in the input file
13314 corresponding to the st_shndx field of each local symbol.
13315
13316 The global hash table entry for the global symbols can be found
13317 via elf_sym_hashes (input_bfd).
13318
1049f94e 13319 When generating relocatable output, this function must handle
5bd4f169
AM
13320 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13321 going to be the section symbol corresponding to the output
13322 section, which means that the addend must be adjusted
13323 accordingly. */
13324
b34976b6 13325static bfd_boolean
4ce794b7
AM
13326ppc64_elf_relocate_section (bfd *output_bfd,
13327 struct bfd_link_info *info,
13328 bfd *input_bfd,
13329 asection *input_section,
13330 bfd_byte *contents,
13331 Elf_Internal_Rela *relocs,
13332 Elf_Internal_Sym *local_syms,
13333 asection **local_sections)
5bd4f169 13334{
65f38f15 13335 struct ppc_link_hash_table *htab;
5bd4f169
AM
13336 Elf_Internal_Shdr *symtab_hdr;
13337 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13338 Elf_Internal_Rela *rel;
c316a17c 13339 Elf_Internal_Rela *wrel;
5bd4f169 13340 Elf_Internal_Rela *relend;
411e1bfb
AM
13341 Elf_Internal_Rela outrel;
13342 bfd_byte *loc;
411e1bfb 13343 struct got_entry **local_got_ents;
5bd4f169 13344 bfd_vma TOCstart;
b34976b6
AM
13345 bfd_boolean ret = TRUE;
13346 bfd_boolean is_opd;
794e51c0
AM
13347 /* Assume 'at' branch hints. */
13348 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 13349 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 13350
65f38f15 13351 /* Initialize howto table if needed. */
5bd4f169 13352 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13353 ppc_howto_init ();
13354
65f38f15 13355 htab = ppc_hash_table (info);
4dfe6ac6
NC
13356 if (htab == NULL)
13357 return FALSE;
ee75fd95
AM
13358
13359 /* Don't relocate stub sections. */
e7d1c40c 13360 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13361 return TRUE;
13362
7af5d5c4
AM
13363 if (!is_ppc64_elf (input_bfd))
13364 {
13365 bfd_set_error (bfd_error_wrong_format);
13366 return FALSE;
13367 }
0ffa91dd 13368
411e1bfb 13369 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13370 TOCstart = elf_gp (output_bfd);
0ffa91dd 13371 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13372 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13373 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13374
c316a17c 13375 rel = wrel = relocs;
5bd4f169 13376 relend = relocs + input_section->reloc_count;
c316a17c 13377 for (; rel < relend; wrel++, rel++)
5bd4f169 13378 {
04c9666a 13379 enum elf_ppc64_reloc_type r_type;
31c76678 13380 bfd_vma addend;
5bd4f169
AM
13381 bfd_reloc_status_type r;
13382 Elf_Internal_Sym *sym;
13383 asection *sec;
039b3fef
AM
13384 struct elf_link_hash_entry *h_elf;
13385 struct ppc_link_hash_entry *h;
13386 struct ppc_link_hash_entry *fdh;
5bd4f169 13387 const char *sym_name;
0d4792f7 13388 unsigned long r_symndx, toc_symndx;
3a71aa26 13389 bfd_vma toc_addend;
f961d9dd
AM
13390 unsigned char tls_mask, tls_gd, tls_type;
13391 unsigned char sym_type;
5bd4f169 13392 bfd_vma relocation;
23cedd1d 13393 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 13394 bfd_boolean warned;
bc30df16 13395 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13396 unsigned int insn;
e11840f9 13397 unsigned int mask;
721956f4
AM
13398 struct ppc_stub_hash_entry *stub_entry;
13399 bfd_vma max_br_offset;
13400 bfd_vma from;
c316a17c 13401 Elf_Internal_Rela orig_rel;
b80eed39
AM
13402 reloc_howto_type *howto;
13403 struct reloc_howto_struct alt_howto;
5bd4f169 13404
c316a17c
AM
13405 again:
13406 orig_rel = *rel;
13407
4ce794b7 13408 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13409 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13410
13411 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13412 symbol of the previous ADDR64 reloc. The symbol gives us the
13413 proper TOC base to use. */
13414 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13415 && wrel != relocs
13416 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13417 && is_opd)
c316a17c 13418 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13419
4ce794b7
AM
13420 sym = NULL;
13421 sec = NULL;
039b3fef 13422 h_elf = NULL;
4ce794b7 13423 sym_name = NULL;
b34976b6
AM
13424 unresolved_reloc = FALSE;
13425 warned = FALSE;
65f38f15 13426
0b13192e 13427 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13428 {
13429 /* It's a local symbol. */
74f0fb50 13430 struct _opd_sec_data *opd;
4025353c 13431
5bd4f169
AM
13432 sym = local_syms + r_symndx;
13433 sec = local_sections[r_symndx];
26c61ae5 13434 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13435 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13436 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13437 opd = get_opd_info (sec);
13438 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13439 {
51aecdc5
AM
13440 long adjust = opd->adjust[OPD_NDX (sym->st_value
13441 + rel->r_addend)];
4025353c
AM
13442 if (adjust == -1)
13443 relocation = 0;
13444 else
4cc603a5
AM
13445 {
13446 /* If this is a relocation against the opd section sym
13447 and we have edited .opd, adjust the reloc addend so
13448 that ld -r and ld --emit-relocs output is correct.
13449 If it is a reloc against some other .opd symbol,
13450 then the symbol value will be adjusted later. */
13451 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13452 rel->r_addend += adjust;
13453 else
13454 relocation += adjust;
13455 }
1e2f5b6e 13456 }
5bd4f169
AM
13457 }
13458 else
13459 {
62d887d4
L
13460 bfd_boolean ignored;
13461
b2a8e766
AM
13462 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13463 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13464 h_elf, sec, relocation,
62d887d4 13465 unresolved_reloc, warned, ignored);
039b3fef
AM
13466 sym_name = h_elf->root.root.string;
13467 sym_type = h_elf->type;
b69fdb4e
AM
13468 if (sec != NULL
13469 && sec->owner == output_bfd
13470 && strcmp (sec->name, ".opd") == 0)
13471 {
13472 /* This is a symbol defined in a linker script. All
13473 such are defined in output sections, even those
13474 defined by simple assignment from a symbol defined in
13475 an input section. Transfer the symbol to an
13476 appropriate input .opd section, so that a branch to
13477 this symbol will be mapped to the location specified
13478 by the opd entry. */
13479 struct bfd_link_order *lo;
13480 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13481 if (lo->type == bfd_indirect_link_order)
13482 {
13483 asection *isec = lo->u.indirect.section;
13484 if (h_elf->root.u.def.value >= isec->output_offset
13485 && h_elf->root.u.def.value < (isec->output_offset
13486 + isec->size))
13487 {
13488 h_elf->root.u.def.value -= isec->output_offset;
13489 h_elf->root.u.def.section = isec;
13490 sec = isec;
13491 break;
13492 }
13493 }
13494 }
5bd4f169 13495 }
039b3fef 13496 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13497
dbaa2011 13498 if (sec != NULL && discarded_section (sec))
c316a17c
AM
13499 {
13500 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13501 input_bfd, input_section,
0930cb30 13502 contents, rel->r_offset);
c316a17c
AM
13503 wrel->r_offset = rel->r_offset;
13504 wrel->r_info = 0;
13505 wrel->r_addend = 0;
13506
13507 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 13508 symbols defined in discarded sections. Not done for
c316a17c
AM
13509 non-debug to preserve relocs in .eh_frame which the
13510 eh_frame editing code expects to be present. */
13511 if (bfd_link_relocatable (info)
13512 && (input_section->flags & SEC_DEBUGGING))
13513 wrel--;
13514
13515 continue;
13516 }
ab96bf03 13517
0e1862bb 13518 if (bfd_link_relocatable (info))
c316a17c 13519 goto copy_reloc;
ab96bf03 13520
f40da81b
AM
13521 if (h != NULL && &h->elf == htab->elf.hgot)
13522 {
6f20ed8a 13523 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
13524 sec = bfd_abs_section_ptr;
13525 unresolved_reloc = FALSE;
13526 }
13527
951fd09b
AM
13528 /* TLS optimizations. Replace instruction sequences and relocs
13529 based on information we collected in tls_optimize. We edit
13530 RELOCS so that --emit-relocs will output something sensible
13531 for the final instruction stream. */
13532 tls_mask = 0;
13533 tls_gd = 0;
0d4792f7 13534 toc_symndx = 0;
727fc41e
AM
13535 if (h != NULL)
13536 tls_mask = h->tls_mask;
13537 else if (local_got_ents != NULL)
411e1bfb 13538 {
e054468f
AM
13539 struct plt_entry **local_plt = (struct plt_entry **)
13540 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 13541 unsigned char *lgot_masks = (unsigned char *)
e054468f 13542 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
13543 tls_mask = lgot_masks[r_symndx];
13544 }
37da22e5 13545 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
13546 && (r_type == R_PPC64_TLS
13547 || r_type == R_PPC64_TLSGD
13548 || r_type == R_PPC64_TLSLD))
13549 {
13550 /* Check for toc tls entries. */
f961d9dd 13551 unsigned char *toc_tls;
0d4792f7 13552
727fc41e
AM
13553 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13554 &local_syms, rel, input_bfd))
13555 return FALSE;
0d4792f7 13556
727fc41e
AM
13557 if (toc_tls)
13558 tls_mask = *toc_tls;
0d4792f7
AM
13559 }
13560
13561 /* Check that tls relocs are used with tls syms, and non-tls
13562 relocs are used with non-tls syms. */
cf35638d 13563 if (r_symndx != STN_UNDEF
0d4792f7
AM
13564 && r_type != R_PPC64_NONE
13565 && (h == NULL
039b3fef
AM
13566 || h->elf.root.type == bfd_link_hash_defined
13567 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
13568 && (IS_PPC64_TLS_RELOC (r_type)
13569 != (sym_type == STT_TLS
13570 || (sym_type == STT_SECTION
13571 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 13572 {
37da22e5 13573 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
13574 && (r_type == R_PPC64_TLS
13575 || r_type == R_PPC64_TLSGD
13576 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
13577 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13578 ;
13579 else
25f53a85 13580 info->callbacks->einfo
1d483afe 13581 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 13582 /* xgettext:c-format */
c1c8c1ef 13583 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 13584 /* xgettext:c-format */
c1c8c1ef 13585 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 13586 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
13587 ppc64_elf_howto_table[r_type]->name,
13588 sym_name);
411e1bfb
AM
13589 }
13590
13591 /* Ensure reloc mapping code below stays sane. */
13592 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13593 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13594 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13595 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13596 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13597 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13598 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13599 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13600 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13601 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13602 abort ();
0d4792f7 13603
411e1bfb
AM
13604 switch (r_type)
13605 {
13606 default:
411e1bfb
AM
13607 break;
13608
ba761f19 13609 case R_PPC64_LO_DS_OPT:
95f0d0d2 13610 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
13611 if ((insn & (0x3f << 26)) != 58u << 26)
13612 abort ();
13613 insn += (14u << 26) - (58u << 26);
95f0d0d2 13614 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
13615 r_type = R_PPC64_TOC16_LO;
13616 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13617 break;
13618
411e1bfb
AM
13619 case R_PPC64_TOC16:
13620 case R_PPC64_TOC16_LO:
13621 case R_PPC64_TOC16_DS:
13622 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
13623 {
13624 /* Check for toc tls entries. */
f961d9dd 13625 unsigned char *toc_tls;
951fd09b 13626 int retval;
411e1bfb 13627
3a71aa26
AM
13628 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13629 &local_syms, rel, input_bfd);
951fd09b 13630 if (retval == 0)
411e1bfb
AM
13631 return FALSE;
13632
13633 if (toc_tls)
13634 {
951fd09b 13635 tls_mask = *toc_tls;
411e1bfb
AM
13636 if (r_type == R_PPC64_TOC16_DS
13637 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 13638 {
37da22e5 13639 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
13640 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13641 goto toctprel;
13642 }
411e1bfb 13643 else
951fd09b
AM
13644 {
13645 /* If we found a GD reloc pair, then we might be
13646 doing a GD->IE transition. */
13647 if (retval == 2)
13648 {
13649 tls_gd = TLS_TPRELGD;
37da22e5
AM
13650 if ((tls_mask & TLS_TLS) != 0
13651 && (tls_mask & TLS_GD) == 0)
102890f0 13652 goto tls_ldgd_opt;
951fd09b
AM
13653 }
13654 else if (retval == 3)
13655 {
37da22e5
AM
13656 if ((tls_mask & TLS_TLS) != 0
13657 && (tls_mask & TLS_LD) == 0)
102890f0 13658 goto tls_ldgd_opt;
951fd09b
AM
13659 }
13660 }
411e1bfb
AM
13661 }
13662 }
13663 break;
13664
9d6ded02
AM
13665 case R_PPC64_GOT_TPREL16_HI:
13666 case R_PPC64_GOT_TPREL16_HA:
37da22e5 13667 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
13668 && (tls_mask & TLS_TPREL) == 0)
13669 {
13670 rel->r_offset -= d_offset;
95f0d0d2 13671 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
13672 r_type = R_PPC64_NONE;
13673 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13674 }
13675 break;
13676
411e1bfb
AM
13677 case R_PPC64_GOT_TPREL16_DS:
13678 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 13679 if ((tls_mask & TLS_TLS) != 0
951fd09b 13680 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13681 {
81407a69 13682 toctprel:
95f0d0d2 13683 insn = bfd_get_32 (input_bfd,
c316a17c 13684 contents + rel->r_offset - d_offset);
411e1bfb
AM
13685 insn &= 31 << 21;
13686 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 13687 bfd_put_32 (input_bfd, insn,
c316a17c 13688 contents + rel->r_offset - d_offset);
411e1bfb 13689 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13690 if (toc_symndx != 0)
13691 {
13692 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13693 rel->r_addend = toc_addend;
0d4792f7
AM
13694 /* We changed the symbol. Start over in order to
13695 get h, sym, sec etc. right. */
c316a17c 13696 goto again;
0d4792f7
AM
13697 }
13698 else
13699 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13700 }
13701 break;
13702
13703 case R_PPC64_TLS:
37da22e5 13704 if ((tls_mask & TLS_TLS) != 0
951fd09b 13705 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13706 {
95f0d0d2 13707 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
13708 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13709 if (insn == 0)
411e1bfb 13710 abort ();
95f0d0d2 13711 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 13712 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13713 PPC64_TPREL16_LO which is at low-order half-word. */
13714 rel->r_offset += d_offset;
0d4792f7
AM
13715 r_type = R_PPC64_TPREL16_LO;
13716 if (toc_symndx != 0)
13717 {
13718 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13719 rel->r_addend = toc_addend;
0d4792f7
AM
13720 /* We changed the symbol. Start over in order to
13721 get h, sym, sec etc. right. */
c316a17c 13722 goto again;
0d4792f7
AM
13723 }
13724 else
13725 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13726 }
13727 break;
13728
411e1bfb
AM
13729 case R_PPC64_GOT_TLSGD16_HI:
13730 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 13731 tls_gd = TLS_TPRELGD;
37da22e5 13732 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
13733 goto tls_gdld_hi;
13734 break;
13735
411e1bfb
AM
13736 case R_PPC64_GOT_TLSLD16_HI:
13737 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 13738 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13739 {
951fd09b
AM
13740 tls_gdld_hi:
13741 if ((tls_mask & tls_gd) != 0)
13742 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13743 + R_PPC64_GOT_TPREL16_DS);
13744 else
411e1bfb 13745 {
4fe5ca5b 13746 rel->r_offset -= d_offset;
95f0d0d2 13747 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 13748 r_type = R_PPC64_NONE;
411e1bfb 13749 }
951fd09b 13750 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13751 }
13752 break;
13753
951fd09b
AM
13754 case R_PPC64_GOT_TLSGD16:
13755 case R_PPC64_GOT_TLSGD16_LO:
13756 tls_gd = TLS_TPRELGD;
37da22e5 13757 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13758 goto tls_ldgd_opt;
951fd09b 13759 break;
411e1bfb 13760
951fd09b
AM
13761 case R_PPC64_GOT_TLSLD16:
13762 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 13763 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 13764 {
b9f04fe0 13765 unsigned int insn1, insn2;
102890f0
AM
13766 bfd_vma offset;
13767
13768 tls_ldgd_opt:
727fc41e
AM
13769 offset = (bfd_vma) -1;
13770 /* If not using the newer R_PPC64_TLSGD/LD to mark
13771 __tls_get_addr calls, we must trust that the call
13772 stays with its arg setup insns, ie. that the next
13773 reloc is the __tls_get_addr call associated with
13774 the current reloc. Edit both insns. */
13775 if (input_section->has_tls_get_addr_call
13776 && rel + 1 < relend
13777 && branch_reloc_hash_match (input_bfd, rel + 1,
13778 htab->tls_get_addr,
13779 htab->tls_get_addr_fd))
13780 offset = rel[1].r_offset;
b86ac8e3
AM
13781 /* We read the low GOT_TLS (or TOC16) insn because we
13782 need to keep the destination reg. It may be
13783 something other than the usual r3, and moved to r3
13784 before the call by intervening code. */
95f0d0d2 13785 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 13786 contents + rel->r_offset - d_offset);
102890f0 13787 if ((tls_mask & tls_gd) != 0)
411e1bfb 13788 {
102890f0 13789 /* IE */
b86ac8e3 13790 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
13791 insn1 |= 58 << 26; /* ld */
13792 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13793 if (offset != (bfd_vma) -1)
f58d5a2d 13794 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13795 if ((tls_mask & TLS_EXPLICIT) == 0)
13796 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13797 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13798 else
102890f0
AM
13799 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13800 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13801 }
13802 else
13803 {
13804 /* LE */
b86ac8e3
AM
13805 insn1 &= 0x1f << 21;
13806 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
13807 insn2 = 0x38630000; /* addi 3,3,0 */
13808 if (tls_gd == 0)
951fd09b 13809 {
102890f0 13810 /* Was an LD reloc. */
1d483afe
AM
13811 if (toc_symndx)
13812 sec = local_sections[toc_symndx];
13813 for (r_symndx = 0;
13814 r_symndx < symtab_hdr->sh_info;
13815 r_symndx++)
13816 if (local_sections[r_symndx] == sec)
13817 break;
13818 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13819 r_symndx = STN_UNDEF;
102890f0 13820 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13821 if (r_symndx != STN_UNDEF)
1d483afe
AM
13822 rel->r_addend -= (local_syms[r_symndx].st_value
13823 + sec->output_offset
13824 + sec->output_section->vma);
951fd09b 13825 }
102890f0 13826 else if (toc_symndx != 0)
3a71aa26
AM
13827 {
13828 r_symndx = toc_symndx;
13829 rel->r_addend = toc_addend;
13830 }
102890f0
AM
13831 r_type = R_PPC64_TPREL16_HA;
13832 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13833 if (offset != (bfd_vma) -1)
13834 {
13835 rel[1].r_info = ELF64_R_INFO (r_symndx,
13836 R_PPC64_TPREL16_LO);
13837 rel[1].r_offset = offset + d_offset;
13838 rel[1].r_addend = rel->r_addend;
13839 }
102890f0 13840 }
95f0d0d2 13841 bfd_put_32 (input_bfd, insn1,
3a71aa26 13842 contents + rel->r_offset - d_offset);
727fc41e 13843 if (offset != (bfd_vma) -1)
c96e0573
AM
13844 {
13845 bfd_put_32 (input_bfd, insn2, contents + offset);
13846 if (offset + 8 <= input_section->size)
13847 {
13848 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
13849 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
13850 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
13851 }
13852 }
727fc41e
AM
13853 if ((tls_mask & tls_gd) == 0
13854 && (tls_gd == 0 || toc_symndx != 0))
13855 {
13856 /* We changed the symbol. Start over in order
13857 to get h, sym, sec etc. right. */
c316a17c 13858 goto again;
727fc41e
AM
13859 }
13860 }
13861 break;
13862
13863 case R_PPC64_TLSGD:
37da22e5 13864 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 13865 && rel + 1 < relend)
727fc41e 13866 {
b9f04fe0 13867 unsigned int insn2;
727fc41e
AM
13868 bfd_vma offset = rel->r_offset;
13869
23cedd1d
AM
13870 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
13871 {
13872 bfd_put_32 (output_bfd, NOP, contents + offset);
13873 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13874 break;
13875 }
13876
13877 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
13878 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
13879
727fc41e
AM
13880 if ((tls_mask & TLS_TPRELGD) != 0)
13881 {
13882 /* IE */
13883 r_type = R_PPC64_NONE;
13884 insn2 = 0x7c636a14; /* add 3,3,13 */
13885 }
13886 else
13887 {
13888 /* LE */
13889 if (toc_symndx != 0)
13890 {
13891 r_symndx = toc_symndx;
13892 rel->r_addend = toc_addend;
13893 }
13894 r_type = R_PPC64_TPREL16_LO;
13895 rel->r_offset = offset + d_offset;
13896 insn2 = 0x38630000; /* addi 3,3,0 */
13897 }
13898 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13899 /* Zap the reloc on the _tls_get_addr call too. */
13900 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13901 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 13902 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 13903 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 13904 goto again;
411e1bfb 13905 }
411e1bfb
AM
13906 break;
13907
727fc41e 13908 case R_PPC64_TLSLD:
37da22e5 13909 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 13910 && rel + 1 < relend)
727fc41e 13911 {
b9f04fe0 13912 unsigned int insn2;
727fc41e
AM
13913 bfd_vma offset = rel->r_offset;
13914
23cedd1d
AM
13915 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
13916 {
13917 bfd_put_32 (output_bfd, NOP, contents + offset);
13918 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13919 break;
13920 }
13921
13922 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
13923 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
13924
727fc41e
AM
13925 if (toc_symndx)
13926 sec = local_sections[toc_symndx];
13927 for (r_symndx = 0;
13928 r_symndx < symtab_hdr->sh_info;
13929 r_symndx++)
13930 if (local_sections[r_symndx] == sec)
13931 break;
13932 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13933 r_symndx = STN_UNDEF;
727fc41e 13934 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13935 if (r_symndx != STN_UNDEF)
727fc41e
AM
13936 rel->r_addend -= (local_syms[r_symndx].st_value
13937 + sec->output_offset
13938 + sec->output_section->vma);
13939
13940 r_type = R_PPC64_TPREL16_LO;
13941 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13942 rel->r_offset = offset + d_offset;
13943 /* Zap the reloc on the _tls_get_addr call too. */
13944 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13945 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 13946 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 13947 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 13948 goto again;
727fc41e
AM
13949 }
13950 break;
13951
411e1bfb 13952 case R_PPC64_DTPMOD64:
951fd09b
AM
13953 if (rel + 1 < relend
13954 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13955 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13956 {
951fd09b
AM
13957 if ((tls_mask & TLS_GD) == 0)
13958 {
13959 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13960 if ((tls_mask & TLS_TPRELGD) != 0)
13961 r_type = R_PPC64_TPREL64;
13962 else
13963 {
4ce794b7 13964 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13965 r_type = R_PPC64_NONE;
13966 }
13967 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13968 }
13969 }
13970 else
13971 {
13972 if ((tls_mask & TLS_LD) == 0)
411e1bfb 13973 {
4ce794b7 13974 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 13975 r_type = R_PPC64_NONE;
951fd09b 13976 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 13977 }
411e1bfb
AM
13978 }
13979 break;
13980
13981 case R_PPC64_TPREL64:
951fd09b 13982 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
13983 {
13984 r_type = R_PPC64_NONE;
13985 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13986 }
13987 break;
52a82034 13988
006589cf
AM
13989 case R_PPC64_ENTRY:
13990 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13991 if (!bfd_link_pic (info)
13992 && !info->traditional_format
13993 && relocation + 0x80008000 <= 0xffffffff)
13994 {
13995 unsigned int insn1, insn2;
13996
13997 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13998 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13999 if ((insn1 & ~0xfffc) == LD_R2_0R12
14000 && insn2 == ADD_R2_R2_R12)
14001 {
95f0d0d2 14002 bfd_put_32 (input_bfd,
006589cf
AM
14003 LIS_R2 + PPC_HA (relocation),
14004 contents + rel->r_offset);
95f0d0d2 14005 bfd_put_32 (input_bfd,
006589cf
AM
14006 ADDI_R2_R2 + PPC_LO (relocation),
14007 contents + rel->r_offset + 4);
14008 }
14009 }
14010 else
14011 {
14012 relocation -= (rel->r_offset
14013 + input_section->output_offset
14014 + input_section->output_section->vma);
14015 if (relocation + 0x80008000 <= 0xffffffff)
14016 {
14017 unsigned int insn1, insn2;
14018
14019 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14020 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14021 if ((insn1 & ~0xfffc) == LD_R2_0R12
14022 && insn2 == ADD_R2_R2_R12)
14023 {
95f0d0d2 14024 bfd_put_32 (input_bfd,
006589cf
AM
14025 ADDIS_R2_R12 + PPC_HA (relocation),
14026 contents + rel->r_offset);
95f0d0d2 14027 bfd_put_32 (input_bfd,
006589cf
AM
14028 ADDI_R2_R2 + PPC_LO (relocation),
14029 contents + rel->r_offset + 4);
14030 }
14031 }
14032 }
14033 break;
14034
52a82034
AM
14035 case R_PPC64_REL16_HA:
14036 /* If we are generating a non-PIC executable, edit
14037 . 0: addis 2,12,.TOC.-0b@ha
14038 . addi 2,2,.TOC.-0b@l
14039 used by ELFv2 global entry points to set up r2, to
14040 . lis 2,.TOC.@ha
14041 . addi 2,2,.TOC.@l
14042 if .TOC. is in range. */
0e1862bb 14043 if (!bfd_link_pic (info)
810d4e75 14044 && !info->traditional_format
006589cf 14045 && !htab->opd_abi
4f038ee5 14046 && rel->r_addend == d_offset
52a82034
AM
14047 && h != NULL && &h->elf == htab->elf.hgot
14048 && rel + 1 < relend
14049 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14050 && rel[1].r_offset == rel->r_offset + 4
14051 && rel[1].r_addend == rel->r_addend + 4
14052 && relocation + 0x80008000 <= 0xffffffff)
14053 {
14054 unsigned int insn1, insn2;
14055 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
14056 insn1 = bfd_get_32 (input_bfd, contents + offset);
14057 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14058 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14059 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14060 {
14061 r_type = R_PPC64_ADDR16_HA;
14062 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14063 rel->r_addend -= d_offset;
14064 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14065 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14066 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14067 }
14068 }
14069 break;
411e1bfb
AM
14070 }
14071
14072 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14073 insn = 0;
b25116a9
AM
14074 max_br_offset = 1 << 25;
14075 addend = rel->r_addend;
bc30df16 14076 reloc_dest = DEST_NORMAL;
65f38f15 14077 switch (r_type)
5bd4f169
AM
14078 {
14079 default:
65f38f15 14080 break;
5bd4f169 14081
3b421ab3
AM
14082 case R_PPC64_TOCSAVE:
14083 if (relocation + addend == (rel->r_offset
14084 + input_section->output_offset
14085 + input_section->output_section->vma)
14086 && tocsave_find (htab, NO_INSERT,
14087 &local_syms, rel, input_bfd))
14088 {
14089 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14090 if (insn == NOP
14091 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14092 bfd_put_32 (input_bfd,
14093 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14094 contents + rel->r_offset);
14095 }
14096 break;
14097
65f38f15
AM
14098 /* Branch taken prediction relocations. */
14099 case R_PPC64_ADDR14_BRTAKEN:
14100 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14101 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14102 /* Fall through. */
65f38f15 14103
86c76c7b 14104 /* Branch not taken prediction relocations. */
65f38f15
AM
14105 case R_PPC64_ADDR14_BRNTAKEN:
14106 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14107 insn |= bfd_get_32 (input_bfd,
411e1bfb 14108 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14109 /* Fall through. */
86c76c7b 14110
b25116a9
AM
14111 case R_PPC64_REL14:
14112 max_br_offset = 1 << 15;
1a0670f3 14113 /* Fall through. */
5bd4f169 14114
65f38f15 14115 case R_PPC64_REL24:
05d0e962 14116 case R_PPC64_REL24_NOTOC:
23cedd1d 14117 case R_PPC64_PLTCALL:
ad8e1ba5
AM
14118 /* Calls to functions with a different TOC, such as calls to
14119 shared objects, need to alter the TOC pointer. This is
14120 done using a linkage stub. A REL24 branching to these
14121 linkage stubs needs to be followed by a nop, as the nop
14122 will be replaced with an instruction to restore the TOC
14123 base pointer. */
8387904d 14124 fdh = h;
b31867b6
AM
14125 if (h != NULL
14126 && h->oh != NULL
14127 && h->oh->is_func_descriptor)
14128 fdh = ppc_follow_link (h->oh);
31c76678
DK
14129 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14130 htab);
23cedd1d
AM
14131 if (r_type == R_PPC64_PLTCALL
14132 && stub_entry != NULL
05d0e962
AM
14133 && stub_entry->stub_type >= ppc_stub_plt_call
14134 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
14135 stub_entry = NULL;
14136
6abec6bc 14137 if (stub_entry != NULL
d0abeec8
AM
14138 && ((stub_entry->stub_type >= ppc_stub_plt_call
14139 && stub_entry->stub_type <= ppc_stub_plt_call_both)
ad8e1ba5 14140 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
14141 || stub_entry->stub_type == ppc_stub_plt_branch_both
14142 || stub_entry->stub_type == ppc_stub_long_branch_r2off
14143 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 14144 {
b25116a9 14145 bfd_boolean can_plt_call = FALSE;
721956f4 14146
6e1816be
AM
14147 if (stub_entry->stub_type == ppc_stub_plt_call
14148 && !htab->opd_abi
14149 && htab->params->plt_localentry0 != 0
14150 && is_elfv2_localentry0 (&h->elf))
14151 {
14152 /* The function doesn't use or change r2. */
14153 can_plt_call = TRUE;
14154 }
05d0e962
AM
14155 else if (r_type == R_PPC64_REL24_NOTOC)
14156 {
14157 /* NOTOC calls don't need to restore r2. */
14158 can_plt_call = TRUE;
14159 }
6e1816be 14160
f378ab09 14161 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
14162 branch and link followed by a nop. The nop is
14163 replaced by an insn to restore r2. */
6e1816be 14164 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14165 {
ba8ca3e7
AM
14166 unsigned long br;
14167
14168 br = bfd_get_32 (input_bfd,
14169 contents + rel->r_offset);
14170 if ((br & 1) != 0)
41bd81ab 14171 {
ba8ca3e7
AM
14172 unsigned long nop;
14173
14174 nop = bfd_get_32 (input_bfd,
14175 contents + rel->r_offset + 4);
23cedd1d
AM
14176 if (nop == LD_R2_0R1 + STK_TOC (htab))
14177 can_plt_call = TRUE;
14178 else if (nop == NOP
14179 || nop == CROR_151515
14180 || nop == CROR_313131)
a7f2871e 14181 {
ba8ca3e7
AM
14182 if (h != NULL
14183 && (h == htab->tls_get_addr_fd
14184 || h == htab->tls_get_addr)
7c9cf415 14185 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14186 {
14187 /* Special stub used, leave nop alone. */
14188 }
14189 else
a078d95a
AM
14190 bfd_put_32 (input_bfd,
14191 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14192 contents + rel->r_offset + 4);
14193 can_plt_call = TRUE;
a7f2871e 14194 }
41bd81ab 14195 }
5bd4f169 14196 }
721956f4 14197
ba8ca3e7 14198 if (!can_plt_call && h != NULL)
721956f4 14199 {
ba8ca3e7
AM
14200 const char *name = h->elf.root.root.string;
14201
14202 if (*name == '.')
14203 ++name;
14204
14205 if (strncmp (name, "__libc_start_main", 17) == 0
14206 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14207 {
ba8ca3e7
AM
14208 /* Allow crt1 branch to go via a toc adjusting
14209 stub. Other calls that never return could do
14210 the same, if we could detect such. */
b25116a9 14211 can_plt_call = TRUE;
6ab189d5 14212 }
ba8ca3e7
AM
14213 }
14214
14215 if (!can_plt_call)
14216 {
14217 /* g++ as of 20130507 emits self-calls without a
14218 following nop. This is arguably wrong since we
14219 have conflicting information. On the one hand a
14220 global symbol and on the other a local call
14221 sequence, but don't error for this special case.
14222 It isn't possible to cheaply verify we have
14223 exactly such a call. Allow all calls to the same
14224 section. */
14225 asection *code_sec = sec;
14226
14227 if (get_opd_info (sec) != NULL)
ad8e1ba5 14228 {
ba8ca3e7
AM
14229 bfd_vma off = (relocation + addend
14230 - sec->output_section->vma
14231 - sec->output_offset);
bc30df16 14232
ba8ca3e7 14233 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14234 }
ba8ca3e7
AM
14235 if (code_sec == input_section)
14236 can_plt_call = TRUE;
14237 }
14238
14239 if (!can_plt_call)
14240 {
05d0e962
AM
14241 if (stub_entry->stub_type >= ppc_stub_plt_call
14242 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 14243 info->callbacks->einfo
695344c0 14244 /* xgettext:c-format */
c1c8c1ef 14245 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 14246 "(plt call stub)\n"),
4805fc55
AM
14247 input_bfd, input_section, rel->r_offset, sym_name);
14248 else
14249 info->callbacks->einfo
695344c0 14250 /* xgettext:c-format */
c1c8c1ef 14251 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 14252 "(toc save/adjust stub)\n"),
4805fc55 14253 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14254
14255 bfd_set_error (bfd_error_bad_value);
14256 ret = FALSE;
721956f4
AM
14257 }
14258
b25116a9 14259 if (can_plt_call
05d0e962
AM
14260 && stub_entry->stub_type >= ppc_stub_plt_call
14261 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
14262 unresolved_reloc = FALSE;
14263 }
14264
6abec6bc
AM
14265 if ((stub_entry == NULL
14266 || stub_entry->stub_type == ppc_stub_long_branch
14267 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14268 && get_opd_info (sec) != NULL)
14269 {
14270 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14271 bfd_vma off = (relocation + addend
14272 - sec->output_section->vma
14273 - sec->output_offset);
aef36ac1 14274 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14275 if (dest != (bfd_vma) -1)
14276 {
14277 relocation = dest;
14278 addend = 0;
bc30df16 14279 reloc_dest = DEST_OPD;
8387904d
AM
14280 }
14281 }
14282
b25116a9
AM
14283 /* If the branch is out of reach we ought to have a long
14284 branch stub. */
14285 from = (rel->r_offset
14286 + input_section->output_offset
14287 + input_section->output_section->vma);
14288
6911b7dc
AM
14289 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14290 ? fdh->elf.other
14291 : sym->st_other);
14292
6abec6bc
AM
14293 if (stub_entry != NULL
14294 && (stub_entry->stub_type == ppc_stub_long_branch
14295 || stub_entry->stub_type == ppc_stub_plt_branch)
14296 && (r_type == R_PPC64_ADDR14_BRTAKEN
14297 || r_type == R_PPC64_ADDR14_BRNTAKEN
14298 || (relocation + addend - from + max_br_offset
14299 < 2 * max_br_offset)))
14300 /* Don't use the stub if this branch is in range. */
14301 stub_entry = NULL;
b25116a9 14302
05d0e962
AM
14303 if (stub_entry != NULL
14304 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
14305 || stub_entry->stub_type == ppc_stub_long_branch_both
14306 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
14307 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14308 && (r_type != R_PPC64_REL24_NOTOC
14309 || ((fdh ? fdh->elf.other : sym->st_other)
14310 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
14311 && (relocation + addend - from + max_br_offset
14312 < 2 * max_br_offset))
14313 stub_entry = NULL;
14314
14315 if (stub_entry != NULL
14316 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
14317 || stub_entry->stub_type == ppc_stub_long_branch_both
14318 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14319 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14320 && r_type == R_PPC64_REL24_NOTOC
14321 && (relocation + addend - from + max_br_offset
14322 < 2 * max_br_offset))
14323 stub_entry = NULL;
14324
b25116a9
AM
14325 if (stub_entry != NULL)
14326 {
14327 /* Munge up the value and addend so that we call the stub
14328 rather than the procedure directly. */
a4b6fadd
AM
14329 asection *stub_sec = stub_entry->group->stub_sec;
14330
14331 if (stub_entry->stub_type == ppc_stub_save_res)
14332 relocation += (stub_sec->output_offset
14333 + stub_sec->output_section->vma
14334 + stub_sec->size - htab->sfpr->size
14335 - htab->sfpr->output_offset
14336 - htab->sfpr->output_section->vma);
14337 else
14338 relocation = (stub_entry->stub_offset
14339 + stub_sec->output_offset
14340 + stub_sec->output_section->vma);
b25116a9 14341 addend = 0;
bc30df16 14342 reloc_dest = DEST_STUB;
3b421ab3 14343
05d0e962
AM
14344 if (((stub_entry->stub_type == ppc_stub_plt_call
14345 && ALWAYS_EMIT_R2SAVE)
14346 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14347 || stub_entry->stub_type == ppc_stub_plt_call_both)
e81b4c93
AM
14348 && !(h != NULL
14349 && (h == htab->tls_get_addr_fd
14350 || h == htab->tls_get_addr)
14351 && htab->params->tls_get_addr_opt)
3b421ab3
AM
14352 && rel + 1 < relend
14353 && rel[1].r_offset == rel->r_offset + 4
14354 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14355 relocation += 4;
05d0e962
AM
14356 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
14357 || stub_entry->stub_type == ppc_stub_plt_branch_both
14358 || stub_entry->stub_type == ppc_stub_plt_call_both)
14359 && r_type == R_PPC64_REL24_NOTOC)
14360 relocation += 4;
b25116a9
AM
14361 }
14362
14363 if (insn != 0)
14364 {
794e51c0 14365 if (is_isa_v2)
721956f4 14366 {
b25116a9
AM
14367 /* Set 'a' bit. This is 0b00010 in BO field for branch
14368 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14369 for branch on CTR insns (BO == 1a00t or 1a01t). */
14370 if ((insn & (0x14 << 21)) == (0x04 << 21))
14371 insn |= 0x02 << 21;
14372 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14373 insn |= 0x08 << 21;
14374 else
14375 break;
14376 }
14377 else
14378 {
14379 /* Invert 'y' bit if not the default. */
4cc603a5 14380 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14381 insn ^= 0x01 << 21;
721956f4 14382 }
b25116a9 14383
95f0d0d2 14384 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 14385 }
e86ce104 14386
06da1e8e
AM
14387 /* NOP out calls to undefined weak functions.
14388 We can thus call a weak function without first
14389 checking whether the function is defined. */
b25116a9 14390 else if (h != NULL
039b3fef 14391 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14392 && h->elf.dynindx == -1
05d0e962
AM
14393 && (r_type == R_PPC64_REL24
14394 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 14395 && relocation == 0
4cc603a5 14396 && addend == 0)
e86ce104 14397 {
95f0d0d2 14398 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 14399 goto copy_reloc;
e86ce104 14400 }
65f38f15
AM
14401 break;
14402 }
5bd4f169 14403
65f38f15 14404 /* Set `addend'. */
411e1bfb 14405 tls_type = 0;
23cedd1d 14406 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
14407 switch (r_type)
14408 {
14409 default:
cf97bcb0
AM
14410 /* xgettext:c-format */
14411 _bfd_error_handler (_("%pB: %s unsupported"),
14412 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 14413
65f38f15 14414 bfd_set_error (bfd_error_bad_value);
b34976b6 14415 ret = FALSE;
c316a17c 14416 goto copy_reloc;
5bd4f169 14417
65f38f15 14418 case R_PPC64_NONE:
411e1bfb 14419 case R_PPC64_TLS:
727fc41e
AM
14420 case R_PPC64_TLSGD:
14421 case R_PPC64_TLSLD:
3b421ab3 14422 case R_PPC64_TOCSAVE:
04c9666a
AM
14423 case R_PPC64_GNU_VTINHERIT:
14424 case R_PPC64_GNU_VTENTRY:
006589cf 14425 case R_PPC64_ENTRY:
c316a17c 14426 goto copy_reloc;
5bd4f169
AM
14427
14428 /* GOT16 relocations. Like an ADDR16 using the symbol's
14429 address in the GOT as relocation value instead of the
411e1bfb 14430 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14431 symbol and put the symbol value there. */
411e1bfb
AM
14432 case R_PPC64_GOT_TLSGD16:
14433 case R_PPC64_GOT_TLSGD16_LO:
14434 case R_PPC64_GOT_TLSGD16_HI:
14435 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14436 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14437 goto dogot;
14438
14439 case R_PPC64_GOT_TLSLD16:
14440 case R_PPC64_GOT_TLSLD16_LO:
14441 case R_PPC64_GOT_TLSLD16_HI:
14442 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14443 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14444 goto dogot;
14445
14446 case R_PPC64_GOT_TPREL16_DS:
14447 case R_PPC64_GOT_TPREL16_LO_DS:
14448 case R_PPC64_GOT_TPREL16_HI:
14449 case R_PPC64_GOT_TPREL16_HA:
14450 tls_type = TLS_TLS | TLS_TPREL;
14451 goto dogot;
14452
14453 case R_PPC64_GOT_DTPREL16_DS:
14454 case R_PPC64_GOT_DTPREL16_LO_DS:
14455 case R_PPC64_GOT_DTPREL16_HI:
14456 case R_PPC64_GOT_DTPREL16_HA:
14457 tls_type = TLS_TLS | TLS_DTPREL;
14458 goto dogot;
14459
65f38f15
AM
14460 case R_PPC64_GOT16:
14461 case R_PPC64_GOT16_LO:
14462 case R_PPC64_GOT16_HI:
14463 case R_PPC64_GOT16_HA:
14464 case R_PPC64_GOT16_DS:
14465 case R_PPC64_GOT16_LO_DS:
411e1bfb 14466 dogot:
5bd4f169
AM
14467 {
14468 /* Relocation is to the entry for this symbol in the global
14469 offset table. */
e717da7e 14470 asection *got;
d881513a 14471 bfd_vma *offp;
5bd4f169 14472 bfd_vma off;
d881513a 14473 unsigned long indx = 0;
927be08e 14474 struct got_entry *ent;
65f38f15 14475
d881513a
AM
14476 if (tls_type == (TLS_TLS | TLS_LD)
14477 && (h == NULL
f5385ebf 14478 || !h->elf.def_dynamic))
927be08e 14479 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14480 else
5bd4f169 14481 {
d881513a
AM
14482 if (h != NULL)
14483 {
f0158f44
AM
14484 if (!htab->elf.dynamic_sections_created
14485 || h->elf.dynindx == -1
14486 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 14487 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
14488 /* This is actually a static link, or it is a
14489 -Bsymbolic link and the symbol is defined
14490 locally, or the symbol was forced to be local
14491 because of a version file. */
14492 ;
14493 else
14494 {
039b3fef 14495 indx = h->elf.dynindx;
d881513a
AM
14496 unresolved_reloc = FALSE;
14497 }
039b3fef 14498 ent = h->elf.got.glist;
d881513a 14499 }
411e1bfb 14500 else
5bd4f169 14501 {
d881513a
AM
14502 if (local_got_ents == NULL)
14503 abort ();
14504 ent = local_got_ents[r_symndx];
5bd4f169 14505 }
d881513a
AM
14506
14507 for (; ent != NULL; ent = ent->next)
31c76678 14508 if (ent->addend == orig_rel.r_addend
e717da7e 14509 && ent->owner == input_bfd
d881513a
AM
14510 && ent->tls_type == tls_type)
14511 break;
5bd4f169 14512 }
411e1bfb 14513
927be08e
AM
14514 if (ent == NULL)
14515 abort ();
14516 if (ent->is_indirect)
14517 ent = ent->got.ent;
14518 offp = &ent->got.offset;
14519 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
14520 if (got == NULL)
14521 abort ();
14522
411e1bfb
AM
14523 /* The offset must always be a multiple of 8. We use the
14524 least significant bit to record whether we have already
14525 processed this entry. */
d881513a 14526 off = *offp;
411e1bfb
AM
14527 if ((off & 1) != 0)
14528 off &= ~1;
5bd4f169
AM
14529 else
14530 {
411e1bfb
AM
14531 /* Generate relocs for the dynamic linker, except in
14532 the case of TLSLD where we'll use one entry per
14533 module. */
25f23106
AM
14534 asection *relgot;
14535 bfd_boolean ifunc;
e717da7e 14536
d881513a 14537 *offp = off | 1;
25f23106
AM
14538 relgot = NULL;
14539 ifunc = (h != NULL
14540 ? h->elf.type == STT_GNU_IFUNC
14541 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 14542 if (ifunc)
82e66161
AM
14543 {
14544 relgot = htab->elf.irelplt;
14545 if (indx == 0)
14546 htab->local_ifunc_resolver = 1;
14547 else if (is_static_defined (&h->elf))
14548 htab->maybe_local_ifunc_resolver = 1;
14549 }
f0158f44
AM
14550 else if (indx != 0
14551 || (bfd_link_pic (info)
14552 && (h == NULL
21d68fcd 14553 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 14554 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
14555 && !h->elf.def_dynamic))
14556 && !(tls_type == (TLS_TLS | TLS_TPREL)
14557 && bfd_link_executable (info)
14558 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 14559 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 14560 if (relgot != NULL)
5bd4f169 14561 {
e717da7e
AM
14562 outrel.r_offset = (got->output_section->vma
14563 + got->output_offset
411e1bfb 14564 + off);
4cc603a5 14565 outrel.r_addend = addend;
d881513a 14566 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 14567 {
411e1bfb 14568 outrel.r_addend = 0;
e515b051 14569 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
14570 if (tls_type == (TLS_TLS | TLS_GD))
14571 {
e717da7e
AM
14572 loc = relgot->contents;
14573 loc += (relgot->reloc_count++
d881513a
AM
14574 * sizeof (Elf64_External_Rela));
14575 bfd_elf64_swap_reloca_out (output_bfd,
14576 &outrel, loc);
e515b051 14577 outrel.r_offset += 8;
4cc603a5 14578 outrel.r_addend = addend;
d881513a
AM
14579 outrel.r_info
14580 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 14581 }
411e1bfb 14582 }
951fd09b 14583 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 14584 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 14585 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 14586 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
14587 else if (indx != 0)
14588 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14589 else
81407a69 14590 {
25f23106
AM
14591 if (ifunc)
14592 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14593 else
14594 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
14595
14596 /* Write the .got section contents for the sake
14597 of prelink. */
e717da7e 14598 loc = got->contents + off;
23fbd6fa
JJ
14599 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14600 loc);
81407a69 14601 }
81407a69
AM
14602
14603 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
14604 {
14605 outrel.r_addend += relocation;
14606 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
14607 {
14608 if (htab->elf.tls_sec == NULL)
14609 outrel.r_addend = 0;
14610 else
14611 outrel.r_addend -= htab->elf.tls_sec->vma;
14612 }
e515b051 14613 }
e717da7e
AM
14614 loc = relgot->contents;
14615 loc += (relgot->reloc_count++
411e1bfb
AM
14616 * sizeof (Elf64_External_Rela));
14617 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14618 }
14619
ad8e1ba5 14620 /* Init the .got section contents here if we're not
81407a69 14621 emitting a reloc. */
d881513a 14622 else
411e1bfb 14623 {
4cc603a5 14624 relocation += addend;
f0158f44 14625 if (tls_type != 0)
411e1bfb 14626 {
989f9879
AM
14627 if (htab->elf.tls_sec == NULL)
14628 relocation = 0;
14629 else
14630 {
f0158f44
AM
14631 if (tls_type & TLS_LD)
14632 relocation = 0;
14633 else
14634 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 14635 if (tls_type & TLS_TPREL)
989f9879
AM
14636 relocation += DTP_OFFSET - TP_OFFSET;
14637 }
5bd4f169 14638
f0158f44 14639 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
14640 {
14641 bfd_put_64 (output_bfd, relocation,
e717da7e 14642 got->contents + off + 8);
676ee2b5 14643 relocation = 1;
7b609f53 14644 }
411e1bfb
AM
14645 }
14646 bfd_put_64 (output_bfd, relocation,
e717da7e 14647 got->contents + off);
5bd4f169
AM
14648 }
14649 }
14650
65f38f15
AM
14651 if (off >= (bfd_vma) -2)
14652 abort ();
14653
bf102f86 14654 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 14655 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 14656 }
65f38f15
AM
14657 break;
14658
14659 case R_PPC64_PLT16_HA:
14660 case R_PPC64_PLT16_HI:
14661 case R_PPC64_PLT16_LO:
08be3224 14662 case R_PPC64_PLT16_LO_DS:
65f38f15
AM
14663 case R_PPC64_PLT32:
14664 case R_PPC64_PLT64:
23cedd1d
AM
14665 case R_PPC64_PLTSEQ:
14666 case R_PPC64_PLTCALL:
65f38f15
AM
14667 /* Relocation is to the entry for this symbol in the
14668 procedure linkage table. */
23cedd1d 14669 unresolved_reloc = TRUE;
cbf95972
AM
14670 {
14671 struct plt_entry **plt_list = NULL;
14672 if (h != NULL)
14673 plt_list = &h->elf.plt.plist;
14674 else if (local_got_ents != NULL)
14675 {
14676 struct plt_entry **local_plt = (struct plt_entry **)
14677 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 14678 plt_list = local_plt + r_symndx;
cbf95972
AM
14679 }
14680 if (plt_list)
14681 {
14682 struct plt_entry *ent;
65f38f15 14683
cbf95972
AM
14684 for (ent = *plt_list; ent != NULL; ent = ent->next)
14685 if (ent->plt.offset != (bfd_vma) -1
14686 && ent->addend == orig_rel.r_addend)
14687 {
14688 asection *plt;
08be3224 14689 bfd_vma got;
cbf95972
AM
14690
14691 plt = htab->elf.splt;
14692 if (!htab->elf.dynamic_sections_created
14693 || h == NULL
14694 || h->elf.dynindx == -1)
2d7ad24e
AM
14695 {
14696 if (h != NULL
14697 ? h->elf.type == STT_GNU_IFUNC
14698 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14699 plt = htab->elf.iplt;
14700 else
14701 plt = htab->pltlocal;
14702 }
14703 relocation = (plt->output_section->vma
14704 + plt->output_offset
14705 + ent->plt.offset);
08be3224 14706 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
14707 || r_type == R_PPC64_PLT16_HI
14708 || r_type == R_PPC64_PLT16_LO
14709 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
14710 {
14711 got = (elf_gp (output_bfd)
14712 + htab->sec_info[input_section->id].toc_off);
14713 relocation -= got;
14714 }
cbf95972
AM
14715 addend = 0;
14716 unresolved_reloc = FALSE;
14717 break;
14718 }
14719 }
14720 }
65f38f15 14721 break;
5bd4f169 14722
0b13192e
AM
14723 case R_PPC64_TOC:
14724 /* Relocation value is TOC base. */
14725 relocation = TOCstart;
cf35638d 14726 if (r_symndx == STN_UNDEF)
6f20ed8a 14727 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
14728 else if (unresolved_reloc)
14729 ;
6f20ed8a
AM
14730 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14731 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
14732 else
14733 unresolved_reloc = TRUE;
ab96bf03 14734 goto dodyn;
0b13192e 14735
5bd4f169
AM
14736 /* TOC16 relocs. We want the offset relative to the TOC base,
14737 which is the address of the start of the TOC plus 0x8000.
14738 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14739 in this order. */
65f38f15
AM
14740 case R_PPC64_TOC16:
14741 case R_PPC64_TOC16_LO:
14742 case R_PPC64_TOC16_HI:
14743 case R_PPC64_TOC16_DS:
14744 case R_PPC64_TOC16_LO_DS:
14745 case R_PPC64_TOC16_HA:
6f20ed8a 14746 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
14747 break;
14748
14749 /* Relocate against the beginning of the section. */
65f38f15
AM
14750 case R_PPC64_SECTOFF:
14751 case R_PPC64_SECTOFF_LO:
14752 case R_PPC64_SECTOFF_HI:
14753 case R_PPC64_SECTOFF_DS:
14754 case R_PPC64_SECTOFF_LO_DS:
14755 case R_PPC64_SECTOFF_HA:
4ce794b7 14756 if (sec != NULL)
65f38f15 14757 addend -= sec->output_section->vma;
5bd4f169
AM
14758 break;
14759
25f23106
AM
14760 case R_PPC64_REL16:
14761 case R_PPC64_REL16_LO:
14762 case R_PPC64_REL16_HI:
14763 case R_PPC64_REL16_HA:
4a969973
AM
14764 case R_PPC64_REL16_HIGH:
14765 case R_PPC64_REL16_HIGHA:
14766 case R_PPC64_REL16_HIGHER:
14767 case R_PPC64_REL16_HIGHERA:
14768 case R_PPC64_REL16_HIGHEST:
14769 case R_PPC64_REL16_HIGHESTA:
a680de9a 14770 case R_PPC64_REL16DX_HA:
25f23106
AM
14771 break;
14772
721956f4
AM
14773 case R_PPC64_REL14:
14774 case R_PPC64_REL14_BRNTAKEN:
14775 case R_PPC64_REL14_BRTAKEN:
5d1634d7 14776 case R_PPC64_REL24:
05d0e962 14777 case R_PPC64_REL24_NOTOC:
5d1634d7
AM
14778 break;
14779
411e1bfb
AM
14780 case R_PPC64_TPREL16:
14781 case R_PPC64_TPREL16_LO:
14782 case R_PPC64_TPREL16_HI:
14783 case R_PPC64_TPREL16_HA:
14784 case R_PPC64_TPREL16_DS:
14785 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
14786 case R_PPC64_TPREL16_HIGH:
14787 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
14788 case R_PPC64_TPREL16_HIGHER:
14789 case R_PPC64_TPREL16_HIGHERA:
14790 case R_PPC64_TPREL16_HIGHEST:
14791 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
14792 if (h != NULL
14793 && h->elf.root.type == bfd_link_hash_undefweak
14794 && h->elf.dynindx == -1)
14795 {
14796 /* Make this relocation against an undefined weak symbol
14797 resolve to zero. This is really just a tweak, since
14798 code using weak externs ought to check that they are
14799 defined before using them. */
14800 bfd_byte *p = contents + rel->r_offset - d_offset;
14801
95f0d0d2 14802 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
14803 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14804 if (insn != 0)
95f0d0d2 14805 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
14806 break;
14807 }
989f9879
AM
14808 if (htab->elf.tls_sec != NULL)
14809 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
14810 /* The TPREL16 relocs shouldn't really be used in shared
14811 libs or with non-local symbols as that will result in
14812 DT_TEXTREL being set, but support them anyway. */
14813 goto dodyn;
411e1bfb
AM
14814
14815 case R_PPC64_DTPREL16:
14816 case R_PPC64_DTPREL16_LO:
14817 case R_PPC64_DTPREL16_HI:
14818 case R_PPC64_DTPREL16_HA:
14819 case R_PPC64_DTPREL16_DS:
14820 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
14821 case R_PPC64_DTPREL16_HIGH:
14822 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
14823 case R_PPC64_DTPREL16_HIGHER:
14824 case R_PPC64_DTPREL16_HIGHERA:
14825 case R_PPC64_DTPREL16_HIGHEST:
14826 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
14827 if (htab->elf.tls_sec != NULL)
14828 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14829 break;
14830
45965137
AM
14831 case R_PPC64_ADDR64_LOCAL:
14832 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14833 ? h->elf.other
14834 : sym->st_other);
14835 break;
14836
e515b051
AM
14837 case R_PPC64_DTPMOD64:
14838 relocation = 1;
14839 addend = 0;
14840 goto dodyn;
14841
411e1bfb 14842 case R_PPC64_TPREL64:
989f9879
AM
14843 if (htab->elf.tls_sec != NULL)
14844 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
14845 goto dodyn;
14846
14847 case R_PPC64_DTPREL64:
989f9879
AM
14848 if (htab->elf.tls_sec != NULL)
14849 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 14850 /* Fall through. */
411e1bfb 14851
65f38f15
AM
14852 /* Relocations that may need to be propagated if this is a
14853 dynamic object. */
04c9666a 14854 case R_PPC64_REL30:
65f38f15
AM
14855 case R_PPC64_REL32:
14856 case R_PPC64_REL64:
14857 case R_PPC64_ADDR14:
14858 case R_PPC64_ADDR14_BRNTAKEN:
14859 case R_PPC64_ADDR14_BRTAKEN:
14860 case R_PPC64_ADDR16:
14861 case R_PPC64_ADDR16_DS:
14862 case R_PPC64_ADDR16_HA:
14863 case R_PPC64_ADDR16_HI:
f9c6b907
AM
14864 case R_PPC64_ADDR16_HIGH:
14865 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14866 case R_PPC64_ADDR16_HIGHER:
14867 case R_PPC64_ADDR16_HIGHERA:
14868 case R_PPC64_ADDR16_HIGHEST:
14869 case R_PPC64_ADDR16_HIGHESTA:
14870 case R_PPC64_ADDR16_LO:
14871 case R_PPC64_ADDR16_LO_DS:
14872 case R_PPC64_ADDR24:
65f38f15
AM
14873 case R_PPC64_ADDR32:
14874 case R_PPC64_ADDR64:
14875 case R_PPC64_UADDR16:
14876 case R_PPC64_UADDR32:
14877 case R_PPC64_UADDR64:
411e1bfb 14878 dodyn:
5d1634d7 14879 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
14880 break;
14881
41bd81ab
AM
14882 if (NO_OPD_RELOCS && is_opd)
14883 break;
14884
8a9e8e72 14885 if (bfd_link_pic (info)
b1b07054
AM
14886 ? ((h == NULL
14887 || h->dyn_relocs != NULL)
14888 && ((h != NULL && pc_dynrelocs (h))
14889 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
14890 : (h != NULL
14891 ? h->dyn_relocs != NULL
d311bc8b 14892 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 14893 {
b34976b6 14894 bfd_boolean skip, relocate;
65f38f15 14895 asection *sreloc;
1cf1f670 14896 bfd_vma out_off;
82e66161 14897 long indx = 0;
65f38f15
AM
14898
14899 /* When generating a dynamic object, these relocations
14900 are copied into the output file to be resolved at run
14901 time. */
14902
b34976b6
AM
14903 skip = FALSE;
14904 relocate = FALSE;
65f38f15 14905
1cf1f670
AM
14906 out_off = _bfd_elf_section_offset (output_bfd, info,
14907 input_section, rel->r_offset);
14908 if (out_off == (bfd_vma) -1)
b34976b6 14909 skip = TRUE;
1cf1f670 14910 else if (out_off == (bfd_vma) -2)
b34976b6 14911 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14912 out_off += (input_section->output_section->vma
14913 + input_section->output_offset);
14914 outrel.r_offset = out_off;
411e1bfb 14915 outrel.r_addend = rel->r_addend;
65f38f15 14916
1cf1f670
AM
14917 /* Optimize unaligned reloc use. */
14918 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14919 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14920 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14921 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14922 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14923 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14924 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14925 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14926 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14927
65f38f15 14928 if (skip)
0bb2d96a 14929 memset (&outrel, 0, sizeof outrel);
afe397ea 14930 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
14931 && !is_opd
14932 && r_type != R_PPC64_TOC)
14acf4dc 14933 {
82e66161
AM
14934 indx = h->elf.dynindx;
14935 BFD_ASSERT (indx != -1);
14936 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 14937 }
65f38f15
AM
14938 else
14939 {
41bd81ab
AM
14940 /* This symbol is local, or marked to become local,
14941 or this is an opd section reloc which must point
14942 at a local function. */
65f38f15 14943 outrel.r_addend += relocation;
e86ce104 14944 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14945 {
3fad3c7c 14946 if (is_opd && h != NULL)
afbe61cf
AM
14947 {
14948 /* Lie about opd entries. This case occurs
14949 when building shared libraries and we
14950 reference a function in another shared
3fad3c7c
AM
14951 lib. The same thing happens for a weak
14952 definition in an application that's
14953 overridden by a strong definition in a
14954 shared lib. (I believe this is a generic
14955 bug in binutils handling of weak syms.)
14956 In these cases we won't use the opd
1e2f5b6e 14957 entry in this lib. */
b34976b6 14958 unresolved_reloc = FALSE;
afbe61cf 14959 }
25f23106
AM
14960 if (!is_opd
14961 && r_type == R_PPC64_ADDR64
14962 && (h != NULL
14963 ? h->elf.type == STT_GNU_IFUNC
14964 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14965 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14966 else
14967 {
14968 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14969
25f23106
AM
14970 /* We need to relocate .opd contents for ld.so.
14971 Prelink also wants simple and consistent rules
14972 for relocs. This make all RELATIVE relocs have
14973 *r_offset equal to r_addend. */
14974 relocate = TRUE;
14975 }
65f38f15
AM
14976 }
14977 else
14978 {
25f23106
AM
14979 if (h != NULL
14980 ? h->elf.type == STT_GNU_IFUNC
14981 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14982 {
25f53a85 14983 info->callbacks->einfo
695344c0 14984 /* xgettext:c-format */
174d0a74 14985 (_("%H: %s for indirect "
c1c8c1ef 14986 "function `%pT' unsupported\n"),
25f53a85 14987 input_bfd, input_section, rel->r_offset,
25f23106
AM
14988 ppc64_elf_howto_table[r_type]->name,
14989 sym_name);
14990 ret = FALSE;
14991 }
cf35638d 14992 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14993 ;
14994 else if (sec == NULL || sec->owner == NULL)
14995 {
14996 bfd_set_error (bfd_error_bad_value);
b34976b6 14997 return FALSE;
65f38f15
AM
14998 }
14999 else
15000 {
15001 asection *osec;
15002
15003 osec = sec->output_section;
15004 indx = elf_section_data (osec)->dynindx;
15005
74541ad4
AM
15006 if (indx == 0)
15007 {
15008 if ((osec->flags & SEC_READONLY) == 0
15009 && htab->elf.data_index_section != NULL)
15010 osec = htab->elf.data_index_section;
15011 else
15012 osec = htab->elf.text_index_section;
15013 indx = elf_section_data (osec)->dynindx;
15014 }
15015 BFD_ASSERT (indx != 0);
15016
65f38f15
AM
15017 /* We are turning this relocation into one
15018 against a section symbol, so subtract out
15019 the output section's address but not the
15020 offset of the input section in the output
15021 section. */
15022 outrel.r_addend -= osec->vma;
15023 }
15024
15025 outrel.r_info = ELF64_R_INFO (indx, r_type);
15026 }
15027 }
15028
15029 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
15030 if (h != NULL
15031 ? h->elf.type == STT_GNU_IFUNC
15032 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
15033 {
15034 sreloc = htab->elf.irelplt;
15035 if (indx == 0)
15036 htab->local_ifunc_resolver = 1;
15037 else if (is_static_defined (&h->elf))
15038 htab->maybe_local_ifunc_resolver = 1;
15039 }
65f38f15
AM
15040 if (sreloc == NULL)
15041 abort ();
15042
dfbb6ac9
AM
15043 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15044 >= sreloc->size)
15045 abort ();
947216bf
AM
15046 loc = sreloc->contents;
15047 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
15048 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15049
15050 /* If this reloc is against an external symbol, it will
15051 be computed at runtime, so there's no need to do
81407a69
AM
15052 anything now. However, for the sake of prelink ensure
15053 that the section contents are a known value. */
2cdcc330 15054 if (!relocate)
81407a69
AM
15055 {
15056 unresolved_reloc = FALSE;
15057 /* The value chosen here is quite arbitrary as ld.so
15058 ignores section contents except for the special
15059 case of .opd where the contents might be accessed
15060 before relocation. Choose zero, as that won't
15061 cause reloc overflow. */
15062 relocation = 0;
15063 addend = 0;
15064 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15065 to improve backward compatibility with older
15066 versions of ld. */
15067 if (r_type == R_PPC64_ADDR64)
15068 addend = outrel.r_addend;
15069 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 15070 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 15071 addend = outrel.r_offset;
81407a69 15072 }
65f38f15 15073 }
5bd4f169
AM
15074 break;
15075
65f38f15
AM
15076 case R_PPC64_COPY:
15077 case R_PPC64_GLOB_DAT:
15078 case R_PPC64_JMP_SLOT:
25f23106 15079 case R_PPC64_JMP_IREL:
65f38f15
AM
15080 case R_PPC64_RELATIVE:
15081 /* We shouldn't ever see these dynamic relocs in relocatable
15082 files. */
ae9a127f 15083 /* Fall through. */
65f38f15
AM
15084
15085 case R_PPC64_PLTGOT16:
15086 case R_PPC64_PLTGOT16_DS:
15087 case R_PPC64_PLTGOT16_HA:
15088 case R_PPC64_PLTGOT16_HI:
15089 case R_PPC64_PLTGOT16_LO:
15090 case R_PPC64_PLTGOT16_LO_DS:
15091 case R_PPC64_PLTREL32:
15092 case R_PPC64_PLTREL64:
15093 /* These ones haven't been implemented yet. */
15094
25f53a85 15095 info->callbacks->einfo
695344c0 15096 /* xgettext:c-format */
c1c8c1ef 15097 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 15098 input_bfd,
4ce794b7 15099 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
15100
15101 bfd_set_error (bfd_error_invalid_operation);
b34976b6 15102 ret = FALSE;
c316a17c 15103 goto copy_reloc;
65f38f15 15104 }
5bd4f169 15105
67f0cbdb
AM
15106 /* Multi-instruction sequences that access the TOC can be
15107 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 15108 to nop; addi rb,r2,x; */
67f0cbdb
AM
15109 switch (r_type)
15110 {
15111 default:
15112 break;
15113
15114 case R_PPC64_GOT_TLSLD16_HI:
15115 case R_PPC64_GOT_TLSGD16_HI:
15116 case R_PPC64_GOT_TPREL16_HI:
15117 case R_PPC64_GOT_DTPREL16_HI:
15118 case R_PPC64_GOT16_HI:
15119 case R_PPC64_TOC16_HI:
15120 /* These relocs would only be useful if building up an
15121 offset to later add to r2, perhaps in an indexed
15122 addressing mode instruction. Don't try to optimize.
15123 Unfortunately, the possibility of someone building up an
15124 offset like this or even with the HA relocs, means that
15125 we need to check the high insn when optimizing the low
15126 insn. */
15127 break;
15128
23cedd1d
AM
15129 case R_PPC64_PLTCALL:
15130 if (unresolved_reloc)
15131 {
15132 /* No plt entry. Make this into a direct call. */
15133 bfd_byte *p = contents + rel->r_offset;
15134 insn = bfd_get_32 (input_bfd, p);
15135 insn &= 1;
15136 bfd_put_32 (input_bfd, B_DOT | insn, p);
15137 bfd_put_32 (input_bfd, NOP, p + 4);
15138 unresolved_reloc = save_unresolved_reloc;
15139 r_type = R_PPC64_REL24;
15140 }
15141 break;
15142
15143 case R_PPC64_PLTSEQ:
15144 if (unresolved_reloc)
15145 {
15146 unresolved_reloc = FALSE;
15147 goto nop_it;
15148 }
15149 break;
15150
15151 case R_PPC64_PLT16_HA:
15152 if (unresolved_reloc)
15153 {
15154 unresolved_reloc = FALSE;
15155 goto nop_it;
15156 }
15157 /* Fall through. */
67f0cbdb
AM
15158 case R_PPC64_GOT_TLSLD16_HA:
15159 case R_PPC64_GOT_TLSGD16_HA:
15160 case R_PPC64_GOT_TPREL16_HA:
15161 case R_PPC64_GOT_DTPREL16_HA:
15162 case R_PPC64_GOT16_HA:
15163 case R_PPC64_TOC16_HA:
98528052 15164 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15165 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 15166 {
23cedd1d
AM
15167 bfd_byte *p;
15168 nop_it:
15169 p = contents + (rel->r_offset & ~3);
98528052 15170 bfd_put_32 (input_bfd, NOP, p);
d830549d 15171 goto copy_reloc;
98528052 15172 }
67f0cbdb
AM
15173 break;
15174
23cedd1d
AM
15175 case R_PPC64_PLT16_LO:
15176 case R_PPC64_PLT16_LO_DS:
15177 if (unresolved_reloc)
15178 {
15179 unresolved_reloc = FALSE;
15180 goto nop_it;
15181 }
15182 /* Fall through. */
67f0cbdb
AM
15183 case R_PPC64_GOT_TLSLD16_LO:
15184 case R_PPC64_GOT_TLSGD16_LO:
15185 case R_PPC64_GOT_TPREL16_LO_DS:
15186 case R_PPC64_GOT_DTPREL16_LO_DS:
15187 case R_PPC64_GOT16_LO:
15188 case R_PPC64_GOT16_LO_DS:
15189 case R_PPC64_TOC16_LO:
15190 case R_PPC64_TOC16_LO_DS:
98528052 15191 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15192 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
15193 {
15194 bfd_byte *p = contents + (rel->r_offset & ~3);
15195 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
15196 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15197 {
15198 /* Transform addic to addi when we change reg. */
15199 insn &= ~((0x3f << 26) | (0x1f << 16));
15200 insn |= (14u << 26) | (2 << 16);
15201 }
15202 else
67f0cbdb 15203 {
98528052
AM
15204 insn &= ~(0x1f << 16);
15205 insn |= 2 << 16;
67f0cbdb 15206 }
560c8763 15207 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
15208 }
15209 break;
9a23f96e
AM
15210
15211 case R_PPC64_TPREL16_HA:
15212 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15213 {
15214 bfd_byte *p = contents + (rel->r_offset & ~3);
15215 insn = bfd_get_32 (input_bfd, p);
15216 if ((insn & ((0x3f << 26) | 0x1f << 16))
15217 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15218 /* xgettext:c-format */
15219 info->callbacks->minfo
15220 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
15221 input_bfd, input_section, rel->r_offset,
15222 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 15223 else
d830549d
AM
15224 {
15225 bfd_put_32 (input_bfd, NOP, p);
15226 goto copy_reloc;
15227 }
9a23f96e
AM
15228 }
15229 break;
15230
15231 case R_PPC64_TPREL16_LO:
15232 case R_PPC64_TPREL16_LO_DS:
15233 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15234 {
15235 bfd_byte *p = contents + (rel->r_offset & ~3);
15236 insn = bfd_get_32 (input_bfd, p);
15237 insn &= ~(0x1f << 16);
15238 insn |= 13 << 16;
15239 bfd_put_32 (input_bfd, insn, p);
15240 }
15241 break;
67f0cbdb
AM
15242 }
15243
65f38f15
AM
15244 /* Do any further special processing. */
15245 switch (r_type)
15246 {
15247 default:
15248 break;
15249
25f23106 15250 case R_PPC64_REL16_HA:
4a969973
AM
15251 case R_PPC64_REL16_HIGHA:
15252 case R_PPC64_REL16_HIGHERA:
15253 case R_PPC64_REL16_HIGHESTA:
a680de9a 15254 case R_PPC64_REL16DX_HA:
f9c6b907
AM
15255 case R_PPC64_ADDR16_HA:
15256 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15257 case R_PPC64_ADDR16_HIGHERA:
15258 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
15259 case R_PPC64_TOC16_HA:
15260 case R_PPC64_SECTOFF_HA:
411e1bfb 15261 case R_PPC64_TPREL16_HA:
f9c6b907 15262 case R_PPC64_TPREL16_HIGHA:
411e1bfb 15263 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 15264 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
15265 case R_PPC64_DTPREL16_HA:
15266 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 15267 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 15268 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
15269 /* It's just possible that this symbol is a weak symbol
15270 that's not actually defined anywhere. In that case,
15271 'sec' would be NULL, and we should leave the symbol
15272 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
15273 if (sec == NULL)
15274 break;
1a0670f3 15275 /* Fall through. */
5c5f6e17
AM
15276
15277 case R_PPC64_GOT16_HA:
15278 case R_PPC64_PLTGOT16_HA:
15279 case R_PPC64_PLT16_HA:
15280 case R_PPC64_GOT_TLSGD16_HA:
15281 case R_PPC64_GOT_TLSLD16_HA:
15282 case R_PPC64_GOT_TPREL16_HA:
15283 case R_PPC64_GOT_DTPREL16_HA:
15284 /* Add 0x10000 if sign bit in 0:15 is set.
15285 Bits 0:15 are not used. */
15286 addend += 0x8000;
65f38f15
AM
15287 break;
15288
15289 case R_PPC64_ADDR16_DS:
15290 case R_PPC64_ADDR16_LO_DS:
15291 case R_PPC64_GOT16_DS:
15292 case R_PPC64_GOT16_LO_DS:
15293 case R_PPC64_PLT16_LO_DS:
15294 case R_PPC64_SECTOFF_DS:
15295 case R_PPC64_SECTOFF_LO_DS:
15296 case R_PPC64_TOC16_DS:
15297 case R_PPC64_TOC16_LO_DS:
15298 case R_PPC64_PLTGOT16_DS:
15299 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
15300 case R_PPC64_GOT_TPREL16_DS:
15301 case R_PPC64_GOT_TPREL16_LO_DS:
15302 case R_PPC64_GOT_DTPREL16_DS:
15303 case R_PPC64_GOT_DTPREL16_LO_DS:
15304 case R_PPC64_TPREL16_DS:
15305 case R_PPC64_TPREL16_LO_DS:
15306 case R_PPC64_DTPREL16_DS:
15307 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
15308 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15309 mask = 3;
a680de9a
PB
15310 /* If this reloc is against an lq, lxv, or stxv insn, then
15311 the value must be a multiple of 16. This is somewhat of
15312 a hack, but the "correct" way to do this by defining _DQ
15313 forms of all the _DS relocs bloats all reloc switches in
15314 this file. It doesn't make much sense to use these
15315 relocs in data, so testing the insn should be safe. */
15316 if ((insn & (0x3f << 26)) == (56u << 26)
15317 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 15318 mask = 15;
a680de9a
PB
15319 relocation += addend;
15320 addend = insn & (mask ^ 3);
15321 if ((relocation & mask) != 0)
65f38f15 15322 {
a680de9a 15323 relocation ^= relocation & mask;
25f53a85 15324 info->callbacks->einfo
695344c0 15325 /* xgettext:c-format */
174d0a74 15326 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 15327 input_bfd, input_section, rel->r_offset,
d830549d 15328 ppc64_elf_howto_table[r_type]->name,
adadcc0c 15329 mask + 1);
65f38f15 15330 bfd_set_error (bfd_error_bad_value);
b34976b6 15331 ret = FALSE;
c316a17c 15332 goto copy_reloc;
65f38f15
AM
15333 }
15334 break;
5bd4f169
AM
15335 }
15336
239e1f3a
AM
15337 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15338 because such sections are not SEC_ALLOC and thus ld.so will
15339 not process them. */
d830549d 15340 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 15341 if (unresolved_reloc
239e1f3a 15342 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
15343 && h->elf.def_dynamic)
15344 && _bfd_elf_section_offset (output_bfd, info, input_section,
15345 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 15346 {
25f53a85 15347 info->callbacks->einfo
695344c0 15348 /* xgettext:c-format */
c1c8c1ef 15349 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 15350 input_bfd, input_section, rel->r_offset,
b80eed39 15351 howto->name,
039b3fef 15352 h->elf.root.root.string);
b34976b6 15353 ret = FALSE;
9c07fe7c 15354 }
5bd4f169 15355
b80eed39
AM
15356 /* 16-bit fields in insns mostly have signed values, but a
15357 few insns have 16-bit unsigned values. Really, we should
15358 have different reloc types. */
15359 if (howto->complain_on_overflow != complain_overflow_dont
15360 && howto->dst_mask == 0xffff
15361 && (input_section->flags & SEC_CODE) != 0)
15362 {
15363 enum complain_overflow complain = complain_overflow_signed;
15364
15365 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
15366 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15367 complain = complain_overflow_bitfield;
15368 else if (howto->rightshift == 0
15369 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15370 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15371 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15372 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15373 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15374 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
15375 complain = complain_overflow_unsigned;
15376 if (howto->complain_on_overflow != complain)
15377 {
15378 alt_howto = *howto;
15379 alt_howto.complain_on_overflow = complain;
15380 howto = &alt_howto;
15381 }
15382 }
15383
a680de9a
PB
15384 if (r_type == R_PPC64_REL16DX_HA)
15385 {
15386 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15387 if (rel->r_offset + 4 > input_section->size)
15388 r = bfd_reloc_outofrange;
15389 else
15390 {
15391 relocation += addend;
15392 relocation -= (rel->r_offset
15393 + input_section->output_offset
15394 + input_section->output_section->vma);
3de43e7b 15395 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
15396 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15397 insn &= ~0x1fffc1;
3de43e7b 15398 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
15399 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15400 r = bfd_reloc_ok;
3de43e7b 15401 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
15402 r = bfd_reloc_overflow;
15403 }
15404 }
15405 else
15406 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15407 rel->r_offset, relocation, addend);
5bd4f169 15408
ef60b7ff 15409 if (r != bfd_reloc_ok)
5bd4f169 15410 {
bc30df16 15411 char *more_info = NULL;
b80eed39 15412 const char *reloc_name = howto->name;
bc30df16
AM
15413
15414 if (reloc_dest != DEST_NORMAL)
15415 {
15416 more_info = bfd_malloc (strlen (reloc_name) + 8);
15417 if (more_info != NULL)
15418 {
15419 strcpy (more_info, reloc_name);
15420 strcat (more_info, (reloc_dest == DEST_OPD
15421 ? " (OPD)" : " (stub)"));
15422 reloc_name = more_info;
15423 }
15424 }
15425
cd27b276 15426 if (r == bfd_reloc_overflow)
5bd4f169 15427 {
8131c122
AM
15428 /* On code like "if (foo) foo();" don't report overflow
15429 on a branch to zero when foo is undefined. */
15430 if (!warned
15431 && (reloc_dest == DEST_STUB
15432 || !(h != NULL
15433 && (h->elf.root.type == bfd_link_hash_undefweak
15434 || h->elf.root.type == bfd_link_hash_undefined)
15435 && is_branch_reloc (r_type))))
1a72702b
AM
15436 info->callbacks->reloc_overflow (info, &h->elf.root,
15437 sym_name, reloc_name,
15438 orig_rel.r_addend,
15439 input_bfd, input_section,
15440 rel->r_offset);
ef60b7ff
AM
15441 }
15442 else
15443 {
25f53a85 15444 info->callbacks->einfo
695344c0 15445 /* xgettext:c-format */
c1c8c1ef 15446 (_("%H: %s against `%pT': error %d\n"),
25f53a85 15447 input_bfd, input_section, rel->r_offset,
bc30df16 15448 reloc_name, sym_name, (int) r);
b34976b6 15449 ret = FALSE;
ef60b7ff 15450 }
bc30df16
AM
15451 if (more_info != NULL)
15452 free (more_info);
5bd4f169 15453 }
c316a17c
AM
15454 copy_reloc:
15455 if (wrel != rel)
15456 *wrel = *rel;
15457 }
15458
15459 if (wrel != rel)
15460 {
15461 Elf_Internal_Shdr *rel_hdr;
15462 size_t deleted = rel - wrel;
15463
15464 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15465 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15466 if (rel_hdr->sh_size == 0)
15467 {
15468 /* It is too late to remove an empty reloc section. Leave
15469 one NONE reloc.
15470 ??? What is wrong with an empty section??? */
15471 rel_hdr->sh_size = rel_hdr->sh_entsize;
15472 deleted -= 1;
15473 }
15474 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15475 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15476 input_section->reloc_count -= deleted;
5bd4f169
AM
15477 }
15478
645ea6a9
AM
15479 /* If we're emitting relocations, then shortly after this function
15480 returns, reloc offsets and addends for this section will be
15481 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15482 file rather than the input. Save a copy of the relocs for
15483 opd_entry_value. */
0e1862bb 15484 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15485 {
15486 bfd_size_type amt;
15487 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15488 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
15489 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15490 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
15491 if (rel == NULL)
15492 return FALSE;
15493 memcpy (rel, relocs, amt);
15494 }
5bd4f169
AM
15495 return ret;
15496}
15497
754021d0
AM
15498/* Adjust the value of any local symbols in opd sections. */
15499
6e0b88f1 15500static int
754021d0
AM
15501ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15502 const char *name ATTRIBUTE_UNUSED,
15503 Elf_Internal_Sym *elfsym,
15504 asection *input_sec,
15505 struct elf_link_hash_entry *h)
15506{
74f0fb50
AM
15507 struct _opd_sec_data *opd;
15508 long adjust;
754021d0
AM
15509 bfd_vma value;
15510
4025353c 15511 if (h != NULL)
6e0b88f1 15512 return 1;
4025353c 15513
74f0fb50
AM
15514 opd = get_opd_info (input_sec);
15515 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 15516 return 1;
754021d0
AM
15517
15518 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 15519 if (!bfd_link_relocatable (info))
754021d0
AM
15520 value -= input_sec->output_section->vma;
15521
51aecdc5 15522 adjust = opd->adjust[OPD_NDX (value)];
4025353c 15523 if (adjust == -1)
6e0b88f1
AM
15524 return 2;
15525
15526 elfsym->st_value += adjust;
15527 return 1;
754021d0
AM
15528}
15529
5bd4f169
AM
15530/* Finish up dynamic symbol handling. We set the contents of various
15531 dynamic sections here. */
15532
b34976b6 15533static bfd_boolean
4ce794b7
AM
15534ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15535 struct bfd_link_info *info,
15536 struct elf_link_hash_entry *h,
4aef7643 15537 Elf_Internal_Sym *sym)
5bd4f169 15538{
65f38f15 15539 struct ppc_link_hash_table *htab;
8387904d 15540 struct plt_entry *ent;
5bd4f169 15541
65f38f15 15542 htab = ppc_hash_table (info);
4dfe6ac6
NC
15543 if (htab == NULL)
15544 return FALSE;
5bd4f169 15545
49c09209
AM
15546 if (!htab->opd_abi && !h->def_regular)
15547 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15548 if (ent->plt.offset != (bfd_vma) -1)
15549 {
15550 /* Mark the symbol as undefined, rather than as
15551 defined in glink. Leave the value if there were
15552 any relocations where pointer equality matters
15553 (this is a clue for the dynamic linker, to make
15554 function pointer comparisons work between an
15555 application and shared library), otherwise set it
15556 to zero. */
15557 sym->st_shndx = SHN_UNDEF;
15558 if (!h->pointer_equality_needed)
15559 sym->st_value = 0;
15560 else if (!h->ref_regular_nonweak)
15561 {
15562 /* This breaks function pointer comparisons, but
15563 that is better than breaking tests for a NULL
15564 function pointer. */
15565 sym->st_value = 0;
15566 }
15567 break;
15568 }
5bd4f169 15569
f5385ebf 15570 if (h->needs_copy)
5bd4f169 15571 {
65f38f15 15572 /* This symbol needs a copy reloc. Set it up. */
49c09209 15573 Elf_Internal_Rela rela;
5474d94f 15574 asection *srel;
49c09209 15575 bfd_byte *loc;
5bd4f169 15576
65f38f15
AM
15577 if (h->dynindx == -1
15578 || (h->root.type != bfd_link_hash_defined
15579 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
15580 || htab->elf.srelbss == NULL
15581 || htab->elf.sreldynrelro == NULL)
65f38f15 15582 abort ();
5bd4f169
AM
15583
15584 rela.r_offset = (h->root.u.def.value
15585 + h->root.u.def.section->output_section->vma
15586 + h->root.u.def.section->output_offset);
15587 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15588 rela.r_addend = 0;
afbf7e8e 15589 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
15590 srel = htab->elf.sreldynrelro;
15591 else
15592 srel = htab->elf.srelbss;
15593 loc = srel->contents;
15594 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 15595 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
15596 }
15597
b34976b6 15598 return TRUE;
5bd4f169
AM
15599}
15600
65f38f15
AM
15601/* Used to decide how to sort relocs in an optimal manner for the
15602 dynamic linker, before writing them out. */
15603
15604static enum elf_reloc_type_class
7e612e98
AM
15605ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15606 const asection *rel_sec,
15607 const Elf_Internal_Rela *rela)
65f38f15 15608{
04c9666a 15609 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
15610 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15611
33e44f2e 15612 if (rel_sec == htab->elf.irelplt)
7e612e98 15613 return reloc_class_ifunc;
a33d1f77 15614
4ce794b7 15615 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 15616 switch (r_type)
65f38f15
AM
15617 {
15618 case R_PPC64_RELATIVE:
15619 return reloc_class_relative;
15620 case R_PPC64_JMP_SLOT:
15621 return reloc_class_plt;
15622 case R_PPC64_COPY:
15623 return reloc_class_copy;
15624 default:
15625 return reloc_class_normal;
15626 }
15627}
15628
5bd4f169
AM
15629/* Finish up the dynamic sections. */
15630
b34976b6 15631static bfd_boolean
4ce794b7
AM
15632ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15633 struct bfd_link_info *info)
5bd4f169 15634{
65f38f15
AM
15635 struct ppc_link_hash_table *htab;
15636 bfd *dynobj;
5bd4f169 15637 asection *sdyn;
5bd4f169 15638
65f38f15 15639 htab = ppc_hash_table (info);
4dfe6ac6
NC
15640 if (htab == NULL)
15641 return FALSE;
15642
65f38f15 15643 dynobj = htab->elf.dynobj;
3d4d4302 15644 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 15645
65f38f15 15646 if (htab->elf.dynamic_sections_created)
5bd4f169 15647 {
5bd4f169
AM
15648 Elf64_External_Dyn *dyncon, *dynconend;
15649
33e44f2e 15650 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 15651 abort ();
5bd4f169
AM
15652
15653 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 15654 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
15655 for (; dyncon < dynconend; dyncon++)
15656 {
15657 Elf_Internal_Dyn dyn;
19397422 15658 asection *s;
5bd4f169
AM
15659
15660 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15661
15662 switch (dyn.d_tag)
15663 {
65f38f15
AM
15664 default:
15665 continue;
5bd4f169 15666
5d1634d7 15667 case DT_PPC64_GLINK:
4ce794b7 15668 s = htab->glink;
6348e046 15669 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
15670 /* We stupidly defined DT_PPC64_GLINK to be the start
15671 of glink rather than the first entry point, which is
15672 what ld.so needs, and now have a bigger stub to
15673 support automatic multiple TOCs. */
9e390558 15674 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
15675 break;
15676
19397422
AM
15677 case DT_PPC64_OPD:
15678 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15679 if (s == NULL)
15680 continue;
15681 dyn.d_un.d_ptr = s->vma;
19397422
AM
15682 break;
15683
e8910a83
AM
15684 case DT_PPC64_OPT:
15685 if (htab->do_multi_toc && htab->multi_toc_needed)
15686 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
15687 if (htab->has_plt_localentry0)
15688 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
15689 break;
15690
19397422
AM
15691 case DT_PPC64_OPDSZ:
15692 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15693 if (s == NULL)
15694 continue;
eea6121a 15695 dyn.d_un.d_val = s->size;
19397422
AM
15696 break;
15697
65f38f15 15698 case DT_PLTGOT:
33e44f2e 15699 s = htab->elf.splt;
6348e046 15700 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
15701 break;
15702
15703 case DT_JMPREL:
33e44f2e 15704 s = htab->elf.srelplt;
6348e046 15705 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 15706 break;
5bd4f169 15707
65f38f15 15708 case DT_PLTRELSZ:
33e44f2e 15709 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 15710 break;
82e66161
AM
15711
15712 case DT_TEXTREL:
15713 if (htab->local_ifunc_resolver)
15714 info->callbacks->einfo
15715 (_("%X%P: text relocations and GNU indirect "
15716 "functions will result in a segfault at runtime\n"));
15717 else if (htab->maybe_local_ifunc_resolver)
15718 info->callbacks->einfo
15719 (_("%P: warning: text relocations and GNU indirect "
15720 "functions may result in a segfault at runtime\n"));
15721 continue;
5bd4f169 15722 }
5bd4f169 15723
65f38f15 15724 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 15725 }
5bd4f169
AM
15726 }
15727
6528b6eb
AM
15728 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15729 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15730 {
15731 /* Fill in the first entry in the global offset table.
15732 We use it to hold the link-time TOCbase. */
15733 bfd_put_64 (output_bfd,
60ee0d4a 15734 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 15735 htab->elf.sgot->contents);
5d1634d7
AM
15736
15737 /* Set .got entry size. */
2cdcc330
AM
15738 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
15739 = 8;
5d1634d7
AM
15740 }
15741
6528b6eb
AM
15742 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15743 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15744 {
15745 /* Set .plt entry size. */
33e44f2e 15746 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 15747 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
15748 }
15749
84f5d08e
AM
15750 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15751 brlt ourselves if emitrelocations. */
15752 if (htab->brlt != NULL
15753 && htab->brlt->reloc_count != 0
15754 && !_bfd_elf_link_output_relocs (output_bfd,
15755 htab->brlt,
d4730f92 15756 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
15757 elf_section_data (htab->brlt)->relocs,
15758 NULL))
15759 return FALSE;
15760
176a0d42
AM
15761 if (htab->glink != NULL
15762 && htab->glink->reloc_count != 0
15763 && !_bfd_elf_link_output_relocs (output_bfd,
15764 htab->glink,
d4730f92 15765 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
15766 elf_section_data (htab->glink)->relocs,
15767 NULL))
15768 return FALSE;
15769
da44f4e5 15770
df136d64
AM
15771 if (htab->glink_eh_frame != NULL
15772 && htab->glink_eh_frame->size != 0
15773 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15774 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15775 htab->glink_eh_frame,
15776 htab->glink_eh_frame->contents))
15777 return FALSE;
58d180e8 15778
e717da7e 15779 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
15780 since we didn't add them to DYNOBJ. We know dynobj is the first
15781 bfd. */
c72f2fb2 15782 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
15783 {
15784 asection *s;
7b53ace3 15785
0c8d6e5c 15786 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
15787 continue;
15788
e717da7e
AM
15789 s = ppc64_elf_tdata (dynobj)->got;
15790 if (s != NULL
eea6121a 15791 && s->size != 0
e717da7e
AM
15792 && s->output_section != bfd_abs_section_ptr
15793 && !bfd_set_section_contents (output_bfd, s->output_section,
15794 s->contents, s->output_offset,
eea6121a 15795 s->size))
e717da7e
AM
15796 return FALSE;
15797 s = ppc64_elf_tdata (dynobj)->relgot;
15798 if (s != NULL
eea6121a 15799 && s->size != 0
e717da7e
AM
15800 && s->output_section != bfd_abs_section_ptr
15801 && !bfd_set_section_contents (output_bfd, s->output_section,
15802 s->contents, s->output_offset,
eea6121a 15803 s->size))
e717da7e
AM
15804 return FALSE;
15805 }
f6c52c13 15806
b34976b6 15807 return TRUE;
5bd4f169
AM
15808}
15809
5bd4f169 15810#include "elf64-target.h"
7b8e7dad
AM
15811
15812/* FreeBSD support */
15813
15814#undef TARGET_LITTLE_SYM
15815#undef TARGET_LITTLE_NAME
15816
15817#undef TARGET_BIG_SYM
6d00b590 15818#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
15819#undef TARGET_BIG_NAME
15820#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15821
15822#undef ELF_OSABI
15823#define ELF_OSABI ELFOSABI_FREEBSD
15824
15825#undef elf64_bed
15826#define elf64_bed elf64_powerpc_fbsd_bed
15827
15828#include "elf64-target.h"
This page took 3.308788 seconds and 4 git commands to generate.