PowerPC D-form prefixed loads and stores
[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;
066f4018
AM
1597
1598 /* Set if got relocs that can be optimised are present in this file. */
1599 unsigned int has_gotrel : 1;
e717da7e
AM
1600};
1601
1602#define ppc64_elf_tdata(bfd) \
1603 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1604
1605#define ppc64_tlsld_got(bfd) \
1606 (&ppc64_elf_tdata (bfd)->tlsld_got)
1607
0c8d6e5c
AM
1608#define is_ppc64_elf(bfd) \
1609 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1610 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1611
e717da7e
AM
1612/* Override the generic function because we store some extras. */
1613
1614static bfd_boolean
1615ppc64_elf_mkobject (bfd *abfd)
1616{
0ffa91dd 1617 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1618 PPC64_ELF_DATA);
e717da7e
AM
1619}
1620
feee612b 1621/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1622 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1623
b34976b6 1624static bfd_boolean
4ce794b7 1625ppc64_elf_object_p (bfd *abfd)
feee612b 1626{
14b57c7c
AM
1627 if (!abfd->arch_info->the_default)
1628 return TRUE;
1629
1630 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1631 {
1632 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1633
1634 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1635 {
1636 /* Relies on arch after 32 bit default being 64 bit default. */
1637 abfd->arch_info = abfd->arch_info->next;
1638 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1639 }
1640 }
14b57c7c 1641 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1642}
1643
d37c89e5
AM
1644/* Support for core dump NOTE sections. */
1645
1646static bfd_boolean
1647ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1648{
eea6121a 1649 size_t offset, size;
d37c89e5
AM
1650
1651 if (note->descsz != 504)
1652 return FALSE;
1653
1654 /* pr_cursig */
228e534f 1655 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1656
1657 /* pr_pid */
228e534f 1658 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1659
1660 /* pr_reg */
1661 offset = 112;
eea6121a 1662 size = 384;
d37c89e5
AM
1663
1664 /* Make a ".reg/999" section. */
1665 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1666 size, note->descpos + offset);
d37c89e5
AM
1667}
1668
1669static bfd_boolean
1670ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1671{
1672 if (note->descsz != 136)
1673 return FALSE;
1674
228e534f 1675 elf_tdata (abfd)->core->pid
bc989cdc 1676 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1677 elf_tdata (abfd)->core->program
d37c89e5 1678 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1679 elf_tdata (abfd)->core->command
d37c89e5
AM
1680 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1681
1682 return TRUE;
1683}
1684
183e98be
AM
1685static char *
1686ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1687 ...)
1688{
1689 switch (note_type)
1690 {
1691 default:
1692 return NULL;
1693
1694 case NT_PRPSINFO:
1695 {
9ef6d1e3 1696 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1697 va_list ap;
1698
1699 va_start (ap, note_type);
75cd47ed 1700 memset (data, 0, sizeof (data));
183e98be 1701 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1702#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1703 DIAGNOSTIC_PUSH;
be3e27bb 1704 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1705 -Wstringop-truncation:
1706 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1707 */
95da9854
L
1708 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1709#endif
183e98be 1710 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1711#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1712 DIAGNOSTIC_POP;
fe75810f 1713#endif
183e98be
AM
1714 va_end (ap);
1715 return elfcore_write_note (abfd, buf, bufsiz,
1716 "CORE", note_type, data, sizeof (data));
1717 }
1718
1719 case NT_PRSTATUS:
1720 {
1721 char data[504];
1722 va_list ap;
1723 long pid;
1724 int cursig;
1725 const void *greg;
1726
1727 va_start (ap, note_type);
1728 memset (data, 0, 112);
1729 pid = va_arg (ap, long);
1730 bfd_put_32 (abfd, pid, data + 32);
1731 cursig = va_arg (ap, int);
1732 bfd_put_16 (abfd, cursig, data + 12);
1733 greg = va_arg (ap, const void *);
1734 memcpy (data + 112, greg, 384);
1735 memset (data + 496, 0, 8);
1736 va_end (ap);
1737 return elfcore_write_note (abfd, buf, bufsiz,
1738 "CORE", note_type, data, sizeof (data));
1739 }
1740 }
1741}
1742
5d35169e
AM
1743/* Add extra PPC sections. */
1744
2cdcc330 1745static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1746{
07d6d2b8
AM
1747 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1748 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1749 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1750 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1751 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1752 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1753 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1754};
1755
7c8fe5c4
AM
1756enum _ppc64_sec_type {
1757 sec_normal = 0,
1758 sec_opd = 1,
1759 sec_toc = 2
1760};
1761
f0abc2a1
AM
1762struct _ppc64_elf_section_data
1763{
1764 struct bfd_elf_section_data elf;
411e1bfb 1765
f0abc2a1
AM
1766 union
1767 {
51aecdc5
AM
1768 /* An array with one entry for each opd function descriptor,
1769 and some spares since opd entries may be either 16 or 24 bytes. */
1770#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
1771 struct _opd_sec_data
1772 {
1773 /* Points to the function code section for local opd entries. */
1774 asection **func_sec;
1775
1776 /* After editing .opd, adjust references to opd local syms. */
1777 long *adjust;
1778 } opd;
7c8fe5c4 1779
3a71aa26
AM
1780 /* An array for toc sections, indexed by offset/8. */
1781 struct _toc_sec_data
1782 {
1783 /* Specifies the relocation symbol index used at a given toc offset. */
1784 unsigned *symndx;
1785
1786 /* And the relocation addend. */
1787 bfd_vma *add;
1788 } toc;
7c8fe5c4
AM
1789 } u;
1790
1791 enum _ppc64_sec_type sec_type:2;
411e1bfb 1792
7c8fe5c4
AM
1793 /* Flag set when small branches are detected. Used to
1794 select suitable defaults for the stub group size. */
1795 unsigned int has_14bit_branch:1;
3e04d765
AM
1796
1797 /* Flag set when PLTCALL relocs are detected. */
1798 unsigned int has_pltcall:1;
066f4018
AM
1799
1800 /* Flag set when section has GOT relocations that can be optimised. */
1801 unsigned int has_gotrel:1;
f0abc2a1
AM
1802};
1803
1804#define ppc64_elf_section_data(sec) \
411e1bfb 1805 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
1806
1807static bfd_boolean
4ce794b7 1808ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1809{
f592407e
AM
1810 if (!sec->used_by_bfd)
1811 {
1812 struct _ppc64_elf_section_data *sdata;
1813 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1814
f592407e
AM
1815 sdata = bfd_zalloc (abfd, amt);
1816 if (sdata == NULL)
1817 return FALSE;
1818 sec->used_by_bfd = sdata;
1819 }
f0abc2a1
AM
1820
1821 return _bfd_elf_new_section_hook (abfd, sec);
1822}
4025353c 1823
74f0fb50 1824static struct _opd_sec_data *
4025353c
AM
1825get_opd_info (asection * sec)
1826{
1827 if (sec != NULL
1828 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 1829 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 1830 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
1831 return NULL;
1832}
90e3cdf2
JJ
1833\f
1834/* Parameters for the qsort hook. */
90e3cdf2 1835static bfd_boolean synthetic_relocatable;
cd285db5 1836static asection *synthetic_opd;
90e3cdf2 1837
699733f6 1838/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
1839
1840static int
1841compare_symbols (const void *ap, const void *bp)
1842{
2cdcc330
AM
1843 const asymbol *a = *(const asymbol **) ap;
1844 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 1845
699733f6
AM
1846 /* Section symbols first. */
1847 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 1848 return -1;
699733f6 1849 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
1850 return 1;
1851
699733f6 1852 /* then .opd symbols. */
cd285db5
AM
1853 if (synthetic_opd != NULL)
1854 {
1855 if (strcmp (a->section->name, ".opd") == 0
1856 && strcmp (b->section->name, ".opd") != 0)
1857 return -1;
1858 if (strcmp (a->section->name, ".opd") != 0
1859 && strcmp (b->section->name, ".opd") == 0)
1860 return 1;
1861 }
90e3cdf2 1862
699733f6 1863 /* then other code symbols. */
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
2cdcc330
AM
1870 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1871 != (SEC_CODE | SEC_ALLOC))
1872 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1873 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
1874 return 1;
1875
1876 if (synthetic_relocatable)
1877 {
1878 if (a->section->id < b->section->id)
1879 return -1;
1880
1881 if (a->section->id > b->section->id)
1882 return 1;
1883 }
1884
1885 if (a->value + a->section->vma < b->value + b->section->vma)
1886 return -1;
1887
1888 if (a->value + a->section->vma > b->value + b->section->vma)
1889 return 1;
1890
4d35a0aa
AM
1891 /* For syms with the same value, prefer strong dynamic global function
1892 syms over other syms. */
1893 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
1894 return -1;
1895
1896 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
1897 return 1;
1898
1899 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
1900 return -1;
1901
1902 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
1903 return 1;
1904
1905 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
1906 return -1;
1907
1908 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
1909 return 1;
1910
1911 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
1912 return -1;
1913
1914 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
1915 return 1;
1916
aaed6f5b 1917 return a > b;
90e3cdf2
JJ
1918}
1919
699733f6 1920/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 1921
699733f6 1922static asymbol *
7292b3ac 1923sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 1924{
699733f6 1925 long mid;
90e3cdf2 1926
7292b3ac 1927 if (id == (unsigned) -1)
699733f6
AM
1928 {
1929 while (lo < hi)
1930 {
1931 mid = (lo + hi) >> 1;
1932 if (syms[mid]->value + syms[mid]->section->vma < value)
1933 lo = mid + 1;
1934 else if (syms[mid]->value + syms[mid]->section->vma > value)
1935 hi = mid;
1936 else
1937 return syms[mid];
1938 }
1939 }
1940 else
1941 {
1942 while (lo < hi)
1943 {
1944 mid = (lo + hi) >> 1;
1945 if (syms[mid]->section->id < id)
1946 lo = mid + 1;
1947 else if (syms[mid]->section->id > id)
1948 hi = mid;
1949 else if (syms[mid]->value < value)
1950 lo = mid + 1;
1951 else if (syms[mid]->value > value)
1952 hi = mid;
1953 else
1954 return syms[mid];
1955 }
1956 }
1957 return NULL;
90e3cdf2
JJ
1958}
1959
468392fb
AM
1960static bfd_boolean
1961section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
1962{
1963 bfd_vma vma = *(bfd_vma *) ptr;
1964 return ((section->flags & SEC_ALLOC) != 0
1965 && section->vma <= vma
1966 && vma < section->vma + section->size);
1967}
1968
699733f6 1969/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
1970 entry syms. Also generate @plt symbols for the glink branch table.
1971 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
1972
1973static long
a7535cf3
AM
1974ppc64_elf_get_synthetic_symtab (bfd *abfd,
1975 long static_count, asymbol **static_syms,
1976 long dyn_count, asymbol **dyn_syms,
c9727e01 1977 asymbol **ret)
90e3cdf2
JJ
1978{
1979 asymbol *s;
0ccf57bd 1980 size_t i, j, count;
90e3cdf2 1981 char *names;
0ccf57bd 1982 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 1983 asection *opd = NULL;
90e3cdf2 1984 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 1985 asymbol **syms;
ee67d69a 1986 int abi = abiversion (abfd);
90e3cdf2
JJ
1987
1988 *ret = NULL;
1989
ee67d69a
AM
1990 if (abi < 2)
1991 {
1992 opd = bfd_get_section_by_name (abfd, ".opd");
1993 if (opd == NULL && abi == 1)
1994 return 0;
1995 }
90e3cdf2 1996
a5259595
AM
1997 syms = NULL;
1998 codesecsym = 0;
1999 codesecsymend = 0;
2000 secsymend = 0;
2001 opdsymend = 0;
2002 symcount = 0;
2003 if (opd != NULL)
c9727e01 2004 {
a5259595
AM
2005 symcount = static_count;
2006 if (!relocatable)
2007 symcount += dyn_count;
2008 if (symcount == 0)
2009 return 0;
c9727e01 2010
a5259595
AM
2011 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2012 if (syms == NULL)
2013 return -1;
90e3cdf2 2014
a5259595
AM
2015 if (!relocatable && static_count != 0 && dyn_count != 0)
2016 {
2017 /* Use both symbol tables. */
2018 memcpy (syms, static_syms, static_count * sizeof (*syms));
2019 memcpy (syms + static_count, dyn_syms,
2020 (dyn_count + 1) * sizeof (*syms));
2021 }
2022 else if (!relocatable && static_count == 0)
2023 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2024 else
2025 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2026
0ccf57bd
AM
2027 /* Trim uninteresting symbols. Interesting symbols are section,
2028 function, and notype symbols. */
2029 for (i = 0, j = 0; i < symcount; ++i)
2030 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2031 | BSF_RELC | BSF_SRELC)) == 0)
2032 syms[j++] = syms[i];
2033 symcount = j;
2034
a5259595
AM
2035 synthetic_relocatable = relocatable;
2036 synthetic_opd = opd;
2037 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2038
a5259595
AM
2039 if (!relocatable && symcount > 1)
2040 {
bfa5bd2a
PA
2041 /* Trim duplicate syms, since we may have merged the normal
2042 and dynamic symbols. Actually, we only care about syms
2043 that have different values, so trim any with the same
2044 value. Don't consider ifunc and ifunc resolver symbols
2045 duplicates however, because GDB wants to know whether a
2046 text symbol is an ifunc resolver. */
a5259595 2047 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2048 {
2049 const asymbol *s0 = syms[i - 1];
2050 const asymbol *s1 = syms[i];
2051
2052 if ((s0->value + s0->section->vma
2053 != s1->value + s1->section->vma)
2054 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2055 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2056 syms[j++] = syms[i];
2057 }
a5259595
AM
2058 symcount = j;
2059 }
699733f6 2060
a5259595
AM
2061 i = 0;
2062 /* Note that here and in compare_symbols we can't compare opd and
2063 sym->section directly. With separate debug info files, the
2064 symbols will be extracted from the debug file while abfd passed
2065 to this function is the real binary. */
0ccf57bd 2066 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2067 ++i;
2068 codesecsym = i;
2069
2070 for (; i < symcount; ++i)
2071 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2072 | SEC_THREAD_LOCAL))
2073 != (SEC_CODE | SEC_ALLOC))
2074 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2075 break;
2076 codesecsymend = i;
2077
2078 for (; i < symcount; ++i)
2079 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2080 break;
2081 secsymend = i;
2082
2083 for (; i < symcount; ++i)
2084 if (strcmp (syms[i]->section->name, ".opd") != 0)
2085 break;
2086 opdsymend = i;
2087
2088 for (; i < symcount; ++i)
2cdcc330
AM
2089 if (((syms[i]->section->flags
2090 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2091 != (SEC_CODE | SEC_ALLOC))
2092 break;
2093 symcount = i;
2094 }
c9727e01 2095 count = 0;
90e3cdf2 2096
699733f6 2097 if (relocatable)
90e3cdf2 2098 {
699733f6
AM
2099 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2100 arelent *r;
2101 size_t size;
0ccf57bd 2102 size_t relcount;
90e3cdf2 2103
468392fb
AM
2104 if (opdsymend == secsymend)
2105 goto done;
2106
699733f6 2107 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2108 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2109 if (relcount == 0)
c9727e01 2110 goto done;
90e3cdf2 2111
7356fed5
AM
2112 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2113 {
2114 count = -1;
2115 goto done;
2116 }
2117
699733f6 2118 size = 0;
595da8c5 2119 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2120 {
2121 asymbol *sym;
90e3cdf2 2122
595da8c5 2123 while (r < opd->relocation + relcount
699733f6
AM
2124 && r->address < syms[i]->value + opd->vma)
2125 ++r;
90e3cdf2 2126
595da8c5 2127 if (r == opd->relocation + relcount)
699733f6 2128 break;
90e3cdf2 2129
699733f6
AM
2130 if (r->address != syms[i]->value + opd->vma)
2131 continue;
90e3cdf2 2132
699733f6
AM
2133 if (r->howto->type != R_PPC64_ADDR64)
2134 continue;
90e3cdf2 2135
699733f6
AM
2136 sym = *r->sym_ptr_ptr;
2137 if (!sym_exists_at (syms, opdsymend, symcount,
2138 sym->section->id, sym->value + r->addend))
2139 {
2140 ++count;
2141 size += sizeof (asymbol);
2142 size += strlen (syms[i]->name) + 2;
2143 }
2144 }
90e3cdf2 2145
c4b0b099
AM
2146 if (size == 0)
2147 goto done;
699733f6
AM
2148 s = *ret = bfd_malloc (size);
2149 if (s == NULL)
2150 {
7356fed5 2151 count = -1;
c9727e01 2152 goto done;
699733f6 2153 }
90e3cdf2 2154
699733f6 2155 names = (char *) (s + count);
90e3cdf2 2156
595da8c5 2157 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2158 {
699733f6 2159 asymbol *sym;
90e3cdf2 2160
595da8c5 2161 while (r < opd->relocation + relcount
699733f6
AM
2162 && r->address < syms[i]->value + opd->vma)
2163 ++r;
90e3cdf2 2164
595da8c5 2165 if (r == opd->relocation + relcount)
699733f6
AM
2166 break;
2167
2168 if (r->address != syms[i]->value + opd->vma)
2169 continue;
2170
2171 if (r->howto->type != R_PPC64_ADDR64)
2172 continue;
90e3cdf2 2173
699733f6
AM
2174 sym = *r->sym_ptr_ptr;
2175 if (!sym_exists_at (syms, opdsymend, symcount,
2176 sym->section->id, sym->value + r->addend))
2177 {
2178 size_t len;
2179
2180 *s = *syms[i];
6ba2a415 2181 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2182 s->section = sym->section;
2183 s->value = sym->value + r->addend;
2184 s->name = names;
2185 *names++ = '.';
2186 len = strlen (syms[i]->name);
2187 memcpy (names, syms[i]->name, len + 1);
2188 names += len + 1;
6f610d07
UW
2189 /* Have udata.p point back to the original symbol this
2190 synthetic symbol was derived from. */
2191 s->udata.p = syms[i];
699733f6
AM
2192 s++;
2193 }
2194 }
2195 }
2196 else
90e3cdf2 2197 {
468392fb 2198 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 2199 bfd_byte *contents = NULL;
699733f6 2200 size_t size;
0ccf57bd 2201 size_t plt_count = 0;
468392fb
AM
2202 bfd_vma glink_vma = 0, resolv_vma = 0;
2203 asection *dynamic, *glink = NULL, *relplt = NULL;
2204 arelent *p;
90e3cdf2 2205
ee67d69a 2206 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2207 {
c4b0b099
AM
2208 free_contents_and_exit_err:
2209 count = -1;
ee67d69a 2210 free_contents_and_exit:
699733f6 2211 if (contents)
ee67d69a 2212 free (contents);
c9727e01 2213 goto done;
699733f6 2214 }
90e3cdf2 2215
699733f6
AM
2216 size = 0;
2217 for (i = secsymend; i < opdsymend; ++i)
2218 {
2219 bfd_vma ent;
90e3cdf2 2220
5ef11c02
AM
2221 /* Ignore bogus symbols. */
2222 if (syms[i]->value > opd->size - 8)
2223 continue;
2224
699733f6
AM
2225 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2226 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2227 {
2228 ++count;
2229 size += sizeof (asymbol);
2230 size += strlen (syms[i]->name) + 2;
2231 }
2232 }
90e3cdf2 2233
468392fb 2234 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2235 if (dyn_count != 0
2236 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2237 {
2238 bfd_byte *dynbuf, *extdyn, *extdynend;
2239 size_t extdynsize;
2240 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2241
2242 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2243 goto free_contents_and_exit_err;
468392fb
AM
2244
2245 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2246 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2247
2248 extdyn = dynbuf;
2249 extdynend = extdyn + dynamic->size;
2250 for (; extdyn < extdynend; extdyn += extdynsize)
2251 {
2252 Elf_Internal_Dyn dyn;
2253 (*swap_dyn_in) (abfd, extdyn, &dyn);
2254
2255 if (dyn.d_tag == DT_NULL)
2256 break;
2257
2258 if (dyn.d_tag == DT_PPC64_GLINK)
2259 {
9e390558
AM
2260 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2261 See comment in ppc64_elf_finish_dynamic_sections. */
2262 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2263 /* The .glink section usually does not survive the final
2264 link; search for the section (usually .text) where the
2265 glink stubs now reside. */
2266 glink = bfd_sections_find_if (abfd, section_covers_vma,
2267 &glink_vma);
2268 break;
2269 }
2270 }
2271
2272 free (dynbuf);
2273 }
2274
2275 if (glink != NULL)
2276 {
2277 /* Determine __glink trampoline by reading the relative branch
2278 from the first glink stub. */
2279 bfd_byte buf[4];
b9e5796b
AM
2280 unsigned int off = 0;
2281
2282 while (bfd_get_section_contents (abfd, glink, buf,
2283 glink_vma + off - glink->vma, 4))
468392fb
AM
2284 {
2285 unsigned int insn = bfd_get_32 (abfd, buf);
2286 insn ^= B_DOT;
2287 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2288 {
2cdcc330
AM
2289 resolv_vma
2290 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2291 break;
2292 }
2293 off += 4;
2294 if (off > 4)
2295 break;
468392fb
AM
2296 }
2297
2298 if (resolv_vma)
2299 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2300
066ee829
AM
2301 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2302 if (relplt != NULL)
2303 {
2304 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2cdcc330 2305 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 2306 goto free_contents_and_exit_err;
68ffbac6 2307
066ee829
AM
2308 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2309 size += plt_count * sizeof (asymbol);
468392fb 2310
066ee829
AM
2311 p = relplt->relocation;
2312 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2313 {
2314 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2315 if (p->addend != 0)
2316 size += sizeof ("+0x") - 1 + 16;
2317 }
066ee829 2318 }
468392fb
AM
2319 }
2320
c4b0b099
AM
2321 if (size == 0)
2322 goto free_contents_and_exit;
699733f6
AM
2323 s = *ret = bfd_malloc (size);
2324 if (s == NULL)
c4b0b099 2325 goto free_contents_and_exit_err;
90e3cdf2 2326
468392fb 2327 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2328
699733f6 2329 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2330 {
699733f6 2331 bfd_vma ent;
90e3cdf2 2332
5ef11c02
AM
2333 if (syms[i]->value > opd->size - 8)
2334 continue;
2335
699733f6
AM
2336 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2337 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2338 {
0ccf57bd 2339 size_t lo, hi;
699733f6 2340 size_t len;
c9727e01 2341 asection *sec = abfd->sections;
90e3cdf2 2342
699733f6
AM
2343 *s = *syms[i];
2344 lo = codesecsym;
2345 hi = codesecsymend;
2346 while (lo < hi)
2347 {
0ccf57bd 2348 size_t mid = (lo + hi) >> 1;
699733f6
AM
2349 if (syms[mid]->section->vma < ent)
2350 lo = mid + 1;
2351 else if (syms[mid]->section->vma > ent)
2352 hi = mid;
2353 else
c9727e01
AM
2354 {
2355 sec = syms[mid]->section;
2356 break;
2357 }
699733f6
AM
2358 }
2359
c9727e01 2360 if (lo >= hi && lo > codesecsym)
699733f6 2361 sec = syms[lo - 1]->section;
699733f6
AM
2362
2363 for (; sec != NULL; sec = sec->next)
2364 {
2365 if (sec->vma > ent)
2366 break;
63524580
JK
2367 /* SEC_LOAD may not be set if SEC is from a separate debug
2368 info file. */
2369 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2370 break;
2371 if ((sec->flags & SEC_CODE) != 0)
2372 s->section = sec;
2373 }
6ba2a415 2374 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2375 s->value = ent - s->section->vma;
2376 s->name = names;
2377 *names++ = '.';
2378 len = strlen (syms[i]->name);
2379 memcpy (names, syms[i]->name, len + 1);
2380 names += len + 1;
6f610d07
UW
2381 /* Have udata.p point back to the original symbol this
2382 synthetic symbol was derived from. */
2383 s->udata.p = syms[i];
699733f6 2384 s++;
90e3cdf2 2385 }
90e3cdf2 2386 }
699733f6 2387 free (contents);
468392fb
AM
2388
2389 if (glink != NULL && relplt != NULL)
2390 {
2391 if (resolv_vma)
2392 {
2393 /* Add a symbol for the main glink trampoline. */
86a4952b 2394 memset (s, 0, sizeof *s);
468392fb 2395 s->the_bfd = abfd;
6ba2a415 2396 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2397 s->section = glink;
2398 s->value = resolv_vma - glink->vma;
2399 s->name = names;
2cdcc330
AM
2400 memcpy (names, "__glink_PLTresolve",
2401 sizeof ("__glink_PLTresolve"));
468392fb
AM
2402 names += sizeof ("__glink_PLTresolve");
2403 s++;
2404 count++;
2405 }
2406
2407 /* FIXME: It would be very much nicer to put sym@plt on the
2408 stub rather than on the glink branch table entry. The
2409 objdump disassembler would then use a sensible symbol
2410 name on plt calls. The difficulty in doing so is
2411 a) finding the stubs, and,
2412 b) matching stubs against plt entries, and,
2413 c) there can be multiple stubs for a given plt entry.
2414
2415 Solving (a) could be done by code scanning, but older
2416 ppc64 binaries used different stubs to current code.
2417 (b) is the tricky one since you need to known the toc
2418 pointer for at least one function that uses a pic stub to
2419 be able to calculate the plt address referenced.
2420 (c) means gdb would need to set multiple breakpoints (or
2421 find the glink branch itself) when setting breakpoints
2422 for pending shared library loads. */
2423 p = relplt->relocation;
2424 for (i = 0; i < plt_count; i++, p++)
2425 {
2426 size_t len;
2427
2428 *s = **p->sym_ptr_ptr;
2429 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2430 we are defining a symbol, ensure one of them is set. */
2431 if ((s->flags & BSF_LOCAL) == 0)
2432 s->flags |= BSF_GLOBAL;
6ba2a415 2433 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2434 s->section = glink;
2435 s->value = glink_vma - glink->vma;
2436 s->name = names;
2437 s->udata.p = NULL;
2438 len = strlen ((*p->sym_ptr_ptr)->name);
2439 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2440 names += len;
e054468f
AM
2441 if (p->addend != 0)
2442 {
2443 memcpy (names, "+0x", sizeof ("+0x") - 1);
2444 names += sizeof ("+0x") - 1;
2445 bfd_sprintf_vma (abfd, names, p->addend);
2446 names += strlen (names);
2447 }
468392fb
AM
2448 memcpy (names, "@plt", sizeof ("@plt"));
2449 names += sizeof ("@plt");
2450 s++;
b9e5796b
AM
2451 if (abi < 2)
2452 {
2453 glink_vma += 8;
2454 if (i >= 0x8000)
2455 glink_vma += 4;
2456 }
2457 else
468392fb
AM
2458 glink_vma += 4;
2459 }
2460 count += plt_count;
2461 }
90e3cdf2
JJ
2462 }
2463
c9727e01 2464 done:
a7535cf3 2465 free (syms);
90e3cdf2
JJ
2466 return count;
2467}
5bd4f169 2468\f
65f38f15
AM
2469/* The following functions are specific to the ELF linker, while
2470 functions above are used generally. Those named ppc64_elf_* are
2471 called by the main ELF linker code. They appear in this file more
2472 or less in the order in which they are called. eg.
2473 ppc64_elf_check_relocs is called early in the link process,
2474 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2475 called.
2476
2477 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2478 functions have both a function code symbol and a function descriptor
2479 symbol. A call to foo in a relocatable object file looks like:
2480
2481 . .text
2482 . x:
2483 . bl .foo
2484 . nop
2485
2486 The function definition in another object file might be:
2487
2488 . .section .opd
2489 . foo: .quad .foo
2490 . .quad .TOC.@tocbase
2491 . .quad 0
2492 .
2493 . .text
2494 . .foo: blr
2495
2496 When the linker resolves the call during a static link, the branch
2497 unsurprisingly just goes to .foo and the .opd information is unused.
2498 If the function definition is in a shared library, things are a little
2499 different: The call goes via a plt call stub, the opd information gets
2500 copied to the plt, and the linker patches the nop.
2501
2502 . x:
2503 . bl .foo_stub
2504 . ld 2,40(1)
2505 .
2506 .
2507 . .foo_stub:
71a39c98
AM
2508 . std 2,40(1) # in practice, the call stub
2509 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2510 . addi 11,11,Lfoo@toc@l # this is the general idea
2511 . ld 12,0(11)
2512 . ld 2,8(11)
2513 . mtctr 12
2514 . ld 11,16(11)
e86ce104
AM
2515 . bctr
2516 .
2517 . .section .plt
2518 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2519
2520 The "reloc ()" notation is supposed to indicate that the linker emits
2521 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2522 copying.
2523
2524 What are the difficulties here? Well, firstly, the relocations
2525 examined by the linker in check_relocs are against the function code
2526 sym .foo, while the dynamic relocation in the plt is emitted against
2527 the function descriptor symbol, foo. Somewhere along the line, we need
2528 to carefully copy dynamic link information from one symbol to the other.
2529 Secondly, the generic part of the elf linker will make .foo a dynamic
2530 symbol as is normal for most other backends. We need foo dynamic
2531 instead, at least for an application final link. However, when
2532 creating a shared library containing foo, we need to have both symbols
2533 dynamic so that references to .foo are satisfied during the early
2534 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2535 definition from some other object, eg. a static library.
2536
2537 Update: As of August 2004, we support a new convention. Function
2538 calls may use the function descriptor symbol, ie. "bl foo". This
2539 behaves exactly as "bl .foo". */
65f38f15 2540
7c8bbca5
AM
2541/* Of those relocs that might be copied as dynamic relocs, this
2542 function selects those that must be copied when linking a shared
2543 library or PIE, even when the symbol is local. */
65f38f15 2544
1d483afe
AM
2545static int
2546must_be_dyn_reloc (struct bfd_link_info *info,
2547 enum elf_ppc64_reloc_type r_type)
2548{
2549 switch (r_type)
2550 {
2551 default:
7c8bbca5
AM
2552 /* Only relative relocs can be resolved when the object load
2553 address isn't fixed. DTPREL64 is excluded because the
2554 dynamic linker needs to differentiate global dynamic from
2555 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2556 return 1;
2557
2558 case R_PPC64_REL32:
2559 case R_PPC64_REL64:
2560 case R_PPC64_REL30:
2561 return 0;
2562
2563 case R_PPC64_TPREL16:
2564 case R_PPC64_TPREL16_LO:
2565 case R_PPC64_TPREL16_HI:
2566 case R_PPC64_TPREL16_HA:
2567 case R_PPC64_TPREL16_DS:
2568 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2569 case R_PPC64_TPREL16_HIGH:
2570 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2571 case R_PPC64_TPREL16_HIGHER:
2572 case R_PPC64_TPREL16_HIGHERA:
2573 case R_PPC64_TPREL16_HIGHEST:
2574 case R_PPC64_TPREL16_HIGHESTA:
2575 case R_PPC64_TPREL64:
7c8bbca5
AM
2576 /* These relocations are relative but in a shared library the
2577 linker doesn't know the thread pointer base. */
2578 return bfd_link_dll (info);
1d483afe
AM
2579 }
2580}
65f38f15 2581
f4656909
AM
2582/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2583 copying dynamic variables from a shared lib into an app's dynbss
2584 section, and instead use a dynamic relocation to point into the
5d35169e
AM
2585 shared lib. With code that gcc generates, it's vital that this be
2586 enabled; In the PowerPC64 ABI, the address of a function is actually
2587 the address of a function descriptor, which resides in the .opd
2588 section. gcc uses the descriptor directly rather than going via the
2589 GOT as some other ABI's do, which means that initialized function
2590 pointers must reference the descriptor. Thus, a function pointer
2591 initialized to the address of a function in a shared library will
2592 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 2593 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
2594 presents a problem as a plt entry for that function is also
2595 initialized from the function descriptor symbol and the copy reloc
2596 may not be initialized first. */
a23b6845 2597#define ELIMINATE_COPY_RELOCS 1
f4656909 2598
721956f4 2599/* Section name for stubs is the associated section name plus this
29942be8
NC
2600 string. */
2601#define STUB_SUFFIX ".stub"
721956f4
AM
2602
2603/* Linker stubs.
2604 ppc_stub_long_branch:
2605 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2606 destination, but a 24 bit branch in a stub section will reach.
2607 . b dest
2608
2609 ppc_stub_plt_branch:
2610 Similar to the above, but a 24 bit branch in the stub section won't
2611 reach its destination.
71a39c98
AM
2612 . addis %r11,%r2,xxx@toc@ha
2613 . ld %r12,xxx@toc@l(%r11)
2614 . mtctr %r12
721956f4
AM
2615 . bctr
2616
2617 ppc_stub_plt_call:
2c66dc6c
AM
2618 Used to call a function in a shared library. If it so happens that
2619 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2620 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 2621 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
2622 . addis %r11,%r2,xxx@toc@ha
2623 . ld %r12,xxx+0@toc@l(%r11)
2624 . mtctr %r12
2625 . ld %r2,xxx+8@toc@l(%r11)
2626 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2627 . bctr
ad8e1ba5
AM
2628
2629 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2630 code to adjust the value and save r2 to support multiple toc sections.
2631 A ppc_stub_long_branch with an r2 offset looks like:
2632 . std %r2,40(%r1)
2633 . addis %r2,%r2,off@ha
2634 . addi %r2,%r2,off@l
2635 . b dest
2636
2637 A ppc_stub_plt_branch with an r2 offset looks like:
2638 . std %r2,40(%r1)
71a39c98
AM
2639 . addis %r11,%r2,xxx@toc@ha
2640 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
2641 . addis %r2,%r2,off@ha
2642 . addi %r2,%r2,off@l
71a39c98 2643 . mtctr %r12
ad8e1ba5 2644 . bctr
ac2df442 2645
05d0e962
AM
2646 All of the above stubs are shown as their ELFv1 variants. ELFv2
2647 variants exist too, simpler for plt calls since a new toc pointer
2648 and static chain are not loaded by the stub. In addition, ELFv2
2649 has some more complex stubs to handle calls marked with NOTOC
2650 relocs from functions where r2 is not a valid toc pointer. These
2651 come in two flavours, the ones shown below, and _both variants that
2652 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2653 one call is from a function where r2 is used as the toc pointer but
2654 needs a toc adjusting stub for small-model multi-toc, and another
2655 call is from a function where r2 is not valid.
2656 ppc_stub_long_branch_notoc:
2657 . mflr %r12
2658 . bcl 20,31,1f
2659 . 1:
2660 . mflr %r11
2661 . mtlr %r12
2cdcc330
AM
2662 . addis %r12,%r11,dest-1b@ha
2663 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2664 . b dest
2665
2666 ppc_stub_plt_branch_notoc:
2667 . mflr %r12
2668 . bcl 20,31,1f
2669 . 1:
2670 . mflr %r11
2671 . mtlr %r12
2672 . lis %r12,xxx-1b@highest
f891966f 2673 . ori %r12,%r12,xxx-1b@higher
05d0e962 2674 . sldi %r12,%r12,32
f891966f 2675 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2676 . ori %r12,%r12,xxx-1b@l
2677 . add %r12,%r11,%r12
2678 . mtctr %r12
2679 . bctr
2680
2681 ppc_stub_plt_call_notoc:
2682 . mflr %r12
2683 . bcl 20,31,1f
2684 . 1:
2685 . mflr %r11
2686 . mtlr %r12
2687 . lis %r12,xxx-1b@highest
f891966f 2688 . ori %r12,%r12,xxx-1b@higher
05d0e962 2689 . sldi %r12,%r12,32
f891966f 2690 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2691 . ori %r12,%r12,xxx-1b@l
2692 . ldx %r12,%r11,%r12
2693 . mtctr %r12
2694 . bctr
2695
2696 In cases where the high instructions would add zero, they are
2697 omitted and following instructions modified in some cases.
2698
2699 For a given stub group (a set of sections all using the same toc
2700 pointer value) there will be just one stub type used for any
2701 particular function symbol. For example, if printf is called from
2702 code with the tocsave optimization (ie. r2 saved in function
2703 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2704 and from other code without the tocsave optimization requiring a
2705 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2706 type will be created. Calls with the tocsave optimization will
2707 enter this stub after the instruction saving r2. A similar
2708 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2709 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2710 to call an external function like printf. If other calls to printf
2711 require a ppc_stub_plt_call linkage stub then a single
2712 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2713 call. If other calls to printf require a ppc_stub_plt_call_r2save
2714 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2715 be created and calls not requiring r2 to be saved will enter the
2716 stub after the r2 save instruction. There is an analogous
2717 hierarchy of long branch and plt branch stubs for local call
2718 linkage. */
721956f4 2719
2cdcc330
AM
2720enum ppc_stub_type
2721{
721956f4
AM
2722 ppc_stub_none,
2723 ppc_stub_long_branch,
ad8e1ba5 2724 ppc_stub_long_branch_r2off,
05d0e962
AM
2725 ppc_stub_long_branch_notoc,
2726 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 2727 ppc_stub_plt_branch,
ad8e1ba5 2728 ppc_stub_plt_branch_r2off,
05d0e962
AM
2729 ppc_stub_plt_branch_notoc,
2730 ppc_stub_plt_branch_both,
794e51c0 2731 ppc_stub_plt_call,
7341d5e2 2732 ppc_stub_plt_call_r2save,
05d0e962
AM
2733 ppc_stub_plt_call_notoc,
2734 ppc_stub_plt_call_both,
a4b6fadd
AM
2735 ppc_stub_global_entry,
2736 ppc_stub_save_res
721956f4
AM
2737};
2738
6f20ed8a
AM
2739/* Information on stub grouping. */
2740struct map_stub
2741{
2742 /* The stub section. */
2743 asection *stub_sec;
2744 /* This is the section to which stubs in the group will be attached. */
2745 asection *link_sec;
a4b6fadd
AM
2746 /* Next group. */
2747 struct map_stub *next;
2748 /* Whether to emit a copy of register save/restore functions in this
2749 group. */
2750 int needs_save_res;
df136d64
AM
2751 /* Current offset within stubs after the insn restoring lr in a
2752 _notoc or _both stub using bcl for pc-relative addressing, or
2753 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2754 unsigned int lr_restore;
2755 /* Accumulated size of EH info emitted to describe return address
2756 if stubs modify lr. Does not include 17 byte FDE header. */
2757 unsigned int eh_size;
2758 /* Offset in glink_eh_frame to the start of EH info for this group. */
2759 unsigned int eh_base;
6f20ed8a
AM
2760};
2761
2cdcc330
AM
2762struct ppc_stub_hash_entry
2763{
721956f4
AM
2764 /* Base hash table entry structure. */
2765 struct bfd_hash_entry root;
2766
ad8e1ba5
AM
2767 enum ppc_stub_type stub_type;
2768
6f20ed8a
AM
2769 /* Group information. */
2770 struct map_stub *group;
721956f4
AM
2771
2772 /* Offset within stub_sec of the beginning of this stub. */
2773 bfd_vma stub_offset;
2774
2775 /* Given the symbol's value and its section we can determine its final
2776 value when building the stubs (so the stub knows where to jump. */
2777 bfd_vma target_value;
2778 asection *target_section;
2779
721956f4
AM
2780 /* The symbol table entry, if any, that this was derived from. */
2781 struct ppc_link_hash_entry *h;
e054468f 2782 struct plt_entry *plt_ent;
721956f4 2783
2d7ad24e
AM
2784 /* Symbol type. */
2785 unsigned char symtype;
2786
6911b7dc
AM
2787 /* Symbol st_other. */
2788 unsigned char other;
721956f4
AM
2789};
2790
2cdcc330
AM
2791struct ppc_branch_hash_entry
2792{
721956f4
AM
2793 /* Base hash table entry structure. */
2794 struct bfd_hash_entry root;
2795
c456f082 2796 /* Offset within branch lookup table. */
721956f4
AM
2797 unsigned int offset;
2798
2799 /* Generation marker. */
2800 unsigned int iter;
2801};
65f38f15 2802
19e08130
AM
2803/* Used to track dynamic relocations for local symbols. */
2804struct ppc_dyn_relocs
2805{
2806 struct ppc_dyn_relocs *next;
2807
2808 /* The input section of the reloc. */
2809 asection *sec;
2810
2811 /* Total number of relocs copied for the input section. */
2812 unsigned int count : 31;
2813
2814 /* Whether this entry is for STT_GNU_IFUNC symbols. */
2815 unsigned int ifunc : 1;
2816};
2817
65f38f15
AM
2818struct ppc_link_hash_entry
2819{
2820 struct elf_link_hash_entry elf;
2821
2cdcc330
AM
2822 union
2823 {
b3fac117
AM
2824 /* A pointer to the most recently used stub hash entry against this
2825 symbol. */
2826 struct ppc_stub_hash_entry *stub_cache;
2827
2828 /* A pointer to the next symbol starting with a '.' */
2829 struct ppc_link_hash_entry *next_dot_sym;
2830 } u;
721956f4 2831
65f38f15 2832 /* Track dynamic relocs copied for this symbol. */
6061a67d 2833 struct elf_dyn_relocs *dyn_relocs;
e86ce104 2834
721956f4 2835 /* Link between function code and descriptor symbols. */
34814b9f 2836 struct ppc_link_hash_entry *oh;
721956f4 2837
e86ce104
AM
2838 /* Flag function code and descriptor symbols. */
2839 unsigned int is_func:1;
2840 unsigned int is_func_descriptor:1;
908b32fc 2841 unsigned int fake:1;
411e1bfb 2842
c5614fa4
AM
2843 /* Whether global opd/toc sym has been adjusted or not.
2844 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
2845 should be set for all globals defined in any opd/toc section. */
754021d0
AM
2846 unsigned int adjust_done:1;
2847
a4b6fadd
AM
2848 /* Set if this is an out-of-line register save/restore function,
2849 with non-standard calling convention. */
2850 unsigned int save_res:1;
2851
8b5f1ed8
AM
2852 /* Set if a duplicate symbol with non-zero localentry is detected,
2853 even when the duplicate symbol does not provide a definition. */
2854 unsigned int non_zero_localentry:1;
2855
411e1bfb 2856 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
2857 Bits are or'd into the mask as the corresponding relocs are
2858 encountered during check_relocs, with TLS_TLS being set when any
2859 of the other TLS bits are set. tls_optimize clears bits when
2860 optimizing to indicate the corresponding GOT entry type is not
2861 needed. If set, TLS_TLS is never cleared. tls_optimize may also
2862 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
2863 separate flag rather than setting TPREL just for convenience in
2864 distinguishing the two cases.
2865 These flags are also kept for local symbols. */
2866#define TLS_TLS 1 /* Any TLS reloc. */
2867#define TLS_GD 2 /* GD reloc. */
2868#define TLS_LD 4 /* LD reloc. */
2869#define TLS_TPREL 8 /* TPREL reloc, => IE. */
2870#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
2871#define TLS_MARK 32 /* __tls_get_addr call marked. */
e7b938ca 2872#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
37da22e5 2873#define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
f961d9dd 2874 unsigned char tls_mask;
37da22e5
AM
2875
2876 /* The above field is also used to mark function symbols. In which
2877 case TLS_TLS will be 0. */
2878#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 2879#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 2880#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
2881};
2882
2883/* ppc64 ELF linker hash table. */
2884
2885struct ppc_link_hash_table
2886{
2887 struct elf_link_hash_table elf;
2888
721956f4
AM
2889 /* The stub hash table. */
2890 struct bfd_hash_table stub_hash_table;
2891
2892 /* Another hash table for plt_branch stubs. */
2893 struct bfd_hash_table branch_hash_table;
2894
3b421ab3
AM
2895 /* Hash table for function prologue tocsave. */
2896 htab_t tocsave_htab;
2897
e7d1c40c
AM
2898 /* Various options and other info passed from the linker. */
2899 struct ppc64_elf_params *params;
721956f4 2900
6f20ed8a
AM
2901 /* The size of sec_info below. */
2902 unsigned int sec_info_arr_size;
2903
2904 /* Per-section array of extra section info. Done this way rather
2905 than as part of ppc64_elf_section_data so we have the info for
2906 non-ppc64 sections. */
2907 struct
2908 {
2909 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 2910 bfd_vma toc_off;
6f20ed8a
AM
2911
2912 union
2913 {
2914 /* The section group that this section belongs to. */
2915 struct map_stub *group;
2916 /* A temp section list pointer. */
2917 asection *list;
2918 } u;
2919 } *sec_info;
721956f4 2920
a4b6fadd
AM
2921 /* Linked list of groups. */
2922 struct map_stub *group;
2923
ad8e1ba5
AM
2924 /* Temp used when calculating TOC pointers. */
2925 bfd_vma toc_curr;
bf102f86
AM
2926 bfd *toc_bfd;
2927 asection *toc_first_sec;
ad8e1ba5 2928
b3fac117
AM
2929 /* Used when adding symbols. */
2930 struct ppc_link_hash_entry *dot_syms;
2931
33e44f2e 2932 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 2933 asection *glink;
9e390558 2934 asection *global_entry;
82bd7b59 2935 asection *sfpr;
2d7ad24e
AM
2936 asection *pltlocal;
2937 asection *relpltlocal;
4ce794b7
AM
2938 asection *brlt;
2939 asection *relbrlt;
58d180e8 2940 asection *glink_eh_frame;
ec338859 2941
8387904d
AM
2942 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
2943 struct ppc_link_hash_entry *tls_get_addr;
2944 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 2945
927be08e
AM
2946 /* The size of reliplt used by got entry relocs. */
2947 bfd_size_type got_reli_size;
2948
9b5ecbd0 2949 /* Statistics. */
7341d5e2 2950 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 2951
ee75fd95
AM
2952 /* Number of stubs against global syms. */
2953 unsigned long stub_globals;
2954
ee67d69a
AM
2955 /* Set if we're linking code with function descriptors. */
2956 unsigned int opd_abi:1;
2957
4c52953f 2958 /* Support for multiple toc sections. */
33c0ec9d 2959 unsigned int do_multi_toc:1;
4c52953f 2960 unsigned int multi_toc_needed:1;
927be08e 2961 unsigned int second_toc_pass:1;
67f0cbdb 2962 unsigned int do_toc_opt:1;
4c52953f 2963
9a23f96e
AM
2964 /* Set if tls optimization is enabled. */
2965 unsigned int do_tls_opt:1;
2966
3e04d765
AM
2967 /* Set if inline plt calls should be converted to direct calls. */
2968 unsigned int can_convert_all_inline_plt:1;
2969
5d1634d7 2970 /* Set on error. */
99877b66 2971 unsigned int stub_error:1;
721956f4 2972
8c5b4e52
AM
2973 /* Whether func_desc_adjust needs to be run over symbols. */
2974 unsigned int need_func_desc_adj:1;
721956f4 2975
82e66161
AM
2976 /* Whether there exist local gnu indirect function resolvers,
2977 referenced by dynamic relocations. */
2978 unsigned int local_ifunc_resolver:1;
2979 unsigned int maybe_local_ifunc_resolver:1;
2980
f378ab09
AM
2981 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
2982 unsigned int has_plt_localentry0:1;
2983
721956f4
AM
2984 /* Incremented every time we size stubs. */
2985 unsigned int stub_iteration;
5d1634d7 2986
87d72d41
AM
2987 /* Small local sym cache. */
2988 struct sym_cache sym_cache;
65f38f15
AM
2989};
2990
4c52953f
AM
2991/* Rename some of the generic section flags to better document how they
2992 are used here. */
b0dddeec
AM
2993
2994/* Nonzero if this section has TLS related relocations. */
2995#define has_tls_reloc sec_flg0
2996
23cedd1d 2997/* Nonzero if this section has an old-style call to __tls_get_addr. */
b0dddeec
AM
2998#define has_tls_get_addr_call sec_flg1
2999
3000/* Nonzero if this section has any toc or got relocs. */
3001#define has_toc_reloc sec_flg2
3002
3003/* Nonzero if this section has a call to another section that uses
3004 the toc or got. */
d77c8a4b 3005#define makes_toc_func_call sec_flg3
b0dddeec
AM
3006
3007/* Recursion protection when determining above flag. */
d77c8a4b 3008#define call_check_in_progress sec_flg4
70cc837d 3009#define call_check_done sec_flg5
4c52953f 3010
65f38f15
AM
3011/* Get the ppc64 ELF linker hash table from a link_info structure. */
3012
3013#define ppc_hash_table(p) \
4dfe6ac6
NC
3014 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3015 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3016
721956f4
AM
3017#define ppc_stub_hash_lookup(table, string, create, copy) \
3018 ((struct ppc_stub_hash_entry *) \
3019 bfd_hash_lookup ((table), (string), (create), (copy)))
3020
3021#define ppc_branch_hash_lookup(table, string, create, copy) \
3022 ((struct ppc_branch_hash_entry *) \
3023 bfd_hash_lookup ((table), (string), (create), (copy)))
3024
3025/* Create an entry in the stub hash table. */
3026
3027static struct bfd_hash_entry *
4ce794b7
AM
3028stub_hash_newfunc (struct bfd_hash_entry *entry,
3029 struct bfd_hash_table *table,
3030 const char *string)
721956f4
AM
3031{
3032 /* Allocate the structure if it has not already been allocated by a
3033 subclass. */
3034 if (entry == NULL)
3035 {
3036 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3037 if (entry == NULL)
3038 return entry;
3039 }
3040
3041 /* Call the allocation method of the superclass. */
3042 entry = bfd_hash_newfunc (entry, table, string);
3043 if (entry != NULL)
3044 {
3045 struct ppc_stub_hash_entry *eh;
3046
3047 /* Initialize the local fields. */
3048 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3049 eh->stub_type = ppc_stub_none;
6f20ed8a 3050 eh->group = NULL;
721956f4
AM
3051 eh->stub_offset = 0;
3052 eh->target_value = 0;
3053 eh->target_section = NULL;
721956f4 3054 eh->h = NULL;
6911b7dc 3055 eh->plt_ent = NULL;
6911b7dc 3056 eh->other = 0;
721956f4
AM
3057 }
3058
3059 return entry;
3060}
3061
3062/* Create an entry in the branch hash table. */
3063
3064static struct bfd_hash_entry *
4ce794b7
AM
3065branch_hash_newfunc (struct bfd_hash_entry *entry,
3066 struct bfd_hash_table *table,
3067 const char *string)
721956f4
AM
3068{
3069 /* Allocate the structure if it has not already been allocated by a
3070 subclass. */
3071 if (entry == NULL)
3072 {
3073 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3074 if (entry == NULL)
3075 return entry;
3076 }
3077
3078 /* Call the allocation method of the superclass. */
3079 entry = bfd_hash_newfunc (entry, table, string);
3080 if (entry != NULL)
3081 {
3082 struct ppc_branch_hash_entry *eh;
3083
3084 /* Initialize the local fields. */
3085 eh = (struct ppc_branch_hash_entry *) entry;
3086 eh->offset = 0;
3087 eh->iter = 0;
3088 }
3089
3090 return entry;
3091}
3092
65f38f15
AM
3093/* Create an entry in a ppc64 ELF linker hash table. */
3094
3095static struct bfd_hash_entry *
4ce794b7
AM
3096link_hash_newfunc (struct bfd_hash_entry *entry,
3097 struct bfd_hash_table *table,
3098 const char *string)
65f38f15
AM
3099{
3100 /* Allocate the structure if it has not already been allocated by a
3101 subclass. */
3102 if (entry == NULL)
3103 {
3104 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3105 if (entry == NULL)
3106 return entry;
3107 }
3108
3109 /* Call the allocation method of the superclass. */
3110 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3111 if (entry != NULL)
3112 {
3113 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3114
b3fac117 3115 memset (&eh->u.stub_cache, 0,
908b32fc 3116 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3117 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3118
3119 /* When making function calls, old ABI code references function entry
3120 points (dot symbols), while new ABI code references the function
3121 descriptor symbol. We need to make any combination of reference and
3122 definition work together, without breaking archive linking.
3123
3124 For a defined function "foo" and an undefined call to "bar":
3125 An old object defines "foo" and ".foo", references ".bar" (possibly
3126 "bar" too).
3127 A new object defines "foo" and references "bar".
3128
3129 A new object thus has no problem with its undefined symbols being
3130 satisfied by definitions in an old object. On the other hand, the
3131 old object won't have ".bar" satisfied by a new object.
3132
3133 Keep a list of newly added dot-symbols. */
3134
3135 if (string[0] == '.')
3136 {
3137 struct ppc_link_hash_table *htab;
3138
3139 htab = (struct ppc_link_hash_table *) table;
3140 eh->u.next_dot_sym = htab->dot_syms;
3141 htab->dot_syms = eh;
3142 }
65f38f15
AM
3143 }
3144
3145 return entry;
3146}
3147
2cdcc330
AM
3148struct tocsave_entry
3149{
3b421ab3
AM
3150 asection *sec;
3151 bfd_vma offset;
3152};
3153
3154static hashval_t
3155tocsave_htab_hash (const void *p)
3156{
3157 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3158 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3159}
3160
3161static int
3162tocsave_htab_eq (const void *p1, const void *p2)
3163{
3164 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3165 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3166 return e1->sec == e2->sec && e1->offset == e2->offset;
3167}
3168
68faa637
AM
3169/* Destroy a ppc64 ELF linker hash table. */
3170
3171static void
d495ab0d 3172ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3173{
d495ab0d 3174 struct ppc_link_hash_table *htab;
68faa637 3175
d495ab0d 3176 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3177 if (htab->tocsave_htab)
3178 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3179 bfd_hash_table_free (&htab->branch_hash_table);
3180 bfd_hash_table_free (&htab->stub_hash_table);
3181 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3182}
3183
65f38f15
AM
3184/* Create a ppc64 ELF linker hash table. */
3185
3186static struct bfd_link_hash_table *
4ce794b7 3187ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3188{
3189 struct ppc_link_hash_table *htab;
3190 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3191
4ce794b7 3192 htab = bfd_zmalloc (amt);
65f38f15
AM
3193 if (htab == NULL)
3194 return NULL;
3195
66eb6687 3196 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3197 sizeof (struct ppc_link_hash_entry),
3198 PPC64_ELF_DATA))
65f38f15 3199 {
e2d34d7d 3200 free (htab);
65f38f15
AM
3201 return NULL;
3202 }
3203
721956f4 3204 /* Init the stub hash table too. */
66eb6687
AM
3205 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3206 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3207 {
d495ab0d 3208 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3209 return NULL;
3210 }
721956f4
AM
3211
3212 /* And the branch hash table. */
66eb6687
AM
3213 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3214 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3215 {
3216 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3217 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3218 return NULL;
3219 }
721956f4 3220
3b421ab3
AM
3221 htab->tocsave_htab = htab_try_create (1024,
3222 tocsave_htab_hash,
3223 tocsave_htab_eq,
3224 NULL);
3225 if (htab->tocsave_htab == NULL)
2915c55b 3226 {
d495ab0d 3227 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3228 return NULL;
3229 }
d495ab0d 3230 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3231
3254fd24
AM
3232 /* Initializing two fields of the union is just cosmetic. We really
3233 only care about glist, but when compiled on a 32-bit host the
3234 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3235 debugger inspection of these fields look nicer. */
a6aa5195
AM
3236 htab->elf.init_got_refcount.refcount = 0;
3237 htab->elf.init_got_refcount.glist = NULL;
3238 htab->elf.init_plt_refcount.refcount = 0;
3239 htab->elf.init_plt_refcount.glist = NULL;
3240 htab->elf.init_got_offset.offset = 0;
3241 htab->elf.init_got_offset.glist = NULL;
3242 htab->elf.init_plt_offset.offset = 0;
3243 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3244
65f38f15
AM
3245 return &htab->elf.root;
3246}
3247
bfeb4a28
AM
3248/* Create sections for linker generated code. */
3249
3250static bfd_boolean
3251create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3252{
3253 struct ppc_link_hash_table *htab;
3254 flagword flags;
3255
3256 htab = ppc_hash_table (info);
3257
bfeb4a28
AM
3258 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3259 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3260 if (htab->params->save_restore_funcs)
3261 {
3262 /* Create .sfpr for code to save and restore fp regs. */
3263 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3264 flags);
3265 if (htab->sfpr == NULL
2cdcc330 3266 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b32547cd
AM
3267 return FALSE;
3268 }
3269
3270 if (bfd_link_relocatable (info))
3271 return TRUE;
bfeb4a28
AM
3272
3273 /* Create .glink for lazy dynamic linking support. */
3274 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3275 flags);
3276 if (htab->glink == NULL
2cdcc330 3277 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
bfeb4a28
AM
3278 return FALSE;
3279
9e390558
AM
3280 /* The part of .glink used by global entry stubs, separate so that
3281 it can be aligned appropriately without affecting htab->glink. */
3282 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3283 flags);
3284 if (htab->global_entry == NULL
2cdcc330 3285 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
9e390558
AM
3286 return FALSE;
3287
bfeb4a28
AM
3288 if (!info->no_ld_generated_unwind_info)
3289 {
3290 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3291 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3292 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3293 ".eh_frame",
3294 flags);
3295 if (htab->glink_eh_frame == NULL
3296 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3297 return FALSE;
3298 }
3299
3300 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3301 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3302 if (htab->elf.iplt == NULL
2cdcc330 3303 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
3304 return FALSE;
3305
3306 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3307 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3308 htab->elf.irelplt
3309 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3310 if (htab->elf.irelplt == NULL
2cdcc330 3311 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
3312 return FALSE;
3313
3314 /* Create branch lookup table for plt_branch stubs. */
3315 flags = (SEC_ALLOC | SEC_LOAD
3316 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3317 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3318 flags);
3319 if (htab->brlt == NULL
2cdcc330 3320 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
bfeb4a28
AM
3321 return FALSE;
3322
2d7ad24e
AM
3323 /* Local plt entries, put in .branch_lt but a separate section for
3324 convenience. */
3325 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3326 flags);
3327 if (htab->pltlocal == NULL
2cdcc330 3328 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
2d7ad24e
AM
3329 return FALSE;
3330
0e1862bb 3331 if (!bfd_link_pic (info))
bfeb4a28
AM
3332 return TRUE;
3333
3334 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3335 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3336 htab->relbrlt
3337 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3338 if (htab->relbrlt == NULL
2cdcc330 3339 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
bfeb4a28
AM
3340 return FALSE;
3341
2d7ad24e
AM
3342 htab->relpltlocal
3343 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3344 if (htab->relpltlocal == NULL
2cdcc330 3345 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
2d7ad24e
AM
3346 return FALSE;
3347
bfeb4a28
AM
3348 return TRUE;
3349}
3350
e717da7e
AM
3351/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3352
bfeb4a28 3353bfd_boolean
e7d1c40c
AM
3354ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3355 struct ppc64_elf_params *params)
e717da7e
AM
3356{
3357 struct ppc_link_hash_table *htab;
3358
e7d1c40c 3359 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3360
3361/* Always hook our dynamic sections into the first bfd, which is the
3362 linker created stub bfd. This ensures that the GOT header is at
3363 the start of the output TOC section. */
3364 htab = ppc_hash_table (info);
e7d1c40c
AM
3365 htab->elf.dynobj = params->stub_bfd;
3366 htab->params = params;
bfeb4a28 3367
bfeb4a28 3368 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3369}
3370
721956f4
AM
3371/* Build a name for an entry in the stub hash table. */
3372
3373static char *
4ce794b7
AM
3374ppc_stub_name (const asection *input_section,
3375 const asection *sym_sec,
3376 const struct ppc_link_hash_entry *h,
3377 const Elf_Internal_Rela *rel)
721956f4
AM
3378{
3379 char *stub_name;
bcaa2f82 3380 ssize_t len;
721956f4
AM
3381
3382 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3383 offsets from a sym as a branch target? In fact, we could
3384 probably assume the addend is always zero. */
3385 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3386
3387 if (h)
3388 {
3389 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3390 stub_name = bfd_malloc (len);
46de2a7c
AM
3391 if (stub_name == NULL)
3392 return stub_name;
3393
bcaa2f82
AM
3394 len = sprintf (stub_name, "%08x.%s+%x",
3395 input_section->id & 0xffffffff,
3396 h->elf.root.root.string,
3397 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3398 }
3399 else
3400 {
ad8e1ba5 3401 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3402 stub_name = bfd_malloc (len);
46de2a7c
AM
3403 if (stub_name == NULL)
3404 return stub_name;
3405
bcaa2f82
AM
3406 len = sprintf (stub_name, "%08x.%x:%x+%x",
3407 input_section->id & 0xffffffff,
3408 sym_sec->id & 0xffffffff,
3409 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3410 (int) rel->r_addend & 0xffffffff);
721956f4 3411 }
bcaa2f82 3412 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3413 stub_name[len - 2] = 0;
721956f4
AM
3414 return stub_name;
3415}
3416
3417/* Look up an entry in the stub hash. Stub entries are cached because
3418 creating the stub name takes a bit of time. */
3419
3420static struct ppc_stub_hash_entry *
4ce794b7
AM
3421ppc_get_stub_entry (const asection *input_section,
3422 const asection *sym_sec,
039b3fef 3423 struct ppc_link_hash_entry *h,
4ce794b7
AM
3424 const Elf_Internal_Rela *rel,
3425 struct ppc_link_hash_table *htab)
721956f4
AM
3426{
3427 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3428 struct map_stub *group;
721956f4
AM
3429
3430 /* If this input section is part of a group of sections sharing one
3431 stub section, then use the id of the first section in the group.
3432 Stub names need to include a section id, as there may well be
3433 more than one stub used to reach say, printf, and we need to
3434 distinguish between them. */
6f20ed8a 3435 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3436 if (group == NULL)
3437 return NULL;
721956f4 3438
b3fac117
AM
3439 if (h != NULL && h->u.stub_cache != NULL
3440 && h->u.stub_cache->h == h
6f20ed8a 3441 && h->u.stub_cache->group == group)
721956f4 3442 {
b3fac117 3443 stub_entry = h->u.stub_cache;
721956f4
AM
3444 }
3445 else
3446 {
3447 char *stub_name;
3448
6f20ed8a 3449 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3450 if (stub_name == NULL)
3451 return NULL;
3452
3453 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3454 stub_name, FALSE, FALSE);
721956f4 3455 if (h != NULL)
b3fac117 3456 h->u.stub_cache = stub_entry;
721956f4
AM
3457
3458 free (stub_name);
3459 }
3460
3461 return stub_entry;
3462}
3463
3464/* Add a new stub entry to the stub hash. Not all fields of the new
3465 stub entry are initialised. */
3466
3467static struct ppc_stub_hash_entry *
4ce794b7
AM
3468ppc_add_stub (const char *stub_name,
3469 asection *section,
25f53a85 3470 struct bfd_link_info *info)
721956f4 3471{
25f53a85 3472 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3473 struct map_stub *group;
721956f4
AM
3474 asection *link_sec;
3475 asection *stub_sec;
3476 struct ppc_stub_hash_entry *stub_entry;
3477
6f20ed8a
AM
3478 group = htab->sec_info[section->id].u.group;
3479 link_sec = group->link_sec;
3480 stub_sec = group->stub_sec;
721956f4
AM
3481 if (stub_sec == NULL)
3482 {
6f20ed8a
AM
3483 size_t namelen;
3484 bfd_size_type len;
3485 char *s_name;
721956f4 3486
6f20ed8a
AM
3487 namelen = strlen (link_sec->name);
3488 len = namelen + sizeof (STUB_SUFFIX);
3489 s_name = bfd_alloc (htab->params->stub_bfd, len);
3490 if (s_name == NULL)
3491 return NULL;
721956f4 3492
6f20ed8a
AM
3493 memcpy (s_name, link_sec->name, namelen);
3494 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3495 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3496 if (stub_sec == NULL)
3497 return NULL;
3498 group->stub_sec = stub_sec;
721956f4
AM
3499 }
3500
3501 /* Enter this entry into the linker stub hash table. */
3502 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3503 TRUE, FALSE);
721956f4
AM
3504 if (stub_entry == NULL)
3505 {
695344c0 3506 /* xgettext:c-format */
cf97bcb0
AM
3507 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3508 section->owner, stub_name);
721956f4
AM
3509 return NULL;
3510 }
3511
6f20ed8a 3512 stub_entry->group = group;
721956f4 3513 stub_entry->stub_offset = 0;
721956f4
AM
3514 return stub_entry;
3515}
3516
e717da7e
AM
3517/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3518 not already done. */
65f38f15 3519
b34976b6 3520static bfd_boolean
e717da7e 3521create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3522{
e717da7e
AM
3523 asection *got, *relgot;
3524 flagword flags;
3525 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3526
0c8d6e5c 3527 if (!is_ppc64_elf (abfd))
0ffa91dd 3528 return FALSE;
4dfe6ac6
NC
3529 if (htab == NULL)
3530 return FALSE;
0ffa91dd 3531
33e44f2e
AM
3532 if (!htab->elf.sgot
3533 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3534 return FALSE;
e717da7e
AM
3535
3536 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3537 | SEC_LINKER_CREATED);
3538
c456f082 3539 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3540 if (!got
e717da7e
AM
3541 || !bfd_set_section_alignment (abfd, got, 3))
3542 return FALSE;
65f38f15 3543
c456f082
AM
3544 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3545 flags | SEC_READONLY);
e717da7e 3546 if (!relgot
2cdcc330 3547 || !bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 3548 return FALSE;
e717da7e
AM
3549
3550 ppc64_elf_tdata (abfd)->got = got;
3551 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 3552 return TRUE;
65f38f15 3553}
5bd4f169 3554
b31867b6
AM
3555/* Follow indirect and warning symbol links. */
3556
3557static inline struct bfd_link_hash_entry *
3558follow_link (struct bfd_link_hash_entry *h)
3559{
3560 while (h->type == bfd_link_hash_indirect
3561 || h->type == bfd_link_hash_warning)
3562 h = h->u.i.link;
3563 return h;
3564}
3565
3566static inline struct elf_link_hash_entry *
3567elf_follow_link (struct elf_link_hash_entry *h)
3568{
3569 return (struct elf_link_hash_entry *) follow_link (&h->root);
3570}
3571
3572static inline struct ppc_link_hash_entry *
3573ppc_follow_link (struct ppc_link_hash_entry *h)
3574{
3575 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3576}
3577
40d16e0b
AM
3578/* Merge PLT info on FROM with that on TO. */
3579
3580static void
3581move_plt_plist (struct ppc_link_hash_entry *from,
3582 struct ppc_link_hash_entry *to)
3583{
3584 if (from->elf.plt.plist != NULL)
3585 {
3586 if (to->elf.plt.plist != NULL)
3587 {
3588 struct plt_entry **entp;
3589 struct plt_entry *ent;
3590
3591 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3592 {
3593 struct plt_entry *dent;
3594
3595 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3596 if (dent->addend == ent->addend)
3597 {
3598 dent->plt.refcount += ent->plt.refcount;
3599 *entp = ent->next;
3600 break;
3601 }
3602 if (dent == NULL)
3603 entp = &ent->next;
3604 }
3605 *entp = to->elf.plt.plist;
3606 }
3607
3608 to->elf.plt.plist = from->elf.plt.plist;
3609 from->elf.plt.plist = NULL;
3610 }
3611}
3612
65f38f15
AM
3613/* Copy the extra info we tack onto an elf_link_hash_entry. */
3614
3615static void
fcfa13d2
AM
3616ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3617 struct elf_link_hash_entry *dir,
3618 struct elf_link_hash_entry *ind)
65f38f15
AM
3619{
3620 struct ppc_link_hash_entry *edir, *eind;
3621
3622 edir = (struct ppc_link_hash_entry *) dir;
3623 eind = (struct ppc_link_hash_entry *) ind;
3624
c79d6685
AM
3625 edir->is_func |= eind->is_func;
3626 edir->is_func_descriptor |= eind->is_func_descriptor;
3627 edir->tls_mask |= eind->tls_mask;
3628 if (eind->oh != NULL)
3629 edir->oh = ppc_follow_link (eind->oh);
3630
474436e6 3631 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
3632 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3633 edir->elf.ref_regular |= eind->elf.ref_regular;
3634 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 3635 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
3636 edir->elf.needs_plt |= eind->elf.needs_plt;
3637 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 3638
d311bc8b
AM
3639 /* If we were called to copy over info for a weak sym, don't copy
3640 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3641 in order to simplify readonly_dynrelocs and save a field in the
3642 symbol hash entry, but that means dyn_relocs can't be used in any
3643 tests about a specific symbol, or affect other symbol flags which
ab2477e1 3644 are then tested. */
d311bc8b 3645 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 3646 return;
d311bc8b 3647
411e1bfb 3648 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 3649 if (eind->dyn_relocs != NULL)
65f38f15 3650 {
bbd7ec4a
AM
3651 if (edir->dyn_relocs != NULL)
3652 {
6061a67d
AM
3653 struct elf_dyn_relocs **pp;
3654 struct elf_dyn_relocs *p;
bbd7ec4a 3655
fcfa13d2 3656 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
3657 list. Merge any entries against the same section. */
3658 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3659 {
6061a67d 3660 struct elf_dyn_relocs *q;
bbd7ec4a
AM
3661
3662 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3663 if (q->sec == p->sec)
3664 {
3665 q->pc_count += p->pc_count;
3666 q->count += p->count;
3667 *pp = p->next;
3668 break;
3669 }
3670 if (q == NULL)
3671 pp = &p->next;
3672 }
3673 *pp = edir->dyn_relocs;
3674 }
3675
65f38f15
AM
3676 edir->dyn_relocs = eind->dyn_relocs;
3677 eind->dyn_relocs = NULL;
3678 }
65f38f15 3679
81848ca0
AM
3680 /* Copy over got entries that we may have already seen to the
3681 symbol which just became indirect. */
411e1bfb
AM
3682 if (eind->elf.got.glist != NULL)
3683 {
3684 if (edir->elf.got.glist != NULL)
3685 {
3686 struct got_entry **entp;
3687 struct got_entry *ent;
3688
3689 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3690 {
3691 struct got_entry *dent;
3692
3693 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3694 if (dent->addend == ent->addend
e717da7e 3695 && dent->owner == ent->owner
411e1bfb
AM
3696 && dent->tls_type == ent->tls_type)
3697 {
3698 dent->got.refcount += ent->got.refcount;
3699 *entp = ent->next;
3700 break;
3701 }
3702 if (dent == NULL)
3703 entp = &ent->next;
3704 }
3705 *entp = edir->elf.got.glist;
3706 }
3707
3708 edir->elf.got.glist = eind->elf.got.glist;
3709 eind->elf.got.glist = NULL;
3710 }
3711
3712 /* And plt entries. */
40d16e0b 3713 move_plt_plist (eind, edir);
411e1bfb 3714
fcfa13d2 3715 if (eind->elf.dynindx != -1)
411e1bfb 3716 {
fcfa13d2
AM
3717 if (edir->elf.dynindx != -1)
3718 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3719 edir->elf.dynstr_index);
411e1bfb
AM
3720 edir->elf.dynindx = eind->elf.dynindx;
3721 edir->elf.dynstr_index = eind->elf.dynstr_index;
3722 eind->elf.dynindx = -1;
3723 eind->elf.dynstr_index = 0;
3724 }
411e1bfb
AM
3725}
3726
8387904d
AM
3727/* Find the function descriptor hash entry from the given function code
3728 hash entry FH. Link the entries via their OH fields. */
3729
3730static struct ppc_link_hash_entry *
b31867b6 3731lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
3732{
3733 struct ppc_link_hash_entry *fdh = fh->oh;
3734
3735 if (fdh == NULL)
3736 {
3737 const char *fd_name = fh->elf.root.root.string + 1;
3738
3739 fdh = (struct ppc_link_hash_entry *)
3740 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
3741 if (fdh == NULL)
3742 return fdh;
3743
3744 fdh->is_func_descriptor = 1;
3745 fdh->oh = fh;
3746 fh->is_func = 1;
3747 fh->oh = fdh;
8387904d
AM
3748 }
3749
8c5b4e52
AM
3750 fdh = ppc_follow_link (fdh);
3751 fdh->is_func_descriptor = 1;
3752 fdh->oh = fh;
3753 return fdh;
8387904d
AM
3754}
3755
8c5b4e52 3756/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
3757
3758static struct ppc_link_hash_entry *
3759make_fdh (struct bfd_link_info *info,
908b32fc 3760 struct ppc_link_hash_entry *fh)
bb700d78 3761{
8c5b4e52
AM
3762 bfd *abfd = fh->elf.root.u.undef.abfd;
3763 struct bfd_link_hash_entry *bh = NULL;
bb700d78 3764 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
3765 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3766 ? BSF_WEAK
3767 : BSF_GLOBAL);
3768
3769 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3770 fh->elf.root.root.string + 1,
3771 flags, bfd_und_section_ptr, 0,
3772 NULL, FALSE, FALSE, &bh))
bb700d78
AM
3773 return NULL;
3774
3775 fdh = (struct ppc_link_hash_entry *) bh;
3776 fdh->elf.non_elf = 0;
908b32fc
AM
3777 fdh->fake = 1;
3778 fdh->is_func_descriptor = 1;
3779 fdh->oh = fh;
3780 fh->is_func = 1;
3781 fh->oh = fdh;
bb700d78
AM
3782 return fdh;
3783}
3784
8387904d
AM
3785/* Fix function descriptor symbols defined in .opd sections to be
3786 function type. */
555cd476
AM
3787
3788static bfd_boolean
c16153ae 3789ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 3790 struct bfd_link_info *info,
555cd476 3791 Elf_Internal_Sym *isym,
6911b7dc 3792 const char **name,
555cd476
AM
3793 flagword *flags ATTRIBUTE_UNUSED,
3794 asection **sec,
b53dfeb2 3795 bfd_vma *value)
555cd476 3796{
b53dfeb2 3797 if (*sec != NULL
f1885d1e 3798 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
3799 {
3800 asection *code_sec;
3801
3802 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3803 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
3804 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
3805
3806 /* If the symbol is a function defined in .opd, and the function
3807 code is in a discarded group, let it appear to be undefined. */
0e1862bb 3808 if (!bfd_link_relocatable (info)
b53dfeb2
AM
3809 && (*sec)->reloc_count != 0
3810 && opd_entry_value (*sec, *value, &code_sec, NULL,
3811 FALSE) != (bfd_vma) -1
3812 && discarded_section (code_sec))
3813 {
3814 *sec = bfd_und_section_ptr;
3815 isym->st_shndx = SHN_UNDEF;
3816 }
3817 }
dbd1e97e
AM
3818 else if (*sec != NULL
3819 && strcmp ((*sec)->name, ".toc") == 0
3820 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
3821 {
3822 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3823 if (htab != NULL)
3824 htab->params->object_in_toc = 1;
3825 }
433817dd 3826
6911b7dc
AM
3827 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3828 {
3829 if (abiversion (ibfd) == 0)
3830 set_abiversion (ibfd, 2);
3831 else if (abiversion (ibfd) == 1)
3832 {
cf97bcb0
AM
3833 _bfd_error_handler (_("symbol '%s' has invalid st_other"
3834 " for ABI version 1"), *name);
6911b7dc
AM
3835 bfd_set_error (bfd_error_bad_value);
3836 return FALSE;
3837 }
3838 }
3839
555cd476
AM
3840 return TRUE;
3841}
3842
6911b7dc
AM
3843/* Merge non-visibility st_other attributes: local entry point. */
3844
3845static void
3846ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
3847 const Elf_Internal_Sym *isym,
3848 bfd_boolean definition,
3849 bfd_boolean dynamic)
3850{
f378ab09 3851 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
3852 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
3853 | ELF_ST_VISIBILITY (h->other));
3854}
3855
8c5b4e52
AM
3856/* Hook called on merging a symbol. We use this to clear "fake" since
3857 we now have a real symbol. */
3858
3859static bfd_boolean
3860ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 3861 const Elf_Internal_Sym *isym,
8c5b4e52
AM
3862 asection **psec ATTRIBUTE_UNUSED,
3863 bfd_boolean newdef ATTRIBUTE_UNUSED,
3864 bfd_boolean olddef ATTRIBUTE_UNUSED,
3865 bfd *oldbfd ATTRIBUTE_UNUSED,
3866 const asection *oldsec ATTRIBUTE_UNUSED)
3867{
3868 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
3869 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3870 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
3871 return TRUE;
3872}
3873
8387904d 3874/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
3875 inclusion of a new ABI object archive that defines "bar".
3876 NAME is a symbol defined in an archive. Return a symbol in the hash
3877 table that might be satisfied by the archive symbols. */
8387904d
AM
3878
3879static struct elf_link_hash_entry *
3880ppc64_elf_archive_symbol_lookup (bfd *abfd,
3881 struct bfd_link_info *info,
3882 const char *name)
3883{
3884 struct elf_link_hash_entry *h;
3885 char *dot_name;
3886 size_t len;
3887
3888 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
3889 if (h != NULL
3890 /* Don't return this sym if it is a fake function descriptor
3891 created by add_symbol_adjust. */
8c5b4e52 3892 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
3893 return h;
3894
3895 if (name[0] == '.')
3896 return h;
3897
3898 len = strlen (name);
3899 dot_name = bfd_alloc (abfd, len + 2);
3900 if (dot_name == NULL)
e99955cd 3901 return (struct elf_link_hash_entry *) -1;
8387904d
AM
3902 dot_name[0] = '.';
3903 memcpy (dot_name + 1, name, len + 1);
3904 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
3905 bfd_release (abfd, dot_name);
3906 return h;
3907}
3908
3909/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
3910 new ABI object defines "bar". Well, at least, undefined dot symbols
3911 are made weak. This stops later archive searches from including an
3912 object if we already have a function descriptor definition. It also
35b0ce59
AM
3913 prevents the linker complaining about undefined symbols.
3914 We also check and correct mismatched symbol visibility here. The
3915 most restrictive visibility of the function descriptor and the
3916 function entry symbol is used. */
8387904d
AM
3917
3918static bfd_boolean
b3fac117 3919add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 3920{
8387904d
AM
3921 struct ppc_link_hash_table *htab;
3922 struct ppc_link_hash_entry *fdh;
3923
b3fac117
AM
3924 if (eh->elf.root.type == bfd_link_hash_warning)
3925 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 3926
8400d40d
AM
3927 if (eh->elf.root.type == bfd_link_hash_indirect)
3928 return TRUE;
3929
b3fac117
AM
3930 if (eh->elf.root.root.string[0] != '.')
3931 abort ();
8387904d 3932
b3fac117 3933 htab = ppc_hash_table (info);
4dfe6ac6
NC
3934 if (htab == NULL)
3935 return FALSE;
3936
b31867b6 3937 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
3938 if (fdh == NULL
3939 && !bfd_link_relocatable (info)
3940 && (eh->elf.root.type == bfd_link_hash_undefined
3941 || eh->elf.root.type == bfd_link_hash_undefweak)
3942 && eh->elf.ref_regular)
3943 {
3944 /* Make an undefined function descriptor sym, in order to
3945 pull in an --as-needed shared lib. Archives are handled
3946 elsewhere. */
3947 fdh = make_fdh (info, eh);
3948 if (fdh == NULL)
3949 return FALSE;
bb700d78 3950 }
8c5b4e52
AM
3951
3952 if (fdh != NULL)
8387904d 3953 {
35b0ce59
AM
3954 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
3955 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
3956
3957 /* Make both descriptor and entry symbol have the most
3958 constraining visibility of either symbol. */
35b0ce59
AM
3959 if (entry_vis < descr_vis)
3960 fdh->elf.other += entry_vis - descr_vis;
3961 else if (entry_vis > descr_vis)
3962 eh->elf.other += descr_vis - entry_vis;
3963
8c5b4e52
AM
3964 /* Propagate reference flags from entry symbol to function
3965 descriptor symbol. */
bc4e12de 3966 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 3967 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
3968 fdh->elf.ref_regular |= eh->elf.ref_regular;
3969 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
3970
3971 if (!fdh->elf.forced_local
3972 && fdh->elf.dynindx == -1
3973 && fdh->elf.versioned != versioned_hidden
3974 && (bfd_link_dll (info)
3975 || fdh->elf.def_dynamic
3976 || fdh->elf.ref_dynamic)
3977 && (eh->elf.ref_regular
3978 || eh->elf.def_regular))
3979 {
2cdcc330 3980 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
8c5b4e52 3981 return FALSE;
35b0ce59 3982 }
8387904d 3983 }
99877b66 3984
8387904d
AM
3985 return TRUE;
3986}
3987
f6c7c3e8
AM
3988/* Set up opd section info and abiversion for IBFD, and process list
3989 of dot-symbols we made in link_hash_newfunc. */
b3fac117 3990
8387904d 3991static bfd_boolean
f6c7c3e8 3992ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 3993{
99877b66 3994 struct ppc_link_hash_table *htab;
b3fac117 3995 struct ppc_link_hash_entry **p, *eh;
459609d6 3996 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 3997
459609d6 3998 if (opd != NULL && opd->size != 0)
b3fac117 3999 {
b9399fcf
AM
4000 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4001 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4002
459609d6
AM
4003 if (abiversion (ibfd) == 0)
4004 set_abiversion (ibfd, 1);
8a2058b5 4005 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4006 {
695344c0 4007 /* xgettext:c-format */
cf97bcb0
AM
4008 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4009 ibfd, abiversion (ibfd));
459609d6
AM
4010 bfd_set_error (bfd_error_bad_value);
4011 return FALSE;
f6c7c3e8 4012 }
b9399fcf 4013 }
f6c7c3e8 4014
b9399fcf
AM
4015 if (is_ppc64_elf (info->output_bfd))
4016 {
4017 /* For input files without an explicit abiversion in e_flags
4018 we should have flagged any with symbol st_other bits set
4019 as ELFv1 and above flagged those with .opd as ELFv2.
4020 Set the output abiversion if not yet set, and for any input
4021 still ambiguous, take its abiversion from the output.
4022 Differences in ABI are reported later. */
4023 if (abiversion (info->output_bfd) == 0)
4024 set_abiversion (info->output_bfd, abiversion (ibfd));
4025 else if (abiversion (ibfd) == 0)
4026 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4027 }
4028
459609d6
AM
4029 htab = ppc_hash_table (info);
4030 if (htab == NULL)
b9399fcf 4031 return TRUE;
459609d6 4032
b9399fcf
AM
4033 if (opd != NULL && opd->size != 0
4034 && (ibfd->flags & DYNAMIC) == 0
4035 && (opd->flags & SEC_RELOC) != 0
4036 && opd->reloc_count != 0
4037 && !bfd_is_abs_section (opd->output_section)
4038 && info->gc_sections)
4039 {
4040 /* Garbage collection needs some extra help with .opd sections.
4041 We don't want to necessarily keep everything referenced by
4042 relocs in .opd, as that would keep all functions. Instead,
4043 if we reference an .opd symbol (a function descriptor), we
4044 want to keep the function code symbol's section. This is
4045 easy for global symbols, but for local syms we need to keep
4046 information about the associated function section. */
4047 bfd_size_type amt;
4048 asection **opd_sym_map;
4049 Elf_Internal_Shdr *symtab_hdr;
4050 Elf_Internal_Rela *relocs, *rel_end, *rel;
4051
4052 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4053 opd_sym_map = bfd_zalloc (ibfd, amt);
4054 if (opd_sym_map == NULL)
4055 return FALSE;
4056 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4057 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4058 info->keep_memory);
4059 if (relocs == NULL)
4060 return FALSE;
4061 symtab_hdr = &elf_symtab_hdr (ibfd);
4062 rel_end = relocs + opd->reloc_count - 1;
4063 for (rel = relocs; rel < rel_end; rel++)
4064 {
4065 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4066 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4067
4068 if (r_type == R_PPC64_ADDR64
4069 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4070 && r_symndx < symtab_hdr->sh_info)
4071 {
4072 Elf_Internal_Sym *isym;
4073 asection *s;
4074
4075 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4076 if (isym == NULL)
4077 {
4078 if (elf_section_data (opd)->relocs != relocs)
4079 free (relocs);
4080 return FALSE;
4081 }
4082
4083 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4084 if (s != NULL && s != opd)
4085 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4086 }
4087 }
4088 if (elf_section_data (opd)->relocs != relocs)
4089 free (relocs);
4090 }
459609d6
AM
4091
4092 p = &htab->dot_syms;
4093 while ((eh = *p) != NULL)
4094 {
4095 *p = NULL;
4096 if (&eh->elf == htab->elf.hgot)
4097 ;
4098 else if (htab->elf.hgot == NULL
4099 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4100 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4101 else if (abiversion (ibfd) <= 1)
4102 {
4103 htab->need_func_desc_adj = 1;
4104 if (!add_symbol_adjust (eh, info))
4105 return FALSE;
4106 }
459609d6
AM
4107 p = &eh->u.next_dot_sym;
4108 }
b3fac117 4109 return TRUE;
8387904d
AM
4110}
4111
97fed1c9
JJ
4112/* Undo hash table changes when an --as-needed input file is determined
4113 not to be needed. */
4114
4115static bfd_boolean
e5034e59
AM
4116ppc64_elf_notice_as_needed (bfd *ibfd,
4117 struct bfd_link_info *info,
4118 enum notice_asneeded_action act)
97fed1c9 4119{
e5034e59
AM
4120 if (act == notice_not_needed)
4121 {
4122 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4123
e5034e59
AM
4124 if (htab == NULL)
4125 return FALSE;
4dfe6ac6 4126
e5034e59
AM
4127 htab->dot_syms = NULL;
4128 }
4129 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4130}
4131
aa374f67
AM
4132/* If --just-symbols against a final linked binary, then assume we need
4133 toc adjusting stubs when calling functions defined there. */
4134
4135static void
4136ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4137{
4138 if ((sec->flags & SEC_CODE) != 0
4139 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4140 && is_ppc64_elf (sec->owner))
4141 {
2c3f079f
AM
4142 if (abiversion (sec->owner) >= 2
4143 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4144 sec->has_toc_reloc = 1;
4145 }
4146 _bfd_elf_link_just_syms (sec, info);
4147}
4148
e054468f 4149static struct plt_entry **
4ce794b7
AM
4150update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4151 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4152{
4153 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4154 struct plt_entry **local_plt;
f961d9dd 4155 unsigned char *local_got_tls_masks;
411e1bfb
AM
4156
4157 if (local_got_ents == NULL)
4158 {
4159 bfd_size_type size = symtab_hdr->sh_info;
4160
e054468f
AM
4161 size *= (sizeof (*local_got_ents)
4162 + sizeof (*local_plt)
4163 + sizeof (*local_got_tls_masks));
4ce794b7 4164 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4165 if (local_got_ents == NULL)
e054468f 4166 return NULL;
411e1bfb
AM
4167 elf_local_got_ents (abfd) = local_got_ents;
4168 }
4169
37da22e5 4170 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4171 {
4172 struct got_entry *ent;
4173
4174 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4175 if (ent->addend == r_addend
4176 && ent->owner == abfd
4177 && ent->tls_type == tls_type)
411e1bfb
AM
4178 break;
4179 if (ent == NULL)
4180 {
4181 bfd_size_type amt = sizeof (*ent);
4ce794b7 4182 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4183 if (ent == NULL)
4184 return FALSE;
4185 ent->next = local_got_ents[r_symndx];
4186 ent->addend = r_addend;
e717da7e 4187 ent->owner = abfd;
411e1bfb 4188 ent->tls_type = tls_type;
927be08e 4189 ent->is_indirect = FALSE;
411e1bfb
AM
4190 ent->got.refcount = 0;
4191 local_got_ents[r_symndx] = ent;
4192 }
4193 ent->got.refcount += 1;
4194 }
4195
e054468f 4196 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4197 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4198 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4199
4200 return local_plt + r_symndx;
65f38f15
AM
4201}
4202
411e1bfb 4203static bfd_boolean
e054468f 4204update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4205{
411e1bfb 4206 struct plt_entry *ent;
1e2f5b6e 4207
e054468f 4208 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4209 if (ent->addend == addend)
4210 break;
4211 if (ent == NULL)
1e2f5b6e 4212 {
411e1bfb 4213 bfd_size_type amt = sizeof (*ent);
4ce794b7 4214 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4215 if (ent == NULL)
4216 return FALSE;
e054468f 4217 ent->next = *plist;
411e1bfb
AM
4218 ent->addend = addend;
4219 ent->plt.refcount = 0;
e054468f 4220 *plist = ent;
1e2f5b6e 4221 }
411e1bfb 4222 ent->plt.refcount += 1;
b34976b6 4223 return TRUE;
1e2f5b6e
AM
4224}
4225
e054468f
AM
4226static bfd_boolean
4227is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4228{
4229 return (r_type == R_PPC64_REL24
05d0e962 4230 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
4231 || r_type == R_PPC64_REL14
4232 || r_type == R_PPC64_REL14_BRTAKEN
4233 || r_type == R_PPC64_REL14_BRNTAKEN
4234 || r_type == R_PPC64_ADDR24
4235 || r_type == R_PPC64_ADDR14
4236 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d
AM
4237 || r_type == R_PPC64_ADDR14_BRNTAKEN
4238 || r_type == R_PPC64_PLTCALL);
4239}
4240
4241/* Relocs on inline plt call sequence insns prior to the call. */
4242
4243static bfd_boolean
4244is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4245{
4246 return (r_type == R_PPC64_PLT16_HA
4247 || r_type == R_PPC64_PLT16_HI
4248 || r_type == R_PPC64_PLT16_LO
4249 || r_type == R_PPC64_PLT16_LO_DS
4250 || r_type == R_PPC64_PLTSEQ);
e054468f
AM
4251}
4252
5bd4f169 4253/* Look through the relocs for a section during the first phase, and
65f38f15 4254 calculate needed space in the global offset table, procedure
5d1634d7 4255 linkage table, and dynamic reloc sections. */
5bd4f169 4256
b34976b6 4257static bfd_boolean
4ce794b7
AM
4258ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4259 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4260{
65f38f15 4261 struct ppc_link_hash_table *htab;
5bd4f169 4262 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4263 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4264 const Elf_Internal_Rela *rel;
4265 const Elf_Internal_Rela *rel_end;
5bd4f169 4266 asection *sreloc;
3a71aa26 4267 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 4268 bfd_boolean is_opd;
5bd4f169 4269
0e1862bb 4270 if (bfd_link_relocatable (info))
b34976b6 4271 return TRUE;
5bd4f169 4272
680a3378
AM
4273 /* Don't do anything special with non-loaded, non-alloced sections.
4274 In particular, any relocs in such sections should not affect GOT
4275 and PLT reference counting (ie. we don't allow them to create GOT
4276 or PLT entries), there's no possibility or desire to optimize TLS
4277 relocs, and there's not much point in propagating relocs to shared
4278 libs that the dynamic linker won't relocate. */
4279 if ((sec->flags & SEC_ALLOC) == 0)
4280 return TRUE;
4281
0c8d6e5c 4282 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4283
65f38f15 4284 htab = ppc_hash_table (info);
4dfe6ac6
NC
4285 if (htab == NULL)
4286 return FALSE;
4287
3a71aa26
AM
4288 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4289 FALSE, FALSE, TRUE);
4290 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4291 FALSE, FALSE, TRUE);
0ffa91dd 4292 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4293 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4294 sreloc = NULL;
b9399fcf 4295 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4296 rel_end = relocs + sec->reloc_count;
4297 for (rel = relocs; rel < rel_end; rel++)
4298 {
4299 unsigned long r_symndx;
4300 struct elf_link_hash_entry *h;
04c9666a 4301 enum elf_ppc64_reloc_type r_type;
727fc41e 4302 int tls_type;
7c8fe5c4 4303 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4304 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
4305
4306 r_symndx = ELF64_R_SYM (rel->r_info);
4307 if (r_symndx < symtab_hdr->sh_info)
4308 h = NULL;
4309 else
973a3492
L
4310 {
4311 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4312 h = elf_follow_link (h);
1c865ab2
AM
4313
4314 if (h == htab->elf.hgot)
4315 sec->has_toc_reloc = 1;
973a3492 4316 }
5bd4f169 4317
727fc41e 4318 tls_type = 0;
e054468f 4319 ifunc = NULL;
25f23106
AM
4320 if (h != NULL)
4321 {
4322 if (h->type == STT_GNU_IFUNC)
4323 {
4324 h->needs_plt = 1;
4325 ifunc = &h->plt.plist;
4326 }
4327 }
4328 else
4329 {
4330 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4331 abfd, r_symndx);
4332 if (isym == NULL)
4333 return FALSE;
4334
4335 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4336 {
4337 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
37da22e5
AM
4338 rel->r_addend,
4339 NON_GOT | PLT_IFUNC);
25f23106
AM
4340 if (ifunc == NULL)
4341 return FALSE;
4342 }
4343 }
727fc41e 4344
cbf95972 4345 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 4346 switch (r_type)
5bd4f169 4347 {
727fc41e
AM
4348 case R_PPC64_TLSGD:
4349 case R_PPC64_TLSLD:
4350 /* These special tls relocs tie a call to __tls_get_addr with
4351 its parameter symbol. */
37da22e5
AM
4352 if (h != NULL)
4353 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4354 else
4355 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4356 rel->r_addend,
4357 NON_GOT | TLS_TLS | TLS_MARK))
4358 return FALSE;
4359 sec->has_tls_reloc = 1;
727fc41e
AM
4360 break;
4361
411e1bfb
AM
4362 case R_PPC64_GOT_TLSLD16:
4363 case R_PPC64_GOT_TLSLD16_LO:
4364 case R_PPC64_GOT_TLSLD16_HI:
4365 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 4366 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4367 goto dogottls;
4368
4369 case R_PPC64_GOT_TLSGD16:
4370 case R_PPC64_GOT_TLSGD16_LO:
4371 case R_PPC64_GOT_TLSGD16_HI:
4372 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 4373 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4374 goto dogottls;
4375
4376 case R_PPC64_GOT_TPREL16_DS:
4377 case R_PPC64_GOT_TPREL16_LO_DS:
4378 case R_PPC64_GOT_TPREL16_HI:
4379 case R_PPC64_GOT_TPREL16_HA:
7c8bbca5 4380 if (bfd_link_dll (info))
411e1bfb
AM
4381 info->flags |= DF_STATIC_TLS;
4382 tls_type = TLS_TLS | TLS_TPREL;
4383 goto dogottls;
4384
4385 case R_PPC64_GOT_DTPREL16_DS:
4386 case R_PPC64_GOT_DTPREL16_LO_DS:
4387 case R_PPC64_GOT_DTPREL16_HI:
4388 case R_PPC64_GOT_DTPREL16_HA:
4389 tls_type = TLS_TLS | TLS_DTPREL;
4390 dogottls:
4391 sec->has_tls_reloc = 1;
066f4018 4392 goto dogot;
411e1bfb 4393
5bd4f169 4394 case R_PPC64_GOT16_DS:
65f38f15 4395 case R_PPC64_GOT16_HA:
066f4018
AM
4396 case R_PPC64_GOT16_LO_DS:
4397 ppc64_elf_tdata (abfd)->has_gotrel = 1;
4398 ppc64_elf_section_data (sec)->has_gotrel = 1;
4399 /* Fall through. */
4400
4401 case R_PPC64_GOT16:
65f38f15
AM
4402 case R_PPC64_GOT16_HI:
4403 case R_PPC64_GOT16_LO:
066f4018 4404 dogot:
65f38f15 4405 /* This symbol requires a global offset table entry. */
4c52953f 4406 sec->has_toc_reloc = 1;
33c0ec9d
AM
4407 if (r_type == R_PPC64_GOT_TLSLD16
4408 || r_type == R_PPC64_GOT_TLSGD16
4409 || r_type == R_PPC64_GOT_TPREL16_DS
4410 || r_type == R_PPC64_GOT_DTPREL16_DS
4411 || r_type == R_PPC64_GOT16
4412 || r_type == R_PPC64_GOT16_DS)
4413 {
4414 htab->do_multi_toc = 1;
d77c8a4b 4415 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4416 }
4417
e717da7e
AM
4418 if (ppc64_elf_tdata (abfd)->got == NULL
4419 && !create_got_section (abfd, info))
b34976b6 4420 return FALSE;
5bd4f169
AM
4421
4422 if (h != NULL)
4423 {
411e1bfb
AM
4424 struct ppc_link_hash_entry *eh;
4425 struct got_entry *ent;
65f38f15 4426
411e1bfb
AM
4427 eh = (struct ppc_link_hash_entry *) h;
4428 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4429 if (ent->addend == rel->r_addend
e717da7e 4430 && ent->owner == abfd
411e1bfb
AM
4431 && ent->tls_type == tls_type)
4432 break;
4433 if (ent == NULL)
5bd4f169 4434 {
411e1bfb 4435 bfd_size_type amt = sizeof (*ent);
4ce794b7 4436 ent = bfd_alloc (abfd, amt);
411e1bfb 4437 if (ent == NULL)
b34976b6 4438 return FALSE;
411e1bfb
AM
4439 ent->next = eh->elf.got.glist;
4440 ent->addend = rel->r_addend;
e717da7e 4441 ent->owner = abfd;
411e1bfb 4442 ent->tls_type = tls_type;
927be08e 4443 ent->is_indirect = FALSE;
411e1bfb
AM
4444 ent->got.refcount = 0;
4445 eh->elf.got.glist = ent;
5bd4f169 4446 }
411e1bfb 4447 ent->got.refcount += 1;
e7b938ca 4448 eh->tls_mask |= tls_type;
5bd4f169 4449 }
411e1bfb
AM
4450 else
4451 /* This is a global offset table entry for a local symbol. */
4452 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4453 rel->r_addend, tls_type))
4454 return FALSE;
a345bc8d
AM
4455
4456 /* We may also need a plt entry if the symbol turns out to be
4457 an ifunc. */
0e1862bb 4458 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
4459 {
4460 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4461 return FALSE;
4462 }
5bd4f169
AM
4463 break;
4464
5bd4f169 4465 case R_PPC64_PLT16_HA:
65f38f15
AM
4466 case R_PPC64_PLT16_HI:
4467 case R_PPC64_PLT16_LO:
08be3224 4468 case R_PPC64_PLT16_LO_DS:
65f38f15
AM
4469 case R_PPC64_PLT32:
4470 case R_PPC64_PLT64:
cbf95972
AM
4471 /* This symbol requires a procedure linkage table entry. */
4472 plt_list = ifunc;
4473 if (h != NULL)
e054468f 4474 {
e054468f
AM
4475 h->needs_plt = 1;
4476 if (h->root.root.string[0] == '.'
4477 && h->root.root.string[1] != '\0')
4478 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2d7ad24e 4479 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4480 plt_list = &h->plt.plist;
4481 }
4482 if (plt_list == NULL)
2d7ad24e
AM
4483 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4484 rel->r_addend,
4485 NON_GOT | PLT_KEEP);
cbf95972
AM
4486 if (!update_plt_info (abfd, plt_list, rel->r_addend))
4487 return FALSE;
5bd4f169
AM
4488 break;
4489
4490 /* The following relocations don't need to propagate the
4491 relocation if linking a shared object since they are
4492 section relative. */
4493 case R_PPC64_SECTOFF:
4494 case R_PPC64_SECTOFF_LO:
4495 case R_PPC64_SECTOFF_HI:
4496 case R_PPC64_SECTOFF_HA:
4497 case R_PPC64_SECTOFF_DS:
4498 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4499 case R_PPC64_DTPREL16:
4500 case R_PPC64_DTPREL16_LO:
4501 case R_PPC64_DTPREL16_HI:
4502 case R_PPC64_DTPREL16_HA:
4503 case R_PPC64_DTPREL16_DS:
4504 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
4505 case R_PPC64_DTPREL16_HIGH:
4506 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
4507 case R_PPC64_DTPREL16_HIGHER:
4508 case R_PPC64_DTPREL16_HIGHERA:
4509 case R_PPC64_DTPREL16_HIGHEST:
4510 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4511 break;
4512
ad8e1ba5 4513 /* Nor do these. */
25f23106
AM
4514 case R_PPC64_REL16:
4515 case R_PPC64_REL16_LO:
4516 case R_PPC64_REL16_HI:
4517 case R_PPC64_REL16_HA:
4a969973
AM
4518 case R_PPC64_REL16_HIGH:
4519 case R_PPC64_REL16_HIGHA:
4520 case R_PPC64_REL16_HIGHER:
4521 case R_PPC64_REL16_HIGHERA:
4522 case R_PPC64_REL16_HIGHEST:
4523 case R_PPC64_REL16_HIGHESTA:
a680de9a 4524 case R_PPC64_REL16DX_HA:
25f23106
AM
4525 break;
4526
45965137
AM
4527 /* Not supported as a dynamic relocation. */
4528 case R_PPC64_ADDR64_LOCAL:
0e1862bb 4529 if (bfd_link_pic (info))
45965137
AM
4530 {
4531 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4532 ppc_howto_init ();
695344c0 4533 /* xgettext:c-format */
174d0a74 4534 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 4535 "in shared libraries and PIEs\n"),
45965137
AM
4536 abfd, sec, rel->r_offset,
4537 ppc64_elf_howto_table[r_type]->name);
4538 bfd_set_error (bfd_error_bad_value);
4539 return FALSE;
4540 }
4541 break;
4542
ad8e1ba5 4543 case R_PPC64_TOC16:
33c0ec9d
AM
4544 case R_PPC64_TOC16_DS:
4545 htab->do_multi_toc = 1;
d77c8a4b 4546 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 4547 /* Fall through. */
ad8e1ba5
AM
4548 case R_PPC64_TOC16_LO:
4549 case R_PPC64_TOC16_HI:
4550 case R_PPC64_TOC16_HA:
ad8e1ba5 4551 case R_PPC64_TOC16_LO_DS:
4c52953f 4552 sec->has_toc_reloc = 1;
ad8e1ba5
AM
4553 break;
4554
006589cf
AM
4555 /* Marker reloc. */
4556 case R_PPC64_ENTRY:
4557 break;
4558
5bd4f169
AM
4559 /* This relocation describes the C++ object vtable hierarchy.
4560 Reconstruct it for later use during GC. */
4561 case R_PPC64_GNU_VTINHERIT:
c152c796 4562 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4563 return FALSE;
5bd4f169
AM
4564 break;
4565
4566 /* This relocation describes which C++ vtable entries are actually
4567 used. Record for later use during GC. */
4568 case R_PPC64_GNU_VTENTRY:
a0ea3a14 4569 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4570 return FALSE;
5bd4f169
AM
4571 break;
4572
721956f4
AM
4573 case R_PPC64_REL14:
4574 case R_PPC64_REL14_BRTAKEN:
4575 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4576 {
4577 asection *dest = NULL;
4578
4579 /* Heuristic: If jumping outside our section, chances are
4580 we are going to need a stub. */
4581 if (h != NULL)
4582 {
4583 /* If the sym is weak it may be overridden later, so
4584 don't assume we know where a weak sym lives. */
4585 if (h->root.type == bfd_link_hash_defined)
4586 dest = h->root.u.def.section;
4587 }
4588 else
87d72d41
AM
4589 {
4590 Elf_Internal_Sym *isym;
4591
4592 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4593 abfd, r_symndx);
4594 if (isym == NULL)
4595 return FALSE;
4596
4597 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4598 }
4599
220c76dd 4600 if (dest != sec)
7c8fe5c4 4601 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4602 }
3e04d765
AM
4603 goto rel24;
4604
4605 case R_PPC64_PLTCALL:
4606 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
4607 /* Fall through. */
4608
5d1634d7 4609 case R_PPC64_REL24:
05d0e962 4610 case R_PPC64_REL24_NOTOC:
3e04d765 4611 rel24:
cbf95972
AM
4612 plt_list = ifunc;
4613 if (h != NULL)
5d1634d7 4614 {
e054468f
AM
4615 h->needs_plt = 1;
4616 if (h->root.root.string[0] == '.'
4617 && h->root.root.string[1] != '\0')
4618 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 4619
3a71aa26 4620 if (h == tga || h == dottga)
cbf95972
AM
4621 {
4622 sec->has_tls_reloc = 1;
4623 if (rel != relocs
4624 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4625 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4626 /* We have a new-style __tls_get_addr call with
4627 a marker reloc. */
4628 ;
4629 else
4630 /* Mark this section as having an old-style call. */
4631 sec->has_tls_get_addr_call = 1;
4632 }
4633 plt_list = &h->plt.plist;
411e1bfb 4634 }
cbf95972
AM
4635
4636 /* We may need a .plt entry if the function this reloc
4637 refers to is in a shared lib. */
4638 if (plt_list
4639 && !update_plt_info (abfd, plt_list, rel->r_addend))
4640 return FALSE;
411e1bfb
AM
4641 break;
4642
cbf95972
AM
4643 case R_PPC64_ADDR14:
4644 case R_PPC64_ADDR14_BRNTAKEN:
4645 case R_PPC64_ADDR14_BRTAKEN:
4646 case R_PPC64_ADDR24:
4647 goto dodyn;
4648
411e1bfb
AM
4649 case R_PPC64_TPREL64:
4650 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 4651 if (bfd_link_dll (info))
411e1bfb
AM
4652 info->flags |= DF_STATIC_TLS;
4653 goto dotlstoc;
4654
4655 case R_PPC64_DTPMOD64:
4656 if (rel + 1 < rel_end
4657 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4658 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 4659 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 4660 else
951fd09b 4661 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
4662 goto dotlstoc;
4663
4664 case R_PPC64_DTPREL64:
4665 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4666 if (rel != relocs
4667 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4668 && rel[-1].r_offset == rel->r_offset - 8)
4669 /* This is the second reloc of a dtpmod, dtprel pair.
4670 Don't mark with TLS_DTPREL. */
4671 goto dodyn;
4672
4673 dotlstoc:
4674 sec->has_tls_reloc = 1;
4675 if (h != NULL)
4676 {
4677 struct ppc_link_hash_entry *eh;
4678 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 4679 eh->tls_mask |= tls_type;
411e1bfb
AM
4680 }
4681 else
4682 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4683 rel->r_addend, tls_type))
4684 return FALSE;
4685
7c8fe5c4
AM
4686 ppc64_sec = ppc64_elf_section_data (sec);
4687 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 4688 {
3a71aa26
AM
4689 bfd_size_type amt;
4690
e7b938ca 4691 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
4692 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4693 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4694 if (ppc64_sec->u.toc.symndx == NULL)
4695 return FALSE;
4696 amt = sec->size * sizeof (bfd_vma) / 8;
4697 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4698 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 4699 return FALSE;
7c8fe5c4
AM
4700 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4701 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
4702 }
4703 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
4704 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
4705 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
4706
4707 /* Mark the second slot of a GD or LD entry.
4708 -1 to indicate GD and -2 to indicate LD. */
4709 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 4710 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 4711 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 4712 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
4713 goto dodyn;
4714
4715 case R_PPC64_TPREL16:
4716 case R_PPC64_TPREL16_LO:
4717 case R_PPC64_TPREL16_HI:
4718 case R_PPC64_TPREL16_HA:
4719 case R_PPC64_TPREL16_DS:
4720 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
4721 case R_PPC64_TPREL16_HIGH:
4722 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
4723 case R_PPC64_TPREL16_HIGHER:
4724 case R_PPC64_TPREL16_HIGHERA:
4725 case R_PPC64_TPREL16_HIGHEST:
4726 case R_PPC64_TPREL16_HIGHESTA:
7c8bbca5
AM
4727 if (bfd_link_dll (info))
4728 info->flags |= DF_STATIC_TLS;
4729 goto dodyn;
5d1634d7 4730
e86ce104 4731 case R_PPC64_ADDR64:
b9399fcf 4732 if (is_opd
1e2f5b6e 4733 && rel + 1 < rel_end
4ce794b7 4734 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 4735 {
8387904d 4736 if (h != NULL)
8c5b4e52 4737 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 4738 }
e86ce104
AM
4739 /* Fall through. */
4740
65f38f15
AM
4741 case R_PPC64_ADDR16:
4742 case R_PPC64_ADDR16_DS:
4743 case R_PPC64_ADDR16_HA:
4744 case R_PPC64_ADDR16_HI:
f9c6b907
AM
4745 case R_PPC64_ADDR16_HIGH:
4746 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
4747 case R_PPC64_ADDR16_HIGHER:
4748 case R_PPC64_ADDR16_HIGHERA:
4749 case R_PPC64_ADDR16_HIGHEST:
4750 case R_PPC64_ADDR16_HIGHESTA:
4751 case R_PPC64_ADDR16_LO:
4752 case R_PPC64_ADDR16_LO_DS:
0e1862bb 4753 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
4754 && rel->r_addend == 0)
4755 {
4756 /* We may need a .plt entry if this reloc refers to a
4757 function in a shared lib. */
4758 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4759 return FALSE;
4760 h->pointer_equality_needed = 1;
4761 }
4762 /* Fall through. */
4763
4764 case R_PPC64_REL30:
4765 case R_PPC64_REL32:
4766 case R_PPC64_REL64:
65f38f15 4767 case R_PPC64_ADDR32:
65f38f15
AM
4768 case R_PPC64_UADDR16:
4769 case R_PPC64_UADDR32:
4770 case R_PPC64_UADDR64:
5bd4f169 4771 case R_PPC64_TOC:
0e1862bb 4772 if (h != NULL && !bfd_link_pic (info))
81848ca0 4773 /* We may need a copy reloc. */
f5385ebf 4774 h->non_got_ref = 1;
81848ca0 4775
41bd81ab 4776 /* Don't propagate .opd relocs. */
b9399fcf 4777 if (NO_OPD_RELOCS && is_opd)
e86ce104 4778 break;
e86ce104 4779
65f38f15
AM
4780 /* If we are creating a shared library, and this is a reloc
4781 against a global symbol, or a non PC relative reloc
4782 against a local symbol, then we need to copy the reloc
4783 into the shared library. However, if we are linking with
4784 -Bsymbolic, we do not need to copy a reloc against a
4785 global symbol which is defined in an object we are
4786 including in the link (i.e., DEF_REGULAR is set). At
4787 this point we have not seen all the input files, so it is
4788 possible that DEF_REGULAR is not set now but will be set
4789 later (it is never cleared). In case of a weak definition,
4790 DEF_REGULAR may be cleared later by a strong definition in
4791 a shared library. We account for that possibility below by
f4656909 4792 storing information in the dyn_relocs field of the hash
65f38f15
AM
4793 table entry. A similar situation occurs when creating
4794 shared libraries and symbol visibility changes render the
4795 symbol local.
4796
4797 If on the other hand, we are creating an executable, we
4798 may need to keep relocations for symbols satisfied by a
4799 dynamic library if we manage to avoid copy relocs for the
4800 symbol. */
411e1bfb 4801 dodyn:
0e1862bb 4802 if ((bfd_link_pic (info)
1d483afe 4803 && (must_be_dyn_reloc (info, r_type)
65f38f15 4804 || (h != NULL
198f1157 4805 && (!SYMBOLIC_BIND (info, h)
65f38f15 4806 || h->root.type == bfd_link_hash_defweak
f5385ebf 4807 || !h->def_regular))))
f4656909 4808 || (ELIMINATE_COPY_RELOCS
0e1862bb 4809 && !bfd_link_pic (info)
65f38f15
AM
4810 && h != NULL
4811 && (h->root.type == bfd_link_hash_defweak
25f23106 4812 || !h->def_regular))
0e1862bb 4813 || (!bfd_link_pic (info)
25f23106 4814 && ifunc != NULL))
5bd4f169 4815 {
65f38f15
AM
4816 /* We must copy these reloc types into the output file.
4817 Create a reloc section in dynobj and make room for
4818 this reloc. */
5bd4f169
AM
4819 if (sreloc == NULL)
4820 {
83bac4b0
NC
4821 sreloc = _bfd_elf_make_dynamic_reloc_section
4822 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 4823
5bd4f169 4824 if (sreloc == NULL)
83bac4b0 4825 return FALSE;
5bd4f169
AM
4826 }
4827
65f38f15
AM
4828 /* If this is a global symbol, we count the number of
4829 relocations we need for this symbol. */
4830 if (h != NULL)
4831 {
19e08130
AM
4832 struct elf_dyn_relocs *p;
4833 struct elf_dyn_relocs **head;
4834
ec338859 4835 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
4836 p = *head;
4837 if (p == NULL || p->sec != sec)
4838 {
4839 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4840 if (p == NULL)
4841 return FALSE;
4842 p->next = *head;
4843 *head = p;
4844 p->sec = sec;
4845 p->count = 0;
4846 p->pc_count = 0;
4847 }
4848 p->count += 1;
4849 if (!must_be_dyn_reloc (info, r_type))
4850 p->pc_count += 1;
65f38f15
AM
4851 }
4852 else
4853 {
ec338859
AM
4854 /* Track dynamic relocs needed for local syms too.
4855 We really need local syms available to do this
4856 easily. Oh well. */
19e08130
AM
4857 struct ppc_dyn_relocs *p;
4858 struct ppc_dyn_relocs **head;
4859 bfd_boolean is_ifunc;
ec338859 4860 asection *s;
6edfbbad 4861 void *vpp;
87d72d41 4862 Elf_Internal_Sym *isym;
6edfbbad 4863
87d72d41
AM
4864 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4865 abfd, r_symndx);
4866 if (isym == NULL)
b34976b6 4867 return FALSE;
ec338859 4868
87d72d41
AM
4869 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4870 if (s == NULL)
4871 s = sec;
4872
6edfbbad 4873 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
4874 head = (struct ppc_dyn_relocs **) vpp;
4875 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
4876 p = *head;
4877 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
4878 p = p->next;
4879 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
4880 {
4881 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4882 if (p == NULL)
4883 return FALSE;
4884 p->next = *head;
4885 *head = p;
4886 p->sec = sec;
4887 p->ifunc = is_ifunc;
4888 p->count = 0;
4889 }
4890 p->count += 1;
ec338859 4891 }
65f38f15 4892 }
5bd4f169 4893 break;
65f38f15
AM
4894
4895 default:
96e0dda4 4896 break;
5bd4f169
AM
4897 }
4898 }
4899
b34976b6 4900 return TRUE;
5bd4f169
AM
4901}
4902
ee67d69a
AM
4903/* Merge backend specific data from an object file to the output
4904 object file when linking. */
4905
4906static bfd_boolean
50e03d47 4907ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 4908{
50e03d47 4909 bfd *obfd = info->output_bfd;
ee67d69a
AM
4910 unsigned long iflags, oflags;
4911
4912 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
4913 return TRUE;
4914
4915 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
4916 return TRUE;
4917
50e03d47 4918 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
4919 return FALSE;
4920
4921 iflags = elf_elfheader (ibfd)->e_flags;
4922 oflags = elf_elfheader (obfd)->e_flags;
4923
f6c7c3e8 4924 if (iflags & ~EF_PPC64_ABI)
ee67d69a 4925 {
4eca0228 4926 _bfd_error_handler
695344c0 4927 /* xgettext:c-format */
871b3ab2 4928 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
4929 bfd_set_error (bfd_error_bad_value);
4930 return FALSE;
4931 }
f6c7c3e8 4932 else if (iflags != oflags && iflags != 0)
ee67d69a 4933 {
4eca0228 4934 _bfd_error_handler
695344c0 4935 /* xgettext:c-format */
871b3ab2 4936 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
4937 ibfd, iflags, oflags);
4938 bfd_set_error (bfd_error_bad_value);
4939 return FALSE;
4940 }
4941
4a91d0ba
AM
4942 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
4943 return FALSE;
005d79fd 4944
ee67d69a 4945 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 4946 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
4947}
4948
4949static bfd_boolean
4950ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
4951{
4952 /* Print normal ELF private data. */
4953 _bfd_elf_print_private_bfd_data (abfd, ptr);
4954
4955 if (elf_elfheader (abfd)->e_flags != 0)
4956 {
4957 FILE *file = ptr;
4958
ee67d69a
AM
4959 fprintf (file, _("private flags = 0x%lx:"),
4960 elf_elfheader (abfd)->e_flags);
4961
4962 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
4963 fprintf (file, _(" [abiv%ld]"),
4964 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
4965 fputc ('\n', file);
4966 }
4967
4968 return TRUE;
4969}
4970
8387904d 4971/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
4972 of the code entry point, and its section, which must be in the same
4973 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
4974
4975static bfd_vma
4976opd_entry_value (asection *opd_sec,
4977 bfd_vma offset,
4978 asection **code_sec,
aef36ac1
AM
4979 bfd_vma *code_off,
4980 bfd_boolean in_code_sec)
8387904d
AM
4981{
4982 bfd *opd_bfd = opd_sec->owner;
8860955f 4983 Elf_Internal_Rela *relocs;
8387904d 4984 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 4985 bfd_vma val;
8387904d 4986
9f296da3
AM
4987 /* No relocs implies we are linking a --just-symbols object, or looking
4988 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
4989 if (opd_sec->reloc_count == 0)
4990 {
729eabd5 4991 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 4992
729eabd5
AM
4993 if (contents == NULL)
4994 {
4995 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
4996 return (bfd_vma) -1;
4997 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
4998 }
ee1e4ede 4999
dbb3fbbb 5000 /* PR 17512: file: 64b9dfbb. */
451dfd38 5001 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5002 return (bfd_vma) -1;
5003
729eabd5 5004 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5005 if (code_sec != NULL)
5006 {
5007 asection *sec, *likely = NULL;
ee1e4ede 5008
aef36ac1 5009 if (in_code_sec)
4b85d634 5010 {
aef36ac1
AM
5011 sec = *code_sec;
5012 if (sec->vma <= val
5013 && val < sec->vma + sec->size)
5014 likely = sec;
5015 else
5016 val = -1;
5017 }
5018 else
5019 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5020 if (sec->vma <= val
5021 && (sec->flags & SEC_LOAD) != 0
5022 && (sec->flags & SEC_ALLOC) != 0)
5023 likely = sec;
5024 if (likely != NULL)
5025 {
5026 *code_sec = likely;
5027 if (code_off != NULL)
5028 *code_off = val - likely->vma;
4b85d634
AM
5029 }
5030 }
aef36ac1 5031 return val;
4b85d634
AM
5032 }
5033
0c8d6e5c 5034 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5035
729eabd5 5036 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5037 if (relocs == NULL)
5038 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
5039 /* PR 17512: file: df8e1fd6. */
5040 if (relocs == NULL)
5041 return (bfd_vma) -1;
645ea6a9 5042
8387904d 5043 /* Go find the opd reloc at the sym address. */
8860955f 5044 lo = relocs;
8387904d 5045 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5046 val = (bfd_vma) -1;
8387904d
AM
5047 while (lo < hi)
5048 {
5049 look = lo + (hi - lo) / 2;
5050 if (look->r_offset < offset)
5051 lo = look + 1;
5052 else if (look->r_offset > offset)
5053 hi = look;
5054 else
5055 {
0ffa91dd
NC
5056 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5057
8387904d
AM
5058 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5059 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5060 {
5061 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5062 asection *sec = NULL;
8387904d 5063
b53dfeb2
AM
5064 if (symndx >= symtab_hdr->sh_info
5065 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5066 {
5067 struct elf_link_hash_entry **sym_hashes;
5068 struct elf_link_hash_entry *rh;
5069
5070 sym_hashes = elf_sym_hashes (opd_bfd);
5071 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5072 if (rh != NULL)
5073 {
5074 rh = elf_follow_link (rh);
bb854a36
AM
5075 if (rh->root.type != bfd_link_hash_defined
5076 && rh->root.type != bfd_link_hash_defweak)
5077 break;
5078 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5079 {
bb854a36
AM
5080 val = rh->root.u.def.value;
5081 sec = rh->root.u.def.section;
b53dfeb2
AM
5082 }
5083 }
5084 }
5085
5086 if (sec == NULL)
5087 {
5088 Elf_Internal_Sym *sym;
5089
5090 if (symndx < symtab_hdr->sh_info)
5091 {
5092 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5093 if (sym == NULL)
5094 {
5095 size_t symcnt = symtab_hdr->sh_info;
5096 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5097 symcnt, 0,
5098 NULL, NULL, NULL);
5099 if (sym == NULL)
5100 break;
5101 symtab_hdr->contents = (bfd_byte *) sym;
5102 }
5103 sym += symndx;
128205bb
AM
5104 }
5105 else
5106 {
b53dfeb2
AM
5107 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5108 1, symndx,
5109 NULL, NULL, NULL);
128205bb
AM
5110 if (sym == NULL)
5111 break;
128205bb 5112 }
b53dfeb2
AM
5113 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5114 if (sec == NULL)
5115 break;
5116 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5117 val = sym->st_value;
8387904d 5118 }
b53dfeb2 5119
8387904d
AM
5120 val += look->r_addend;
5121 if (code_off != NULL)
5122 *code_off = val;
5123 if (code_sec != NULL)
aef36ac1
AM
5124 {
5125 if (in_code_sec && *code_sec != sec)
5126 return -1;
5127 else
5128 *code_sec = sec;
5129 }
b53dfeb2 5130 if (sec->output_section != NULL)
8387904d 5131 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5132 }
5133 break;
5134 }
5135 }
645ea6a9 5136
645ea6a9 5137 return val;
8387904d
AM
5138}
5139
aef36ac1
AM
5140/* If the ELF symbol SYM might be a function in SEC, return the
5141 function size and set *CODE_OFF to the function's entry point,
5142 otherwise return zero. */
9f296da3 5143
aef36ac1
AM
5144static bfd_size_type
5145ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5146 bfd_vma *code_off)
9f296da3 5147{
aef36ac1
AM
5148 bfd_size_type size;
5149
5150 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5151 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5152 return 0;
5153
5154 size = 0;
5155 if (!(sym->flags & BSF_SYNTHETIC))
5156 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5157
5158 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5159 {
b07bca4e
AM
5160 struct _opd_sec_data *opd = get_opd_info (sym->section);
5161 bfd_vma symval = sym->value;
5162
5163 if (opd != NULL
5164 && opd->adjust != NULL
5165 && elf_section_data (sym->section)->relocs != NULL)
5166 {
5167 /* opd_entry_value will use cached relocs that have been
5168 adjusted, but with raw symbols. That means both local
5169 and global symbols need adjusting. */
5170 long adjust = opd->adjust[OPD_NDX (symval)];
5171 if (adjust == -1)
5172 return 0;
5173 symval += adjust;
5174 }
5175
5176 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
5177 &sec, code_off, TRUE) == (bfd_vma) -1)
5178 return 0;
5179 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5180 symbol. This size has nothing to do with the code size of the
5181 function, which is what we're supposed to return, but the
5182 code size isn't available without looking up the dot-sym.
5183 However, doing that would be a waste of time particularly
5184 since elf_find_function will look at the dot-sym anyway.
5185 Now, elf_find_function will keep the largest size of any
5186 function sym found at the code address of interest, so return
5187 1 here to avoid it incorrectly caching a larger function size
5188 for a small function. This does mean we return the wrong
5189 size for a new-ABI function of size 24, but all that does is
5190 disable caching for such functions. */
5191 if (size == 24)
5192 size = 1;
9f296da3 5193 }
aef36ac1
AM
5194 else
5195 {
5196 if (sym->section != sec)
5197 return 0;
5198 *code_off = sym->value;
5199 }
5200 if (size == 0)
5201 size = 1;
5202 return size;
9f296da3
AM
5203}
5204
f378ab09
AM
5205/* Return true if symbol is a strong function defined in an ELFv2
5206 object with st_other localentry bits of zero, ie. its local entry
5207 point coincides with its global entry point. */
5208
5209static bfd_boolean
5210is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5211{
5212 return (h != NULL
5213 && h->type == STT_FUNC
5214 && h->root.type == bfd_link_hash_defined
5215 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 5216 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
5217 && is_ppc64_elf (h->root.u.def.section->owner)
5218 && abiversion (h->root.u.def.section->owner) >= 2);
5219}
5220
854b41e7
AM
5221/* Return true if symbol is defined in a regular object file. */
5222
5223static bfd_boolean
5224is_static_defined (struct elf_link_hash_entry *h)
5225{
5226 return ((h->root.type == bfd_link_hash_defined
5227 || h->root.type == bfd_link_hash_defweak)
5228 && h->root.u.def.section != NULL
5229 && h->root.u.def.section->output_section != NULL);
5230}
5231
b31867b6
AM
5232/* If FDH is a function descriptor symbol, return the associated code
5233 entry symbol if it is defined. Return NULL otherwise. */
5234
5235static struct ppc_link_hash_entry *
5236defined_code_entry (struct ppc_link_hash_entry *fdh)
5237{
5238 if (fdh->is_func_descriptor)
5239 {
5240 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5241 if (fh->elf.root.type == bfd_link_hash_defined
5242 || fh->elf.root.type == bfd_link_hash_defweak)
5243 return fh;
5244 }
5245 return NULL;
5246}
5247
5248/* If FH is a function code entry symbol, return the associated
5249 function descriptor symbol if it is defined. Return NULL otherwise. */
5250
5251static struct ppc_link_hash_entry *
5252defined_func_desc (struct ppc_link_hash_entry *fh)
5253{
5254 if (fh->oh != NULL
5255 && fh->oh->is_func_descriptor)
5256 {
5257 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5258 if (fdh->elf.root.type == bfd_link_hash_defined
5259 || fdh->elf.root.type == bfd_link_hash_defweak)
5260 return fdh;
5261 }
5262 return NULL;
5263}
5264
8c5b4e52
AM
5265static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5266
5267/* Garbage collect sections, after first dealing with dot-symbols. */
5268
5269static bfd_boolean
5270ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5271{
5272 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5273
5274 if (htab != NULL && htab->need_func_desc_adj)
5275 {
5276 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5277 htab->need_func_desc_adj = 0;
5278 }
5279 return bfd_elf_gc_sections (abfd, info);
5280}
5281
74f0fb50
AM
5282/* Mark all our entry sym sections, both opd and code section. */
5283
5284static void
5285ppc64_elf_gc_keep (struct bfd_link_info *info)
5286{
5287 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5288 struct bfd_sym_chain *sym;
5289
4dfe6ac6
NC
5290 if (htab == NULL)
5291 return;
5292
74f0fb50
AM
5293 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5294 {
b31867b6 5295 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5296 asection *sec;
5297
5298 eh = (struct ppc_link_hash_entry *)
b31867b6 5299 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5300 if (eh == NULL)
5301 continue;
5302 if (eh->elf.root.type != bfd_link_hash_defined
5303 && eh->elf.root.type != bfd_link_hash_defweak)
5304 continue;
5305
b31867b6
AM
5306 fh = defined_code_entry (eh);
5307 if (fh != NULL)
74f0fb50 5308 {
b31867b6 5309 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5310 sec->flags |= SEC_KEEP;
5311 }
5312 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5313 && opd_entry_value (eh->elf.root.u.def.section,
5314 eh->elf.root.u.def.value,
aef36ac1 5315 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
5316 sec->flags |= SEC_KEEP;
5317
5318 sec = eh->elf.root.u.def.section;
5319 sec->flags |= SEC_KEEP;
5320 }
5321}
5322
64d03ab5
AM
5323/* Mark sections containing dynamically referenced symbols. When
5324 building shared libraries, we must assume that any visible symbol is
5325 referenced. */
5326
5327static bfd_boolean
5328ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5329{
5330 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5331 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5332 struct ppc_link_hash_entry *fdh;
b407645f 5333 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5334
64d03ab5 5335 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5336 fdh = defined_func_desc (eh);
5337 if (fdh != NULL)
5338 eh = fdh;
64d03ab5
AM
5339
5340 if ((eh->elf.root.type == bfd_link_hash_defined
5341 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 5342 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5343 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5344 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5345 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5346 && (!bfd_link_executable (info)
e278ae05 5347 || info->gc_keep_exported
b407645f
AM
5348 || info->export_dynamic
5349 || (eh->elf.dynamic
5350 && d != NULL
2cdcc330
AM
5351 && (*d->match) (&d->head, NULL,
5352 eh->elf.root.root.string)))
e278ae05 5353 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5354 || !bfd_hide_sym_by_version (info->version_info,
5355 eh->elf.root.root.string)))))
64d03ab5
AM
5356 {
5357 asection *code_sec;
b31867b6 5358 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5359
5360 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5361
5362 /* Function descriptor syms cause the associated
5363 function code sym section to be marked. */
b31867b6
AM
5364 fh = defined_code_entry (eh);
5365 if (fh != NULL)
5366 {
5367 code_sec = fh->elf.root.u.def.section;
5368 code_sec->flags |= SEC_KEEP;
5369 }
64d03ab5
AM
5370 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5371 && opd_entry_value (eh->elf.root.u.def.section,
5372 eh->elf.root.u.def.value,
aef36ac1 5373 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
5374 code_sec->flags |= SEC_KEEP;
5375 }
5376
5377 return TRUE;
5378}
5379
5bd4f169
AM
5380/* Return the section that should be marked against GC for a given
5381 relocation. */
5382
5383static asection *
4ce794b7 5384ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5385 struct bfd_link_info *info,
4ce794b7
AM
5386 Elf_Internal_Rela *rel,
5387 struct elf_link_hash_entry *h,
5388 Elf_Internal_Sym *sym)
5bd4f169 5389{
ccfa59ea
AM
5390 asection *rsec;
5391
ccfa59ea
AM
5392 /* Syms return NULL if we're marking .opd, so we avoid marking all
5393 function sections, as all functions are referenced in .opd. */
5394 rsec = NULL;
5395 if (get_opd_info (sec) != NULL)
5396 return rsec;
1e2f5b6e 5397
5bd4f169
AM
5398 if (h != NULL)
5399 {
04c9666a 5400 enum elf_ppc64_reloc_type r_type;
b31867b6 5401 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5402
4ce794b7 5403 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5404 switch (r_type)
5bd4f169
AM
5405 {
5406 case R_PPC64_GNU_VTINHERIT:
5407 case R_PPC64_GNU_VTENTRY:
5408 break;
5409
5410 default:
5411 switch (h->root.type)
5412 {
5413 case bfd_link_hash_defined:
5414 case bfd_link_hash_defweak:
ccfa59ea 5415 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5416 fdh = defined_func_desc (eh);
5417 if (fdh != NULL)
8c5b4e52
AM
5418 {
5419 /* -mcall-aixdesc code references the dot-symbol on
5420 a call reloc. Mark the function descriptor too
5421 against garbage collection. */
5422 fdh->elf.mark = 1;
60d67dc8
AM
5423 if (fdh->elf.is_weakalias)
5424 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
5425 eh = fdh;
5426 }
1e2f5b6e
AM
5427
5428 /* Function descriptor syms cause the associated
5429 function code sym section to be marked. */
b31867b6
AM
5430 fh = defined_code_entry (eh);
5431 if (fh != NULL)
ccfa59ea
AM
5432 {
5433 /* They also mark their opd section. */
74f0fb50 5434 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5435
b31867b6 5436 rsec = fh->elf.root.u.def.section;
ccfa59ea 5437 }
8387904d
AM
5438 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5439 && opd_entry_value (eh->elf.root.u.def.section,
5440 eh->elf.root.u.def.value,
aef36ac1 5441 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 5442 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5443 else
1e2f5b6e
AM
5444 rsec = h->root.u.def.section;
5445 break;
5bd4f169
AM
5446
5447 case bfd_link_hash_common:
1e2f5b6e
AM
5448 rsec = h->root.u.c.p->section;
5449 break;
5bd4f169
AM
5450
5451 default:
fb34365b 5452 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5453 }
5454 }
5455 }
5456 else
5457 {
74f0fb50 5458 struct _opd_sec_data *opd;
1e2f5b6e
AM
5459
5460 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5461 opd = get_opd_info (rsec);
5462 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5463 {
74f0fb50 5464 rsec->gc_mark = 1;
ccfa59ea 5465
51aecdc5 5466 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 5467 }
5bd4f169
AM
5468 }
5469
1e2f5b6e 5470 return rsec;
5bd4f169
AM
5471}
5472
deb0e272
AM
5473/* The maximum size of .sfpr. */
5474#define SFPR_MAX (218*4)
5475
5476struct sfpr_def_parms
5477{
699733f6
AM
5478 const char name[12];
5479 unsigned char lo, hi;
2cdcc330
AM
5480 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5481 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
5482};
5483
a4b6fadd
AM
5484/* Auto-generate _save*, _rest* functions in .sfpr.
5485 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5486 instead. */
deb0e272 5487
4dfe6ac6 5488static bfd_boolean
a4b6fadd
AM
5489sfpr_define (struct bfd_link_info *info,
5490 const struct sfpr_def_parms *parm,
5491 asection *stub_sec)
deb0e272
AM
5492{
5493 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5494 unsigned int i;
5495 size_t len = strlen (parm->name);
5496 bfd_boolean writing = FALSE;
699733f6 5497 char sym[16];
deb0e272 5498
4dfe6ac6
NC
5499 if (htab == NULL)
5500 return FALSE;
5501
deb0e272
AM
5502 memcpy (sym, parm->name, len);
5503 sym[len + 2] = 0;
5504
5505 for (i = parm->lo; i <= parm->hi; i++)
5506 {
a4b6fadd 5507 struct ppc_link_hash_entry *h;
deb0e272
AM
5508
5509 sym[len + 0] = i / 10 + '0';
5510 sym[len + 1] = i % 10 + '0';
a4b6fadd 5511 h = (struct ppc_link_hash_entry *)
b32547cd 5512 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 5513 if (stub_sec != NULL)
deb0e272 5514 {
a4b6fadd
AM
5515 if (h != NULL
5516 && h->elf.root.type == bfd_link_hash_defined
5517 && h->elf.root.u.def.section == htab->sfpr)
5518 {
5519 struct elf_link_hash_entry *s;
5520 char buf[32];
5521 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5522 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5523 if (s == NULL)
5524 return FALSE;
5525 if (s->root.type == bfd_link_hash_new
5526 || (s->root.type = bfd_link_hash_defined
5527 && s->root.u.def.section == stub_sec))
5528 {
5529 s->root.type = bfd_link_hash_defined;
5530 s->root.u.def.section = stub_sec;
7dda8d3c 5531 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
5532 + h->elf.root.u.def.value);
5533 s->ref_regular = 1;
5534 s->def_regular = 1;
5535 s->ref_regular_nonweak = 1;
5536 s->forced_local = 1;
5537 s->non_elf = 0;
5538 s->root.linker_def = 1;
5539 }
5540 }
5541 continue;
5542 }
5543 if (h != NULL)
5544 {
5545 h->save_res = 1;
5546 if (!h->elf.def_regular)
deb0e272 5547 {
a4b6fadd
AM
5548 h->elf.root.type = bfd_link_hash_defined;
5549 h->elf.root.u.def.section = htab->sfpr;
5550 h->elf.root.u.def.value = htab->sfpr->size;
5551 h->elf.type = STT_FUNC;
5552 h->elf.def_regular = 1;
b32547cd 5553 h->elf.non_elf = 0;
a4b6fadd
AM
5554 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5555 writing = TRUE;
deb0e272 5556 if (htab->sfpr->contents == NULL)
a4b6fadd 5557 {
2cdcc330
AM
5558 htab->sfpr->contents
5559 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd
AM
5560 if (htab->sfpr->contents == NULL)
5561 return FALSE;
5562 }
deb0e272
AM
5563 }
5564 }
5565 if (writing)
5566 {
5567 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5568 if (i != parm->hi)
5569 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5570 else
5571 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5572 htab->sfpr->size = p - htab->sfpr->contents;
5573 }
5574 }
5575
5576 return TRUE;
5577}
5578
5579static bfd_byte *
5580savegpr0 (bfd *abfd, bfd_byte *p, int r)
5581{
5582 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5583 return p + 4;
5584}
5585
5586static bfd_byte *
5587savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5588{
5589 p = savegpr0 (abfd, p, r);
a078d95a 5590 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5591 p = p + 4;
5592 bfd_put_32 (abfd, BLR, p);
5593 return p + 4;
5594}
5595
5596static bfd_byte *
5597restgpr0 (bfd *abfd, bfd_byte *p, int r)
5598{
5599 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5600 return p + 4;
5601}
5602
5603static bfd_byte *
5604restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5605{
a078d95a 5606 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5607 p = p + 4;
5608 p = restgpr0 (abfd, p, r);
5609 bfd_put_32 (abfd, MTLR_R0, p);
5610 p = p + 4;
5611 if (r == 29)
5612 {
5613 p = restgpr0 (abfd, p, 30);
5614 p = restgpr0 (abfd, p, 31);
5615 }
5616 bfd_put_32 (abfd, BLR, p);
5617 return p + 4;
5618}
5619
5620static bfd_byte *
5621savegpr1 (bfd *abfd, bfd_byte *p, int r)
5622{
5623 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5624 return p + 4;
5625}
5626
5627static bfd_byte *
5628savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5629{
5630 p = savegpr1 (abfd, p, r);
5631 bfd_put_32 (abfd, BLR, p);
5632 return p + 4;
5633}
5634
5635static bfd_byte *
5636restgpr1 (bfd *abfd, bfd_byte *p, int r)
5637{
5638 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5639 return p + 4;
5640}
5641
5642static bfd_byte *
5643restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5644{
5645 p = restgpr1 (abfd, p, r);
5646 bfd_put_32 (abfd, BLR, p);
5647 return p + 4;
5648}
5649
5650static bfd_byte *
5651savefpr (bfd *abfd, bfd_byte *p, int r)
5652{
5653 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5654 return p + 4;
5655}
5656
5657static bfd_byte *
5658savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5659{
5660 p = savefpr (abfd, p, r);
a078d95a 5661 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5662 p = p + 4;
5663 bfd_put_32 (abfd, BLR, p);
5664 return p + 4;
5665}
5666
5667static bfd_byte *
5668restfpr (bfd *abfd, bfd_byte *p, int r)
5669{
5670 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5671 return p + 4;
5672}
5673
5674static bfd_byte *
5675restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5676{
a078d95a 5677 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5678 p = p + 4;
5679 p = restfpr (abfd, p, r);
5680 bfd_put_32 (abfd, MTLR_R0, p);
5681 p = p + 4;
5682 if (r == 29)
5683 {
5684 p = restfpr (abfd, p, 30);
5685 p = restfpr (abfd, p, 31);
5686 }
5687 bfd_put_32 (abfd, BLR, p);
5688 return p + 4;
5689}
5690
5691static bfd_byte *
5692savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5693{
5694 p = savefpr (abfd, p, r);
5695 bfd_put_32 (abfd, BLR, p);
5696 return p + 4;
5697}
5698
5699static bfd_byte *
5700restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5701{
5702 p = restfpr (abfd, p, r);
5703 bfd_put_32 (abfd, BLR, p);
5704 return p + 4;
5705}
5706
5707static bfd_byte *
5708savevr (bfd *abfd, bfd_byte *p, int r)
5709{
5710 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5711 p = p + 4;
5712 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5713 return p + 4;
5714}
5715
5716static bfd_byte *
5717savevr_tail (bfd *abfd, bfd_byte *p, int r)
5718{
5719 p = savevr (abfd, p, r);
5720 bfd_put_32 (abfd, BLR, p);
5721 return p + 4;
5722}
5723
5724static bfd_byte *
5725restvr (bfd *abfd, bfd_byte *p, int r)
5726{
5727 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5728 p = p + 4;
5729 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5730 return p + 4;
5731}
5732
5733static bfd_byte *
5734restvr_tail (bfd *abfd, bfd_byte *p, int r)
5735{
5736 p = restvr (abfd, p, r);
5737 bfd_put_32 (abfd, BLR, p);
5738 return p + 4;
5739}
5740
e86ce104
AM
5741/* Called via elf_link_hash_traverse to transfer dynamic linking
5742 information on function code symbol entries to their corresponding
5743 function descriptor symbol entries. */
deb0e272 5744
b34976b6 5745static bfd_boolean
4ce794b7 5746func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 5747{
e86ce104 5748 struct bfd_link_info *info;
65f38f15 5749 struct ppc_link_hash_table *htab;
50bc7936
AM
5750 struct ppc_link_hash_entry *fh;
5751 struct ppc_link_hash_entry *fdh;
5752 bfd_boolean force_local;
5bd4f169 5753
50bc7936
AM
5754 fh = (struct ppc_link_hash_entry *) h;
5755 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 5756 return TRUE;
e86ce104 5757
8c5b4e52
AM
5758 if (!fh->is_func)
5759 return TRUE;
5760
5761 if (fh->elf.root.root.string[0] != '.'
5762 || fh->elf.root.root.string[1] == '\0')
5763 return TRUE;
5764
4ce794b7 5765 info = inf;
65f38f15 5766 htab = ppc_hash_table (info);
4dfe6ac6
NC
5767 if (htab == NULL)
5768 return FALSE;
5bd4f169 5769
8c5b4e52
AM
5770 /* Find the corresponding function descriptor symbol. */
5771 fdh = lookup_fdh (fh, htab);
5772
c09bdfe5
AM
5773 /* Resolve undefined references to dot-symbols as the value
5774 in the function descriptor, if we have one in a regular object.
5775 This is to satisfy cases like ".quad .foo". Calls to functions
5776 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
5777 if ((fh->elf.root.type == bfd_link_hash_undefined
5778 || fh->elf.root.type == bfd_link_hash_undefweak)
5779 && (fdh->elf.root.type == bfd_link_hash_defined
5780 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
5781 && get_opd_info (fdh->elf.root.u.def.section) != NULL
5782 && opd_entry_value (fdh->elf.root.u.def.section,
5783 fdh->elf.root.u.def.value,
c09bdfe5 5784 &fh->elf.root.u.def.section,
aef36ac1 5785 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 5786 {
b31867b6 5787 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 5788 fh->elf.forced_local = 1;
b31867b6
AM
5789 fh->elf.def_regular = fdh->elf.def_regular;
5790 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
5791 }
5792
8c5b4e52
AM
5793 if (!fh->elf.dynamic)
5794 {
5795 struct plt_entry *ent;
5bd4f169 5796
8c5b4e52
AM
5797 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5798 if (ent->plt.refcount > 0)
5799 break;
5800 if (ent == NULL)
5801 return TRUE;
5802 }
5bd4f169 5803
8c5b4e52 5804 /* Create a descriptor as undefined if necessary. */
50bc7936 5805 if (fdh == NULL
0e1862bb 5806 && !bfd_link_executable (info)
50bc7936
AM
5807 && (fh->elf.root.type == bfd_link_hash_undefined
5808 || fh->elf.root.type == bfd_link_hash_undefweak))
5809 {
908b32fc 5810 fdh = make_fdh (info, fh);
bb700d78
AM
5811 if (fdh == NULL)
5812 return FALSE;
50bc7936 5813 }
648cca2c 5814
8c5b4e52 5815 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
5816 if (fdh != NULL
5817 && fdh->fake
8c5b4e52
AM
5818 && (fh->elf.root.type == bfd_link_hash_defined
5819 || fh->elf.root.type == bfd_link_hash_defweak))
5820 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 5821
8c5b4e52
AM
5822 /* Transfer dynamic linking information to the function descriptor. */
5823 if (fdh != NULL)
5824 {
f5385ebf
AM
5825 fdh->elf.ref_regular |= fh->elf.ref_regular;
5826 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5827 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5828 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
5829 fdh->elf.dynamic |= fh->elf.dynamic;
5830 fdh->elf.needs_plt |= (fh->elf.needs_plt
5831 || fh->elf.type == STT_FUNC
5832 || fh->elf.type == STT_GNU_IFUNC);
5833 move_plt_plist (fh, fdh);
5834
5835 if (!fdh->elf.forced_local
5836 && fh->elf.dynindx != -1)
5837 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5838 return FALSE;
e86ce104
AM
5839 }
5840
50bc7936
AM
5841 /* Now that the info is on the function descriptor, clear the
5842 function code sym info. Any function code syms for which we
5843 don't have a definition in a regular file, we force local.
5844 This prevents a shared library from exporting syms that have
5845 been imported from another library. Function code syms that
5846 are really in the library we must leave global to prevent the
5847 linker dragging in a definition from a static library. */
93f3fa99
AM
5848 force_local = (!fh->elf.def_regular
5849 || fdh == NULL
5850 || !fdh->elf.def_regular
5851 || fdh->elf.forced_local);
50bc7936
AM
5852 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5853
b34976b6 5854 return TRUE;
e86ce104 5855}
40b8271b 5856
a4b6fadd
AM
5857static const struct sfpr_def_parms save_res_funcs[] =
5858 {
5859 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5860 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5861 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5862 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5863 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5864 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5865 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5866 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5867 { "._savef", 14, 31, savefpr, savefpr1_tail },
5868 { "._restf", 14, 31, restfpr, restfpr1_tail },
5869 { "_savevr_", 20, 31, savevr, savevr_tail },
5870 { "_restvr_", 20, 31, restvr, restvr_tail }
5871 };
5872
e86ce104 5873/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
5874 this hook to a) provide some gcc support functions, and b) transfer
5875 dynamic linking information gathered so far on function code symbol
5876 entries, to their corresponding function descriptor symbol entries. */
deb0e272 5877
b34976b6 5878static bfd_boolean
4ce794b7
AM
5879ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5880 struct bfd_link_info *info)
e86ce104
AM
5881{
5882 struct ppc_link_hash_table *htab;
5883
5884 htab = ppc_hash_table (info);
4dfe6ac6
NC
5885 if (htab == NULL)
5886 return FALSE;
5887
b32547cd
AM
5888 /* Provide any missing _save* and _rest* functions. */
5889 if (htab->sfpr != NULL)
5890 {
5891 unsigned int i;
5892
5893 htab->sfpr->size = 0;
5894 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
5895 if (!sfpr_define (info, &save_res_funcs[i], NULL))
5896 return FALSE;
5897 if (htab->sfpr->size == 0)
5898 htab->sfpr->flags |= SEC_EXCLUDE;
5899 }
5900
5901 if (bfd_link_relocatable (info))
5902 return TRUE;
5903
5904 if (htab->elf.hgot != NULL)
dba6fa9b
AM
5905 {
5906 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
5907 /* Make .TOC. defined so as to prevent it being made dynamic.
5908 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
5909 if (!htab->elf.hgot->def_regular
5910 || htab->elf.hgot->root.type != bfd_link_hash_defined)
5911 {
5912 htab->elf.hgot->root.type = bfd_link_hash_defined;
5913 htab->elf.hgot->root.u.def.value = 0;
5914 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
5915 htab->elf.hgot->def_regular = 1;
5916 htab->elf.hgot->root.linker_def = 1;
5917 }
dba6fa9b 5918 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
5919 htab->elf.hgot->other
5920 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 5921 }
c66bb0ee 5922
8c5b4e52
AM
5923 if (htab->need_func_desc_adj)
5924 {
5925 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5926 htab->need_func_desc_adj = 0;
5927 }
805fc799 5928
b34976b6 5929 return TRUE;
e86ce104
AM
5930}
5931
98bbb1b8 5932/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 5933
98bbb1b8 5934static asection *
a345bc8d
AM
5935readonly_dynrelocs (struct elf_link_hash_entry *h)
5936{
5937 struct ppc_link_hash_entry *eh;
5938 struct elf_dyn_relocs *p;
5939
5940 eh = (struct ppc_link_hash_entry *) h;
5941 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5942 {
5943 asection *s = p->sec->output_section;
5944
5945 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 5946 return p->sec;
a345bc8d 5947 }
98bbb1b8 5948 return NULL;
a345bc8d
AM
5949}
5950
d311bc8b 5951/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
5952 aliases, that apply to read-only sections. Cannot be used after
5953 size_dynamic_sections. */
d311bc8b
AM
5954
5955static bfd_boolean
5956alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
5957{
5958 struct ppc_link_hash_entry *eh;
5959
5960 eh = (struct ppc_link_hash_entry *) h;
5961 do
5962 {
5963 if (readonly_dynrelocs (&eh->elf))
5964 return TRUE;
ab2477e1 5965 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
2cdcc330
AM
5966 }
5967 while (eh != NULL && &eh->elf != h);
d311bc8b
AM
5968
5969 return FALSE;
5970}
8a2058b5 5971
8a9e8e72
AM
5972/* Return whether EH has pc-relative dynamic relocs. */
5973
5974static bfd_boolean
5975pc_dynrelocs (struct ppc_link_hash_entry *eh)
5976{
5977 struct elf_dyn_relocs *p;
5978
5979 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5980 if (p->pc_count != 0)
5981 return TRUE;
5982 return FALSE;
5983}
5984
8a2058b5
AM
5985/* Return true if a global entry stub will be created for H. Valid
5986 for ELFv2 before plt entries have been allocated. */
5987
5988static bfd_boolean
5989global_entry_stub (struct elf_link_hash_entry *h)
5990{
5991 struct plt_entry *pent;
5992
5993 if (!h->pointer_equality_needed
5994 || h->def_regular)
5995 return FALSE;
5996
5997 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
5998 if (pent->plt.refcount > 0
5999 && pent->addend == 0)
6000 return TRUE;
6001
6002 return FALSE;
6003}
6004
e86ce104
AM
6005/* Adjust a symbol defined by a dynamic object and referenced by a
6006 regular object. The current definition is in some section of the
6007 dynamic object, but we're not including those sections. We have to
6008 change the definition to something the rest of the link can
6009 understand. */
6010
b34976b6 6011static bfd_boolean
4ce794b7
AM
6012ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6013 struct elf_link_hash_entry *h)
e86ce104
AM
6014{
6015 struct ppc_link_hash_table *htab;
5474d94f 6016 asection *s, *srel;
e86ce104
AM
6017
6018 htab = ppc_hash_table (info);
4dfe6ac6
NC
6019 if (htab == NULL)
6020 return FALSE;
e86ce104
AM
6021
6022 /* Deal with function syms. */
6023 if (h->type == STT_FUNC
e054468f 6024 || h->type == STT_GNU_IFUNC
f5385ebf 6025 || h->needs_plt)
e86ce104 6026 {
529fe20e
AM
6027 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6028 || SYMBOL_CALLS_LOCAL (info, h)
6029 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6030 /* Discard dyn_relocs when non-pic if we've decided that a
6031 function symbol is local and not an ifunc. We keep dynamic
6032 relocs for ifuncs when local rather than always emitting a
6033 plt call stub for them and defining the symbol on the call
6034 stub. We can't do that for ELFv1 anyway (a function symbol
6035 is defined on a descriptor, not code) and it can be faster at
6036 run-time due to not needing to bounce through a stub. The
6037 dyn_relocs for ifuncs will be applied even in a static
6038 executable. */
6039 if (!bfd_link_pic (info)
6040 && h->type != STT_GNU_IFUNC
6041 && local)
6042 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6043
e86ce104
AM
6044 /* Clear procedure linkage table information for any symbol that
6045 won't need a .plt entry. */
411e1bfb
AM
6046 struct plt_entry *ent;
6047 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6048 if (ent->plt.refcount > 0)
6049 break;
8387904d 6050 if (ent == NULL
2d7ad24e
AM
6051 || (h->type != STT_GNU_IFUNC
6052 && local
3e04d765
AM
6053 && (htab->can_convert_all_inline_plt
6054 || (((struct ppc_link_hash_entry *) h)->tls_mask
6055 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6056 {
411e1bfb 6057 h->plt.plist = NULL;
f5385ebf 6058 h->needs_plt = 0;
d1eca1e4 6059 h->pointer_equality_needed = 0;
40b8271b 6060 }
8a2058b5 6061 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6062 {
d1eca1e4
AM
6063 /* Taking a function's address in a read/write section
6064 doesn't require us to define the function symbol in the
6065 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6066 be used instead. The reason we prefer a few more dynamic
6067 relocs is that calling via a global entry stub costs a
6068 few more instructions, and pointer_equality_needed causes
6069 extra work in ld.so when resolving these symbols. */
529fe20e 6070 if (global_entry_stub (h))
d1eca1e4 6071 {
ab2477e1 6072 if (!readonly_dynrelocs (h))
529fe20e
AM
6073 {
6074 h->pointer_equality_needed = 0;
04383fd1
AM
6075 /* If we haven't seen a branch reloc and the symbol
6076 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6077 if (!h->needs_plt)
6078 h->plt.plist = NULL;
6079 }
6080 else if (!bfd_link_pic (info))
6081 /* We are going to be defining the function symbol on the
6082 plt stub, so no dyn_relocs needed when non-pic. */
6083 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
6084 }
6085
3988aed5
AM
6086 /* ELFv2 function symbols can't have copy relocs. */
6087 return TRUE;
6088 }
6089 else if (!h->needs_plt
ab2477e1 6090 && !readonly_dynrelocs (h))
3988aed5 6091 {
04383fd1
AM
6092 /* If we haven't seen a branch reloc and the symbol isn't an
6093 ifunc then we don't need a plt entry. */
3988aed5
AM
6094 h->plt.plist = NULL;
6095 h->pointer_equality_needed = 0;
a345bc8d
AM
6096 return TRUE;
6097 }
5bd4f169 6098 }
bbd7ec4a 6099 else
411e1bfb 6100 h->plt.plist = NULL;
5bd4f169
AM
6101
6102 /* If this is a weak symbol, and there is a real definition, the
6103 processor independent code will have arranged for us to see the
6104 real definition first, and we can just use the same value. */
60d67dc8 6105 if (h->is_weakalias)
5bd4f169 6106 {
60d67dc8
AM
6107 struct elf_link_hash_entry *def = weakdef (h);
6108 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6109 h->root.u.def.section = def->root.u.def.section;
6110 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6111 if (def->root.u.def.section == htab->elf.sdynbss
6112 || def->root.u.def.section == htab->elf.sdynrelro)
6113 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 6114 return TRUE;
5bd4f169
AM
6115 }
6116
5bd4f169
AM
6117 /* If we are creating a shared library, we must presume that the
6118 only references to the symbol are via the global offset table.
6119 For such cases we need not do anything here; the relocations will
6120 be handled correctly by relocate_section. */
0e1862bb 6121 if (bfd_link_pic (info))
b34976b6 6122 return TRUE;
5bd4f169 6123
65f38f15
AM
6124 /* If there are no references to this symbol that do not use the
6125 GOT, we don't need to generate a copy reloc. */
f5385ebf 6126 if (!h->non_got_ref)
b34976b6 6127 return TRUE;
65f38f15 6128
b186458a 6129 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6130 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6131
d93d1c80
AM
6132 /* If -z nocopyreloc was given, don't generate them either. */
6133 || info->nocopyreloc
a127494f 6134
dce2246a 6135 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6136 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 6137 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 6138
d93d1c80
AM
6139 /* Protected variables do not work with .dynbss. The copy in
6140 .dynbss won't be used by the shared library with the protected
6141 definition for the variable. Text relocations are preferable
6142 to an incorrect program. */
6143 || h->protected_def)
529fe20e 6144 return TRUE;
a127494f 6145
5d35169e 6146 if (h->plt.plist != NULL)
97b639ba
AM
6147 {
6148 /* We should never get here, but unfortunately there are versions
6149 of gcc out there that improperly (for this ABI) put initialized
6150 function pointers, vtable refs and suchlike in read-only
6151 sections. Allow them to proceed, but warn that this might
6152 break at runtime. */
25f53a85 6153 info->callbacks->einfo
c1c8c1ef 6154 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6155 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6156 h->root.root.string);
6157 }
5d35169e
AM
6158
6159 /* This is a reference to a symbol defined by a dynamic object which
6160 is not a function. */
6161
5bd4f169
AM
6162 /* We must allocate the symbol in our .dynbss section, which will
6163 become part of the .bss section of the executable. There will be
6164 an entry for this symbol in the .dynsym section. The dynamic
6165 object will contain position independent code, so all references
6166 from the dynamic object to this symbol will go through the global
6167 offset table. The dynamic linker will use the .dynsym entry to
6168 determine the address it must put in the global offset table, so
6169 both the dynamic object and the regular object will refer to the
6170 same memory location for the variable. */
5474d94f
AM
6171 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6172 {
6173 s = htab->elf.sdynrelro;
6174 srel = htab->elf.sreldynrelro;
6175 }
6176 else
6177 {
6178 s = htab->elf.sdynbss;
6179 srel = htab->elf.srelbss;
6180 }
1d7e9d18 6181 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6182 {
4a7e5234
AM
6183 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6184 linker to copy the initial value out of the dynamic object
6185 and into the runtime process image. */
5474d94f 6186 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6187 h->needs_copy = 1;
5bd4f169
AM
6188 }
6189
529fe20e
AM
6190 /* We no longer want dyn_relocs. */
6191 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 6192 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6193}
6194
e86ce104
AM
6195/* If given a function descriptor symbol, hide both the function code
6196 sym and the descriptor. */
6197static void
4ce794b7
AM
6198ppc64_elf_hide_symbol (struct bfd_link_info *info,
6199 struct elf_link_hash_entry *h,
6200 bfd_boolean force_local)
e86ce104 6201{
34814b9f 6202 struct ppc_link_hash_entry *eh;
e86ce104
AM
6203 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6204
87469ba2
AM
6205 if (ppc_hash_table (info) == NULL)
6206 return;
6207
34814b9f
AM
6208 eh = (struct ppc_link_hash_entry *) h;
6209 if (eh->is_func_descriptor)
e86ce104 6210 {
34814b9f 6211 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6212
721956f4 6213 if (fh == NULL)
d1329ca3
AM
6214 {
6215 const char *p, *q;
b8ac2841 6216 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6217 char save;
6218
6219 /* We aren't supposed to use alloca in BFD because on
6220 systems which do not have alloca the version in libiberty
6221 calls xmalloc, which might cause the program to crash
6222 when it runs out of memory. This function doesn't have a
6223 return status, so there's no way to gracefully return an
6224 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6225 accessed; It's either a string in an ELF string table,
6226 or allocated in an objalloc structure. */
d1329ca3 6227
34814b9f 6228 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6229 save = *p;
6230 *(char *) p = '.';
34814b9f 6231 fh = (struct ppc_link_hash_entry *)
b8ac2841 6232 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6233 *(char *) p = save;
6234
6235 /* Unfortunately, if it so happens that the string we were
6236 looking for was allocated immediately before this string,
6237 then we overwrote the string terminator. That's the only
6238 reason the lookup should fail. */
6239 if (fh == NULL)
6240 {
34814b9f
AM
6241 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6242 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6243 --q, --p;
34814b9f
AM
6244 if (q < eh->elf.root.root.string && *p == '.')
6245 fh = (struct ppc_link_hash_entry *)
b8ac2841 6246 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6247 }
6248 if (fh != NULL)
6249 {
34814b9f
AM
6250 eh->oh = fh;
6251 fh->oh = eh;
d1329ca3
AM
6252 }
6253 }
e86ce104 6254 if (fh != NULL)
34814b9f 6255 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6256 }
6257}
6258
411e1bfb 6259static bfd_boolean
8843416a
AM
6260get_sym_h (struct elf_link_hash_entry **hp,
6261 Elf_Internal_Sym **symp,
6262 asection **symsecp,
f961d9dd 6263 unsigned char **tls_maskp,
8843416a
AM
6264 Elf_Internal_Sym **locsymsp,
6265 unsigned long r_symndx,
6266 bfd *ibfd)
411e1bfb 6267{
0ffa91dd 6268 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6269
6270 if (r_symndx >= symtab_hdr->sh_info)
6271 {
6272 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6273 struct elf_link_hash_entry *h;
6274
6275 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6276 h = elf_follow_link (h);
411e1bfb
AM
6277
6278 if (hp != NULL)
6279 *hp = h;
6280
6281 if (symp != NULL)
6282 *symp = NULL;
6283
6284 if (symsecp != NULL)
6285 {
6286 asection *symsec = NULL;
6287 if (h->root.type == bfd_link_hash_defined
6288 || h->root.type == bfd_link_hash_defweak)
6289 symsec = h->root.u.def.section;
6290 *symsecp = symsec;
6291 }
6292
e7b938ca 6293 if (tls_maskp != NULL)
411e1bfb
AM
6294 {
6295 struct ppc_link_hash_entry *eh;
6296
6297 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6298 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6299 }
6300 }
6301 else
6302 {
6303 Elf_Internal_Sym *sym;
6304 Elf_Internal_Sym *locsyms = *locsymsp;
6305
6306 if (locsyms == NULL)
6307 {
6308 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6309 if (locsyms == NULL)
6310 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6311 symtab_hdr->sh_info,
6312 0, NULL, NULL, NULL);
6313 if (locsyms == NULL)
6314 return FALSE;
6315 *locsymsp = locsyms;
6316 }
6317 sym = locsyms + r_symndx;
6318
6319 if (hp != NULL)
6320 *hp = NULL;
6321
6322 if (symp != NULL)
6323 *symp = sym;
6324
6325 if (symsecp != NULL)
cb33740c 6326 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6327
e7b938ca 6328 if (tls_maskp != NULL)
411e1bfb
AM
6329 {
6330 struct got_entry **lgot_ents;
f961d9dd 6331 unsigned char *tls_mask;
411e1bfb 6332
e7b938ca 6333 tls_mask = NULL;
411e1bfb
AM
6334 lgot_ents = elf_local_got_ents (ibfd);
6335 if (lgot_ents != NULL)
6336 {
e054468f
AM
6337 struct plt_entry **local_plt = (struct plt_entry **)
6338 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6339 unsigned char *lgot_masks = (unsigned char *)
e054468f 6340 (local_plt + symtab_hdr->sh_info);
e7b938ca 6341 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6342 }
e7b938ca 6343 *tls_maskp = tls_mask;
411e1bfb
AM
6344 }
6345 }
6346 return TRUE;
6347}
6348
e7b938ca 6349/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6350 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6351 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6352
6353static int
f961d9dd 6354get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6355 unsigned long *toc_symndx,
6356 bfd_vma *toc_addend,
0d4792f7 6357 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6358 const Elf_Internal_Rela *rel,
6359 bfd *ibfd)
411e1bfb
AM
6360{
6361 unsigned long r_symndx;
0d4792f7 6362 int next_r;
411e1bfb
AM
6363 struct elf_link_hash_entry *h;
6364 Elf_Internal_Sym *sym;
6365 asection *sec;
6366 bfd_vma off;
6367
6368 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6369 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6370 return 0;
411e1bfb 6371
37da22e5
AM
6372 if ((*tls_maskp != NULL
6373 && (**tls_maskp & TLS_TLS) != 0
6374 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 6375 || sec == NULL
6bee8834 6376 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6377 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6378 return 1;
411e1bfb
AM
6379
6380 /* Look inside a TOC section too. */
6381 if (h != NULL)
6382 {
6383 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6384 off = h->root.u.def.value;
6385 }
6386 else
6387 off = sym->st_value;
6388 off += rel->r_addend;
6389 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6390 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6391 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6392 if (toc_symndx != NULL)
6393 *toc_symndx = r_symndx;
3a71aa26
AM
6394 if (toc_addend != NULL)
6395 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6396 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6397 return 0;
854b41e7 6398 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6399 && (next_r == -1 || next_r == -2))
6400 return 1 - next_r;
951fd09b 6401 return 1;
411e1bfb
AM
6402}
6403
3b421ab3
AM
6404/* Find (or create) an entry in the tocsave hash table. */
6405
6406static struct tocsave_entry *
6407tocsave_find (struct ppc_link_hash_table *htab,
6408 enum insert_option insert,
6409 Elf_Internal_Sym **local_syms,
6410 const Elf_Internal_Rela *irela,
6411 bfd *ibfd)
6412{
6413 unsigned long r_indx;
6414 struct elf_link_hash_entry *h;
6415 Elf_Internal_Sym *sym;
6416 struct tocsave_entry ent, *p;
6417 hashval_t hash;
6418 struct tocsave_entry **slot;
6419
6420 r_indx = ELF64_R_SYM (irela->r_info);
6421 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6422 return NULL;
6423 if (ent.sec == NULL || ent.sec->output_section == NULL)
6424 {
4eca0228 6425 _bfd_error_handler
871b3ab2 6426 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
6427 return NULL;
6428 }
6429
6430 if (h != NULL)
6431 ent.offset = h->root.u.def.value;
6432 else
6433 ent.offset = sym->st_value;
6434 ent.offset += irela->r_addend;
6435
6436 hash = tocsave_htab_hash (&ent);
6437 slot = ((struct tocsave_entry **)
6438 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6439 if (slot == NULL)
6440 return NULL;
6441
6442 if (*slot == NULL)
6443 {
6444 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6445 if (p == NULL)
6446 return NULL;
6447 *p = ent;
6448 *slot = p;
6449 }
6450 return *slot;
6451}
6452
754021d0 6453/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6454 code for the old ABI, these will already have been done. */
754021d0
AM
6455
6456static bfd_boolean
6457adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6458{
6459 struct ppc_link_hash_entry *eh;
6460 asection *sym_sec;
74f0fb50 6461 struct _opd_sec_data *opd;
754021d0
AM
6462
6463 if (h->root.type == bfd_link_hash_indirect)
6464 return TRUE;
6465
754021d0
AM
6466 if (h->root.type != bfd_link_hash_defined
6467 && h->root.type != bfd_link_hash_defweak)
6468 return TRUE;
6469
6470 eh = (struct ppc_link_hash_entry *) h;
6471 if (eh->adjust_done)
6472 return TRUE;
6473
6474 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6475 opd = get_opd_info (sym_sec);
6476 if (opd != NULL && opd->adjust != NULL)
754021d0 6477 {
51aecdc5 6478 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
6479 if (adjust == -1)
6480 {
6481 /* This entry has been deleted. */
b3fac117 6482 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6483 if (dsec == NULL)
6484 {
6485 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 6486 if (discarded_section (dsec))
81688140 6487 {
b3fac117 6488 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6489 break;
6490 }
6491 }
4025353c 6492 eh->elf.root.u.def.value = 0;
81688140 6493 eh->elf.root.u.def.section = dsec;
4025353c
AM
6494 }
6495 else
6496 eh->elf.root.u.def.value += adjust;
754021d0
AM
6497 eh->adjust_done = 1;
6498 }
6499 return TRUE;
6500}
6501
8c1d1bb8 6502/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 6503 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
6504 have already been determined. */
6505
6506static bfd_boolean
6507dec_dynrel_count (bfd_vma r_info,
6508 asection *sec,
6509 struct bfd_link_info *info,
6510 Elf_Internal_Sym **local_syms,
6511 struct elf_link_hash_entry *h,
19e08130 6512 Elf_Internal_Sym *sym)
8c1d1bb8
AM
6513{
6514 enum elf_ppc64_reloc_type r_type;
19e08130 6515 asection *sym_sec = NULL;
8c1d1bb8
AM
6516
6517 /* Can this reloc be dynamic? This switch, and later tests here
6518 should be kept in sync with the code in check_relocs. */
6519 r_type = ELF64_R_TYPE (r_info);
6520 switch (r_type)
6521 {
6522 default:
6523 return TRUE;
6524
6525 case R_PPC64_TPREL16:
6526 case R_PPC64_TPREL16_LO:
6527 case R_PPC64_TPREL16_HI:
6528 case R_PPC64_TPREL16_HA:
6529 case R_PPC64_TPREL16_DS:
6530 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
6531 case R_PPC64_TPREL16_HIGH:
6532 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
6533 case R_PPC64_TPREL16_HIGHER:
6534 case R_PPC64_TPREL16_HIGHERA:
6535 case R_PPC64_TPREL16_HIGHEST:
6536 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8
AM
6537 case R_PPC64_TPREL64:
6538 case R_PPC64_DTPMOD64:
6539 case R_PPC64_DTPREL64:
6540 case R_PPC64_ADDR64:
6541 case R_PPC64_REL30:
6542 case R_PPC64_REL32:
6543 case R_PPC64_REL64:
6544 case R_PPC64_ADDR14:
6545 case R_PPC64_ADDR14_BRNTAKEN:
6546 case R_PPC64_ADDR14_BRTAKEN:
6547 case R_PPC64_ADDR16:
6548 case R_PPC64_ADDR16_DS:
6549 case R_PPC64_ADDR16_HA:
6550 case R_PPC64_ADDR16_HI:
f9c6b907
AM
6551 case R_PPC64_ADDR16_HIGH:
6552 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
6553 case R_PPC64_ADDR16_HIGHER:
6554 case R_PPC64_ADDR16_HIGHERA:
6555 case R_PPC64_ADDR16_HIGHEST:
6556 case R_PPC64_ADDR16_HIGHESTA:
6557 case R_PPC64_ADDR16_LO:
6558 case R_PPC64_ADDR16_LO_DS:
6559 case R_PPC64_ADDR24:
6560 case R_PPC64_ADDR32:
6561 case R_PPC64_UADDR16:
6562 case R_PPC64_UADDR32:
6563 case R_PPC64_UADDR64:
6564 case R_PPC64_TOC:
6565 break;
6566 }
6567
6568 if (local_syms != NULL)
6569 {
6570 unsigned long r_symndx;
8c1d1bb8
AM
6571 bfd *ibfd = sec->owner;
6572
6573 r_symndx = ELF64_R_SYM (r_info);
6574 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6575 return FALSE;
6576 }
6577
0e1862bb 6578 if ((bfd_link_pic (info)
1d483afe 6579 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 6580 || (h != NULL
198f1157 6581 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
6582 || h->root.type == bfd_link_hash_defweak
6583 || !h->def_regular))))
6584 || (ELIMINATE_COPY_RELOCS
0e1862bb 6585 && !bfd_link_pic (info)
8c1d1bb8
AM
6586 && h != NULL
6587 && (h->root.type == bfd_link_hash_defweak
6588 || !h->def_regular)))
6589 ;
6590 else
6591 return TRUE;
6592
6593 if (h != NULL)
6edfbbad 6594 {
19e08130
AM
6595 struct elf_dyn_relocs *p;
6596 struct elf_dyn_relocs **pp;
6597 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6598
6599 /* elf_gc_sweep may have already removed all dyn relocs associated
6600 with local syms for a given section. Also, symbol flags are
6601 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6602 report a dynreloc miscount. */
6603 if (*pp == NULL && info->gc_sections)
6604 return TRUE;
6605
6606 while ((p = *pp) != NULL)
60124e18 6607 {
19e08130
AM
6608 if (p->sec == sec)
6609 {
6610 if (!must_be_dyn_reloc (info, r_type))
6611 p->pc_count -= 1;
6612 p->count -= 1;
6613 if (p->count == 0)
6614 *pp = p->next;
6615 return TRUE;
6616 }
6617 pp = &p->next;
60124e18 6618 }
6edfbbad 6619 }
19e08130
AM
6620 else
6621 {
6622 struct ppc_dyn_relocs *p;
6623 struct ppc_dyn_relocs **pp;
6624 void *vpp;
6625 bfd_boolean is_ifunc;
8c1d1bb8 6626
19e08130
AM
6627 if (local_syms == NULL)
6628 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6629 if (sym_sec == NULL)
6630 sym_sec = sec;
c57da1a7 6631
19e08130
AM
6632 vpp = &elf_section_data (sym_sec)->local_dynrel;
6633 pp = (struct ppc_dyn_relocs **) vpp;
6634
6635 if (*pp == NULL && info->gc_sections)
6636 return TRUE;
6637
6638 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6639 while ((p = *pp) != NULL)
8c1d1bb8 6640 {
19e08130
AM
6641 if (p->sec == sec && p->ifunc == is_ifunc)
6642 {
6643 p->count -= 1;
6644 if (p->count == 0)
6645 *pp = p->next;
6646 return TRUE;
6647 }
6648 pp = &p->next;
8c1d1bb8 6649 }
8c1d1bb8
AM
6650 }
6651
695344c0 6652 /* xgettext:c-format */
cf97bcb0
AM
6653 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6654 sec->owner, sec);
8c1d1bb8
AM
6655 bfd_set_error (bfd_error_bad_value);
6656 return FALSE;
6657}
6658
754021d0
AM
6659/* Remove unused Official Procedure Descriptor entries. Currently we
6660 only remove those associated with functions in discarded link-once
6661 sections, or weakly defined functions that have been overridden. It
6662 would be possible to remove many more entries for statically linked
6663 applications. */
6664
b34976b6 6665bfd_boolean
e7d1c40c 6666ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
6667{
6668 bfd *ibfd;
754021d0 6669 bfd_boolean some_edited = FALSE;
3f764659 6670 asection *need_pad = NULL;
e7d1c40c
AM
6671 struct ppc_link_hash_table *htab;
6672
6673 htab = ppc_hash_table (info);
6674 if (htab == NULL)
6675 return FALSE;
1e2f5b6e 6676
c72f2fb2 6677 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
6678 {
6679 asection *sec;
6680 Elf_Internal_Rela *relstart, *rel, *relend;
6681 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 6682 Elf_Internal_Sym *local_syms;
74f0fb50 6683 struct _opd_sec_data *opd;
51aecdc5 6684 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 6685 bfd_size_type cnt_16b = 0;
1e2f5b6e 6686
854b41e7
AM
6687 if (!is_ppc64_elf (ibfd))
6688 continue;
6689
1e2f5b6e 6690 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 6691 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
6692 continue;
6693
dbaa2011 6694 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
6695 continue;
6696
1e2f5b6e
AM
6697 if (sec->output_section == bfd_abs_section_ptr)
6698 continue;
6699
6700 /* Look through the section relocs. */
6701 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6702 continue;
6703
6cdc0ccc 6704 local_syms = NULL;
0ffa91dd 6705 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
6706
6707 /* Read the relocations. */
4ce794b7 6708 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 6709 info->keep_memory);
1e2f5b6e 6710 if (relstart == NULL)
b34976b6 6711 return FALSE;
1e2f5b6e
AM
6712
6713 /* First run through the relocs to check they are sane, and to
6714 determine whether we need to edit this opd section. */
b34976b6 6715 need_edit = FALSE;
51aecdc5 6716 broken = FALSE;
3f764659 6717 need_pad = sec;
1e2f5b6e 6718 relend = relstart + sec->reloc_count;
50bc7936 6719 for (rel = relstart; rel < relend; )
1e2f5b6e 6720 {
04c9666a 6721 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
6722 unsigned long r_symndx;
6723 asection *sym_sec;
6724 struct elf_link_hash_entry *h;
6725 Elf_Internal_Sym *sym;
51aecdc5 6726 bfd_vma offset;
1e2f5b6e 6727
51aecdc5 6728 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
6729 only interested in the reloc pointing to a function entry
6730 point. */
51aecdc5
AM
6731 offset = rel->r_offset;
6732 if (rel + 1 == relend
6733 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
6734 {
6735 /* If someone messes with .opd alignment then after a
6736 "ld -r" we might have padding in the middle of .opd.
6737 Also, there's nothing to prevent someone putting
6738 something silly in .opd with the assembler. No .opd
b34976b6 6739 optimization for them! */
3f764659 6740 broken_opd:
4eca0228 6741 _bfd_error_handler
871b3ab2 6742 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 6743 broken = TRUE;
1e2f5b6e
AM
6744 break;
6745 }
6746
50bc7936
AM
6747 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6748 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6749 {
4eca0228 6750 _bfd_error_handler
695344c0 6751 /* xgettext:c-format */
871b3ab2 6752 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 6753 ibfd, r_type);
51aecdc5 6754 broken = TRUE;
50bc7936
AM
6755 break;
6756 }
6757
1e2f5b6e 6758 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
6759 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6760 r_symndx, ibfd))
50bc7936 6761 goto error_ret;
1e2f5b6e
AM
6762
6763 if (sym_sec == NULL || sym_sec->owner == NULL)
6764 {
411e1bfb
AM
6765 const char *sym_name;
6766 if (h != NULL)
6767 sym_name = h->root.root.string;
6768 else
26c61ae5
L
6769 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6770 sym_sec);
411e1bfb 6771
4eca0228 6772 _bfd_error_handler
695344c0 6773 /* xgettext:c-format */
871b3ab2 6774 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 6775 ibfd, sym_name);
51aecdc5 6776 broken = TRUE;
1e2f5b6e
AM
6777 break;
6778 }
6779
51020317
AM
6780 /* opd entries are always for functions defined in the
6781 current input bfd. If the symbol isn't defined in the
6782 input bfd, then we won't be using the function in this
6783 bfd; It must be defined in a linkonce section in another
6784 bfd, or is weak. It's also possible that we are
6785 discarding the function due to a linker script /DISCARD/,
6786 which we test for via the output_section. */
6787 if (sym_sec->owner != ibfd
6788 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 6789 need_edit = TRUE;
1e2f5b6e 6790
50bc7936 6791 rel += 2;
51aecdc5
AM
6792 if (rel + 1 == relend
6793 || (rel + 2 < relend
6794 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
6795 ++rel;
6796
6797 if (rel == relend)
3f764659
JJ
6798 {
6799 if (sec->size == offset + 24)
6800 {
6801 need_pad = NULL;
6802 break;
6803 }
51aecdc5 6804 if (sec->size == offset + 16)
3f764659
JJ
6805 {
6806 cnt_16b++;
6807 break;
6808 }
6809 goto broken_opd;
6810 }
3f764659
JJ
6811 else if (rel + 1 < relend
6812 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6813 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6814 {
51aecdc5
AM
6815 if (rel[0].r_offset == offset + 16)
6816 cnt_16b++;
6817 else if (rel[0].r_offset != offset + 24)
6818 goto broken_opd;
3f764659
JJ
6819 }
6820 else
6821 goto broken_opd;
1e2f5b6e
AM
6822 }
6823
e7d1c40c 6824 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 6825
51aecdc5 6826 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
6827 {
6828 Elf_Internal_Rela *write_rel;
d4730f92 6829 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 6830 bfd_byte *rptr, *wptr;
983bddc8 6831 bfd_byte *new_contents;
74f0fb50
AM
6832 bfd_size_type amt;
6833
983bddc8 6834 new_contents = NULL;
51aecdc5 6835 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 6836 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 6837 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
6838 if (opd->adjust == NULL)
6839 return FALSE;
1e2f5b6e
AM
6840
6841 /* This seems a waste of time as input .opd sections are all
6842 zeros as generated by gcc, but I suppose there's no reason
6843 this will always be so. We might start putting something in
6844 the third word of .opd entries. */
6845 if ((sec->flags & SEC_IN_MEMORY) == 0)
6846 {
eea6121a
AM
6847 bfd_byte *loc;
6848 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 6849 {
eea6121a
AM
6850 if (loc != NULL)
6851 free (loc);
50bc7936 6852 error_ret:
6cdc0ccc
AM
6853 if (local_syms != NULL
6854 && symtab_hdr->contents != (unsigned char *) local_syms)
6855 free (local_syms);
6cdc0ccc
AM
6856 if (elf_section_data (sec)->relocs != relstart)
6857 free (relstart);
b34976b6 6858 return FALSE;
6cdc0ccc 6859 }
1e2f5b6e
AM
6860 sec->contents = loc;
6861 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6862 }
6863
6864 elf_section_data (sec)->relocs = relstart;
6865
3f764659 6866 new_contents = sec->contents;
3f764659
JJ
6867 if (add_aux_fields)
6868 {
6869 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6870 if (new_contents == NULL)
6871 return FALSE;
51aecdc5 6872 need_pad = NULL;
3f764659 6873 }
b4f4e59f
AM
6874 wptr = new_contents;
6875 rptr = sec->contents;
1e2f5b6e 6876 write_rel = relstart;
51aecdc5 6877 for (rel = relstart; rel < relend; )
1e2f5b6e 6878 {
50bc7936
AM
6879 unsigned long r_symndx;
6880 asection *sym_sec;
6881 struct elf_link_hash_entry *h;
51aecdc5 6882 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 6883 Elf_Internal_Sym *sym;
51aecdc5
AM
6884 long opd_ent_size;
6885 Elf_Internal_Rela *next_rel;
6886 bfd_boolean skip;
50bc7936
AM
6887
6888 r_symndx = ELF64_R_SYM (rel->r_info);
6889 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 6890 r_symndx, ibfd))
50bc7936
AM
6891 goto error_ret;
6892
51aecdc5
AM
6893 next_rel = rel + 2;
6894 if (next_rel + 1 == relend
6895 || (next_rel + 2 < relend
6896 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
6897 ++next_rel;
6898
6899 /* See if the .opd entry is full 24 byte or
6900 16 byte (with fd_aux entry overlapped with next
6901 fd_func). */
6902 opd_ent_size = 24;
6903 if (next_rel == relend)
1e2f5b6e 6904 {
51aecdc5 6905 if (sec->size == rel->r_offset + 16)
3f764659 6906 opd_ent_size = 16;
51aecdc5
AM
6907 }
6908 else if (next_rel->r_offset == rel->r_offset + 16)
6909 opd_ent_size = 16;
3f764659 6910
51aecdc5
AM
6911 if (h != NULL
6912 && h->root.root.string[0] == '.')
6913 {
8c5b4e52
AM
6914 fdh = ((struct ppc_link_hash_entry *) h)->oh;
6915 if (fdh != NULL)
6916 {
6917 fdh = ppc_follow_link (fdh);
6918 if (fdh->elf.root.type != bfd_link_hash_defined
6919 && fdh->elf.root.type != bfd_link_hash_defweak)
6920 fdh = NULL;
6921 }
51aecdc5 6922 }
1e2f5b6e 6923
51aecdc5
AM
6924 skip = (sym_sec->owner != ibfd
6925 || sym_sec->output_section == bfd_abs_section_ptr);
6926 if (skip)
6927 {
6928 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 6929 {
51aecdc5
AM
6930 /* Arrange for the function descriptor sym
6931 to be dropped. */
6932 fdh->elf.root.u.def.value = 0;
6933 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 6934 }
51aecdc5 6935 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 6936
0e1862bb 6937 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
6938 rel = next_rel;
6939 else
6940 while (1)
6941 {
6942 if (!dec_dynrel_count (rel->r_info, sec, info,
6943 NULL, h, sym))
6944 goto error_ret;
754021d0 6945
51aecdc5
AM
6946 if (++rel == next_rel)
6947 break;
1e2f5b6e 6948
51aecdc5
AM
6949 r_symndx = ELF64_R_SYM (rel->r_info);
6950 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6951 r_symndx, ibfd))
6952 goto error_ret;
6953 }
50bc7936
AM
6954 }
6955 else
1e2f5b6e 6956 {
51aecdc5
AM
6957 /* We'll be keeping this opd entry. */
6958 long adjust;
6959
6960 if (fdh != NULL)
6961 {
6962 /* Redefine the function descriptor symbol to
6963 this location in the opd section. It is
6964 necessary to update the value here rather
6965 than using an array of adjustments as we do
6966 for local symbols, because various places
6967 in the generic ELF code use the value
6968 stored in u.def.value. */
6969 fdh->elf.root.u.def.value = wptr - new_contents;
6970 fdh->adjust_done = 1;
6971 }
6972
6973 /* Local syms are a bit tricky. We could
6974 tweak them as they can be cached, but
6975 we'd need to look through the local syms
6976 for the function descriptor sym which we
6977 don't have at the moment. So keep an
6978 array of adjustments. */
6979 adjust = (wptr - new_contents) - (rptr - sec->contents);
6980 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
6981
6982 if (wptr != rptr)
6983 memcpy (wptr, rptr, opd_ent_size);
6984 wptr += opd_ent_size;
6985 if (add_aux_fields && opd_ent_size == 16)
6986 {
6987 memset (wptr, '\0', 8);
6988 wptr += 8;
6989 }
6990
50bc7936 6991 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
6992 new opd entries. */
6993 for ( ; rel != next_rel; ++rel)
6994 {
6995 rel->r_offset += adjust;
6996 if (write_rel != rel)
6997 memcpy (write_rel, rel, sizeof (*rel));
6998 ++write_rel;
6999 }
1e2f5b6e 7000 }
51aecdc5
AM
7001
7002 rptr += opd_ent_size;
1e2f5b6e
AM
7003 }
7004
3f764659 7005 sec->size = wptr - new_contents;
1e2f5b6e 7006 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7007 if (add_aux_fields)
7008 {
7009 free (sec->contents);
7010 sec->contents = new_contents;
7011 }
7012
05bf9422 7013 /* Fudge the header size too, as this is used later in
cdcf6e38 7014 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7015 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7016 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7017 some_edited = TRUE;
1e2f5b6e 7018 }
6cdc0ccc 7019 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7020 free (relstart);
6cdc0ccc 7021
411e1bfb
AM
7022 if (local_syms != NULL
7023 && symtab_hdr->contents != (unsigned char *) local_syms)
7024 {
7025 if (!info->keep_memory)
7026 free (local_syms);
7027 else
7028 symtab_hdr->contents = (unsigned char *) local_syms;
7029 }
7030 }
7031
754021d0
AM
7032 if (some_edited)
7033 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7034
3f764659
JJ
7035 /* If we are doing a final link and the last .opd entry is just 16 byte
7036 long, add a 8 byte padding after it. */
0e1862bb 7037 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7038 {
7039 bfd_byte *p;
7040
7041 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7042 {
7043 BFD_ASSERT (need_pad->size > 0);
7044
7045 p = bfd_malloc (need_pad->size + 8);
7046 if (p == NULL)
7047 return FALSE;
699733f6 7048
2cdcc330
AM
7049 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7050 p, 0, need_pad->size))
3f764659
JJ
7051 return FALSE;
7052
7053 need_pad->contents = p;
7054 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7055 }
7056 else
7057 {
7058 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7059 if (p == NULL)
7060 return FALSE;
7061
7062 need_pad->contents = p;
7063 }
7064
7065 memset (need_pad->contents + need_pad->size, 0, 8);
7066 need_pad->size += 8;
7067 }
7068
411e1bfb
AM
7069 return TRUE;
7070}
7071
3e04d765
AM
7072/* Analyze inline PLT call relocations to see whether calls to locally
7073 defined functions can be converted to direct calls. */
7074
7075bfd_boolean
7076ppc64_elf_inline_plt (struct bfd_link_info *info)
7077{
7078 struct ppc_link_hash_table *htab;
7079 bfd *ibfd;
7080 asection *sec;
7081 bfd_vma low_vma, high_vma, limit;
7082
7083 htab = ppc_hash_table (info);
7084 if (htab == NULL)
7085 return FALSE;
7086
7087 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7088 reduced somewhat to cater for possible stubs that might be added
7089 between the call and its destination. */
7090 if (htab->params->group_size < 0)
7091 {
7092 limit = -htab->params->group_size;
7093 if (limit == 1)
7094 limit = 0x1e00000;
7095 }
7096 else
7097 {
7098 limit = htab->params->group_size;
7099 if (limit == 1)
7100 limit = 0x1c00000;
7101 }
7102
7103 low_vma = -1;
7104 high_vma = 0;
7105 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7106 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7107 {
7108 if (low_vma > sec->vma)
7109 low_vma = sec->vma;
7110 if (high_vma < sec->vma + sec->size)
7111 high_vma = sec->vma + sec->size;
7112 }
7113
7114 /* If a "bl" can reach anywhere in local code sections, then we can
7115 convert all inline PLT sequences to direct calls when the symbol
7116 is local. */
7117 if (high_vma - low_vma < limit)
7118 {
7119 htab->can_convert_all_inline_plt = 1;
7120 return TRUE;
7121 }
7122
7123 /* Otherwise, go looking through relocs for cases where a direct
7124 call won't reach. Mark the symbol on any such reloc to disable
7125 the optimization and keep the PLT entry as it seems likely that
7126 this will be better than creating trampolines. Note that this
7127 will disable the optimization for all inline PLT calls to a
7128 particular symbol, not just those that won't reach. The
7129 difficulty in doing a more precise optimization is that the
7130 linker needs to make a decision depending on whether a
7131 particular R_PPC64_PLTCALL insn can be turned into a direct
7132 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7133 the sequence, and there is nothing that ties those relocs
7134 together except their symbol. */
7135
7136 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7137 {
7138 Elf_Internal_Shdr *symtab_hdr;
7139 Elf_Internal_Sym *local_syms;
7140
7141 if (!is_ppc64_elf (ibfd))
7142 continue;
7143
7144 local_syms = NULL;
7145 symtab_hdr = &elf_symtab_hdr (ibfd);
7146
7147 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7148 if (ppc64_elf_section_data (sec)->has_pltcall
7149 && !bfd_is_abs_section (sec->output_section))
7150 {
7151 Elf_Internal_Rela *relstart, *rel, *relend;
7152
7153 /* Read the relocations. */
7154 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7155 info->keep_memory);
7156 if (relstart == NULL)
7157 return FALSE;
7158
7159 relend = relstart + sec->reloc_count;
7160 for (rel = relstart; rel < relend; )
7161 {
7162 enum elf_ppc64_reloc_type r_type;
7163 unsigned long r_symndx;
7164 asection *sym_sec;
7165 struct elf_link_hash_entry *h;
7166 Elf_Internal_Sym *sym;
7167 unsigned char *tls_maskp;
7168
7169 r_type = ELF64_R_TYPE (rel->r_info);
7170 if (r_type != R_PPC64_PLTCALL)
7171 continue;
7172
7173 r_symndx = ELF64_R_SYM (rel->r_info);
7174 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7175 r_symndx, ibfd))
7176 {
7177 if (elf_section_data (sec)->relocs != relstart)
7178 free (relstart);
7179 if (local_syms != NULL
2cdcc330 7180 && symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765
AM
7181 free (local_syms);
7182 return FALSE;
7183 }
7184
7185 if (sym_sec != NULL && sym_sec->output_section != NULL)
7186 {
7187 bfd_vma from, to;
7188 if (h != NULL)
7189 to = h->root.u.def.value;
7190 else
7191 to = sym->st_value;
7192 to += (rel->r_addend
7193 + sym_sec->output_offset
7194 + sym_sec->output_section->vma);
7195 from = (rel->r_offset
7196 + sec->output_offset
7197 + sec->output_section->vma);
7198 if (to - from + limit < 2 * limit)
7199 *tls_maskp &= ~PLT_KEEP;
7200 }
7201 }
7202 if (elf_section_data (sec)->relocs != relstart)
7203 free (relstart);
7204 }
7205
7206 if (local_syms != NULL
7207 && symtab_hdr->contents != (unsigned char *) local_syms)
7208 {
7209 if (!info->keep_memory)
7210 free (local_syms);
7211 else
7212 symtab_hdr->contents = (unsigned char *) local_syms;
7213 }
7214 }
7215
7216 return TRUE;
7217}
7218
e1918d23 7219/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7220
e1918d23 7221asection *
e7d1c40c 7222ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7223{
411e1bfb
AM
7224 struct ppc_link_hash_table *htab;
7225
411e1bfb 7226 htab = ppc_hash_table (info);
4dfe6ac6
NC
7227 if (htab == NULL)
7228 return NULL;
7229
ee67d69a
AM
7230 if (abiversion (info->output_bfd) == 1)
7231 htab->opd_abi = 1;
7232
e7d1c40c 7233 if (htab->params->no_multi_toc)
33c0ec9d
AM
7234 htab->do_multi_toc = 0;
7235 else if (!htab->do_multi_toc)
e7d1c40c 7236 htab->params->no_multi_toc = 1;
33c0ec9d 7237
8b5f1ed8
AM
7238 /* Default to --no-plt-localentry, as this option can cause problems
7239 with symbol interposition. For example, glibc libpthread.so and
7240 libc.so duplicate many pthread symbols, with a fallback
7241 implementation in libc.so. In some cases the fallback does more
7242 work than the pthread implementation. __pthread_condattr_destroy
7243 is one such symbol: the libpthread.so implementation is
7244 localentry:0 while the libc.so implementation is localentry:8.
7245 An app that "cleverly" uses dlopen to only load necessary
7246 libraries at runtime may omit loading libpthread.so when not
7247 running multi-threaded, which then results in the libc.so
7248 fallback symbols being used and ld.so complaining. Now there
7249 are workarounds in ld (see non_zero_localentry) to detect the
7250 pthread situation, but that may not be the only case where
7251 --plt-localentry can cause trouble. */
f378ab09 7252 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7253 htab->params->plt_localentry0 = 0;
d44c746a
AM
7254 if (htab->params->plt_localentry0
7255 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7256 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
7257 _bfd_error_handler
7258 (_("warning: --plt-localentry is especially dangerous without "
7259 "ld.so support to detect ABI violations"));
f378ab09 7260
3a71aa26
AM
7261 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7262 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7263 FALSE, FALSE, TRUE));
a7f2871e
AM
7264 /* Move dynamic linking info to the function descriptor sym. */
7265 if (htab->tls_get_addr != NULL)
7266 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7267 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7268 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7269 FALSE, FALSE, TRUE));
7c9cf415 7270 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
7271 {
7272 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7273
7274 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7275 FALSE, FALSE, TRUE);
7276 if (opt != NULL)
7277 func_desc_adjust (opt, info);
7278 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7279 FALSE, FALSE, TRUE);
7280 if (opt_fd != NULL
7281 && (opt_fd->root.type == bfd_link_hash_defined
7282 || opt_fd->root.type == bfd_link_hash_defweak))
7283 {
7284 /* If glibc supports an optimized __tls_get_addr call stub,
7285 signalled by the presence of __tls_get_addr_opt, and we'll
7286 be calling __tls_get_addr via a plt call stub, then
7287 make __tls_get_addr point to __tls_get_addr_opt. */
7288 tga_fd = &htab->tls_get_addr_fd->elf;
7289 if (htab->elf.dynamic_sections_created
7290 && tga_fd != NULL
7291 && (tga_fd->type == STT_FUNC
7292 || tga_fd->needs_plt)
7293 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 7294 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
7295 {
7296 struct plt_entry *ent;
7297
7298 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7299 if (ent->plt.refcount > 0)
7300 break;
7301 if (ent != NULL)
7302 {
7303 tga_fd->root.type = bfd_link_hash_indirect;
7304 tga_fd->root.u.i.link = &opt_fd->root;
7305 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 7306 opt_fd->mark = 1;
a7f2871e
AM
7307 if (opt_fd->dynindx != -1)
7308 {
7309 /* Use __tls_get_addr_opt in dynamic relocations. */
7310 opt_fd->dynindx = -1;
7311 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7312 opt_fd->dynstr_index);
7313 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7314 return NULL;
a7f2871e 7315 }
2cdcc330
AM
7316 htab->tls_get_addr_fd
7317 = (struct ppc_link_hash_entry *) opt_fd;
a7f2871e
AM
7318 tga = &htab->tls_get_addr->elf;
7319 if (opt != NULL && tga != NULL)
7320 {
7321 tga->root.type = bfd_link_hash_indirect;
7322 tga->root.u.i.link = &opt->root;
7323 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 7324 opt->mark = 1;
a7f2871e
AM
7325 _bfd_elf_link_hash_hide_symbol (info, opt,
7326 tga->forced_local);
7327 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7328 }
7329 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7330 htab->tls_get_addr_fd->is_func_descriptor = 1;
7331 if (htab->tls_get_addr != NULL)
7332 {
7333 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7334 htab->tls_get_addr->is_func = 1;
7335 }
7336 }
7337 }
7338 }
7c9cf415
AM
7339 else if (htab->params->tls_get_addr_opt < 0)
7340 htab->params->tls_get_addr_opt = 0;
a7f2871e 7341 }
33c0ec9d 7342 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7343}
8387904d 7344
3a71aa26
AM
7345/* Return TRUE iff REL is a branch reloc with a global symbol matching
7346 HASH1 or HASH2. */
8387904d 7347
3a71aa26
AM
7348static bfd_boolean
7349branch_reloc_hash_match (const bfd *ibfd,
7350 const Elf_Internal_Rela *rel,
7351 const struct ppc_link_hash_entry *hash1,
7352 const struct ppc_link_hash_entry *hash2)
7353{
7354 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7355 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7356 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7357
e054468f 7358 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7359 {
3a71aa26
AM
7360 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7361 struct elf_link_hash_entry *h;
8387904d 7362
3a71aa26 7363 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7364 h = elf_follow_link (h);
3a71aa26
AM
7365 if (h == &hash1->elf || h == &hash2->elf)
7366 return TRUE;
a48ebf4d 7367 }
3a71aa26 7368 return FALSE;
951fd09b 7369}
411e1bfb 7370
951fd09b
AM
7371/* Run through all the TLS relocs looking for optimization
7372 opportunities. The linker has been hacked (see ppc64elf.em) to do
7373 a preliminary section layout so that we know the TLS segment
7374 offsets. We can't optimize earlier because some optimizations need
7375 to know the tp offset, and we need to optimize before allocating
7376 dynamic relocations. */
7377
7378bfd_boolean
33c0ec9d 7379ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7380{
7381 bfd *ibfd;
7382 asection *sec;
7383 struct ppc_link_hash_table *htab;
663a1470 7384 unsigned char *toc_ref;
102890f0 7385 int pass;
951fd09b 7386
3cbc1e5e 7387 if (!bfd_link_executable (info))
411e1bfb
AM
7388 return TRUE;
7389
951fd09b 7390 htab = ppc_hash_table (info);
4dfe6ac6
NC
7391 if (htab == NULL)
7392 return FALSE;
7393
663a1470
AM
7394 /* Make two passes over the relocs. On the first pass, mark toc
7395 entries involved with tls relocs, and check that tls relocs
7396 involved in setting up a tls_get_addr call are indeed followed by
7397 such a call. If they are not, we can't do any tls optimization.
7398 On the second pass twiddle tls_mask flags to notify
7399 relocate_section that optimization can be done, and adjust got
7400 and plt refcounts. */
7401 toc_ref = NULL;
7402 for (pass = 0; pass < 2; ++pass)
c72f2fb2 7403 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
7404 {
7405 Elf_Internal_Sym *locsyms = NULL;
7406 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7407
102890f0
AM
7408 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7409 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7410 {
7411 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7412 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7413
102890f0
AM
7414 /* Read the relocations. */
7415 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7416 info->keep_memory);
7417 if (relstart == NULL)
2915c55b
JK
7418 {
7419 free (toc_ref);
7420 return FALSE;
7421 }
411e1bfb 7422
102890f0
AM
7423 relend = relstart + sec->reloc_count;
7424 for (rel = relstart; rel < relend; rel++)
7425 {
7426 enum elf_ppc64_reloc_type r_type;
7427 unsigned long r_symndx;
7428 struct elf_link_hash_entry *h;
7429 Elf_Internal_Sym *sym;
7430 asection *sym_sec;
f961d9dd
AM
7431 unsigned char *tls_mask;
7432 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
7433 bfd_vma value;
7434 bfd_boolean ok_tprel, is_local;
7435 long toc_ref_index = 0;
7436 int expecting_tls_get_addr = 0;
663a1470 7437 bfd_boolean ret = FALSE;
411e1bfb 7438
102890f0
AM
7439 r_symndx = ELF64_R_SYM (rel->r_info);
7440 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7441 r_symndx, ibfd))
7442 {
7443 err_free_rel:
7444 if (elf_section_data (sec)->relocs != relstart)
7445 free (relstart);
7446 if (toc_ref != NULL)
7447 free (toc_ref);
7448 if (locsyms != NULL
0ffa91dd 7449 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7450 != (unsigned char *) locsyms))
7451 free (locsyms);
663a1470 7452 return ret;
102890f0 7453 }
411e1bfb 7454
102890f0
AM
7455 if (h != NULL)
7456 {
766bc656
AM
7457 if (h->root.type == bfd_link_hash_defined
7458 || h->root.type == bfd_link_hash_defweak)
7459 value = h->root.u.def.value;
7460 else if (h->root.type == bfd_link_hash_undefweak)
7461 value = 0;
7462 else
663a1470
AM
7463 {
7464 found_tls_get_addr_arg = 0;
7465 continue;
7466 }
102890f0
AM
7467 }
7468 else
7469 /* Symbols referenced by TLS relocs must be of type
7470 STT_TLS. So no need for .opd local sym adjust. */
7471 value = sym->st_value;
7472
7473 ok_tprel = FALSE;
7474 is_local = FALSE;
7475 if (h == NULL
7476 || !h->def_dynamic)
7477 {
7478 is_local = TRUE;
766bc656
AM
7479 if (h != NULL
7480 && h->root.type == bfd_link_hash_undefweak)
7481 ok_tprel = TRUE;
c27b8c2a
AM
7482 else if (sym_sec != NULL
7483 && sym_sec->output_section != NULL)
766bc656
AM
7484 {
7485 value += sym_sec->output_offset;
7486 value += sym_sec->output_section->vma;
7487 value -= htab->elf.tls_sec->vma;
7488 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7489 < (bfd_vma) 1 << 32);
7490 }
102890f0 7491 }
951fd09b 7492
102890f0 7493 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7494 /* If this section has old-style __tls_get_addr calls
7495 without marker relocs, then check that each
7496 __tls_get_addr call reloc is preceded by a reloc
7497 that conceivably belongs to the __tls_get_addr arg
7498 setup insn. If we don't find matching arg setup
7499 relocs, don't do any tls optimization. */
7500 if (pass == 0
7501 && sec->has_tls_get_addr_call
7502 && h != NULL
7503 && (h == &htab->tls_get_addr->elf
7504 || h == &htab->tls_get_addr_fd->elf)
7505 && !found_tls_get_addr_arg
7506 && is_branch_reloc (r_type))
7507 {
25f53a85 7508 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
7509 "TLS optimization disabled\n"),
7510 ibfd, sec, rel->r_offset);
7511 ret = TRUE;
7512 goto err_free_rel;
7513 }
7514
7515 found_tls_get_addr_arg = 0;
102890f0
AM
7516 switch (r_type)
7517 {
7518 case R_PPC64_GOT_TLSLD16:
7519 case R_PPC64_GOT_TLSLD16_LO:
7520 expecting_tls_get_addr = 1;
663a1470 7521 found_tls_get_addr_arg = 1;
1a0670f3 7522 /* Fall through. */
102890f0
AM
7523
7524 case R_PPC64_GOT_TLSLD16_HI:
7525 case R_PPC64_GOT_TLSLD16_HA:
7526 /* These relocs should never be against a symbol
7527 defined in a shared lib. Leave them alone if
7528 that turns out to be the case. */
7529 if (!is_local)
7530 continue;
411e1bfb 7531
102890f0 7532 /* LD -> LE */
411e1bfb 7533 tls_set = 0;
102890f0
AM
7534 tls_clear = TLS_LD;
7535 tls_type = TLS_TLS | TLS_LD;
7536 break;
411e1bfb 7537
102890f0
AM
7538 case R_PPC64_GOT_TLSGD16:
7539 case R_PPC64_GOT_TLSGD16_LO:
7540 expecting_tls_get_addr = 1;
663a1470 7541 found_tls_get_addr_arg = 1;
1a0670f3 7542 /* Fall through. */
102890f0
AM
7543
7544 case R_PPC64_GOT_TLSGD16_HI:
7545 case R_PPC64_GOT_TLSGD16_HA:
7546 if (ok_tprel)
7547 /* GD -> LE */
411e1bfb 7548 tls_set = 0;
102890f0
AM
7549 else
7550 /* GD -> IE */
7551 tls_set = TLS_TLS | TLS_TPRELGD;
7552 tls_clear = TLS_GD;
7553 tls_type = TLS_TLS | TLS_GD;
7554 break;
7555
7556 case R_PPC64_GOT_TPREL16_DS:
7557 case R_PPC64_GOT_TPREL16_LO_DS:
7558 case R_PPC64_GOT_TPREL16_HI:
7559 case R_PPC64_GOT_TPREL16_HA:
7560 if (ok_tprel)
7561 {
7562 /* IE -> LE */
7563 tls_set = 0;
7564 tls_clear = TLS_TPREL;
7565 tls_type = TLS_TLS | TLS_TPREL;
7566 break;
7567 }
411e1bfb
AM
7568 continue;
7569
727fc41e
AM
7570 case R_PPC64_TLSGD:
7571 case R_PPC64_TLSLD:
23cedd1d
AM
7572 if (rel + 1 < relend
7573 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7574 {
7575 if (pass != 0
2cdcc330
AM
7576 && (ELF64_R_TYPE (rel[1].r_info)
7577 != R_PPC64_PLTSEQ))
23cedd1d
AM
7578 {
7579 r_symndx = ELF64_R_SYM (rel[1].r_info);
7580 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 7581 r_symndx, ibfd))
23cedd1d
AM
7582 goto err_free_rel;
7583 if (h != NULL)
7584 {
7585 struct plt_entry *ent = NULL;
7586
7587 for (ent = h->plt.plist;
7588 ent != NULL;
7589 ent = ent->next)
7590 if (ent->addend == rel[1].r_addend)
7591 break;
7592
7593 if (ent != NULL
7594 && ent->plt.refcount > 0)
7595 ent->plt.refcount -= 1;
7596 }
7597 }
7598 continue;
7599 }
663a1470 7600 found_tls_get_addr_arg = 1;
1a0670f3 7601 /* Fall through. */
663a1470
AM
7602
7603 case R_PPC64_TLS:
7604 case R_PPC64_TOC16:
7605 case R_PPC64_TOC16_LO:
102890f0
AM
7606 if (sym_sec == NULL || sym_sec != toc)
7607 continue;
7608
7609 /* Mark this toc entry as referenced by a TLS
7610 code sequence. We can do that now in the
7611 case of R_PPC64_TLS, and after checking for
7612 tls_get_addr for the TOC16 relocs. */
7613 if (toc_ref == NULL)
2cdcc330
AM
7614 toc_ref
7615 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
7616 if (toc_ref == NULL)
7617 goto err_free_rel;
7618
102890f0
AM
7619 if (h != NULL)
7620 value = h->root.u.def.value;
7621 else
7622 value = sym->st_value;
7623 value += rel->r_addend;
73242275
AM
7624 if (value % 8 != 0)
7625 continue;
7626 BFD_ASSERT (value < toc->size
7627 && toc->output_offset % 8 == 0);
663a1470 7628 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7629 if (r_type == R_PPC64_TLS
7630 || r_type == R_PPC64_TLSGD
7631 || r_type == R_PPC64_TLSLD)
102890f0
AM
7632 {
7633 toc_ref[toc_ref_index] = 1;
7634 continue;
7635 }
7636
7637 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7638 continue;
7639
7640 tls_set = 0;
7641 tls_clear = 0;
7642 expecting_tls_get_addr = 2;
7643 break;
7644
7645 case R_PPC64_TPREL64:
7646 if (pass == 0
7647 || sec != toc
7648 || toc_ref == NULL
663a1470 7649 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7650 continue;
7651 if (ok_tprel)
7652 {
7653 /* IE -> LE */
7654 tls_set = TLS_EXPLICIT;
7655 tls_clear = TLS_TPREL;
7656 break;
7657 }
7658 continue;
7659
7660 case R_PPC64_DTPMOD64:
7661 if (pass == 0
7662 || sec != toc
7663 || toc_ref == NULL
663a1470 7664 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7665 continue;
7666 if (rel + 1 < relend
7667 && (rel[1].r_info
7668 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7669 && rel[1].r_offset == rel->r_offset + 8)
7670 {
7671 if (ok_tprel)
7672 /* GD -> LE */
7673 tls_set = TLS_EXPLICIT | TLS_GD;
7674 else
7675 /* GD -> IE */
7676 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7677 tls_clear = TLS_GD;
7678 }
7679 else
7680 {
7681 if (!is_local)
7682 continue;
7683
7684 /* LD -> LE */
7685 tls_set = TLS_EXPLICIT;
7686 tls_clear = TLS_LD;
7687 }
7688 break;
7689
7690 default:
7691 continue;
7692 }
7693
7694 if (pass == 0)
7695 {
727fc41e
AM
7696 if (!expecting_tls_get_addr
7697 || !sec->has_tls_get_addr_call)
102890f0
AM
7698 continue;
7699
3a71aa26
AM
7700 if (rel + 1 < relend
7701 && branch_reloc_hash_match (ibfd, rel + 1,
7702 htab->tls_get_addr,
7703 htab->tls_get_addr_fd))
102890f0 7704 {
3a71aa26 7705 if (expecting_tls_get_addr == 2)
102890f0 7706 {
3a71aa26 7707 /* Check for toc tls entries. */
f961d9dd 7708 unsigned char *toc_tls;
3a71aa26
AM
7709 int retval;
7710
7711 retval = get_tls_mask (&toc_tls, NULL, NULL,
7712 &locsyms,
7713 rel, ibfd);
7714 if (retval == 0)
7715 goto err_free_rel;
663a1470
AM
7716 if (toc_tls != NULL)
7717 {
37da22e5
AM
7718 if ((*toc_tls & TLS_TLS) != 0
7719 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
7720 found_tls_get_addr_arg = 1;
7721 if (retval > 1)
7722 toc_ref[toc_ref_index] = 1;
7723 }
102890f0 7724 }
3a71aa26 7725 continue;
102890f0
AM
7726 }
7727
102890f0
AM
7728 /* Uh oh, we didn't find the expected call. We
7729 could just mark this symbol to exclude it
7730 from tls optimization but it's safer to skip
663a1470 7731 the entire optimization. */
695344c0 7732 /* xgettext:c-format */
25f53a85 7733 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
7734 "TLS optimization disabled\n"),
7735 ibfd, sec, rel->r_offset);
7736 ret = TRUE;
7737 goto err_free_rel;
102890f0
AM
7738 }
7739
37da22e5
AM
7740 /* If we don't have old-style __tls_get_addr calls
7741 without TLSGD/TLSLD marker relocs, and we haven't
7742 found a new-style __tls_get_addr call with a
7743 marker for this symbol, then we either have a
7744 broken object file or an -mlongcall style
7745 indirect call to __tls_get_addr without a marker.
7746 Disable optimization in this case. */
7747 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
7748 && (tls_set & TLS_EXPLICIT) == 0
7749 && !sec->has_tls_get_addr_call
7750 && ((*tls_mask & (TLS_TLS | TLS_MARK))
7751 != (TLS_TLS | TLS_MARK)))
7752 continue;
7753
23cedd1d 7754 if (expecting_tls_get_addr)
102890f0 7755 {
23cedd1d
AM
7756 struct plt_entry *ent = NULL;
7757
7758 if (htab->tls_get_addr != NULL)
7759 for (ent = htab->tls_get_addr->elf.plt.plist;
7760 ent != NULL;
7761 ent = ent->next)
7762 if (ent->addend == 0)
102890f0 7763 break;
411e1bfb 7764
23cedd1d
AM
7765 if (ent == NULL && htab->tls_get_addr_fd != NULL)
7766 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7767 ent != NULL;
7768 ent = ent->next)
7769 if (ent->addend == 0)
102890f0 7770 break;
23cedd1d
AM
7771
7772 if (ent != NULL
7773 && ent->plt.refcount > 0)
7774 ent->plt.refcount -= 1;
102890f0 7775 }
411e1bfb 7776
102890f0 7777 if (tls_clear == 0)
30038c59
AM
7778 continue;
7779
102890f0
AM
7780 if ((tls_set & TLS_EXPLICIT) == 0)
7781 {
7782 struct got_entry *ent;
411e1bfb 7783
102890f0
AM
7784 /* Adjust got entry for this reloc. */
7785 if (h != NULL)
7786 ent = h->got.glist;
7787 else
7788 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 7789
102890f0
AM
7790 for (; ent != NULL; ent = ent->next)
7791 if (ent->addend == rel->r_addend
7792 && ent->owner == ibfd
7793 && ent->tls_type == tls_type)
7794 break;
7795 if (ent == NULL)
7796 abort ();
411e1bfb 7797
102890f0
AM
7798 if (tls_set == 0)
7799 {
7800 /* We managed to get rid of a got entry. */
7801 if (ent->got.refcount > 0)
7802 ent->got.refcount -= 1;
7803 }
7804 }
7805 else
7806 {
7807 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7808 we'll lose one or two dyn relocs. */
7809 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 7810 NULL, h, sym))
102890f0 7811 return FALSE;
411e1bfb 7812
102890f0
AM
7813 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7814 {
7815 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 7816 NULL, h, sym))
102890f0
AM
7817 return FALSE;
7818 }
7819 }
411e1bfb 7820
102890f0
AM
7821 *tls_mask |= tls_set;
7822 *tls_mask &= ~tls_clear;
7823 }
8c1d1bb8 7824
102890f0
AM
7825 if (elf_section_data (sec)->relocs != relstart)
7826 free (relstart);
7827 }
411e1bfb 7828
663a1470
AM
7829 if (locsyms != NULL
7830 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7831 {
7832 if (!info->keep_memory)
7833 free (locsyms);
7834 else
7835 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7836 }
7837 }
411e1bfb 7838
663a1470
AM
7839 if (toc_ref != NULL)
7840 free (toc_ref);
9a23f96e 7841 htab->do_tls_opt = 1;
b34976b6 7842 return TRUE;
1e2f5b6e 7843}
b34976b6 7844
c5614fa4
AM
7845/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7846 the values of any global symbols in a toc section that has been
7847 edited. Globals in toc sections should be a rarity, so this function
7848 sets a flag if any are found in toc sections other than the one just
de194d85 7849 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
7850
7851struct adjust_toc_info
7852{
7853 asection *toc;
7854 unsigned long *skip;
7855 bfd_boolean global_toc_syms;
7856};
7857
ba761f19
AM
7858enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7859
c5614fa4
AM
7860static bfd_boolean
7861adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7862{
7863 struct ppc_link_hash_entry *eh;
7864 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 7865 unsigned long i;
c5614fa4 7866
c5614fa4
AM
7867 if (h->root.type != bfd_link_hash_defined
7868 && h->root.type != bfd_link_hash_defweak)
7869 return TRUE;
7870
7871 eh = (struct ppc_link_hash_entry *) h;
7872 if (eh->adjust_done)
7873 return TRUE;
7874
7875 if (eh->elf.root.u.def.section == toc_inf->toc)
7876 {
854b41e7
AM
7877 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7878 i = toc_inf->toc->rawsize >> 3;
c5614fa4 7879 else
854b41e7
AM
7880 i = eh->elf.root.u.def.value >> 3;
7881
ba761f19 7882 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 7883 {
4eca0228 7884 _bfd_error_handler
854b41e7
AM
7885 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7886 do
7887 ++i;
ba761f19 7888 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 7889 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 7890 }
854b41e7
AM
7891
7892 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
7893 eh->adjust_done = 1;
7894 }
7895 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7896 toc_inf->global_toc_syms = TRUE;
7897
7898 return TRUE;
7899}
7900
39eeab25
AM
7901/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
7902 on a _LO variety toc/got reloc. */
560c8763
AM
7903
7904static bfd_boolean
39eeab25 7905ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 7906{
39eeab25
AM
7907 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7908 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
7909 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7910 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7911 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7912 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7913 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7914 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7915 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7916 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7917 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7918 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7919 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7920 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7921 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
7922 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7923 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7924 /* Exclude lfqu by testing reloc. If relocs are ever
7925 defined for the reduced D field in psq_lu then those
7926 will need testing too. */
7927 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7928 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7929 && (insn & 1) == 0)
7930 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7931 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7932 /* Exclude stfqu. psq_stu as above for psq_lu. */
7933 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7934 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7935 && (insn & 1) == 0));
560c8763
AM
7936}
7937
c5614fa4
AM
7938/* Examine all relocs referencing .toc sections in order to remove
7939 unused .toc entries. */
7940
7941bfd_boolean
33c0ec9d 7942ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
7943{
7944 bfd *ibfd;
7945 struct adjust_toc_info toc_inf;
67f0cbdb 7946 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 7947
67f0cbdb 7948 htab->do_toc_opt = 1;
c5614fa4 7949 toc_inf.global_toc_syms = TRUE;
c72f2fb2 7950 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
7951 {
7952 asection *toc, *sec;
7953 Elf_Internal_Shdr *symtab_hdr;
7954 Elf_Internal_Sym *local_syms;
425b145b 7955 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
7956 unsigned long *skip, *drop;
7957 unsigned char *used;
7958 unsigned char *keep, last, some_unused;
7959
854b41e7
AM
7960 if (!is_ppc64_elf (ibfd))
7961 continue;
7962
c5614fa4
AM
7963 toc = bfd_get_section_by_name (ibfd, ".toc");
7964 if (toc == NULL
92b7a70f 7965 || toc->size == 0
dbaa2011
AM
7966 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
7967 || discarded_section (toc))
c5614fa4
AM
7968 continue;
7969
425b145b 7970 toc_relocs = NULL;
c5614fa4 7971 local_syms = NULL;
0ffa91dd 7972 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
7973
7974 /* Look at sections dropped from the final link. */
7975 skip = NULL;
7976 relstart = NULL;
7977 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7978 {
7979 if (sec->reloc_count == 0
dbaa2011 7980 || !discarded_section (sec)
c5614fa4
AM
7981 || get_opd_info (sec)
7982 || (sec->flags & SEC_ALLOC) == 0
7983 || (sec->flags & SEC_DEBUGGING) != 0)
7984 continue;
7985
7986 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7987 if (relstart == NULL)
7988 goto error_ret;
7989
7990 /* Run through the relocs to see which toc entries might be
7991 unused. */
7992 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7993 {
7994 enum elf_ppc64_reloc_type r_type;
7995 unsigned long r_symndx;
7996 asection *sym_sec;
7997 struct elf_link_hash_entry *h;
7998 Elf_Internal_Sym *sym;
7999 bfd_vma val;
8000
8001 r_type = ELF64_R_TYPE (rel->r_info);
8002 switch (r_type)
8003 {
8004 default:
8005 continue;
8006
8007 case R_PPC64_TOC16:
8008 case R_PPC64_TOC16_LO:
8009 case R_PPC64_TOC16_HI:
8010 case R_PPC64_TOC16_HA:
8011 case R_PPC64_TOC16_DS:
8012 case R_PPC64_TOC16_LO_DS:
8013 break;
8014 }
8015
8016 r_symndx = ELF64_R_SYM (rel->r_info);
8017 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8018 r_symndx, ibfd))
8019 goto error_ret;
8020
8021 if (sym_sec != toc)
8022 continue;
8023
8024 if (h != NULL)
8025 val = h->root.u.def.value;
8026 else
8027 val = sym->st_value;
8028 val += rel->r_addend;
8029
8030 if (val >= toc->size)
8031 continue;
8032
8033 /* Anything in the toc ought to be aligned to 8 bytes.
8034 If not, don't mark as unused. */
8035 if (val & 7)
8036 continue;
8037
8038 if (skip == NULL)
8039 {
854b41e7 8040 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8041 if (skip == NULL)
8042 goto error_ret;
8043 }
8044
ba761f19 8045 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8046 }
8047
8048 if (elf_section_data (sec)->relocs != relstart)
8049 free (relstart);
8050 }
8051
ba761f19
AM
8052 /* For largetoc loads of address constants, we can convert
8053 . addis rx,2,addr@got@ha
8054 . ld ry,addr@got@l(rx)
8055 to
8056 . addis rx,2,addr@toc@ha
8057 . addi ry,rx,addr@toc@l
8058 when addr is within 2G of the toc pointer. This then means
8059 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8060
ba761f19
AM
8061 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8062 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8063 && toc->reloc_count != 0)
8064 {
8065 /* Read toc relocs. */
425b145b
AM
8066 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8067 info->keep_memory);
8068 if (toc_relocs == NULL)
ba761f19
AM
8069 goto error_ret;
8070
425b145b 8071 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8072 {
8073 enum elf_ppc64_reloc_type r_type;
8074 unsigned long r_symndx;
8075 asection *sym_sec;
8076 struct elf_link_hash_entry *h;
8077 Elf_Internal_Sym *sym;
8078 bfd_vma val, addr;
8079
8080 r_type = ELF64_R_TYPE (rel->r_info);
8081 if (r_type != R_PPC64_ADDR64)
8082 continue;
8083
8084 r_symndx = ELF64_R_SYM (rel->r_info);
8085 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8086 r_symndx, ibfd))
8087 goto error_ret;
8088
425b145b 8089 if (sym_sec == NULL
c27b8c2a 8090 || sym_sec->output_section == NULL
dbaa2011 8091 || discarded_section (sym_sec))
425b145b
AM
8092 continue;
8093
afe397ea 8094 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8095 continue;
8096
8097 if (h != NULL)
bddc25c9
AM
8098 {
8099 if (h->type == STT_GNU_IFUNC)
8100 continue;
8101 val = h->root.u.def.value;
8102 }
ba761f19 8103 else
bddc25c9
AM
8104 {
8105 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8106 continue;
8107 val = sym->st_value;
8108 }
ba761f19
AM
8109 val += rel->r_addend;
8110 val += sym_sec->output_section->vma + sym_sec->output_offset;
8111
8112 /* We don't yet know the exact toc pointer value, but we
8113 know it will be somewhere in the toc section. Don't
8114 optimize if the difference from any possible toc
8115 pointer is outside [ff..f80008000, 7fff7fff]. */
8116 addr = toc->output_section->vma + TOC_BASE_OFF;
8117 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8118 continue;
8119
8120 addr = toc->output_section->vma + toc->output_section->rawsize;
8121 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8122 continue;
8123
8124 if (skip == NULL)
8125 {
8126 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8127 if (skip == NULL)
8128 goto error_ret;
8129 }
8130
8131 skip[rel->r_offset >> 3]
425b145b 8132 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8133 }
ba761f19
AM
8134 }
8135
c5614fa4
AM
8136 if (skip == NULL)
8137 continue;
8138
8139 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8140 if (used == NULL)
8141 {
8142 error_ret:
8143 if (local_syms != NULL
8144 && symtab_hdr->contents != (unsigned char *) local_syms)
8145 free (local_syms);
8146 if (sec != NULL
8147 && relstart != NULL
8148 && elf_section_data (sec)->relocs != relstart)
8149 free (relstart);
425b145b
AM
8150 if (toc_relocs != NULL
8151 && elf_section_data (toc)->relocs != toc_relocs)
8152 free (toc_relocs);
c5614fa4
AM
8153 if (skip != NULL)
8154 free (skip);
8155 return FALSE;
8156 }
8157
30038c59
AM
8158 /* Now check all kept sections that might reference the toc.
8159 Check the toc itself last. */
8160 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8161 : ibfd->sections);
c5614fa4 8162 sec != NULL;
c5614fa4 8163 sec = (sec == toc ? NULL
c5614fa4 8164 : sec->next == NULL ? toc
30038c59 8165 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8166 : sec->next))
8167 {
8168 int repeat;
8169
8170 if (sec->reloc_count == 0
dbaa2011 8171 || discarded_section (sec)
c5614fa4
AM
8172 || get_opd_info (sec)
8173 || (sec->flags & SEC_ALLOC) == 0
8174 || (sec->flags & SEC_DEBUGGING) != 0)
8175 continue;
8176
854b41e7
AM
8177 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8178 info->keep_memory);
c5614fa4 8179 if (relstart == NULL)
2915c55b
JK
8180 {
8181 free (used);
8182 goto error_ret;
8183 }
c5614fa4
AM
8184
8185 /* Mark toc entries referenced as used. */
c5614fa4 8186 do
d4f1ee75
AM
8187 {
8188 repeat = 0;
8189 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8190 {
8191 enum elf_ppc64_reloc_type r_type;
8192 unsigned long r_symndx;
8193 asection *sym_sec;
8194 struct elf_link_hash_entry *h;
8195 Elf_Internal_Sym *sym;
8196 bfd_vma val;
8197 enum {no_check, check_lo, check_ha} insn_check;
98528052 8198
d4f1ee75
AM
8199 r_type = ELF64_R_TYPE (rel->r_info);
8200 switch (r_type)
8201 {
8202 default:
8203 insn_check = no_check;
8204 break;
98528052 8205
d4f1ee75
AM
8206 case R_PPC64_GOT_TLSLD16_HA:
8207 case R_PPC64_GOT_TLSGD16_HA:
8208 case R_PPC64_GOT_TPREL16_HA:
8209 case R_PPC64_GOT_DTPREL16_HA:
8210 case R_PPC64_GOT16_HA:
8211 case R_PPC64_TOC16_HA:
8212 insn_check = check_ha;
8213 break;
98528052 8214
d4f1ee75
AM
8215 case R_PPC64_GOT_TLSLD16_LO:
8216 case R_PPC64_GOT_TLSGD16_LO:
8217 case R_PPC64_GOT_TPREL16_LO_DS:
8218 case R_PPC64_GOT_DTPREL16_LO_DS:
8219 case R_PPC64_GOT16_LO:
8220 case R_PPC64_GOT16_LO_DS:
8221 case R_PPC64_TOC16_LO:
8222 case R_PPC64_TOC16_LO_DS:
8223 insn_check = check_lo;
8224 break;
8225 }
560c8763 8226
d4f1ee75
AM
8227 if (insn_check != no_check)
8228 {
8229 bfd_vma off = rel->r_offset & ~3;
8230 unsigned char buf[4];
8231 unsigned int insn;
c5614fa4 8232
d4f1ee75
AM
8233 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8234 {
8235 free (used);
8236 goto error_ret;
8237 }
8238 insn = bfd_get_32 (ibfd, buf);
8239 if (insn_check == check_lo
39eeab25 8240 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
8241 : ((insn & ((0x3f << 26) | 0x1f << 16))
8242 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8243 {
8244 char str[12];
8245
8246 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8247 sprintf (str, "%#08x", insn);
8248 info->callbacks->einfo
695344c0 8249 /* xgettext:c-format */
174d0a74 8250 (_("%H: toc optimization is not supported for"
cf97bcb0 8251 " %s instruction\n"),
d4f1ee75
AM
8252 ibfd, sec, rel->r_offset & ~3, str);
8253 }
8254 }
c5614fa4 8255
d4f1ee75
AM
8256 switch (r_type)
8257 {
8258 case R_PPC64_TOC16:
8259 case R_PPC64_TOC16_LO:
8260 case R_PPC64_TOC16_HI:
8261 case R_PPC64_TOC16_HA:
8262 case R_PPC64_TOC16_DS:
8263 case R_PPC64_TOC16_LO_DS:
8264 /* In case we're taking addresses of toc entries. */
8265 case R_PPC64_ADDR64:
8266 break;
c5614fa4 8267
d4f1ee75
AM
8268 default:
8269 continue;
8270 }
c5614fa4 8271
d4f1ee75
AM
8272 r_symndx = ELF64_R_SYM (rel->r_info);
8273 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8274 r_symndx, ibfd))
8275 {
8276 free (used);
8277 goto error_ret;
8278 }
c5614fa4 8279
d4f1ee75
AM
8280 if (sym_sec != toc)
8281 continue;
c5614fa4 8282
d4f1ee75
AM
8283 if (h != NULL)
8284 val = h->root.u.def.value;
8285 else
8286 val = sym->st_value;
8287 val += rel->r_addend;
ba761f19 8288
d4f1ee75
AM
8289 if (val >= toc->size)
8290 continue;
ba761f19 8291
d4f1ee75
AM
8292 if ((skip[val >> 3] & can_optimize) != 0)
8293 {
8294 bfd_vma off;
8295 unsigned char opc;
8296
8297 switch (r_type)
8298 {
8299 case R_PPC64_TOC16_HA:
ba761f19 8300 break;
ba761f19 8301
d4f1ee75
AM
8302 case R_PPC64_TOC16_LO_DS:
8303 off = rel->r_offset;
8304 off += (bfd_big_endian (ibfd) ? -2 : 3);
8305 if (!bfd_get_section_contents (ibfd, sec, &opc,
8306 off, 1))
8307 {
8308 free (used);
8309 goto error_ret;
8310 }
8311 if ((opc & (0x3f << 2)) == (58u << 2))
8312 break;
1a0670f3 8313 /* Fall through. */
ba761f19 8314
d4f1ee75
AM
8315 default:
8316 /* Wrong sort of reloc, or not a ld. We may
8317 as well clear ref_from_discarded too. */
8318 skip[val >> 3] = 0;
8319 }
8320 }
8321
8322 if (sec != toc)
8323 used[val >> 3] = 1;
8324 /* For the toc section, we only mark as used if this
8325 entry itself isn't unused. */
8326 else if ((used[rel->r_offset >> 3]
8327 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8328 && !used[val >> 3])
8329 {
8330 /* Do all the relocs again, to catch reference
8331 chains. */
8332 repeat = 1;
8333 used[val >> 3] = 1;
8334 }
8335 }
8336 }
c5614fa4 8337 while (repeat);
854b41e7
AM
8338
8339 if (elf_section_data (sec)->relocs != relstart)
8340 free (relstart);
c5614fa4
AM
8341 }
8342
8343 /* Merge the used and skip arrays. Assume that TOC
8344 doublewords not appearing as either used or unused belong
de194d85 8345 to an entry more than one doubleword in size. */
c5614fa4
AM
8346 for (drop = skip, keep = used, last = 0, some_unused = 0;
8347 drop < skip + (toc->size + 7) / 8;
8348 ++drop, ++keep)
8349 {
8350 if (*keep)
8351 {
ba761f19
AM
8352 *drop &= ~ref_from_discarded;
8353 if ((*drop & can_optimize) != 0)
8354 some_unused = 1;
c5614fa4
AM
8355 last = 0;
8356 }
b140b010 8357 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8358 {
8359 some_unused = 1;
ba761f19 8360 last = ref_from_discarded;
c5614fa4
AM
8361 }
8362 else
8363 *drop = last;
8364 }
8365
8366 free (used);
8367
8368 if (some_unused)
8369 {
8370 bfd_byte *contents, *src;
8371 unsigned long off;
d62b3684 8372 Elf_Internal_Sym *sym;
ba761f19 8373 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8374
8375 /* Shuffle the toc contents, and at the same time convert the
8376 skip array from booleans into offsets. */
8377 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8378 goto error_ret;
8379
8380 elf_section_data (toc)->this_hdr.contents = contents;
8381
8382 for (src = contents, off = 0, drop = skip;
8383 src < contents + toc->size;
8384 src += 8, ++drop)
8385 {
ba761f19
AM
8386 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8387 off += 8;
c5614fa4
AM
8388 else if (off != 0)
8389 {
8390 *drop = off;
8391 memcpy (src - off, src, 8);
8392 }
8393 }
854b41e7 8394 *drop = off;
c5614fa4
AM
8395 toc->rawsize = toc->size;
8396 toc->size = src - contents - off;
8397
ba761f19
AM
8398 /* Adjust addends for relocs against the toc section sym,
8399 and optimize any accesses we can. */
c5614fa4
AM
8400 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8401 {
8402 if (sec->reloc_count == 0
dbaa2011 8403 || discarded_section (sec))
c5614fa4
AM
8404 continue;
8405
8406 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8407 info->keep_memory);
c5614fa4
AM
8408 if (relstart == NULL)
8409 goto error_ret;
8410
8411 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8412 {
8413 enum elf_ppc64_reloc_type r_type;
8414 unsigned long r_symndx;
8415 asection *sym_sec;
8416 struct elf_link_hash_entry *h;
854b41e7 8417 bfd_vma val;
c5614fa4
AM
8418
8419 r_type = ELF64_R_TYPE (rel->r_info);
8420 switch (r_type)
8421 {
8422 default:
8423 continue;
8424
8425 case R_PPC64_TOC16:
8426 case R_PPC64_TOC16_LO:
8427 case R_PPC64_TOC16_HI:
8428 case R_PPC64_TOC16_HA:
8429 case R_PPC64_TOC16_DS:
8430 case R_PPC64_TOC16_LO_DS:
8431 case R_PPC64_ADDR64:
8432 break;
8433 }
8434
8435 r_symndx = ELF64_R_SYM (rel->r_info);
8436 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8437 r_symndx, ibfd))
8438 goto error_ret;
8439
ba761f19 8440 if (sym_sec != toc)
c5614fa4
AM
8441 continue;
8442
ba761f19
AM
8443 if (h != NULL)
8444 val = h->root.u.def.value;
8445 else
8446 {
8447 val = sym->st_value;
8448 if (val != 0)
8449 local_toc_syms = TRUE;
8450 }
8451
8452 val += rel->r_addend;
854b41e7
AM
8453
8454 if (val > toc->rawsize)
8455 val = toc->rawsize;
ba761f19
AM
8456 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8457 continue;
8458 else if ((skip[val >> 3] & can_optimize) != 0)
8459 {
8460 Elf_Internal_Rela *tocrel
425b145b 8461 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8462 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8463
8464 switch (r_type)
8465 {
8466 case R_PPC64_TOC16_HA:
8467 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8468 break;
8469
8470 case R_PPC64_TOC16_LO_DS:
8471 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8472 break;
8473
8474 default:
28942f62
AM
8475 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8476 ppc_howto_init ();
b140b010 8477 info->callbacks->einfo
695344c0 8478 /* xgettext:c-format */
174d0a74 8479 (_("%H: %s references "
b140b010
AM
8480 "optimized away TOC entry\n"),
8481 ibfd, sec, rel->r_offset,
8482 ppc64_elf_howto_table[r_type]->name);
8483 bfd_set_error (bfd_error_bad_value);
8484 goto error_ret;
ba761f19
AM
8485 }
8486 rel->r_addend = tocrel->r_addend;
8487 elf_section_data (sec)->relocs = relstart;
8488 continue;
8489 }
8490
8491 if (h != NULL || sym->st_value != 0)
8492 continue;
854b41e7
AM
8493
8494 rel->r_addend -= skip[val >> 3];
8495 elf_section_data (sec)->relocs = relstart;
c5614fa4 8496 }
854b41e7
AM
8497
8498 if (elf_section_data (sec)->relocs != relstart)
8499 free (relstart);
c5614fa4
AM
8500 }
8501
8502 /* We shouldn't have local or global symbols defined in the TOC,
8503 but handle them anyway. */
df22d223
AM
8504 if (local_syms != NULL)
8505 for (sym = local_syms;
8506 sym < local_syms + symtab_hdr->sh_info;
8507 ++sym)
8508 if (sym->st_value != 0
8509 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8510 {
8511 unsigned long i;
854b41e7 8512
df22d223
AM
8513 if (sym->st_value > toc->rawsize)
8514 i = toc->rawsize >> 3;
8515 else
8516 i = sym->st_value >> 3;
854b41e7 8517
df22d223
AM
8518 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8519 {
8520 if (local_toc_syms)
4eca0228 8521 _bfd_error_handler
df22d223
AM
8522 (_("%s defined on removed toc entry"),
8523 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8524 do
8525 ++i;
8526 while ((skip[i] & (ref_from_discarded | can_optimize)));
8527 sym->st_value = (bfd_vma) i << 3;
8528 }
d62b3684 8529
df22d223
AM
8530 sym->st_value -= skip[i];
8531 symtab_hdr->contents = (unsigned char *) local_syms;
8532 }
c5614fa4 8533
854b41e7 8534 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8535 if (toc_inf.global_toc_syms)
8536 {
8537 toc_inf.toc = toc;
8538 toc_inf.skip = skip;
8539 toc_inf.global_toc_syms = FALSE;
8540 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8541 &toc_inf);
8542 }
854b41e7
AM
8543
8544 if (toc->reloc_count != 0)
8545 {
d4730f92 8546 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8547 Elf_Internal_Rela *wrel;
8548 bfd_size_type sz;
8549
854b41e7 8550 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8551 if (toc_relocs == NULL)
8552 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8553 info->keep_memory);
8554 if (toc_relocs == NULL)
8555 goto error_ret;
8556
425b145b
AM
8557 wrel = toc_relocs;
8558 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8559 if ((skip[rel->r_offset >> 3]
8560 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8561 {
8562 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8563 wrel->r_info = rel->r_info;
8564 wrel->r_addend = rel->r_addend;
8565 ++wrel;
8566 }
8567 else if (!dec_dynrel_count (rel->r_info, toc, info,
8568 &local_syms, NULL, NULL))
8569 goto error_ret;
8570
425b145b
AM
8571 elf_section_data (toc)->relocs = toc_relocs;
8572 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8573 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8574 sz = rel_hdr->sh_entsize;
8575 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8576 }
c5614fa4 8577 }
28be611c
AM
8578 else if (toc_relocs != NULL
8579 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8580 free (toc_relocs);
c5614fa4
AM
8581
8582 if (local_syms != NULL
8583 && symtab_hdr->contents != (unsigned char *) local_syms)
8584 {
8585 if (!info->keep_memory)
8586 free (local_syms);
8587 else
8588 symtab_hdr->contents = (unsigned char *) local_syms;
8589 }
8590 free (skip);
8591 }
8592
066f4018
AM
8593 /* Look for cases where we can change an indirect GOT access to
8594 a GOT relative access, possibly reducing the number of GOT
8595 entries. */
8596 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8597 {
8598 asection *sec;
8599 Elf_Internal_Shdr *symtab_hdr;
8600 Elf_Internal_Sym *local_syms;
8601 Elf_Internal_Rela *relstart, *rel;
8602 bfd_vma got;
8603
8604 if (!is_ppc64_elf (ibfd))
8605 continue;
8606
8607 if (!ppc64_elf_tdata (ibfd)->has_gotrel)
8608 continue;
8609
8610 sec = ppc64_elf_tdata (ibfd)->got;
8611 got = sec->output_section->vma + sec->output_offset + 0x8000;
8612
8613 local_syms = NULL;
8614 symtab_hdr = &elf_symtab_hdr (ibfd);
8615
8616 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8617 {
8618 if (sec->reloc_count == 0
8619 || !ppc64_elf_section_data (sec)->has_gotrel
8620 || discarded_section (sec))
8621 continue;
8622
8623 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8624 info->keep_memory);
8625 if (relstart == NULL)
8626 {
8627 got_error_ret:
8628 if (local_syms != NULL
8629 && symtab_hdr->contents != (unsigned char *) local_syms)
8630 free (local_syms);
8631 if (sec != NULL
8632 && relstart != NULL
8633 && elf_section_data (sec)->relocs != relstart)
8634 free (relstart);
8635 return FALSE;
8636 }
8637
8638 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8639 {
8640 enum elf_ppc64_reloc_type r_type;
8641 unsigned long r_symndx;
8642 Elf_Internal_Sym *sym;
8643 asection *sym_sec;
8644 struct elf_link_hash_entry *h;
8645 struct got_entry *ent;
8646 bfd_vma val;
8647 unsigned char buf[4];
8648 unsigned int insn;
8649
8650 r_type = ELF64_R_TYPE (rel->r_info);
8651 switch (r_type)
8652 {
8653 default:
8654 continue;
8655
8656 case R_PPC64_GOT16_DS:
8657 case R_PPC64_GOT16_HA:
8658 case R_PPC64_GOT16_LO_DS:
8659 break;
8660 }
8661
8662 r_symndx = ELF64_R_SYM (rel->r_info);
8663 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8664 r_symndx, ibfd))
8665 goto got_error_ret;
8666
8667 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8668 continue;
8669
8670 if (h != NULL)
8671 val = h->root.u.def.value;
8672 else
8673 val = sym->st_value;
8674 val += rel->r_addend;
8675 val += sym_sec->output_section->vma + sym_sec->output_offset;
8676
8677 switch (r_type)
8678 {
8679 default:
8680 continue;
8681
8682 case R_PPC64_GOT16_DS:
8683 if (val - got + 0x8000 >= 0x10000)
8684 continue;
8685 if (!bfd_get_section_contents (ibfd, sec, buf,
8686 rel->r_offset & ~3, 4))
8687 goto got_error_ret;
8688 insn = bfd_get_32 (ibfd, buf);
8689 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
8690 continue;
8691 break;
8692
8693 case R_PPC64_GOT16_HA:
8694 if (val - got + 0x80008000ULL >= 0x100000000ULL)
8695 continue;
8696
8697 if (!bfd_get_section_contents (ibfd, sec, buf,
8698 rel->r_offset & ~3, 4))
8699 goto got_error_ret;
8700 insn = bfd_get_32 (ibfd, buf);
8701 if (((insn & ((0x3f << 26) | 0x1f << 16))
8702 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8703 continue;
8704 break;
8705
8706 case R_PPC64_GOT16_LO_DS:
8707 if (val - got + 0x80008000ULL >= 0x100000000ULL)
8708 continue;
8709 if (!bfd_get_section_contents (ibfd, sec, buf,
8710 rel->r_offset & ~3, 4))
8711 goto got_error_ret;
8712 insn = bfd_get_32 (ibfd, buf);
8713 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
8714 continue;
8715 break;
8716 }
8717
8718 if (h != NULL)
8719 ent = h->got.glist;
8720 else
8721 {
8722 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
8723 ent = local_got_ents[r_symndx];
8724 }
8725 for (; ent != NULL; ent = ent->next)
8726 if (ent->addend == rel->r_addend
8727 && ent->owner == ibfd
8728 && ent->tls_type == 0)
8729 break;
8730 BFD_ASSERT (ent && ent->got.refcount > 0);
8731 ent->got.refcount -= 1;
8732 }
8733
8734 if (elf_section_data (sec)->relocs != relstart)
8735 free (relstart);
8736 }
8737
8738 if (local_syms != NULL
8739 && symtab_hdr->contents != (unsigned char *) local_syms)
8740 {
8741 if (!info->keep_memory)
8742 free (local_syms);
8743 else
8744 symtab_hdr->contents = (unsigned char *) local_syms;
8745 }
8746 }
8747
c5614fa4
AM
8748 return TRUE;
8749}
8750
1bbe0902
AM
8751/* Return true iff input section I references the TOC using
8752 instructions limited to +/-32k offsets. */
8753
8754bfd_boolean
8755ppc64_elf_has_small_toc_reloc (asection *i)
8756{
8757 return (is_ppc64_elf (i->owner)
8758 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8759}
8760
927be08e
AM
8761/* Allocate space for one GOT entry. */
8762
8763static void
8764allocate_got (struct elf_link_hash_entry *h,
8765 struct bfd_link_info *info,
8766 struct got_entry *gent)
8767{
8768 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
8769 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8770 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8771 ? 16 : 8);
8772 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8773 ? 2 : 1) * sizeof (Elf64_External_Rela);
8774 asection *got = ppc64_elf_tdata (gent->owner)->got;
8775
8776 gent->got.offset = got->size;
8777 got->size += entsize;
8778
19e08130 8779 if (h->type == STT_GNU_IFUNC)
927be08e 8780 {
33e44f2e 8781 htab->elf.irelplt->size += rentsize;
19e08130 8782 htab->got_reli_size += rentsize;
927be08e 8783 }
f15d0b54
AM
8784 else if (((bfd_link_pic (info)
8785 && !((gent->tls_type & TLS_TPREL) != 0
8786 && bfd_link_executable (info)
8787 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
8788 || (htab->elf.dynamic_sections_created
8789 && h->dynindx != -1
8790 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 8791 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 8792 {
19e08130 8793 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 8794 relgot->size += rentsize;
927be08e
AM
8795 }
8796}
8797
7865406b
AM
8798/* This function merges got entries in the same toc group. */
8799
8800static void
8801merge_got_entries (struct got_entry **pent)
8802{
8803 struct got_entry *ent, *ent2;
8804
8805 for (ent = *pent; ent != NULL; ent = ent->next)
8806 if (!ent->is_indirect)
8807 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8808 if (!ent2->is_indirect
8809 && ent2->addend == ent->addend
8810 && ent2->tls_type == ent->tls_type
8811 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8812 {
8813 ent2->is_indirect = TRUE;
8814 ent2->got.ent = ent;
8815 }
8816}
8817
46434633 8818/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
8819
8820static bfd_boolean
46434633
AM
8821ensure_undef_dynamic (struct bfd_link_info *info,
8822 struct elf_link_hash_entry *h)
f0158f44
AM
8823{
8824 struct elf_link_hash_table *htab = elf_hash_table (info);
8825
8826 if (htab->dynamic_sections_created
46434633
AM
8827 && ((info->dynamic_undefined_weak != 0
8828 && h->root.type == bfd_link_hash_undefweak)
8829 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
8830 && h->dynindx == -1
8831 && !h->forced_local
8832 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
8833 return bfd_elf_link_record_dynamic_symbol (info, h);
8834 return TRUE;
8835}
8836
65f38f15
AM
8837/* Allocate space in .plt, .got and associated reloc sections for
8838 dynamic relocs. */
5bd4f169 8839
b34976b6 8840static bfd_boolean
4ce794b7 8841allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 8842{
65f38f15
AM
8843 struct bfd_link_info *info;
8844 struct ppc_link_hash_table *htab;
5bd4f169 8845 asection *s;
65f38f15 8846 struct ppc_link_hash_entry *eh;
0b8bcf0d 8847 struct got_entry **pgent, *gent;
5bd4f169 8848
e92d460e 8849 if (h->root.type == bfd_link_hash_indirect)
b34976b6 8850 return TRUE;
5bd4f169 8851
65f38f15
AM
8852 info = (struct bfd_link_info *) inf;
8853 htab = ppc_hash_table (info);
4dfe6ac6
NC
8854 if (htab == NULL)
8855 return FALSE;
5bd4f169 8856
951fd09b
AM
8857 eh = (struct ppc_link_hash_entry *) h;
8858 /* Run through the TLS GD got entries first if we're changing them
8859 to TPREL. */
37da22e5 8860 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
951fd09b
AM
8861 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8862 if (gent->got.refcount > 0
8863 && (gent->tls_type & TLS_GD) != 0)
8864 {
8865 /* This was a GD entry that has been converted to TPREL. If
8866 there happens to be a TPREL entry we can use that one. */
8867 struct got_entry *ent;
8868 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8869 if (ent->got.refcount > 0
8870 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
8871 && ent->addend == gent->addend
8872 && ent->owner == gent->owner)
951fd09b
AM
8873 {
8874 gent->got.refcount = 0;
8875 break;
8876 }
8877
8878 /* If not, then we'll be using our own TPREL entry. */
8879 if (gent->got.refcount != 0)
8880 gent->tls_type = TLS_TLS | TLS_TPREL;
8881 }
8882
7865406b
AM
8883 /* Remove any list entry that won't generate a word in the GOT before
8884 we call merge_got_entries. Otherwise we risk merging to empty
8885 entries. */
0b8bcf0d
AM
8886 pgent = &h->got.glist;
8887 while ((gent = *pgent) != NULL)
411e1bfb 8888 if (gent->got.refcount > 0)
7865406b
AM
8889 {
8890 if ((gent->tls_type & TLS_LD) != 0
8891 && !h->def_dynamic)
8892 {
8893 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8894 *pgent = gent->next;
8895 }
8896 else
8897 pgent = &gent->next;
8898 }
8899 else
8900 *pgent = gent->next;
8901
8902 if (!htab->do_multi_toc)
8903 merge_got_entries (&h->got.glist);
8904
8905 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8906 if (!gent->is_indirect)
411e1bfb 8907 {
46434633
AM
8908 /* Make sure this symbol is output as a dynamic symbol. */
8909 if (!ensure_undef_dynamic (info, h))
f0158f44 8910 return FALSE;
65f38f15 8911
0c8d6e5c 8912 if (!is_ppc64_elf (gent->owner))
927be08e 8913 abort ();
0ffa91dd 8914
927be08e 8915 allocate_got (h, info, gent);
411e1bfb 8916 }
65f38f15 8917
954b63d4
AM
8918 /* If no dynamic sections we can't have dynamic relocs, except for
8919 IFUNCs which are handled even in static executables. */
8a2058b5
AM
8920 if (!htab->elf.dynamic_sections_created
8921 && h->type != STT_GNU_IFUNC)
8922 eh->dyn_relocs = NULL;
8923
529fe20e
AM
8924 /* Discard relocs on undefined symbols that must be local. */
8925 else if (h->root.type == bfd_link_hash_undefined
8926 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8927 eh->dyn_relocs = NULL;
8928
954b63d4
AM
8929 /* Also discard relocs on undefined weak syms with non-default
8930 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 8931 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
8932 eh->dyn_relocs = NULL;
8933
8a2058b5 8934 if (eh->dyn_relocs != NULL)
65f38f15 8935 {
8a2058b5
AM
8936 struct elf_dyn_relocs *p, **pp;
8937
57e7d118
AM
8938 /* In the shared -Bsymbolic case, discard space allocated for
8939 dynamic pc-relative relocs against symbols which turn out to
8940 be defined in regular objects. For the normal shared case,
8941 discard space for relocs that have become local due to symbol
8942 visibility changes. */
8943
8944 if (bfd_link_pic (info))
65f38f15 8945 {
57e7d118
AM
8946 /* Relocs that use pc_count are those that appear on a call
8947 insn, or certain REL relocs (see must_be_dyn_reloc) that
8948 can be generated via assembly. We want calls to
8949 protected symbols to resolve directly to the function
8950 rather than going via the plt. If people want function
8951 pointer comparisons to work as expected then they should
8952 avoid writing weird assembly. */
8953 if (SYMBOL_CALLS_LOCAL (info, h))
8954 {
57e7d118
AM
8955 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8956 {
8957 p->count -= p->pc_count;
8958 p->pc_count = 0;
8959 if (p->count == 0)
8960 *pp = p->next;
8961 else
8962 pp = &p->next;
8963 }
8964 }
65f38f15 8965
954b63d4 8966 if (eh->dyn_relocs != NULL)
5bd4f169 8967 {
46434633
AM
8968 /* Make sure this symbol is output as a dynamic symbol. */
8969 if (!ensure_undef_dynamic (info, h))
f0158f44 8970 return FALSE;
5bd4f169 8971 }
65f38f15 8972 }
529fe20e 8973 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 8974 {
8a2058b5 8975 /* For the non-pic case, discard space for relocs against
57e7d118
AM
8976 symbols which turn out to need copy relocs or are not
8977 dynamic. */
529fe20e
AM
8978 if (h->dynamic_adjusted
8979 && !h->def_regular
8980 && !ELF_COMMON_DEF_P (h))
f0158f44 8981 {
46434633
AM
8982 /* Make sure this symbol is output as a dynamic symbol. */
8983 if (!ensure_undef_dynamic (info, h))
f0158f44 8984 return FALSE;
dfbb6ac9 8985
f0158f44
AM
8986 if (h->dynindx == -1)
8987 eh->dyn_relocs = NULL;
8988 }
8989 else
8a2058b5 8990 eh->dyn_relocs = NULL;
57e7d118
AM
8991 }
8992
8993 /* Finally, allocate space. */
8994 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8995 {
8996 asection *sreloc = elf_section_data (p->sec)->sreloc;
8997 if (eh->elf.type == STT_GNU_IFUNC)
8998 sreloc = htab->elf.irelplt;
8999 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9000 }
65f38f15 9001 }
57e7d118 9002
2d7ad24e
AM
9003 /* We might need a PLT entry when the symbol
9004 a) is dynamic, or
9005 b) is an ifunc, or
9006 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9007 d) has plt16 relocs and we are linking statically. */
9008 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9009 || h->type == STT_GNU_IFUNC
9010 || (h->needs_plt && h->dynamic_adjusted)
9011 || (h->needs_plt
9012 && h->def_regular
9013 && !htab->elf.dynamic_sections_created
3e04d765 9014 && !htab->can_convert_all_inline_plt
2d7ad24e
AM
9015 && (((struct ppc_link_hash_entry *) h)->tls_mask
9016 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 9017 {
57e7d118
AM
9018 struct plt_entry *pent;
9019 bfd_boolean doneone = FALSE;
9020 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9021 if (pent->plt.refcount > 0)
9022 {
9023 if (!htab->elf.dynamic_sections_created
9024 || h->dynindx == -1)
9025 {
2d7ad24e
AM
9026 if (h->type == STT_GNU_IFUNC)
9027 {
9028 s = htab->elf.iplt;
9029 pent->plt.offset = s->size;
9030 s->size += PLT_ENTRY_SIZE (htab);
9031 s = htab->elf.irelplt;
9032 }
9033 else
9034 {
9035 s = htab->pltlocal;
9036 pent->plt.offset = s->size;
9037 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9038 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9039 }
57e7d118
AM
9040 }
9041 else
9042 {
9043 /* If this is the first .plt entry, make room for the special
9044 first entry. */
9045 s = htab->elf.splt;
9046 if (s->size == 0)
9047 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9048
57e7d118 9049 pent->plt.offset = s->size;
65f38f15 9050
57e7d118
AM
9051 /* Make room for this entry. */
9052 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9053
57e7d118
AM
9054 /* Make room for the .glink code. */
9055 s = htab->glink;
9056 if (s->size == 0)
9e390558 9057 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9058 if (htab->opd_abi)
9059 {
9060 /* We need bigger stubs past index 32767. */
9e390558 9061 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9062 s->size += 4;
9063 s->size += 2*4;
9064 }
9065 else
9066 s->size += 4;
65f38f15 9067
57e7d118
AM
9068 /* We also need to make an entry in the .rela.plt section. */
9069 s = htab->elf.srelplt;
9070 }
2d7ad24e
AM
9071 if (s != NULL)
9072 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
9073 doneone = TRUE;
9074 }
9075 else
9076 pent->plt.offset = (bfd_vma) -1;
9077 if (!doneone)
9078 {
9079 h->plt.plist = NULL;
9080 h->needs_plt = 0;
9081 }
65f38f15 9082 }
57e7d118 9083 else
65f38f15 9084 {
57e7d118
AM
9085 h->plt.plist = NULL;
9086 h->needs_plt = 0;
65f38f15
AM
9087 }
9088
b34976b6 9089 return TRUE;
65f38f15
AM
9090}
9091
9e390558
AM
9092#define PPC_LO(v) ((v) & 0xffff)
9093#define PPC_HI(v) (((v) >> 16) & 0xffff)
9094#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9095
a345bc8d
AM
9096/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9097 to set up space for global entry stubs. These are put in glink,
9098 after the branch table. */
65f38f15 9099
b34976b6 9100static bfd_boolean
a345bc8d 9101size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9102{
a345bc8d
AM
9103 struct bfd_link_info *info;
9104 struct ppc_link_hash_table *htab;
9105 struct plt_entry *pent;
9e390558 9106 asection *s, *plt;
65f38f15 9107
a345bc8d
AM
9108 if (h->root.type == bfd_link_hash_indirect)
9109 return TRUE;
65f38f15 9110
a345bc8d
AM
9111 if (!h->pointer_equality_needed)
9112 return TRUE;
65f38f15 9113
a345bc8d
AM
9114 if (h->def_regular)
9115 return TRUE;
65f38f15 9116
a345bc8d
AM
9117 info = inf;
9118 htab = ppc_hash_table (info);
9119 if (htab == NULL)
9120 return FALSE;
9121
9e390558
AM
9122 s = htab->global_entry;
9123 plt = htab->elf.splt;
a345bc8d
AM
9124 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9125 if (pent->plt.offset != (bfd_vma) -1
9126 && pent->addend == 0)
9127 {
afe397ea
AM
9128 /* For ELFv2, if this symbol is not defined in a regular file
9129 and we are not generating a shared library or pie, then we
9130 need to define the symbol in the executable on a call stub.
9131 This is to avoid text relocations. */
9e390558
AM
9132 bfd_vma off, stub_align, stub_off, stub_size;
9133 unsigned int align_power;
9134
9135 stub_size = 16;
9136 stub_off = s->size;
9137 if (htab->params->plt_stub_align >= 0)
9138 align_power = htab->params->plt_stub_align;
9139 else
9140 align_power = -htab->params->plt_stub_align;
9141 /* Setting section alignment is delayed until we know it is
9142 non-empty. Otherwise the .text output section will be
9143 aligned at least to plt_stub_align even when no global
9144 entry stubs are needed. */
9145 if (s->alignment_power < align_power)
9146 s->alignment_power = align_power;
9147 stub_align = (bfd_vma) 1 << align_power;
9148 if (htab->params->plt_stub_align >= 0
9149 || ((((stub_off + stub_size - 1) & -stub_align)
9150 - (stub_off & -stub_align))
9151 > ((stub_size - 1) & -stub_align)))
9152 stub_off = (stub_off + stub_align - 1) & -stub_align;
9153 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9154 off -= stub_off + s->output_offset + s->output_section->vma;
9155 /* Note that for --plt-stub-align negative we have a possible
9156 dependency between stub offset and size. Break that
9157 dependency by assuming the max stub size when calculating
9158 the stub offset. */
9159 if (PPC_HA (off) == 0)
9160 stub_size -= 4;
8a2058b5 9161 h->root.type = bfd_link_hash_defined;
afe397ea 9162 h->root.u.def.section = s;
9e390558
AM
9163 h->root.u.def.value = stub_off;
9164 s->size = stub_off + stub_size;
a345bc8d
AM
9165 break;
9166 }
9167 return TRUE;
9168}
9169
9170/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9171 read-only sections. */
9172
9173static bfd_boolean
98bbb1b8 9174maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 9175{
98bbb1b8
AM
9176 asection *sec;
9177
a345bc8d
AM
9178 if (h->root.type == bfd_link_hash_indirect)
9179 return TRUE;
9180
98bbb1b8
AM
9181 sec = readonly_dynrelocs (h);
9182 if (sec != NULL)
a345bc8d 9183 {
98bbb1b8
AM
9184 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9185
9186 info->flags |= DF_TEXTREL;
2cdcc330
AM
9187 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9188 " in read-only section `%pA'\n"),
9189 sec->owner, h->root.root.string, sec);
a345bc8d
AM
9190
9191 /* Not an error, just cut short the traversal. */
9192 return FALSE;
65f38f15 9193 }
b34976b6 9194 return TRUE;
65f38f15
AM
9195}
9196
9197/* Set the sizes of the dynamic sections. */
9198
b34976b6 9199static bfd_boolean
ee67d69a 9200ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9201 struct bfd_link_info *info)
65f38f15
AM
9202{
9203 struct ppc_link_hash_table *htab;
9204 bfd *dynobj;
9205 asection *s;
b34976b6 9206 bfd_boolean relocs;
65f38f15 9207 bfd *ibfd;
7865406b 9208 struct got_entry *first_tlsld;
65f38f15
AM
9209
9210 htab = ppc_hash_table (info);
4dfe6ac6
NC
9211 if (htab == NULL)
9212 return FALSE;
9213
65f38f15
AM
9214 dynobj = htab->elf.dynobj;
9215 if (dynobj == NULL)
9216 abort ();
9217
9218 if (htab->elf.dynamic_sections_created)
9219 {
9220 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9221 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9222 {
3d4d4302 9223 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9224 if (s == NULL)
9225 abort ();
eea6121a 9226 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9227 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9228 }
9229 }
9230
9231 /* Set up .got offsets for local syms, and space for local dynamic
9232 relocs. */
c72f2fb2 9233 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9234 {
411e1bfb
AM
9235 struct got_entry **lgot_ents;
9236 struct got_entry **end_lgot_ents;
e054468f
AM
9237 struct plt_entry **local_plt;
9238 struct plt_entry **end_local_plt;
f961d9dd 9239 unsigned char *lgot_masks;
65f38f15
AM
9240 bfd_size_type locsymcount;
9241 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9242
0c8d6e5c 9243 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9244 continue;
9245
9246 for (s = ibfd->sections; s != NULL; s = s->next)
9247 {
19e08130 9248 struct ppc_dyn_relocs *p;
65f38f15 9249
6edfbbad 9250 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9251 {
ec338859
AM
9252 if (!bfd_is_abs_section (p->sec)
9253 && bfd_is_abs_section (p->sec->output_section))
9254 {
9255 /* Input section has been discarded, either because
9256 it is a copy of a linkonce section or due to
9257 linker script /DISCARD/, so we'll be discarding
9258 the relocs too. */
9259 }
248866a8 9260 else if (p->count != 0)
ec338859 9261 {
19e08130
AM
9262 asection *srel = elf_section_data (p->sec)->sreloc;
9263 if (p->ifunc)
33e44f2e 9264 srel = htab->elf.irelplt;
eea6121a 9265 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9266 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9267 info->flags |= DF_TEXTREL;
ec338859 9268 }
65f38f15
AM
9269 }
9270 }
9271
411e1bfb
AM
9272 lgot_ents = elf_local_got_ents (ibfd);
9273 if (!lgot_ents)
65f38f15
AM
9274 continue;
9275
0ffa91dd 9276 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9277 locsymcount = symtab_hdr->sh_info;
411e1bfb 9278 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9279 local_plt = (struct plt_entry **) end_lgot_ents;
9280 end_local_plt = local_plt + locsymcount;
f961d9dd 9281 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9282 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9283 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9284 {
0b8bcf0d 9285 struct got_entry **pent, *ent;
411e1bfb 9286
0b8bcf0d
AM
9287 pent = lgot_ents;
9288 while ((ent = *pent) != NULL)
411e1bfb
AM
9289 if (ent->got.refcount > 0)
9290 {
e7b938ca 9291 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9292 {
927be08e 9293 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9294 *pent = ent->next;
411e1bfb
AM
9295 }
9296 else
9297 {
19e08130
AM
9298 unsigned int ent_size = 8;
9299 unsigned int rel_size = sizeof (Elf64_External_Rela);
9300
eea6121a 9301 ent->got.offset = s->size;
e7b938ca 9302 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9303 {
19e08130
AM
9304 ent_size *= 2;
9305 rel_size *= 2;
9306 }
9307 s->size += ent_size;
37da22e5 9308 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 9309 {
33e44f2e 9310 htab->elf.irelplt->size += rel_size;
19e08130
AM
9311 htab->got_reli_size += rel_size;
9312 }
f15d0b54
AM
9313 else if (bfd_link_pic (info)
9314 && !((ent->tls_type & TLS_TPREL) != 0
9315 && bfd_link_executable (info)))
19e08130
AM
9316 {
9317 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9318 srel->size += rel_size;
927be08e 9319 }
0b8bcf0d 9320 pent = &ent->next;
411e1bfb
AM
9321 }
9322 }
9323 else
0b8bcf0d 9324 *pent = ent->next;
65f38f15 9325 }
e054468f 9326
2d7ad24e
AM
9327 /* Allocate space for plt calls to local syms. */
9328 lgot_masks = (unsigned char *) end_local_plt;
9329 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
9330 {
9331 struct plt_entry *ent;
9332
9333 for (ent = *local_plt; ent != NULL; ent = ent->next)
9334 if (ent->plt.refcount > 0)
9335 {
2d7ad24e
AM
9336 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9337 {
9338 s = htab->elf.iplt;
9339 ent->plt.offset = s->size;
9340 s->size += PLT_ENTRY_SIZE (htab);
9341 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9342 }
3e04d765
AM
9343 else if (htab->can_convert_all_inline_plt
9344 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
9345 ent->plt.offset = (bfd_vma) -1;
9346 else
9347 {
9348 s = htab->pltlocal;
9349 ent->plt.offset = s->size;
9350 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9351 if (bfd_link_pic (info))
9352 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9353 }
e054468f
AM
9354 }
9355 else
9356 ent->plt.offset = (bfd_vma) -1;
9357 }
65f38f15
AM
9358 }
9359
9360 /* Allocate global sym .plt and .got entries, and space for global
9361 sym dynamic relocs. */
4ce794b7 9362 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 9363
0e1862bb 9364 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 9365 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 9366
7865406b 9367 first_tlsld = NULL;
c72f2fb2 9368 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 9369 {
7865406b
AM
9370 struct got_entry *ent;
9371
0c8d6e5c 9372 if (!is_ppc64_elf (ibfd))
102890f0
AM
9373 continue;
9374
7865406b
AM
9375 ent = ppc64_tlsld_got (ibfd);
9376 if (ent->got.refcount > 0)
102890f0 9377 {
7865406b 9378 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9379 {
7865406b
AM
9380 ent->is_indirect = TRUE;
9381 ent->got.ent = first_tlsld;
9382 }
9383 else
9384 {
9385 if (first_tlsld == NULL)
9386 first_tlsld = ent;
9387 s = ppc64_elf_tdata (ibfd)->got;
9388 ent->got.offset = s->size;
9389 ent->owner = ibfd;
9390 s->size += 16;
0e1862bb 9391 if (bfd_link_pic (info))
7865406b
AM
9392 {
9393 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9394 srel->size += sizeof (Elf64_External_Rela);
9395 }
102890f0
AM
9396 }
9397 }
9398 else
7865406b 9399 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9400 }
9401
65f38f15
AM
9402 /* We now have determined the sizes of the various dynamic sections.
9403 Allocate memory for them. */
b34976b6 9404 relocs = FALSE;
65f38f15
AM
9405 for (s = dynobj->sections; s != NULL; s = s->next)
9406 {
9407 if ((s->flags & SEC_LINKER_CREATED) == 0)
9408 continue;
9409
4ce794b7 9410 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9411 /* These haven't been allocated yet; don't strip. */
9412 continue;
33e44f2e
AM
9413 else if (s == htab->elf.sgot
9414 || s == htab->elf.splt
9415 || s == htab->elf.iplt
2d7ad24e 9416 || s == htab->pltlocal
c456f082 9417 || s == htab->glink
9e390558 9418 || s == htab->global_entry
5474d94f
AM
9419 || s == htab->elf.sdynbss
9420 || s == htab->elf.sdynrelro)
65f38f15
AM
9421 {
9422 /* Strip this section if we don't need it; see the
9423 comment below. */
5bd4f169 9424 }
58d180e8
AM
9425 else if (s == htab->glink_eh_frame)
9426 {
9427 if (!bfd_is_abs_section (s->output_section))
9428 /* Not sized yet. */
9429 continue;
9430 }
70cc837d 9431 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9432 {
c456f082 9433 if (s->size != 0)
5bd4f169 9434 {
33e44f2e 9435 if (s != htab->elf.srelplt)
b34976b6 9436 relocs = TRUE;
5bd4f169
AM
9437
9438 /* We use the reloc_count field as a counter if we need
9439 to copy relocs into the output file. */
9440 s->reloc_count = 0;
9441 }
9442 }
65f38f15 9443 else
5bd4f169
AM
9444 {
9445 /* It's not one of our sections, so don't allocate space. */
9446 continue;
9447 }
9448
eea6121a 9449 if (s->size == 0)
5bd4f169 9450 {
c456f082
AM
9451 /* If we don't need this section, strip it from the
9452 output file. This is mostly to handle .rela.bss and
9453 .rela.plt. We must create both sections in
9454 create_dynamic_sections, because they must be created
9455 before the linker maps input sections to output
9456 sections. The linker does that before
9457 adjust_dynamic_symbol is called, and it is that
9458 function which decides whether anything needs to go
9459 into these sections. */
8423293d 9460 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9461 continue;
9462 }
9463
06bcf541
AM
9464 if (bfd_is_abs_section (s->output_section))
9465 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9466 s->name);
9467
c456f082 9468 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9469 continue;
9470
65f38f15
AM
9471 /* Allocate memory for the section contents. We use bfd_zalloc
9472 here in case unused entries are not reclaimed before the
9473 section's contents are written out. This should not happen,
411e1bfb
AM
9474 but this way if it does we get a R_PPC64_NONE reloc in .rela
9475 sections instead of garbage.
9476 We also rely on the section contents being zero when writing
5474d94f 9477 the GOT and .dynrelro. */
eea6121a 9478 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9479 if (s->contents == NULL)
b34976b6 9480 return FALSE;
5bd4f169
AM
9481 }
9482
c72f2fb2 9483 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 9484 {
0c8d6e5c 9485 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9486 continue;
9487
e717da7e 9488 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 9489 if (s != NULL && s != htab->elf.sgot)
e717da7e 9490 {
eea6121a 9491 if (s->size == 0)
8423293d 9492 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9493 else
9494 {
eea6121a 9495 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9496 if (s->contents == NULL)
9497 return FALSE;
9498 }
9499 }
9500 s = ppc64_elf_tdata (ibfd)->relgot;
9501 if (s != NULL)
9502 {
eea6121a 9503 if (s->size == 0)
8423293d 9504 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9505 else
9506 {
eea6121a 9507 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9508 if (s->contents == NULL)
9509 return FALSE;
9510 relocs = TRUE;
9511 s->reloc_count = 0;
9512 }
9513 }
9514 }
9515
e86ce104 9516 if (htab->elf.dynamic_sections_created)
5bd4f169 9517 {
e8910a83
AM
9518 bfd_boolean tls_opt;
9519
5bd4f169
AM
9520 /* Add some entries to the .dynamic section. We fill in the
9521 values later, in ppc64_elf_finish_dynamic_sections, but we
9522 must add the entries now so that we get the correct size for
9523 the .dynamic section. The DT_DEBUG entry is filled in by the
9524 dynamic linker and used by the debugger. */
dc810e39 9525#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9526 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9527
0e1862bb 9528 if (bfd_link_executable (info))
5bd4f169 9529 {
dc810e39 9530 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9531 return FALSE;
5bd4f169
AM
9532 }
9533
33e44f2e 9534 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 9535 {
dc810e39
AM
9536 if (!add_dynamic_entry (DT_PLTGOT, 0)
9537 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9538 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9539 || !add_dynamic_entry (DT_JMPREL, 0)
9540 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9541 return FALSE;
5bd4f169
AM
9542 }
9543
ee67d69a 9544 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
9545 {
9546 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9547 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9548 return FALSE;
19397422
AM
9549 }
9550
7c9cf415 9551 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
9552 && htab->tls_get_addr_fd != NULL
9553 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9554 if (tls_opt || !htab->opd_abi)
9555 {
9556 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9557 return FALSE;
9558 }
a7f2871e 9559
5bd4f169
AM
9560 if (relocs)
9561 {
dc810e39
AM
9562 if (!add_dynamic_entry (DT_RELA, 0)
9563 || !add_dynamic_entry (DT_RELASZ, 0)
9564 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9565 return FALSE;
5bd4f169 9566
65f38f15
AM
9567 /* If any dynamic relocs apply to a read-only section,
9568 then we need a DT_TEXTREL entry. */
248866a8 9569 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 9570 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 9571
65f38f15 9572 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9573 {
65f38f15 9574 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9575 return FALSE;
5bd4f169 9576 }
5bd4f169 9577 }
5bd4f169 9578 }
65f38f15 9579#undef add_dynamic_entry
5bd4f169 9580
b34976b6 9581 return TRUE;
5bd4f169
AM
9582}
9583
a345bc8d
AM
9584/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
9585
9586static bfd_boolean
9587ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9588{
9589 if (h->plt.plist != NULL
9590 && !h->def_regular
9591 && !h->pointer_equality_needed)
9592 return FALSE;
9593
9594 return _bfd_elf_hash_symbol (h);
9595}
9596
721956f4 9597/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9598
4ce794b7
AM
9599static inline enum ppc_stub_type
9600ppc_type_of_stub (asection *input_sec,
9601 const Elf_Internal_Rela *rel,
9602 struct ppc_link_hash_entry **hash,
e054468f 9603 struct plt_entry **plt_ent,
6911b7dc
AM
9604 bfd_vma destination,
9605 unsigned long local_off)
5bd4f169 9606{
721956f4
AM
9607 struct ppc_link_hash_entry *h = *hash;
9608 bfd_vma location;
9609 bfd_vma branch_offset;
9610 bfd_vma max_branch_offset;
4ce794b7 9611 enum elf_ppc64_reloc_type r_type;
5bd4f169 9612
721956f4
AM
9613 if (h != NULL)
9614 {
e054468f 9615 struct plt_entry *ent;
7fe2b9a6 9616 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
9617 if (h->oh != NULL
9618 && h->oh->is_func_descriptor)
7b8f6675
AM
9619 {
9620 fdh = ppc_follow_link (h->oh);
9621 *hash = fdh;
9622 }
8387904d 9623
e054468f
AM
9624 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9625 if (ent->addend == rel->r_addend
9626 && ent->plt.offset != (bfd_vma) -1)
9627 {
e054468f
AM
9628 *plt_ent = ent;
9629 return ppc_stub_plt_call;
9630 }
5bd4f169 9631
7fe2b9a6
AM
9632 /* Here, we know we don't have a plt entry. If we don't have a
9633 either a defined function descriptor or a defined entry symbol
9634 in a regular object file, then it is pointless trying to make
9635 any other type of stub. */
854b41e7
AM
9636 if (!is_static_defined (&fdh->elf)
9637 && !is_static_defined (&h->elf))
721956f4 9638 return ppc_stub_none;
5d1634d7 9639 }
e054468f
AM
9640 else if (elf_local_got_ents (input_sec->owner) != NULL)
9641 {
9642 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9643 struct plt_entry **local_plt = (struct plt_entry **)
9644 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9645 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9646
9647 if (local_plt[r_symndx] != NULL)
9648 {
9649 struct plt_entry *ent;
9650
9651 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9652 if (ent->addend == rel->r_addend
9653 && ent->plt.offset != (bfd_vma) -1)
9654 {
9655 *plt_ent = ent;
9656 return ppc_stub_plt_call;
9657 }
9658 }
9659 }
5d1634d7 9660
721956f4
AM
9661 /* Determine where the call point is. */
9662 location = (input_sec->output_offset
9663 + input_sec->output_section->vma
9664 + rel->r_offset);
5d1634d7 9665
721956f4
AM
9666 branch_offset = destination - location;
9667 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 9668
721956f4
AM
9669 /* Determine if a long branch stub is needed. */
9670 max_branch_offset = 1 << 25;
23cedd1d
AM
9671 if (r_type == R_PPC64_REL14
9672 || r_type == R_PPC64_REL14_BRTAKEN
9673 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 9674 max_branch_offset = 1 << 15;
5d1634d7 9675
6911b7dc 9676 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
9677 /* We need a stub. Figure out whether a long_branch or plt_branch
9678 is needed later. */
9679 return ppc_stub_long_branch;
5d1634d7 9680
721956f4 9681 return ppc_stub_none;
5d1634d7
AM
9682}
9683
f891966f
AM
9684/* Gets the address of a label (1:) in r11 and builds an offset in r12,
9685 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
9686 . mflr %r12
9687 . bcl 20,31,1f
9688 .1: mflr %r11
9689 . mtlr %r12
05d0e962 9690 . lis %r12,xxx-1b@highest
f891966f 9691 . ori %r12,%r12,xxx-1b@higher
05d0e962 9692 . sldi %r12,%r12,32
f891966f 9693 . oris %r12,%r12,xxx-1b@high
05d0e962 9694 . ori %r12,%r12,xxx-1b@l
f891966f 9695 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
9696
9697static bfd_byte *
9698build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
9699{
f891966f
AM
9700 bfd_put_32 (abfd, MFLR_R12, p);
9701 p += 4;
9702 bfd_put_32 (abfd, BCL_20_31, p);
9703 p += 4;
9704 bfd_put_32 (abfd, MFLR_R11, p);
9705 p += 4;
9706 bfd_put_32 (abfd, MTLR_R12, p);
9707 p += 4;
05d0e962
AM
9708 if (off + 0x8000 < 0x10000)
9709 {
9710 if (load)
9711 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
9712 else
9713 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
9714 p += 4;
9715 }
9716 else if (off + 0x80008000ULL < 0x100000000ULL)
9717 {
9718 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
9719 p += 4;
9720 if (load)
9721 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
9722 else
9723 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
9724 p += 4;
9725 }
9726 else
9727 {
9728 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9729 {
9730 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
9731 p += 4;
9732 }
9733 else
9734 {
9735 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
9736 p += 4;
9737 if (((off >> 32) & 0xffff) != 0)
9738 {
9739 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
9740 p += 4;
9741 }
9742 }
9743 if (((off >> 32) & 0xffffffffULL) != 0)
9744 {
9745 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
9746 p += 4;
9747 }
9748 if (PPC_HI (off) != 0)
9749 {
9750 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
9751 p += 4;
9752 }
9753 if (PPC_LO (off) != 0)
9754 {
9755 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
9756 p += 4;
9757 }
9758 if (load)
9759 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
9760 else
9761 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
9762 p += 4;
9763 }
9764 return p;
9765}
9766
9767static unsigned int
9768size_offset (bfd_vma off)
9769{
9770 unsigned int size;
9771 if (off + 0x8000 < 0x10000)
9772 size = 4;
9773 else if (off + 0x80008000ULL < 0x100000000ULL)
9774 size = 8;
9775 else
9776 {
9777 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9778 size = 4;
9779 else
9780 {
9781 size = 4;
9782 if (((off >> 32) & 0xffff) != 0)
9783 size += 4;
9784 }
9785 if (((off >> 32) & 0xffffffffULL) != 0)
9786 size += 4;
9787 if (PPC_HI (off) != 0)
9788 size += 4;
9789 if (PPC_LO (off) != 0)
9790 size += 4;
9791 size += 4;
9792 }
f891966f 9793 return size + 16;
05d0e962
AM
9794}
9795
3d58e1fc
AM
9796static unsigned int
9797num_relocs_for_offset (bfd_vma off)
9798{
9799 unsigned int num_rel;
9800 if (off + 0x8000 < 0x10000)
9801 num_rel = 1;
9802 else if (off + 0x80008000ULL < 0x100000000ULL)
9803 num_rel = 2;
9804 else
9805 {
9806 num_rel = 1;
9807 if (off + 0x800000000000ULL >= 0x1000000000000ULL
9808 && ((off >> 32) & 0xffff) != 0)
9809 num_rel += 1;
9810 if (PPC_HI (off) != 0)
9811 num_rel += 1;
9812 if (PPC_LO (off) != 0)
9813 num_rel += 1;
9814 }
9815 return num_rel;
9816}
9817
9818static Elf_Internal_Rela *
9819emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
9820 bfd_vma roff, bfd_vma targ, bfd_vma off)
9821{
9822 bfd_vma relative_targ = targ - (roff - 8);
9823 if (bfd_big_endian (info->output_bfd))
9824 roff += 2;
9825 r->r_offset = roff;
9826 r->r_addend = relative_targ + roff;
9827 if (off + 0x8000 < 0x10000)
9828 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
9829 else if (off + 0x80008000ULL < 0x100000000ULL)
9830 {
9831 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
9832 ++r;
9833 roff += 4;
9834 r->r_offset = roff;
9835 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
9836 r->r_addend = relative_targ + roff;
9837 }
9838 else
9839 {
9840 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9841 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
9842 else
9843 {
9844 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
9845 if (((off >> 32) & 0xffff) != 0)
9846 {
9847 ++r;
9848 roff += 4;
9849 r->r_offset = roff;
9850 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
9851 r->r_addend = relative_targ + roff;
9852 }
9853 }
9854 if (((off >> 32) & 0xffffffffULL) != 0)
9855 roff += 4;
9856 if (PPC_HI (off) != 0)
9857 {
9858 ++r;
9859 roff += 4;
9860 r->r_offset = roff;
9861 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
9862 r->r_addend = relative_targ + roff;
9863 }
9864 if (PPC_LO (off) != 0)
9865 {
9866 ++r;
9867 roff += 4;
9868 r->r_offset = roff;
9869 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
9870 r->r_addend = relative_targ + roff;
9871 }
9872 }
9873 return r;
9874}
9875
df136d64
AM
9876/* Emit .eh_frame opcode to advance pc by DELTA. */
9877
9878static bfd_byte *
9879eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
9880{
9881 delta /= 4;
9882 if (delta < 64)
9883 *eh++ = DW_CFA_advance_loc + delta;
9884 else if (delta < 256)
9885 {
9886 *eh++ = DW_CFA_advance_loc1;
9887 *eh++ = delta;
9888 }
9889 else if (delta < 65536)
9890 {
9891 *eh++ = DW_CFA_advance_loc2;
9892 bfd_put_16 (abfd, delta, eh);
9893 eh += 2;
9894 }
9895 else
9896 {
9897 *eh++ = DW_CFA_advance_loc4;
9898 bfd_put_32 (abfd, delta, eh);
9899 eh += 4;
9900 }
9901 return eh;
9902}
9903
9904/* Size of required .eh_frame opcode to advance pc by DELTA. */
9905
9906static unsigned int
9907eh_advance_size (unsigned int delta)
9908{
9909 if (delta < 64 * 4)
9910 /* DW_CFA_advance_loc+[1..63]. */
9911 return 1;
9912 if (delta < 256 * 4)
9913 /* DW_CFA_advance_loc1, byte. */
9914 return 2;
9915 if (delta < 65536 * 4)
9916 /* DW_CFA_advance_loc2, 2 bytes. */
9917 return 3;
9918 /* DW_CFA_advance_loc4, 4 bytes. */
9919 return 5;
9920}
9921
794e51c0
AM
9922/* With power7 weakly ordered memory model, it is possible for ld.so
9923 to update a plt entry in one thread and have another thread see a
9924 stale zero toc entry. To avoid this we need some sort of acquire
9925 barrier in the call stub. One solution is to make the load of the
9926 toc word seem to appear to depend on the load of the function entry
9927 word. Another solution is to test for r2 being zero, and branch to
9928 the appropriate glink entry if so.
9929
9930 . fake dep barrier compare
71a39c98
AM
9931 . ld 12,xxx(2) ld 12,xxx(2)
9932 . mtctr 12 mtctr 12
9933 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
9934 . add 2,2,11 cmpldi 2,0
9935 . ld 2,xxx+8(2) bnectr+
9936 . bctr b <glink_entry>
9937
9938 The solution involving the compare turns out to be faster, so
9939 that's what we use unless the branch won't reach. */
9940
9941#define ALWAYS_USE_FAKE_DEP 0
9942#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 9943
794e51c0
AM
9944static inline unsigned int
9945plt_stub_size (struct ppc_link_hash_table *htab,
9946 struct ppc_stub_hash_entry *stub_entry,
9947 bfd_vma off)
9948{
05d0e962 9949 unsigned size;
b9e5796b 9950
05d0e962
AM
9951 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
9952 {
f891966f 9953 size = 8 + size_offset (off - 8);
05d0e962
AM
9954 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
9955 size += 4;
9956 return size;
9957 }
9958
9959 size = 12;
b9e5796b
AM
9960 if (ALWAYS_EMIT_R2SAVE
9961 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9962 size += 4;
9963 if (PPC_HA (off) != 0)
794e51c0 9964 size += 4;
b9e5796b
AM
9965 if (htab->opd_abi)
9966 {
9967 size += 4;
e7d1c40c 9968 if (htab->params->plt_static_chain)
b9e5796b 9969 size += 4;
bd4d2eaa
AM
9970 if (htab->params->plt_thread_safe
9971 && htab->elf.dynamic_sections_created
9972 && stub_entry->h != NULL
9973 && stub_entry->h->elf.dynindx != -1)
b9e5796b 9974 size += 8;
e7d1c40c 9975 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
9976 size += 4;
9977 }
794e51c0
AM
9978 if (stub_entry->h != NULL
9979 && (stub_entry->h == htab->tls_get_addr_fd
9980 || stub_entry->h == htab->tls_get_addr)
7c9cf415 9981 && htab->params->tls_get_addr_opt)
f378ab09
AM
9982 {
9983 size += 7 * 4;
e81b4c93 9984 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 9985 size += 6 * 4;
f378ab09 9986 }
794e51c0
AM
9987 return size;
9988}
9989
2420fff6
AM
9990/* Depending on the sign of plt_stub_align:
9991 If positive, return the padding to align to a 2**plt_stub_align
9992 boundary.
9993 If negative, if this stub would cross fewer 2**plt_stub_align
9994 boundaries if we align, then return the padding needed to do so. */
9995
794e51c0
AM
9996static inline unsigned int
9997plt_stub_pad (struct ppc_link_hash_table *htab,
9998 struct ppc_stub_hash_entry *stub_entry,
9999 bfd_vma plt_off)
10000{
2420fff6 10001 int stub_align;
1aa42141 10002 unsigned stub_size;
6f20ed8a 10003 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10004
2420fff6
AM
10005 if (htab->params->plt_stub_align >= 0)
10006 {
10007 stub_align = 1 << htab->params->plt_stub_align;
10008 if ((stub_off & (stub_align - 1)) != 0)
10009 return stub_align - (stub_off & (stub_align - 1));
10010 return 0;
10011 }
10012
10013 stub_align = 1 << -htab->params->plt_stub_align;
1aa42141 10014 stub_size = plt_stub_size (htab, stub_entry, plt_off);
794e51c0 10015 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10016 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10017 return stub_align - (stub_off & (stub_align - 1));
10018 return 0;
10019}
10020
10021/* Build a .plt call stub. */
10022
10023static inline bfd_byte *
10024build_plt_stub (struct ppc_link_hash_table *htab,
10025 struct ppc_stub_hash_entry *stub_entry,
10026 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10027{
e7d1c40c 10028 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10029 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10030 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10031 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10032 && htab->elf.dynamic_sections_created
10033 && stub_entry->h != NULL
10034 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10035 bfd_boolean use_fake_dep = plt_thread_safe;
10036 bfd_vma cmp_branch_off = 0;
10037
10038 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10039 && plt_load_toc
794e51c0 10040 && plt_thread_safe
bd4d2eaa
AM
10041 && !((stub_entry->h == htab->tls_get_addr_fd
10042 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10043 && htab->params->tls_get_addr_opt))
794e51c0
AM
10044 {
10045 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10046 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10047 / PLT_ENTRY_SIZE (htab));
9e390558 10048 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10049 bfd_vma to, from;
10050
68d62958
AM
10051 if (pltindex > 32768)
10052 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10053 to = (glinkoff
10054 + htab->glink->output_offset
10055 + htab->glink->output_section->vma);
6f20ed8a 10056 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10057 + 4 * (ALWAYS_EMIT_R2SAVE
10058 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10059 + 4 * (PPC_HA (offset) != 0)
10060 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10061 != PPC_HA (offset))
10062 + 4 * (plt_static_chain != 0)
10063 + 20
6f20ed8a
AM
10064 + stub_entry->group->stub_sec->output_offset
10065 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10066 cmp_branch_off = to - from;
10067 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10068 }
10069
ac2df442
AM
10070 if (PPC_HA (offset) != 0)
10071 {
176a0d42
AM
10072 if (r != NULL)
10073 {
794e51c0
AM
10074 if (ALWAYS_EMIT_R2SAVE
10075 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10076 r[0].r_offset += 4;
176a0d42 10077 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10078 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10079 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10080 r[1].r_addend = r[0].r_addend;
b9e5796b 10081 if (plt_load_toc)
176a0d42 10082 {
b9e5796b 10083 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10084 {
b9e5796b
AM
10085 r[2].r_offset = r[1].r_offset + 4;
10086 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10087 r[2].r_addend = r[0].r_addend;
10088 }
10089 else
10090 {
10091 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10092 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10093 r[2].r_addend = r[0].r_addend + 8;
10094 if (plt_static_chain)
10095 {
10096 r[3].r_offset = r[2].r_offset + 4;
10097 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10098 r[3].r_addend = r[0].r_addend + 16;
10099 }
c7131b65 10100 }
176a0d42
AM
10101 }
10102 }
794e51c0
AM
10103 if (ALWAYS_EMIT_R2SAVE
10104 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10105 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10106 if (plt_load_toc)
10107 {
10108 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10109 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10110 }
10111 else
10112 {
10113 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10114 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10115 }
b9e5796b
AM
10116 if (plt_load_toc
10117 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10118 {
71a39c98 10119 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10120 offset = 0;
10121 }
71a39c98 10122 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10123 if (plt_load_toc)
794e51c0 10124 {
b9e5796b
AM
10125 if (use_fake_dep)
10126 {
10127 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10128 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10129 }
10130 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10131 if (plt_static_chain)
10132 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10133 }
ac2df442
AM
10134 }
10135 else
10136 {
176a0d42
AM
10137 if (r != NULL)
10138 {
794e51c0
AM
10139 if (ALWAYS_EMIT_R2SAVE
10140 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10141 r[0].r_offset += 4;
176a0d42 10142 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10143 if (plt_load_toc)
176a0d42 10144 {
b9e5796b 10145 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10146 {
b9e5796b
AM
10147 r[1].r_offset = r[0].r_offset + 4;
10148 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10149 r[1].r_addend = r[0].r_addend;
10150 }
10151 else
10152 {
10153 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10154 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10155 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10156 if (plt_static_chain)
10157 {
10158 r[2].r_offset = r[1].r_offset + 4;
10159 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10160 r[2].r_addend = r[0].r_addend + 8;
10161 }
c7131b65 10162 }
176a0d42
AM
10163 }
10164 }
794e51c0
AM
10165 if (ALWAYS_EMIT_R2SAVE
10166 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10167 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10168 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10169 if (plt_load_toc
10170 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10171 {
10172 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10173 offset = 0;
10174 }
71a39c98 10175 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10176 if (plt_load_toc)
794e51c0 10177 {
b9e5796b
AM
10178 if (use_fake_dep)
10179 {
10180 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10181 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10182 }
10183 if (plt_static_chain)
10184 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10185 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10186 }
ac2df442 10187 }
b9e5796b 10188 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10189 {
10190 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10191 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10192 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10193 }
10194 else
407aa07c 10195 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10196 return p;
10197}
10198
a7f2871e
AM
10199/* Build a special .plt call stub for __tls_get_addr. */
10200
10201#define LD_R11_0R3 0xe9630000
10202#define LD_R12_0R3 0xe9830000
10203#define MR_R0_R3 0x7c601b78
10204#define CMPDI_R11_0 0x2c2b0000
10205#define ADD_R3_R12_R13 0x7c6c6a14
10206#define BEQLR 0x4d820020
10207#define MR_R3_R0 0x7c030378
a7f2871e
AM
10208#define STD_R11_0R1 0xf9610000
10209#define BCTRL 0x4e800421
10210#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10211#define MTLR_R11 0x7d6803a6
10212
10213static inline bfd_byte *
794e51c0
AM
10214build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10215 struct ppc_stub_hash_entry *stub_entry,
10216 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10217{
e7d1c40c 10218 bfd *obfd = htab->params->stub_bfd;
df136d64 10219 bfd_byte *loc = p;
794e51c0 10220
a7f2871e
AM
10221 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10222 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10223 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10224 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10225 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10226 bfd_put_32 (obfd, BEQLR, p), p += 4;
10227 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
10228 if (r != NULL)
10229 r[0].r_offset += 7 * 4;
e81b4c93 10230 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
f378ab09
AM
10231 return build_plt_stub (htab, stub_entry, p, offset, r);
10232
a7f2871e 10233 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10234 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10235
10236 if (r != NULL)
f378ab09 10237 r[0].r_offset += 2 * 4;
794e51c0 10238 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 10239 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 10240
a078d95a 10241 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10242 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10243 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10244 bfd_put_32 (obfd, BLR, p), p += 4;
10245
df136d64
AM
10246 if (htab->glink_eh_frame != NULL
10247 && htab->glink_eh_frame->size != 0)
10248 {
10249 bfd_byte *base, *eh;
10250 unsigned int lr_used, delta;
10251
10252 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10253 eh = base + stub_entry->group->eh_size;
10254 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10255 delta = lr_used - stub_entry->group->lr_restore;
10256 stub_entry->group->lr_restore = lr_used + 16;
10257 eh = eh_advance (htab->elf.dynobj, eh, delta);
10258 *eh++ = DW_CFA_offset_extended_sf;
10259 *eh++ = 65;
10260 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10261 *eh++ = DW_CFA_advance_loc + 4;
10262 *eh++ = DW_CFA_restore_extended;
10263 *eh++ = 65;
10264 stub_entry->group->eh_size = eh - base;
10265 }
a7f2871e
AM
10266 return p;
10267}
10268
176a0d42
AM
10269static Elf_Internal_Rela *
10270get_relocs (asection *sec, int count)
10271{
10272 Elf_Internal_Rela *relocs;
10273 struct bfd_elf_section_data *elfsec_data;
10274
10275 elfsec_data = elf_section_data (sec);
10276 relocs = elfsec_data->relocs;
10277 if (relocs == NULL)
10278 {
10279 bfd_size_type relsize;
10280 relsize = sec->reloc_count * sizeof (*relocs);
10281 relocs = bfd_alloc (sec->owner, relsize);
10282 if (relocs == NULL)
10283 return NULL;
10284 elfsec_data->relocs = relocs;
d4730f92
BS
10285 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10286 sizeof (Elf_Internal_Shdr));
10287 if (elfsec_data->rela.hdr == NULL)
10288 return NULL;
10289 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10290 * sizeof (Elf64_External_Rela));
10291 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10292 sec->reloc_count = 0;
10293 }
10294 relocs += sec->reloc_count;
10295 sec->reloc_count += count;
10296 return relocs;
10297}
10298
3d58e1fc
AM
10299/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10300 forms, to the equivalent relocs against the global symbol given by
10301 STUB_ENTRY->H. */
10302
10303static bfd_boolean
10304use_global_in_relocs (struct ppc_link_hash_table *htab,
10305 struct ppc_stub_hash_entry *stub_entry,
10306 Elf_Internal_Rela *r, unsigned int num_rel)
10307{
10308 struct elf_link_hash_entry **hashes;
10309 unsigned long symndx;
10310 struct ppc_link_hash_entry *h;
10311 bfd_vma symval;
10312
10313 /* Relocs are always against symbols in their own object file. Fake
10314 up global sym hashes for the stub bfd (which has no symbols). */
10315 hashes = elf_sym_hashes (htab->params->stub_bfd);
10316 if (hashes == NULL)
10317 {
10318 bfd_size_type hsize;
10319
10320 /* When called the first time, stub_globals will contain the
10321 total number of symbols seen during stub sizing. After
10322 allocating, stub_globals is used as an index to fill the
10323 hashes array. */
10324 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10325 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10326 if (hashes == NULL)
10327 return FALSE;
10328 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10329 htab->stub_globals = 1;
10330 }
10331 symndx = htab->stub_globals++;
10332 h = stub_entry->h;
10333 hashes[symndx] = &h->elf;
10334 if (h->oh != NULL && h->oh->is_func)
10335 h = ppc_follow_link (h->oh);
10336 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10337 || h->elf.root.type == bfd_link_hash_defweak);
10338 symval = (h->elf.root.u.def.value
10339 + h->elf.root.u.def.section->output_offset
10340 + h->elf.root.u.def.section->output_section->vma);
10341 while (num_rel-- != 0)
10342 {
10343 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10344 if (h->elf.root.u.def.section != stub_entry->target_section)
10345 {
10346 /* H is an opd symbol. The addend must be zero, and the
10347 branch reloc is the only one we can convert. */
10348 r->r_addend = 0;
10349 break;
10350 }
10351 else
10352 r->r_addend -= symval;
10353 --r;
10354 }
10355 return TRUE;
10356}
10357
aa374f67 10358static bfd_vma
25f53a85 10359get_r2off (struct bfd_link_info *info,
aa374f67
AM
10360 struct ppc_stub_hash_entry *stub_entry)
10361{
25f53a85 10362 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10363 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10364
10365 if (r2off == 0)
10366 {
10367 /* Support linking -R objects. Get the toc pointer from the
10368 opd entry. */
10369 char buf[8];
b9e5796b
AM
10370 if (!htab->opd_abi)
10371 return r2off;
aa374f67
AM
10372 asection *opd = stub_entry->h->elf.root.u.def.section;
10373 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10374
10375 if (strcmp (opd->name, ".opd") != 0
10376 || opd->reloc_count != 0)
10377 {
2cdcc330
AM
10378 info->callbacks->einfo
10379 (_("%P: cannot find opd entry toc for `%pT'\n"),
10380 stub_entry->h->elf.root.root.string);
aa374f67 10381 bfd_set_error (bfd_error_bad_value);
a7c49797 10382 return (bfd_vma) -1;
aa374f67
AM
10383 }
10384 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10385 return (bfd_vma) -1;
aa374f67 10386 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10387 r2off -= elf_gp (info->output_bfd);
aa374f67 10388 }
6f20ed8a 10389 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10390 return r2off;
10391}
10392
b34976b6 10393static bfd_boolean
4ce794b7 10394ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10395{
721956f4
AM
10396 struct ppc_stub_hash_entry *stub_entry;
10397 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10398 struct bfd_link_info *info;
10399 struct ppc_link_hash_table *htab;
721956f4 10400 bfd_byte *loc;
3d58e1fc 10401 bfd_byte *p, *relp;
1aa42141 10402 bfd_vma targ, off;
176a0d42 10403 Elf_Internal_Rela *r;
e054468f 10404 asection *plt;
3d58e1fc 10405 int num_rel;
5d1634d7 10406
721956f4
AM
10407 /* Massage our args to the form they really have. */
10408 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10409 info = in_arg;
5d1634d7 10410
5d1634d7 10411 htab = ppc_hash_table (info);
4dfe6ac6
NC
10412 if (htab == NULL)
10413 return FALSE;
5d1634d7 10414
1aa42141 10415 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 10416 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10417
4ce794b7 10418 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10419 switch (stub_entry->stub_type)
5d1634d7 10420 {
721956f4 10421 case ppc_stub_long_branch:
ad8e1ba5 10422 case ppc_stub_long_branch_r2off:
721956f4 10423 /* Branches are relative. This is where we are going to. */
1aa42141 10424 targ = (stub_entry->target_value
6911b7dc
AM
10425 + stub_entry->target_section->output_offset
10426 + stub_entry->target_section->output_section->vma);
1aa42141 10427 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 10428
721956f4 10429 /* And this is where we are coming from. */
1aa42141
AM
10430 off = (stub_entry->stub_offset
10431 + stub_entry->group->stub_sec->output_offset
10432 + stub_entry->group->stub_sec->output_section->vma);
10433 off = targ - off;
e86ce104 10434
9e390558 10435 p = loc;
ac2df442 10436 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10437 {
25f53a85 10438 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10439
a7c49797 10440 if (r2off == (bfd_vma) -1)
aa374f67
AM
10441 {
10442 htab->stub_error = TRUE;
10443 return FALSE;
10444 }
9e390558
AM
10445 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10446 p += 4;
ac2df442
AM
10447 if (PPC_HA (r2off) != 0)
10448 {
e7d1c40c 10449 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10450 ADDIS_R2_R2 | PPC_HA (r2off), p);
10451 p += 4;
a7c49797
AM
10452 }
10453 if (PPC_LO (r2off) != 0)
10454 {
10455 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10456 ADDI_R2_R2 | PPC_LO (r2off), p);
10457 p += 4;
ac2df442 10458 }
9e390558 10459 off -= p - loc;
ad8e1ba5 10460 }
9e390558
AM
10461 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10462 p += 4;
ad8e1ba5 10463
5c3dead3
AM
10464 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10465 {
cf97bcb0
AM
10466 _bfd_error_handler
10467 (_("long branch stub `%s' offset overflow"),
bc30df16 10468 stub_entry->root.string);
5c3dead3
AM
10469 htab->stub_error = TRUE;
10470 return FALSE;
10471 }
ee75fd95
AM
10472
10473 if (info->emitrelocations)
10474 {
6f20ed8a 10475 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
10476 if (r == NULL)
10477 return FALSE;
9e390558 10478 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 10479 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 10480 r->r_addend = targ;
3d58e1fc
AM
10481 if (stub_entry->h != NULL
10482 && !use_global_in_relocs (htab, stub_entry, r, 1))
10483 return FALSE;
ee75fd95 10484 }
721956f4 10485 break;
e86ce104 10486
721956f4 10487 case ppc_stub_plt_branch:
ad8e1ba5 10488 case ppc_stub_plt_branch_r2off:
721956f4
AM
10489 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10490 stub_entry->root.string + 9,
b34976b6 10491 FALSE, FALSE);
721956f4
AM
10492 if (br_entry == NULL)
10493 {
cf97bcb0
AM
10494 _bfd_error_handler (_("can't find branch stub `%s'"),
10495 stub_entry->root.string);
b34976b6
AM
10496 htab->stub_error = TRUE;
10497 return FALSE;
721956f4
AM
10498 }
10499
1aa42141 10500 targ = (stub_entry->target_value
176a0d42
AM
10501 + stub_entry->target_section->output_offset
10502 + stub_entry->target_section->output_section->vma);
6911b7dc 10503 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 10504 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10505
1aa42141 10506 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 10507 htab->brlt->contents + br_entry->offset);
721956f4 10508
f94498ff 10509 if (br_entry->iter == htab->stub_iteration)
721956f4 10510 {
f94498ff 10511 br_entry->iter = 0;
84f5d08e 10512
f94498ff 10513 if (htab->relbrlt != NULL)
84f5d08e 10514 {
f94498ff
AM
10515 /* Create a reloc for the branch lookup table entry. */
10516 Elf_Internal_Rela rela;
10517 bfd_byte *rl;
10518
10519 rela.r_offset = (br_entry->offset
10520 + htab->brlt->output_offset
10521 + htab->brlt->output_section->vma);
10522 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 10523 rela.r_addend = targ;
f94498ff
AM
10524
10525 rl = htab->relbrlt->contents;
10526 rl += (htab->relbrlt->reloc_count++
10527 * sizeof (Elf64_External_Rela));
10528 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10529 }
10530 else if (info->emitrelocations)
10531 {
176a0d42
AM
10532 r = get_relocs (htab->brlt, 1);
10533 if (r == NULL)
10534 return FALSE;
10535 /* brlt, being SEC_LINKER_CREATED does not go through the
10536 normal reloc processing. Symbols and offsets are not
10537 translated from input file to output file form, so
10538 set up the offset per the output file. */
f94498ff
AM
10539 r->r_offset = (br_entry->offset
10540 + htab->brlt->output_offset
10541 + htab->brlt->output_section->vma);
10542 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 10543 r->r_addend = targ;
84f5d08e 10544 }
84f5d08e 10545 }
721956f4 10546
1aa42141 10547 targ = (br_entry->offset
176a0d42
AM
10548 + htab->brlt->output_offset
10549 + htab->brlt->output_section->vma);
10550
1aa42141
AM
10551 off = (elf_gp (info->output_bfd)
10552 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10553 off = targ - off;
721956f4 10554
ad8e1ba5 10555 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 10556 {
25f53a85 10557 info->callbacks->einfo
c1c8c1ef 10558 (_("%P: linkage table error against `%pT'\n"),
721956f4 10559 stub_entry->root.string);
5d1634d7 10560 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10561 htab->stub_error = TRUE;
10562 return FALSE;
5d1634d7 10563 }
41bd81ab 10564
176a0d42
AM
10565 if (info->emitrelocations)
10566 {
6f20ed8a 10567 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
10568 if (r == NULL)
10569 return FALSE;
6f20ed8a 10570 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
10571 if (bfd_big_endian (info->output_bfd))
10572 r[0].r_offset += 2;
00f412ee 10573 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
10574 r[0].r_offset += 4;
10575 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 10576 r[0].r_addend = targ;
176a0d42
AM
10577 if (PPC_HA (off) != 0)
10578 {
10579 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10580 r[1].r_offset = r[0].r_offset + 4;
10581 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10582 r[1].r_addend = r[0].r_addend;
10583 }
10584 }
10585
9e390558 10586 p = loc;
00f412ee 10587 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 10588 {
176a0d42 10589 if (PPC_HA (off) != 0)
ac2df442 10590 {
e7d1c40c 10591 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10592 ADDIS_R12_R2 | PPC_HA (off), p);
10593 p += 4;
e7d1c40c 10594 bfd_put_32 (htab->params->stub_bfd,
9e390558 10595 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
10596 }
10597 else
9e390558
AM
10598 bfd_put_32 (htab->params->stub_bfd,
10599 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
10600 }
10601 else
10602 {
25f53a85 10603 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 10604
a7c49797 10605 if (r2off == (bfd_vma) -1)
aa374f67
AM
10606 {
10607 htab->stub_error = TRUE;
10608 return FALSE;
10609 }
ad8e1ba5 10610
9e390558
AM
10611 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10612 p += 4;
176a0d42 10613 if (PPC_HA (off) != 0)
ac2df442 10614 {
e7d1c40c 10615 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10616 ADDIS_R12_R2 | PPC_HA (off), p);
10617 p += 4;
e7d1c40c 10618 bfd_put_32 (htab->params->stub_bfd,
9e390558 10619 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
10620 }
10621 else
9e390558 10622 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
10623
10624 if (PPC_HA (r2off) != 0)
10625 {
9e390558 10626 p += 4;
e7d1c40c 10627 bfd_put_32 (htab->params->stub_bfd,
9e390558 10628 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
10629 }
10630 if (PPC_LO (r2off) != 0)
10631 {
9e390558 10632 p += 4;
e7d1c40c 10633 bfd_put_32 (htab->params->stub_bfd,
9e390558 10634 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 10635 }
ad8e1ba5 10636 }
9e390558
AM
10637 p += 4;
10638 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10639 p += 4;
407aa07c
AM
10640 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10641 p += 4;
721956f4 10642 break;
5d1634d7 10643
05d0e962
AM
10644 case ppc_stub_long_branch_notoc:
10645 case ppc_stub_long_branch_both:
10646 case ppc_stub_plt_branch_notoc:
10647 case ppc_stub_plt_branch_both:
10648 case ppc_stub_plt_call_notoc:
10649 case ppc_stub_plt_call_both:
10650 p = loc;
f891966f 10651 off = (stub_entry->stub_offset
05d0e962
AM
10652 + stub_entry->group->stub_sec->output_offset
10653 + stub_entry->group->stub_sec->output_section->vma);
10654 if (stub_entry->stub_type == ppc_stub_long_branch_both
10655 || stub_entry->stub_type == ppc_stub_plt_branch_both
10656 || stub_entry->stub_type == ppc_stub_plt_call_both)
10657 {
10658 off += 4;
10659 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10660 p += 4;
10661 }
10662 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10663 {
10664 targ = stub_entry->plt_ent->plt.offset & ~1;
10665 if (targ >= (bfd_vma) -2)
10666 abort ();
10667
10668 plt = htab->elf.splt;
10669 if (!htab->elf.dynamic_sections_created
10670 || stub_entry->h == NULL
10671 || stub_entry->h->elf.dynindx == -1)
10672 {
10673 if (stub_entry->symtype == STT_GNU_IFUNC)
10674 plt = htab->elf.iplt;
10675 else
10676 plt = htab->pltlocal;
10677 }
10678 targ += plt->output_offset + plt->output_section->vma;
10679 }
10680 else
10681 targ = (stub_entry->target_value
10682 + stub_entry->target_section->output_offset
10683 + stub_entry->target_section->output_section->vma);
10684 off = targ - off;
f891966f 10685
3d58e1fc
AM
10686 relp = p;
10687 num_rel = 0;
f891966f
AM
10688 /* The notoc stubs calculate their target (either a PLT entry or
10689 the global entry point of a function) relative to the PC
10690 returned by the "bcl" two instructions past the start of the
10691 sequence emitted by build_offset. The offset is therefore 8
10692 less than calculated from the start of the sequence. */
10693 off -= 8;
05d0e962
AM
10694 p = build_offset (htab->params->stub_bfd, p, off,
10695 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
f891966f 10696 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
05d0e962 10697 {
f891966f 10698 bfd_vma from;
3d58e1fc 10699 num_rel = 1;
f891966f
AM
10700 from = (stub_entry->stub_offset
10701 + stub_entry->group->stub_sec->output_offset
10702 + stub_entry->group->stub_sec->output_section->vma
10703 + (p - loc));
05d0e962 10704 bfd_put_32 (htab->params->stub_bfd,
f891966f 10705 B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
10706 }
10707 else
10708 {
10709 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10710 p += 4;
10711 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10712 }
10713 p += 4;
df136d64 10714
3d58e1fc
AM
10715 if (info->emitrelocations)
10716 {
10717 bfd_vma roff;
10718 num_rel += num_relocs_for_offset (off);
10719 r = get_relocs (stub_entry->group->stub_sec, num_rel);
10720 if (r == NULL)
10721 return FALSE;
10722 roff = relp + 16 - stub_entry->group->stub_sec->contents;
10723 r = emit_relocs_for_offset (info, r, roff, targ, off);
10724 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
10725 || stub_entry->stub_type == ppc_stub_long_branch_both)
10726 {
10727 ++r;
10728 roff = p - 4 - stub_entry->group->stub_sec->contents;
10729 r->r_offset = roff;
10730 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10731 r->r_addend = targ;
10732 if (stub_entry->h != NULL
10733 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
10734 return FALSE;
10735 }
10736 }
10737
df136d64
AM
10738 if (htab->glink_eh_frame != NULL
10739 && htab->glink_eh_frame->size != 0)
10740 {
10741 bfd_byte *base, *eh;
10742 unsigned int lr_used, delta;
10743
10744 base = (htab->glink_eh_frame->contents
10745 + stub_entry->group->eh_base + 17);
10746 eh = base + stub_entry->group->eh_size;
10747 lr_used = stub_entry->stub_offset + 8;
10748 if (stub_entry->stub_type == ppc_stub_long_branch_both
10749 || stub_entry->stub_type == ppc_stub_plt_branch_both
10750 || stub_entry->stub_type == ppc_stub_plt_call_both)
10751 lr_used += 4;
10752 delta = lr_used - stub_entry->group->lr_restore;
10753 stub_entry->group->lr_restore = lr_used + 8;
10754 eh = eh_advance (htab->elf.dynobj, eh, delta);
10755 *eh++ = DW_CFA_register;
10756 *eh++ = 65;
10757 *eh++ = 12;
10758 *eh++ = DW_CFA_advance_loc + 2;
10759 *eh++ = DW_CFA_restore_extended;
10760 *eh++ = 65;
10761 stub_entry->group->eh_size = eh - base;
10762 }
05d0e962
AM
10763 break;
10764
721956f4 10765 case ppc_stub_plt_call:
794e51c0 10766 case ppc_stub_plt_call_r2save:
e054468f 10767 if (stub_entry->h != NULL
b31867b6
AM
10768 && stub_entry->h->is_func_descriptor
10769 && stub_entry->h->oh != NULL)
c862ae31 10770 {
b31867b6
AM
10771 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10772
10773 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 10774 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
10775 if (fh->elf.root.type == bfd_link_hash_undefined
10776 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
10777 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 10778 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
10779 }
10780
721956f4 10781 /* Now build the stub. */
1aa42141
AM
10782 targ = stub_entry->plt_ent->plt.offset & ~1;
10783 if (targ >= (bfd_vma) -2)
721956f4
AM
10784 abort ();
10785
33e44f2e 10786 plt = htab->elf.splt;
25f23106
AM
10787 if (!htab->elf.dynamic_sections_created
10788 || stub_entry->h == NULL
10789 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
10790 {
10791 if (stub_entry->symtype == STT_GNU_IFUNC)
10792 plt = htab->elf.iplt;
10793 else
10794 plt = htab->pltlocal;
10795 }
1aa42141 10796 targ += plt->output_offset + plt->output_section->vma;
e054468f 10797
1aa42141
AM
10798 off = (elf_gp (info->output_bfd)
10799 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10800 off = targ - off;
721956f4 10801
ad8e1ba5 10802 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 10803 {
25f53a85 10804 info->callbacks->einfo
695344c0 10805 /* xgettext:c-format */
c1c8c1ef 10806 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
10807 stub_entry->h != NULL
10808 ? stub_entry->h->elf.root.root.string
10809 : "<local sym>");
721956f4 10810 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10811 htab->stub_error = TRUE;
10812 return FALSE;
721956f4
AM
10813 }
10814
176a0d42
AM
10815 r = NULL;
10816 if (info->emitrelocations)
10817 {
6f20ed8a 10818 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
10819 ((PPC_HA (off) != 0)
10820 + (htab->opd_abi
e7d1c40c 10821 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
10822 && PPC_HA (off + 16) == PPC_HA (off))
10823 : 1)));
176a0d42
AM
10824 if (r == NULL)
10825 return FALSE;
6f20ed8a 10826 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
10827 if (bfd_big_endian (info->output_bfd))
10828 r[0].r_offset += 2;
1aa42141 10829 r[0].r_addend = targ;
176a0d42 10830 }
a7f2871e
AM
10831 if (stub_entry->h != NULL
10832 && (stub_entry->h == htab->tls_get_addr_fd
10833 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10834 && htab->params->tls_get_addr_opt)
794e51c0 10835 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 10836 else
794e51c0 10837 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
10838 break;
10839
a4b6fadd
AM
10840 case ppc_stub_save_res:
10841 return TRUE;
10842
721956f4
AM
10843 default:
10844 BFD_FAIL ();
b34976b6 10845 return FALSE;
721956f4
AM
10846 }
10847
1aa42141 10848 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 10849
e7d1c40c 10850 if (htab->params->emit_stub_syms)
97b639ba
AM
10851 {
10852 struct elf_link_hash_entry *h;
ee75fd95
AM
10853 size_t len1, len2;
10854 char *name;
10855 const char *const stub_str[] = { "long_branch",
05d0e962
AM
10856 "long_branch",
10857 "long_branch",
3f6ff479
AM
10858 "long_branch",
10859 "plt_branch",
ee75fd95 10860 "plt_branch",
05d0e962
AM
10861 "plt_branch",
10862 "plt_branch",
10863 "plt_call",
10864 "plt_call",
794e51c0 10865 "plt_call",
ee75fd95
AM
10866 "plt_call" };
10867
10868 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10869 len2 = strlen (stub_entry->root.string);
10870 name = bfd_malloc (len1 + len2 + 2);
10871 if (name == NULL)
10872 return FALSE;
10873 memcpy (name, stub_entry->root.string, 9);
10874 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10875 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10876 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
10877 if (h == NULL)
10878 return FALSE;
10879 if (h->root.type == bfd_link_hash_new)
10880 {
10881 h->root.type = bfd_link_hash_defined;
6f20ed8a 10882 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 10883 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
10884 h->ref_regular = 1;
10885 h->def_regular = 1;
10886 h->ref_regular_nonweak = 1;
10887 h->forced_local = 1;
10888 h->non_elf = 0;
2ec55de3 10889 h->root.linker_def = 1;
97b639ba
AM
10890 }
10891 }
10892
b34976b6 10893 return TRUE;
721956f4
AM
10894}
10895
10896/* As above, but don't actually build the stub. Just bump offset so
10897 we know stub section sizes, and select plt_branch stubs where
10898 long_branch stubs won't do. */
10899
b34976b6 10900static bfd_boolean
4ce794b7 10901ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
10902{
10903 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 10904 struct bfd_link_info *info;
721956f4 10905 struct ppc_link_hash_table *htab;
f891966f
AM
10906 asection *plt;
10907 bfd_vma targ, off, r2off;
10908 unsigned int size, extra, lr_used, delta;
721956f4
AM
10909
10910 /* Massage our args to the form they really have. */
10911 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
10912 info = in_arg;
10913
10914 htab = ppc_hash_table (info);
4dfe6ac6
NC
10915 if (htab == NULL)
10916 return FALSE;
721956f4 10917
1aa42141
AM
10918 /* Make a note of the offset within the stubs for this entry. */
10919 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10920
a4b6fadd
AM
10921 if (stub_entry->h != NULL
10922 && stub_entry->h->save_res
10923 && stub_entry->h->elf.root.type == bfd_link_hash_defined
10924 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
10925 {
10926 /* Don't make stubs to out-of-line register save/restore
10927 functions. Instead, emit copies of the functions. */
10928 stub_entry->group->needs_save_res = 1;
10929 stub_entry->stub_type = ppc_stub_save_res;
10930 return TRUE;
10931 }
10932
f891966f 10933 switch (stub_entry->stub_type)
721956f4 10934 {
f891966f
AM
10935 case ppc_stub_plt_branch:
10936 case ppc_stub_plt_branch_r2off:
10937 /* Reset the stub type from the plt branch variant in case we now
10938 can reach with a shorter stub. */
10939 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10940 /* Fall through. */
10941 case ppc_stub_long_branch:
10942 case ppc_stub_long_branch_r2off:
1aa42141
AM
10943 targ = (stub_entry->target_value
10944 + stub_entry->target_section->output_offset
10945 + stub_entry->target_section->output_section->vma);
f891966f 10946 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
1aa42141
AM
10947 off = (stub_entry->stub_offset
10948 + stub_entry->group->stub_sec->output_offset
10949 + stub_entry->group->stub_sec->output_section->vma);
721956f4 10950
ad8e1ba5 10951 size = 4;
f891966f 10952 r2off = 0;
ad8e1ba5
AM
10953 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10954 {
25f53a85 10955 r2off = get_r2off (info, stub_entry);
a7c49797 10956 if (r2off == (bfd_vma) -1)
aa374f67
AM
10957 {
10958 htab->stub_error = TRUE;
10959 return FALSE;
10960 }
a7c49797 10961 size = 8;
ac2df442 10962 if (PPC_HA (r2off) != 0)
a7c49797
AM
10963 size += 4;
10964 if (PPC_LO (r2off) != 0)
10965 size += 4;
1aa42141 10966 off += size - 4;
ad8e1ba5 10967 }
1aa42141 10968 off = targ - off;
ad8e1ba5 10969
f891966f
AM
10970 /* If the branch offset is too big, use a ppc_stub_plt_branch.
10971 Do the same for -R objects without function descriptors. */
10972 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
10973 && r2off == 0
10974 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
10975 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 10976 {
f891966f 10977 struct ppc_branch_hash_entry *br_entry;
df136d64 10978
f891966f
AM
10979 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10980 stub_entry->root.string + 9,
10981 TRUE, FALSE);
10982 if (br_entry == NULL)
721956f4 10983 {
f891966f
AM
10984 _bfd_error_handler (_("can't build branch stub `%s'"),
10985 stub_entry->root.string);
10986 htab->stub_error = TRUE;
10987 return FALSE;
721956f4
AM
10988 }
10989
f891966f 10990 if (br_entry->iter != htab->stub_iteration)
721956f4 10991 {
f891966f
AM
10992 br_entry->iter = htab->stub_iteration;
10993 br_entry->offset = htab->brlt->size;
10994 htab->brlt->size += 8;
63bc6f6c 10995
f891966f
AM
10996 if (htab->relbrlt != NULL)
10997 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10998 else if (info->emitrelocations)
84f5d08e 10999 {
f891966f
AM
11000 htab->brlt->reloc_count += 1;
11001 htab->brlt->flags |= SEC_RELOC;
05d0e962 11002 }
f891966f 11003 }
ac2df442 11004
f891966f
AM
11005 targ = (br_entry->offset
11006 + htab->brlt->output_offset
11007 + htab->brlt->output_section->vma);
11008 off = (elf_gp (info->output_bfd)
11009 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11010 off = targ - off;
176a0d42 11011
f891966f
AM
11012 if (info->emitrelocations)
11013 {
11014 stub_entry->group->stub_sec->reloc_count
11015 += 1 + (PPC_HA (off) != 0);
11016 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11017 }
05d0e962 11018
f891966f
AM
11019 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11020 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11021 {
11022 size = 12;
11023 if (PPC_HA (off) != 0)
11024 size = 16;
ac2df442 11025 }
f891966f 11026 else
ac2df442 11027 {
f891966f
AM
11028 size = 16;
11029 if (PPC_HA (off) != 0)
11030 size += 4;
11031
11032 if (PPC_HA (r2off) != 0)
11033 size += 4;
11034 if (PPC_LO (r2off) != 0)
11035 size += 4;
ac2df442 11036 }
721956f4 11037 }
f891966f
AM
11038 else if (info->emitrelocations)
11039 {
11040 stub_entry->group->stub_sec->reloc_count += 1;
11041 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11042 }
11043 break;
11044
11045 case ppc_stub_plt_branch_notoc:
11046 case ppc_stub_plt_branch_both:
11047 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11048 /* Fall through. */
11049 case ppc_stub_long_branch_notoc:
11050 case ppc_stub_long_branch_both:
11051 off = (stub_entry->stub_offset
11052 + stub_entry->group->stub_sec->output_offset
11053 + stub_entry->group->stub_sec->output_section->vma);
11054 size = 0;
11055 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11056 size = 4;
11057 off += size;
11058 targ = (stub_entry->target_value
11059 + stub_entry->target_section->output_offset
11060 + stub_entry->target_section->output_section->vma);
11061 off = targ - off;
11062
3d58e1fc
AM
11063 if (info->emitrelocations)
11064 {
11065 stub_entry->group->stub_sec->reloc_count
11066 += num_relocs_for_offset (off);
11067 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11068 }
11069
f891966f
AM
11070 extra = size_offset (off - 8);
11071 /* Include branch insn plus those in the offset sequence. */
11072 size += 4 + extra;
11073 /* The branch insn is at the end, or "extra" bytes along. So
11074 its offset will be "extra" bytes less that that already
11075 calculated. */
11076 off -= extra;
11077
11078 /* After the bcl, lr has been modified so we need to emit
11079 .eh_frame info saying the return address is in r12. */
11080 lr_used = stub_entry->stub_offset + 8;
11081 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11082 lr_used += 4;
11083 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11084 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11085 DW_CFA_restore_extended 65. */
11086 delta = lr_used - stub_entry->group->lr_restore;
11087 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11088 stub_entry->group->lr_restore = lr_used + 8;
11089
11090 /* If the branch can't reach, use a plt_branch. */
11091 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11092 {
11093 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11094 - ppc_stub_long_branch_notoc);
11095 size += 4;
11096 }
3d58e1fc
AM
11097 else if (info->emitrelocations)
11098 stub_entry->group->stub_sec->reloc_count +=1;
f891966f
AM
11099 break;
11100
11101 case ppc_stub_plt_call_notoc:
11102 case ppc_stub_plt_call_both:
11103 off = (stub_entry->stub_offset
11104 + stub_entry->group->stub_sec->output_offset
11105 + stub_entry->group->stub_sec->output_section->vma);
11106 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11107 off += 4;
11108 targ = stub_entry->plt_ent->plt.offset & ~1;
11109 if (targ >= (bfd_vma) -2)
11110 abort ();
11111
11112 plt = htab->elf.splt;
11113 if (!htab->elf.dynamic_sections_created
11114 || stub_entry->h == NULL
11115 || stub_entry->h->elf.dynindx == -1)
11116 {
11117 if (stub_entry->symtype == STT_GNU_IFUNC)
11118 plt = htab->elf.iplt;
11119 else
11120 plt = htab->pltlocal;
11121 }
11122 targ += plt->output_offset + plt->output_section->vma;
11123 off = targ - off;
11124
11125 if (htab->params->plt_stub_align != 0)
11126 {
11127 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11128
11129 stub_entry->group->stub_sec->size += pad;
11130 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11131 off -= pad;
11132 }
11133
3d58e1fc
AM
11134 if (info->emitrelocations)
11135 {
11136 stub_entry->group->stub_sec->reloc_count
11137 += num_relocs_for_offset (off - 8);
11138 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11139 }
11140
f891966f
AM
11141 size = plt_stub_size (htab, stub_entry, off);
11142
11143 /* After the bcl, lr has been modified so we need to emit
11144 .eh_frame info saying the return address is in r12. */
11145 lr_used = stub_entry->stub_offset + 8;
11146 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11147 lr_used += 4;
11148 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11149 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11150 DW_CFA_restore_extended 65. */
11151 delta = lr_used - stub_entry->group->lr_restore;
11152 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11153 stub_entry->group->lr_restore = lr_used + 8;
11154 break;
11155
11156 case ppc_stub_plt_call:
11157 case ppc_stub_plt_call_r2save:
11158 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11159 if (targ >= (bfd_vma) -2)
11160 abort ();
11161 plt = htab->elf.splt;
11162 if (!htab->elf.dynamic_sections_created
11163 || stub_entry->h == NULL
11164 || stub_entry->h->elf.dynindx == -1)
11165 {
11166 if (stub_entry->symtype == STT_GNU_IFUNC)
11167 plt = htab->elf.iplt;
11168 else
11169 plt = htab->pltlocal;
11170 }
11171 targ += plt->output_offset + plt->output_section->vma;
11172
11173 off = (elf_gp (info->output_bfd)
11174 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11175 off = targ - off;
11176
11177 if (htab->params->plt_stub_align != 0)
11178 {
11179 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11180
11181 stub_entry->group->stub_sec->size += pad;
11182 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11183 }
11184
11185 if (info->emitrelocations)
11186 {
11187 stub_entry->group->stub_sec->reloc_count
11188 += ((PPC_HA (off) != 0)
11189 + (htab->opd_abi
11190 ? 2 + (htab->params->plt_static_chain
11191 && PPC_HA (off + 16) == PPC_HA (off))
11192 : 1));
11193 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11194 }
11195
11196 size = plt_stub_size (htab, stub_entry, off);
11197
11198 if (stub_entry->h != NULL
11199 && (stub_entry->h == htab->tls_get_addr_fd
11200 || stub_entry->h == htab->tls_get_addr)
11201 && htab->params->tls_get_addr_opt
11202 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11203 {
11204 /* After the bctrl, lr has been modified so we need to
11205 emit .eh_frame info saying the return address is
11206 on the stack. In fact we put the EH info specifying
11207 that the return address is on the stack *at* the
11208 call rather than after it, because the EH info for a
11209 call needs to be specified by that point.
11210 See libgcc/unwind-dw2.c execute_cfa_program. */
11211 lr_used = stub_entry->stub_offset + size - 20;
11212 /* The eh_frame info will consist of a DW_CFA_advance_loc
11213 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11214 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11215 delta = lr_used - stub_entry->group->lr_restore;
11216 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11217 stub_entry->group->lr_restore = size - 4;
11218 }
11219 break;
11220
11221 default:
11222 BFD_FAIL ();
11223 return FALSE;
721956f4
AM
11224 }
11225
6f20ed8a 11226 stub_entry->group->stub_sec->size += size;
b34976b6 11227 return TRUE;
721956f4
AM
11228}
11229
11230/* Set up various things so that we can make a list of input sections
11231 for each output section included in the link. Returns -1 on error,
cedb70c5 11232 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11233
11234int
e7d1c40c 11235ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11236{
6f20ed8a 11237 unsigned int id;
721956f4
AM
11238 bfd_size_type amt;
11239 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11240
4dfe6ac6
NC
11241 if (htab == NULL)
11242 return -1;
4c52953f 11243
7cf7fcc8 11244 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
11245 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11246 htab->sec_info = bfd_zmalloc (amt);
11247 if (htab->sec_info == NULL)
721956f4
AM
11248 return -1;
11249
3d6f9012
AM
11250 /* Set toc_off for com, und, abs and ind sections. */
11251 for (id = 0; id < 3; id++)
6f20ed8a 11252 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11253
721956f4
AM
11254 return 1;
11255}
11256
927be08e
AM
11257/* Set up for first pass at multitoc partitioning. */
11258
11259void
11260ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11261{
11262 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11263
1c865ab2 11264 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11265 htab->toc_bfd = NULL;
11266 htab->toc_first_sec = NULL;
11267}
11268
e717da7e
AM
11269/* The linker repeatedly calls this function for each TOC input section
11270 and linker generated GOT section. Group input bfds such that the toc
927be08e 11271 within a group is less than 64k in size. */
ad8e1ba5 11272
927be08e 11273bfd_boolean
4ce794b7 11274ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11275{
11276 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11277 bfd_vma addr, off, limit;
ad8e1ba5 11278
4dfe6ac6
NC
11279 if (htab == NULL)
11280 return FALSE;
11281
927be08e 11282 if (!htab->second_toc_pass)
4c52953f 11283 {
927be08e 11284 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11285 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11286
11287 if (new_bfd)
bf102f86
AM
11288 {
11289 htab->toc_bfd = isec->owner;
11290 htab->toc_first_sec = isec;
11291 }
927be08e 11292
bf102f86
AM
11293 addr = isec->output_offset + isec->output_section->vma;
11294 off = addr - htab->toc_curr;
d77c8a4b
AM
11295 limit = 0x80008000;
11296 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11297 limit = 0x10000;
11298 if (off + isec->size > limit)
bf102f86
AM
11299 {
11300 addr = (htab->toc_first_sec->output_offset
11301 + htab->toc_first_sec->output_section->vma);
11302 htab->toc_curr = addr;
a27e685f 11303 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11304 }
99877b66 11305
927be08e
AM
11306 /* toc_curr is the base address of this toc group. Set elf_gp
11307 for the input section to be the offset relative to the
11308 output toc base plus 0x8000. Making the input elf_gp an
11309 offset allows us to move the toc as a whole without
11310 recalculating input elf_gp. */
06bcf541 11311 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
11312 off += TOC_BASE_OFF;
11313
11314 /* Die if someone uses a linker script that doesn't keep input
11315 file .toc and .got together. */
a4fd3de5
AM
11316 if (new_bfd
11317 && elf_gp (isec->owner) != 0
927be08e
AM
11318 && elf_gp (isec->owner) != off)
11319 return FALSE;
11320
11321 elf_gp (isec->owner) = off;
11322 return TRUE;
4c52953f 11323 }
927be08e
AM
11324
11325 /* During the second pass toc_first_sec points to the start of
11326 a toc group, and toc_curr is used to track the old elf_gp.
11327 We use toc_bfd to ensure we only look at each bfd once. */
11328 if (htab->toc_bfd == isec->owner)
11329 return TRUE;
11330 htab->toc_bfd = isec->owner;
11331
11332 if (htab->toc_first_sec == NULL
11333 || htab->toc_curr != elf_gp (isec->owner))
11334 {
11335 htab->toc_curr = elf_gp (isec->owner);
11336 htab->toc_first_sec = isec;
11337 }
11338 addr = (htab->toc_first_sec->output_offset
11339 + htab->toc_first_sec->output_section->vma);
06bcf541 11340 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
11341 elf_gp (isec->owner) = off;
11342
11343 return TRUE;
ad8e1ba5
AM
11344}
11345
927be08e
AM
11346/* Called via elf_link_hash_traverse to merge GOT entries for global
11347 symbol H. */
11348
11349static bfd_boolean
11350merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11351{
11352 if (h->root.type == bfd_link_hash_indirect)
11353 return TRUE;
11354
927be08e
AM
11355 merge_got_entries (&h->got.glist);
11356
11357 return TRUE;
11358}
11359
11360/* Called via elf_link_hash_traverse to allocate GOT entries for global
11361 symbol H. */
11362
11363static bfd_boolean
11364reallocate_got (struct elf_link_hash_entry *h, void *inf)
11365{
11366 struct got_entry *gent;
11367
11368 if (h->root.type == bfd_link_hash_indirect)
11369 return TRUE;
11370
927be08e
AM
11371 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11372 if (!gent->is_indirect)
11373 allocate_got (h, (struct bfd_link_info *) inf, gent);
11374 return TRUE;
11375}
11376
11377/* Called on the first multitoc pass after the last call to
11378 ppc64_elf_next_toc_section. This function removes duplicate GOT
11379 entries. */
11380
11381bfd_boolean
11382ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11383{
11384 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11385 struct bfd *ibfd, *ibfd2;
11386 bfd_boolean done_something;
11387
11388 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11389
7865406b
AM
11390 if (!htab->do_multi_toc)
11391 return FALSE;
11392
d0fae19d 11393 /* Merge global sym got entries within a toc group. */
927be08e
AM
11394 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11395
11396 /* And tlsld_got. */
c72f2fb2 11397 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11398 {
11399 struct got_entry *ent, *ent2;
11400
11401 if (!is_ppc64_elf (ibfd))
11402 continue;
11403
11404 ent = ppc64_tlsld_got (ibfd);
11405 if (!ent->is_indirect
11406 && ent->got.offset != (bfd_vma) -1)
11407 {
c72f2fb2 11408 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11409 {
11410 if (!is_ppc64_elf (ibfd2))
11411 continue;
11412
11413 ent2 = ppc64_tlsld_got (ibfd2);
11414 if (!ent2->is_indirect
11415 && ent2->got.offset != (bfd_vma) -1
11416 && elf_gp (ibfd2) == elf_gp (ibfd))
11417 {
11418 ent2->is_indirect = TRUE;
11419 ent2->got.ent = ent;
11420 }
11421 }
11422 }
11423 }
11424
11425 /* Zap sizes of got sections. */
33e44f2e
AM
11426 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11427 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11428 htab->got_reli_size = 0;
11429
c72f2fb2 11430 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11431 {
11432 asection *got, *relgot;
11433
11434 if (!is_ppc64_elf (ibfd))
11435 continue;
11436
11437 got = ppc64_elf_tdata (ibfd)->got;
11438 if (got != NULL)
11439 {
11440 got->rawsize = got->size;
11441 got->size = 0;
11442 relgot = ppc64_elf_tdata (ibfd)->relgot;
11443 relgot->rawsize = relgot->size;
11444 relgot->size = 0;
11445 }
11446 }
11447
11448 /* Now reallocate the got, local syms first. We don't need to
11449 allocate section contents again since we never increase size. */
c72f2fb2 11450 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11451 {
11452 struct got_entry **lgot_ents;
11453 struct got_entry **end_lgot_ents;
11454 struct plt_entry **local_plt;
11455 struct plt_entry **end_local_plt;
f961d9dd 11456 unsigned char *lgot_masks;
927be08e
AM
11457 bfd_size_type locsymcount;
11458 Elf_Internal_Shdr *symtab_hdr;
19e08130 11459 asection *s;
927be08e
AM
11460
11461 if (!is_ppc64_elf (ibfd))
11462 continue;
11463
11464 lgot_ents = elf_local_got_ents (ibfd);
11465 if (!lgot_ents)
11466 continue;
11467
11468 symtab_hdr = &elf_symtab_hdr (ibfd);
11469 locsymcount = symtab_hdr->sh_info;
11470 end_lgot_ents = lgot_ents + locsymcount;
11471 local_plt = (struct plt_entry **) end_lgot_ents;
11472 end_local_plt = local_plt + locsymcount;
f961d9dd 11473 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11474 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11475 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11476 {
11477 struct got_entry *ent;
11478
11479 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11480 {
19e08130
AM
11481 unsigned int ent_size = 8;
11482 unsigned int rel_size = sizeof (Elf64_External_Rela);
11483
d0fae19d
AM
11484 ent->got.offset = s->size;
11485 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11486 {
19e08130
AM
11487 ent_size *= 2;
11488 rel_size *= 2;
11489 }
11490 s->size += ent_size;
37da22e5 11491 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 11492 {
33e44f2e 11493 htab->elf.irelplt->size += rel_size;
19e08130
AM
11494 htab->got_reli_size += rel_size;
11495 }
6a3858a6
AM
11496 else if (bfd_link_pic (info)
11497 && !((ent->tls_type & TLS_TPREL) != 0
11498 && bfd_link_executable (info)))
19e08130
AM
11499 {
11500 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11501 srel->size += rel_size;
d0fae19d
AM
11502 }
11503 }
927be08e
AM
11504 }
11505 }
11506
11507 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11508
c72f2fb2 11509 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11510 {
11511 struct got_entry *ent;
11512
11513 if (!is_ppc64_elf (ibfd))
11514 continue;
11515
11516 ent = ppc64_tlsld_got (ibfd);
11517 if (!ent->is_indirect
11518 && ent->got.offset != (bfd_vma) -1)
11519 {
11520 asection *s = ppc64_elf_tdata (ibfd)->got;
11521 ent->got.offset = s->size;
11522 s->size += 16;
0e1862bb 11523 if (bfd_link_pic (info))
927be08e
AM
11524 {
11525 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11526 srel->size += sizeof (Elf64_External_Rela);
11527 }
11528 }
11529 }
11530
33e44f2e 11531 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11532 if (!done_something)
c72f2fb2 11533 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11534 {
11535 asection *got;
11536
11537 if (!is_ppc64_elf (ibfd))
11538 continue;
11539
11540 got = ppc64_elf_tdata (ibfd)->got;
11541 if (got != NULL)
11542 {
11543 done_something = got->rawsize != got->size;
11544 if (done_something)
11545 break;
11546 }
11547 }
11548
11549 if (done_something)
e7d1c40c 11550 (*htab->params->layout_sections_again) ();
927be08e
AM
11551
11552 /* Set up for second pass over toc sections to recalculate elf_gp
11553 on input sections. */
11554 htab->toc_bfd = NULL;
11555 htab->toc_first_sec = NULL;
11556 htab->second_toc_pass = TRUE;
11557 return done_something;
11558}
11559
11560/* Called after second pass of multitoc partitioning. */
11561
11562void
11563ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11564{
11565 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11566
11567 /* After the second pass, toc_curr tracks the TOC offset used
11568 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11569 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11570}
11571
9b5ecbd0
AM
11572/* No toc references were found in ISEC. If the code in ISEC makes no
11573 calls, then there's no need to use toc adjusting stubs when branching
11574 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11575 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11576 needed, and 2 if a cyclical call-graph was found but no other reason
11577 for a stub was detected. If called from the top level, a return of
11578 2 means the same as a return of 0. */
9b5ecbd0
AM
11579
11580static int
4ce794b7 11581toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11582{
9b5ecbd0 11583 int ret;
70cc837d
AM
11584
11585 /* Mark this section as checked. */
11586 isec->call_check_done = 1;
9b5ecbd0 11587
772119ce
AM
11588 /* We know none of our code bearing sections will need toc stubs. */
11589 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11590 return 0;
11591
eea6121a 11592 if (isec->size == 0)
082c50f8
AM
11593 return 0;
11594
4c52953f
AM
11595 if (isec->output_section == NULL)
11596 return 0;
11597
4c52953f 11598 ret = 0;
70cc837d 11599 if (isec->reloc_count != 0)
9b5ecbd0 11600 {
70cc837d
AM
11601 Elf_Internal_Rela *relstart, *rel;
11602 Elf_Internal_Sym *local_syms;
11603 struct ppc_link_hash_table *htab;
2917689a 11604
70cc837d
AM
11605 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11606 info->keep_memory);
11607 if (relstart == NULL)
11608 return -1;
90aecf7a 11609
70cc837d
AM
11610 /* Look for branches to outside of this section. */
11611 local_syms = NULL;
11612 htab = ppc_hash_table (info);
11613 if (htab == NULL)
11614 return -1;
4c52953f 11615
70cc837d 11616 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11617 {
70cc837d
AM
11618 enum elf_ppc64_reloc_type r_type;
11619 unsigned long r_symndx;
11620 struct elf_link_hash_entry *h;
11621 struct ppc_link_hash_entry *eh;
11622 Elf_Internal_Sym *sym;
11623 asection *sym_sec;
11624 struct _opd_sec_data *opd;
11625 bfd_vma sym_value;
11626 bfd_vma dest;
11627
11628 r_type = ELF64_R_TYPE (rel->r_info);
11629 if (r_type != R_PPC64_REL24
05d0e962 11630 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
11631 && r_type != R_PPC64_REL14
11632 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d
AM
11633 && r_type != R_PPC64_REL14_BRNTAKEN
11634 && r_type != R_PPC64_PLTCALL)
70cc837d 11635 continue;
4c52953f 11636
70cc837d
AM
11637 r_symndx = ELF64_R_SYM (rel->r_info);
11638 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11639 isec->owner))
4c52953f 11640 {
70cc837d
AM
11641 ret = -1;
11642 break;
11643 }
4c52953f 11644
70cc837d
AM
11645 /* Calls to dynamic lib functions go through a plt call stub
11646 that uses r2. */
11647 eh = (struct ppc_link_hash_entry *) h;
11648 if (eh != NULL
11649 && (eh->elf.plt.plist != NULL
11650 || (eh->oh != NULL
11651 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11652 {
11653 ret = 1;
11654 break;
4c52953f
AM
11655 }
11656
70cc837d
AM
11657 if (sym_sec == NULL)
11658 /* Ignore other undefined symbols. */
4c52953f 11659 continue;
4c52953f 11660
70cc837d
AM
11661 /* Assume branches to other sections not included in the
11662 link need stubs too, to cover -R and absolute syms. */
11663 if (sym_sec->output_section == NULL)
11664 {
11665 ret = 1;
11666 break;
11667 }
4c52953f 11668
70cc837d
AM
11669 if (h == NULL)
11670 sym_value = sym->st_value;
11671 else
11672 {
11673 if (h->root.type != bfd_link_hash_defined
11674 && h->root.type != bfd_link_hash_defweak)
11675 abort ();
11676 sym_value = h->root.u.def.value;
11677 }
11678 sym_value += rel->r_addend;
4c52953f 11679
70cc837d
AM
11680 /* If this branch reloc uses an opd sym, find the code section. */
11681 opd = get_opd_info (sym_sec);
11682 if (opd != NULL)
11683 {
11684 if (h == NULL && opd->adjust != NULL)
11685 {
11686 long adjust;
4c52953f 11687
92a9c616 11688 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
11689 if (adjust == -1)
11690 /* Assume deleted functions won't ever be called. */
11691 continue;
11692 sym_value += adjust;
11693 }
4c52953f 11694
aef36ac1
AM
11695 dest = opd_entry_value (sym_sec, sym_value,
11696 &sym_sec, NULL, FALSE);
70cc837d
AM
11697 if (dest == (bfd_vma) -1)
11698 continue;
11699 }
11700 else
11701 dest = (sym_value
11702 + sym_sec->output_offset
11703 + sym_sec->output_section->vma);
4c52953f 11704
70cc837d
AM
11705 /* Ignore branch to self. */
11706 if (sym_sec == isec)
11707 continue;
4c52953f 11708
70cc837d
AM
11709 /* If the called function uses the toc, we need a stub. */
11710 if (sym_sec->has_toc_reloc
11711 || sym_sec->makes_toc_func_call)
4c52953f 11712 {
70cc837d 11713 ret = 1;
4c52953f
AM
11714 break;
11715 }
70cc837d
AM
11716
11717 /* Assume any branch that needs a long branch stub might in fact
11718 need a plt_branch stub. A plt_branch stub uses r2. */
11719 else if (dest - (isec->output_offset
11720 + isec->output_section->vma
6911b7dc
AM
11721 + rel->r_offset) + (1 << 25)
11722 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11723 ? h->other
11724 : sym->st_other))
4c52953f 11725 {
70cc837d
AM
11726 ret = 1;
11727 break;
11728 }
11729
11730 /* If calling back to a section in the process of being
11731 tested, we can't say for sure that no toc adjusting stubs
11732 are needed, so don't return zero. */
11733 else if (sym_sec->call_check_in_progress)
11734 ret = 2;
11735
11736 /* Branches to another section that itself doesn't have any TOC
11737 references are OK. Recursively call ourselves to check. */
11738 else if (!sym_sec->call_check_done)
11739 {
11740 int recur;
11741
11742 /* Mark current section as indeterminate, so that other
11743 sections that call back to current won't be marked as
11744 known. */
11745 isec->call_check_in_progress = 1;
11746 recur = toc_adjusting_stub_needed (info, sym_sec);
11747 isec->call_check_in_progress = 0;
11748
4c52953f
AM
11749 if (recur != 0)
11750 {
70cc837d
AM
11751 ret = recur;
11752 if (recur != 2)
11753 break;
4c52953f
AM
11754 }
11755 }
4c52953f 11756 }
70cc837d
AM
11757
11758 if (local_syms != NULL
11759 && (elf_symtab_hdr (isec->owner).contents
11760 != (unsigned char *) local_syms))
11761 free (local_syms);
11762 if (elf_section_data (isec)->relocs != relstart)
11763 free (relstart);
9b5ecbd0
AM
11764 }
11765
70cc837d
AM
11766 if ((ret & 1) == 0
11767 && isec->map_head.s != NULL
11768 && (strcmp (isec->output_section->name, ".init") == 0
11769 || strcmp (isec->output_section->name, ".fini") == 0))
11770 {
11771 if (isec->map_head.s->has_toc_reloc
11772 || isec->map_head.s->makes_toc_func_call)
11773 ret = 1;
11774 else if (!isec->map_head.s->call_check_done)
11775 {
11776 int recur;
11777 isec->call_check_in_progress = 1;
11778 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11779 isec->call_check_in_progress = 0;
11780 if (recur != 0)
11781 ret = recur;
11782 }
11783 }
11784
11785 if (ret == 1)
11786 isec->makes_toc_func_call = 1;
4c52953f 11787
9b5ecbd0
AM
11788 return ret;
11789}
11790
721956f4
AM
11791/* The linker repeatedly calls this function for each input section,
11792 in the order that input sections are linked into output sections.
11793 Build lists of input sections to determine groupings between which
11794 we may insert linker stubs. */
11795
9b5ecbd0 11796bfd_boolean
4ce794b7 11797ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
11798{
11799 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11800
4dfe6ac6
NC
11801 if (htab == NULL)
11802 return FALSE;
11803
734b6cf9 11804 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 11805 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 11806 {
3d6f9012
AM
11807 /* This happens to make the list in reverse order,
11808 which is what we want. */
6f20ed8a
AM
11809 htab->sec_info[isec->id].u.list
11810 = htab->sec_info[isec->output_section->id].u.list;
11811 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 11812 }
ad8e1ba5 11813
4c52953f 11814 if (htab->multi_toc_needed)
9b5ecbd0 11815 {
8b974ba3
AM
11816 /* Analyse sections that aren't already flagged as needing a
11817 valid toc pointer. Exclude .fixup for the linux kernel.
11818 .fixup contains branches, but only back to the function that
11819 hit an exception. */
11820 if (!(isec->has_toc_reloc
11821 || (isec->flags & SEC_CODE) == 0
11822 || strcmp (isec->name, ".fixup") == 0
11823 || isec->call_check_done))
11824 {
11825 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 11826 return FALSE;
8b974ba3
AM
11827 }
11828 /* Make all sections use the TOC assigned for this object file.
11829 This will be wrong for pasted sections; We fix that in
11830 check_pasted_section(). */
11831 if (elf_gp (isec->owner) != 0)
11832 htab->toc_curr = elf_gp (isec->owner);
11833 }
11834
6f20ed8a 11835 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 11836 return TRUE;
721956f4
AM
11837}
11838
70cc837d
AM
11839/* Check that all .init and .fini sections use the same toc, if they
11840 have toc relocs. */
11841
11842static bfd_boolean
11843check_pasted_section (struct bfd_link_info *info, const char *name)
11844{
11845 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11846
11847 if (o != NULL)
11848 {
11849 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11850 bfd_vma toc_off = 0;
11851 asection *i;
11852
11853 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11854 if (i->has_toc_reloc)
11855 {
11856 if (toc_off == 0)
6f20ed8a
AM
11857 toc_off = htab->sec_info[i->id].toc_off;
11858 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
11859 return FALSE;
11860 }
6683a28d
AM
11861
11862 if (toc_off == 0)
11863 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11864 if (i->makes_toc_func_call)
11865 {
6f20ed8a 11866 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
11867 break;
11868 }
11869
70cc837d
AM
11870 /* Make sure the whole pasted function uses the same toc offset. */
11871 if (toc_off != 0)
11872 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 11873 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
11874 }
11875 return TRUE;
11876}
11877
11878bfd_boolean
11879ppc64_elf_check_init_fini (struct bfd_link_info *info)
11880{
11881 return (check_pasted_section (info, ".init")
11882 & check_pasted_section (info, ".fini"));
11883}
11884
721956f4
AM
11885/* See whether we can group stub sections together. Grouping stub
11886 sections may result in fewer stubs. More importantly, we need to
11887 put all .init* and .fini* stubs at the beginning of the .init or
11888 .fini output sections respectively, because glibc splits the
11889 _init and _fini functions into multiple parts. Putting a stub in
11890 the middle of a function is not a good idea. */
11891
6f20ed8a
AM
11892static bfd_boolean
11893group_sections (struct bfd_link_info *info,
4ce794b7
AM
11894 bfd_size_type stub_group_size,
11895 bfd_boolean stubs_always_before_branch)
721956f4 11896{
6f20ed8a
AM
11897 struct ppc_link_hash_table *htab;
11898 asection *osec;
7c8fe5c4
AM
11899 bfd_boolean suppress_size_errors;
11900
6f20ed8a
AM
11901 htab = ppc_hash_table (info);
11902 if (htab == NULL)
11903 return FALSE;
11904
7c8fe5c4 11905 suppress_size_errors = FALSE;
7c8fe5c4
AM
11906 if (stub_group_size == 1)
11907 {
11908 /* Default values. */
11909 if (stubs_always_before_branch)
09f92717 11910 stub_group_size = 0x1e00000;
7c8fe5c4 11911 else
09f92717 11912 stub_group_size = 0x1c00000;
7c8fe5c4
AM
11913 suppress_size_errors = TRUE;
11914 }
11915
6f20ed8a 11916 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 11917 {
6f20ed8a
AM
11918 asection *tail;
11919
11920 if (osec->id >= htab->sec_info_arr_size)
11921 continue;
11922
11923 tail = htab->sec_info[osec->id].u.list;
734b6cf9 11924 while (tail != NULL)
721956f4 11925 {
734b6cf9
AM
11926 asection *curr;
11927 asection *prev;
11928 bfd_size_type total;
11929 bfd_boolean big_sec;
11930 bfd_vma curr_toc;
6f20ed8a 11931 struct map_stub *group;
09f92717 11932 bfd_size_type group_size;
734b6cf9
AM
11933
11934 curr = tail;
eea6121a 11935 total = tail->size;
09f92717
AM
11936 group_size = (ppc64_elf_section_data (tail) != NULL
11937 && ppc64_elf_section_data (tail)->has_14bit_branch
11938 ? stub_group_size >> 10 : stub_group_size);
11939
11940 big_sec = total > group_size;
7c8fe5c4 11941 if (big_sec && !suppress_size_errors)
695344c0 11942 /* xgettext:c-format */
871b3ab2 11943 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 11944 tail->owner, tail);
6f20ed8a 11945 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 11946
6f20ed8a 11947 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 11948 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
11949 < (ppc64_elf_section_data (prev) != NULL
11950 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 11951 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 11952 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
11953 curr = prev;
11954
11955 /* OK, the size from the start of CURR to the end is less
09f92717 11956 than group_size and thus can be handled by one stub
734b6cf9 11957 section. (or the tail section is itself larger than
09f92717
AM
11958 group_size, in which case we may be toast.) We should
11959 really be keeping track of the total size of stubs added
11960 here, as stubs contribute to the final output section
11961 size. That's a little tricky, and this way will only
11962 break if stubs added make the total size more than 2^25,
11963 ie. for the default stub_group_size, if stubs total more
11964 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
11965 group = bfd_alloc (curr->owner, sizeof (*group));
11966 if (group == NULL)
11967 return FALSE;
11968 group->link_sec = curr;
11969 group->stub_sec = NULL;
a4b6fadd 11970 group->needs_save_res = 0;
df136d64
AM
11971 group->lr_restore = 0;
11972 group->eh_size = 0;
11973 group->eh_base = 0;
a4b6fadd
AM
11974 group->next = htab->group;
11975 htab->group = group;
734b6cf9 11976 do
721956f4 11977 {
6f20ed8a 11978 prev = htab->sec_info[tail->id].u.list;
734b6cf9 11979 /* Set up this stub group. */
6f20ed8a 11980 htab->sec_info[tail->id].u.group = group;
721956f4 11981 }
734b6cf9
AM
11982 while (tail != curr && (tail = prev) != NULL);
11983
09f92717 11984 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
11985 bytes before the stub section can be handled by it too.
11986 Don't do this if we have a really large section after the
11987 stubs, as adding more stubs increases the chance that
11988 branches may not reach into the stub section. */
11989 if (!stubs_always_before_branch && !big_sec)
11990 {
11991 total = 0;
11992 while (prev != NULL
11993 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
11994 < (ppc64_elf_section_data (prev) != NULL
11995 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
11996 ? (group_size = stub_group_size >> 10)
11997 : group_size))
6f20ed8a 11998 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
11999 {
12000 tail = prev;
6f20ed8a
AM
12001 prev = htab->sec_info[tail->id].u.list;
12002 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12003 }
12004 }
12005 tail = prev;
721956f4
AM
12006 }
12007 }
6f20ed8a 12008 return TRUE;
721956f4
AM
12009}
12010
58d180e8
AM
12011static const unsigned char glink_eh_frame_cie[] =
12012{
12013 0, 0, 0, 16, /* length. */
12014 0, 0, 0, 0, /* id. */
12015 1, /* CIE version. */
12016 'z', 'R', 0, /* Augmentation string. */
12017 4, /* Code alignment. */
12018 0x78, /* Data alignment. */
12019 65, /* RA reg. */
12020 1, /* Augmentation size. */
12021 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12022 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12023};
12024
d969d15f
AM
12025/* Stripping output sections is normally done before dynamic section
12026 symbols have been allocated. This function is called later, and
12027 handles cases like htab->brlt which is mapped to its own output
12028 section. */
12029
12030static void
12031maybe_strip_output (struct bfd_link_info *info, asection *isec)
12032{
12033 if (isec->size == 0
12034 && isec->output_section->size == 0
53d8967a 12035 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12036 && !bfd_section_removed_from_list (info->output_bfd,
12037 isec->output_section)
12038 && elf_section_data (isec->output_section)->dynindx == 0)
12039 {
12040 isec->output_section->flags |= SEC_EXCLUDE;
12041 bfd_section_list_remove (info->output_bfd, isec->output_section);
12042 info->output_bfd->section_count--;
12043 }
12044}
12045
721956f4
AM
12046/* Determine and set the size of the stub section for a final link.
12047
12048 The basic idea here is to examine all the relocations looking for
12049 PC-relative calls to a target that is unreachable with a "bl"
12050 instruction. */
12051
b34976b6 12052bfd_boolean
e7d1c40c 12053ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12054{
12055 bfd_size_type stub_group_size;
b34976b6 12056 bfd_boolean stubs_always_before_branch;
721956f4
AM
12057 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12058
4dfe6ac6
NC
12059 if (htab == NULL)
12060 return FALSE;
12061
0e1862bb 12062 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12063 htab->params->plt_thread_safe = 1;
b9e5796b 12064 if (!htab->opd_abi)
e7d1c40c
AM
12065 htab->params->plt_thread_safe = 0;
12066 else if (htab->params->plt_thread_safe == -1)
794e51c0 12067 {
e2458743 12068 static const char *const thread_starter[] =
794e51c0
AM
12069 {
12070 "pthread_create",
12071 /* libstdc++ */
12072 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12073 /* librt */
12074 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12075 "mq_notify", "create_timer",
12076 /* libanl */
12077 "getaddrinfo_a",
12078 /* libgomp */
2300b5a1 12079 "GOMP_parallel",
794e51c0 12080 "GOMP_parallel_start",
2300b5a1 12081 "GOMP_parallel_loop_static",
794e51c0 12082 "GOMP_parallel_loop_static_start",
2300b5a1 12083 "GOMP_parallel_loop_dynamic",
794e51c0 12084 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12085 "GOMP_parallel_loop_guided",
794e51c0 12086 "GOMP_parallel_loop_guided_start",
2300b5a1 12087 "GOMP_parallel_loop_runtime",
794e51c0 12088 "GOMP_parallel_loop_runtime_start",
2300b5a1 12089 "GOMP_parallel_sections",
68ffbac6 12090 "GOMP_parallel_sections_start",
f9dffbf0
AM
12091 /* libgo */
12092 "__go_go",
794e51c0
AM
12093 };
12094 unsigned i;
12095
a4b6fadd 12096 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12097 {
12098 struct elf_link_hash_entry *h;
12099 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12100 FALSE, FALSE, TRUE);
e7d1c40c
AM
12101 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12102 if (htab->params->plt_thread_safe)
794e51c0
AM
12103 break;
12104 }
12105 }
e7d1c40c
AM
12106 stubs_always_before_branch = htab->params->group_size < 0;
12107 if (htab->params->group_size < 0)
12108 stub_group_size = -htab->params->group_size;
721956f4 12109 else
e7d1c40c 12110 stub_group_size = htab->params->group_size;
721956f4 12111
6f20ed8a
AM
12112 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12113 return FALSE;
721956f4 12114
c9301e31
AM
12115#define STUB_SHRINK_ITER 20
12116 /* Loop until no stubs added. After iteration 20 of this loop we may
12117 exit on a stub section shrinking. This is to break out of a
12118 pathological case where adding stubs on one iteration decreases
12119 section gaps (perhaps due to alignment), which then requires
12120 fewer or smaller stubs on the next iteration. */
12121
721956f4
AM
12122 while (1)
12123 {
12124 bfd *input_bfd;
12125 unsigned int bfd_indx;
a4b6fadd 12126 struct map_stub *group;
721956f4
AM
12127
12128 htab->stub_iteration += 1;
721956f4
AM
12129
12130 for (input_bfd = info->input_bfds, bfd_indx = 0;
12131 input_bfd != NULL;
c72f2fb2 12132 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12133 {
12134 Elf_Internal_Shdr *symtab_hdr;
12135 asection *section;
6cdc0ccc 12136 Elf_Internal_Sym *local_syms = NULL;
721956f4 12137
0c8d6e5c 12138 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12139 continue;
12140
721956f4 12141 /* We'll need the symbol table in a second. */
0ffa91dd 12142 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12143 if (symtab_hdr->sh_info == 0)
12144 continue;
12145
721956f4
AM
12146 /* Walk over each section attached to the input bfd. */
12147 for (section = input_bfd->sections;
12148 section != NULL;
12149 section = section->next)
12150 {
721956f4 12151 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12152
12153 /* If there aren't any relocs, then there's nothing more
12154 to do. */
12155 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12156 || (section->flags & SEC_ALLOC) == 0
12157 || (section->flags & SEC_LOAD) == 0
12158 || (section->flags & SEC_CODE) == 0
721956f4
AM
12159 || section->reloc_count == 0)
12160 continue;
12161
12162 /* If this section is a link-once section that will be
12163 discarded, then don't create any stubs. */
12164 if (section->output_section == NULL
927be08e 12165 || section->output_section->owner != info->output_bfd)
721956f4
AM
12166 continue;
12167
1e2f5b6e
AM
12168 /* Get the relocs. */
12169 internal_relocs
4ce794b7 12170 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12171 info->keep_memory);
721956f4 12172 if (internal_relocs == NULL)
1e2f5b6e 12173 goto error_ret_free_local;
721956f4
AM
12174
12175 /* Now examine each relocation. */
12176 irela = internal_relocs;
12177 irelaend = irela + section->reloc_count;
12178 for (; irela < irelaend; irela++)
12179 {
4ce794b7
AM
12180 enum elf_ppc64_reloc_type r_type;
12181 unsigned int r_indx;
721956f4
AM
12182 enum ppc_stub_type stub_type;
12183 struct ppc_stub_hash_entry *stub_entry;
8387904d 12184 asection *sym_sec, *code_sec;
e054468f 12185 bfd_vma sym_value, code_value;
721956f4 12186 bfd_vma destination;
6911b7dc 12187 unsigned long local_off;
8843416a 12188 bfd_boolean ok_dest;
721956f4 12189 struct ppc_link_hash_entry *hash;
8387904d 12190 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12191 struct elf_link_hash_entry *h;
12192 Elf_Internal_Sym *sym;
721956f4
AM
12193 char *stub_name;
12194 const asection *id_sec;
74f0fb50 12195 struct _opd_sec_data *opd;
e054468f 12196 struct plt_entry *plt_ent;
721956f4
AM
12197
12198 r_type = ELF64_R_TYPE (irela->r_info);
12199 r_indx = ELF64_R_SYM (irela->r_info);
12200
4ce794b7 12201 if (r_type >= R_PPC64_max)
721956f4
AM
12202 {
12203 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12204 goto error_ret_free_internal;
721956f4
AM
12205 }
12206
12207 /* Only look for stubs on branch instructions. */
4ce794b7 12208 if (r_type != R_PPC64_REL24
05d0e962 12209 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
12210 && r_type != R_PPC64_REL14
12211 && r_type != R_PPC64_REL14_BRTAKEN
12212 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12213 continue;
12214
12215 /* Now determine the call target, its name, value,
12216 section. */
411e1bfb
AM
12217 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12218 r_indx, input_bfd))
12219 goto error_ret_free_internal;
12220 hash = (struct ppc_link_hash_entry *) h;
12221
8843416a 12222 ok_dest = FALSE;
8387904d 12223 fdh = NULL;
7fe2b9a6 12224 sym_value = 0;
411e1bfb 12225 if (hash == NULL)
721956f4 12226 {
411e1bfb 12227 sym_value = sym->st_value;
c27b8c2a
AM
12228 if (sym_sec != NULL
12229 && sym_sec->output_section != NULL)
12230 ok_dest = TRUE;
721956f4 12231 }
7fe2b9a6
AM
12232 else if (hash->elf.root.type == bfd_link_hash_defined
12233 || hash->elf.root.type == bfd_link_hash_defweak)
12234 {
12235 sym_value = hash->elf.root.u.def.value;
12236 if (sym_sec->output_section != NULL)
12237 ok_dest = TRUE;
12238 }
12239 else if (hash->elf.root.type == bfd_link_hash_undefweak
12240 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12241 {
99877b66 12242 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12243 use the func descriptor sym instead if it is
12244 defined. */
ceb1f1ef 12245 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12246 && hash->oh != NULL)
8387904d 12247 {
8c5b4e52 12248 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12249 if (fdh->elf.root.type == bfd_link_hash_defined
12250 || fdh->elf.root.type == bfd_link_hash_defweak)
12251 {
12252 sym_sec = fdh->elf.root.u.def.section;
12253 sym_value = fdh->elf.root.u.def.value;
12254 if (sym_sec->output_section != NULL)
12255 ok_dest = TRUE;
12256 }
99877b66
AM
12257 else
12258 fdh = NULL;
8387904d 12259 }
7fe2b9a6
AM
12260 }
12261 else
12262 {
12263 bfd_set_error (bfd_error_bad_value);
12264 goto error_ret_free_internal;
721956f4
AM
12265 }
12266
8843416a 12267 destination = 0;
6911b7dc 12268 local_off = 0;
8843416a
AM
12269 if (ok_dest)
12270 {
12271 sym_value += irela->r_addend;
12272 destination = (sym_value
12273 + sym_sec->output_offset
12274 + sym_sec->output_section->vma);
6911b7dc
AM
12275 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12276 ? hash->elf.other
12277 : sym->st_other);
8843416a
AM
12278 }
12279
8387904d 12280 code_sec = sym_sec;
e054468f 12281 code_value = sym_value;
74f0fb50
AM
12282 opd = get_opd_info (sym_sec);
12283 if (opd != NULL)
8387904d
AM
12284 {
12285 bfd_vma dest;
12286
74f0fb50 12287 if (hash == NULL && opd->adjust != NULL)
8387904d 12288 {
51aecdc5 12289 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12290 if (adjust == -1)
12291 continue;
e054468f 12292 code_value += adjust;
8387904d
AM
12293 sym_value += adjust;
12294 }
12295 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12296 &code_sec, &code_value, FALSE);
8387904d
AM
12297 if (dest != (bfd_vma) -1)
12298 {
12299 destination = dest;
12300 if (fdh != NULL)
12301 {
12302 /* Fixup old ABI sym to point at code
12303 entry. */
99877b66 12304 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12305 hash->elf.root.u.def.section = code_sec;
e054468f 12306 hash->elf.root.u.def.value = code_value;
8387904d
AM
12307 }
12308 }
12309 }
12310
721956f4 12311 /* Determine what (if any) linker stub is needed. */
e054468f 12312 plt_ent = NULL;
721956f4 12313 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12314 &plt_ent, destination,
12315 local_off);
ad8e1ba5 12316
05d0e962
AM
12317 if (r_type == R_PPC64_REL24_NOTOC)
12318 {
12319 if (stub_type == ppc_stub_plt_call)
12320 stub_type = ppc_stub_plt_call_notoc;
12321 else if (stub_type == ppc_stub_long_branch
12322 || (code_sec != NULL
12323 && code_sec->output_section != NULL
12324 && (((hash ? hash->elf.other : sym->st_other)
12325 & STO_PPC64_LOCAL_MASK)
12326 != 1 << STO_PPC64_LOCAL_BIT)))
12327 stub_type = ppc_stub_long_branch_notoc;
12328 }
12329 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
12330 {
12331 /* Check whether we need a TOC adjusting stub.
12332 Since the linker pastes together pieces from
12333 different object files when creating the
12334 _init and _fini functions, it may be that a
12335 call to what looks like a local sym is in
12336 fact a call needing a TOC adjustment. */
33cb30a1
AM
12337 if ((code_sec != NULL
12338 && code_sec->output_section != NULL
12339 && (htab->sec_info[code_sec->id].toc_off
12340 != htab->sec_info[section->id].toc_off)
12341 && (code_sec->has_toc_reloc
12342 || code_sec->makes_toc_func_call))
12343 || (((hash ? hash->elf.other : sym->st_other)
12344 & STO_PPC64_LOCAL_MASK)
12345 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
12346 stub_type = ppc_stub_long_branch_r2off;
12347 }
12348
721956f4
AM
12349 if (stub_type == ppc_stub_none)
12350 continue;
12351
411e1bfb
AM
12352 /* __tls_get_addr calls might be eliminated. */
12353 if (stub_type != ppc_stub_plt_call
05d0e962 12354 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 12355 && hash != NULL
8387904d
AM
12356 && (hash == htab->tls_get_addr
12357 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12358 && section->has_tls_reloc
12359 && irela != internal_relocs)
12360 {
12361 /* Get tls info. */
f961d9dd 12362 unsigned char *tls_mask;
411e1bfb 12363
3a71aa26 12364 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12365 irela - 1, input_bfd))
12366 goto error_ret_free_internal;
37da22e5 12367 if ((*tls_mask & TLS_TLS) != 0)
411e1bfb
AM
12368 continue;
12369 }
12370
f378ab09 12371 if (stub_type == ppc_stub_plt_call)
794e51c0 12372 {
6e1816be
AM
12373 if (!htab->opd_abi
12374 && htab->params->plt_localentry0 != 0
12375 && is_elfv2_localentry0 (&hash->elf))
12376 htab->has_plt_localentry0 = 1;
12377 else if (irela + 1 < irelaend
12378 && irela[1].r_offset == irela->r_offset + 4
12379 && (ELF64_R_TYPE (irela[1].r_info)
12380 == R_PPC64_TOCSAVE))
f378ab09
AM
12381 {
12382 if (!tocsave_find (htab, INSERT,
12383 &local_syms, irela + 1, input_bfd))
12384 goto error_ret_free_internal;
12385 }
f378ab09
AM
12386 else
12387 stub_type = ppc_stub_plt_call_r2save;
794e51c0 12388 }
3b421ab3 12389
721956f4 12390 /* Support for grouping stub sections. */
6f20ed8a 12391 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12392
12393 /* Get the name of this stub. */
12394 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12395 if (!stub_name)
12396 goto error_ret_free_internal;
12397
12398 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12399 stub_name, FALSE, FALSE);
721956f4
AM
12400 if (stub_entry != NULL)
12401 {
05d0e962
AM
12402 enum ppc_stub_type old_type;
12403 /* A stub has already been created, but it may
12404 not be the required type. We shouldn't be
12405 transitioning from plt_call to long_branch
12406 stubs or vice versa, but we might be
12407 upgrading from plt_call to plt_call_r2save or
12408 from long_branch to long_branch_r2off. */
721956f4 12409 free (stub_name);
05d0e962
AM
12410 old_type = stub_entry->stub_type;
12411 switch (old_type)
12412 {
12413 default:
12414 abort ();
12415
12416 case ppc_stub_save_res:
12417 continue;
12418
12419 case ppc_stub_plt_call:
12420 case ppc_stub_plt_call_r2save:
12421 case ppc_stub_plt_call_notoc:
12422 case ppc_stub_plt_call_both:
12423 if (stub_type == ppc_stub_plt_call)
12424 continue;
12425 else if (stub_type == ppc_stub_plt_call_r2save)
12426 {
12427 if (old_type == ppc_stub_plt_call_notoc)
12428 stub_type = ppc_stub_plt_call_both;
12429 }
12430 else if (stub_type == ppc_stub_plt_call_notoc)
12431 {
12432 if (old_type == ppc_stub_plt_call_r2save)
12433 stub_type = ppc_stub_plt_call_both;
12434 }
12435 else
12436 abort ();
12437 break;
12438
12439 case ppc_stub_plt_branch:
12440 case ppc_stub_plt_branch_r2off:
12441 case ppc_stub_plt_branch_notoc:
12442 case ppc_stub_plt_branch_both:
12443 old_type += (ppc_stub_long_branch
12444 - ppc_stub_plt_branch);
12445 /* Fall through. */
12446 case ppc_stub_long_branch:
12447 case ppc_stub_long_branch_r2off:
12448 case ppc_stub_long_branch_notoc:
12449 case ppc_stub_long_branch_both:
12450 if (stub_type == ppc_stub_long_branch)
12451 continue;
12452 else if (stub_type == ppc_stub_long_branch_r2off)
12453 {
12454 if (old_type == ppc_stub_long_branch_notoc)
12455 stub_type = ppc_stub_long_branch_both;
12456 }
12457 else if (stub_type == ppc_stub_long_branch_notoc)
12458 {
12459 if (old_type == ppc_stub_long_branch_r2off)
12460 stub_type = ppc_stub_long_branch_both;
12461 }
12462 else
12463 abort ();
12464 break;
12465 }
12466 if (old_type < stub_type)
794e51c0 12467 stub_entry->stub_type = stub_type;
721956f4
AM
12468 continue;
12469 }
12470
25f53a85 12471 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12472 if (stub_entry == NULL)
12473 {
12474 free (stub_name);
6cdc0ccc
AM
12475 error_ret_free_internal:
12476 if (elf_section_data (section)->relocs == NULL)
12477 free (internal_relocs);
12478 error_ret_free_local:
12479 if (local_syms != NULL
12480 && (symtab_hdr->contents
12481 != (unsigned char *) local_syms))
12482 free (local_syms);
b34976b6 12483 return FALSE;
721956f4
AM
12484 }
12485
ad8e1ba5 12486 stub_entry->stub_type = stub_type;
05d0e962
AM
12487 if (stub_type >= ppc_stub_plt_call
12488 && stub_type <= ppc_stub_plt_call_both)
e054468f 12489 {
05d0e962
AM
12490 stub_entry->target_value = sym_value;
12491 stub_entry->target_section = sym_sec;
e054468f
AM
12492 }
12493 else
12494 {
05d0e962
AM
12495 stub_entry->target_value = code_value;
12496 stub_entry->target_section = code_sec;
e054468f 12497 }
721956f4 12498 stub_entry->h = hash;
e054468f 12499 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
12500 stub_entry->symtype
12501 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 12502 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 12503
3d58e1fc
AM
12504 if (hash != NULL
12505 && (hash->elf.root.type == bfd_link_hash_defined
12506 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 12507 htab->stub_globals += 1;
721956f4
AM
12508 }
12509
12510 /* We're done with the internal relocs, free them. */
6cdc0ccc 12511 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12512 free (internal_relocs);
721956f4 12513 }
6cdc0ccc
AM
12514
12515 if (local_syms != NULL
12516 && symtab_hdr->contents != (unsigned char *) local_syms)
12517 {
12518 if (!info->keep_memory)
12519 free (local_syms);
12520 else
12521 symtab_hdr->contents = (unsigned char *) local_syms;
12522 }
721956f4
AM
12523 }
12524
5c3dead3 12525 /* We may have added some stubs. Find out the new size of the
721956f4 12526 stub sections. */
d4aaa2a0 12527 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
12528 {
12529 group->lr_restore = 0;
12530 group->eh_size = 0;
12531 if (group->stub_sec != NULL)
12532 {
12533 asection *stub_sec = group->stub_sec;
12534
12535 if (htab->stub_iteration <= STUB_SHRINK_ITER
12536 || stub_sec->rawsize < stub_sec->size)
12537 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12538 stub_sec->rawsize = stub_sec->size;
12539 stub_sec->size = 0;
12540 stub_sec->reloc_count = 0;
12541 stub_sec->flags &= ~SEC_RELOC;
12542 }
12543 }
eea6121a 12544
ba21f564
AM
12545 if (htab->stub_iteration <= STUB_SHRINK_ITER
12546 || htab->brlt->rawsize < htab->brlt->size)
12547 htab->brlt->rawsize = htab->brlt->size;
eea6121a 12548 htab->brlt->size = 0;
84f5d08e
AM
12549 htab->brlt->reloc_count = 0;
12550 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12551 if (htab->relbrlt != NULL)
eea6121a 12552 htab->relbrlt->size = 0;
721956f4 12553
63bc6f6c 12554 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12555
a4b6fadd
AM
12556 for (group = htab->group; group != NULL; group = group->next)
12557 if (group->needs_save_res)
12558 group->stub_sec->size += htab->sfpr->size;
12559
176a0d42
AM
12560 if (info->emitrelocations
12561 && htab->glink != NULL && htab->glink->size != 0)
12562 {
12563 htab->glink->reloc_count = 1;
12564 htab->glink->flags |= SEC_RELOC;
12565 }
12566
58d180e8
AM
12567 if (htab->glink_eh_frame != NULL
12568 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 12569 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 12570 {
2e0ce1c8 12571 size_t size = 0, align = 4;
58d180e8 12572
d4aaa2a0 12573 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
12574 if (group->eh_size != 0)
12575 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 12576 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 12577 size += (24 + align - 1) & -align;
58d180e8 12578 if (size != 0)
2e0ce1c8
AM
12579 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12580 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12581 size = (size + align - 1) & -align;
58d180e8
AM
12582 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12583 htab->glink_eh_frame->size = size;
12584 }
12585
e7d1c40c 12586 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
12587 for (group = htab->group; group != NULL; group = group->next)
12588 if (group->stub_sec != NULL)
691d2e9a
AM
12589 {
12590 int align = abs (htab->params->plt_stub_align);
12591 group->stub_sec->size
12592 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12593 }
d4aaa2a0
AM
12594
12595 for (group = htab->group; group != NULL; group = group->next)
12596 if (group->stub_sec != NULL
12597 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 12598 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 12599 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
12600 break;
12601
d4aaa2a0 12602 if (group == NULL
ba21f564
AM
12603 && (htab->brlt->rawsize == htab->brlt->size
12604 || (htab->stub_iteration > STUB_SHRINK_ITER
12605 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
12606 && (htab->glink_eh_frame == NULL
12607 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12608 break;
12609
721956f4 12610 /* Ask the linker to do its stuff. */
e7d1c40c 12611 (*htab->params->layout_sections_again) ();
721956f4
AM
12612 }
12613
da44f4e5
AM
12614 if (htab->glink_eh_frame != NULL
12615 && htab->glink_eh_frame->size != 0)
12616 {
12617 bfd_vma val;
12618 bfd_byte *p, *last_fde;
12619 size_t last_fde_len, size, align, pad;
d4aaa2a0 12620 struct map_stub *group;
da44f4e5 12621
df136d64
AM
12622 /* It is necessary to at least have a rough outline of the
12623 linker generated CIEs and FDEs written before
12624 bfd_elf_discard_info is run, in order for these FDEs to be
12625 indexed in .eh_frame_hdr. */
da44f4e5
AM
12626 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12627 if (p == NULL)
12628 return FALSE;
12629 htab->glink_eh_frame->contents = p;
12630 last_fde = p;
2e0ce1c8 12631 align = 4;
da44f4e5
AM
12632
12633 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12634 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 12635 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 12636 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 12637 p += last_fde_len + 4;
da44f4e5 12638
d4aaa2a0 12639 for (group = htab->group; group != NULL; group = group->next)
df136d64 12640 if (group->eh_size != 0)
da44f4e5 12641 {
df136d64 12642 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 12643 last_fde = p;
df136d64 12644 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 12645 /* FDE length. */
2e0ce1c8 12646 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12647 p += 4;
12648 /* CIE pointer. */
12649 val = p - htab->glink_eh_frame->contents;
12650 bfd_put_32 (htab->elf.dynobj, val, p);
12651 p += 4;
12652 /* Offset to stub section, written later. */
12653 p += 4;
12654 /* stub section size. */
d4aaa2a0 12655 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
12656 p += 4;
12657 /* Augmentation. */
12658 p += 1;
df136d64
AM
12659 /* Make sure we don't have all nops. This is enough for
12660 elf-eh-frame.c to detect the last non-nop opcode. */
12661 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 12662 p = last_fde + last_fde_len + 4;
da44f4e5
AM
12663 }
12664 if (htab->glink != NULL && htab->glink->size != 0)
12665 {
12666 last_fde = p;
2e0ce1c8 12667 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 12668 /* FDE length. */
2e0ce1c8 12669 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12670 p += 4;
12671 /* CIE pointer. */
12672 val = p - htab->glink_eh_frame->contents;
12673 bfd_put_32 (htab->elf.dynobj, val, p);
12674 p += 4;
12675 /* Offset to .glink, written later. */
12676 p += 4;
12677 /* .glink size. */
12678 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12679 p += 4;
12680 /* Augmentation. */
12681 p += 1;
12682
12683 *p++ = DW_CFA_advance_loc + 1;
12684 *p++ = DW_CFA_register;
12685 *p++ = 65;
9f08fa5c 12686 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 12687 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
12688 *p++ = DW_CFA_restore_extended;
12689 *p++ = 65;
2e0ce1c8 12690 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
12691 }
12692 /* Subsume any padding into the last FDE if user .eh_frame
12693 sections are aligned more than glink_eh_frame. Otherwise any
12694 zero padding will be seen as a terminator. */
2e0ce1c8 12695 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 12696 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 12697 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
12698 htab->glink_eh_frame->size = size + pad;
12699 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12700 }
12701
d969d15f
AM
12702 maybe_strip_output (info, htab->brlt);
12703 if (htab->glink_eh_frame != NULL)
12704 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12705
b34976b6 12706 return TRUE;
721956f4
AM
12707}
12708
12709/* Called after we have determined section placement. If sections
805fc799 12710 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12711
805fc799 12712bfd_vma
1c865ab2 12713ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12714{
805fc799 12715 asection *s;
a27e685f 12716 bfd_vma TOCstart, adjust;
721956f4 12717
43417696
AM
12718 if (info != NULL)
12719 {
12720 struct elf_link_hash_entry *h;
12721 struct elf_link_hash_table *htab = elf_hash_table (info);
12722
12723 if (is_elf_hash_table (htab)
12724 && htab->hgot != NULL)
12725 h = htab->hgot;
12726 else
12727 {
12728 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12729 if (is_elf_hash_table (htab))
12730 htab->hgot = h;
12731 }
12732 if (h != NULL
12733 && h->root.type == bfd_link_hash_defined
12734 && !h->root.linker_def
12735 && (!is_elf_hash_table (htab)
12736 || h->def_regular))
12737 {
12738 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12739 + h->root.u.def.section->output_offset
12740 + h->root.u.def.section->output_section->vma);
12741 _bfd_set_gp_value (obfd, TOCstart);
12742 return TOCstart;
12743 }
12744 }
12745
805fc799
AM
12746 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12747 order. The TOC starts where the first of these sections starts. */
12748 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12749 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12750 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12751 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12752 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12753 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12754 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12755 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12756 {
12757 /* This may happen for
12758 o references to TOC base (SYM@toc / TOC[tc0]) without a
12759 .toc directive
12760 o bad linker script
12761 o --gc-sections and empty TOC sections
12762
12763 FIXME: Warn user? */
12764
12765 /* Look for a likely section. We probably won't even be
12766 using TOCstart. */
12767 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12768 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12769 | SEC_EXCLUDE))
805fc799
AM
12770 == (SEC_ALLOC | SEC_SMALL_DATA))
12771 break;
721956f4 12772 if (s == NULL)
805fc799 12773 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12774 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12775 == (SEC_ALLOC | SEC_SMALL_DATA))
12776 break;
721956f4 12777 if (s == NULL)
805fc799 12778 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12779 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12780 == SEC_ALLOC)
805fc799 12781 break;
721956f4 12782 if (s == NULL)
805fc799 12783 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12784 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12785 break;
12786 }
721956f4 12787
805fc799
AM
12788 TOCstart = 0;
12789 if (s != NULL)
12790 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12791
a27e685f
AM
12792 /* Force alignment. */
12793 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12794 TOCstart -= adjust;
1c865ab2
AM
12795 _bfd_set_gp_value (obfd, TOCstart);
12796
810d4e75 12797 if (info != NULL && s != NULL)
1c865ab2
AM
12798 {
12799 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12800
810d4e75
AM
12801 if (htab != NULL)
12802 {
12803 if (htab->elf.hgot != NULL)
12804 {
a27e685f 12805 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
12806 htab->elf.hgot->root.u.def.section = s;
12807 }
12808 }
12809 else
1c865ab2 12810 {
810d4e75
AM
12811 struct bfd_link_hash_entry *bh = NULL;
12812 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
12813 s, TOC_BASE_OFF - adjust,
12814 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
12815 }
12816 }
805fc799 12817 return TOCstart;
721956f4
AM
12818}
12819
a345bc8d 12820/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 12821 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
12822
12823static bfd_boolean
49c09209 12824build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
12825{
12826 struct bfd_link_info *info;
12827 struct ppc_link_hash_table *htab;
49c09209 12828 struct plt_entry *ent;
a345bc8d
AM
12829 asection *s;
12830
12831 if (h->root.type == bfd_link_hash_indirect)
12832 return TRUE;
12833
49c09209
AM
12834 info = inf;
12835 htab = ppc_hash_table (info);
12836 if (htab == NULL)
12837 return FALSE;
12838
12839 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12840 if (ent->plt.offset != (bfd_vma) -1)
12841 {
12842 /* This symbol has an entry in the procedure linkage
12843 table. Set it up. */
12844 Elf_Internal_Rela rela;
2d7ad24e 12845 asection *plt, *relplt;
49c09209
AM
12846 bfd_byte *loc;
12847
12848 if (!htab->elf.dynamic_sections_created
12849 || h->dynindx == -1)
12850 {
12851 if (!(h->def_regular
12852 && (h->root.type == bfd_link_hash_defined
12853 || h->root.type == bfd_link_hash_defweak)))
12854 continue;
2d7ad24e
AM
12855 if (h->type == STT_GNU_IFUNC)
12856 {
12857 plt = htab->elf.iplt;
12858 relplt = htab->elf.irelplt;
12859 htab->local_ifunc_resolver = 1;
12860 if (htab->opd_abi)
12861 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
12862 else
12863 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12864 }
49c09209 12865 else
2d7ad24e
AM
12866 {
12867 plt = htab->pltlocal;
12868 if (bfd_link_pic (info))
12869 {
12870 relplt = htab->relpltlocal;
12871 if (htab->opd_abi)
12872 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
12873 else
12874 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12875 }
12876 else
12877 relplt = NULL;
12878 }
49c09209
AM
12879 rela.r_addend = (h->root.u.def.value
12880 + h->root.u.def.section->output_offset
12881 + h->root.u.def.section->output_section->vma
12882 + ent->addend);
2d7ad24e
AM
12883
12884 if (relplt == NULL)
12885 {
12886 loc = plt->contents + ent->plt.offset;
12887 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
12888 if (htab->opd_abi)
12889 {
12890 bfd_vma toc = elf_gp (info->output_bfd);
12891 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
12892 bfd_put_64 (info->output_bfd, toc, loc + 8);
12893 }
12894 }
12895 else
12896 {
12897 rela.r_offset = (plt->output_section->vma
12898 + plt->output_offset
12899 + ent->plt.offset);
12900 loc = relplt->contents + (relplt->reloc_count++
12901 * sizeof (Elf64_External_Rela));
12902 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12903 }
49c09209
AM
12904 }
12905 else
12906 {
12907 rela.r_offset = (htab->elf.splt->output_section->vma
12908 + htab->elf.splt->output_offset
12909 + ent->plt.offset);
12910 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
12911 rela.r_addend = ent->addend;
12912 loc = (htab->elf.srelplt->contents
12913 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
12914 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
12915 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
12916 htab->maybe_local_ifunc_resolver = 1;
2d7ad24e 12917 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 12918 }
49c09209
AM
12919 }
12920
a345bc8d
AM
12921 if (!h->pointer_equality_needed)
12922 return TRUE;
12923
12924 if (h->def_regular)
12925 return TRUE;
12926
9e390558 12927 s = htab->global_entry;
49c09209
AM
12928 if (s == NULL || s->size == 0)
12929 return TRUE;
12930
12931 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12932 if (ent->plt.offset != (bfd_vma) -1
12933 && ent->addend == 0)
a345bc8d
AM
12934 {
12935 bfd_byte *p;
12936 asection *plt;
12937 bfd_vma off;
12938
a345bc8d 12939 p = s->contents + h->root.u.def.value;
33e44f2e 12940 plt = htab->elf.splt;
a345bc8d
AM
12941 if (!htab->elf.dynamic_sections_created
12942 || h->dynindx == -1)
2d7ad24e
AM
12943 {
12944 if (h->type == STT_GNU_IFUNC)
12945 plt = htab->elf.iplt;
12946 else
12947 plt = htab->pltlocal;
12948 }
49c09209 12949 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
12950 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12951
12952 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12953 {
12954 info->callbacks->einfo
c1c8c1ef 12955 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
12956 h->root.root.string);
12957 bfd_set_error (bfd_error_bad_value);
12958 htab->stub_error = TRUE;
12959 }
12960
7341d5e2
AM
12961 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12962 if (htab->params->emit_stub_syms)
12963 {
12964 size_t len = strlen (h->root.root.string);
12965 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12966
12967 if (name == NULL)
12968 return FALSE;
12969
12970 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12971 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12972 if (h == NULL)
12973 return FALSE;
12974 if (h->root.type == bfd_link_hash_new)
12975 {
12976 h->root.type = bfd_link_hash_defined;
12977 h->root.u.def.section = s;
12978 h->root.u.def.value = p - s->contents;
12979 h->ref_regular = 1;
12980 h->def_regular = 1;
12981 h->ref_regular_nonweak = 1;
12982 h->forced_local = 1;
12983 h->non_elf = 0;
2ec55de3 12984 h->root.linker_def = 1;
7341d5e2
AM
12985 }
12986 }
12987
a345bc8d
AM
12988 if (PPC_HA (off) != 0)
12989 {
12990 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12991 p += 4;
12992 }
12993 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12994 p += 4;
12995 bfd_put_32 (s->owner, MTCTR_R12, p);
12996 p += 4;
407aa07c 12997 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
12998 break;
12999 }
13000 return TRUE;
13001}
13002
49c09209
AM
13003/* Write PLT relocs for locals. */
13004
13005static bfd_boolean
13006write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13007{
13008 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13009 bfd *ibfd;
13010
13011 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13012 {
13013 struct got_entry **lgot_ents, **end_lgot_ents;
13014 struct plt_entry **local_plt, **lplt, **end_local_plt;
13015 Elf_Internal_Shdr *symtab_hdr;
13016 bfd_size_type locsymcount;
13017 Elf_Internal_Sym *local_syms = NULL;
13018 struct plt_entry *ent;
13019
13020 if (!is_ppc64_elf (ibfd))
13021 continue;
13022
13023 lgot_ents = elf_local_got_ents (ibfd);
13024 if (!lgot_ents)
13025 continue;
13026
13027 symtab_hdr = &elf_symtab_hdr (ibfd);
13028 locsymcount = symtab_hdr->sh_info;
13029 end_lgot_ents = lgot_ents + locsymcount;
13030 local_plt = (struct plt_entry **) end_lgot_ents;
13031 end_local_plt = local_plt + locsymcount;
13032 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13033 for (ent = *lplt; ent != NULL; ent = ent->next)
13034 if (ent->plt.offset != (bfd_vma) -1)
13035 {
13036 Elf_Internal_Sym *sym;
13037 asection *sym_sec;
13038 asection *plt, *relplt;
13039 bfd_byte *loc;
13040 bfd_vma val;
49c09209
AM
13041
13042 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13043 lplt - local_plt, ibfd))
13044 {
13045 if (local_syms != NULL
13046 && symtab_hdr->contents != (unsigned char *) local_syms)
13047 free (local_syms);
13048 return FALSE;
13049 }
13050
13051 val = sym->st_value + ent->addend;
ba85c15d
AM
13052 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13053 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
49c09209
AM
13054 if (sym_sec != NULL && sym_sec->output_section != NULL)
13055 val += sym_sec->output_offset + sym_sec->output_section->vma;
13056
2d7ad24e
AM
13057 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13058 {
13059 htab->local_ifunc_resolver = 1;
13060 plt = htab->elf.iplt;
13061 relplt = htab->elf.irelplt;
13062 }
13063 else
13064 {
13065 plt = htab->pltlocal;
13066 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13067 }
49c09209 13068
2d7ad24e
AM
13069 if (relplt == NULL)
13070 {
13071 loc = plt->contents + ent->plt.offset;
13072 bfd_put_64 (info->output_bfd, val, loc);
13073 if (htab->opd_abi)
13074 {
13075 bfd_vma toc = elf_gp (ibfd);
13076 bfd_put_64 (info->output_bfd, toc, loc + 8);
13077 }
13078 }
49c09209 13079 else
2d7ad24e
AM
13080 {
13081 Elf_Internal_Rela rela;
13082 rela.r_offset = (ent->plt.offset
13083 + plt->output_offset
13084 + plt->output_section->vma);
13085 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13086 {
13087 if (htab->opd_abi)
13088 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13089 else
13090 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13091 }
13092 else
13093 {
13094 if (htab->opd_abi)
13095 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13096 else
13097 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13098 }
13099 rela.r_addend = val;
13100 loc = relplt->contents + (relplt->reloc_count++
13101 * sizeof (Elf64_External_Rela));
13102 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13103 }
49c09209
AM
13104 }
13105
13106 if (local_syms != NULL
13107 && symtab_hdr->contents != (unsigned char *) local_syms)
13108 {
13109 if (!info->keep_memory)
13110 free (local_syms);
13111 else
13112 symtab_hdr->contents = (unsigned char *) local_syms;
13113 }
13114 }
13115 return TRUE;
13116}
13117
721956f4
AM
13118/* Build all the stubs associated with the current output file.
13119 The stubs are kept in a hash table attached to the main linker
13120 hash table. This function is called via gldelf64ppc_finish. */
13121
b34976b6 13122bfd_boolean
e7d1c40c 13123ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13124 char **stats)
5d1634d7
AM
13125{
13126 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13127 struct map_stub *group;
721956f4 13128 asection *stub_sec;
5d1634d7 13129 bfd_byte *p;
e717da7e 13130 int stub_sec_count = 0;
5d1634d7 13131
4dfe6ac6
NC
13132 if (htab == NULL)
13133 return FALSE;
13134
eea6121a 13135 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 13136 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13137 {
13138 group->eh_size = 0;
13139 group->lr_restore = 0;
13140 if ((stub_sec = group->stub_sec) != NULL
13141 && stub_sec->size != 0)
13142 {
13143 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13144 stub_sec->size);
13145 if (stub_sec->contents == NULL)
13146 return FALSE;
13147 stub_sec->size = 0;
13148 }
13149 }
5d1634d7 13150
23eb7e01 13151 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13152 {
9f951329 13153 unsigned int indx;
ad8e1ba5 13154 bfd_vma plt0;
9f951329 13155
721956f4 13156 /* Build the .glink plt call stub. */
e7d1c40c 13157 if (htab->params->emit_stub_syms)
97b639ba
AM
13158 {
13159 struct elf_link_hash_entry *h;
468392fb
AM
13160 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13161 TRUE, FALSE, FALSE);
97b639ba
AM
13162 if (h == NULL)
13163 return FALSE;
13164 if (h->root.type == bfd_link_hash_new)
13165 {
13166 h->root.type = bfd_link_hash_defined;
13167 h->root.u.def.section = htab->glink;
ee4bf8d2 13168 h->root.u.def.value = 8;
f5385ebf
AM
13169 h->ref_regular = 1;
13170 h->def_regular = 1;
13171 h->ref_regular_nonweak = 1;
13172 h->forced_local = 1;
13173 h->non_elf = 0;
2ec55de3 13174 h->root.linker_def = 1;
97b639ba
AM
13175 }
13176 }
33e44f2e
AM
13177 plt0 = (htab->elf.splt->output_section->vma
13178 + htab->elf.splt->output_offset
13179 - 16);
176a0d42
AM
13180 if (info->emitrelocations)
13181 {
13182 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13183 if (r == NULL)
13184 return FALSE;
13185 r->r_offset = (htab->glink->output_offset
13186 + htab->glink->output_section->vma);
13187 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13188 r->r_addend = plt0;
13189 }
4ce794b7 13190 p = htab->glink->contents;
176a0d42 13191 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13192 bfd_put_64 (htab->glink->owner, plt0, p);
13193 p += 8;
b9e5796b
AM
13194 if (htab->opd_abi)
13195 {
13196 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13197 p += 4;
13198 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13199 p += 4;
13200 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13201 p += 4;
13202 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13203 p += 4;
13204 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13205 p += 4;
13206 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13207 p += 4;
13208 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13209 p += 4;
13210 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13211 p += 4;
13212 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13213 p += 4;
13214 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13215 p += 4;
13216 }
13217 else
13218 {
13219 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13220 p += 4;
13221 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13222 p += 4;
13223 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13224 p += 4;
f378ab09
AM
13225 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13226 p += 4;
b9e5796b
AM
13227 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13228 p += 4;
13229 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13230 p += 4;
13231 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13232 p += 4;
13233 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13234 p += 4;
13235 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13236 p += 4;
13237 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13238 p += 4;
13239 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13240 p += 4;
13241 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13242 p += 4;
13243 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13244 p += 4;
13245 }
407aa07c
AM
13246 bfd_put_32 (htab->glink->owner, BCTR, p);
13247 p += 4;
c75bc4f7 13248 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 13249
9f951329
AM
13250 /* Build the .glink lazy link call stubs. */
13251 indx = 0;
9e390558 13252 while (p < htab->glink->contents + htab->glink->size)
9f951329 13253 {
b9e5796b 13254 if (htab->opd_abi)
9f951329 13255 {
b9e5796b
AM
13256 if (indx < 0x8000)
13257 {
13258 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13259 p += 4;
13260 }
13261 else
13262 {
13263 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13264 p += 4;
13265 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13266 p);
13267 p += 4;
13268 }
9f951329 13269 }
4ce794b7 13270 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13271 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13272 indx++;
9f951329
AM
13273 p += 4;
13274 }
5d1634d7 13275 }
5d1634d7 13276
49c09209
AM
13277 /* Build .glink global entry stubs, and PLT relocs for globals. */
13278 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13279
13280 if (!write_plt_relocs_for_local_syms (info))
13281 return FALSE;
9e390558 13282
7341d5e2 13283 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13284 {
4ce794b7 13285 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13286 htab->brlt->size);
4ce794b7 13287 if (htab->brlt->contents == NULL)
b34976b6 13288 return FALSE;
721956f4 13289 }
ee75fd95 13290 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13291 {
13292 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13293 htab->relbrlt->size);
63bc6f6c
AM
13294 if (htab->relbrlt->contents == NULL)
13295 return FALSE;
13296 }
5d1634d7 13297
721956f4
AM
13298 /* Build the stubs as directed by the stub hash table. */
13299 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13300
a4b6fadd
AM
13301 for (group = htab->group; group != NULL; group = group->next)
13302 if (group->needs_save_res)
7dda8d3c 13303 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 13304
aa8a7074
AM
13305 if (htab->relbrlt != NULL)
13306 htab->relbrlt->reloc_count = 0;
13307
e7d1c40c 13308 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13309 for (group = htab->group; group != NULL; group = group->next)
13310 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
13311 {
13312 int align = abs (htab->params->plt_stub_align);
13313 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13314 }
794e51c0 13315
7dda8d3c
AM
13316 for (group = htab->group; group != NULL; group = group->next)
13317 if (group->needs_save_res)
13318 {
13319 stub_sec = group->stub_sec;
13320 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13321 htab->sfpr->contents, htab->sfpr->size);
13322 if (htab->params->emit_stub_syms)
13323 {
13324 unsigned int i;
13325
13326 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13327 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13328 return FALSE;
13329 }
13330 }
13331
df136d64
AM
13332 if (htab->glink_eh_frame != NULL
13333 && htab->glink_eh_frame->size != 0)
13334 {
13335 bfd_vma val;
13336 size_t align = 4;
13337
13338 p = htab->glink_eh_frame->contents;
13339 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13340
13341 for (group = htab->group; group != NULL; group = group->next)
13342 if (group->eh_size != 0)
13343 {
13344 /* Offset to stub section. */
13345 val = (group->stub_sec->output_section->vma
13346 + group->stub_sec->output_offset);
13347 val -= (htab->glink_eh_frame->output_section->vma
13348 + htab->glink_eh_frame->output_offset
13349 + (p + 8 - htab->glink_eh_frame->contents));
13350 if (val + 0x80000000 > 0xffffffff)
13351 {
13352 _bfd_error_handler
13353 (_("%s offset too large for .eh_frame sdata4 encoding"),
13354 group->stub_sec->name);
13355 return FALSE;
13356 }
13357 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13358 p += (group->eh_size + 17 + 3) & -4;
13359 }
13360 if (htab->glink != NULL && htab->glink->size != 0)
13361 {
13362 /* Offset to .glink. */
13363 val = (htab->glink->output_section->vma
13364 + htab->glink->output_offset
13365 + 8);
13366 val -= (htab->glink_eh_frame->output_section->vma
13367 + htab->glink_eh_frame->output_offset
13368 + (p + 8 - htab->glink_eh_frame->contents));
13369 if (val + 0x80000000 > 0xffffffff)
13370 {
13371 _bfd_error_handler
13372 (_("%s offset too large for .eh_frame sdata4 encoding"),
13373 htab->glink->name);
13374 return FALSE;
13375 }
13376 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13377 p += (24 + align - 1) & -align;
13378 }
13379 }
13380
d4aaa2a0
AM
13381 for (group = htab->group; group != NULL; group = group->next)
13382 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
13383 {
13384 stub_sec_count += 1;
c9301e31
AM
13385 if (stub_sec->rawsize != stub_sec->size
13386 && (htab->stub_iteration <= STUB_SHRINK_ITER
13387 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13388 break;
13389 }
5d1634d7 13390
25516cc5 13391 if (group != NULL)
5d1634d7 13392 {
b34976b6 13393 htab->stub_error = TRUE;
cf97bcb0 13394 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 13395 }
721956f4 13396
d2a300cf
AM
13397 if (htab->stub_error)
13398 return FALSE;
13399
13400 if (stats != NULL)
13401 {
db84fff3 13402 size_t len;
d2a300cf
AM
13403 *stats = bfd_malloc (500);
13404 if (*stats == NULL)
13405 return FALSE;
13406
db84fff3
AM
13407 len = sprintf (*stats,
13408 ngettext ("linker stubs in %u group\n",
13409 "linker stubs in %u groups\n",
13410 stub_sec_count),
13411 stub_sec_count);
05d0e962
AM
13412 sprintf (*stats + len, _(" branch %lu\n"
13413 " branch toc adj %lu\n"
13414 " branch notoc %lu\n"
13415 " branch both %lu\n"
13416 " long branch %lu\n"
13417 " long toc adj %lu\n"
13418 " long notoc %lu\n"
13419 " long both %lu\n"
13420 " plt call %lu\n"
13421 " plt call save %lu\n"
13422 " plt call notoc %lu\n"
13423 " plt call both %lu\n"
13424 " global entry %lu"),
4ce794b7
AM
13425 htab->stub_count[ppc_stub_long_branch - 1],
13426 htab->stub_count[ppc_stub_long_branch_r2off - 1],
05d0e962
AM
13427 htab->stub_count[ppc_stub_long_branch_notoc - 1],
13428 htab->stub_count[ppc_stub_long_branch_both - 1],
4ce794b7
AM
13429 htab->stub_count[ppc_stub_plt_branch - 1],
13430 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
05d0e962
AM
13431 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
13432 htab->stub_count[ppc_stub_plt_branch_both - 1],
794e51c0 13433 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2 13434 htab->stub_count[ppc_stub_plt_call_r2save - 1],
05d0e962
AM
13435 htab->stub_count[ppc_stub_plt_call_notoc - 1],
13436 htab->stub_count[ppc_stub_plt_call_both - 1],
7341d5e2 13437 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13438 }
13439 return TRUE;
5bd4f169
AM
13440}
13441
60124e18
AM
13442/* What to do when ld finds relocations against symbols defined in
13443 discarded sections. */
13444
13445static unsigned int
13446ppc64_elf_action_discarded (asection *sec)
13447{
13448 if (strcmp (".opd", sec->name) == 0)
13449 return 0;
13450
13451 if (strcmp (".toc", sec->name) == 0)
13452 return 0;
13453
bce50a28
JJ
13454 if (strcmp (".toc1", sec->name) == 0)
13455 return 0;
13456
60124e18
AM
13457 return _bfd_elf_default_action_discarded (sec);
13458}
13459
5bd4f169
AM
13460/* The RELOCATE_SECTION function is called by the ELF backend linker
13461 to handle the relocations for a section.
13462
13463 The relocs are always passed as Rela structures; if the section
13464 actually uses Rel structures, the r_addend field will always be
13465 zero.
13466
13467 This function is responsible for adjust the section contents as
13468 necessary, and (if using Rela relocs and generating a
1049f94e 13469 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13470 necessary.
13471
13472 This function does not have to worry about setting the reloc
13473 address or the reloc symbol index.
13474
13475 LOCAL_SYMS is a pointer to the swapped in local symbols.
13476
13477 LOCAL_SECTIONS is an array giving the section in the input file
13478 corresponding to the st_shndx field of each local symbol.
13479
13480 The global hash table entry for the global symbols can be found
13481 via elf_sym_hashes (input_bfd).
13482
1049f94e 13483 When generating relocatable output, this function must handle
5bd4f169
AM
13484 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13485 going to be the section symbol corresponding to the output
13486 section, which means that the addend must be adjusted
13487 accordingly. */
13488
b34976b6 13489static bfd_boolean
4ce794b7
AM
13490ppc64_elf_relocate_section (bfd *output_bfd,
13491 struct bfd_link_info *info,
13492 bfd *input_bfd,
13493 asection *input_section,
13494 bfd_byte *contents,
13495 Elf_Internal_Rela *relocs,
13496 Elf_Internal_Sym *local_syms,
13497 asection **local_sections)
5bd4f169 13498{
65f38f15 13499 struct ppc_link_hash_table *htab;
5bd4f169
AM
13500 Elf_Internal_Shdr *symtab_hdr;
13501 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13502 Elf_Internal_Rela *rel;
c316a17c 13503 Elf_Internal_Rela *wrel;
5bd4f169 13504 Elf_Internal_Rela *relend;
411e1bfb
AM
13505 Elf_Internal_Rela outrel;
13506 bfd_byte *loc;
411e1bfb 13507 struct got_entry **local_got_ents;
5bd4f169 13508 bfd_vma TOCstart;
b34976b6
AM
13509 bfd_boolean ret = TRUE;
13510 bfd_boolean is_opd;
794e51c0
AM
13511 /* Assume 'at' branch hints. */
13512 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 13513 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 13514
65f38f15 13515 /* Initialize howto table if needed. */
5bd4f169 13516 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13517 ppc_howto_init ();
13518
65f38f15 13519 htab = ppc_hash_table (info);
4dfe6ac6
NC
13520 if (htab == NULL)
13521 return FALSE;
ee75fd95
AM
13522
13523 /* Don't relocate stub sections. */
e7d1c40c 13524 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13525 return TRUE;
13526
7af5d5c4
AM
13527 if (!is_ppc64_elf (input_bfd))
13528 {
13529 bfd_set_error (bfd_error_wrong_format);
13530 return FALSE;
13531 }
0ffa91dd 13532
411e1bfb 13533 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13534 TOCstart = elf_gp (output_bfd);
0ffa91dd 13535 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13536 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13537 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13538
c316a17c 13539 rel = wrel = relocs;
5bd4f169 13540 relend = relocs + input_section->reloc_count;
c316a17c 13541 for (; rel < relend; wrel++, rel++)
5bd4f169 13542 {
04c9666a 13543 enum elf_ppc64_reloc_type r_type;
31c76678 13544 bfd_vma addend;
5bd4f169
AM
13545 bfd_reloc_status_type r;
13546 Elf_Internal_Sym *sym;
13547 asection *sec;
039b3fef
AM
13548 struct elf_link_hash_entry *h_elf;
13549 struct ppc_link_hash_entry *h;
13550 struct ppc_link_hash_entry *fdh;
5bd4f169 13551 const char *sym_name;
0d4792f7 13552 unsigned long r_symndx, toc_symndx;
3a71aa26 13553 bfd_vma toc_addend;
f961d9dd
AM
13554 unsigned char tls_mask, tls_gd, tls_type;
13555 unsigned char sym_type;
5bd4f169 13556 bfd_vma relocation;
23cedd1d 13557 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 13558 bfd_boolean warned;
bc30df16 13559 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13560 unsigned int insn;
e11840f9 13561 unsigned int mask;
721956f4
AM
13562 struct ppc_stub_hash_entry *stub_entry;
13563 bfd_vma max_br_offset;
13564 bfd_vma from;
c316a17c 13565 Elf_Internal_Rela orig_rel;
b80eed39
AM
13566 reloc_howto_type *howto;
13567 struct reloc_howto_struct alt_howto;
5bd4f169 13568
c316a17c
AM
13569 again:
13570 orig_rel = *rel;
13571
4ce794b7 13572 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13573 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13574
13575 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13576 symbol of the previous ADDR64 reloc. The symbol gives us the
13577 proper TOC base to use. */
13578 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13579 && wrel != relocs
13580 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13581 && is_opd)
c316a17c 13582 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13583
4ce794b7
AM
13584 sym = NULL;
13585 sec = NULL;
039b3fef 13586 h_elf = NULL;
4ce794b7 13587 sym_name = NULL;
b34976b6
AM
13588 unresolved_reloc = FALSE;
13589 warned = FALSE;
65f38f15 13590
0b13192e 13591 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13592 {
13593 /* It's a local symbol. */
74f0fb50 13594 struct _opd_sec_data *opd;
4025353c 13595
5bd4f169
AM
13596 sym = local_syms + r_symndx;
13597 sec = local_sections[r_symndx];
26c61ae5 13598 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13599 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13600 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13601 opd = get_opd_info (sec);
13602 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13603 {
51aecdc5
AM
13604 long adjust = opd->adjust[OPD_NDX (sym->st_value
13605 + rel->r_addend)];
4025353c
AM
13606 if (adjust == -1)
13607 relocation = 0;
13608 else
4cc603a5
AM
13609 {
13610 /* If this is a relocation against the opd section sym
13611 and we have edited .opd, adjust the reloc addend so
13612 that ld -r and ld --emit-relocs output is correct.
13613 If it is a reloc against some other .opd symbol,
13614 then the symbol value will be adjusted later. */
13615 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13616 rel->r_addend += adjust;
13617 else
13618 relocation += adjust;
13619 }
1e2f5b6e 13620 }
5bd4f169
AM
13621 }
13622 else
13623 {
62d887d4
L
13624 bfd_boolean ignored;
13625
b2a8e766
AM
13626 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13627 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13628 h_elf, sec, relocation,
62d887d4 13629 unresolved_reloc, warned, ignored);
039b3fef
AM
13630 sym_name = h_elf->root.root.string;
13631 sym_type = h_elf->type;
b69fdb4e
AM
13632 if (sec != NULL
13633 && sec->owner == output_bfd
13634 && strcmp (sec->name, ".opd") == 0)
13635 {
13636 /* This is a symbol defined in a linker script. All
13637 such are defined in output sections, even those
13638 defined by simple assignment from a symbol defined in
13639 an input section. Transfer the symbol to an
13640 appropriate input .opd section, so that a branch to
13641 this symbol will be mapped to the location specified
13642 by the opd entry. */
13643 struct bfd_link_order *lo;
13644 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13645 if (lo->type == bfd_indirect_link_order)
13646 {
13647 asection *isec = lo->u.indirect.section;
13648 if (h_elf->root.u.def.value >= isec->output_offset
13649 && h_elf->root.u.def.value < (isec->output_offset
13650 + isec->size))
13651 {
13652 h_elf->root.u.def.value -= isec->output_offset;
13653 h_elf->root.u.def.section = isec;
13654 sec = isec;
13655 break;
13656 }
13657 }
13658 }
5bd4f169 13659 }
039b3fef 13660 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13661
dbaa2011 13662 if (sec != NULL && discarded_section (sec))
c316a17c
AM
13663 {
13664 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13665 input_bfd, input_section,
0930cb30 13666 contents, rel->r_offset);
c316a17c
AM
13667 wrel->r_offset = rel->r_offset;
13668 wrel->r_info = 0;
13669 wrel->r_addend = 0;
13670
13671 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 13672 symbols defined in discarded sections. Not done for
c316a17c
AM
13673 non-debug to preserve relocs in .eh_frame which the
13674 eh_frame editing code expects to be present. */
13675 if (bfd_link_relocatable (info)
13676 && (input_section->flags & SEC_DEBUGGING))
13677 wrel--;
13678
13679 continue;
13680 }
ab96bf03 13681
0e1862bb 13682 if (bfd_link_relocatable (info))
c316a17c 13683 goto copy_reloc;
ab96bf03 13684
f40da81b
AM
13685 if (h != NULL && &h->elf == htab->elf.hgot)
13686 {
6f20ed8a 13687 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
13688 sec = bfd_abs_section_ptr;
13689 unresolved_reloc = FALSE;
13690 }
13691
951fd09b
AM
13692 /* TLS optimizations. Replace instruction sequences and relocs
13693 based on information we collected in tls_optimize. We edit
13694 RELOCS so that --emit-relocs will output something sensible
13695 for the final instruction stream. */
13696 tls_mask = 0;
13697 tls_gd = 0;
0d4792f7 13698 toc_symndx = 0;
727fc41e
AM
13699 if (h != NULL)
13700 tls_mask = h->tls_mask;
13701 else if (local_got_ents != NULL)
411e1bfb 13702 {
e054468f
AM
13703 struct plt_entry **local_plt = (struct plt_entry **)
13704 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 13705 unsigned char *lgot_masks = (unsigned char *)
e054468f 13706 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
13707 tls_mask = lgot_masks[r_symndx];
13708 }
37da22e5 13709 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
13710 && (r_type == R_PPC64_TLS
13711 || r_type == R_PPC64_TLSGD
13712 || r_type == R_PPC64_TLSLD))
13713 {
13714 /* Check for toc tls entries. */
f961d9dd 13715 unsigned char *toc_tls;
0d4792f7 13716
727fc41e
AM
13717 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13718 &local_syms, rel, input_bfd))
13719 return FALSE;
0d4792f7 13720
727fc41e
AM
13721 if (toc_tls)
13722 tls_mask = *toc_tls;
0d4792f7
AM
13723 }
13724
13725 /* Check that tls relocs are used with tls syms, and non-tls
13726 relocs are used with non-tls syms. */
cf35638d 13727 if (r_symndx != STN_UNDEF
0d4792f7
AM
13728 && r_type != R_PPC64_NONE
13729 && (h == NULL
039b3fef
AM
13730 || h->elf.root.type == bfd_link_hash_defined
13731 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
13732 && (IS_PPC64_TLS_RELOC (r_type)
13733 != (sym_type == STT_TLS
13734 || (sym_type == STT_SECTION
13735 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 13736 {
37da22e5 13737 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
13738 && (r_type == R_PPC64_TLS
13739 || r_type == R_PPC64_TLSGD
13740 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
13741 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13742 ;
13743 else
25f53a85 13744 info->callbacks->einfo
1d483afe 13745 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 13746 /* xgettext:c-format */
c1c8c1ef 13747 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 13748 /* xgettext:c-format */
c1c8c1ef 13749 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 13750 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
13751 ppc64_elf_howto_table[r_type]->name,
13752 sym_name);
411e1bfb
AM
13753 }
13754
13755 /* Ensure reloc mapping code below stays sane. */
13756 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13757 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13758 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13759 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13760 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13761 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13762 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13763 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13764 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13765 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13766 abort ();
0d4792f7 13767
411e1bfb
AM
13768 switch (r_type)
13769 {
13770 default:
411e1bfb
AM
13771 break;
13772
ba761f19 13773 case R_PPC64_LO_DS_OPT:
95f0d0d2 13774 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
13775 if ((insn & (0x3f << 26)) != 58u << 26)
13776 abort ();
13777 insn += (14u << 26) - (58u << 26);
95f0d0d2 13778 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
13779 r_type = R_PPC64_TOC16_LO;
13780 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13781 break;
13782
411e1bfb
AM
13783 case R_PPC64_TOC16:
13784 case R_PPC64_TOC16_LO:
13785 case R_PPC64_TOC16_DS:
13786 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
13787 {
13788 /* Check for toc tls entries. */
f961d9dd 13789 unsigned char *toc_tls;
951fd09b 13790 int retval;
411e1bfb 13791
3a71aa26
AM
13792 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13793 &local_syms, rel, input_bfd);
951fd09b 13794 if (retval == 0)
411e1bfb
AM
13795 return FALSE;
13796
13797 if (toc_tls)
13798 {
951fd09b 13799 tls_mask = *toc_tls;
411e1bfb
AM
13800 if (r_type == R_PPC64_TOC16_DS
13801 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 13802 {
37da22e5 13803 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
13804 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13805 goto toctprel;
13806 }
411e1bfb 13807 else
951fd09b
AM
13808 {
13809 /* If we found a GD reloc pair, then we might be
13810 doing a GD->IE transition. */
13811 if (retval == 2)
13812 {
13813 tls_gd = TLS_TPRELGD;
37da22e5
AM
13814 if ((tls_mask & TLS_TLS) != 0
13815 && (tls_mask & TLS_GD) == 0)
102890f0 13816 goto tls_ldgd_opt;
951fd09b
AM
13817 }
13818 else if (retval == 3)
13819 {
37da22e5
AM
13820 if ((tls_mask & TLS_TLS) != 0
13821 && (tls_mask & TLS_LD) == 0)
102890f0 13822 goto tls_ldgd_opt;
951fd09b
AM
13823 }
13824 }
411e1bfb
AM
13825 }
13826 }
13827 break;
13828
9d6ded02
AM
13829 case R_PPC64_GOT_TPREL16_HI:
13830 case R_PPC64_GOT_TPREL16_HA:
37da22e5 13831 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
13832 && (tls_mask & TLS_TPREL) == 0)
13833 {
13834 rel->r_offset -= d_offset;
95f0d0d2 13835 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
13836 r_type = R_PPC64_NONE;
13837 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13838 }
13839 break;
13840
411e1bfb
AM
13841 case R_PPC64_GOT_TPREL16_DS:
13842 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 13843 if ((tls_mask & TLS_TLS) != 0
951fd09b 13844 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13845 {
81407a69 13846 toctprel:
95f0d0d2 13847 insn = bfd_get_32 (input_bfd,
c316a17c 13848 contents + rel->r_offset - d_offset);
411e1bfb
AM
13849 insn &= 31 << 21;
13850 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 13851 bfd_put_32 (input_bfd, insn,
c316a17c 13852 contents + rel->r_offset - d_offset);
411e1bfb 13853 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13854 if (toc_symndx != 0)
13855 {
13856 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13857 rel->r_addend = toc_addend;
0d4792f7
AM
13858 /* We changed the symbol. Start over in order to
13859 get h, sym, sec etc. right. */
c316a17c 13860 goto again;
0d4792f7
AM
13861 }
13862 else
13863 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13864 }
13865 break;
13866
13867 case R_PPC64_TLS:
37da22e5 13868 if ((tls_mask & TLS_TLS) != 0
951fd09b 13869 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13870 {
95f0d0d2 13871 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
13872 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13873 if (insn == 0)
411e1bfb 13874 abort ();
95f0d0d2 13875 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 13876 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13877 PPC64_TPREL16_LO which is at low-order half-word. */
13878 rel->r_offset += d_offset;
0d4792f7
AM
13879 r_type = R_PPC64_TPREL16_LO;
13880 if (toc_symndx != 0)
13881 {
13882 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13883 rel->r_addend = toc_addend;
0d4792f7
AM
13884 /* We changed the symbol. Start over in order to
13885 get h, sym, sec etc. right. */
c316a17c 13886 goto again;
0d4792f7
AM
13887 }
13888 else
13889 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13890 }
13891 break;
13892
411e1bfb
AM
13893 case R_PPC64_GOT_TLSGD16_HI:
13894 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 13895 tls_gd = TLS_TPRELGD;
37da22e5 13896 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
13897 goto tls_gdld_hi;
13898 break;
13899
411e1bfb
AM
13900 case R_PPC64_GOT_TLSLD16_HI:
13901 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 13902 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13903 {
951fd09b
AM
13904 tls_gdld_hi:
13905 if ((tls_mask & tls_gd) != 0)
13906 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13907 + R_PPC64_GOT_TPREL16_DS);
13908 else
411e1bfb 13909 {
4fe5ca5b 13910 rel->r_offset -= d_offset;
95f0d0d2 13911 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 13912 r_type = R_PPC64_NONE;
411e1bfb 13913 }
951fd09b 13914 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13915 }
13916 break;
13917
951fd09b
AM
13918 case R_PPC64_GOT_TLSGD16:
13919 case R_PPC64_GOT_TLSGD16_LO:
13920 tls_gd = TLS_TPRELGD;
37da22e5 13921 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13922 goto tls_ldgd_opt;
951fd09b 13923 break;
411e1bfb 13924
951fd09b
AM
13925 case R_PPC64_GOT_TLSLD16:
13926 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 13927 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 13928 {
b9f04fe0 13929 unsigned int insn1, insn2;
102890f0
AM
13930 bfd_vma offset;
13931
13932 tls_ldgd_opt:
727fc41e
AM
13933 offset = (bfd_vma) -1;
13934 /* If not using the newer R_PPC64_TLSGD/LD to mark
13935 __tls_get_addr calls, we must trust that the call
13936 stays with its arg setup insns, ie. that the next
13937 reloc is the __tls_get_addr call associated with
13938 the current reloc. Edit both insns. */
13939 if (input_section->has_tls_get_addr_call
13940 && rel + 1 < relend
13941 && branch_reloc_hash_match (input_bfd, rel + 1,
13942 htab->tls_get_addr,
13943 htab->tls_get_addr_fd))
13944 offset = rel[1].r_offset;
b86ac8e3
AM
13945 /* We read the low GOT_TLS (or TOC16) insn because we
13946 need to keep the destination reg. It may be
13947 something other than the usual r3, and moved to r3
13948 before the call by intervening code. */
95f0d0d2 13949 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 13950 contents + rel->r_offset - d_offset);
102890f0 13951 if ((tls_mask & tls_gd) != 0)
411e1bfb 13952 {
102890f0 13953 /* IE */
b86ac8e3 13954 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
13955 insn1 |= 58 << 26; /* ld */
13956 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13957 if (offset != (bfd_vma) -1)
f58d5a2d 13958 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13959 if ((tls_mask & TLS_EXPLICIT) == 0)
13960 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13961 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13962 else
102890f0
AM
13963 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13964 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13965 }
13966 else
13967 {
13968 /* LE */
b86ac8e3
AM
13969 insn1 &= 0x1f << 21;
13970 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
13971 insn2 = 0x38630000; /* addi 3,3,0 */
13972 if (tls_gd == 0)
951fd09b 13973 {
102890f0 13974 /* Was an LD reloc. */
1d483afe
AM
13975 if (toc_symndx)
13976 sec = local_sections[toc_symndx];
13977 for (r_symndx = 0;
13978 r_symndx < symtab_hdr->sh_info;
13979 r_symndx++)
13980 if (local_sections[r_symndx] == sec)
13981 break;
13982 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13983 r_symndx = STN_UNDEF;
102890f0 13984 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13985 if (r_symndx != STN_UNDEF)
1d483afe
AM
13986 rel->r_addend -= (local_syms[r_symndx].st_value
13987 + sec->output_offset
13988 + sec->output_section->vma);
951fd09b 13989 }
102890f0 13990 else if (toc_symndx != 0)
3a71aa26
AM
13991 {
13992 r_symndx = toc_symndx;
13993 rel->r_addend = toc_addend;
13994 }
102890f0
AM
13995 r_type = R_PPC64_TPREL16_HA;
13996 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13997 if (offset != (bfd_vma) -1)
13998 {
13999 rel[1].r_info = ELF64_R_INFO (r_symndx,
14000 R_PPC64_TPREL16_LO);
14001 rel[1].r_offset = offset + d_offset;
14002 rel[1].r_addend = rel->r_addend;
14003 }
102890f0 14004 }
95f0d0d2 14005 bfd_put_32 (input_bfd, insn1,
3a71aa26 14006 contents + rel->r_offset - d_offset);
727fc41e 14007 if (offset != (bfd_vma) -1)
c96e0573
AM
14008 {
14009 bfd_put_32 (input_bfd, insn2, contents + offset);
14010 if (offset + 8 <= input_section->size)
14011 {
14012 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14013 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14014 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14015 }
14016 }
727fc41e
AM
14017 if ((tls_mask & tls_gd) == 0
14018 && (tls_gd == 0 || toc_symndx != 0))
14019 {
14020 /* We changed the symbol. Start over in order
14021 to get h, sym, sec etc. right. */
c316a17c 14022 goto again;
727fc41e
AM
14023 }
14024 }
14025 break;
14026
14027 case R_PPC64_TLSGD:
37da22e5 14028 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 14029 && rel + 1 < relend)
727fc41e 14030 {
b9f04fe0 14031 unsigned int insn2;
727fc41e
AM
14032 bfd_vma offset = rel->r_offset;
14033
23cedd1d
AM
14034 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14035 {
14036 bfd_put_32 (output_bfd, NOP, contents + offset);
14037 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14038 break;
14039 }
14040
14041 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14042 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14043
727fc41e
AM
14044 if ((tls_mask & TLS_TPRELGD) != 0)
14045 {
14046 /* IE */
14047 r_type = R_PPC64_NONE;
14048 insn2 = 0x7c636a14; /* add 3,3,13 */
14049 }
14050 else
14051 {
14052 /* LE */
14053 if (toc_symndx != 0)
14054 {
14055 r_symndx = toc_symndx;
14056 rel->r_addend = toc_addend;
14057 }
14058 r_type = R_PPC64_TPREL16_LO;
14059 rel->r_offset = offset + d_offset;
14060 insn2 = 0x38630000; /* addi 3,3,0 */
14061 }
14062 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14063 /* Zap the reloc on the _tls_get_addr call too. */
14064 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14065 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 14066 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 14067 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 14068 goto again;
411e1bfb 14069 }
411e1bfb
AM
14070 break;
14071
727fc41e 14072 case R_PPC64_TLSLD:
37da22e5 14073 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 14074 && rel + 1 < relend)
727fc41e 14075 {
b9f04fe0 14076 unsigned int insn2;
727fc41e
AM
14077 bfd_vma offset = rel->r_offset;
14078
23cedd1d
AM
14079 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14080 {
14081 bfd_put_32 (output_bfd, NOP, contents + offset);
14082 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14083 break;
14084 }
14085
14086 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14087 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14088
727fc41e
AM
14089 if (toc_symndx)
14090 sec = local_sections[toc_symndx];
14091 for (r_symndx = 0;
14092 r_symndx < symtab_hdr->sh_info;
14093 r_symndx++)
14094 if (local_sections[r_symndx] == sec)
14095 break;
14096 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 14097 r_symndx = STN_UNDEF;
727fc41e 14098 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 14099 if (r_symndx != STN_UNDEF)
727fc41e
AM
14100 rel->r_addend -= (local_syms[r_symndx].st_value
14101 + sec->output_offset
14102 + sec->output_section->vma);
14103
14104 r_type = R_PPC64_TPREL16_LO;
14105 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14106 rel->r_offset = offset + d_offset;
14107 /* Zap the reloc on the _tls_get_addr call too. */
14108 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14109 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 14110 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 14111 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 14112 goto again;
727fc41e
AM
14113 }
14114 break;
14115
411e1bfb 14116 case R_PPC64_DTPMOD64:
951fd09b
AM
14117 if (rel + 1 < relend
14118 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14119 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 14120 {
951fd09b
AM
14121 if ((tls_mask & TLS_GD) == 0)
14122 {
14123 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14124 if ((tls_mask & TLS_TPRELGD) != 0)
14125 r_type = R_PPC64_TPREL64;
14126 else
14127 {
4ce794b7 14128 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
14129 r_type = R_PPC64_NONE;
14130 }
14131 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14132 }
14133 }
14134 else
14135 {
14136 if ((tls_mask & TLS_LD) == 0)
411e1bfb 14137 {
4ce794b7 14138 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 14139 r_type = R_PPC64_NONE;
951fd09b 14140 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 14141 }
411e1bfb
AM
14142 }
14143 break;
14144
14145 case R_PPC64_TPREL64:
951fd09b 14146 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
14147 {
14148 r_type = R_PPC64_NONE;
14149 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14150 }
14151 break;
52a82034 14152
006589cf
AM
14153 case R_PPC64_ENTRY:
14154 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14155 if (!bfd_link_pic (info)
14156 && !info->traditional_format
14157 && relocation + 0x80008000 <= 0xffffffff)
14158 {
14159 unsigned int insn1, insn2;
14160
14161 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14162 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14163 if ((insn1 & ~0xfffc) == LD_R2_0R12
14164 && insn2 == ADD_R2_R2_R12)
14165 {
95f0d0d2 14166 bfd_put_32 (input_bfd,
006589cf
AM
14167 LIS_R2 + PPC_HA (relocation),
14168 contents + rel->r_offset);
95f0d0d2 14169 bfd_put_32 (input_bfd,
006589cf
AM
14170 ADDI_R2_R2 + PPC_LO (relocation),
14171 contents + rel->r_offset + 4);
14172 }
14173 }
14174 else
14175 {
14176 relocation -= (rel->r_offset
14177 + input_section->output_offset
14178 + input_section->output_section->vma);
14179 if (relocation + 0x80008000 <= 0xffffffff)
14180 {
14181 unsigned int insn1, insn2;
14182
14183 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14184 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14185 if ((insn1 & ~0xfffc) == LD_R2_0R12
14186 && insn2 == ADD_R2_R2_R12)
14187 {
95f0d0d2 14188 bfd_put_32 (input_bfd,
006589cf
AM
14189 ADDIS_R2_R12 + PPC_HA (relocation),
14190 contents + rel->r_offset);
95f0d0d2 14191 bfd_put_32 (input_bfd,
006589cf
AM
14192 ADDI_R2_R2 + PPC_LO (relocation),
14193 contents + rel->r_offset + 4);
14194 }
14195 }
14196 }
14197 break;
14198
52a82034
AM
14199 case R_PPC64_REL16_HA:
14200 /* If we are generating a non-PIC executable, edit
14201 . 0: addis 2,12,.TOC.-0b@ha
14202 . addi 2,2,.TOC.-0b@l
14203 used by ELFv2 global entry points to set up r2, to
14204 . lis 2,.TOC.@ha
14205 . addi 2,2,.TOC.@l
14206 if .TOC. is in range. */
0e1862bb 14207 if (!bfd_link_pic (info)
810d4e75 14208 && !info->traditional_format
006589cf 14209 && !htab->opd_abi
4f038ee5 14210 && rel->r_addend == d_offset
52a82034
AM
14211 && h != NULL && &h->elf == htab->elf.hgot
14212 && rel + 1 < relend
14213 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14214 && rel[1].r_offset == rel->r_offset + 4
14215 && rel[1].r_addend == rel->r_addend + 4
14216 && relocation + 0x80008000 <= 0xffffffff)
14217 {
14218 unsigned int insn1, insn2;
14219 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
14220 insn1 = bfd_get_32 (input_bfd, contents + offset);
14221 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14222 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14223 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14224 {
14225 r_type = R_PPC64_ADDR16_HA;
14226 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14227 rel->r_addend -= d_offset;
14228 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14229 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14230 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14231 }
14232 }
14233 break;
411e1bfb
AM
14234 }
14235
14236 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14237 insn = 0;
b25116a9
AM
14238 max_br_offset = 1 << 25;
14239 addend = rel->r_addend;
bc30df16 14240 reloc_dest = DEST_NORMAL;
65f38f15 14241 switch (r_type)
5bd4f169
AM
14242 {
14243 default:
65f38f15 14244 break;
5bd4f169 14245
3b421ab3
AM
14246 case R_PPC64_TOCSAVE:
14247 if (relocation + addend == (rel->r_offset
14248 + input_section->output_offset
14249 + input_section->output_section->vma)
14250 && tocsave_find (htab, NO_INSERT,
14251 &local_syms, rel, input_bfd))
14252 {
14253 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14254 if (insn == NOP
14255 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14256 bfd_put_32 (input_bfd,
14257 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14258 contents + rel->r_offset);
14259 }
14260 break;
14261
65f38f15
AM
14262 /* Branch taken prediction relocations. */
14263 case R_PPC64_ADDR14_BRTAKEN:
14264 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14265 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14266 /* Fall through. */
65f38f15 14267
86c76c7b 14268 /* Branch not taken prediction relocations. */
65f38f15
AM
14269 case R_PPC64_ADDR14_BRNTAKEN:
14270 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14271 insn |= bfd_get_32 (input_bfd,
411e1bfb 14272 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14273 /* Fall through. */
86c76c7b 14274
b25116a9
AM
14275 case R_PPC64_REL14:
14276 max_br_offset = 1 << 15;
1a0670f3 14277 /* Fall through. */
5bd4f169 14278
65f38f15 14279 case R_PPC64_REL24:
05d0e962 14280 case R_PPC64_REL24_NOTOC:
23cedd1d 14281 case R_PPC64_PLTCALL:
ad8e1ba5
AM
14282 /* Calls to functions with a different TOC, such as calls to
14283 shared objects, need to alter the TOC pointer. This is
14284 done using a linkage stub. A REL24 branching to these
14285 linkage stubs needs to be followed by a nop, as the nop
14286 will be replaced with an instruction to restore the TOC
14287 base pointer. */
8387904d 14288 fdh = h;
b31867b6
AM
14289 if (h != NULL
14290 && h->oh != NULL
14291 && h->oh->is_func_descriptor)
14292 fdh = ppc_follow_link (h->oh);
31c76678
DK
14293 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14294 htab);
23cedd1d
AM
14295 if (r_type == R_PPC64_PLTCALL
14296 && stub_entry != NULL
05d0e962
AM
14297 && stub_entry->stub_type >= ppc_stub_plt_call
14298 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
14299 stub_entry = NULL;
14300
6abec6bc 14301 if (stub_entry != NULL
d0abeec8
AM
14302 && ((stub_entry->stub_type >= ppc_stub_plt_call
14303 && stub_entry->stub_type <= ppc_stub_plt_call_both)
ad8e1ba5 14304 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
14305 || stub_entry->stub_type == ppc_stub_plt_branch_both
14306 || stub_entry->stub_type == ppc_stub_long_branch_r2off
14307 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 14308 {
b25116a9 14309 bfd_boolean can_plt_call = FALSE;
721956f4 14310
6e1816be
AM
14311 if (stub_entry->stub_type == ppc_stub_plt_call
14312 && !htab->opd_abi
14313 && htab->params->plt_localentry0 != 0
14314 && is_elfv2_localentry0 (&h->elf))
14315 {
14316 /* The function doesn't use or change r2. */
14317 can_plt_call = TRUE;
14318 }
05d0e962
AM
14319 else if (r_type == R_PPC64_REL24_NOTOC)
14320 {
14321 /* NOTOC calls don't need to restore r2. */
14322 can_plt_call = TRUE;
14323 }
6e1816be 14324
f378ab09 14325 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
14326 branch and link followed by a nop. The nop is
14327 replaced by an insn to restore r2. */
6e1816be 14328 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14329 {
ba8ca3e7
AM
14330 unsigned long br;
14331
14332 br = bfd_get_32 (input_bfd,
14333 contents + rel->r_offset);
14334 if ((br & 1) != 0)
41bd81ab 14335 {
ba8ca3e7
AM
14336 unsigned long nop;
14337
14338 nop = bfd_get_32 (input_bfd,
14339 contents + rel->r_offset + 4);
23cedd1d
AM
14340 if (nop == LD_R2_0R1 + STK_TOC (htab))
14341 can_plt_call = TRUE;
14342 else if (nop == NOP
14343 || nop == CROR_151515
14344 || nop == CROR_313131)
a7f2871e 14345 {
ba8ca3e7
AM
14346 if (h != NULL
14347 && (h == htab->tls_get_addr_fd
14348 || h == htab->tls_get_addr)
7c9cf415 14349 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14350 {
14351 /* Special stub used, leave nop alone. */
14352 }
14353 else
a078d95a
AM
14354 bfd_put_32 (input_bfd,
14355 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14356 contents + rel->r_offset + 4);
14357 can_plt_call = TRUE;
a7f2871e 14358 }
41bd81ab 14359 }
5bd4f169 14360 }
721956f4 14361
ba8ca3e7 14362 if (!can_plt_call && h != NULL)
721956f4 14363 {
ba8ca3e7
AM
14364 const char *name = h->elf.root.root.string;
14365
14366 if (*name == '.')
14367 ++name;
14368
14369 if (strncmp (name, "__libc_start_main", 17) == 0
14370 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14371 {
ba8ca3e7
AM
14372 /* Allow crt1 branch to go via a toc adjusting
14373 stub. Other calls that never return could do
14374 the same, if we could detect such. */
b25116a9 14375 can_plt_call = TRUE;
6ab189d5 14376 }
ba8ca3e7
AM
14377 }
14378
14379 if (!can_plt_call)
14380 {
14381 /* g++ as of 20130507 emits self-calls without a
14382 following nop. This is arguably wrong since we
14383 have conflicting information. On the one hand a
14384 global symbol and on the other a local call
14385 sequence, but don't error for this special case.
14386 It isn't possible to cheaply verify we have
14387 exactly such a call. Allow all calls to the same
14388 section. */
14389 asection *code_sec = sec;
14390
14391 if (get_opd_info (sec) != NULL)
ad8e1ba5 14392 {
ba8ca3e7
AM
14393 bfd_vma off = (relocation + addend
14394 - sec->output_section->vma
14395 - sec->output_offset);
bc30df16 14396
ba8ca3e7 14397 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14398 }
ba8ca3e7
AM
14399 if (code_sec == input_section)
14400 can_plt_call = TRUE;
14401 }
14402
14403 if (!can_plt_call)
14404 {
05d0e962
AM
14405 if (stub_entry->stub_type >= ppc_stub_plt_call
14406 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 14407 info->callbacks->einfo
695344c0 14408 /* xgettext:c-format */
c1c8c1ef 14409 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 14410 "(plt call stub)\n"),
4805fc55
AM
14411 input_bfd, input_section, rel->r_offset, sym_name);
14412 else
14413 info->callbacks->einfo
695344c0 14414 /* xgettext:c-format */
c1c8c1ef 14415 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 14416 "(toc save/adjust stub)\n"),
4805fc55 14417 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14418
14419 bfd_set_error (bfd_error_bad_value);
14420 ret = FALSE;
721956f4
AM
14421 }
14422
b25116a9 14423 if (can_plt_call
05d0e962
AM
14424 && stub_entry->stub_type >= ppc_stub_plt_call
14425 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
14426 unresolved_reloc = FALSE;
14427 }
14428
6abec6bc
AM
14429 if ((stub_entry == NULL
14430 || stub_entry->stub_type == ppc_stub_long_branch
14431 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14432 && get_opd_info (sec) != NULL)
14433 {
14434 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14435 bfd_vma off = (relocation + addend
14436 - sec->output_section->vma
14437 - sec->output_offset);
aef36ac1 14438 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14439 if (dest != (bfd_vma) -1)
14440 {
14441 relocation = dest;
14442 addend = 0;
bc30df16 14443 reloc_dest = DEST_OPD;
8387904d
AM
14444 }
14445 }
14446
b25116a9
AM
14447 /* If the branch is out of reach we ought to have a long
14448 branch stub. */
14449 from = (rel->r_offset
14450 + input_section->output_offset
14451 + input_section->output_section->vma);
14452
6911b7dc
AM
14453 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14454 ? fdh->elf.other
14455 : sym->st_other);
14456
6abec6bc
AM
14457 if (stub_entry != NULL
14458 && (stub_entry->stub_type == ppc_stub_long_branch
14459 || stub_entry->stub_type == ppc_stub_plt_branch)
14460 && (r_type == R_PPC64_ADDR14_BRTAKEN
14461 || r_type == R_PPC64_ADDR14_BRNTAKEN
14462 || (relocation + addend - from + max_br_offset
14463 < 2 * max_br_offset)))
14464 /* Don't use the stub if this branch is in range. */
14465 stub_entry = NULL;
b25116a9 14466
05d0e962
AM
14467 if (stub_entry != NULL
14468 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
14469 || stub_entry->stub_type == ppc_stub_long_branch_both
14470 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
14471 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14472 && (r_type != R_PPC64_REL24_NOTOC
14473 || ((fdh ? fdh->elf.other : sym->st_other)
14474 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
14475 && (relocation + addend - from + max_br_offset
14476 < 2 * max_br_offset))
14477 stub_entry = NULL;
14478
14479 if (stub_entry != NULL
14480 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
14481 || stub_entry->stub_type == ppc_stub_long_branch_both
14482 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14483 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14484 && r_type == R_PPC64_REL24_NOTOC
14485 && (relocation + addend - from + max_br_offset
14486 < 2 * max_br_offset))
14487 stub_entry = NULL;
14488
b25116a9
AM
14489 if (stub_entry != NULL)
14490 {
14491 /* Munge up the value and addend so that we call the stub
14492 rather than the procedure directly. */
a4b6fadd
AM
14493 asection *stub_sec = stub_entry->group->stub_sec;
14494
14495 if (stub_entry->stub_type == ppc_stub_save_res)
14496 relocation += (stub_sec->output_offset
14497 + stub_sec->output_section->vma
14498 + stub_sec->size - htab->sfpr->size
14499 - htab->sfpr->output_offset
14500 - htab->sfpr->output_section->vma);
14501 else
14502 relocation = (stub_entry->stub_offset
14503 + stub_sec->output_offset
14504 + stub_sec->output_section->vma);
b25116a9 14505 addend = 0;
bc30df16 14506 reloc_dest = DEST_STUB;
3b421ab3 14507
05d0e962
AM
14508 if (((stub_entry->stub_type == ppc_stub_plt_call
14509 && ALWAYS_EMIT_R2SAVE)
14510 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14511 || stub_entry->stub_type == ppc_stub_plt_call_both)
e81b4c93
AM
14512 && !(h != NULL
14513 && (h == htab->tls_get_addr_fd
14514 || h == htab->tls_get_addr)
14515 && htab->params->tls_get_addr_opt)
3b421ab3
AM
14516 && rel + 1 < relend
14517 && rel[1].r_offset == rel->r_offset + 4
14518 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14519 relocation += 4;
05d0e962
AM
14520 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
14521 || stub_entry->stub_type == ppc_stub_plt_branch_both
14522 || stub_entry->stub_type == ppc_stub_plt_call_both)
14523 && r_type == R_PPC64_REL24_NOTOC)
14524 relocation += 4;
b25116a9
AM
14525 }
14526
14527 if (insn != 0)
14528 {
794e51c0 14529 if (is_isa_v2)
721956f4 14530 {
b25116a9
AM
14531 /* Set 'a' bit. This is 0b00010 in BO field for branch
14532 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14533 for branch on CTR insns (BO == 1a00t or 1a01t). */
14534 if ((insn & (0x14 << 21)) == (0x04 << 21))
14535 insn |= 0x02 << 21;
14536 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14537 insn |= 0x08 << 21;
14538 else
14539 break;
14540 }
14541 else
14542 {
14543 /* Invert 'y' bit if not the default. */
4cc603a5 14544 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14545 insn ^= 0x01 << 21;
721956f4 14546 }
b25116a9 14547
95f0d0d2 14548 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 14549 }
e86ce104 14550
06da1e8e
AM
14551 /* NOP out calls to undefined weak functions.
14552 We can thus call a weak function without first
14553 checking whether the function is defined. */
b25116a9 14554 else if (h != NULL
039b3fef 14555 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14556 && h->elf.dynindx == -1
05d0e962
AM
14557 && (r_type == R_PPC64_REL24
14558 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 14559 && relocation == 0
4cc603a5 14560 && addend == 0)
e86ce104 14561 {
95f0d0d2 14562 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 14563 goto copy_reloc;
e86ce104 14564 }
65f38f15 14565 break;
066f4018
AM
14566
14567 case R_PPC64_GOT16_DS:
14568 from = TOCstart + htab->sec_info[input_section->id].toc_off;
14569 if (relocation + addend - from + 0x8000 < 0x10000
14570 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14571 {
14572 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14573 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
14574 {
14575 insn += (14u << 26) - (58u << 26);
14576 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
14577 r_type = R_PPC64_TOC16;
14578 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14579 }
14580 }
14581 break;
14582
14583 case R_PPC64_GOT16_LO_DS:
14584 case R_PPC64_GOT16_HA:
14585 from = TOCstart + htab->sec_info[input_section->id].toc_off;
14586 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
14587 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14588 {
14589 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14590 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
14591 {
14592 insn += (14u << 26) - (58u << 26);
14593 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
14594 r_type = R_PPC64_TOC16_LO;
14595 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14596 }
14597 else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
14598 {
14599 r_type = R_PPC64_TOC16_HA;
14600 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14601 }
14602 }
14603 break;
65f38f15 14604 }
5bd4f169 14605
65f38f15 14606 /* Set `addend'. */
411e1bfb 14607 tls_type = 0;
23cedd1d 14608 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
14609 switch (r_type)
14610 {
14611 default:
cf97bcb0
AM
14612 /* xgettext:c-format */
14613 _bfd_error_handler (_("%pB: %s unsupported"),
14614 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 14615
65f38f15 14616 bfd_set_error (bfd_error_bad_value);
b34976b6 14617 ret = FALSE;
c316a17c 14618 goto copy_reloc;
5bd4f169 14619
65f38f15 14620 case R_PPC64_NONE:
411e1bfb 14621 case R_PPC64_TLS:
727fc41e
AM
14622 case R_PPC64_TLSGD:
14623 case R_PPC64_TLSLD:
3b421ab3 14624 case R_PPC64_TOCSAVE:
04c9666a
AM
14625 case R_PPC64_GNU_VTINHERIT:
14626 case R_PPC64_GNU_VTENTRY:
006589cf 14627 case R_PPC64_ENTRY:
c316a17c 14628 goto copy_reloc;
5bd4f169
AM
14629
14630 /* GOT16 relocations. Like an ADDR16 using the symbol's
14631 address in the GOT as relocation value instead of the
411e1bfb 14632 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14633 symbol and put the symbol value there. */
411e1bfb
AM
14634 case R_PPC64_GOT_TLSGD16:
14635 case R_PPC64_GOT_TLSGD16_LO:
14636 case R_PPC64_GOT_TLSGD16_HI:
14637 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14638 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14639 goto dogot;
14640
14641 case R_PPC64_GOT_TLSLD16:
14642 case R_PPC64_GOT_TLSLD16_LO:
14643 case R_PPC64_GOT_TLSLD16_HI:
14644 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14645 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14646 goto dogot;
14647
14648 case R_PPC64_GOT_TPREL16_DS:
14649 case R_PPC64_GOT_TPREL16_LO_DS:
14650 case R_PPC64_GOT_TPREL16_HI:
14651 case R_PPC64_GOT_TPREL16_HA:
14652 tls_type = TLS_TLS | TLS_TPREL;
14653 goto dogot;
14654
14655 case R_PPC64_GOT_DTPREL16_DS:
14656 case R_PPC64_GOT_DTPREL16_LO_DS:
14657 case R_PPC64_GOT_DTPREL16_HI:
14658 case R_PPC64_GOT_DTPREL16_HA:
14659 tls_type = TLS_TLS | TLS_DTPREL;
14660 goto dogot;
14661
65f38f15
AM
14662 case R_PPC64_GOT16:
14663 case R_PPC64_GOT16_LO:
14664 case R_PPC64_GOT16_HI:
14665 case R_PPC64_GOT16_HA:
14666 case R_PPC64_GOT16_DS:
14667 case R_PPC64_GOT16_LO_DS:
411e1bfb 14668 dogot:
5bd4f169
AM
14669 {
14670 /* Relocation is to the entry for this symbol in the global
14671 offset table. */
e717da7e 14672 asection *got;
d881513a 14673 bfd_vma *offp;
5bd4f169 14674 bfd_vma off;
d881513a 14675 unsigned long indx = 0;
927be08e 14676 struct got_entry *ent;
65f38f15 14677
d881513a
AM
14678 if (tls_type == (TLS_TLS | TLS_LD)
14679 && (h == NULL
f5385ebf 14680 || !h->elf.def_dynamic))
927be08e 14681 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14682 else
5bd4f169 14683 {
d881513a
AM
14684 if (h != NULL)
14685 {
f0158f44
AM
14686 if (!htab->elf.dynamic_sections_created
14687 || h->elf.dynindx == -1
14688 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 14689 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
14690 /* This is actually a static link, or it is a
14691 -Bsymbolic link and the symbol is defined
14692 locally, or the symbol was forced to be local
14693 because of a version file. */
14694 ;
14695 else
14696 {
039b3fef 14697 indx = h->elf.dynindx;
d881513a
AM
14698 unresolved_reloc = FALSE;
14699 }
039b3fef 14700 ent = h->elf.got.glist;
d881513a 14701 }
411e1bfb 14702 else
5bd4f169 14703 {
d881513a
AM
14704 if (local_got_ents == NULL)
14705 abort ();
14706 ent = local_got_ents[r_symndx];
5bd4f169 14707 }
d881513a
AM
14708
14709 for (; ent != NULL; ent = ent->next)
31c76678 14710 if (ent->addend == orig_rel.r_addend
e717da7e 14711 && ent->owner == input_bfd
d881513a
AM
14712 && ent->tls_type == tls_type)
14713 break;
5bd4f169 14714 }
411e1bfb 14715
927be08e
AM
14716 if (ent == NULL)
14717 abort ();
14718 if (ent->is_indirect)
14719 ent = ent->got.ent;
14720 offp = &ent->got.offset;
14721 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
14722 if (got == NULL)
14723 abort ();
14724
411e1bfb
AM
14725 /* The offset must always be a multiple of 8. We use the
14726 least significant bit to record whether we have already
14727 processed this entry. */
d881513a 14728 off = *offp;
411e1bfb
AM
14729 if ((off & 1) != 0)
14730 off &= ~1;
5bd4f169
AM
14731 else
14732 {
411e1bfb
AM
14733 /* Generate relocs for the dynamic linker, except in
14734 the case of TLSLD where we'll use one entry per
14735 module. */
25f23106
AM
14736 asection *relgot;
14737 bfd_boolean ifunc;
e717da7e 14738
d881513a 14739 *offp = off | 1;
25f23106
AM
14740 relgot = NULL;
14741 ifunc = (h != NULL
14742 ? h->elf.type == STT_GNU_IFUNC
14743 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 14744 if (ifunc)
82e66161
AM
14745 {
14746 relgot = htab->elf.irelplt;
14747 if (indx == 0)
14748 htab->local_ifunc_resolver = 1;
14749 else if (is_static_defined (&h->elf))
14750 htab->maybe_local_ifunc_resolver = 1;
14751 }
f0158f44
AM
14752 else if (indx != 0
14753 || (bfd_link_pic (info)
14754 && (h == NULL
21d68fcd 14755 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 14756 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
14757 && !h->elf.def_dynamic))
14758 && !(tls_type == (TLS_TLS | TLS_TPREL)
14759 && bfd_link_executable (info)
14760 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 14761 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 14762 if (relgot != NULL)
5bd4f169 14763 {
e717da7e
AM
14764 outrel.r_offset = (got->output_section->vma
14765 + got->output_offset
411e1bfb 14766 + off);
4cc603a5 14767 outrel.r_addend = addend;
d881513a 14768 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 14769 {
411e1bfb 14770 outrel.r_addend = 0;
e515b051 14771 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
14772 if (tls_type == (TLS_TLS | TLS_GD))
14773 {
e717da7e
AM
14774 loc = relgot->contents;
14775 loc += (relgot->reloc_count++
d881513a
AM
14776 * sizeof (Elf64_External_Rela));
14777 bfd_elf64_swap_reloca_out (output_bfd,
14778 &outrel, loc);
e515b051 14779 outrel.r_offset += 8;
4cc603a5 14780 outrel.r_addend = addend;
d881513a
AM
14781 outrel.r_info
14782 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 14783 }
411e1bfb 14784 }
951fd09b 14785 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 14786 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 14787 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 14788 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
14789 else if (indx != 0)
14790 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14791 else
81407a69 14792 {
25f23106
AM
14793 if (ifunc)
14794 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14795 else
14796 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
14797
14798 /* Write the .got section contents for the sake
14799 of prelink. */
e717da7e 14800 loc = got->contents + off;
23fbd6fa
JJ
14801 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14802 loc);
81407a69 14803 }
81407a69
AM
14804
14805 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
14806 {
14807 outrel.r_addend += relocation;
14808 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
14809 {
14810 if (htab->elf.tls_sec == NULL)
14811 outrel.r_addend = 0;
14812 else
14813 outrel.r_addend -= htab->elf.tls_sec->vma;
14814 }
e515b051 14815 }
e717da7e
AM
14816 loc = relgot->contents;
14817 loc += (relgot->reloc_count++
411e1bfb
AM
14818 * sizeof (Elf64_External_Rela));
14819 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14820 }
14821
ad8e1ba5 14822 /* Init the .got section contents here if we're not
81407a69 14823 emitting a reloc. */
d881513a 14824 else
411e1bfb 14825 {
4cc603a5 14826 relocation += addend;
f0158f44 14827 if (tls_type != 0)
411e1bfb 14828 {
989f9879
AM
14829 if (htab->elf.tls_sec == NULL)
14830 relocation = 0;
14831 else
14832 {
f0158f44
AM
14833 if (tls_type & TLS_LD)
14834 relocation = 0;
14835 else
14836 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 14837 if (tls_type & TLS_TPREL)
989f9879
AM
14838 relocation += DTP_OFFSET - TP_OFFSET;
14839 }
5bd4f169 14840
f0158f44 14841 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
14842 {
14843 bfd_put_64 (output_bfd, relocation,
e717da7e 14844 got->contents + off + 8);
676ee2b5 14845 relocation = 1;
7b609f53 14846 }
411e1bfb
AM
14847 }
14848 bfd_put_64 (output_bfd, relocation,
e717da7e 14849 got->contents + off);
5bd4f169
AM
14850 }
14851 }
14852
65f38f15
AM
14853 if (off >= (bfd_vma) -2)
14854 abort ();
14855
bf102f86 14856 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 14857 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 14858 }
65f38f15
AM
14859 break;
14860
14861 case R_PPC64_PLT16_HA:
14862 case R_PPC64_PLT16_HI:
14863 case R_PPC64_PLT16_LO:
08be3224 14864 case R_PPC64_PLT16_LO_DS:
65f38f15
AM
14865 case R_PPC64_PLT32:
14866 case R_PPC64_PLT64:
23cedd1d
AM
14867 case R_PPC64_PLTSEQ:
14868 case R_PPC64_PLTCALL:
65f38f15
AM
14869 /* Relocation is to the entry for this symbol in the
14870 procedure linkage table. */
23cedd1d 14871 unresolved_reloc = TRUE;
cbf95972
AM
14872 {
14873 struct plt_entry **plt_list = NULL;
14874 if (h != NULL)
14875 plt_list = &h->elf.plt.plist;
14876 else if (local_got_ents != NULL)
14877 {
14878 struct plt_entry **local_plt = (struct plt_entry **)
14879 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 14880 plt_list = local_plt + r_symndx;
cbf95972
AM
14881 }
14882 if (plt_list)
14883 {
14884 struct plt_entry *ent;
65f38f15 14885
cbf95972
AM
14886 for (ent = *plt_list; ent != NULL; ent = ent->next)
14887 if (ent->plt.offset != (bfd_vma) -1
14888 && ent->addend == orig_rel.r_addend)
14889 {
14890 asection *plt;
08be3224 14891 bfd_vma got;
cbf95972
AM
14892
14893 plt = htab->elf.splt;
14894 if (!htab->elf.dynamic_sections_created
14895 || h == NULL
14896 || h->elf.dynindx == -1)
2d7ad24e
AM
14897 {
14898 if (h != NULL
14899 ? h->elf.type == STT_GNU_IFUNC
14900 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14901 plt = htab->elf.iplt;
14902 else
14903 plt = htab->pltlocal;
14904 }
14905 relocation = (plt->output_section->vma
14906 + plt->output_offset
14907 + ent->plt.offset);
08be3224 14908 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
14909 || r_type == R_PPC64_PLT16_HI
14910 || r_type == R_PPC64_PLT16_LO
14911 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
14912 {
14913 got = (elf_gp (output_bfd)
14914 + htab->sec_info[input_section->id].toc_off);
14915 relocation -= got;
14916 }
cbf95972
AM
14917 addend = 0;
14918 unresolved_reloc = FALSE;
14919 break;
14920 }
14921 }
14922 }
65f38f15 14923 break;
5bd4f169 14924
0b13192e
AM
14925 case R_PPC64_TOC:
14926 /* Relocation value is TOC base. */
14927 relocation = TOCstart;
cf35638d 14928 if (r_symndx == STN_UNDEF)
6f20ed8a 14929 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
14930 else if (unresolved_reloc)
14931 ;
6f20ed8a
AM
14932 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14933 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
14934 else
14935 unresolved_reloc = TRUE;
ab96bf03 14936 goto dodyn;
0b13192e 14937
5bd4f169
AM
14938 /* TOC16 relocs. We want the offset relative to the TOC base,
14939 which is the address of the start of the TOC plus 0x8000.
14940 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14941 in this order. */
65f38f15
AM
14942 case R_PPC64_TOC16:
14943 case R_PPC64_TOC16_LO:
14944 case R_PPC64_TOC16_HI:
14945 case R_PPC64_TOC16_DS:
14946 case R_PPC64_TOC16_LO_DS:
14947 case R_PPC64_TOC16_HA:
6f20ed8a 14948 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
14949 break;
14950
14951 /* Relocate against the beginning of the section. */
65f38f15
AM
14952 case R_PPC64_SECTOFF:
14953 case R_PPC64_SECTOFF_LO:
14954 case R_PPC64_SECTOFF_HI:
14955 case R_PPC64_SECTOFF_DS:
14956 case R_PPC64_SECTOFF_LO_DS:
14957 case R_PPC64_SECTOFF_HA:
4ce794b7 14958 if (sec != NULL)
65f38f15 14959 addend -= sec->output_section->vma;
5bd4f169
AM
14960 break;
14961
25f23106
AM
14962 case R_PPC64_REL16:
14963 case R_PPC64_REL16_LO:
14964 case R_PPC64_REL16_HI:
14965 case R_PPC64_REL16_HA:
4a969973
AM
14966 case R_PPC64_REL16_HIGH:
14967 case R_PPC64_REL16_HIGHA:
14968 case R_PPC64_REL16_HIGHER:
14969 case R_PPC64_REL16_HIGHERA:
14970 case R_PPC64_REL16_HIGHEST:
14971 case R_PPC64_REL16_HIGHESTA:
a680de9a 14972 case R_PPC64_REL16DX_HA:
25f23106
AM
14973 break;
14974
721956f4
AM
14975 case R_PPC64_REL14:
14976 case R_PPC64_REL14_BRNTAKEN:
14977 case R_PPC64_REL14_BRTAKEN:
5d1634d7 14978 case R_PPC64_REL24:
05d0e962 14979 case R_PPC64_REL24_NOTOC:
5d1634d7
AM
14980 break;
14981
411e1bfb
AM
14982 case R_PPC64_TPREL16:
14983 case R_PPC64_TPREL16_LO:
14984 case R_PPC64_TPREL16_HI:
14985 case R_PPC64_TPREL16_HA:
14986 case R_PPC64_TPREL16_DS:
14987 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
14988 case R_PPC64_TPREL16_HIGH:
14989 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
14990 case R_PPC64_TPREL16_HIGHER:
14991 case R_PPC64_TPREL16_HIGHERA:
14992 case R_PPC64_TPREL16_HIGHEST:
14993 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
14994 if (h != NULL
14995 && h->elf.root.type == bfd_link_hash_undefweak
14996 && h->elf.dynindx == -1)
14997 {
14998 /* Make this relocation against an undefined weak symbol
14999 resolve to zero. This is really just a tweak, since
15000 code using weak externs ought to check that they are
15001 defined before using them. */
15002 bfd_byte *p = contents + rel->r_offset - d_offset;
15003
95f0d0d2 15004 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
15005 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15006 if (insn != 0)
95f0d0d2 15007 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
15008 break;
15009 }
989f9879
AM
15010 if (htab->elf.tls_sec != NULL)
15011 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
15012 /* The TPREL16 relocs shouldn't really be used in shared
15013 libs or with non-local symbols as that will result in
15014 DT_TEXTREL being set, but support them anyway. */
15015 goto dodyn;
411e1bfb
AM
15016
15017 case R_PPC64_DTPREL16:
15018 case R_PPC64_DTPREL16_LO:
15019 case R_PPC64_DTPREL16_HI:
15020 case R_PPC64_DTPREL16_HA:
15021 case R_PPC64_DTPREL16_DS:
15022 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
15023 case R_PPC64_DTPREL16_HIGH:
15024 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
15025 case R_PPC64_DTPREL16_HIGHER:
15026 case R_PPC64_DTPREL16_HIGHERA:
15027 case R_PPC64_DTPREL16_HIGHEST:
15028 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
15029 if (htab->elf.tls_sec != NULL)
15030 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
15031 break;
15032
45965137
AM
15033 case R_PPC64_ADDR64_LOCAL:
15034 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15035 ? h->elf.other
15036 : sym->st_other);
15037 break;
15038
e515b051
AM
15039 case R_PPC64_DTPMOD64:
15040 relocation = 1;
15041 addend = 0;
15042 goto dodyn;
15043
411e1bfb 15044 case R_PPC64_TPREL64:
989f9879
AM
15045 if (htab->elf.tls_sec != NULL)
15046 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
15047 goto dodyn;
15048
15049 case R_PPC64_DTPREL64:
989f9879
AM
15050 if (htab->elf.tls_sec != NULL)
15051 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 15052 /* Fall through. */
411e1bfb 15053
65f38f15
AM
15054 /* Relocations that may need to be propagated if this is a
15055 dynamic object. */
04c9666a 15056 case R_PPC64_REL30:
65f38f15
AM
15057 case R_PPC64_REL32:
15058 case R_PPC64_REL64:
15059 case R_PPC64_ADDR14:
15060 case R_PPC64_ADDR14_BRNTAKEN:
15061 case R_PPC64_ADDR14_BRTAKEN:
15062 case R_PPC64_ADDR16:
15063 case R_PPC64_ADDR16_DS:
15064 case R_PPC64_ADDR16_HA:
15065 case R_PPC64_ADDR16_HI:
f9c6b907
AM
15066 case R_PPC64_ADDR16_HIGH:
15067 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15068 case R_PPC64_ADDR16_HIGHER:
15069 case R_PPC64_ADDR16_HIGHERA:
15070 case R_PPC64_ADDR16_HIGHEST:
15071 case R_PPC64_ADDR16_HIGHESTA:
15072 case R_PPC64_ADDR16_LO:
15073 case R_PPC64_ADDR16_LO_DS:
15074 case R_PPC64_ADDR24:
65f38f15
AM
15075 case R_PPC64_ADDR32:
15076 case R_PPC64_ADDR64:
15077 case R_PPC64_UADDR16:
15078 case R_PPC64_UADDR32:
15079 case R_PPC64_UADDR64:
411e1bfb 15080 dodyn:
5d1634d7 15081 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
15082 break;
15083
41bd81ab
AM
15084 if (NO_OPD_RELOCS && is_opd)
15085 break;
15086
8a9e8e72 15087 if (bfd_link_pic (info)
b1b07054
AM
15088 ? ((h == NULL
15089 || h->dyn_relocs != NULL)
15090 && ((h != NULL && pc_dynrelocs (h))
15091 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
15092 : (h != NULL
15093 ? h->dyn_relocs != NULL
d311bc8b 15094 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 15095 {
b34976b6 15096 bfd_boolean skip, relocate;
65f38f15 15097 asection *sreloc;
1cf1f670 15098 bfd_vma out_off;
82e66161 15099 long indx = 0;
65f38f15
AM
15100
15101 /* When generating a dynamic object, these relocations
15102 are copied into the output file to be resolved at run
15103 time. */
15104
b34976b6
AM
15105 skip = FALSE;
15106 relocate = FALSE;
65f38f15 15107
1cf1f670
AM
15108 out_off = _bfd_elf_section_offset (output_bfd, info,
15109 input_section, rel->r_offset);
15110 if (out_off == (bfd_vma) -1)
b34976b6 15111 skip = TRUE;
1cf1f670 15112 else if (out_off == (bfd_vma) -2)
b34976b6 15113 skip = TRUE, relocate = TRUE;
1cf1f670
AM
15114 out_off += (input_section->output_section->vma
15115 + input_section->output_offset);
15116 outrel.r_offset = out_off;
411e1bfb 15117 outrel.r_addend = rel->r_addend;
65f38f15 15118
1cf1f670
AM
15119 /* Optimize unaligned reloc use. */
15120 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15121 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15122 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15123 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15124 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15125 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15126 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15127 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15128 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15129
65f38f15 15130 if (skip)
0bb2d96a 15131 memset (&outrel, 0, sizeof outrel);
afe397ea 15132 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
15133 && !is_opd
15134 && r_type != R_PPC64_TOC)
14acf4dc 15135 {
82e66161
AM
15136 indx = h->elf.dynindx;
15137 BFD_ASSERT (indx != -1);
15138 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 15139 }
65f38f15
AM
15140 else
15141 {
41bd81ab
AM
15142 /* This symbol is local, or marked to become local,
15143 or this is an opd section reloc which must point
15144 at a local function. */
65f38f15 15145 outrel.r_addend += relocation;
e86ce104 15146 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 15147 {
3fad3c7c 15148 if (is_opd && h != NULL)
afbe61cf
AM
15149 {
15150 /* Lie about opd entries. This case occurs
15151 when building shared libraries and we
15152 reference a function in another shared
3fad3c7c
AM
15153 lib. The same thing happens for a weak
15154 definition in an application that's
15155 overridden by a strong definition in a
15156 shared lib. (I believe this is a generic
15157 bug in binutils handling of weak syms.)
15158 In these cases we won't use the opd
1e2f5b6e 15159 entry in this lib. */
b34976b6 15160 unresolved_reloc = FALSE;
afbe61cf 15161 }
25f23106
AM
15162 if (!is_opd
15163 && r_type == R_PPC64_ADDR64
15164 && (h != NULL
15165 ? h->elf.type == STT_GNU_IFUNC
15166 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15167 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15168 else
15169 {
15170 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 15171
25f23106
AM
15172 /* We need to relocate .opd contents for ld.so.
15173 Prelink also wants simple and consistent rules
15174 for relocs. This make all RELATIVE relocs have
15175 *r_offset equal to r_addend. */
15176 relocate = TRUE;
15177 }
65f38f15
AM
15178 }
15179 else
15180 {
25f23106
AM
15181 if (h != NULL
15182 ? h->elf.type == STT_GNU_IFUNC
15183 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15184 {
25f53a85 15185 info->callbacks->einfo
695344c0 15186 /* xgettext:c-format */
174d0a74 15187 (_("%H: %s for indirect "
c1c8c1ef 15188 "function `%pT' unsupported\n"),
25f53a85 15189 input_bfd, input_section, rel->r_offset,
25f23106
AM
15190 ppc64_elf_howto_table[r_type]->name,
15191 sym_name);
15192 ret = FALSE;
15193 }
cf35638d 15194 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
15195 ;
15196 else if (sec == NULL || sec->owner == NULL)
15197 {
15198 bfd_set_error (bfd_error_bad_value);
b34976b6 15199 return FALSE;
65f38f15
AM
15200 }
15201 else
15202 {
15203 asection *osec;
15204
15205 osec = sec->output_section;
15206 indx = elf_section_data (osec)->dynindx;
15207
74541ad4
AM
15208 if (indx == 0)
15209 {
15210 if ((osec->flags & SEC_READONLY) == 0
15211 && htab->elf.data_index_section != NULL)
15212 osec = htab->elf.data_index_section;
15213 else
15214 osec = htab->elf.text_index_section;
15215 indx = elf_section_data (osec)->dynindx;
15216 }
15217 BFD_ASSERT (indx != 0);
15218
65f38f15
AM
15219 /* We are turning this relocation into one
15220 against a section symbol, so subtract out
15221 the output section's address but not the
15222 offset of the input section in the output
15223 section. */
15224 outrel.r_addend -= osec->vma;
15225 }
15226
15227 outrel.r_info = ELF64_R_INFO (indx, r_type);
15228 }
15229 }
15230
15231 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
15232 if (h != NULL
15233 ? h->elf.type == STT_GNU_IFUNC
15234 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
15235 {
15236 sreloc = htab->elf.irelplt;
15237 if (indx == 0)
15238 htab->local_ifunc_resolver = 1;
15239 else if (is_static_defined (&h->elf))
15240 htab->maybe_local_ifunc_resolver = 1;
15241 }
65f38f15
AM
15242 if (sreloc == NULL)
15243 abort ();
15244
dfbb6ac9
AM
15245 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15246 >= sreloc->size)
15247 abort ();
947216bf
AM
15248 loc = sreloc->contents;
15249 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
15250 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15251
15252 /* If this reloc is against an external symbol, it will
15253 be computed at runtime, so there's no need to do
81407a69
AM
15254 anything now. However, for the sake of prelink ensure
15255 that the section contents are a known value. */
2cdcc330 15256 if (!relocate)
81407a69
AM
15257 {
15258 unresolved_reloc = FALSE;
15259 /* The value chosen here is quite arbitrary as ld.so
15260 ignores section contents except for the special
15261 case of .opd where the contents might be accessed
15262 before relocation. Choose zero, as that won't
15263 cause reloc overflow. */
15264 relocation = 0;
15265 addend = 0;
15266 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15267 to improve backward compatibility with older
15268 versions of ld. */
15269 if (r_type == R_PPC64_ADDR64)
15270 addend = outrel.r_addend;
15271 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 15272 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 15273 addend = outrel.r_offset;
81407a69 15274 }
65f38f15 15275 }
5bd4f169
AM
15276 break;
15277
65f38f15
AM
15278 case R_PPC64_COPY:
15279 case R_PPC64_GLOB_DAT:
15280 case R_PPC64_JMP_SLOT:
25f23106 15281 case R_PPC64_JMP_IREL:
65f38f15
AM
15282 case R_PPC64_RELATIVE:
15283 /* We shouldn't ever see these dynamic relocs in relocatable
15284 files. */
ae9a127f 15285 /* Fall through. */
65f38f15
AM
15286
15287 case R_PPC64_PLTGOT16:
15288 case R_PPC64_PLTGOT16_DS:
15289 case R_PPC64_PLTGOT16_HA:
15290 case R_PPC64_PLTGOT16_HI:
15291 case R_PPC64_PLTGOT16_LO:
15292 case R_PPC64_PLTGOT16_LO_DS:
15293 case R_PPC64_PLTREL32:
15294 case R_PPC64_PLTREL64:
15295 /* These ones haven't been implemented yet. */
15296
25f53a85 15297 info->callbacks->einfo
695344c0 15298 /* xgettext:c-format */
c1c8c1ef 15299 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 15300 input_bfd,
4ce794b7 15301 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
15302
15303 bfd_set_error (bfd_error_invalid_operation);
b34976b6 15304 ret = FALSE;
c316a17c 15305 goto copy_reloc;
65f38f15 15306 }
5bd4f169 15307
67f0cbdb
AM
15308 /* Multi-instruction sequences that access the TOC can be
15309 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 15310 to nop; addi rb,r2,x; */
67f0cbdb
AM
15311 switch (r_type)
15312 {
15313 default:
15314 break;
15315
15316 case R_PPC64_GOT_TLSLD16_HI:
15317 case R_PPC64_GOT_TLSGD16_HI:
15318 case R_PPC64_GOT_TPREL16_HI:
15319 case R_PPC64_GOT_DTPREL16_HI:
15320 case R_PPC64_GOT16_HI:
15321 case R_PPC64_TOC16_HI:
15322 /* These relocs would only be useful if building up an
15323 offset to later add to r2, perhaps in an indexed
15324 addressing mode instruction. Don't try to optimize.
15325 Unfortunately, the possibility of someone building up an
15326 offset like this or even with the HA relocs, means that
15327 we need to check the high insn when optimizing the low
15328 insn. */
15329 break;
15330
23cedd1d
AM
15331 case R_PPC64_PLTCALL:
15332 if (unresolved_reloc)
15333 {
15334 /* No plt entry. Make this into a direct call. */
15335 bfd_byte *p = contents + rel->r_offset;
15336 insn = bfd_get_32 (input_bfd, p);
15337 insn &= 1;
15338 bfd_put_32 (input_bfd, B_DOT | insn, p);
15339 bfd_put_32 (input_bfd, NOP, p + 4);
15340 unresolved_reloc = save_unresolved_reloc;
15341 r_type = R_PPC64_REL24;
15342 }
15343 break;
15344
15345 case R_PPC64_PLTSEQ:
15346 if (unresolved_reloc)
15347 {
15348 unresolved_reloc = FALSE;
15349 goto nop_it;
15350 }
15351 break;
15352
15353 case R_PPC64_PLT16_HA:
15354 if (unresolved_reloc)
15355 {
15356 unresolved_reloc = FALSE;
15357 goto nop_it;
15358 }
15359 /* Fall through. */
67f0cbdb
AM
15360 case R_PPC64_GOT_TLSLD16_HA:
15361 case R_PPC64_GOT_TLSGD16_HA:
15362 case R_PPC64_GOT_TPREL16_HA:
15363 case R_PPC64_GOT_DTPREL16_HA:
15364 case R_PPC64_GOT16_HA:
15365 case R_PPC64_TOC16_HA:
98528052 15366 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15367 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 15368 {
23cedd1d
AM
15369 bfd_byte *p;
15370 nop_it:
15371 p = contents + (rel->r_offset & ~3);
98528052 15372 bfd_put_32 (input_bfd, NOP, p);
d830549d 15373 goto copy_reloc;
98528052 15374 }
67f0cbdb
AM
15375 break;
15376
23cedd1d
AM
15377 case R_PPC64_PLT16_LO:
15378 case R_PPC64_PLT16_LO_DS:
15379 if (unresolved_reloc)
15380 {
15381 unresolved_reloc = FALSE;
15382 goto nop_it;
15383 }
15384 /* Fall through. */
67f0cbdb
AM
15385 case R_PPC64_GOT_TLSLD16_LO:
15386 case R_PPC64_GOT_TLSGD16_LO:
15387 case R_PPC64_GOT_TPREL16_LO_DS:
15388 case R_PPC64_GOT_DTPREL16_LO_DS:
15389 case R_PPC64_GOT16_LO:
15390 case R_PPC64_GOT16_LO_DS:
15391 case R_PPC64_TOC16_LO:
15392 case R_PPC64_TOC16_LO_DS:
98528052 15393 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15394 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
15395 {
15396 bfd_byte *p = contents + (rel->r_offset & ~3);
15397 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
15398 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15399 {
15400 /* Transform addic to addi when we change reg. */
15401 insn &= ~((0x3f << 26) | (0x1f << 16));
15402 insn |= (14u << 26) | (2 << 16);
15403 }
15404 else
67f0cbdb 15405 {
98528052
AM
15406 insn &= ~(0x1f << 16);
15407 insn |= 2 << 16;
67f0cbdb 15408 }
560c8763 15409 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
15410 }
15411 break;
9a23f96e
AM
15412
15413 case R_PPC64_TPREL16_HA:
15414 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15415 {
15416 bfd_byte *p = contents + (rel->r_offset & ~3);
15417 insn = bfd_get_32 (input_bfd, p);
15418 if ((insn & ((0x3f << 26) | 0x1f << 16))
15419 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15420 /* xgettext:c-format */
15421 info->callbacks->minfo
15422 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
15423 input_bfd, input_section, rel->r_offset,
15424 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 15425 else
d830549d
AM
15426 {
15427 bfd_put_32 (input_bfd, NOP, p);
15428 goto copy_reloc;
15429 }
9a23f96e
AM
15430 }
15431 break;
15432
15433 case R_PPC64_TPREL16_LO:
15434 case R_PPC64_TPREL16_LO_DS:
15435 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15436 {
15437 bfd_byte *p = contents + (rel->r_offset & ~3);
15438 insn = bfd_get_32 (input_bfd, p);
15439 insn &= ~(0x1f << 16);
15440 insn |= 13 << 16;
15441 bfd_put_32 (input_bfd, insn, p);
15442 }
15443 break;
67f0cbdb
AM
15444 }
15445
65f38f15
AM
15446 /* Do any further special processing. */
15447 switch (r_type)
15448 {
15449 default:
15450 break;
15451
25f23106 15452 case R_PPC64_REL16_HA:
4a969973
AM
15453 case R_PPC64_REL16_HIGHA:
15454 case R_PPC64_REL16_HIGHERA:
15455 case R_PPC64_REL16_HIGHESTA:
a680de9a 15456 case R_PPC64_REL16DX_HA:
f9c6b907
AM
15457 case R_PPC64_ADDR16_HA:
15458 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15459 case R_PPC64_ADDR16_HIGHERA:
15460 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
15461 case R_PPC64_TOC16_HA:
15462 case R_PPC64_SECTOFF_HA:
411e1bfb 15463 case R_PPC64_TPREL16_HA:
f9c6b907 15464 case R_PPC64_TPREL16_HIGHA:
411e1bfb 15465 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 15466 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
15467 case R_PPC64_DTPREL16_HA:
15468 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 15469 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 15470 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
15471 /* It's just possible that this symbol is a weak symbol
15472 that's not actually defined anywhere. In that case,
15473 'sec' would be NULL, and we should leave the symbol
15474 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
15475 if (sec == NULL)
15476 break;
1a0670f3 15477 /* Fall through. */
5c5f6e17
AM
15478
15479 case R_PPC64_GOT16_HA:
15480 case R_PPC64_PLTGOT16_HA:
15481 case R_PPC64_PLT16_HA:
15482 case R_PPC64_GOT_TLSGD16_HA:
15483 case R_PPC64_GOT_TLSLD16_HA:
15484 case R_PPC64_GOT_TPREL16_HA:
15485 case R_PPC64_GOT_DTPREL16_HA:
15486 /* Add 0x10000 if sign bit in 0:15 is set.
15487 Bits 0:15 are not used. */
15488 addend += 0x8000;
65f38f15
AM
15489 break;
15490
15491 case R_PPC64_ADDR16_DS:
15492 case R_PPC64_ADDR16_LO_DS:
15493 case R_PPC64_GOT16_DS:
15494 case R_PPC64_GOT16_LO_DS:
15495 case R_PPC64_PLT16_LO_DS:
15496 case R_PPC64_SECTOFF_DS:
15497 case R_PPC64_SECTOFF_LO_DS:
15498 case R_PPC64_TOC16_DS:
15499 case R_PPC64_TOC16_LO_DS:
15500 case R_PPC64_PLTGOT16_DS:
15501 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
15502 case R_PPC64_GOT_TPREL16_DS:
15503 case R_PPC64_GOT_TPREL16_LO_DS:
15504 case R_PPC64_GOT_DTPREL16_DS:
15505 case R_PPC64_GOT_DTPREL16_LO_DS:
15506 case R_PPC64_TPREL16_DS:
15507 case R_PPC64_TPREL16_LO_DS:
15508 case R_PPC64_DTPREL16_DS:
15509 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
15510 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15511 mask = 3;
a680de9a
PB
15512 /* If this reloc is against an lq, lxv, or stxv insn, then
15513 the value must be a multiple of 16. This is somewhat of
15514 a hack, but the "correct" way to do this by defining _DQ
15515 forms of all the _DS relocs bloats all reloc switches in
15516 this file. It doesn't make much sense to use these
15517 relocs in data, so testing the insn should be safe. */
15518 if ((insn & (0x3f << 26)) == (56u << 26)
15519 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 15520 mask = 15;
a680de9a
PB
15521 relocation += addend;
15522 addend = insn & (mask ^ 3);
15523 if ((relocation & mask) != 0)
65f38f15 15524 {
a680de9a 15525 relocation ^= relocation & mask;
25f53a85 15526 info->callbacks->einfo
695344c0 15527 /* xgettext:c-format */
174d0a74 15528 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 15529 input_bfd, input_section, rel->r_offset,
d830549d 15530 ppc64_elf_howto_table[r_type]->name,
adadcc0c 15531 mask + 1);
65f38f15 15532 bfd_set_error (bfd_error_bad_value);
b34976b6 15533 ret = FALSE;
c316a17c 15534 goto copy_reloc;
65f38f15
AM
15535 }
15536 break;
5bd4f169
AM
15537 }
15538
239e1f3a
AM
15539 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15540 because such sections are not SEC_ALLOC and thus ld.so will
15541 not process them. */
d830549d 15542 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 15543 if (unresolved_reloc
239e1f3a 15544 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
15545 && h->elf.def_dynamic)
15546 && _bfd_elf_section_offset (output_bfd, info, input_section,
15547 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 15548 {
25f53a85 15549 info->callbacks->einfo
695344c0 15550 /* xgettext:c-format */
c1c8c1ef 15551 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 15552 input_bfd, input_section, rel->r_offset,
b80eed39 15553 howto->name,
039b3fef 15554 h->elf.root.root.string);
b34976b6 15555 ret = FALSE;
9c07fe7c 15556 }
5bd4f169 15557
b80eed39
AM
15558 /* 16-bit fields in insns mostly have signed values, but a
15559 few insns have 16-bit unsigned values. Really, we should
15560 have different reloc types. */
15561 if (howto->complain_on_overflow != complain_overflow_dont
15562 && howto->dst_mask == 0xffff
15563 && (input_section->flags & SEC_CODE) != 0)
15564 {
15565 enum complain_overflow complain = complain_overflow_signed;
15566
15567 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
15568 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15569 complain = complain_overflow_bitfield;
15570 else if (howto->rightshift == 0
15571 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15572 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15573 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15574 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15575 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15576 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
15577 complain = complain_overflow_unsigned;
15578 if (howto->complain_on_overflow != complain)
15579 {
15580 alt_howto = *howto;
15581 alt_howto.complain_on_overflow = complain;
15582 howto = &alt_howto;
15583 }
15584 }
15585
a680de9a
PB
15586 if (r_type == R_PPC64_REL16DX_HA)
15587 {
15588 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15589 if (rel->r_offset + 4 > input_section->size)
15590 r = bfd_reloc_outofrange;
15591 else
15592 {
15593 relocation += addend;
15594 relocation -= (rel->r_offset
15595 + input_section->output_offset
15596 + input_section->output_section->vma);
3de43e7b 15597 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
15598 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15599 insn &= ~0x1fffc1;
3de43e7b 15600 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
15601 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15602 r = bfd_reloc_ok;
3de43e7b 15603 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
15604 r = bfd_reloc_overflow;
15605 }
15606 }
15607 else
15608 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15609 rel->r_offset, relocation, addend);
5bd4f169 15610
ef60b7ff 15611 if (r != bfd_reloc_ok)
5bd4f169 15612 {
bc30df16 15613 char *more_info = NULL;
b80eed39 15614 const char *reloc_name = howto->name;
bc30df16
AM
15615
15616 if (reloc_dest != DEST_NORMAL)
15617 {
15618 more_info = bfd_malloc (strlen (reloc_name) + 8);
15619 if (more_info != NULL)
15620 {
15621 strcpy (more_info, reloc_name);
15622 strcat (more_info, (reloc_dest == DEST_OPD
15623 ? " (OPD)" : " (stub)"));
15624 reloc_name = more_info;
15625 }
15626 }
15627
cd27b276 15628 if (r == bfd_reloc_overflow)
5bd4f169 15629 {
8131c122
AM
15630 /* On code like "if (foo) foo();" don't report overflow
15631 on a branch to zero when foo is undefined. */
15632 if (!warned
15633 && (reloc_dest == DEST_STUB
15634 || !(h != NULL
15635 && (h->elf.root.type == bfd_link_hash_undefweak
15636 || h->elf.root.type == bfd_link_hash_undefined)
15637 && is_branch_reloc (r_type))))
1a72702b
AM
15638 info->callbacks->reloc_overflow (info, &h->elf.root,
15639 sym_name, reloc_name,
15640 orig_rel.r_addend,
15641 input_bfd, input_section,
15642 rel->r_offset);
ef60b7ff
AM
15643 }
15644 else
15645 {
25f53a85 15646 info->callbacks->einfo
695344c0 15647 /* xgettext:c-format */
c1c8c1ef 15648 (_("%H: %s against `%pT': error %d\n"),
25f53a85 15649 input_bfd, input_section, rel->r_offset,
bc30df16 15650 reloc_name, sym_name, (int) r);
b34976b6 15651 ret = FALSE;
ef60b7ff 15652 }
bc30df16
AM
15653 if (more_info != NULL)
15654 free (more_info);
5bd4f169 15655 }
c316a17c
AM
15656 copy_reloc:
15657 if (wrel != rel)
15658 *wrel = *rel;
15659 }
15660
15661 if (wrel != rel)
15662 {
15663 Elf_Internal_Shdr *rel_hdr;
15664 size_t deleted = rel - wrel;
15665
15666 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15667 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15668 if (rel_hdr->sh_size == 0)
15669 {
15670 /* It is too late to remove an empty reloc section. Leave
15671 one NONE reloc.
15672 ??? What is wrong with an empty section??? */
15673 rel_hdr->sh_size = rel_hdr->sh_entsize;
15674 deleted -= 1;
15675 }
15676 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15677 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15678 input_section->reloc_count -= deleted;
5bd4f169
AM
15679 }
15680
645ea6a9
AM
15681 /* If we're emitting relocations, then shortly after this function
15682 returns, reloc offsets and addends for this section will be
15683 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15684 file rather than the input. Save a copy of the relocs for
15685 opd_entry_value. */
0e1862bb 15686 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15687 {
15688 bfd_size_type amt;
15689 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15690 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
15691 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15692 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
15693 if (rel == NULL)
15694 return FALSE;
15695 memcpy (rel, relocs, amt);
15696 }
5bd4f169
AM
15697 return ret;
15698}
15699
754021d0
AM
15700/* Adjust the value of any local symbols in opd sections. */
15701
6e0b88f1 15702static int
754021d0
AM
15703ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15704 const char *name ATTRIBUTE_UNUSED,
15705 Elf_Internal_Sym *elfsym,
15706 asection *input_sec,
15707 struct elf_link_hash_entry *h)
15708{
74f0fb50
AM
15709 struct _opd_sec_data *opd;
15710 long adjust;
754021d0
AM
15711 bfd_vma value;
15712
4025353c 15713 if (h != NULL)
6e0b88f1 15714 return 1;
4025353c 15715
74f0fb50
AM
15716 opd = get_opd_info (input_sec);
15717 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 15718 return 1;
754021d0
AM
15719
15720 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 15721 if (!bfd_link_relocatable (info))
754021d0
AM
15722 value -= input_sec->output_section->vma;
15723
51aecdc5 15724 adjust = opd->adjust[OPD_NDX (value)];
4025353c 15725 if (adjust == -1)
6e0b88f1
AM
15726 return 2;
15727
15728 elfsym->st_value += adjust;
15729 return 1;
754021d0
AM
15730}
15731
5bd4f169
AM
15732/* Finish up dynamic symbol handling. We set the contents of various
15733 dynamic sections here. */
15734
b34976b6 15735static bfd_boolean
4ce794b7
AM
15736ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15737 struct bfd_link_info *info,
15738 struct elf_link_hash_entry *h,
4aef7643 15739 Elf_Internal_Sym *sym)
5bd4f169 15740{
65f38f15 15741 struct ppc_link_hash_table *htab;
8387904d 15742 struct plt_entry *ent;
5bd4f169 15743
65f38f15 15744 htab = ppc_hash_table (info);
4dfe6ac6
NC
15745 if (htab == NULL)
15746 return FALSE;
5bd4f169 15747
49c09209
AM
15748 if (!htab->opd_abi && !h->def_regular)
15749 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15750 if (ent->plt.offset != (bfd_vma) -1)
15751 {
15752 /* Mark the symbol as undefined, rather than as
15753 defined in glink. Leave the value if there were
15754 any relocations where pointer equality matters
15755 (this is a clue for the dynamic linker, to make
15756 function pointer comparisons work between an
15757 application and shared library), otherwise set it
15758 to zero. */
15759 sym->st_shndx = SHN_UNDEF;
15760 if (!h->pointer_equality_needed)
15761 sym->st_value = 0;
15762 else if (!h->ref_regular_nonweak)
15763 {
15764 /* This breaks function pointer comparisons, but
15765 that is better than breaking tests for a NULL
15766 function pointer. */
15767 sym->st_value = 0;
15768 }
15769 break;
15770 }
5bd4f169 15771
f5385ebf 15772 if (h->needs_copy)
5bd4f169 15773 {
65f38f15 15774 /* This symbol needs a copy reloc. Set it up. */
49c09209 15775 Elf_Internal_Rela rela;
5474d94f 15776 asection *srel;
49c09209 15777 bfd_byte *loc;
5bd4f169 15778
65f38f15
AM
15779 if (h->dynindx == -1
15780 || (h->root.type != bfd_link_hash_defined
15781 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
15782 || htab->elf.srelbss == NULL
15783 || htab->elf.sreldynrelro == NULL)
65f38f15 15784 abort ();
5bd4f169
AM
15785
15786 rela.r_offset = (h->root.u.def.value
15787 + h->root.u.def.section->output_section->vma
15788 + h->root.u.def.section->output_offset);
15789 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15790 rela.r_addend = 0;
afbf7e8e 15791 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
15792 srel = htab->elf.sreldynrelro;
15793 else
15794 srel = htab->elf.srelbss;
15795 loc = srel->contents;
15796 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 15797 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
15798 }
15799
b34976b6 15800 return TRUE;
5bd4f169
AM
15801}
15802
65f38f15
AM
15803/* Used to decide how to sort relocs in an optimal manner for the
15804 dynamic linker, before writing them out. */
15805
15806static enum elf_reloc_type_class
7e612e98
AM
15807ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15808 const asection *rel_sec,
15809 const Elf_Internal_Rela *rela)
65f38f15 15810{
04c9666a 15811 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
15812 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15813
33e44f2e 15814 if (rel_sec == htab->elf.irelplt)
7e612e98 15815 return reloc_class_ifunc;
a33d1f77 15816
4ce794b7 15817 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 15818 switch (r_type)
65f38f15
AM
15819 {
15820 case R_PPC64_RELATIVE:
15821 return reloc_class_relative;
15822 case R_PPC64_JMP_SLOT:
15823 return reloc_class_plt;
15824 case R_PPC64_COPY:
15825 return reloc_class_copy;
15826 default:
15827 return reloc_class_normal;
15828 }
15829}
15830
5bd4f169
AM
15831/* Finish up the dynamic sections. */
15832
b34976b6 15833static bfd_boolean
4ce794b7
AM
15834ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15835 struct bfd_link_info *info)
5bd4f169 15836{
65f38f15
AM
15837 struct ppc_link_hash_table *htab;
15838 bfd *dynobj;
5bd4f169 15839 asection *sdyn;
5bd4f169 15840
65f38f15 15841 htab = ppc_hash_table (info);
4dfe6ac6
NC
15842 if (htab == NULL)
15843 return FALSE;
15844
65f38f15 15845 dynobj = htab->elf.dynobj;
3d4d4302 15846 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 15847
65f38f15 15848 if (htab->elf.dynamic_sections_created)
5bd4f169 15849 {
5bd4f169
AM
15850 Elf64_External_Dyn *dyncon, *dynconend;
15851
33e44f2e 15852 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 15853 abort ();
5bd4f169
AM
15854
15855 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 15856 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
15857 for (; dyncon < dynconend; dyncon++)
15858 {
15859 Elf_Internal_Dyn dyn;
19397422 15860 asection *s;
5bd4f169
AM
15861
15862 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15863
15864 switch (dyn.d_tag)
15865 {
65f38f15
AM
15866 default:
15867 continue;
5bd4f169 15868
5d1634d7 15869 case DT_PPC64_GLINK:
4ce794b7 15870 s = htab->glink;
6348e046 15871 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
15872 /* We stupidly defined DT_PPC64_GLINK to be the start
15873 of glink rather than the first entry point, which is
15874 what ld.so needs, and now have a bigger stub to
15875 support automatic multiple TOCs. */
9e390558 15876 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
15877 break;
15878
19397422
AM
15879 case DT_PPC64_OPD:
15880 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15881 if (s == NULL)
15882 continue;
15883 dyn.d_un.d_ptr = s->vma;
19397422
AM
15884 break;
15885
e8910a83
AM
15886 case DT_PPC64_OPT:
15887 if (htab->do_multi_toc && htab->multi_toc_needed)
15888 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
15889 if (htab->has_plt_localentry0)
15890 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
15891 break;
15892
19397422
AM
15893 case DT_PPC64_OPDSZ:
15894 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15895 if (s == NULL)
15896 continue;
eea6121a 15897 dyn.d_un.d_val = s->size;
19397422
AM
15898 break;
15899
65f38f15 15900 case DT_PLTGOT:
33e44f2e 15901 s = htab->elf.splt;
6348e046 15902 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
15903 break;
15904
15905 case DT_JMPREL:
33e44f2e 15906 s = htab->elf.srelplt;
6348e046 15907 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 15908 break;
5bd4f169 15909
65f38f15 15910 case DT_PLTRELSZ:
33e44f2e 15911 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 15912 break;
82e66161
AM
15913
15914 case DT_TEXTREL:
15915 if (htab->local_ifunc_resolver)
15916 info->callbacks->einfo
15917 (_("%X%P: text relocations and GNU indirect "
15918 "functions will result in a segfault at runtime\n"));
15919 else if (htab->maybe_local_ifunc_resolver)
15920 info->callbacks->einfo
15921 (_("%P: warning: text relocations and GNU indirect "
15922 "functions may result in a segfault at runtime\n"));
15923 continue;
5bd4f169 15924 }
5bd4f169 15925
65f38f15 15926 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 15927 }
5bd4f169
AM
15928 }
15929
6528b6eb
AM
15930 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15931 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15932 {
15933 /* Fill in the first entry in the global offset table.
15934 We use it to hold the link-time TOCbase. */
15935 bfd_put_64 (output_bfd,
60ee0d4a 15936 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 15937 htab->elf.sgot->contents);
5d1634d7
AM
15938
15939 /* Set .got entry size. */
2cdcc330
AM
15940 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
15941 = 8;
5d1634d7
AM
15942 }
15943
6528b6eb
AM
15944 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15945 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15946 {
15947 /* Set .plt entry size. */
33e44f2e 15948 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 15949 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
15950 }
15951
84f5d08e
AM
15952 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15953 brlt ourselves if emitrelocations. */
15954 if (htab->brlt != NULL
15955 && htab->brlt->reloc_count != 0
15956 && !_bfd_elf_link_output_relocs (output_bfd,
15957 htab->brlt,
d4730f92 15958 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
15959 elf_section_data (htab->brlt)->relocs,
15960 NULL))
15961 return FALSE;
15962
176a0d42
AM
15963 if (htab->glink != NULL
15964 && htab->glink->reloc_count != 0
15965 && !_bfd_elf_link_output_relocs (output_bfd,
15966 htab->glink,
d4730f92 15967 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
15968 elf_section_data (htab->glink)->relocs,
15969 NULL))
15970 return FALSE;
15971
da44f4e5 15972
df136d64
AM
15973 if (htab->glink_eh_frame != NULL
15974 && htab->glink_eh_frame->size != 0
15975 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15976 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15977 htab->glink_eh_frame,
15978 htab->glink_eh_frame->contents))
15979 return FALSE;
58d180e8 15980
e717da7e 15981 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
15982 since we didn't add them to DYNOBJ. We know dynobj is the first
15983 bfd. */
c72f2fb2 15984 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
15985 {
15986 asection *s;
7b53ace3 15987
0c8d6e5c 15988 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
15989 continue;
15990
e717da7e
AM
15991 s = ppc64_elf_tdata (dynobj)->got;
15992 if (s != NULL
eea6121a 15993 && s->size != 0
e717da7e
AM
15994 && s->output_section != bfd_abs_section_ptr
15995 && !bfd_set_section_contents (output_bfd, s->output_section,
15996 s->contents, s->output_offset,
eea6121a 15997 s->size))
e717da7e
AM
15998 return FALSE;
15999 s = ppc64_elf_tdata (dynobj)->relgot;
16000 if (s != NULL
eea6121a 16001 && s->size != 0
e717da7e
AM
16002 && s->output_section != bfd_abs_section_ptr
16003 && !bfd_set_section_contents (output_bfd, s->output_section,
16004 s->contents, s->output_offset,
eea6121a 16005 s->size))
e717da7e
AM
16006 return FALSE;
16007 }
f6c52c13 16008
b34976b6 16009 return TRUE;
5bd4f169
AM
16010}
16011
5bd4f169 16012#include "elf64-target.h"
7b8e7dad
AM
16013
16014/* FreeBSD support */
16015
16016#undef TARGET_LITTLE_SYM
16017#undef TARGET_LITTLE_NAME
16018
16019#undef TARGET_BIG_SYM
6d00b590 16020#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
16021#undef TARGET_BIG_NAME
16022#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16023
16024#undef ELF_OSABI
16025#define ELF_OSABI ELFOSABI_FREEBSD
16026
16027#undef elf64_bed
16028#define elf64_bed elf64_powerpc_fbsd_bed
16029
16030#include "elf64-target.h"
This page took 2.625295 seconds and 4 git commands to generate.