]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
PowerPC GOT_PCREL34 optimisation
[thirdparty/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 **);
5663e321
AM
54static bfd_reloc_status_type ppc64_elf_prefix_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 56static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 58static bfd_vma opd_entry_value
aef36ac1 59 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
5bd4f169 60
6d00b590 61#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
ad8e1ba5 62#define TARGET_LITTLE_NAME "elf64-powerpcle"
6d00b590 63#define TARGET_BIG_SYM powerpc_elf64_vec
ad8e1ba5
AM
64#define TARGET_BIG_NAME "elf64-powerpc"
65#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 66#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
67#define ELF_MACHINE_CODE EM_PPC64
68#define ELF_MAXPAGESIZE 0x10000
702d1671
AM
69#define ELF_COMMONPAGESIZE 0x1000
70#define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
ad8e1ba5
AM
71#define elf_info_to_howto ppc64_elf_info_to_howto
72
73#define elf_backend_want_got_sym 0
74#define elf_backend_want_plt_sym 0
75#define elf_backend_plt_alignment 3
76#define elf_backend_plt_not_loaded 1
ad8e1ba5 77#define elf_backend_got_header_size 8
5474d94f 78#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
79#define elf_backend_can_gc_sections 1
80#define elf_backend_can_refcount 1
81#define elf_backend_rela_normal 1
64f52338 82#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 83#define elf_backend_default_execstack 0
ad8e1ba5 84
e717da7e 85#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 86#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 87#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
88#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
89#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
90#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
91#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 92#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 93#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 94#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
95
96#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
97#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
98#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 99#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 100#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 101#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 102#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 103#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 104#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 105#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 106#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 107#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 108#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 109#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 110#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
111#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
112#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 113#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
ad8e1ba5
AM
114#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
115#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 116#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 117#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 118#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
119#define elf_backend_relocate_section ppc64_elf_relocate_section
120#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
121#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
122#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 123#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 124#define elf_backend_special_sections ppc64_elf_special_sections
6911b7dc 125#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 126#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 127#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 128
5bd4f169
AM
129/* The name of the dynamic interpreter. This is put in the .interp
130 section. */
131#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
132
133/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 134#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
2d7ad24e 135#define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
5bd4f169
AM
136
137/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 138#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 139
a078d95a
AM
140/* Offsets to some stack save slots. */
141#define STK_LR 16
142#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 143/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
144 CR save slot. Used only by optimised __tls_get_addr call stub,
145 relying on __tls_get_addr_opt not saving CR.. */
146#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
147
5bd4f169 148/* TOC base pointers offset from start of TOC. */
411e1bfb 149#define TOC_BASE_OFF 0x8000
a27e685f
AM
150/* TOC base alignment. */
151#define TOC_BASE_ALIGN 256
411e1bfb
AM
152
153/* Offset of tp and dtp pointers from start of TLS block. */
154#define TP_OFFSET 0x7000
155#define DTP_OFFSET 0x8000
5bd4f169 156
ad8e1ba5
AM
157/* .plt call stub instructions. The normal stub is like this, but
158 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 159 insert an addi to adjust r11. */
a078d95a 160#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
161#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
162#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
163#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
164#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
165#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
166#define BCTR 0x4e800420 /* bctr */
167
07d6d2b8 168#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
05d0e962
AM
169#define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
170#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
07d6d2b8
AM
171#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
172#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
173
174#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
175#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
176#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
177#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
178#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
179#define BNECTR 0x4ca20420 /* bnectr+ */
180#define BNECTR_P4 0x4ce20420 /* bnectr+ */
794e51c0 181
71a39c98 182#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 183#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 184#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 185
07d6d2b8
AM
186#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
187#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
188#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 189
07d6d2b8 190#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
05d0e962 191#define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
006589cf
AM
192#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
193#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
05d0e962 194#define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
a345bc8d 195#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
05d0e962
AM
196#define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
197#define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
07d6d2b8 198#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
05d0e962
AM
199#define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
200#define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
201#define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
5663e321 202#define PNOP 0x0700000000000000ULL
a345bc8d 203
9e390558
AM
204/* __glink_PLTresolve stub instructions. We enter with the index in R0. */
205#define GLINK_PLTRESOLVE_SIZE(htab) \
407aa07c 206 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
ee4bf8d2
AM
207 /* 0: */
208 /* .quad plt0-1f */
209 /* __glink: */
210#define MFLR_R12 0x7d8802a6 /* mflr %12 */
211#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
212 /* 1: */
213#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 214 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 215#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
216#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
217 /* ld %12,0(%11) */
218 /* ld %2,8(%11) */
219 /* mtctr %12 */
220 /* ld %11,16(%11) */
ee4bf8d2 221 /* bctr */
b9e5796b
AM
222#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
223#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
224#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
225#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
226#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
227
228/* Pad with this. */
229#define NOP 0x60000000
230
721956f4
AM
231/* Some other nops. */
232#define CROR_151515 0x4def7b82
233#define CROR_313131 0x4ffffb82
234
cedb70c5 235/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
236#define LI_R0_0 0x38000000 /* li %r0,0 */
237#define B_DOT 0x48000000 /* b . */
238
239/* After that, we need two instructions to load the index, followed by
240 a branch. */
241#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 242#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 243
deb0e272
AM
244/* Instructions used by the save and restore reg functions. */
245#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
246#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
247#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
248#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
249#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
250#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
251#define LI_R12_0 0x39800000 /* li %r12,0 */
252#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
253#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
254#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
255#define BLR 0x4e800020 /* blr */
256
41bd81ab
AM
257/* Since .opd is an array of descriptors and each entry will end up
258 with identical R_PPC64_RELATIVE relocs, there is really no need to
259 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 260 relocate .opd without reloc entries. */
41bd81ab
AM
261#ifndef NO_OPD_RELOCS
262#define NO_OPD_RELOCS 0
263#endif
810d4e75 264
a4b6fadd
AM
265#ifndef ARRAY_SIZE
266#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
267#endif
268
810d4e75
AM
269static inline int
270abiversion (bfd *abfd)
271{
272 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
273}
274
275static inline void
276set_abiversion (bfd *abfd, int ver)
277{
278 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
279 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
280}
5bd4f169
AM
281\f
282/* Relocation HOWTO's. */
46807bf4
AM
283/* Like other ELF RELA targets that don't apply multiple
284 field-altering relocations to the same localation, src_mask is
285 always zero and pcrel_offset is the same as pc_relative.
286 PowerPC can always use a zero bitpos, even when the field is not at
287 the LSB. For example, a REL24 could use rightshift=2, bisize=24
288 and bitpos=2 which matches the ABI description, or as we do here,
289 rightshift=0, bitsize=26 and bitpos=0. */
290#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
291 complain, special_func) \
292 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
293 complain_overflow_ ## complain, special_func, \
294 #type, FALSE, 0, mask, pc_relative)
295
04c9666a 296static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 297
f3185997
NC
298static reloc_howto_type ppc64_elf_howto_raw[] =
299{
5bd4f169 300 /* This reloc does nothing. */
46807bf4
AM
301 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
302 bfd_elf_generic_reloc),
5bd4f169
AM
303
304 /* A standard 32 bit relocation. */
46807bf4
AM
305 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
306 bfd_elf_generic_reloc),
5bd4f169
AM
307
308 /* An absolute 26 bit branch; the lower two bits must be zero.
309 FIXME: we don't check that, we just clear them. */
46807bf4
AM
310 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
311 bfd_elf_generic_reloc),
5bd4f169
AM
312
313 /* A standard 16 bit relocation. */
46807bf4
AM
314 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
315 bfd_elf_generic_reloc),
5bd4f169
AM
316
317 /* A 16 bit relocation without overflow. */
46807bf4
AM
318 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
319 bfd_elf_generic_reloc),
5bd4f169
AM
320
321 /* Bits 16-31 of an address. */
46807bf4
AM
322 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
323 bfd_elf_generic_reloc),
5bd4f169
AM
324
325 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
326 bits, treated as a signed number, is negative. */
46807bf4
AM
327 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
328 ppc64_elf_ha_reloc),
5bd4f169
AM
329
330 /* An absolute 16 bit branch; the lower two bits must be zero.
331 FIXME: we don't check that, we just clear them. */
46807bf4
AM
332 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
333 ppc64_elf_branch_reloc),
5bd4f169
AM
334
335 /* An absolute 16 bit branch, for which bit 10 should be set to
336 indicate that the branch is expected to be taken. The lower two
337 bits must be zero. */
46807bf4
AM
338 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
339 ppc64_elf_brtaken_reloc),
5bd4f169
AM
340
341 /* An absolute 16 bit branch, for which bit 10 should be set to
342 indicate that the branch is not expected to be taken. The lower
343 two bits must be zero. */
46807bf4
AM
344 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
345 ppc64_elf_brtaken_reloc),
5bd4f169
AM
346
347 /* A relative 26 bit branch; the lower two bits must be zero. */
46807bf4
AM
348 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
349 ppc64_elf_branch_reloc),
5bd4f169 350
05d0e962 351 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
46807bf4
AM
352 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
353 ppc64_elf_branch_reloc),
05d0e962 354
5bd4f169 355 /* A relative 16 bit branch; the lower two bits must be zero. */
46807bf4
AM
356 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
357 ppc64_elf_branch_reloc),
5bd4f169
AM
358
359 /* A relative 16 bit branch. Bit 10 should be set to indicate that
360 the branch is expected to be taken. The lower two bits must be
361 zero. */
46807bf4
AM
362 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
363 ppc64_elf_brtaken_reloc),
5bd4f169
AM
364
365 /* A relative 16 bit branch. Bit 10 should be set to indicate that
366 the branch is not expected to be taken. The lower two bits must
367 be zero. */
46807bf4
AM
368 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
369 ppc64_elf_brtaken_reloc),
5bd4f169
AM
370
371 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
372 symbol. */
46807bf4
AM
373 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
374 ppc64_elf_unhandled_reloc),
5bd4f169
AM
375
376 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
377 the symbol. */
46807bf4
AM
378 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
379 ppc64_elf_unhandled_reloc),
5bd4f169
AM
380
381 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
382 the symbol. */
46807bf4
AM
383 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
384 ppc64_elf_unhandled_reloc),
5bd4f169
AM
385
386 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
387 the symbol. */
46807bf4
AM
388 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
389 ppc64_elf_unhandled_reloc),
5bd4f169
AM
390
391 /* This is used only by the dynamic linker. The symbol should exist
392 both in the object being run and in some shared library. The
393 dynamic linker copies the data addressed by the symbol from the
394 shared library into the object, because the object being
395 run has to have the data at some particular address. */
46807bf4
AM
396 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
397 ppc64_elf_unhandled_reloc),
5bd4f169
AM
398
399 /* Like R_PPC64_ADDR64, but used when setting global offset table
400 entries. */
46807bf4
AM
401 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
402 ppc64_elf_unhandled_reloc),
5bd4f169
AM
403
404 /* Created by the link editor. Marks a procedure linkage table
405 entry for a symbol. */
46807bf4
AM
406 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
407 ppc64_elf_unhandled_reloc),
5bd4f169
AM
408
409 /* Used only by the dynamic linker. When the object is run, this
410 doubleword64 is set to the load address of the object, plus the
411 addend. */
46807bf4
AM
412 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
413 bfd_elf_generic_reloc),
5bd4f169
AM
414
415 /* Like R_PPC64_ADDR32, but may be unaligned. */
46807bf4
AM
416 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
417 bfd_elf_generic_reloc),
5bd4f169
AM
418
419 /* Like R_PPC64_ADDR16, but may be unaligned. */
46807bf4
AM
420 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
421 bfd_elf_generic_reloc),
5bd4f169
AM
422
423 /* 32-bit PC relative. */
46807bf4
AM
424 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
425 bfd_elf_generic_reloc),
5bd4f169 426
10ed1bba 427 /* 32-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
428 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
429 ppc64_elf_unhandled_reloc),
5bd4f169
AM
430
431 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
432 FIXME: R_PPC64_PLTREL32 not supported. */
46807bf4
AM
433 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
434 ppc64_elf_unhandled_reloc),
5bd4f169
AM
435
436 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
437 the symbol. */
46807bf4
AM
438 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
439 ppc64_elf_unhandled_reloc),
5bd4f169
AM
440
441 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
442 the symbol. */
46807bf4
AM
443 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
444 ppc64_elf_unhandled_reloc),
5bd4f169
AM
445
446 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
447 the symbol. */
46807bf4
AM
448 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
449 ppc64_elf_unhandled_reloc),
5bd4f169 450
c061c2d8 451 /* 16-bit section relative relocation. */
46807bf4
AM
452 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
453 ppc64_elf_sectoff_reloc),
5bd4f169 454
c061c2d8 455 /* Like R_PPC64_SECTOFF, but no overflow warning. */
46807bf4
AM
456 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
457 ppc64_elf_sectoff_reloc),
5bd4f169
AM
458
459 /* 16-bit upper half section relative relocation. */
46807bf4
AM
460 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
461 ppc64_elf_sectoff_reloc),
5bd4f169
AM
462
463 /* 16-bit upper half adjusted section relative relocation. */
46807bf4
AM
464 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
465 ppc64_elf_sectoff_ha_reloc),
5bd4f169 466
04c9666a 467 /* Like R_PPC64_REL24 without touching the two least significant bits. */
46807bf4
AM
468 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
469 bfd_elf_generic_reloc),
5bd4f169
AM
470
471 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
472
473 /* A standard 64-bit relocation. */
46807bf4
AM
474 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
475 bfd_elf_generic_reloc),
5bd4f169
AM
476
477 /* The bits 32-47 of an address. */
46807bf4
AM
478 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
479 bfd_elf_generic_reloc),
5bd4f169
AM
480
481 /* The bits 32-47 of an address, plus 1 if the contents of the low
482 16 bits, treated as a signed number, is negative. */
46807bf4
AM
483 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
484 ppc64_elf_ha_reloc),
5bd4f169
AM
485
486 /* The bits 48-63 of an address. */
46807bf4
AM
487 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
488 bfd_elf_generic_reloc),
5bd4f169
AM
489
490 /* The bits 48-63 of an address, plus 1 if the contents of the low
491 16 bits, treated as a signed number, is negative. */
46807bf4
AM
492 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
493 ppc64_elf_ha_reloc),
5bd4f169
AM
494
495 /* Like ADDR64, but may be unaligned. */
46807bf4
AM
496 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
497 bfd_elf_generic_reloc),
5bd4f169
AM
498
499 /* 64-bit relative relocation. */
46807bf4
AM
500 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
501 bfd_elf_generic_reloc),
5bd4f169 502
cedb70c5 503 /* 64-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
504 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
505 ppc64_elf_unhandled_reloc),
5bd4f169
AM
506
507 /* 64-bit PC relative relocation to the symbol's procedure linkage
508 table. */
509 /* FIXME: R_PPC64_PLTREL64 not supported. */
46807bf4
AM
510 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
511 ppc64_elf_unhandled_reloc),
5bd4f169
AM
512
513 /* 16 bit TOC-relative relocation. */
5bd4f169 514 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
46807bf4
AM
515 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
516 ppc64_elf_toc_reloc),
5bd4f169
AM
517
518 /* 16 bit TOC-relative relocation without overflow. */
5bd4f169 519 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
46807bf4
AM
520 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
521 ppc64_elf_toc_reloc),
5bd4f169
AM
522
523 /* 16 bit TOC-relative relocation, high 16 bits. */
5bd4f169 524 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
46807bf4
AM
525 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
526 ppc64_elf_toc_reloc),
5bd4f169
AM
527
528 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
529 contents of the low 16 bits, treated as a signed number, is
530 negative. */
5bd4f169 531 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
46807bf4
AM
532 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
533 ppc64_elf_toc_ha_reloc),
5bd4f169
AM
534
535 /* 64-bit relocation; insert value of TOC base (.TOC.). */
5bd4f169 536 /* R_PPC64_TOC 51 doubleword64 .TOC. */
46807bf4
AM
537 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
538 ppc64_elf_toc64_reloc),
5bd4f169
AM
539
540 /* Like R_PPC64_GOT16, but also informs the link editor that the
541 value to relocate may (!) refer to a PLT entry which the link
542 editor (a) may replace with the symbol value. If the link editor
543 is unable to fully resolve the symbol, it may (b) create a PLT
544 entry and store the address to the new PLT entry in the GOT.
545 This permits lazy resolution of function symbols at run time.
546 The link editor may also skip all of this and just (c) emit a
547 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
548 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
46807bf4
AM
549 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
550 ppc64_elf_unhandled_reloc),
411e1bfb
AM
551
552 /* Like R_PPC64_PLTGOT16, but without overflow. */
553 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
554 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
555 ppc64_elf_unhandled_reloc),
411e1bfb
AM
556
557 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
558 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
46807bf4
AM
559 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
560 ppc64_elf_unhandled_reloc),
411e1bfb
AM
561
562 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
563 1 if the contents of the low 16 bits, treated as a signed number,
564 is negative. */
565 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
46807bf4
AM
566 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
567 ppc64_elf_unhandled_reloc),
411e1bfb
AM
568
569 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
46807bf4
AM
570 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
571 bfd_elf_generic_reloc),
411e1bfb
AM
572
573 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
46807bf4
AM
574 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
575 bfd_elf_generic_reloc),
411e1bfb
AM
576
577 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
46807bf4
AM
578 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
579 ppc64_elf_unhandled_reloc),
411e1bfb
AM
580
581 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
46807bf4
AM
582 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
583 ppc64_elf_unhandled_reloc),
411e1bfb
AM
584
585 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
46807bf4
AM
586 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
587 ppc64_elf_unhandled_reloc),
411e1bfb
AM
588
589 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
46807bf4
AM
590 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
591 ppc64_elf_sectoff_reloc),
411e1bfb
AM
592
593 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
46807bf4
AM
594 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
595 ppc64_elf_sectoff_reloc),
411e1bfb
AM
596
597 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
46807bf4
AM
598 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
599 ppc64_elf_toc_reloc),
411e1bfb
AM
600
601 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
46807bf4
AM
602 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
603 ppc64_elf_toc_reloc),
411e1bfb
AM
604
605 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
606 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
46807bf4
AM
607 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
608 ppc64_elf_unhandled_reloc),
411e1bfb
AM
609
610 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
611 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
612 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
613 ppc64_elf_unhandled_reloc),
411e1bfb 614
727fc41e 615 /* Marker relocs for TLS. */
46807bf4
AM
616 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
617 bfd_elf_generic_reloc),
618
619 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
620 bfd_elf_generic_reloc),
621
622 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
623 bfd_elf_generic_reloc),
727fc41e 624
23cedd1d
AM
625 /* Marker reloc for optimizing r2 save in prologue rather than on
626 each plt call stub. */
46807bf4
AM
627 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
628 bfd_elf_generic_reloc),
3b421ab3 629
23cedd1d 630 /* Marker relocs on inline plt call instructions. */
46807bf4
AM
631 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
632 bfd_elf_generic_reloc),
633
634 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
635 bfd_elf_generic_reloc),
23cedd1d 636
411e1bfb
AM
637 /* Computes the load module index of the load module that contains the
638 definition of its TLS sym. */
46807bf4
AM
639 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
640 ppc64_elf_unhandled_reloc),
411e1bfb
AM
641
642 /* Computes a dtv-relative displacement, the difference between the value
643 of sym+add and the base address of the thread-local storage block that
644 contains the definition of sym, minus 0x8000. */
46807bf4
AM
645 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
646 ppc64_elf_unhandled_reloc),
411e1bfb
AM
647
648 /* A 16 bit dtprel reloc. */
46807bf4
AM
649 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
650 ppc64_elf_unhandled_reloc),
411e1bfb
AM
651
652 /* Like DTPREL16, but no overflow. */
46807bf4
AM
653 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
654 ppc64_elf_unhandled_reloc),
411e1bfb
AM
655
656 /* Like DTPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
657 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
658 ppc64_elf_unhandled_reloc),
411e1bfb
AM
659
660 /* Like DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
661 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
662 ppc64_elf_unhandled_reloc),
411e1bfb
AM
663
664 /* Like DTPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
665 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
666 ppc64_elf_unhandled_reloc),
411e1bfb
AM
667
668 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
669 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
670 ppc64_elf_unhandled_reloc),
411e1bfb
AM
671
672 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
673 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
674 ppc64_elf_unhandled_reloc),
411e1bfb
AM
675
676 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
677 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
678 ppc64_elf_unhandled_reloc),
411e1bfb
AM
679
680 /* Like DTPREL16, but for insns with a DS field. */
46807bf4
AM
681 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
682 ppc64_elf_unhandled_reloc),
411e1bfb
AM
683
684 /* Like DTPREL16_DS, but no overflow. */
46807bf4
AM
685 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
686 ppc64_elf_unhandled_reloc),
411e1bfb
AM
687
688 /* Computes a tp-relative displacement, the difference between the value of
689 sym+add and the value of the thread pointer (r13). */
46807bf4
AM
690 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
691 ppc64_elf_unhandled_reloc),
411e1bfb
AM
692
693 /* A 16 bit tprel reloc. */
46807bf4
AM
694 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
695 ppc64_elf_unhandled_reloc),
411e1bfb
AM
696
697 /* Like TPREL16, but no overflow. */
46807bf4
AM
698 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
699 ppc64_elf_unhandled_reloc),
411e1bfb
AM
700
701 /* Like TPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
702 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
703 ppc64_elf_unhandled_reloc),
411e1bfb
AM
704
705 /* Like TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
706 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
707 ppc64_elf_unhandled_reloc),
411e1bfb
AM
708
709 /* Like TPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
710 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
711 ppc64_elf_unhandled_reloc),
411e1bfb
AM
712
713 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
714 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
715 ppc64_elf_unhandled_reloc),
411e1bfb
AM
716
717 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
718 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
719 ppc64_elf_unhandled_reloc),
411e1bfb
AM
720
721 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
722 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
723 ppc64_elf_unhandled_reloc),
411e1bfb
AM
724
725 /* Like TPREL16, but for insns with a DS field. */
46807bf4
AM
726 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
727 ppc64_elf_unhandled_reloc),
411e1bfb
AM
728
729 /* Like TPREL16_DS, but no overflow. */
46807bf4
AM
730 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
731 ppc64_elf_unhandled_reloc),
411e1bfb
AM
732
733 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
734 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
735 to the first entry relative to the TOC base (r2). */
46807bf4
AM
736 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
737 ppc64_elf_unhandled_reloc),
5bd4f169 738
411e1bfb 739 /* Like GOT_TLSGD16, but no overflow. */
46807bf4
AM
740 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
741 ppc64_elf_unhandled_reloc),
5bd4f169 742
411e1bfb 743 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
46807bf4
AM
744 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
745 ppc64_elf_unhandled_reloc),
5bd4f169 746
411e1bfb 747 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
46807bf4
AM
748 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
749 ppc64_elf_unhandled_reloc),
5bd4f169 750
411e1bfb
AM
751 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
752 with values (sym+add)@dtpmod and zero, and computes the offset to the
753 first entry relative to the TOC base (r2). */
46807bf4
AM
754 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
755 ppc64_elf_unhandled_reloc),
5bd4f169 756
411e1bfb 757 /* Like GOT_TLSLD16, but no overflow. */
46807bf4
AM
758 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
759 ppc64_elf_unhandled_reloc),
5bd4f169 760
411e1bfb 761 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
46807bf4
AM
762 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
763 ppc64_elf_unhandled_reloc),
5bd4f169 764
411e1bfb 765 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
46807bf4
AM
766 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
767 ppc64_elf_unhandled_reloc),
5bd4f169 768
411e1bfb
AM
769 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
770 the offset to the entry relative to the TOC base (r2). */
46807bf4
AM
771 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
772 ppc64_elf_unhandled_reloc),
5bd4f169 773
411e1bfb 774 /* Like GOT_DTPREL16_DS, but no overflow. */
46807bf4
AM
775 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
776 ppc64_elf_unhandled_reloc),
5bd4f169 777
411e1bfb 778 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
779 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
780 ppc64_elf_unhandled_reloc),
5bd4f169 781
411e1bfb 782 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
783 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
784 ppc64_elf_unhandled_reloc),
411e1bfb
AM
785
786 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
787 offset to the entry relative to the TOC base (r2). */
46807bf4
AM
788 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
789 ppc64_elf_unhandled_reloc),
5bd4f169 790
411e1bfb 791 /* Like GOT_TPREL16_DS, but no overflow. */
46807bf4
AM
792 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
793 ppc64_elf_unhandled_reloc),
5bd4f169 794
411e1bfb 795 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
796 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
797 ppc64_elf_unhandled_reloc),
5bd4f169 798
411e1bfb 799 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
800 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
801 ppc64_elf_unhandled_reloc),
802
803 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
804 ppc64_elf_unhandled_reloc),
805
806 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
807 bfd_elf_generic_reloc),
e054468f 808
25f23106 809 /* A 16 bit relative relocation. */
46807bf4
AM
810 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
811 bfd_elf_generic_reloc),
25f23106
AM
812
813 /* A 16 bit relative relocation without overflow. */
46807bf4
AM
814 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
815 bfd_elf_generic_reloc),
25f23106
AM
816
817 /* The high order 16 bits of a relative address. */
46807bf4
AM
818 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
819 bfd_elf_generic_reloc),
25f23106
AM
820
821 /* The high order 16 bits of a relative address, plus 1 if the contents of
822 the low 16 bits, treated as a signed number, is negative. */
46807bf4
AM
823 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
824 ppc64_elf_ha_reloc),
25f23106 825
4a969973
AM
826 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
827 bfd_elf_generic_reloc),
828
829 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
830 ppc64_elf_ha_reloc),
831
832 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
833 bfd_elf_generic_reloc),
834
835 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
836 ppc64_elf_ha_reloc),
837
838 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
839 bfd_elf_generic_reloc),
840
841 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
842 ppc64_elf_ha_reloc),
843
a680de9a 844 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
46807bf4
AM
845 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
846 ppc64_elf_ha_reloc),
a680de9a 847
7ba71655 848 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
46807bf4
AM
849 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
850 ppc64_elf_ha_reloc),
7ba71655 851
f9c6b907 852 /* Like R_PPC64_ADDR16_HI, but no overflow. */
46807bf4
AM
853 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
854 bfd_elf_generic_reloc),
f9c6b907
AM
855
856 /* Like R_PPC64_ADDR16_HA, but no overflow. */
46807bf4
AM
857 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
858 ppc64_elf_ha_reloc),
f9c6b907
AM
859
860 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
46807bf4
AM
861 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
862 ppc64_elf_unhandled_reloc),
f9c6b907
AM
863
864 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
46807bf4
AM
865 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
866 ppc64_elf_unhandled_reloc),
f9c6b907
AM
867
868 /* Like R_PPC64_TPREL16_HI, but no overflow. */
46807bf4
AM
869 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
870 ppc64_elf_unhandled_reloc),
f9c6b907
AM
871
872 /* Like R_PPC64_TPREL16_HA, but no overflow. */
46807bf4
AM
873 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
874 ppc64_elf_unhandled_reloc),
f9c6b907 875
006589cf 876 /* Marker reloc on ELFv2 large-model function entry. */
46807bf4
AM
877 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
878 bfd_elf_generic_reloc),
006589cf 879
45965137 880 /* Like ADDR64, but use local entry point of function. */
46807bf4
AM
881 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
882 bfd_elf_generic_reloc),
45965137 883
5663e321
AM
884 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, FALSE, dont,
885 bfd_elf_generic_reloc),
886
887 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, FALSE, dont,
888 bfd_elf_generic_reloc),
889
890 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, FALSE, dont,
891 bfd_elf_generic_reloc),
892
893 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
894 ppc64_elf_prefix_reloc),
895
896 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, dont,
897 ppc64_elf_prefix_reloc),
898
899 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
900 ppc64_elf_prefix_reloc),
901
902 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
903 ppc64_elf_prefix_reloc),
904
905 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
906 ppc64_elf_prefix_reloc),
907
908 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
909 ppc64_elf_unhandled_reloc),
910
911 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
912 ppc64_elf_unhandled_reloc),
913
914 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
915 ppc64_elf_unhandled_reloc),
916
917 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
918 bfd_elf_generic_reloc),
919
920 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
921 ppc64_elf_ha_reloc),
922
923 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
924 bfd_elf_generic_reloc),
925
926 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
927 ppc64_elf_ha_reloc),
928
929 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
930 bfd_elf_generic_reloc),
931
932 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
933 ppc64_elf_ha_reloc),
934
935 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
936 bfd_elf_generic_reloc),
937
938 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
939 ppc64_elf_ha_reloc),
940
941 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
942 ppc64_elf_prefix_reloc),
943
944 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
945 ppc64_elf_prefix_reloc),
946
5bd4f169 947 /* GNU extension to record C++ vtable hierarchy. */
46807bf4
AM
948 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
949 NULL),
5bd4f169
AM
950
951 /* GNU extension to record C++ vtable member usage. */
46807bf4
AM
952 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
953 NULL),
5bd4f169
AM
954};
955
956\f
957/* Initialize the ppc64_elf_howto_table, so that linear accesses can
958 be done. */
959
960static void
4ce794b7 961ppc_howto_init (void)
5bd4f169
AM
962{
963 unsigned int i, type;
964
a4b6fadd 965 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
966 {
967 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 968 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
969 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
970 }
971}
972
973static reloc_howto_type *
f3185997 974ppc64_elf_reloc_type_lookup (bfd *abfd,
4ce794b7 975 bfd_reloc_code_real_type code)
5bd4f169 976{
411e1bfb 977 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
978
979 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
980 /* Initialize howto table if needed. */
981 ppc_howto_init ();
982
4ce794b7 983 switch (code)
5bd4f169
AM
984 {
985 default:
f3185997 986 /* xgettext:c-format */
2cdcc330
AM
987 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
988 (int) code);
f3185997 989 bfd_set_error (bfd_error_bad_value);
4ce794b7 990 return NULL;
5bd4f169 991
411e1bfb
AM
992 case BFD_RELOC_NONE: r = R_PPC64_NONE;
993 break;
994 case BFD_RELOC_32: r = R_PPC64_ADDR32;
995 break;
996 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
997 break;
998 case BFD_RELOC_16: r = R_PPC64_ADDR16;
999 break;
1000 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1001 break;
1002 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1003 break;
f9c6b907
AM
1004 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1005 break;
411e1bfb 1006 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 1007 break;
f9c6b907
AM
1008 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1009 break;
411e1bfb 1010 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 1011 break;
411e1bfb 1012 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 1013 break;
411e1bfb 1014 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 1015 break;
411e1bfb 1016 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 1017 break;
05d0e962
AM
1018 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1019 break;
411e1bfb 1020 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 1021 break;
411e1bfb 1022 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 1023 break;
411e1bfb 1024 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 1025 break;
411e1bfb 1026 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 1027 break;
411e1bfb 1028 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 1029 break;
411e1bfb 1030 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 1031 break;
411e1bfb 1032 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 1033 break;
411e1bfb 1034 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 1035 break;
411e1bfb 1036 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 1037 break;
411e1bfb 1038 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 1039 break;
411e1bfb 1040 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 1041 break;
411e1bfb 1042 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 1043 break;
411e1bfb 1044 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 1045 break;
411e1bfb 1046 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 1047 break;
411e1bfb 1048 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 1049 break;
411e1bfb 1050 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 1051 break;
411e1bfb 1052 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 1053 break;
411e1bfb 1054 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 1055 break;
411e1bfb 1056 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 1057 break;
411e1bfb 1058 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 1059 break;
411e1bfb 1060 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 1061 break;
411e1bfb 1062 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 1063 break;
411e1bfb 1064 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 1065 break;
411e1bfb 1066 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1067 break;
411e1bfb 1068 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1069 break;
411e1bfb 1070 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1071 break;
411e1bfb 1072 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1073 break;
411e1bfb 1074 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1075 break;
411e1bfb 1076 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1077 break;
411e1bfb 1078 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1079 break;
411e1bfb 1080 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1081 break;
411e1bfb 1082 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1083 break;
411e1bfb 1084 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1085 break;
411e1bfb 1086 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1087 break;
411e1bfb 1088 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1089 break;
411e1bfb 1090 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1091 break;
411e1bfb 1092 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1093 break;
411e1bfb 1094 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1095 break;
411e1bfb 1096 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1097 break;
411e1bfb 1098 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1099 break;
411e1bfb 1100 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1101 break;
411e1bfb 1102 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1103 break;
411e1bfb 1104 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1105 break;
411e1bfb 1106 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1107 break;
411e1bfb 1108 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1109 break;
411e1bfb 1110 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1111 break;
411e1bfb 1112 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1113 break;
411e1bfb 1114 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1115 break;
411e1bfb 1116 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1117 break;
727fc41e
AM
1118 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1119 break;
1120 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1121 break;
411e1bfb 1122 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1123 break;
411e1bfb 1124 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1125 break;
411e1bfb 1126 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1127 break;
411e1bfb 1128 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1129 break;
f9c6b907
AM
1130 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1131 break;
411e1bfb 1132 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1133 break;
f9c6b907
AM
1134 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1135 break;
411e1bfb 1136 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1137 break;
411e1bfb
AM
1138 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1139 break;
1140 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1141 break;
1142 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1143 break;
f9c6b907
AM
1144 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1145 break;
411e1bfb
AM
1146 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1147 break;
f9c6b907
AM
1148 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1149 break;
411e1bfb
AM
1150 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1151 break;
1152 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1153 break;
1154 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1155 break;
1156 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1157 break;
1158 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1159 break;
1160 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1161 break;
1162 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1163 break;
1164 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1165 break;
1166 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1167 break;
1168 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1169 break;
1170 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1171 break;
1172 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1173 break;
1174 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1175 break;
1176 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1177 break;
1178 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1179 break;
1180 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1181 break;
1182 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1183 break;
1184 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1185 break;
1186 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1187 break;
1188 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1189 break;
1190 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1191 break;
1192 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1193 break;
1194 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1195 break;
1196 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1197 break;
1198 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1199 break;
1200 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1201 break;
1202 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1203 break;
1204 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1205 break;
1206 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1207 break;
25f23106
AM
1208 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1209 break;
1210 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1211 break;
1212 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1213 break;
1214 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1215 break;
4a969973
AM
1216 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1217 break;
1218 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1219 break;
1220 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1221 break;
1222 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1223 break;
1224 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1225 break;
1226 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1227 break;
7ba71655
AM
1228 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1229 break;
a680de9a
PB
1230 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1231 break;
006589cf
AM
1232 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1233 break;
45965137
AM
1234 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1235 break;
5663e321
AM
1236 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1237 break;
1238 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1239 break;
1240 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1241 break;
1242 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1243 break;
1244 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1245 break;
1246 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1247 break;
1248 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1249 break;
1250 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1251 break;
1252 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1253 break;
1254 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1255 break;
1256 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1257 break;
1258 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1259 break;
1260 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1261 break;
1262 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1263 break;
1264 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1265 break;
1266 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1267 break;
1268 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1269 break;
411e1bfb
AM
1270 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1271 break;
1272 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
1273 break;
1274 }
1275
4ce794b7 1276 return ppc64_elf_howto_table[r];
5bd4f169
AM
1277};
1278
157090f7
AM
1279static reloc_howto_type *
1280ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1281 const char *r_name)
1282{
1283 unsigned int i;
1284
a4b6fadd 1285 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
1286 if (ppc64_elf_howto_raw[i].name != NULL
1287 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1288 return &ppc64_elf_howto_raw[i];
1289
1290 return NULL;
1291}
1292
5bd4f169
AM
1293/* Set the howto pointer for a PowerPC ELF reloc. */
1294
f3185997 1295static bfd_boolean
4aef7643 1296ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 1297 Elf_Internal_Rela *dst)
5bd4f169 1298{
65f38f15
AM
1299 unsigned int type;
1300
ef60b7ff 1301 /* Initialize howto table if needed. */
5bd4f169 1302 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
1303 ppc_howto_init ();
1304
65f38f15 1305 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 1306 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 1307 {
695344c0 1308 /* xgettext:c-format */
0aa13fee 1309 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 1310 abfd, type);
f3185997
NC
1311 bfd_set_error (bfd_error_bad_value);
1312 return FALSE;
d0fb9a8d 1313 }
65f38f15 1314 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
1315 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1316 {
1317 /* xgettext:c-format */
1318 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1319 abfd, type);
1320 bfd_set_error (bfd_error_bad_value);
1321 return FALSE;
1322 }
2cdcc330 1323
f3185997 1324 return TRUE;
5bd4f169
AM
1325}
1326
04c9666a 1327/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
1328
1329static bfd_reloc_status_type
4ce794b7
AM
1330ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1331 void *data, asection *input_section,
1332 bfd *output_bfd, char **error_message)
5bd4f169 1333{
a680de9a
PB
1334 enum elf_ppc64_reloc_type r_type;
1335 long insn;
1336 bfd_size_type octets;
3de43e7b 1337 bfd_vma value;
a680de9a 1338
805fc799
AM
1339 /* If this is a relocatable link (output_bfd test tells us), just
1340 call the generic function. Any adjustment will be done at final
1341 link time. */
1342 if (output_bfd != NULL)
cedb70c5 1343 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1344 input_section, output_bfd, error_message);
1345
5663e321
AM
1346 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1347 We won't actually be using the low bits, so trashing them
805fc799 1348 doesn't matter. */
a680de9a 1349 r_type = reloc_entry->howto->type;
5663e321
AM
1350 if (r_type == R_PPC64_ADDR16_HIGHERA34
1351 || r_type == R_PPC64_ADDR16_HIGHESTA34
1352 || r_type == R_PPC64_REL16_HIGHERA34
1353 || r_type == R_PPC64_REL16_HIGHESTA34)
1354 reloc_entry->addend += 1ULL << 33;
1355 else
1356 reloc_entry->addend += 1U << 15;
a680de9a
PB
1357 if (r_type != R_PPC64_REL16DX_HA)
1358 return bfd_reloc_continue;
1359
1360 value = 0;
1361 if (!bfd_is_com_section (symbol->section))
1362 value = symbol->value;
1363 value += (reloc_entry->addend
1364 + symbol->section->output_offset
1365 + symbol->section->output_section->vma);
1366 value -= (reloc_entry->address
1367 + input_section->output_offset
1368 + input_section->output_section->vma);
3de43e7b 1369 value = (bfd_signed_vma) value >> 16;
a680de9a
PB
1370
1371 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1372 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1373 insn &= ~0x1fffc1;
3de43e7b 1374 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 1375 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 1376 if (value + 0x8000 > 0xffff)
a680de9a
PB
1377 return bfd_reloc_overflow;
1378 return bfd_reloc_ok;
805fc799 1379}
5bd4f169 1380
2441e016
AM
1381static bfd_reloc_status_type
1382ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1383 void *data, asection *input_section,
1384 bfd *output_bfd, char **error_message)
1385{
1386 if (output_bfd != NULL)
1387 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1388 input_section, output_bfd, error_message);
1389
699733f6
AM
1390 if (strcmp (symbol->section->name, ".opd") == 0
1391 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
1392 {
1393 bfd_vma dest = opd_entry_value (symbol->section,
1394 symbol->value + reloc_entry->addend,
aef36ac1 1395 NULL, NULL, FALSE);
2441e016
AM
1396 if (dest != (bfd_vma) -1)
1397 reloc_entry->addend = dest - (symbol->value
1398 + symbol->section->output_section->vma
1399 + symbol->section->output_offset);
1400 }
810d4e75
AM
1401 else
1402 {
1403 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1404
1405 if (symbol->section->owner != abfd
9f284bf9 1406 && symbol->section->owner != NULL
810d4e75
AM
1407 && abiversion (symbol->section->owner) >= 2)
1408 {
1409 unsigned int i;
1410
1411 for (i = 0; i < symbol->section->owner->symcount; ++i)
1412 {
1413 asymbol *symdef = symbol->section->owner->outsymbols[i];
1414
1415 if (strcmp (symdef->name, symbol->name) == 0)
1416 {
1417 elfsym = (elf_symbol_type *) symdef;
1418 break;
1419 }
1420 }
1421 }
1422 reloc_entry->addend
1423 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1424 }
2441e016
AM
1425 return bfd_reloc_continue;
1426}
1427
805fc799 1428static bfd_reloc_status_type
4ce794b7
AM
1429ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1430 void *data, asection *input_section,
1431 bfd *output_bfd, char **error_message)
805fc799
AM
1432{
1433 long insn;
04c9666a 1434 enum elf_ppc64_reloc_type r_type;
805fc799 1435 bfd_size_type octets;
794e51c0
AM
1436 /* Assume 'at' branch hints. */
1437 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
1438
1439 /* If this is a relocatable link (output_bfd test tells us), just
1440 call the generic function. Any adjustment will be done at final
1441 link time. */
5bd4f169 1442 if (output_bfd != NULL)
cedb70c5 1443 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1444 input_section, output_bfd, error_message);
1445
1446 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1447 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1448 insn &= ~(0x01 << 21);
4ce794b7 1449 r_type = reloc_entry->howto->type;
805fc799
AM
1450 if (r_type == R_PPC64_ADDR14_BRTAKEN
1451 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 1452 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 1453
794e51c0 1454 if (is_isa_v2)
5bd4f169 1455 {
805fc799
AM
1456 /* Set 'a' bit. This is 0b00010 in BO field for branch
1457 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1458 for branch on CTR insns (BO == 1a00t or 1a01t). */
1459 if ((insn & (0x14 << 21)) == (0x04 << 21))
1460 insn |= 0x02 << 21;
1461 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1462 insn |= 0x08 << 21;
1463 else
2441e016 1464 goto out;
5bd4f169 1465 }
805fc799
AM
1466 else
1467 {
1468 bfd_vma target = 0;
1469 bfd_vma from;
5bd4f169 1470
805fc799
AM
1471 if (!bfd_is_com_section (symbol->section))
1472 target = symbol->value;
1473 target += symbol->section->output_section->vma;
1474 target += symbol->section->output_offset;
1475 target += reloc_entry->addend;
5bd4f169 1476
805fc799
AM
1477 from = (reloc_entry->address
1478 + input_section->output_offset
1479 + input_section->output_section->vma);
5bd4f169 1480
805fc799
AM
1481 /* Invert 'y' bit if not the default. */
1482 if ((bfd_signed_vma) (target - from) < 0)
1483 insn ^= 0x01 << 21;
1484 }
4ce794b7 1485 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
1486 out:
1487 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1488 input_section, output_bfd, error_message);
805fc799 1489}
5bd4f169 1490
805fc799 1491static bfd_reloc_status_type
4ce794b7
AM
1492ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1493 void *data, asection *input_section,
1494 bfd *output_bfd, char **error_message)
805fc799
AM
1495{
1496 /* If this is a relocatable link (output_bfd test tells us), just
1497 call the generic function. Any adjustment will be done at final
1498 link time. */
1499 if (output_bfd != NULL)
cedb70c5 1500 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 1501 input_section, output_bfd, error_message);
5bd4f169 1502
805fc799
AM
1503 /* Subtract the symbol section base address. */
1504 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
1505 return bfd_reloc_continue;
1506}
1507
805fc799 1508static bfd_reloc_status_type
4ce794b7
AM
1509ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1510 void *data, asection *input_section,
1511 bfd *output_bfd, char **error_message)
805fc799
AM
1512{
1513 /* If this is a relocatable link (output_bfd test tells us), just
1514 call the generic function. Any adjustment will be done at final
1515 link time. */
1516 if (output_bfd != NULL)
cedb70c5 1517 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1518 input_section, output_bfd, error_message);
1519
1520 /* Subtract the symbol section base address. */
1521 reloc_entry->addend -= symbol->section->output_section->vma;
1522
1523 /* Adjust the addend for sign extension of the low 16 bits. */
1524 reloc_entry->addend += 0x8000;
1525 return bfd_reloc_continue;
1526}
1527
1528static bfd_reloc_status_type
4ce794b7
AM
1529ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1530 void *data, asection *input_section,
1531 bfd *output_bfd, char **error_message)
805fc799
AM
1532{
1533 bfd_vma TOCstart;
1534
1535 /* If this is a relocatable link (output_bfd test tells us), just
1536 call the generic function. Any adjustment will be done at final
1537 link time. */
1538 if (output_bfd != NULL)
cedb70c5 1539 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1540 input_section, output_bfd, error_message);
1541
1542 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1543 if (TOCstart == 0)
1c865ab2 1544 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1545
1546 /* Subtract the TOC base address. */
1547 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1548 return bfd_reloc_continue;
1549}
1550
1551static bfd_reloc_status_type
4ce794b7
AM
1552ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1553 void *data, asection *input_section,
1554 bfd *output_bfd, char **error_message)
805fc799
AM
1555{
1556 bfd_vma TOCstart;
1557
1558 /* If this is a relocatable link (output_bfd test tells us), just
1559 call the generic function. Any adjustment will be done at final
1560 link time. */
1561 if (output_bfd != NULL)
cedb70c5 1562 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1563 input_section, output_bfd, error_message);
1564
1565 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1566 if (TOCstart == 0)
1c865ab2 1567 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1568
1569 /* Subtract the TOC base address. */
1570 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1571
1572 /* Adjust the addend for sign extension of the low 16 bits. */
1573 reloc_entry->addend += 0x8000;
1574 return bfd_reloc_continue;
1575}
1576
1577static bfd_reloc_status_type
4ce794b7
AM
1578ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1579 void *data, asection *input_section,
1580 bfd *output_bfd, char **error_message)
805fc799
AM
1581{
1582 bfd_vma TOCstart;
1583 bfd_size_type octets;
1584
1585 /* If this is a relocatable link (output_bfd test tells us), just
1586 call the generic function. Any adjustment will be done at final
1587 link time. */
1588 if (output_bfd != NULL)
cedb70c5 1589 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1590 input_section, output_bfd, error_message);
1591
1592 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1593 if (TOCstart == 0)
1c865ab2 1594 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1595
1596 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1597 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1598 return bfd_reloc_ok;
1599}
1600
5663e321
AM
1601static bfd_reloc_status_type
1602ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1603 void *data, asection *input_section,
1604 bfd *output_bfd, char **error_message)
1605{
1606 uint64_t insn;
1607 bfd_vma targ;
1608
1609 if (output_bfd != NULL)
1610 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1611 input_section, output_bfd, error_message);
1612
1613 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1614 insn <<= 32;
1615 insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1616
1617 targ = (symbol->section->output_section->vma
1618 + symbol->section->output_offset
1619 + reloc_entry->addend);
1620 if (!bfd_is_com_section (symbol->section))
1621 targ += symbol->value;
1622 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1623 targ += 1ULL << 33;
1624 if (reloc_entry->howto->pc_relative)
1625 {
1626 bfd_vma from = (reloc_entry->address
1627 + input_section->output_offset
1628 + input_section->output_section->vma);
1629 targ -=from;
1630 }
1631 targ >>= reloc_entry->howto->rightshift;
1632 insn &= ~reloc_entry->howto->dst_mask;
1633 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1634 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1635 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1636 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1637 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1638 >= 1ULL << reloc_entry->howto->bitsize))
1639 return bfd_reloc_overflow;
1640 return bfd_reloc_ok;
1641}
1642
805fc799 1643static bfd_reloc_status_type
4ce794b7
AM
1644ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1645 void *data, asection *input_section,
1646 bfd *output_bfd, char **error_message)
805fc799
AM
1647{
1648 /* If this is a relocatable link (output_bfd test tells us), just
1649 call the generic function. Any adjustment will be done at final
1650 link time. */
1651 if (output_bfd != NULL)
cedb70c5 1652 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1653 input_section, output_bfd, error_message);
1654
1655 if (error_message != NULL)
1656 {
1657 static char buf[60];
1658 sprintf (buf, "generic linker can't handle %s",
1659 reloc_entry->howto->name);
1660 *error_message = buf;
1661 }
1662 return bfd_reloc_dangerous;
1663}
1664
927be08e
AM
1665/* Track GOT entries needed for a given symbol. We might need more
1666 than one got entry per symbol. */
1667struct got_entry
1668{
1669 struct got_entry *next;
1670
1671 /* The symbol addend that we'll be placing in the GOT. */
1672 bfd_vma addend;
1673
1674 /* Unlike other ELF targets, we use separate GOT entries for the same
1675 symbol referenced from different input files. This is to support
1676 automatic multiple TOC/GOT sections, where the TOC base can vary
1677 from one input file to another. After partitioning into TOC groups
1678 we merge entries within the group.
1679
1680 Point to the BFD owning this GOT entry. */
1681 bfd *owner;
1682
1683 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1684 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1685 unsigned char tls_type;
927be08e
AM
1686
1687 /* Non-zero if got.ent points to real entry. */
f961d9dd 1688 unsigned char is_indirect;
927be08e
AM
1689
1690 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1691 union
2cdcc330
AM
1692 {
1693 bfd_signed_vma refcount;
1694 bfd_vma offset;
1695 struct got_entry *ent;
1696 } got;
927be08e
AM
1697};
1698
1699/* The same for PLT. */
1700struct plt_entry
1701{
1702 struct plt_entry *next;
1703
1704 bfd_vma addend;
1705
1706 union
2cdcc330
AM
1707 {
1708 bfd_signed_vma refcount;
1709 bfd_vma offset;
1710 } plt;
927be08e
AM
1711};
1712
e717da7e
AM
1713struct ppc64_elf_obj_tdata
1714{
1715 struct elf_obj_tdata elf;
1716
1717 /* Shortcuts to dynamic linker sections. */
1718 asection *got;
1719 asection *relgot;
1720
b3fac117
AM
1721 /* Used during garbage collection. We attach global symbols defined
1722 on removed .opd entries to this section so that the sym is removed. */
1723 asection *deleted_section;
81688140 1724
927be08e 1725 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1726 sections means we potentially need one of these for each input bfd. */
927be08e 1727 struct got_entry tlsld_got;
8860955f 1728
2cdcc330
AM
1729 union
1730 {
729eabd5
AM
1731 /* A copy of relocs before they are modified for --emit-relocs. */
1732 Elf_Internal_Rela *relocs;
1733
1734 /* Section contents. */
1735 bfd_byte *contents;
1736 } opd;
d77c8a4b
AM
1737
1738 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1739 the reloc to be in the range -32768 to 32767. */
98528052
AM
1740 unsigned int has_small_toc_reloc : 1;
1741
560c8763
AM
1742 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1743 instruction not one we handle. */
1744 unsigned int unexpected_toc_insn : 1;
066f4018
AM
1745
1746 /* Set if got relocs that can be optimised are present in this file. */
1747 unsigned int has_gotrel : 1;
e717da7e
AM
1748};
1749
1750#define ppc64_elf_tdata(bfd) \
1751 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1752
1753#define ppc64_tlsld_got(bfd) \
1754 (&ppc64_elf_tdata (bfd)->tlsld_got)
1755
0c8d6e5c
AM
1756#define is_ppc64_elf(bfd) \
1757 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1758 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1759
e717da7e
AM
1760/* Override the generic function because we store some extras. */
1761
1762static bfd_boolean
1763ppc64_elf_mkobject (bfd *abfd)
1764{
0ffa91dd 1765 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1766 PPC64_ELF_DATA);
e717da7e
AM
1767}
1768
feee612b 1769/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1770 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1771
b34976b6 1772static bfd_boolean
4ce794b7 1773ppc64_elf_object_p (bfd *abfd)
feee612b 1774{
14b57c7c
AM
1775 if (!abfd->arch_info->the_default)
1776 return TRUE;
1777
1778 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1779 {
1780 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1781
1782 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1783 {
1784 /* Relies on arch after 32 bit default being 64 bit default. */
1785 abfd->arch_info = abfd->arch_info->next;
1786 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1787 }
1788 }
14b57c7c 1789 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1790}
1791
d37c89e5
AM
1792/* Support for core dump NOTE sections. */
1793
1794static bfd_boolean
1795ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1796{
eea6121a 1797 size_t offset, size;
d37c89e5
AM
1798
1799 if (note->descsz != 504)
1800 return FALSE;
1801
1802 /* pr_cursig */
228e534f 1803 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1804
1805 /* pr_pid */
228e534f 1806 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1807
1808 /* pr_reg */
1809 offset = 112;
eea6121a 1810 size = 384;
d37c89e5
AM
1811
1812 /* Make a ".reg/999" section. */
1813 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1814 size, note->descpos + offset);
d37c89e5
AM
1815}
1816
1817static bfd_boolean
1818ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1819{
1820 if (note->descsz != 136)
1821 return FALSE;
1822
228e534f 1823 elf_tdata (abfd)->core->pid
bc989cdc 1824 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1825 elf_tdata (abfd)->core->program
d37c89e5 1826 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1827 elf_tdata (abfd)->core->command
d37c89e5
AM
1828 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1829
1830 return TRUE;
1831}
1832
183e98be
AM
1833static char *
1834ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1835 ...)
1836{
1837 switch (note_type)
1838 {
1839 default:
1840 return NULL;
1841
1842 case NT_PRPSINFO:
1843 {
9ef6d1e3 1844 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1845 va_list ap;
1846
1847 va_start (ap, note_type);
75cd47ed 1848 memset (data, 0, sizeof (data));
183e98be 1849 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1850#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1851 DIAGNOSTIC_PUSH;
be3e27bb 1852 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1853 -Wstringop-truncation:
1854 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1855 */
95da9854
L
1856 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1857#endif
183e98be 1858 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1859#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1860 DIAGNOSTIC_POP;
fe75810f 1861#endif
183e98be
AM
1862 va_end (ap);
1863 return elfcore_write_note (abfd, buf, bufsiz,
1864 "CORE", note_type, data, sizeof (data));
1865 }
1866
1867 case NT_PRSTATUS:
1868 {
1869 char data[504];
1870 va_list ap;
1871 long pid;
1872 int cursig;
1873 const void *greg;
1874
1875 va_start (ap, note_type);
1876 memset (data, 0, 112);
1877 pid = va_arg (ap, long);
1878 bfd_put_32 (abfd, pid, data + 32);
1879 cursig = va_arg (ap, int);
1880 bfd_put_16 (abfd, cursig, data + 12);
1881 greg = va_arg (ap, const void *);
1882 memcpy (data + 112, greg, 384);
1883 memset (data + 496, 0, 8);
1884 va_end (ap);
1885 return elfcore_write_note (abfd, buf, bufsiz,
1886 "CORE", note_type, data, sizeof (data));
1887 }
1888 }
1889}
1890
5d35169e
AM
1891/* Add extra PPC sections. */
1892
2cdcc330 1893static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1894{
07d6d2b8
AM
1895 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1896 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1897 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1898 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1899 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1900 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1901 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1902};
1903
7c8fe5c4
AM
1904enum _ppc64_sec_type {
1905 sec_normal = 0,
1906 sec_opd = 1,
1907 sec_toc = 2
1908};
1909
f0abc2a1
AM
1910struct _ppc64_elf_section_data
1911{
1912 struct bfd_elf_section_data elf;
411e1bfb 1913
f0abc2a1
AM
1914 union
1915 {
51aecdc5
AM
1916 /* An array with one entry for each opd function descriptor,
1917 and some spares since opd entries may be either 16 or 24 bytes. */
1918#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
1919 struct _opd_sec_data
1920 {
1921 /* Points to the function code section for local opd entries. */
1922 asection **func_sec;
1923
1924 /* After editing .opd, adjust references to opd local syms. */
1925 long *adjust;
1926 } opd;
7c8fe5c4 1927
3a71aa26
AM
1928 /* An array for toc sections, indexed by offset/8. */
1929 struct _toc_sec_data
1930 {
1931 /* Specifies the relocation symbol index used at a given toc offset. */
1932 unsigned *symndx;
1933
1934 /* And the relocation addend. */
1935 bfd_vma *add;
1936 } toc;
7c8fe5c4
AM
1937 } u;
1938
1939 enum _ppc64_sec_type sec_type:2;
411e1bfb 1940
7c8fe5c4
AM
1941 /* Flag set when small branches are detected. Used to
1942 select suitable defaults for the stub group size. */
1943 unsigned int has_14bit_branch:1;
3e04d765
AM
1944
1945 /* Flag set when PLTCALL relocs are detected. */
1946 unsigned int has_pltcall:1;
066f4018
AM
1947
1948 /* Flag set when section has GOT relocations that can be optimised. */
1949 unsigned int has_gotrel:1;
f0abc2a1
AM
1950};
1951
1952#define ppc64_elf_section_data(sec) \
411e1bfb 1953 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
1954
1955static bfd_boolean
4ce794b7 1956ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1957{
f592407e
AM
1958 if (!sec->used_by_bfd)
1959 {
1960 struct _ppc64_elf_section_data *sdata;
1961 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1962
f592407e
AM
1963 sdata = bfd_zalloc (abfd, amt);
1964 if (sdata == NULL)
1965 return FALSE;
1966 sec->used_by_bfd = sdata;
1967 }
f0abc2a1
AM
1968
1969 return _bfd_elf_new_section_hook (abfd, sec);
1970}
4025353c 1971
74f0fb50 1972static struct _opd_sec_data *
4025353c
AM
1973get_opd_info (asection * sec)
1974{
1975 if (sec != NULL
1976 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 1977 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 1978 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
1979 return NULL;
1980}
90e3cdf2
JJ
1981\f
1982/* Parameters for the qsort hook. */
90e3cdf2 1983static bfd_boolean synthetic_relocatable;
cd285db5 1984static asection *synthetic_opd;
90e3cdf2 1985
699733f6 1986/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
1987
1988static int
1989compare_symbols (const void *ap, const void *bp)
1990{
2cdcc330
AM
1991 const asymbol *a = *(const asymbol **) ap;
1992 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 1993
699733f6
AM
1994 /* Section symbols first. */
1995 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 1996 return -1;
699733f6 1997 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
1998 return 1;
1999
699733f6 2000 /* then .opd symbols. */
cd285db5
AM
2001 if (synthetic_opd != NULL)
2002 {
2003 if (strcmp (a->section->name, ".opd") == 0
2004 && strcmp (b->section->name, ".opd") != 0)
2005 return -1;
2006 if (strcmp (a->section->name, ".opd") != 0
2007 && strcmp (b->section->name, ".opd") == 0)
2008 return 1;
2009 }
90e3cdf2 2010
699733f6 2011 /* then other code symbols. */
2cdcc330
AM
2012 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2013 == (SEC_CODE | SEC_ALLOC))
2014 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2015 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2016 return -1;
2017
2cdcc330
AM
2018 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2019 != (SEC_CODE | SEC_ALLOC))
2020 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2021 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2022 return 1;
2023
2024 if (synthetic_relocatable)
2025 {
2026 if (a->section->id < b->section->id)
2027 return -1;
2028
2029 if (a->section->id > b->section->id)
2030 return 1;
2031 }
2032
2033 if (a->value + a->section->vma < b->value + b->section->vma)
2034 return -1;
2035
2036 if (a->value + a->section->vma > b->value + b->section->vma)
2037 return 1;
2038
4d35a0aa
AM
2039 /* For syms with the same value, prefer strong dynamic global function
2040 syms over other syms. */
2041 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2042 return -1;
2043
2044 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2045 return 1;
2046
2047 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2048 return -1;
2049
2050 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2051 return 1;
2052
2053 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2054 return -1;
2055
2056 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2057 return 1;
2058
2059 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2060 return -1;
2061
2062 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2063 return 1;
2064
aaed6f5b 2065 return a > b;
90e3cdf2
JJ
2066}
2067
699733f6 2068/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2069
699733f6 2070static asymbol *
7292b3ac 2071sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 2072{
699733f6 2073 long mid;
90e3cdf2 2074
7292b3ac 2075 if (id == (unsigned) -1)
699733f6
AM
2076 {
2077 while (lo < hi)
2078 {
2079 mid = (lo + hi) >> 1;
2080 if (syms[mid]->value + syms[mid]->section->vma < value)
2081 lo = mid + 1;
2082 else if (syms[mid]->value + syms[mid]->section->vma > value)
2083 hi = mid;
2084 else
2085 return syms[mid];
2086 }
2087 }
2088 else
2089 {
2090 while (lo < hi)
2091 {
2092 mid = (lo + hi) >> 1;
2093 if (syms[mid]->section->id < id)
2094 lo = mid + 1;
2095 else if (syms[mid]->section->id > id)
2096 hi = mid;
2097 else if (syms[mid]->value < value)
2098 lo = mid + 1;
2099 else if (syms[mid]->value > value)
2100 hi = mid;
2101 else
2102 return syms[mid];
2103 }
2104 }
2105 return NULL;
90e3cdf2
JJ
2106}
2107
468392fb
AM
2108static bfd_boolean
2109section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2110{
2111 bfd_vma vma = *(bfd_vma *) ptr;
2112 return ((section->flags & SEC_ALLOC) != 0
2113 && section->vma <= vma
2114 && vma < section->vma + section->size);
2115}
2116
699733f6 2117/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
2118 entry syms. Also generate @plt symbols for the glink branch table.
2119 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
2120
2121static long
a7535cf3
AM
2122ppc64_elf_get_synthetic_symtab (bfd *abfd,
2123 long static_count, asymbol **static_syms,
2124 long dyn_count, asymbol **dyn_syms,
c9727e01 2125 asymbol **ret)
90e3cdf2
JJ
2126{
2127 asymbol *s;
0ccf57bd 2128 size_t i, j, count;
90e3cdf2 2129 char *names;
0ccf57bd 2130 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 2131 asection *opd = NULL;
90e3cdf2 2132 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2133 asymbol **syms;
ee67d69a 2134 int abi = abiversion (abfd);
90e3cdf2
JJ
2135
2136 *ret = NULL;
2137
ee67d69a
AM
2138 if (abi < 2)
2139 {
2140 opd = bfd_get_section_by_name (abfd, ".opd");
2141 if (opd == NULL && abi == 1)
2142 return 0;
2143 }
90e3cdf2 2144
a5259595
AM
2145 syms = NULL;
2146 codesecsym = 0;
2147 codesecsymend = 0;
2148 secsymend = 0;
2149 opdsymend = 0;
2150 symcount = 0;
2151 if (opd != NULL)
c9727e01 2152 {
a5259595
AM
2153 symcount = static_count;
2154 if (!relocatable)
2155 symcount += dyn_count;
2156 if (symcount == 0)
2157 return 0;
c9727e01 2158
a5259595
AM
2159 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2160 if (syms == NULL)
2161 return -1;
90e3cdf2 2162
a5259595
AM
2163 if (!relocatable && static_count != 0 && dyn_count != 0)
2164 {
2165 /* Use both symbol tables. */
2166 memcpy (syms, static_syms, static_count * sizeof (*syms));
2167 memcpy (syms + static_count, dyn_syms,
2168 (dyn_count + 1) * sizeof (*syms));
2169 }
2170 else if (!relocatable && static_count == 0)
2171 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2172 else
2173 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2174
0ccf57bd
AM
2175 /* Trim uninteresting symbols. Interesting symbols are section,
2176 function, and notype symbols. */
2177 for (i = 0, j = 0; i < symcount; ++i)
2178 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2179 | BSF_RELC | BSF_SRELC)) == 0)
2180 syms[j++] = syms[i];
2181 symcount = j;
2182
a5259595
AM
2183 synthetic_relocatable = relocatable;
2184 synthetic_opd = opd;
2185 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2186
a5259595
AM
2187 if (!relocatable && symcount > 1)
2188 {
bfa5bd2a
PA
2189 /* Trim duplicate syms, since we may have merged the normal
2190 and dynamic symbols. Actually, we only care about syms
2191 that have different values, so trim any with the same
2192 value. Don't consider ifunc and ifunc resolver symbols
2193 duplicates however, because GDB wants to know whether a
2194 text symbol is an ifunc resolver. */
a5259595 2195 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2196 {
2197 const asymbol *s0 = syms[i - 1];
2198 const asymbol *s1 = syms[i];
2199
2200 if ((s0->value + s0->section->vma
2201 != s1->value + s1->section->vma)
2202 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2203 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2204 syms[j++] = syms[i];
2205 }
a5259595
AM
2206 symcount = j;
2207 }
699733f6 2208
a5259595
AM
2209 i = 0;
2210 /* Note that here and in compare_symbols we can't compare opd and
2211 sym->section directly. With separate debug info files, the
2212 symbols will be extracted from the debug file while abfd passed
2213 to this function is the real binary. */
0ccf57bd 2214 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2215 ++i;
2216 codesecsym = i;
2217
2218 for (; i < symcount; ++i)
2219 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2220 | SEC_THREAD_LOCAL))
2221 != (SEC_CODE | SEC_ALLOC))
2222 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2223 break;
2224 codesecsymend = i;
2225
2226 for (; i < symcount; ++i)
2227 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2228 break;
2229 secsymend = i;
2230
2231 for (; i < symcount; ++i)
2232 if (strcmp (syms[i]->section->name, ".opd") != 0)
2233 break;
2234 opdsymend = i;
2235
2236 for (; i < symcount; ++i)
2cdcc330
AM
2237 if (((syms[i]->section->flags
2238 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2239 != (SEC_CODE | SEC_ALLOC))
2240 break;
2241 symcount = i;
2242 }
c9727e01 2243 count = 0;
90e3cdf2 2244
699733f6 2245 if (relocatable)
90e3cdf2 2246 {
699733f6
AM
2247 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2248 arelent *r;
2249 size_t size;
0ccf57bd 2250 size_t relcount;
90e3cdf2 2251
468392fb
AM
2252 if (opdsymend == secsymend)
2253 goto done;
2254
699733f6 2255 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2256 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2257 if (relcount == 0)
c9727e01 2258 goto done;
90e3cdf2 2259
7356fed5
AM
2260 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2261 {
2262 count = -1;
2263 goto done;
2264 }
2265
699733f6 2266 size = 0;
595da8c5 2267 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2268 {
2269 asymbol *sym;
90e3cdf2 2270
595da8c5 2271 while (r < opd->relocation + relcount
699733f6
AM
2272 && r->address < syms[i]->value + opd->vma)
2273 ++r;
90e3cdf2 2274
595da8c5 2275 if (r == opd->relocation + relcount)
699733f6 2276 break;
90e3cdf2 2277
699733f6
AM
2278 if (r->address != syms[i]->value + opd->vma)
2279 continue;
90e3cdf2 2280
699733f6
AM
2281 if (r->howto->type != R_PPC64_ADDR64)
2282 continue;
90e3cdf2 2283
699733f6
AM
2284 sym = *r->sym_ptr_ptr;
2285 if (!sym_exists_at (syms, opdsymend, symcount,
2286 sym->section->id, sym->value + r->addend))
2287 {
2288 ++count;
2289 size += sizeof (asymbol);
2290 size += strlen (syms[i]->name) + 2;
2291 }
2292 }
90e3cdf2 2293
c4b0b099
AM
2294 if (size == 0)
2295 goto done;
699733f6
AM
2296 s = *ret = bfd_malloc (size);
2297 if (s == NULL)
2298 {
7356fed5 2299 count = -1;
c9727e01 2300 goto done;
699733f6 2301 }
90e3cdf2 2302
699733f6 2303 names = (char *) (s + count);
90e3cdf2 2304
595da8c5 2305 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2306 {
699733f6 2307 asymbol *sym;
90e3cdf2 2308
595da8c5 2309 while (r < opd->relocation + relcount
699733f6
AM
2310 && r->address < syms[i]->value + opd->vma)
2311 ++r;
90e3cdf2 2312
595da8c5 2313 if (r == opd->relocation + relcount)
699733f6
AM
2314 break;
2315
2316 if (r->address != syms[i]->value + opd->vma)
2317 continue;
2318
2319 if (r->howto->type != R_PPC64_ADDR64)
2320 continue;
90e3cdf2 2321
699733f6
AM
2322 sym = *r->sym_ptr_ptr;
2323 if (!sym_exists_at (syms, opdsymend, symcount,
2324 sym->section->id, sym->value + r->addend))
2325 {
2326 size_t len;
2327
2328 *s = *syms[i];
6ba2a415 2329 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2330 s->section = sym->section;
2331 s->value = sym->value + r->addend;
2332 s->name = names;
2333 *names++ = '.';
2334 len = strlen (syms[i]->name);
2335 memcpy (names, syms[i]->name, len + 1);
2336 names += len + 1;
6f610d07
UW
2337 /* Have udata.p point back to the original symbol this
2338 synthetic symbol was derived from. */
2339 s->udata.p = syms[i];
699733f6
AM
2340 s++;
2341 }
2342 }
2343 }
2344 else
90e3cdf2 2345 {
468392fb 2346 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 2347 bfd_byte *contents = NULL;
699733f6 2348 size_t size;
0ccf57bd 2349 size_t plt_count = 0;
468392fb
AM
2350 bfd_vma glink_vma = 0, resolv_vma = 0;
2351 asection *dynamic, *glink = NULL, *relplt = NULL;
2352 arelent *p;
90e3cdf2 2353
ee67d69a 2354 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2355 {
c4b0b099
AM
2356 free_contents_and_exit_err:
2357 count = -1;
ee67d69a 2358 free_contents_and_exit:
699733f6 2359 if (contents)
ee67d69a 2360 free (contents);
c9727e01 2361 goto done;
699733f6 2362 }
90e3cdf2 2363
699733f6
AM
2364 size = 0;
2365 for (i = secsymend; i < opdsymend; ++i)
2366 {
2367 bfd_vma ent;
90e3cdf2 2368
5ef11c02
AM
2369 /* Ignore bogus symbols. */
2370 if (syms[i]->value > opd->size - 8)
2371 continue;
2372
699733f6
AM
2373 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2374 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2375 {
2376 ++count;
2377 size += sizeof (asymbol);
2378 size += strlen (syms[i]->name) + 2;
2379 }
2380 }
90e3cdf2 2381
468392fb 2382 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2383 if (dyn_count != 0
2384 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2385 {
2386 bfd_byte *dynbuf, *extdyn, *extdynend;
2387 size_t extdynsize;
2388 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2389
2390 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2391 goto free_contents_and_exit_err;
468392fb
AM
2392
2393 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2394 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2395
2396 extdyn = dynbuf;
2397 extdynend = extdyn + dynamic->size;
2398 for (; extdyn < extdynend; extdyn += extdynsize)
2399 {
2400 Elf_Internal_Dyn dyn;
2401 (*swap_dyn_in) (abfd, extdyn, &dyn);
2402
2403 if (dyn.d_tag == DT_NULL)
2404 break;
2405
2406 if (dyn.d_tag == DT_PPC64_GLINK)
2407 {
9e390558
AM
2408 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2409 See comment in ppc64_elf_finish_dynamic_sections. */
2410 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2411 /* The .glink section usually does not survive the final
2412 link; search for the section (usually .text) where the
2413 glink stubs now reside. */
2414 glink = bfd_sections_find_if (abfd, section_covers_vma,
2415 &glink_vma);
2416 break;
2417 }
2418 }
2419
2420 free (dynbuf);
2421 }
2422
2423 if (glink != NULL)
2424 {
2425 /* Determine __glink trampoline by reading the relative branch
2426 from the first glink stub. */
2427 bfd_byte buf[4];
b9e5796b
AM
2428 unsigned int off = 0;
2429
2430 while (bfd_get_section_contents (abfd, glink, buf,
2431 glink_vma + off - glink->vma, 4))
468392fb
AM
2432 {
2433 unsigned int insn = bfd_get_32 (abfd, buf);
2434 insn ^= B_DOT;
2435 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2436 {
2cdcc330
AM
2437 resolv_vma
2438 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2439 break;
2440 }
2441 off += 4;
2442 if (off > 4)
2443 break;
468392fb
AM
2444 }
2445
2446 if (resolv_vma)
2447 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2448
066ee829
AM
2449 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2450 if (relplt != NULL)
2451 {
2452 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2cdcc330 2453 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 2454 goto free_contents_and_exit_err;
68ffbac6 2455
066ee829
AM
2456 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2457 size += plt_count * sizeof (asymbol);
468392fb 2458
066ee829
AM
2459 p = relplt->relocation;
2460 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2461 {
2462 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2463 if (p->addend != 0)
2464 size += sizeof ("+0x") - 1 + 16;
2465 }
066ee829 2466 }
468392fb
AM
2467 }
2468
c4b0b099
AM
2469 if (size == 0)
2470 goto free_contents_and_exit;
699733f6
AM
2471 s = *ret = bfd_malloc (size);
2472 if (s == NULL)
c4b0b099 2473 goto free_contents_and_exit_err;
90e3cdf2 2474
468392fb 2475 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2476
699733f6 2477 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2478 {
699733f6 2479 bfd_vma ent;
90e3cdf2 2480
5ef11c02
AM
2481 if (syms[i]->value > opd->size - 8)
2482 continue;
2483
699733f6
AM
2484 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2485 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2486 {
0ccf57bd 2487 size_t lo, hi;
699733f6 2488 size_t len;
c9727e01 2489 asection *sec = abfd->sections;
90e3cdf2 2490
699733f6
AM
2491 *s = *syms[i];
2492 lo = codesecsym;
2493 hi = codesecsymend;
2494 while (lo < hi)
2495 {
0ccf57bd 2496 size_t mid = (lo + hi) >> 1;
699733f6
AM
2497 if (syms[mid]->section->vma < ent)
2498 lo = mid + 1;
2499 else if (syms[mid]->section->vma > ent)
2500 hi = mid;
2501 else
c9727e01
AM
2502 {
2503 sec = syms[mid]->section;
2504 break;
2505 }
699733f6
AM
2506 }
2507
c9727e01 2508 if (lo >= hi && lo > codesecsym)
699733f6 2509 sec = syms[lo - 1]->section;
699733f6
AM
2510
2511 for (; sec != NULL; sec = sec->next)
2512 {
2513 if (sec->vma > ent)
2514 break;
63524580
JK
2515 /* SEC_LOAD may not be set if SEC is from a separate debug
2516 info file. */
2517 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2518 break;
2519 if ((sec->flags & SEC_CODE) != 0)
2520 s->section = sec;
2521 }
6ba2a415 2522 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2523 s->value = ent - s->section->vma;
2524 s->name = names;
2525 *names++ = '.';
2526 len = strlen (syms[i]->name);
2527 memcpy (names, syms[i]->name, len + 1);
2528 names += len + 1;
6f610d07
UW
2529 /* Have udata.p point back to the original symbol this
2530 synthetic symbol was derived from. */
2531 s->udata.p = syms[i];
699733f6 2532 s++;
90e3cdf2 2533 }
90e3cdf2 2534 }
699733f6 2535 free (contents);
468392fb
AM
2536
2537 if (glink != NULL && relplt != NULL)
2538 {
2539 if (resolv_vma)
2540 {
2541 /* Add a symbol for the main glink trampoline. */
86a4952b 2542 memset (s, 0, sizeof *s);
468392fb 2543 s->the_bfd = abfd;
6ba2a415 2544 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2545 s->section = glink;
2546 s->value = resolv_vma - glink->vma;
2547 s->name = names;
2cdcc330
AM
2548 memcpy (names, "__glink_PLTresolve",
2549 sizeof ("__glink_PLTresolve"));
468392fb
AM
2550 names += sizeof ("__glink_PLTresolve");
2551 s++;
2552 count++;
2553 }
2554
2555 /* FIXME: It would be very much nicer to put sym@plt on the
2556 stub rather than on the glink branch table entry. The
2557 objdump disassembler would then use a sensible symbol
2558 name on plt calls. The difficulty in doing so is
2559 a) finding the stubs, and,
2560 b) matching stubs against plt entries, and,
2561 c) there can be multiple stubs for a given plt entry.
2562
2563 Solving (a) could be done by code scanning, but older
2564 ppc64 binaries used different stubs to current code.
2565 (b) is the tricky one since you need to known the toc
2566 pointer for at least one function that uses a pic stub to
2567 be able to calculate the plt address referenced.
2568 (c) means gdb would need to set multiple breakpoints (or
2569 find the glink branch itself) when setting breakpoints
2570 for pending shared library loads. */
2571 p = relplt->relocation;
2572 for (i = 0; i < plt_count; i++, p++)
2573 {
2574 size_t len;
2575
2576 *s = **p->sym_ptr_ptr;
2577 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2578 we are defining a symbol, ensure one of them is set. */
2579 if ((s->flags & BSF_LOCAL) == 0)
2580 s->flags |= BSF_GLOBAL;
6ba2a415 2581 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2582 s->section = glink;
2583 s->value = glink_vma - glink->vma;
2584 s->name = names;
2585 s->udata.p = NULL;
2586 len = strlen ((*p->sym_ptr_ptr)->name);
2587 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2588 names += len;
e054468f
AM
2589 if (p->addend != 0)
2590 {
2591 memcpy (names, "+0x", sizeof ("+0x") - 1);
2592 names += sizeof ("+0x") - 1;
2593 bfd_sprintf_vma (abfd, names, p->addend);
2594 names += strlen (names);
2595 }
468392fb
AM
2596 memcpy (names, "@plt", sizeof ("@plt"));
2597 names += sizeof ("@plt");
2598 s++;
b9e5796b
AM
2599 if (abi < 2)
2600 {
2601 glink_vma += 8;
2602 if (i >= 0x8000)
2603 glink_vma += 4;
2604 }
2605 else
468392fb
AM
2606 glink_vma += 4;
2607 }
2608 count += plt_count;
2609 }
90e3cdf2
JJ
2610 }
2611
c9727e01 2612 done:
a7535cf3 2613 free (syms);
90e3cdf2
JJ
2614 return count;
2615}
5bd4f169 2616\f
65f38f15
AM
2617/* The following functions are specific to the ELF linker, while
2618 functions above are used generally. Those named ppc64_elf_* are
2619 called by the main ELF linker code. They appear in this file more
2620 or less in the order in which they are called. eg.
2621 ppc64_elf_check_relocs is called early in the link process,
2622 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2623 called.
2624
2625 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2626 functions have both a function code symbol and a function descriptor
2627 symbol. A call to foo in a relocatable object file looks like:
2628
2629 . .text
2630 . x:
2631 . bl .foo
2632 . nop
2633
2634 The function definition in another object file might be:
2635
2636 . .section .opd
2637 . foo: .quad .foo
2638 . .quad .TOC.@tocbase
2639 . .quad 0
2640 .
2641 . .text
2642 . .foo: blr
2643
2644 When the linker resolves the call during a static link, the branch
2645 unsurprisingly just goes to .foo and the .opd information is unused.
2646 If the function definition is in a shared library, things are a little
2647 different: The call goes via a plt call stub, the opd information gets
2648 copied to the plt, and the linker patches the nop.
2649
2650 . x:
2651 . bl .foo_stub
2652 . ld 2,40(1)
2653 .
2654 .
2655 . .foo_stub:
71a39c98
AM
2656 . std 2,40(1) # in practice, the call stub
2657 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2658 . addi 11,11,Lfoo@toc@l # this is the general idea
2659 . ld 12,0(11)
2660 . ld 2,8(11)
2661 . mtctr 12
2662 . ld 11,16(11)
e86ce104
AM
2663 . bctr
2664 .
2665 . .section .plt
2666 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2667
2668 The "reloc ()" notation is supposed to indicate that the linker emits
2669 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2670 copying.
2671
2672 What are the difficulties here? Well, firstly, the relocations
2673 examined by the linker in check_relocs are against the function code
2674 sym .foo, while the dynamic relocation in the plt is emitted against
2675 the function descriptor symbol, foo. Somewhere along the line, we need
2676 to carefully copy dynamic link information from one symbol to the other.
2677 Secondly, the generic part of the elf linker will make .foo a dynamic
2678 symbol as is normal for most other backends. We need foo dynamic
2679 instead, at least for an application final link. However, when
2680 creating a shared library containing foo, we need to have both symbols
2681 dynamic so that references to .foo are satisfied during the early
2682 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2683 definition from some other object, eg. a static library.
2684
2685 Update: As of August 2004, we support a new convention. Function
2686 calls may use the function descriptor symbol, ie. "bl foo". This
2687 behaves exactly as "bl .foo". */
65f38f15 2688
7c8bbca5
AM
2689/* Of those relocs that might be copied as dynamic relocs, this
2690 function selects those that must be copied when linking a shared
2691 library or PIE, even when the symbol is local. */
65f38f15 2692
1d483afe
AM
2693static int
2694must_be_dyn_reloc (struct bfd_link_info *info,
2695 enum elf_ppc64_reloc_type r_type)
2696{
2697 switch (r_type)
2698 {
2699 default:
7c8bbca5
AM
2700 /* Only relative relocs can be resolved when the object load
2701 address isn't fixed. DTPREL64 is excluded because the
2702 dynamic linker needs to differentiate global dynamic from
2703 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2704 return 1;
2705
2706 case R_PPC64_REL32:
2707 case R_PPC64_REL64:
2708 case R_PPC64_REL30:
2709 return 0;
2710
2711 case R_PPC64_TPREL16:
2712 case R_PPC64_TPREL16_LO:
2713 case R_PPC64_TPREL16_HI:
2714 case R_PPC64_TPREL16_HA:
2715 case R_PPC64_TPREL16_DS:
2716 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2717 case R_PPC64_TPREL16_HIGH:
2718 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2719 case R_PPC64_TPREL16_HIGHER:
2720 case R_PPC64_TPREL16_HIGHERA:
2721 case R_PPC64_TPREL16_HIGHEST:
2722 case R_PPC64_TPREL16_HIGHESTA:
2723 case R_PPC64_TPREL64:
7c8bbca5
AM
2724 /* These relocations are relative but in a shared library the
2725 linker doesn't know the thread pointer base. */
2726 return bfd_link_dll (info);
1d483afe
AM
2727 }
2728}
65f38f15 2729
f4656909
AM
2730/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2731 copying dynamic variables from a shared lib into an app's dynbss
2732 section, and instead use a dynamic relocation to point into the
5d35169e
AM
2733 shared lib. With code that gcc generates, it's vital that this be
2734 enabled; In the PowerPC64 ABI, the address of a function is actually
2735 the address of a function descriptor, which resides in the .opd
2736 section. gcc uses the descriptor directly rather than going via the
2737 GOT as some other ABI's do, which means that initialized function
2738 pointers must reference the descriptor. Thus, a function pointer
2739 initialized to the address of a function in a shared library will
2740 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 2741 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
2742 presents a problem as a plt entry for that function is also
2743 initialized from the function descriptor symbol and the copy reloc
2744 may not be initialized first. */
a23b6845 2745#define ELIMINATE_COPY_RELOCS 1
f4656909 2746
721956f4 2747/* Section name for stubs is the associated section name plus this
29942be8
NC
2748 string. */
2749#define STUB_SUFFIX ".stub"
721956f4
AM
2750
2751/* Linker stubs.
2752 ppc_stub_long_branch:
2753 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2754 destination, but a 24 bit branch in a stub section will reach.
2755 . b dest
2756
2757 ppc_stub_plt_branch:
2758 Similar to the above, but a 24 bit branch in the stub section won't
2759 reach its destination.
71a39c98
AM
2760 . addis %r11,%r2,xxx@toc@ha
2761 . ld %r12,xxx@toc@l(%r11)
2762 . mtctr %r12
721956f4
AM
2763 . bctr
2764
2765 ppc_stub_plt_call:
2c66dc6c
AM
2766 Used to call a function in a shared library. If it so happens that
2767 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2768 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 2769 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
2770 . addis %r11,%r2,xxx@toc@ha
2771 . ld %r12,xxx+0@toc@l(%r11)
2772 . mtctr %r12
2773 . ld %r2,xxx+8@toc@l(%r11)
2774 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2775 . bctr
ad8e1ba5
AM
2776
2777 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2778 code to adjust the value and save r2 to support multiple toc sections.
2779 A ppc_stub_long_branch with an r2 offset looks like:
2780 . std %r2,40(%r1)
2781 . addis %r2,%r2,off@ha
2782 . addi %r2,%r2,off@l
2783 . b dest
2784
2785 A ppc_stub_plt_branch with an r2 offset looks like:
2786 . std %r2,40(%r1)
71a39c98
AM
2787 . addis %r11,%r2,xxx@toc@ha
2788 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
2789 . addis %r2,%r2,off@ha
2790 . addi %r2,%r2,off@l
71a39c98 2791 . mtctr %r12
ad8e1ba5 2792 . bctr
ac2df442 2793
05d0e962
AM
2794 All of the above stubs are shown as their ELFv1 variants. ELFv2
2795 variants exist too, simpler for plt calls since a new toc pointer
2796 and static chain are not loaded by the stub. In addition, ELFv2
2797 has some more complex stubs to handle calls marked with NOTOC
2798 relocs from functions where r2 is not a valid toc pointer. These
2799 come in two flavours, the ones shown below, and _both variants that
2800 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2801 one call is from a function where r2 is used as the toc pointer but
2802 needs a toc adjusting stub for small-model multi-toc, and another
2803 call is from a function where r2 is not valid.
2804 ppc_stub_long_branch_notoc:
2805 . mflr %r12
2806 . bcl 20,31,1f
2807 . 1:
2808 . mflr %r11
2809 . mtlr %r12
2cdcc330
AM
2810 . addis %r12,%r11,dest-1b@ha
2811 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2812 . b dest
2813
2814 ppc_stub_plt_branch_notoc:
2815 . mflr %r12
2816 . bcl 20,31,1f
2817 . 1:
2818 . mflr %r11
2819 . mtlr %r12
2820 . lis %r12,xxx-1b@highest
f891966f 2821 . ori %r12,%r12,xxx-1b@higher
05d0e962 2822 . sldi %r12,%r12,32
f891966f 2823 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2824 . ori %r12,%r12,xxx-1b@l
2825 . add %r12,%r11,%r12
2826 . mtctr %r12
2827 . bctr
2828
2829 ppc_stub_plt_call_notoc:
2830 . mflr %r12
2831 . bcl 20,31,1f
2832 . 1:
2833 . mflr %r11
2834 . mtlr %r12
2835 . lis %r12,xxx-1b@highest
f891966f 2836 . ori %r12,%r12,xxx-1b@higher
05d0e962 2837 . sldi %r12,%r12,32
f891966f 2838 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2839 . ori %r12,%r12,xxx-1b@l
2840 . ldx %r12,%r11,%r12
2841 . mtctr %r12
2842 . bctr
2843
2844 In cases where the high instructions would add zero, they are
2845 omitted and following instructions modified in some cases.
2846
2847 For a given stub group (a set of sections all using the same toc
2848 pointer value) there will be just one stub type used for any
2849 particular function symbol. For example, if printf is called from
2850 code with the tocsave optimization (ie. r2 saved in function
2851 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2852 and from other code without the tocsave optimization requiring a
2853 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2854 type will be created. Calls with the tocsave optimization will
2855 enter this stub after the instruction saving r2. A similar
2856 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2857 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2858 to call an external function like printf. If other calls to printf
2859 require a ppc_stub_plt_call linkage stub then a single
2860 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2861 call. If other calls to printf require a ppc_stub_plt_call_r2save
2862 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2863 be created and calls not requiring r2 to be saved will enter the
2864 stub after the r2 save instruction. There is an analogous
2865 hierarchy of long branch and plt branch stubs for local call
2866 linkage. */
721956f4 2867
2cdcc330
AM
2868enum ppc_stub_type
2869{
721956f4
AM
2870 ppc_stub_none,
2871 ppc_stub_long_branch,
ad8e1ba5 2872 ppc_stub_long_branch_r2off,
05d0e962
AM
2873 ppc_stub_long_branch_notoc,
2874 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 2875 ppc_stub_plt_branch,
ad8e1ba5 2876 ppc_stub_plt_branch_r2off,
05d0e962
AM
2877 ppc_stub_plt_branch_notoc,
2878 ppc_stub_plt_branch_both,
794e51c0 2879 ppc_stub_plt_call,
7341d5e2 2880 ppc_stub_plt_call_r2save,
05d0e962
AM
2881 ppc_stub_plt_call_notoc,
2882 ppc_stub_plt_call_both,
a4b6fadd
AM
2883 ppc_stub_global_entry,
2884 ppc_stub_save_res
721956f4
AM
2885};
2886
6f20ed8a
AM
2887/* Information on stub grouping. */
2888struct map_stub
2889{
2890 /* The stub section. */
2891 asection *stub_sec;
2892 /* This is the section to which stubs in the group will be attached. */
2893 asection *link_sec;
a4b6fadd
AM
2894 /* Next group. */
2895 struct map_stub *next;
2896 /* Whether to emit a copy of register save/restore functions in this
2897 group. */
2898 int needs_save_res;
df136d64
AM
2899 /* Current offset within stubs after the insn restoring lr in a
2900 _notoc or _both stub using bcl for pc-relative addressing, or
2901 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2902 unsigned int lr_restore;
2903 /* Accumulated size of EH info emitted to describe return address
2904 if stubs modify lr. Does not include 17 byte FDE header. */
2905 unsigned int eh_size;
2906 /* Offset in glink_eh_frame to the start of EH info for this group. */
2907 unsigned int eh_base;
6f20ed8a
AM
2908};
2909
2cdcc330
AM
2910struct ppc_stub_hash_entry
2911{
721956f4
AM
2912 /* Base hash table entry structure. */
2913 struct bfd_hash_entry root;
2914
ad8e1ba5
AM
2915 enum ppc_stub_type stub_type;
2916
6f20ed8a
AM
2917 /* Group information. */
2918 struct map_stub *group;
721956f4
AM
2919
2920 /* Offset within stub_sec of the beginning of this stub. */
2921 bfd_vma stub_offset;
2922
2923 /* Given the symbol's value and its section we can determine its final
2924 value when building the stubs (so the stub knows where to jump. */
2925 bfd_vma target_value;
2926 asection *target_section;
2927
721956f4
AM
2928 /* The symbol table entry, if any, that this was derived from. */
2929 struct ppc_link_hash_entry *h;
e054468f 2930 struct plt_entry *plt_ent;
721956f4 2931
2d7ad24e
AM
2932 /* Symbol type. */
2933 unsigned char symtype;
2934
6911b7dc
AM
2935 /* Symbol st_other. */
2936 unsigned char other;
721956f4
AM
2937};
2938
2cdcc330
AM
2939struct ppc_branch_hash_entry
2940{
721956f4
AM
2941 /* Base hash table entry structure. */
2942 struct bfd_hash_entry root;
2943
c456f082 2944 /* Offset within branch lookup table. */
721956f4
AM
2945 unsigned int offset;
2946
2947 /* Generation marker. */
2948 unsigned int iter;
2949};
65f38f15 2950
19e08130
AM
2951/* Used to track dynamic relocations for local symbols. */
2952struct ppc_dyn_relocs
2953{
2954 struct ppc_dyn_relocs *next;
2955
2956 /* The input section of the reloc. */
2957 asection *sec;
2958
2959 /* Total number of relocs copied for the input section. */
2960 unsigned int count : 31;
2961
2962 /* Whether this entry is for STT_GNU_IFUNC symbols. */
2963 unsigned int ifunc : 1;
2964};
2965
65f38f15
AM
2966struct ppc_link_hash_entry
2967{
2968 struct elf_link_hash_entry elf;
2969
2cdcc330
AM
2970 union
2971 {
b3fac117
AM
2972 /* A pointer to the most recently used stub hash entry against this
2973 symbol. */
2974 struct ppc_stub_hash_entry *stub_cache;
2975
2976 /* A pointer to the next symbol starting with a '.' */
2977 struct ppc_link_hash_entry *next_dot_sym;
2978 } u;
721956f4 2979
65f38f15 2980 /* Track dynamic relocs copied for this symbol. */
6061a67d 2981 struct elf_dyn_relocs *dyn_relocs;
e86ce104 2982
721956f4 2983 /* Link between function code and descriptor symbols. */
34814b9f 2984 struct ppc_link_hash_entry *oh;
721956f4 2985
e86ce104
AM
2986 /* Flag function code and descriptor symbols. */
2987 unsigned int is_func:1;
2988 unsigned int is_func_descriptor:1;
908b32fc 2989 unsigned int fake:1;
411e1bfb 2990
c5614fa4
AM
2991 /* Whether global opd/toc sym has been adjusted or not.
2992 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
2993 should be set for all globals defined in any opd/toc section. */
754021d0
AM
2994 unsigned int adjust_done:1;
2995
a4b6fadd
AM
2996 /* Set if this is an out-of-line register save/restore function,
2997 with non-standard calling convention. */
2998 unsigned int save_res:1;
2999
8b5f1ed8
AM
3000 /* Set if a duplicate symbol with non-zero localentry is detected,
3001 even when the duplicate symbol does not provide a definition. */
3002 unsigned int non_zero_localentry:1;
3003
411e1bfb 3004 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3005 Bits are or'd into the mask as the corresponding relocs are
3006 encountered during check_relocs, with TLS_TLS being set when any
3007 of the other TLS bits are set. tls_optimize clears bits when
3008 optimizing to indicate the corresponding GOT entry type is not
3009 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3010 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
3011 separate flag rather than setting TPREL just for convenience in
3012 distinguishing the two cases.
3013 These flags are also kept for local symbols. */
3014#define TLS_TLS 1 /* Any TLS reloc. */
3015#define TLS_GD 2 /* GD reloc. */
3016#define TLS_LD 4 /* LD reloc. */
3017#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3018#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3019#define TLS_MARK 32 /* __tls_get_addr call marked. */
e7b938ca 3020#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
37da22e5 3021#define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
f961d9dd 3022 unsigned char tls_mask;
37da22e5
AM
3023
3024 /* The above field is also used to mark function symbols. In which
3025 case TLS_TLS will be 0. */
3026#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3027#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3028#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3029};
3030
3031/* ppc64 ELF linker hash table. */
3032
3033struct ppc_link_hash_table
3034{
3035 struct elf_link_hash_table elf;
3036
721956f4
AM
3037 /* The stub hash table. */
3038 struct bfd_hash_table stub_hash_table;
3039
3040 /* Another hash table for plt_branch stubs. */
3041 struct bfd_hash_table branch_hash_table;
3042
3b421ab3
AM
3043 /* Hash table for function prologue tocsave. */
3044 htab_t tocsave_htab;
3045
e7d1c40c
AM
3046 /* Various options and other info passed from the linker. */
3047 struct ppc64_elf_params *params;
721956f4 3048
6f20ed8a
AM
3049 /* The size of sec_info below. */
3050 unsigned int sec_info_arr_size;
3051
3052 /* Per-section array of extra section info. Done this way rather
3053 than as part of ppc64_elf_section_data so we have the info for
3054 non-ppc64 sections. */
3055 struct
3056 {
3057 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3058 bfd_vma toc_off;
6f20ed8a
AM
3059
3060 union
3061 {
3062 /* The section group that this section belongs to. */
3063 struct map_stub *group;
3064 /* A temp section list pointer. */
3065 asection *list;
3066 } u;
3067 } *sec_info;
721956f4 3068
a4b6fadd
AM
3069 /* Linked list of groups. */
3070 struct map_stub *group;
3071
ad8e1ba5
AM
3072 /* Temp used when calculating TOC pointers. */
3073 bfd_vma toc_curr;
bf102f86
AM
3074 bfd *toc_bfd;
3075 asection *toc_first_sec;
ad8e1ba5 3076
b3fac117
AM
3077 /* Used when adding symbols. */
3078 struct ppc_link_hash_entry *dot_syms;
3079
33e44f2e 3080 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3081 asection *glink;
9e390558 3082 asection *global_entry;
82bd7b59 3083 asection *sfpr;
2d7ad24e
AM
3084 asection *pltlocal;
3085 asection *relpltlocal;
4ce794b7
AM
3086 asection *brlt;
3087 asection *relbrlt;
58d180e8 3088 asection *glink_eh_frame;
ec338859 3089
8387904d
AM
3090 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3091 struct ppc_link_hash_entry *tls_get_addr;
3092 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3093
927be08e
AM
3094 /* The size of reliplt used by got entry relocs. */
3095 bfd_size_type got_reli_size;
3096
9b5ecbd0 3097 /* Statistics. */
7341d5e2 3098 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 3099
ee75fd95
AM
3100 /* Number of stubs against global syms. */
3101 unsigned long stub_globals;
3102
ee67d69a
AM
3103 /* Set if we're linking code with function descriptors. */
3104 unsigned int opd_abi:1;
3105
4c52953f 3106 /* Support for multiple toc sections. */
33c0ec9d 3107 unsigned int do_multi_toc:1;
4c52953f 3108 unsigned int multi_toc_needed:1;
927be08e 3109 unsigned int second_toc_pass:1;
67f0cbdb 3110 unsigned int do_toc_opt:1;
4c52953f 3111
9a23f96e
AM
3112 /* Set if tls optimization is enabled. */
3113 unsigned int do_tls_opt:1;
3114
3e04d765
AM
3115 /* Set if inline plt calls should be converted to direct calls. */
3116 unsigned int can_convert_all_inline_plt:1;
3117
5d1634d7 3118 /* Set on error. */
99877b66 3119 unsigned int stub_error:1;
721956f4 3120
8c5b4e52
AM
3121 /* Whether func_desc_adjust needs to be run over symbols. */
3122 unsigned int need_func_desc_adj:1;
721956f4 3123
82e66161
AM
3124 /* Whether there exist local gnu indirect function resolvers,
3125 referenced by dynamic relocations. */
3126 unsigned int local_ifunc_resolver:1;
3127 unsigned int maybe_local_ifunc_resolver:1;
3128
f378ab09
AM
3129 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3130 unsigned int has_plt_localentry0:1;
3131
5663e321
AM
3132 /* Whether calls are made via the PLT from NOTOC functions. */
3133 unsigned int notoc_plt:1;
3134
721956f4
AM
3135 /* Incremented every time we size stubs. */
3136 unsigned int stub_iteration;
5d1634d7 3137
87d72d41
AM
3138 /* Small local sym cache. */
3139 struct sym_cache sym_cache;
65f38f15
AM
3140};
3141
4c52953f
AM
3142/* Rename some of the generic section flags to better document how they
3143 are used here. */
b0dddeec
AM
3144
3145/* Nonzero if this section has TLS related relocations. */
3146#define has_tls_reloc sec_flg0
3147
23cedd1d 3148/* Nonzero if this section has an old-style call to __tls_get_addr. */
b0dddeec
AM
3149#define has_tls_get_addr_call sec_flg1
3150
3151/* Nonzero if this section has any toc or got relocs. */
3152#define has_toc_reloc sec_flg2
3153
3154/* Nonzero if this section has a call to another section that uses
3155 the toc or got. */
d77c8a4b 3156#define makes_toc_func_call sec_flg3
b0dddeec
AM
3157
3158/* Recursion protection when determining above flag. */
d77c8a4b 3159#define call_check_in_progress sec_flg4
70cc837d 3160#define call_check_done sec_flg5
4c52953f 3161
65f38f15
AM
3162/* Get the ppc64 ELF linker hash table from a link_info structure. */
3163
3164#define ppc_hash_table(p) \
4dfe6ac6
NC
3165 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3166 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3167
721956f4
AM
3168#define ppc_stub_hash_lookup(table, string, create, copy) \
3169 ((struct ppc_stub_hash_entry *) \
3170 bfd_hash_lookup ((table), (string), (create), (copy)))
3171
3172#define ppc_branch_hash_lookup(table, string, create, copy) \
3173 ((struct ppc_branch_hash_entry *) \
3174 bfd_hash_lookup ((table), (string), (create), (copy)))
3175
3176/* Create an entry in the stub hash table. */
3177
3178static struct bfd_hash_entry *
4ce794b7
AM
3179stub_hash_newfunc (struct bfd_hash_entry *entry,
3180 struct bfd_hash_table *table,
3181 const char *string)
721956f4
AM
3182{
3183 /* Allocate the structure if it has not already been allocated by a
3184 subclass. */
3185 if (entry == NULL)
3186 {
3187 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3188 if (entry == NULL)
3189 return entry;
3190 }
3191
3192 /* Call the allocation method of the superclass. */
3193 entry = bfd_hash_newfunc (entry, table, string);
3194 if (entry != NULL)
3195 {
3196 struct ppc_stub_hash_entry *eh;
3197
3198 /* Initialize the local fields. */
3199 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3200 eh->stub_type = ppc_stub_none;
6f20ed8a 3201 eh->group = NULL;
721956f4
AM
3202 eh->stub_offset = 0;
3203 eh->target_value = 0;
3204 eh->target_section = NULL;
721956f4 3205 eh->h = NULL;
6911b7dc 3206 eh->plt_ent = NULL;
6911b7dc 3207 eh->other = 0;
721956f4
AM
3208 }
3209
3210 return entry;
3211}
3212
3213/* Create an entry in the branch hash table. */
3214
3215static struct bfd_hash_entry *
4ce794b7
AM
3216branch_hash_newfunc (struct bfd_hash_entry *entry,
3217 struct bfd_hash_table *table,
3218 const char *string)
721956f4
AM
3219{
3220 /* Allocate the structure if it has not already been allocated by a
3221 subclass. */
3222 if (entry == NULL)
3223 {
3224 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3225 if (entry == NULL)
3226 return entry;
3227 }
3228
3229 /* Call the allocation method of the superclass. */
3230 entry = bfd_hash_newfunc (entry, table, string);
3231 if (entry != NULL)
3232 {
3233 struct ppc_branch_hash_entry *eh;
3234
3235 /* Initialize the local fields. */
3236 eh = (struct ppc_branch_hash_entry *) entry;
3237 eh->offset = 0;
3238 eh->iter = 0;
3239 }
3240
3241 return entry;
3242}
3243
65f38f15
AM
3244/* Create an entry in a ppc64 ELF linker hash table. */
3245
3246static struct bfd_hash_entry *
4ce794b7
AM
3247link_hash_newfunc (struct bfd_hash_entry *entry,
3248 struct bfd_hash_table *table,
3249 const char *string)
65f38f15
AM
3250{
3251 /* Allocate the structure if it has not already been allocated by a
3252 subclass. */
3253 if (entry == NULL)
3254 {
3255 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3256 if (entry == NULL)
3257 return entry;
3258 }
3259
3260 /* Call the allocation method of the superclass. */
3261 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3262 if (entry != NULL)
3263 {
3264 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3265
b3fac117 3266 memset (&eh->u.stub_cache, 0,
908b32fc 3267 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3268 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3269
3270 /* When making function calls, old ABI code references function entry
3271 points (dot symbols), while new ABI code references the function
3272 descriptor symbol. We need to make any combination of reference and
3273 definition work together, without breaking archive linking.
3274
3275 For a defined function "foo" and an undefined call to "bar":
3276 An old object defines "foo" and ".foo", references ".bar" (possibly
3277 "bar" too).
3278 A new object defines "foo" and references "bar".
3279
3280 A new object thus has no problem with its undefined symbols being
3281 satisfied by definitions in an old object. On the other hand, the
3282 old object won't have ".bar" satisfied by a new object.
3283
3284 Keep a list of newly added dot-symbols. */
3285
3286 if (string[0] == '.')
3287 {
3288 struct ppc_link_hash_table *htab;
3289
3290 htab = (struct ppc_link_hash_table *) table;
3291 eh->u.next_dot_sym = htab->dot_syms;
3292 htab->dot_syms = eh;
3293 }
65f38f15
AM
3294 }
3295
3296 return entry;
3297}
3298
2cdcc330
AM
3299struct tocsave_entry
3300{
3b421ab3
AM
3301 asection *sec;
3302 bfd_vma offset;
3303};
3304
3305static hashval_t
3306tocsave_htab_hash (const void *p)
3307{
3308 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3309 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3310}
3311
3312static int
3313tocsave_htab_eq (const void *p1, const void *p2)
3314{
3315 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3316 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3317 return e1->sec == e2->sec && e1->offset == e2->offset;
3318}
3319
68faa637
AM
3320/* Destroy a ppc64 ELF linker hash table. */
3321
3322static void
d495ab0d 3323ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3324{
d495ab0d 3325 struct ppc_link_hash_table *htab;
68faa637 3326
d495ab0d 3327 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3328 if (htab->tocsave_htab)
3329 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3330 bfd_hash_table_free (&htab->branch_hash_table);
3331 bfd_hash_table_free (&htab->stub_hash_table);
3332 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3333}
3334
65f38f15
AM
3335/* Create a ppc64 ELF linker hash table. */
3336
3337static struct bfd_link_hash_table *
4ce794b7 3338ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3339{
3340 struct ppc_link_hash_table *htab;
3341 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3342
4ce794b7 3343 htab = bfd_zmalloc (amt);
65f38f15
AM
3344 if (htab == NULL)
3345 return NULL;
3346
66eb6687 3347 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3348 sizeof (struct ppc_link_hash_entry),
3349 PPC64_ELF_DATA))
65f38f15 3350 {
e2d34d7d 3351 free (htab);
65f38f15
AM
3352 return NULL;
3353 }
3354
721956f4 3355 /* Init the stub hash table too. */
66eb6687
AM
3356 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3357 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3358 {
d495ab0d 3359 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3360 return NULL;
3361 }
721956f4
AM
3362
3363 /* And the branch hash table. */
66eb6687
AM
3364 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3365 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3366 {
3367 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3368 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3369 return NULL;
3370 }
721956f4 3371
3b421ab3
AM
3372 htab->tocsave_htab = htab_try_create (1024,
3373 tocsave_htab_hash,
3374 tocsave_htab_eq,
3375 NULL);
3376 if (htab->tocsave_htab == NULL)
2915c55b 3377 {
d495ab0d 3378 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3379 return NULL;
3380 }
d495ab0d 3381 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3382
3254fd24
AM
3383 /* Initializing two fields of the union is just cosmetic. We really
3384 only care about glist, but when compiled on a 32-bit host the
3385 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3386 debugger inspection of these fields look nicer. */
a6aa5195
AM
3387 htab->elf.init_got_refcount.refcount = 0;
3388 htab->elf.init_got_refcount.glist = NULL;
3389 htab->elf.init_plt_refcount.refcount = 0;
3390 htab->elf.init_plt_refcount.glist = NULL;
3391 htab->elf.init_got_offset.offset = 0;
3392 htab->elf.init_got_offset.glist = NULL;
3393 htab->elf.init_plt_offset.offset = 0;
3394 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3395
65f38f15
AM
3396 return &htab->elf.root;
3397}
3398
bfeb4a28
AM
3399/* Create sections for linker generated code. */
3400
3401static bfd_boolean
3402create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3403{
3404 struct ppc_link_hash_table *htab;
3405 flagword flags;
3406
3407 htab = ppc_hash_table (info);
3408
bfeb4a28
AM
3409 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3410 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3411 if (htab->params->save_restore_funcs)
3412 {
3413 /* Create .sfpr for code to save and restore fp regs. */
3414 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3415 flags);
3416 if (htab->sfpr == NULL
2cdcc330 3417 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b32547cd
AM
3418 return FALSE;
3419 }
3420
3421 if (bfd_link_relocatable (info))
3422 return TRUE;
bfeb4a28
AM
3423
3424 /* Create .glink for lazy dynamic linking support. */
3425 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3426 flags);
3427 if (htab->glink == NULL
2cdcc330 3428 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
bfeb4a28
AM
3429 return FALSE;
3430
9e390558
AM
3431 /* The part of .glink used by global entry stubs, separate so that
3432 it can be aligned appropriately without affecting htab->glink. */
3433 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3434 flags);
3435 if (htab->global_entry == NULL
2cdcc330 3436 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
9e390558
AM
3437 return FALSE;
3438
bfeb4a28
AM
3439 if (!info->no_ld_generated_unwind_info)
3440 {
3441 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3442 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3443 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3444 ".eh_frame",
3445 flags);
3446 if (htab->glink_eh_frame == NULL
3447 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3448 return FALSE;
3449 }
3450
3451 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3452 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3453 if (htab->elf.iplt == NULL
2cdcc330 3454 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
3455 return FALSE;
3456
3457 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3458 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3459 htab->elf.irelplt
3460 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3461 if (htab->elf.irelplt == NULL
2cdcc330 3462 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
3463 return FALSE;
3464
3465 /* Create branch lookup table for plt_branch stubs. */
3466 flags = (SEC_ALLOC | SEC_LOAD
3467 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3468 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3469 flags);
3470 if (htab->brlt == NULL
2cdcc330 3471 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
bfeb4a28
AM
3472 return FALSE;
3473
2d7ad24e
AM
3474 /* Local plt entries, put in .branch_lt but a separate section for
3475 convenience. */
3476 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3477 flags);
3478 if (htab->pltlocal == NULL
2cdcc330 3479 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
2d7ad24e
AM
3480 return FALSE;
3481
0e1862bb 3482 if (!bfd_link_pic (info))
bfeb4a28
AM
3483 return TRUE;
3484
3485 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3486 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3487 htab->relbrlt
3488 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3489 if (htab->relbrlt == NULL
2cdcc330 3490 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
bfeb4a28
AM
3491 return FALSE;
3492
2d7ad24e
AM
3493 htab->relpltlocal
3494 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3495 if (htab->relpltlocal == NULL
2cdcc330 3496 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
2d7ad24e
AM
3497 return FALSE;
3498
bfeb4a28
AM
3499 return TRUE;
3500}
3501
e717da7e
AM
3502/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3503
bfeb4a28 3504bfd_boolean
e7d1c40c
AM
3505ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3506 struct ppc64_elf_params *params)
e717da7e
AM
3507{
3508 struct ppc_link_hash_table *htab;
3509
e7d1c40c 3510 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3511
3512/* Always hook our dynamic sections into the first bfd, which is the
3513 linker created stub bfd. This ensures that the GOT header is at
3514 the start of the output TOC section. */
3515 htab = ppc_hash_table (info);
e7d1c40c
AM
3516 htab->elf.dynobj = params->stub_bfd;
3517 htab->params = params;
bfeb4a28 3518
bfeb4a28 3519 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3520}
3521
721956f4
AM
3522/* Build a name for an entry in the stub hash table. */
3523
3524static char *
4ce794b7
AM
3525ppc_stub_name (const asection *input_section,
3526 const asection *sym_sec,
3527 const struct ppc_link_hash_entry *h,
3528 const Elf_Internal_Rela *rel)
721956f4
AM
3529{
3530 char *stub_name;
bcaa2f82 3531 ssize_t len;
721956f4
AM
3532
3533 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3534 offsets from a sym as a branch target? In fact, we could
3535 probably assume the addend is always zero. */
3536 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3537
3538 if (h)
3539 {
3540 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3541 stub_name = bfd_malloc (len);
46de2a7c
AM
3542 if (stub_name == NULL)
3543 return stub_name;
3544
bcaa2f82
AM
3545 len = sprintf (stub_name, "%08x.%s+%x",
3546 input_section->id & 0xffffffff,
3547 h->elf.root.root.string,
3548 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3549 }
3550 else
3551 {
ad8e1ba5 3552 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3553 stub_name = bfd_malloc (len);
46de2a7c
AM
3554 if (stub_name == NULL)
3555 return stub_name;
3556
bcaa2f82
AM
3557 len = sprintf (stub_name, "%08x.%x:%x+%x",
3558 input_section->id & 0xffffffff,
3559 sym_sec->id & 0xffffffff,
3560 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3561 (int) rel->r_addend & 0xffffffff);
721956f4 3562 }
bcaa2f82 3563 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3564 stub_name[len - 2] = 0;
721956f4
AM
3565 return stub_name;
3566}
3567
3568/* Look up an entry in the stub hash. Stub entries are cached because
3569 creating the stub name takes a bit of time. */
3570
3571static struct ppc_stub_hash_entry *
4ce794b7
AM
3572ppc_get_stub_entry (const asection *input_section,
3573 const asection *sym_sec,
039b3fef 3574 struct ppc_link_hash_entry *h,
4ce794b7
AM
3575 const Elf_Internal_Rela *rel,
3576 struct ppc_link_hash_table *htab)
721956f4
AM
3577{
3578 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3579 struct map_stub *group;
721956f4
AM
3580
3581 /* If this input section is part of a group of sections sharing one
3582 stub section, then use the id of the first section in the group.
3583 Stub names need to include a section id, as there may well be
3584 more than one stub used to reach say, printf, and we need to
3585 distinguish between them. */
6f20ed8a 3586 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3587 if (group == NULL)
3588 return NULL;
721956f4 3589
b3fac117
AM
3590 if (h != NULL && h->u.stub_cache != NULL
3591 && h->u.stub_cache->h == h
6f20ed8a 3592 && h->u.stub_cache->group == group)
721956f4 3593 {
b3fac117 3594 stub_entry = h->u.stub_cache;
721956f4
AM
3595 }
3596 else
3597 {
3598 char *stub_name;
3599
6f20ed8a 3600 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3601 if (stub_name == NULL)
3602 return NULL;
3603
3604 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3605 stub_name, FALSE, FALSE);
721956f4 3606 if (h != NULL)
b3fac117 3607 h->u.stub_cache = stub_entry;
721956f4
AM
3608
3609 free (stub_name);
3610 }
3611
3612 return stub_entry;
3613}
3614
3615/* Add a new stub entry to the stub hash. Not all fields of the new
3616 stub entry are initialised. */
3617
3618static struct ppc_stub_hash_entry *
4ce794b7
AM
3619ppc_add_stub (const char *stub_name,
3620 asection *section,
25f53a85 3621 struct bfd_link_info *info)
721956f4 3622{
25f53a85 3623 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3624 struct map_stub *group;
721956f4
AM
3625 asection *link_sec;
3626 asection *stub_sec;
3627 struct ppc_stub_hash_entry *stub_entry;
3628
6f20ed8a
AM
3629 group = htab->sec_info[section->id].u.group;
3630 link_sec = group->link_sec;
3631 stub_sec = group->stub_sec;
721956f4
AM
3632 if (stub_sec == NULL)
3633 {
6f20ed8a
AM
3634 size_t namelen;
3635 bfd_size_type len;
3636 char *s_name;
721956f4 3637
6f20ed8a
AM
3638 namelen = strlen (link_sec->name);
3639 len = namelen + sizeof (STUB_SUFFIX);
3640 s_name = bfd_alloc (htab->params->stub_bfd, len);
3641 if (s_name == NULL)
3642 return NULL;
721956f4 3643
6f20ed8a
AM
3644 memcpy (s_name, link_sec->name, namelen);
3645 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3646 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3647 if (stub_sec == NULL)
3648 return NULL;
3649 group->stub_sec = stub_sec;
721956f4
AM
3650 }
3651
3652 /* Enter this entry into the linker stub hash table. */
3653 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3654 TRUE, FALSE);
721956f4
AM
3655 if (stub_entry == NULL)
3656 {
695344c0 3657 /* xgettext:c-format */
cf97bcb0
AM
3658 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3659 section->owner, stub_name);
721956f4
AM
3660 return NULL;
3661 }
3662
6f20ed8a 3663 stub_entry->group = group;
721956f4 3664 stub_entry->stub_offset = 0;
721956f4
AM
3665 return stub_entry;
3666}
3667
e717da7e
AM
3668/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3669 not already done. */
65f38f15 3670
b34976b6 3671static bfd_boolean
e717da7e 3672create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3673{
e717da7e
AM
3674 asection *got, *relgot;
3675 flagword flags;
3676 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3677
0c8d6e5c 3678 if (!is_ppc64_elf (abfd))
0ffa91dd 3679 return FALSE;
4dfe6ac6
NC
3680 if (htab == NULL)
3681 return FALSE;
0ffa91dd 3682
33e44f2e
AM
3683 if (!htab->elf.sgot
3684 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3685 return FALSE;
e717da7e
AM
3686
3687 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3688 | SEC_LINKER_CREATED);
3689
c456f082 3690 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3691 if (!got
e717da7e
AM
3692 || !bfd_set_section_alignment (abfd, got, 3))
3693 return FALSE;
65f38f15 3694
c456f082
AM
3695 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3696 flags | SEC_READONLY);
e717da7e 3697 if (!relgot
2cdcc330 3698 || !bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 3699 return FALSE;
e717da7e
AM
3700
3701 ppc64_elf_tdata (abfd)->got = got;
3702 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 3703 return TRUE;
65f38f15 3704}
5bd4f169 3705
b31867b6
AM
3706/* Follow indirect and warning symbol links. */
3707
3708static inline struct bfd_link_hash_entry *
3709follow_link (struct bfd_link_hash_entry *h)
3710{
3711 while (h->type == bfd_link_hash_indirect
3712 || h->type == bfd_link_hash_warning)
3713 h = h->u.i.link;
3714 return h;
3715}
3716
3717static inline struct elf_link_hash_entry *
3718elf_follow_link (struct elf_link_hash_entry *h)
3719{
3720 return (struct elf_link_hash_entry *) follow_link (&h->root);
3721}
3722
3723static inline struct ppc_link_hash_entry *
3724ppc_follow_link (struct ppc_link_hash_entry *h)
3725{
3726 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3727}
3728
40d16e0b
AM
3729/* Merge PLT info on FROM with that on TO. */
3730
3731static void
3732move_plt_plist (struct ppc_link_hash_entry *from,
3733 struct ppc_link_hash_entry *to)
3734{
3735 if (from->elf.plt.plist != NULL)
3736 {
3737 if (to->elf.plt.plist != NULL)
3738 {
3739 struct plt_entry **entp;
3740 struct plt_entry *ent;
3741
3742 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3743 {
3744 struct plt_entry *dent;
3745
3746 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3747 if (dent->addend == ent->addend)
3748 {
3749 dent->plt.refcount += ent->plt.refcount;
3750 *entp = ent->next;
3751 break;
3752 }
3753 if (dent == NULL)
3754 entp = &ent->next;
3755 }
3756 *entp = to->elf.plt.plist;
3757 }
3758
3759 to->elf.plt.plist = from->elf.plt.plist;
3760 from->elf.plt.plist = NULL;
3761 }
3762}
3763
65f38f15
AM
3764/* Copy the extra info we tack onto an elf_link_hash_entry. */
3765
3766static void
fcfa13d2
AM
3767ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3768 struct elf_link_hash_entry *dir,
3769 struct elf_link_hash_entry *ind)
65f38f15
AM
3770{
3771 struct ppc_link_hash_entry *edir, *eind;
3772
3773 edir = (struct ppc_link_hash_entry *) dir;
3774 eind = (struct ppc_link_hash_entry *) ind;
3775
c79d6685
AM
3776 edir->is_func |= eind->is_func;
3777 edir->is_func_descriptor |= eind->is_func_descriptor;
3778 edir->tls_mask |= eind->tls_mask;
3779 if (eind->oh != NULL)
3780 edir->oh = ppc_follow_link (eind->oh);
3781
474436e6 3782 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
3783 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3784 edir->elf.ref_regular |= eind->elf.ref_regular;
3785 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 3786 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
3787 edir->elf.needs_plt |= eind->elf.needs_plt;
3788 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 3789
d311bc8b
AM
3790 /* If we were called to copy over info for a weak sym, don't copy
3791 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3792 in order to simplify readonly_dynrelocs and save a field in the
3793 symbol hash entry, but that means dyn_relocs can't be used in any
3794 tests about a specific symbol, or affect other symbol flags which
ab2477e1 3795 are then tested. */
d311bc8b 3796 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 3797 return;
d311bc8b 3798
411e1bfb 3799 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 3800 if (eind->dyn_relocs != NULL)
65f38f15 3801 {
bbd7ec4a
AM
3802 if (edir->dyn_relocs != NULL)
3803 {
6061a67d
AM
3804 struct elf_dyn_relocs **pp;
3805 struct elf_dyn_relocs *p;
bbd7ec4a 3806
fcfa13d2 3807 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
3808 list. Merge any entries against the same section. */
3809 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3810 {
6061a67d 3811 struct elf_dyn_relocs *q;
bbd7ec4a
AM
3812
3813 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3814 if (q->sec == p->sec)
3815 {
3816 q->pc_count += p->pc_count;
3817 q->count += p->count;
3818 *pp = p->next;
3819 break;
3820 }
3821 if (q == NULL)
3822 pp = &p->next;
3823 }
3824 *pp = edir->dyn_relocs;
3825 }
3826
65f38f15
AM
3827 edir->dyn_relocs = eind->dyn_relocs;
3828 eind->dyn_relocs = NULL;
3829 }
65f38f15 3830
81848ca0
AM
3831 /* Copy over got entries that we may have already seen to the
3832 symbol which just became indirect. */
411e1bfb
AM
3833 if (eind->elf.got.glist != NULL)
3834 {
3835 if (edir->elf.got.glist != NULL)
3836 {
3837 struct got_entry **entp;
3838 struct got_entry *ent;
3839
3840 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3841 {
3842 struct got_entry *dent;
3843
3844 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3845 if (dent->addend == ent->addend
e717da7e 3846 && dent->owner == ent->owner
411e1bfb
AM
3847 && dent->tls_type == ent->tls_type)
3848 {
3849 dent->got.refcount += ent->got.refcount;
3850 *entp = ent->next;
3851 break;
3852 }
3853 if (dent == NULL)
3854 entp = &ent->next;
3855 }
3856 *entp = edir->elf.got.glist;
3857 }
3858
3859 edir->elf.got.glist = eind->elf.got.glist;
3860 eind->elf.got.glist = NULL;
3861 }
3862
3863 /* And plt entries. */
40d16e0b 3864 move_plt_plist (eind, edir);
411e1bfb 3865
fcfa13d2 3866 if (eind->elf.dynindx != -1)
411e1bfb 3867 {
fcfa13d2
AM
3868 if (edir->elf.dynindx != -1)
3869 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3870 edir->elf.dynstr_index);
411e1bfb
AM
3871 edir->elf.dynindx = eind->elf.dynindx;
3872 edir->elf.dynstr_index = eind->elf.dynstr_index;
3873 eind->elf.dynindx = -1;
3874 eind->elf.dynstr_index = 0;
3875 }
411e1bfb
AM
3876}
3877
8387904d
AM
3878/* Find the function descriptor hash entry from the given function code
3879 hash entry FH. Link the entries via their OH fields. */
3880
3881static struct ppc_link_hash_entry *
b31867b6 3882lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
3883{
3884 struct ppc_link_hash_entry *fdh = fh->oh;
3885
3886 if (fdh == NULL)
3887 {
3888 const char *fd_name = fh->elf.root.root.string + 1;
3889
3890 fdh = (struct ppc_link_hash_entry *)
3891 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
3892 if (fdh == NULL)
3893 return fdh;
3894
3895 fdh->is_func_descriptor = 1;
3896 fdh->oh = fh;
3897 fh->is_func = 1;
3898 fh->oh = fdh;
8387904d
AM
3899 }
3900
8c5b4e52
AM
3901 fdh = ppc_follow_link (fdh);
3902 fdh->is_func_descriptor = 1;
3903 fdh->oh = fh;
3904 return fdh;
8387904d
AM
3905}
3906
8c5b4e52 3907/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
3908
3909static struct ppc_link_hash_entry *
3910make_fdh (struct bfd_link_info *info,
908b32fc 3911 struct ppc_link_hash_entry *fh)
bb700d78 3912{
8c5b4e52
AM
3913 bfd *abfd = fh->elf.root.u.undef.abfd;
3914 struct bfd_link_hash_entry *bh = NULL;
bb700d78 3915 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
3916 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3917 ? BSF_WEAK
3918 : BSF_GLOBAL);
3919
3920 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3921 fh->elf.root.root.string + 1,
3922 flags, bfd_und_section_ptr, 0,
3923 NULL, FALSE, FALSE, &bh))
bb700d78
AM
3924 return NULL;
3925
3926 fdh = (struct ppc_link_hash_entry *) bh;
3927 fdh->elf.non_elf = 0;
908b32fc
AM
3928 fdh->fake = 1;
3929 fdh->is_func_descriptor = 1;
3930 fdh->oh = fh;
3931 fh->is_func = 1;
3932 fh->oh = fdh;
bb700d78
AM
3933 return fdh;
3934}
3935
8387904d
AM
3936/* Fix function descriptor symbols defined in .opd sections to be
3937 function type. */
555cd476
AM
3938
3939static bfd_boolean
c16153ae 3940ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 3941 struct bfd_link_info *info,
555cd476 3942 Elf_Internal_Sym *isym,
6911b7dc 3943 const char **name,
555cd476
AM
3944 flagword *flags ATTRIBUTE_UNUSED,
3945 asection **sec,
b53dfeb2 3946 bfd_vma *value)
555cd476 3947{
b53dfeb2 3948 if (*sec != NULL
f1885d1e 3949 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
3950 {
3951 asection *code_sec;
3952
3953 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3954 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
3955 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
3956
3957 /* If the symbol is a function defined in .opd, and the function
3958 code is in a discarded group, let it appear to be undefined. */
0e1862bb 3959 if (!bfd_link_relocatable (info)
b53dfeb2
AM
3960 && (*sec)->reloc_count != 0
3961 && opd_entry_value (*sec, *value, &code_sec, NULL,
3962 FALSE) != (bfd_vma) -1
3963 && discarded_section (code_sec))
3964 {
3965 *sec = bfd_und_section_ptr;
3966 isym->st_shndx = SHN_UNDEF;
3967 }
3968 }
dbd1e97e
AM
3969 else if (*sec != NULL
3970 && strcmp ((*sec)->name, ".toc") == 0
3971 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
3972 {
3973 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3974 if (htab != NULL)
3975 htab->params->object_in_toc = 1;
3976 }
433817dd 3977
6911b7dc
AM
3978 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3979 {
3980 if (abiversion (ibfd) == 0)
3981 set_abiversion (ibfd, 2);
3982 else if (abiversion (ibfd) == 1)
3983 {
cf97bcb0
AM
3984 _bfd_error_handler (_("symbol '%s' has invalid st_other"
3985 " for ABI version 1"), *name);
6911b7dc
AM
3986 bfd_set_error (bfd_error_bad_value);
3987 return FALSE;
3988 }
3989 }
3990
555cd476
AM
3991 return TRUE;
3992}
3993
6911b7dc
AM
3994/* Merge non-visibility st_other attributes: local entry point. */
3995
3996static void
3997ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
3998 const Elf_Internal_Sym *isym,
3999 bfd_boolean definition,
4000 bfd_boolean dynamic)
4001{
f378ab09 4002 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
4003 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4004 | ELF_ST_VISIBILITY (h->other));
4005}
4006
8c5b4e52
AM
4007/* Hook called on merging a symbol. We use this to clear "fake" since
4008 we now have a real symbol. */
4009
4010static bfd_boolean
4011ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4012 const Elf_Internal_Sym *isym,
8c5b4e52
AM
4013 asection **psec ATTRIBUTE_UNUSED,
4014 bfd_boolean newdef ATTRIBUTE_UNUSED,
4015 bfd_boolean olddef ATTRIBUTE_UNUSED,
4016 bfd *oldbfd ATTRIBUTE_UNUSED,
4017 const asection *oldsec ATTRIBUTE_UNUSED)
4018{
4019 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
4020 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4021 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
4022 return TRUE;
4023}
4024
8387904d 4025/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4026 inclusion of a new ABI object archive that defines "bar".
4027 NAME is a symbol defined in an archive. Return a symbol in the hash
4028 table that might be satisfied by the archive symbols. */
8387904d
AM
4029
4030static struct elf_link_hash_entry *
4031ppc64_elf_archive_symbol_lookup (bfd *abfd,
4032 struct bfd_link_info *info,
4033 const char *name)
4034{
4035 struct elf_link_hash_entry *h;
4036 char *dot_name;
4037 size_t len;
4038
4039 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4040 if (h != NULL
4041 /* Don't return this sym if it is a fake function descriptor
4042 created by add_symbol_adjust. */
8c5b4e52 4043 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
4044 return h;
4045
4046 if (name[0] == '.')
4047 return h;
4048
4049 len = strlen (name);
4050 dot_name = bfd_alloc (abfd, len + 2);
4051 if (dot_name == NULL)
e99955cd 4052 return (struct elf_link_hash_entry *) -1;
8387904d
AM
4053 dot_name[0] = '.';
4054 memcpy (dot_name + 1, name, len + 1);
4055 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4056 bfd_release (abfd, dot_name);
4057 return h;
4058}
4059
4060/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4061 new ABI object defines "bar". Well, at least, undefined dot symbols
4062 are made weak. This stops later archive searches from including an
4063 object if we already have a function descriptor definition. It also
35b0ce59
AM
4064 prevents the linker complaining about undefined symbols.
4065 We also check and correct mismatched symbol visibility here. The
4066 most restrictive visibility of the function descriptor and the
4067 function entry symbol is used. */
8387904d
AM
4068
4069static bfd_boolean
b3fac117 4070add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4071{
8387904d
AM
4072 struct ppc_link_hash_table *htab;
4073 struct ppc_link_hash_entry *fdh;
4074
b3fac117
AM
4075 if (eh->elf.root.type == bfd_link_hash_warning)
4076 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4077
8400d40d
AM
4078 if (eh->elf.root.type == bfd_link_hash_indirect)
4079 return TRUE;
4080
b3fac117
AM
4081 if (eh->elf.root.root.string[0] != '.')
4082 abort ();
8387904d 4083
b3fac117 4084 htab = ppc_hash_table (info);
4dfe6ac6
NC
4085 if (htab == NULL)
4086 return FALSE;
4087
b31867b6 4088 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4089 if (fdh == NULL
4090 && !bfd_link_relocatable (info)
4091 && (eh->elf.root.type == bfd_link_hash_undefined
4092 || eh->elf.root.type == bfd_link_hash_undefweak)
4093 && eh->elf.ref_regular)
4094 {
4095 /* Make an undefined function descriptor sym, in order to
4096 pull in an --as-needed shared lib. Archives are handled
4097 elsewhere. */
4098 fdh = make_fdh (info, eh);
4099 if (fdh == NULL)
4100 return FALSE;
bb700d78 4101 }
8c5b4e52
AM
4102
4103 if (fdh != NULL)
8387904d 4104 {
35b0ce59
AM
4105 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4106 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4107
4108 /* Make both descriptor and entry symbol have the most
4109 constraining visibility of either symbol. */
35b0ce59
AM
4110 if (entry_vis < descr_vis)
4111 fdh->elf.other += entry_vis - descr_vis;
4112 else if (entry_vis > descr_vis)
4113 eh->elf.other += descr_vis - entry_vis;
4114
8c5b4e52
AM
4115 /* Propagate reference flags from entry symbol to function
4116 descriptor symbol. */
bc4e12de 4117 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4118 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4119 fdh->elf.ref_regular |= eh->elf.ref_regular;
4120 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4121
4122 if (!fdh->elf.forced_local
4123 && fdh->elf.dynindx == -1
4124 && fdh->elf.versioned != versioned_hidden
4125 && (bfd_link_dll (info)
4126 || fdh->elf.def_dynamic
4127 || fdh->elf.ref_dynamic)
4128 && (eh->elf.ref_regular
4129 || eh->elf.def_regular))
4130 {
2cdcc330 4131 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
8c5b4e52 4132 return FALSE;
35b0ce59 4133 }
8387904d 4134 }
99877b66 4135
8387904d
AM
4136 return TRUE;
4137}
4138
f6c7c3e8
AM
4139/* Set up opd section info and abiversion for IBFD, and process list
4140 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4141
8387904d 4142static bfd_boolean
f6c7c3e8 4143ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4144{
99877b66 4145 struct ppc_link_hash_table *htab;
b3fac117 4146 struct ppc_link_hash_entry **p, *eh;
459609d6 4147 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4148
459609d6 4149 if (opd != NULL && opd->size != 0)
b3fac117 4150 {
b9399fcf
AM
4151 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4152 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4153
459609d6
AM
4154 if (abiversion (ibfd) == 0)
4155 set_abiversion (ibfd, 1);
8a2058b5 4156 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4157 {
695344c0 4158 /* xgettext:c-format */
cf97bcb0
AM
4159 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4160 ibfd, abiversion (ibfd));
459609d6
AM
4161 bfd_set_error (bfd_error_bad_value);
4162 return FALSE;
f6c7c3e8 4163 }
b9399fcf 4164 }
f6c7c3e8 4165
b9399fcf
AM
4166 if (is_ppc64_elf (info->output_bfd))
4167 {
4168 /* For input files without an explicit abiversion in e_flags
4169 we should have flagged any with symbol st_other bits set
4170 as ELFv1 and above flagged those with .opd as ELFv2.
4171 Set the output abiversion if not yet set, and for any input
4172 still ambiguous, take its abiversion from the output.
4173 Differences in ABI are reported later. */
4174 if (abiversion (info->output_bfd) == 0)
4175 set_abiversion (info->output_bfd, abiversion (ibfd));
4176 else if (abiversion (ibfd) == 0)
4177 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4178 }
4179
459609d6
AM
4180 htab = ppc_hash_table (info);
4181 if (htab == NULL)
b9399fcf 4182 return TRUE;
459609d6 4183
b9399fcf
AM
4184 if (opd != NULL && opd->size != 0
4185 && (ibfd->flags & DYNAMIC) == 0
4186 && (opd->flags & SEC_RELOC) != 0
4187 && opd->reloc_count != 0
4188 && !bfd_is_abs_section (opd->output_section)
4189 && info->gc_sections)
4190 {
4191 /* Garbage collection needs some extra help with .opd sections.
4192 We don't want to necessarily keep everything referenced by
4193 relocs in .opd, as that would keep all functions. Instead,
4194 if we reference an .opd symbol (a function descriptor), we
4195 want to keep the function code symbol's section. This is
4196 easy for global symbols, but for local syms we need to keep
4197 information about the associated function section. */
4198 bfd_size_type amt;
4199 asection **opd_sym_map;
4200 Elf_Internal_Shdr *symtab_hdr;
4201 Elf_Internal_Rela *relocs, *rel_end, *rel;
4202
4203 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4204 opd_sym_map = bfd_zalloc (ibfd, amt);
4205 if (opd_sym_map == NULL)
4206 return FALSE;
4207 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4208 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4209 info->keep_memory);
4210 if (relocs == NULL)
4211 return FALSE;
4212 symtab_hdr = &elf_symtab_hdr (ibfd);
4213 rel_end = relocs + opd->reloc_count - 1;
4214 for (rel = relocs; rel < rel_end; rel++)
4215 {
4216 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4217 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4218
4219 if (r_type == R_PPC64_ADDR64
4220 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4221 && r_symndx < symtab_hdr->sh_info)
4222 {
4223 Elf_Internal_Sym *isym;
4224 asection *s;
4225
4226 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4227 if (isym == NULL)
4228 {
4229 if (elf_section_data (opd)->relocs != relocs)
4230 free (relocs);
4231 return FALSE;
4232 }
4233
4234 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4235 if (s != NULL && s != opd)
4236 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4237 }
4238 }
4239 if (elf_section_data (opd)->relocs != relocs)
4240 free (relocs);
4241 }
459609d6
AM
4242
4243 p = &htab->dot_syms;
4244 while ((eh = *p) != NULL)
4245 {
4246 *p = NULL;
4247 if (&eh->elf == htab->elf.hgot)
4248 ;
4249 else if (htab->elf.hgot == NULL
4250 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4251 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4252 else if (abiversion (ibfd) <= 1)
4253 {
4254 htab->need_func_desc_adj = 1;
4255 if (!add_symbol_adjust (eh, info))
4256 return FALSE;
4257 }
459609d6
AM
4258 p = &eh->u.next_dot_sym;
4259 }
b3fac117 4260 return TRUE;
8387904d
AM
4261}
4262
97fed1c9
JJ
4263/* Undo hash table changes when an --as-needed input file is determined
4264 not to be needed. */
4265
4266static bfd_boolean
e5034e59
AM
4267ppc64_elf_notice_as_needed (bfd *ibfd,
4268 struct bfd_link_info *info,
4269 enum notice_asneeded_action act)
97fed1c9 4270{
e5034e59
AM
4271 if (act == notice_not_needed)
4272 {
4273 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4274
e5034e59
AM
4275 if (htab == NULL)
4276 return FALSE;
4dfe6ac6 4277
e5034e59
AM
4278 htab->dot_syms = NULL;
4279 }
4280 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4281}
4282
aa374f67
AM
4283/* If --just-symbols against a final linked binary, then assume we need
4284 toc adjusting stubs when calling functions defined there. */
4285
4286static void
4287ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4288{
4289 if ((sec->flags & SEC_CODE) != 0
4290 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4291 && is_ppc64_elf (sec->owner))
4292 {
2c3f079f
AM
4293 if (abiversion (sec->owner) >= 2
4294 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4295 sec->has_toc_reloc = 1;
4296 }
4297 _bfd_elf_link_just_syms (sec, info);
4298}
4299
e054468f 4300static struct plt_entry **
4ce794b7
AM
4301update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4302 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4303{
4304 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4305 struct plt_entry **local_plt;
f961d9dd 4306 unsigned char *local_got_tls_masks;
411e1bfb
AM
4307
4308 if (local_got_ents == NULL)
4309 {
4310 bfd_size_type size = symtab_hdr->sh_info;
4311
e054468f
AM
4312 size *= (sizeof (*local_got_ents)
4313 + sizeof (*local_plt)
4314 + sizeof (*local_got_tls_masks));
4ce794b7 4315 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4316 if (local_got_ents == NULL)
e054468f 4317 return NULL;
411e1bfb
AM
4318 elf_local_got_ents (abfd) = local_got_ents;
4319 }
4320
37da22e5 4321 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4322 {
4323 struct got_entry *ent;
4324
4325 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4326 if (ent->addend == r_addend
4327 && ent->owner == abfd
4328 && ent->tls_type == tls_type)
411e1bfb
AM
4329 break;
4330 if (ent == NULL)
4331 {
4332 bfd_size_type amt = sizeof (*ent);
4ce794b7 4333 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4334 if (ent == NULL)
4335 return FALSE;
4336 ent->next = local_got_ents[r_symndx];
4337 ent->addend = r_addend;
e717da7e 4338 ent->owner = abfd;
411e1bfb 4339 ent->tls_type = tls_type;
927be08e 4340 ent->is_indirect = FALSE;
411e1bfb
AM
4341 ent->got.refcount = 0;
4342 local_got_ents[r_symndx] = ent;
4343 }
4344 ent->got.refcount += 1;
4345 }
4346
e054468f 4347 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4348 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4349 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4350
4351 return local_plt + r_symndx;
65f38f15
AM
4352}
4353
411e1bfb 4354static bfd_boolean
e054468f 4355update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4356{
411e1bfb 4357 struct plt_entry *ent;
1e2f5b6e 4358
e054468f 4359 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4360 if (ent->addend == addend)
4361 break;
4362 if (ent == NULL)
1e2f5b6e 4363 {
411e1bfb 4364 bfd_size_type amt = sizeof (*ent);
4ce794b7 4365 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4366 if (ent == NULL)
4367 return FALSE;
e054468f 4368 ent->next = *plist;
411e1bfb
AM
4369 ent->addend = addend;
4370 ent->plt.refcount = 0;
e054468f 4371 *plist = ent;
1e2f5b6e 4372 }
411e1bfb 4373 ent->plt.refcount += 1;
b34976b6 4374 return TRUE;
1e2f5b6e
AM
4375}
4376
e054468f
AM
4377static bfd_boolean
4378is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4379{
4380 return (r_type == R_PPC64_REL24
05d0e962 4381 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
4382 || r_type == R_PPC64_REL14
4383 || r_type == R_PPC64_REL14_BRTAKEN
4384 || r_type == R_PPC64_REL14_BRNTAKEN
4385 || r_type == R_PPC64_ADDR24
4386 || r_type == R_PPC64_ADDR14
4387 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4388 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4389 || r_type == R_PPC64_PLTCALL
4390 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4391}
4392
4393/* Relocs on inline plt call sequence insns prior to the call. */
4394
4395static bfd_boolean
4396is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4397{
4398 return (r_type == R_PPC64_PLT16_HA
4399 || r_type == R_PPC64_PLT16_HI
4400 || r_type == R_PPC64_PLT16_LO
4401 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4402 || r_type == R_PPC64_PLT_PCREL34
4403 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4404 || r_type == R_PPC64_PLTSEQ
4405 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4406}
4407
5bd4f169 4408/* Look through the relocs for a section during the first phase, and
65f38f15 4409 calculate needed space in the global offset table, procedure
5d1634d7 4410 linkage table, and dynamic reloc sections. */
5bd4f169 4411
b34976b6 4412static bfd_boolean
4ce794b7
AM
4413ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4414 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4415{
65f38f15 4416 struct ppc_link_hash_table *htab;
5bd4f169 4417 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4418 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4419 const Elf_Internal_Rela *rel;
4420 const Elf_Internal_Rela *rel_end;
5bd4f169 4421 asection *sreloc;
3a71aa26 4422 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 4423 bfd_boolean is_opd;
5bd4f169 4424
0e1862bb 4425 if (bfd_link_relocatable (info))
b34976b6 4426 return TRUE;
5bd4f169 4427
680a3378
AM
4428 /* Don't do anything special with non-loaded, non-alloced sections.
4429 In particular, any relocs in such sections should not affect GOT
4430 and PLT reference counting (ie. we don't allow them to create GOT
4431 or PLT entries), there's no possibility or desire to optimize TLS
4432 relocs, and there's not much point in propagating relocs to shared
4433 libs that the dynamic linker won't relocate. */
4434 if ((sec->flags & SEC_ALLOC) == 0)
4435 return TRUE;
4436
0c8d6e5c 4437 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4438
65f38f15 4439 htab = ppc_hash_table (info);
4dfe6ac6
NC
4440 if (htab == NULL)
4441 return FALSE;
4442
3a71aa26
AM
4443 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4444 FALSE, FALSE, TRUE);
4445 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4446 FALSE, FALSE, TRUE);
0ffa91dd 4447 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4448 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4449 sreloc = NULL;
b9399fcf 4450 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4451 rel_end = relocs + sec->reloc_count;
4452 for (rel = relocs; rel < rel_end; rel++)
4453 {
4454 unsigned long r_symndx;
4455 struct elf_link_hash_entry *h;
04c9666a 4456 enum elf_ppc64_reloc_type r_type;
727fc41e 4457 int tls_type;
7c8fe5c4 4458 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4459 struct plt_entry **ifunc, **plt_list;
5663e321 4460 bfd_vma sym_addend;
5bd4f169
AM
4461
4462 r_symndx = ELF64_R_SYM (rel->r_info);
4463 if (r_symndx < symtab_hdr->sh_info)
4464 h = NULL;
4465 else
973a3492
L
4466 {
4467 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4468 h = elf_follow_link (h);
1c865ab2
AM
4469
4470 if (h == htab->elf.hgot)
4471 sec->has_toc_reloc = 1;
973a3492 4472 }
5bd4f169 4473
727fc41e 4474 tls_type = 0;
e054468f 4475 ifunc = NULL;
5663e321
AM
4476 r_type = ELF64_R_TYPE (rel->r_info);
4477 switch (r_type)
4478 {
4479 default:
4480 /* Somewhat foolishly, because the ABIs don't specifically
4481 allow it, ppc64 gas and ld support GOT and PLT relocs
4482 with non-zero addends where the addend results in
4483 sym+addend being stored in the GOT or PLT entry. This
4484 can't be supported for pcrel relocs because the addend is
4485 used to specify the pcrel offset. */
4486 sym_addend = rel->r_addend;
4487 break;
4488 case R_PPC64_GOT_PCREL34:
4489 case R_PPC64_PLT_PCREL34:
4490 case R_PPC64_PLT_PCREL34_NOTOC:
4491 sym_addend = 0;
4492 break;
4493 }
25f23106
AM
4494 if (h != NULL)
4495 {
4496 if (h->type == STT_GNU_IFUNC)
4497 {
4498 h->needs_plt = 1;
4499 ifunc = &h->plt.plist;
4500 }
4501 }
4502 else
4503 {
4504 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4505 abfd, r_symndx);
4506 if (isym == NULL)
4507 return FALSE;
4508
4509 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4510 {
4511 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4512 sym_addend,
37da22e5 4513 NON_GOT | PLT_IFUNC);
25f23106
AM
4514 if (ifunc == NULL)
4515 return FALSE;
4516 }
4517 }
727fc41e 4518
a33d1f77 4519 switch (r_type)
5bd4f169 4520 {
727fc41e
AM
4521 case R_PPC64_TLSGD:
4522 case R_PPC64_TLSLD:
4523 /* These special tls relocs tie a call to __tls_get_addr with
4524 its parameter symbol. */
37da22e5
AM
4525 if (h != NULL)
4526 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4527 else
4528 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4529 sym_addend,
37da22e5
AM
4530 NON_GOT | TLS_TLS | TLS_MARK))
4531 return FALSE;
4532 sec->has_tls_reloc = 1;
727fc41e
AM
4533 break;
4534
411e1bfb
AM
4535 case R_PPC64_GOT_TLSLD16:
4536 case R_PPC64_GOT_TLSLD16_LO:
4537 case R_PPC64_GOT_TLSLD16_HI:
4538 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 4539 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4540 goto dogottls;
4541
4542 case R_PPC64_GOT_TLSGD16:
4543 case R_PPC64_GOT_TLSGD16_LO:
4544 case R_PPC64_GOT_TLSGD16_HI:
4545 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 4546 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4547 goto dogottls;
4548
4549 case R_PPC64_GOT_TPREL16_DS:
4550 case R_PPC64_GOT_TPREL16_LO_DS:
4551 case R_PPC64_GOT_TPREL16_HI:
4552 case R_PPC64_GOT_TPREL16_HA:
7c8bbca5 4553 if (bfd_link_dll (info))
411e1bfb
AM
4554 info->flags |= DF_STATIC_TLS;
4555 tls_type = TLS_TLS | TLS_TPREL;
4556 goto dogottls;
4557
4558 case R_PPC64_GOT_DTPREL16_DS:
4559 case R_PPC64_GOT_DTPREL16_LO_DS:
4560 case R_PPC64_GOT_DTPREL16_HI:
4561 case R_PPC64_GOT_DTPREL16_HA:
4562 tls_type = TLS_TLS | TLS_DTPREL;
4563 dogottls:
4564 sec->has_tls_reloc = 1;
066f4018 4565 goto dogot;
411e1bfb 4566
5bd4f169 4567 case R_PPC64_GOT16_DS:
65f38f15 4568 case R_PPC64_GOT16_HA:
066f4018 4569 case R_PPC64_GOT16_LO_DS:
4a421c53 4570 case R_PPC64_GOT_PCREL34:
066f4018
AM
4571 ppc64_elf_tdata (abfd)->has_gotrel = 1;
4572 ppc64_elf_section_data (sec)->has_gotrel = 1;
4573 /* Fall through. */
4574
4575 case R_PPC64_GOT16:
65f38f15
AM
4576 case R_PPC64_GOT16_HI:
4577 case R_PPC64_GOT16_LO:
066f4018 4578 dogot:
65f38f15 4579 /* This symbol requires a global offset table entry. */
4c52953f 4580 sec->has_toc_reloc = 1;
33c0ec9d
AM
4581 if (r_type == R_PPC64_GOT_TLSLD16
4582 || r_type == R_PPC64_GOT_TLSGD16
4583 || r_type == R_PPC64_GOT_TPREL16_DS
4584 || r_type == R_PPC64_GOT_DTPREL16_DS
4585 || r_type == R_PPC64_GOT16
4586 || r_type == R_PPC64_GOT16_DS)
4587 {
4588 htab->do_multi_toc = 1;
d77c8a4b 4589 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4590 }
4591
e717da7e
AM
4592 if (ppc64_elf_tdata (abfd)->got == NULL
4593 && !create_got_section (abfd, info))
b34976b6 4594 return FALSE;
5bd4f169
AM
4595
4596 if (h != NULL)
4597 {
411e1bfb
AM
4598 struct ppc_link_hash_entry *eh;
4599 struct got_entry *ent;
65f38f15 4600
411e1bfb
AM
4601 eh = (struct ppc_link_hash_entry *) h;
4602 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5663e321 4603 if (ent->addend == sym_addend
e717da7e 4604 && ent->owner == abfd
411e1bfb
AM
4605 && ent->tls_type == tls_type)
4606 break;
4607 if (ent == NULL)
5bd4f169 4608 {
411e1bfb 4609 bfd_size_type amt = sizeof (*ent);
4ce794b7 4610 ent = bfd_alloc (abfd, amt);
411e1bfb 4611 if (ent == NULL)
b34976b6 4612 return FALSE;
411e1bfb 4613 ent->next = eh->elf.got.glist;
5663e321 4614 ent->addend = sym_addend;
e717da7e 4615 ent->owner = abfd;
411e1bfb 4616 ent->tls_type = tls_type;
927be08e 4617 ent->is_indirect = FALSE;
411e1bfb
AM
4618 ent->got.refcount = 0;
4619 eh->elf.got.glist = ent;
5bd4f169 4620 }
411e1bfb 4621 ent->got.refcount += 1;
e7b938ca 4622 eh->tls_mask |= tls_type;
5bd4f169 4623 }
411e1bfb
AM
4624 else
4625 /* This is a global offset table entry for a local symbol. */
4626 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4627 sym_addend, tls_type))
411e1bfb 4628 return FALSE;
a345bc8d
AM
4629
4630 /* We may also need a plt entry if the symbol turns out to be
4631 an ifunc. */
0e1862bb 4632 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d 4633 {
5663e321 4634 if (!update_plt_info (abfd, &h->plt.plist, sym_addend))
a345bc8d
AM
4635 return FALSE;
4636 }
5bd4f169
AM
4637 break;
4638
5bd4f169 4639 case R_PPC64_PLT16_HA:
65f38f15
AM
4640 case R_PPC64_PLT16_HI:
4641 case R_PPC64_PLT16_LO:
08be3224 4642 case R_PPC64_PLT16_LO_DS:
5663e321
AM
4643 case R_PPC64_PLT_PCREL34:
4644 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
4645 case R_PPC64_PLT32:
4646 case R_PPC64_PLT64:
cbf95972
AM
4647 /* This symbol requires a procedure linkage table entry. */
4648 plt_list = ifunc;
4649 if (h != NULL)
e054468f 4650 {
e054468f
AM
4651 h->needs_plt = 1;
4652 if (h->root.root.string[0] == '.'
4653 && h->root.root.string[1] != '\0')
4654 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2d7ad24e 4655 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4656 plt_list = &h->plt.plist;
4657 }
4658 if (plt_list == NULL)
2d7ad24e 4659 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4660 sym_addend,
2d7ad24e 4661 NON_GOT | PLT_KEEP);
5663e321 4662 if (!update_plt_info (abfd, plt_list, sym_addend))
cbf95972 4663 return FALSE;
5bd4f169
AM
4664 break;
4665
4666 /* The following relocations don't need to propagate the
4667 relocation if linking a shared object since they are
4668 section relative. */
4669 case R_PPC64_SECTOFF:
4670 case R_PPC64_SECTOFF_LO:
4671 case R_PPC64_SECTOFF_HI:
4672 case R_PPC64_SECTOFF_HA:
4673 case R_PPC64_SECTOFF_DS:
4674 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4675 case R_PPC64_DTPREL16:
4676 case R_PPC64_DTPREL16_LO:
4677 case R_PPC64_DTPREL16_HI:
4678 case R_PPC64_DTPREL16_HA:
4679 case R_PPC64_DTPREL16_DS:
4680 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
4681 case R_PPC64_DTPREL16_HIGH:
4682 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
4683 case R_PPC64_DTPREL16_HIGHER:
4684 case R_PPC64_DTPREL16_HIGHERA:
4685 case R_PPC64_DTPREL16_HIGHEST:
4686 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4687 break;
4688
ad8e1ba5 4689 /* Nor do these. */
25f23106
AM
4690 case R_PPC64_REL16:
4691 case R_PPC64_REL16_LO:
4692 case R_PPC64_REL16_HI:
4693 case R_PPC64_REL16_HA:
4a969973
AM
4694 case R_PPC64_REL16_HIGH:
4695 case R_PPC64_REL16_HIGHA:
4696 case R_PPC64_REL16_HIGHER:
4697 case R_PPC64_REL16_HIGHERA:
4698 case R_PPC64_REL16_HIGHEST:
4699 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
4700 case R_PPC64_REL16_HIGHER34:
4701 case R_PPC64_REL16_HIGHERA34:
4702 case R_PPC64_REL16_HIGHEST34:
4703 case R_PPC64_REL16_HIGHESTA34:
a680de9a 4704 case R_PPC64_REL16DX_HA:
25f23106
AM
4705 break;
4706
45965137
AM
4707 /* Not supported as a dynamic relocation. */
4708 case R_PPC64_ADDR64_LOCAL:
0e1862bb 4709 if (bfd_link_pic (info))
45965137
AM
4710 {
4711 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4712 ppc_howto_init ();
695344c0 4713 /* xgettext:c-format */
174d0a74 4714 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 4715 "in shared libraries and PIEs\n"),
45965137
AM
4716 abfd, sec, rel->r_offset,
4717 ppc64_elf_howto_table[r_type]->name);
4718 bfd_set_error (bfd_error_bad_value);
4719 return FALSE;
4720 }
4721 break;
4722
ad8e1ba5 4723 case R_PPC64_TOC16:
33c0ec9d
AM
4724 case R_PPC64_TOC16_DS:
4725 htab->do_multi_toc = 1;
d77c8a4b 4726 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 4727 /* Fall through. */
ad8e1ba5
AM
4728 case R_PPC64_TOC16_LO:
4729 case R_PPC64_TOC16_HI:
4730 case R_PPC64_TOC16_HA:
ad8e1ba5 4731 case R_PPC64_TOC16_LO_DS:
4c52953f 4732 sec->has_toc_reloc = 1;
ad8e1ba5
AM
4733 break;
4734
006589cf
AM
4735 /* Marker reloc. */
4736 case R_PPC64_ENTRY:
4737 break;
4738
5bd4f169
AM
4739 /* This relocation describes the C++ object vtable hierarchy.
4740 Reconstruct it for later use during GC. */
4741 case R_PPC64_GNU_VTINHERIT:
c152c796 4742 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4743 return FALSE;
5bd4f169
AM
4744 break;
4745
4746 /* This relocation describes which C++ vtable entries are actually
4747 used. Record for later use during GC. */
4748 case R_PPC64_GNU_VTENTRY:
a0ea3a14 4749 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4750 return FALSE;
5bd4f169
AM
4751 break;
4752
721956f4
AM
4753 case R_PPC64_REL14:
4754 case R_PPC64_REL14_BRTAKEN:
4755 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4756 {
4757 asection *dest = NULL;
4758
4759 /* Heuristic: If jumping outside our section, chances are
4760 we are going to need a stub. */
4761 if (h != NULL)
4762 {
4763 /* If the sym is weak it may be overridden later, so
4764 don't assume we know where a weak sym lives. */
4765 if (h->root.type == bfd_link_hash_defined)
4766 dest = h->root.u.def.section;
4767 }
4768 else
87d72d41
AM
4769 {
4770 Elf_Internal_Sym *isym;
4771
4772 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4773 abfd, r_symndx);
4774 if (isym == NULL)
4775 return FALSE;
4776
4777 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4778 }
4779
220c76dd 4780 if (dest != sec)
7c8fe5c4 4781 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4782 }
3e04d765
AM
4783 goto rel24;
4784
4785 case R_PPC64_PLTCALL:
5663e321 4786 case R_PPC64_PLTCALL_NOTOC:
3e04d765 4787 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
4788 /* Fall through. */
4789
5d1634d7 4790 case R_PPC64_REL24:
05d0e962 4791 case R_PPC64_REL24_NOTOC:
3e04d765 4792 rel24:
cbf95972
AM
4793 plt_list = ifunc;
4794 if (h != NULL)
5d1634d7 4795 {
e054468f
AM
4796 h->needs_plt = 1;
4797 if (h->root.root.string[0] == '.'
4798 && h->root.root.string[1] != '\0')
4799 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 4800
3a71aa26 4801 if (h == tga || h == dottga)
cbf95972
AM
4802 {
4803 sec->has_tls_reloc = 1;
4804 if (rel != relocs
4805 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4806 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4807 /* We have a new-style __tls_get_addr call with
4808 a marker reloc. */
4809 ;
4810 else
4811 /* Mark this section as having an old-style call. */
4812 sec->has_tls_get_addr_call = 1;
4813 }
4814 plt_list = &h->plt.plist;
411e1bfb 4815 }
cbf95972
AM
4816
4817 /* We may need a .plt entry if the function this reloc
4818 refers to is in a shared lib. */
4819 if (plt_list
5663e321 4820 && !update_plt_info (abfd, plt_list, sym_addend))
cbf95972 4821 return FALSE;
411e1bfb
AM
4822 break;
4823
cbf95972
AM
4824 case R_PPC64_ADDR14:
4825 case R_PPC64_ADDR14_BRNTAKEN:
4826 case R_PPC64_ADDR14_BRTAKEN:
4827 case R_PPC64_ADDR24:
4828 goto dodyn;
4829
411e1bfb
AM
4830 case R_PPC64_TPREL64:
4831 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 4832 if (bfd_link_dll (info))
411e1bfb
AM
4833 info->flags |= DF_STATIC_TLS;
4834 goto dotlstoc;
4835
4836 case R_PPC64_DTPMOD64:
4837 if (rel + 1 < rel_end
4838 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4839 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 4840 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 4841 else
951fd09b 4842 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
4843 goto dotlstoc;
4844
4845 case R_PPC64_DTPREL64:
4846 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4847 if (rel != relocs
4848 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4849 && rel[-1].r_offset == rel->r_offset - 8)
4850 /* This is the second reloc of a dtpmod, dtprel pair.
4851 Don't mark with TLS_DTPREL. */
4852 goto dodyn;
4853
4854 dotlstoc:
4855 sec->has_tls_reloc = 1;
4856 if (h != NULL)
4857 {
4858 struct ppc_link_hash_entry *eh;
4859 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 4860 eh->tls_mask |= tls_type;
411e1bfb
AM
4861 }
4862 else
4863 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4864 sym_addend, tls_type))
411e1bfb
AM
4865 return FALSE;
4866
7c8fe5c4
AM
4867 ppc64_sec = ppc64_elf_section_data (sec);
4868 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 4869 {
3a71aa26
AM
4870 bfd_size_type amt;
4871
e7b938ca 4872 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
4873 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4874 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4875 if (ppc64_sec->u.toc.symndx == NULL)
4876 return FALSE;
4877 amt = sec->size * sizeof (bfd_vma) / 8;
4878 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4879 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 4880 return FALSE;
7c8fe5c4
AM
4881 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4882 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
4883 }
4884 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26 4885 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5663e321 4886 ppc64_sec->u.toc.add[rel->r_offset / 8] = sym_addend;
951fd09b
AM
4887
4888 /* Mark the second slot of a GD or LD entry.
4889 -1 to indicate GD and -2 to indicate LD. */
4890 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 4891 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 4892 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 4893 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
4894 goto dodyn;
4895
4896 case R_PPC64_TPREL16:
4897 case R_PPC64_TPREL16_LO:
4898 case R_PPC64_TPREL16_HI:
4899 case R_PPC64_TPREL16_HA:
4900 case R_PPC64_TPREL16_DS:
4901 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
4902 case R_PPC64_TPREL16_HIGH:
4903 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
4904 case R_PPC64_TPREL16_HIGHER:
4905 case R_PPC64_TPREL16_HIGHERA:
4906 case R_PPC64_TPREL16_HIGHEST:
4907 case R_PPC64_TPREL16_HIGHESTA:
7c8bbca5
AM
4908 if (bfd_link_dll (info))
4909 info->flags |= DF_STATIC_TLS;
4910 goto dodyn;
5d1634d7 4911
e86ce104 4912 case R_PPC64_ADDR64:
b9399fcf 4913 if (is_opd
1e2f5b6e 4914 && rel + 1 < rel_end
4ce794b7 4915 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 4916 {
8387904d 4917 if (h != NULL)
8c5b4e52 4918 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 4919 }
e86ce104
AM
4920 /* Fall through. */
4921
65f38f15
AM
4922 case R_PPC64_ADDR16:
4923 case R_PPC64_ADDR16_DS:
4924 case R_PPC64_ADDR16_HA:
4925 case R_PPC64_ADDR16_HI:
f9c6b907
AM
4926 case R_PPC64_ADDR16_HIGH:
4927 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
4928 case R_PPC64_ADDR16_HIGHER:
4929 case R_PPC64_ADDR16_HIGHERA:
4930 case R_PPC64_ADDR16_HIGHEST:
4931 case R_PPC64_ADDR16_HIGHESTA:
4932 case R_PPC64_ADDR16_LO:
4933 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
4934 case R_PPC64_D34:
4935 case R_PPC64_D34_LO:
4936 case R_PPC64_D34_HI30:
4937 case R_PPC64_D34_HA30:
4938 case R_PPC64_ADDR16_HIGHER34:
4939 case R_PPC64_ADDR16_HIGHERA34:
4940 case R_PPC64_ADDR16_HIGHEST34:
4941 case R_PPC64_ADDR16_HIGHESTA34:
4942 case R_PPC64_D28:
0e1862bb 4943 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
4944 && rel->r_addend == 0)
4945 {
4946 /* We may need a .plt entry if this reloc refers to a
4947 function in a shared lib. */
5663e321 4948 if (!update_plt_info (abfd, &h->plt.plist, 0))
a345bc8d
AM
4949 return FALSE;
4950 h->pointer_equality_needed = 1;
4951 }
4952 /* Fall through. */
4953
4954 case R_PPC64_REL30:
4955 case R_PPC64_REL32:
4956 case R_PPC64_REL64:
65f38f15 4957 case R_PPC64_ADDR32:
65f38f15
AM
4958 case R_PPC64_UADDR16:
4959 case R_PPC64_UADDR32:
4960 case R_PPC64_UADDR64:
5bd4f169 4961 case R_PPC64_TOC:
0e1862bb 4962 if (h != NULL && !bfd_link_pic (info))
81848ca0 4963 /* We may need a copy reloc. */
f5385ebf 4964 h->non_got_ref = 1;
81848ca0 4965
41bd81ab 4966 /* Don't propagate .opd relocs. */
b9399fcf 4967 if (NO_OPD_RELOCS && is_opd)
e86ce104 4968 break;
e86ce104 4969
65f38f15
AM
4970 /* If we are creating a shared library, and this is a reloc
4971 against a global symbol, or a non PC relative reloc
4972 against a local symbol, then we need to copy the reloc
4973 into the shared library. However, if we are linking with
4974 -Bsymbolic, we do not need to copy a reloc against a
4975 global symbol which is defined in an object we are
4976 including in the link (i.e., DEF_REGULAR is set). At
4977 this point we have not seen all the input files, so it is
4978 possible that DEF_REGULAR is not set now but will be set
4979 later (it is never cleared). In case of a weak definition,
4980 DEF_REGULAR may be cleared later by a strong definition in
4981 a shared library. We account for that possibility below by
f4656909 4982 storing information in the dyn_relocs field of the hash
65f38f15
AM
4983 table entry. A similar situation occurs when creating
4984 shared libraries and symbol visibility changes render the
4985 symbol local.
4986
4987 If on the other hand, we are creating an executable, we
4988 may need to keep relocations for symbols satisfied by a
4989 dynamic library if we manage to avoid copy relocs for the
4990 symbol. */
411e1bfb 4991 dodyn:
0e1862bb 4992 if ((bfd_link_pic (info)
1d483afe 4993 && (must_be_dyn_reloc (info, r_type)
65f38f15 4994 || (h != NULL
198f1157 4995 && (!SYMBOLIC_BIND (info, h)
65f38f15 4996 || h->root.type == bfd_link_hash_defweak
f5385ebf 4997 || !h->def_regular))))
f4656909 4998 || (ELIMINATE_COPY_RELOCS
0e1862bb 4999 && !bfd_link_pic (info)
65f38f15
AM
5000 && h != NULL
5001 && (h->root.type == bfd_link_hash_defweak
25f23106 5002 || !h->def_regular))
0e1862bb 5003 || (!bfd_link_pic (info)
25f23106 5004 && ifunc != NULL))
5bd4f169 5005 {
65f38f15
AM
5006 /* We must copy these reloc types into the output file.
5007 Create a reloc section in dynobj and make room for
5008 this reloc. */
5bd4f169
AM
5009 if (sreloc == NULL)
5010 {
83bac4b0
NC
5011 sreloc = _bfd_elf_make_dynamic_reloc_section
5012 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5013
5bd4f169 5014 if (sreloc == NULL)
83bac4b0 5015 return FALSE;
5bd4f169
AM
5016 }
5017
65f38f15
AM
5018 /* If this is a global symbol, we count the number of
5019 relocations we need for this symbol. */
5020 if (h != NULL)
5021 {
19e08130
AM
5022 struct elf_dyn_relocs *p;
5023 struct elf_dyn_relocs **head;
5024
ec338859 5025 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5026 p = *head;
5027 if (p == NULL || p->sec != sec)
5028 {
5029 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5030 if (p == NULL)
5031 return FALSE;
5032 p->next = *head;
5033 *head = p;
5034 p->sec = sec;
5035 p->count = 0;
5036 p->pc_count = 0;
5037 }
5038 p->count += 1;
5039 if (!must_be_dyn_reloc (info, r_type))
5040 p->pc_count += 1;
65f38f15
AM
5041 }
5042 else
5043 {
ec338859
AM
5044 /* Track dynamic relocs needed for local syms too.
5045 We really need local syms available to do this
5046 easily. Oh well. */
19e08130
AM
5047 struct ppc_dyn_relocs *p;
5048 struct ppc_dyn_relocs **head;
5049 bfd_boolean is_ifunc;
ec338859 5050 asection *s;
6edfbbad 5051 void *vpp;
87d72d41 5052 Elf_Internal_Sym *isym;
6edfbbad 5053
87d72d41
AM
5054 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5055 abfd, r_symndx);
5056 if (isym == NULL)
b34976b6 5057 return FALSE;
ec338859 5058
87d72d41
AM
5059 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5060 if (s == NULL)
5061 s = sec;
5062
6edfbbad 5063 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5064 head = (struct ppc_dyn_relocs **) vpp;
5065 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5066 p = *head;
5067 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5068 p = p->next;
5069 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5070 {
5071 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5072 if (p == NULL)
5073 return FALSE;
5074 p->next = *head;
5075 *head = p;
5076 p->sec = sec;
5077 p->ifunc = is_ifunc;
5078 p->count = 0;
5079 }
5080 p->count += 1;
ec338859 5081 }
65f38f15 5082 }
5bd4f169 5083 break;
65f38f15
AM
5084
5085 default:
96e0dda4 5086 break;
5bd4f169
AM
5087 }
5088 }
5089
b34976b6 5090 return TRUE;
5bd4f169
AM
5091}
5092
ee67d69a
AM
5093/* Merge backend specific data from an object file to the output
5094 object file when linking. */
5095
5096static bfd_boolean
50e03d47 5097ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5098{
50e03d47 5099 bfd *obfd = info->output_bfd;
ee67d69a
AM
5100 unsigned long iflags, oflags;
5101
5102 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5103 return TRUE;
5104
5105 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5106 return TRUE;
5107
50e03d47 5108 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
5109 return FALSE;
5110
5111 iflags = elf_elfheader (ibfd)->e_flags;
5112 oflags = elf_elfheader (obfd)->e_flags;
5113
f6c7c3e8 5114 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5115 {
4eca0228 5116 _bfd_error_handler
695344c0 5117 /* xgettext:c-format */
871b3ab2 5118 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
5119 bfd_set_error (bfd_error_bad_value);
5120 return FALSE;
5121 }
f6c7c3e8 5122 else if (iflags != oflags && iflags != 0)
ee67d69a 5123 {
4eca0228 5124 _bfd_error_handler
695344c0 5125 /* xgettext:c-format */
871b3ab2 5126 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5127 ibfd, iflags, oflags);
5128 bfd_set_error (bfd_error_bad_value);
5129 return FALSE;
5130 }
5131
4a91d0ba
AM
5132 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5133 return FALSE;
005d79fd 5134
ee67d69a 5135 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5136 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5137}
5138
5139static bfd_boolean
5140ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5141{
5142 /* Print normal ELF private data. */
5143 _bfd_elf_print_private_bfd_data (abfd, ptr);
5144
5145 if (elf_elfheader (abfd)->e_flags != 0)
5146 {
5147 FILE *file = ptr;
5148
ee67d69a
AM
5149 fprintf (file, _("private flags = 0x%lx:"),
5150 elf_elfheader (abfd)->e_flags);
5151
5152 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5153 fprintf (file, _(" [abiv%ld]"),
5154 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5155 fputc ('\n', file);
5156 }
5157
5158 return TRUE;
5159}
5160
8387904d 5161/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5162 of the code entry point, and its section, which must be in the same
5163 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5164
5165static bfd_vma
5166opd_entry_value (asection *opd_sec,
5167 bfd_vma offset,
5168 asection **code_sec,
aef36ac1
AM
5169 bfd_vma *code_off,
5170 bfd_boolean in_code_sec)
8387904d
AM
5171{
5172 bfd *opd_bfd = opd_sec->owner;
8860955f 5173 Elf_Internal_Rela *relocs;
8387904d 5174 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5175 bfd_vma val;
8387904d 5176
9f296da3
AM
5177 /* No relocs implies we are linking a --just-symbols object, or looking
5178 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5179 if (opd_sec->reloc_count == 0)
5180 {
729eabd5 5181 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5182
729eabd5
AM
5183 if (contents == NULL)
5184 {
5185 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5186 return (bfd_vma) -1;
5187 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5188 }
ee1e4ede 5189
dbb3fbbb 5190 /* PR 17512: file: 64b9dfbb. */
451dfd38 5191 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5192 return (bfd_vma) -1;
5193
729eabd5 5194 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5195 if (code_sec != NULL)
5196 {
5197 asection *sec, *likely = NULL;
ee1e4ede 5198
aef36ac1 5199 if (in_code_sec)
4b85d634 5200 {
aef36ac1
AM
5201 sec = *code_sec;
5202 if (sec->vma <= val
5203 && val < sec->vma + sec->size)
5204 likely = sec;
5205 else
5206 val = -1;
5207 }
5208 else
5209 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5210 if (sec->vma <= val
5211 && (sec->flags & SEC_LOAD) != 0
5212 && (sec->flags & SEC_ALLOC) != 0)
5213 likely = sec;
5214 if (likely != NULL)
5215 {
5216 *code_sec = likely;
5217 if (code_off != NULL)
5218 *code_off = val - likely->vma;
4b85d634
AM
5219 }
5220 }
aef36ac1 5221 return val;
4b85d634
AM
5222 }
5223
0c8d6e5c 5224 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5225
729eabd5 5226 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5227 if (relocs == NULL)
5228 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
5229 /* PR 17512: file: df8e1fd6. */
5230 if (relocs == NULL)
5231 return (bfd_vma) -1;
645ea6a9 5232
8387904d 5233 /* Go find the opd reloc at the sym address. */
8860955f 5234 lo = relocs;
8387904d 5235 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5236 val = (bfd_vma) -1;
8387904d
AM
5237 while (lo < hi)
5238 {
5239 look = lo + (hi - lo) / 2;
5240 if (look->r_offset < offset)
5241 lo = look + 1;
5242 else if (look->r_offset > offset)
5243 hi = look;
5244 else
5245 {
0ffa91dd
NC
5246 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5247
8387904d
AM
5248 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5249 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5250 {
5251 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5252 asection *sec = NULL;
8387904d 5253
b53dfeb2
AM
5254 if (symndx >= symtab_hdr->sh_info
5255 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5256 {
5257 struct elf_link_hash_entry **sym_hashes;
5258 struct elf_link_hash_entry *rh;
5259
5260 sym_hashes = elf_sym_hashes (opd_bfd);
5261 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5262 if (rh != NULL)
5263 {
5264 rh = elf_follow_link (rh);
bb854a36
AM
5265 if (rh->root.type != bfd_link_hash_defined
5266 && rh->root.type != bfd_link_hash_defweak)
5267 break;
5268 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5269 {
bb854a36
AM
5270 val = rh->root.u.def.value;
5271 sec = rh->root.u.def.section;
b53dfeb2
AM
5272 }
5273 }
5274 }
5275
5276 if (sec == NULL)
5277 {
5278 Elf_Internal_Sym *sym;
5279
5280 if (symndx < symtab_hdr->sh_info)
5281 {
5282 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5283 if (sym == NULL)
5284 {
5285 size_t symcnt = symtab_hdr->sh_info;
5286 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5287 symcnt, 0,
5288 NULL, NULL, NULL);
5289 if (sym == NULL)
5290 break;
5291 symtab_hdr->contents = (bfd_byte *) sym;
5292 }
5293 sym += symndx;
128205bb
AM
5294 }
5295 else
5296 {
b53dfeb2
AM
5297 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5298 1, symndx,
5299 NULL, NULL, NULL);
128205bb
AM
5300 if (sym == NULL)
5301 break;
128205bb 5302 }
b53dfeb2
AM
5303 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5304 if (sec == NULL)
5305 break;
5306 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5307 val = sym->st_value;
8387904d 5308 }
b53dfeb2 5309
8387904d
AM
5310 val += look->r_addend;
5311 if (code_off != NULL)
5312 *code_off = val;
5313 if (code_sec != NULL)
aef36ac1
AM
5314 {
5315 if (in_code_sec && *code_sec != sec)
5316 return -1;
5317 else
5318 *code_sec = sec;
5319 }
b53dfeb2 5320 if (sec->output_section != NULL)
8387904d 5321 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5322 }
5323 break;
5324 }
5325 }
645ea6a9 5326
645ea6a9 5327 return val;
8387904d
AM
5328}
5329
aef36ac1
AM
5330/* If the ELF symbol SYM might be a function in SEC, return the
5331 function size and set *CODE_OFF to the function's entry point,
5332 otherwise return zero. */
9f296da3 5333
aef36ac1
AM
5334static bfd_size_type
5335ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5336 bfd_vma *code_off)
9f296da3 5337{
aef36ac1
AM
5338 bfd_size_type size;
5339
5340 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5341 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5342 return 0;
5343
5344 size = 0;
5345 if (!(sym->flags & BSF_SYNTHETIC))
5346 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5347
5348 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5349 {
b07bca4e
AM
5350 struct _opd_sec_data *opd = get_opd_info (sym->section);
5351 bfd_vma symval = sym->value;
5352
5353 if (opd != NULL
5354 && opd->adjust != NULL
5355 && elf_section_data (sym->section)->relocs != NULL)
5356 {
5357 /* opd_entry_value will use cached relocs that have been
5358 adjusted, but with raw symbols. That means both local
5359 and global symbols need adjusting. */
5360 long adjust = opd->adjust[OPD_NDX (symval)];
5361 if (adjust == -1)
5362 return 0;
5363 symval += adjust;
5364 }
5365
5366 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
5367 &sec, code_off, TRUE) == (bfd_vma) -1)
5368 return 0;
5369 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5370 symbol. This size has nothing to do with the code size of the
5371 function, which is what we're supposed to return, but the
5372 code size isn't available without looking up the dot-sym.
5373 However, doing that would be a waste of time particularly
5374 since elf_find_function will look at the dot-sym anyway.
5375 Now, elf_find_function will keep the largest size of any
5376 function sym found at the code address of interest, so return
5377 1 here to avoid it incorrectly caching a larger function size
5378 for a small function. This does mean we return the wrong
5379 size for a new-ABI function of size 24, but all that does is
5380 disable caching for such functions. */
5381 if (size == 24)
5382 size = 1;
9f296da3 5383 }
aef36ac1
AM
5384 else
5385 {
5386 if (sym->section != sec)
5387 return 0;
5388 *code_off = sym->value;
5389 }
5390 if (size == 0)
5391 size = 1;
5392 return size;
9f296da3
AM
5393}
5394
f378ab09
AM
5395/* Return true if symbol is a strong function defined in an ELFv2
5396 object with st_other localentry bits of zero, ie. its local entry
5397 point coincides with its global entry point. */
5398
5399static bfd_boolean
5400is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5401{
5402 return (h != NULL
5403 && h->type == STT_FUNC
5404 && h->root.type == bfd_link_hash_defined
5405 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 5406 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
5407 && is_ppc64_elf (h->root.u.def.section->owner)
5408 && abiversion (h->root.u.def.section->owner) >= 2);
5409}
5410
854b41e7
AM
5411/* Return true if symbol is defined in a regular object file. */
5412
5413static bfd_boolean
5414is_static_defined (struct elf_link_hash_entry *h)
5415{
5416 return ((h->root.type == bfd_link_hash_defined
5417 || h->root.type == bfd_link_hash_defweak)
5418 && h->root.u.def.section != NULL
5419 && h->root.u.def.section->output_section != NULL);
5420}
5421
b31867b6
AM
5422/* If FDH is a function descriptor symbol, return the associated code
5423 entry symbol if it is defined. Return NULL otherwise. */
5424
5425static struct ppc_link_hash_entry *
5426defined_code_entry (struct ppc_link_hash_entry *fdh)
5427{
5428 if (fdh->is_func_descriptor)
5429 {
5430 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5431 if (fh->elf.root.type == bfd_link_hash_defined
5432 || fh->elf.root.type == bfd_link_hash_defweak)
5433 return fh;
5434 }
5435 return NULL;
5436}
5437
5438/* If FH is a function code entry symbol, return the associated
5439 function descriptor symbol if it is defined. Return NULL otherwise. */
5440
5441static struct ppc_link_hash_entry *
5442defined_func_desc (struct ppc_link_hash_entry *fh)
5443{
5444 if (fh->oh != NULL
5445 && fh->oh->is_func_descriptor)
5446 {
5447 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5448 if (fdh->elf.root.type == bfd_link_hash_defined
5449 || fdh->elf.root.type == bfd_link_hash_defweak)
5450 return fdh;
5451 }
5452 return NULL;
5453}
5454
8c5b4e52
AM
5455static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5456
5457/* Garbage collect sections, after first dealing with dot-symbols. */
5458
5459static bfd_boolean
5460ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5461{
5462 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5463
5464 if (htab != NULL && htab->need_func_desc_adj)
5465 {
5466 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5467 htab->need_func_desc_adj = 0;
5468 }
5469 return bfd_elf_gc_sections (abfd, info);
5470}
5471
74f0fb50
AM
5472/* Mark all our entry sym sections, both opd and code section. */
5473
5474static void
5475ppc64_elf_gc_keep (struct bfd_link_info *info)
5476{
5477 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5478 struct bfd_sym_chain *sym;
5479
4dfe6ac6
NC
5480 if (htab == NULL)
5481 return;
5482
74f0fb50
AM
5483 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5484 {
b31867b6 5485 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5486 asection *sec;
5487
5488 eh = (struct ppc_link_hash_entry *)
b31867b6 5489 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5490 if (eh == NULL)
5491 continue;
5492 if (eh->elf.root.type != bfd_link_hash_defined
5493 && eh->elf.root.type != bfd_link_hash_defweak)
5494 continue;
5495
b31867b6
AM
5496 fh = defined_code_entry (eh);
5497 if (fh != NULL)
74f0fb50 5498 {
b31867b6 5499 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5500 sec->flags |= SEC_KEEP;
5501 }
5502 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5503 && opd_entry_value (eh->elf.root.u.def.section,
5504 eh->elf.root.u.def.value,
aef36ac1 5505 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
5506 sec->flags |= SEC_KEEP;
5507
5508 sec = eh->elf.root.u.def.section;
5509 sec->flags |= SEC_KEEP;
5510 }
5511}
5512
64d03ab5
AM
5513/* Mark sections containing dynamically referenced symbols. When
5514 building shared libraries, we must assume that any visible symbol is
5515 referenced. */
5516
5517static bfd_boolean
5518ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5519{
5520 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5521 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5522 struct ppc_link_hash_entry *fdh;
b407645f 5523 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5524
64d03ab5 5525 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5526 fdh = defined_func_desc (eh);
5527 if (fdh != NULL)
5528 eh = fdh;
64d03ab5
AM
5529
5530 if ((eh->elf.root.type == bfd_link_hash_defined
5531 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 5532 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5533 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5534 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5535 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5536 && (!bfd_link_executable (info)
e278ae05 5537 || info->gc_keep_exported
b407645f
AM
5538 || info->export_dynamic
5539 || (eh->elf.dynamic
5540 && d != NULL
2cdcc330
AM
5541 && (*d->match) (&d->head, NULL,
5542 eh->elf.root.root.string)))
e278ae05 5543 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5544 || !bfd_hide_sym_by_version (info->version_info,
5545 eh->elf.root.root.string)))))
64d03ab5
AM
5546 {
5547 asection *code_sec;
b31867b6 5548 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5549
5550 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5551
5552 /* Function descriptor syms cause the associated
5553 function code sym section to be marked. */
b31867b6
AM
5554 fh = defined_code_entry (eh);
5555 if (fh != NULL)
5556 {
5557 code_sec = fh->elf.root.u.def.section;
5558 code_sec->flags |= SEC_KEEP;
5559 }
64d03ab5
AM
5560 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5561 && opd_entry_value (eh->elf.root.u.def.section,
5562 eh->elf.root.u.def.value,
aef36ac1 5563 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
5564 code_sec->flags |= SEC_KEEP;
5565 }
5566
5567 return TRUE;
5568}
5569
5bd4f169
AM
5570/* Return the section that should be marked against GC for a given
5571 relocation. */
5572
5573static asection *
4ce794b7 5574ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5575 struct bfd_link_info *info,
4ce794b7
AM
5576 Elf_Internal_Rela *rel,
5577 struct elf_link_hash_entry *h,
5578 Elf_Internal_Sym *sym)
5bd4f169 5579{
ccfa59ea
AM
5580 asection *rsec;
5581
ccfa59ea
AM
5582 /* Syms return NULL if we're marking .opd, so we avoid marking all
5583 function sections, as all functions are referenced in .opd. */
5584 rsec = NULL;
5585 if (get_opd_info (sec) != NULL)
5586 return rsec;
1e2f5b6e 5587
5bd4f169
AM
5588 if (h != NULL)
5589 {
04c9666a 5590 enum elf_ppc64_reloc_type r_type;
b31867b6 5591 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5592
4ce794b7 5593 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5594 switch (r_type)
5bd4f169
AM
5595 {
5596 case R_PPC64_GNU_VTINHERIT:
5597 case R_PPC64_GNU_VTENTRY:
5598 break;
5599
5600 default:
5601 switch (h->root.type)
5602 {
5603 case bfd_link_hash_defined:
5604 case bfd_link_hash_defweak:
ccfa59ea 5605 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5606 fdh = defined_func_desc (eh);
5607 if (fdh != NULL)
8c5b4e52
AM
5608 {
5609 /* -mcall-aixdesc code references the dot-symbol on
5610 a call reloc. Mark the function descriptor too
5611 against garbage collection. */
5612 fdh->elf.mark = 1;
60d67dc8
AM
5613 if (fdh->elf.is_weakalias)
5614 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
5615 eh = fdh;
5616 }
1e2f5b6e
AM
5617
5618 /* Function descriptor syms cause the associated
5619 function code sym section to be marked. */
b31867b6
AM
5620 fh = defined_code_entry (eh);
5621 if (fh != NULL)
ccfa59ea
AM
5622 {
5623 /* They also mark their opd section. */
74f0fb50 5624 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5625
b31867b6 5626 rsec = fh->elf.root.u.def.section;
ccfa59ea 5627 }
8387904d
AM
5628 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5629 && opd_entry_value (eh->elf.root.u.def.section,
5630 eh->elf.root.u.def.value,
aef36ac1 5631 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 5632 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5633 else
1e2f5b6e
AM
5634 rsec = h->root.u.def.section;
5635 break;
5bd4f169
AM
5636
5637 case bfd_link_hash_common:
1e2f5b6e
AM
5638 rsec = h->root.u.c.p->section;
5639 break;
5bd4f169
AM
5640
5641 default:
fb34365b 5642 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5643 }
5644 }
5645 }
5646 else
5647 {
74f0fb50 5648 struct _opd_sec_data *opd;
1e2f5b6e
AM
5649
5650 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5651 opd = get_opd_info (rsec);
5652 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5653 {
74f0fb50 5654 rsec->gc_mark = 1;
ccfa59ea 5655
51aecdc5 5656 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 5657 }
5bd4f169
AM
5658 }
5659
1e2f5b6e 5660 return rsec;
5bd4f169
AM
5661}
5662
deb0e272
AM
5663/* The maximum size of .sfpr. */
5664#define SFPR_MAX (218*4)
5665
5666struct sfpr_def_parms
5667{
699733f6
AM
5668 const char name[12];
5669 unsigned char lo, hi;
2cdcc330
AM
5670 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5671 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
5672};
5673
a4b6fadd
AM
5674/* Auto-generate _save*, _rest* functions in .sfpr.
5675 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5676 instead. */
deb0e272 5677
4dfe6ac6 5678static bfd_boolean
a4b6fadd
AM
5679sfpr_define (struct bfd_link_info *info,
5680 const struct sfpr_def_parms *parm,
5681 asection *stub_sec)
deb0e272
AM
5682{
5683 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5684 unsigned int i;
5685 size_t len = strlen (parm->name);
5686 bfd_boolean writing = FALSE;
699733f6 5687 char sym[16];
deb0e272 5688
4dfe6ac6
NC
5689 if (htab == NULL)
5690 return FALSE;
5691
deb0e272
AM
5692 memcpy (sym, parm->name, len);
5693 sym[len + 2] = 0;
5694
5695 for (i = parm->lo; i <= parm->hi; i++)
5696 {
a4b6fadd 5697 struct ppc_link_hash_entry *h;
deb0e272
AM
5698
5699 sym[len + 0] = i / 10 + '0';
5700 sym[len + 1] = i % 10 + '0';
a4b6fadd 5701 h = (struct ppc_link_hash_entry *)
b32547cd 5702 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 5703 if (stub_sec != NULL)
deb0e272 5704 {
a4b6fadd
AM
5705 if (h != NULL
5706 && h->elf.root.type == bfd_link_hash_defined
5707 && h->elf.root.u.def.section == htab->sfpr)
5708 {
5709 struct elf_link_hash_entry *s;
5710 char buf[32];
5711 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5712 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5713 if (s == NULL)
5714 return FALSE;
5715 if (s->root.type == bfd_link_hash_new
5716 || (s->root.type = bfd_link_hash_defined
5717 && s->root.u.def.section == stub_sec))
5718 {
5719 s->root.type = bfd_link_hash_defined;
5720 s->root.u.def.section = stub_sec;
7dda8d3c 5721 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
5722 + h->elf.root.u.def.value);
5723 s->ref_regular = 1;
5724 s->def_regular = 1;
5725 s->ref_regular_nonweak = 1;
5726 s->forced_local = 1;
5727 s->non_elf = 0;
5728 s->root.linker_def = 1;
5729 }
5730 }
5731 continue;
5732 }
5733 if (h != NULL)
5734 {
5735 h->save_res = 1;
5736 if (!h->elf.def_regular)
deb0e272 5737 {
a4b6fadd
AM
5738 h->elf.root.type = bfd_link_hash_defined;
5739 h->elf.root.u.def.section = htab->sfpr;
5740 h->elf.root.u.def.value = htab->sfpr->size;
5741 h->elf.type = STT_FUNC;
5742 h->elf.def_regular = 1;
b32547cd 5743 h->elf.non_elf = 0;
a4b6fadd
AM
5744 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5745 writing = TRUE;
deb0e272 5746 if (htab->sfpr->contents == NULL)
a4b6fadd 5747 {
2cdcc330
AM
5748 htab->sfpr->contents
5749 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd
AM
5750 if (htab->sfpr->contents == NULL)
5751 return FALSE;
5752 }
deb0e272
AM
5753 }
5754 }
5755 if (writing)
5756 {
5757 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5758 if (i != parm->hi)
5759 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5760 else
5761 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5762 htab->sfpr->size = p - htab->sfpr->contents;
5763 }
5764 }
5765
5766 return TRUE;
5767}
5768
5769static bfd_byte *
5770savegpr0 (bfd *abfd, bfd_byte *p, int r)
5771{
5772 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5773 return p + 4;
5774}
5775
5776static bfd_byte *
5777savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5778{
5779 p = savegpr0 (abfd, p, r);
a078d95a 5780 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5781 p = p + 4;
5782 bfd_put_32 (abfd, BLR, p);
5783 return p + 4;
5784}
5785
5786static bfd_byte *
5787restgpr0 (bfd *abfd, bfd_byte *p, int r)
5788{
5789 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5790 return p + 4;
5791}
5792
5793static bfd_byte *
5794restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5795{
a078d95a 5796 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5797 p = p + 4;
5798 p = restgpr0 (abfd, p, r);
5799 bfd_put_32 (abfd, MTLR_R0, p);
5800 p = p + 4;
5801 if (r == 29)
5802 {
5803 p = restgpr0 (abfd, p, 30);
5804 p = restgpr0 (abfd, p, 31);
5805 }
5806 bfd_put_32 (abfd, BLR, p);
5807 return p + 4;
5808}
5809
5810static bfd_byte *
5811savegpr1 (bfd *abfd, bfd_byte *p, int r)
5812{
5813 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5814 return p + 4;
5815}
5816
5817static bfd_byte *
5818savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5819{
5820 p = savegpr1 (abfd, p, r);
5821 bfd_put_32 (abfd, BLR, p);
5822 return p + 4;
5823}
5824
5825static bfd_byte *
5826restgpr1 (bfd *abfd, bfd_byte *p, int r)
5827{
5828 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5829 return p + 4;
5830}
5831
5832static bfd_byte *
5833restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5834{
5835 p = restgpr1 (abfd, p, r);
5836 bfd_put_32 (abfd, BLR, p);
5837 return p + 4;
5838}
5839
5840static bfd_byte *
5841savefpr (bfd *abfd, bfd_byte *p, int r)
5842{
5843 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5844 return p + 4;
5845}
5846
5847static bfd_byte *
5848savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5849{
5850 p = savefpr (abfd, p, r);
a078d95a 5851 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5852 p = p + 4;
5853 bfd_put_32 (abfd, BLR, p);
5854 return p + 4;
5855}
5856
5857static bfd_byte *
5858restfpr (bfd *abfd, bfd_byte *p, int r)
5859{
5860 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5861 return p + 4;
5862}
5863
5864static bfd_byte *
5865restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5866{
a078d95a 5867 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5868 p = p + 4;
5869 p = restfpr (abfd, p, r);
5870 bfd_put_32 (abfd, MTLR_R0, p);
5871 p = p + 4;
5872 if (r == 29)
5873 {
5874 p = restfpr (abfd, p, 30);
5875 p = restfpr (abfd, p, 31);
5876 }
5877 bfd_put_32 (abfd, BLR, p);
5878 return p + 4;
5879}
5880
5881static bfd_byte *
5882savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5883{
5884 p = savefpr (abfd, p, r);
5885 bfd_put_32 (abfd, BLR, p);
5886 return p + 4;
5887}
5888
5889static bfd_byte *
5890restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5891{
5892 p = restfpr (abfd, p, r);
5893 bfd_put_32 (abfd, BLR, p);
5894 return p + 4;
5895}
5896
5897static bfd_byte *
5898savevr (bfd *abfd, bfd_byte *p, int r)
5899{
5900 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5901 p = p + 4;
5902 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5903 return p + 4;
5904}
5905
5906static bfd_byte *
5907savevr_tail (bfd *abfd, bfd_byte *p, int r)
5908{
5909 p = savevr (abfd, p, r);
5910 bfd_put_32 (abfd, BLR, p);
5911 return p + 4;
5912}
5913
5914static bfd_byte *
5915restvr (bfd *abfd, bfd_byte *p, int r)
5916{
5917 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5918 p = p + 4;
5919 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5920 return p + 4;
5921}
5922
5923static bfd_byte *
5924restvr_tail (bfd *abfd, bfd_byte *p, int r)
5925{
5926 p = restvr (abfd, p, r);
5927 bfd_put_32 (abfd, BLR, p);
5928 return p + 4;
5929}
5930
e86ce104
AM
5931/* Called via elf_link_hash_traverse to transfer dynamic linking
5932 information on function code symbol entries to their corresponding
5933 function descriptor symbol entries. */
deb0e272 5934
b34976b6 5935static bfd_boolean
4ce794b7 5936func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 5937{
e86ce104 5938 struct bfd_link_info *info;
65f38f15 5939 struct ppc_link_hash_table *htab;
50bc7936
AM
5940 struct ppc_link_hash_entry *fh;
5941 struct ppc_link_hash_entry *fdh;
5942 bfd_boolean force_local;
5bd4f169 5943
50bc7936
AM
5944 fh = (struct ppc_link_hash_entry *) h;
5945 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 5946 return TRUE;
e86ce104 5947
8c5b4e52
AM
5948 if (!fh->is_func)
5949 return TRUE;
5950
5951 if (fh->elf.root.root.string[0] != '.'
5952 || fh->elf.root.root.string[1] == '\0')
5953 return TRUE;
5954
4ce794b7 5955 info = inf;
65f38f15 5956 htab = ppc_hash_table (info);
4dfe6ac6
NC
5957 if (htab == NULL)
5958 return FALSE;
5bd4f169 5959
8c5b4e52
AM
5960 /* Find the corresponding function descriptor symbol. */
5961 fdh = lookup_fdh (fh, htab);
5962
c09bdfe5
AM
5963 /* Resolve undefined references to dot-symbols as the value
5964 in the function descriptor, if we have one in a regular object.
5965 This is to satisfy cases like ".quad .foo". Calls to functions
5966 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
5967 if ((fh->elf.root.type == bfd_link_hash_undefined
5968 || fh->elf.root.type == bfd_link_hash_undefweak)
5969 && (fdh->elf.root.type == bfd_link_hash_defined
5970 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
5971 && get_opd_info (fdh->elf.root.u.def.section) != NULL
5972 && opd_entry_value (fdh->elf.root.u.def.section,
5973 fdh->elf.root.u.def.value,
c09bdfe5 5974 &fh->elf.root.u.def.section,
aef36ac1 5975 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 5976 {
b31867b6 5977 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 5978 fh->elf.forced_local = 1;
b31867b6
AM
5979 fh->elf.def_regular = fdh->elf.def_regular;
5980 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
5981 }
5982
8c5b4e52
AM
5983 if (!fh->elf.dynamic)
5984 {
5985 struct plt_entry *ent;
5bd4f169 5986
8c5b4e52
AM
5987 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5988 if (ent->plt.refcount > 0)
5989 break;
5990 if (ent == NULL)
5991 return TRUE;
5992 }
5bd4f169 5993
8c5b4e52 5994 /* Create a descriptor as undefined if necessary. */
50bc7936 5995 if (fdh == NULL
0e1862bb 5996 && !bfd_link_executable (info)
50bc7936
AM
5997 && (fh->elf.root.type == bfd_link_hash_undefined
5998 || fh->elf.root.type == bfd_link_hash_undefweak))
5999 {
908b32fc 6000 fdh = make_fdh (info, fh);
bb700d78
AM
6001 if (fdh == NULL)
6002 return FALSE;
50bc7936 6003 }
648cca2c 6004
8c5b4e52 6005 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6006 if (fdh != NULL
6007 && fdh->fake
8c5b4e52
AM
6008 && (fh->elf.root.type == bfd_link_hash_defined
6009 || fh->elf.root.type == bfd_link_hash_defweak))
6010 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 6011
8c5b4e52
AM
6012 /* Transfer dynamic linking information to the function descriptor. */
6013 if (fdh != NULL)
6014 {
f5385ebf
AM
6015 fdh->elf.ref_regular |= fh->elf.ref_regular;
6016 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6017 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6018 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6019 fdh->elf.dynamic |= fh->elf.dynamic;
6020 fdh->elf.needs_plt |= (fh->elf.needs_plt
6021 || fh->elf.type == STT_FUNC
6022 || fh->elf.type == STT_GNU_IFUNC);
6023 move_plt_plist (fh, fdh);
6024
6025 if (!fdh->elf.forced_local
6026 && fh->elf.dynindx != -1)
6027 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6028 return FALSE;
e86ce104
AM
6029 }
6030
50bc7936
AM
6031 /* Now that the info is on the function descriptor, clear the
6032 function code sym info. Any function code syms for which we
6033 don't have a definition in a regular file, we force local.
6034 This prevents a shared library from exporting syms that have
6035 been imported from another library. Function code syms that
6036 are really in the library we must leave global to prevent the
6037 linker dragging in a definition from a static library. */
93f3fa99
AM
6038 force_local = (!fh->elf.def_regular
6039 || fdh == NULL
6040 || !fdh->elf.def_regular
6041 || fdh->elf.forced_local);
50bc7936
AM
6042 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6043
b34976b6 6044 return TRUE;
e86ce104 6045}
40b8271b 6046
a4b6fadd
AM
6047static const struct sfpr_def_parms save_res_funcs[] =
6048 {
6049 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6050 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6051 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6052 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6053 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6054 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6055 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6056 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6057 { "._savef", 14, 31, savefpr, savefpr1_tail },
6058 { "._restf", 14, 31, restfpr, restfpr1_tail },
6059 { "_savevr_", 20, 31, savevr, savevr_tail },
6060 { "_restvr_", 20, 31, restvr, restvr_tail }
6061 };
6062
e86ce104 6063/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6064 this hook to a) provide some gcc support functions, and b) transfer
6065 dynamic linking information gathered so far on function code symbol
6066 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6067
b34976b6 6068static bfd_boolean
4ce794b7
AM
6069ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6070 struct bfd_link_info *info)
e86ce104
AM
6071{
6072 struct ppc_link_hash_table *htab;
6073
6074 htab = ppc_hash_table (info);
4dfe6ac6
NC
6075 if (htab == NULL)
6076 return FALSE;
6077
b32547cd
AM
6078 /* Provide any missing _save* and _rest* functions. */
6079 if (htab->sfpr != NULL)
6080 {
6081 unsigned int i;
6082
6083 htab->sfpr->size = 0;
6084 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6085 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6086 return FALSE;
6087 if (htab->sfpr->size == 0)
6088 htab->sfpr->flags |= SEC_EXCLUDE;
6089 }
6090
6091 if (bfd_link_relocatable (info))
6092 return TRUE;
6093
6094 if (htab->elf.hgot != NULL)
dba6fa9b
AM
6095 {
6096 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6097 /* Make .TOC. defined so as to prevent it being made dynamic.
6098 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6099 if (!htab->elf.hgot->def_regular
6100 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6101 {
6102 htab->elf.hgot->root.type = bfd_link_hash_defined;
6103 htab->elf.hgot->root.u.def.value = 0;
6104 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6105 htab->elf.hgot->def_regular = 1;
6106 htab->elf.hgot->root.linker_def = 1;
6107 }
dba6fa9b 6108 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6109 htab->elf.hgot->other
6110 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6111 }
c66bb0ee 6112
8c5b4e52
AM
6113 if (htab->need_func_desc_adj)
6114 {
6115 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6116 htab->need_func_desc_adj = 0;
6117 }
805fc799 6118
b34976b6 6119 return TRUE;
e86ce104
AM
6120}
6121
98bbb1b8 6122/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 6123
98bbb1b8 6124static asection *
a345bc8d
AM
6125readonly_dynrelocs (struct elf_link_hash_entry *h)
6126{
6127 struct ppc_link_hash_entry *eh;
6128 struct elf_dyn_relocs *p;
6129
6130 eh = (struct ppc_link_hash_entry *) h;
6131 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6132 {
6133 asection *s = p->sec->output_section;
6134
6135 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 6136 return p->sec;
a345bc8d 6137 }
98bbb1b8 6138 return NULL;
a345bc8d
AM
6139}
6140
d311bc8b 6141/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6142 aliases, that apply to read-only sections. Cannot be used after
6143 size_dynamic_sections. */
d311bc8b
AM
6144
6145static bfd_boolean
6146alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6147{
6148 struct ppc_link_hash_entry *eh;
6149
6150 eh = (struct ppc_link_hash_entry *) h;
6151 do
6152 {
6153 if (readonly_dynrelocs (&eh->elf))
6154 return TRUE;
ab2477e1 6155 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
2cdcc330
AM
6156 }
6157 while (eh != NULL && &eh->elf != h);
d311bc8b
AM
6158
6159 return FALSE;
6160}
8a2058b5 6161
8a9e8e72
AM
6162/* Return whether EH has pc-relative dynamic relocs. */
6163
6164static bfd_boolean
6165pc_dynrelocs (struct ppc_link_hash_entry *eh)
6166{
6167 struct elf_dyn_relocs *p;
6168
6169 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6170 if (p->pc_count != 0)
6171 return TRUE;
6172 return FALSE;
6173}
6174
8a2058b5
AM
6175/* Return true if a global entry stub will be created for H. Valid
6176 for ELFv2 before plt entries have been allocated. */
6177
6178static bfd_boolean
6179global_entry_stub (struct elf_link_hash_entry *h)
6180{
6181 struct plt_entry *pent;
6182
6183 if (!h->pointer_equality_needed
6184 || h->def_regular)
6185 return FALSE;
6186
6187 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6188 if (pent->plt.refcount > 0
6189 && pent->addend == 0)
6190 return TRUE;
6191
6192 return FALSE;
6193}
6194
e86ce104
AM
6195/* Adjust a symbol defined by a dynamic object and referenced by a
6196 regular object. The current definition is in some section of the
6197 dynamic object, but we're not including those sections. We have to
6198 change the definition to something the rest of the link can
6199 understand. */
6200
b34976b6 6201static bfd_boolean
4ce794b7
AM
6202ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6203 struct elf_link_hash_entry *h)
e86ce104
AM
6204{
6205 struct ppc_link_hash_table *htab;
5474d94f 6206 asection *s, *srel;
e86ce104
AM
6207
6208 htab = ppc_hash_table (info);
4dfe6ac6
NC
6209 if (htab == NULL)
6210 return FALSE;
e86ce104
AM
6211
6212 /* Deal with function syms. */
6213 if (h->type == STT_FUNC
e054468f 6214 || h->type == STT_GNU_IFUNC
f5385ebf 6215 || h->needs_plt)
e86ce104 6216 {
529fe20e
AM
6217 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6218 || SYMBOL_CALLS_LOCAL (info, h)
6219 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6220 /* Discard dyn_relocs when non-pic if we've decided that a
6221 function symbol is local and not an ifunc. We keep dynamic
6222 relocs for ifuncs when local rather than always emitting a
6223 plt call stub for them and defining the symbol on the call
6224 stub. We can't do that for ELFv1 anyway (a function symbol
6225 is defined on a descriptor, not code) and it can be faster at
6226 run-time due to not needing to bounce through a stub. The
6227 dyn_relocs for ifuncs will be applied even in a static
6228 executable. */
6229 if (!bfd_link_pic (info)
6230 && h->type != STT_GNU_IFUNC
6231 && local)
6232 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6233
e86ce104
AM
6234 /* Clear procedure linkage table information for any symbol that
6235 won't need a .plt entry. */
411e1bfb
AM
6236 struct plt_entry *ent;
6237 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6238 if (ent->plt.refcount > 0)
6239 break;
8387904d 6240 if (ent == NULL
2d7ad24e
AM
6241 || (h->type != STT_GNU_IFUNC
6242 && local
3e04d765
AM
6243 && (htab->can_convert_all_inline_plt
6244 || (((struct ppc_link_hash_entry *) h)->tls_mask
6245 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6246 {
411e1bfb 6247 h->plt.plist = NULL;
f5385ebf 6248 h->needs_plt = 0;
d1eca1e4 6249 h->pointer_equality_needed = 0;
40b8271b 6250 }
8a2058b5 6251 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6252 {
d1eca1e4
AM
6253 /* Taking a function's address in a read/write section
6254 doesn't require us to define the function symbol in the
6255 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6256 be used instead. The reason we prefer a few more dynamic
6257 relocs is that calling via a global entry stub costs a
6258 few more instructions, and pointer_equality_needed causes
6259 extra work in ld.so when resolving these symbols. */
529fe20e 6260 if (global_entry_stub (h))
d1eca1e4 6261 {
ab2477e1 6262 if (!readonly_dynrelocs (h))
529fe20e
AM
6263 {
6264 h->pointer_equality_needed = 0;
04383fd1
AM
6265 /* If we haven't seen a branch reloc and the symbol
6266 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6267 if (!h->needs_plt)
6268 h->plt.plist = NULL;
6269 }
6270 else if (!bfd_link_pic (info))
6271 /* We are going to be defining the function symbol on the
6272 plt stub, so no dyn_relocs needed when non-pic. */
6273 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
6274 }
6275
3988aed5
AM
6276 /* ELFv2 function symbols can't have copy relocs. */
6277 return TRUE;
6278 }
6279 else if (!h->needs_plt
ab2477e1 6280 && !readonly_dynrelocs (h))
3988aed5 6281 {
04383fd1
AM
6282 /* If we haven't seen a branch reloc and the symbol isn't an
6283 ifunc then we don't need a plt entry. */
3988aed5
AM
6284 h->plt.plist = NULL;
6285 h->pointer_equality_needed = 0;
a345bc8d
AM
6286 return TRUE;
6287 }
5bd4f169 6288 }
bbd7ec4a 6289 else
411e1bfb 6290 h->plt.plist = NULL;
5bd4f169
AM
6291
6292 /* If this is a weak symbol, and there is a real definition, the
6293 processor independent code will have arranged for us to see the
6294 real definition first, and we can just use the same value. */
60d67dc8 6295 if (h->is_weakalias)
5bd4f169 6296 {
60d67dc8
AM
6297 struct elf_link_hash_entry *def = weakdef (h);
6298 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6299 h->root.u.def.section = def->root.u.def.section;
6300 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6301 if (def->root.u.def.section == htab->elf.sdynbss
6302 || def->root.u.def.section == htab->elf.sdynrelro)
6303 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 6304 return TRUE;
5bd4f169
AM
6305 }
6306
5bd4f169
AM
6307 /* If we are creating a shared library, we must presume that the
6308 only references to the symbol are via the global offset table.
6309 For such cases we need not do anything here; the relocations will
6310 be handled correctly by relocate_section. */
0e1862bb 6311 if (bfd_link_pic (info))
b34976b6 6312 return TRUE;
5bd4f169 6313
65f38f15
AM
6314 /* If there are no references to this symbol that do not use the
6315 GOT, we don't need to generate a copy reloc. */
f5385ebf 6316 if (!h->non_got_ref)
b34976b6 6317 return TRUE;
65f38f15 6318
b186458a 6319 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6320 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6321
d93d1c80
AM
6322 /* If -z nocopyreloc was given, don't generate them either. */
6323 || info->nocopyreloc
a127494f 6324
dce2246a 6325 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6326 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 6327 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 6328
d93d1c80
AM
6329 /* Protected variables do not work with .dynbss. The copy in
6330 .dynbss won't be used by the shared library with the protected
6331 definition for the variable. Text relocations are preferable
6332 to an incorrect program. */
6333 || h->protected_def)
529fe20e 6334 return TRUE;
a127494f 6335
5d35169e 6336 if (h->plt.plist != NULL)
97b639ba
AM
6337 {
6338 /* We should never get here, but unfortunately there are versions
6339 of gcc out there that improperly (for this ABI) put initialized
6340 function pointers, vtable refs and suchlike in read-only
6341 sections. Allow them to proceed, but warn that this might
6342 break at runtime. */
25f53a85 6343 info->callbacks->einfo
c1c8c1ef 6344 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6345 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6346 h->root.root.string);
6347 }
5d35169e
AM
6348
6349 /* This is a reference to a symbol defined by a dynamic object which
6350 is not a function. */
6351
5bd4f169
AM
6352 /* We must allocate the symbol in our .dynbss section, which will
6353 become part of the .bss section of the executable. There will be
6354 an entry for this symbol in the .dynsym section. The dynamic
6355 object will contain position independent code, so all references
6356 from the dynamic object to this symbol will go through the global
6357 offset table. The dynamic linker will use the .dynsym entry to
6358 determine the address it must put in the global offset table, so
6359 both the dynamic object and the regular object will refer to the
6360 same memory location for the variable. */
5474d94f
AM
6361 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6362 {
6363 s = htab->elf.sdynrelro;
6364 srel = htab->elf.sreldynrelro;
6365 }
6366 else
6367 {
6368 s = htab->elf.sdynbss;
6369 srel = htab->elf.srelbss;
6370 }
1d7e9d18 6371 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6372 {
4a7e5234
AM
6373 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6374 linker to copy the initial value out of the dynamic object
6375 and into the runtime process image. */
5474d94f 6376 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6377 h->needs_copy = 1;
5bd4f169
AM
6378 }
6379
529fe20e
AM
6380 /* We no longer want dyn_relocs. */
6381 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 6382 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6383}
6384
e86ce104
AM
6385/* If given a function descriptor symbol, hide both the function code
6386 sym and the descriptor. */
6387static void
4ce794b7
AM
6388ppc64_elf_hide_symbol (struct bfd_link_info *info,
6389 struct elf_link_hash_entry *h,
6390 bfd_boolean force_local)
e86ce104 6391{
34814b9f 6392 struct ppc_link_hash_entry *eh;
e86ce104
AM
6393 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6394
87469ba2
AM
6395 if (ppc_hash_table (info) == NULL)
6396 return;
6397
34814b9f
AM
6398 eh = (struct ppc_link_hash_entry *) h;
6399 if (eh->is_func_descriptor)
e86ce104 6400 {
34814b9f 6401 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6402
721956f4 6403 if (fh == NULL)
d1329ca3
AM
6404 {
6405 const char *p, *q;
b8ac2841 6406 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6407 char save;
6408
6409 /* We aren't supposed to use alloca in BFD because on
6410 systems which do not have alloca the version in libiberty
6411 calls xmalloc, which might cause the program to crash
6412 when it runs out of memory. This function doesn't have a
6413 return status, so there's no way to gracefully return an
6414 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6415 accessed; It's either a string in an ELF string table,
6416 or allocated in an objalloc structure. */
d1329ca3 6417
34814b9f 6418 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6419 save = *p;
6420 *(char *) p = '.';
34814b9f 6421 fh = (struct ppc_link_hash_entry *)
b8ac2841 6422 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6423 *(char *) p = save;
6424
6425 /* Unfortunately, if it so happens that the string we were
6426 looking for was allocated immediately before this string,
6427 then we overwrote the string terminator. That's the only
6428 reason the lookup should fail. */
6429 if (fh == NULL)
6430 {
34814b9f
AM
6431 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6432 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6433 --q, --p;
34814b9f
AM
6434 if (q < eh->elf.root.root.string && *p == '.')
6435 fh = (struct ppc_link_hash_entry *)
b8ac2841 6436 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6437 }
6438 if (fh != NULL)
6439 {
34814b9f
AM
6440 eh->oh = fh;
6441 fh->oh = eh;
d1329ca3
AM
6442 }
6443 }
e86ce104 6444 if (fh != NULL)
34814b9f 6445 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6446 }
6447}
6448
411e1bfb 6449static bfd_boolean
8843416a
AM
6450get_sym_h (struct elf_link_hash_entry **hp,
6451 Elf_Internal_Sym **symp,
6452 asection **symsecp,
f961d9dd 6453 unsigned char **tls_maskp,
8843416a
AM
6454 Elf_Internal_Sym **locsymsp,
6455 unsigned long r_symndx,
6456 bfd *ibfd)
411e1bfb 6457{
0ffa91dd 6458 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6459
6460 if (r_symndx >= symtab_hdr->sh_info)
6461 {
6462 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6463 struct elf_link_hash_entry *h;
6464
6465 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6466 h = elf_follow_link (h);
411e1bfb
AM
6467
6468 if (hp != NULL)
6469 *hp = h;
6470
6471 if (symp != NULL)
6472 *symp = NULL;
6473
6474 if (symsecp != NULL)
6475 {
6476 asection *symsec = NULL;
6477 if (h->root.type == bfd_link_hash_defined
6478 || h->root.type == bfd_link_hash_defweak)
6479 symsec = h->root.u.def.section;
6480 *symsecp = symsec;
6481 }
6482
e7b938ca 6483 if (tls_maskp != NULL)
411e1bfb
AM
6484 {
6485 struct ppc_link_hash_entry *eh;
6486
6487 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6488 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6489 }
6490 }
6491 else
6492 {
6493 Elf_Internal_Sym *sym;
6494 Elf_Internal_Sym *locsyms = *locsymsp;
6495
6496 if (locsyms == NULL)
6497 {
6498 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6499 if (locsyms == NULL)
6500 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6501 symtab_hdr->sh_info,
6502 0, NULL, NULL, NULL);
6503 if (locsyms == NULL)
6504 return FALSE;
6505 *locsymsp = locsyms;
6506 }
6507 sym = locsyms + r_symndx;
6508
6509 if (hp != NULL)
6510 *hp = NULL;
6511
6512 if (symp != NULL)
6513 *symp = sym;
6514
6515 if (symsecp != NULL)
cb33740c 6516 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6517
e7b938ca 6518 if (tls_maskp != NULL)
411e1bfb
AM
6519 {
6520 struct got_entry **lgot_ents;
f961d9dd 6521 unsigned char *tls_mask;
411e1bfb 6522
e7b938ca 6523 tls_mask = NULL;
411e1bfb
AM
6524 lgot_ents = elf_local_got_ents (ibfd);
6525 if (lgot_ents != NULL)
6526 {
e054468f
AM
6527 struct plt_entry **local_plt = (struct plt_entry **)
6528 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6529 unsigned char *lgot_masks = (unsigned char *)
e054468f 6530 (local_plt + symtab_hdr->sh_info);
e7b938ca 6531 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6532 }
e7b938ca 6533 *tls_maskp = tls_mask;
411e1bfb
AM
6534 }
6535 }
6536 return TRUE;
6537}
6538
e7b938ca 6539/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6540 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6541 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6542
6543static int
f961d9dd 6544get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6545 unsigned long *toc_symndx,
6546 bfd_vma *toc_addend,
0d4792f7 6547 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6548 const Elf_Internal_Rela *rel,
6549 bfd *ibfd)
411e1bfb
AM
6550{
6551 unsigned long r_symndx;
0d4792f7 6552 int next_r;
411e1bfb
AM
6553 struct elf_link_hash_entry *h;
6554 Elf_Internal_Sym *sym;
6555 asection *sec;
6556 bfd_vma off;
6557
6558 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6559 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6560 return 0;
411e1bfb 6561
37da22e5
AM
6562 if ((*tls_maskp != NULL
6563 && (**tls_maskp & TLS_TLS) != 0
6564 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 6565 || sec == NULL
6bee8834 6566 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6567 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6568 return 1;
411e1bfb
AM
6569
6570 /* Look inside a TOC section too. */
6571 if (h != NULL)
6572 {
6573 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6574 off = h->root.u.def.value;
6575 }
6576 else
6577 off = sym->st_value;
6578 off += rel->r_addend;
6579 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6580 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6581 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6582 if (toc_symndx != NULL)
6583 *toc_symndx = r_symndx;
3a71aa26
AM
6584 if (toc_addend != NULL)
6585 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6586 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6587 return 0;
854b41e7 6588 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6589 && (next_r == -1 || next_r == -2))
6590 return 1 - next_r;
951fd09b 6591 return 1;
411e1bfb
AM
6592}
6593
3b421ab3
AM
6594/* Find (or create) an entry in the tocsave hash table. */
6595
6596static struct tocsave_entry *
6597tocsave_find (struct ppc_link_hash_table *htab,
6598 enum insert_option insert,
6599 Elf_Internal_Sym **local_syms,
6600 const Elf_Internal_Rela *irela,
6601 bfd *ibfd)
6602{
6603 unsigned long r_indx;
6604 struct elf_link_hash_entry *h;
6605 Elf_Internal_Sym *sym;
6606 struct tocsave_entry ent, *p;
6607 hashval_t hash;
6608 struct tocsave_entry **slot;
6609
6610 r_indx = ELF64_R_SYM (irela->r_info);
6611 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6612 return NULL;
6613 if (ent.sec == NULL || ent.sec->output_section == NULL)
6614 {
4eca0228 6615 _bfd_error_handler
871b3ab2 6616 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
6617 return NULL;
6618 }
6619
6620 if (h != NULL)
6621 ent.offset = h->root.u.def.value;
6622 else
6623 ent.offset = sym->st_value;
6624 ent.offset += irela->r_addend;
6625
6626 hash = tocsave_htab_hash (&ent);
6627 slot = ((struct tocsave_entry **)
6628 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6629 if (slot == NULL)
6630 return NULL;
6631
6632 if (*slot == NULL)
6633 {
6634 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6635 if (p == NULL)
6636 return NULL;
6637 *p = ent;
6638 *slot = p;
6639 }
6640 return *slot;
6641}
6642
754021d0 6643/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6644 code for the old ABI, these will already have been done. */
754021d0
AM
6645
6646static bfd_boolean
6647adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6648{
6649 struct ppc_link_hash_entry *eh;
6650 asection *sym_sec;
74f0fb50 6651 struct _opd_sec_data *opd;
754021d0
AM
6652
6653 if (h->root.type == bfd_link_hash_indirect)
6654 return TRUE;
6655
754021d0
AM
6656 if (h->root.type != bfd_link_hash_defined
6657 && h->root.type != bfd_link_hash_defweak)
6658 return TRUE;
6659
6660 eh = (struct ppc_link_hash_entry *) h;
6661 if (eh->adjust_done)
6662 return TRUE;
6663
6664 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6665 opd = get_opd_info (sym_sec);
6666 if (opd != NULL && opd->adjust != NULL)
754021d0 6667 {
51aecdc5 6668 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
6669 if (adjust == -1)
6670 {
6671 /* This entry has been deleted. */
b3fac117 6672 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6673 if (dsec == NULL)
6674 {
6675 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 6676 if (discarded_section (dsec))
81688140 6677 {
b3fac117 6678 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6679 break;
6680 }
6681 }
4025353c 6682 eh->elf.root.u.def.value = 0;
81688140 6683 eh->elf.root.u.def.section = dsec;
4025353c
AM
6684 }
6685 else
6686 eh->elf.root.u.def.value += adjust;
754021d0
AM
6687 eh->adjust_done = 1;
6688 }
6689 return TRUE;
6690}
6691
8c1d1bb8 6692/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 6693 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
6694 have already been determined. */
6695
6696static bfd_boolean
6697dec_dynrel_count (bfd_vma r_info,
6698 asection *sec,
6699 struct bfd_link_info *info,
6700 Elf_Internal_Sym **local_syms,
6701 struct elf_link_hash_entry *h,
19e08130 6702 Elf_Internal_Sym *sym)
8c1d1bb8
AM
6703{
6704 enum elf_ppc64_reloc_type r_type;
19e08130 6705 asection *sym_sec = NULL;
8c1d1bb8
AM
6706
6707 /* Can this reloc be dynamic? This switch, and later tests here
6708 should be kept in sync with the code in check_relocs. */
6709 r_type = ELF64_R_TYPE (r_info);
6710 switch (r_type)
6711 {
6712 default:
6713 return TRUE;
6714
6715 case R_PPC64_TPREL16:
6716 case R_PPC64_TPREL16_LO:
6717 case R_PPC64_TPREL16_HI:
6718 case R_PPC64_TPREL16_HA:
6719 case R_PPC64_TPREL16_DS:
6720 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
6721 case R_PPC64_TPREL16_HIGH:
6722 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
6723 case R_PPC64_TPREL16_HIGHER:
6724 case R_PPC64_TPREL16_HIGHERA:
6725 case R_PPC64_TPREL16_HIGHEST:
6726 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8
AM
6727 case R_PPC64_TPREL64:
6728 case R_PPC64_DTPMOD64:
6729 case R_PPC64_DTPREL64:
6730 case R_PPC64_ADDR64:
6731 case R_PPC64_REL30:
6732 case R_PPC64_REL32:
6733 case R_PPC64_REL64:
6734 case R_PPC64_ADDR14:
6735 case R_PPC64_ADDR14_BRNTAKEN:
6736 case R_PPC64_ADDR14_BRTAKEN:
6737 case R_PPC64_ADDR16:
6738 case R_PPC64_ADDR16_DS:
6739 case R_PPC64_ADDR16_HA:
6740 case R_PPC64_ADDR16_HI:
f9c6b907
AM
6741 case R_PPC64_ADDR16_HIGH:
6742 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
6743 case R_PPC64_ADDR16_HIGHER:
6744 case R_PPC64_ADDR16_HIGHERA:
6745 case R_PPC64_ADDR16_HIGHEST:
6746 case R_PPC64_ADDR16_HIGHESTA:
6747 case R_PPC64_ADDR16_LO:
6748 case R_PPC64_ADDR16_LO_DS:
6749 case R_PPC64_ADDR24:
6750 case R_PPC64_ADDR32:
6751 case R_PPC64_UADDR16:
6752 case R_PPC64_UADDR32:
6753 case R_PPC64_UADDR64:
6754 case R_PPC64_TOC:
5663e321
AM
6755 case R_PPC64_D34:
6756 case R_PPC64_D34_LO:
6757 case R_PPC64_D34_HI30:
6758 case R_PPC64_D34_HA30:
6759 case R_PPC64_ADDR16_HIGHER34:
6760 case R_PPC64_ADDR16_HIGHERA34:
6761 case R_PPC64_ADDR16_HIGHEST34:
6762 case R_PPC64_ADDR16_HIGHESTA34:
6763 case R_PPC64_D28:
8c1d1bb8
AM
6764 break;
6765 }
6766
6767 if (local_syms != NULL)
6768 {
6769 unsigned long r_symndx;
8c1d1bb8
AM
6770 bfd *ibfd = sec->owner;
6771
6772 r_symndx = ELF64_R_SYM (r_info);
6773 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6774 return FALSE;
6775 }
6776
0e1862bb 6777 if ((bfd_link_pic (info)
1d483afe 6778 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 6779 || (h != NULL
198f1157 6780 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
6781 || h->root.type == bfd_link_hash_defweak
6782 || !h->def_regular))))
6783 || (ELIMINATE_COPY_RELOCS
0e1862bb 6784 && !bfd_link_pic (info)
8c1d1bb8
AM
6785 && h != NULL
6786 && (h->root.type == bfd_link_hash_defweak
6787 || !h->def_regular)))
6788 ;
6789 else
6790 return TRUE;
6791
6792 if (h != NULL)
6edfbbad 6793 {
19e08130
AM
6794 struct elf_dyn_relocs *p;
6795 struct elf_dyn_relocs **pp;
6796 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6797
6798 /* elf_gc_sweep may have already removed all dyn relocs associated
6799 with local syms for a given section. Also, symbol flags are
6800 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6801 report a dynreloc miscount. */
6802 if (*pp == NULL && info->gc_sections)
6803 return TRUE;
6804
6805 while ((p = *pp) != NULL)
60124e18 6806 {
19e08130
AM
6807 if (p->sec == sec)
6808 {
6809 if (!must_be_dyn_reloc (info, r_type))
6810 p->pc_count -= 1;
6811 p->count -= 1;
6812 if (p->count == 0)
6813 *pp = p->next;
6814 return TRUE;
6815 }
6816 pp = &p->next;
60124e18 6817 }
6edfbbad 6818 }
19e08130
AM
6819 else
6820 {
6821 struct ppc_dyn_relocs *p;
6822 struct ppc_dyn_relocs **pp;
6823 void *vpp;
6824 bfd_boolean is_ifunc;
8c1d1bb8 6825
19e08130
AM
6826 if (local_syms == NULL)
6827 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6828 if (sym_sec == NULL)
6829 sym_sec = sec;
c57da1a7 6830
19e08130
AM
6831 vpp = &elf_section_data (sym_sec)->local_dynrel;
6832 pp = (struct ppc_dyn_relocs **) vpp;
6833
6834 if (*pp == NULL && info->gc_sections)
6835 return TRUE;
6836
6837 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6838 while ((p = *pp) != NULL)
8c1d1bb8 6839 {
19e08130
AM
6840 if (p->sec == sec && p->ifunc == is_ifunc)
6841 {
6842 p->count -= 1;
6843 if (p->count == 0)
6844 *pp = p->next;
6845 return TRUE;
6846 }
6847 pp = &p->next;
8c1d1bb8 6848 }
8c1d1bb8
AM
6849 }
6850
695344c0 6851 /* xgettext:c-format */
cf97bcb0
AM
6852 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6853 sec->owner, sec);
8c1d1bb8
AM
6854 bfd_set_error (bfd_error_bad_value);
6855 return FALSE;
6856}
6857
754021d0
AM
6858/* Remove unused Official Procedure Descriptor entries. Currently we
6859 only remove those associated with functions in discarded link-once
6860 sections, or weakly defined functions that have been overridden. It
6861 would be possible to remove many more entries for statically linked
6862 applications. */
6863
b34976b6 6864bfd_boolean
e7d1c40c 6865ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
6866{
6867 bfd *ibfd;
754021d0 6868 bfd_boolean some_edited = FALSE;
3f764659 6869 asection *need_pad = NULL;
e7d1c40c
AM
6870 struct ppc_link_hash_table *htab;
6871
6872 htab = ppc_hash_table (info);
6873 if (htab == NULL)
6874 return FALSE;
1e2f5b6e 6875
c72f2fb2 6876 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
6877 {
6878 asection *sec;
6879 Elf_Internal_Rela *relstart, *rel, *relend;
6880 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 6881 Elf_Internal_Sym *local_syms;
74f0fb50 6882 struct _opd_sec_data *opd;
51aecdc5 6883 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 6884 bfd_size_type cnt_16b = 0;
1e2f5b6e 6885
854b41e7
AM
6886 if (!is_ppc64_elf (ibfd))
6887 continue;
6888
1e2f5b6e 6889 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 6890 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
6891 continue;
6892
dbaa2011 6893 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
6894 continue;
6895
1e2f5b6e
AM
6896 if (sec->output_section == bfd_abs_section_ptr)
6897 continue;
6898
6899 /* Look through the section relocs. */
6900 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6901 continue;
6902
6cdc0ccc 6903 local_syms = NULL;
0ffa91dd 6904 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
6905
6906 /* Read the relocations. */
4ce794b7 6907 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 6908 info->keep_memory);
1e2f5b6e 6909 if (relstart == NULL)
b34976b6 6910 return FALSE;
1e2f5b6e
AM
6911
6912 /* First run through the relocs to check they are sane, and to
6913 determine whether we need to edit this opd section. */
b34976b6 6914 need_edit = FALSE;
51aecdc5 6915 broken = FALSE;
3f764659 6916 need_pad = sec;
1e2f5b6e 6917 relend = relstart + sec->reloc_count;
50bc7936 6918 for (rel = relstart; rel < relend; )
1e2f5b6e 6919 {
04c9666a 6920 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
6921 unsigned long r_symndx;
6922 asection *sym_sec;
6923 struct elf_link_hash_entry *h;
6924 Elf_Internal_Sym *sym;
51aecdc5 6925 bfd_vma offset;
1e2f5b6e 6926
51aecdc5 6927 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
6928 only interested in the reloc pointing to a function entry
6929 point. */
51aecdc5
AM
6930 offset = rel->r_offset;
6931 if (rel + 1 == relend
6932 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
6933 {
6934 /* If someone messes with .opd alignment then after a
6935 "ld -r" we might have padding in the middle of .opd.
6936 Also, there's nothing to prevent someone putting
6937 something silly in .opd with the assembler. No .opd
b34976b6 6938 optimization for them! */
3f764659 6939 broken_opd:
4eca0228 6940 _bfd_error_handler
871b3ab2 6941 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 6942 broken = TRUE;
1e2f5b6e
AM
6943 break;
6944 }
6945
50bc7936
AM
6946 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6947 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6948 {
4eca0228 6949 _bfd_error_handler
695344c0 6950 /* xgettext:c-format */
871b3ab2 6951 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 6952 ibfd, r_type);
51aecdc5 6953 broken = TRUE;
50bc7936
AM
6954 break;
6955 }
6956
1e2f5b6e 6957 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
6958 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6959 r_symndx, ibfd))
50bc7936 6960 goto error_ret;
1e2f5b6e
AM
6961
6962 if (sym_sec == NULL || sym_sec->owner == NULL)
6963 {
411e1bfb
AM
6964 const char *sym_name;
6965 if (h != NULL)
6966 sym_name = h->root.root.string;
6967 else
26c61ae5
L
6968 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6969 sym_sec);
411e1bfb 6970
4eca0228 6971 _bfd_error_handler
695344c0 6972 /* xgettext:c-format */
871b3ab2 6973 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 6974 ibfd, sym_name);
51aecdc5 6975 broken = TRUE;
1e2f5b6e
AM
6976 break;
6977 }
6978
51020317
AM
6979 /* opd entries are always for functions defined in the
6980 current input bfd. If the symbol isn't defined in the
6981 input bfd, then we won't be using the function in this
6982 bfd; It must be defined in a linkonce section in another
6983 bfd, or is weak. It's also possible that we are
6984 discarding the function due to a linker script /DISCARD/,
6985 which we test for via the output_section. */
6986 if (sym_sec->owner != ibfd
6987 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 6988 need_edit = TRUE;
1e2f5b6e 6989
50bc7936 6990 rel += 2;
51aecdc5
AM
6991 if (rel + 1 == relend
6992 || (rel + 2 < relend
6993 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
6994 ++rel;
6995
6996 if (rel == relend)
3f764659
JJ
6997 {
6998 if (sec->size == offset + 24)
6999 {
7000 need_pad = NULL;
7001 break;
7002 }
51aecdc5 7003 if (sec->size == offset + 16)
3f764659
JJ
7004 {
7005 cnt_16b++;
7006 break;
7007 }
7008 goto broken_opd;
7009 }
3f764659
JJ
7010 else if (rel + 1 < relend
7011 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7012 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7013 {
51aecdc5
AM
7014 if (rel[0].r_offset == offset + 16)
7015 cnt_16b++;
7016 else if (rel[0].r_offset != offset + 24)
7017 goto broken_opd;
3f764659
JJ
7018 }
7019 else
7020 goto broken_opd;
1e2f5b6e
AM
7021 }
7022
e7d1c40c 7023 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7024
51aecdc5 7025 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7026 {
7027 Elf_Internal_Rela *write_rel;
d4730f92 7028 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7029 bfd_byte *rptr, *wptr;
983bddc8 7030 bfd_byte *new_contents;
74f0fb50
AM
7031 bfd_size_type amt;
7032
983bddc8 7033 new_contents = NULL;
51aecdc5 7034 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7035 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7036 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7037 if (opd->adjust == NULL)
7038 return FALSE;
1e2f5b6e
AM
7039
7040 /* This seems a waste of time as input .opd sections are all
7041 zeros as generated by gcc, but I suppose there's no reason
7042 this will always be so. We might start putting something in
7043 the third word of .opd entries. */
7044 if ((sec->flags & SEC_IN_MEMORY) == 0)
7045 {
eea6121a
AM
7046 bfd_byte *loc;
7047 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7048 {
eea6121a
AM
7049 if (loc != NULL)
7050 free (loc);
50bc7936 7051 error_ret:
6cdc0ccc
AM
7052 if (local_syms != NULL
7053 && symtab_hdr->contents != (unsigned char *) local_syms)
7054 free (local_syms);
6cdc0ccc
AM
7055 if (elf_section_data (sec)->relocs != relstart)
7056 free (relstart);
b34976b6 7057 return FALSE;
6cdc0ccc 7058 }
1e2f5b6e
AM
7059 sec->contents = loc;
7060 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7061 }
7062
7063 elf_section_data (sec)->relocs = relstart;
7064
3f764659 7065 new_contents = sec->contents;
3f764659
JJ
7066 if (add_aux_fields)
7067 {
7068 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7069 if (new_contents == NULL)
7070 return FALSE;
51aecdc5 7071 need_pad = NULL;
3f764659 7072 }
b4f4e59f
AM
7073 wptr = new_contents;
7074 rptr = sec->contents;
1e2f5b6e 7075 write_rel = relstart;
51aecdc5 7076 for (rel = relstart; rel < relend; )
1e2f5b6e 7077 {
50bc7936
AM
7078 unsigned long r_symndx;
7079 asection *sym_sec;
7080 struct elf_link_hash_entry *h;
51aecdc5 7081 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7082 Elf_Internal_Sym *sym;
51aecdc5
AM
7083 long opd_ent_size;
7084 Elf_Internal_Rela *next_rel;
7085 bfd_boolean skip;
50bc7936
AM
7086
7087 r_symndx = ELF64_R_SYM (rel->r_info);
7088 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7089 r_symndx, ibfd))
50bc7936
AM
7090 goto error_ret;
7091
51aecdc5
AM
7092 next_rel = rel + 2;
7093 if (next_rel + 1 == relend
7094 || (next_rel + 2 < relend
7095 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7096 ++next_rel;
7097
7098 /* See if the .opd entry is full 24 byte or
7099 16 byte (with fd_aux entry overlapped with next
7100 fd_func). */
7101 opd_ent_size = 24;
7102 if (next_rel == relend)
1e2f5b6e 7103 {
51aecdc5 7104 if (sec->size == rel->r_offset + 16)
3f764659 7105 opd_ent_size = 16;
51aecdc5
AM
7106 }
7107 else if (next_rel->r_offset == rel->r_offset + 16)
7108 opd_ent_size = 16;
3f764659 7109
51aecdc5
AM
7110 if (h != NULL
7111 && h->root.root.string[0] == '.')
7112 {
8c5b4e52
AM
7113 fdh = ((struct ppc_link_hash_entry *) h)->oh;
7114 if (fdh != NULL)
7115 {
7116 fdh = ppc_follow_link (fdh);
7117 if (fdh->elf.root.type != bfd_link_hash_defined
7118 && fdh->elf.root.type != bfd_link_hash_defweak)
7119 fdh = NULL;
7120 }
51aecdc5 7121 }
1e2f5b6e 7122
51aecdc5
AM
7123 skip = (sym_sec->owner != ibfd
7124 || sym_sec->output_section == bfd_abs_section_ptr);
7125 if (skip)
7126 {
7127 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7128 {
51aecdc5
AM
7129 /* Arrange for the function descriptor sym
7130 to be dropped. */
7131 fdh->elf.root.u.def.value = 0;
7132 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7133 }
51aecdc5 7134 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7135
0e1862bb 7136 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7137 rel = next_rel;
7138 else
7139 while (1)
7140 {
7141 if (!dec_dynrel_count (rel->r_info, sec, info,
7142 NULL, h, sym))
7143 goto error_ret;
754021d0 7144
51aecdc5
AM
7145 if (++rel == next_rel)
7146 break;
1e2f5b6e 7147
51aecdc5
AM
7148 r_symndx = ELF64_R_SYM (rel->r_info);
7149 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7150 r_symndx, ibfd))
7151 goto error_ret;
7152 }
50bc7936
AM
7153 }
7154 else
1e2f5b6e 7155 {
51aecdc5
AM
7156 /* We'll be keeping this opd entry. */
7157 long adjust;
7158
7159 if (fdh != NULL)
7160 {
7161 /* Redefine the function descriptor symbol to
7162 this location in the opd section. It is
7163 necessary to update the value here rather
7164 than using an array of adjustments as we do
7165 for local symbols, because various places
7166 in the generic ELF code use the value
7167 stored in u.def.value. */
7168 fdh->elf.root.u.def.value = wptr - new_contents;
7169 fdh->adjust_done = 1;
7170 }
7171
7172 /* Local syms are a bit tricky. We could
7173 tweak them as they can be cached, but
7174 we'd need to look through the local syms
7175 for the function descriptor sym which we
7176 don't have at the moment. So keep an
7177 array of adjustments. */
7178 adjust = (wptr - new_contents) - (rptr - sec->contents);
7179 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7180
7181 if (wptr != rptr)
7182 memcpy (wptr, rptr, opd_ent_size);
7183 wptr += opd_ent_size;
7184 if (add_aux_fields && opd_ent_size == 16)
7185 {
7186 memset (wptr, '\0', 8);
7187 wptr += 8;
7188 }
7189
50bc7936 7190 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7191 new opd entries. */
7192 for ( ; rel != next_rel; ++rel)
7193 {
7194 rel->r_offset += adjust;
7195 if (write_rel != rel)
7196 memcpy (write_rel, rel, sizeof (*rel));
7197 ++write_rel;
7198 }
1e2f5b6e 7199 }
51aecdc5
AM
7200
7201 rptr += opd_ent_size;
1e2f5b6e
AM
7202 }
7203
3f764659 7204 sec->size = wptr - new_contents;
1e2f5b6e 7205 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7206 if (add_aux_fields)
7207 {
7208 free (sec->contents);
7209 sec->contents = new_contents;
7210 }
7211
05bf9422 7212 /* Fudge the header size too, as this is used later in
cdcf6e38 7213 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7214 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7215 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7216 some_edited = TRUE;
1e2f5b6e 7217 }
6cdc0ccc 7218 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7219 free (relstart);
6cdc0ccc 7220
411e1bfb
AM
7221 if (local_syms != NULL
7222 && symtab_hdr->contents != (unsigned char *) local_syms)
7223 {
7224 if (!info->keep_memory)
7225 free (local_syms);
7226 else
7227 symtab_hdr->contents = (unsigned char *) local_syms;
7228 }
7229 }
7230
754021d0
AM
7231 if (some_edited)
7232 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7233
3f764659
JJ
7234 /* If we are doing a final link and the last .opd entry is just 16 byte
7235 long, add a 8 byte padding after it. */
0e1862bb 7236 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7237 {
7238 bfd_byte *p;
7239
7240 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7241 {
7242 BFD_ASSERT (need_pad->size > 0);
7243
7244 p = bfd_malloc (need_pad->size + 8);
7245 if (p == NULL)
7246 return FALSE;
699733f6 7247
2cdcc330
AM
7248 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7249 p, 0, need_pad->size))
3f764659
JJ
7250 return FALSE;
7251
7252 need_pad->contents = p;
7253 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7254 }
7255 else
7256 {
7257 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7258 if (p == NULL)
7259 return FALSE;
7260
7261 need_pad->contents = p;
7262 }
7263
7264 memset (need_pad->contents + need_pad->size, 0, 8);
7265 need_pad->size += 8;
7266 }
7267
411e1bfb
AM
7268 return TRUE;
7269}
7270
3e04d765
AM
7271/* Analyze inline PLT call relocations to see whether calls to locally
7272 defined functions can be converted to direct calls. */
7273
7274bfd_boolean
7275ppc64_elf_inline_plt (struct bfd_link_info *info)
7276{
7277 struct ppc_link_hash_table *htab;
7278 bfd *ibfd;
7279 asection *sec;
7280 bfd_vma low_vma, high_vma, limit;
7281
7282 htab = ppc_hash_table (info);
7283 if (htab == NULL)
7284 return FALSE;
7285
7286 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7287 reduced somewhat to cater for possible stubs that might be added
7288 between the call and its destination. */
7289 if (htab->params->group_size < 0)
7290 {
7291 limit = -htab->params->group_size;
7292 if (limit == 1)
7293 limit = 0x1e00000;
7294 }
7295 else
7296 {
7297 limit = htab->params->group_size;
7298 if (limit == 1)
7299 limit = 0x1c00000;
7300 }
7301
7302 low_vma = -1;
7303 high_vma = 0;
7304 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7305 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7306 {
7307 if (low_vma > sec->vma)
7308 low_vma = sec->vma;
7309 if (high_vma < sec->vma + sec->size)
7310 high_vma = sec->vma + sec->size;
7311 }
7312
7313 /* If a "bl" can reach anywhere in local code sections, then we can
7314 convert all inline PLT sequences to direct calls when the symbol
7315 is local. */
7316 if (high_vma - low_vma < limit)
7317 {
7318 htab->can_convert_all_inline_plt = 1;
7319 return TRUE;
7320 }
7321
7322 /* Otherwise, go looking through relocs for cases where a direct
7323 call won't reach. Mark the symbol on any such reloc to disable
7324 the optimization and keep the PLT entry as it seems likely that
7325 this will be better than creating trampolines. Note that this
7326 will disable the optimization for all inline PLT calls to a
7327 particular symbol, not just those that won't reach. The
7328 difficulty in doing a more precise optimization is that the
7329 linker needs to make a decision depending on whether a
7330 particular R_PPC64_PLTCALL insn can be turned into a direct
7331 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7332 the sequence, and there is nothing that ties those relocs
7333 together except their symbol. */
7334
7335 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7336 {
7337 Elf_Internal_Shdr *symtab_hdr;
7338 Elf_Internal_Sym *local_syms;
7339
7340 if (!is_ppc64_elf (ibfd))
7341 continue;
7342
7343 local_syms = NULL;
7344 symtab_hdr = &elf_symtab_hdr (ibfd);
7345
7346 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7347 if (ppc64_elf_section_data (sec)->has_pltcall
7348 && !bfd_is_abs_section (sec->output_section))
7349 {
7350 Elf_Internal_Rela *relstart, *rel, *relend;
7351
7352 /* Read the relocations. */
7353 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7354 info->keep_memory);
7355 if (relstart == NULL)
7356 return FALSE;
7357
7358 relend = relstart + sec->reloc_count;
7359 for (rel = relstart; rel < relend; )
7360 {
7361 enum elf_ppc64_reloc_type r_type;
7362 unsigned long r_symndx;
7363 asection *sym_sec;
7364 struct elf_link_hash_entry *h;
7365 Elf_Internal_Sym *sym;
7366 unsigned char *tls_maskp;
7367
7368 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7369 if (r_type != R_PPC64_PLTCALL
7370 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7371 continue;
7372
7373 r_symndx = ELF64_R_SYM (rel->r_info);
7374 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7375 r_symndx, ibfd))
7376 {
7377 if (elf_section_data (sec)->relocs != relstart)
7378 free (relstart);
7379 if (local_syms != NULL
2cdcc330 7380 && symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765
AM
7381 free (local_syms);
7382 return FALSE;
7383 }
7384
7385 if (sym_sec != NULL && sym_sec->output_section != NULL)
7386 {
7387 bfd_vma from, to;
7388 if (h != NULL)
7389 to = h->root.u.def.value;
7390 else
7391 to = sym->st_value;
7392 to += (rel->r_addend
7393 + sym_sec->output_offset
7394 + sym_sec->output_section->vma);
7395 from = (rel->r_offset
7396 + sec->output_offset
7397 + sec->output_section->vma);
5663e321
AM
7398 if (to - from + limit < 2 * limit
7399 && !(r_type == R_PPC64_PLTCALL_NOTOC
7400 && (((h ? h->other : sym->st_other)
7401 & STO_PPC64_LOCAL_MASK)
7402 != 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7403 *tls_maskp &= ~PLT_KEEP;
7404 }
7405 }
7406 if (elf_section_data (sec)->relocs != relstart)
7407 free (relstart);
7408 }
7409
7410 if (local_syms != NULL
7411 && symtab_hdr->contents != (unsigned char *) local_syms)
7412 {
7413 if (!info->keep_memory)
7414 free (local_syms);
7415 else
7416 symtab_hdr->contents = (unsigned char *) local_syms;
7417 }
7418 }
7419
7420 return TRUE;
7421}
7422
e1918d23 7423/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7424
e1918d23 7425asection *
e7d1c40c 7426ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7427{
411e1bfb
AM
7428 struct ppc_link_hash_table *htab;
7429
411e1bfb 7430 htab = ppc_hash_table (info);
4dfe6ac6
NC
7431 if (htab == NULL)
7432 return NULL;
7433
ee67d69a
AM
7434 if (abiversion (info->output_bfd) == 1)
7435 htab->opd_abi = 1;
7436
e7d1c40c 7437 if (htab->params->no_multi_toc)
33c0ec9d
AM
7438 htab->do_multi_toc = 0;
7439 else if (!htab->do_multi_toc)
e7d1c40c 7440 htab->params->no_multi_toc = 1;
33c0ec9d 7441
8b5f1ed8
AM
7442 /* Default to --no-plt-localentry, as this option can cause problems
7443 with symbol interposition. For example, glibc libpthread.so and
7444 libc.so duplicate many pthread symbols, with a fallback
7445 implementation in libc.so. In some cases the fallback does more
7446 work than the pthread implementation. __pthread_condattr_destroy
7447 is one such symbol: the libpthread.so implementation is
7448 localentry:0 while the libc.so implementation is localentry:8.
7449 An app that "cleverly" uses dlopen to only load necessary
7450 libraries at runtime may omit loading libpthread.so when not
7451 running multi-threaded, which then results in the libc.so
7452 fallback symbols being used and ld.so complaining. Now there
7453 are workarounds in ld (see non_zero_localentry) to detect the
7454 pthread situation, but that may not be the only case where
7455 --plt-localentry can cause trouble. */
f378ab09 7456 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7457 htab->params->plt_localentry0 = 0;
d44c746a
AM
7458 if (htab->params->plt_localentry0
7459 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7460 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
7461 _bfd_error_handler
7462 (_("warning: --plt-localentry is especially dangerous without "
7463 "ld.so support to detect ABI violations"));
f378ab09 7464
3a71aa26
AM
7465 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7466 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7467 FALSE, FALSE, TRUE));
a7f2871e
AM
7468 /* Move dynamic linking info to the function descriptor sym. */
7469 if (htab->tls_get_addr != NULL)
7470 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7471 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7472 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7473 FALSE, FALSE, TRUE));
7c9cf415 7474 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
7475 {
7476 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7477
7478 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7479 FALSE, FALSE, TRUE);
7480 if (opt != NULL)
7481 func_desc_adjust (opt, info);
7482 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7483 FALSE, FALSE, TRUE);
7484 if (opt_fd != NULL
7485 && (opt_fd->root.type == bfd_link_hash_defined
7486 || opt_fd->root.type == bfd_link_hash_defweak))
7487 {
7488 /* If glibc supports an optimized __tls_get_addr call stub,
7489 signalled by the presence of __tls_get_addr_opt, and we'll
7490 be calling __tls_get_addr via a plt call stub, then
7491 make __tls_get_addr point to __tls_get_addr_opt. */
7492 tga_fd = &htab->tls_get_addr_fd->elf;
7493 if (htab->elf.dynamic_sections_created
7494 && tga_fd != NULL
7495 && (tga_fd->type == STT_FUNC
7496 || tga_fd->needs_plt)
7497 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 7498 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
7499 {
7500 struct plt_entry *ent;
7501
7502 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7503 if (ent->plt.refcount > 0)
7504 break;
7505 if (ent != NULL)
7506 {
7507 tga_fd->root.type = bfd_link_hash_indirect;
7508 tga_fd->root.u.i.link = &opt_fd->root;
7509 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 7510 opt_fd->mark = 1;
a7f2871e
AM
7511 if (opt_fd->dynindx != -1)
7512 {
7513 /* Use __tls_get_addr_opt in dynamic relocations. */
7514 opt_fd->dynindx = -1;
7515 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7516 opt_fd->dynstr_index);
7517 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7518 return NULL;
a7f2871e 7519 }
2cdcc330
AM
7520 htab->tls_get_addr_fd
7521 = (struct ppc_link_hash_entry *) opt_fd;
a7f2871e
AM
7522 tga = &htab->tls_get_addr->elf;
7523 if (opt != NULL && tga != NULL)
7524 {
7525 tga->root.type = bfd_link_hash_indirect;
7526 tga->root.u.i.link = &opt->root;
7527 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 7528 opt->mark = 1;
a7f2871e
AM
7529 _bfd_elf_link_hash_hide_symbol (info, opt,
7530 tga->forced_local);
7531 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7532 }
7533 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7534 htab->tls_get_addr_fd->is_func_descriptor = 1;
7535 if (htab->tls_get_addr != NULL)
7536 {
7537 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7538 htab->tls_get_addr->is_func = 1;
7539 }
7540 }
7541 }
7542 }
7c9cf415
AM
7543 else if (htab->params->tls_get_addr_opt < 0)
7544 htab->params->tls_get_addr_opt = 0;
a7f2871e 7545 }
33c0ec9d 7546 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7547}
8387904d 7548
3a71aa26
AM
7549/* Return TRUE iff REL is a branch reloc with a global symbol matching
7550 HASH1 or HASH2. */
8387904d 7551
3a71aa26
AM
7552static bfd_boolean
7553branch_reloc_hash_match (const bfd *ibfd,
7554 const Elf_Internal_Rela *rel,
7555 const struct ppc_link_hash_entry *hash1,
7556 const struct ppc_link_hash_entry *hash2)
7557{
7558 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7559 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7560 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7561
e054468f 7562 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7563 {
3a71aa26
AM
7564 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7565 struct elf_link_hash_entry *h;
8387904d 7566
3a71aa26 7567 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7568 h = elf_follow_link (h);
3a71aa26
AM
7569 if (h == &hash1->elf || h == &hash2->elf)
7570 return TRUE;
a48ebf4d 7571 }
3a71aa26 7572 return FALSE;
951fd09b 7573}
411e1bfb 7574
951fd09b
AM
7575/* Run through all the TLS relocs looking for optimization
7576 opportunities. The linker has been hacked (see ppc64elf.em) to do
7577 a preliminary section layout so that we know the TLS segment
7578 offsets. We can't optimize earlier because some optimizations need
7579 to know the tp offset, and we need to optimize before allocating
7580 dynamic relocations. */
7581
7582bfd_boolean
33c0ec9d 7583ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7584{
7585 bfd *ibfd;
7586 asection *sec;
7587 struct ppc_link_hash_table *htab;
663a1470 7588 unsigned char *toc_ref;
102890f0 7589 int pass;
951fd09b 7590
3cbc1e5e 7591 if (!bfd_link_executable (info))
411e1bfb
AM
7592 return TRUE;
7593
951fd09b 7594 htab = ppc_hash_table (info);
4dfe6ac6
NC
7595 if (htab == NULL)
7596 return FALSE;
7597
663a1470
AM
7598 /* Make two passes over the relocs. On the first pass, mark toc
7599 entries involved with tls relocs, and check that tls relocs
7600 involved in setting up a tls_get_addr call are indeed followed by
7601 such a call. If they are not, we can't do any tls optimization.
7602 On the second pass twiddle tls_mask flags to notify
7603 relocate_section that optimization can be done, and adjust got
7604 and plt refcounts. */
7605 toc_ref = NULL;
7606 for (pass = 0; pass < 2; ++pass)
c72f2fb2 7607 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
7608 {
7609 Elf_Internal_Sym *locsyms = NULL;
7610 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7611
102890f0
AM
7612 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7613 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7614 {
7615 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7616 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7617
102890f0
AM
7618 /* Read the relocations. */
7619 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7620 info->keep_memory);
7621 if (relstart == NULL)
2915c55b
JK
7622 {
7623 free (toc_ref);
7624 return FALSE;
7625 }
411e1bfb 7626
102890f0
AM
7627 relend = relstart + sec->reloc_count;
7628 for (rel = relstart; rel < relend; rel++)
7629 {
7630 enum elf_ppc64_reloc_type r_type;
7631 unsigned long r_symndx;
7632 struct elf_link_hash_entry *h;
7633 Elf_Internal_Sym *sym;
7634 asection *sym_sec;
f961d9dd
AM
7635 unsigned char *tls_mask;
7636 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
7637 bfd_vma value;
7638 bfd_boolean ok_tprel, is_local;
7639 long toc_ref_index = 0;
7640 int expecting_tls_get_addr = 0;
663a1470 7641 bfd_boolean ret = FALSE;
411e1bfb 7642
102890f0
AM
7643 r_symndx = ELF64_R_SYM (rel->r_info);
7644 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7645 r_symndx, ibfd))
7646 {
7647 err_free_rel:
7648 if (elf_section_data (sec)->relocs != relstart)
7649 free (relstart);
7650 if (toc_ref != NULL)
7651 free (toc_ref);
7652 if (locsyms != NULL
0ffa91dd 7653 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7654 != (unsigned char *) locsyms))
7655 free (locsyms);
663a1470 7656 return ret;
102890f0 7657 }
411e1bfb 7658
102890f0
AM
7659 if (h != NULL)
7660 {
766bc656
AM
7661 if (h->root.type == bfd_link_hash_defined
7662 || h->root.type == bfd_link_hash_defweak)
7663 value = h->root.u.def.value;
7664 else if (h->root.type == bfd_link_hash_undefweak)
7665 value = 0;
7666 else
663a1470
AM
7667 {
7668 found_tls_get_addr_arg = 0;
7669 continue;
7670 }
102890f0
AM
7671 }
7672 else
7673 /* Symbols referenced by TLS relocs must be of type
7674 STT_TLS. So no need for .opd local sym adjust. */
7675 value = sym->st_value;
7676
7677 ok_tprel = FALSE;
7678 is_local = FALSE;
7679 if (h == NULL
7680 || !h->def_dynamic)
7681 {
7682 is_local = TRUE;
766bc656
AM
7683 if (h != NULL
7684 && h->root.type == bfd_link_hash_undefweak)
7685 ok_tprel = TRUE;
c27b8c2a
AM
7686 else if (sym_sec != NULL
7687 && sym_sec->output_section != NULL)
766bc656
AM
7688 {
7689 value += sym_sec->output_offset;
7690 value += sym_sec->output_section->vma;
7691 value -= htab->elf.tls_sec->vma;
7692 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7693 < (bfd_vma) 1 << 32);
7694 }
102890f0 7695 }
951fd09b 7696
102890f0 7697 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7698 /* If this section has old-style __tls_get_addr calls
7699 without marker relocs, then check that each
7700 __tls_get_addr call reloc is preceded by a reloc
7701 that conceivably belongs to the __tls_get_addr arg
7702 setup insn. If we don't find matching arg setup
7703 relocs, don't do any tls optimization. */
7704 if (pass == 0
7705 && sec->has_tls_get_addr_call
7706 && h != NULL
7707 && (h == &htab->tls_get_addr->elf
7708 || h == &htab->tls_get_addr_fd->elf)
7709 && !found_tls_get_addr_arg
7710 && is_branch_reloc (r_type))
7711 {
25f53a85 7712 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
7713 "TLS optimization disabled\n"),
7714 ibfd, sec, rel->r_offset);
7715 ret = TRUE;
7716 goto err_free_rel;
7717 }
7718
7719 found_tls_get_addr_arg = 0;
102890f0
AM
7720 switch (r_type)
7721 {
7722 case R_PPC64_GOT_TLSLD16:
7723 case R_PPC64_GOT_TLSLD16_LO:
7724 expecting_tls_get_addr = 1;
663a1470 7725 found_tls_get_addr_arg = 1;
1a0670f3 7726 /* Fall through. */
102890f0
AM
7727
7728 case R_PPC64_GOT_TLSLD16_HI:
7729 case R_PPC64_GOT_TLSLD16_HA:
7730 /* These relocs should never be against a symbol
7731 defined in a shared lib. Leave them alone if
7732 that turns out to be the case. */
7733 if (!is_local)
7734 continue;
411e1bfb 7735
102890f0 7736 /* LD -> LE */
411e1bfb 7737 tls_set = 0;
102890f0
AM
7738 tls_clear = TLS_LD;
7739 tls_type = TLS_TLS | TLS_LD;
7740 break;
411e1bfb 7741
102890f0
AM
7742 case R_PPC64_GOT_TLSGD16:
7743 case R_PPC64_GOT_TLSGD16_LO:
7744 expecting_tls_get_addr = 1;
663a1470 7745 found_tls_get_addr_arg = 1;
1a0670f3 7746 /* Fall through. */
102890f0
AM
7747
7748 case R_PPC64_GOT_TLSGD16_HI:
7749 case R_PPC64_GOT_TLSGD16_HA:
7750 if (ok_tprel)
7751 /* GD -> LE */
411e1bfb 7752 tls_set = 0;
102890f0
AM
7753 else
7754 /* GD -> IE */
7755 tls_set = TLS_TLS | TLS_TPRELGD;
7756 tls_clear = TLS_GD;
7757 tls_type = TLS_TLS | TLS_GD;
7758 break;
7759
7760 case R_PPC64_GOT_TPREL16_DS:
7761 case R_PPC64_GOT_TPREL16_LO_DS:
7762 case R_PPC64_GOT_TPREL16_HI:
7763 case R_PPC64_GOT_TPREL16_HA:
7764 if (ok_tprel)
7765 {
7766 /* IE -> LE */
7767 tls_set = 0;
7768 tls_clear = TLS_TPREL;
7769 tls_type = TLS_TLS | TLS_TPREL;
7770 break;
7771 }
411e1bfb
AM
7772 continue;
7773
727fc41e
AM
7774 case R_PPC64_TLSGD:
7775 case R_PPC64_TLSLD:
23cedd1d
AM
7776 if (rel + 1 < relend
7777 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7778 {
7779 if (pass != 0
2cdcc330 7780 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
7781 != R_PPC64_PLTSEQ)
7782 && (ELF64_R_TYPE (rel[1].r_info)
7783 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
7784 {
7785 r_symndx = ELF64_R_SYM (rel[1].r_info);
7786 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 7787 r_symndx, ibfd))
23cedd1d
AM
7788 goto err_free_rel;
7789 if (h != NULL)
7790 {
7791 struct plt_entry *ent = NULL;
7792
7793 for (ent = h->plt.plist;
7794 ent != NULL;
7795 ent = ent->next)
7796 if (ent->addend == rel[1].r_addend)
7797 break;
7798
7799 if (ent != NULL
7800 && ent->plt.refcount > 0)
7801 ent->plt.refcount -= 1;
7802 }
7803 }
7804 continue;
7805 }
663a1470 7806 found_tls_get_addr_arg = 1;
1a0670f3 7807 /* Fall through. */
663a1470
AM
7808
7809 case R_PPC64_TLS:
7810 case R_PPC64_TOC16:
7811 case R_PPC64_TOC16_LO:
102890f0
AM
7812 if (sym_sec == NULL || sym_sec != toc)
7813 continue;
7814
7815 /* Mark this toc entry as referenced by a TLS
7816 code sequence. We can do that now in the
7817 case of R_PPC64_TLS, and after checking for
7818 tls_get_addr for the TOC16 relocs. */
7819 if (toc_ref == NULL)
2cdcc330
AM
7820 toc_ref
7821 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
7822 if (toc_ref == NULL)
7823 goto err_free_rel;
7824
102890f0
AM
7825 if (h != NULL)
7826 value = h->root.u.def.value;
7827 else
7828 value = sym->st_value;
7829 value += rel->r_addend;
73242275
AM
7830 if (value % 8 != 0)
7831 continue;
7832 BFD_ASSERT (value < toc->size
7833 && toc->output_offset % 8 == 0);
663a1470 7834 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7835 if (r_type == R_PPC64_TLS
7836 || r_type == R_PPC64_TLSGD
7837 || r_type == R_PPC64_TLSLD)
102890f0
AM
7838 {
7839 toc_ref[toc_ref_index] = 1;
7840 continue;
7841 }
7842
7843 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7844 continue;
7845
7846 tls_set = 0;
7847 tls_clear = 0;
7848 expecting_tls_get_addr = 2;
7849 break;
7850
7851 case R_PPC64_TPREL64:
7852 if (pass == 0
7853 || sec != toc
7854 || toc_ref == NULL
663a1470 7855 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7856 continue;
7857 if (ok_tprel)
7858 {
7859 /* IE -> LE */
7860 tls_set = TLS_EXPLICIT;
7861 tls_clear = TLS_TPREL;
7862 break;
7863 }
7864 continue;
7865
7866 case R_PPC64_DTPMOD64:
7867 if (pass == 0
7868 || sec != toc
7869 || toc_ref == NULL
663a1470 7870 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7871 continue;
7872 if (rel + 1 < relend
7873 && (rel[1].r_info
7874 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7875 && rel[1].r_offset == rel->r_offset + 8)
7876 {
7877 if (ok_tprel)
7878 /* GD -> LE */
7879 tls_set = TLS_EXPLICIT | TLS_GD;
7880 else
7881 /* GD -> IE */
7882 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7883 tls_clear = TLS_GD;
7884 }
7885 else
7886 {
7887 if (!is_local)
7888 continue;
7889
7890 /* LD -> LE */
7891 tls_set = TLS_EXPLICIT;
7892 tls_clear = TLS_LD;
7893 }
7894 break;
7895
7896 default:
7897 continue;
7898 }
7899
7900 if (pass == 0)
7901 {
727fc41e
AM
7902 if (!expecting_tls_get_addr
7903 || !sec->has_tls_get_addr_call)
102890f0
AM
7904 continue;
7905
3a71aa26
AM
7906 if (rel + 1 < relend
7907 && branch_reloc_hash_match (ibfd, rel + 1,
7908 htab->tls_get_addr,
7909 htab->tls_get_addr_fd))
102890f0 7910 {
3a71aa26 7911 if (expecting_tls_get_addr == 2)
102890f0 7912 {
3a71aa26 7913 /* Check for toc tls entries. */
f961d9dd 7914 unsigned char *toc_tls;
3a71aa26
AM
7915 int retval;
7916
7917 retval = get_tls_mask (&toc_tls, NULL, NULL,
7918 &locsyms,
7919 rel, ibfd);
7920 if (retval == 0)
7921 goto err_free_rel;
663a1470
AM
7922 if (toc_tls != NULL)
7923 {
37da22e5
AM
7924 if ((*toc_tls & TLS_TLS) != 0
7925 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
7926 found_tls_get_addr_arg = 1;
7927 if (retval > 1)
7928 toc_ref[toc_ref_index] = 1;
7929 }
102890f0 7930 }
3a71aa26 7931 continue;
102890f0
AM
7932 }
7933
102890f0
AM
7934 /* Uh oh, we didn't find the expected call. We
7935 could just mark this symbol to exclude it
7936 from tls optimization but it's safer to skip
663a1470 7937 the entire optimization. */
695344c0 7938 /* xgettext:c-format */
25f53a85 7939 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
7940 "TLS optimization disabled\n"),
7941 ibfd, sec, rel->r_offset);
7942 ret = TRUE;
7943 goto err_free_rel;
102890f0
AM
7944 }
7945
37da22e5
AM
7946 /* If we don't have old-style __tls_get_addr calls
7947 without TLSGD/TLSLD marker relocs, and we haven't
7948 found a new-style __tls_get_addr call with a
7949 marker for this symbol, then we either have a
7950 broken object file or an -mlongcall style
7951 indirect call to __tls_get_addr without a marker.
7952 Disable optimization in this case. */
7953 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
7954 && (tls_set & TLS_EXPLICIT) == 0
7955 && !sec->has_tls_get_addr_call
7956 && ((*tls_mask & (TLS_TLS | TLS_MARK))
7957 != (TLS_TLS | TLS_MARK)))
7958 continue;
7959
23cedd1d 7960 if (expecting_tls_get_addr)
102890f0 7961 {
23cedd1d
AM
7962 struct plt_entry *ent = NULL;
7963
7964 if (htab->tls_get_addr != NULL)
7965 for (ent = htab->tls_get_addr->elf.plt.plist;
7966 ent != NULL;
7967 ent = ent->next)
7968 if (ent->addend == 0)
102890f0 7969 break;
411e1bfb 7970
23cedd1d
AM
7971 if (ent == NULL && htab->tls_get_addr_fd != NULL)
7972 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7973 ent != NULL;
7974 ent = ent->next)
7975 if (ent->addend == 0)
102890f0 7976 break;
23cedd1d
AM
7977
7978 if (ent != NULL
7979 && ent->plt.refcount > 0)
7980 ent->plt.refcount -= 1;
102890f0 7981 }
411e1bfb 7982
102890f0 7983 if (tls_clear == 0)
30038c59
AM
7984 continue;
7985
102890f0
AM
7986 if ((tls_set & TLS_EXPLICIT) == 0)
7987 {
7988 struct got_entry *ent;
411e1bfb 7989
102890f0
AM
7990 /* Adjust got entry for this reloc. */
7991 if (h != NULL)
7992 ent = h->got.glist;
7993 else
7994 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 7995
102890f0
AM
7996 for (; ent != NULL; ent = ent->next)
7997 if (ent->addend == rel->r_addend
7998 && ent->owner == ibfd
7999 && ent->tls_type == tls_type)
8000 break;
8001 if (ent == NULL)
8002 abort ();
411e1bfb 8003
102890f0
AM
8004 if (tls_set == 0)
8005 {
8006 /* We managed to get rid of a got entry. */
8007 if (ent->got.refcount > 0)
8008 ent->got.refcount -= 1;
8009 }
8010 }
8011 else
8012 {
8013 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8014 we'll lose one or two dyn relocs. */
8015 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8016 NULL, h, sym))
102890f0 8017 return FALSE;
411e1bfb 8018
102890f0
AM
8019 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8020 {
8021 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8022 NULL, h, sym))
102890f0
AM
8023 return FALSE;
8024 }
8025 }
411e1bfb 8026
102890f0
AM
8027 *tls_mask |= tls_set;
8028 *tls_mask &= ~tls_clear;
8029 }
8c1d1bb8 8030
102890f0
AM
8031 if (elf_section_data (sec)->relocs != relstart)
8032 free (relstart);
8033 }
411e1bfb 8034
663a1470
AM
8035 if (locsyms != NULL
8036 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8037 {
8038 if (!info->keep_memory)
8039 free (locsyms);
8040 else
8041 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8042 }
8043 }
411e1bfb 8044
663a1470
AM
8045 if (toc_ref != NULL)
8046 free (toc_ref);
9a23f96e 8047 htab->do_tls_opt = 1;
b34976b6 8048 return TRUE;
1e2f5b6e 8049}
b34976b6 8050
c5614fa4
AM
8051/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8052 the values of any global symbols in a toc section that has been
8053 edited. Globals in toc sections should be a rarity, so this function
8054 sets a flag if any are found in toc sections other than the one just
de194d85 8055 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8056
8057struct adjust_toc_info
8058{
8059 asection *toc;
8060 unsigned long *skip;
8061 bfd_boolean global_toc_syms;
8062};
8063
ba761f19
AM
8064enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8065
c5614fa4
AM
8066static bfd_boolean
8067adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8068{
8069 struct ppc_link_hash_entry *eh;
8070 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8071 unsigned long i;
c5614fa4 8072
c5614fa4
AM
8073 if (h->root.type != bfd_link_hash_defined
8074 && h->root.type != bfd_link_hash_defweak)
8075 return TRUE;
8076
8077 eh = (struct ppc_link_hash_entry *) h;
8078 if (eh->adjust_done)
8079 return TRUE;
8080
8081 if (eh->elf.root.u.def.section == toc_inf->toc)
8082 {
854b41e7
AM
8083 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8084 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8085 else
854b41e7
AM
8086 i = eh->elf.root.u.def.value >> 3;
8087
ba761f19 8088 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8089 {
4eca0228 8090 _bfd_error_handler
854b41e7
AM
8091 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8092 do
8093 ++i;
ba761f19 8094 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8095 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8096 }
854b41e7
AM
8097
8098 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8099 eh->adjust_done = 1;
8100 }
8101 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8102 toc_inf->global_toc_syms = TRUE;
8103
8104 return TRUE;
8105}
8106
39eeab25
AM
8107/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8108 on a _LO variety toc/got reloc. */
560c8763
AM
8109
8110static bfd_boolean
39eeab25 8111ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8112{
39eeab25
AM
8113 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8114 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
8115 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8116 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8117 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8118 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8119 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8120 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8121 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8122 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8123 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8124 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8125 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8126 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8127 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
8128 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8129 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8130 /* Exclude lfqu by testing reloc. If relocs are ever
8131 defined for the reduced D field in psq_lu then those
8132 will need testing too. */
8133 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8134 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8135 && (insn & 1) == 0)
8136 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8137 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8138 /* Exclude stfqu. psq_stu as above for psq_lu. */
8139 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8140 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8141 && (insn & 1) == 0));
560c8763
AM
8142}
8143
4a421c53
AM
8144/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8145 pld ra,symbol@got@pcrel
8146 load/store rt,0(ra)
8147 or
8148 paddi ra,symbol@pcrel
8149 load/store rt,0(ra)
8150 may be translated to
8151 pload/pstore rt,symbol@pcrel
8152 nop.
8153 This function returns true if the optimization is possible, placing
8154 the prefix insn in *PINSN1 and a NOP in *PINSN2.
8155
8156 On entry to this function, the linker has already determined that
8157 the pld can be replaced with paddi: *PINSN1 is that paddi insn,
8158 while *PINSN2 is the second instruction. */
8159
8160static bfd_boolean
8161xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2)
8162{
8163 uint32_t insn2 = *pinsn2 >> 32;
8164 uint64_t i1new;
8165
8166 /* Check that regs match. */
8167 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
8168 return FALSE;
8169
8170 switch ((insn2 >> 26) & 63)
8171 {
8172 default:
8173 return FALSE;
8174
8175 case 32: /* lwz */
8176 case 34: /* lbz */
8177 case 36: /* stw */
8178 case 38: /* stb */
8179 case 40: /* lhz */
8180 case 42: /* lha */
8181 case 44: /* sth */
8182 case 48: /* lfs */
8183 case 50: /* lfd */
8184 case 52: /* stfs */
8185 case 54: /* stfd */
8186 /* These are the PMLS cases, where we just need to tack a prefix
8187 on the insn. Check that the D field is zero. */
8188 if ((insn2 & 0xffff) != 0)
8189 return FALSE;
8190 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8191 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8192 break;
8193
8194 case 58: /* lwa, ld */
8195 if ((insn2 & 0xfffd) != 0)
8196 return FALSE;
8197 i1new = ((1ULL << 58) | (1ULL << 52)
8198 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8199 | (insn2 & (31ULL << 21)));
8200 break;
8201
8202 case 57: /* lxsd, lxssp */
8203 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
8204 return FALSE;
8205 i1new = ((1ULL << 58) | (1ULL << 52)
8206 | ((40ULL | (insn2 & 3)) << 26)
8207 | (insn2 & (31ULL << 21)));
8208 break;
8209
8210 case 61: /* stxsd, stxssp, lxv, stxv */
8211 if ((insn2 & 3) == 0)
8212 return FALSE;
8213 else if ((insn2 & 3) >= 2)
8214 {
8215 if ((insn2 & 0xfffc) != 0)
8216 return FALSE;
8217 i1new = ((1ULL << 58) | (1ULL << 52)
8218 | ((44ULL | (insn2 & 3)) << 26)
8219 | (insn2 & (31ULL << 21)));
8220 }
8221 else
8222 {
8223 if ((insn2 & 0xfff0) != 0)
8224 return FALSE;
8225 i1new = ((1ULL << 58) | (1ULL << 52)
8226 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8227 | (insn2 & (31ULL << 21)));
8228 }
8229 break;
8230
8231 case 56: /* lq */
8232 if ((insn2 & 0xffff) != 0)
8233 return FALSE;
8234 i1new = ((1ULL << 58) | (1ULL << 52)
8235 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8236 break;
8237
8238 case 62: /* std, stq */
8239 if ((insn2 & 0xfffd) != 0)
8240 return FALSE;
8241 i1new = ((1ULL << 58) | (1ULL << 52)
8242 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8243 | (insn2 & (31ULL << 21)));
8244 break;
8245 }
8246
8247 *pinsn1 = i1new;
8248 *pinsn2 = (uint64_t) NOP << 32;
8249 return TRUE;
8250}
8251
c5614fa4
AM
8252/* Examine all relocs referencing .toc sections in order to remove
8253 unused .toc entries. */
8254
8255bfd_boolean
33c0ec9d 8256ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8257{
8258 bfd *ibfd;
8259 struct adjust_toc_info toc_inf;
67f0cbdb 8260 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8261
67f0cbdb 8262 htab->do_toc_opt = 1;
c5614fa4 8263 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8264 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8265 {
8266 asection *toc, *sec;
8267 Elf_Internal_Shdr *symtab_hdr;
8268 Elf_Internal_Sym *local_syms;
425b145b 8269 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8270 unsigned long *skip, *drop;
8271 unsigned char *used;
8272 unsigned char *keep, last, some_unused;
8273
854b41e7
AM
8274 if (!is_ppc64_elf (ibfd))
8275 continue;
8276
c5614fa4
AM
8277 toc = bfd_get_section_by_name (ibfd, ".toc");
8278 if (toc == NULL
92b7a70f 8279 || toc->size == 0
dbaa2011
AM
8280 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8281 || discarded_section (toc))
c5614fa4
AM
8282 continue;
8283
425b145b 8284 toc_relocs = NULL;
c5614fa4 8285 local_syms = NULL;
0ffa91dd 8286 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8287
8288 /* Look at sections dropped from the final link. */
8289 skip = NULL;
8290 relstart = NULL;
8291 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8292 {
8293 if (sec->reloc_count == 0
dbaa2011 8294 || !discarded_section (sec)
c5614fa4
AM
8295 || get_opd_info (sec)
8296 || (sec->flags & SEC_ALLOC) == 0
8297 || (sec->flags & SEC_DEBUGGING) != 0)
8298 continue;
8299
8300 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8301 if (relstart == NULL)
8302 goto error_ret;
8303
8304 /* Run through the relocs to see which toc entries might be
8305 unused. */
8306 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8307 {
8308 enum elf_ppc64_reloc_type r_type;
8309 unsigned long r_symndx;
8310 asection *sym_sec;
8311 struct elf_link_hash_entry *h;
8312 Elf_Internal_Sym *sym;
8313 bfd_vma val;
8314
8315 r_type = ELF64_R_TYPE (rel->r_info);
8316 switch (r_type)
8317 {
8318 default:
8319 continue;
8320
8321 case R_PPC64_TOC16:
8322 case R_PPC64_TOC16_LO:
8323 case R_PPC64_TOC16_HI:
8324 case R_PPC64_TOC16_HA:
8325 case R_PPC64_TOC16_DS:
8326 case R_PPC64_TOC16_LO_DS:
8327 break;
8328 }
8329
8330 r_symndx = ELF64_R_SYM (rel->r_info);
8331 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8332 r_symndx, ibfd))
8333 goto error_ret;
8334
8335 if (sym_sec != toc)
8336 continue;
8337
8338 if (h != NULL)
8339 val = h->root.u.def.value;
8340 else
8341 val = sym->st_value;
8342 val += rel->r_addend;
8343
8344 if (val >= toc->size)
8345 continue;
8346
8347 /* Anything in the toc ought to be aligned to 8 bytes.
8348 If not, don't mark as unused. */
8349 if (val & 7)
8350 continue;
8351
8352 if (skip == NULL)
8353 {
854b41e7 8354 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8355 if (skip == NULL)
8356 goto error_ret;
8357 }
8358
ba761f19 8359 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8360 }
8361
8362 if (elf_section_data (sec)->relocs != relstart)
8363 free (relstart);
8364 }
8365
ba761f19
AM
8366 /* For largetoc loads of address constants, we can convert
8367 . addis rx,2,addr@got@ha
8368 . ld ry,addr@got@l(rx)
8369 to
8370 . addis rx,2,addr@toc@ha
8371 . addi ry,rx,addr@toc@l
8372 when addr is within 2G of the toc pointer. This then means
8373 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8374
ba761f19
AM
8375 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8376 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8377 && toc->reloc_count != 0)
8378 {
8379 /* Read toc relocs. */
425b145b
AM
8380 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8381 info->keep_memory);
8382 if (toc_relocs == NULL)
ba761f19
AM
8383 goto error_ret;
8384
425b145b 8385 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8386 {
8387 enum elf_ppc64_reloc_type r_type;
8388 unsigned long r_symndx;
8389 asection *sym_sec;
8390 struct elf_link_hash_entry *h;
8391 Elf_Internal_Sym *sym;
8392 bfd_vma val, addr;
8393
8394 r_type = ELF64_R_TYPE (rel->r_info);
8395 if (r_type != R_PPC64_ADDR64)
8396 continue;
8397
8398 r_symndx = ELF64_R_SYM (rel->r_info);
8399 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8400 r_symndx, ibfd))
8401 goto error_ret;
8402
425b145b 8403 if (sym_sec == NULL
c27b8c2a 8404 || sym_sec->output_section == NULL
dbaa2011 8405 || discarded_section (sym_sec))
425b145b
AM
8406 continue;
8407
afe397ea 8408 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8409 continue;
8410
8411 if (h != NULL)
bddc25c9
AM
8412 {
8413 if (h->type == STT_GNU_IFUNC)
8414 continue;
8415 val = h->root.u.def.value;
8416 }
ba761f19 8417 else
bddc25c9
AM
8418 {
8419 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8420 continue;
8421 val = sym->st_value;
8422 }
ba761f19
AM
8423 val += rel->r_addend;
8424 val += sym_sec->output_section->vma + sym_sec->output_offset;
8425
8426 /* We don't yet know the exact toc pointer value, but we
8427 know it will be somewhere in the toc section. Don't
8428 optimize if the difference from any possible toc
8429 pointer is outside [ff..f80008000, 7fff7fff]. */
8430 addr = toc->output_section->vma + TOC_BASE_OFF;
8431 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8432 continue;
8433
8434 addr = toc->output_section->vma + toc->output_section->rawsize;
8435 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8436 continue;
8437
8438 if (skip == NULL)
8439 {
8440 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8441 if (skip == NULL)
8442 goto error_ret;
8443 }
8444
8445 skip[rel->r_offset >> 3]
425b145b 8446 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8447 }
ba761f19
AM
8448 }
8449
c5614fa4
AM
8450 if (skip == NULL)
8451 continue;
8452
8453 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8454 if (used == NULL)
8455 {
8456 error_ret:
8457 if (local_syms != NULL
8458 && symtab_hdr->contents != (unsigned char *) local_syms)
8459 free (local_syms);
8460 if (sec != NULL
8461 && relstart != NULL
8462 && elf_section_data (sec)->relocs != relstart)
8463 free (relstart);
425b145b
AM
8464 if (toc_relocs != NULL
8465 && elf_section_data (toc)->relocs != toc_relocs)
8466 free (toc_relocs);
c5614fa4
AM
8467 if (skip != NULL)
8468 free (skip);
8469 return FALSE;
8470 }
8471
30038c59
AM
8472 /* Now check all kept sections that might reference the toc.
8473 Check the toc itself last. */
8474 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8475 : ibfd->sections);
c5614fa4 8476 sec != NULL;
c5614fa4 8477 sec = (sec == toc ? NULL
c5614fa4 8478 : sec->next == NULL ? toc
30038c59 8479 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8480 : sec->next))
8481 {
8482 int repeat;
8483
8484 if (sec->reloc_count == 0
dbaa2011 8485 || discarded_section (sec)
c5614fa4
AM
8486 || get_opd_info (sec)
8487 || (sec->flags & SEC_ALLOC) == 0
8488 || (sec->flags & SEC_DEBUGGING) != 0)
8489 continue;
8490
854b41e7
AM
8491 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8492 info->keep_memory);
c5614fa4 8493 if (relstart == NULL)
2915c55b
JK
8494 {
8495 free (used);
8496 goto error_ret;
8497 }
c5614fa4
AM
8498
8499 /* Mark toc entries referenced as used. */
c5614fa4 8500 do
d4f1ee75
AM
8501 {
8502 repeat = 0;
8503 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8504 {
8505 enum elf_ppc64_reloc_type r_type;
8506 unsigned long r_symndx;
8507 asection *sym_sec;
8508 struct elf_link_hash_entry *h;
8509 Elf_Internal_Sym *sym;
8510 bfd_vma val;
8511 enum {no_check, check_lo, check_ha} insn_check;
98528052 8512
d4f1ee75
AM
8513 r_type = ELF64_R_TYPE (rel->r_info);
8514 switch (r_type)
8515 {
8516 default:
8517 insn_check = no_check;
8518 break;
98528052 8519
d4f1ee75
AM
8520 case R_PPC64_GOT_TLSLD16_HA:
8521 case R_PPC64_GOT_TLSGD16_HA:
8522 case R_PPC64_GOT_TPREL16_HA:
8523 case R_PPC64_GOT_DTPREL16_HA:
8524 case R_PPC64_GOT16_HA:
8525 case R_PPC64_TOC16_HA:
8526 insn_check = check_ha;
8527 break;
98528052 8528
d4f1ee75
AM
8529 case R_PPC64_GOT_TLSLD16_LO:
8530 case R_PPC64_GOT_TLSGD16_LO:
8531 case R_PPC64_GOT_TPREL16_LO_DS:
8532 case R_PPC64_GOT_DTPREL16_LO_DS:
8533 case R_PPC64_GOT16_LO:
8534 case R_PPC64_GOT16_LO_DS:
8535 case R_PPC64_TOC16_LO:
8536 case R_PPC64_TOC16_LO_DS:
8537 insn_check = check_lo;
8538 break;
8539 }
560c8763 8540
d4f1ee75
AM
8541 if (insn_check != no_check)
8542 {
8543 bfd_vma off = rel->r_offset & ~3;
8544 unsigned char buf[4];
8545 unsigned int insn;
c5614fa4 8546
d4f1ee75
AM
8547 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8548 {
8549 free (used);
8550 goto error_ret;
8551 }
8552 insn = bfd_get_32 (ibfd, buf);
8553 if (insn_check == check_lo
39eeab25 8554 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
8555 : ((insn & ((0x3f << 26) | 0x1f << 16))
8556 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8557 {
8558 char str[12];
8559
8560 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8561 sprintf (str, "%#08x", insn);
8562 info->callbacks->einfo
695344c0 8563 /* xgettext:c-format */
174d0a74 8564 (_("%H: toc optimization is not supported for"
cf97bcb0 8565 " %s instruction\n"),
d4f1ee75
AM
8566 ibfd, sec, rel->r_offset & ~3, str);
8567 }
8568 }
c5614fa4 8569
d4f1ee75
AM
8570 switch (r_type)
8571 {
8572 case R_PPC64_TOC16:
8573 case R_PPC64_TOC16_LO:
8574 case R_PPC64_TOC16_HI:
8575 case R_PPC64_TOC16_HA:
8576 case R_PPC64_TOC16_DS:
8577 case R_PPC64_TOC16_LO_DS:
8578 /* In case we're taking addresses of toc entries. */
8579 case R_PPC64_ADDR64:
8580 break;
c5614fa4 8581
d4f1ee75
AM
8582 default:
8583 continue;
8584 }
c5614fa4 8585
d4f1ee75
AM
8586 r_symndx = ELF64_R_SYM (rel->r_info);
8587 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8588 r_symndx, ibfd))
8589 {
8590 free (used);
8591 goto error_ret;
8592 }
c5614fa4 8593
d4f1ee75
AM
8594 if (sym_sec != toc)
8595 continue;
c5614fa4 8596
d4f1ee75
AM
8597 if (h != NULL)
8598 val = h->root.u.def.value;
8599 else
8600 val = sym->st_value;
8601 val += rel->r_addend;
ba761f19 8602
d4f1ee75
AM
8603 if (val >= toc->size)
8604 continue;
ba761f19 8605
d4f1ee75
AM
8606 if ((skip[val >> 3] & can_optimize) != 0)
8607 {
8608 bfd_vma off;
8609 unsigned char opc;
8610
8611 switch (r_type)
8612 {
8613 case R_PPC64_TOC16_HA:
ba761f19 8614 break;
ba761f19 8615
d4f1ee75
AM
8616 case R_PPC64_TOC16_LO_DS:
8617 off = rel->r_offset;
8618 off += (bfd_big_endian (ibfd) ? -2 : 3);
8619 if (!bfd_get_section_contents (ibfd, sec, &opc,
8620 off, 1))
8621 {
8622 free (used);
8623 goto error_ret;
8624 }
8625 if ((opc & (0x3f << 2)) == (58u << 2))
8626 break;
1a0670f3 8627 /* Fall through. */
ba761f19 8628
d4f1ee75
AM
8629 default:
8630 /* Wrong sort of reloc, or not a ld. We may
8631 as well clear ref_from_discarded too. */
8632 skip[val >> 3] = 0;
8633 }
8634 }
8635
8636 if (sec != toc)
8637 used[val >> 3] = 1;
8638 /* For the toc section, we only mark as used if this
8639 entry itself isn't unused. */
8640 else if ((used[rel->r_offset >> 3]
8641 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8642 && !used[val >> 3])
8643 {
8644 /* Do all the relocs again, to catch reference
8645 chains. */
8646 repeat = 1;
8647 used[val >> 3] = 1;
8648 }
8649 }
8650 }
c5614fa4 8651 while (repeat);
854b41e7
AM
8652
8653 if (elf_section_data (sec)->relocs != relstart)
8654 free (relstart);
c5614fa4
AM
8655 }
8656
8657 /* Merge the used and skip arrays. Assume that TOC
8658 doublewords not appearing as either used or unused belong
de194d85 8659 to an entry more than one doubleword in size. */
c5614fa4
AM
8660 for (drop = skip, keep = used, last = 0, some_unused = 0;
8661 drop < skip + (toc->size + 7) / 8;
8662 ++drop, ++keep)
8663 {
8664 if (*keep)
8665 {
ba761f19
AM
8666 *drop &= ~ref_from_discarded;
8667 if ((*drop & can_optimize) != 0)
8668 some_unused = 1;
c5614fa4
AM
8669 last = 0;
8670 }
b140b010 8671 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8672 {
8673 some_unused = 1;
ba761f19 8674 last = ref_from_discarded;
c5614fa4
AM
8675 }
8676 else
8677 *drop = last;
8678 }
8679
8680 free (used);
8681
8682 if (some_unused)
8683 {
8684 bfd_byte *contents, *src;
8685 unsigned long off;
d62b3684 8686 Elf_Internal_Sym *sym;
ba761f19 8687 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8688
8689 /* Shuffle the toc contents, and at the same time convert the
8690 skip array from booleans into offsets. */
8691 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8692 goto error_ret;
8693
8694 elf_section_data (toc)->this_hdr.contents = contents;
8695
8696 for (src = contents, off = 0, drop = skip;
8697 src < contents + toc->size;
8698 src += 8, ++drop)
8699 {
ba761f19
AM
8700 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8701 off += 8;
c5614fa4
AM
8702 else if (off != 0)
8703 {
8704 *drop = off;
8705 memcpy (src - off, src, 8);
8706 }
8707 }
854b41e7 8708 *drop = off;
c5614fa4
AM
8709 toc->rawsize = toc->size;
8710 toc->size = src - contents - off;
8711
ba761f19
AM
8712 /* Adjust addends for relocs against the toc section sym,
8713 and optimize any accesses we can. */
c5614fa4
AM
8714 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8715 {
8716 if (sec->reloc_count == 0
dbaa2011 8717 || discarded_section (sec))
c5614fa4
AM
8718 continue;
8719
8720 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8721 info->keep_memory);
c5614fa4
AM
8722 if (relstart == NULL)
8723 goto error_ret;
8724
8725 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8726 {
8727 enum elf_ppc64_reloc_type r_type;
8728 unsigned long r_symndx;
8729 asection *sym_sec;
8730 struct elf_link_hash_entry *h;
854b41e7 8731 bfd_vma val;
c5614fa4
AM
8732
8733 r_type = ELF64_R_TYPE (rel->r_info);
8734 switch (r_type)
8735 {
8736 default:
8737 continue;
8738
8739 case R_PPC64_TOC16:
8740 case R_PPC64_TOC16_LO:
8741 case R_PPC64_TOC16_HI:
8742 case R_PPC64_TOC16_HA:
8743 case R_PPC64_TOC16_DS:
8744 case R_PPC64_TOC16_LO_DS:
8745 case R_PPC64_ADDR64:
8746 break;
8747 }
8748
8749 r_symndx = ELF64_R_SYM (rel->r_info);
8750 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8751 r_symndx, ibfd))
8752 goto error_ret;
8753
ba761f19 8754 if (sym_sec != toc)
c5614fa4
AM
8755 continue;
8756
ba761f19
AM
8757 if (h != NULL)
8758 val = h->root.u.def.value;
8759 else
8760 {
8761 val = sym->st_value;
8762 if (val != 0)
8763 local_toc_syms = TRUE;
8764 }
8765
8766 val += rel->r_addend;
854b41e7
AM
8767
8768 if (val > toc->rawsize)
8769 val = toc->rawsize;
ba761f19
AM
8770 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8771 continue;
8772 else if ((skip[val >> 3] & can_optimize) != 0)
8773 {
8774 Elf_Internal_Rela *tocrel
425b145b 8775 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8776 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8777
8778 switch (r_type)
8779 {
8780 case R_PPC64_TOC16_HA:
8781 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8782 break;
8783
8784 case R_PPC64_TOC16_LO_DS:
8785 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8786 break;
8787
8788 default:
28942f62
AM
8789 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8790 ppc_howto_init ();
b140b010 8791 info->callbacks->einfo
695344c0 8792 /* xgettext:c-format */
174d0a74 8793 (_("%H: %s references "
b140b010
AM
8794 "optimized away TOC entry\n"),
8795 ibfd, sec, rel->r_offset,
8796 ppc64_elf_howto_table[r_type]->name);
8797 bfd_set_error (bfd_error_bad_value);
8798 goto error_ret;
ba761f19
AM
8799 }
8800 rel->r_addend = tocrel->r_addend;
8801 elf_section_data (sec)->relocs = relstart;
8802 continue;
8803 }
8804
8805 if (h != NULL || sym->st_value != 0)
8806 continue;
854b41e7
AM
8807
8808 rel->r_addend -= skip[val >> 3];
8809 elf_section_data (sec)->relocs = relstart;
c5614fa4 8810 }
854b41e7
AM
8811
8812 if (elf_section_data (sec)->relocs != relstart)
8813 free (relstart);
c5614fa4
AM
8814 }
8815
8816 /* We shouldn't have local or global symbols defined in the TOC,
8817 but handle them anyway. */
df22d223
AM
8818 if (local_syms != NULL)
8819 for (sym = local_syms;
8820 sym < local_syms + symtab_hdr->sh_info;
8821 ++sym)
8822 if (sym->st_value != 0
8823 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8824 {
8825 unsigned long i;
854b41e7 8826
df22d223
AM
8827 if (sym->st_value > toc->rawsize)
8828 i = toc->rawsize >> 3;
8829 else
8830 i = sym->st_value >> 3;
854b41e7 8831
df22d223
AM
8832 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8833 {
8834 if (local_toc_syms)
4eca0228 8835 _bfd_error_handler
df22d223
AM
8836 (_("%s defined on removed toc entry"),
8837 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8838 do
8839 ++i;
8840 while ((skip[i] & (ref_from_discarded | can_optimize)));
8841 sym->st_value = (bfd_vma) i << 3;
8842 }
d62b3684 8843
df22d223
AM
8844 sym->st_value -= skip[i];
8845 symtab_hdr->contents = (unsigned char *) local_syms;
8846 }
c5614fa4 8847
854b41e7 8848 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8849 if (toc_inf.global_toc_syms)
8850 {
8851 toc_inf.toc = toc;
8852 toc_inf.skip = skip;
8853 toc_inf.global_toc_syms = FALSE;
8854 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8855 &toc_inf);
8856 }
854b41e7
AM
8857
8858 if (toc->reloc_count != 0)
8859 {
d4730f92 8860 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8861 Elf_Internal_Rela *wrel;
8862 bfd_size_type sz;
8863
854b41e7 8864 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8865 if (toc_relocs == NULL)
8866 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8867 info->keep_memory);
8868 if (toc_relocs == NULL)
8869 goto error_ret;
8870
425b145b
AM
8871 wrel = toc_relocs;
8872 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8873 if ((skip[rel->r_offset >> 3]
8874 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8875 {
8876 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8877 wrel->r_info = rel->r_info;
8878 wrel->r_addend = rel->r_addend;
8879 ++wrel;
8880 }
8881 else if (!dec_dynrel_count (rel->r_info, toc, info,
8882 &local_syms, NULL, NULL))
8883 goto error_ret;
8884
425b145b
AM
8885 elf_section_data (toc)->relocs = toc_relocs;
8886 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8887 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8888 sz = rel_hdr->sh_entsize;
8889 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8890 }
c5614fa4 8891 }
28be611c
AM
8892 else if (toc_relocs != NULL
8893 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8894 free (toc_relocs);
c5614fa4
AM
8895
8896 if (local_syms != NULL
8897 && symtab_hdr->contents != (unsigned char *) local_syms)
8898 {
8899 if (!info->keep_memory)
8900 free (local_syms);
8901 else
8902 symtab_hdr->contents = (unsigned char *) local_syms;
8903 }
8904 free (skip);
8905 }
8906
066f4018 8907 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
8908 a GOT relative or PC relative access, possibly reducing the
8909 number of GOT entries. */
066f4018
AM
8910 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8911 {
8912 asection *sec;
8913 Elf_Internal_Shdr *symtab_hdr;
8914 Elf_Internal_Sym *local_syms;
8915 Elf_Internal_Rela *relstart, *rel;
8916 bfd_vma got;
8917
8918 if (!is_ppc64_elf (ibfd))
8919 continue;
8920
8921 if (!ppc64_elf_tdata (ibfd)->has_gotrel)
8922 continue;
8923
8924 sec = ppc64_elf_tdata (ibfd)->got;
8925 got = sec->output_section->vma + sec->output_offset + 0x8000;
8926
8927 local_syms = NULL;
8928 symtab_hdr = &elf_symtab_hdr (ibfd);
8929
8930 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8931 {
8932 if (sec->reloc_count == 0
8933 || !ppc64_elf_section_data (sec)->has_gotrel
8934 || discarded_section (sec))
8935 continue;
8936
8937 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8938 info->keep_memory);
8939 if (relstart == NULL)
8940 {
8941 got_error_ret:
8942 if (local_syms != NULL
8943 && symtab_hdr->contents != (unsigned char *) local_syms)
8944 free (local_syms);
8945 if (sec != NULL
8946 && relstart != NULL
8947 && elf_section_data (sec)->relocs != relstart)
8948 free (relstart);
8949 return FALSE;
8950 }
8951
8952 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8953 {
8954 enum elf_ppc64_reloc_type r_type;
8955 unsigned long r_symndx;
8956 Elf_Internal_Sym *sym;
8957 asection *sym_sec;
8958 struct elf_link_hash_entry *h;
8959 struct got_entry *ent;
4a421c53
AM
8960 bfd_vma sym_addend, val, pc;
8961 unsigned char buf[8];
066f4018
AM
8962 unsigned int insn;
8963
8964 r_type = ELF64_R_TYPE (rel->r_info);
8965 switch (r_type)
8966 {
8967 default:
8968 continue;
8969
8970 case R_PPC64_GOT16_DS:
8971 case R_PPC64_GOT16_HA:
8972 case R_PPC64_GOT16_LO_DS:
4a421c53
AM
8973 sym_addend = rel->r_addend;
8974 break;
8975
8976 case R_PPC64_GOT_PCREL34:
8977 sym_addend = 0;
066f4018
AM
8978 break;
8979 }
8980
8981 r_symndx = ELF64_R_SYM (rel->r_info);
8982 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8983 r_symndx, ibfd))
8984 goto got_error_ret;
8985
8986 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8987 continue;
8988
8989 if (h != NULL)
8990 val = h->root.u.def.value;
8991 else
8992 val = sym->st_value;
4a421c53 8993 val += sym_addend;
066f4018
AM
8994 val += sym_sec->output_section->vma + sym_sec->output_offset;
8995
8996 switch (r_type)
8997 {
8998 default:
8999 continue;
9000
9001 case R_PPC64_GOT16_DS:
9002 if (val - got + 0x8000 >= 0x10000)
9003 continue;
9004 if (!bfd_get_section_contents (ibfd, sec, buf,
9005 rel->r_offset & ~3, 4))
9006 goto got_error_ret;
9007 insn = bfd_get_32 (ibfd, buf);
9008 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9009 continue;
9010 break;
9011
9012 case R_PPC64_GOT16_HA:
9013 if (val - got + 0x80008000ULL >= 0x100000000ULL)
9014 continue;
9015
9016 if (!bfd_get_section_contents (ibfd, sec, buf,
9017 rel->r_offset & ~3, 4))
9018 goto got_error_ret;
9019 insn = bfd_get_32 (ibfd, buf);
9020 if (((insn & ((0x3f << 26) | 0x1f << 16))
9021 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9022 continue;
9023 break;
9024
9025 case R_PPC64_GOT16_LO_DS:
9026 if (val - got + 0x80008000ULL >= 0x100000000ULL)
9027 continue;
9028 if (!bfd_get_section_contents (ibfd, sec, buf,
9029 rel->r_offset & ~3, 4))
9030 goto got_error_ret;
9031 insn = bfd_get_32 (ibfd, buf);
9032 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9033 continue;
9034 break;
4a421c53
AM
9035
9036 case R_PPC64_GOT_PCREL34:
9037 pc = rel->r_offset;
9038 pc += sec->output_section->vma + sec->output_offset;
9039 if (val - pc + (1ULL << 33) >= 1ULL << 34)
9040 continue;
9041 if (!bfd_get_section_contents (ibfd, sec, buf,
9042 rel->r_offset & ~3, 8))
9043 goto got_error_ret;
9044 insn = bfd_get_32 (ibfd, buf);
9045 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9046 continue;
9047 insn = bfd_get_32 (ibfd, buf + 4);
9048 if ((insn & (0x3f << 26)) != 57u << 26)
9049 continue;
9050 break;
066f4018
AM
9051 }
9052
9053 if (h != NULL)
9054 ent = h->got.glist;
9055 else
9056 {
9057 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9058 ent = local_got_ents[r_symndx];
9059 }
9060 for (; ent != NULL; ent = ent->next)
4a421c53 9061 if (ent->addend == sym_addend
066f4018
AM
9062 && ent->owner == ibfd
9063 && ent->tls_type == 0)
9064 break;
9065 BFD_ASSERT (ent && ent->got.refcount > 0);
9066 ent->got.refcount -= 1;
9067 }
9068
9069 if (elf_section_data (sec)->relocs != relstart)
9070 free (relstart);
9071 }
9072
9073 if (local_syms != NULL
9074 && symtab_hdr->contents != (unsigned char *) local_syms)
9075 {
9076 if (!info->keep_memory)
9077 free (local_syms);
9078 else
9079 symtab_hdr->contents = (unsigned char *) local_syms;
9080 }
9081 }
9082
c5614fa4
AM
9083 return TRUE;
9084}
9085
1bbe0902
AM
9086/* Return true iff input section I references the TOC using
9087 instructions limited to +/-32k offsets. */
9088
9089bfd_boolean
9090ppc64_elf_has_small_toc_reloc (asection *i)
9091{
9092 return (is_ppc64_elf (i->owner)
9093 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9094}
9095
927be08e
AM
9096/* Allocate space for one GOT entry. */
9097
9098static void
9099allocate_got (struct elf_link_hash_entry *h,
9100 struct bfd_link_info *info,
9101 struct got_entry *gent)
9102{
9103 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9104 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9105 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9106 ? 16 : 8);
9107 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9108 ? 2 : 1) * sizeof (Elf64_External_Rela);
9109 asection *got = ppc64_elf_tdata (gent->owner)->got;
9110
9111 gent->got.offset = got->size;
9112 got->size += entsize;
9113
19e08130 9114 if (h->type == STT_GNU_IFUNC)
927be08e 9115 {
33e44f2e 9116 htab->elf.irelplt->size += rentsize;
19e08130 9117 htab->got_reli_size += rentsize;
927be08e 9118 }
f15d0b54
AM
9119 else if (((bfd_link_pic (info)
9120 && !((gent->tls_type & TLS_TPREL) != 0
9121 && bfd_link_executable (info)
9122 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9123 || (htab->elf.dynamic_sections_created
9124 && h->dynindx != -1
9125 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9126 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9127 {
19e08130 9128 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9129 relgot->size += rentsize;
927be08e
AM
9130 }
9131}
9132
7865406b
AM
9133/* This function merges got entries in the same toc group. */
9134
9135static void
9136merge_got_entries (struct got_entry **pent)
9137{
9138 struct got_entry *ent, *ent2;
9139
9140 for (ent = *pent; ent != NULL; ent = ent->next)
9141 if (!ent->is_indirect)
9142 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9143 if (!ent2->is_indirect
9144 && ent2->addend == ent->addend
9145 && ent2->tls_type == ent->tls_type
9146 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9147 {
9148 ent2->is_indirect = TRUE;
9149 ent2->got.ent = ent;
9150 }
9151}
9152
46434633 9153/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9154
9155static bfd_boolean
46434633
AM
9156ensure_undef_dynamic (struct bfd_link_info *info,
9157 struct elf_link_hash_entry *h)
f0158f44
AM
9158{
9159 struct elf_link_hash_table *htab = elf_hash_table (info);
9160
9161 if (htab->dynamic_sections_created
46434633
AM
9162 && ((info->dynamic_undefined_weak != 0
9163 && h->root.type == bfd_link_hash_undefweak)
9164 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9165 && h->dynindx == -1
9166 && !h->forced_local
9167 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9168 return bfd_elf_link_record_dynamic_symbol (info, h);
9169 return TRUE;
9170}
9171
65f38f15
AM
9172/* Allocate space in .plt, .got and associated reloc sections for
9173 dynamic relocs. */
5bd4f169 9174
b34976b6 9175static bfd_boolean
4ce794b7 9176allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9177{
65f38f15
AM
9178 struct bfd_link_info *info;
9179 struct ppc_link_hash_table *htab;
5bd4f169 9180 asection *s;
65f38f15 9181 struct ppc_link_hash_entry *eh;
0b8bcf0d 9182 struct got_entry **pgent, *gent;
5bd4f169 9183
e92d460e 9184 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9185 return TRUE;
5bd4f169 9186
65f38f15
AM
9187 info = (struct bfd_link_info *) inf;
9188 htab = ppc_hash_table (info);
4dfe6ac6
NC
9189 if (htab == NULL)
9190 return FALSE;
5bd4f169 9191
951fd09b
AM
9192 eh = (struct ppc_link_hash_entry *) h;
9193 /* Run through the TLS GD got entries first if we're changing them
9194 to TPREL. */
37da22e5 9195 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
951fd09b
AM
9196 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9197 if (gent->got.refcount > 0
9198 && (gent->tls_type & TLS_GD) != 0)
9199 {
9200 /* This was a GD entry that has been converted to TPREL. If
9201 there happens to be a TPREL entry we can use that one. */
9202 struct got_entry *ent;
9203 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9204 if (ent->got.refcount > 0
9205 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9206 && ent->addend == gent->addend
9207 && ent->owner == gent->owner)
951fd09b
AM
9208 {
9209 gent->got.refcount = 0;
9210 break;
9211 }
9212
9213 /* If not, then we'll be using our own TPREL entry. */
9214 if (gent->got.refcount != 0)
9215 gent->tls_type = TLS_TLS | TLS_TPREL;
9216 }
9217
7865406b
AM
9218 /* Remove any list entry that won't generate a word in the GOT before
9219 we call merge_got_entries. Otherwise we risk merging to empty
9220 entries. */
0b8bcf0d
AM
9221 pgent = &h->got.glist;
9222 while ((gent = *pgent) != NULL)
411e1bfb 9223 if (gent->got.refcount > 0)
7865406b
AM
9224 {
9225 if ((gent->tls_type & TLS_LD) != 0
9226 && !h->def_dynamic)
9227 {
9228 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9229 *pgent = gent->next;
9230 }
9231 else
9232 pgent = &gent->next;
9233 }
9234 else
9235 *pgent = gent->next;
9236
9237 if (!htab->do_multi_toc)
9238 merge_got_entries (&h->got.glist);
9239
9240 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9241 if (!gent->is_indirect)
411e1bfb 9242 {
46434633
AM
9243 /* Make sure this symbol is output as a dynamic symbol. */
9244 if (!ensure_undef_dynamic (info, h))
f0158f44 9245 return FALSE;
65f38f15 9246
0c8d6e5c 9247 if (!is_ppc64_elf (gent->owner))
927be08e 9248 abort ();
0ffa91dd 9249
927be08e 9250 allocate_got (h, info, gent);
411e1bfb 9251 }
65f38f15 9252
954b63d4
AM
9253 /* If no dynamic sections we can't have dynamic relocs, except for
9254 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9255 if (!htab->elf.dynamic_sections_created
9256 && h->type != STT_GNU_IFUNC)
9257 eh->dyn_relocs = NULL;
9258
529fe20e
AM
9259 /* Discard relocs on undefined symbols that must be local. */
9260 else if (h->root.type == bfd_link_hash_undefined
9261 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9262 eh->dyn_relocs = NULL;
9263
954b63d4
AM
9264 /* Also discard relocs on undefined weak syms with non-default
9265 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9266 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
9267 eh->dyn_relocs = NULL;
9268
8a2058b5 9269 if (eh->dyn_relocs != NULL)
65f38f15 9270 {
8a2058b5
AM
9271 struct elf_dyn_relocs *p, **pp;
9272
57e7d118
AM
9273 /* In the shared -Bsymbolic case, discard space allocated for
9274 dynamic pc-relative relocs against symbols which turn out to
9275 be defined in regular objects. For the normal shared case,
9276 discard space for relocs that have become local due to symbol
9277 visibility changes. */
9278
9279 if (bfd_link_pic (info))
65f38f15 9280 {
57e7d118
AM
9281 /* Relocs that use pc_count are those that appear on a call
9282 insn, or certain REL relocs (see must_be_dyn_reloc) that
9283 can be generated via assembly. We want calls to
9284 protected symbols to resolve directly to the function
9285 rather than going via the plt. If people want function
9286 pointer comparisons to work as expected then they should
9287 avoid writing weird assembly. */
9288 if (SYMBOL_CALLS_LOCAL (info, h))
9289 {
57e7d118
AM
9290 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9291 {
9292 p->count -= p->pc_count;
9293 p->pc_count = 0;
9294 if (p->count == 0)
9295 *pp = p->next;
9296 else
9297 pp = &p->next;
9298 }
9299 }
65f38f15 9300
954b63d4 9301 if (eh->dyn_relocs != NULL)
5bd4f169 9302 {
46434633
AM
9303 /* Make sure this symbol is output as a dynamic symbol. */
9304 if (!ensure_undef_dynamic (info, h))
f0158f44 9305 return FALSE;
5bd4f169 9306 }
65f38f15 9307 }
529fe20e 9308 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 9309 {
8a2058b5 9310 /* For the non-pic case, discard space for relocs against
57e7d118
AM
9311 symbols which turn out to need copy relocs or are not
9312 dynamic. */
529fe20e
AM
9313 if (h->dynamic_adjusted
9314 && !h->def_regular
9315 && !ELF_COMMON_DEF_P (h))
f0158f44 9316 {
46434633
AM
9317 /* Make sure this symbol is output as a dynamic symbol. */
9318 if (!ensure_undef_dynamic (info, h))
f0158f44 9319 return FALSE;
dfbb6ac9 9320
f0158f44
AM
9321 if (h->dynindx == -1)
9322 eh->dyn_relocs = NULL;
9323 }
9324 else
8a2058b5 9325 eh->dyn_relocs = NULL;
57e7d118
AM
9326 }
9327
9328 /* Finally, allocate space. */
9329 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9330 {
9331 asection *sreloc = elf_section_data (p->sec)->sreloc;
9332 if (eh->elf.type == STT_GNU_IFUNC)
9333 sreloc = htab->elf.irelplt;
9334 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9335 }
65f38f15 9336 }
57e7d118 9337
2d7ad24e
AM
9338 /* We might need a PLT entry when the symbol
9339 a) is dynamic, or
9340 b) is an ifunc, or
9341 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9342 d) has plt16 relocs and we are linking statically. */
9343 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9344 || h->type == STT_GNU_IFUNC
9345 || (h->needs_plt && h->dynamic_adjusted)
9346 || (h->needs_plt
9347 && h->def_regular
9348 && !htab->elf.dynamic_sections_created
3e04d765 9349 && !htab->can_convert_all_inline_plt
2d7ad24e
AM
9350 && (((struct ppc_link_hash_entry *) h)->tls_mask
9351 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 9352 {
57e7d118
AM
9353 struct plt_entry *pent;
9354 bfd_boolean doneone = FALSE;
9355 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9356 if (pent->plt.refcount > 0)
9357 {
9358 if (!htab->elf.dynamic_sections_created
9359 || h->dynindx == -1)
9360 {
2d7ad24e
AM
9361 if (h->type == STT_GNU_IFUNC)
9362 {
9363 s = htab->elf.iplt;
9364 pent->plt.offset = s->size;
9365 s->size += PLT_ENTRY_SIZE (htab);
9366 s = htab->elf.irelplt;
9367 }
9368 else
9369 {
9370 s = htab->pltlocal;
9371 pent->plt.offset = s->size;
9372 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9373 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9374 }
57e7d118
AM
9375 }
9376 else
9377 {
9378 /* If this is the first .plt entry, make room for the special
9379 first entry. */
9380 s = htab->elf.splt;
9381 if (s->size == 0)
9382 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9383
57e7d118 9384 pent->plt.offset = s->size;
65f38f15 9385
57e7d118
AM
9386 /* Make room for this entry. */
9387 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9388
57e7d118
AM
9389 /* Make room for the .glink code. */
9390 s = htab->glink;
9391 if (s->size == 0)
9e390558 9392 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9393 if (htab->opd_abi)
9394 {
9395 /* We need bigger stubs past index 32767. */
9e390558 9396 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9397 s->size += 4;
9398 s->size += 2*4;
9399 }
9400 else
9401 s->size += 4;
65f38f15 9402
57e7d118
AM
9403 /* We also need to make an entry in the .rela.plt section. */
9404 s = htab->elf.srelplt;
9405 }
2d7ad24e
AM
9406 if (s != NULL)
9407 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
9408 doneone = TRUE;
9409 }
9410 else
9411 pent->plt.offset = (bfd_vma) -1;
9412 if (!doneone)
9413 {
9414 h->plt.plist = NULL;
9415 h->needs_plt = 0;
9416 }
65f38f15 9417 }
57e7d118 9418 else
65f38f15 9419 {
57e7d118
AM
9420 h->plt.plist = NULL;
9421 h->needs_plt = 0;
65f38f15
AM
9422 }
9423
b34976b6 9424 return TRUE;
65f38f15
AM
9425}
9426
9e390558
AM
9427#define PPC_LO(v) ((v) & 0xffff)
9428#define PPC_HI(v) (((v) >> 16) & 0xffff)
9429#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9430
a345bc8d
AM
9431/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9432 to set up space for global entry stubs. These are put in glink,
9433 after the branch table. */
65f38f15 9434
b34976b6 9435static bfd_boolean
a345bc8d 9436size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9437{
a345bc8d
AM
9438 struct bfd_link_info *info;
9439 struct ppc_link_hash_table *htab;
9440 struct plt_entry *pent;
9e390558 9441 asection *s, *plt;
65f38f15 9442
a345bc8d
AM
9443 if (h->root.type == bfd_link_hash_indirect)
9444 return TRUE;
65f38f15 9445
a345bc8d
AM
9446 if (!h->pointer_equality_needed)
9447 return TRUE;
65f38f15 9448
a345bc8d
AM
9449 if (h->def_regular)
9450 return TRUE;
65f38f15 9451
a345bc8d
AM
9452 info = inf;
9453 htab = ppc_hash_table (info);
9454 if (htab == NULL)
9455 return FALSE;
9456
9e390558
AM
9457 s = htab->global_entry;
9458 plt = htab->elf.splt;
a345bc8d
AM
9459 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9460 if (pent->plt.offset != (bfd_vma) -1
9461 && pent->addend == 0)
9462 {
afe397ea
AM
9463 /* For ELFv2, if this symbol is not defined in a regular file
9464 and we are not generating a shared library or pie, then we
9465 need to define the symbol in the executable on a call stub.
9466 This is to avoid text relocations. */
9e390558
AM
9467 bfd_vma off, stub_align, stub_off, stub_size;
9468 unsigned int align_power;
9469
9470 stub_size = 16;
9471 stub_off = s->size;
9472 if (htab->params->plt_stub_align >= 0)
9473 align_power = htab->params->plt_stub_align;
9474 else
9475 align_power = -htab->params->plt_stub_align;
9476 /* Setting section alignment is delayed until we know it is
9477 non-empty. Otherwise the .text output section will be
9478 aligned at least to plt_stub_align even when no global
9479 entry stubs are needed. */
9480 if (s->alignment_power < align_power)
9481 s->alignment_power = align_power;
9482 stub_align = (bfd_vma) 1 << align_power;
9483 if (htab->params->plt_stub_align >= 0
9484 || ((((stub_off + stub_size - 1) & -stub_align)
9485 - (stub_off & -stub_align))
9486 > ((stub_size - 1) & -stub_align)))
9487 stub_off = (stub_off + stub_align - 1) & -stub_align;
9488 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9489 off -= stub_off + s->output_offset + s->output_section->vma;
9490 /* Note that for --plt-stub-align negative we have a possible
9491 dependency between stub offset and size. Break that
9492 dependency by assuming the max stub size when calculating
9493 the stub offset. */
9494 if (PPC_HA (off) == 0)
9495 stub_size -= 4;
8a2058b5 9496 h->root.type = bfd_link_hash_defined;
afe397ea 9497 h->root.u.def.section = s;
9e390558
AM
9498 h->root.u.def.value = stub_off;
9499 s->size = stub_off + stub_size;
a345bc8d
AM
9500 break;
9501 }
9502 return TRUE;
9503}
9504
9505/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9506 read-only sections. */
9507
9508static bfd_boolean
98bbb1b8 9509maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 9510{
98bbb1b8
AM
9511 asection *sec;
9512
a345bc8d
AM
9513 if (h->root.type == bfd_link_hash_indirect)
9514 return TRUE;
9515
98bbb1b8
AM
9516 sec = readonly_dynrelocs (h);
9517 if (sec != NULL)
a345bc8d 9518 {
98bbb1b8
AM
9519 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9520
9521 info->flags |= DF_TEXTREL;
2cdcc330
AM
9522 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9523 " in read-only section `%pA'\n"),
9524 sec->owner, h->root.root.string, sec);
a345bc8d
AM
9525
9526 /* Not an error, just cut short the traversal. */
9527 return FALSE;
65f38f15 9528 }
b34976b6 9529 return TRUE;
65f38f15
AM
9530}
9531
9532/* Set the sizes of the dynamic sections. */
9533
b34976b6 9534static bfd_boolean
ee67d69a 9535ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9536 struct bfd_link_info *info)
65f38f15
AM
9537{
9538 struct ppc_link_hash_table *htab;
9539 bfd *dynobj;
9540 asection *s;
b34976b6 9541 bfd_boolean relocs;
65f38f15 9542 bfd *ibfd;
7865406b 9543 struct got_entry *first_tlsld;
65f38f15
AM
9544
9545 htab = ppc_hash_table (info);
4dfe6ac6
NC
9546 if (htab == NULL)
9547 return FALSE;
9548
65f38f15
AM
9549 dynobj = htab->elf.dynobj;
9550 if (dynobj == NULL)
9551 abort ();
9552
9553 if (htab->elf.dynamic_sections_created)
9554 {
9555 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9556 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9557 {
3d4d4302 9558 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9559 if (s == NULL)
9560 abort ();
eea6121a 9561 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9562 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9563 }
9564 }
9565
9566 /* Set up .got offsets for local syms, and space for local dynamic
9567 relocs. */
c72f2fb2 9568 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9569 {
411e1bfb
AM
9570 struct got_entry **lgot_ents;
9571 struct got_entry **end_lgot_ents;
e054468f
AM
9572 struct plt_entry **local_plt;
9573 struct plt_entry **end_local_plt;
f961d9dd 9574 unsigned char *lgot_masks;
65f38f15
AM
9575 bfd_size_type locsymcount;
9576 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9577
0c8d6e5c 9578 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9579 continue;
9580
9581 for (s = ibfd->sections; s != NULL; s = s->next)
9582 {
19e08130 9583 struct ppc_dyn_relocs *p;
65f38f15 9584
6edfbbad 9585 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9586 {
ec338859
AM
9587 if (!bfd_is_abs_section (p->sec)
9588 && bfd_is_abs_section (p->sec->output_section))
9589 {
9590 /* Input section has been discarded, either because
9591 it is a copy of a linkonce section or due to
9592 linker script /DISCARD/, so we'll be discarding
9593 the relocs too. */
9594 }
248866a8 9595 else if (p->count != 0)
ec338859 9596 {
19e08130
AM
9597 asection *srel = elf_section_data (p->sec)->sreloc;
9598 if (p->ifunc)
33e44f2e 9599 srel = htab->elf.irelplt;
eea6121a 9600 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9601 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9602 info->flags |= DF_TEXTREL;
ec338859 9603 }
65f38f15
AM
9604 }
9605 }
9606
411e1bfb
AM
9607 lgot_ents = elf_local_got_ents (ibfd);
9608 if (!lgot_ents)
65f38f15
AM
9609 continue;
9610
0ffa91dd 9611 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9612 locsymcount = symtab_hdr->sh_info;
411e1bfb 9613 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9614 local_plt = (struct plt_entry **) end_lgot_ents;
9615 end_local_plt = local_plt + locsymcount;
f961d9dd 9616 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9617 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9618 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9619 {
0b8bcf0d 9620 struct got_entry **pent, *ent;
411e1bfb 9621
0b8bcf0d
AM
9622 pent = lgot_ents;
9623 while ((ent = *pent) != NULL)
411e1bfb
AM
9624 if (ent->got.refcount > 0)
9625 {
e7b938ca 9626 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9627 {
927be08e 9628 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9629 *pent = ent->next;
411e1bfb
AM
9630 }
9631 else
9632 {
19e08130
AM
9633 unsigned int ent_size = 8;
9634 unsigned int rel_size = sizeof (Elf64_External_Rela);
9635
eea6121a 9636 ent->got.offset = s->size;
e7b938ca 9637 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9638 {
19e08130
AM
9639 ent_size *= 2;
9640 rel_size *= 2;
9641 }
9642 s->size += ent_size;
37da22e5 9643 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 9644 {
33e44f2e 9645 htab->elf.irelplt->size += rel_size;
19e08130
AM
9646 htab->got_reli_size += rel_size;
9647 }
f15d0b54
AM
9648 else if (bfd_link_pic (info)
9649 && !((ent->tls_type & TLS_TPREL) != 0
9650 && bfd_link_executable (info)))
19e08130
AM
9651 {
9652 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9653 srel->size += rel_size;
927be08e 9654 }
0b8bcf0d 9655 pent = &ent->next;
411e1bfb
AM
9656 }
9657 }
9658 else
0b8bcf0d 9659 *pent = ent->next;
65f38f15 9660 }
e054468f 9661
2d7ad24e
AM
9662 /* Allocate space for plt calls to local syms. */
9663 lgot_masks = (unsigned char *) end_local_plt;
9664 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
9665 {
9666 struct plt_entry *ent;
9667
9668 for (ent = *local_plt; ent != NULL; ent = ent->next)
9669 if (ent->plt.refcount > 0)
9670 {
2d7ad24e
AM
9671 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9672 {
9673 s = htab->elf.iplt;
9674 ent->plt.offset = s->size;
9675 s->size += PLT_ENTRY_SIZE (htab);
9676 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9677 }
3e04d765
AM
9678 else if (htab->can_convert_all_inline_plt
9679 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
9680 ent->plt.offset = (bfd_vma) -1;
9681 else
9682 {
9683 s = htab->pltlocal;
9684 ent->plt.offset = s->size;
9685 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9686 if (bfd_link_pic (info))
9687 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9688 }
e054468f
AM
9689 }
9690 else
9691 ent->plt.offset = (bfd_vma) -1;
9692 }
65f38f15
AM
9693 }
9694
9695 /* Allocate global sym .plt and .got entries, and space for global
9696 sym dynamic relocs. */
4ce794b7 9697 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 9698
0e1862bb 9699 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 9700 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 9701
7865406b 9702 first_tlsld = NULL;
c72f2fb2 9703 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 9704 {
7865406b
AM
9705 struct got_entry *ent;
9706
0c8d6e5c 9707 if (!is_ppc64_elf (ibfd))
102890f0
AM
9708 continue;
9709
7865406b
AM
9710 ent = ppc64_tlsld_got (ibfd);
9711 if (ent->got.refcount > 0)
102890f0 9712 {
7865406b 9713 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9714 {
7865406b
AM
9715 ent->is_indirect = TRUE;
9716 ent->got.ent = first_tlsld;
9717 }
9718 else
9719 {
9720 if (first_tlsld == NULL)
9721 first_tlsld = ent;
9722 s = ppc64_elf_tdata (ibfd)->got;
9723 ent->got.offset = s->size;
9724 ent->owner = ibfd;
9725 s->size += 16;
0e1862bb 9726 if (bfd_link_pic (info))
7865406b
AM
9727 {
9728 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9729 srel->size += sizeof (Elf64_External_Rela);
9730 }
102890f0
AM
9731 }
9732 }
9733 else
7865406b 9734 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9735 }
9736
65f38f15
AM
9737 /* We now have determined the sizes of the various dynamic sections.
9738 Allocate memory for them. */
b34976b6 9739 relocs = FALSE;
65f38f15
AM
9740 for (s = dynobj->sections; s != NULL; s = s->next)
9741 {
9742 if ((s->flags & SEC_LINKER_CREATED) == 0)
9743 continue;
9744
4ce794b7 9745 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9746 /* These haven't been allocated yet; don't strip. */
9747 continue;
33e44f2e
AM
9748 else if (s == htab->elf.sgot
9749 || s == htab->elf.splt
9750 || s == htab->elf.iplt
2d7ad24e 9751 || s == htab->pltlocal
c456f082 9752 || s == htab->glink
9e390558 9753 || s == htab->global_entry
5474d94f
AM
9754 || s == htab->elf.sdynbss
9755 || s == htab->elf.sdynrelro)
65f38f15
AM
9756 {
9757 /* Strip this section if we don't need it; see the
9758 comment below. */
5bd4f169 9759 }
58d180e8
AM
9760 else if (s == htab->glink_eh_frame)
9761 {
9762 if (!bfd_is_abs_section (s->output_section))
9763 /* Not sized yet. */
9764 continue;
9765 }
70cc837d 9766 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9767 {
c456f082 9768 if (s->size != 0)
5bd4f169 9769 {
33e44f2e 9770 if (s != htab->elf.srelplt)
b34976b6 9771 relocs = TRUE;
5bd4f169
AM
9772
9773 /* We use the reloc_count field as a counter if we need
9774 to copy relocs into the output file. */
9775 s->reloc_count = 0;
9776 }
9777 }
65f38f15 9778 else
5bd4f169
AM
9779 {
9780 /* It's not one of our sections, so don't allocate space. */
9781 continue;
9782 }
9783
eea6121a 9784 if (s->size == 0)
5bd4f169 9785 {
c456f082
AM
9786 /* If we don't need this section, strip it from the
9787 output file. This is mostly to handle .rela.bss and
9788 .rela.plt. We must create both sections in
9789 create_dynamic_sections, because they must be created
9790 before the linker maps input sections to output
9791 sections. The linker does that before
9792 adjust_dynamic_symbol is called, and it is that
9793 function which decides whether anything needs to go
9794 into these sections. */
8423293d 9795 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9796 continue;
9797 }
9798
06bcf541
AM
9799 if (bfd_is_abs_section (s->output_section))
9800 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9801 s->name);
9802
c456f082 9803 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9804 continue;
9805
65f38f15
AM
9806 /* Allocate memory for the section contents. We use bfd_zalloc
9807 here in case unused entries are not reclaimed before the
9808 section's contents are written out. This should not happen,
411e1bfb
AM
9809 but this way if it does we get a R_PPC64_NONE reloc in .rela
9810 sections instead of garbage.
9811 We also rely on the section contents being zero when writing
5474d94f 9812 the GOT and .dynrelro. */
eea6121a 9813 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9814 if (s->contents == NULL)
b34976b6 9815 return FALSE;
5bd4f169
AM
9816 }
9817
c72f2fb2 9818 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 9819 {
0c8d6e5c 9820 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9821 continue;
9822
e717da7e 9823 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 9824 if (s != NULL && s != htab->elf.sgot)
e717da7e 9825 {
eea6121a 9826 if (s->size == 0)
8423293d 9827 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9828 else
9829 {
eea6121a 9830 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9831 if (s->contents == NULL)
9832 return FALSE;
9833 }
9834 }
9835 s = ppc64_elf_tdata (ibfd)->relgot;
9836 if (s != NULL)
9837 {
eea6121a 9838 if (s->size == 0)
8423293d 9839 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9840 else
9841 {
eea6121a 9842 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9843 if (s->contents == NULL)
9844 return FALSE;
9845 relocs = TRUE;
9846 s->reloc_count = 0;
9847 }
9848 }
9849 }
9850
e86ce104 9851 if (htab->elf.dynamic_sections_created)
5bd4f169 9852 {
e8910a83
AM
9853 bfd_boolean tls_opt;
9854
5bd4f169
AM
9855 /* Add some entries to the .dynamic section. We fill in the
9856 values later, in ppc64_elf_finish_dynamic_sections, but we
9857 must add the entries now so that we get the correct size for
9858 the .dynamic section. The DT_DEBUG entry is filled in by the
9859 dynamic linker and used by the debugger. */
dc810e39 9860#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9861 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9862
0e1862bb 9863 if (bfd_link_executable (info))
5bd4f169 9864 {
dc810e39 9865 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9866 return FALSE;
5bd4f169
AM
9867 }
9868
33e44f2e 9869 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 9870 {
dc810e39
AM
9871 if (!add_dynamic_entry (DT_PLTGOT, 0)
9872 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9873 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9874 || !add_dynamic_entry (DT_JMPREL, 0)
9875 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9876 return FALSE;
5bd4f169
AM
9877 }
9878
ee67d69a 9879 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
9880 {
9881 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9882 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9883 return FALSE;
19397422
AM
9884 }
9885
7c9cf415 9886 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
9887 && htab->tls_get_addr_fd != NULL
9888 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9889 if (tls_opt || !htab->opd_abi)
9890 {
9891 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9892 return FALSE;
9893 }
a7f2871e 9894
5bd4f169
AM
9895 if (relocs)
9896 {
dc810e39
AM
9897 if (!add_dynamic_entry (DT_RELA, 0)
9898 || !add_dynamic_entry (DT_RELASZ, 0)
9899 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9900 return FALSE;
5bd4f169 9901
65f38f15
AM
9902 /* If any dynamic relocs apply to a read-only section,
9903 then we need a DT_TEXTREL entry. */
248866a8 9904 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 9905 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 9906
65f38f15 9907 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9908 {
65f38f15 9909 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9910 return FALSE;
5bd4f169 9911 }
5bd4f169 9912 }
5bd4f169 9913 }
65f38f15 9914#undef add_dynamic_entry
5bd4f169 9915
b34976b6 9916 return TRUE;
5bd4f169
AM
9917}
9918
a345bc8d
AM
9919/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
9920
9921static bfd_boolean
9922ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9923{
9924 if (h->plt.plist != NULL
9925 && !h->def_regular
9926 && !h->pointer_equality_needed)
9927 return FALSE;
9928
9929 return _bfd_elf_hash_symbol (h);
9930}
9931
721956f4 9932/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9933
4ce794b7
AM
9934static inline enum ppc_stub_type
9935ppc_type_of_stub (asection *input_sec,
9936 const Elf_Internal_Rela *rel,
9937 struct ppc_link_hash_entry **hash,
e054468f 9938 struct plt_entry **plt_ent,
6911b7dc
AM
9939 bfd_vma destination,
9940 unsigned long local_off)
5bd4f169 9941{
721956f4
AM
9942 struct ppc_link_hash_entry *h = *hash;
9943 bfd_vma location;
9944 bfd_vma branch_offset;
9945 bfd_vma max_branch_offset;
4ce794b7 9946 enum elf_ppc64_reloc_type r_type;
5bd4f169 9947
721956f4
AM
9948 if (h != NULL)
9949 {
e054468f 9950 struct plt_entry *ent;
7fe2b9a6 9951 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
9952 if (h->oh != NULL
9953 && h->oh->is_func_descriptor)
7b8f6675
AM
9954 {
9955 fdh = ppc_follow_link (h->oh);
9956 *hash = fdh;
9957 }
8387904d 9958
e054468f
AM
9959 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9960 if (ent->addend == rel->r_addend
9961 && ent->plt.offset != (bfd_vma) -1)
9962 {
e054468f
AM
9963 *plt_ent = ent;
9964 return ppc_stub_plt_call;
9965 }
5bd4f169 9966
7fe2b9a6
AM
9967 /* Here, we know we don't have a plt entry. If we don't have a
9968 either a defined function descriptor or a defined entry symbol
9969 in a regular object file, then it is pointless trying to make
9970 any other type of stub. */
854b41e7
AM
9971 if (!is_static_defined (&fdh->elf)
9972 && !is_static_defined (&h->elf))
721956f4 9973 return ppc_stub_none;
5d1634d7 9974 }
e054468f
AM
9975 else if (elf_local_got_ents (input_sec->owner) != NULL)
9976 {
9977 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9978 struct plt_entry **local_plt = (struct plt_entry **)
9979 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9980 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9981
9982 if (local_plt[r_symndx] != NULL)
9983 {
9984 struct plt_entry *ent;
9985
9986 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9987 if (ent->addend == rel->r_addend
9988 && ent->plt.offset != (bfd_vma) -1)
9989 {
9990 *plt_ent = ent;
9991 return ppc_stub_plt_call;
9992 }
9993 }
9994 }
5d1634d7 9995
721956f4
AM
9996 /* Determine where the call point is. */
9997 location = (input_sec->output_offset
9998 + input_sec->output_section->vma
9999 + rel->r_offset);
5d1634d7 10000
721956f4
AM
10001 branch_offset = destination - location;
10002 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10003
721956f4
AM
10004 /* Determine if a long branch stub is needed. */
10005 max_branch_offset = 1 << 25;
23cedd1d
AM
10006 if (r_type == R_PPC64_REL14
10007 || r_type == R_PPC64_REL14_BRTAKEN
10008 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10009 max_branch_offset = 1 << 15;
5d1634d7 10010
6911b7dc 10011 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10012 /* We need a stub. Figure out whether a long_branch or plt_branch
10013 is needed later. */
10014 return ppc_stub_long_branch;
5d1634d7 10015
721956f4 10016 return ppc_stub_none;
5d1634d7
AM
10017}
10018
f891966f
AM
10019/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10020 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10021 . mflr %r12
10022 . bcl 20,31,1f
10023 .1: mflr %r11
10024 . mtlr %r12
05d0e962 10025 . lis %r12,xxx-1b@highest
f891966f 10026 . ori %r12,%r12,xxx-1b@higher
05d0e962 10027 . sldi %r12,%r12,32
f891966f 10028 . oris %r12,%r12,xxx-1b@high
05d0e962 10029 . ori %r12,%r12,xxx-1b@l
f891966f 10030 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10031
10032static bfd_byte *
10033build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10034{
f891966f
AM
10035 bfd_put_32 (abfd, MFLR_R12, p);
10036 p += 4;
10037 bfd_put_32 (abfd, BCL_20_31, p);
10038 p += 4;
10039 bfd_put_32 (abfd, MFLR_R11, p);
10040 p += 4;
10041 bfd_put_32 (abfd, MTLR_R12, p);
10042 p += 4;
05d0e962
AM
10043 if (off + 0x8000 < 0x10000)
10044 {
10045 if (load)
10046 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10047 else
10048 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10049 p += 4;
10050 }
10051 else if (off + 0x80008000ULL < 0x100000000ULL)
10052 {
10053 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10054 p += 4;
10055 if (load)
10056 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10057 else
10058 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10059 p += 4;
10060 }
10061 else
10062 {
10063 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10064 {
10065 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10066 p += 4;
10067 }
10068 else
10069 {
10070 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10071 p += 4;
10072 if (((off >> 32) & 0xffff) != 0)
10073 {
10074 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10075 p += 4;
10076 }
10077 }
10078 if (((off >> 32) & 0xffffffffULL) != 0)
10079 {
10080 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10081 p += 4;
10082 }
10083 if (PPC_HI (off) != 0)
10084 {
10085 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10086 p += 4;
10087 }
10088 if (PPC_LO (off) != 0)
10089 {
10090 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10091 p += 4;
10092 }
10093 if (load)
10094 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10095 else
10096 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10097 p += 4;
10098 }
10099 return p;
10100}
10101
10102static unsigned int
10103size_offset (bfd_vma off)
10104{
10105 unsigned int size;
10106 if (off + 0x8000 < 0x10000)
10107 size = 4;
10108 else if (off + 0x80008000ULL < 0x100000000ULL)
10109 size = 8;
10110 else
10111 {
10112 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10113 size = 4;
10114 else
10115 {
10116 size = 4;
10117 if (((off >> 32) & 0xffff) != 0)
10118 size += 4;
10119 }
10120 if (((off >> 32) & 0xffffffffULL) != 0)
10121 size += 4;
10122 if (PPC_HI (off) != 0)
10123 size += 4;
10124 if (PPC_LO (off) != 0)
10125 size += 4;
10126 size += 4;
10127 }
f891966f 10128 return size + 16;
05d0e962
AM
10129}
10130
3d58e1fc
AM
10131static unsigned int
10132num_relocs_for_offset (bfd_vma off)
10133{
10134 unsigned int num_rel;
10135 if (off + 0x8000 < 0x10000)
10136 num_rel = 1;
10137 else if (off + 0x80008000ULL < 0x100000000ULL)
10138 num_rel = 2;
10139 else
10140 {
10141 num_rel = 1;
10142 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10143 && ((off >> 32) & 0xffff) != 0)
10144 num_rel += 1;
10145 if (PPC_HI (off) != 0)
10146 num_rel += 1;
10147 if (PPC_LO (off) != 0)
10148 num_rel += 1;
10149 }
10150 return num_rel;
10151}
10152
10153static Elf_Internal_Rela *
10154emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10155 bfd_vma roff, bfd_vma targ, bfd_vma off)
10156{
10157 bfd_vma relative_targ = targ - (roff - 8);
10158 if (bfd_big_endian (info->output_bfd))
10159 roff += 2;
10160 r->r_offset = roff;
10161 r->r_addend = relative_targ + roff;
10162 if (off + 0x8000 < 0x10000)
10163 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10164 else if (off + 0x80008000ULL < 0x100000000ULL)
10165 {
10166 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10167 ++r;
10168 roff += 4;
10169 r->r_offset = roff;
10170 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10171 r->r_addend = relative_targ + roff;
10172 }
10173 else
10174 {
10175 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10176 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10177 else
10178 {
10179 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10180 if (((off >> 32) & 0xffff) != 0)
10181 {
10182 ++r;
10183 roff += 4;
10184 r->r_offset = roff;
10185 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10186 r->r_addend = relative_targ + roff;
10187 }
10188 }
10189 if (((off >> 32) & 0xffffffffULL) != 0)
10190 roff += 4;
10191 if (PPC_HI (off) != 0)
10192 {
10193 ++r;
10194 roff += 4;
10195 r->r_offset = roff;
10196 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10197 r->r_addend = relative_targ + roff;
10198 }
10199 if (PPC_LO (off) != 0)
10200 {
10201 ++r;
10202 roff += 4;
10203 r->r_offset = roff;
10204 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10205 r->r_addend = relative_targ + roff;
10206 }
10207 }
10208 return r;
10209}
10210
df136d64
AM
10211/* Emit .eh_frame opcode to advance pc by DELTA. */
10212
10213static bfd_byte *
10214eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10215{
10216 delta /= 4;
10217 if (delta < 64)
10218 *eh++ = DW_CFA_advance_loc + delta;
10219 else if (delta < 256)
10220 {
10221 *eh++ = DW_CFA_advance_loc1;
10222 *eh++ = delta;
10223 }
10224 else if (delta < 65536)
10225 {
10226 *eh++ = DW_CFA_advance_loc2;
10227 bfd_put_16 (abfd, delta, eh);
10228 eh += 2;
10229 }
10230 else
10231 {
10232 *eh++ = DW_CFA_advance_loc4;
10233 bfd_put_32 (abfd, delta, eh);
10234 eh += 4;
10235 }
10236 return eh;
10237}
10238
10239/* Size of required .eh_frame opcode to advance pc by DELTA. */
10240
10241static unsigned int
10242eh_advance_size (unsigned int delta)
10243{
10244 if (delta < 64 * 4)
10245 /* DW_CFA_advance_loc+[1..63]. */
10246 return 1;
10247 if (delta < 256 * 4)
10248 /* DW_CFA_advance_loc1, byte. */
10249 return 2;
10250 if (delta < 65536 * 4)
10251 /* DW_CFA_advance_loc2, 2 bytes. */
10252 return 3;
10253 /* DW_CFA_advance_loc4, 4 bytes. */
10254 return 5;
10255}
10256
794e51c0
AM
10257/* With power7 weakly ordered memory model, it is possible for ld.so
10258 to update a plt entry in one thread and have another thread see a
10259 stale zero toc entry. To avoid this we need some sort of acquire
10260 barrier in the call stub. One solution is to make the load of the
10261 toc word seem to appear to depend on the load of the function entry
10262 word. Another solution is to test for r2 being zero, and branch to
10263 the appropriate glink entry if so.
10264
10265 . fake dep barrier compare
71a39c98
AM
10266 . ld 12,xxx(2) ld 12,xxx(2)
10267 . mtctr 12 mtctr 12
10268 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10269 . add 2,2,11 cmpldi 2,0
10270 . ld 2,xxx+8(2) bnectr+
10271 . bctr b <glink_entry>
10272
10273 The solution involving the compare turns out to be faster, so
10274 that's what we use unless the branch won't reach. */
10275
10276#define ALWAYS_USE_FAKE_DEP 0
10277#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10278
794e51c0
AM
10279static inline unsigned int
10280plt_stub_size (struct ppc_link_hash_table *htab,
10281 struct ppc_stub_hash_entry *stub_entry,
10282 bfd_vma off)
10283{
05d0e962 10284 unsigned size;
b9e5796b 10285
05d0e962
AM
10286 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10287 {
f891966f 10288 size = 8 + size_offset (off - 8);
05d0e962
AM
10289 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10290 size += 4;
10291 return size;
10292 }
10293
10294 size = 12;
b9e5796b
AM
10295 if (ALWAYS_EMIT_R2SAVE
10296 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10297 size += 4;
10298 if (PPC_HA (off) != 0)
794e51c0 10299 size += 4;
b9e5796b
AM
10300 if (htab->opd_abi)
10301 {
10302 size += 4;
e7d1c40c 10303 if (htab->params->plt_static_chain)
b9e5796b 10304 size += 4;
bd4d2eaa
AM
10305 if (htab->params->plt_thread_safe
10306 && htab->elf.dynamic_sections_created
10307 && stub_entry->h != NULL
10308 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10309 size += 8;
e7d1c40c 10310 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10311 size += 4;
10312 }
794e51c0
AM
10313 if (stub_entry->h != NULL
10314 && (stub_entry->h == htab->tls_get_addr_fd
10315 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10316 && htab->params->tls_get_addr_opt)
f378ab09
AM
10317 {
10318 size += 7 * 4;
e81b4c93 10319 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 10320 size += 6 * 4;
f378ab09 10321 }
794e51c0
AM
10322 return size;
10323}
10324
2420fff6
AM
10325/* Depending on the sign of plt_stub_align:
10326 If positive, return the padding to align to a 2**plt_stub_align
10327 boundary.
10328 If negative, if this stub would cross fewer 2**plt_stub_align
10329 boundaries if we align, then return the padding needed to do so. */
10330
794e51c0
AM
10331static inline unsigned int
10332plt_stub_pad (struct ppc_link_hash_table *htab,
10333 struct ppc_stub_hash_entry *stub_entry,
10334 bfd_vma plt_off)
10335{
2420fff6 10336 int stub_align;
1aa42141 10337 unsigned stub_size;
6f20ed8a 10338 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10339
2420fff6
AM
10340 if (htab->params->plt_stub_align >= 0)
10341 {
10342 stub_align = 1 << htab->params->plt_stub_align;
10343 if ((stub_off & (stub_align - 1)) != 0)
10344 return stub_align - (stub_off & (stub_align - 1));
10345 return 0;
10346 }
10347
10348 stub_align = 1 << -htab->params->plt_stub_align;
1aa42141 10349 stub_size = plt_stub_size (htab, stub_entry, plt_off);
794e51c0 10350 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10351 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10352 return stub_align - (stub_off & (stub_align - 1));
10353 return 0;
10354}
10355
10356/* Build a .plt call stub. */
10357
10358static inline bfd_byte *
10359build_plt_stub (struct ppc_link_hash_table *htab,
10360 struct ppc_stub_hash_entry *stub_entry,
10361 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10362{
e7d1c40c 10363 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10364 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10365 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10366 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10367 && htab->elf.dynamic_sections_created
10368 && stub_entry->h != NULL
10369 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10370 bfd_boolean use_fake_dep = plt_thread_safe;
10371 bfd_vma cmp_branch_off = 0;
10372
10373 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10374 && plt_load_toc
794e51c0 10375 && plt_thread_safe
bd4d2eaa
AM
10376 && !((stub_entry->h == htab->tls_get_addr_fd
10377 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10378 && htab->params->tls_get_addr_opt))
794e51c0
AM
10379 {
10380 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10381 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10382 / PLT_ENTRY_SIZE (htab));
9e390558 10383 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10384 bfd_vma to, from;
10385
68d62958
AM
10386 if (pltindex > 32768)
10387 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10388 to = (glinkoff
10389 + htab->glink->output_offset
10390 + htab->glink->output_section->vma);
6f20ed8a 10391 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10392 + 4 * (ALWAYS_EMIT_R2SAVE
10393 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10394 + 4 * (PPC_HA (offset) != 0)
10395 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10396 != PPC_HA (offset))
10397 + 4 * (plt_static_chain != 0)
10398 + 20
6f20ed8a
AM
10399 + stub_entry->group->stub_sec->output_offset
10400 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10401 cmp_branch_off = to - from;
10402 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10403 }
10404
ac2df442
AM
10405 if (PPC_HA (offset) != 0)
10406 {
176a0d42
AM
10407 if (r != NULL)
10408 {
794e51c0
AM
10409 if (ALWAYS_EMIT_R2SAVE
10410 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10411 r[0].r_offset += 4;
176a0d42 10412 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10413 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10414 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10415 r[1].r_addend = r[0].r_addend;
b9e5796b 10416 if (plt_load_toc)
176a0d42 10417 {
b9e5796b 10418 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10419 {
b9e5796b
AM
10420 r[2].r_offset = r[1].r_offset + 4;
10421 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10422 r[2].r_addend = r[0].r_addend;
10423 }
10424 else
10425 {
10426 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10427 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10428 r[2].r_addend = r[0].r_addend + 8;
10429 if (plt_static_chain)
10430 {
10431 r[3].r_offset = r[2].r_offset + 4;
10432 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10433 r[3].r_addend = r[0].r_addend + 16;
10434 }
c7131b65 10435 }
176a0d42
AM
10436 }
10437 }
794e51c0
AM
10438 if (ALWAYS_EMIT_R2SAVE
10439 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10440 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10441 if (plt_load_toc)
10442 {
10443 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10444 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10445 }
10446 else
10447 {
10448 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10449 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10450 }
b9e5796b
AM
10451 if (plt_load_toc
10452 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10453 {
71a39c98 10454 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10455 offset = 0;
10456 }
71a39c98 10457 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10458 if (plt_load_toc)
794e51c0 10459 {
b9e5796b
AM
10460 if (use_fake_dep)
10461 {
10462 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10463 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10464 }
10465 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10466 if (plt_static_chain)
10467 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10468 }
ac2df442
AM
10469 }
10470 else
10471 {
176a0d42
AM
10472 if (r != NULL)
10473 {
794e51c0
AM
10474 if (ALWAYS_EMIT_R2SAVE
10475 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10476 r[0].r_offset += 4;
176a0d42 10477 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10478 if (plt_load_toc)
176a0d42 10479 {
b9e5796b 10480 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10481 {
b9e5796b
AM
10482 r[1].r_offset = r[0].r_offset + 4;
10483 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10484 r[1].r_addend = r[0].r_addend;
10485 }
10486 else
10487 {
10488 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10489 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10490 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10491 if (plt_static_chain)
10492 {
10493 r[2].r_offset = r[1].r_offset + 4;
10494 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10495 r[2].r_addend = r[0].r_addend + 8;
10496 }
c7131b65 10497 }
176a0d42
AM
10498 }
10499 }
794e51c0
AM
10500 if (ALWAYS_EMIT_R2SAVE
10501 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10502 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10503 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10504 if (plt_load_toc
10505 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10506 {
10507 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10508 offset = 0;
10509 }
71a39c98 10510 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10511 if (plt_load_toc)
794e51c0 10512 {
b9e5796b
AM
10513 if (use_fake_dep)
10514 {
10515 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10516 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10517 }
10518 if (plt_static_chain)
10519 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10520 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10521 }
ac2df442 10522 }
b9e5796b 10523 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10524 {
10525 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10526 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10527 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10528 }
10529 else
407aa07c 10530 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10531 return p;
10532}
10533
a7f2871e
AM
10534/* Build a special .plt call stub for __tls_get_addr. */
10535
10536#define LD_R11_0R3 0xe9630000
10537#define LD_R12_0R3 0xe9830000
10538#define MR_R0_R3 0x7c601b78
10539#define CMPDI_R11_0 0x2c2b0000
10540#define ADD_R3_R12_R13 0x7c6c6a14
10541#define BEQLR 0x4d820020
10542#define MR_R3_R0 0x7c030378
a7f2871e
AM
10543#define STD_R11_0R1 0xf9610000
10544#define BCTRL 0x4e800421
10545#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10546#define MTLR_R11 0x7d6803a6
10547
10548static inline bfd_byte *
794e51c0
AM
10549build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10550 struct ppc_stub_hash_entry *stub_entry,
10551 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10552{
e7d1c40c 10553 bfd *obfd = htab->params->stub_bfd;
df136d64 10554 bfd_byte *loc = p;
794e51c0 10555
a7f2871e
AM
10556 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10557 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10558 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10559 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10560 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10561 bfd_put_32 (obfd, BEQLR, p), p += 4;
10562 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
10563 if (r != NULL)
10564 r[0].r_offset += 7 * 4;
e81b4c93 10565 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
f378ab09
AM
10566 return build_plt_stub (htab, stub_entry, p, offset, r);
10567
a7f2871e 10568 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10569 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10570
10571 if (r != NULL)
f378ab09 10572 r[0].r_offset += 2 * 4;
794e51c0 10573 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 10574 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 10575
a078d95a 10576 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10577 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10578 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10579 bfd_put_32 (obfd, BLR, p), p += 4;
10580
df136d64
AM
10581 if (htab->glink_eh_frame != NULL
10582 && htab->glink_eh_frame->size != 0)
10583 {
10584 bfd_byte *base, *eh;
10585 unsigned int lr_used, delta;
10586
10587 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10588 eh = base + stub_entry->group->eh_size;
10589 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10590 delta = lr_used - stub_entry->group->lr_restore;
10591 stub_entry->group->lr_restore = lr_used + 16;
10592 eh = eh_advance (htab->elf.dynobj, eh, delta);
10593 *eh++ = DW_CFA_offset_extended_sf;
10594 *eh++ = 65;
10595 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10596 *eh++ = DW_CFA_advance_loc + 4;
10597 *eh++ = DW_CFA_restore_extended;
10598 *eh++ = 65;
10599 stub_entry->group->eh_size = eh - base;
10600 }
a7f2871e
AM
10601 return p;
10602}
10603
176a0d42
AM
10604static Elf_Internal_Rela *
10605get_relocs (asection *sec, int count)
10606{
10607 Elf_Internal_Rela *relocs;
10608 struct bfd_elf_section_data *elfsec_data;
10609
10610 elfsec_data = elf_section_data (sec);
10611 relocs = elfsec_data->relocs;
10612 if (relocs == NULL)
10613 {
10614 bfd_size_type relsize;
10615 relsize = sec->reloc_count * sizeof (*relocs);
10616 relocs = bfd_alloc (sec->owner, relsize);
10617 if (relocs == NULL)
10618 return NULL;
10619 elfsec_data->relocs = relocs;
d4730f92
BS
10620 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10621 sizeof (Elf_Internal_Shdr));
10622 if (elfsec_data->rela.hdr == NULL)
10623 return NULL;
10624 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10625 * sizeof (Elf64_External_Rela));
10626 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10627 sec->reloc_count = 0;
10628 }
10629 relocs += sec->reloc_count;
10630 sec->reloc_count += count;
10631 return relocs;
10632}
10633
3d58e1fc
AM
10634/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10635 forms, to the equivalent relocs against the global symbol given by
10636 STUB_ENTRY->H. */
10637
10638static bfd_boolean
10639use_global_in_relocs (struct ppc_link_hash_table *htab,
10640 struct ppc_stub_hash_entry *stub_entry,
10641 Elf_Internal_Rela *r, unsigned int num_rel)
10642{
10643 struct elf_link_hash_entry **hashes;
10644 unsigned long symndx;
10645 struct ppc_link_hash_entry *h;
10646 bfd_vma symval;
10647
10648 /* Relocs are always against symbols in their own object file. Fake
10649 up global sym hashes for the stub bfd (which has no symbols). */
10650 hashes = elf_sym_hashes (htab->params->stub_bfd);
10651 if (hashes == NULL)
10652 {
10653 bfd_size_type hsize;
10654
10655 /* When called the first time, stub_globals will contain the
10656 total number of symbols seen during stub sizing. After
10657 allocating, stub_globals is used as an index to fill the
10658 hashes array. */
10659 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10660 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10661 if (hashes == NULL)
10662 return FALSE;
10663 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10664 htab->stub_globals = 1;
10665 }
10666 symndx = htab->stub_globals++;
10667 h = stub_entry->h;
10668 hashes[symndx] = &h->elf;
10669 if (h->oh != NULL && h->oh->is_func)
10670 h = ppc_follow_link (h->oh);
10671 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10672 || h->elf.root.type == bfd_link_hash_defweak);
10673 symval = (h->elf.root.u.def.value
10674 + h->elf.root.u.def.section->output_offset
10675 + h->elf.root.u.def.section->output_section->vma);
10676 while (num_rel-- != 0)
10677 {
10678 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10679 if (h->elf.root.u.def.section != stub_entry->target_section)
10680 {
10681 /* H is an opd symbol. The addend must be zero, and the
10682 branch reloc is the only one we can convert. */
10683 r->r_addend = 0;
10684 break;
10685 }
10686 else
10687 r->r_addend -= symval;
10688 --r;
10689 }
10690 return TRUE;
10691}
10692
aa374f67 10693static bfd_vma
25f53a85 10694get_r2off (struct bfd_link_info *info,
aa374f67
AM
10695 struct ppc_stub_hash_entry *stub_entry)
10696{
25f53a85 10697 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10698 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10699
10700 if (r2off == 0)
10701 {
10702 /* Support linking -R objects. Get the toc pointer from the
10703 opd entry. */
10704 char buf[8];
b9e5796b
AM
10705 if (!htab->opd_abi)
10706 return r2off;
aa374f67
AM
10707 asection *opd = stub_entry->h->elf.root.u.def.section;
10708 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10709
10710 if (strcmp (opd->name, ".opd") != 0
10711 || opd->reloc_count != 0)
10712 {
2cdcc330
AM
10713 info->callbacks->einfo
10714 (_("%P: cannot find opd entry toc for `%pT'\n"),
10715 stub_entry->h->elf.root.root.string);
aa374f67 10716 bfd_set_error (bfd_error_bad_value);
a7c49797 10717 return (bfd_vma) -1;
aa374f67
AM
10718 }
10719 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10720 return (bfd_vma) -1;
aa374f67 10721 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10722 r2off -= elf_gp (info->output_bfd);
aa374f67 10723 }
6f20ed8a 10724 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10725 return r2off;
10726}
10727
b34976b6 10728static bfd_boolean
4ce794b7 10729ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10730{
721956f4
AM
10731 struct ppc_stub_hash_entry *stub_entry;
10732 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10733 struct bfd_link_info *info;
10734 struct ppc_link_hash_table *htab;
721956f4 10735 bfd_byte *loc;
3d58e1fc 10736 bfd_byte *p, *relp;
1aa42141 10737 bfd_vma targ, off;
176a0d42 10738 Elf_Internal_Rela *r;
e054468f 10739 asection *plt;
3d58e1fc 10740 int num_rel;
5d1634d7 10741
721956f4
AM
10742 /* Massage our args to the form they really have. */
10743 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10744 info = in_arg;
5d1634d7 10745
5d1634d7 10746 htab = ppc_hash_table (info);
4dfe6ac6
NC
10747 if (htab == NULL)
10748 return FALSE;
5d1634d7 10749
1aa42141 10750 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 10751 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10752
4ce794b7 10753 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10754 switch (stub_entry->stub_type)
5d1634d7 10755 {
721956f4 10756 case ppc_stub_long_branch:
ad8e1ba5 10757 case ppc_stub_long_branch_r2off:
721956f4 10758 /* Branches are relative. This is where we are going to. */
1aa42141 10759 targ = (stub_entry->target_value
6911b7dc
AM
10760 + stub_entry->target_section->output_offset
10761 + stub_entry->target_section->output_section->vma);
1aa42141 10762 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 10763
721956f4 10764 /* And this is where we are coming from. */
1aa42141
AM
10765 off = (stub_entry->stub_offset
10766 + stub_entry->group->stub_sec->output_offset
10767 + stub_entry->group->stub_sec->output_section->vma);
10768 off = targ - off;
e86ce104 10769
9e390558 10770 p = loc;
ac2df442 10771 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10772 {
25f53a85 10773 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10774
a7c49797 10775 if (r2off == (bfd_vma) -1)
aa374f67
AM
10776 {
10777 htab->stub_error = TRUE;
10778 return FALSE;
10779 }
9e390558
AM
10780 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10781 p += 4;
ac2df442
AM
10782 if (PPC_HA (r2off) != 0)
10783 {
e7d1c40c 10784 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10785 ADDIS_R2_R2 | PPC_HA (r2off), p);
10786 p += 4;
a7c49797
AM
10787 }
10788 if (PPC_LO (r2off) != 0)
10789 {
10790 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10791 ADDI_R2_R2 | PPC_LO (r2off), p);
10792 p += 4;
ac2df442 10793 }
9e390558 10794 off -= p - loc;
ad8e1ba5 10795 }
9e390558
AM
10796 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10797 p += 4;
ad8e1ba5 10798
5c3dead3
AM
10799 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10800 {
cf97bcb0
AM
10801 _bfd_error_handler
10802 (_("long branch stub `%s' offset overflow"),
bc30df16 10803 stub_entry->root.string);
5c3dead3
AM
10804 htab->stub_error = TRUE;
10805 return FALSE;
10806 }
ee75fd95
AM
10807
10808 if (info->emitrelocations)
10809 {
6f20ed8a 10810 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
10811 if (r == NULL)
10812 return FALSE;
9e390558 10813 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 10814 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 10815 r->r_addend = targ;
3d58e1fc
AM
10816 if (stub_entry->h != NULL
10817 && !use_global_in_relocs (htab, stub_entry, r, 1))
10818 return FALSE;
ee75fd95 10819 }
721956f4 10820 break;
e86ce104 10821
721956f4 10822 case ppc_stub_plt_branch:
ad8e1ba5 10823 case ppc_stub_plt_branch_r2off:
721956f4
AM
10824 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10825 stub_entry->root.string + 9,
b34976b6 10826 FALSE, FALSE);
721956f4
AM
10827 if (br_entry == NULL)
10828 {
cf97bcb0
AM
10829 _bfd_error_handler (_("can't find branch stub `%s'"),
10830 stub_entry->root.string);
b34976b6
AM
10831 htab->stub_error = TRUE;
10832 return FALSE;
721956f4
AM
10833 }
10834
1aa42141 10835 targ = (stub_entry->target_value
176a0d42
AM
10836 + stub_entry->target_section->output_offset
10837 + stub_entry->target_section->output_section->vma);
6911b7dc 10838 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 10839 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10840
1aa42141 10841 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 10842 htab->brlt->contents + br_entry->offset);
721956f4 10843
f94498ff 10844 if (br_entry->iter == htab->stub_iteration)
721956f4 10845 {
f94498ff 10846 br_entry->iter = 0;
84f5d08e 10847
f94498ff 10848 if (htab->relbrlt != NULL)
84f5d08e 10849 {
f94498ff
AM
10850 /* Create a reloc for the branch lookup table entry. */
10851 Elf_Internal_Rela rela;
10852 bfd_byte *rl;
10853
10854 rela.r_offset = (br_entry->offset
10855 + htab->brlt->output_offset
10856 + htab->brlt->output_section->vma);
10857 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 10858 rela.r_addend = targ;
f94498ff
AM
10859
10860 rl = htab->relbrlt->contents;
10861 rl += (htab->relbrlt->reloc_count++
10862 * sizeof (Elf64_External_Rela));
10863 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10864 }
10865 else if (info->emitrelocations)
10866 {
176a0d42
AM
10867 r = get_relocs (htab->brlt, 1);
10868 if (r == NULL)
10869 return FALSE;
10870 /* brlt, being SEC_LINKER_CREATED does not go through the
10871 normal reloc processing. Symbols and offsets are not
10872 translated from input file to output file form, so
10873 set up the offset per the output file. */
f94498ff
AM
10874 r->r_offset = (br_entry->offset
10875 + htab->brlt->output_offset
10876 + htab->brlt->output_section->vma);
10877 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 10878 r->r_addend = targ;
84f5d08e 10879 }
84f5d08e 10880 }
721956f4 10881
1aa42141 10882 targ = (br_entry->offset
176a0d42
AM
10883 + htab->brlt->output_offset
10884 + htab->brlt->output_section->vma);
10885
1aa42141
AM
10886 off = (elf_gp (info->output_bfd)
10887 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10888 off = targ - off;
721956f4 10889
ad8e1ba5 10890 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 10891 {
25f53a85 10892 info->callbacks->einfo
c1c8c1ef 10893 (_("%P: linkage table error against `%pT'\n"),
721956f4 10894 stub_entry->root.string);
5d1634d7 10895 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10896 htab->stub_error = TRUE;
10897 return FALSE;
5d1634d7 10898 }
41bd81ab 10899
176a0d42
AM
10900 if (info->emitrelocations)
10901 {
6f20ed8a 10902 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
10903 if (r == NULL)
10904 return FALSE;
6f20ed8a 10905 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
10906 if (bfd_big_endian (info->output_bfd))
10907 r[0].r_offset += 2;
00f412ee 10908 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
10909 r[0].r_offset += 4;
10910 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 10911 r[0].r_addend = targ;
176a0d42
AM
10912 if (PPC_HA (off) != 0)
10913 {
10914 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10915 r[1].r_offset = r[0].r_offset + 4;
10916 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10917 r[1].r_addend = r[0].r_addend;
10918 }
10919 }
10920
9e390558 10921 p = loc;
00f412ee 10922 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 10923 {
176a0d42 10924 if (PPC_HA (off) != 0)
ac2df442 10925 {
e7d1c40c 10926 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10927 ADDIS_R12_R2 | PPC_HA (off), p);
10928 p += 4;
e7d1c40c 10929 bfd_put_32 (htab->params->stub_bfd,
9e390558 10930 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
10931 }
10932 else
9e390558
AM
10933 bfd_put_32 (htab->params->stub_bfd,
10934 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
10935 }
10936 else
10937 {
25f53a85 10938 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 10939
a7c49797 10940 if (r2off == (bfd_vma) -1)
aa374f67
AM
10941 {
10942 htab->stub_error = TRUE;
10943 return FALSE;
10944 }
ad8e1ba5 10945
9e390558
AM
10946 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10947 p += 4;
176a0d42 10948 if (PPC_HA (off) != 0)
ac2df442 10949 {
e7d1c40c 10950 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10951 ADDIS_R12_R2 | PPC_HA (off), p);
10952 p += 4;
e7d1c40c 10953 bfd_put_32 (htab->params->stub_bfd,
9e390558 10954 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
10955 }
10956 else
9e390558 10957 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
10958
10959 if (PPC_HA (r2off) != 0)
10960 {
9e390558 10961 p += 4;
e7d1c40c 10962 bfd_put_32 (htab->params->stub_bfd,
9e390558 10963 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
10964 }
10965 if (PPC_LO (r2off) != 0)
10966 {
9e390558 10967 p += 4;
e7d1c40c 10968 bfd_put_32 (htab->params->stub_bfd,
9e390558 10969 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 10970 }
ad8e1ba5 10971 }
9e390558
AM
10972 p += 4;
10973 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10974 p += 4;
407aa07c
AM
10975 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10976 p += 4;
721956f4 10977 break;
5d1634d7 10978
05d0e962
AM
10979 case ppc_stub_long_branch_notoc:
10980 case ppc_stub_long_branch_both:
10981 case ppc_stub_plt_branch_notoc:
10982 case ppc_stub_plt_branch_both:
10983 case ppc_stub_plt_call_notoc:
10984 case ppc_stub_plt_call_both:
10985 p = loc;
f891966f 10986 off = (stub_entry->stub_offset
05d0e962
AM
10987 + stub_entry->group->stub_sec->output_offset
10988 + stub_entry->group->stub_sec->output_section->vma);
10989 if (stub_entry->stub_type == ppc_stub_long_branch_both
10990 || stub_entry->stub_type == ppc_stub_plt_branch_both
10991 || stub_entry->stub_type == ppc_stub_plt_call_both)
10992 {
10993 off += 4;
10994 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10995 p += 4;
10996 }
10997 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10998 {
10999 targ = stub_entry->plt_ent->plt.offset & ~1;
11000 if (targ >= (bfd_vma) -2)
11001 abort ();
11002
11003 plt = htab->elf.splt;
11004 if (!htab->elf.dynamic_sections_created
11005 || stub_entry->h == NULL
11006 || stub_entry->h->elf.dynindx == -1)
11007 {
11008 if (stub_entry->symtype == STT_GNU_IFUNC)
11009 plt = htab->elf.iplt;
11010 else
11011 plt = htab->pltlocal;
11012 }
11013 targ += plt->output_offset + plt->output_section->vma;
11014 }
11015 else
11016 targ = (stub_entry->target_value
11017 + stub_entry->target_section->output_offset
11018 + stub_entry->target_section->output_section->vma);
11019 off = targ - off;
f891966f 11020
3d58e1fc
AM
11021 relp = p;
11022 num_rel = 0;
f891966f
AM
11023 /* The notoc stubs calculate their target (either a PLT entry or
11024 the global entry point of a function) relative to the PC
11025 returned by the "bcl" two instructions past the start of the
11026 sequence emitted by build_offset. The offset is therefore 8
11027 less than calculated from the start of the sequence. */
11028 off -= 8;
05d0e962
AM
11029 p = build_offset (htab->params->stub_bfd, p, off,
11030 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
f891966f 11031 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
05d0e962 11032 {
f891966f 11033 bfd_vma from;
3d58e1fc 11034 num_rel = 1;
f891966f
AM
11035 from = (stub_entry->stub_offset
11036 + stub_entry->group->stub_sec->output_offset
11037 + stub_entry->group->stub_sec->output_section->vma
11038 + (p - loc));
05d0e962 11039 bfd_put_32 (htab->params->stub_bfd,
f891966f 11040 B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
11041 }
11042 else
11043 {
11044 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11045 p += 4;
11046 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11047 }
11048 p += 4;
df136d64 11049
3d58e1fc
AM
11050 if (info->emitrelocations)
11051 {
11052 bfd_vma roff;
11053 num_rel += num_relocs_for_offset (off);
11054 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11055 if (r == NULL)
11056 return FALSE;
11057 roff = relp + 16 - stub_entry->group->stub_sec->contents;
11058 r = emit_relocs_for_offset (info, r, roff, targ, off);
11059 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11060 || stub_entry->stub_type == ppc_stub_long_branch_both)
11061 {
11062 ++r;
11063 roff = p - 4 - stub_entry->group->stub_sec->contents;
11064 r->r_offset = roff;
11065 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11066 r->r_addend = targ;
11067 if (stub_entry->h != NULL
11068 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11069 return FALSE;
11070 }
11071 }
11072
df136d64
AM
11073 if (htab->glink_eh_frame != NULL
11074 && htab->glink_eh_frame->size != 0)
11075 {
11076 bfd_byte *base, *eh;
11077 unsigned int lr_used, delta;
11078
11079 base = (htab->glink_eh_frame->contents
11080 + stub_entry->group->eh_base + 17);
11081 eh = base + stub_entry->group->eh_size;
11082 lr_used = stub_entry->stub_offset + 8;
11083 if (stub_entry->stub_type == ppc_stub_long_branch_both
11084 || stub_entry->stub_type == ppc_stub_plt_branch_both
11085 || stub_entry->stub_type == ppc_stub_plt_call_both)
11086 lr_used += 4;
11087 delta = lr_used - stub_entry->group->lr_restore;
11088 stub_entry->group->lr_restore = lr_used + 8;
11089 eh = eh_advance (htab->elf.dynobj, eh, delta);
11090 *eh++ = DW_CFA_register;
11091 *eh++ = 65;
11092 *eh++ = 12;
11093 *eh++ = DW_CFA_advance_loc + 2;
11094 *eh++ = DW_CFA_restore_extended;
11095 *eh++ = 65;
11096 stub_entry->group->eh_size = eh - base;
11097 }
05d0e962
AM
11098 break;
11099
721956f4 11100 case ppc_stub_plt_call:
794e51c0 11101 case ppc_stub_plt_call_r2save:
e054468f 11102 if (stub_entry->h != NULL
b31867b6
AM
11103 && stub_entry->h->is_func_descriptor
11104 && stub_entry->h->oh != NULL)
c862ae31 11105 {
b31867b6
AM
11106 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11107
11108 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11109 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11110 if (fh->elf.root.type == bfd_link_hash_undefined
11111 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11112 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11113 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11114 }
11115
721956f4 11116 /* Now build the stub. */
1aa42141
AM
11117 targ = stub_entry->plt_ent->plt.offset & ~1;
11118 if (targ >= (bfd_vma) -2)
721956f4
AM
11119 abort ();
11120
33e44f2e 11121 plt = htab->elf.splt;
25f23106
AM
11122 if (!htab->elf.dynamic_sections_created
11123 || stub_entry->h == NULL
11124 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11125 {
11126 if (stub_entry->symtype == STT_GNU_IFUNC)
11127 plt = htab->elf.iplt;
11128 else
11129 plt = htab->pltlocal;
11130 }
1aa42141 11131 targ += plt->output_offset + plt->output_section->vma;
e054468f 11132
1aa42141
AM
11133 off = (elf_gp (info->output_bfd)
11134 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11135 off = targ - off;
721956f4 11136
ad8e1ba5 11137 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11138 {
25f53a85 11139 info->callbacks->einfo
695344c0 11140 /* xgettext:c-format */
c1c8c1ef 11141 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11142 stub_entry->h != NULL
11143 ? stub_entry->h->elf.root.root.string
11144 : "<local sym>");
721956f4 11145 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11146 htab->stub_error = TRUE;
11147 return FALSE;
721956f4
AM
11148 }
11149
176a0d42
AM
11150 r = NULL;
11151 if (info->emitrelocations)
11152 {
6f20ed8a 11153 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11154 ((PPC_HA (off) != 0)
11155 + (htab->opd_abi
e7d1c40c 11156 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11157 && PPC_HA (off + 16) == PPC_HA (off))
11158 : 1)));
176a0d42
AM
11159 if (r == NULL)
11160 return FALSE;
6f20ed8a 11161 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11162 if (bfd_big_endian (info->output_bfd))
11163 r[0].r_offset += 2;
1aa42141 11164 r[0].r_addend = targ;
176a0d42 11165 }
a7f2871e
AM
11166 if (stub_entry->h != NULL
11167 && (stub_entry->h == htab->tls_get_addr_fd
11168 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11169 && htab->params->tls_get_addr_opt)
794e51c0 11170 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11171 else
794e51c0 11172 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11173 break;
11174
a4b6fadd
AM
11175 case ppc_stub_save_res:
11176 return TRUE;
11177
721956f4
AM
11178 default:
11179 BFD_FAIL ();
b34976b6 11180 return FALSE;
721956f4
AM
11181 }
11182
1aa42141 11183 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 11184
e7d1c40c 11185 if (htab->params->emit_stub_syms)
97b639ba
AM
11186 {
11187 struct elf_link_hash_entry *h;
ee75fd95
AM
11188 size_t len1, len2;
11189 char *name;
11190 const char *const stub_str[] = { "long_branch",
05d0e962
AM
11191 "long_branch",
11192 "long_branch",
3f6ff479
AM
11193 "long_branch",
11194 "plt_branch",
ee75fd95 11195 "plt_branch",
05d0e962
AM
11196 "plt_branch",
11197 "plt_branch",
11198 "plt_call",
11199 "plt_call",
794e51c0 11200 "plt_call",
ee75fd95
AM
11201 "plt_call" };
11202
11203 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11204 len2 = strlen (stub_entry->root.string);
11205 name = bfd_malloc (len1 + len2 + 2);
11206 if (name == NULL)
11207 return FALSE;
11208 memcpy (name, stub_entry->root.string, 9);
11209 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11210 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11211 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11212 if (h == NULL)
11213 return FALSE;
11214 if (h->root.type == bfd_link_hash_new)
11215 {
11216 h->root.type = bfd_link_hash_defined;
6f20ed8a 11217 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11218 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11219 h->ref_regular = 1;
11220 h->def_regular = 1;
11221 h->ref_regular_nonweak = 1;
11222 h->forced_local = 1;
11223 h->non_elf = 0;
2ec55de3 11224 h->root.linker_def = 1;
97b639ba
AM
11225 }
11226 }
11227
b34976b6 11228 return TRUE;
721956f4
AM
11229}
11230
11231/* As above, but don't actually build the stub. Just bump offset so
11232 we know stub section sizes, and select plt_branch stubs where
11233 long_branch stubs won't do. */
11234
b34976b6 11235static bfd_boolean
4ce794b7 11236ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11237{
11238 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11239 struct bfd_link_info *info;
721956f4 11240 struct ppc_link_hash_table *htab;
f891966f
AM
11241 asection *plt;
11242 bfd_vma targ, off, r2off;
11243 unsigned int size, extra, lr_used, delta;
721956f4
AM
11244
11245 /* Massage our args to the form they really have. */
11246 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11247 info = in_arg;
11248
11249 htab = ppc_hash_table (info);
4dfe6ac6
NC
11250 if (htab == NULL)
11251 return FALSE;
721956f4 11252
1aa42141
AM
11253 /* Make a note of the offset within the stubs for this entry. */
11254 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11255
a4b6fadd
AM
11256 if (stub_entry->h != NULL
11257 && stub_entry->h->save_res
11258 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11259 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11260 {
11261 /* Don't make stubs to out-of-line register save/restore
11262 functions. Instead, emit copies of the functions. */
11263 stub_entry->group->needs_save_res = 1;
11264 stub_entry->stub_type = ppc_stub_save_res;
11265 return TRUE;
11266 }
11267
f891966f 11268 switch (stub_entry->stub_type)
721956f4 11269 {
f891966f
AM
11270 case ppc_stub_plt_branch:
11271 case ppc_stub_plt_branch_r2off:
11272 /* Reset the stub type from the plt branch variant in case we now
11273 can reach with a shorter stub. */
11274 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11275 /* Fall through. */
11276 case ppc_stub_long_branch:
11277 case ppc_stub_long_branch_r2off:
1aa42141
AM
11278 targ = (stub_entry->target_value
11279 + stub_entry->target_section->output_offset
11280 + stub_entry->target_section->output_section->vma);
f891966f 11281 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
1aa42141
AM
11282 off = (stub_entry->stub_offset
11283 + stub_entry->group->stub_sec->output_offset
11284 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11285
ad8e1ba5 11286 size = 4;
f891966f 11287 r2off = 0;
ad8e1ba5
AM
11288 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11289 {
25f53a85 11290 r2off = get_r2off (info, stub_entry);
a7c49797 11291 if (r2off == (bfd_vma) -1)
aa374f67
AM
11292 {
11293 htab->stub_error = TRUE;
11294 return FALSE;
11295 }
a7c49797 11296 size = 8;
ac2df442 11297 if (PPC_HA (r2off) != 0)
a7c49797
AM
11298 size += 4;
11299 if (PPC_LO (r2off) != 0)
11300 size += 4;
1aa42141 11301 off += size - 4;
ad8e1ba5 11302 }
1aa42141 11303 off = targ - off;
ad8e1ba5 11304
f891966f
AM
11305 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11306 Do the same for -R objects without function descriptors. */
11307 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11308 && r2off == 0
11309 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11310 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 11311 {
f891966f 11312 struct ppc_branch_hash_entry *br_entry;
df136d64 11313
f891966f
AM
11314 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11315 stub_entry->root.string + 9,
11316 TRUE, FALSE);
11317 if (br_entry == NULL)
721956f4 11318 {
f891966f
AM
11319 _bfd_error_handler (_("can't build branch stub `%s'"),
11320 stub_entry->root.string);
11321 htab->stub_error = TRUE;
11322 return FALSE;
721956f4
AM
11323 }
11324
f891966f 11325 if (br_entry->iter != htab->stub_iteration)
721956f4 11326 {
f891966f
AM
11327 br_entry->iter = htab->stub_iteration;
11328 br_entry->offset = htab->brlt->size;
11329 htab->brlt->size += 8;
63bc6f6c 11330
f891966f
AM
11331 if (htab->relbrlt != NULL)
11332 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11333 else if (info->emitrelocations)
84f5d08e 11334 {
f891966f
AM
11335 htab->brlt->reloc_count += 1;
11336 htab->brlt->flags |= SEC_RELOC;
05d0e962 11337 }
f891966f 11338 }
ac2df442 11339
f891966f
AM
11340 targ = (br_entry->offset
11341 + htab->brlt->output_offset
11342 + htab->brlt->output_section->vma);
11343 off = (elf_gp (info->output_bfd)
11344 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11345 off = targ - off;
176a0d42 11346
f891966f
AM
11347 if (info->emitrelocations)
11348 {
11349 stub_entry->group->stub_sec->reloc_count
11350 += 1 + (PPC_HA (off) != 0);
11351 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11352 }
05d0e962 11353
f891966f
AM
11354 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11355 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11356 {
11357 size = 12;
11358 if (PPC_HA (off) != 0)
11359 size = 16;
ac2df442 11360 }
f891966f 11361 else
ac2df442 11362 {
f891966f
AM
11363 size = 16;
11364 if (PPC_HA (off) != 0)
11365 size += 4;
11366
11367 if (PPC_HA (r2off) != 0)
11368 size += 4;
11369 if (PPC_LO (r2off) != 0)
11370 size += 4;
ac2df442 11371 }
721956f4 11372 }
f891966f
AM
11373 else if (info->emitrelocations)
11374 {
11375 stub_entry->group->stub_sec->reloc_count += 1;
11376 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11377 }
11378 break;
11379
11380 case ppc_stub_plt_branch_notoc:
11381 case ppc_stub_plt_branch_both:
11382 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11383 /* Fall through. */
11384 case ppc_stub_long_branch_notoc:
11385 case ppc_stub_long_branch_both:
11386 off = (stub_entry->stub_offset
11387 + stub_entry->group->stub_sec->output_offset
11388 + stub_entry->group->stub_sec->output_section->vma);
11389 size = 0;
11390 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11391 size = 4;
11392 off += size;
11393 targ = (stub_entry->target_value
11394 + stub_entry->target_section->output_offset
11395 + stub_entry->target_section->output_section->vma);
11396 off = targ - off;
11397
3d58e1fc
AM
11398 if (info->emitrelocations)
11399 {
11400 stub_entry->group->stub_sec->reloc_count
11401 += num_relocs_for_offset (off);
11402 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11403 }
11404
f891966f
AM
11405 extra = size_offset (off - 8);
11406 /* Include branch insn plus those in the offset sequence. */
11407 size += 4 + extra;
11408 /* The branch insn is at the end, or "extra" bytes along. So
11409 its offset will be "extra" bytes less that that already
11410 calculated. */
11411 off -= extra;
11412
11413 /* After the bcl, lr has been modified so we need to emit
11414 .eh_frame info saying the return address is in r12. */
11415 lr_used = stub_entry->stub_offset + 8;
11416 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11417 lr_used += 4;
11418 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11419 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11420 DW_CFA_restore_extended 65. */
11421 delta = lr_used - stub_entry->group->lr_restore;
11422 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11423 stub_entry->group->lr_restore = lr_used + 8;
11424
11425 /* If the branch can't reach, use a plt_branch. */
11426 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11427 {
11428 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11429 - ppc_stub_long_branch_notoc);
11430 size += 4;
11431 }
3d58e1fc
AM
11432 else if (info->emitrelocations)
11433 stub_entry->group->stub_sec->reloc_count +=1;
f891966f
AM
11434 break;
11435
11436 case ppc_stub_plt_call_notoc:
11437 case ppc_stub_plt_call_both:
11438 off = (stub_entry->stub_offset
11439 + stub_entry->group->stub_sec->output_offset
11440 + stub_entry->group->stub_sec->output_section->vma);
11441 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11442 off += 4;
11443 targ = stub_entry->plt_ent->plt.offset & ~1;
11444 if (targ >= (bfd_vma) -2)
11445 abort ();
11446
11447 plt = htab->elf.splt;
11448 if (!htab->elf.dynamic_sections_created
11449 || stub_entry->h == NULL
11450 || stub_entry->h->elf.dynindx == -1)
11451 {
11452 if (stub_entry->symtype == STT_GNU_IFUNC)
11453 plt = htab->elf.iplt;
11454 else
11455 plt = htab->pltlocal;
11456 }
11457 targ += plt->output_offset + plt->output_section->vma;
11458 off = targ - off;
11459
11460 if (htab->params->plt_stub_align != 0)
11461 {
11462 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11463
11464 stub_entry->group->stub_sec->size += pad;
11465 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11466 off -= pad;
11467 }
11468
3d58e1fc
AM
11469 if (info->emitrelocations)
11470 {
11471 stub_entry->group->stub_sec->reloc_count
11472 += num_relocs_for_offset (off - 8);
11473 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11474 }
11475
f891966f
AM
11476 size = plt_stub_size (htab, stub_entry, off);
11477
11478 /* After the bcl, lr has been modified so we need to emit
11479 .eh_frame info saying the return address is in r12. */
11480 lr_used = stub_entry->stub_offset + 8;
11481 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11482 lr_used += 4;
11483 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11484 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11485 DW_CFA_restore_extended 65. */
11486 delta = lr_used - stub_entry->group->lr_restore;
11487 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11488 stub_entry->group->lr_restore = lr_used + 8;
11489 break;
11490
11491 case ppc_stub_plt_call:
11492 case ppc_stub_plt_call_r2save:
11493 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11494 if (targ >= (bfd_vma) -2)
11495 abort ();
11496 plt = htab->elf.splt;
11497 if (!htab->elf.dynamic_sections_created
11498 || stub_entry->h == NULL
11499 || stub_entry->h->elf.dynindx == -1)
11500 {
11501 if (stub_entry->symtype == STT_GNU_IFUNC)
11502 plt = htab->elf.iplt;
11503 else
11504 plt = htab->pltlocal;
11505 }
11506 targ += plt->output_offset + plt->output_section->vma;
11507
11508 off = (elf_gp (info->output_bfd)
11509 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11510 off = targ - off;
11511
11512 if (htab->params->plt_stub_align != 0)
11513 {
11514 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11515
11516 stub_entry->group->stub_sec->size += pad;
11517 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11518 }
11519
11520 if (info->emitrelocations)
11521 {
11522 stub_entry->group->stub_sec->reloc_count
11523 += ((PPC_HA (off) != 0)
11524 + (htab->opd_abi
11525 ? 2 + (htab->params->plt_static_chain
11526 && PPC_HA (off + 16) == PPC_HA (off))
11527 : 1));
11528 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11529 }
11530
11531 size = plt_stub_size (htab, stub_entry, off);
11532
11533 if (stub_entry->h != NULL
11534 && (stub_entry->h == htab->tls_get_addr_fd
11535 || stub_entry->h == htab->tls_get_addr)
11536 && htab->params->tls_get_addr_opt
11537 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11538 {
11539 /* After the bctrl, lr has been modified so we need to
11540 emit .eh_frame info saying the return address is
11541 on the stack. In fact we put the EH info specifying
11542 that the return address is on the stack *at* the
11543 call rather than after it, because the EH info for a
11544 call needs to be specified by that point.
11545 See libgcc/unwind-dw2.c execute_cfa_program. */
11546 lr_used = stub_entry->stub_offset + size - 20;
11547 /* The eh_frame info will consist of a DW_CFA_advance_loc
11548 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11549 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11550 delta = lr_used - stub_entry->group->lr_restore;
11551 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11552 stub_entry->group->lr_restore = size - 4;
11553 }
11554 break;
11555
11556 default:
11557 BFD_FAIL ();
11558 return FALSE;
721956f4
AM
11559 }
11560
6f20ed8a 11561 stub_entry->group->stub_sec->size += size;
b34976b6 11562 return TRUE;
721956f4
AM
11563}
11564
11565/* Set up various things so that we can make a list of input sections
11566 for each output section included in the link. Returns -1 on error,
cedb70c5 11567 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11568
11569int
e7d1c40c 11570ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11571{
6f20ed8a 11572 unsigned int id;
721956f4
AM
11573 bfd_size_type amt;
11574 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11575
4dfe6ac6
NC
11576 if (htab == NULL)
11577 return -1;
4c52953f 11578
7cf7fcc8 11579 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
11580 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11581 htab->sec_info = bfd_zmalloc (amt);
11582 if (htab->sec_info == NULL)
721956f4
AM
11583 return -1;
11584
3d6f9012
AM
11585 /* Set toc_off for com, und, abs and ind sections. */
11586 for (id = 0; id < 3; id++)
6f20ed8a 11587 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11588
721956f4
AM
11589 return 1;
11590}
11591
927be08e
AM
11592/* Set up for first pass at multitoc partitioning. */
11593
11594void
11595ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11596{
11597 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11598
1c865ab2 11599 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11600 htab->toc_bfd = NULL;
11601 htab->toc_first_sec = NULL;
11602}
11603
e717da7e
AM
11604/* The linker repeatedly calls this function for each TOC input section
11605 and linker generated GOT section. Group input bfds such that the toc
927be08e 11606 within a group is less than 64k in size. */
ad8e1ba5 11607
927be08e 11608bfd_boolean
4ce794b7 11609ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11610{
11611 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11612 bfd_vma addr, off, limit;
ad8e1ba5 11613
4dfe6ac6
NC
11614 if (htab == NULL)
11615 return FALSE;
11616
927be08e 11617 if (!htab->second_toc_pass)
4c52953f 11618 {
927be08e 11619 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11620 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11621
11622 if (new_bfd)
bf102f86
AM
11623 {
11624 htab->toc_bfd = isec->owner;
11625 htab->toc_first_sec = isec;
11626 }
927be08e 11627
bf102f86
AM
11628 addr = isec->output_offset + isec->output_section->vma;
11629 off = addr - htab->toc_curr;
d77c8a4b
AM
11630 limit = 0x80008000;
11631 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11632 limit = 0x10000;
11633 if (off + isec->size > limit)
bf102f86
AM
11634 {
11635 addr = (htab->toc_first_sec->output_offset
11636 + htab->toc_first_sec->output_section->vma);
11637 htab->toc_curr = addr;
a27e685f 11638 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11639 }
99877b66 11640
927be08e
AM
11641 /* toc_curr is the base address of this toc group. Set elf_gp
11642 for the input section to be the offset relative to the
11643 output toc base plus 0x8000. Making the input elf_gp an
11644 offset allows us to move the toc as a whole without
11645 recalculating input elf_gp. */
06bcf541 11646 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
11647 off += TOC_BASE_OFF;
11648
11649 /* Die if someone uses a linker script that doesn't keep input
11650 file .toc and .got together. */
a4fd3de5
AM
11651 if (new_bfd
11652 && elf_gp (isec->owner) != 0
927be08e
AM
11653 && elf_gp (isec->owner) != off)
11654 return FALSE;
11655
11656 elf_gp (isec->owner) = off;
11657 return TRUE;
4c52953f 11658 }
927be08e
AM
11659
11660 /* During the second pass toc_first_sec points to the start of
11661 a toc group, and toc_curr is used to track the old elf_gp.
11662 We use toc_bfd to ensure we only look at each bfd once. */
11663 if (htab->toc_bfd == isec->owner)
11664 return TRUE;
11665 htab->toc_bfd = isec->owner;
11666
11667 if (htab->toc_first_sec == NULL
11668 || htab->toc_curr != elf_gp (isec->owner))
11669 {
11670 htab->toc_curr = elf_gp (isec->owner);
11671 htab->toc_first_sec = isec;
11672 }
11673 addr = (htab->toc_first_sec->output_offset
11674 + htab->toc_first_sec->output_section->vma);
06bcf541 11675 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
11676 elf_gp (isec->owner) = off;
11677
11678 return TRUE;
ad8e1ba5
AM
11679}
11680
927be08e
AM
11681/* Called via elf_link_hash_traverse to merge GOT entries for global
11682 symbol H. */
11683
11684static bfd_boolean
11685merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11686{
11687 if (h->root.type == bfd_link_hash_indirect)
11688 return TRUE;
11689
927be08e
AM
11690 merge_got_entries (&h->got.glist);
11691
11692 return TRUE;
11693}
11694
11695/* Called via elf_link_hash_traverse to allocate GOT entries for global
11696 symbol H. */
11697
11698static bfd_boolean
11699reallocate_got (struct elf_link_hash_entry *h, void *inf)
11700{
11701 struct got_entry *gent;
11702
11703 if (h->root.type == bfd_link_hash_indirect)
11704 return TRUE;
11705
927be08e
AM
11706 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11707 if (!gent->is_indirect)
11708 allocate_got (h, (struct bfd_link_info *) inf, gent);
11709 return TRUE;
11710}
11711
11712/* Called on the first multitoc pass after the last call to
11713 ppc64_elf_next_toc_section. This function removes duplicate GOT
11714 entries. */
11715
11716bfd_boolean
11717ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11718{
11719 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11720 struct bfd *ibfd, *ibfd2;
11721 bfd_boolean done_something;
11722
11723 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11724
7865406b
AM
11725 if (!htab->do_multi_toc)
11726 return FALSE;
11727
d0fae19d 11728 /* Merge global sym got entries within a toc group. */
927be08e
AM
11729 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11730
11731 /* And tlsld_got. */
c72f2fb2 11732 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11733 {
11734 struct got_entry *ent, *ent2;
11735
11736 if (!is_ppc64_elf (ibfd))
11737 continue;
11738
11739 ent = ppc64_tlsld_got (ibfd);
11740 if (!ent->is_indirect
11741 && ent->got.offset != (bfd_vma) -1)
11742 {
c72f2fb2 11743 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11744 {
11745 if (!is_ppc64_elf (ibfd2))
11746 continue;
11747
11748 ent2 = ppc64_tlsld_got (ibfd2);
11749 if (!ent2->is_indirect
11750 && ent2->got.offset != (bfd_vma) -1
11751 && elf_gp (ibfd2) == elf_gp (ibfd))
11752 {
11753 ent2->is_indirect = TRUE;
11754 ent2->got.ent = ent;
11755 }
11756 }
11757 }
11758 }
11759
11760 /* Zap sizes of got sections. */
33e44f2e
AM
11761 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11762 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11763 htab->got_reli_size = 0;
11764
c72f2fb2 11765 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11766 {
11767 asection *got, *relgot;
11768
11769 if (!is_ppc64_elf (ibfd))
11770 continue;
11771
11772 got = ppc64_elf_tdata (ibfd)->got;
11773 if (got != NULL)
11774 {
11775 got->rawsize = got->size;
11776 got->size = 0;
11777 relgot = ppc64_elf_tdata (ibfd)->relgot;
11778 relgot->rawsize = relgot->size;
11779 relgot->size = 0;
11780 }
11781 }
11782
11783 /* Now reallocate the got, local syms first. We don't need to
11784 allocate section contents again since we never increase size. */
c72f2fb2 11785 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11786 {
11787 struct got_entry **lgot_ents;
11788 struct got_entry **end_lgot_ents;
11789 struct plt_entry **local_plt;
11790 struct plt_entry **end_local_plt;
f961d9dd 11791 unsigned char *lgot_masks;
927be08e
AM
11792 bfd_size_type locsymcount;
11793 Elf_Internal_Shdr *symtab_hdr;
19e08130 11794 asection *s;
927be08e
AM
11795
11796 if (!is_ppc64_elf (ibfd))
11797 continue;
11798
11799 lgot_ents = elf_local_got_ents (ibfd);
11800 if (!lgot_ents)
11801 continue;
11802
11803 symtab_hdr = &elf_symtab_hdr (ibfd);
11804 locsymcount = symtab_hdr->sh_info;
11805 end_lgot_ents = lgot_ents + locsymcount;
11806 local_plt = (struct plt_entry **) end_lgot_ents;
11807 end_local_plt = local_plt + locsymcount;
f961d9dd 11808 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11809 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11810 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11811 {
11812 struct got_entry *ent;
11813
11814 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11815 {
19e08130
AM
11816 unsigned int ent_size = 8;
11817 unsigned int rel_size = sizeof (Elf64_External_Rela);
11818
d0fae19d
AM
11819 ent->got.offset = s->size;
11820 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11821 {
19e08130
AM
11822 ent_size *= 2;
11823 rel_size *= 2;
11824 }
11825 s->size += ent_size;
37da22e5 11826 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 11827 {
33e44f2e 11828 htab->elf.irelplt->size += rel_size;
19e08130
AM
11829 htab->got_reli_size += rel_size;
11830 }
6a3858a6
AM
11831 else if (bfd_link_pic (info)
11832 && !((ent->tls_type & TLS_TPREL) != 0
11833 && bfd_link_executable (info)))
19e08130
AM
11834 {
11835 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11836 srel->size += rel_size;
d0fae19d
AM
11837 }
11838 }
927be08e
AM
11839 }
11840 }
11841
11842 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11843
c72f2fb2 11844 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11845 {
11846 struct got_entry *ent;
11847
11848 if (!is_ppc64_elf (ibfd))
11849 continue;
11850
11851 ent = ppc64_tlsld_got (ibfd);
11852 if (!ent->is_indirect
11853 && ent->got.offset != (bfd_vma) -1)
11854 {
11855 asection *s = ppc64_elf_tdata (ibfd)->got;
11856 ent->got.offset = s->size;
11857 s->size += 16;
0e1862bb 11858 if (bfd_link_pic (info))
927be08e
AM
11859 {
11860 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11861 srel->size += sizeof (Elf64_External_Rela);
11862 }
11863 }
11864 }
11865
33e44f2e 11866 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11867 if (!done_something)
c72f2fb2 11868 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11869 {
11870 asection *got;
11871
11872 if (!is_ppc64_elf (ibfd))
11873 continue;
11874
11875 got = ppc64_elf_tdata (ibfd)->got;
11876 if (got != NULL)
11877 {
11878 done_something = got->rawsize != got->size;
11879 if (done_something)
11880 break;
11881 }
11882 }
11883
11884 if (done_something)
e7d1c40c 11885 (*htab->params->layout_sections_again) ();
927be08e
AM
11886
11887 /* Set up for second pass over toc sections to recalculate elf_gp
11888 on input sections. */
11889 htab->toc_bfd = NULL;
11890 htab->toc_first_sec = NULL;
11891 htab->second_toc_pass = TRUE;
11892 return done_something;
11893}
11894
11895/* Called after second pass of multitoc partitioning. */
11896
11897void
11898ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11899{
11900 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11901
11902 /* After the second pass, toc_curr tracks the TOC offset used
11903 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11904 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11905}
11906
9b5ecbd0
AM
11907/* No toc references were found in ISEC. If the code in ISEC makes no
11908 calls, then there's no need to use toc adjusting stubs when branching
11909 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11910 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11911 needed, and 2 if a cyclical call-graph was found but no other reason
11912 for a stub was detected. If called from the top level, a return of
11913 2 means the same as a return of 0. */
9b5ecbd0
AM
11914
11915static int
4ce794b7 11916toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11917{
9b5ecbd0 11918 int ret;
70cc837d
AM
11919
11920 /* Mark this section as checked. */
11921 isec->call_check_done = 1;
9b5ecbd0 11922
772119ce
AM
11923 /* We know none of our code bearing sections will need toc stubs. */
11924 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11925 return 0;
11926
eea6121a 11927 if (isec->size == 0)
082c50f8
AM
11928 return 0;
11929
4c52953f
AM
11930 if (isec->output_section == NULL)
11931 return 0;
11932
4c52953f 11933 ret = 0;
70cc837d 11934 if (isec->reloc_count != 0)
9b5ecbd0 11935 {
70cc837d
AM
11936 Elf_Internal_Rela *relstart, *rel;
11937 Elf_Internal_Sym *local_syms;
11938 struct ppc_link_hash_table *htab;
2917689a 11939
70cc837d
AM
11940 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11941 info->keep_memory);
11942 if (relstart == NULL)
11943 return -1;
90aecf7a 11944
70cc837d
AM
11945 /* Look for branches to outside of this section. */
11946 local_syms = NULL;
11947 htab = ppc_hash_table (info);
11948 if (htab == NULL)
11949 return -1;
4c52953f 11950
70cc837d 11951 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11952 {
70cc837d
AM
11953 enum elf_ppc64_reloc_type r_type;
11954 unsigned long r_symndx;
11955 struct elf_link_hash_entry *h;
11956 struct ppc_link_hash_entry *eh;
11957 Elf_Internal_Sym *sym;
11958 asection *sym_sec;
11959 struct _opd_sec_data *opd;
11960 bfd_vma sym_value;
11961 bfd_vma dest;
11962
11963 r_type = ELF64_R_TYPE (rel->r_info);
11964 if (r_type != R_PPC64_REL24
05d0e962 11965 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
11966 && r_type != R_PPC64_REL14
11967 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 11968 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
11969 && r_type != R_PPC64_PLTCALL
11970 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 11971 continue;
4c52953f 11972
70cc837d
AM
11973 r_symndx = ELF64_R_SYM (rel->r_info);
11974 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11975 isec->owner))
4c52953f 11976 {
70cc837d
AM
11977 ret = -1;
11978 break;
11979 }
4c52953f 11980
70cc837d
AM
11981 /* Calls to dynamic lib functions go through a plt call stub
11982 that uses r2. */
11983 eh = (struct ppc_link_hash_entry *) h;
11984 if (eh != NULL
11985 && (eh->elf.plt.plist != NULL
11986 || (eh->oh != NULL
11987 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11988 {
11989 ret = 1;
11990 break;
4c52953f
AM
11991 }
11992
70cc837d
AM
11993 if (sym_sec == NULL)
11994 /* Ignore other undefined symbols. */
4c52953f 11995 continue;
4c52953f 11996
70cc837d
AM
11997 /* Assume branches to other sections not included in the
11998 link need stubs too, to cover -R and absolute syms. */
11999 if (sym_sec->output_section == NULL)
12000 {
12001 ret = 1;
12002 break;
12003 }
4c52953f 12004
70cc837d
AM
12005 if (h == NULL)
12006 sym_value = sym->st_value;
12007 else
12008 {
12009 if (h->root.type != bfd_link_hash_defined
12010 && h->root.type != bfd_link_hash_defweak)
12011 abort ();
12012 sym_value = h->root.u.def.value;
12013 }
12014 sym_value += rel->r_addend;
4c52953f 12015
70cc837d
AM
12016 /* If this branch reloc uses an opd sym, find the code section. */
12017 opd = get_opd_info (sym_sec);
12018 if (opd != NULL)
12019 {
12020 if (h == NULL && opd->adjust != NULL)
12021 {
12022 long adjust;
4c52953f 12023
92a9c616 12024 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12025 if (adjust == -1)
12026 /* Assume deleted functions won't ever be called. */
12027 continue;
12028 sym_value += adjust;
12029 }
4c52953f 12030
aef36ac1
AM
12031 dest = opd_entry_value (sym_sec, sym_value,
12032 &sym_sec, NULL, FALSE);
70cc837d
AM
12033 if (dest == (bfd_vma) -1)
12034 continue;
12035 }
12036 else
12037 dest = (sym_value
12038 + sym_sec->output_offset
12039 + sym_sec->output_section->vma);
4c52953f 12040
70cc837d
AM
12041 /* Ignore branch to self. */
12042 if (sym_sec == isec)
12043 continue;
4c52953f 12044
70cc837d
AM
12045 /* If the called function uses the toc, we need a stub. */
12046 if (sym_sec->has_toc_reloc
12047 || sym_sec->makes_toc_func_call)
4c52953f 12048 {
70cc837d 12049 ret = 1;
4c52953f
AM
12050 break;
12051 }
70cc837d
AM
12052
12053 /* Assume any branch that needs a long branch stub might in fact
12054 need a plt_branch stub. A plt_branch stub uses r2. */
12055 else if (dest - (isec->output_offset
12056 + isec->output_section->vma
6911b7dc
AM
12057 + rel->r_offset) + (1 << 25)
12058 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12059 ? h->other
12060 : sym->st_other))
4c52953f 12061 {
70cc837d
AM
12062 ret = 1;
12063 break;
12064 }
12065
12066 /* If calling back to a section in the process of being
12067 tested, we can't say for sure that no toc adjusting stubs
12068 are needed, so don't return zero. */
12069 else if (sym_sec->call_check_in_progress)
12070 ret = 2;
12071
12072 /* Branches to another section that itself doesn't have any TOC
12073 references are OK. Recursively call ourselves to check. */
12074 else if (!sym_sec->call_check_done)
12075 {
12076 int recur;
12077
12078 /* Mark current section as indeterminate, so that other
12079 sections that call back to current won't be marked as
12080 known. */
12081 isec->call_check_in_progress = 1;
12082 recur = toc_adjusting_stub_needed (info, sym_sec);
12083 isec->call_check_in_progress = 0;
12084
4c52953f
AM
12085 if (recur != 0)
12086 {
70cc837d
AM
12087 ret = recur;
12088 if (recur != 2)
12089 break;
4c52953f
AM
12090 }
12091 }
4c52953f 12092 }
70cc837d
AM
12093
12094 if (local_syms != NULL
12095 && (elf_symtab_hdr (isec->owner).contents
12096 != (unsigned char *) local_syms))
12097 free (local_syms);
12098 if (elf_section_data (isec)->relocs != relstart)
12099 free (relstart);
9b5ecbd0
AM
12100 }
12101
70cc837d
AM
12102 if ((ret & 1) == 0
12103 && isec->map_head.s != NULL
12104 && (strcmp (isec->output_section->name, ".init") == 0
12105 || strcmp (isec->output_section->name, ".fini") == 0))
12106 {
12107 if (isec->map_head.s->has_toc_reloc
12108 || isec->map_head.s->makes_toc_func_call)
12109 ret = 1;
12110 else if (!isec->map_head.s->call_check_done)
12111 {
12112 int recur;
12113 isec->call_check_in_progress = 1;
12114 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12115 isec->call_check_in_progress = 0;
12116 if (recur != 0)
12117 ret = recur;
12118 }
12119 }
12120
12121 if (ret == 1)
12122 isec->makes_toc_func_call = 1;
4c52953f 12123
9b5ecbd0
AM
12124 return ret;
12125}
12126
721956f4
AM
12127/* The linker repeatedly calls this function for each input section,
12128 in the order that input sections are linked into output sections.
12129 Build lists of input sections to determine groupings between which
12130 we may insert linker stubs. */
12131
9b5ecbd0 12132bfd_boolean
4ce794b7 12133ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12134{
12135 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12136
4dfe6ac6
NC
12137 if (htab == NULL)
12138 return FALSE;
12139
734b6cf9 12140 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12141 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12142 {
3d6f9012
AM
12143 /* This happens to make the list in reverse order,
12144 which is what we want. */
6f20ed8a
AM
12145 htab->sec_info[isec->id].u.list
12146 = htab->sec_info[isec->output_section->id].u.list;
12147 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12148 }
ad8e1ba5 12149
4c52953f 12150 if (htab->multi_toc_needed)
9b5ecbd0 12151 {
8b974ba3
AM
12152 /* Analyse sections that aren't already flagged as needing a
12153 valid toc pointer. Exclude .fixup for the linux kernel.
12154 .fixup contains branches, but only back to the function that
12155 hit an exception. */
12156 if (!(isec->has_toc_reloc
12157 || (isec->flags & SEC_CODE) == 0
12158 || strcmp (isec->name, ".fixup") == 0
12159 || isec->call_check_done))
12160 {
12161 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12162 return FALSE;
8b974ba3
AM
12163 }
12164 /* Make all sections use the TOC assigned for this object file.
12165 This will be wrong for pasted sections; We fix that in
12166 check_pasted_section(). */
12167 if (elf_gp (isec->owner) != 0)
12168 htab->toc_curr = elf_gp (isec->owner);
12169 }
12170
6f20ed8a 12171 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12172 return TRUE;
721956f4
AM
12173}
12174
70cc837d
AM
12175/* Check that all .init and .fini sections use the same toc, if they
12176 have toc relocs. */
12177
12178static bfd_boolean
12179check_pasted_section (struct bfd_link_info *info, const char *name)
12180{
12181 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12182
12183 if (o != NULL)
12184 {
12185 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12186 bfd_vma toc_off = 0;
12187 asection *i;
12188
12189 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12190 if (i->has_toc_reloc)
12191 {
12192 if (toc_off == 0)
6f20ed8a
AM
12193 toc_off = htab->sec_info[i->id].toc_off;
12194 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12195 return FALSE;
12196 }
6683a28d
AM
12197
12198 if (toc_off == 0)
12199 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12200 if (i->makes_toc_func_call)
12201 {
6f20ed8a 12202 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12203 break;
12204 }
12205
70cc837d
AM
12206 /* Make sure the whole pasted function uses the same toc offset. */
12207 if (toc_off != 0)
12208 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12209 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12210 }
12211 return TRUE;
12212}
12213
12214bfd_boolean
12215ppc64_elf_check_init_fini (struct bfd_link_info *info)
12216{
12217 return (check_pasted_section (info, ".init")
12218 & check_pasted_section (info, ".fini"));
12219}
12220
721956f4
AM
12221/* See whether we can group stub sections together. Grouping stub
12222 sections may result in fewer stubs. More importantly, we need to
12223 put all .init* and .fini* stubs at the beginning of the .init or
12224 .fini output sections respectively, because glibc splits the
12225 _init and _fini functions into multiple parts. Putting a stub in
12226 the middle of a function is not a good idea. */
12227
6f20ed8a
AM
12228static bfd_boolean
12229group_sections (struct bfd_link_info *info,
4ce794b7
AM
12230 bfd_size_type stub_group_size,
12231 bfd_boolean stubs_always_before_branch)
721956f4 12232{
6f20ed8a
AM
12233 struct ppc_link_hash_table *htab;
12234 asection *osec;
7c8fe5c4
AM
12235 bfd_boolean suppress_size_errors;
12236
6f20ed8a
AM
12237 htab = ppc_hash_table (info);
12238 if (htab == NULL)
12239 return FALSE;
12240
7c8fe5c4 12241 suppress_size_errors = FALSE;
7c8fe5c4
AM
12242 if (stub_group_size == 1)
12243 {
12244 /* Default values. */
12245 if (stubs_always_before_branch)
09f92717 12246 stub_group_size = 0x1e00000;
7c8fe5c4 12247 else
09f92717 12248 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12249 suppress_size_errors = TRUE;
12250 }
12251
6f20ed8a 12252 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12253 {
6f20ed8a
AM
12254 asection *tail;
12255
12256 if (osec->id >= htab->sec_info_arr_size)
12257 continue;
12258
12259 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12260 while (tail != NULL)
721956f4 12261 {
734b6cf9
AM
12262 asection *curr;
12263 asection *prev;
12264 bfd_size_type total;
12265 bfd_boolean big_sec;
12266 bfd_vma curr_toc;
6f20ed8a 12267 struct map_stub *group;
09f92717 12268 bfd_size_type group_size;
734b6cf9
AM
12269
12270 curr = tail;
eea6121a 12271 total = tail->size;
09f92717
AM
12272 group_size = (ppc64_elf_section_data (tail) != NULL
12273 && ppc64_elf_section_data (tail)->has_14bit_branch
12274 ? stub_group_size >> 10 : stub_group_size);
12275
12276 big_sec = total > group_size;
7c8fe5c4 12277 if (big_sec && !suppress_size_errors)
695344c0 12278 /* xgettext:c-format */
871b3ab2 12279 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 12280 tail->owner, tail);
6f20ed8a 12281 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12282
6f20ed8a 12283 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12284 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12285 < (ppc64_elf_section_data (prev) != NULL
12286 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12287 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12288 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12289 curr = prev;
12290
12291 /* OK, the size from the start of CURR to the end is less
09f92717 12292 than group_size and thus can be handled by one stub
734b6cf9 12293 section. (or the tail section is itself larger than
09f92717
AM
12294 group_size, in which case we may be toast.) We should
12295 really be keeping track of the total size of stubs added
12296 here, as stubs contribute to the final output section
12297 size. That's a little tricky, and this way will only
12298 break if stubs added make the total size more than 2^25,
12299 ie. for the default stub_group_size, if stubs total more
12300 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12301 group = bfd_alloc (curr->owner, sizeof (*group));
12302 if (group == NULL)
12303 return FALSE;
12304 group->link_sec = curr;
12305 group->stub_sec = NULL;
a4b6fadd 12306 group->needs_save_res = 0;
df136d64
AM
12307 group->lr_restore = 0;
12308 group->eh_size = 0;
12309 group->eh_base = 0;
a4b6fadd
AM
12310 group->next = htab->group;
12311 htab->group = group;
734b6cf9 12312 do
721956f4 12313 {
6f20ed8a 12314 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12315 /* Set up this stub group. */
6f20ed8a 12316 htab->sec_info[tail->id].u.group = group;
721956f4 12317 }
734b6cf9
AM
12318 while (tail != curr && (tail = prev) != NULL);
12319
09f92717 12320 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12321 bytes before the stub section can be handled by it too.
12322 Don't do this if we have a really large section after the
12323 stubs, as adding more stubs increases the chance that
12324 branches may not reach into the stub section. */
12325 if (!stubs_always_before_branch && !big_sec)
12326 {
12327 total = 0;
12328 while (prev != NULL
12329 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12330 < (ppc64_elf_section_data (prev) != NULL
12331 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
12332 ? (group_size = stub_group_size >> 10)
12333 : group_size))
6f20ed8a 12334 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12335 {
12336 tail = prev;
6f20ed8a
AM
12337 prev = htab->sec_info[tail->id].u.list;
12338 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12339 }
12340 }
12341 tail = prev;
721956f4
AM
12342 }
12343 }
6f20ed8a 12344 return TRUE;
721956f4
AM
12345}
12346
58d180e8
AM
12347static const unsigned char glink_eh_frame_cie[] =
12348{
12349 0, 0, 0, 16, /* length. */
12350 0, 0, 0, 0, /* id. */
12351 1, /* CIE version. */
12352 'z', 'R', 0, /* Augmentation string. */
12353 4, /* Code alignment. */
12354 0x78, /* Data alignment. */
12355 65, /* RA reg. */
12356 1, /* Augmentation size. */
12357 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12358 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12359};
12360
d969d15f
AM
12361/* Stripping output sections is normally done before dynamic section
12362 symbols have been allocated. This function is called later, and
12363 handles cases like htab->brlt which is mapped to its own output
12364 section. */
12365
12366static void
12367maybe_strip_output (struct bfd_link_info *info, asection *isec)
12368{
12369 if (isec->size == 0
12370 && isec->output_section->size == 0
53d8967a 12371 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12372 && !bfd_section_removed_from_list (info->output_bfd,
12373 isec->output_section)
12374 && elf_section_data (isec->output_section)->dynindx == 0)
12375 {
12376 isec->output_section->flags |= SEC_EXCLUDE;
12377 bfd_section_list_remove (info->output_bfd, isec->output_section);
12378 info->output_bfd->section_count--;
12379 }
12380}
12381
721956f4
AM
12382/* Determine and set the size of the stub section for a final link.
12383
12384 The basic idea here is to examine all the relocations looking for
12385 PC-relative calls to a target that is unreachable with a "bl"
12386 instruction. */
12387
b34976b6 12388bfd_boolean
e7d1c40c 12389ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12390{
12391 bfd_size_type stub_group_size;
b34976b6 12392 bfd_boolean stubs_always_before_branch;
721956f4
AM
12393 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12394
4dfe6ac6
NC
12395 if (htab == NULL)
12396 return FALSE;
12397
0e1862bb 12398 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12399 htab->params->plt_thread_safe = 1;
b9e5796b 12400 if (!htab->opd_abi)
e7d1c40c
AM
12401 htab->params->plt_thread_safe = 0;
12402 else if (htab->params->plt_thread_safe == -1)
794e51c0 12403 {
e2458743 12404 static const char *const thread_starter[] =
794e51c0
AM
12405 {
12406 "pthread_create",
12407 /* libstdc++ */
12408 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12409 /* librt */
12410 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12411 "mq_notify", "create_timer",
12412 /* libanl */
12413 "getaddrinfo_a",
12414 /* libgomp */
2300b5a1 12415 "GOMP_parallel",
794e51c0 12416 "GOMP_parallel_start",
2300b5a1 12417 "GOMP_parallel_loop_static",
794e51c0 12418 "GOMP_parallel_loop_static_start",
2300b5a1 12419 "GOMP_parallel_loop_dynamic",
794e51c0 12420 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12421 "GOMP_parallel_loop_guided",
794e51c0 12422 "GOMP_parallel_loop_guided_start",
2300b5a1 12423 "GOMP_parallel_loop_runtime",
794e51c0 12424 "GOMP_parallel_loop_runtime_start",
2300b5a1 12425 "GOMP_parallel_sections",
68ffbac6 12426 "GOMP_parallel_sections_start",
f9dffbf0
AM
12427 /* libgo */
12428 "__go_go",
794e51c0
AM
12429 };
12430 unsigned i;
12431
a4b6fadd 12432 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12433 {
12434 struct elf_link_hash_entry *h;
12435 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12436 FALSE, FALSE, TRUE);
e7d1c40c
AM
12437 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12438 if (htab->params->plt_thread_safe)
794e51c0
AM
12439 break;
12440 }
12441 }
e7d1c40c
AM
12442 stubs_always_before_branch = htab->params->group_size < 0;
12443 if (htab->params->group_size < 0)
12444 stub_group_size = -htab->params->group_size;
721956f4 12445 else
e7d1c40c 12446 stub_group_size = htab->params->group_size;
721956f4 12447
6f20ed8a
AM
12448 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12449 return FALSE;
721956f4 12450
c9301e31
AM
12451#define STUB_SHRINK_ITER 20
12452 /* Loop until no stubs added. After iteration 20 of this loop we may
12453 exit on a stub section shrinking. This is to break out of a
12454 pathological case where adding stubs on one iteration decreases
12455 section gaps (perhaps due to alignment), which then requires
12456 fewer or smaller stubs on the next iteration. */
12457
721956f4
AM
12458 while (1)
12459 {
12460 bfd *input_bfd;
12461 unsigned int bfd_indx;
a4b6fadd 12462 struct map_stub *group;
721956f4
AM
12463
12464 htab->stub_iteration += 1;
721956f4
AM
12465
12466 for (input_bfd = info->input_bfds, bfd_indx = 0;
12467 input_bfd != NULL;
c72f2fb2 12468 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12469 {
12470 Elf_Internal_Shdr *symtab_hdr;
12471 asection *section;
6cdc0ccc 12472 Elf_Internal_Sym *local_syms = NULL;
721956f4 12473
0c8d6e5c 12474 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12475 continue;
12476
721956f4 12477 /* We'll need the symbol table in a second. */
0ffa91dd 12478 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12479 if (symtab_hdr->sh_info == 0)
12480 continue;
12481
721956f4
AM
12482 /* Walk over each section attached to the input bfd. */
12483 for (section = input_bfd->sections;
12484 section != NULL;
12485 section = section->next)
12486 {
721956f4 12487 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12488
12489 /* If there aren't any relocs, then there's nothing more
12490 to do. */
12491 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12492 || (section->flags & SEC_ALLOC) == 0
12493 || (section->flags & SEC_LOAD) == 0
12494 || (section->flags & SEC_CODE) == 0
721956f4
AM
12495 || section->reloc_count == 0)
12496 continue;
12497
12498 /* If this section is a link-once section that will be
12499 discarded, then don't create any stubs. */
12500 if (section->output_section == NULL
927be08e 12501 || section->output_section->owner != info->output_bfd)
721956f4
AM
12502 continue;
12503
1e2f5b6e
AM
12504 /* Get the relocs. */
12505 internal_relocs
4ce794b7 12506 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12507 info->keep_memory);
721956f4 12508 if (internal_relocs == NULL)
1e2f5b6e 12509 goto error_ret_free_local;
721956f4
AM
12510
12511 /* Now examine each relocation. */
12512 irela = internal_relocs;
12513 irelaend = irela + section->reloc_count;
12514 for (; irela < irelaend; irela++)
12515 {
4ce794b7
AM
12516 enum elf_ppc64_reloc_type r_type;
12517 unsigned int r_indx;
721956f4
AM
12518 enum ppc_stub_type stub_type;
12519 struct ppc_stub_hash_entry *stub_entry;
8387904d 12520 asection *sym_sec, *code_sec;
e054468f 12521 bfd_vma sym_value, code_value;
721956f4 12522 bfd_vma destination;
6911b7dc 12523 unsigned long local_off;
8843416a 12524 bfd_boolean ok_dest;
721956f4 12525 struct ppc_link_hash_entry *hash;
8387904d 12526 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12527 struct elf_link_hash_entry *h;
12528 Elf_Internal_Sym *sym;
721956f4
AM
12529 char *stub_name;
12530 const asection *id_sec;
74f0fb50 12531 struct _opd_sec_data *opd;
e054468f 12532 struct plt_entry *plt_ent;
721956f4
AM
12533
12534 r_type = ELF64_R_TYPE (irela->r_info);
12535 r_indx = ELF64_R_SYM (irela->r_info);
12536
4ce794b7 12537 if (r_type >= R_PPC64_max)
721956f4
AM
12538 {
12539 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12540 goto error_ret_free_internal;
721956f4
AM
12541 }
12542
12543 /* Only look for stubs on branch instructions. */
4ce794b7 12544 if (r_type != R_PPC64_REL24
05d0e962 12545 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
12546 && r_type != R_PPC64_REL14
12547 && r_type != R_PPC64_REL14_BRTAKEN
12548 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12549 continue;
12550
12551 /* Now determine the call target, its name, value,
12552 section. */
411e1bfb
AM
12553 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12554 r_indx, input_bfd))
12555 goto error_ret_free_internal;
12556 hash = (struct ppc_link_hash_entry *) h;
12557
8843416a 12558 ok_dest = FALSE;
8387904d 12559 fdh = NULL;
7fe2b9a6 12560 sym_value = 0;
411e1bfb 12561 if (hash == NULL)
721956f4 12562 {
411e1bfb 12563 sym_value = sym->st_value;
c27b8c2a
AM
12564 if (sym_sec != NULL
12565 && sym_sec->output_section != NULL)
12566 ok_dest = TRUE;
721956f4 12567 }
7fe2b9a6
AM
12568 else if (hash->elf.root.type == bfd_link_hash_defined
12569 || hash->elf.root.type == bfd_link_hash_defweak)
12570 {
12571 sym_value = hash->elf.root.u.def.value;
12572 if (sym_sec->output_section != NULL)
12573 ok_dest = TRUE;
12574 }
12575 else if (hash->elf.root.type == bfd_link_hash_undefweak
12576 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12577 {
99877b66 12578 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12579 use the func descriptor sym instead if it is
12580 defined. */
ceb1f1ef 12581 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12582 && hash->oh != NULL)
8387904d 12583 {
8c5b4e52 12584 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12585 if (fdh->elf.root.type == bfd_link_hash_defined
12586 || fdh->elf.root.type == bfd_link_hash_defweak)
12587 {
12588 sym_sec = fdh->elf.root.u.def.section;
12589 sym_value = fdh->elf.root.u.def.value;
12590 if (sym_sec->output_section != NULL)
12591 ok_dest = TRUE;
12592 }
99877b66
AM
12593 else
12594 fdh = NULL;
8387904d 12595 }
7fe2b9a6
AM
12596 }
12597 else
12598 {
12599 bfd_set_error (bfd_error_bad_value);
12600 goto error_ret_free_internal;
721956f4
AM
12601 }
12602
8843416a 12603 destination = 0;
6911b7dc 12604 local_off = 0;
8843416a
AM
12605 if (ok_dest)
12606 {
12607 sym_value += irela->r_addend;
12608 destination = (sym_value
12609 + sym_sec->output_offset
12610 + sym_sec->output_section->vma);
6911b7dc
AM
12611 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12612 ? hash->elf.other
12613 : sym->st_other);
8843416a
AM
12614 }
12615
8387904d 12616 code_sec = sym_sec;
e054468f 12617 code_value = sym_value;
74f0fb50
AM
12618 opd = get_opd_info (sym_sec);
12619 if (opd != NULL)
8387904d
AM
12620 {
12621 bfd_vma dest;
12622
74f0fb50 12623 if (hash == NULL && opd->adjust != NULL)
8387904d 12624 {
51aecdc5 12625 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12626 if (adjust == -1)
12627 continue;
e054468f 12628 code_value += adjust;
8387904d
AM
12629 sym_value += adjust;
12630 }
12631 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12632 &code_sec, &code_value, FALSE);
8387904d
AM
12633 if (dest != (bfd_vma) -1)
12634 {
12635 destination = dest;
12636 if (fdh != NULL)
12637 {
12638 /* Fixup old ABI sym to point at code
12639 entry. */
99877b66 12640 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12641 hash->elf.root.u.def.section = code_sec;
e054468f 12642 hash->elf.root.u.def.value = code_value;
8387904d
AM
12643 }
12644 }
12645 }
12646
721956f4 12647 /* Determine what (if any) linker stub is needed. */
e054468f 12648 plt_ent = NULL;
721956f4 12649 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12650 &plt_ent, destination,
12651 local_off);
ad8e1ba5 12652
05d0e962
AM
12653 if (r_type == R_PPC64_REL24_NOTOC)
12654 {
12655 if (stub_type == ppc_stub_plt_call)
12656 stub_type = ppc_stub_plt_call_notoc;
12657 else if (stub_type == ppc_stub_long_branch
12658 || (code_sec != NULL
12659 && code_sec->output_section != NULL
12660 && (((hash ? hash->elf.other : sym->st_other)
12661 & STO_PPC64_LOCAL_MASK)
12662 != 1 << STO_PPC64_LOCAL_BIT)))
12663 stub_type = ppc_stub_long_branch_notoc;
12664 }
12665 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
12666 {
12667 /* Check whether we need a TOC adjusting stub.
12668 Since the linker pastes together pieces from
12669 different object files when creating the
12670 _init and _fini functions, it may be that a
12671 call to what looks like a local sym is in
12672 fact a call needing a TOC adjustment. */
33cb30a1
AM
12673 if ((code_sec != NULL
12674 && code_sec->output_section != NULL
12675 && (htab->sec_info[code_sec->id].toc_off
12676 != htab->sec_info[section->id].toc_off)
12677 && (code_sec->has_toc_reloc
12678 || code_sec->makes_toc_func_call))
12679 || (((hash ? hash->elf.other : sym->st_other)
12680 & STO_PPC64_LOCAL_MASK)
12681 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
12682 stub_type = ppc_stub_long_branch_r2off;
12683 }
12684
721956f4
AM
12685 if (stub_type == ppc_stub_none)
12686 continue;
12687
411e1bfb
AM
12688 /* __tls_get_addr calls might be eliminated. */
12689 if (stub_type != ppc_stub_plt_call
05d0e962 12690 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 12691 && hash != NULL
8387904d
AM
12692 && (hash == htab->tls_get_addr
12693 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12694 && section->has_tls_reloc
12695 && irela != internal_relocs)
12696 {
12697 /* Get tls info. */
f961d9dd 12698 unsigned char *tls_mask;
411e1bfb 12699
3a71aa26 12700 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12701 irela - 1, input_bfd))
12702 goto error_ret_free_internal;
37da22e5 12703 if ((*tls_mask & TLS_TLS) != 0)
411e1bfb
AM
12704 continue;
12705 }
12706
f378ab09 12707 if (stub_type == ppc_stub_plt_call)
794e51c0 12708 {
6e1816be
AM
12709 if (!htab->opd_abi
12710 && htab->params->plt_localentry0 != 0
12711 && is_elfv2_localentry0 (&hash->elf))
12712 htab->has_plt_localentry0 = 1;
12713 else if (irela + 1 < irelaend
12714 && irela[1].r_offset == irela->r_offset + 4
12715 && (ELF64_R_TYPE (irela[1].r_info)
12716 == R_PPC64_TOCSAVE))
f378ab09
AM
12717 {
12718 if (!tocsave_find (htab, INSERT,
12719 &local_syms, irela + 1, input_bfd))
12720 goto error_ret_free_internal;
12721 }
f378ab09
AM
12722 else
12723 stub_type = ppc_stub_plt_call_r2save;
794e51c0 12724 }
3b421ab3 12725
721956f4 12726 /* Support for grouping stub sections. */
6f20ed8a 12727 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12728
12729 /* Get the name of this stub. */
12730 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12731 if (!stub_name)
12732 goto error_ret_free_internal;
12733
12734 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12735 stub_name, FALSE, FALSE);
721956f4
AM
12736 if (stub_entry != NULL)
12737 {
05d0e962
AM
12738 enum ppc_stub_type old_type;
12739 /* A stub has already been created, but it may
12740 not be the required type. We shouldn't be
12741 transitioning from plt_call to long_branch
12742 stubs or vice versa, but we might be
12743 upgrading from plt_call to plt_call_r2save or
12744 from long_branch to long_branch_r2off. */
721956f4 12745 free (stub_name);
05d0e962
AM
12746 old_type = stub_entry->stub_type;
12747 switch (old_type)
12748 {
12749 default:
12750 abort ();
12751
12752 case ppc_stub_save_res:
12753 continue;
12754
12755 case ppc_stub_plt_call:
12756 case ppc_stub_plt_call_r2save:
12757 case ppc_stub_plt_call_notoc:
12758 case ppc_stub_plt_call_both:
12759 if (stub_type == ppc_stub_plt_call)
12760 continue;
12761 else if (stub_type == ppc_stub_plt_call_r2save)
12762 {
12763 if (old_type == ppc_stub_plt_call_notoc)
12764 stub_type = ppc_stub_plt_call_both;
12765 }
12766 else if (stub_type == ppc_stub_plt_call_notoc)
12767 {
12768 if (old_type == ppc_stub_plt_call_r2save)
12769 stub_type = ppc_stub_plt_call_both;
12770 }
12771 else
12772 abort ();
12773 break;
12774
12775 case ppc_stub_plt_branch:
12776 case ppc_stub_plt_branch_r2off:
12777 case ppc_stub_plt_branch_notoc:
12778 case ppc_stub_plt_branch_both:
12779 old_type += (ppc_stub_long_branch
12780 - ppc_stub_plt_branch);
12781 /* Fall through. */
12782 case ppc_stub_long_branch:
12783 case ppc_stub_long_branch_r2off:
12784 case ppc_stub_long_branch_notoc:
12785 case ppc_stub_long_branch_both:
12786 if (stub_type == ppc_stub_long_branch)
12787 continue;
12788 else if (stub_type == ppc_stub_long_branch_r2off)
12789 {
12790 if (old_type == ppc_stub_long_branch_notoc)
12791 stub_type = ppc_stub_long_branch_both;
12792 }
12793 else if (stub_type == ppc_stub_long_branch_notoc)
12794 {
12795 if (old_type == ppc_stub_long_branch_r2off)
12796 stub_type = ppc_stub_long_branch_both;
12797 }
12798 else
12799 abort ();
12800 break;
12801 }
12802 if (old_type < stub_type)
794e51c0 12803 stub_entry->stub_type = stub_type;
721956f4
AM
12804 continue;
12805 }
12806
25f53a85 12807 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12808 if (stub_entry == NULL)
12809 {
12810 free (stub_name);
6cdc0ccc
AM
12811 error_ret_free_internal:
12812 if (elf_section_data (section)->relocs == NULL)
12813 free (internal_relocs);
12814 error_ret_free_local:
12815 if (local_syms != NULL
12816 && (symtab_hdr->contents
12817 != (unsigned char *) local_syms))
12818 free (local_syms);
b34976b6 12819 return FALSE;
721956f4
AM
12820 }
12821
ad8e1ba5 12822 stub_entry->stub_type = stub_type;
05d0e962
AM
12823 if (stub_type >= ppc_stub_plt_call
12824 && stub_type <= ppc_stub_plt_call_both)
e054468f 12825 {
05d0e962
AM
12826 stub_entry->target_value = sym_value;
12827 stub_entry->target_section = sym_sec;
e054468f
AM
12828 }
12829 else
12830 {
05d0e962
AM
12831 stub_entry->target_value = code_value;
12832 stub_entry->target_section = code_sec;
e054468f 12833 }
721956f4 12834 stub_entry->h = hash;
e054468f 12835 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
12836 stub_entry->symtype
12837 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 12838 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 12839
3d58e1fc
AM
12840 if (hash != NULL
12841 && (hash->elf.root.type == bfd_link_hash_defined
12842 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 12843 htab->stub_globals += 1;
721956f4
AM
12844 }
12845
12846 /* We're done with the internal relocs, free them. */
6cdc0ccc 12847 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12848 free (internal_relocs);
721956f4 12849 }
6cdc0ccc
AM
12850
12851 if (local_syms != NULL
12852 && symtab_hdr->contents != (unsigned char *) local_syms)
12853 {
12854 if (!info->keep_memory)
12855 free (local_syms);
12856 else
12857 symtab_hdr->contents = (unsigned char *) local_syms;
12858 }
721956f4
AM
12859 }
12860
5c3dead3 12861 /* We may have added some stubs. Find out the new size of the
721956f4 12862 stub sections. */
d4aaa2a0 12863 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
12864 {
12865 group->lr_restore = 0;
12866 group->eh_size = 0;
12867 if (group->stub_sec != NULL)
12868 {
12869 asection *stub_sec = group->stub_sec;
12870
12871 if (htab->stub_iteration <= STUB_SHRINK_ITER
12872 || stub_sec->rawsize < stub_sec->size)
12873 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12874 stub_sec->rawsize = stub_sec->size;
12875 stub_sec->size = 0;
12876 stub_sec->reloc_count = 0;
12877 stub_sec->flags &= ~SEC_RELOC;
12878 }
12879 }
eea6121a 12880
ba21f564
AM
12881 if (htab->stub_iteration <= STUB_SHRINK_ITER
12882 || htab->brlt->rawsize < htab->brlt->size)
12883 htab->brlt->rawsize = htab->brlt->size;
eea6121a 12884 htab->brlt->size = 0;
84f5d08e
AM
12885 htab->brlt->reloc_count = 0;
12886 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12887 if (htab->relbrlt != NULL)
eea6121a 12888 htab->relbrlt->size = 0;
721956f4 12889
63bc6f6c 12890 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12891
a4b6fadd
AM
12892 for (group = htab->group; group != NULL; group = group->next)
12893 if (group->needs_save_res)
12894 group->stub_sec->size += htab->sfpr->size;
12895
176a0d42
AM
12896 if (info->emitrelocations
12897 && htab->glink != NULL && htab->glink->size != 0)
12898 {
12899 htab->glink->reloc_count = 1;
12900 htab->glink->flags |= SEC_RELOC;
12901 }
12902
58d180e8
AM
12903 if (htab->glink_eh_frame != NULL
12904 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 12905 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 12906 {
2e0ce1c8 12907 size_t size = 0, align = 4;
58d180e8 12908
d4aaa2a0 12909 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
12910 if (group->eh_size != 0)
12911 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 12912 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 12913 size += (24 + align - 1) & -align;
58d180e8 12914 if (size != 0)
2e0ce1c8
AM
12915 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12916 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12917 size = (size + align - 1) & -align;
58d180e8
AM
12918 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12919 htab->glink_eh_frame->size = size;
12920 }
12921
e7d1c40c 12922 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
12923 for (group = htab->group; group != NULL; group = group->next)
12924 if (group->stub_sec != NULL)
691d2e9a
AM
12925 {
12926 int align = abs (htab->params->plt_stub_align);
12927 group->stub_sec->size
12928 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12929 }
d4aaa2a0
AM
12930
12931 for (group = htab->group; group != NULL; group = group->next)
12932 if (group->stub_sec != NULL
12933 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 12934 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 12935 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
12936 break;
12937
d4aaa2a0 12938 if (group == NULL
ba21f564
AM
12939 && (htab->brlt->rawsize == htab->brlt->size
12940 || (htab->stub_iteration > STUB_SHRINK_ITER
12941 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
12942 && (htab->glink_eh_frame == NULL
12943 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12944 break;
12945
721956f4 12946 /* Ask the linker to do its stuff. */
e7d1c40c 12947 (*htab->params->layout_sections_again) ();
721956f4
AM
12948 }
12949
da44f4e5
AM
12950 if (htab->glink_eh_frame != NULL
12951 && htab->glink_eh_frame->size != 0)
12952 {
12953 bfd_vma val;
12954 bfd_byte *p, *last_fde;
12955 size_t last_fde_len, size, align, pad;
d4aaa2a0 12956 struct map_stub *group;
da44f4e5 12957
df136d64
AM
12958 /* It is necessary to at least have a rough outline of the
12959 linker generated CIEs and FDEs written before
12960 bfd_elf_discard_info is run, in order for these FDEs to be
12961 indexed in .eh_frame_hdr. */
da44f4e5
AM
12962 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12963 if (p == NULL)
12964 return FALSE;
12965 htab->glink_eh_frame->contents = p;
12966 last_fde = p;
2e0ce1c8 12967 align = 4;
da44f4e5
AM
12968
12969 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12970 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 12971 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 12972 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 12973 p += last_fde_len + 4;
da44f4e5 12974
d4aaa2a0 12975 for (group = htab->group; group != NULL; group = group->next)
df136d64 12976 if (group->eh_size != 0)
da44f4e5 12977 {
df136d64 12978 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 12979 last_fde = p;
df136d64 12980 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 12981 /* FDE length. */
2e0ce1c8 12982 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12983 p += 4;
12984 /* CIE pointer. */
12985 val = p - htab->glink_eh_frame->contents;
12986 bfd_put_32 (htab->elf.dynobj, val, p);
12987 p += 4;
12988 /* Offset to stub section, written later. */
12989 p += 4;
12990 /* stub section size. */
d4aaa2a0 12991 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
12992 p += 4;
12993 /* Augmentation. */
12994 p += 1;
df136d64
AM
12995 /* Make sure we don't have all nops. This is enough for
12996 elf-eh-frame.c to detect the last non-nop opcode. */
12997 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 12998 p = last_fde + last_fde_len + 4;
da44f4e5
AM
12999 }
13000 if (htab->glink != NULL && htab->glink->size != 0)
13001 {
13002 last_fde = p;
2e0ce1c8 13003 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13004 /* FDE length. */
2e0ce1c8 13005 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13006 p += 4;
13007 /* CIE pointer. */
13008 val = p - htab->glink_eh_frame->contents;
13009 bfd_put_32 (htab->elf.dynobj, val, p);
13010 p += 4;
13011 /* Offset to .glink, written later. */
13012 p += 4;
13013 /* .glink size. */
13014 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13015 p += 4;
13016 /* Augmentation. */
13017 p += 1;
13018
13019 *p++ = DW_CFA_advance_loc + 1;
13020 *p++ = DW_CFA_register;
13021 *p++ = 65;
9f08fa5c 13022 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 13023 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
13024 *p++ = DW_CFA_restore_extended;
13025 *p++ = 65;
2e0ce1c8 13026 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13027 }
13028 /* Subsume any padding into the last FDE if user .eh_frame
13029 sections are aligned more than glink_eh_frame. Otherwise any
13030 zero padding will be seen as a terminator. */
2e0ce1c8 13031 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13032 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13033 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13034 htab->glink_eh_frame->size = size + pad;
13035 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13036 }
13037
d969d15f
AM
13038 maybe_strip_output (info, htab->brlt);
13039 if (htab->glink_eh_frame != NULL)
13040 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13041
b34976b6 13042 return TRUE;
721956f4
AM
13043}
13044
13045/* Called after we have determined section placement. If sections
805fc799 13046 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13047
805fc799 13048bfd_vma
1c865ab2 13049ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13050{
805fc799 13051 asection *s;
a27e685f 13052 bfd_vma TOCstart, adjust;
721956f4 13053
43417696
AM
13054 if (info != NULL)
13055 {
13056 struct elf_link_hash_entry *h;
13057 struct elf_link_hash_table *htab = elf_hash_table (info);
13058
13059 if (is_elf_hash_table (htab)
13060 && htab->hgot != NULL)
13061 h = htab->hgot;
13062 else
13063 {
13064 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13065 if (is_elf_hash_table (htab))
13066 htab->hgot = h;
13067 }
13068 if (h != NULL
13069 && h->root.type == bfd_link_hash_defined
13070 && !h->root.linker_def
13071 && (!is_elf_hash_table (htab)
13072 || h->def_regular))
13073 {
13074 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13075 + h->root.u.def.section->output_offset
13076 + h->root.u.def.section->output_section->vma);
13077 _bfd_set_gp_value (obfd, TOCstart);
13078 return TOCstart;
13079 }
13080 }
13081
805fc799
AM
13082 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13083 order. The TOC starts where the first of these sections starts. */
13084 s = bfd_get_section_by_name (obfd, ".got");
e054468f 13085 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13086 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 13087 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13088 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 13089 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13090 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 13091 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
13092 {
13093 /* This may happen for
13094 o references to TOC base (SYM@toc / TOC[tc0]) without a
13095 .toc directive
13096 o bad linker script
13097 o --gc-sections and empty TOC sections
13098
13099 FIXME: Warn user? */
13100
13101 /* Look for a likely section. We probably won't even be
13102 using TOCstart. */
13103 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13104 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13105 | SEC_EXCLUDE))
805fc799
AM
13106 == (SEC_ALLOC | SEC_SMALL_DATA))
13107 break;
721956f4 13108 if (s == NULL)
805fc799 13109 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13110 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
13111 == (SEC_ALLOC | SEC_SMALL_DATA))
13112 break;
721956f4 13113 if (s == NULL)
805fc799 13114 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13115 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13116 == SEC_ALLOC)
805fc799 13117 break;
721956f4 13118 if (s == NULL)
805fc799 13119 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13120 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
13121 break;
13122 }
721956f4 13123
805fc799
AM
13124 TOCstart = 0;
13125 if (s != NULL)
13126 TOCstart = s->output_section->vma + s->output_offset;
721956f4 13127
a27e685f
AM
13128 /* Force alignment. */
13129 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13130 TOCstart -= adjust;
1c865ab2
AM
13131 _bfd_set_gp_value (obfd, TOCstart);
13132
810d4e75 13133 if (info != NULL && s != NULL)
1c865ab2
AM
13134 {
13135 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13136
810d4e75
AM
13137 if (htab != NULL)
13138 {
13139 if (htab->elf.hgot != NULL)
13140 {
a27e685f 13141 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
13142 htab->elf.hgot->root.u.def.section = s;
13143 }
13144 }
13145 else
1c865ab2 13146 {
810d4e75
AM
13147 struct bfd_link_hash_entry *bh = NULL;
13148 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
13149 s, TOC_BASE_OFF - adjust,
13150 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
13151 }
13152 }
805fc799 13153 return TOCstart;
721956f4
AM
13154}
13155
a345bc8d 13156/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 13157 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
13158
13159static bfd_boolean
49c09209 13160build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
13161{
13162 struct bfd_link_info *info;
13163 struct ppc_link_hash_table *htab;
49c09209 13164 struct plt_entry *ent;
a345bc8d
AM
13165 asection *s;
13166
13167 if (h->root.type == bfd_link_hash_indirect)
13168 return TRUE;
13169
49c09209
AM
13170 info = inf;
13171 htab = ppc_hash_table (info);
13172 if (htab == NULL)
13173 return FALSE;
13174
13175 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13176 if (ent->plt.offset != (bfd_vma) -1)
13177 {
13178 /* This symbol has an entry in the procedure linkage
13179 table. Set it up. */
13180 Elf_Internal_Rela rela;
2d7ad24e 13181 asection *plt, *relplt;
49c09209
AM
13182 bfd_byte *loc;
13183
13184 if (!htab->elf.dynamic_sections_created
13185 || h->dynindx == -1)
13186 {
13187 if (!(h->def_regular
13188 && (h->root.type == bfd_link_hash_defined
13189 || h->root.type == bfd_link_hash_defweak)))
13190 continue;
2d7ad24e
AM
13191 if (h->type == STT_GNU_IFUNC)
13192 {
13193 plt = htab->elf.iplt;
13194 relplt = htab->elf.irelplt;
13195 htab->local_ifunc_resolver = 1;
13196 if (htab->opd_abi)
13197 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13198 else
13199 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13200 }
49c09209 13201 else
2d7ad24e
AM
13202 {
13203 plt = htab->pltlocal;
13204 if (bfd_link_pic (info))
13205 {
13206 relplt = htab->relpltlocal;
13207 if (htab->opd_abi)
13208 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13209 else
13210 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13211 }
13212 else
13213 relplt = NULL;
13214 }
49c09209
AM
13215 rela.r_addend = (h->root.u.def.value
13216 + h->root.u.def.section->output_offset
13217 + h->root.u.def.section->output_section->vma
13218 + ent->addend);
2d7ad24e
AM
13219
13220 if (relplt == NULL)
13221 {
13222 loc = plt->contents + ent->plt.offset;
13223 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13224 if (htab->opd_abi)
13225 {
13226 bfd_vma toc = elf_gp (info->output_bfd);
13227 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13228 bfd_put_64 (info->output_bfd, toc, loc + 8);
13229 }
13230 }
13231 else
13232 {
13233 rela.r_offset = (plt->output_section->vma
13234 + plt->output_offset
13235 + ent->plt.offset);
13236 loc = relplt->contents + (relplt->reloc_count++
13237 * sizeof (Elf64_External_Rela));
13238 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13239 }
49c09209
AM
13240 }
13241 else
13242 {
13243 rela.r_offset = (htab->elf.splt->output_section->vma
13244 + htab->elf.splt->output_offset
13245 + ent->plt.offset);
13246 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13247 rela.r_addend = ent->addend;
13248 loc = (htab->elf.srelplt->contents
13249 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13250 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13251 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13252 htab->maybe_local_ifunc_resolver = 1;
2d7ad24e 13253 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 13254 }
49c09209
AM
13255 }
13256
a345bc8d
AM
13257 if (!h->pointer_equality_needed)
13258 return TRUE;
13259
13260 if (h->def_regular)
13261 return TRUE;
13262
9e390558 13263 s = htab->global_entry;
49c09209
AM
13264 if (s == NULL || s->size == 0)
13265 return TRUE;
13266
13267 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13268 if (ent->plt.offset != (bfd_vma) -1
13269 && ent->addend == 0)
a345bc8d
AM
13270 {
13271 bfd_byte *p;
13272 asection *plt;
13273 bfd_vma off;
13274
a345bc8d 13275 p = s->contents + h->root.u.def.value;
33e44f2e 13276 plt = htab->elf.splt;
a345bc8d
AM
13277 if (!htab->elf.dynamic_sections_created
13278 || h->dynindx == -1)
2d7ad24e
AM
13279 {
13280 if (h->type == STT_GNU_IFUNC)
13281 plt = htab->elf.iplt;
13282 else
13283 plt = htab->pltlocal;
13284 }
49c09209 13285 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
13286 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13287
13288 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13289 {
13290 info->callbacks->einfo
c1c8c1ef 13291 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
13292 h->root.root.string);
13293 bfd_set_error (bfd_error_bad_value);
13294 htab->stub_error = TRUE;
13295 }
13296
7341d5e2
AM
13297 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13298 if (htab->params->emit_stub_syms)
13299 {
13300 size_t len = strlen (h->root.root.string);
13301 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13302
13303 if (name == NULL)
13304 return FALSE;
13305
13306 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13307 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13308 if (h == NULL)
13309 return FALSE;
13310 if (h->root.type == bfd_link_hash_new)
13311 {
13312 h->root.type = bfd_link_hash_defined;
13313 h->root.u.def.section = s;
13314 h->root.u.def.value = p - s->contents;
13315 h->ref_regular = 1;
13316 h->def_regular = 1;
13317 h->ref_regular_nonweak = 1;
13318 h->forced_local = 1;
13319 h->non_elf = 0;
2ec55de3 13320 h->root.linker_def = 1;
7341d5e2
AM
13321 }
13322 }
13323
a345bc8d
AM
13324 if (PPC_HA (off) != 0)
13325 {
13326 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13327 p += 4;
13328 }
13329 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13330 p += 4;
13331 bfd_put_32 (s->owner, MTCTR_R12, p);
13332 p += 4;
407aa07c 13333 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
13334 break;
13335 }
13336 return TRUE;
13337}
13338
49c09209
AM
13339/* Write PLT relocs for locals. */
13340
13341static bfd_boolean
13342write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13343{
13344 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13345 bfd *ibfd;
13346
13347 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13348 {
13349 struct got_entry **lgot_ents, **end_lgot_ents;
13350 struct plt_entry **local_plt, **lplt, **end_local_plt;
13351 Elf_Internal_Shdr *symtab_hdr;
13352 bfd_size_type locsymcount;
13353 Elf_Internal_Sym *local_syms = NULL;
13354 struct plt_entry *ent;
13355
13356 if (!is_ppc64_elf (ibfd))
13357 continue;
13358
13359 lgot_ents = elf_local_got_ents (ibfd);
13360 if (!lgot_ents)
13361 continue;
13362
13363 symtab_hdr = &elf_symtab_hdr (ibfd);
13364 locsymcount = symtab_hdr->sh_info;
13365 end_lgot_ents = lgot_ents + locsymcount;
13366 local_plt = (struct plt_entry **) end_lgot_ents;
13367 end_local_plt = local_plt + locsymcount;
13368 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13369 for (ent = *lplt; ent != NULL; ent = ent->next)
13370 if (ent->plt.offset != (bfd_vma) -1)
13371 {
13372 Elf_Internal_Sym *sym;
13373 asection *sym_sec;
13374 asection *plt, *relplt;
13375 bfd_byte *loc;
13376 bfd_vma val;
49c09209
AM
13377
13378 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13379 lplt - local_plt, ibfd))
13380 {
13381 if (local_syms != NULL
13382 && symtab_hdr->contents != (unsigned char *) local_syms)
13383 free (local_syms);
13384 return FALSE;
13385 }
13386
13387 val = sym->st_value + ent->addend;
ba85c15d
AM
13388 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13389 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
49c09209
AM
13390 if (sym_sec != NULL && sym_sec->output_section != NULL)
13391 val += sym_sec->output_offset + sym_sec->output_section->vma;
13392
2d7ad24e
AM
13393 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13394 {
13395 htab->local_ifunc_resolver = 1;
13396 plt = htab->elf.iplt;
13397 relplt = htab->elf.irelplt;
13398 }
13399 else
13400 {
13401 plt = htab->pltlocal;
13402 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13403 }
49c09209 13404
2d7ad24e
AM
13405 if (relplt == NULL)
13406 {
13407 loc = plt->contents + ent->plt.offset;
13408 bfd_put_64 (info->output_bfd, val, loc);
13409 if (htab->opd_abi)
13410 {
13411 bfd_vma toc = elf_gp (ibfd);
13412 bfd_put_64 (info->output_bfd, toc, loc + 8);
13413 }
13414 }
49c09209 13415 else
2d7ad24e
AM
13416 {
13417 Elf_Internal_Rela rela;
13418 rela.r_offset = (ent->plt.offset
13419 + plt->output_offset
13420 + plt->output_section->vma);
13421 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13422 {
13423 if (htab->opd_abi)
13424 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13425 else
13426 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13427 }
13428 else
13429 {
13430 if (htab->opd_abi)
13431 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13432 else
13433 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13434 }
13435 rela.r_addend = val;
13436 loc = relplt->contents + (relplt->reloc_count++
13437 * sizeof (Elf64_External_Rela));
13438 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13439 }
49c09209
AM
13440 }
13441
13442 if (local_syms != NULL
13443 && symtab_hdr->contents != (unsigned char *) local_syms)
13444 {
13445 if (!info->keep_memory)
13446 free (local_syms);
13447 else
13448 symtab_hdr->contents = (unsigned char *) local_syms;
13449 }
13450 }
13451 return TRUE;
13452}
13453
721956f4
AM
13454/* Build all the stubs associated with the current output file.
13455 The stubs are kept in a hash table attached to the main linker
13456 hash table. This function is called via gldelf64ppc_finish. */
13457
b34976b6 13458bfd_boolean
e7d1c40c 13459ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13460 char **stats)
5d1634d7
AM
13461{
13462 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13463 struct map_stub *group;
721956f4 13464 asection *stub_sec;
5d1634d7 13465 bfd_byte *p;
e717da7e 13466 int stub_sec_count = 0;
5d1634d7 13467
4dfe6ac6
NC
13468 if (htab == NULL)
13469 return FALSE;
13470
eea6121a 13471 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 13472 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13473 {
13474 group->eh_size = 0;
13475 group->lr_restore = 0;
13476 if ((stub_sec = group->stub_sec) != NULL
13477 && stub_sec->size != 0)
13478 {
13479 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13480 stub_sec->size);
13481 if (stub_sec->contents == NULL)
13482 return FALSE;
13483 stub_sec->size = 0;
13484 }
13485 }
5d1634d7 13486
23eb7e01 13487 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13488 {
9f951329 13489 unsigned int indx;
ad8e1ba5 13490 bfd_vma plt0;
9f951329 13491
721956f4 13492 /* Build the .glink plt call stub. */
e7d1c40c 13493 if (htab->params->emit_stub_syms)
97b639ba
AM
13494 {
13495 struct elf_link_hash_entry *h;
468392fb
AM
13496 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13497 TRUE, FALSE, FALSE);
97b639ba
AM
13498 if (h == NULL)
13499 return FALSE;
13500 if (h->root.type == bfd_link_hash_new)
13501 {
13502 h->root.type = bfd_link_hash_defined;
13503 h->root.u.def.section = htab->glink;
ee4bf8d2 13504 h->root.u.def.value = 8;
f5385ebf
AM
13505 h->ref_regular = 1;
13506 h->def_regular = 1;
13507 h->ref_regular_nonweak = 1;
13508 h->forced_local = 1;
13509 h->non_elf = 0;
2ec55de3 13510 h->root.linker_def = 1;
97b639ba
AM
13511 }
13512 }
33e44f2e
AM
13513 plt0 = (htab->elf.splt->output_section->vma
13514 + htab->elf.splt->output_offset
13515 - 16);
176a0d42
AM
13516 if (info->emitrelocations)
13517 {
13518 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13519 if (r == NULL)
13520 return FALSE;
13521 r->r_offset = (htab->glink->output_offset
13522 + htab->glink->output_section->vma);
13523 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13524 r->r_addend = plt0;
13525 }
4ce794b7 13526 p = htab->glink->contents;
176a0d42 13527 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13528 bfd_put_64 (htab->glink->owner, plt0, p);
13529 p += 8;
b9e5796b
AM
13530 if (htab->opd_abi)
13531 {
13532 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13533 p += 4;
13534 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13535 p += 4;
13536 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13537 p += 4;
13538 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13539 p += 4;
13540 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13541 p += 4;
13542 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13543 p += 4;
13544 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13545 p += 4;
13546 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13547 p += 4;
13548 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13549 p += 4;
13550 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13551 p += 4;
13552 }
13553 else
13554 {
13555 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13556 p += 4;
13557 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13558 p += 4;
13559 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13560 p += 4;
f378ab09
AM
13561 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13562 p += 4;
b9e5796b
AM
13563 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13564 p += 4;
13565 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13566 p += 4;
13567 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13568 p += 4;
13569 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13570 p += 4;
13571 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13572 p += 4;
13573 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13574 p += 4;
13575 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13576 p += 4;
13577 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13578 p += 4;
13579 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13580 p += 4;
13581 }
407aa07c
AM
13582 bfd_put_32 (htab->glink->owner, BCTR, p);
13583 p += 4;
c75bc4f7 13584 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 13585
9f951329
AM
13586 /* Build the .glink lazy link call stubs. */
13587 indx = 0;
9e390558 13588 while (p < htab->glink->contents + htab->glink->size)
9f951329 13589 {
b9e5796b 13590 if (htab->opd_abi)
9f951329 13591 {
b9e5796b
AM
13592 if (indx < 0x8000)
13593 {
13594 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13595 p += 4;
13596 }
13597 else
13598 {
13599 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13600 p += 4;
13601 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13602 p);
13603 p += 4;
13604 }
9f951329 13605 }
4ce794b7 13606 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13607 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13608 indx++;
9f951329
AM
13609 p += 4;
13610 }
5d1634d7 13611 }
5d1634d7 13612
49c09209
AM
13613 /* Build .glink global entry stubs, and PLT relocs for globals. */
13614 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13615
13616 if (!write_plt_relocs_for_local_syms (info))
13617 return FALSE;
9e390558 13618
7341d5e2 13619 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13620 {
4ce794b7 13621 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13622 htab->brlt->size);
4ce794b7 13623 if (htab->brlt->contents == NULL)
b34976b6 13624 return FALSE;
721956f4 13625 }
ee75fd95 13626 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13627 {
13628 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13629 htab->relbrlt->size);
63bc6f6c
AM
13630 if (htab->relbrlt->contents == NULL)
13631 return FALSE;
13632 }
5d1634d7 13633
721956f4
AM
13634 /* Build the stubs as directed by the stub hash table. */
13635 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13636
a4b6fadd
AM
13637 for (group = htab->group; group != NULL; group = group->next)
13638 if (group->needs_save_res)
7dda8d3c 13639 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 13640
aa8a7074
AM
13641 if (htab->relbrlt != NULL)
13642 htab->relbrlt->reloc_count = 0;
13643
e7d1c40c 13644 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13645 for (group = htab->group; group != NULL; group = group->next)
13646 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
13647 {
13648 int align = abs (htab->params->plt_stub_align);
13649 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13650 }
794e51c0 13651
7dda8d3c
AM
13652 for (group = htab->group; group != NULL; group = group->next)
13653 if (group->needs_save_res)
13654 {
13655 stub_sec = group->stub_sec;
13656 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13657 htab->sfpr->contents, htab->sfpr->size);
13658 if (htab->params->emit_stub_syms)
13659 {
13660 unsigned int i;
13661
13662 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13663 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13664 return FALSE;
13665 }
13666 }
13667
df136d64
AM
13668 if (htab->glink_eh_frame != NULL
13669 && htab->glink_eh_frame->size != 0)
13670 {
13671 bfd_vma val;
13672 size_t align = 4;
13673
13674 p = htab->glink_eh_frame->contents;
13675 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13676
13677 for (group = htab->group; group != NULL; group = group->next)
13678 if (group->eh_size != 0)
13679 {
13680 /* Offset to stub section. */
13681 val = (group->stub_sec->output_section->vma
13682 + group->stub_sec->output_offset);
13683 val -= (htab->glink_eh_frame->output_section->vma
13684 + htab->glink_eh_frame->output_offset
13685 + (p + 8 - htab->glink_eh_frame->contents));
13686 if (val + 0x80000000 > 0xffffffff)
13687 {
13688 _bfd_error_handler
13689 (_("%s offset too large for .eh_frame sdata4 encoding"),
13690 group->stub_sec->name);
13691 return FALSE;
13692 }
13693 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13694 p += (group->eh_size + 17 + 3) & -4;
13695 }
13696 if (htab->glink != NULL && htab->glink->size != 0)
13697 {
13698 /* Offset to .glink. */
13699 val = (htab->glink->output_section->vma
13700 + htab->glink->output_offset
13701 + 8);
13702 val -= (htab->glink_eh_frame->output_section->vma
13703 + htab->glink_eh_frame->output_offset
13704 + (p + 8 - htab->glink_eh_frame->contents));
13705 if (val + 0x80000000 > 0xffffffff)
13706 {
13707 _bfd_error_handler
13708 (_("%s offset too large for .eh_frame sdata4 encoding"),
13709 htab->glink->name);
13710 return FALSE;
13711 }
13712 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13713 p += (24 + align - 1) & -align;
13714 }
13715 }
13716
d4aaa2a0
AM
13717 for (group = htab->group; group != NULL; group = group->next)
13718 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
13719 {
13720 stub_sec_count += 1;
c9301e31
AM
13721 if (stub_sec->rawsize != stub_sec->size
13722 && (htab->stub_iteration <= STUB_SHRINK_ITER
13723 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13724 break;
13725 }
5d1634d7 13726
25516cc5 13727 if (group != NULL)
5d1634d7 13728 {
b34976b6 13729 htab->stub_error = TRUE;
cf97bcb0 13730 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 13731 }
721956f4 13732
d2a300cf
AM
13733 if (htab->stub_error)
13734 return FALSE;
13735
13736 if (stats != NULL)
13737 {
db84fff3 13738 size_t len;
d2a300cf
AM
13739 *stats = bfd_malloc (500);
13740 if (*stats == NULL)
13741 return FALSE;
13742
db84fff3
AM
13743 len = sprintf (*stats,
13744 ngettext ("linker stubs in %u group\n",
13745 "linker stubs in %u groups\n",
13746 stub_sec_count),
13747 stub_sec_count);
05d0e962
AM
13748 sprintf (*stats + len, _(" branch %lu\n"
13749 " branch toc adj %lu\n"
13750 " branch notoc %lu\n"
13751 " branch both %lu\n"
13752 " long branch %lu\n"
13753 " long toc adj %lu\n"
13754 " long notoc %lu\n"
13755 " long both %lu\n"
13756 " plt call %lu\n"
13757 " plt call save %lu\n"
13758 " plt call notoc %lu\n"
13759 " plt call both %lu\n"
13760 " global entry %lu"),
4ce794b7
AM
13761 htab->stub_count[ppc_stub_long_branch - 1],
13762 htab->stub_count[ppc_stub_long_branch_r2off - 1],
05d0e962
AM
13763 htab->stub_count[ppc_stub_long_branch_notoc - 1],
13764 htab->stub_count[ppc_stub_long_branch_both - 1],
4ce794b7
AM
13765 htab->stub_count[ppc_stub_plt_branch - 1],
13766 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
05d0e962
AM
13767 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
13768 htab->stub_count[ppc_stub_plt_branch_both - 1],
794e51c0 13769 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2 13770 htab->stub_count[ppc_stub_plt_call_r2save - 1],
05d0e962
AM
13771 htab->stub_count[ppc_stub_plt_call_notoc - 1],
13772 htab->stub_count[ppc_stub_plt_call_both - 1],
7341d5e2 13773 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13774 }
13775 return TRUE;
5bd4f169
AM
13776}
13777
60124e18
AM
13778/* What to do when ld finds relocations against symbols defined in
13779 discarded sections. */
13780
13781static unsigned int
13782ppc64_elf_action_discarded (asection *sec)
13783{
13784 if (strcmp (".opd", sec->name) == 0)
13785 return 0;
13786
13787 if (strcmp (".toc", sec->name) == 0)
13788 return 0;
13789
bce50a28
JJ
13790 if (strcmp (".toc1", sec->name) == 0)
13791 return 0;
13792
60124e18
AM
13793 return _bfd_elf_default_action_discarded (sec);
13794}
13795
5bd4f169
AM
13796/* The RELOCATE_SECTION function is called by the ELF backend linker
13797 to handle the relocations for a section.
13798
13799 The relocs are always passed as Rela structures; if the section
13800 actually uses Rel structures, the r_addend field will always be
13801 zero.
13802
13803 This function is responsible for adjust the section contents as
13804 necessary, and (if using Rela relocs and generating a
1049f94e 13805 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13806 necessary.
13807
13808 This function does not have to worry about setting the reloc
13809 address or the reloc symbol index.
13810
13811 LOCAL_SYMS is a pointer to the swapped in local symbols.
13812
13813 LOCAL_SECTIONS is an array giving the section in the input file
13814 corresponding to the st_shndx field of each local symbol.
13815
13816 The global hash table entry for the global symbols can be found
13817 via elf_sym_hashes (input_bfd).
13818
1049f94e 13819 When generating relocatable output, this function must handle
5bd4f169
AM
13820 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13821 going to be the section symbol corresponding to the output
13822 section, which means that the addend must be adjusted
13823 accordingly. */
13824
b34976b6 13825static bfd_boolean
4ce794b7
AM
13826ppc64_elf_relocate_section (bfd *output_bfd,
13827 struct bfd_link_info *info,
13828 bfd *input_bfd,
13829 asection *input_section,
13830 bfd_byte *contents,
13831 Elf_Internal_Rela *relocs,
13832 Elf_Internal_Sym *local_syms,
13833 asection **local_sections)
5bd4f169 13834{
65f38f15 13835 struct ppc_link_hash_table *htab;
5bd4f169
AM
13836 Elf_Internal_Shdr *symtab_hdr;
13837 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13838 Elf_Internal_Rela *rel;
c316a17c 13839 Elf_Internal_Rela *wrel;
5bd4f169 13840 Elf_Internal_Rela *relend;
411e1bfb
AM
13841 Elf_Internal_Rela outrel;
13842 bfd_byte *loc;
411e1bfb 13843 struct got_entry **local_got_ents;
5bd4f169 13844 bfd_vma TOCstart;
b34976b6
AM
13845 bfd_boolean ret = TRUE;
13846 bfd_boolean is_opd;
794e51c0
AM
13847 /* Assume 'at' branch hints. */
13848 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 13849 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 13850
65f38f15 13851 /* Initialize howto table if needed. */
5bd4f169 13852 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13853 ppc_howto_init ();
13854
65f38f15 13855 htab = ppc_hash_table (info);
4dfe6ac6
NC
13856 if (htab == NULL)
13857 return FALSE;
ee75fd95
AM
13858
13859 /* Don't relocate stub sections. */
e7d1c40c 13860 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13861 return TRUE;
13862
7af5d5c4
AM
13863 if (!is_ppc64_elf (input_bfd))
13864 {
13865 bfd_set_error (bfd_error_wrong_format);
13866 return FALSE;
13867 }
0ffa91dd 13868
411e1bfb 13869 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13870 TOCstart = elf_gp (output_bfd);
0ffa91dd 13871 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13872 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13873 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13874
c316a17c 13875 rel = wrel = relocs;
5bd4f169 13876 relend = relocs + input_section->reloc_count;
c316a17c 13877 for (; rel < relend; wrel++, rel++)
5bd4f169 13878 {
04c9666a 13879 enum elf_ppc64_reloc_type r_type;
31c76678 13880 bfd_vma addend;
5bd4f169
AM
13881 bfd_reloc_status_type r;
13882 Elf_Internal_Sym *sym;
13883 asection *sec;
039b3fef
AM
13884 struct elf_link_hash_entry *h_elf;
13885 struct ppc_link_hash_entry *h;
13886 struct ppc_link_hash_entry *fdh;
5bd4f169 13887 const char *sym_name;
0d4792f7 13888 unsigned long r_symndx, toc_symndx;
3a71aa26 13889 bfd_vma toc_addend;
f961d9dd
AM
13890 unsigned char tls_mask, tls_gd, tls_type;
13891 unsigned char sym_type;
5bd4f169 13892 bfd_vma relocation;
23cedd1d 13893 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 13894 bfd_boolean warned;
bc30df16 13895 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13896 unsigned int insn;
e11840f9 13897 unsigned int mask;
721956f4
AM
13898 struct ppc_stub_hash_entry *stub_entry;
13899 bfd_vma max_br_offset;
13900 bfd_vma from;
c316a17c 13901 Elf_Internal_Rela orig_rel;
b80eed39
AM
13902 reloc_howto_type *howto;
13903 struct reloc_howto_struct alt_howto;
4a421c53
AM
13904 uint64_t pinsn;
13905 bfd_vma offset;
5bd4f169 13906
c316a17c
AM
13907 again:
13908 orig_rel = *rel;
13909
4ce794b7 13910 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13911 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13912
13913 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13914 symbol of the previous ADDR64 reloc. The symbol gives us the
13915 proper TOC base to use. */
13916 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13917 && wrel != relocs
13918 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13919 && is_opd)
c316a17c 13920 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13921
4ce794b7
AM
13922 sym = NULL;
13923 sec = NULL;
039b3fef 13924 h_elf = NULL;
4ce794b7 13925 sym_name = NULL;
b34976b6
AM
13926 unresolved_reloc = FALSE;
13927 warned = FALSE;
65f38f15 13928
0b13192e 13929 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13930 {
13931 /* It's a local symbol. */
74f0fb50 13932 struct _opd_sec_data *opd;
4025353c 13933
5bd4f169
AM
13934 sym = local_syms + r_symndx;
13935 sec = local_sections[r_symndx];
26c61ae5 13936 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13937 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13938 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13939 opd = get_opd_info (sec);
13940 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13941 {
51aecdc5
AM
13942 long adjust = opd->adjust[OPD_NDX (sym->st_value
13943 + rel->r_addend)];
4025353c
AM
13944 if (adjust == -1)
13945 relocation = 0;
13946 else
4cc603a5
AM
13947 {
13948 /* If this is a relocation against the opd section sym
13949 and we have edited .opd, adjust the reloc addend so
13950 that ld -r and ld --emit-relocs output is correct.
13951 If it is a reloc against some other .opd symbol,
13952 then the symbol value will be adjusted later. */
13953 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13954 rel->r_addend += adjust;
13955 else
13956 relocation += adjust;
13957 }
1e2f5b6e 13958 }
5bd4f169
AM
13959 }
13960 else
13961 {
62d887d4
L
13962 bfd_boolean ignored;
13963
b2a8e766
AM
13964 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13965 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13966 h_elf, sec, relocation,
62d887d4 13967 unresolved_reloc, warned, ignored);
039b3fef
AM
13968 sym_name = h_elf->root.root.string;
13969 sym_type = h_elf->type;
b69fdb4e
AM
13970 if (sec != NULL
13971 && sec->owner == output_bfd
13972 && strcmp (sec->name, ".opd") == 0)
13973 {
13974 /* This is a symbol defined in a linker script. All
13975 such are defined in output sections, even those
13976 defined by simple assignment from a symbol defined in
13977 an input section. Transfer the symbol to an
13978 appropriate input .opd section, so that a branch to
13979 this symbol will be mapped to the location specified
13980 by the opd entry. */
13981 struct bfd_link_order *lo;
13982 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13983 if (lo->type == bfd_indirect_link_order)
13984 {
13985 asection *isec = lo->u.indirect.section;
13986 if (h_elf->root.u.def.value >= isec->output_offset
13987 && h_elf->root.u.def.value < (isec->output_offset
13988 + isec->size))
13989 {
13990 h_elf->root.u.def.value -= isec->output_offset;
13991 h_elf->root.u.def.section = isec;
13992 sec = isec;
13993 break;
13994 }
13995 }
13996 }
5bd4f169 13997 }
039b3fef 13998 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13999
dbaa2011 14000 if (sec != NULL && discarded_section (sec))
c316a17c
AM
14001 {
14002 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14003 input_bfd, input_section,
0930cb30 14004 contents, rel->r_offset);
c316a17c
AM
14005 wrel->r_offset = rel->r_offset;
14006 wrel->r_info = 0;
14007 wrel->r_addend = 0;
14008
14009 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 14010 symbols defined in discarded sections. Not done for
c316a17c
AM
14011 non-debug to preserve relocs in .eh_frame which the
14012 eh_frame editing code expects to be present. */
14013 if (bfd_link_relocatable (info)
14014 && (input_section->flags & SEC_DEBUGGING))
14015 wrel--;
14016
14017 continue;
14018 }
ab96bf03 14019
0e1862bb 14020 if (bfd_link_relocatable (info))
c316a17c 14021 goto copy_reloc;
ab96bf03 14022
f40da81b
AM
14023 if (h != NULL && &h->elf == htab->elf.hgot)
14024 {
6f20ed8a 14025 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
14026 sec = bfd_abs_section_ptr;
14027 unresolved_reloc = FALSE;
14028 }
14029
951fd09b
AM
14030 /* TLS optimizations. Replace instruction sequences and relocs
14031 based on information we collected in tls_optimize. We edit
14032 RELOCS so that --emit-relocs will output something sensible
14033 for the final instruction stream. */
14034 tls_mask = 0;
14035 tls_gd = 0;
0d4792f7 14036 toc_symndx = 0;
727fc41e
AM
14037 if (h != NULL)
14038 tls_mask = h->tls_mask;
14039 else if (local_got_ents != NULL)
411e1bfb 14040 {
e054468f
AM
14041 struct plt_entry **local_plt = (struct plt_entry **)
14042 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 14043 unsigned char *lgot_masks = (unsigned char *)
e054468f 14044 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
14045 tls_mask = lgot_masks[r_symndx];
14046 }
37da22e5 14047 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
14048 && (r_type == R_PPC64_TLS
14049 || r_type == R_PPC64_TLSGD
14050 || r_type == R_PPC64_TLSLD))
14051 {
14052 /* Check for toc tls entries. */
f961d9dd 14053 unsigned char *toc_tls;
0d4792f7 14054
727fc41e
AM
14055 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14056 &local_syms, rel, input_bfd))
14057 return FALSE;
0d4792f7 14058
727fc41e
AM
14059 if (toc_tls)
14060 tls_mask = *toc_tls;
0d4792f7
AM
14061 }
14062
14063 /* Check that tls relocs are used with tls syms, and non-tls
14064 relocs are used with non-tls syms. */
cf35638d 14065 if (r_symndx != STN_UNDEF
0d4792f7
AM
14066 && r_type != R_PPC64_NONE
14067 && (h == NULL
039b3fef
AM
14068 || h->elf.root.type == bfd_link_hash_defined
14069 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
14070 && (IS_PPC64_TLS_RELOC (r_type)
14071 != (sym_type == STT_TLS
14072 || (sym_type == STT_SECTION
14073 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 14074 {
37da22e5 14075 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
14076 && (r_type == R_PPC64_TLS
14077 || r_type == R_PPC64_TLSGD
14078 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
14079 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14080 ;
14081 else
25f53a85 14082 info->callbacks->einfo
1d483afe 14083 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 14084 /* xgettext:c-format */
c1c8c1ef 14085 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 14086 /* xgettext:c-format */
c1c8c1ef 14087 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 14088 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
14089 ppc64_elf_howto_table[r_type]->name,
14090 sym_name);
411e1bfb
AM
14091 }
14092
14093 /* Ensure reloc mapping code below stays sane. */
14094 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14095 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14096 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14097 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14098 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14099 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14100 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14101 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14102 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14103 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14104 abort ();
0d4792f7 14105
411e1bfb
AM
14106 switch (r_type)
14107 {
14108 default:
411e1bfb
AM
14109 break;
14110
ba761f19 14111 case R_PPC64_LO_DS_OPT:
95f0d0d2 14112 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
14113 if ((insn & (0x3f << 26)) != 58u << 26)
14114 abort ();
14115 insn += (14u << 26) - (58u << 26);
95f0d0d2 14116 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
14117 r_type = R_PPC64_TOC16_LO;
14118 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14119 break;
14120
411e1bfb
AM
14121 case R_PPC64_TOC16:
14122 case R_PPC64_TOC16_LO:
14123 case R_PPC64_TOC16_DS:
14124 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
14125 {
14126 /* Check for toc tls entries. */
f961d9dd 14127 unsigned char *toc_tls;
951fd09b 14128 int retval;
411e1bfb 14129
3a71aa26
AM
14130 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14131 &local_syms, rel, input_bfd);
951fd09b 14132 if (retval == 0)
411e1bfb
AM
14133 return FALSE;
14134
14135 if (toc_tls)
14136 {
951fd09b 14137 tls_mask = *toc_tls;
411e1bfb
AM
14138 if (r_type == R_PPC64_TOC16_DS
14139 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 14140 {
37da22e5 14141 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
14142 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14143 goto toctprel;
14144 }
411e1bfb 14145 else
951fd09b
AM
14146 {
14147 /* If we found a GD reloc pair, then we might be
14148 doing a GD->IE transition. */
14149 if (retval == 2)
14150 {
14151 tls_gd = TLS_TPRELGD;
37da22e5
AM
14152 if ((tls_mask & TLS_TLS) != 0
14153 && (tls_mask & TLS_GD) == 0)
102890f0 14154 goto tls_ldgd_opt;
951fd09b
AM
14155 }
14156 else if (retval == 3)
14157 {
37da22e5
AM
14158 if ((tls_mask & TLS_TLS) != 0
14159 && (tls_mask & TLS_LD) == 0)
102890f0 14160 goto tls_ldgd_opt;
951fd09b
AM
14161 }
14162 }
411e1bfb
AM
14163 }
14164 }
14165 break;
14166
9d6ded02
AM
14167 case R_PPC64_GOT_TPREL16_HI:
14168 case R_PPC64_GOT_TPREL16_HA:
37da22e5 14169 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
14170 && (tls_mask & TLS_TPREL) == 0)
14171 {
14172 rel->r_offset -= d_offset;
95f0d0d2 14173 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
14174 r_type = R_PPC64_NONE;
14175 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14176 }
14177 break;
14178
411e1bfb
AM
14179 case R_PPC64_GOT_TPREL16_DS:
14180 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 14181 if ((tls_mask & TLS_TLS) != 0
951fd09b 14182 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14183 {
81407a69 14184 toctprel:
95f0d0d2 14185 insn = bfd_get_32 (input_bfd,
c316a17c 14186 contents + rel->r_offset - d_offset);
411e1bfb
AM
14187 insn &= 31 << 21;
14188 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 14189 bfd_put_32 (input_bfd, insn,
c316a17c 14190 contents + rel->r_offset - d_offset);
411e1bfb 14191 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
14192 if (toc_symndx != 0)
14193 {
14194 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14195 rel->r_addend = toc_addend;
0d4792f7
AM
14196 /* We changed the symbol. Start over in order to
14197 get h, sym, sec etc. right. */
c316a17c 14198 goto again;
0d4792f7
AM
14199 }
14200 else
14201 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14202 }
14203 break;
14204
14205 case R_PPC64_TLS:
37da22e5 14206 if ((tls_mask & TLS_TLS) != 0
951fd09b 14207 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14208 {
95f0d0d2 14209 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
14210 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14211 if (insn == 0)
411e1bfb 14212 abort ();
95f0d0d2 14213 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 14214 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
14215 PPC64_TPREL16_LO which is at low-order half-word. */
14216 rel->r_offset += d_offset;
0d4792f7
AM
14217 r_type = R_PPC64_TPREL16_LO;
14218 if (toc_symndx != 0)
14219 {
14220 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14221 rel->r_addend = toc_addend;
0d4792f7
AM
14222 /* We changed the symbol. Start over in order to
14223 get h, sym, sec etc. right. */
c316a17c 14224 goto again;
0d4792f7
AM
14225 }
14226 else
14227 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14228 }
14229 break;
14230
411e1bfb
AM
14231 case R_PPC64_GOT_TLSGD16_HI:
14232 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14233 tls_gd = TLS_TPRELGD;
37da22e5 14234 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
14235 goto tls_gdld_hi;
14236 break;
14237
411e1bfb
AM
14238 case R_PPC64_GOT_TLSLD16_HI:
14239 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 14240 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 14241 {
951fd09b
AM
14242 tls_gdld_hi:
14243 if ((tls_mask & tls_gd) != 0)
14244 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14245 + R_PPC64_GOT_TPREL16_DS);
14246 else
411e1bfb 14247 {
4fe5ca5b 14248 rel->r_offset -= d_offset;
95f0d0d2 14249 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 14250 r_type = R_PPC64_NONE;
411e1bfb 14251 }
951fd09b 14252 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14253 }
14254 break;
14255
951fd09b
AM
14256 case R_PPC64_GOT_TLSGD16:
14257 case R_PPC64_GOT_TLSGD16_LO:
14258 tls_gd = TLS_TPRELGD;
37da22e5 14259 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 14260 goto tls_ldgd_opt;
951fd09b 14261 break;
411e1bfb 14262
951fd09b
AM
14263 case R_PPC64_GOT_TLSLD16:
14264 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 14265 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 14266 {
b9f04fe0 14267 unsigned int insn1, insn2;
102890f0
AM
14268
14269 tls_ldgd_opt:
727fc41e
AM
14270 offset = (bfd_vma) -1;
14271 /* If not using the newer R_PPC64_TLSGD/LD to mark
14272 __tls_get_addr calls, we must trust that the call
14273 stays with its arg setup insns, ie. that the next
14274 reloc is the __tls_get_addr call associated with
14275 the current reloc. Edit both insns. */
14276 if (input_section->has_tls_get_addr_call
14277 && rel + 1 < relend
14278 && branch_reloc_hash_match (input_bfd, rel + 1,
14279 htab->tls_get_addr,
14280 htab->tls_get_addr_fd))
14281 offset = rel[1].r_offset;
b86ac8e3
AM
14282 /* We read the low GOT_TLS (or TOC16) insn because we
14283 need to keep the destination reg. It may be
14284 something other than the usual r3, and moved to r3
14285 before the call by intervening code. */
95f0d0d2 14286 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 14287 contents + rel->r_offset - d_offset);
102890f0 14288 if ((tls_mask & tls_gd) != 0)
411e1bfb 14289 {
102890f0 14290 /* IE */
b86ac8e3 14291 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
14292 insn1 |= 58 << 26; /* ld */
14293 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 14294 if (offset != (bfd_vma) -1)
f58d5a2d 14295 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
14296 if ((tls_mask & TLS_EXPLICIT) == 0)
14297 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14298 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 14299 else
102890f0
AM
14300 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14301 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14302 }
14303 else
14304 {
14305 /* LE */
b86ac8e3
AM
14306 insn1 &= 0x1f << 21;
14307 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
14308 insn2 = 0x38630000; /* addi 3,3,0 */
14309 if (tls_gd == 0)
951fd09b 14310 {
102890f0 14311 /* Was an LD reloc. */
1d483afe
AM
14312 if (toc_symndx)
14313 sec = local_sections[toc_symndx];
14314 for (r_symndx = 0;
14315 r_symndx < symtab_hdr->sh_info;
14316 r_symndx++)
14317 if (local_sections[r_symndx] == sec)
14318 break;
14319 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 14320 r_symndx = STN_UNDEF;
102890f0 14321 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 14322 if (r_symndx != STN_UNDEF)
1d483afe
AM
14323 rel->r_addend -= (local_syms[r_symndx].st_value
14324 + sec->output_offset
14325 + sec->output_section->vma);
951fd09b 14326 }
102890f0 14327 else if (toc_symndx != 0)
3a71aa26
AM
14328 {
14329 r_symndx = toc_symndx;
14330 rel->r_addend = toc_addend;
14331 }
102890f0
AM
14332 r_type = R_PPC64_TPREL16_HA;
14333 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
14334 if (offset != (bfd_vma) -1)
14335 {
14336 rel[1].r_info = ELF64_R_INFO (r_symndx,
14337 R_PPC64_TPREL16_LO);
14338 rel[1].r_offset = offset + d_offset;
14339 rel[1].r_addend = rel->r_addend;
14340 }
102890f0 14341 }
95f0d0d2 14342 bfd_put_32 (input_bfd, insn1,
3a71aa26 14343 contents + rel->r_offset - d_offset);
727fc41e 14344 if (offset != (bfd_vma) -1)
c96e0573
AM
14345 {
14346 bfd_put_32 (input_bfd, insn2, contents + offset);
14347 if (offset + 8 <= input_section->size)
14348 {
14349 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14350 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14351 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14352 }
14353 }
727fc41e
AM
14354 if ((tls_mask & tls_gd) == 0
14355 && (tls_gd == 0 || toc_symndx != 0))
14356 {
14357 /* We changed the symbol. Start over in order
14358 to get h, sym, sec etc. right. */
c316a17c 14359 goto again;
727fc41e
AM
14360 }
14361 }
14362 break;
14363
14364 case R_PPC64_TLSGD:
37da22e5 14365 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 14366 && rel + 1 < relend)
727fc41e 14367 {
b9f04fe0 14368 unsigned int insn2;
5663e321 14369 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 14370
4a421c53 14371 offset = rel->r_offset;
5663e321 14372 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
14373 {
14374 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
14375 if (r_type1 == R_PPC64_PLT_PCREL34
14376 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14377 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
14378 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14379 break;
14380 }
14381
14382 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14383 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14384
727fc41e
AM
14385 if ((tls_mask & TLS_TPRELGD) != 0)
14386 {
14387 /* IE */
14388 r_type = R_PPC64_NONE;
14389 insn2 = 0x7c636a14; /* add 3,3,13 */
14390 }
14391 else
14392 {
14393 /* LE */
14394 if (toc_symndx != 0)
14395 {
14396 r_symndx = toc_symndx;
14397 rel->r_addend = toc_addend;
14398 }
14399 r_type = R_PPC64_TPREL16_LO;
14400 rel->r_offset = offset + d_offset;
14401 insn2 = 0x38630000; /* addi 3,3,0 */
14402 }
14403 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14404 /* Zap the reloc on the _tls_get_addr call too. */
14405 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14406 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 14407 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 14408 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 14409 goto again;
411e1bfb 14410 }
411e1bfb
AM
14411 break;
14412
727fc41e 14413 case R_PPC64_TLSLD:
37da22e5 14414 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 14415 && rel + 1 < relend)
727fc41e 14416 {
b9f04fe0 14417 unsigned int insn2;
5663e321 14418 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 14419
4a421c53 14420 offset = rel->r_offset;
5663e321 14421 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
14422 {
14423 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
14424 if (r_type1 == R_PPC64_PLT_PCREL34
14425 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14426 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
14427 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14428 break;
14429 }
14430
14431 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14432 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14433
727fc41e
AM
14434 if (toc_symndx)
14435 sec = local_sections[toc_symndx];
14436 for (r_symndx = 0;
14437 r_symndx < symtab_hdr->sh_info;
14438 r_symndx++)
14439 if (local_sections[r_symndx] == sec)
14440 break;
14441 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 14442 r_symndx = STN_UNDEF;
727fc41e 14443 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 14444 if (r_symndx != STN_UNDEF)
727fc41e
AM
14445 rel->r_addend -= (local_syms[r_symndx].st_value
14446 + sec->output_offset
14447 + sec->output_section->vma);
14448
14449 r_type = R_PPC64_TPREL16_LO;
14450 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14451 rel->r_offset = offset + d_offset;
14452 /* Zap the reloc on the _tls_get_addr call too. */
14453 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14454 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 14455 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 14456 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 14457 goto again;
727fc41e
AM
14458 }
14459 break;
14460
411e1bfb 14461 case R_PPC64_DTPMOD64:
951fd09b
AM
14462 if (rel + 1 < relend
14463 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14464 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 14465 {
951fd09b
AM
14466 if ((tls_mask & TLS_GD) == 0)
14467 {
14468 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14469 if ((tls_mask & TLS_TPRELGD) != 0)
14470 r_type = R_PPC64_TPREL64;
14471 else
14472 {
4ce794b7 14473 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
14474 r_type = R_PPC64_NONE;
14475 }
14476 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14477 }
14478 }
14479 else
14480 {
14481 if ((tls_mask & TLS_LD) == 0)
411e1bfb 14482 {
4ce794b7 14483 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 14484 r_type = R_PPC64_NONE;
951fd09b 14485 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 14486 }
411e1bfb
AM
14487 }
14488 break;
14489
14490 case R_PPC64_TPREL64:
951fd09b 14491 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
14492 {
14493 r_type = R_PPC64_NONE;
14494 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14495 }
14496 break;
52a82034 14497
006589cf
AM
14498 case R_PPC64_ENTRY:
14499 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14500 if (!bfd_link_pic (info)
14501 && !info->traditional_format
14502 && relocation + 0x80008000 <= 0xffffffff)
14503 {
14504 unsigned int insn1, insn2;
14505
14506 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14507 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14508 if ((insn1 & ~0xfffc) == LD_R2_0R12
14509 && insn2 == ADD_R2_R2_R12)
14510 {
95f0d0d2 14511 bfd_put_32 (input_bfd,
006589cf
AM
14512 LIS_R2 + PPC_HA (relocation),
14513 contents + rel->r_offset);
95f0d0d2 14514 bfd_put_32 (input_bfd,
006589cf
AM
14515 ADDI_R2_R2 + PPC_LO (relocation),
14516 contents + rel->r_offset + 4);
14517 }
14518 }
14519 else
14520 {
14521 relocation -= (rel->r_offset
14522 + input_section->output_offset
14523 + input_section->output_section->vma);
14524 if (relocation + 0x80008000 <= 0xffffffff)
14525 {
14526 unsigned int insn1, insn2;
14527
14528 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14529 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14530 if ((insn1 & ~0xfffc) == LD_R2_0R12
14531 && insn2 == ADD_R2_R2_R12)
14532 {
95f0d0d2 14533 bfd_put_32 (input_bfd,
006589cf
AM
14534 ADDIS_R2_R12 + PPC_HA (relocation),
14535 contents + rel->r_offset);
95f0d0d2 14536 bfd_put_32 (input_bfd,
006589cf
AM
14537 ADDI_R2_R2 + PPC_LO (relocation),
14538 contents + rel->r_offset + 4);
14539 }
14540 }
14541 }
14542 break;
14543
52a82034
AM
14544 case R_PPC64_REL16_HA:
14545 /* If we are generating a non-PIC executable, edit
14546 . 0: addis 2,12,.TOC.-0b@ha
14547 . addi 2,2,.TOC.-0b@l
14548 used by ELFv2 global entry points to set up r2, to
14549 . lis 2,.TOC.@ha
14550 . addi 2,2,.TOC.@l
14551 if .TOC. is in range. */
0e1862bb 14552 if (!bfd_link_pic (info)
810d4e75 14553 && !info->traditional_format
006589cf 14554 && !htab->opd_abi
4f038ee5 14555 && rel->r_addend == d_offset
52a82034
AM
14556 && h != NULL && &h->elf == htab->elf.hgot
14557 && rel + 1 < relend
14558 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14559 && rel[1].r_offset == rel->r_offset + 4
14560 && rel[1].r_addend == rel->r_addend + 4
14561 && relocation + 0x80008000 <= 0xffffffff)
14562 {
14563 unsigned int insn1, insn2;
4a421c53 14564 offset = rel->r_offset - d_offset;
95f0d0d2
AM
14565 insn1 = bfd_get_32 (input_bfd, contents + offset);
14566 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14567 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14568 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14569 {
14570 r_type = R_PPC64_ADDR16_HA;
14571 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14572 rel->r_addend -= d_offset;
14573 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14574 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14575 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14576 }
14577 }
14578 break;
411e1bfb
AM
14579 }
14580
14581 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14582 insn = 0;
b25116a9
AM
14583 max_br_offset = 1 << 25;
14584 addend = rel->r_addend;
bc30df16 14585 reloc_dest = DEST_NORMAL;
65f38f15 14586 switch (r_type)
5bd4f169
AM
14587 {
14588 default:
65f38f15 14589 break;
5bd4f169 14590
3b421ab3
AM
14591 case R_PPC64_TOCSAVE:
14592 if (relocation + addend == (rel->r_offset
14593 + input_section->output_offset
14594 + input_section->output_section->vma)
14595 && tocsave_find (htab, NO_INSERT,
14596 &local_syms, rel, input_bfd))
14597 {
14598 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14599 if (insn == NOP
14600 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14601 bfd_put_32 (input_bfd,
14602 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14603 contents + rel->r_offset);
14604 }
14605 break;
14606
65f38f15
AM
14607 /* Branch taken prediction relocations. */
14608 case R_PPC64_ADDR14_BRTAKEN:
14609 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14610 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14611 /* Fall through. */
65f38f15 14612
86c76c7b 14613 /* Branch not taken prediction relocations. */
65f38f15
AM
14614 case R_PPC64_ADDR14_BRNTAKEN:
14615 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14616 insn |= bfd_get_32 (input_bfd,
411e1bfb 14617 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14618 /* Fall through. */
86c76c7b 14619
b25116a9
AM
14620 case R_PPC64_REL14:
14621 max_br_offset = 1 << 15;
1a0670f3 14622 /* Fall through. */
5bd4f169 14623
65f38f15 14624 case R_PPC64_REL24:
05d0e962 14625 case R_PPC64_REL24_NOTOC:
23cedd1d 14626 case R_PPC64_PLTCALL:
5663e321 14627 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
14628 /* Calls to functions with a different TOC, such as calls to
14629 shared objects, need to alter the TOC pointer. This is
14630 done using a linkage stub. A REL24 branching to these
14631 linkage stubs needs to be followed by a nop, as the nop
14632 will be replaced with an instruction to restore the TOC
14633 base pointer. */
8387904d 14634 fdh = h;
b31867b6
AM
14635 if (h != NULL
14636 && h->oh != NULL
14637 && h->oh->is_func_descriptor)
14638 fdh = ppc_follow_link (h->oh);
31c76678
DK
14639 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14640 htab);
5663e321
AM
14641 if ((r_type == R_PPC64_PLTCALL
14642 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 14643 && stub_entry != NULL
05d0e962
AM
14644 && stub_entry->stub_type >= ppc_stub_plt_call
14645 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
14646 stub_entry = NULL;
14647
6abec6bc 14648 if (stub_entry != NULL
d0abeec8
AM
14649 && ((stub_entry->stub_type >= ppc_stub_plt_call
14650 && stub_entry->stub_type <= ppc_stub_plt_call_both)
ad8e1ba5 14651 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
14652 || stub_entry->stub_type == ppc_stub_plt_branch_both
14653 || stub_entry->stub_type == ppc_stub_long_branch_r2off
14654 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 14655 {
b25116a9 14656 bfd_boolean can_plt_call = FALSE;
721956f4 14657
6e1816be
AM
14658 if (stub_entry->stub_type == ppc_stub_plt_call
14659 && !htab->opd_abi
14660 && htab->params->plt_localentry0 != 0
14661 && is_elfv2_localentry0 (&h->elf))
14662 {
14663 /* The function doesn't use or change r2. */
14664 can_plt_call = TRUE;
14665 }
05d0e962
AM
14666 else if (r_type == R_PPC64_REL24_NOTOC)
14667 {
14668 /* NOTOC calls don't need to restore r2. */
14669 can_plt_call = TRUE;
14670 }
6e1816be 14671
f378ab09 14672 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
14673 branch and link followed by a nop. The nop is
14674 replaced by an insn to restore r2. */
6e1816be 14675 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14676 {
ba8ca3e7
AM
14677 unsigned long br;
14678
14679 br = bfd_get_32 (input_bfd,
14680 contents + rel->r_offset);
14681 if ((br & 1) != 0)
41bd81ab 14682 {
ba8ca3e7
AM
14683 unsigned long nop;
14684
14685 nop = bfd_get_32 (input_bfd,
14686 contents + rel->r_offset + 4);
23cedd1d
AM
14687 if (nop == LD_R2_0R1 + STK_TOC (htab))
14688 can_plt_call = TRUE;
14689 else if (nop == NOP
14690 || nop == CROR_151515
14691 || nop == CROR_313131)
a7f2871e 14692 {
ba8ca3e7
AM
14693 if (h != NULL
14694 && (h == htab->tls_get_addr_fd
14695 || h == htab->tls_get_addr)
7c9cf415 14696 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14697 {
14698 /* Special stub used, leave nop alone. */
14699 }
14700 else
a078d95a
AM
14701 bfd_put_32 (input_bfd,
14702 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14703 contents + rel->r_offset + 4);
14704 can_plt_call = TRUE;
a7f2871e 14705 }
41bd81ab 14706 }
5bd4f169 14707 }
721956f4 14708
ba8ca3e7 14709 if (!can_plt_call && h != NULL)
721956f4 14710 {
ba8ca3e7
AM
14711 const char *name = h->elf.root.root.string;
14712
14713 if (*name == '.')
14714 ++name;
14715
14716 if (strncmp (name, "__libc_start_main", 17) == 0
14717 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14718 {
ba8ca3e7
AM
14719 /* Allow crt1 branch to go via a toc adjusting
14720 stub. Other calls that never return could do
14721 the same, if we could detect such. */
b25116a9 14722 can_plt_call = TRUE;
6ab189d5 14723 }
ba8ca3e7
AM
14724 }
14725
14726 if (!can_plt_call)
14727 {
14728 /* g++ as of 20130507 emits self-calls without a
14729 following nop. This is arguably wrong since we
14730 have conflicting information. On the one hand a
14731 global symbol and on the other a local call
14732 sequence, but don't error for this special case.
14733 It isn't possible to cheaply verify we have
14734 exactly such a call. Allow all calls to the same
14735 section. */
14736 asection *code_sec = sec;
14737
14738 if (get_opd_info (sec) != NULL)
ad8e1ba5 14739 {
ba8ca3e7
AM
14740 bfd_vma off = (relocation + addend
14741 - sec->output_section->vma
14742 - sec->output_offset);
bc30df16 14743
ba8ca3e7 14744 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14745 }
ba8ca3e7
AM
14746 if (code_sec == input_section)
14747 can_plt_call = TRUE;
14748 }
14749
14750 if (!can_plt_call)
14751 {
05d0e962
AM
14752 if (stub_entry->stub_type >= ppc_stub_plt_call
14753 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 14754 info->callbacks->einfo
695344c0 14755 /* xgettext:c-format */
c1c8c1ef 14756 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 14757 "(plt call stub)\n"),
4805fc55
AM
14758 input_bfd, input_section, rel->r_offset, sym_name);
14759 else
14760 info->callbacks->einfo
695344c0 14761 /* xgettext:c-format */
c1c8c1ef 14762 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 14763 "(toc save/adjust stub)\n"),
4805fc55 14764 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14765
14766 bfd_set_error (bfd_error_bad_value);
14767 ret = FALSE;
721956f4
AM
14768 }
14769
b25116a9 14770 if (can_plt_call
05d0e962
AM
14771 && stub_entry->stub_type >= ppc_stub_plt_call
14772 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
14773 unresolved_reloc = FALSE;
14774 }
14775
6abec6bc
AM
14776 if ((stub_entry == NULL
14777 || stub_entry->stub_type == ppc_stub_long_branch
14778 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14779 && get_opd_info (sec) != NULL)
14780 {
14781 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14782 bfd_vma off = (relocation + addend
14783 - sec->output_section->vma
14784 - sec->output_offset);
aef36ac1 14785 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14786 if (dest != (bfd_vma) -1)
14787 {
14788 relocation = dest;
14789 addend = 0;
bc30df16 14790 reloc_dest = DEST_OPD;
8387904d
AM
14791 }
14792 }
14793
b25116a9
AM
14794 /* If the branch is out of reach we ought to have a long
14795 branch stub. */
14796 from = (rel->r_offset
14797 + input_section->output_offset
14798 + input_section->output_section->vma);
14799
6911b7dc
AM
14800 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14801 ? fdh->elf.other
14802 : sym->st_other);
14803
6abec6bc
AM
14804 if (stub_entry != NULL
14805 && (stub_entry->stub_type == ppc_stub_long_branch
14806 || stub_entry->stub_type == ppc_stub_plt_branch)
14807 && (r_type == R_PPC64_ADDR14_BRTAKEN
14808 || r_type == R_PPC64_ADDR14_BRNTAKEN
14809 || (relocation + addend - from + max_br_offset
14810 < 2 * max_br_offset)))
14811 /* Don't use the stub if this branch is in range. */
14812 stub_entry = NULL;
b25116a9 14813
05d0e962
AM
14814 if (stub_entry != NULL
14815 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
14816 || stub_entry->stub_type == ppc_stub_long_branch_both
14817 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
14818 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14819 && (r_type != R_PPC64_REL24_NOTOC
14820 || ((fdh ? fdh->elf.other : sym->st_other)
14821 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
14822 && (relocation + addend - from + max_br_offset
14823 < 2 * max_br_offset))
14824 stub_entry = NULL;
14825
14826 if (stub_entry != NULL
14827 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
14828 || stub_entry->stub_type == ppc_stub_long_branch_both
14829 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14830 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14831 && r_type == R_PPC64_REL24_NOTOC
14832 && (relocation + addend - from + max_br_offset
14833 < 2 * max_br_offset))
14834 stub_entry = NULL;
14835
b25116a9
AM
14836 if (stub_entry != NULL)
14837 {
14838 /* Munge up the value and addend so that we call the stub
14839 rather than the procedure directly. */
a4b6fadd
AM
14840 asection *stub_sec = stub_entry->group->stub_sec;
14841
14842 if (stub_entry->stub_type == ppc_stub_save_res)
14843 relocation += (stub_sec->output_offset
14844 + stub_sec->output_section->vma
14845 + stub_sec->size - htab->sfpr->size
14846 - htab->sfpr->output_offset
14847 - htab->sfpr->output_section->vma);
14848 else
14849 relocation = (stub_entry->stub_offset
14850 + stub_sec->output_offset
14851 + stub_sec->output_section->vma);
b25116a9 14852 addend = 0;
bc30df16 14853 reloc_dest = DEST_STUB;
3b421ab3 14854
05d0e962
AM
14855 if (((stub_entry->stub_type == ppc_stub_plt_call
14856 && ALWAYS_EMIT_R2SAVE)
14857 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14858 || stub_entry->stub_type == ppc_stub_plt_call_both)
e81b4c93
AM
14859 && !(h != NULL
14860 && (h == htab->tls_get_addr_fd
14861 || h == htab->tls_get_addr)
14862 && htab->params->tls_get_addr_opt)
3b421ab3
AM
14863 && rel + 1 < relend
14864 && rel[1].r_offset == rel->r_offset + 4
14865 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14866 relocation += 4;
05d0e962
AM
14867 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
14868 || stub_entry->stub_type == ppc_stub_plt_branch_both
14869 || stub_entry->stub_type == ppc_stub_plt_call_both)
14870 && r_type == R_PPC64_REL24_NOTOC)
14871 relocation += 4;
5663e321
AM
14872
14873 if (r_type == R_PPC64_REL24_NOTOC
14874 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
14875 || stub_entry->stub_type == ppc_stub_plt_call_both))
14876 htab->notoc_plt = 1;
b25116a9
AM
14877 }
14878
14879 if (insn != 0)
14880 {
794e51c0 14881 if (is_isa_v2)
721956f4 14882 {
b25116a9
AM
14883 /* Set 'a' bit. This is 0b00010 in BO field for branch
14884 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14885 for branch on CTR insns (BO == 1a00t or 1a01t). */
14886 if ((insn & (0x14 << 21)) == (0x04 << 21))
14887 insn |= 0x02 << 21;
14888 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14889 insn |= 0x08 << 21;
14890 else
14891 break;
14892 }
14893 else
14894 {
14895 /* Invert 'y' bit if not the default. */
4cc603a5 14896 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14897 insn ^= 0x01 << 21;
721956f4 14898 }
b25116a9 14899
95f0d0d2 14900 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 14901 }
e86ce104 14902
06da1e8e
AM
14903 /* NOP out calls to undefined weak functions.
14904 We can thus call a weak function without first
14905 checking whether the function is defined. */
b25116a9 14906 else if (h != NULL
039b3fef 14907 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14908 && h->elf.dynindx == -1
05d0e962
AM
14909 && (r_type == R_PPC64_REL24
14910 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 14911 && relocation == 0
4cc603a5 14912 && addend == 0)
e86ce104 14913 {
95f0d0d2 14914 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 14915 goto copy_reloc;
e86ce104 14916 }
65f38f15 14917 break;
066f4018
AM
14918
14919 case R_PPC64_GOT16_DS:
14920 from = TOCstart + htab->sec_info[input_section->id].toc_off;
14921 if (relocation + addend - from + 0x8000 < 0x10000
14922 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14923 {
14924 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14925 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
14926 {
14927 insn += (14u << 26) - (58u << 26);
14928 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
14929 r_type = R_PPC64_TOC16;
14930 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14931 }
14932 }
14933 break;
14934
14935 case R_PPC64_GOT16_LO_DS:
14936 case R_PPC64_GOT16_HA:
14937 from = TOCstart + htab->sec_info[input_section->id].toc_off;
14938 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
14939 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14940 {
14941 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14942 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
14943 {
14944 insn += (14u << 26) - (58u << 26);
14945 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
14946 r_type = R_PPC64_TOC16_LO;
14947 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14948 }
14949 else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
14950 {
14951 r_type = R_PPC64_TOC16_HA;
14952 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14953 }
14954 }
14955 break;
4a421c53
AM
14956
14957 case R_PPC64_GOT_PCREL34:
14958 from = (rel->r_offset
14959 + input_section->output_section->vma
14960 + input_section->output_offset);
14961 if (relocation - from + (1ULL << 33) < 1ULL << 34
14962 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14963 {
14964 offset = rel->r_offset;
14965 pinsn = bfd_get_32 (input_bfd, contents + offset);
14966 pinsn <<= 32;
14967 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
14968 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
14969 == ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
14970 {
14971 /* Replace with paddi. */
14972 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
14973 r_type = R_PPC64_PCREL34;
14974 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14975 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
14976 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
14977 goto pcrelopt;
14978 }
14979 }
14980 break;
14981
14982 case R_PPC64_PCREL34:
14983 if (SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14984 {
14985 offset = rel->r_offset;
14986 pinsn = bfd_get_32 (input_bfd, contents + offset);
14987 pinsn <<= 32;
14988 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
14989 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
14990 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
14991 | (14ULL << 26) /* paddi */))
14992 {
14993 pcrelopt:
14994 if (rel + 1 < relend
14995 && rel[1].r_offset == offset
14996 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT))
14997 {
14998 bfd_vma off2 = rel[1].r_addend;
14999 if (off2 == 0)
15000 /* zero means next insn. */
15001 off2 = 8;
15002 off2 += offset;
15003 if (off2 + 4 <= input_section->size)
15004 {
15005 uint64_t pinsn2;
15006 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
15007 pinsn2 <<= 32;
15008 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15009 break;
15010 if (xlate_pcrel_opt (&pinsn, &pinsn2))
15011 {
15012 bfd_put_32 (input_bfd, pinsn >> 32,
15013 contents + offset);
15014 bfd_put_32 (input_bfd, pinsn,
15015 contents + offset + 4);
15016 bfd_put_32 (input_bfd, pinsn2 >> 32,
15017 contents + off2);
15018 }
15019 }
15020 }
15021 }
15022 }
15023 break;
65f38f15 15024 }
5bd4f169 15025
65f38f15 15026 /* Set `addend'. */
411e1bfb 15027 tls_type = 0;
23cedd1d 15028 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
15029 switch (r_type)
15030 {
15031 default:
cf97bcb0
AM
15032 /* xgettext:c-format */
15033 _bfd_error_handler (_("%pB: %s unsupported"),
15034 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 15035
65f38f15 15036 bfd_set_error (bfd_error_bad_value);
b34976b6 15037 ret = FALSE;
c316a17c 15038 goto copy_reloc;
5bd4f169 15039
65f38f15 15040 case R_PPC64_NONE:
411e1bfb 15041 case R_PPC64_TLS:
727fc41e
AM
15042 case R_PPC64_TLSGD:
15043 case R_PPC64_TLSLD:
3b421ab3 15044 case R_PPC64_TOCSAVE:
04c9666a
AM
15045 case R_PPC64_GNU_VTINHERIT:
15046 case R_PPC64_GNU_VTENTRY:
006589cf 15047 case R_PPC64_ENTRY:
4a421c53 15048 case R_PPC64_PCREL_OPT:
c316a17c 15049 goto copy_reloc;
5bd4f169
AM
15050
15051 /* GOT16 relocations. Like an ADDR16 using the symbol's
15052 address in the GOT as relocation value instead of the
411e1bfb 15053 symbol's value itself. Also, create a GOT entry for the
5bd4f169 15054 symbol and put the symbol value there. */
411e1bfb
AM
15055 case R_PPC64_GOT_TLSGD16:
15056 case R_PPC64_GOT_TLSGD16_LO:
15057 case R_PPC64_GOT_TLSGD16_HI:
15058 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 15059 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
15060 goto dogot;
15061
15062 case R_PPC64_GOT_TLSLD16:
15063 case R_PPC64_GOT_TLSLD16_LO:
15064 case R_PPC64_GOT_TLSLD16_HI:
15065 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 15066 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
15067 goto dogot;
15068
15069 case R_PPC64_GOT_TPREL16_DS:
15070 case R_PPC64_GOT_TPREL16_LO_DS:
15071 case R_PPC64_GOT_TPREL16_HI:
15072 case R_PPC64_GOT_TPREL16_HA:
15073 tls_type = TLS_TLS | TLS_TPREL;
15074 goto dogot;
15075
15076 case R_PPC64_GOT_DTPREL16_DS:
15077 case R_PPC64_GOT_DTPREL16_LO_DS:
15078 case R_PPC64_GOT_DTPREL16_HI:
15079 case R_PPC64_GOT_DTPREL16_HA:
15080 tls_type = TLS_TLS | TLS_DTPREL;
15081 goto dogot;
15082
65f38f15
AM
15083 case R_PPC64_GOT16:
15084 case R_PPC64_GOT16_LO:
15085 case R_PPC64_GOT16_HI:
15086 case R_PPC64_GOT16_HA:
15087 case R_PPC64_GOT16_DS:
15088 case R_PPC64_GOT16_LO_DS:
5663e321 15089 case R_PPC64_GOT_PCREL34:
411e1bfb 15090 dogot:
5bd4f169
AM
15091 {
15092 /* Relocation is to the entry for this symbol in the global
15093 offset table. */
e717da7e 15094 asection *got;
d881513a 15095 bfd_vma *offp;
5bd4f169 15096 bfd_vma off;
d881513a 15097 unsigned long indx = 0;
927be08e 15098 struct got_entry *ent;
5663e321
AM
15099 bfd_vma sym_addend = orig_rel.r_addend;
15100
15101 if (r_type == R_PPC64_GOT_PCREL34)
15102 sym_addend = 0;
65f38f15 15103
d881513a
AM
15104 if (tls_type == (TLS_TLS | TLS_LD)
15105 && (h == NULL
f5385ebf 15106 || !h->elf.def_dynamic))
927be08e 15107 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 15108 else
5bd4f169 15109 {
d881513a
AM
15110 if (h != NULL)
15111 {
f0158f44
AM
15112 if (!htab->elf.dynamic_sections_created
15113 || h->elf.dynindx == -1
15114 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 15115 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
15116 /* This is actually a static link, or it is a
15117 -Bsymbolic link and the symbol is defined
15118 locally, or the symbol was forced to be local
15119 because of a version file. */
15120 ;
15121 else
15122 {
039b3fef 15123 indx = h->elf.dynindx;
d881513a
AM
15124 unresolved_reloc = FALSE;
15125 }
039b3fef 15126 ent = h->elf.got.glist;
d881513a 15127 }
411e1bfb 15128 else
5bd4f169 15129 {
d881513a
AM
15130 if (local_got_ents == NULL)
15131 abort ();
15132 ent = local_got_ents[r_symndx];
5bd4f169 15133 }
d881513a
AM
15134
15135 for (; ent != NULL; ent = ent->next)
5663e321 15136 if (ent->addend == sym_addend
e717da7e 15137 && ent->owner == input_bfd
d881513a
AM
15138 && ent->tls_type == tls_type)
15139 break;
5bd4f169 15140 }
411e1bfb 15141
927be08e
AM
15142 if (ent == NULL)
15143 abort ();
15144 if (ent->is_indirect)
15145 ent = ent->got.ent;
15146 offp = &ent->got.offset;
15147 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
15148 if (got == NULL)
15149 abort ();
15150
411e1bfb
AM
15151 /* The offset must always be a multiple of 8. We use the
15152 least significant bit to record whether we have already
15153 processed this entry. */
d881513a 15154 off = *offp;
411e1bfb
AM
15155 if ((off & 1) != 0)
15156 off &= ~1;
5bd4f169
AM
15157 else
15158 {
411e1bfb
AM
15159 /* Generate relocs for the dynamic linker, except in
15160 the case of TLSLD where we'll use one entry per
15161 module. */
25f23106
AM
15162 asection *relgot;
15163 bfd_boolean ifunc;
e717da7e 15164
d881513a 15165 *offp = off | 1;
25f23106
AM
15166 relgot = NULL;
15167 ifunc = (h != NULL
15168 ? h->elf.type == STT_GNU_IFUNC
15169 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 15170 if (ifunc)
82e66161
AM
15171 {
15172 relgot = htab->elf.irelplt;
15173 if (indx == 0)
15174 htab->local_ifunc_resolver = 1;
15175 else if (is_static_defined (&h->elf))
15176 htab->maybe_local_ifunc_resolver = 1;
15177 }
f0158f44
AM
15178 else if (indx != 0
15179 || (bfd_link_pic (info)
15180 && (h == NULL
21d68fcd 15181 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 15182 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
15183 && !h->elf.def_dynamic))
15184 && !(tls_type == (TLS_TLS | TLS_TPREL)
15185 && bfd_link_executable (info)
15186 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 15187 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 15188 if (relgot != NULL)
5bd4f169 15189 {
e717da7e
AM
15190 outrel.r_offset = (got->output_section->vma
15191 + got->output_offset
411e1bfb 15192 + off);
5663e321 15193 outrel.r_addend = sym_addend;
d881513a 15194 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 15195 {
411e1bfb 15196 outrel.r_addend = 0;
e515b051 15197 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
15198 if (tls_type == (TLS_TLS | TLS_GD))
15199 {
e717da7e
AM
15200 loc = relgot->contents;
15201 loc += (relgot->reloc_count++
d881513a
AM
15202 * sizeof (Elf64_External_Rela));
15203 bfd_elf64_swap_reloca_out (output_bfd,
15204 &outrel, loc);
e515b051 15205 outrel.r_offset += 8;
5663e321 15206 outrel.r_addend = sym_addend;
d881513a
AM
15207 outrel.r_info
15208 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 15209 }
411e1bfb 15210 }
951fd09b 15211 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 15212 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 15213 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 15214 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
15215 else if (indx != 0)
15216 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15217 else
81407a69 15218 {
25f23106
AM
15219 if (ifunc)
15220 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15221 else
15222 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
15223
15224 /* Write the .got section contents for the sake
15225 of prelink. */
e717da7e 15226 loc = got->contents + off;
23fbd6fa
JJ
15227 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15228 loc);
81407a69 15229 }
81407a69
AM
15230
15231 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
15232 {
15233 outrel.r_addend += relocation;
15234 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
15235 {
15236 if (htab->elf.tls_sec == NULL)
15237 outrel.r_addend = 0;
15238 else
15239 outrel.r_addend -= htab->elf.tls_sec->vma;
15240 }
e515b051 15241 }
e717da7e
AM
15242 loc = relgot->contents;
15243 loc += (relgot->reloc_count++
411e1bfb
AM
15244 * sizeof (Elf64_External_Rela));
15245 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15246 }
15247
ad8e1ba5 15248 /* Init the .got section contents here if we're not
81407a69 15249 emitting a reloc. */
d881513a 15250 else
411e1bfb 15251 {
5663e321 15252 relocation += sym_addend;
f0158f44 15253 if (tls_type != 0)
411e1bfb 15254 {
989f9879
AM
15255 if (htab->elf.tls_sec == NULL)
15256 relocation = 0;
15257 else
15258 {
f0158f44
AM
15259 if (tls_type & TLS_LD)
15260 relocation = 0;
15261 else
15262 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 15263 if (tls_type & TLS_TPREL)
989f9879
AM
15264 relocation += DTP_OFFSET - TP_OFFSET;
15265 }
5bd4f169 15266
f0158f44 15267 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
15268 {
15269 bfd_put_64 (output_bfd, relocation,
e717da7e 15270 got->contents + off + 8);
676ee2b5 15271 relocation = 1;
7b609f53 15272 }
411e1bfb
AM
15273 }
15274 bfd_put_64 (output_bfd, relocation,
e717da7e 15275 got->contents + off);
5bd4f169
AM
15276 }
15277 }
15278
65f38f15
AM
15279 if (off >= (bfd_vma) -2)
15280 abort ();
15281
bf102f86 15282 relocation = got->output_section->vma + got->output_offset + off;
5663e321
AM
15283 if (r_type != R_PPC64_GOT_PCREL34)
15284 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 15285 }
65f38f15
AM
15286 break;
15287
15288 case R_PPC64_PLT16_HA:
15289 case R_PPC64_PLT16_HI:
15290 case R_PPC64_PLT16_LO:
08be3224 15291 case R_PPC64_PLT16_LO_DS:
5663e321
AM
15292 case R_PPC64_PLT_PCREL34:
15293 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
15294 case R_PPC64_PLT32:
15295 case R_PPC64_PLT64:
23cedd1d 15296 case R_PPC64_PLTSEQ:
5663e321 15297 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 15298 case R_PPC64_PLTCALL:
5663e321 15299 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
15300 /* Relocation is to the entry for this symbol in the
15301 procedure linkage table. */
23cedd1d 15302 unresolved_reloc = TRUE;
cbf95972
AM
15303 {
15304 struct plt_entry **plt_list = NULL;
15305 if (h != NULL)
15306 plt_list = &h->elf.plt.plist;
15307 else if (local_got_ents != NULL)
15308 {
15309 struct plt_entry **local_plt = (struct plt_entry **)
15310 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 15311 plt_list = local_plt + r_symndx;
cbf95972
AM
15312 }
15313 if (plt_list)
15314 {
15315 struct plt_entry *ent;
5663e321
AM
15316 bfd_vma sym_addend = orig_rel.r_addend;
15317
15318 if (r_type == R_PPC64_PLT_PCREL34
15319 || r_type == R_PPC64_PLT_PCREL34_NOTOC)
15320 sym_addend = 0;
65f38f15 15321
cbf95972
AM
15322 for (ent = *plt_list; ent != NULL; ent = ent->next)
15323 if (ent->plt.offset != (bfd_vma) -1
5663e321 15324 && ent->addend == sym_addend)
cbf95972
AM
15325 {
15326 asection *plt;
08be3224 15327 bfd_vma got;
cbf95972
AM
15328
15329 plt = htab->elf.splt;
15330 if (!htab->elf.dynamic_sections_created
15331 || h == NULL
15332 || h->elf.dynindx == -1)
2d7ad24e
AM
15333 {
15334 if (h != NULL
15335 ? h->elf.type == STT_GNU_IFUNC
15336 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15337 plt = htab->elf.iplt;
15338 else
15339 plt = htab->pltlocal;
15340 }
15341 relocation = (plt->output_section->vma
15342 + plt->output_offset
15343 + ent->plt.offset);
08be3224 15344 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
15345 || r_type == R_PPC64_PLT16_HI
15346 || r_type == R_PPC64_PLT16_LO
15347 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
15348 {
15349 got = (elf_gp (output_bfd)
15350 + htab->sec_info[input_section->id].toc_off);
15351 relocation -= got;
15352 }
5663e321
AM
15353 if (r_type != R_PPC64_PLT_PCREL34
15354 && r_type != R_PPC64_PLT_PCREL34_NOTOC)
15355 addend = 0;
cbf95972
AM
15356 unresolved_reloc = FALSE;
15357 break;
15358 }
15359 }
15360 }
65f38f15 15361 break;
5bd4f169 15362
0b13192e
AM
15363 case R_PPC64_TOC:
15364 /* Relocation value is TOC base. */
15365 relocation = TOCstart;
cf35638d 15366 if (r_symndx == STN_UNDEF)
6f20ed8a 15367 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
15368 else if (unresolved_reloc)
15369 ;
6f20ed8a
AM
15370 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15371 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
15372 else
15373 unresolved_reloc = TRUE;
ab96bf03 15374 goto dodyn;
0b13192e 15375
5bd4f169
AM
15376 /* TOC16 relocs. We want the offset relative to the TOC base,
15377 which is the address of the start of the TOC plus 0x8000.
15378 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15379 in this order. */
65f38f15
AM
15380 case R_PPC64_TOC16:
15381 case R_PPC64_TOC16_LO:
15382 case R_PPC64_TOC16_HI:
15383 case R_PPC64_TOC16_DS:
15384 case R_PPC64_TOC16_LO_DS:
15385 case R_PPC64_TOC16_HA:
6f20ed8a 15386 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
15387 break;
15388
15389 /* Relocate against the beginning of the section. */
65f38f15
AM
15390 case R_PPC64_SECTOFF:
15391 case R_PPC64_SECTOFF_LO:
15392 case R_PPC64_SECTOFF_HI:
15393 case R_PPC64_SECTOFF_DS:
15394 case R_PPC64_SECTOFF_LO_DS:
15395 case R_PPC64_SECTOFF_HA:
4ce794b7 15396 if (sec != NULL)
65f38f15 15397 addend -= sec->output_section->vma;
5bd4f169
AM
15398 break;
15399
25f23106
AM
15400 case R_PPC64_REL16:
15401 case R_PPC64_REL16_LO:
15402 case R_PPC64_REL16_HI:
15403 case R_PPC64_REL16_HA:
4a969973
AM
15404 case R_PPC64_REL16_HIGH:
15405 case R_PPC64_REL16_HIGHA:
15406 case R_PPC64_REL16_HIGHER:
15407 case R_PPC64_REL16_HIGHERA:
15408 case R_PPC64_REL16_HIGHEST:
15409 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
15410 case R_PPC64_REL16_HIGHER34:
15411 case R_PPC64_REL16_HIGHERA34:
15412 case R_PPC64_REL16_HIGHEST34:
15413 case R_PPC64_REL16_HIGHESTA34:
a680de9a 15414 case R_PPC64_REL16DX_HA:
721956f4
AM
15415 case R_PPC64_REL14:
15416 case R_PPC64_REL14_BRNTAKEN:
15417 case R_PPC64_REL14_BRTAKEN:
5d1634d7 15418 case R_PPC64_REL24:
05d0e962 15419 case R_PPC64_REL24_NOTOC:
5663e321
AM
15420 case R_PPC64_PCREL34:
15421 case R_PPC64_PCREL28:
5d1634d7
AM
15422 break;
15423
411e1bfb
AM
15424 case R_PPC64_TPREL16:
15425 case R_PPC64_TPREL16_LO:
15426 case R_PPC64_TPREL16_HI:
15427 case R_PPC64_TPREL16_HA:
15428 case R_PPC64_TPREL16_DS:
15429 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
15430 case R_PPC64_TPREL16_HIGH:
15431 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
15432 case R_PPC64_TPREL16_HIGHER:
15433 case R_PPC64_TPREL16_HIGHERA:
15434 case R_PPC64_TPREL16_HIGHEST:
15435 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
15436 if (h != NULL
15437 && h->elf.root.type == bfd_link_hash_undefweak
15438 && h->elf.dynindx == -1)
15439 {
15440 /* Make this relocation against an undefined weak symbol
15441 resolve to zero. This is really just a tweak, since
15442 code using weak externs ought to check that they are
15443 defined before using them. */
15444 bfd_byte *p = contents + rel->r_offset - d_offset;
15445
95f0d0d2 15446 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
15447 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15448 if (insn != 0)
95f0d0d2 15449 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
15450 break;
15451 }
989f9879
AM
15452 if (htab->elf.tls_sec != NULL)
15453 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
15454 /* The TPREL16 relocs shouldn't really be used in shared
15455 libs or with non-local symbols as that will result in
15456 DT_TEXTREL being set, but support them anyway. */
15457 goto dodyn;
411e1bfb
AM
15458
15459 case R_PPC64_DTPREL16:
15460 case R_PPC64_DTPREL16_LO:
15461 case R_PPC64_DTPREL16_HI:
15462 case R_PPC64_DTPREL16_HA:
15463 case R_PPC64_DTPREL16_DS:
15464 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
15465 case R_PPC64_DTPREL16_HIGH:
15466 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
15467 case R_PPC64_DTPREL16_HIGHER:
15468 case R_PPC64_DTPREL16_HIGHERA:
15469 case R_PPC64_DTPREL16_HIGHEST:
15470 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
15471 if (htab->elf.tls_sec != NULL)
15472 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
15473 break;
15474
45965137
AM
15475 case R_PPC64_ADDR64_LOCAL:
15476 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15477 ? h->elf.other
15478 : sym->st_other);
15479 break;
15480
e515b051
AM
15481 case R_PPC64_DTPMOD64:
15482 relocation = 1;
15483 addend = 0;
15484 goto dodyn;
15485
411e1bfb 15486 case R_PPC64_TPREL64:
989f9879
AM
15487 if (htab->elf.tls_sec != NULL)
15488 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
15489 goto dodyn;
15490
15491 case R_PPC64_DTPREL64:
989f9879
AM
15492 if (htab->elf.tls_sec != NULL)
15493 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 15494 /* Fall through. */
411e1bfb 15495
65f38f15
AM
15496 /* Relocations that may need to be propagated if this is a
15497 dynamic object. */
04c9666a 15498 case R_PPC64_REL30:
65f38f15
AM
15499 case R_PPC64_REL32:
15500 case R_PPC64_REL64:
15501 case R_PPC64_ADDR14:
15502 case R_PPC64_ADDR14_BRNTAKEN:
15503 case R_PPC64_ADDR14_BRTAKEN:
15504 case R_PPC64_ADDR16:
15505 case R_PPC64_ADDR16_DS:
15506 case R_PPC64_ADDR16_HA:
15507 case R_PPC64_ADDR16_HI:
f9c6b907
AM
15508 case R_PPC64_ADDR16_HIGH:
15509 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15510 case R_PPC64_ADDR16_HIGHER:
15511 case R_PPC64_ADDR16_HIGHERA:
15512 case R_PPC64_ADDR16_HIGHEST:
15513 case R_PPC64_ADDR16_HIGHESTA:
15514 case R_PPC64_ADDR16_LO:
15515 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
15516 case R_PPC64_ADDR16_HIGHER34:
15517 case R_PPC64_ADDR16_HIGHERA34:
15518 case R_PPC64_ADDR16_HIGHEST34:
15519 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 15520 case R_PPC64_ADDR24:
65f38f15
AM
15521 case R_PPC64_ADDR32:
15522 case R_PPC64_ADDR64:
15523 case R_PPC64_UADDR16:
15524 case R_PPC64_UADDR32:
15525 case R_PPC64_UADDR64:
5663e321
AM
15526 case R_PPC64_D34:
15527 case R_PPC64_D34_LO:
15528 case R_PPC64_D34_HI30:
15529 case R_PPC64_D34_HA30:
15530 case R_PPC64_D28:
411e1bfb 15531 dodyn:
5d1634d7 15532 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
15533 break;
15534
41bd81ab
AM
15535 if (NO_OPD_RELOCS && is_opd)
15536 break;
15537
8a9e8e72 15538 if (bfd_link_pic (info)
b1b07054
AM
15539 ? ((h == NULL
15540 || h->dyn_relocs != NULL)
15541 && ((h != NULL && pc_dynrelocs (h))
15542 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
15543 : (h != NULL
15544 ? h->dyn_relocs != NULL
d311bc8b 15545 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 15546 {
b34976b6 15547 bfd_boolean skip, relocate;
65f38f15 15548 asection *sreloc;
1cf1f670 15549 bfd_vma out_off;
82e66161 15550 long indx = 0;
65f38f15
AM
15551
15552 /* When generating a dynamic object, these relocations
15553 are copied into the output file to be resolved at run
15554 time. */
15555
b34976b6
AM
15556 skip = FALSE;
15557 relocate = FALSE;
65f38f15 15558
1cf1f670
AM
15559 out_off = _bfd_elf_section_offset (output_bfd, info,
15560 input_section, rel->r_offset);
15561 if (out_off == (bfd_vma) -1)
b34976b6 15562 skip = TRUE;
1cf1f670 15563 else if (out_off == (bfd_vma) -2)
b34976b6 15564 skip = TRUE, relocate = TRUE;
1cf1f670
AM
15565 out_off += (input_section->output_section->vma
15566 + input_section->output_offset);
15567 outrel.r_offset = out_off;
411e1bfb 15568 outrel.r_addend = rel->r_addend;
65f38f15 15569
1cf1f670
AM
15570 /* Optimize unaligned reloc use. */
15571 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15572 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15573 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15574 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15575 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15576 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15577 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15578 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15579 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15580
65f38f15 15581 if (skip)
0bb2d96a 15582 memset (&outrel, 0, sizeof outrel);
afe397ea 15583 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
15584 && !is_opd
15585 && r_type != R_PPC64_TOC)
14acf4dc 15586 {
82e66161
AM
15587 indx = h->elf.dynindx;
15588 BFD_ASSERT (indx != -1);
15589 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 15590 }
65f38f15
AM
15591 else
15592 {
41bd81ab
AM
15593 /* This symbol is local, or marked to become local,
15594 or this is an opd section reloc which must point
15595 at a local function. */
65f38f15 15596 outrel.r_addend += relocation;
e86ce104 15597 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 15598 {
3fad3c7c 15599 if (is_opd && h != NULL)
afbe61cf
AM
15600 {
15601 /* Lie about opd entries. This case occurs
15602 when building shared libraries and we
15603 reference a function in another shared
3fad3c7c
AM
15604 lib. The same thing happens for a weak
15605 definition in an application that's
15606 overridden by a strong definition in a
15607 shared lib. (I believe this is a generic
15608 bug in binutils handling of weak syms.)
15609 In these cases we won't use the opd
1e2f5b6e 15610 entry in this lib. */
b34976b6 15611 unresolved_reloc = FALSE;
afbe61cf 15612 }
25f23106
AM
15613 if (!is_opd
15614 && r_type == R_PPC64_ADDR64
15615 && (h != NULL
15616 ? h->elf.type == STT_GNU_IFUNC
15617 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15618 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15619 else
15620 {
15621 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 15622
25f23106
AM
15623 /* We need to relocate .opd contents for ld.so.
15624 Prelink also wants simple and consistent rules
15625 for relocs. This make all RELATIVE relocs have
15626 *r_offset equal to r_addend. */
15627 relocate = TRUE;
15628 }
65f38f15
AM
15629 }
15630 else
15631 {
25f23106
AM
15632 if (h != NULL
15633 ? h->elf.type == STT_GNU_IFUNC
15634 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15635 {
25f53a85 15636 info->callbacks->einfo
695344c0 15637 /* xgettext:c-format */
174d0a74 15638 (_("%H: %s for indirect "
c1c8c1ef 15639 "function `%pT' unsupported\n"),
25f53a85 15640 input_bfd, input_section, rel->r_offset,
25f23106
AM
15641 ppc64_elf_howto_table[r_type]->name,
15642 sym_name);
15643 ret = FALSE;
15644 }
cf35638d 15645 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
15646 ;
15647 else if (sec == NULL || sec->owner == NULL)
15648 {
15649 bfd_set_error (bfd_error_bad_value);
b34976b6 15650 return FALSE;
65f38f15
AM
15651 }
15652 else
15653 {
15654 asection *osec;
15655
15656 osec = sec->output_section;
15657 indx = elf_section_data (osec)->dynindx;
15658
74541ad4
AM
15659 if (indx == 0)
15660 {
15661 if ((osec->flags & SEC_READONLY) == 0
15662 && htab->elf.data_index_section != NULL)
15663 osec = htab->elf.data_index_section;
15664 else
15665 osec = htab->elf.text_index_section;
15666 indx = elf_section_data (osec)->dynindx;
15667 }
15668 BFD_ASSERT (indx != 0);
15669
65f38f15
AM
15670 /* We are turning this relocation into one
15671 against a section symbol, so subtract out
15672 the output section's address but not the
15673 offset of the input section in the output
15674 section. */
15675 outrel.r_addend -= osec->vma;
15676 }
15677
15678 outrel.r_info = ELF64_R_INFO (indx, r_type);
15679 }
15680 }
15681
15682 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
15683 if (h != NULL
15684 ? h->elf.type == STT_GNU_IFUNC
15685 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
15686 {
15687 sreloc = htab->elf.irelplt;
15688 if (indx == 0)
15689 htab->local_ifunc_resolver = 1;
15690 else if (is_static_defined (&h->elf))
15691 htab->maybe_local_ifunc_resolver = 1;
15692 }
65f38f15
AM
15693 if (sreloc == NULL)
15694 abort ();
15695
dfbb6ac9
AM
15696 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15697 >= sreloc->size)
15698 abort ();
947216bf
AM
15699 loc = sreloc->contents;
15700 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
15701 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15702
15703 /* If this reloc is against an external symbol, it will
15704 be computed at runtime, so there's no need to do
81407a69
AM
15705 anything now. However, for the sake of prelink ensure
15706 that the section contents are a known value. */
2cdcc330 15707 if (!relocate)
81407a69
AM
15708 {
15709 unresolved_reloc = FALSE;
15710 /* The value chosen here is quite arbitrary as ld.so
15711 ignores section contents except for the special
15712 case of .opd where the contents might be accessed
15713 before relocation. Choose zero, as that won't
15714 cause reloc overflow. */
15715 relocation = 0;
15716 addend = 0;
15717 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15718 to improve backward compatibility with older
15719 versions of ld. */
15720 if (r_type == R_PPC64_ADDR64)
15721 addend = outrel.r_addend;
15722 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 15723 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 15724 addend = outrel.r_offset;
81407a69 15725 }
65f38f15 15726 }
5bd4f169
AM
15727 break;
15728
65f38f15
AM
15729 case R_PPC64_COPY:
15730 case R_PPC64_GLOB_DAT:
15731 case R_PPC64_JMP_SLOT:
25f23106 15732 case R_PPC64_JMP_IREL:
65f38f15
AM
15733 case R_PPC64_RELATIVE:
15734 /* We shouldn't ever see these dynamic relocs in relocatable
15735 files. */
ae9a127f 15736 /* Fall through. */
65f38f15
AM
15737
15738 case R_PPC64_PLTGOT16:
15739 case R_PPC64_PLTGOT16_DS:
15740 case R_PPC64_PLTGOT16_HA:
15741 case R_PPC64_PLTGOT16_HI:
15742 case R_PPC64_PLTGOT16_LO:
15743 case R_PPC64_PLTGOT16_LO_DS:
15744 case R_PPC64_PLTREL32:
15745 case R_PPC64_PLTREL64:
15746 /* These ones haven't been implemented yet. */
15747
25f53a85 15748 info->callbacks->einfo
695344c0 15749 /* xgettext:c-format */
c1c8c1ef 15750 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 15751 input_bfd,
4ce794b7 15752 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
15753
15754 bfd_set_error (bfd_error_invalid_operation);
b34976b6 15755 ret = FALSE;
c316a17c 15756 goto copy_reloc;
65f38f15 15757 }
5bd4f169 15758
67f0cbdb
AM
15759 /* Multi-instruction sequences that access the TOC can be
15760 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 15761 to nop; addi rb,r2,x; */
67f0cbdb
AM
15762 switch (r_type)
15763 {
15764 default:
15765 break;
15766
15767 case R_PPC64_GOT_TLSLD16_HI:
15768 case R_PPC64_GOT_TLSGD16_HI:
15769 case R_PPC64_GOT_TPREL16_HI:
15770 case R_PPC64_GOT_DTPREL16_HI:
15771 case R_PPC64_GOT16_HI:
15772 case R_PPC64_TOC16_HI:
15773 /* These relocs would only be useful if building up an
15774 offset to later add to r2, perhaps in an indexed
15775 addressing mode instruction. Don't try to optimize.
15776 Unfortunately, the possibility of someone building up an
15777 offset like this or even with the HA relocs, means that
15778 we need to check the high insn when optimizing the low
15779 insn. */
15780 break;
15781
5663e321
AM
15782 case R_PPC64_PLTCALL_NOTOC:
15783 if (!unresolved_reloc)
15784 htab->notoc_plt = 1;
15785 /* Fall through. */
23cedd1d
AM
15786 case R_PPC64_PLTCALL:
15787 if (unresolved_reloc)
15788 {
15789 /* No plt entry. Make this into a direct call. */
15790 bfd_byte *p = contents + rel->r_offset;
15791 insn = bfd_get_32 (input_bfd, p);
15792 insn &= 1;
15793 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
15794 if (r_type == R_PPC64_PLTCALL)
15795 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
15796 unresolved_reloc = save_unresolved_reloc;
15797 r_type = R_PPC64_REL24;
15798 }
15799 break;
15800
5663e321 15801 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
15802 case R_PPC64_PLTSEQ:
15803 if (unresolved_reloc)
15804 {
15805 unresolved_reloc = FALSE;
15806 goto nop_it;
15807 }
15808 break;
15809
5663e321
AM
15810 case R_PPC64_PLT_PCREL34_NOTOC:
15811 if (!unresolved_reloc)
15812 htab->notoc_plt = 1;
15813 /* Fall through. */
15814 case R_PPC64_PLT_PCREL34:
15815 if (unresolved_reloc)
15816 {
15817 bfd_byte *p = contents + rel->r_offset;
15818 bfd_put_32 (input_bfd, PNOP >> 32, p);
15819 bfd_put_32 (input_bfd, PNOP, p + 4);
15820 unresolved_reloc = FALSE;
15821 goto copy_reloc;
15822 }
15823 break;
15824
23cedd1d
AM
15825 case R_PPC64_PLT16_HA:
15826 if (unresolved_reloc)
15827 {
15828 unresolved_reloc = FALSE;
15829 goto nop_it;
15830 }
15831 /* Fall through. */
67f0cbdb
AM
15832 case R_PPC64_GOT_TLSLD16_HA:
15833 case R_PPC64_GOT_TLSGD16_HA:
15834 case R_PPC64_GOT_TPREL16_HA:
15835 case R_PPC64_GOT_DTPREL16_HA:
15836 case R_PPC64_GOT16_HA:
15837 case R_PPC64_TOC16_HA:
98528052 15838 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15839 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 15840 {
23cedd1d
AM
15841 bfd_byte *p;
15842 nop_it:
15843 p = contents + (rel->r_offset & ~3);
98528052 15844 bfd_put_32 (input_bfd, NOP, p);
d830549d 15845 goto copy_reloc;
98528052 15846 }
67f0cbdb
AM
15847 break;
15848
23cedd1d
AM
15849 case R_PPC64_PLT16_LO:
15850 case R_PPC64_PLT16_LO_DS:
15851 if (unresolved_reloc)
15852 {
15853 unresolved_reloc = FALSE;
15854 goto nop_it;
15855 }
15856 /* Fall through. */
67f0cbdb
AM
15857 case R_PPC64_GOT_TLSLD16_LO:
15858 case R_PPC64_GOT_TLSGD16_LO:
15859 case R_PPC64_GOT_TPREL16_LO_DS:
15860 case R_PPC64_GOT_DTPREL16_LO_DS:
15861 case R_PPC64_GOT16_LO:
15862 case R_PPC64_GOT16_LO_DS:
15863 case R_PPC64_TOC16_LO:
15864 case R_PPC64_TOC16_LO_DS:
98528052 15865 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15866 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
15867 {
15868 bfd_byte *p = contents + (rel->r_offset & ~3);
15869 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
15870 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15871 {
15872 /* Transform addic to addi when we change reg. */
15873 insn &= ~((0x3f << 26) | (0x1f << 16));
15874 insn |= (14u << 26) | (2 << 16);
15875 }
15876 else
67f0cbdb 15877 {
98528052
AM
15878 insn &= ~(0x1f << 16);
15879 insn |= 2 << 16;
67f0cbdb 15880 }
560c8763 15881 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
15882 }
15883 break;
9a23f96e
AM
15884
15885 case R_PPC64_TPREL16_HA:
15886 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15887 {
15888 bfd_byte *p = contents + (rel->r_offset & ~3);
15889 insn = bfd_get_32 (input_bfd, p);
15890 if ((insn & ((0x3f << 26) | 0x1f << 16))
15891 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15892 /* xgettext:c-format */
15893 info->callbacks->minfo
15894 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
15895 input_bfd, input_section, rel->r_offset,
15896 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 15897 else
d830549d
AM
15898 {
15899 bfd_put_32 (input_bfd, NOP, p);
15900 goto copy_reloc;
15901 }
9a23f96e
AM
15902 }
15903 break;
15904
15905 case R_PPC64_TPREL16_LO:
15906 case R_PPC64_TPREL16_LO_DS:
15907 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15908 {
15909 bfd_byte *p = contents + (rel->r_offset & ~3);
15910 insn = bfd_get_32 (input_bfd, p);
15911 insn &= ~(0x1f << 16);
15912 insn |= 13 << 16;
15913 bfd_put_32 (input_bfd, insn, p);
15914 }
15915 break;
67f0cbdb
AM
15916 }
15917
65f38f15
AM
15918 /* Do any further special processing. */
15919 switch (r_type)
15920 {
15921 default:
15922 break;
15923
25f23106 15924 case R_PPC64_REL16_HA:
4a969973
AM
15925 case R_PPC64_REL16_HIGHA:
15926 case R_PPC64_REL16_HIGHERA:
15927 case R_PPC64_REL16_HIGHESTA:
a680de9a 15928 case R_PPC64_REL16DX_HA:
f9c6b907
AM
15929 case R_PPC64_ADDR16_HA:
15930 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15931 case R_PPC64_ADDR16_HIGHERA:
15932 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
15933 case R_PPC64_TOC16_HA:
15934 case R_PPC64_SECTOFF_HA:
411e1bfb 15935 case R_PPC64_TPREL16_HA:
f9c6b907 15936 case R_PPC64_TPREL16_HIGHA:
411e1bfb 15937 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 15938 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
15939 case R_PPC64_DTPREL16_HA:
15940 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 15941 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 15942 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
15943 /* It's just possible that this symbol is a weak symbol
15944 that's not actually defined anywhere. In that case,
15945 'sec' would be NULL, and we should leave the symbol
15946 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
15947 if (sec == NULL)
15948 break;
1a0670f3 15949 /* Fall through. */
5c5f6e17
AM
15950
15951 case R_PPC64_GOT16_HA:
15952 case R_PPC64_PLTGOT16_HA:
15953 case R_PPC64_PLT16_HA:
15954 case R_PPC64_GOT_TLSGD16_HA:
15955 case R_PPC64_GOT_TLSLD16_HA:
15956 case R_PPC64_GOT_TPREL16_HA:
15957 case R_PPC64_GOT_DTPREL16_HA:
15958 /* Add 0x10000 if sign bit in 0:15 is set.
15959 Bits 0:15 are not used. */
15960 addend += 0x8000;
65f38f15
AM
15961 break;
15962
5663e321
AM
15963 case R_PPC64_D34_HA30:
15964 case R_PPC64_ADDR16_HIGHERA34:
15965 case R_PPC64_ADDR16_HIGHESTA34:
15966 case R_PPC64_REL16_HIGHERA34:
15967 case R_PPC64_REL16_HIGHESTA34:
15968 if (sec != NULL)
15969 addend += 1ULL << 33;
15970 break;
15971
65f38f15
AM
15972 case R_PPC64_ADDR16_DS:
15973 case R_PPC64_ADDR16_LO_DS:
15974 case R_PPC64_GOT16_DS:
15975 case R_PPC64_GOT16_LO_DS:
15976 case R_PPC64_PLT16_LO_DS:
15977 case R_PPC64_SECTOFF_DS:
15978 case R_PPC64_SECTOFF_LO_DS:
15979 case R_PPC64_TOC16_DS:
15980 case R_PPC64_TOC16_LO_DS:
15981 case R_PPC64_PLTGOT16_DS:
15982 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
15983 case R_PPC64_GOT_TPREL16_DS:
15984 case R_PPC64_GOT_TPREL16_LO_DS:
15985 case R_PPC64_GOT_DTPREL16_DS:
15986 case R_PPC64_GOT_DTPREL16_LO_DS:
15987 case R_PPC64_TPREL16_DS:
15988 case R_PPC64_TPREL16_LO_DS:
15989 case R_PPC64_DTPREL16_DS:
15990 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
15991 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15992 mask = 3;
a680de9a
PB
15993 /* If this reloc is against an lq, lxv, or stxv insn, then
15994 the value must be a multiple of 16. This is somewhat of
15995 a hack, but the "correct" way to do this by defining _DQ
15996 forms of all the _DS relocs bloats all reloc switches in
15997 this file. It doesn't make much sense to use these
15998 relocs in data, so testing the insn should be safe. */
15999 if ((insn & (0x3f << 26)) == (56u << 26)
16000 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 16001 mask = 15;
a680de9a
PB
16002 relocation += addend;
16003 addend = insn & (mask ^ 3);
16004 if ((relocation & mask) != 0)
65f38f15 16005 {
a680de9a 16006 relocation ^= relocation & mask;
25f53a85 16007 info->callbacks->einfo
695344c0 16008 /* xgettext:c-format */
174d0a74 16009 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 16010 input_bfd, input_section, rel->r_offset,
d830549d 16011 ppc64_elf_howto_table[r_type]->name,
adadcc0c 16012 mask + 1);
65f38f15 16013 bfd_set_error (bfd_error_bad_value);
b34976b6 16014 ret = FALSE;
c316a17c 16015 goto copy_reloc;
65f38f15
AM
16016 }
16017 break;
5bd4f169
AM
16018 }
16019
239e1f3a
AM
16020 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16021 because such sections are not SEC_ALLOC and thus ld.so will
16022 not process them. */
d830549d 16023 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 16024 if (unresolved_reloc
239e1f3a 16025 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
16026 && h->elf.def_dynamic)
16027 && _bfd_elf_section_offset (output_bfd, info, input_section,
16028 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 16029 {
25f53a85 16030 info->callbacks->einfo
695344c0 16031 /* xgettext:c-format */
c1c8c1ef 16032 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 16033 input_bfd, input_section, rel->r_offset,
b80eed39 16034 howto->name,
039b3fef 16035 h->elf.root.root.string);
b34976b6 16036 ret = FALSE;
9c07fe7c 16037 }
5bd4f169 16038
b80eed39
AM
16039 /* 16-bit fields in insns mostly have signed values, but a
16040 few insns have 16-bit unsigned values. Really, we should
16041 have different reloc types. */
16042 if (howto->complain_on_overflow != complain_overflow_dont
16043 && howto->dst_mask == 0xffff
16044 && (input_section->flags & SEC_CODE) != 0)
16045 {
16046 enum complain_overflow complain = complain_overflow_signed;
16047
16048 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
16049 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16050 complain = complain_overflow_bitfield;
16051 else if (howto->rightshift == 0
16052 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16053 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16054 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16055 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16056 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16057 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
16058 complain = complain_overflow_unsigned;
16059 if (howto->complain_on_overflow != complain)
16060 {
16061 alt_howto = *howto;
16062 alt_howto.complain_on_overflow = complain;
16063 howto = &alt_howto;
16064 }
16065 }
16066
5663e321 16067 switch (r_type)
a680de9a 16068 {
5663e321
AM
16069 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
16070 case R_PPC64_D34:
16071 case R_PPC64_D34_LO:
16072 case R_PPC64_D34_HI30:
16073 case R_PPC64_D34_HA30:
16074 case R_PPC64_PCREL34:
16075 case R_PPC64_GOT_PCREL34:
16076 case R_PPC64_PLT_PCREL34:
16077 case R_PPC64_PLT_PCREL34_NOTOC:
16078 case R_PPC64_D28:
16079 case R_PPC64_PCREL28:
16080 if (rel->r_offset + 8 > input_section->size)
16081 r = bfd_reloc_outofrange;
16082 else
16083 {
5663e321
AM
16084 relocation += addend;
16085 if (howto->pc_relative)
16086 relocation -= (rel->r_offset
16087 + input_section->output_offset
16088 + input_section->output_section->vma);
16089 relocation >>= howto->rightshift;
16090
16091 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16092 pinsn <<= 32;
16093 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16094
16095 pinsn &= ~howto->dst_mask;
16096 pinsn |= (((relocation << 16) | (relocation & 0xffff))
16097 & howto->dst_mask);
16098 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
16099 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
16100 r = bfd_reloc_ok;
16101 if (howto->complain_on_overflow == complain_overflow_signed
16102 && (relocation + (1ULL << (howto->bitsize - 1))
16103 >= 1ULL << howto->bitsize))
16104 r = bfd_reloc_overflow;
16105 }
16106 break;
16107
16108 case R_PPC64_REL16DX_HA:
a680de9a
PB
16109 if (rel->r_offset + 4 > input_section->size)
16110 r = bfd_reloc_outofrange;
16111 else
16112 {
16113 relocation += addend;
16114 relocation -= (rel->r_offset
16115 + input_section->output_offset
16116 + input_section->output_section->vma);
3de43e7b 16117 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
16118 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16119 insn &= ~0x1fffc1;
3de43e7b 16120 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
16121 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16122 r = bfd_reloc_ok;
3de43e7b 16123 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
16124 r = bfd_reloc_overflow;
16125 }
5663e321
AM
16126 break;
16127
16128 default:
16129 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
16130 contents, rel->r_offset,
16131 relocation, addend);
a680de9a 16132 }
5bd4f169 16133
ef60b7ff 16134 if (r != bfd_reloc_ok)
5bd4f169 16135 {
bc30df16 16136 char *more_info = NULL;
b80eed39 16137 const char *reloc_name = howto->name;
bc30df16
AM
16138
16139 if (reloc_dest != DEST_NORMAL)
16140 {
16141 more_info = bfd_malloc (strlen (reloc_name) + 8);
16142 if (more_info != NULL)
16143 {
16144 strcpy (more_info, reloc_name);
16145 strcat (more_info, (reloc_dest == DEST_OPD
16146 ? " (OPD)" : " (stub)"));
16147 reloc_name = more_info;
16148 }
16149 }
16150
cd27b276 16151 if (r == bfd_reloc_overflow)
5bd4f169 16152 {
8131c122
AM
16153 /* On code like "if (foo) foo();" don't report overflow
16154 on a branch to zero when foo is undefined. */
16155 if (!warned
16156 && (reloc_dest == DEST_STUB
16157 || !(h != NULL
16158 && (h->elf.root.type == bfd_link_hash_undefweak
16159 || h->elf.root.type == bfd_link_hash_undefined)
16160 && is_branch_reloc (r_type))))
1a72702b
AM
16161 info->callbacks->reloc_overflow (info, &h->elf.root,
16162 sym_name, reloc_name,
16163 orig_rel.r_addend,
16164 input_bfd, input_section,
16165 rel->r_offset);
ef60b7ff
AM
16166 }
16167 else
16168 {
25f53a85 16169 info->callbacks->einfo
695344c0 16170 /* xgettext:c-format */
c1c8c1ef 16171 (_("%H: %s against `%pT': error %d\n"),
25f53a85 16172 input_bfd, input_section, rel->r_offset,
bc30df16 16173 reloc_name, sym_name, (int) r);
b34976b6 16174 ret = FALSE;
ef60b7ff 16175 }
bc30df16
AM
16176 if (more_info != NULL)
16177 free (more_info);
5bd4f169 16178 }
c316a17c
AM
16179 copy_reloc:
16180 if (wrel != rel)
16181 *wrel = *rel;
16182 }
16183
16184 if (wrel != rel)
16185 {
16186 Elf_Internal_Shdr *rel_hdr;
16187 size_t deleted = rel - wrel;
16188
16189 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16190 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16191 if (rel_hdr->sh_size == 0)
16192 {
16193 /* It is too late to remove an empty reloc section. Leave
16194 one NONE reloc.
16195 ??? What is wrong with an empty section??? */
16196 rel_hdr->sh_size = rel_hdr->sh_entsize;
16197 deleted -= 1;
16198 }
16199 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16200 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16201 input_section->reloc_count -= deleted;
5bd4f169
AM
16202 }
16203
645ea6a9
AM
16204 /* If we're emitting relocations, then shortly after this function
16205 returns, reloc offsets and addends for this section will be
16206 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
16207 file rather than the input. Save a copy of the relocs for
16208 opd_entry_value. */
0e1862bb 16209 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
16210 {
16211 bfd_size_type amt;
16212 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16213 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
16214 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16215 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
16216 if (rel == NULL)
16217 return FALSE;
16218 memcpy (rel, relocs, amt);
16219 }
5bd4f169
AM
16220 return ret;
16221}
16222
754021d0
AM
16223/* Adjust the value of any local symbols in opd sections. */
16224
6e0b88f1 16225static int
754021d0
AM
16226ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16227 const char *name ATTRIBUTE_UNUSED,
16228 Elf_Internal_Sym *elfsym,
16229 asection *input_sec,
16230 struct elf_link_hash_entry *h)
16231{
74f0fb50
AM
16232 struct _opd_sec_data *opd;
16233 long adjust;
754021d0
AM
16234 bfd_vma value;
16235
4025353c 16236 if (h != NULL)
6e0b88f1 16237 return 1;
4025353c 16238
74f0fb50
AM
16239 opd = get_opd_info (input_sec);
16240 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 16241 return 1;
754021d0
AM
16242
16243 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 16244 if (!bfd_link_relocatable (info))
754021d0
AM
16245 value -= input_sec->output_section->vma;
16246
51aecdc5 16247 adjust = opd->adjust[OPD_NDX (value)];
4025353c 16248 if (adjust == -1)
6e0b88f1
AM
16249 return 2;
16250
16251 elfsym->st_value += adjust;
16252 return 1;
754021d0
AM
16253}
16254
5bd4f169
AM
16255/* Finish up dynamic symbol handling. We set the contents of various
16256 dynamic sections here. */
16257
b34976b6 16258static bfd_boolean
4ce794b7
AM
16259ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16260 struct bfd_link_info *info,
16261 struct elf_link_hash_entry *h,
4aef7643 16262 Elf_Internal_Sym *sym)
5bd4f169 16263{
65f38f15 16264 struct ppc_link_hash_table *htab;
8387904d 16265 struct plt_entry *ent;
5bd4f169 16266
65f38f15 16267 htab = ppc_hash_table (info);
4dfe6ac6
NC
16268 if (htab == NULL)
16269 return FALSE;
5bd4f169 16270
49c09209
AM
16271 if (!htab->opd_abi && !h->def_regular)
16272 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16273 if (ent->plt.offset != (bfd_vma) -1)
16274 {
16275 /* Mark the symbol as undefined, rather than as
16276 defined in glink. Leave the value if there were
16277 any relocations where pointer equality matters
16278 (this is a clue for the dynamic linker, to make
16279 function pointer comparisons work between an
16280 application and shared library), otherwise set it
16281 to zero. */
16282 sym->st_shndx = SHN_UNDEF;
16283 if (!h->pointer_equality_needed)
16284 sym->st_value = 0;
16285 else if (!h->ref_regular_nonweak)
16286 {
16287 /* This breaks function pointer comparisons, but
16288 that is better than breaking tests for a NULL
16289 function pointer. */
16290 sym->st_value = 0;
16291 }
16292 break;
16293 }
5bd4f169 16294
f5385ebf 16295 if (h->needs_copy)
5bd4f169 16296 {
65f38f15 16297 /* This symbol needs a copy reloc. Set it up. */
49c09209 16298 Elf_Internal_Rela rela;
5474d94f 16299 asection *srel;
49c09209 16300 bfd_byte *loc;
5bd4f169 16301
65f38f15
AM
16302 if (h->dynindx == -1
16303 || (h->root.type != bfd_link_hash_defined
16304 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
16305 || htab->elf.srelbss == NULL
16306 || htab->elf.sreldynrelro == NULL)
65f38f15 16307 abort ();
5bd4f169
AM
16308
16309 rela.r_offset = (h->root.u.def.value
16310 + h->root.u.def.section->output_section->vma
16311 + h->root.u.def.section->output_offset);
16312 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16313 rela.r_addend = 0;
afbf7e8e 16314 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
16315 srel = htab->elf.sreldynrelro;
16316 else
16317 srel = htab->elf.srelbss;
16318 loc = srel->contents;
16319 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 16320 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
16321 }
16322
b34976b6 16323 return TRUE;
5bd4f169
AM
16324}
16325
65f38f15
AM
16326/* Used to decide how to sort relocs in an optimal manner for the
16327 dynamic linker, before writing them out. */
16328
16329static enum elf_reloc_type_class
7e612e98
AM
16330ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16331 const asection *rel_sec,
16332 const Elf_Internal_Rela *rela)
65f38f15 16333{
04c9666a 16334 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
16335 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16336
33e44f2e 16337 if (rel_sec == htab->elf.irelplt)
7e612e98 16338 return reloc_class_ifunc;
a33d1f77 16339
4ce794b7 16340 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 16341 switch (r_type)
65f38f15
AM
16342 {
16343 case R_PPC64_RELATIVE:
16344 return reloc_class_relative;
16345 case R_PPC64_JMP_SLOT:
16346 return reloc_class_plt;
16347 case R_PPC64_COPY:
16348 return reloc_class_copy;
16349 default:
16350 return reloc_class_normal;
16351 }
16352}
16353
5bd4f169
AM
16354/* Finish up the dynamic sections. */
16355
b34976b6 16356static bfd_boolean
4ce794b7
AM
16357ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16358 struct bfd_link_info *info)
5bd4f169 16359{
65f38f15
AM
16360 struct ppc_link_hash_table *htab;
16361 bfd *dynobj;
5bd4f169 16362 asection *sdyn;
5bd4f169 16363
65f38f15 16364 htab = ppc_hash_table (info);
4dfe6ac6
NC
16365 if (htab == NULL)
16366 return FALSE;
16367
65f38f15 16368 dynobj = htab->elf.dynobj;
3d4d4302 16369 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 16370
65f38f15 16371 if (htab->elf.dynamic_sections_created)
5bd4f169 16372 {
5bd4f169
AM
16373 Elf64_External_Dyn *dyncon, *dynconend;
16374
33e44f2e 16375 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 16376 abort ();
5bd4f169
AM
16377
16378 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 16379 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
16380 for (; dyncon < dynconend; dyncon++)
16381 {
16382 Elf_Internal_Dyn dyn;
19397422 16383 asection *s;
5bd4f169
AM
16384
16385 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16386
16387 switch (dyn.d_tag)
16388 {
65f38f15
AM
16389 default:
16390 continue;
5bd4f169 16391
5d1634d7 16392 case DT_PPC64_GLINK:
4ce794b7 16393 s = htab->glink;
6348e046 16394 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
16395 /* We stupidly defined DT_PPC64_GLINK to be the start
16396 of glink rather than the first entry point, which is
16397 what ld.so needs, and now have a bigger stub to
16398 support automatic multiple TOCs. */
9e390558 16399 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
16400 break;
16401
19397422
AM
16402 case DT_PPC64_OPD:
16403 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16404 if (s == NULL)
16405 continue;
16406 dyn.d_un.d_ptr = s->vma;
19397422
AM
16407 break;
16408
e8910a83 16409 case DT_PPC64_OPT:
5663e321
AM
16410 if ((htab->do_multi_toc && htab->multi_toc_needed)
16411 || htab->notoc_plt)
e8910a83 16412 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
16413 if (htab->has_plt_localentry0)
16414 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
16415 break;
16416
19397422
AM
16417 case DT_PPC64_OPDSZ:
16418 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16419 if (s == NULL)
16420 continue;
eea6121a 16421 dyn.d_un.d_val = s->size;
19397422
AM
16422 break;
16423
65f38f15 16424 case DT_PLTGOT:
33e44f2e 16425 s = htab->elf.splt;
6348e046 16426 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
16427 break;
16428
16429 case DT_JMPREL:
33e44f2e 16430 s = htab->elf.srelplt;
6348e046 16431 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 16432 break;
5bd4f169 16433
65f38f15 16434 case DT_PLTRELSZ:
33e44f2e 16435 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 16436 break;
82e66161
AM
16437
16438 case DT_TEXTREL:
16439 if (htab->local_ifunc_resolver)
16440 info->callbacks->einfo
16441 (_("%X%P: text relocations and GNU indirect "
16442 "functions will result in a segfault at runtime\n"));
16443 else if (htab->maybe_local_ifunc_resolver)
16444 info->callbacks->einfo
16445 (_("%P: warning: text relocations and GNU indirect "
16446 "functions may result in a segfault at runtime\n"));
16447 continue;
5bd4f169 16448 }
5bd4f169 16449
65f38f15 16450 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 16451 }
5bd4f169
AM
16452 }
16453
6528b6eb
AM
16454 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16455 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16456 {
16457 /* Fill in the first entry in the global offset table.
16458 We use it to hold the link-time TOCbase. */
16459 bfd_put_64 (output_bfd,
60ee0d4a 16460 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 16461 htab->elf.sgot->contents);
5d1634d7
AM
16462
16463 /* Set .got entry size. */
2cdcc330
AM
16464 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16465 = 8;
5d1634d7
AM
16466 }
16467
6528b6eb
AM
16468 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16469 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16470 {
16471 /* Set .plt entry size. */
33e44f2e 16472 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 16473 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
16474 }
16475
84f5d08e
AM
16476 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16477 brlt ourselves if emitrelocations. */
16478 if (htab->brlt != NULL
16479 && htab->brlt->reloc_count != 0
16480 && !_bfd_elf_link_output_relocs (output_bfd,
16481 htab->brlt,
d4730f92 16482 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
16483 elf_section_data (htab->brlt)->relocs,
16484 NULL))
16485 return FALSE;
16486
176a0d42
AM
16487 if (htab->glink != NULL
16488 && htab->glink->reloc_count != 0
16489 && !_bfd_elf_link_output_relocs (output_bfd,
16490 htab->glink,
d4730f92 16491 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
16492 elf_section_data (htab->glink)->relocs,
16493 NULL))
16494 return FALSE;
16495
da44f4e5 16496
df136d64
AM
16497 if (htab->glink_eh_frame != NULL
16498 && htab->glink_eh_frame->size != 0
16499 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16500 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16501 htab->glink_eh_frame,
16502 htab->glink_eh_frame->contents))
16503 return FALSE;
58d180e8 16504
e717da7e 16505 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
16506 since we didn't add them to DYNOBJ. We know dynobj is the first
16507 bfd. */
c72f2fb2 16508 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
16509 {
16510 asection *s;
7b53ace3 16511
0c8d6e5c 16512 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
16513 continue;
16514
e717da7e
AM
16515 s = ppc64_elf_tdata (dynobj)->got;
16516 if (s != NULL
eea6121a 16517 && s->size != 0
e717da7e
AM
16518 && s->output_section != bfd_abs_section_ptr
16519 && !bfd_set_section_contents (output_bfd, s->output_section,
16520 s->contents, s->output_offset,
eea6121a 16521 s->size))
e717da7e
AM
16522 return FALSE;
16523 s = ppc64_elf_tdata (dynobj)->relgot;
16524 if (s != NULL
eea6121a 16525 && s->size != 0
e717da7e
AM
16526 && s->output_section != bfd_abs_section_ptr
16527 && !bfd_set_section_contents (output_bfd, s->output_section,
16528 s->contents, s->output_offset,
eea6121a 16529 s->size))
e717da7e
AM
16530 return FALSE;
16531 }
f6c52c13 16532
b34976b6 16533 return TRUE;
5bd4f169
AM
16534}
16535
5bd4f169 16536#include "elf64-target.h"
7b8e7dad
AM
16537
16538/* FreeBSD support */
16539
16540#undef TARGET_LITTLE_SYM
16541#undef TARGET_LITTLE_NAME
16542
16543#undef TARGET_BIG_SYM
6d00b590 16544#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
16545#undef TARGET_BIG_NAME
16546#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16547
16548#undef ELF_OSABI
16549#define ELF_OSABI ELFOSABI_FREEBSD
16550
16551#undef elf64_bed
16552#define elf64_bed elf64_powerpc_fbsd_bed
16553
16554#include "elf64-target.h"