Make extern declaration of dwarf_always_disassemble correct
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
82704155 2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
5bd4f169
AM
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 5 Largely rewritten by Alan Modra.
5bd4f169 6
ae9a127f 7 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 8
ae9a127f
NC
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
cd123cb7 11 the Free Software Foundation; either version 3 of the License, or
ae9a127f 12 (at your option) any later version.
5bd4f169 13
ae9a127f
NC
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
5bd4f169 18
4ce794b7
AM
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 22
cd123cb7 23
4ce794b7
AM
24/* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 27
3db64b00 28#include "sysdep.h"
183e98be 29#include <stdarg.h>
5bd4f169 30#include "bfd.h"
5bd4f169
AM
31#include "bfdlink.h"
32#include "libbfd.h"
33#include "elf-bfd.h"
04c9666a 34#include "elf/ppc64.h"
5d1634d7 35#include "elf64-ppc.h"
58d180e8 36#include "dwarf2.h"
5bd4f169 37
805fc799 38static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
40static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 42static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 44static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 46static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 48static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
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
04bdff6a 190#define LI_R11_0 0x39600000 /* li %r11,0 */
07d6d2b8 191#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
04bdff6a 192#define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
05d0e962 193#define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
006589cf
AM
194#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
195#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
05d0e962 196#define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
a345bc8d 197#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
05d0e962 198#define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
04bdff6a 199#define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
05d0e962 200#define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
07d6d2b8 201#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
04bdff6a 202#define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
05d0e962
AM
203#define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
204#define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
205#define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
04bdff6a
AM
206#define PADDI_R12_PC 0x0610000039800000ULL
207#define PLD_R12_PC 0x04100000e5800000ULL
5663e321 208#define PNOP 0x0700000000000000ULL
a345bc8d 209
9e390558
AM
210/* __glink_PLTresolve stub instructions. We enter with the index in R0. */
211#define GLINK_PLTRESOLVE_SIZE(htab) \
407aa07c 212 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
ee4bf8d2
AM
213 /* 0: */
214 /* .quad plt0-1f */
215 /* __glink: */
216#define MFLR_R12 0x7d8802a6 /* mflr %12 */
217#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
218 /* 1: */
219#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 220 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 221#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
222#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
223 /* ld %12,0(%11) */
224 /* ld %2,8(%11) */
225 /* mtctr %12 */
226 /* ld %11,16(%11) */
ee4bf8d2 227 /* bctr */
b9e5796b
AM
228#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
229#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
230#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
231#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
232#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
233
234/* Pad with this. */
235#define NOP 0x60000000
236
721956f4
AM
237/* Some other nops. */
238#define CROR_151515 0x4def7b82
239#define CROR_313131 0x4ffffb82
240
cedb70c5 241/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
242#define LI_R0_0 0x38000000 /* li %r0,0 */
243#define B_DOT 0x48000000 /* b . */
244
245/* After that, we need two instructions to load the index, followed by
246 a branch. */
247#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 248#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 249
deb0e272
AM
250/* Instructions used by the save and restore reg functions. */
251#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
252#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
253#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
254#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
255#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
256#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
257#define LI_R12_0 0x39800000 /* li %r12,0 */
258#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
259#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
260#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
261#define BLR 0x4e800020 /* blr */
262
41bd81ab
AM
263/* Since .opd is an array of descriptors and each entry will end up
264 with identical R_PPC64_RELATIVE relocs, there is really no need to
265 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 266 relocate .opd without reloc entries. */
41bd81ab
AM
267#ifndef NO_OPD_RELOCS
268#define NO_OPD_RELOCS 0
269#endif
810d4e75 270
a4b6fadd
AM
271#ifndef ARRAY_SIZE
272#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
273#endif
274
810d4e75
AM
275static inline int
276abiversion (bfd *abfd)
277{
278 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
279}
280
281static inline void
282set_abiversion (bfd *abfd, int ver)
283{
284 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
285 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
286}
5bd4f169
AM
287\f
288/* Relocation HOWTO's. */
46807bf4
AM
289/* Like other ELF RELA targets that don't apply multiple
290 field-altering relocations to the same localation, src_mask is
291 always zero and pcrel_offset is the same as pc_relative.
292 PowerPC can always use a zero bitpos, even when the field is not at
293 the LSB. For example, a REL24 could use rightshift=2, bisize=24
294 and bitpos=2 which matches the ABI description, or as we do here,
295 rightshift=0, bitsize=26 and bitpos=0. */
296#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
297 complain, special_func) \
298 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
299 complain_overflow_ ## complain, special_func, \
300 #type, FALSE, 0, mask, pc_relative)
301
04c9666a 302static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 303
f3185997
NC
304static reloc_howto_type ppc64_elf_howto_raw[] =
305{
5bd4f169 306 /* This reloc does nothing. */
46807bf4
AM
307 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
308 bfd_elf_generic_reloc),
5bd4f169
AM
309
310 /* A standard 32 bit relocation. */
46807bf4
AM
311 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
312 bfd_elf_generic_reloc),
5bd4f169
AM
313
314 /* An absolute 26 bit branch; the lower two bits must be zero.
315 FIXME: we don't check that, we just clear them. */
46807bf4
AM
316 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
317 bfd_elf_generic_reloc),
5bd4f169
AM
318
319 /* A standard 16 bit relocation. */
46807bf4
AM
320 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
321 bfd_elf_generic_reloc),
5bd4f169
AM
322
323 /* A 16 bit relocation without overflow. */
46807bf4
AM
324 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
325 bfd_elf_generic_reloc),
5bd4f169
AM
326
327 /* Bits 16-31 of an address. */
46807bf4
AM
328 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
329 bfd_elf_generic_reloc),
5bd4f169
AM
330
331 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
332 bits, treated as a signed number, is negative. */
46807bf4
AM
333 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
334 ppc64_elf_ha_reloc),
5bd4f169
AM
335
336 /* An absolute 16 bit branch; the lower two bits must be zero.
337 FIXME: we don't check that, we just clear them. */
46807bf4
AM
338 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
339 ppc64_elf_branch_reloc),
5bd4f169
AM
340
341 /* An absolute 16 bit branch, for which bit 10 should be set to
342 indicate that the branch is expected to be taken. The lower two
343 bits must be zero. */
46807bf4
AM
344 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
345 ppc64_elf_brtaken_reloc),
5bd4f169
AM
346
347 /* An absolute 16 bit branch, for which bit 10 should be set to
348 indicate that the branch is not expected to be taken. The lower
349 two bits must be zero. */
46807bf4
AM
350 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
351 ppc64_elf_brtaken_reloc),
5bd4f169
AM
352
353 /* A relative 26 bit branch; the lower two bits must be zero. */
46807bf4
AM
354 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
355 ppc64_elf_branch_reloc),
5bd4f169 356
05d0e962 357 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
46807bf4
AM
358 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
359 ppc64_elf_branch_reloc),
05d0e962 360
5bd4f169 361 /* A relative 16 bit branch; the lower two bits must be zero. */
46807bf4
AM
362 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
363 ppc64_elf_branch_reloc),
5bd4f169
AM
364
365 /* A relative 16 bit branch. Bit 10 should be set to indicate that
366 the branch is expected to be taken. The lower two bits must be
367 zero. */
46807bf4
AM
368 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
369 ppc64_elf_brtaken_reloc),
5bd4f169
AM
370
371 /* A relative 16 bit branch. Bit 10 should be set to indicate that
372 the branch is not expected to be taken. The lower two bits must
373 be zero. */
46807bf4
AM
374 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
375 ppc64_elf_brtaken_reloc),
5bd4f169
AM
376
377 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
378 symbol. */
46807bf4
AM
379 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
380 ppc64_elf_unhandled_reloc),
5bd4f169
AM
381
382 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
383 the symbol. */
46807bf4
AM
384 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
385 ppc64_elf_unhandled_reloc),
5bd4f169
AM
386
387 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
388 the symbol. */
46807bf4
AM
389 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
390 ppc64_elf_unhandled_reloc),
5bd4f169
AM
391
392 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
393 the symbol. */
46807bf4
AM
394 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
395 ppc64_elf_unhandled_reloc),
5bd4f169
AM
396
397 /* This is used only by the dynamic linker. The symbol should exist
398 both in the object being run and in some shared library. The
399 dynamic linker copies the data addressed by the symbol from the
400 shared library into the object, because the object being
401 run has to have the data at some particular address. */
46807bf4
AM
402 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
403 ppc64_elf_unhandled_reloc),
5bd4f169
AM
404
405 /* Like R_PPC64_ADDR64, but used when setting global offset table
406 entries. */
46807bf4
AM
407 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
408 ppc64_elf_unhandled_reloc),
5bd4f169
AM
409
410 /* Created by the link editor. Marks a procedure linkage table
411 entry for a symbol. */
46807bf4
AM
412 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
413 ppc64_elf_unhandled_reloc),
5bd4f169
AM
414
415 /* Used only by the dynamic linker. When the object is run, this
416 doubleword64 is set to the load address of the object, plus the
417 addend. */
46807bf4
AM
418 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
419 bfd_elf_generic_reloc),
5bd4f169
AM
420
421 /* Like R_PPC64_ADDR32, but may be unaligned. */
46807bf4
AM
422 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
423 bfd_elf_generic_reloc),
5bd4f169
AM
424
425 /* Like R_PPC64_ADDR16, but may be unaligned. */
46807bf4
AM
426 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
427 bfd_elf_generic_reloc),
5bd4f169
AM
428
429 /* 32-bit PC relative. */
46807bf4
AM
430 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
431 bfd_elf_generic_reloc),
5bd4f169 432
10ed1bba 433 /* 32-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
434 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
435 ppc64_elf_unhandled_reloc),
5bd4f169
AM
436
437 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
438 FIXME: R_PPC64_PLTREL32 not supported. */
46807bf4
AM
439 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
440 ppc64_elf_unhandled_reloc),
5bd4f169
AM
441
442 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
443 the symbol. */
46807bf4
AM
444 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
445 ppc64_elf_unhandled_reloc),
5bd4f169
AM
446
447 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
448 the symbol. */
46807bf4
AM
449 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
450 ppc64_elf_unhandled_reloc),
5bd4f169
AM
451
452 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
453 the symbol. */
46807bf4
AM
454 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
455 ppc64_elf_unhandled_reloc),
5bd4f169 456
c061c2d8 457 /* 16-bit section relative relocation. */
46807bf4
AM
458 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
459 ppc64_elf_sectoff_reloc),
5bd4f169 460
c061c2d8 461 /* Like R_PPC64_SECTOFF, but no overflow warning. */
46807bf4
AM
462 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
463 ppc64_elf_sectoff_reloc),
5bd4f169
AM
464
465 /* 16-bit upper half section relative relocation. */
46807bf4
AM
466 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
467 ppc64_elf_sectoff_reloc),
5bd4f169
AM
468
469 /* 16-bit upper half adjusted section relative relocation. */
46807bf4
AM
470 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
471 ppc64_elf_sectoff_ha_reloc),
5bd4f169 472
04c9666a 473 /* Like R_PPC64_REL24 without touching the two least significant bits. */
46807bf4
AM
474 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
475 bfd_elf_generic_reloc),
5bd4f169
AM
476
477 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
478
479 /* A standard 64-bit relocation. */
46807bf4
AM
480 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
481 bfd_elf_generic_reloc),
5bd4f169
AM
482
483 /* The bits 32-47 of an address. */
46807bf4
AM
484 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
485 bfd_elf_generic_reloc),
5bd4f169
AM
486
487 /* The bits 32-47 of an address, plus 1 if the contents of the low
488 16 bits, treated as a signed number, is negative. */
46807bf4
AM
489 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
490 ppc64_elf_ha_reloc),
5bd4f169
AM
491
492 /* The bits 48-63 of an address. */
46807bf4
AM
493 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
494 bfd_elf_generic_reloc),
5bd4f169
AM
495
496 /* The bits 48-63 of an address, plus 1 if the contents of the low
497 16 bits, treated as a signed number, is negative. */
46807bf4
AM
498 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
499 ppc64_elf_ha_reloc),
5bd4f169
AM
500
501 /* Like ADDR64, but may be unaligned. */
46807bf4
AM
502 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
503 bfd_elf_generic_reloc),
5bd4f169
AM
504
505 /* 64-bit relative relocation. */
46807bf4
AM
506 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
507 bfd_elf_generic_reloc),
5bd4f169 508
cedb70c5 509 /* 64-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
510 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
511 ppc64_elf_unhandled_reloc),
5bd4f169
AM
512
513 /* 64-bit PC relative relocation to the symbol's procedure linkage
514 table. */
515 /* FIXME: R_PPC64_PLTREL64 not supported. */
46807bf4
AM
516 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
517 ppc64_elf_unhandled_reloc),
5bd4f169
AM
518
519 /* 16 bit TOC-relative relocation. */
5bd4f169 520 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
46807bf4
AM
521 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
522 ppc64_elf_toc_reloc),
5bd4f169
AM
523
524 /* 16 bit TOC-relative relocation without overflow. */
5bd4f169 525 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
46807bf4
AM
526 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
527 ppc64_elf_toc_reloc),
5bd4f169
AM
528
529 /* 16 bit TOC-relative relocation, high 16 bits. */
5bd4f169 530 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
46807bf4
AM
531 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
532 ppc64_elf_toc_reloc),
5bd4f169
AM
533
534 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
535 contents of the low 16 bits, treated as a signed number, is
536 negative. */
5bd4f169 537 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
46807bf4
AM
538 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
539 ppc64_elf_toc_ha_reloc),
5bd4f169
AM
540
541 /* 64-bit relocation; insert value of TOC base (.TOC.). */
5bd4f169 542 /* R_PPC64_TOC 51 doubleword64 .TOC. */
46807bf4
AM
543 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
544 ppc64_elf_toc64_reloc),
5bd4f169
AM
545
546 /* Like R_PPC64_GOT16, but also informs the link editor that the
547 value to relocate may (!) refer to a PLT entry which the link
548 editor (a) may replace with the symbol value. If the link editor
549 is unable to fully resolve the symbol, it may (b) create a PLT
550 entry and store the address to the new PLT entry in the GOT.
551 This permits lazy resolution of function symbols at run time.
552 The link editor may also skip all of this and just (c) emit a
553 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
554 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
46807bf4
AM
555 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
556 ppc64_elf_unhandled_reloc),
411e1bfb
AM
557
558 /* Like R_PPC64_PLTGOT16, but without overflow. */
559 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
560 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
561 ppc64_elf_unhandled_reloc),
411e1bfb
AM
562
563 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
564 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
46807bf4
AM
565 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
566 ppc64_elf_unhandled_reloc),
411e1bfb
AM
567
568 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
569 1 if the contents of the low 16 bits, treated as a signed number,
570 is negative. */
571 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
46807bf4
AM
572 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
573 ppc64_elf_unhandled_reloc),
411e1bfb
AM
574
575 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
46807bf4
AM
576 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
577 bfd_elf_generic_reloc),
411e1bfb
AM
578
579 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
46807bf4
AM
580 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
581 bfd_elf_generic_reloc),
411e1bfb
AM
582
583 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
46807bf4
AM
584 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
585 ppc64_elf_unhandled_reloc),
411e1bfb
AM
586
587 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
46807bf4
AM
588 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
589 ppc64_elf_unhandled_reloc),
411e1bfb
AM
590
591 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
46807bf4
AM
592 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
593 ppc64_elf_unhandled_reloc),
411e1bfb
AM
594
595 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
46807bf4
AM
596 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
597 ppc64_elf_sectoff_reloc),
411e1bfb
AM
598
599 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
46807bf4
AM
600 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
601 ppc64_elf_sectoff_reloc),
411e1bfb
AM
602
603 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
46807bf4
AM
604 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
605 ppc64_elf_toc_reloc),
411e1bfb
AM
606
607 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
46807bf4
AM
608 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
609 ppc64_elf_toc_reloc),
411e1bfb
AM
610
611 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
612 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
46807bf4
AM
613 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
614 ppc64_elf_unhandled_reloc),
411e1bfb
AM
615
616 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
617 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
618 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
619 ppc64_elf_unhandled_reloc),
411e1bfb 620
727fc41e 621 /* Marker relocs for TLS. */
46807bf4
AM
622 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
623 bfd_elf_generic_reloc),
624
625 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
626 bfd_elf_generic_reloc),
627
628 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
629 bfd_elf_generic_reloc),
727fc41e 630
23cedd1d
AM
631 /* Marker reloc for optimizing r2 save in prologue rather than on
632 each plt call stub. */
46807bf4
AM
633 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
634 bfd_elf_generic_reloc),
3b421ab3 635
23cedd1d 636 /* Marker relocs on inline plt call instructions. */
46807bf4
AM
637 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
638 bfd_elf_generic_reloc),
639
640 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
641 bfd_elf_generic_reloc),
23cedd1d 642
411e1bfb
AM
643 /* Computes the load module index of the load module that contains the
644 definition of its TLS sym. */
46807bf4
AM
645 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
646 ppc64_elf_unhandled_reloc),
411e1bfb
AM
647
648 /* Computes a dtv-relative displacement, the difference between the value
649 of sym+add and the base address of the thread-local storage block that
650 contains the definition of sym, minus 0x8000. */
46807bf4
AM
651 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
652 ppc64_elf_unhandled_reloc),
411e1bfb
AM
653
654 /* A 16 bit dtprel reloc. */
46807bf4
AM
655 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
656 ppc64_elf_unhandled_reloc),
411e1bfb
AM
657
658 /* Like DTPREL16, but no overflow. */
46807bf4
AM
659 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
660 ppc64_elf_unhandled_reloc),
411e1bfb
AM
661
662 /* Like DTPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
663 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
664 ppc64_elf_unhandled_reloc),
411e1bfb
AM
665
666 /* Like DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
667 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
668 ppc64_elf_unhandled_reloc),
411e1bfb
AM
669
670 /* Like DTPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
671 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
672 ppc64_elf_unhandled_reloc),
411e1bfb
AM
673
674 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
675 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
676 ppc64_elf_unhandled_reloc),
411e1bfb
AM
677
678 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
679 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
680 ppc64_elf_unhandled_reloc),
411e1bfb
AM
681
682 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
683 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
684 ppc64_elf_unhandled_reloc),
411e1bfb
AM
685
686 /* Like DTPREL16, but for insns with a DS field. */
46807bf4
AM
687 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
688 ppc64_elf_unhandled_reloc),
411e1bfb
AM
689
690 /* Like DTPREL16_DS, but no overflow. */
46807bf4
AM
691 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
692 ppc64_elf_unhandled_reloc),
411e1bfb
AM
693
694 /* Computes a tp-relative displacement, the difference between the value of
695 sym+add and the value of the thread pointer (r13). */
46807bf4
AM
696 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
697 ppc64_elf_unhandled_reloc),
411e1bfb
AM
698
699 /* A 16 bit tprel reloc. */
46807bf4
AM
700 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
701 ppc64_elf_unhandled_reloc),
411e1bfb
AM
702
703 /* Like TPREL16, but no overflow. */
46807bf4
AM
704 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
705 ppc64_elf_unhandled_reloc),
411e1bfb
AM
706
707 /* Like TPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
708 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
709 ppc64_elf_unhandled_reloc),
411e1bfb
AM
710
711 /* Like TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
712 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
713 ppc64_elf_unhandled_reloc),
411e1bfb
AM
714
715 /* Like TPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
716 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
717 ppc64_elf_unhandled_reloc),
411e1bfb
AM
718
719 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
720 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
721 ppc64_elf_unhandled_reloc),
411e1bfb
AM
722
723 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
724 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
725 ppc64_elf_unhandled_reloc),
411e1bfb
AM
726
727 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
728 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
729 ppc64_elf_unhandled_reloc),
411e1bfb
AM
730
731 /* Like TPREL16, but for insns with a DS field. */
46807bf4
AM
732 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
733 ppc64_elf_unhandled_reloc),
411e1bfb
AM
734
735 /* Like TPREL16_DS, but no overflow. */
46807bf4
AM
736 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
737 ppc64_elf_unhandled_reloc),
411e1bfb
AM
738
739 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
740 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
741 to the first entry relative to the TOC base (r2). */
46807bf4
AM
742 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
743 ppc64_elf_unhandled_reloc),
5bd4f169 744
411e1bfb 745 /* Like GOT_TLSGD16, but no overflow. */
46807bf4
AM
746 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
747 ppc64_elf_unhandled_reloc),
5bd4f169 748
411e1bfb 749 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
46807bf4
AM
750 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
751 ppc64_elf_unhandled_reloc),
5bd4f169 752
411e1bfb 753 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
46807bf4
AM
754 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
755 ppc64_elf_unhandled_reloc),
5bd4f169 756
411e1bfb
AM
757 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
758 with values (sym+add)@dtpmod and zero, and computes the offset to the
759 first entry relative to the TOC base (r2). */
46807bf4
AM
760 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
761 ppc64_elf_unhandled_reloc),
5bd4f169 762
411e1bfb 763 /* Like GOT_TLSLD16, but no overflow. */
46807bf4
AM
764 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
765 ppc64_elf_unhandled_reloc),
5bd4f169 766
411e1bfb 767 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
46807bf4
AM
768 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
769 ppc64_elf_unhandled_reloc),
5bd4f169 770
411e1bfb 771 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
46807bf4
AM
772 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
773 ppc64_elf_unhandled_reloc),
5bd4f169 774
411e1bfb
AM
775 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
776 the offset to the entry relative to the TOC base (r2). */
46807bf4
AM
777 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
778 ppc64_elf_unhandled_reloc),
5bd4f169 779
411e1bfb 780 /* Like GOT_DTPREL16_DS, but no overflow. */
46807bf4
AM
781 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
782 ppc64_elf_unhandled_reloc),
5bd4f169 783
411e1bfb 784 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
785 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
786 ppc64_elf_unhandled_reloc),
5bd4f169 787
411e1bfb 788 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
789 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
790 ppc64_elf_unhandled_reloc),
411e1bfb
AM
791
792 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
793 offset to the entry relative to the TOC base (r2). */
46807bf4
AM
794 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
795 ppc64_elf_unhandled_reloc),
5bd4f169 796
411e1bfb 797 /* Like GOT_TPREL16_DS, but no overflow. */
46807bf4
AM
798 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
799 ppc64_elf_unhandled_reloc),
5bd4f169 800
411e1bfb 801 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
802 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
803 ppc64_elf_unhandled_reloc),
5bd4f169 804
411e1bfb 805 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
806 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
807 ppc64_elf_unhandled_reloc),
808
809 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
810 ppc64_elf_unhandled_reloc),
811
812 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
813 bfd_elf_generic_reloc),
e054468f 814
25f23106 815 /* A 16 bit relative relocation. */
46807bf4
AM
816 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
817 bfd_elf_generic_reloc),
25f23106
AM
818
819 /* A 16 bit relative relocation without overflow. */
46807bf4
AM
820 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
821 bfd_elf_generic_reloc),
25f23106
AM
822
823 /* The high order 16 bits of a relative address. */
46807bf4
AM
824 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
825 bfd_elf_generic_reloc),
25f23106
AM
826
827 /* The high order 16 bits of a relative address, plus 1 if the contents of
828 the low 16 bits, treated as a signed number, is negative. */
46807bf4
AM
829 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
830 ppc64_elf_ha_reloc),
25f23106 831
4a969973
AM
832 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
833 bfd_elf_generic_reloc),
834
835 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
836 ppc64_elf_ha_reloc),
837
838 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
839 bfd_elf_generic_reloc),
840
841 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
842 ppc64_elf_ha_reloc),
843
844 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
845 bfd_elf_generic_reloc),
846
847 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
848 ppc64_elf_ha_reloc),
849
a680de9a 850 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
46807bf4
AM
851 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
852 ppc64_elf_ha_reloc),
a680de9a 853
7ba71655 854 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
46807bf4
AM
855 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
856 ppc64_elf_ha_reloc),
7ba71655 857
f9c6b907 858 /* Like R_PPC64_ADDR16_HI, but no overflow. */
46807bf4
AM
859 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
860 bfd_elf_generic_reloc),
f9c6b907
AM
861
862 /* Like R_PPC64_ADDR16_HA, but no overflow. */
46807bf4
AM
863 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
864 ppc64_elf_ha_reloc),
f9c6b907
AM
865
866 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
46807bf4
AM
867 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
868 ppc64_elf_unhandled_reloc),
f9c6b907
AM
869
870 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
46807bf4
AM
871 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
872 ppc64_elf_unhandled_reloc),
f9c6b907
AM
873
874 /* Like R_PPC64_TPREL16_HI, but no overflow. */
46807bf4
AM
875 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
876 ppc64_elf_unhandled_reloc),
f9c6b907
AM
877
878 /* Like R_PPC64_TPREL16_HA, but no overflow. */
46807bf4
AM
879 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
880 ppc64_elf_unhandled_reloc),
f9c6b907 881
006589cf 882 /* Marker reloc on ELFv2 large-model function entry. */
46807bf4
AM
883 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
884 bfd_elf_generic_reloc),
006589cf 885
45965137 886 /* Like ADDR64, but use local entry point of function. */
46807bf4
AM
887 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
888 bfd_elf_generic_reloc),
45965137 889
5663e321
AM
890 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, FALSE, dont,
891 bfd_elf_generic_reloc),
892
893 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, FALSE, dont,
894 bfd_elf_generic_reloc),
895
896 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, FALSE, dont,
897 bfd_elf_generic_reloc),
898
899 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
900 ppc64_elf_prefix_reloc),
901
902 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, dont,
903 ppc64_elf_prefix_reloc),
904
905 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
906 ppc64_elf_prefix_reloc),
907
908 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
909 ppc64_elf_prefix_reloc),
910
911 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
912 ppc64_elf_prefix_reloc),
913
914 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
915 ppc64_elf_unhandled_reloc),
916
917 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
918 ppc64_elf_unhandled_reloc),
919
920 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
921 ppc64_elf_unhandled_reloc),
922
c213164a
AM
923 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
924 ppc64_elf_unhandled_reloc),
925
926 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
927 ppc64_elf_unhandled_reloc),
928
929 HOW (R_PPC64_GOT_TLSGD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
930 ppc64_elf_unhandled_reloc),
931
932 HOW (R_PPC64_GOT_TLSLD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
933 ppc64_elf_unhandled_reloc),
934
935 HOW (R_PPC64_GOT_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
936 ppc64_elf_unhandled_reloc),
937
938 HOW (R_PPC64_GOT_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
939 ppc64_elf_unhandled_reloc),
940
5663e321
AM
941 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
942 bfd_elf_generic_reloc),
943
944 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
945 ppc64_elf_ha_reloc),
946
947 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
948 bfd_elf_generic_reloc),
949
950 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
951 ppc64_elf_ha_reloc),
952
953 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
954 bfd_elf_generic_reloc),
955
956 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
957 ppc64_elf_ha_reloc),
958
959 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
960 bfd_elf_generic_reloc),
961
962 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
963 ppc64_elf_ha_reloc),
964
965 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
966 ppc64_elf_prefix_reloc),
967
968 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
969 ppc64_elf_prefix_reloc),
970
5bd4f169 971 /* GNU extension to record C++ vtable hierarchy. */
46807bf4
AM
972 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
973 NULL),
5bd4f169
AM
974
975 /* GNU extension to record C++ vtable member usage. */
46807bf4
AM
976 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
977 NULL),
5bd4f169
AM
978};
979
980\f
981/* Initialize the ppc64_elf_howto_table, so that linear accesses can
982 be done. */
983
984static void
4ce794b7 985ppc_howto_init (void)
5bd4f169
AM
986{
987 unsigned int i, type;
988
a4b6fadd 989 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
990 {
991 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 992 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
993 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
994 }
995}
996
997static reloc_howto_type *
f3185997 998ppc64_elf_reloc_type_lookup (bfd *abfd,
4ce794b7 999 bfd_reloc_code_real_type code)
5bd4f169 1000{
411e1bfb 1001 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
1002
1003 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1004 /* Initialize howto table if needed. */
1005 ppc_howto_init ();
1006
4ce794b7 1007 switch (code)
5bd4f169
AM
1008 {
1009 default:
f3185997 1010 /* xgettext:c-format */
2cdcc330
AM
1011 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1012 (int) code);
f3185997 1013 bfd_set_error (bfd_error_bad_value);
4ce794b7 1014 return NULL;
5bd4f169 1015
411e1bfb
AM
1016 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1017 break;
1018 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1019 break;
1020 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1021 break;
1022 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1023 break;
1024 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1025 break;
1026 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1027 break;
f9c6b907
AM
1028 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1029 break;
411e1bfb 1030 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 1031 break;
f9c6b907
AM
1032 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1033 break;
411e1bfb 1034 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 1035 break;
411e1bfb 1036 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 1037 break;
411e1bfb 1038 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 1039 break;
411e1bfb 1040 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 1041 break;
05d0e962
AM
1042 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1043 break;
411e1bfb 1044 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 1045 break;
411e1bfb 1046 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 1047 break;
411e1bfb 1048 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 1049 break;
411e1bfb 1050 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 1051 break;
411e1bfb 1052 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 1053 break;
411e1bfb 1054 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 1055 break;
411e1bfb 1056 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 1057 break;
411e1bfb 1058 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 1059 break;
411e1bfb 1060 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 1061 break;
411e1bfb 1062 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 1063 break;
411e1bfb 1064 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 1065 break;
411e1bfb 1066 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 1067 break;
411e1bfb 1068 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 1069 break;
411e1bfb 1070 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 1071 break;
411e1bfb 1072 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 1073 break;
411e1bfb 1074 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 1075 break;
411e1bfb 1076 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 1077 break;
411e1bfb 1078 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 1079 break;
411e1bfb 1080 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 1081 break;
411e1bfb 1082 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 1083 break;
411e1bfb 1084 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 1085 break;
411e1bfb 1086 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 1087 break;
411e1bfb 1088 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 1089 break;
411e1bfb 1090 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1091 break;
411e1bfb 1092 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1093 break;
411e1bfb 1094 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1095 break;
411e1bfb 1096 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1097 break;
411e1bfb 1098 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1099 break;
411e1bfb 1100 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1101 break;
411e1bfb 1102 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1103 break;
411e1bfb 1104 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1105 break;
411e1bfb 1106 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1107 break;
411e1bfb 1108 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1109 break;
411e1bfb 1110 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1111 break;
411e1bfb 1112 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1113 break;
411e1bfb 1114 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1115 break;
411e1bfb 1116 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1117 break;
411e1bfb 1118 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1119 break;
411e1bfb 1120 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1121 break;
411e1bfb 1122 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1123 break;
411e1bfb 1124 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1125 break;
411e1bfb 1126 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1127 break;
411e1bfb 1128 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1129 break;
411e1bfb 1130 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1131 break;
411e1bfb 1132 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1133 break;
411e1bfb 1134 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1135 break;
411e1bfb 1136 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1137 break;
411e1bfb 1138 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1139 break;
c213164a 1140 case BFD_RELOC_PPC64_TLS_PCREL:
411e1bfb 1141 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1142 break;
727fc41e
AM
1143 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1144 break;
1145 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1146 break;
411e1bfb 1147 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1148 break;
411e1bfb 1149 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1150 break;
411e1bfb 1151 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1152 break;
411e1bfb 1153 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1154 break;
f9c6b907
AM
1155 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1156 break;
411e1bfb 1157 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1158 break;
f9c6b907
AM
1159 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1160 break;
411e1bfb 1161 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1162 break;
411e1bfb
AM
1163 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1164 break;
1165 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1166 break;
1167 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1168 break;
f9c6b907
AM
1169 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1170 break;
411e1bfb
AM
1171 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1172 break;
f9c6b907
AM
1173 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1174 break;
411e1bfb
AM
1175 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1176 break;
1177 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1178 break;
1179 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1180 break;
1181 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1182 break;
1183 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1184 break;
1185 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1186 break;
1187 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1188 break;
1189 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1190 break;
1191 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1192 break;
1193 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1194 break;
1195 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1196 break;
1197 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1198 break;
1199 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1200 break;
1201 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1202 break;
1203 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1204 break;
1205 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1206 break;
1207 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1208 break;
1209 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1210 break;
1211 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1212 break;
1213 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1214 break;
1215 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1216 break;
1217 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1218 break;
1219 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1220 break;
1221 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1222 break;
1223 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1224 break;
1225 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1226 break;
1227 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1228 break;
1229 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1230 break;
1231 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1232 break;
25f23106
AM
1233 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1234 break;
1235 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1236 break;
1237 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1238 break;
1239 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1240 break;
4a969973
AM
1241 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1242 break;
1243 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1244 break;
1245 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1246 break;
1247 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1248 break;
1249 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1250 break;
1251 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1252 break;
7ba71655
AM
1253 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1254 break;
a680de9a
PB
1255 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1256 break;
006589cf
AM
1257 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1258 break;
45965137
AM
1259 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1260 break;
5663e321
AM
1261 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1262 break;
1263 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1264 break;
1265 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1266 break;
1267 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1268 break;
1269 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1270 break;
1271 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1272 break;
1273 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1274 break;
c213164a
AM
1275 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1276 break;
1277 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1278 break;
1279 case BFD_RELOC_PPC64_GOT_TLSGD34: r = R_PPC64_GOT_TLSGD34;
1280 break;
1281 case BFD_RELOC_PPC64_GOT_TLSLD34: r = R_PPC64_GOT_TLSLD34;
1282 break;
1283 case BFD_RELOC_PPC64_GOT_TPREL34: r = R_PPC64_GOT_TPREL34;
1284 break;
1285 case BFD_RELOC_PPC64_GOT_DTPREL34: r = R_PPC64_GOT_DTPREL34;
1286 break;
5663e321
AM
1287 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1288 break;
1289 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1290 break;
1291 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1292 break;
1293 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1294 break;
1295 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1296 break;
1297 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1298 break;
1299 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1300 break;
1301 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1302 break;
1303 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1304 break;
1305 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1306 break;
411e1bfb
AM
1307 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1308 break;
1309 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
1310 break;
1311 }
1312
4ce794b7 1313 return ppc64_elf_howto_table[r];
5bd4f169
AM
1314};
1315
157090f7
AM
1316static reloc_howto_type *
1317ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1318 const char *r_name)
1319{
1320 unsigned int i;
1321
a4b6fadd 1322 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
1323 if (ppc64_elf_howto_raw[i].name != NULL
1324 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1325 return &ppc64_elf_howto_raw[i];
1326
1327 return NULL;
1328}
1329
5bd4f169
AM
1330/* Set the howto pointer for a PowerPC ELF reloc. */
1331
f3185997 1332static bfd_boolean
4aef7643 1333ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 1334 Elf_Internal_Rela *dst)
5bd4f169 1335{
65f38f15
AM
1336 unsigned int type;
1337
ef60b7ff 1338 /* Initialize howto table if needed. */
5bd4f169 1339 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
1340 ppc_howto_init ();
1341
65f38f15 1342 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 1343 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 1344 {
695344c0 1345 /* xgettext:c-format */
0aa13fee 1346 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 1347 abfd, type);
f3185997
NC
1348 bfd_set_error (bfd_error_bad_value);
1349 return FALSE;
d0fb9a8d 1350 }
65f38f15 1351 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
1352 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1353 {
1354 /* xgettext:c-format */
1355 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1356 abfd, type);
1357 bfd_set_error (bfd_error_bad_value);
1358 return FALSE;
1359 }
2cdcc330 1360
f3185997 1361 return TRUE;
5bd4f169
AM
1362}
1363
04c9666a 1364/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
1365
1366static bfd_reloc_status_type
4ce794b7
AM
1367ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1368 void *data, asection *input_section,
1369 bfd *output_bfd, char **error_message)
5bd4f169 1370{
a680de9a
PB
1371 enum elf_ppc64_reloc_type r_type;
1372 long insn;
1373 bfd_size_type octets;
3de43e7b 1374 bfd_vma value;
a680de9a 1375
805fc799
AM
1376 /* If this is a relocatable link (output_bfd test tells us), just
1377 call the generic function. Any adjustment will be done at final
1378 link time. */
1379 if (output_bfd != NULL)
cedb70c5 1380 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1381 input_section, output_bfd, error_message);
1382
5663e321
AM
1383 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1384 We won't actually be using the low bits, so trashing them
805fc799 1385 doesn't matter. */
a680de9a 1386 r_type = reloc_entry->howto->type;
5663e321
AM
1387 if (r_type == R_PPC64_ADDR16_HIGHERA34
1388 || r_type == R_PPC64_ADDR16_HIGHESTA34
1389 || r_type == R_PPC64_REL16_HIGHERA34
1390 || r_type == R_PPC64_REL16_HIGHESTA34)
1391 reloc_entry->addend += 1ULL << 33;
1392 else
1393 reloc_entry->addend += 1U << 15;
a680de9a
PB
1394 if (r_type != R_PPC64_REL16DX_HA)
1395 return bfd_reloc_continue;
1396
1397 value = 0;
1398 if (!bfd_is_com_section (symbol->section))
1399 value = symbol->value;
1400 value += (reloc_entry->addend
1401 + symbol->section->output_offset
1402 + symbol->section->output_section->vma);
1403 value -= (reloc_entry->address
1404 + input_section->output_offset
1405 + input_section->output_section->vma);
3de43e7b 1406 value = (bfd_signed_vma) value >> 16;
a680de9a
PB
1407
1408 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1409 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1410 insn &= ~0x1fffc1;
3de43e7b 1411 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 1412 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 1413 if (value + 0x8000 > 0xffff)
a680de9a
PB
1414 return bfd_reloc_overflow;
1415 return bfd_reloc_ok;
805fc799 1416}
5bd4f169 1417
2441e016
AM
1418static bfd_reloc_status_type
1419ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1420 void *data, asection *input_section,
1421 bfd *output_bfd, char **error_message)
1422{
1423 if (output_bfd != NULL)
1424 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1425 input_section, output_bfd, error_message);
1426
699733f6
AM
1427 if (strcmp (symbol->section->name, ".opd") == 0
1428 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
1429 {
1430 bfd_vma dest = opd_entry_value (symbol->section,
1431 symbol->value + reloc_entry->addend,
aef36ac1 1432 NULL, NULL, FALSE);
2441e016
AM
1433 if (dest != (bfd_vma) -1)
1434 reloc_entry->addend = dest - (symbol->value
1435 + symbol->section->output_section->vma
1436 + symbol->section->output_offset);
1437 }
810d4e75
AM
1438 else
1439 {
1440 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1441
1442 if (symbol->section->owner != abfd
9f284bf9 1443 && symbol->section->owner != NULL
810d4e75
AM
1444 && abiversion (symbol->section->owner) >= 2)
1445 {
1446 unsigned int i;
1447
1448 for (i = 0; i < symbol->section->owner->symcount; ++i)
1449 {
1450 asymbol *symdef = symbol->section->owner->outsymbols[i];
1451
1452 if (strcmp (symdef->name, symbol->name) == 0)
1453 {
1454 elfsym = (elf_symbol_type *) symdef;
1455 break;
1456 }
1457 }
1458 }
1459 reloc_entry->addend
1460 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1461 }
2441e016
AM
1462 return bfd_reloc_continue;
1463}
1464
805fc799 1465static bfd_reloc_status_type
4ce794b7
AM
1466ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1467 void *data, asection *input_section,
1468 bfd *output_bfd, char **error_message)
805fc799
AM
1469{
1470 long insn;
04c9666a 1471 enum elf_ppc64_reloc_type r_type;
805fc799 1472 bfd_size_type octets;
794e51c0
AM
1473 /* Assume 'at' branch hints. */
1474 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
1475
1476 /* If this is a relocatable link (output_bfd test tells us), just
1477 call the generic function. Any adjustment will be done at final
1478 link time. */
5bd4f169 1479 if (output_bfd != NULL)
cedb70c5 1480 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1481 input_section, output_bfd, error_message);
1482
1483 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1484 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1485 insn &= ~(0x01 << 21);
4ce794b7 1486 r_type = reloc_entry->howto->type;
805fc799
AM
1487 if (r_type == R_PPC64_ADDR14_BRTAKEN
1488 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 1489 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 1490
794e51c0 1491 if (is_isa_v2)
5bd4f169 1492 {
805fc799
AM
1493 /* Set 'a' bit. This is 0b00010 in BO field for branch
1494 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1495 for branch on CTR insns (BO == 1a00t or 1a01t). */
1496 if ((insn & (0x14 << 21)) == (0x04 << 21))
1497 insn |= 0x02 << 21;
1498 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1499 insn |= 0x08 << 21;
1500 else
2441e016 1501 goto out;
5bd4f169 1502 }
805fc799
AM
1503 else
1504 {
1505 bfd_vma target = 0;
1506 bfd_vma from;
5bd4f169 1507
805fc799
AM
1508 if (!bfd_is_com_section (symbol->section))
1509 target = symbol->value;
1510 target += symbol->section->output_section->vma;
1511 target += symbol->section->output_offset;
1512 target += reloc_entry->addend;
5bd4f169 1513
805fc799
AM
1514 from = (reloc_entry->address
1515 + input_section->output_offset
1516 + input_section->output_section->vma);
5bd4f169 1517
805fc799
AM
1518 /* Invert 'y' bit if not the default. */
1519 if ((bfd_signed_vma) (target - from) < 0)
1520 insn ^= 0x01 << 21;
1521 }
4ce794b7 1522 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
1523 out:
1524 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1525 input_section, output_bfd, error_message);
805fc799 1526}
5bd4f169 1527
805fc799 1528static bfd_reloc_status_type
4ce794b7
AM
1529ppc64_elf_sectoff_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 /* If this is a relocatable link (output_bfd test tells us), just
1534 call the generic function. Any adjustment will be done at final
1535 link time. */
1536 if (output_bfd != NULL)
cedb70c5 1537 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 1538 input_section, output_bfd, error_message);
5bd4f169 1539
805fc799
AM
1540 /* Subtract the symbol section base address. */
1541 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
1542 return bfd_reloc_continue;
1543}
1544
805fc799 1545static bfd_reloc_status_type
4ce794b7
AM
1546ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1547 void *data, asection *input_section,
1548 bfd *output_bfd, char **error_message)
805fc799
AM
1549{
1550 /* If this is a relocatable link (output_bfd test tells us), just
1551 call the generic function. Any adjustment will be done at final
1552 link time. */
1553 if (output_bfd != NULL)
cedb70c5 1554 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1555 input_section, output_bfd, error_message);
1556
1557 /* Subtract the symbol section base address. */
1558 reloc_entry->addend -= symbol->section->output_section->vma;
1559
1560 /* Adjust the addend for sign extension of the low 16 bits. */
1561 reloc_entry->addend += 0x8000;
1562 return bfd_reloc_continue;
1563}
1564
1565static bfd_reloc_status_type
4ce794b7
AM
1566ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1567 void *data, asection *input_section,
1568 bfd *output_bfd, char **error_message)
805fc799
AM
1569{
1570 bfd_vma TOCstart;
1571
1572 /* If this is a relocatable link (output_bfd test tells us), just
1573 call the generic function. Any adjustment will be done at final
1574 link time. */
1575 if (output_bfd != NULL)
cedb70c5 1576 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1577 input_section, output_bfd, error_message);
1578
1579 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1580 if (TOCstart == 0)
1c865ab2 1581 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1582
1583 /* Subtract the TOC base address. */
1584 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1585 return bfd_reloc_continue;
1586}
1587
1588static bfd_reloc_status_type
4ce794b7
AM
1589ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1590 void *data, asection *input_section,
1591 bfd *output_bfd, char **error_message)
805fc799
AM
1592{
1593 bfd_vma TOCstart;
1594
1595 /* If this is a relocatable link (output_bfd test tells us), just
1596 call the generic function. Any adjustment will be done at final
1597 link time. */
1598 if (output_bfd != NULL)
cedb70c5 1599 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1600 input_section, output_bfd, error_message);
1601
1602 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1603 if (TOCstart == 0)
1c865ab2 1604 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1605
1606 /* Subtract the TOC base address. */
1607 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1608
1609 /* Adjust the addend for sign extension of the low 16 bits. */
1610 reloc_entry->addend += 0x8000;
1611 return bfd_reloc_continue;
1612}
1613
1614static bfd_reloc_status_type
4ce794b7
AM
1615ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1616 void *data, asection *input_section,
1617 bfd *output_bfd, char **error_message)
805fc799
AM
1618{
1619 bfd_vma TOCstart;
1620 bfd_size_type octets;
1621
1622 /* If this is a relocatable link (output_bfd test tells us), just
1623 call the generic function. Any adjustment will be done at final
1624 link time. */
1625 if (output_bfd != NULL)
cedb70c5 1626 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1627 input_section, output_bfd, error_message);
1628
1629 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1630 if (TOCstart == 0)
1c865ab2 1631 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1632
1633 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1634 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1635 return bfd_reloc_ok;
1636}
1637
5663e321
AM
1638static bfd_reloc_status_type
1639ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1640 void *data, asection *input_section,
1641 bfd *output_bfd, char **error_message)
1642{
1643 uint64_t insn;
1644 bfd_vma targ;
1645
1646 if (output_bfd != NULL)
1647 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1648 input_section, output_bfd, error_message);
1649
1650 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1651 insn <<= 32;
1652 insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1653
1654 targ = (symbol->section->output_section->vma
1655 + symbol->section->output_offset
1656 + reloc_entry->addend);
1657 if (!bfd_is_com_section (symbol->section))
1658 targ += symbol->value;
1659 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1660 targ += 1ULL << 33;
1661 if (reloc_entry->howto->pc_relative)
1662 {
1663 bfd_vma from = (reloc_entry->address
1664 + input_section->output_offset
1665 + input_section->output_section->vma);
1666 targ -=from;
1667 }
1668 targ >>= reloc_entry->howto->rightshift;
1669 insn &= ~reloc_entry->howto->dst_mask;
1670 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1671 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1672 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1673 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1674 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1675 >= 1ULL << reloc_entry->howto->bitsize))
1676 return bfd_reloc_overflow;
1677 return bfd_reloc_ok;
1678}
1679
805fc799 1680static bfd_reloc_status_type
4ce794b7
AM
1681ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1682 void *data, asection *input_section,
1683 bfd *output_bfd, char **error_message)
805fc799
AM
1684{
1685 /* If this is a relocatable link (output_bfd test tells us), just
1686 call the generic function. Any adjustment will be done at final
1687 link time. */
1688 if (output_bfd != NULL)
cedb70c5 1689 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1690 input_section, output_bfd, error_message);
1691
1692 if (error_message != NULL)
1693 {
1694 static char buf[60];
1695 sprintf (buf, "generic linker can't handle %s",
1696 reloc_entry->howto->name);
1697 *error_message = buf;
1698 }
1699 return bfd_reloc_dangerous;
1700}
1701
927be08e
AM
1702/* Track GOT entries needed for a given symbol. We might need more
1703 than one got entry per symbol. */
1704struct got_entry
1705{
1706 struct got_entry *next;
1707
1708 /* The symbol addend that we'll be placing in the GOT. */
1709 bfd_vma addend;
1710
1711 /* Unlike other ELF targets, we use separate GOT entries for the same
1712 symbol referenced from different input files. This is to support
1713 automatic multiple TOC/GOT sections, where the TOC base can vary
1714 from one input file to another. After partitioning into TOC groups
1715 we merge entries within the group.
1716
1717 Point to the BFD owning this GOT entry. */
1718 bfd *owner;
1719
1720 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1721 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1722 unsigned char tls_type;
927be08e
AM
1723
1724 /* Non-zero if got.ent points to real entry. */
f961d9dd 1725 unsigned char is_indirect;
927be08e
AM
1726
1727 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1728 union
2cdcc330
AM
1729 {
1730 bfd_signed_vma refcount;
1731 bfd_vma offset;
1732 struct got_entry *ent;
1733 } got;
927be08e
AM
1734};
1735
1736/* The same for PLT. */
1737struct plt_entry
1738{
1739 struct plt_entry *next;
1740
1741 bfd_vma addend;
1742
1743 union
2cdcc330
AM
1744 {
1745 bfd_signed_vma refcount;
1746 bfd_vma offset;
1747 } plt;
927be08e
AM
1748};
1749
e717da7e
AM
1750struct ppc64_elf_obj_tdata
1751{
1752 struct elf_obj_tdata elf;
1753
1754 /* Shortcuts to dynamic linker sections. */
1755 asection *got;
1756 asection *relgot;
1757
b3fac117
AM
1758 /* Used during garbage collection. We attach global symbols defined
1759 on removed .opd entries to this section so that the sym is removed. */
1760 asection *deleted_section;
81688140 1761
927be08e 1762 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1763 sections means we potentially need one of these for each input bfd. */
927be08e 1764 struct got_entry tlsld_got;
8860955f 1765
2cdcc330
AM
1766 union
1767 {
729eabd5
AM
1768 /* A copy of relocs before they are modified for --emit-relocs. */
1769 Elf_Internal_Rela *relocs;
1770
1771 /* Section contents. */
1772 bfd_byte *contents;
1773 } opd;
d77c8a4b
AM
1774
1775 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1776 the reloc to be in the range -32768 to 32767. */
98528052
AM
1777 unsigned int has_small_toc_reloc : 1;
1778
560c8763
AM
1779 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1780 instruction not one we handle. */
1781 unsigned int unexpected_toc_insn : 1;
066f4018 1782
903b777d
AM
1783 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1784 this file. */
1785 unsigned int has_optrel : 1;
e717da7e
AM
1786};
1787
1788#define ppc64_elf_tdata(bfd) \
1789 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1790
1791#define ppc64_tlsld_got(bfd) \
1792 (&ppc64_elf_tdata (bfd)->tlsld_got)
1793
0c8d6e5c
AM
1794#define is_ppc64_elf(bfd) \
1795 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1796 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1797
e717da7e
AM
1798/* Override the generic function because we store some extras. */
1799
1800static bfd_boolean
1801ppc64_elf_mkobject (bfd *abfd)
1802{
0ffa91dd 1803 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1804 PPC64_ELF_DATA);
e717da7e
AM
1805}
1806
feee612b 1807/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1808 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1809
b34976b6 1810static bfd_boolean
4ce794b7 1811ppc64_elf_object_p (bfd *abfd)
feee612b 1812{
14b57c7c
AM
1813 if (!abfd->arch_info->the_default)
1814 return TRUE;
1815
1816 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1817 {
1818 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1819
1820 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1821 {
1822 /* Relies on arch after 32 bit default being 64 bit default. */
1823 abfd->arch_info = abfd->arch_info->next;
1824 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1825 }
1826 }
14b57c7c 1827 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1828}
1829
d37c89e5
AM
1830/* Support for core dump NOTE sections. */
1831
1832static bfd_boolean
1833ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1834{
eea6121a 1835 size_t offset, size;
d37c89e5
AM
1836
1837 if (note->descsz != 504)
1838 return FALSE;
1839
1840 /* pr_cursig */
228e534f 1841 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1842
1843 /* pr_pid */
228e534f 1844 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1845
1846 /* pr_reg */
1847 offset = 112;
eea6121a 1848 size = 384;
d37c89e5
AM
1849
1850 /* Make a ".reg/999" section. */
1851 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1852 size, note->descpos + offset);
d37c89e5
AM
1853}
1854
1855static bfd_boolean
1856ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1857{
1858 if (note->descsz != 136)
1859 return FALSE;
1860
228e534f 1861 elf_tdata (abfd)->core->pid
bc989cdc 1862 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1863 elf_tdata (abfd)->core->program
d37c89e5 1864 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1865 elf_tdata (abfd)->core->command
d37c89e5
AM
1866 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1867
1868 return TRUE;
1869}
1870
183e98be
AM
1871static char *
1872ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1873 ...)
1874{
1875 switch (note_type)
1876 {
1877 default:
1878 return NULL;
1879
1880 case NT_PRPSINFO:
1881 {
9ef6d1e3 1882 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1883 va_list ap;
1884
1885 va_start (ap, note_type);
75cd47ed 1886 memset (data, 0, sizeof (data));
183e98be 1887 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1888#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1889 DIAGNOSTIC_PUSH;
be3e27bb 1890 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1891 -Wstringop-truncation:
1892 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1893 */
95da9854
L
1894 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1895#endif
183e98be 1896 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1897#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1898 DIAGNOSTIC_POP;
fe75810f 1899#endif
183e98be
AM
1900 va_end (ap);
1901 return elfcore_write_note (abfd, buf, bufsiz,
1902 "CORE", note_type, data, sizeof (data));
1903 }
1904
1905 case NT_PRSTATUS:
1906 {
1907 char data[504];
1908 va_list ap;
1909 long pid;
1910 int cursig;
1911 const void *greg;
1912
1913 va_start (ap, note_type);
1914 memset (data, 0, 112);
1915 pid = va_arg (ap, long);
1916 bfd_put_32 (abfd, pid, data + 32);
1917 cursig = va_arg (ap, int);
1918 bfd_put_16 (abfd, cursig, data + 12);
1919 greg = va_arg (ap, const void *);
1920 memcpy (data + 112, greg, 384);
1921 memset (data + 496, 0, 8);
1922 va_end (ap);
1923 return elfcore_write_note (abfd, buf, bufsiz,
1924 "CORE", note_type, data, sizeof (data));
1925 }
1926 }
1927}
1928
5d35169e
AM
1929/* Add extra PPC sections. */
1930
2cdcc330 1931static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1932{
07d6d2b8
AM
1933 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1934 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1935 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1936 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1937 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1938 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1939 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1940};
1941
7c8fe5c4
AM
1942enum _ppc64_sec_type {
1943 sec_normal = 0,
1944 sec_opd = 1,
1945 sec_toc = 2
1946};
1947
f0abc2a1
AM
1948struct _ppc64_elf_section_data
1949{
1950 struct bfd_elf_section_data elf;
411e1bfb 1951
f0abc2a1
AM
1952 union
1953 {
51aecdc5
AM
1954 /* An array with one entry for each opd function descriptor,
1955 and some spares since opd entries may be either 16 or 24 bytes. */
1956#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
1957 struct _opd_sec_data
1958 {
1959 /* Points to the function code section for local opd entries. */
1960 asection **func_sec;
1961
1962 /* After editing .opd, adjust references to opd local syms. */
1963 long *adjust;
1964 } opd;
7c8fe5c4 1965
3a71aa26
AM
1966 /* An array for toc sections, indexed by offset/8. */
1967 struct _toc_sec_data
1968 {
1969 /* Specifies the relocation symbol index used at a given toc offset. */
1970 unsigned *symndx;
1971
1972 /* And the relocation addend. */
1973 bfd_vma *add;
1974 } toc;
7c8fe5c4
AM
1975 } u;
1976
1977 enum _ppc64_sec_type sec_type:2;
411e1bfb 1978
7c8fe5c4
AM
1979 /* Flag set when small branches are detected. Used to
1980 select suitable defaults for the stub group size. */
1981 unsigned int has_14bit_branch:1;
3e04d765
AM
1982
1983 /* Flag set when PLTCALL relocs are detected. */
1984 unsigned int has_pltcall:1;
066f4018 1985
903b777d
AM
1986 /* Flag set when section has PLT/GOT/TOC relocations that can be
1987 optimised. */
1988 unsigned int has_optrel:1;
f0abc2a1
AM
1989};
1990
1991#define ppc64_elf_section_data(sec) \
411e1bfb 1992 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
1993
1994static bfd_boolean
4ce794b7 1995ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1996{
f592407e
AM
1997 if (!sec->used_by_bfd)
1998 {
1999 struct _ppc64_elf_section_data *sdata;
2000 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 2001
f592407e
AM
2002 sdata = bfd_zalloc (abfd, amt);
2003 if (sdata == NULL)
2004 return FALSE;
2005 sec->used_by_bfd = sdata;
2006 }
f0abc2a1
AM
2007
2008 return _bfd_elf_new_section_hook (abfd, sec);
2009}
4025353c 2010
74f0fb50 2011static struct _opd_sec_data *
4025353c
AM
2012get_opd_info (asection * sec)
2013{
2014 if (sec != NULL
2015 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2016 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2017 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2018 return NULL;
2019}
90e3cdf2
JJ
2020\f
2021/* Parameters for the qsort hook. */
90e3cdf2 2022static bfd_boolean synthetic_relocatable;
cd285db5 2023static asection *synthetic_opd;
90e3cdf2 2024
699733f6 2025/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2026
2027static int
2028compare_symbols (const void *ap, const void *bp)
2029{
2cdcc330
AM
2030 const asymbol *a = *(const asymbol **) ap;
2031 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 2032
699733f6
AM
2033 /* Section symbols first. */
2034 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2035 return -1;
699733f6 2036 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2037 return 1;
2038
699733f6 2039 /* then .opd symbols. */
cd285db5
AM
2040 if (synthetic_opd != NULL)
2041 {
2042 if (strcmp (a->section->name, ".opd") == 0
2043 && strcmp (b->section->name, ".opd") != 0)
2044 return -1;
2045 if (strcmp (a->section->name, ".opd") != 0
2046 && strcmp (b->section->name, ".opd") == 0)
2047 return 1;
2048 }
90e3cdf2 2049
699733f6 2050 /* then other code symbols. */
2cdcc330
AM
2051 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2052 == (SEC_CODE | SEC_ALLOC))
2053 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2054 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2055 return -1;
2056
2cdcc330
AM
2057 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2058 != (SEC_CODE | SEC_ALLOC))
2059 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2060 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2061 return 1;
2062
2063 if (synthetic_relocatable)
2064 {
2065 if (a->section->id < b->section->id)
2066 return -1;
2067
2068 if (a->section->id > b->section->id)
2069 return 1;
2070 }
2071
2072 if (a->value + a->section->vma < b->value + b->section->vma)
2073 return -1;
2074
2075 if (a->value + a->section->vma > b->value + b->section->vma)
2076 return 1;
2077
4d35a0aa
AM
2078 /* For syms with the same value, prefer strong dynamic global function
2079 syms over other syms. */
2080 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2081 return -1;
2082
2083 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2084 return 1;
2085
2086 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2087 return -1;
2088
2089 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2090 return 1;
2091
2092 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2093 return -1;
2094
2095 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2096 return 1;
2097
2098 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2099 return -1;
2100
2101 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2102 return 1;
2103
aaed6f5b 2104 return a > b;
90e3cdf2
JJ
2105}
2106
699733f6 2107/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2108
699733f6 2109static asymbol *
7292b3ac 2110sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 2111{
699733f6 2112 long mid;
90e3cdf2 2113
7292b3ac 2114 if (id == (unsigned) -1)
699733f6
AM
2115 {
2116 while (lo < hi)
2117 {
2118 mid = (lo + hi) >> 1;
2119 if (syms[mid]->value + syms[mid]->section->vma < value)
2120 lo = mid + 1;
2121 else if (syms[mid]->value + syms[mid]->section->vma > value)
2122 hi = mid;
2123 else
2124 return syms[mid];
2125 }
2126 }
2127 else
2128 {
2129 while (lo < hi)
2130 {
2131 mid = (lo + hi) >> 1;
2132 if (syms[mid]->section->id < id)
2133 lo = mid + 1;
2134 else if (syms[mid]->section->id > id)
2135 hi = mid;
2136 else if (syms[mid]->value < value)
2137 lo = mid + 1;
2138 else if (syms[mid]->value > value)
2139 hi = mid;
2140 else
2141 return syms[mid];
2142 }
2143 }
2144 return NULL;
90e3cdf2
JJ
2145}
2146
468392fb
AM
2147static bfd_boolean
2148section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2149{
2150 bfd_vma vma = *(bfd_vma *) ptr;
2151 return ((section->flags & SEC_ALLOC) != 0
2152 && section->vma <= vma
2153 && vma < section->vma + section->size);
2154}
2155
699733f6 2156/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
2157 entry syms. Also generate @plt symbols for the glink branch table.
2158 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
2159
2160static long
a7535cf3
AM
2161ppc64_elf_get_synthetic_symtab (bfd *abfd,
2162 long static_count, asymbol **static_syms,
2163 long dyn_count, asymbol **dyn_syms,
c9727e01 2164 asymbol **ret)
90e3cdf2
JJ
2165{
2166 asymbol *s;
0ccf57bd 2167 size_t i, j, count;
90e3cdf2 2168 char *names;
0ccf57bd 2169 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 2170 asection *opd = NULL;
90e3cdf2 2171 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2172 asymbol **syms;
ee67d69a 2173 int abi = abiversion (abfd);
90e3cdf2
JJ
2174
2175 *ret = NULL;
2176
ee67d69a
AM
2177 if (abi < 2)
2178 {
2179 opd = bfd_get_section_by_name (abfd, ".opd");
2180 if (opd == NULL && abi == 1)
2181 return 0;
2182 }
90e3cdf2 2183
a5259595
AM
2184 syms = NULL;
2185 codesecsym = 0;
2186 codesecsymend = 0;
2187 secsymend = 0;
2188 opdsymend = 0;
2189 symcount = 0;
2190 if (opd != NULL)
c9727e01 2191 {
a5259595
AM
2192 symcount = static_count;
2193 if (!relocatable)
2194 symcount += dyn_count;
2195 if (symcount == 0)
2196 return 0;
c9727e01 2197
a5259595
AM
2198 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2199 if (syms == NULL)
2200 return -1;
90e3cdf2 2201
a5259595
AM
2202 if (!relocatable && static_count != 0 && dyn_count != 0)
2203 {
2204 /* Use both symbol tables. */
2205 memcpy (syms, static_syms, static_count * sizeof (*syms));
2206 memcpy (syms + static_count, dyn_syms,
2207 (dyn_count + 1) * sizeof (*syms));
2208 }
2209 else if (!relocatable && static_count == 0)
2210 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2211 else
2212 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2213
0ccf57bd
AM
2214 /* Trim uninteresting symbols. Interesting symbols are section,
2215 function, and notype symbols. */
2216 for (i = 0, j = 0; i < symcount; ++i)
2217 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2218 | BSF_RELC | BSF_SRELC)) == 0)
2219 syms[j++] = syms[i];
2220 symcount = j;
2221
a5259595
AM
2222 synthetic_relocatable = relocatable;
2223 synthetic_opd = opd;
2224 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2225
a5259595
AM
2226 if (!relocatable && symcount > 1)
2227 {
bfa5bd2a
PA
2228 /* Trim duplicate syms, since we may have merged the normal
2229 and dynamic symbols. Actually, we only care about syms
2230 that have different values, so trim any with the same
2231 value. Don't consider ifunc and ifunc resolver symbols
2232 duplicates however, because GDB wants to know whether a
2233 text symbol is an ifunc resolver. */
a5259595 2234 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2235 {
2236 const asymbol *s0 = syms[i - 1];
2237 const asymbol *s1 = syms[i];
2238
2239 if ((s0->value + s0->section->vma
2240 != s1->value + s1->section->vma)
2241 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2242 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2243 syms[j++] = syms[i];
2244 }
a5259595
AM
2245 symcount = j;
2246 }
699733f6 2247
a5259595
AM
2248 i = 0;
2249 /* Note that here and in compare_symbols we can't compare opd and
2250 sym->section directly. With separate debug info files, the
2251 symbols will be extracted from the debug file while abfd passed
2252 to this function is the real binary. */
0ccf57bd 2253 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2254 ++i;
2255 codesecsym = i;
2256
2257 for (; i < symcount; ++i)
2258 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2259 | SEC_THREAD_LOCAL))
2260 != (SEC_CODE | SEC_ALLOC))
2261 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2262 break;
2263 codesecsymend = i;
2264
2265 for (; i < symcount; ++i)
2266 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2267 break;
2268 secsymend = i;
2269
2270 for (; i < symcount; ++i)
2271 if (strcmp (syms[i]->section->name, ".opd") != 0)
2272 break;
2273 opdsymend = i;
2274
2275 for (; i < symcount; ++i)
2cdcc330
AM
2276 if (((syms[i]->section->flags
2277 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2278 != (SEC_CODE | SEC_ALLOC))
2279 break;
2280 symcount = i;
2281 }
c9727e01 2282 count = 0;
90e3cdf2 2283
699733f6 2284 if (relocatable)
90e3cdf2 2285 {
699733f6
AM
2286 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2287 arelent *r;
2288 size_t size;
0ccf57bd 2289 size_t relcount;
90e3cdf2 2290
468392fb
AM
2291 if (opdsymend == secsymend)
2292 goto done;
2293
699733f6 2294 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2295 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2296 if (relcount == 0)
c9727e01 2297 goto done;
90e3cdf2 2298
7356fed5
AM
2299 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2300 {
2301 count = -1;
2302 goto done;
2303 }
2304
699733f6 2305 size = 0;
595da8c5 2306 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2307 {
2308 asymbol *sym;
90e3cdf2 2309
595da8c5 2310 while (r < opd->relocation + relcount
699733f6
AM
2311 && r->address < syms[i]->value + opd->vma)
2312 ++r;
90e3cdf2 2313
595da8c5 2314 if (r == opd->relocation + relcount)
699733f6 2315 break;
90e3cdf2 2316
699733f6
AM
2317 if (r->address != syms[i]->value + opd->vma)
2318 continue;
90e3cdf2 2319
699733f6
AM
2320 if (r->howto->type != R_PPC64_ADDR64)
2321 continue;
90e3cdf2 2322
699733f6
AM
2323 sym = *r->sym_ptr_ptr;
2324 if (!sym_exists_at (syms, opdsymend, symcount,
2325 sym->section->id, sym->value + r->addend))
2326 {
2327 ++count;
2328 size += sizeof (asymbol);
2329 size += strlen (syms[i]->name) + 2;
2330 }
2331 }
90e3cdf2 2332
c4b0b099
AM
2333 if (size == 0)
2334 goto done;
699733f6
AM
2335 s = *ret = bfd_malloc (size);
2336 if (s == NULL)
2337 {
7356fed5 2338 count = -1;
c9727e01 2339 goto done;
699733f6 2340 }
90e3cdf2 2341
699733f6 2342 names = (char *) (s + count);
90e3cdf2 2343
595da8c5 2344 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2345 {
699733f6 2346 asymbol *sym;
90e3cdf2 2347
595da8c5 2348 while (r < opd->relocation + relcount
699733f6
AM
2349 && r->address < syms[i]->value + opd->vma)
2350 ++r;
90e3cdf2 2351
595da8c5 2352 if (r == opd->relocation + relcount)
699733f6
AM
2353 break;
2354
2355 if (r->address != syms[i]->value + opd->vma)
2356 continue;
2357
2358 if (r->howto->type != R_PPC64_ADDR64)
2359 continue;
90e3cdf2 2360
699733f6
AM
2361 sym = *r->sym_ptr_ptr;
2362 if (!sym_exists_at (syms, opdsymend, symcount,
2363 sym->section->id, sym->value + r->addend))
2364 {
2365 size_t len;
2366
2367 *s = *syms[i];
6ba2a415 2368 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2369 s->section = sym->section;
2370 s->value = sym->value + r->addend;
2371 s->name = names;
2372 *names++ = '.';
2373 len = strlen (syms[i]->name);
2374 memcpy (names, syms[i]->name, len + 1);
2375 names += len + 1;
6f610d07
UW
2376 /* Have udata.p point back to the original symbol this
2377 synthetic symbol was derived from. */
2378 s->udata.p = syms[i];
699733f6
AM
2379 s++;
2380 }
2381 }
2382 }
2383 else
90e3cdf2 2384 {
468392fb 2385 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 2386 bfd_byte *contents = NULL;
699733f6 2387 size_t size;
0ccf57bd 2388 size_t plt_count = 0;
468392fb
AM
2389 bfd_vma glink_vma = 0, resolv_vma = 0;
2390 asection *dynamic, *glink = NULL, *relplt = NULL;
2391 arelent *p;
90e3cdf2 2392
ee67d69a 2393 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2394 {
c4b0b099
AM
2395 free_contents_and_exit_err:
2396 count = -1;
ee67d69a 2397 free_contents_and_exit:
699733f6 2398 if (contents)
ee67d69a 2399 free (contents);
c9727e01 2400 goto done;
699733f6 2401 }
90e3cdf2 2402
699733f6
AM
2403 size = 0;
2404 for (i = secsymend; i < opdsymend; ++i)
2405 {
2406 bfd_vma ent;
90e3cdf2 2407
5ef11c02
AM
2408 /* Ignore bogus symbols. */
2409 if (syms[i]->value > opd->size - 8)
2410 continue;
2411
699733f6
AM
2412 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2413 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2414 {
2415 ++count;
2416 size += sizeof (asymbol);
2417 size += strlen (syms[i]->name) + 2;
2418 }
2419 }
90e3cdf2 2420
468392fb 2421 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2422 if (dyn_count != 0
2423 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2424 {
2425 bfd_byte *dynbuf, *extdyn, *extdynend;
2426 size_t extdynsize;
2427 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2428
2429 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2430 goto free_contents_and_exit_err;
468392fb
AM
2431
2432 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2433 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2434
2435 extdyn = dynbuf;
2436 extdynend = extdyn + dynamic->size;
2437 for (; extdyn < extdynend; extdyn += extdynsize)
2438 {
2439 Elf_Internal_Dyn dyn;
2440 (*swap_dyn_in) (abfd, extdyn, &dyn);
2441
2442 if (dyn.d_tag == DT_NULL)
2443 break;
2444
2445 if (dyn.d_tag == DT_PPC64_GLINK)
2446 {
9e390558
AM
2447 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2448 See comment in ppc64_elf_finish_dynamic_sections. */
2449 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2450 /* The .glink section usually does not survive the final
2451 link; search for the section (usually .text) where the
2452 glink stubs now reside. */
2453 glink = bfd_sections_find_if (abfd, section_covers_vma,
2454 &glink_vma);
2455 break;
2456 }
2457 }
2458
2459 free (dynbuf);
2460 }
2461
2462 if (glink != NULL)
2463 {
2464 /* Determine __glink trampoline by reading the relative branch
2465 from the first glink stub. */
2466 bfd_byte buf[4];
b9e5796b
AM
2467 unsigned int off = 0;
2468
2469 while (bfd_get_section_contents (abfd, glink, buf,
2470 glink_vma + off - glink->vma, 4))
468392fb
AM
2471 {
2472 unsigned int insn = bfd_get_32 (abfd, buf);
2473 insn ^= B_DOT;
2474 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2475 {
2cdcc330
AM
2476 resolv_vma
2477 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2478 break;
2479 }
2480 off += 4;
2481 if (off > 4)
2482 break;
468392fb
AM
2483 }
2484
2485 if (resolv_vma)
2486 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2487
066ee829
AM
2488 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2489 if (relplt != NULL)
2490 {
2491 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2cdcc330 2492 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 2493 goto free_contents_and_exit_err;
68ffbac6 2494
066ee829
AM
2495 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2496 size += plt_count * sizeof (asymbol);
468392fb 2497
066ee829
AM
2498 p = relplt->relocation;
2499 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2500 {
2501 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2502 if (p->addend != 0)
2503 size += sizeof ("+0x") - 1 + 16;
2504 }
066ee829 2505 }
468392fb
AM
2506 }
2507
c4b0b099
AM
2508 if (size == 0)
2509 goto free_contents_and_exit;
699733f6
AM
2510 s = *ret = bfd_malloc (size);
2511 if (s == NULL)
c4b0b099 2512 goto free_contents_and_exit_err;
90e3cdf2 2513
468392fb 2514 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2515
699733f6 2516 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2517 {
699733f6 2518 bfd_vma ent;
90e3cdf2 2519
5ef11c02
AM
2520 if (syms[i]->value > opd->size - 8)
2521 continue;
2522
699733f6
AM
2523 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2524 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2525 {
0ccf57bd 2526 size_t lo, hi;
699733f6 2527 size_t len;
c9727e01 2528 asection *sec = abfd->sections;
90e3cdf2 2529
699733f6
AM
2530 *s = *syms[i];
2531 lo = codesecsym;
2532 hi = codesecsymend;
2533 while (lo < hi)
2534 {
0ccf57bd 2535 size_t mid = (lo + hi) >> 1;
699733f6
AM
2536 if (syms[mid]->section->vma < ent)
2537 lo = mid + 1;
2538 else if (syms[mid]->section->vma > ent)
2539 hi = mid;
2540 else
c9727e01
AM
2541 {
2542 sec = syms[mid]->section;
2543 break;
2544 }
699733f6
AM
2545 }
2546
c9727e01 2547 if (lo >= hi && lo > codesecsym)
699733f6 2548 sec = syms[lo - 1]->section;
699733f6
AM
2549
2550 for (; sec != NULL; sec = sec->next)
2551 {
2552 if (sec->vma > ent)
2553 break;
63524580
JK
2554 /* SEC_LOAD may not be set if SEC is from a separate debug
2555 info file. */
2556 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2557 break;
2558 if ((sec->flags & SEC_CODE) != 0)
2559 s->section = sec;
2560 }
6ba2a415 2561 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2562 s->value = ent - s->section->vma;
2563 s->name = names;
2564 *names++ = '.';
2565 len = strlen (syms[i]->name);
2566 memcpy (names, syms[i]->name, len + 1);
2567 names += len + 1;
6f610d07
UW
2568 /* Have udata.p point back to the original symbol this
2569 synthetic symbol was derived from. */
2570 s->udata.p = syms[i];
699733f6 2571 s++;
90e3cdf2 2572 }
90e3cdf2 2573 }
699733f6 2574 free (contents);
468392fb
AM
2575
2576 if (glink != NULL && relplt != NULL)
2577 {
2578 if (resolv_vma)
2579 {
2580 /* Add a symbol for the main glink trampoline. */
86a4952b 2581 memset (s, 0, sizeof *s);
468392fb 2582 s->the_bfd = abfd;
6ba2a415 2583 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2584 s->section = glink;
2585 s->value = resolv_vma - glink->vma;
2586 s->name = names;
2cdcc330
AM
2587 memcpy (names, "__glink_PLTresolve",
2588 sizeof ("__glink_PLTresolve"));
468392fb
AM
2589 names += sizeof ("__glink_PLTresolve");
2590 s++;
2591 count++;
2592 }
2593
2594 /* FIXME: It would be very much nicer to put sym@plt on the
2595 stub rather than on the glink branch table entry. The
2596 objdump disassembler would then use a sensible symbol
2597 name on plt calls. The difficulty in doing so is
2598 a) finding the stubs, and,
2599 b) matching stubs against plt entries, and,
2600 c) there can be multiple stubs for a given plt entry.
2601
2602 Solving (a) could be done by code scanning, but older
2603 ppc64 binaries used different stubs to current code.
2604 (b) is the tricky one since you need to known the toc
2605 pointer for at least one function that uses a pic stub to
2606 be able to calculate the plt address referenced.
2607 (c) means gdb would need to set multiple breakpoints (or
2608 find the glink branch itself) when setting breakpoints
2609 for pending shared library loads. */
2610 p = relplt->relocation;
2611 for (i = 0; i < plt_count; i++, p++)
2612 {
2613 size_t len;
2614
2615 *s = **p->sym_ptr_ptr;
2616 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2617 we are defining a symbol, ensure one of them is set. */
2618 if ((s->flags & BSF_LOCAL) == 0)
2619 s->flags |= BSF_GLOBAL;
6ba2a415 2620 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2621 s->section = glink;
2622 s->value = glink_vma - glink->vma;
2623 s->name = names;
2624 s->udata.p = NULL;
2625 len = strlen ((*p->sym_ptr_ptr)->name);
2626 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2627 names += len;
e054468f
AM
2628 if (p->addend != 0)
2629 {
2630 memcpy (names, "+0x", sizeof ("+0x") - 1);
2631 names += sizeof ("+0x") - 1;
2632 bfd_sprintf_vma (abfd, names, p->addend);
2633 names += strlen (names);
2634 }
468392fb
AM
2635 memcpy (names, "@plt", sizeof ("@plt"));
2636 names += sizeof ("@plt");
2637 s++;
b9e5796b
AM
2638 if (abi < 2)
2639 {
2640 glink_vma += 8;
2641 if (i >= 0x8000)
2642 glink_vma += 4;
2643 }
2644 else
468392fb
AM
2645 glink_vma += 4;
2646 }
2647 count += plt_count;
2648 }
90e3cdf2
JJ
2649 }
2650
c9727e01 2651 done:
a7535cf3 2652 free (syms);
90e3cdf2
JJ
2653 return count;
2654}
5bd4f169 2655\f
65f38f15
AM
2656/* The following functions are specific to the ELF linker, while
2657 functions above are used generally. Those named ppc64_elf_* are
2658 called by the main ELF linker code. They appear in this file more
2659 or less in the order in which they are called. eg.
2660 ppc64_elf_check_relocs is called early in the link process,
2661 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2662 called.
2663
2664 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2665 functions have both a function code symbol and a function descriptor
2666 symbol. A call to foo in a relocatable object file looks like:
2667
2668 . .text
2669 . x:
2670 . bl .foo
2671 . nop
2672
2673 The function definition in another object file might be:
2674
2675 . .section .opd
2676 . foo: .quad .foo
2677 . .quad .TOC.@tocbase
2678 . .quad 0
2679 .
2680 . .text
2681 . .foo: blr
2682
2683 When the linker resolves the call during a static link, the branch
2684 unsurprisingly just goes to .foo and the .opd information is unused.
2685 If the function definition is in a shared library, things are a little
2686 different: The call goes via a plt call stub, the opd information gets
2687 copied to the plt, and the linker patches the nop.
2688
2689 . x:
2690 . bl .foo_stub
2691 . ld 2,40(1)
2692 .
2693 .
2694 . .foo_stub:
71a39c98
AM
2695 . std 2,40(1) # in practice, the call stub
2696 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2697 . addi 11,11,Lfoo@toc@l # this is the general idea
2698 . ld 12,0(11)
2699 . ld 2,8(11)
2700 . mtctr 12
2701 . ld 11,16(11)
e86ce104
AM
2702 . bctr
2703 .
2704 . .section .plt
2705 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2706
2707 The "reloc ()" notation is supposed to indicate that the linker emits
2708 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2709 copying.
2710
2711 What are the difficulties here? Well, firstly, the relocations
2712 examined by the linker in check_relocs are against the function code
2713 sym .foo, while the dynamic relocation in the plt is emitted against
2714 the function descriptor symbol, foo. Somewhere along the line, we need
2715 to carefully copy dynamic link information from one symbol to the other.
2716 Secondly, the generic part of the elf linker will make .foo a dynamic
2717 symbol as is normal for most other backends. We need foo dynamic
2718 instead, at least for an application final link. However, when
2719 creating a shared library containing foo, we need to have both symbols
2720 dynamic so that references to .foo are satisfied during the early
2721 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2722 definition from some other object, eg. a static library.
2723
2724 Update: As of August 2004, we support a new convention. Function
2725 calls may use the function descriptor symbol, ie. "bl foo". This
2726 behaves exactly as "bl .foo". */
65f38f15 2727
7c8bbca5
AM
2728/* Of those relocs that might be copied as dynamic relocs, this
2729 function selects those that must be copied when linking a shared
2730 library or PIE, even when the symbol is local. */
65f38f15 2731
1d483afe
AM
2732static int
2733must_be_dyn_reloc (struct bfd_link_info *info,
2734 enum elf_ppc64_reloc_type r_type)
2735{
2736 switch (r_type)
2737 {
2738 default:
7c8bbca5
AM
2739 /* Only relative relocs can be resolved when the object load
2740 address isn't fixed. DTPREL64 is excluded because the
2741 dynamic linker needs to differentiate global dynamic from
2742 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2743 return 1;
2744
2745 case R_PPC64_REL32:
2746 case R_PPC64_REL64:
2747 case R_PPC64_REL30:
2748 return 0;
2749
2750 case R_PPC64_TPREL16:
2751 case R_PPC64_TPREL16_LO:
2752 case R_PPC64_TPREL16_HI:
2753 case R_PPC64_TPREL16_HA:
2754 case R_PPC64_TPREL16_DS:
2755 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2756 case R_PPC64_TPREL16_HIGH:
2757 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2758 case R_PPC64_TPREL16_HIGHER:
2759 case R_PPC64_TPREL16_HIGHERA:
2760 case R_PPC64_TPREL16_HIGHEST:
2761 case R_PPC64_TPREL16_HIGHESTA:
2762 case R_PPC64_TPREL64:
c213164a 2763 case R_PPC64_TPREL34:
7c8bbca5
AM
2764 /* These relocations are relative but in a shared library the
2765 linker doesn't know the thread pointer base. */
2766 return bfd_link_dll (info);
1d483afe
AM
2767 }
2768}
65f38f15 2769
f4656909
AM
2770/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2771 copying dynamic variables from a shared lib into an app's dynbss
2772 section, and instead use a dynamic relocation to point into the
5d35169e
AM
2773 shared lib. With code that gcc generates, it's vital that this be
2774 enabled; In the PowerPC64 ABI, the address of a function is actually
2775 the address of a function descriptor, which resides in the .opd
2776 section. gcc uses the descriptor directly rather than going via the
2777 GOT as some other ABI's do, which means that initialized function
2778 pointers must reference the descriptor. Thus, a function pointer
2779 initialized to the address of a function in a shared library will
2780 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 2781 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
2782 presents a problem as a plt entry for that function is also
2783 initialized from the function descriptor symbol and the copy reloc
2784 may not be initialized first. */
a23b6845 2785#define ELIMINATE_COPY_RELOCS 1
f4656909 2786
721956f4 2787/* Section name for stubs is the associated section name plus this
29942be8
NC
2788 string. */
2789#define STUB_SUFFIX ".stub"
721956f4
AM
2790
2791/* Linker stubs.
2792 ppc_stub_long_branch:
2793 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2794 destination, but a 24 bit branch in a stub section will reach.
2795 . b dest
2796
2797 ppc_stub_plt_branch:
2798 Similar to the above, but a 24 bit branch in the stub section won't
2799 reach its destination.
71a39c98
AM
2800 . addis %r11,%r2,xxx@toc@ha
2801 . ld %r12,xxx@toc@l(%r11)
2802 . mtctr %r12
721956f4
AM
2803 . bctr
2804
2805 ppc_stub_plt_call:
2c66dc6c
AM
2806 Used to call a function in a shared library. If it so happens that
2807 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2808 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 2809 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
2810 . addis %r11,%r2,xxx@toc@ha
2811 . ld %r12,xxx+0@toc@l(%r11)
2812 . mtctr %r12
2813 . ld %r2,xxx+8@toc@l(%r11)
2814 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2815 . bctr
ad8e1ba5
AM
2816
2817 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2818 code to adjust the value and save r2 to support multiple toc sections.
2819 A ppc_stub_long_branch with an r2 offset looks like:
2820 . std %r2,40(%r1)
2821 . addis %r2,%r2,off@ha
2822 . addi %r2,%r2,off@l
2823 . b dest
2824
2825 A ppc_stub_plt_branch with an r2 offset looks like:
2826 . std %r2,40(%r1)
71a39c98
AM
2827 . addis %r11,%r2,xxx@toc@ha
2828 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
2829 . addis %r2,%r2,off@ha
2830 . addi %r2,%r2,off@l
71a39c98 2831 . mtctr %r12
ad8e1ba5 2832 . bctr
ac2df442 2833
05d0e962
AM
2834 All of the above stubs are shown as their ELFv1 variants. ELFv2
2835 variants exist too, simpler for plt calls since a new toc pointer
2836 and static chain are not loaded by the stub. In addition, ELFv2
2837 has some more complex stubs to handle calls marked with NOTOC
2838 relocs from functions where r2 is not a valid toc pointer. These
2839 come in two flavours, the ones shown below, and _both variants that
2840 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2841 one call is from a function where r2 is used as the toc pointer but
2842 needs a toc adjusting stub for small-model multi-toc, and another
2843 call is from a function where r2 is not valid.
2844 ppc_stub_long_branch_notoc:
2845 . mflr %r12
2846 . bcl 20,31,1f
2847 . 1:
2848 . mflr %r11
2849 . mtlr %r12
2cdcc330
AM
2850 . addis %r12,%r11,dest-1b@ha
2851 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2852 . b dest
2853
2854 ppc_stub_plt_branch_notoc:
2855 . mflr %r12
2856 . bcl 20,31,1f
2857 . 1:
2858 . mflr %r11
2859 . mtlr %r12
2860 . lis %r12,xxx-1b@highest
f891966f 2861 . ori %r12,%r12,xxx-1b@higher
05d0e962 2862 . sldi %r12,%r12,32
f891966f 2863 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2864 . ori %r12,%r12,xxx-1b@l
2865 . add %r12,%r11,%r12
2866 . mtctr %r12
2867 . bctr
2868
2869 ppc_stub_plt_call_notoc:
2870 . mflr %r12
2871 . bcl 20,31,1f
2872 . 1:
2873 . mflr %r11
2874 . mtlr %r12
2875 . lis %r12,xxx-1b@highest
f891966f 2876 . ori %r12,%r12,xxx-1b@higher
05d0e962 2877 . sldi %r12,%r12,32
f891966f 2878 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2879 . ori %r12,%r12,xxx-1b@l
2880 . ldx %r12,%r11,%r12
2881 . mtctr %r12
2882 . bctr
2883
04bdff6a
AM
2884 There are also ELFv1 powerxx variants of these stubs.
2885 ppc_stub_long_branch_notoc:
d4b87b1e 2886 . pla %r12,dest@pcrel
04bdff6a
AM
2887 . b dest
2888 ppc_stub_plt_branch_notoc:
2889 . lis %r11,(dest-1f)@highesta34
2890 . ori %r11,%r11,(dest-1f)@highera34
2891 . sldi %r11,%r11,34
d4b87b1e 2892 . 1: pla %r12,dest@pcrel
04bdff6a
AM
2893 . add %r12,%r11,%r12
2894 . mtctr %r12
2895 . bctr
2896 ppc_stub_plt_call_notoc:
2897 . lis %r11,(xxx-1f)@highesta34
2898 . ori %r11,%r11,(xxx-1f)@highera34
2899 . sldi %r11,%r11,34
d4b87b1e 2900 . 1: pla %r12,xxx@pcrel
04bdff6a
AM
2901 . ldx %r12,%r11,%r12
2902 . mtctr %r12
2903 . bctr
2904
05d0e962
AM
2905 In cases where the high instructions would add zero, they are
2906 omitted and following instructions modified in some cases.
04bdff6a
AM
2907 For example, a powerxx ppc_stub_plt_call_notoc might simplify down
2908 to
2909 . pld %r12,xxx@pcrel
2910 . mtctr %r12
2911 . bctr
05d0e962
AM
2912
2913 For a given stub group (a set of sections all using the same toc
2914 pointer value) there will be just one stub type used for any
2915 particular function symbol. For example, if printf is called from
2916 code with the tocsave optimization (ie. r2 saved in function
2917 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2918 and from other code without the tocsave optimization requiring a
2919 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2920 type will be created. Calls with the tocsave optimization will
2921 enter this stub after the instruction saving r2. A similar
2922 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2923 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2924 to call an external function like printf. If other calls to printf
2925 require a ppc_stub_plt_call linkage stub then a single
2926 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2927 call. If other calls to printf require a ppc_stub_plt_call_r2save
2928 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2929 be created and calls not requiring r2 to be saved will enter the
2930 stub after the r2 save instruction. There is an analogous
2931 hierarchy of long branch and plt branch stubs for local call
2932 linkage. */
721956f4 2933
2cdcc330
AM
2934enum ppc_stub_type
2935{
721956f4
AM
2936 ppc_stub_none,
2937 ppc_stub_long_branch,
ad8e1ba5 2938 ppc_stub_long_branch_r2off,
05d0e962
AM
2939 ppc_stub_long_branch_notoc,
2940 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 2941 ppc_stub_plt_branch,
ad8e1ba5 2942 ppc_stub_plt_branch_r2off,
05d0e962
AM
2943 ppc_stub_plt_branch_notoc,
2944 ppc_stub_plt_branch_both,
794e51c0 2945 ppc_stub_plt_call,
7341d5e2 2946 ppc_stub_plt_call_r2save,
05d0e962
AM
2947 ppc_stub_plt_call_notoc,
2948 ppc_stub_plt_call_both,
a4b6fadd
AM
2949 ppc_stub_global_entry,
2950 ppc_stub_save_res
721956f4
AM
2951};
2952
6f20ed8a
AM
2953/* Information on stub grouping. */
2954struct map_stub
2955{
2956 /* The stub section. */
2957 asection *stub_sec;
2958 /* This is the section to which stubs in the group will be attached. */
2959 asection *link_sec;
a4b6fadd
AM
2960 /* Next group. */
2961 struct map_stub *next;
2962 /* Whether to emit a copy of register save/restore functions in this
2963 group. */
2964 int needs_save_res;
df136d64
AM
2965 /* Current offset within stubs after the insn restoring lr in a
2966 _notoc or _both stub using bcl for pc-relative addressing, or
2967 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2968 unsigned int lr_restore;
2969 /* Accumulated size of EH info emitted to describe return address
2970 if stubs modify lr. Does not include 17 byte FDE header. */
2971 unsigned int eh_size;
2972 /* Offset in glink_eh_frame to the start of EH info for this group. */
2973 unsigned int eh_base;
6f20ed8a
AM
2974};
2975
2cdcc330
AM
2976struct ppc_stub_hash_entry
2977{
721956f4
AM
2978 /* Base hash table entry structure. */
2979 struct bfd_hash_entry root;
2980
ad8e1ba5
AM
2981 enum ppc_stub_type stub_type;
2982
6f20ed8a
AM
2983 /* Group information. */
2984 struct map_stub *group;
721956f4
AM
2985
2986 /* Offset within stub_sec of the beginning of this stub. */
2987 bfd_vma stub_offset;
2988
2989 /* Given the symbol's value and its section we can determine its final
2990 value when building the stubs (so the stub knows where to jump. */
2991 bfd_vma target_value;
2992 asection *target_section;
2993
721956f4
AM
2994 /* The symbol table entry, if any, that this was derived from. */
2995 struct ppc_link_hash_entry *h;
e054468f 2996 struct plt_entry *plt_ent;
721956f4 2997
2d7ad24e
AM
2998 /* Symbol type. */
2999 unsigned char symtype;
3000
6911b7dc
AM
3001 /* Symbol st_other. */
3002 unsigned char other;
721956f4
AM
3003};
3004
2cdcc330
AM
3005struct ppc_branch_hash_entry
3006{
721956f4
AM
3007 /* Base hash table entry structure. */
3008 struct bfd_hash_entry root;
3009
c456f082 3010 /* Offset within branch lookup table. */
721956f4
AM
3011 unsigned int offset;
3012
3013 /* Generation marker. */
3014 unsigned int iter;
3015};
65f38f15 3016
19e08130
AM
3017/* Used to track dynamic relocations for local symbols. */
3018struct ppc_dyn_relocs
3019{
3020 struct ppc_dyn_relocs *next;
3021
3022 /* The input section of the reloc. */
3023 asection *sec;
3024
3025 /* Total number of relocs copied for the input section. */
3026 unsigned int count : 31;
3027
3028 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3029 unsigned int ifunc : 1;
3030};
3031
65f38f15
AM
3032struct ppc_link_hash_entry
3033{
3034 struct elf_link_hash_entry elf;
3035
2cdcc330
AM
3036 union
3037 {
b3fac117
AM
3038 /* A pointer to the most recently used stub hash entry against this
3039 symbol. */
3040 struct ppc_stub_hash_entry *stub_cache;
3041
3042 /* A pointer to the next symbol starting with a '.' */
3043 struct ppc_link_hash_entry *next_dot_sym;
3044 } u;
721956f4 3045
65f38f15 3046 /* Track dynamic relocs copied for this symbol. */
6061a67d 3047 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3048
721956f4 3049 /* Link between function code and descriptor symbols. */
34814b9f 3050 struct ppc_link_hash_entry *oh;
721956f4 3051
e86ce104
AM
3052 /* Flag function code and descriptor symbols. */
3053 unsigned int is_func:1;
3054 unsigned int is_func_descriptor:1;
908b32fc 3055 unsigned int fake:1;
411e1bfb 3056
c5614fa4
AM
3057 /* Whether global opd/toc sym has been adjusted or not.
3058 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3059 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3060 unsigned int adjust_done:1;
3061
a4b6fadd
AM
3062 /* Set if this is an out-of-line register save/restore function,
3063 with non-standard calling convention. */
3064 unsigned int save_res:1;
3065
8b5f1ed8
AM
3066 /* Set if a duplicate symbol with non-zero localentry is detected,
3067 even when the duplicate symbol does not provide a definition. */
3068 unsigned int non_zero_localentry:1;
3069
411e1bfb 3070 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3071 Bits are or'd into the mask as the corresponding relocs are
3072 encountered during check_relocs, with TLS_TLS being set when any
3073 of the other TLS bits are set. tls_optimize clears bits when
3074 optimizing to indicate the corresponding GOT entry type is not
3075 needed. If set, TLS_TLS is never cleared. tls_optimize may also
b00a0a86 3076 set TLS_GDIE when a GD reloc turns into an IE one.
37da22e5
AM
3077 These flags are also kept for local symbols. */
3078#define TLS_TLS 1 /* Any TLS reloc. */
3079#define TLS_GD 2 /* GD reloc. */
3080#define TLS_LD 4 /* LD reloc. */
3081#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3082#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3083#define TLS_MARK 32 /* __tls_get_addr call marked. */
b00a0a86 3084#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
46e9995a 3085#define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
f961d9dd 3086 unsigned char tls_mask;
37da22e5
AM
3087
3088 /* The above field is also used to mark function symbols. In which
3089 case TLS_TLS will be 0. */
3090#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3091#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3092#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3093};
3094
3095/* ppc64 ELF linker hash table. */
3096
3097struct ppc_link_hash_table
3098{
3099 struct elf_link_hash_table elf;
3100
721956f4
AM
3101 /* The stub hash table. */
3102 struct bfd_hash_table stub_hash_table;
3103
3104 /* Another hash table for plt_branch stubs. */
3105 struct bfd_hash_table branch_hash_table;
3106
3b421ab3
AM
3107 /* Hash table for function prologue tocsave. */
3108 htab_t tocsave_htab;
3109
e7d1c40c
AM
3110 /* Various options and other info passed from the linker. */
3111 struct ppc64_elf_params *params;
721956f4 3112
6f20ed8a
AM
3113 /* The size of sec_info below. */
3114 unsigned int sec_info_arr_size;
3115
3116 /* Per-section array of extra section info. Done this way rather
3117 than as part of ppc64_elf_section_data so we have the info for
3118 non-ppc64 sections. */
3119 struct
3120 {
3121 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3122 bfd_vma toc_off;
6f20ed8a
AM
3123
3124 union
3125 {
3126 /* The section group that this section belongs to. */
3127 struct map_stub *group;
3128 /* A temp section list pointer. */
3129 asection *list;
3130 } u;
3131 } *sec_info;
721956f4 3132
a4b6fadd
AM
3133 /* Linked list of groups. */
3134 struct map_stub *group;
3135
ad8e1ba5
AM
3136 /* Temp used when calculating TOC pointers. */
3137 bfd_vma toc_curr;
bf102f86
AM
3138 bfd *toc_bfd;
3139 asection *toc_first_sec;
ad8e1ba5 3140
b3fac117
AM
3141 /* Used when adding symbols. */
3142 struct ppc_link_hash_entry *dot_syms;
3143
33e44f2e 3144 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3145 asection *glink;
9e390558 3146 asection *global_entry;
82bd7b59 3147 asection *sfpr;
2d7ad24e
AM
3148 asection *pltlocal;
3149 asection *relpltlocal;
4ce794b7
AM
3150 asection *brlt;
3151 asection *relbrlt;
58d180e8 3152 asection *glink_eh_frame;
ec338859 3153
8387904d
AM
3154 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3155 struct ppc_link_hash_entry *tls_get_addr;
3156 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3157
927be08e
AM
3158 /* The size of reliplt used by got entry relocs. */
3159 bfd_size_type got_reli_size;
3160
9b5ecbd0 3161 /* Statistics. */
7341d5e2 3162 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 3163
ee75fd95
AM
3164 /* Number of stubs against global syms. */
3165 unsigned long stub_globals;
3166
ee67d69a
AM
3167 /* Set if we're linking code with function descriptors. */
3168 unsigned int opd_abi:1;
3169
4c52953f 3170 /* Support for multiple toc sections. */
33c0ec9d 3171 unsigned int do_multi_toc:1;
4c52953f 3172 unsigned int multi_toc_needed:1;
927be08e 3173 unsigned int second_toc_pass:1;
67f0cbdb 3174 unsigned int do_toc_opt:1;
4c52953f 3175
9a23f96e
AM
3176 /* Set if tls optimization is enabled. */
3177 unsigned int do_tls_opt:1;
3178
3e04d765
AM
3179 /* Set if inline plt calls should be converted to direct calls. */
3180 unsigned int can_convert_all_inline_plt:1;
3181
5d1634d7 3182 /* Set on error. */
99877b66 3183 unsigned int stub_error:1;
721956f4 3184
8c5b4e52
AM
3185 /* Whether func_desc_adjust needs to be run over symbols. */
3186 unsigned int need_func_desc_adj:1;
721956f4 3187
82e66161
AM
3188 /* Whether there exist local gnu indirect function resolvers,
3189 referenced by dynamic relocations. */
3190 unsigned int local_ifunc_resolver:1;
3191 unsigned int maybe_local_ifunc_resolver:1;
3192
f378ab09
AM
3193 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3194 unsigned int has_plt_localentry0:1;
3195
5663e321
AM
3196 /* Whether calls are made via the PLT from NOTOC functions. */
3197 unsigned int notoc_plt:1;
3198
04bdff6a
AM
3199 /* Whether to use powerxx instructions in linkage stubs. */
3200 unsigned int powerxx_stubs:1;
3201
721956f4
AM
3202 /* Incremented every time we size stubs. */
3203 unsigned int stub_iteration;
5d1634d7 3204
87d72d41
AM
3205 /* Small local sym cache. */
3206 struct sym_cache sym_cache;
65f38f15
AM
3207};
3208
4c52953f
AM
3209/* Rename some of the generic section flags to better document how they
3210 are used here. */
b0dddeec
AM
3211
3212/* Nonzero if this section has TLS related relocations. */
3213#define has_tls_reloc sec_flg0
3214
23cedd1d 3215/* Nonzero if this section has an old-style call to __tls_get_addr. */
b0dddeec
AM
3216#define has_tls_get_addr_call sec_flg1
3217
3218/* Nonzero if this section has any toc or got relocs. */
3219#define has_toc_reloc sec_flg2
3220
3221/* Nonzero if this section has a call to another section that uses
3222 the toc or got. */
d77c8a4b 3223#define makes_toc_func_call sec_flg3
b0dddeec
AM
3224
3225/* Recursion protection when determining above flag. */
d77c8a4b 3226#define call_check_in_progress sec_flg4
70cc837d 3227#define call_check_done sec_flg5
4c52953f 3228
65f38f15
AM
3229/* Get the ppc64 ELF linker hash table from a link_info structure. */
3230
3231#define ppc_hash_table(p) \
4dfe6ac6
NC
3232 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3233 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3234
721956f4
AM
3235#define ppc_stub_hash_lookup(table, string, create, copy) \
3236 ((struct ppc_stub_hash_entry *) \
3237 bfd_hash_lookup ((table), (string), (create), (copy)))
3238
3239#define ppc_branch_hash_lookup(table, string, create, copy) \
3240 ((struct ppc_branch_hash_entry *) \
3241 bfd_hash_lookup ((table), (string), (create), (copy)))
3242
3243/* Create an entry in the stub hash table. */
3244
3245static struct bfd_hash_entry *
4ce794b7
AM
3246stub_hash_newfunc (struct bfd_hash_entry *entry,
3247 struct bfd_hash_table *table,
3248 const char *string)
721956f4
AM
3249{
3250 /* Allocate the structure if it has not already been allocated by a
3251 subclass. */
3252 if (entry == NULL)
3253 {
3254 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3255 if (entry == NULL)
3256 return entry;
3257 }
3258
3259 /* Call the allocation method of the superclass. */
3260 entry = bfd_hash_newfunc (entry, table, string);
3261 if (entry != NULL)
3262 {
3263 struct ppc_stub_hash_entry *eh;
3264
3265 /* Initialize the local fields. */
3266 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3267 eh->stub_type = ppc_stub_none;
6f20ed8a 3268 eh->group = NULL;
721956f4
AM
3269 eh->stub_offset = 0;
3270 eh->target_value = 0;
3271 eh->target_section = NULL;
721956f4 3272 eh->h = NULL;
6911b7dc 3273 eh->plt_ent = NULL;
6911b7dc 3274 eh->other = 0;
721956f4
AM
3275 }
3276
3277 return entry;
3278}
3279
3280/* Create an entry in the branch hash table. */
3281
3282static struct bfd_hash_entry *
4ce794b7
AM
3283branch_hash_newfunc (struct bfd_hash_entry *entry,
3284 struct bfd_hash_table *table,
3285 const char *string)
721956f4
AM
3286{
3287 /* Allocate the structure if it has not already been allocated by a
3288 subclass. */
3289 if (entry == NULL)
3290 {
3291 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3292 if (entry == NULL)
3293 return entry;
3294 }
3295
3296 /* Call the allocation method of the superclass. */
3297 entry = bfd_hash_newfunc (entry, table, string);
3298 if (entry != NULL)
3299 {
3300 struct ppc_branch_hash_entry *eh;
3301
3302 /* Initialize the local fields. */
3303 eh = (struct ppc_branch_hash_entry *) entry;
3304 eh->offset = 0;
3305 eh->iter = 0;
3306 }
3307
3308 return entry;
3309}
3310
65f38f15
AM
3311/* Create an entry in a ppc64 ELF linker hash table. */
3312
3313static struct bfd_hash_entry *
4ce794b7
AM
3314link_hash_newfunc (struct bfd_hash_entry *entry,
3315 struct bfd_hash_table *table,
3316 const char *string)
65f38f15
AM
3317{
3318 /* Allocate the structure if it has not already been allocated by a
3319 subclass. */
3320 if (entry == NULL)
3321 {
3322 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3323 if (entry == NULL)
3324 return entry;
3325 }
3326
3327 /* Call the allocation method of the superclass. */
3328 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3329 if (entry != NULL)
3330 {
3331 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3332
b3fac117 3333 memset (&eh->u.stub_cache, 0,
908b32fc 3334 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3335 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3336
3337 /* When making function calls, old ABI code references function entry
3338 points (dot symbols), while new ABI code references the function
3339 descriptor symbol. We need to make any combination of reference and
3340 definition work together, without breaking archive linking.
3341
3342 For a defined function "foo" and an undefined call to "bar":
3343 An old object defines "foo" and ".foo", references ".bar" (possibly
3344 "bar" too).
3345 A new object defines "foo" and references "bar".
3346
3347 A new object thus has no problem with its undefined symbols being
3348 satisfied by definitions in an old object. On the other hand, the
3349 old object won't have ".bar" satisfied by a new object.
3350
3351 Keep a list of newly added dot-symbols. */
3352
3353 if (string[0] == '.')
3354 {
3355 struct ppc_link_hash_table *htab;
3356
3357 htab = (struct ppc_link_hash_table *) table;
3358 eh->u.next_dot_sym = htab->dot_syms;
3359 htab->dot_syms = eh;
3360 }
65f38f15
AM
3361 }
3362
3363 return entry;
3364}
3365
2cdcc330
AM
3366struct tocsave_entry
3367{
3b421ab3
AM
3368 asection *sec;
3369 bfd_vma offset;
3370};
3371
3372static hashval_t
3373tocsave_htab_hash (const void *p)
3374{
3375 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3376 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3377}
3378
3379static int
3380tocsave_htab_eq (const void *p1, const void *p2)
3381{
3382 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3383 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3384 return e1->sec == e2->sec && e1->offset == e2->offset;
3385}
3386
68faa637
AM
3387/* Destroy a ppc64 ELF linker hash table. */
3388
3389static void
d495ab0d 3390ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3391{
d495ab0d 3392 struct ppc_link_hash_table *htab;
68faa637 3393
d495ab0d 3394 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3395 if (htab->tocsave_htab)
3396 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3397 bfd_hash_table_free (&htab->branch_hash_table);
3398 bfd_hash_table_free (&htab->stub_hash_table);
3399 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3400}
3401
65f38f15
AM
3402/* Create a ppc64 ELF linker hash table. */
3403
3404static struct bfd_link_hash_table *
4ce794b7 3405ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3406{
3407 struct ppc_link_hash_table *htab;
3408 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3409
4ce794b7 3410 htab = bfd_zmalloc (amt);
65f38f15
AM
3411 if (htab == NULL)
3412 return NULL;
3413
66eb6687 3414 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3415 sizeof (struct ppc_link_hash_entry),
3416 PPC64_ELF_DATA))
65f38f15 3417 {
e2d34d7d 3418 free (htab);
65f38f15
AM
3419 return NULL;
3420 }
3421
721956f4 3422 /* Init the stub hash table too. */
66eb6687
AM
3423 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3424 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3425 {
d495ab0d 3426 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3427 return NULL;
3428 }
721956f4
AM
3429
3430 /* And the branch hash table. */
66eb6687
AM
3431 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3432 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3433 {
3434 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3435 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3436 return NULL;
3437 }
721956f4 3438
3b421ab3
AM
3439 htab->tocsave_htab = htab_try_create (1024,
3440 tocsave_htab_hash,
3441 tocsave_htab_eq,
3442 NULL);
3443 if (htab->tocsave_htab == NULL)
2915c55b 3444 {
d495ab0d 3445 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3446 return NULL;
3447 }
d495ab0d 3448 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3449
3254fd24
AM
3450 /* Initializing two fields of the union is just cosmetic. We really
3451 only care about glist, but when compiled on a 32-bit host the
3452 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3453 debugger inspection of these fields look nicer. */
a6aa5195
AM
3454 htab->elf.init_got_refcount.refcount = 0;
3455 htab->elf.init_got_refcount.glist = NULL;
3456 htab->elf.init_plt_refcount.refcount = 0;
3457 htab->elf.init_plt_refcount.glist = NULL;
3458 htab->elf.init_got_offset.offset = 0;
3459 htab->elf.init_got_offset.glist = NULL;
3460 htab->elf.init_plt_offset.offset = 0;
3461 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3462
65f38f15
AM
3463 return &htab->elf.root;
3464}
3465
bfeb4a28
AM
3466/* Create sections for linker generated code. */
3467
3468static bfd_boolean
3469create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3470{
3471 struct ppc_link_hash_table *htab;
3472 flagword flags;
3473
3474 htab = ppc_hash_table (info);
3475
bfeb4a28
AM
3476 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3477 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3478 if (htab->params->save_restore_funcs)
3479 {
3480 /* Create .sfpr for code to save and restore fp regs. */
3481 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3482 flags);
3483 if (htab->sfpr == NULL
2cdcc330 3484 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b32547cd
AM
3485 return FALSE;
3486 }
3487
3488 if (bfd_link_relocatable (info))
3489 return TRUE;
bfeb4a28
AM
3490
3491 /* Create .glink for lazy dynamic linking support. */
3492 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3493 flags);
3494 if (htab->glink == NULL
2cdcc330 3495 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
bfeb4a28
AM
3496 return FALSE;
3497
9e390558
AM
3498 /* The part of .glink used by global entry stubs, separate so that
3499 it can be aligned appropriately without affecting htab->glink. */
3500 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3501 flags);
3502 if (htab->global_entry == NULL
2cdcc330 3503 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
9e390558
AM
3504 return FALSE;
3505
bfeb4a28
AM
3506 if (!info->no_ld_generated_unwind_info)
3507 {
3508 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3509 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3510 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3511 ".eh_frame",
3512 flags);
3513 if (htab->glink_eh_frame == NULL
3514 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3515 return FALSE;
3516 }
3517
3518 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3519 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3520 if (htab->elf.iplt == NULL
2cdcc330 3521 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
3522 return FALSE;
3523
3524 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3525 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3526 htab->elf.irelplt
3527 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3528 if (htab->elf.irelplt == NULL
2cdcc330 3529 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
3530 return FALSE;
3531
3532 /* Create branch lookup table for plt_branch stubs. */
3533 flags = (SEC_ALLOC | SEC_LOAD
3534 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3535 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3536 flags);
3537 if (htab->brlt == NULL
2cdcc330 3538 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
bfeb4a28
AM
3539 return FALSE;
3540
2d7ad24e
AM
3541 /* Local plt entries, put in .branch_lt but a separate section for
3542 convenience. */
3543 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3544 flags);
3545 if (htab->pltlocal == NULL
2cdcc330 3546 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
2d7ad24e
AM
3547 return FALSE;
3548
0e1862bb 3549 if (!bfd_link_pic (info))
bfeb4a28
AM
3550 return TRUE;
3551
3552 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3553 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3554 htab->relbrlt
3555 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3556 if (htab->relbrlt == NULL
2cdcc330 3557 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
bfeb4a28
AM
3558 return FALSE;
3559
2d7ad24e
AM
3560 htab->relpltlocal
3561 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3562 if (htab->relpltlocal == NULL
2cdcc330 3563 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
2d7ad24e
AM
3564 return FALSE;
3565
bfeb4a28
AM
3566 return TRUE;
3567}
3568
e717da7e
AM
3569/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3570
bfeb4a28 3571bfd_boolean
e7d1c40c
AM
3572ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3573 struct ppc64_elf_params *params)
e717da7e
AM
3574{
3575 struct ppc_link_hash_table *htab;
3576
e7d1c40c 3577 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3578
3579/* Always hook our dynamic sections into the first bfd, which is the
3580 linker created stub bfd. This ensures that the GOT header is at
3581 the start of the output TOC section. */
3582 htab = ppc_hash_table (info);
e7d1c40c
AM
3583 htab->elf.dynobj = params->stub_bfd;
3584 htab->params = params;
bfeb4a28 3585
bfeb4a28 3586 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3587}
3588
721956f4
AM
3589/* Build a name for an entry in the stub hash table. */
3590
3591static char *
4ce794b7
AM
3592ppc_stub_name (const asection *input_section,
3593 const asection *sym_sec,
3594 const struct ppc_link_hash_entry *h,
3595 const Elf_Internal_Rela *rel)
721956f4
AM
3596{
3597 char *stub_name;
bcaa2f82 3598 ssize_t len;
721956f4
AM
3599
3600 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3601 offsets from a sym as a branch target? In fact, we could
3602 probably assume the addend is always zero. */
3603 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3604
3605 if (h)
3606 {
3607 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3608 stub_name = bfd_malloc (len);
46de2a7c
AM
3609 if (stub_name == NULL)
3610 return stub_name;
3611
bcaa2f82
AM
3612 len = sprintf (stub_name, "%08x.%s+%x",
3613 input_section->id & 0xffffffff,
3614 h->elf.root.root.string,
3615 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3616 }
3617 else
3618 {
ad8e1ba5 3619 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3620 stub_name = bfd_malloc (len);
46de2a7c
AM
3621 if (stub_name == NULL)
3622 return stub_name;
3623
bcaa2f82
AM
3624 len = sprintf (stub_name, "%08x.%x:%x+%x",
3625 input_section->id & 0xffffffff,
3626 sym_sec->id & 0xffffffff,
3627 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3628 (int) rel->r_addend & 0xffffffff);
721956f4 3629 }
bcaa2f82 3630 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3631 stub_name[len - 2] = 0;
721956f4
AM
3632 return stub_name;
3633}
3634
3635/* Look up an entry in the stub hash. Stub entries are cached because
3636 creating the stub name takes a bit of time. */
3637
3638static struct ppc_stub_hash_entry *
4ce794b7
AM
3639ppc_get_stub_entry (const asection *input_section,
3640 const asection *sym_sec,
039b3fef 3641 struct ppc_link_hash_entry *h,
4ce794b7
AM
3642 const Elf_Internal_Rela *rel,
3643 struct ppc_link_hash_table *htab)
721956f4
AM
3644{
3645 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3646 struct map_stub *group;
721956f4
AM
3647
3648 /* If this input section is part of a group of sections sharing one
3649 stub section, then use the id of the first section in the group.
3650 Stub names need to include a section id, as there may well be
3651 more than one stub used to reach say, printf, and we need to
3652 distinguish between them. */
6f20ed8a 3653 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3654 if (group == NULL)
3655 return NULL;
721956f4 3656
b3fac117
AM
3657 if (h != NULL && h->u.stub_cache != NULL
3658 && h->u.stub_cache->h == h
6f20ed8a 3659 && h->u.stub_cache->group == group)
721956f4 3660 {
b3fac117 3661 stub_entry = h->u.stub_cache;
721956f4
AM
3662 }
3663 else
3664 {
3665 char *stub_name;
3666
6f20ed8a 3667 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3668 if (stub_name == NULL)
3669 return NULL;
3670
3671 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3672 stub_name, FALSE, FALSE);
721956f4 3673 if (h != NULL)
b3fac117 3674 h->u.stub_cache = stub_entry;
721956f4
AM
3675
3676 free (stub_name);
3677 }
3678
3679 return stub_entry;
3680}
3681
3682/* Add a new stub entry to the stub hash. Not all fields of the new
3683 stub entry are initialised. */
3684
3685static struct ppc_stub_hash_entry *
4ce794b7
AM
3686ppc_add_stub (const char *stub_name,
3687 asection *section,
25f53a85 3688 struct bfd_link_info *info)
721956f4 3689{
25f53a85 3690 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3691 struct map_stub *group;
721956f4
AM
3692 asection *link_sec;
3693 asection *stub_sec;
3694 struct ppc_stub_hash_entry *stub_entry;
3695
6f20ed8a
AM
3696 group = htab->sec_info[section->id].u.group;
3697 link_sec = group->link_sec;
3698 stub_sec = group->stub_sec;
721956f4
AM
3699 if (stub_sec == NULL)
3700 {
6f20ed8a
AM
3701 size_t namelen;
3702 bfd_size_type len;
3703 char *s_name;
721956f4 3704
6f20ed8a
AM
3705 namelen = strlen (link_sec->name);
3706 len = namelen + sizeof (STUB_SUFFIX);
3707 s_name = bfd_alloc (htab->params->stub_bfd, len);
3708 if (s_name == NULL)
3709 return NULL;
721956f4 3710
6f20ed8a
AM
3711 memcpy (s_name, link_sec->name, namelen);
3712 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3713 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3714 if (stub_sec == NULL)
3715 return NULL;
3716 group->stub_sec = stub_sec;
721956f4
AM
3717 }
3718
3719 /* Enter this entry into the linker stub hash table. */
3720 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3721 TRUE, FALSE);
721956f4
AM
3722 if (stub_entry == NULL)
3723 {
695344c0 3724 /* xgettext:c-format */
cf97bcb0
AM
3725 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3726 section->owner, stub_name);
721956f4
AM
3727 return NULL;
3728 }
3729
6f20ed8a 3730 stub_entry->group = group;
721956f4 3731 stub_entry->stub_offset = 0;
721956f4
AM
3732 return stub_entry;
3733}
3734
e717da7e
AM
3735/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3736 not already done. */
65f38f15 3737
b34976b6 3738static bfd_boolean
e717da7e 3739create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3740{
e717da7e
AM
3741 asection *got, *relgot;
3742 flagword flags;
3743 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3744
0c8d6e5c 3745 if (!is_ppc64_elf (abfd))
0ffa91dd 3746 return FALSE;
4dfe6ac6
NC
3747 if (htab == NULL)
3748 return FALSE;
0ffa91dd 3749
33e44f2e
AM
3750 if (!htab->elf.sgot
3751 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3752 return FALSE;
e717da7e
AM
3753
3754 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3755 | SEC_LINKER_CREATED);
3756
c456f082 3757 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3758 if (!got
e717da7e
AM
3759 || !bfd_set_section_alignment (abfd, got, 3))
3760 return FALSE;
65f38f15 3761
c456f082
AM
3762 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3763 flags | SEC_READONLY);
e717da7e 3764 if (!relgot
2cdcc330 3765 || !bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 3766 return FALSE;
e717da7e
AM
3767
3768 ppc64_elf_tdata (abfd)->got = got;
3769 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 3770 return TRUE;
65f38f15 3771}
5bd4f169 3772
b31867b6
AM
3773/* Follow indirect and warning symbol links. */
3774
3775static inline struct bfd_link_hash_entry *
3776follow_link (struct bfd_link_hash_entry *h)
3777{
3778 while (h->type == bfd_link_hash_indirect
3779 || h->type == bfd_link_hash_warning)
3780 h = h->u.i.link;
3781 return h;
3782}
3783
3784static inline struct elf_link_hash_entry *
3785elf_follow_link (struct elf_link_hash_entry *h)
3786{
3787 return (struct elf_link_hash_entry *) follow_link (&h->root);
3788}
3789
3790static inline struct ppc_link_hash_entry *
3791ppc_follow_link (struct ppc_link_hash_entry *h)
3792{
3793 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3794}
3795
40d16e0b
AM
3796/* Merge PLT info on FROM with that on TO. */
3797
3798static void
3799move_plt_plist (struct ppc_link_hash_entry *from,
3800 struct ppc_link_hash_entry *to)
3801{
3802 if (from->elf.plt.plist != NULL)
3803 {
3804 if (to->elf.plt.plist != NULL)
3805 {
3806 struct plt_entry **entp;
3807 struct plt_entry *ent;
3808
3809 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3810 {
3811 struct plt_entry *dent;
3812
3813 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3814 if (dent->addend == ent->addend)
3815 {
3816 dent->plt.refcount += ent->plt.refcount;
3817 *entp = ent->next;
3818 break;
3819 }
3820 if (dent == NULL)
3821 entp = &ent->next;
3822 }
3823 *entp = to->elf.plt.plist;
3824 }
3825
3826 to->elf.plt.plist = from->elf.plt.plist;
3827 from->elf.plt.plist = NULL;
3828 }
3829}
3830
65f38f15
AM
3831/* Copy the extra info we tack onto an elf_link_hash_entry. */
3832
3833static void
fcfa13d2
AM
3834ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3835 struct elf_link_hash_entry *dir,
3836 struct elf_link_hash_entry *ind)
65f38f15
AM
3837{
3838 struct ppc_link_hash_entry *edir, *eind;
3839
3840 edir = (struct ppc_link_hash_entry *) dir;
3841 eind = (struct ppc_link_hash_entry *) ind;
3842
c79d6685
AM
3843 edir->is_func |= eind->is_func;
3844 edir->is_func_descriptor |= eind->is_func_descriptor;
3845 edir->tls_mask |= eind->tls_mask;
3846 if (eind->oh != NULL)
3847 edir->oh = ppc_follow_link (eind->oh);
3848
474436e6 3849 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
3850 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3851 edir->elf.ref_regular |= eind->elf.ref_regular;
3852 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 3853 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
3854 edir->elf.needs_plt |= eind->elf.needs_plt;
3855 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 3856
d311bc8b
AM
3857 /* If we were called to copy over info for a weak sym, don't copy
3858 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3859 in order to simplify readonly_dynrelocs and save a field in the
3860 symbol hash entry, but that means dyn_relocs can't be used in any
3861 tests about a specific symbol, or affect other symbol flags which
ab2477e1 3862 are then tested. */
d311bc8b 3863 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 3864 return;
d311bc8b 3865
411e1bfb 3866 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 3867 if (eind->dyn_relocs != NULL)
65f38f15 3868 {
bbd7ec4a
AM
3869 if (edir->dyn_relocs != NULL)
3870 {
6061a67d
AM
3871 struct elf_dyn_relocs **pp;
3872 struct elf_dyn_relocs *p;
bbd7ec4a 3873
fcfa13d2 3874 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
3875 list. Merge any entries against the same section. */
3876 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3877 {
6061a67d 3878 struct elf_dyn_relocs *q;
bbd7ec4a
AM
3879
3880 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3881 if (q->sec == p->sec)
3882 {
3883 q->pc_count += p->pc_count;
3884 q->count += p->count;
3885 *pp = p->next;
3886 break;
3887 }
3888 if (q == NULL)
3889 pp = &p->next;
3890 }
3891 *pp = edir->dyn_relocs;
3892 }
3893
65f38f15
AM
3894 edir->dyn_relocs = eind->dyn_relocs;
3895 eind->dyn_relocs = NULL;
3896 }
65f38f15 3897
81848ca0
AM
3898 /* Copy over got entries that we may have already seen to the
3899 symbol which just became indirect. */
411e1bfb
AM
3900 if (eind->elf.got.glist != NULL)
3901 {
3902 if (edir->elf.got.glist != NULL)
3903 {
3904 struct got_entry **entp;
3905 struct got_entry *ent;
3906
3907 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3908 {
3909 struct got_entry *dent;
3910
3911 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3912 if (dent->addend == ent->addend
e717da7e 3913 && dent->owner == ent->owner
411e1bfb
AM
3914 && dent->tls_type == ent->tls_type)
3915 {
3916 dent->got.refcount += ent->got.refcount;
3917 *entp = ent->next;
3918 break;
3919 }
3920 if (dent == NULL)
3921 entp = &ent->next;
3922 }
3923 *entp = edir->elf.got.glist;
3924 }
3925
3926 edir->elf.got.glist = eind->elf.got.glist;
3927 eind->elf.got.glist = NULL;
3928 }
3929
3930 /* And plt entries. */
40d16e0b 3931 move_plt_plist (eind, edir);
411e1bfb 3932
fcfa13d2 3933 if (eind->elf.dynindx != -1)
411e1bfb 3934 {
fcfa13d2
AM
3935 if (edir->elf.dynindx != -1)
3936 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3937 edir->elf.dynstr_index);
411e1bfb
AM
3938 edir->elf.dynindx = eind->elf.dynindx;
3939 edir->elf.dynstr_index = eind->elf.dynstr_index;
3940 eind->elf.dynindx = -1;
3941 eind->elf.dynstr_index = 0;
3942 }
411e1bfb
AM
3943}
3944
8387904d
AM
3945/* Find the function descriptor hash entry from the given function code
3946 hash entry FH. Link the entries via their OH fields. */
3947
3948static struct ppc_link_hash_entry *
b31867b6 3949lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
3950{
3951 struct ppc_link_hash_entry *fdh = fh->oh;
3952
3953 if (fdh == NULL)
3954 {
3955 const char *fd_name = fh->elf.root.root.string + 1;
3956
3957 fdh = (struct ppc_link_hash_entry *)
3958 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
3959 if (fdh == NULL)
3960 return fdh;
3961
3962 fdh->is_func_descriptor = 1;
3963 fdh->oh = fh;
3964 fh->is_func = 1;
3965 fh->oh = fdh;
8387904d
AM
3966 }
3967
8c5b4e52
AM
3968 fdh = ppc_follow_link (fdh);
3969 fdh->is_func_descriptor = 1;
3970 fdh->oh = fh;
3971 return fdh;
8387904d
AM
3972}
3973
8c5b4e52 3974/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
3975
3976static struct ppc_link_hash_entry *
3977make_fdh (struct bfd_link_info *info,
908b32fc 3978 struct ppc_link_hash_entry *fh)
bb700d78 3979{
8c5b4e52
AM
3980 bfd *abfd = fh->elf.root.u.undef.abfd;
3981 struct bfd_link_hash_entry *bh = NULL;
bb700d78 3982 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
3983 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3984 ? BSF_WEAK
3985 : BSF_GLOBAL);
3986
3987 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3988 fh->elf.root.root.string + 1,
3989 flags, bfd_und_section_ptr, 0,
3990 NULL, FALSE, FALSE, &bh))
bb700d78
AM
3991 return NULL;
3992
3993 fdh = (struct ppc_link_hash_entry *) bh;
3994 fdh->elf.non_elf = 0;
908b32fc
AM
3995 fdh->fake = 1;
3996 fdh->is_func_descriptor = 1;
3997 fdh->oh = fh;
3998 fh->is_func = 1;
3999 fh->oh = fdh;
bb700d78
AM
4000 return fdh;
4001}
4002
8387904d
AM
4003/* Fix function descriptor symbols defined in .opd sections to be
4004 function type. */
555cd476
AM
4005
4006static bfd_boolean
c16153ae 4007ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4008 struct bfd_link_info *info,
555cd476 4009 Elf_Internal_Sym *isym,
6911b7dc 4010 const char **name,
555cd476
AM
4011 flagword *flags ATTRIBUTE_UNUSED,
4012 asection **sec,
b53dfeb2 4013 bfd_vma *value)
555cd476 4014{
b53dfeb2 4015 if (*sec != NULL
f1885d1e 4016 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4017 {
4018 asection *code_sec;
4019
4020 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4021 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4022 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4023
4024 /* If the symbol is a function defined in .opd, and the function
4025 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4026 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4027 && (*sec)->reloc_count != 0
4028 && opd_entry_value (*sec, *value, &code_sec, NULL,
4029 FALSE) != (bfd_vma) -1
4030 && discarded_section (code_sec))
4031 {
4032 *sec = bfd_und_section_ptr;
4033 isym->st_shndx = SHN_UNDEF;
4034 }
4035 }
dbd1e97e
AM
4036 else if (*sec != NULL
4037 && strcmp ((*sec)->name, ".toc") == 0
4038 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4039 {
4040 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4041 if (htab != NULL)
4042 htab->params->object_in_toc = 1;
4043 }
433817dd 4044
6911b7dc
AM
4045 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4046 {
4047 if (abiversion (ibfd) == 0)
4048 set_abiversion (ibfd, 2);
4049 else if (abiversion (ibfd) == 1)
4050 {
cf97bcb0
AM
4051 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4052 " for ABI version 1"), *name);
6911b7dc
AM
4053 bfd_set_error (bfd_error_bad_value);
4054 return FALSE;
4055 }
4056 }
4057
555cd476
AM
4058 return TRUE;
4059}
4060
6911b7dc
AM
4061/* Merge non-visibility st_other attributes: local entry point. */
4062
4063static void
4064ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4065 const Elf_Internal_Sym *isym,
4066 bfd_boolean definition,
4067 bfd_boolean dynamic)
4068{
f378ab09 4069 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
4070 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4071 | ELF_ST_VISIBILITY (h->other));
4072}
4073
8c5b4e52
AM
4074/* Hook called on merging a symbol. We use this to clear "fake" since
4075 we now have a real symbol. */
4076
4077static bfd_boolean
4078ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4079 const Elf_Internal_Sym *isym,
8c5b4e52
AM
4080 asection **psec ATTRIBUTE_UNUSED,
4081 bfd_boolean newdef ATTRIBUTE_UNUSED,
4082 bfd_boolean olddef ATTRIBUTE_UNUSED,
4083 bfd *oldbfd ATTRIBUTE_UNUSED,
4084 const asection *oldsec ATTRIBUTE_UNUSED)
4085{
4086 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
4087 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4088 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
4089 return TRUE;
4090}
4091
8387904d 4092/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4093 inclusion of a new ABI object archive that defines "bar".
4094 NAME is a symbol defined in an archive. Return a symbol in the hash
4095 table that might be satisfied by the archive symbols. */
8387904d
AM
4096
4097static struct elf_link_hash_entry *
4098ppc64_elf_archive_symbol_lookup (bfd *abfd,
4099 struct bfd_link_info *info,
4100 const char *name)
4101{
4102 struct elf_link_hash_entry *h;
4103 char *dot_name;
4104 size_t len;
4105
4106 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4107 if (h != NULL
4108 /* Don't return this sym if it is a fake function descriptor
4109 created by add_symbol_adjust. */
8c5b4e52 4110 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
4111 return h;
4112
4113 if (name[0] == '.')
4114 return h;
4115
4116 len = strlen (name);
4117 dot_name = bfd_alloc (abfd, len + 2);
4118 if (dot_name == NULL)
e99955cd 4119 return (struct elf_link_hash_entry *) -1;
8387904d
AM
4120 dot_name[0] = '.';
4121 memcpy (dot_name + 1, name, len + 1);
4122 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4123 bfd_release (abfd, dot_name);
4124 return h;
4125}
4126
4127/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4128 new ABI object defines "bar". Well, at least, undefined dot symbols
4129 are made weak. This stops later archive searches from including an
4130 object if we already have a function descriptor definition. It also
35b0ce59
AM
4131 prevents the linker complaining about undefined symbols.
4132 We also check and correct mismatched symbol visibility here. The
4133 most restrictive visibility of the function descriptor and the
4134 function entry symbol is used. */
8387904d
AM
4135
4136static bfd_boolean
b3fac117 4137add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4138{
8387904d
AM
4139 struct ppc_link_hash_table *htab;
4140 struct ppc_link_hash_entry *fdh;
4141
b3fac117
AM
4142 if (eh->elf.root.type == bfd_link_hash_warning)
4143 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4144
8400d40d
AM
4145 if (eh->elf.root.type == bfd_link_hash_indirect)
4146 return TRUE;
4147
b3fac117
AM
4148 if (eh->elf.root.root.string[0] != '.')
4149 abort ();
8387904d 4150
b3fac117 4151 htab = ppc_hash_table (info);
4dfe6ac6
NC
4152 if (htab == NULL)
4153 return FALSE;
4154
b31867b6 4155 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4156 if (fdh == NULL
4157 && !bfd_link_relocatable (info)
4158 && (eh->elf.root.type == bfd_link_hash_undefined
4159 || eh->elf.root.type == bfd_link_hash_undefweak)
4160 && eh->elf.ref_regular)
4161 {
4162 /* Make an undefined function descriptor sym, in order to
4163 pull in an --as-needed shared lib. Archives are handled
4164 elsewhere. */
4165 fdh = make_fdh (info, eh);
4166 if (fdh == NULL)
4167 return FALSE;
bb700d78 4168 }
8c5b4e52
AM
4169
4170 if (fdh != NULL)
8387904d 4171 {
35b0ce59
AM
4172 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4173 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4174
4175 /* Make both descriptor and entry symbol have the most
4176 constraining visibility of either symbol. */
35b0ce59
AM
4177 if (entry_vis < descr_vis)
4178 fdh->elf.other += entry_vis - descr_vis;
4179 else if (entry_vis > descr_vis)
4180 eh->elf.other += descr_vis - entry_vis;
4181
8c5b4e52
AM
4182 /* Propagate reference flags from entry symbol to function
4183 descriptor symbol. */
bc4e12de 4184 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4185 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4186 fdh->elf.ref_regular |= eh->elf.ref_regular;
4187 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4188
4189 if (!fdh->elf.forced_local
4190 && fdh->elf.dynindx == -1
4191 && fdh->elf.versioned != versioned_hidden
4192 && (bfd_link_dll (info)
4193 || fdh->elf.def_dynamic
4194 || fdh->elf.ref_dynamic)
4195 && (eh->elf.ref_regular
4196 || eh->elf.def_regular))
4197 {
2cdcc330 4198 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
8c5b4e52 4199 return FALSE;
35b0ce59 4200 }
8387904d 4201 }
99877b66 4202
8387904d
AM
4203 return TRUE;
4204}
4205
f6c7c3e8
AM
4206/* Set up opd section info and abiversion for IBFD, and process list
4207 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4208
8387904d 4209static bfd_boolean
f6c7c3e8 4210ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4211{
99877b66 4212 struct ppc_link_hash_table *htab;
b3fac117 4213 struct ppc_link_hash_entry **p, *eh;
459609d6 4214 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4215
459609d6 4216 if (opd != NULL && opd->size != 0)
b3fac117 4217 {
b9399fcf
AM
4218 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4219 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4220
459609d6
AM
4221 if (abiversion (ibfd) == 0)
4222 set_abiversion (ibfd, 1);
8a2058b5 4223 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4224 {
695344c0 4225 /* xgettext:c-format */
cf97bcb0
AM
4226 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4227 ibfd, abiversion (ibfd));
459609d6
AM
4228 bfd_set_error (bfd_error_bad_value);
4229 return FALSE;
f6c7c3e8 4230 }
b9399fcf 4231 }
f6c7c3e8 4232
b9399fcf
AM
4233 if (is_ppc64_elf (info->output_bfd))
4234 {
4235 /* For input files without an explicit abiversion in e_flags
4236 we should have flagged any with symbol st_other bits set
4237 as ELFv1 and above flagged those with .opd as ELFv2.
4238 Set the output abiversion if not yet set, and for any input
4239 still ambiguous, take its abiversion from the output.
4240 Differences in ABI are reported later. */
4241 if (abiversion (info->output_bfd) == 0)
4242 set_abiversion (info->output_bfd, abiversion (ibfd));
4243 else if (abiversion (ibfd) == 0)
4244 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4245 }
4246
459609d6
AM
4247 htab = ppc_hash_table (info);
4248 if (htab == NULL)
b9399fcf 4249 return TRUE;
459609d6 4250
b9399fcf
AM
4251 if (opd != NULL && opd->size != 0
4252 && (ibfd->flags & DYNAMIC) == 0
4253 && (opd->flags & SEC_RELOC) != 0
4254 && opd->reloc_count != 0
4255 && !bfd_is_abs_section (opd->output_section)
4256 && info->gc_sections)
4257 {
4258 /* Garbage collection needs some extra help with .opd sections.
4259 We don't want to necessarily keep everything referenced by
4260 relocs in .opd, as that would keep all functions. Instead,
4261 if we reference an .opd symbol (a function descriptor), we
4262 want to keep the function code symbol's section. This is
4263 easy for global symbols, but for local syms we need to keep
4264 information about the associated function section. */
4265 bfd_size_type amt;
4266 asection **opd_sym_map;
4267 Elf_Internal_Shdr *symtab_hdr;
4268 Elf_Internal_Rela *relocs, *rel_end, *rel;
4269
4270 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4271 opd_sym_map = bfd_zalloc (ibfd, amt);
4272 if (opd_sym_map == NULL)
4273 return FALSE;
4274 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4275 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4276 info->keep_memory);
4277 if (relocs == NULL)
4278 return FALSE;
4279 symtab_hdr = &elf_symtab_hdr (ibfd);
4280 rel_end = relocs + opd->reloc_count - 1;
4281 for (rel = relocs; rel < rel_end; rel++)
4282 {
4283 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4284 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4285
4286 if (r_type == R_PPC64_ADDR64
4287 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4288 && r_symndx < symtab_hdr->sh_info)
4289 {
4290 Elf_Internal_Sym *isym;
4291 asection *s;
4292
4293 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4294 if (isym == NULL)
4295 {
4296 if (elf_section_data (opd)->relocs != relocs)
4297 free (relocs);
4298 return FALSE;
4299 }
4300
4301 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4302 if (s != NULL && s != opd)
4303 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4304 }
4305 }
4306 if (elf_section_data (opd)->relocs != relocs)
4307 free (relocs);
4308 }
459609d6
AM
4309
4310 p = &htab->dot_syms;
4311 while ((eh = *p) != NULL)
4312 {
4313 *p = NULL;
4314 if (&eh->elf == htab->elf.hgot)
4315 ;
4316 else if (htab->elf.hgot == NULL
4317 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4318 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4319 else if (abiversion (ibfd) <= 1)
4320 {
4321 htab->need_func_desc_adj = 1;
4322 if (!add_symbol_adjust (eh, info))
4323 return FALSE;
4324 }
459609d6
AM
4325 p = &eh->u.next_dot_sym;
4326 }
b3fac117 4327 return TRUE;
8387904d
AM
4328}
4329
97fed1c9
JJ
4330/* Undo hash table changes when an --as-needed input file is determined
4331 not to be needed. */
4332
4333static bfd_boolean
e5034e59
AM
4334ppc64_elf_notice_as_needed (bfd *ibfd,
4335 struct bfd_link_info *info,
4336 enum notice_asneeded_action act)
97fed1c9 4337{
e5034e59
AM
4338 if (act == notice_not_needed)
4339 {
4340 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4341
e5034e59
AM
4342 if (htab == NULL)
4343 return FALSE;
4dfe6ac6 4344
e5034e59
AM
4345 htab->dot_syms = NULL;
4346 }
4347 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4348}
4349
aa374f67
AM
4350/* If --just-symbols against a final linked binary, then assume we need
4351 toc adjusting stubs when calling functions defined there. */
4352
4353static void
4354ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4355{
4356 if ((sec->flags & SEC_CODE) != 0
4357 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4358 && is_ppc64_elf (sec->owner))
4359 {
2c3f079f
AM
4360 if (abiversion (sec->owner) >= 2
4361 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4362 sec->has_toc_reloc = 1;
4363 }
4364 _bfd_elf_link_just_syms (sec, info);
4365}
4366
e054468f 4367static struct plt_entry **
4ce794b7
AM
4368update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4369 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4370{
4371 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4372 struct plt_entry **local_plt;
f961d9dd 4373 unsigned char *local_got_tls_masks;
411e1bfb
AM
4374
4375 if (local_got_ents == NULL)
4376 {
4377 bfd_size_type size = symtab_hdr->sh_info;
4378
e054468f
AM
4379 size *= (sizeof (*local_got_ents)
4380 + sizeof (*local_plt)
4381 + sizeof (*local_got_tls_masks));
4ce794b7 4382 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4383 if (local_got_ents == NULL)
e054468f 4384 return NULL;
411e1bfb
AM
4385 elf_local_got_ents (abfd) = local_got_ents;
4386 }
4387
37da22e5 4388 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4389 {
4390 struct got_entry *ent;
4391
4392 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4393 if (ent->addend == r_addend
4394 && ent->owner == abfd
4395 && ent->tls_type == tls_type)
411e1bfb
AM
4396 break;
4397 if (ent == NULL)
4398 {
4399 bfd_size_type amt = sizeof (*ent);
4ce794b7 4400 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4401 if (ent == NULL)
4402 return FALSE;
4403 ent->next = local_got_ents[r_symndx];
4404 ent->addend = r_addend;
e717da7e 4405 ent->owner = abfd;
411e1bfb 4406 ent->tls_type = tls_type;
927be08e 4407 ent->is_indirect = FALSE;
411e1bfb
AM
4408 ent->got.refcount = 0;
4409 local_got_ents[r_symndx] = ent;
4410 }
4411 ent->got.refcount += 1;
4412 }
4413
e054468f 4414 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4415 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4416 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4417
4418 return local_plt + r_symndx;
65f38f15
AM
4419}
4420
411e1bfb 4421static bfd_boolean
e054468f 4422update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4423{
411e1bfb 4424 struct plt_entry *ent;
1e2f5b6e 4425
e054468f 4426 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4427 if (ent->addend == addend)
4428 break;
4429 if (ent == NULL)
1e2f5b6e 4430 {
411e1bfb 4431 bfd_size_type amt = sizeof (*ent);
4ce794b7 4432 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4433 if (ent == NULL)
4434 return FALSE;
e054468f 4435 ent->next = *plist;
411e1bfb
AM
4436 ent->addend = addend;
4437 ent->plt.refcount = 0;
e054468f 4438 *plist = ent;
1e2f5b6e 4439 }
411e1bfb 4440 ent->plt.refcount += 1;
b34976b6 4441 return TRUE;
1e2f5b6e
AM
4442}
4443
e054468f
AM
4444static bfd_boolean
4445is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4446{
4447 return (r_type == R_PPC64_REL24
05d0e962 4448 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
4449 || r_type == R_PPC64_REL14
4450 || r_type == R_PPC64_REL14_BRTAKEN
4451 || r_type == R_PPC64_REL14_BRNTAKEN
4452 || r_type == R_PPC64_ADDR24
4453 || r_type == R_PPC64_ADDR14
4454 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4455 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4456 || r_type == R_PPC64_PLTCALL
4457 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4458}
4459
4460/* Relocs on inline plt call sequence insns prior to the call. */
4461
4462static bfd_boolean
4463is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4464{
4465 return (r_type == R_PPC64_PLT16_HA
4466 || r_type == R_PPC64_PLT16_HI
4467 || r_type == R_PPC64_PLT16_LO
4468 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4469 || r_type == R_PPC64_PLT_PCREL34
4470 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4471 || r_type == R_PPC64_PLTSEQ
4472 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4473}
4474
5bd4f169 4475/* Look through the relocs for a section during the first phase, and
65f38f15 4476 calculate needed space in the global offset table, procedure
5d1634d7 4477 linkage table, and dynamic reloc sections. */
5bd4f169 4478
b34976b6 4479static bfd_boolean
4ce794b7
AM
4480ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4481 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4482{
65f38f15 4483 struct ppc_link_hash_table *htab;
5bd4f169 4484 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4485 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4486 const Elf_Internal_Rela *rel;
4487 const Elf_Internal_Rela *rel_end;
5bd4f169 4488 asection *sreloc;
3a71aa26 4489 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 4490 bfd_boolean is_opd;
5bd4f169 4491
0e1862bb 4492 if (bfd_link_relocatable (info))
b34976b6 4493 return TRUE;
5bd4f169 4494
680a3378
AM
4495 /* Don't do anything special with non-loaded, non-alloced sections.
4496 In particular, any relocs in such sections should not affect GOT
4497 and PLT reference counting (ie. we don't allow them to create GOT
4498 or PLT entries), there's no possibility or desire to optimize TLS
4499 relocs, and there's not much point in propagating relocs to shared
4500 libs that the dynamic linker won't relocate. */
4501 if ((sec->flags & SEC_ALLOC) == 0)
4502 return TRUE;
4503
0c8d6e5c 4504 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4505
65f38f15 4506 htab = ppc_hash_table (info);
4dfe6ac6
NC
4507 if (htab == NULL)
4508 return FALSE;
4509
3a71aa26
AM
4510 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4511 FALSE, FALSE, TRUE);
4512 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4513 FALSE, FALSE, TRUE);
0ffa91dd 4514 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4515 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4516 sreloc = NULL;
b9399fcf 4517 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4518 rel_end = relocs + sec->reloc_count;
4519 for (rel = relocs; rel < rel_end; rel++)
4520 {
4521 unsigned long r_symndx;
4522 struct elf_link_hash_entry *h;
04c9666a 4523 enum elf_ppc64_reloc_type r_type;
727fc41e 4524 int tls_type;
7c8fe5c4 4525 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4526 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
4527
4528 r_symndx = ELF64_R_SYM (rel->r_info);
4529 if (r_symndx < symtab_hdr->sh_info)
4530 h = NULL;
4531 else
973a3492
L
4532 {
4533 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4534 h = elf_follow_link (h);
1c865ab2
AM
4535
4536 if (h == htab->elf.hgot)
4537 sec->has_toc_reloc = 1;
973a3492 4538 }
5bd4f169 4539
727fc41e 4540 tls_type = 0;
e054468f 4541 ifunc = NULL;
5663e321
AM
4542 r_type = ELF64_R_TYPE (rel->r_info);
4543 switch (r_type)
4544 {
04bdff6a
AM
4545 case R_PPC64_D34:
4546 case R_PPC64_D34_LO:
4547 case R_PPC64_D34_HI30:
4548 case R_PPC64_D34_HA30:
4549 case R_PPC64_D28:
c213164a
AM
4550 case R_PPC64_TPREL34:
4551 case R_PPC64_DTPREL34:
04bdff6a 4552 case R_PPC64_PCREL34:
5663e321 4553 case R_PPC64_GOT_PCREL34:
c213164a
AM
4554 case R_PPC64_GOT_TLSGD34:
4555 case R_PPC64_GOT_TLSLD34:
4556 case R_PPC64_GOT_TPREL34:
4557 case R_PPC64_GOT_DTPREL34:
5663e321
AM
4558 case R_PPC64_PLT_PCREL34:
4559 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a
AM
4560 case R_PPC64_PCREL28:
4561 htab->powerxx_stubs = 1;
133a1f60
AM
4562 break;
4563 default:
5663e321
AM
4564 break;
4565 }
903b777d
AM
4566
4567 switch (r_type)
4568 {
4569 case R_PPC64_PLT16_HA:
4570 case R_PPC64_GOT_TLSLD16_HA:
4571 case R_PPC64_GOT_TLSGD16_HA:
4572 case R_PPC64_GOT_TPREL16_HA:
4573 case R_PPC64_GOT_DTPREL16_HA:
4574 case R_PPC64_GOT16_HA:
4575 case R_PPC64_TOC16_HA:
4576 case R_PPC64_PLT16_LO:
4577 case R_PPC64_PLT16_LO_DS:
4578 case R_PPC64_GOT_TLSLD16_LO:
4579 case R_PPC64_GOT_TLSGD16_LO:
4580 case R_PPC64_GOT_TPREL16_LO_DS:
4581 case R_PPC64_GOT_DTPREL16_LO_DS:
4582 case R_PPC64_GOT16_LO:
4583 case R_PPC64_GOT16_LO_DS:
4584 case R_PPC64_TOC16_LO:
4585 case R_PPC64_TOC16_LO_DS:
4586 case R_PPC64_GOT_PCREL34:
4587 ppc64_elf_tdata (abfd)->has_optrel = 1;
4588 ppc64_elf_section_data (sec)->has_optrel = 1;
4589 break;
4590 default:
4591 break;
4592 }
4593
25f23106
AM
4594 if (h != NULL)
4595 {
4596 if (h->type == STT_GNU_IFUNC)
4597 {
4598 h->needs_plt = 1;
4599 ifunc = &h->plt.plist;
4600 }
4601 }
4602 else
4603 {
4604 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4605 abfd, r_symndx);
4606 if (isym == NULL)
4607 return FALSE;
4608
4609 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4610 {
4611 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4612 rel->r_addend,
37da22e5 4613 NON_GOT | PLT_IFUNC);
25f23106
AM
4614 if (ifunc == NULL)
4615 return FALSE;
4616 }
4617 }
727fc41e 4618
a33d1f77 4619 switch (r_type)
5bd4f169 4620 {
727fc41e
AM
4621 case R_PPC64_TLSGD:
4622 case R_PPC64_TLSLD:
4623 /* These special tls relocs tie a call to __tls_get_addr with
4624 its parameter symbol. */
37da22e5
AM
4625 if (h != NULL)
4626 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4627 else
4628 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4629 rel->r_addend,
37da22e5
AM
4630 NON_GOT | TLS_TLS | TLS_MARK))
4631 return FALSE;
4632 sec->has_tls_reloc = 1;
727fc41e
AM
4633 break;
4634
411e1bfb
AM
4635 case R_PPC64_GOT_TLSLD16:
4636 case R_PPC64_GOT_TLSLD16_LO:
4637 case R_PPC64_GOT_TLSLD16_HI:
4638 case R_PPC64_GOT_TLSLD16_HA:
c213164a 4639 case R_PPC64_GOT_TLSLD34:
951fd09b 4640 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4641 goto dogottls;
4642
4643 case R_PPC64_GOT_TLSGD16:
4644 case R_PPC64_GOT_TLSGD16_LO:
4645 case R_PPC64_GOT_TLSGD16_HI:
4646 case R_PPC64_GOT_TLSGD16_HA:
c213164a 4647 case R_PPC64_GOT_TLSGD34:
951fd09b 4648 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4649 goto dogottls;
4650
4651 case R_PPC64_GOT_TPREL16_DS:
4652 case R_PPC64_GOT_TPREL16_LO_DS:
4653 case R_PPC64_GOT_TPREL16_HI:
4654 case R_PPC64_GOT_TPREL16_HA:
c213164a 4655 case R_PPC64_GOT_TPREL34:
7c8bbca5 4656 if (bfd_link_dll (info))
411e1bfb
AM
4657 info->flags |= DF_STATIC_TLS;
4658 tls_type = TLS_TLS | TLS_TPREL;
4659 goto dogottls;
4660
4661 case R_PPC64_GOT_DTPREL16_DS:
4662 case R_PPC64_GOT_DTPREL16_LO_DS:
4663 case R_PPC64_GOT_DTPREL16_HI:
4664 case R_PPC64_GOT_DTPREL16_HA:
c213164a 4665 case R_PPC64_GOT_DTPREL34:
411e1bfb
AM
4666 tls_type = TLS_TLS | TLS_DTPREL;
4667 dogottls:
4668 sec->has_tls_reloc = 1;
066f4018 4669 goto dogot;
411e1bfb 4670
903b777d
AM
4671 case R_PPC64_GOT16:
4672 case R_PPC64_GOT16_LO:
4673 case R_PPC64_GOT16_HI:
65f38f15 4674 case R_PPC64_GOT16_HA:
903b777d 4675 case R_PPC64_GOT16_DS:
066f4018 4676 case R_PPC64_GOT16_LO_DS:
4a421c53 4677 case R_PPC64_GOT_PCREL34:
066f4018 4678 dogot:
65f38f15 4679 /* This symbol requires a global offset table entry. */
4c52953f 4680 sec->has_toc_reloc = 1;
33c0ec9d
AM
4681 if (r_type == R_PPC64_GOT_TLSLD16
4682 || r_type == R_PPC64_GOT_TLSGD16
4683 || r_type == R_PPC64_GOT_TPREL16_DS
4684 || r_type == R_PPC64_GOT_DTPREL16_DS
4685 || r_type == R_PPC64_GOT16
4686 || r_type == R_PPC64_GOT16_DS)
4687 {
4688 htab->do_multi_toc = 1;
d77c8a4b 4689 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4690 }
4691
e717da7e
AM
4692 if (ppc64_elf_tdata (abfd)->got == NULL
4693 && !create_got_section (abfd, info))
b34976b6 4694 return FALSE;
5bd4f169
AM
4695
4696 if (h != NULL)
4697 {
411e1bfb
AM
4698 struct ppc_link_hash_entry *eh;
4699 struct got_entry *ent;
65f38f15 4700
411e1bfb
AM
4701 eh = (struct ppc_link_hash_entry *) h;
4702 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
133a1f60 4703 if (ent->addend == rel->r_addend
e717da7e 4704 && ent->owner == abfd
411e1bfb
AM
4705 && ent->tls_type == tls_type)
4706 break;
4707 if (ent == NULL)
5bd4f169 4708 {
411e1bfb 4709 bfd_size_type amt = sizeof (*ent);
4ce794b7 4710 ent = bfd_alloc (abfd, amt);
411e1bfb 4711 if (ent == NULL)
b34976b6 4712 return FALSE;
411e1bfb 4713 ent->next = eh->elf.got.glist;
133a1f60 4714 ent->addend = rel->r_addend;
e717da7e 4715 ent->owner = abfd;
411e1bfb 4716 ent->tls_type = tls_type;
927be08e 4717 ent->is_indirect = FALSE;
411e1bfb
AM
4718 ent->got.refcount = 0;
4719 eh->elf.got.glist = ent;
5bd4f169 4720 }
411e1bfb 4721 ent->got.refcount += 1;
e7b938ca 4722 eh->tls_mask |= tls_type;
5bd4f169 4723 }
411e1bfb
AM
4724 else
4725 /* This is a global offset table entry for a local symbol. */
4726 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4727 rel->r_addend, tls_type))
411e1bfb 4728 return FALSE;
a345bc8d
AM
4729
4730 /* We may also need a plt entry if the symbol turns out to be
4731 an ifunc. */
0e1862bb 4732 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d 4733 {
133a1f60 4734 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
a345bc8d
AM
4735 return FALSE;
4736 }
5bd4f169
AM
4737 break;
4738
5bd4f169 4739 case R_PPC64_PLT16_HA:
65f38f15
AM
4740 case R_PPC64_PLT16_HI:
4741 case R_PPC64_PLT16_LO:
08be3224 4742 case R_PPC64_PLT16_LO_DS:
5663e321
AM
4743 case R_PPC64_PLT_PCREL34:
4744 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
4745 case R_PPC64_PLT32:
4746 case R_PPC64_PLT64:
cbf95972
AM
4747 /* This symbol requires a procedure linkage table entry. */
4748 plt_list = ifunc;
4749 if (h != NULL)
e054468f 4750 {
e054468f
AM
4751 h->needs_plt = 1;
4752 if (h->root.root.string[0] == '.'
4753 && h->root.root.string[1] != '\0')
4754 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2d7ad24e 4755 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4756 plt_list = &h->plt.plist;
4757 }
4758 if (plt_list == NULL)
2d7ad24e 4759 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4760 rel->r_addend,
2d7ad24e 4761 NON_GOT | PLT_KEEP);
133a1f60 4762 if (!update_plt_info (abfd, plt_list, rel->r_addend))
cbf95972 4763 return FALSE;
5bd4f169
AM
4764 break;
4765
4766 /* The following relocations don't need to propagate the
4767 relocation if linking a shared object since they are
4768 section relative. */
4769 case R_PPC64_SECTOFF:
4770 case R_PPC64_SECTOFF_LO:
4771 case R_PPC64_SECTOFF_HI:
4772 case R_PPC64_SECTOFF_HA:
4773 case R_PPC64_SECTOFF_DS:
4774 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4775 case R_PPC64_DTPREL16:
4776 case R_PPC64_DTPREL16_LO:
4777 case R_PPC64_DTPREL16_HI:
4778 case R_PPC64_DTPREL16_HA:
4779 case R_PPC64_DTPREL16_DS:
4780 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
4781 case R_PPC64_DTPREL16_HIGH:
4782 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
4783 case R_PPC64_DTPREL16_HIGHER:
4784 case R_PPC64_DTPREL16_HIGHERA:
4785 case R_PPC64_DTPREL16_HIGHEST:
4786 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4787 break;
4788
ad8e1ba5 4789 /* Nor do these. */
25f23106
AM
4790 case R_PPC64_REL16:
4791 case R_PPC64_REL16_LO:
4792 case R_PPC64_REL16_HI:
4793 case R_PPC64_REL16_HA:
4a969973
AM
4794 case R_PPC64_REL16_HIGH:
4795 case R_PPC64_REL16_HIGHA:
4796 case R_PPC64_REL16_HIGHER:
4797 case R_PPC64_REL16_HIGHERA:
4798 case R_PPC64_REL16_HIGHEST:
4799 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
4800 case R_PPC64_REL16_HIGHER34:
4801 case R_PPC64_REL16_HIGHERA34:
4802 case R_PPC64_REL16_HIGHEST34:
4803 case R_PPC64_REL16_HIGHESTA34:
a680de9a 4804 case R_PPC64_REL16DX_HA:
25f23106
AM
4805 break;
4806
45965137
AM
4807 /* Not supported as a dynamic relocation. */
4808 case R_PPC64_ADDR64_LOCAL:
0e1862bb 4809 if (bfd_link_pic (info))
45965137
AM
4810 {
4811 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4812 ppc_howto_init ();
695344c0 4813 /* xgettext:c-format */
174d0a74 4814 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 4815 "in shared libraries and PIEs\n"),
45965137
AM
4816 abfd, sec, rel->r_offset,
4817 ppc64_elf_howto_table[r_type]->name);
4818 bfd_set_error (bfd_error_bad_value);
4819 return FALSE;
4820 }
4821 break;
4822
ad8e1ba5 4823 case R_PPC64_TOC16:
33c0ec9d
AM
4824 case R_PPC64_TOC16_DS:
4825 htab->do_multi_toc = 1;
d77c8a4b 4826 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 4827 /* Fall through. */
ad8e1ba5
AM
4828 case R_PPC64_TOC16_LO:
4829 case R_PPC64_TOC16_HI:
4830 case R_PPC64_TOC16_HA:
ad8e1ba5 4831 case R_PPC64_TOC16_LO_DS:
4c52953f 4832 sec->has_toc_reloc = 1;
ad8e1ba5
AM
4833 break;
4834
006589cf
AM
4835 /* Marker reloc. */
4836 case R_PPC64_ENTRY:
4837 break;
4838
5bd4f169
AM
4839 /* This relocation describes the C++ object vtable hierarchy.
4840 Reconstruct it for later use during GC. */
4841 case R_PPC64_GNU_VTINHERIT:
c152c796 4842 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4843 return FALSE;
5bd4f169
AM
4844 break;
4845
4846 /* This relocation describes which C++ vtable entries are actually
4847 used. Record for later use during GC. */
4848 case R_PPC64_GNU_VTENTRY:
a0ea3a14 4849 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4850 return FALSE;
5bd4f169
AM
4851 break;
4852
721956f4
AM
4853 case R_PPC64_REL14:
4854 case R_PPC64_REL14_BRTAKEN:
4855 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4856 {
4857 asection *dest = NULL;
4858
4859 /* Heuristic: If jumping outside our section, chances are
4860 we are going to need a stub. */
4861 if (h != NULL)
4862 {
4863 /* If the sym is weak it may be overridden later, so
4864 don't assume we know where a weak sym lives. */
4865 if (h->root.type == bfd_link_hash_defined)
4866 dest = h->root.u.def.section;
4867 }
4868 else
87d72d41
AM
4869 {
4870 Elf_Internal_Sym *isym;
4871
4872 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4873 abfd, r_symndx);
4874 if (isym == NULL)
4875 return FALSE;
4876
4877 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4878 }
4879
220c76dd 4880 if (dest != sec)
7c8fe5c4 4881 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4882 }
3e04d765
AM
4883 goto rel24;
4884
4885 case R_PPC64_PLTCALL:
5663e321 4886 case R_PPC64_PLTCALL_NOTOC:
3e04d765 4887 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
4888 /* Fall through. */
4889
5d1634d7 4890 case R_PPC64_REL24:
05d0e962 4891 case R_PPC64_REL24_NOTOC:
3e04d765 4892 rel24:
cbf95972
AM
4893 plt_list = ifunc;
4894 if (h != NULL)
5d1634d7 4895 {
e054468f
AM
4896 h->needs_plt = 1;
4897 if (h->root.root.string[0] == '.'
4898 && h->root.root.string[1] != '\0')
4899 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 4900
3a71aa26 4901 if (h == tga || h == dottga)
cbf95972
AM
4902 {
4903 sec->has_tls_reloc = 1;
4904 if (rel != relocs
4905 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4906 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4907 /* We have a new-style __tls_get_addr call with
4908 a marker reloc. */
4909 ;
4910 else
4911 /* Mark this section as having an old-style call. */
4912 sec->has_tls_get_addr_call = 1;
4913 }
4914 plt_list = &h->plt.plist;
411e1bfb 4915 }
cbf95972
AM
4916
4917 /* We may need a .plt entry if the function this reloc
4918 refers to is in a shared lib. */
4919 if (plt_list
133a1f60 4920 && !update_plt_info (abfd, plt_list, rel->r_addend))
cbf95972 4921 return FALSE;
411e1bfb
AM
4922 break;
4923
cbf95972
AM
4924 case R_PPC64_ADDR14:
4925 case R_PPC64_ADDR14_BRNTAKEN:
4926 case R_PPC64_ADDR14_BRTAKEN:
4927 case R_PPC64_ADDR24:
4928 goto dodyn;
4929
411e1bfb
AM
4930 case R_PPC64_TPREL64:
4931 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 4932 if (bfd_link_dll (info))
411e1bfb
AM
4933 info->flags |= DF_STATIC_TLS;
4934 goto dotlstoc;
4935
4936 case R_PPC64_DTPMOD64:
4937 if (rel + 1 < rel_end
4938 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4939 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 4940 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 4941 else
951fd09b 4942 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
4943 goto dotlstoc;
4944
4945 case R_PPC64_DTPREL64:
4946 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4947 if (rel != relocs
4948 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4949 && rel[-1].r_offset == rel->r_offset - 8)
4950 /* This is the second reloc of a dtpmod, dtprel pair.
4951 Don't mark with TLS_DTPREL. */
4952 goto dodyn;
4953
4954 dotlstoc:
4955 sec->has_tls_reloc = 1;
4956 if (h != NULL)
4957 {
4958 struct ppc_link_hash_entry *eh;
4959 eh = (struct ppc_link_hash_entry *) h;
46e9995a 4960 eh->tls_mask |= tls_type & 0xff;
411e1bfb
AM
4961 }
4962 else
4963 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4964 rel->r_addend, tls_type))
411e1bfb
AM
4965 return FALSE;
4966
7c8fe5c4
AM
4967 ppc64_sec = ppc64_elf_section_data (sec);
4968 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 4969 {
3a71aa26
AM
4970 bfd_size_type amt;
4971
e7b938ca 4972 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
4973 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4974 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4975 if (ppc64_sec->u.toc.symndx == NULL)
4976 return FALSE;
4977 amt = sec->size * sizeof (bfd_vma) / 8;
4978 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4979 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 4980 return FALSE;
7c8fe5c4
AM
4981 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4982 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
4983 }
4984 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26 4985 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
133a1f60 4986 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
4987
4988 /* Mark the second slot of a GD or LD entry.
4989 -1 to indicate GD and -2 to indicate LD. */
4990 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 4991 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 4992 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 4993 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
4994 goto dodyn;
4995
4996 case R_PPC64_TPREL16:
4997 case R_PPC64_TPREL16_LO:
4998 case R_PPC64_TPREL16_HI:
4999 case R_PPC64_TPREL16_HA:
5000 case R_PPC64_TPREL16_DS:
5001 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5002 case R_PPC64_TPREL16_HIGH:
5003 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5004 case R_PPC64_TPREL16_HIGHER:
5005 case R_PPC64_TPREL16_HIGHERA:
5006 case R_PPC64_TPREL16_HIGHEST:
5007 case R_PPC64_TPREL16_HIGHESTA:
c213164a 5008 case R_PPC64_TPREL34:
7c8bbca5
AM
5009 if (bfd_link_dll (info))
5010 info->flags |= DF_STATIC_TLS;
5011 goto dodyn;
5d1634d7 5012
e86ce104 5013 case R_PPC64_ADDR64:
b9399fcf 5014 if (is_opd
1e2f5b6e 5015 && rel + 1 < rel_end
4ce794b7 5016 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5017 {
8387904d 5018 if (h != NULL)
8c5b4e52 5019 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 5020 }
e86ce104
AM
5021 /* Fall through. */
5022
65f38f15
AM
5023 case R_PPC64_ADDR16:
5024 case R_PPC64_ADDR16_DS:
5025 case R_PPC64_ADDR16_HA:
5026 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5027 case R_PPC64_ADDR16_HIGH:
5028 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5029 case R_PPC64_ADDR16_HIGHER:
5030 case R_PPC64_ADDR16_HIGHERA:
5031 case R_PPC64_ADDR16_HIGHEST:
5032 case R_PPC64_ADDR16_HIGHESTA:
5033 case R_PPC64_ADDR16_LO:
5034 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
5035 case R_PPC64_D34:
5036 case R_PPC64_D34_LO:
5037 case R_PPC64_D34_HI30:
5038 case R_PPC64_D34_HA30:
5039 case R_PPC64_ADDR16_HIGHER34:
5040 case R_PPC64_ADDR16_HIGHERA34:
5041 case R_PPC64_ADDR16_HIGHEST34:
5042 case R_PPC64_ADDR16_HIGHESTA34:
5043 case R_PPC64_D28:
0e1862bb 5044 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5045 && rel->r_addend == 0)
5046 {
5047 /* We may need a .plt entry if this reloc refers to a
5048 function in a shared lib. */
5663e321 5049 if (!update_plt_info (abfd, &h->plt.plist, 0))
a345bc8d
AM
5050 return FALSE;
5051 h->pointer_equality_needed = 1;
5052 }
5053 /* Fall through. */
5054
5055 case R_PPC64_REL30:
5056 case R_PPC64_REL32:
5057 case R_PPC64_REL64:
65f38f15 5058 case R_PPC64_ADDR32:
65f38f15
AM
5059 case R_PPC64_UADDR16:
5060 case R_PPC64_UADDR32:
5061 case R_PPC64_UADDR64:
5bd4f169 5062 case R_PPC64_TOC:
0e1862bb 5063 if (h != NULL && !bfd_link_pic (info))
81848ca0 5064 /* We may need a copy reloc. */
f5385ebf 5065 h->non_got_ref = 1;
81848ca0 5066
41bd81ab 5067 /* Don't propagate .opd relocs. */
b9399fcf 5068 if (NO_OPD_RELOCS && is_opd)
e86ce104 5069 break;
e86ce104 5070
65f38f15
AM
5071 /* If we are creating a shared library, and this is a reloc
5072 against a global symbol, or a non PC relative reloc
5073 against a local symbol, then we need to copy the reloc
5074 into the shared library. However, if we are linking with
5075 -Bsymbolic, we do not need to copy a reloc against a
5076 global symbol which is defined in an object we are
5077 including in the link (i.e., DEF_REGULAR is set). At
5078 this point we have not seen all the input files, so it is
5079 possible that DEF_REGULAR is not set now but will be set
5080 later (it is never cleared). In case of a weak definition,
5081 DEF_REGULAR may be cleared later by a strong definition in
5082 a shared library. We account for that possibility below by
f4656909 5083 storing information in the dyn_relocs field of the hash
65f38f15
AM
5084 table entry. A similar situation occurs when creating
5085 shared libraries and symbol visibility changes render the
5086 symbol local.
5087
5088 If on the other hand, we are creating an executable, we
5089 may need to keep relocations for symbols satisfied by a
5090 dynamic library if we manage to avoid copy relocs for the
5091 symbol. */
411e1bfb 5092 dodyn:
0e1862bb 5093 if ((bfd_link_pic (info)
1d483afe 5094 && (must_be_dyn_reloc (info, r_type)
65f38f15 5095 || (h != NULL
198f1157 5096 && (!SYMBOLIC_BIND (info, h)
65f38f15 5097 || h->root.type == bfd_link_hash_defweak
f5385ebf 5098 || !h->def_regular))))
f4656909 5099 || (ELIMINATE_COPY_RELOCS
0e1862bb 5100 && !bfd_link_pic (info)
65f38f15
AM
5101 && h != NULL
5102 && (h->root.type == bfd_link_hash_defweak
25f23106 5103 || !h->def_regular))
0e1862bb 5104 || (!bfd_link_pic (info)
25f23106 5105 && ifunc != NULL))
5bd4f169 5106 {
65f38f15
AM
5107 /* We must copy these reloc types into the output file.
5108 Create a reloc section in dynobj and make room for
5109 this reloc. */
5bd4f169
AM
5110 if (sreloc == NULL)
5111 {
83bac4b0
NC
5112 sreloc = _bfd_elf_make_dynamic_reloc_section
5113 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5114
5bd4f169 5115 if (sreloc == NULL)
83bac4b0 5116 return FALSE;
5bd4f169
AM
5117 }
5118
65f38f15
AM
5119 /* If this is a global symbol, we count the number of
5120 relocations we need for this symbol. */
5121 if (h != NULL)
5122 {
19e08130
AM
5123 struct elf_dyn_relocs *p;
5124 struct elf_dyn_relocs **head;
5125
ec338859 5126 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5127 p = *head;
5128 if (p == NULL || p->sec != sec)
5129 {
5130 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5131 if (p == NULL)
5132 return FALSE;
5133 p->next = *head;
5134 *head = p;
5135 p->sec = sec;
5136 p->count = 0;
5137 p->pc_count = 0;
5138 }
5139 p->count += 1;
5140 if (!must_be_dyn_reloc (info, r_type))
5141 p->pc_count += 1;
65f38f15
AM
5142 }
5143 else
5144 {
ec338859
AM
5145 /* Track dynamic relocs needed for local syms too.
5146 We really need local syms available to do this
5147 easily. Oh well. */
19e08130
AM
5148 struct ppc_dyn_relocs *p;
5149 struct ppc_dyn_relocs **head;
5150 bfd_boolean is_ifunc;
ec338859 5151 asection *s;
6edfbbad 5152 void *vpp;
87d72d41 5153 Elf_Internal_Sym *isym;
6edfbbad 5154
87d72d41
AM
5155 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5156 abfd, r_symndx);
5157 if (isym == NULL)
b34976b6 5158 return FALSE;
ec338859 5159
87d72d41
AM
5160 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5161 if (s == NULL)
5162 s = sec;
5163
6edfbbad 5164 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5165 head = (struct ppc_dyn_relocs **) vpp;
5166 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5167 p = *head;
5168 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5169 p = p->next;
5170 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5171 {
5172 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5173 if (p == NULL)
5174 return FALSE;
5175 p->next = *head;
5176 *head = p;
5177 p->sec = sec;
5178 p->ifunc = is_ifunc;
5179 p->count = 0;
5180 }
5181 p->count += 1;
ec338859 5182 }
65f38f15 5183 }
5bd4f169 5184 break;
65f38f15
AM
5185
5186 default:
96e0dda4 5187 break;
5bd4f169
AM
5188 }
5189 }
5190
b34976b6 5191 return TRUE;
5bd4f169
AM
5192}
5193
ee67d69a
AM
5194/* Merge backend specific data from an object file to the output
5195 object file when linking. */
5196
5197static bfd_boolean
50e03d47 5198ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5199{
50e03d47 5200 bfd *obfd = info->output_bfd;
ee67d69a
AM
5201 unsigned long iflags, oflags;
5202
5203 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5204 return TRUE;
5205
5206 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5207 return TRUE;
5208
50e03d47 5209 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
5210 return FALSE;
5211
5212 iflags = elf_elfheader (ibfd)->e_flags;
5213 oflags = elf_elfheader (obfd)->e_flags;
5214
f6c7c3e8 5215 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5216 {
4eca0228 5217 _bfd_error_handler
695344c0 5218 /* xgettext:c-format */
871b3ab2 5219 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
5220 bfd_set_error (bfd_error_bad_value);
5221 return FALSE;
5222 }
f6c7c3e8 5223 else if (iflags != oflags && iflags != 0)
ee67d69a 5224 {
4eca0228 5225 _bfd_error_handler
695344c0 5226 /* xgettext:c-format */
871b3ab2 5227 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5228 ibfd, iflags, oflags);
5229 bfd_set_error (bfd_error_bad_value);
5230 return FALSE;
5231 }
5232
4a91d0ba
AM
5233 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5234 return FALSE;
005d79fd 5235
ee67d69a 5236 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5237 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5238}
5239
5240static bfd_boolean
5241ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5242{
5243 /* Print normal ELF private data. */
5244 _bfd_elf_print_private_bfd_data (abfd, ptr);
5245
5246 if (elf_elfheader (abfd)->e_flags != 0)
5247 {
5248 FILE *file = ptr;
5249
ee67d69a
AM
5250 fprintf (file, _("private flags = 0x%lx:"),
5251 elf_elfheader (abfd)->e_flags);
5252
5253 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5254 fprintf (file, _(" [abiv%ld]"),
5255 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5256 fputc ('\n', file);
5257 }
5258
5259 return TRUE;
5260}
5261
8387904d 5262/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5263 of the code entry point, and its section, which must be in the same
5264 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5265
5266static bfd_vma
5267opd_entry_value (asection *opd_sec,
5268 bfd_vma offset,
5269 asection **code_sec,
aef36ac1
AM
5270 bfd_vma *code_off,
5271 bfd_boolean in_code_sec)
8387904d
AM
5272{
5273 bfd *opd_bfd = opd_sec->owner;
8860955f 5274 Elf_Internal_Rela *relocs;
8387904d 5275 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5276 bfd_vma val;
8387904d 5277
9f296da3
AM
5278 /* No relocs implies we are linking a --just-symbols object, or looking
5279 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5280 if (opd_sec->reloc_count == 0)
5281 {
729eabd5 5282 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5283
729eabd5
AM
5284 if (contents == NULL)
5285 {
5286 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5287 return (bfd_vma) -1;
5288 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5289 }
ee1e4ede 5290
dbb3fbbb 5291 /* PR 17512: file: 64b9dfbb. */
451dfd38 5292 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5293 return (bfd_vma) -1;
5294
729eabd5 5295 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5296 if (code_sec != NULL)
5297 {
5298 asection *sec, *likely = NULL;
ee1e4ede 5299
aef36ac1 5300 if (in_code_sec)
4b85d634 5301 {
aef36ac1
AM
5302 sec = *code_sec;
5303 if (sec->vma <= val
5304 && val < sec->vma + sec->size)
5305 likely = sec;
5306 else
5307 val = -1;
5308 }
5309 else
5310 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5311 if (sec->vma <= val
5312 && (sec->flags & SEC_LOAD) != 0
5313 && (sec->flags & SEC_ALLOC) != 0)
5314 likely = sec;
5315 if (likely != NULL)
5316 {
5317 *code_sec = likely;
5318 if (code_off != NULL)
5319 *code_off = val - likely->vma;
4b85d634
AM
5320 }
5321 }
aef36ac1 5322 return val;
4b85d634
AM
5323 }
5324
0c8d6e5c 5325 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5326
729eabd5 5327 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5328 if (relocs == NULL)
5329 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
5330 /* PR 17512: file: df8e1fd6. */
5331 if (relocs == NULL)
5332 return (bfd_vma) -1;
645ea6a9 5333
8387904d 5334 /* Go find the opd reloc at the sym address. */
8860955f 5335 lo = relocs;
8387904d 5336 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5337 val = (bfd_vma) -1;
8387904d
AM
5338 while (lo < hi)
5339 {
5340 look = lo + (hi - lo) / 2;
5341 if (look->r_offset < offset)
5342 lo = look + 1;
5343 else if (look->r_offset > offset)
5344 hi = look;
5345 else
5346 {
0ffa91dd
NC
5347 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5348
8387904d
AM
5349 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5350 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5351 {
5352 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5353 asection *sec = NULL;
8387904d 5354
b53dfeb2
AM
5355 if (symndx >= symtab_hdr->sh_info
5356 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5357 {
5358 struct elf_link_hash_entry **sym_hashes;
5359 struct elf_link_hash_entry *rh;
5360
5361 sym_hashes = elf_sym_hashes (opd_bfd);
5362 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5363 if (rh != NULL)
5364 {
5365 rh = elf_follow_link (rh);
bb854a36
AM
5366 if (rh->root.type != bfd_link_hash_defined
5367 && rh->root.type != bfd_link_hash_defweak)
5368 break;
5369 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5370 {
bb854a36
AM
5371 val = rh->root.u.def.value;
5372 sec = rh->root.u.def.section;
b53dfeb2
AM
5373 }
5374 }
5375 }
5376
5377 if (sec == NULL)
5378 {
5379 Elf_Internal_Sym *sym;
5380
5381 if (symndx < symtab_hdr->sh_info)
5382 {
5383 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5384 if (sym == NULL)
5385 {
5386 size_t symcnt = symtab_hdr->sh_info;
5387 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5388 symcnt, 0,
5389 NULL, NULL, NULL);
5390 if (sym == NULL)
5391 break;
5392 symtab_hdr->contents = (bfd_byte *) sym;
5393 }
5394 sym += symndx;
128205bb
AM
5395 }
5396 else
5397 {
b53dfeb2
AM
5398 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5399 1, symndx,
5400 NULL, NULL, NULL);
128205bb
AM
5401 if (sym == NULL)
5402 break;
128205bb 5403 }
b53dfeb2
AM
5404 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5405 if (sec == NULL)
5406 break;
5407 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5408 val = sym->st_value;
8387904d 5409 }
b53dfeb2 5410
8387904d
AM
5411 val += look->r_addend;
5412 if (code_off != NULL)
5413 *code_off = val;
5414 if (code_sec != NULL)
aef36ac1
AM
5415 {
5416 if (in_code_sec && *code_sec != sec)
5417 return -1;
5418 else
5419 *code_sec = sec;
5420 }
b53dfeb2 5421 if (sec->output_section != NULL)
8387904d 5422 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5423 }
5424 break;
5425 }
5426 }
645ea6a9 5427
645ea6a9 5428 return val;
8387904d
AM
5429}
5430
aef36ac1
AM
5431/* If the ELF symbol SYM might be a function in SEC, return the
5432 function size and set *CODE_OFF to the function's entry point,
5433 otherwise return zero. */
9f296da3 5434
aef36ac1
AM
5435static bfd_size_type
5436ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5437 bfd_vma *code_off)
9f296da3 5438{
aef36ac1
AM
5439 bfd_size_type size;
5440
5441 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5442 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5443 return 0;
5444
5445 size = 0;
5446 if (!(sym->flags & BSF_SYNTHETIC))
5447 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5448
5449 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5450 {
b07bca4e
AM
5451 struct _opd_sec_data *opd = get_opd_info (sym->section);
5452 bfd_vma symval = sym->value;
5453
5454 if (opd != NULL
5455 && opd->adjust != NULL
5456 && elf_section_data (sym->section)->relocs != NULL)
5457 {
5458 /* opd_entry_value will use cached relocs that have been
5459 adjusted, but with raw symbols. That means both local
5460 and global symbols need adjusting. */
5461 long adjust = opd->adjust[OPD_NDX (symval)];
5462 if (adjust == -1)
5463 return 0;
5464 symval += adjust;
5465 }
5466
5467 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
5468 &sec, code_off, TRUE) == (bfd_vma) -1)
5469 return 0;
5470 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5471 symbol. This size has nothing to do with the code size of the
5472 function, which is what we're supposed to return, but the
5473 code size isn't available without looking up the dot-sym.
5474 However, doing that would be a waste of time particularly
5475 since elf_find_function will look at the dot-sym anyway.
5476 Now, elf_find_function will keep the largest size of any
5477 function sym found at the code address of interest, so return
5478 1 here to avoid it incorrectly caching a larger function size
5479 for a small function. This does mean we return the wrong
5480 size for a new-ABI function of size 24, but all that does is
5481 disable caching for such functions. */
5482 if (size == 24)
5483 size = 1;
9f296da3 5484 }
aef36ac1
AM
5485 else
5486 {
5487 if (sym->section != sec)
5488 return 0;
5489 *code_off = sym->value;
5490 }
5491 if (size == 0)
5492 size = 1;
5493 return size;
9f296da3
AM
5494}
5495
f378ab09
AM
5496/* Return true if symbol is a strong function defined in an ELFv2
5497 object with st_other localentry bits of zero, ie. its local entry
5498 point coincides with its global entry point. */
5499
5500static bfd_boolean
5501is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5502{
5503 return (h != NULL
5504 && h->type == STT_FUNC
5505 && h->root.type == bfd_link_hash_defined
5506 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 5507 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
5508 && is_ppc64_elf (h->root.u.def.section->owner)
5509 && abiversion (h->root.u.def.section->owner) >= 2);
5510}
5511
854b41e7
AM
5512/* Return true if symbol is defined in a regular object file. */
5513
5514static bfd_boolean
5515is_static_defined (struct elf_link_hash_entry *h)
5516{
5517 return ((h->root.type == bfd_link_hash_defined
5518 || h->root.type == bfd_link_hash_defweak)
5519 && h->root.u.def.section != NULL
5520 && h->root.u.def.section->output_section != NULL);
5521}
5522
b31867b6
AM
5523/* If FDH is a function descriptor symbol, return the associated code
5524 entry symbol if it is defined. Return NULL otherwise. */
5525
5526static struct ppc_link_hash_entry *
5527defined_code_entry (struct ppc_link_hash_entry *fdh)
5528{
5529 if (fdh->is_func_descriptor)
5530 {
5531 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5532 if (fh->elf.root.type == bfd_link_hash_defined
5533 || fh->elf.root.type == bfd_link_hash_defweak)
5534 return fh;
5535 }
5536 return NULL;
5537}
5538
5539/* If FH is a function code entry symbol, return the associated
5540 function descriptor symbol if it is defined. Return NULL otherwise. */
5541
5542static struct ppc_link_hash_entry *
5543defined_func_desc (struct ppc_link_hash_entry *fh)
5544{
5545 if (fh->oh != NULL
5546 && fh->oh->is_func_descriptor)
5547 {
5548 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5549 if (fdh->elf.root.type == bfd_link_hash_defined
5550 || fdh->elf.root.type == bfd_link_hash_defweak)
5551 return fdh;
5552 }
5553 return NULL;
5554}
5555
8c5b4e52
AM
5556static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5557
5558/* Garbage collect sections, after first dealing with dot-symbols. */
5559
5560static bfd_boolean
5561ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5562{
5563 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5564
5565 if (htab != NULL && htab->need_func_desc_adj)
5566 {
5567 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5568 htab->need_func_desc_adj = 0;
5569 }
5570 return bfd_elf_gc_sections (abfd, info);
5571}
5572
74f0fb50
AM
5573/* Mark all our entry sym sections, both opd and code section. */
5574
5575static void
5576ppc64_elf_gc_keep (struct bfd_link_info *info)
5577{
5578 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5579 struct bfd_sym_chain *sym;
5580
4dfe6ac6
NC
5581 if (htab == NULL)
5582 return;
5583
74f0fb50
AM
5584 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5585 {
b31867b6 5586 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5587 asection *sec;
5588
5589 eh = (struct ppc_link_hash_entry *)
b31867b6 5590 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5591 if (eh == NULL)
5592 continue;
5593 if (eh->elf.root.type != bfd_link_hash_defined
5594 && eh->elf.root.type != bfd_link_hash_defweak)
5595 continue;
5596
b31867b6
AM
5597 fh = defined_code_entry (eh);
5598 if (fh != NULL)
74f0fb50 5599 {
b31867b6 5600 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5601 sec->flags |= SEC_KEEP;
5602 }
5603 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5604 && opd_entry_value (eh->elf.root.u.def.section,
5605 eh->elf.root.u.def.value,
aef36ac1 5606 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
5607 sec->flags |= SEC_KEEP;
5608
5609 sec = eh->elf.root.u.def.section;
5610 sec->flags |= SEC_KEEP;
5611 }
5612}
5613
64d03ab5
AM
5614/* Mark sections containing dynamically referenced symbols. When
5615 building shared libraries, we must assume that any visible symbol is
5616 referenced. */
5617
5618static bfd_boolean
5619ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5620{
5621 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5622 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5623 struct ppc_link_hash_entry *fdh;
b407645f 5624 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5625
64d03ab5 5626 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5627 fdh = defined_func_desc (eh);
5628 if (fdh != NULL)
5629 eh = fdh;
64d03ab5
AM
5630
5631 if ((eh->elf.root.type == bfd_link_hash_defined
5632 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 5633 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5634 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5635 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5636 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5637 && (!bfd_link_executable (info)
e278ae05 5638 || info->gc_keep_exported
b407645f
AM
5639 || info->export_dynamic
5640 || (eh->elf.dynamic
5641 && d != NULL
2cdcc330
AM
5642 && (*d->match) (&d->head, NULL,
5643 eh->elf.root.root.string)))
e278ae05 5644 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5645 || !bfd_hide_sym_by_version (info->version_info,
5646 eh->elf.root.root.string)))))
64d03ab5
AM
5647 {
5648 asection *code_sec;
b31867b6 5649 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5650
5651 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5652
5653 /* Function descriptor syms cause the associated
5654 function code sym section to be marked. */
b31867b6
AM
5655 fh = defined_code_entry (eh);
5656 if (fh != NULL)
5657 {
5658 code_sec = fh->elf.root.u.def.section;
5659 code_sec->flags |= SEC_KEEP;
5660 }
64d03ab5
AM
5661 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5662 && opd_entry_value (eh->elf.root.u.def.section,
5663 eh->elf.root.u.def.value,
aef36ac1 5664 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
5665 code_sec->flags |= SEC_KEEP;
5666 }
5667
5668 return TRUE;
5669}
5670
5bd4f169
AM
5671/* Return the section that should be marked against GC for a given
5672 relocation. */
5673
5674static asection *
4ce794b7 5675ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5676 struct bfd_link_info *info,
4ce794b7
AM
5677 Elf_Internal_Rela *rel,
5678 struct elf_link_hash_entry *h,
5679 Elf_Internal_Sym *sym)
5bd4f169 5680{
ccfa59ea
AM
5681 asection *rsec;
5682
ccfa59ea
AM
5683 /* Syms return NULL if we're marking .opd, so we avoid marking all
5684 function sections, as all functions are referenced in .opd. */
5685 rsec = NULL;
5686 if (get_opd_info (sec) != NULL)
5687 return rsec;
1e2f5b6e 5688
5bd4f169
AM
5689 if (h != NULL)
5690 {
04c9666a 5691 enum elf_ppc64_reloc_type r_type;
b31867b6 5692 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5693
4ce794b7 5694 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5695 switch (r_type)
5bd4f169
AM
5696 {
5697 case R_PPC64_GNU_VTINHERIT:
5698 case R_PPC64_GNU_VTENTRY:
5699 break;
5700
5701 default:
5702 switch (h->root.type)
5703 {
5704 case bfd_link_hash_defined:
5705 case bfd_link_hash_defweak:
ccfa59ea 5706 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5707 fdh = defined_func_desc (eh);
5708 if (fdh != NULL)
8c5b4e52
AM
5709 {
5710 /* -mcall-aixdesc code references the dot-symbol on
5711 a call reloc. Mark the function descriptor too
5712 against garbage collection. */
5713 fdh->elf.mark = 1;
60d67dc8
AM
5714 if (fdh->elf.is_weakalias)
5715 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
5716 eh = fdh;
5717 }
1e2f5b6e
AM
5718
5719 /* Function descriptor syms cause the associated
5720 function code sym section to be marked. */
b31867b6
AM
5721 fh = defined_code_entry (eh);
5722 if (fh != NULL)
ccfa59ea
AM
5723 {
5724 /* They also mark their opd section. */
74f0fb50 5725 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5726
b31867b6 5727 rsec = fh->elf.root.u.def.section;
ccfa59ea 5728 }
8387904d
AM
5729 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5730 && opd_entry_value (eh->elf.root.u.def.section,
5731 eh->elf.root.u.def.value,
aef36ac1 5732 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 5733 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5734 else
1e2f5b6e
AM
5735 rsec = h->root.u.def.section;
5736 break;
5bd4f169
AM
5737
5738 case bfd_link_hash_common:
1e2f5b6e
AM
5739 rsec = h->root.u.c.p->section;
5740 break;
5bd4f169
AM
5741
5742 default:
fb34365b 5743 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5744 }
5745 }
5746 }
5747 else
5748 {
74f0fb50 5749 struct _opd_sec_data *opd;
1e2f5b6e
AM
5750
5751 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5752 opd = get_opd_info (rsec);
5753 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5754 {
74f0fb50 5755 rsec->gc_mark = 1;
ccfa59ea 5756
51aecdc5 5757 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 5758 }
5bd4f169
AM
5759 }
5760
1e2f5b6e 5761 return rsec;
5bd4f169
AM
5762}
5763
deb0e272
AM
5764/* The maximum size of .sfpr. */
5765#define SFPR_MAX (218*4)
5766
5767struct sfpr_def_parms
5768{
699733f6
AM
5769 const char name[12];
5770 unsigned char lo, hi;
2cdcc330
AM
5771 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5772 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
5773};
5774
a4b6fadd
AM
5775/* Auto-generate _save*, _rest* functions in .sfpr.
5776 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5777 instead. */
deb0e272 5778
4dfe6ac6 5779static bfd_boolean
a4b6fadd
AM
5780sfpr_define (struct bfd_link_info *info,
5781 const struct sfpr_def_parms *parm,
5782 asection *stub_sec)
deb0e272
AM
5783{
5784 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5785 unsigned int i;
5786 size_t len = strlen (parm->name);
5787 bfd_boolean writing = FALSE;
699733f6 5788 char sym[16];
deb0e272 5789
4dfe6ac6
NC
5790 if (htab == NULL)
5791 return FALSE;
5792
deb0e272
AM
5793 memcpy (sym, parm->name, len);
5794 sym[len + 2] = 0;
5795
5796 for (i = parm->lo; i <= parm->hi; i++)
5797 {
a4b6fadd 5798 struct ppc_link_hash_entry *h;
deb0e272
AM
5799
5800 sym[len + 0] = i / 10 + '0';
5801 sym[len + 1] = i % 10 + '0';
a4b6fadd 5802 h = (struct ppc_link_hash_entry *)
b32547cd 5803 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 5804 if (stub_sec != NULL)
deb0e272 5805 {
a4b6fadd
AM
5806 if (h != NULL
5807 && h->elf.root.type == bfd_link_hash_defined
5808 && h->elf.root.u.def.section == htab->sfpr)
5809 {
5810 struct elf_link_hash_entry *s;
5811 char buf[32];
5812 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5813 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5814 if (s == NULL)
5815 return FALSE;
5816 if (s->root.type == bfd_link_hash_new
5817 || (s->root.type = bfd_link_hash_defined
5818 && s->root.u.def.section == stub_sec))
5819 {
5820 s->root.type = bfd_link_hash_defined;
5821 s->root.u.def.section = stub_sec;
7dda8d3c 5822 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
5823 + h->elf.root.u.def.value);
5824 s->ref_regular = 1;
5825 s->def_regular = 1;
5826 s->ref_regular_nonweak = 1;
5827 s->forced_local = 1;
5828 s->non_elf = 0;
5829 s->root.linker_def = 1;
5830 }
5831 }
5832 continue;
5833 }
5834 if (h != NULL)
5835 {
5836 h->save_res = 1;
5837 if (!h->elf.def_regular)
deb0e272 5838 {
a4b6fadd
AM
5839 h->elf.root.type = bfd_link_hash_defined;
5840 h->elf.root.u.def.section = htab->sfpr;
5841 h->elf.root.u.def.value = htab->sfpr->size;
5842 h->elf.type = STT_FUNC;
5843 h->elf.def_regular = 1;
b32547cd 5844 h->elf.non_elf = 0;
a4b6fadd
AM
5845 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5846 writing = TRUE;
deb0e272 5847 if (htab->sfpr->contents == NULL)
a4b6fadd 5848 {
2cdcc330
AM
5849 htab->sfpr->contents
5850 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd
AM
5851 if (htab->sfpr->contents == NULL)
5852 return FALSE;
5853 }
deb0e272
AM
5854 }
5855 }
5856 if (writing)
5857 {
5858 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5859 if (i != parm->hi)
5860 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5861 else
5862 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5863 htab->sfpr->size = p - htab->sfpr->contents;
5864 }
5865 }
5866
5867 return TRUE;
5868}
5869
5870static bfd_byte *
5871savegpr0 (bfd *abfd, bfd_byte *p, int r)
5872{
5873 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5874 return p + 4;
5875}
5876
5877static bfd_byte *
5878savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5879{
5880 p = savegpr0 (abfd, p, r);
a078d95a 5881 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5882 p = p + 4;
5883 bfd_put_32 (abfd, BLR, p);
5884 return p + 4;
5885}
5886
5887static bfd_byte *
5888restgpr0 (bfd *abfd, bfd_byte *p, int r)
5889{
5890 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5891 return p + 4;
5892}
5893
5894static bfd_byte *
5895restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5896{
a078d95a 5897 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5898 p = p + 4;
5899 p = restgpr0 (abfd, p, r);
5900 bfd_put_32 (abfd, MTLR_R0, p);
5901 p = p + 4;
5902 if (r == 29)
5903 {
5904 p = restgpr0 (abfd, p, 30);
5905 p = restgpr0 (abfd, p, 31);
5906 }
5907 bfd_put_32 (abfd, BLR, p);
5908 return p + 4;
5909}
5910
5911static bfd_byte *
5912savegpr1 (bfd *abfd, bfd_byte *p, int r)
5913{
5914 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5915 return p + 4;
5916}
5917
5918static bfd_byte *
5919savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5920{
5921 p = savegpr1 (abfd, p, r);
5922 bfd_put_32 (abfd, BLR, p);
5923 return p + 4;
5924}
5925
5926static bfd_byte *
5927restgpr1 (bfd *abfd, bfd_byte *p, int r)
5928{
5929 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5930 return p + 4;
5931}
5932
5933static bfd_byte *
5934restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5935{
5936 p = restgpr1 (abfd, p, r);
5937 bfd_put_32 (abfd, BLR, p);
5938 return p + 4;
5939}
5940
5941static bfd_byte *
5942savefpr (bfd *abfd, bfd_byte *p, int r)
5943{
5944 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5945 return p + 4;
5946}
5947
5948static bfd_byte *
5949savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5950{
5951 p = savefpr (abfd, p, r);
a078d95a 5952 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5953 p = p + 4;
5954 bfd_put_32 (abfd, BLR, p);
5955 return p + 4;
5956}
5957
5958static bfd_byte *
5959restfpr (bfd *abfd, bfd_byte *p, int r)
5960{
5961 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5962 return p + 4;
5963}
5964
5965static bfd_byte *
5966restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5967{
a078d95a 5968 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5969 p = p + 4;
5970 p = restfpr (abfd, p, r);
5971 bfd_put_32 (abfd, MTLR_R0, p);
5972 p = p + 4;
5973 if (r == 29)
5974 {
5975 p = restfpr (abfd, p, 30);
5976 p = restfpr (abfd, p, 31);
5977 }
5978 bfd_put_32 (abfd, BLR, p);
5979 return p + 4;
5980}
5981
5982static bfd_byte *
5983savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5984{
5985 p = savefpr (abfd, p, r);
5986 bfd_put_32 (abfd, BLR, p);
5987 return p + 4;
5988}
5989
5990static bfd_byte *
5991restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5992{
5993 p = restfpr (abfd, p, r);
5994 bfd_put_32 (abfd, BLR, p);
5995 return p + 4;
5996}
5997
5998static bfd_byte *
5999savevr (bfd *abfd, bfd_byte *p, int r)
6000{
6001 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6002 p = p + 4;
6003 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6004 return p + 4;
6005}
6006
6007static bfd_byte *
6008savevr_tail (bfd *abfd, bfd_byte *p, int r)
6009{
6010 p = savevr (abfd, p, r);
6011 bfd_put_32 (abfd, BLR, p);
6012 return p + 4;
6013}
6014
6015static bfd_byte *
6016restvr (bfd *abfd, bfd_byte *p, int r)
6017{
6018 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6019 p = p + 4;
6020 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6021 return p + 4;
6022}
6023
6024static bfd_byte *
6025restvr_tail (bfd *abfd, bfd_byte *p, int r)
6026{
6027 p = restvr (abfd, p, r);
6028 bfd_put_32 (abfd, BLR, p);
6029 return p + 4;
6030}
6031
e86ce104
AM
6032/* Called via elf_link_hash_traverse to transfer dynamic linking
6033 information on function code symbol entries to their corresponding
6034 function descriptor symbol entries. */
deb0e272 6035
b34976b6 6036static bfd_boolean
4ce794b7 6037func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6038{
e86ce104 6039 struct bfd_link_info *info;
65f38f15 6040 struct ppc_link_hash_table *htab;
50bc7936
AM
6041 struct ppc_link_hash_entry *fh;
6042 struct ppc_link_hash_entry *fdh;
6043 bfd_boolean force_local;
5bd4f169 6044
50bc7936
AM
6045 fh = (struct ppc_link_hash_entry *) h;
6046 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6047 return TRUE;
e86ce104 6048
8c5b4e52
AM
6049 if (!fh->is_func)
6050 return TRUE;
6051
6052 if (fh->elf.root.root.string[0] != '.'
6053 || fh->elf.root.root.string[1] == '\0')
6054 return TRUE;
6055
4ce794b7 6056 info = inf;
65f38f15 6057 htab = ppc_hash_table (info);
4dfe6ac6
NC
6058 if (htab == NULL)
6059 return FALSE;
5bd4f169 6060
8c5b4e52
AM
6061 /* Find the corresponding function descriptor symbol. */
6062 fdh = lookup_fdh (fh, htab);
6063
c09bdfe5
AM
6064 /* Resolve undefined references to dot-symbols as the value
6065 in the function descriptor, if we have one in a regular object.
6066 This is to satisfy cases like ".quad .foo". Calls to functions
6067 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6068 if ((fh->elf.root.type == bfd_link_hash_undefined
6069 || fh->elf.root.type == bfd_link_hash_undefweak)
6070 && (fdh->elf.root.type == bfd_link_hash_defined
6071 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6072 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6073 && opd_entry_value (fdh->elf.root.u.def.section,
6074 fdh->elf.root.u.def.value,
c09bdfe5 6075 &fh->elf.root.u.def.section,
aef36ac1 6076 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6077 {
b31867b6 6078 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6079 fh->elf.forced_local = 1;
b31867b6
AM
6080 fh->elf.def_regular = fdh->elf.def_regular;
6081 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6082 }
6083
8c5b4e52
AM
6084 if (!fh->elf.dynamic)
6085 {
6086 struct plt_entry *ent;
5bd4f169 6087
8c5b4e52
AM
6088 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6089 if (ent->plt.refcount > 0)
6090 break;
6091 if (ent == NULL)
6092 return TRUE;
6093 }
5bd4f169 6094
8c5b4e52 6095 /* Create a descriptor as undefined if necessary. */
50bc7936 6096 if (fdh == NULL
0e1862bb 6097 && !bfd_link_executable (info)
50bc7936
AM
6098 && (fh->elf.root.type == bfd_link_hash_undefined
6099 || fh->elf.root.type == bfd_link_hash_undefweak))
6100 {
908b32fc 6101 fdh = make_fdh (info, fh);
bb700d78
AM
6102 if (fdh == NULL)
6103 return FALSE;
50bc7936 6104 }
648cca2c 6105
8c5b4e52 6106 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6107 if (fdh != NULL
6108 && fdh->fake
8c5b4e52
AM
6109 && (fh->elf.root.type == bfd_link_hash_defined
6110 || fh->elf.root.type == bfd_link_hash_defweak))
6111 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 6112
8c5b4e52
AM
6113 /* Transfer dynamic linking information to the function descriptor. */
6114 if (fdh != NULL)
6115 {
f5385ebf
AM
6116 fdh->elf.ref_regular |= fh->elf.ref_regular;
6117 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6118 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6119 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6120 fdh->elf.dynamic |= fh->elf.dynamic;
6121 fdh->elf.needs_plt |= (fh->elf.needs_plt
6122 || fh->elf.type == STT_FUNC
6123 || fh->elf.type == STT_GNU_IFUNC);
6124 move_plt_plist (fh, fdh);
6125
6126 if (!fdh->elf.forced_local
6127 && fh->elf.dynindx != -1)
6128 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6129 return FALSE;
e86ce104
AM
6130 }
6131
50bc7936
AM
6132 /* Now that the info is on the function descriptor, clear the
6133 function code sym info. Any function code syms for which we
6134 don't have a definition in a regular file, we force local.
6135 This prevents a shared library from exporting syms that have
6136 been imported from another library. Function code syms that
6137 are really in the library we must leave global to prevent the
6138 linker dragging in a definition from a static library. */
93f3fa99
AM
6139 force_local = (!fh->elf.def_regular
6140 || fdh == NULL
6141 || !fdh->elf.def_regular
6142 || fdh->elf.forced_local);
50bc7936
AM
6143 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6144
b34976b6 6145 return TRUE;
e86ce104 6146}
40b8271b 6147
a4b6fadd
AM
6148static const struct sfpr_def_parms save_res_funcs[] =
6149 {
6150 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6151 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6152 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6153 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6154 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6155 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6156 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6157 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6158 { "._savef", 14, 31, savefpr, savefpr1_tail },
6159 { "._restf", 14, 31, restfpr, restfpr1_tail },
6160 { "_savevr_", 20, 31, savevr, savevr_tail },
6161 { "_restvr_", 20, 31, restvr, restvr_tail }
6162 };
6163
e86ce104 6164/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6165 this hook to a) provide some gcc support functions, and b) transfer
6166 dynamic linking information gathered so far on function code symbol
6167 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6168
b34976b6 6169static bfd_boolean
4ce794b7
AM
6170ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6171 struct bfd_link_info *info)
e86ce104
AM
6172{
6173 struct ppc_link_hash_table *htab;
6174
6175 htab = ppc_hash_table (info);
4dfe6ac6
NC
6176 if (htab == NULL)
6177 return FALSE;
6178
b32547cd
AM
6179 /* Provide any missing _save* and _rest* functions. */
6180 if (htab->sfpr != NULL)
6181 {
6182 unsigned int i;
6183
6184 htab->sfpr->size = 0;
6185 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6186 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6187 return FALSE;
6188 if (htab->sfpr->size == 0)
6189 htab->sfpr->flags |= SEC_EXCLUDE;
6190 }
6191
6192 if (bfd_link_relocatable (info))
6193 return TRUE;
6194
6195 if (htab->elf.hgot != NULL)
dba6fa9b
AM
6196 {
6197 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6198 /* Make .TOC. defined so as to prevent it being made dynamic.
6199 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6200 if (!htab->elf.hgot->def_regular
6201 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6202 {
6203 htab->elf.hgot->root.type = bfd_link_hash_defined;
6204 htab->elf.hgot->root.u.def.value = 0;
6205 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6206 htab->elf.hgot->def_regular = 1;
6207 htab->elf.hgot->root.linker_def = 1;
6208 }
dba6fa9b 6209 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6210 htab->elf.hgot->other
6211 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6212 }
c66bb0ee 6213
8c5b4e52
AM
6214 if (htab->need_func_desc_adj)
6215 {
6216 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6217 htab->need_func_desc_adj = 0;
6218 }
805fc799 6219
b34976b6 6220 return TRUE;
e86ce104
AM
6221}
6222
98bbb1b8 6223/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 6224
98bbb1b8 6225static asection *
a345bc8d
AM
6226readonly_dynrelocs (struct elf_link_hash_entry *h)
6227{
6228 struct ppc_link_hash_entry *eh;
6229 struct elf_dyn_relocs *p;
6230
6231 eh = (struct ppc_link_hash_entry *) h;
6232 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6233 {
6234 asection *s = p->sec->output_section;
6235
6236 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 6237 return p->sec;
a345bc8d 6238 }
98bbb1b8 6239 return NULL;
a345bc8d
AM
6240}
6241
d311bc8b 6242/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6243 aliases, that apply to read-only sections. Cannot be used after
6244 size_dynamic_sections. */
d311bc8b
AM
6245
6246static bfd_boolean
6247alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6248{
6249 struct ppc_link_hash_entry *eh;
6250
6251 eh = (struct ppc_link_hash_entry *) h;
6252 do
6253 {
6254 if (readonly_dynrelocs (&eh->elf))
6255 return TRUE;
ab2477e1 6256 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
2cdcc330
AM
6257 }
6258 while (eh != NULL && &eh->elf != h);
d311bc8b
AM
6259
6260 return FALSE;
6261}
8a2058b5 6262
8a9e8e72
AM
6263/* Return whether EH has pc-relative dynamic relocs. */
6264
6265static bfd_boolean
6266pc_dynrelocs (struct ppc_link_hash_entry *eh)
6267{
6268 struct elf_dyn_relocs *p;
6269
6270 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6271 if (p->pc_count != 0)
6272 return TRUE;
6273 return FALSE;
6274}
6275
8a2058b5
AM
6276/* Return true if a global entry stub will be created for H. Valid
6277 for ELFv2 before plt entries have been allocated. */
6278
6279static bfd_boolean
6280global_entry_stub (struct elf_link_hash_entry *h)
6281{
6282 struct plt_entry *pent;
6283
6284 if (!h->pointer_equality_needed
6285 || h->def_regular)
6286 return FALSE;
6287
6288 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6289 if (pent->plt.refcount > 0
6290 && pent->addend == 0)
6291 return TRUE;
6292
6293 return FALSE;
6294}
6295
e86ce104
AM
6296/* Adjust a symbol defined by a dynamic object and referenced by a
6297 regular object. The current definition is in some section of the
6298 dynamic object, but we're not including those sections. We have to
6299 change the definition to something the rest of the link can
6300 understand. */
6301
b34976b6 6302static bfd_boolean
4ce794b7
AM
6303ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6304 struct elf_link_hash_entry *h)
e86ce104
AM
6305{
6306 struct ppc_link_hash_table *htab;
5474d94f 6307 asection *s, *srel;
e86ce104
AM
6308
6309 htab = ppc_hash_table (info);
4dfe6ac6
NC
6310 if (htab == NULL)
6311 return FALSE;
e86ce104
AM
6312
6313 /* Deal with function syms. */
6314 if (h->type == STT_FUNC
e054468f 6315 || h->type == STT_GNU_IFUNC
f5385ebf 6316 || h->needs_plt)
e86ce104 6317 {
529fe20e
AM
6318 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6319 || SYMBOL_CALLS_LOCAL (info, h)
6320 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6321 /* Discard dyn_relocs when non-pic if we've decided that a
6322 function symbol is local and not an ifunc. We keep dynamic
6323 relocs for ifuncs when local rather than always emitting a
6324 plt call stub for them and defining the symbol on the call
6325 stub. We can't do that for ELFv1 anyway (a function symbol
6326 is defined on a descriptor, not code) and it can be faster at
6327 run-time due to not needing to bounce through a stub. The
6328 dyn_relocs for ifuncs will be applied even in a static
6329 executable. */
6330 if (!bfd_link_pic (info)
6331 && h->type != STT_GNU_IFUNC
6332 && local)
6333 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6334
e86ce104
AM
6335 /* Clear procedure linkage table information for any symbol that
6336 won't need a .plt entry. */
411e1bfb
AM
6337 struct plt_entry *ent;
6338 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6339 if (ent->plt.refcount > 0)
6340 break;
8387904d 6341 if (ent == NULL
2d7ad24e
AM
6342 || (h->type != STT_GNU_IFUNC
6343 && local
3e04d765
AM
6344 && (htab->can_convert_all_inline_plt
6345 || (((struct ppc_link_hash_entry *) h)->tls_mask
6346 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6347 {
411e1bfb 6348 h->plt.plist = NULL;
f5385ebf 6349 h->needs_plt = 0;
d1eca1e4 6350 h->pointer_equality_needed = 0;
40b8271b 6351 }
8a2058b5 6352 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6353 {
d1eca1e4
AM
6354 /* Taking a function's address in a read/write section
6355 doesn't require us to define the function symbol in the
6356 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6357 be used instead. The reason we prefer a few more dynamic
6358 relocs is that calling via a global entry stub costs a
6359 few more instructions, and pointer_equality_needed causes
6360 extra work in ld.so when resolving these symbols. */
529fe20e 6361 if (global_entry_stub (h))
d1eca1e4 6362 {
ab2477e1 6363 if (!readonly_dynrelocs (h))
529fe20e
AM
6364 {
6365 h->pointer_equality_needed = 0;
04383fd1
AM
6366 /* If we haven't seen a branch reloc and the symbol
6367 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6368 if (!h->needs_plt)
6369 h->plt.plist = NULL;
6370 }
6371 else if (!bfd_link_pic (info))
6372 /* We are going to be defining the function symbol on the
6373 plt stub, so no dyn_relocs needed when non-pic. */
6374 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
6375 }
6376
3988aed5
AM
6377 /* ELFv2 function symbols can't have copy relocs. */
6378 return TRUE;
6379 }
6380 else if (!h->needs_plt
ab2477e1 6381 && !readonly_dynrelocs (h))
3988aed5 6382 {
04383fd1
AM
6383 /* If we haven't seen a branch reloc and the symbol isn't an
6384 ifunc then we don't need a plt entry. */
3988aed5
AM
6385 h->plt.plist = NULL;
6386 h->pointer_equality_needed = 0;
a345bc8d
AM
6387 return TRUE;
6388 }
5bd4f169 6389 }
bbd7ec4a 6390 else
411e1bfb 6391 h->plt.plist = NULL;
5bd4f169
AM
6392
6393 /* If this is a weak symbol, and there is a real definition, the
6394 processor independent code will have arranged for us to see the
6395 real definition first, and we can just use the same value. */
60d67dc8 6396 if (h->is_weakalias)
5bd4f169 6397 {
60d67dc8
AM
6398 struct elf_link_hash_entry *def = weakdef (h);
6399 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6400 h->root.u.def.section = def->root.u.def.section;
6401 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6402 if (def->root.u.def.section == htab->elf.sdynbss
6403 || def->root.u.def.section == htab->elf.sdynrelro)
6404 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 6405 return TRUE;
5bd4f169
AM
6406 }
6407
5bd4f169
AM
6408 /* If we are creating a shared library, we must presume that the
6409 only references to the symbol are via the global offset table.
6410 For such cases we need not do anything here; the relocations will
6411 be handled correctly by relocate_section. */
0e1862bb 6412 if (bfd_link_pic (info))
b34976b6 6413 return TRUE;
5bd4f169 6414
65f38f15
AM
6415 /* If there are no references to this symbol that do not use the
6416 GOT, we don't need to generate a copy reloc. */
f5385ebf 6417 if (!h->non_got_ref)
b34976b6 6418 return TRUE;
65f38f15 6419
b186458a 6420 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6421 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6422
d93d1c80
AM
6423 /* If -z nocopyreloc was given, don't generate them either. */
6424 || info->nocopyreloc
a127494f 6425
dce2246a 6426 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6427 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 6428 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 6429
d93d1c80
AM
6430 /* Protected variables do not work with .dynbss. The copy in
6431 .dynbss won't be used by the shared library with the protected
6432 definition for the variable. Text relocations are preferable
6433 to an incorrect program. */
6434 || h->protected_def)
529fe20e 6435 return TRUE;
a127494f 6436
5d35169e 6437 if (h->plt.plist != NULL)
97b639ba
AM
6438 {
6439 /* We should never get here, but unfortunately there are versions
6440 of gcc out there that improperly (for this ABI) put initialized
6441 function pointers, vtable refs and suchlike in read-only
6442 sections. Allow them to proceed, but warn that this might
6443 break at runtime. */
25f53a85 6444 info->callbacks->einfo
c1c8c1ef 6445 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6446 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6447 h->root.root.string);
6448 }
5d35169e
AM
6449
6450 /* This is a reference to a symbol defined by a dynamic object which
6451 is not a function. */
6452
5bd4f169
AM
6453 /* We must allocate the symbol in our .dynbss section, which will
6454 become part of the .bss section of the executable. There will be
6455 an entry for this symbol in the .dynsym section. The dynamic
6456 object will contain position independent code, so all references
6457 from the dynamic object to this symbol will go through the global
6458 offset table. The dynamic linker will use the .dynsym entry to
6459 determine the address it must put in the global offset table, so
6460 both the dynamic object and the regular object will refer to the
6461 same memory location for the variable. */
5474d94f
AM
6462 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6463 {
6464 s = htab->elf.sdynrelro;
6465 srel = htab->elf.sreldynrelro;
6466 }
6467 else
6468 {
6469 s = htab->elf.sdynbss;
6470 srel = htab->elf.srelbss;
6471 }
1d7e9d18 6472 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6473 {
4a7e5234
AM
6474 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6475 linker to copy the initial value out of the dynamic object
6476 and into the runtime process image. */
5474d94f 6477 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6478 h->needs_copy = 1;
5bd4f169
AM
6479 }
6480
529fe20e
AM
6481 /* We no longer want dyn_relocs. */
6482 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 6483 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6484}
6485
e86ce104
AM
6486/* If given a function descriptor symbol, hide both the function code
6487 sym and the descriptor. */
6488static void
4ce794b7
AM
6489ppc64_elf_hide_symbol (struct bfd_link_info *info,
6490 struct elf_link_hash_entry *h,
6491 bfd_boolean force_local)
e86ce104 6492{
34814b9f 6493 struct ppc_link_hash_entry *eh;
e86ce104
AM
6494 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6495
87469ba2
AM
6496 if (ppc_hash_table (info) == NULL)
6497 return;
6498
34814b9f
AM
6499 eh = (struct ppc_link_hash_entry *) h;
6500 if (eh->is_func_descriptor)
e86ce104 6501 {
34814b9f 6502 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6503
721956f4 6504 if (fh == NULL)
d1329ca3
AM
6505 {
6506 const char *p, *q;
b8ac2841 6507 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6508 char save;
6509
6510 /* We aren't supposed to use alloca in BFD because on
6511 systems which do not have alloca the version in libiberty
6512 calls xmalloc, which might cause the program to crash
6513 when it runs out of memory. This function doesn't have a
6514 return status, so there's no way to gracefully return an
6515 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6516 accessed; It's either a string in an ELF string table,
6517 or allocated in an objalloc structure. */
d1329ca3 6518
34814b9f 6519 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6520 save = *p;
6521 *(char *) p = '.';
34814b9f 6522 fh = (struct ppc_link_hash_entry *)
b8ac2841 6523 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6524 *(char *) p = save;
6525
6526 /* Unfortunately, if it so happens that the string we were
6527 looking for was allocated immediately before this string,
6528 then we overwrote the string terminator. That's the only
6529 reason the lookup should fail. */
6530 if (fh == NULL)
6531 {
34814b9f
AM
6532 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6533 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6534 --q, --p;
34814b9f
AM
6535 if (q < eh->elf.root.root.string && *p == '.')
6536 fh = (struct ppc_link_hash_entry *)
b8ac2841 6537 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6538 }
6539 if (fh != NULL)
6540 {
34814b9f
AM
6541 eh->oh = fh;
6542 fh->oh = eh;
d1329ca3
AM
6543 }
6544 }
e86ce104 6545 if (fh != NULL)
34814b9f 6546 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6547 }
6548}
6549
411e1bfb 6550static bfd_boolean
8843416a
AM
6551get_sym_h (struct elf_link_hash_entry **hp,
6552 Elf_Internal_Sym **symp,
6553 asection **symsecp,
f961d9dd 6554 unsigned char **tls_maskp,
8843416a
AM
6555 Elf_Internal_Sym **locsymsp,
6556 unsigned long r_symndx,
6557 bfd *ibfd)
411e1bfb 6558{
0ffa91dd 6559 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6560
6561 if (r_symndx >= symtab_hdr->sh_info)
6562 {
6563 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6564 struct elf_link_hash_entry *h;
6565
6566 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6567 h = elf_follow_link (h);
411e1bfb
AM
6568
6569 if (hp != NULL)
6570 *hp = h;
6571
6572 if (symp != NULL)
6573 *symp = NULL;
6574
6575 if (symsecp != NULL)
6576 {
6577 asection *symsec = NULL;
6578 if (h->root.type == bfd_link_hash_defined
6579 || h->root.type == bfd_link_hash_defweak)
6580 symsec = h->root.u.def.section;
6581 *symsecp = symsec;
6582 }
6583
e7b938ca 6584 if (tls_maskp != NULL)
411e1bfb
AM
6585 {
6586 struct ppc_link_hash_entry *eh;
6587
6588 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6589 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6590 }
6591 }
6592 else
6593 {
6594 Elf_Internal_Sym *sym;
6595 Elf_Internal_Sym *locsyms = *locsymsp;
6596
6597 if (locsyms == NULL)
6598 {
6599 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6600 if (locsyms == NULL)
6601 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6602 symtab_hdr->sh_info,
6603 0, NULL, NULL, NULL);
6604 if (locsyms == NULL)
6605 return FALSE;
6606 *locsymsp = locsyms;
6607 }
6608 sym = locsyms + r_symndx;
6609
6610 if (hp != NULL)
6611 *hp = NULL;
6612
6613 if (symp != NULL)
6614 *symp = sym;
6615
6616 if (symsecp != NULL)
cb33740c 6617 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6618
e7b938ca 6619 if (tls_maskp != NULL)
411e1bfb
AM
6620 {
6621 struct got_entry **lgot_ents;
f961d9dd 6622 unsigned char *tls_mask;
411e1bfb 6623
e7b938ca 6624 tls_mask = NULL;
411e1bfb
AM
6625 lgot_ents = elf_local_got_ents (ibfd);
6626 if (lgot_ents != NULL)
6627 {
e054468f
AM
6628 struct plt_entry **local_plt = (struct plt_entry **)
6629 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6630 unsigned char *lgot_masks = (unsigned char *)
e054468f 6631 (local_plt + symtab_hdr->sh_info);
e7b938ca 6632 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6633 }
e7b938ca 6634 *tls_maskp = tls_mask;
411e1bfb
AM
6635 }
6636 }
6637 return TRUE;
6638}
6639
e7b938ca 6640/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6641 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6642 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6643
6644static int
f961d9dd 6645get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6646 unsigned long *toc_symndx,
6647 bfd_vma *toc_addend,
0d4792f7 6648 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6649 const Elf_Internal_Rela *rel,
6650 bfd *ibfd)
411e1bfb
AM
6651{
6652 unsigned long r_symndx;
0d4792f7 6653 int next_r;
411e1bfb
AM
6654 struct elf_link_hash_entry *h;
6655 Elf_Internal_Sym *sym;
6656 asection *sec;
6657 bfd_vma off;
6658
6659 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6660 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6661 return 0;
411e1bfb 6662
37da22e5
AM
6663 if ((*tls_maskp != NULL
6664 && (**tls_maskp & TLS_TLS) != 0
6665 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 6666 || sec == NULL
6bee8834 6667 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6668 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6669 return 1;
411e1bfb
AM
6670
6671 /* Look inside a TOC section too. */
6672 if (h != NULL)
6673 {
6674 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6675 off = h->root.u.def.value;
6676 }
6677 else
6678 off = sym->st_value;
6679 off += rel->r_addend;
6680 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6681 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6682 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6683 if (toc_symndx != NULL)
6684 *toc_symndx = r_symndx;
3a71aa26
AM
6685 if (toc_addend != NULL)
6686 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6687 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6688 return 0;
854b41e7 6689 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6690 && (next_r == -1 || next_r == -2))
6691 return 1 - next_r;
951fd09b 6692 return 1;
411e1bfb
AM
6693}
6694
3b421ab3
AM
6695/* Find (or create) an entry in the tocsave hash table. */
6696
6697static struct tocsave_entry *
6698tocsave_find (struct ppc_link_hash_table *htab,
6699 enum insert_option insert,
6700 Elf_Internal_Sym **local_syms,
6701 const Elf_Internal_Rela *irela,
6702 bfd *ibfd)
6703{
6704 unsigned long r_indx;
6705 struct elf_link_hash_entry *h;
6706 Elf_Internal_Sym *sym;
6707 struct tocsave_entry ent, *p;
6708 hashval_t hash;
6709 struct tocsave_entry **slot;
6710
6711 r_indx = ELF64_R_SYM (irela->r_info);
6712 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6713 return NULL;
6714 if (ent.sec == NULL || ent.sec->output_section == NULL)
6715 {
4eca0228 6716 _bfd_error_handler
871b3ab2 6717 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
6718 return NULL;
6719 }
6720
6721 if (h != NULL)
6722 ent.offset = h->root.u.def.value;
6723 else
6724 ent.offset = sym->st_value;
6725 ent.offset += irela->r_addend;
6726
6727 hash = tocsave_htab_hash (&ent);
6728 slot = ((struct tocsave_entry **)
6729 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6730 if (slot == NULL)
6731 return NULL;
6732
6733 if (*slot == NULL)
6734 {
6735 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6736 if (p == NULL)
6737 return NULL;
6738 *p = ent;
6739 *slot = p;
6740 }
6741 return *slot;
6742}
6743
754021d0 6744/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6745 code for the old ABI, these will already have been done. */
754021d0
AM
6746
6747static bfd_boolean
6748adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6749{
6750 struct ppc_link_hash_entry *eh;
6751 asection *sym_sec;
74f0fb50 6752 struct _opd_sec_data *opd;
754021d0
AM
6753
6754 if (h->root.type == bfd_link_hash_indirect)
6755 return TRUE;
6756
754021d0
AM
6757 if (h->root.type != bfd_link_hash_defined
6758 && h->root.type != bfd_link_hash_defweak)
6759 return TRUE;
6760
6761 eh = (struct ppc_link_hash_entry *) h;
6762 if (eh->adjust_done)
6763 return TRUE;
6764
6765 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6766 opd = get_opd_info (sym_sec);
6767 if (opd != NULL && opd->adjust != NULL)
754021d0 6768 {
51aecdc5 6769 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
6770 if (adjust == -1)
6771 {
6772 /* This entry has been deleted. */
b3fac117 6773 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6774 if (dsec == NULL)
6775 {
6776 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 6777 if (discarded_section (dsec))
81688140 6778 {
b3fac117 6779 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6780 break;
6781 }
6782 }
4025353c 6783 eh->elf.root.u.def.value = 0;
81688140 6784 eh->elf.root.u.def.section = dsec;
4025353c
AM
6785 }
6786 else
6787 eh->elf.root.u.def.value += adjust;
754021d0
AM
6788 eh->adjust_done = 1;
6789 }
6790 return TRUE;
6791}
6792
8c1d1bb8 6793/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 6794 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
6795 have already been determined. */
6796
6797static bfd_boolean
6798dec_dynrel_count (bfd_vma r_info,
6799 asection *sec,
6800 struct bfd_link_info *info,
6801 Elf_Internal_Sym **local_syms,
6802 struct elf_link_hash_entry *h,
19e08130 6803 Elf_Internal_Sym *sym)
8c1d1bb8
AM
6804{
6805 enum elf_ppc64_reloc_type r_type;
19e08130 6806 asection *sym_sec = NULL;
8c1d1bb8
AM
6807
6808 /* Can this reloc be dynamic? This switch, and later tests here
6809 should be kept in sync with the code in check_relocs. */
6810 r_type = ELF64_R_TYPE (r_info);
6811 switch (r_type)
6812 {
6813 default:
6814 return TRUE;
6815
6816 case R_PPC64_TPREL16:
6817 case R_PPC64_TPREL16_LO:
6818 case R_PPC64_TPREL16_HI:
6819 case R_PPC64_TPREL16_HA:
6820 case R_PPC64_TPREL16_DS:
6821 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
6822 case R_PPC64_TPREL16_HIGH:
6823 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
6824 case R_PPC64_TPREL16_HIGHER:
6825 case R_PPC64_TPREL16_HIGHERA:
6826 case R_PPC64_TPREL16_HIGHEST:
6827 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8 6828 case R_PPC64_TPREL64:
c213164a 6829 case R_PPC64_TPREL34:
8c1d1bb8
AM
6830 case R_PPC64_DTPMOD64:
6831 case R_PPC64_DTPREL64:
6832 case R_PPC64_ADDR64:
6833 case R_PPC64_REL30:
6834 case R_PPC64_REL32:
6835 case R_PPC64_REL64:
6836 case R_PPC64_ADDR14:
6837 case R_PPC64_ADDR14_BRNTAKEN:
6838 case R_PPC64_ADDR14_BRTAKEN:
6839 case R_PPC64_ADDR16:
6840 case R_PPC64_ADDR16_DS:
6841 case R_PPC64_ADDR16_HA:
6842 case R_PPC64_ADDR16_HI:
f9c6b907
AM
6843 case R_PPC64_ADDR16_HIGH:
6844 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
6845 case R_PPC64_ADDR16_HIGHER:
6846 case R_PPC64_ADDR16_HIGHERA:
6847 case R_PPC64_ADDR16_HIGHEST:
6848 case R_PPC64_ADDR16_HIGHESTA:
6849 case R_PPC64_ADDR16_LO:
6850 case R_PPC64_ADDR16_LO_DS:
6851 case R_PPC64_ADDR24:
6852 case R_PPC64_ADDR32:
6853 case R_PPC64_UADDR16:
6854 case R_PPC64_UADDR32:
6855 case R_PPC64_UADDR64:
6856 case R_PPC64_TOC:
5663e321
AM
6857 case R_PPC64_D34:
6858 case R_PPC64_D34_LO:
6859 case R_PPC64_D34_HI30:
6860 case R_PPC64_D34_HA30:
6861 case R_PPC64_ADDR16_HIGHER34:
6862 case R_PPC64_ADDR16_HIGHERA34:
6863 case R_PPC64_ADDR16_HIGHEST34:
6864 case R_PPC64_ADDR16_HIGHESTA34:
6865 case R_PPC64_D28:
8c1d1bb8
AM
6866 break;
6867 }
6868
6869 if (local_syms != NULL)
6870 {
6871 unsigned long r_symndx;
8c1d1bb8
AM
6872 bfd *ibfd = sec->owner;
6873
6874 r_symndx = ELF64_R_SYM (r_info);
6875 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6876 return FALSE;
6877 }
6878
0e1862bb 6879 if ((bfd_link_pic (info)
1d483afe 6880 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 6881 || (h != NULL
198f1157 6882 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
6883 || h->root.type == bfd_link_hash_defweak
6884 || !h->def_regular))))
6885 || (ELIMINATE_COPY_RELOCS
0e1862bb 6886 && !bfd_link_pic (info)
8c1d1bb8
AM
6887 && h != NULL
6888 && (h->root.type == bfd_link_hash_defweak
6889 || !h->def_regular)))
6890 ;
6891 else
6892 return TRUE;
6893
6894 if (h != NULL)
6edfbbad 6895 {
19e08130
AM
6896 struct elf_dyn_relocs *p;
6897 struct elf_dyn_relocs **pp;
6898 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6899
6900 /* elf_gc_sweep may have already removed all dyn relocs associated
6901 with local syms for a given section. Also, symbol flags are
6902 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6903 report a dynreloc miscount. */
6904 if (*pp == NULL && info->gc_sections)
6905 return TRUE;
6906
6907 while ((p = *pp) != NULL)
60124e18 6908 {
19e08130
AM
6909 if (p->sec == sec)
6910 {
6911 if (!must_be_dyn_reloc (info, r_type))
6912 p->pc_count -= 1;
6913 p->count -= 1;
6914 if (p->count == 0)
6915 *pp = p->next;
6916 return TRUE;
6917 }
6918 pp = &p->next;
60124e18 6919 }
6edfbbad 6920 }
19e08130
AM
6921 else
6922 {
6923 struct ppc_dyn_relocs *p;
6924 struct ppc_dyn_relocs **pp;
6925 void *vpp;
6926 bfd_boolean is_ifunc;
8c1d1bb8 6927
19e08130
AM
6928 if (local_syms == NULL)
6929 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6930 if (sym_sec == NULL)
6931 sym_sec = sec;
c57da1a7 6932
19e08130
AM
6933 vpp = &elf_section_data (sym_sec)->local_dynrel;
6934 pp = (struct ppc_dyn_relocs **) vpp;
6935
6936 if (*pp == NULL && info->gc_sections)
6937 return TRUE;
6938
6939 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6940 while ((p = *pp) != NULL)
8c1d1bb8 6941 {
19e08130
AM
6942 if (p->sec == sec && p->ifunc == is_ifunc)
6943 {
6944 p->count -= 1;
6945 if (p->count == 0)
6946 *pp = p->next;
6947 return TRUE;
6948 }
6949 pp = &p->next;
8c1d1bb8 6950 }
8c1d1bb8
AM
6951 }
6952
695344c0 6953 /* xgettext:c-format */
cf97bcb0
AM
6954 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6955 sec->owner, sec);
8c1d1bb8
AM
6956 bfd_set_error (bfd_error_bad_value);
6957 return FALSE;
6958}
6959
754021d0
AM
6960/* Remove unused Official Procedure Descriptor entries. Currently we
6961 only remove those associated with functions in discarded link-once
6962 sections, or weakly defined functions that have been overridden. It
6963 would be possible to remove many more entries for statically linked
6964 applications. */
6965
b34976b6 6966bfd_boolean
e7d1c40c 6967ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
6968{
6969 bfd *ibfd;
754021d0 6970 bfd_boolean some_edited = FALSE;
3f764659 6971 asection *need_pad = NULL;
e7d1c40c
AM
6972 struct ppc_link_hash_table *htab;
6973
6974 htab = ppc_hash_table (info);
6975 if (htab == NULL)
6976 return FALSE;
1e2f5b6e 6977
c72f2fb2 6978 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
6979 {
6980 asection *sec;
6981 Elf_Internal_Rela *relstart, *rel, *relend;
6982 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 6983 Elf_Internal_Sym *local_syms;
74f0fb50 6984 struct _opd_sec_data *opd;
51aecdc5 6985 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 6986 bfd_size_type cnt_16b = 0;
1e2f5b6e 6987
854b41e7
AM
6988 if (!is_ppc64_elf (ibfd))
6989 continue;
6990
1e2f5b6e 6991 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 6992 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
6993 continue;
6994
dbaa2011 6995 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
6996 continue;
6997
1e2f5b6e
AM
6998 if (sec->output_section == bfd_abs_section_ptr)
6999 continue;
7000
7001 /* Look through the section relocs. */
7002 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7003 continue;
7004
6cdc0ccc 7005 local_syms = NULL;
0ffa91dd 7006 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7007
7008 /* Read the relocations. */
4ce794b7 7009 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7010 info->keep_memory);
1e2f5b6e 7011 if (relstart == NULL)
b34976b6 7012 return FALSE;
1e2f5b6e
AM
7013
7014 /* First run through the relocs to check they are sane, and to
7015 determine whether we need to edit this opd section. */
b34976b6 7016 need_edit = FALSE;
51aecdc5 7017 broken = FALSE;
3f764659 7018 need_pad = sec;
1e2f5b6e 7019 relend = relstart + sec->reloc_count;
50bc7936 7020 for (rel = relstart; rel < relend; )
1e2f5b6e 7021 {
04c9666a 7022 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7023 unsigned long r_symndx;
7024 asection *sym_sec;
7025 struct elf_link_hash_entry *h;
7026 Elf_Internal_Sym *sym;
51aecdc5 7027 bfd_vma offset;
1e2f5b6e 7028
51aecdc5 7029 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7030 only interested in the reloc pointing to a function entry
7031 point. */
51aecdc5
AM
7032 offset = rel->r_offset;
7033 if (rel + 1 == relend
7034 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7035 {
7036 /* If someone messes with .opd alignment then after a
7037 "ld -r" we might have padding in the middle of .opd.
7038 Also, there's nothing to prevent someone putting
7039 something silly in .opd with the assembler. No .opd
b34976b6 7040 optimization for them! */
3f764659 7041 broken_opd:
4eca0228 7042 _bfd_error_handler
871b3ab2 7043 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7044 broken = TRUE;
1e2f5b6e
AM
7045 break;
7046 }
7047
50bc7936
AM
7048 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7049 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7050 {
4eca0228 7051 _bfd_error_handler
695344c0 7052 /* xgettext:c-format */
871b3ab2 7053 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7054 ibfd, r_type);
51aecdc5 7055 broken = TRUE;
50bc7936
AM
7056 break;
7057 }
7058
1e2f5b6e 7059 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7060 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7061 r_symndx, ibfd))
50bc7936 7062 goto error_ret;
1e2f5b6e
AM
7063
7064 if (sym_sec == NULL || sym_sec->owner == NULL)
7065 {
411e1bfb
AM
7066 const char *sym_name;
7067 if (h != NULL)
7068 sym_name = h->root.root.string;
7069 else
26c61ae5
L
7070 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7071 sym_sec);
411e1bfb 7072
4eca0228 7073 _bfd_error_handler
695344c0 7074 /* xgettext:c-format */
871b3ab2 7075 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7076 ibfd, sym_name);
51aecdc5 7077 broken = TRUE;
1e2f5b6e
AM
7078 break;
7079 }
7080
51020317
AM
7081 /* opd entries are always for functions defined in the
7082 current input bfd. If the symbol isn't defined in the
7083 input bfd, then we won't be using the function in this
7084 bfd; It must be defined in a linkonce section in another
7085 bfd, or is weak. It's also possible that we are
7086 discarding the function due to a linker script /DISCARD/,
7087 which we test for via the output_section. */
7088 if (sym_sec->owner != ibfd
7089 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7090 need_edit = TRUE;
1e2f5b6e 7091
50bc7936 7092 rel += 2;
51aecdc5
AM
7093 if (rel + 1 == relend
7094 || (rel + 2 < relend
7095 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7096 ++rel;
7097
7098 if (rel == relend)
3f764659
JJ
7099 {
7100 if (sec->size == offset + 24)
7101 {
7102 need_pad = NULL;
7103 break;
7104 }
51aecdc5 7105 if (sec->size == offset + 16)
3f764659
JJ
7106 {
7107 cnt_16b++;
7108 break;
7109 }
7110 goto broken_opd;
7111 }
3f764659
JJ
7112 else if (rel + 1 < relend
7113 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7114 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7115 {
51aecdc5
AM
7116 if (rel[0].r_offset == offset + 16)
7117 cnt_16b++;
7118 else if (rel[0].r_offset != offset + 24)
7119 goto broken_opd;
3f764659
JJ
7120 }
7121 else
7122 goto broken_opd;
1e2f5b6e
AM
7123 }
7124
e7d1c40c 7125 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7126
51aecdc5 7127 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7128 {
7129 Elf_Internal_Rela *write_rel;
d4730f92 7130 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7131 bfd_byte *rptr, *wptr;
983bddc8 7132 bfd_byte *new_contents;
74f0fb50
AM
7133 bfd_size_type amt;
7134
983bddc8 7135 new_contents = NULL;
51aecdc5 7136 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7137 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7138 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7139 if (opd->adjust == NULL)
7140 return FALSE;
1e2f5b6e
AM
7141
7142 /* This seems a waste of time as input .opd sections are all
7143 zeros as generated by gcc, but I suppose there's no reason
7144 this will always be so. We might start putting something in
7145 the third word of .opd entries. */
7146 if ((sec->flags & SEC_IN_MEMORY) == 0)
7147 {
eea6121a
AM
7148 bfd_byte *loc;
7149 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7150 {
eea6121a
AM
7151 if (loc != NULL)
7152 free (loc);
50bc7936 7153 error_ret:
6cdc0ccc
AM
7154 if (local_syms != NULL
7155 && symtab_hdr->contents != (unsigned char *) local_syms)
7156 free (local_syms);
6cdc0ccc
AM
7157 if (elf_section_data (sec)->relocs != relstart)
7158 free (relstart);
b34976b6 7159 return FALSE;
6cdc0ccc 7160 }
1e2f5b6e
AM
7161 sec->contents = loc;
7162 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7163 }
7164
7165 elf_section_data (sec)->relocs = relstart;
7166
3f764659 7167 new_contents = sec->contents;
3f764659
JJ
7168 if (add_aux_fields)
7169 {
7170 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7171 if (new_contents == NULL)
7172 return FALSE;
51aecdc5 7173 need_pad = NULL;
3f764659 7174 }
b4f4e59f
AM
7175 wptr = new_contents;
7176 rptr = sec->contents;
1e2f5b6e 7177 write_rel = relstart;
51aecdc5 7178 for (rel = relstart; rel < relend; )
1e2f5b6e 7179 {
50bc7936
AM
7180 unsigned long r_symndx;
7181 asection *sym_sec;
7182 struct elf_link_hash_entry *h;
51aecdc5 7183 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7184 Elf_Internal_Sym *sym;
51aecdc5
AM
7185 long opd_ent_size;
7186 Elf_Internal_Rela *next_rel;
7187 bfd_boolean skip;
50bc7936
AM
7188
7189 r_symndx = ELF64_R_SYM (rel->r_info);
7190 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7191 r_symndx, ibfd))
50bc7936
AM
7192 goto error_ret;
7193
51aecdc5
AM
7194 next_rel = rel + 2;
7195 if (next_rel + 1 == relend
7196 || (next_rel + 2 < relend
7197 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7198 ++next_rel;
7199
7200 /* See if the .opd entry is full 24 byte or
7201 16 byte (with fd_aux entry overlapped with next
7202 fd_func). */
7203 opd_ent_size = 24;
7204 if (next_rel == relend)
1e2f5b6e 7205 {
51aecdc5 7206 if (sec->size == rel->r_offset + 16)
3f764659 7207 opd_ent_size = 16;
51aecdc5
AM
7208 }
7209 else if (next_rel->r_offset == rel->r_offset + 16)
7210 opd_ent_size = 16;
3f764659 7211
51aecdc5
AM
7212 if (h != NULL
7213 && h->root.root.string[0] == '.')
7214 {
8c5b4e52
AM
7215 fdh = ((struct ppc_link_hash_entry *) h)->oh;
7216 if (fdh != NULL)
7217 {
7218 fdh = ppc_follow_link (fdh);
7219 if (fdh->elf.root.type != bfd_link_hash_defined
7220 && fdh->elf.root.type != bfd_link_hash_defweak)
7221 fdh = NULL;
7222 }
51aecdc5 7223 }
1e2f5b6e 7224
51aecdc5
AM
7225 skip = (sym_sec->owner != ibfd
7226 || sym_sec->output_section == bfd_abs_section_ptr);
7227 if (skip)
7228 {
7229 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7230 {
51aecdc5
AM
7231 /* Arrange for the function descriptor sym
7232 to be dropped. */
7233 fdh->elf.root.u.def.value = 0;
7234 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7235 }
51aecdc5 7236 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7237
0e1862bb 7238 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7239 rel = next_rel;
7240 else
7241 while (1)
7242 {
7243 if (!dec_dynrel_count (rel->r_info, sec, info,
7244 NULL, h, sym))
7245 goto error_ret;
754021d0 7246
51aecdc5
AM
7247 if (++rel == next_rel)
7248 break;
1e2f5b6e 7249
51aecdc5
AM
7250 r_symndx = ELF64_R_SYM (rel->r_info);
7251 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7252 r_symndx, ibfd))
7253 goto error_ret;
7254 }
50bc7936
AM
7255 }
7256 else
1e2f5b6e 7257 {
51aecdc5
AM
7258 /* We'll be keeping this opd entry. */
7259 long adjust;
7260
7261 if (fdh != NULL)
7262 {
7263 /* Redefine the function descriptor symbol to
7264 this location in the opd section. It is
7265 necessary to update the value here rather
7266 than using an array of adjustments as we do
7267 for local symbols, because various places
7268 in the generic ELF code use the value
7269 stored in u.def.value. */
7270 fdh->elf.root.u.def.value = wptr - new_contents;
7271 fdh->adjust_done = 1;
7272 }
7273
7274 /* Local syms are a bit tricky. We could
7275 tweak them as they can be cached, but
7276 we'd need to look through the local syms
7277 for the function descriptor sym which we
7278 don't have at the moment. So keep an
7279 array of adjustments. */
7280 adjust = (wptr - new_contents) - (rptr - sec->contents);
7281 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7282
7283 if (wptr != rptr)
7284 memcpy (wptr, rptr, opd_ent_size);
7285 wptr += opd_ent_size;
7286 if (add_aux_fields && opd_ent_size == 16)
7287 {
7288 memset (wptr, '\0', 8);
7289 wptr += 8;
7290 }
7291
50bc7936 7292 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7293 new opd entries. */
7294 for ( ; rel != next_rel; ++rel)
7295 {
7296 rel->r_offset += adjust;
7297 if (write_rel != rel)
7298 memcpy (write_rel, rel, sizeof (*rel));
7299 ++write_rel;
7300 }
1e2f5b6e 7301 }
51aecdc5
AM
7302
7303 rptr += opd_ent_size;
1e2f5b6e
AM
7304 }
7305
3f764659 7306 sec->size = wptr - new_contents;
1e2f5b6e 7307 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7308 if (add_aux_fields)
7309 {
7310 free (sec->contents);
7311 sec->contents = new_contents;
7312 }
7313
05bf9422 7314 /* Fudge the header size too, as this is used later in
cdcf6e38 7315 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7316 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7317 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7318 some_edited = TRUE;
1e2f5b6e 7319 }
6cdc0ccc 7320 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7321 free (relstart);
6cdc0ccc 7322
411e1bfb
AM
7323 if (local_syms != NULL
7324 && symtab_hdr->contents != (unsigned char *) local_syms)
7325 {
7326 if (!info->keep_memory)
7327 free (local_syms);
7328 else
7329 symtab_hdr->contents = (unsigned char *) local_syms;
7330 }
7331 }
7332
754021d0
AM
7333 if (some_edited)
7334 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7335
3f764659
JJ
7336 /* If we are doing a final link and the last .opd entry is just 16 byte
7337 long, add a 8 byte padding after it. */
0e1862bb 7338 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7339 {
7340 bfd_byte *p;
7341
7342 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7343 {
7344 BFD_ASSERT (need_pad->size > 0);
7345
7346 p = bfd_malloc (need_pad->size + 8);
7347 if (p == NULL)
7348 return FALSE;
699733f6 7349
2cdcc330
AM
7350 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7351 p, 0, need_pad->size))
3f764659
JJ
7352 return FALSE;
7353
7354 need_pad->contents = p;
7355 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7356 }
7357 else
7358 {
7359 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7360 if (p == NULL)
7361 return FALSE;
7362
7363 need_pad->contents = p;
7364 }
7365
7366 memset (need_pad->contents + need_pad->size, 0, 8);
7367 need_pad->size += 8;
7368 }
7369
411e1bfb
AM
7370 return TRUE;
7371}
7372
3e04d765
AM
7373/* Analyze inline PLT call relocations to see whether calls to locally
7374 defined functions can be converted to direct calls. */
7375
7376bfd_boolean
7377ppc64_elf_inline_plt (struct bfd_link_info *info)
7378{
7379 struct ppc_link_hash_table *htab;
7380 bfd *ibfd;
7381 asection *sec;
7382 bfd_vma low_vma, high_vma, limit;
7383
7384 htab = ppc_hash_table (info);
7385 if (htab == NULL)
7386 return FALSE;
7387
7388 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7389 reduced somewhat to cater for possible stubs that might be added
7390 between the call and its destination. */
7391 if (htab->params->group_size < 0)
7392 {
7393 limit = -htab->params->group_size;
7394 if (limit == 1)
7395 limit = 0x1e00000;
7396 }
7397 else
7398 {
7399 limit = htab->params->group_size;
7400 if (limit == 1)
7401 limit = 0x1c00000;
7402 }
7403
7404 low_vma = -1;
7405 high_vma = 0;
7406 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7407 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7408 {
7409 if (low_vma > sec->vma)
7410 low_vma = sec->vma;
7411 if (high_vma < sec->vma + sec->size)
7412 high_vma = sec->vma + sec->size;
7413 }
7414
7415 /* If a "bl" can reach anywhere in local code sections, then we can
7416 convert all inline PLT sequences to direct calls when the symbol
7417 is local. */
7418 if (high_vma - low_vma < limit)
7419 {
7420 htab->can_convert_all_inline_plt = 1;
7421 return TRUE;
7422 }
7423
7424 /* Otherwise, go looking through relocs for cases where a direct
7425 call won't reach. Mark the symbol on any such reloc to disable
7426 the optimization and keep the PLT entry as it seems likely that
7427 this will be better than creating trampolines. Note that this
7428 will disable the optimization for all inline PLT calls to a
7429 particular symbol, not just those that won't reach. The
7430 difficulty in doing a more precise optimization is that the
7431 linker needs to make a decision depending on whether a
7432 particular R_PPC64_PLTCALL insn can be turned into a direct
7433 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7434 the sequence, and there is nothing that ties those relocs
7435 together except their symbol. */
7436
7437 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7438 {
7439 Elf_Internal_Shdr *symtab_hdr;
7440 Elf_Internal_Sym *local_syms;
7441
7442 if (!is_ppc64_elf (ibfd))
7443 continue;
7444
7445 local_syms = NULL;
7446 symtab_hdr = &elf_symtab_hdr (ibfd);
7447
7448 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7449 if (ppc64_elf_section_data (sec)->has_pltcall
7450 && !bfd_is_abs_section (sec->output_section))
7451 {
7452 Elf_Internal_Rela *relstart, *rel, *relend;
7453
7454 /* Read the relocations. */
7455 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7456 info->keep_memory);
7457 if (relstart == NULL)
7458 return FALSE;
7459
7460 relend = relstart + sec->reloc_count;
7461 for (rel = relstart; rel < relend; )
7462 {
7463 enum elf_ppc64_reloc_type r_type;
7464 unsigned long r_symndx;
7465 asection *sym_sec;
7466 struct elf_link_hash_entry *h;
7467 Elf_Internal_Sym *sym;
7468 unsigned char *tls_maskp;
7469
7470 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7471 if (r_type != R_PPC64_PLTCALL
7472 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7473 continue;
7474
7475 r_symndx = ELF64_R_SYM (rel->r_info);
7476 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7477 r_symndx, ibfd))
7478 {
7479 if (elf_section_data (sec)->relocs != relstart)
7480 free (relstart);
7481 if (local_syms != NULL
2cdcc330 7482 && symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765
AM
7483 free (local_syms);
7484 return FALSE;
7485 }
7486
7487 if (sym_sec != NULL && sym_sec->output_section != NULL)
7488 {
7489 bfd_vma from, to;
7490 if (h != NULL)
7491 to = h->root.u.def.value;
7492 else
7493 to = sym->st_value;
7494 to += (rel->r_addend
7495 + sym_sec->output_offset
7496 + sym_sec->output_section->vma);
7497 from = (rel->r_offset
7498 + sec->output_offset
7499 + sec->output_section->vma);
5663e321
AM
7500 if (to - from + limit < 2 * limit
7501 && !(r_type == R_PPC64_PLTCALL_NOTOC
7502 && (((h ? h->other : sym->st_other)
7503 & STO_PPC64_LOCAL_MASK)
4a4e7361 7504 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7505 *tls_maskp &= ~PLT_KEEP;
7506 }
7507 }
7508 if (elf_section_data (sec)->relocs != relstart)
7509 free (relstart);
7510 }
7511
7512 if (local_syms != NULL
7513 && symtab_hdr->contents != (unsigned char *) local_syms)
7514 {
7515 if (!info->keep_memory)
7516 free (local_syms);
7517 else
7518 symtab_hdr->contents = (unsigned char *) local_syms;
7519 }
7520 }
7521
7522 return TRUE;
7523}
7524
e1918d23 7525/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7526
e1918d23 7527asection *
e7d1c40c 7528ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7529{
411e1bfb
AM
7530 struct ppc_link_hash_table *htab;
7531
411e1bfb 7532 htab = ppc_hash_table (info);
4dfe6ac6
NC
7533 if (htab == NULL)
7534 return NULL;
7535
ee67d69a
AM
7536 if (abiversion (info->output_bfd) == 1)
7537 htab->opd_abi = 1;
7538
e7d1c40c 7539 if (htab->params->no_multi_toc)
33c0ec9d
AM
7540 htab->do_multi_toc = 0;
7541 else if (!htab->do_multi_toc)
e7d1c40c 7542 htab->params->no_multi_toc = 1;
33c0ec9d 7543
8b5f1ed8
AM
7544 /* Default to --no-plt-localentry, as this option can cause problems
7545 with symbol interposition. For example, glibc libpthread.so and
7546 libc.so duplicate many pthread symbols, with a fallback
7547 implementation in libc.so. In some cases the fallback does more
7548 work than the pthread implementation. __pthread_condattr_destroy
7549 is one such symbol: the libpthread.so implementation is
7550 localentry:0 while the libc.so implementation is localentry:8.
7551 An app that "cleverly" uses dlopen to only load necessary
7552 libraries at runtime may omit loading libpthread.so when not
7553 running multi-threaded, which then results in the libc.so
7554 fallback symbols being used and ld.so complaining. Now there
7555 are workarounds in ld (see non_zero_localentry) to detect the
7556 pthread situation, but that may not be the only case where
7557 --plt-localentry can cause trouble. */
f378ab09 7558 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7559 htab->params->plt_localentry0 = 0;
d44c746a
AM
7560 if (htab->params->plt_localentry0
7561 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7562 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
7563 _bfd_error_handler
7564 (_("warning: --plt-localentry is especially dangerous without "
7565 "ld.so support to detect ABI violations"));
f378ab09 7566
3a71aa26
AM
7567 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7568 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7569 FALSE, FALSE, TRUE));
a7f2871e
AM
7570 /* Move dynamic linking info to the function descriptor sym. */
7571 if (htab->tls_get_addr != NULL)
7572 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7573 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7574 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7575 FALSE, FALSE, TRUE));
7c9cf415 7576 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
7577 {
7578 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7579
7580 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7581 FALSE, FALSE, TRUE);
7582 if (opt != NULL)
7583 func_desc_adjust (opt, info);
7584 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7585 FALSE, FALSE, TRUE);
7586 if (opt_fd != NULL
7587 && (opt_fd->root.type == bfd_link_hash_defined
7588 || opt_fd->root.type == bfd_link_hash_defweak))
7589 {
7590 /* If glibc supports an optimized __tls_get_addr call stub,
7591 signalled by the presence of __tls_get_addr_opt, and we'll
7592 be calling __tls_get_addr via a plt call stub, then
7593 make __tls_get_addr point to __tls_get_addr_opt. */
7594 tga_fd = &htab->tls_get_addr_fd->elf;
7595 if (htab->elf.dynamic_sections_created
7596 && tga_fd != NULL
7597 && (tga_fd->type == STT_FUNC
7598 || tga_fd->needs_plt)
7599 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 7600 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
7601 {
7602 struct plt_entry *ent;
7603
7604 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7605 if (ent->plt.refcount > 0)
7606 break;
7607 if (ent != NULL)
7608 {
7609 tga_fd->root.type = bfd_link_hash_indirect;
7610 tga_fd->root.u.i.link = &opt_fd->root;
7611 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 7612 opt_fd->mark = 1;
a7f2871e
AM
7613 if (opt_fd->dynindx != -1)
7614 {
7615 /* Use __tls_get_addr_opt in dynamic relocations. */
7616 opt_fd->dynindx = -1;
7617 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7618 opt_fd->dynstr_index);
7619 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7620 return NULL;
a7f2871e 7621 }
2cdcc330
AM
7622 htab->tls_get_addr_fd
7623 = (struct ppc_link_hash_entry *) opt_fd;
a7f2871e
AM
7624 tga = &htab->tls_get_addr->elf;
7625 if (opt != NULL && tga != NULL)
7626 {
7627 tga->root.type = bfd_link_hash_indirect;
7628 tga->root.u.i.link = &opt->root;
7629 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 7630 opt->mark = 1;
a7f2871e
AM
7631 _bfd_elf_link_hash_hide_symbol (info, opt,
7632 tga->forced_local);
7633 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7634 }
7635 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7636 htab->tls_get_addr_fd->is_func_descriptor = 1;
7637 if (htab->tls_get_addr != NULL)
7638 {
7639 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7640 htab->tls_get_addr->is_func = 1;
7641 }
7642 }
7643 }
7644 }
7c9cf415
AM
7645 else if (htab->params->tls_get_addr_opt < 0)
7646 htab->params->tls_get_addr_opt = 0;
a7f2871e 7647 }
33c0ec9d 7648 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7649}
8387904d 7650
3a71aa26
AM
7651/* Return TRUE iff REL is a branch reloc with a global symbol matching
7652 HASH1 or HASH2. */
8387904d 7653
3a71aa26
AM
7654static bfd_boolean
7655branch_reloc_hash_match (const bfd *ibfd,
7656 const Elf_Internal_Rela *rel,
7657 const struct ppc_link_hash_entry *hash1,
7658 const struct ppc_link_hash_entry *hash2)
7659{
7660 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7661 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7662 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7663
e054468f 7664 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7665 {
3a71aa26
AM
7666 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7667 struct elf_link_hash_entry *h;
8387904d 7668
3a71aa26 7669 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7670 h = elf_follow_link (h);
3a71aa26
AM
7671 if (h == &hash1->elf || h == &hash2->elf)
7672 return TRUE;
a48ebf4d 7673 }
3a71aa26 7674 return FALSE;
951fd09b 7675}
411e1bfb 7676
951fd09b
AM
7677/* Run through all the TLS relocs looking for optimization
7678 opportunities. The linker has been hacked (see ppc64elf.em) to do
7679 a preliminary section layout so that we know the TLS segment
7680 offsets. We can't optimize earlier because some optimizations need
7681 to know the tp offset, and we need to optimize before allocating
7682 dynamic relocations. */
7683
7684bfd_boolean
33c0ec9d 7685ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7686{
7687 bfd *ibfd;
7688 asection *sec;
7689 struct ppc_link_hash_table *htab;
663a1470 7690 unsigned char *toc_ref;
102890f0 7691 int pass;
951fd09b 7692
3cbc1e5e 7693 if (!bfd_link_executable (info))
411e1bfb
AM
7694 return TRUE;
7695
951fd09b 7696 htab = ppc_hash_table (info);
4dfe6ac6
NC
7697 if (htab == NULL)
7698 return FALSE;
7699
663a1470
AM
7700 /* Make two passes over the relocs. On the first pass, mark toc
7701 entries involved with tls relocs, and check that tls relocs
7702 involved in setting up a tls_get_addr call are indeed followed by
7703 such a call. If they are not, we can't do any tls optimization.
7704 On the second pass twiddle tls_mask flags to notify
7705 relocate_section that optimization can be done, and adjust got
7706 and plt refcounts. */
7707 toc_ref = NULL;
7708 for (pass = 0; pass < 2; ++pass)
c72f2fb2 7709 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
7710 {
7711 Elf_Internal_Sym *locsyms = NULL;
7712 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7713
102890f0
AM
7714 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7715 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7716 {
7717 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7718 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7719
102890f0
AM
7720 /* Read the relocations. */
7721 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7722 info->keep_memory);
7723 if (relstart == NULL)
2915c55b
JK
7724 {
7725 free (toc_ref);
7726 return FALSE;
7727 }
411e1bfb 7728
102890f0
AM
7729 relend = relstart + sec->reloc_count;
7730 for (rel = relstart; rel < relend; rel++)
7731 {
7732 enum elf_ppc64_reloc_type r_type;
7733 unsigned long r_symndx;
7734 struct elf_link_hash_entry *h;
7735 Elf_Internal_Sym *sym;
7736 asection *sym_sec;
f961d9dd 7737 unsigned char *tls_mask;
46e9995a 7738 unsigned int tls_set, tls_clear, tls_type = 0;
102890f0
AM
7739 bfd_vma value;
7740 bfd_boolean ok_tprel, is_local;
7741 long toc_ref_index = 0;
7742 int expecting_tls_get_addr = 0;
663a1470 7743 bfd_boolean ret = FALSE;
411e1bfb 7744
102890f0
AM
7745 r_symndx = ELF64_R_SYM (rel->r_info);
7746 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7747 r_symndx, ibfd))
7748 {
7749 err_free_rel:
7750 if (elf_section_data (sec)->relocs != relstart)
7751 free (relstart);
7752 if (toc_ref != NULL)
7753 free (toc_ref);
7754 if (locsyms != NULL
0ffa91dd 7755 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7756 != (unsigned char *) locsyms))
7757 free (locsyms);
663a1470 7758 return ret;
102890f0 7759 }
411e1bfb 7760
102890f0
AM
7761 if (h != NULL)
7762 {
766bc656
AM
7763 if (h->root.type == bfd_link_hash_defined
7764 || h->root.type == bfd_link_hash_defweak)
7765 value = h->root.u.def.value;
7766 else if (h->root.type == bfd_link_hash_undefweak)
7767 value = 0;
7768 else
663a1470
AM
7769 {
7770 found_tls_get_addr_arg = 0;
7771 continue;
7772 }
102890f0
AM
7773 }
7774 else
7775 /* Symbols referenced by TLS relocs must be of type
7776 STT_TLS. So no need for .opd local sym adjust. */
7777 value = sym->st_value;
7778
7779 ok_tprel = FALSE;
7780 is_local = FALSE;
7781 if (h == NULL
7782 || !h->def_dynamic)
7783 {
7784 is_local = TRUE;
766bc656
AM
7785 if (h != NULL
7786 && h->root.type == bfd_link_hash_undefweak)
7787 ok_tprel = TRUE;
c27b8c2a
AM
7788 else if (sym_sec != NULL
7789 && sym_sec->output_section != NULL)
766bc656
AM
7790 {
7791 value += sym_sec->output_offset;
7792 value += sym_sec->output_section->vma;
0b147428 7793 value -= htab->elf.tls_sec->vma + TP_OFFSET;
c213164a
AM
7794 /* Note that even though the prefix insns
7795 allow a 1<<33 offset we use the same test
7796 as for addis;addi. There may be a mix of
7797 pcrel and non-pcrel code and the decision
7798 to optimise is per symbol, not per TLS
7799 sequence. */
0b147428 7800 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
766bc656 7801 }
102890f0 7802 }
951fd09b 7803
102890f0 7804 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7805 /* If this section has old-style __tls_get_addr calls
7806 without marker relocs, then check that each
7807 __tls_get_addr call reloc is preceded by a reloc
7808 that conceivably belongs to the __tls_get_addr arg
7809 setup insn. If we don't find matching arg setup
7810 relocs, don't do any tls optimization. */
7811 if (pass == 0
7812 && sec->has_tls_get_addr_call
7813 && h != NULL
7814 && (h == &htab->tls_get_addr->elf
7815 || h == &htab->tls_get_addr_fd->elf)
7816 && !found_tls_get_addr_arg
7817 && is_branch_reloc (r_type))
7818 {
25f53a85 7819 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
7820 "TLS optimization disabled\n"),
7821 ibfd, sec, rel->r_offset);
7822 ret = TRUE;
7823 goto err_free_rel;
7824 }
7825
7826 found_tls_get_addr_arg = 0;
102890f0
AM
7827 switch (r_type)
7828 {
7829 case R_PPC64_GOT_TLSLD16:
7830 case R_PPC64_GOT_TLSLD16_LO:
c213164a 7831 case R_PPC64_GOT_TLSLD34:
102890f0 7832 expecting_tls_get_addr = 1;
663a1470 7833 found_tls_get_addr_arg = 1;
1a0670f3 7834 /* Fall through. */
102890f0
AM
7835
7836 case R_PPC64_GOT_TLSLD16_HI:
7837 case R_PPC64_GOT_TLSLD16_HA:
7838 /* These relocs should never be against a symbol
7839 defined in a shared lib. Leave them alone if
7840 that turns out to be the case. */
7841 if (!is_local)
7842 continue;
411e1bfb 7843
102890f0 7844 /* LD -> LE */
411e1bfb 7845 tls_set = 0;
102890f0
AM
7846 tls_clear = TLS_LD;
7847 tls_type = TLS_TLS | TLS_LD;
7848 break;
411e1bfb 7849
102890f0
AM
7850 case R_PPC64_GOT_TLSGD16:
7851 case R_PPC64_GOT_TLSGD16_LO:
c213164a 7852 case R_PPC64_GOT_TLSGD34:
102890f0 7853 expecting_tls_get_addr = 1;
663a1470 7854 found_tls_get_addr_arg = 1;
1a0670f3 7855 /* Fall through. */
102890f0
AM
7856
7857 case R_PPC64_GOT_TLSGD16_HI:
7858 case R_PPC64_GOT_TLSGD16_HA:
7859 if (ok_tprel)
7860 /* GD -> LE */
411e1bfb 7861 tls_set = 0;
102890f0
AM
7862 else
7863 /* GD -> IE */
b00a0a86 7864 tls_set = TLS_TLS | TLS_GDIE;
102890f0
AM
7865 tls_clear = TLS_GD;
7866 tls_type = TLS_TLS | TLS_GD;
7867 break;
7868
c213164a 7869 case R_PPC64_GOT_TPREL34:
102890f0
AM
7870 case R_PPC64_GOT_TPREL16_DS:
7871 case R_PPC64_GOT_TPREL16_LO_DS:
7872 case R_PPC64_GOT_TPREL16_HI:
7873 case R_PPC64_GOT_TPREL16_HA:
7874 if (ok_tprel)
7875 {
7876 /* IE -> LE */
7877 tls_set = 0;
7878 tls_clear = TLS_TPREL;
7879 tls_type = TLS_TLS | TLS_TPREL;
7880 break;
7881 }
411e1bfb
AM
7882 continue;
7883
727fc41e
AM
7884 case R_PPC64_TLSGD:
7885 case R_PPC64_TLSLD:
23cedd1d
AM
7886 if (rel + 1 < relend
7887 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7888 {
7889 if (pass != 0
2cdcc330 7890 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
7891 != R_PPC64_PLTSEQ)
7892 && (ELF64_R_TYPE (rel[1].r_info)
7893 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
7894 {
7895 r_symndx = ELF64_R_SYM (rel[1].r_info);
7896 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 7897 r_symndx, ibfd))
23cedd1d
AM
7898 goto err_free_rel;
7899 if (h != NULL)
7900 {
7901 struct plt_entry *ent = NULL;
7902
7903 for (ent = h->plt.plist;
7904 ent != NULL;
7905 ent = ent->next)
7906 if (ent->addend == rel[1].r_addend)
7907 break;
7908
7909 if (ent != NULL
7910 && ent->plt.refcount > 0)
7911 ent->plt.refcount -= 1;
7912 }
7913 }
7914 continue;
7915 }
663a1470 7916 found_tls_get_addr_arg = 1;
1a0670f3 7917 /* Fall through. */
663a1470
AM
7918
7919 case R_PPC64_TLS:
7920 case R_PPC64_TOC16:
7921 case R_PPC64_TOC16_LO:
102890f0
AM
7922 if (sym_sec == NULL || sym_sec != toc)
7923 continue;
7924
7925 /* Mark this toc entry as referenced by a TLS
7926 code sequence. We can do that now in the
7927 case of R_PPC64_TLS, and after checking for
7928 tls_get_addr for the TOC16 relocs. */
7929 if (toc_ref == NULL)
2cdcc330
AM
7930 toc_ref
7931 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
7932 if (toc_ref == NULL)
7933 goto err_free_rel;
7934
102890f0
AM
7935 if (h != NULL)
7936 value = h->root.u.def.value;
7937 else
7938 value = sym->st_value;
7939 value += rel->r_addend;
73242275
AM
7940 if (value % 8 != 0)
7941 continue;
7942 BFD_ASSERT (value < toc->size
7943 && toc->output_offset % 8 == 0);
663a1470 7944 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7945 if (r_type == R_PPC64_TLS
7946 || r_type == R_PPC64_TLSGD
7947 || r_type == R_PPC64_TLSLD)
102890f0
AM
7948 {
7949 toc_ref[toc_ref_index] = 1;
7950 continue;
7951 }
7952
7953 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7954 continue;
7955
7956 tls_set = 0;
7957 tls_clear = 0;
7958 expecting_tls_get_addr = 2;
7959 break;
7960
7961 case R_PPC64_TPREL64:
7962 if (pass == 0
7963 || sec != toc
7964 || toc_ref == NULL
663a1470 7965 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7966 continue;
7967 if (ok_tprel)
7968 {
7969 /* IE -> LE */
7970 tls_set = TLS_EXPLICIT;
7971 tls_clear = TLS_TPREL;
7972 break;
7973 }
7974 continue;
7975
7976 case R_PPC64_DTPMOD64:
7977 if (pass == 0
7978 || sec != toc
7979 || toc_ref == NULL
663a1470 7980 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7981 continue;
7982 if (rel + 1 < relend
7983 && (rel[1].r_info
7984 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7985 && rel[1].r_offset == rel->r_offset + 8)
7986 {
7987 if (ok_tprel)
7988 /* GD -> LE */
7989 tls_set = TLS_EXPLICIT | TLS_GD;
7990 else
7991 /* GD -> IE */
b00a0a86 7992 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
102890f0
AM
7993 tls_clear = TLS_GD;
7994 }
7995 else
7996 {
7997 if (!is_local)
7998 continue;
7999
8000 /* LD -> LE */
8001 tls_set = TLS_EXPLICIT;
8002 tls_clear = TLS_LD;
8003 }
8004 break;
8005
8006 default:
8007 continue;
8008 }
8009
8010 if (pass == 0)
8011 {
727fc41e
AM
8012 if (!expecting_tls_get_addr
8013 || !sec->has_tls_get_addr_call)
102890f0
AM
8014 continue;
8015
3a71aa26
AM
8016 if (rel + 1 < relend
8017 && branch_reloc_hash_match (ibfd, rel + 1,
8018 htab->tls_get_addr,
8019 htab->tls_get_addr_fd))
102890f0 8020 {
3a71aa26 8021 if (expecting_tls_get_addr == 2)
102890f0 8022 {
3a71aa26 8023 /* Check for toc tls entries. */
f961d9dd 8024 unsigned char *toc_tls;
3a71aa26
AM
8025 int retval;
8026
8027 retval = get_tls_mask (&toc_tls, NULL, NULL,
8028 &locsyms,
8029 rel, ibfd);
8030 if (retval == 0)
8031 goto err_free_rel;
663a1470
AM
8032 if (toc_tls != NULL)
8033 {
37da22e5
AM
8034 if ((*toc_tls & TLS_TLS) != 0
8035 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8036 found_tls_get_addr_arg = 1;
8037 if (retval > 1)
8038 toc_ref[toc_ref_index] = 1;
8039 }
102890f0 8040 }
3a71aa26 8041 continue;
102890f0
AM
8042 }
8043
102890f0
AM
8044 /* Uh oh, we didn't find the expected call. We
8045 could just mark this symbol to exclude it
8046 from tls optimization but it's safer to skip
663a1470 8047 the entire optimization. */
695344c0 8048 /* xgettext:c-format */
25f53a85 8049 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8050 "TLS optimization disabled\n"),
8051 ibfd, sec, rel->r_offset);
8052 ret = TRUE;
8053 goto err_free_rel;
102890f0
AM
8054 }
8055
37da22e5
AM
8056 /* If we don't have old-style __tls_get_addr calls
8057 without TLSGD/TLSLD marker relocs, and we haven't
8058 found a new-style __tls_get_addr call with a
8059 marker for this symbol, then we either have a
8060 broken object file or an -mlongcall style
8061 indirect call to __tls_get_addr without a marker.
8062 Disable optimization in this case. */
8063 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8064 && (tls_set & TLS_EXPLICIT) == 0
8065 && !sec->has_tls_get_addr_call
8066 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8067 != (TLS_TLS | TLS_MARK)))
8068 continue;
8069
23cedd1d 8070 if (expecting_tls_get_addr)
102890f0 8071 {
23cedd1d
AM
8072 struct plt_entry *ent = NULL;
8073
8074 if (htab->tls_get_addr != NULL)
8075 for (ent = htab->tls_get_addr->elf.plt.plist;
8076 ent != NULL;
8077 ent = ent->next)
8078 if (ent->addend == 0)
102890f0 8079 break;
411e1bfb 8080
23cedd1d
AM
8081 if (ent == NULL && htab->tls_get_addr_fd != NULL)
8082 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8083 ent != NULL;
8084 ent = ent->next)
8085 if (ent->addend == 0)
102890f0 8086 break;
23cedd1d
AM
8087
8088 if (ent != NULL
8089 && ent->plt.refcount > 0)
8090 ent->plt.refcount -= 1;
102890f0 8091 }
411e1bfb 8092
102890f0 8093 if (tls_clear == 0)
30038c59
AM
8094 continue;
8095
102890f0
AM
8096 if ((tls_set & TLS_EXPLICIT) == 0)
8097 {
8098 struct got_entry *ent;
411e1bfb 8099
102890f0
AM
8100 /* Adjust got entry for this reloc. */
8101 if (h != NULL)
8102 ent = h->got.glist;
8103 else
8104 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8105
102890f0
AM
8106 for (; ent != NULL; ent = ent->next)
8107 if (ent->addend == rel->r_addend
8108 && ent->owner == ibfd
8109 && ent->tls_type == tls_type)
8110 break;
8111 if (ent == NULL)
8112 abort ();
411e1bfb 8113
102890f0
AM
8114 if (tls_set == 0)
8115 {
8116 /* We managed to get rid of a got entry. */
8117 if (ent->got.refcount > 0)
8118 ent->got.refcount -= 1;
8119 }
8120 }
8121 else
8122 {
8123 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8124 we'll lose one or two dyn relocs. */
8125 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8126 NULL, h, sym))
102890f0 8127 return FALSE;
411e1bfb 8128
102890f0
AM
8129 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8130 {
8131 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8132 NULL, h, sym))
102890f0
AM
8133 return FALSE;
8134 }
8135 }
411e1bfb 8136
46e9995a 8137 *tls_mask |= tls_set & 0xff;
102890f0
AM
8138 *tls_mask &= ~tls_clear;
8139 }
8c1d1bb8 8140
102890f0
AM
8141 if (elf_section_data (sec)->relocs != relstart)
8142 free (relstart);
8143 }
411e1bfb 8144
663a1470
AM
8145 if (locsyms != NULL
8146 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8147 {
8148 if (!info->keep_memory)
8149 free (locsyms);
8150 else
8151 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8152 }
8153 }
411e1bfb 8154
663a1470
AM
8155 if (toc_ref != NULL)
8156 free (toc_ref);
9a23f96e 8157 htab->do_tls_opt = 1;
b34976b6 8158 return TRUE;
1e2f5b6e 8159}
b34976b6 8160
c5614fa4
AM
8161/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8162 the values of any global symbols in a toc section that has been
8163 edited. Globals in toc sections should be a rarity, so this function
8164 sets a flag if any are found in toc sections other than the one just
de194d85 8165 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8166
8167struct adjust_toc_info
8168{
8169 asection *toc;
8170 unsigned long *skip;
8171 bfd_boolean global_toc_syms;
8172};
8173
ba761f19
AM
8174enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8175
c5614fa4
AM
8176static bfd_boolean
8177adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8178{
8179 struct ppc_link_hash_entry *eh;
8180 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8181 unsigned long i;
c5614fa4 8182
c5614fa4
AM
8183 if (h->root.type != bfd_link_hash_defined
8184 && h->root.type != bfd_link_hash_defweak)
8185 return TRUE;
8186
8187 eh = (struct ppc_link_hash_entry *) h;
8188 if (eh->adjust_done)
8189 return TRUE;
8190
8191 if (eh->elf.root.u.def.section == toc_inf->toc)
8192 {
854b41e7
AM
8193 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8194 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8195 else
854b41e7
AM
8196 i = eh->elf.root.u.def.value >> 3;
8197
ba761f19 8198 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8199 {
4eca0228 8200 _bfd_error_handler
854b41e7
AM
8201 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8202 do
8203 ++i;
ba761f19 8204 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8205 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8206 }
854b41e7
AM
8207
8208 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8209 eh->adjust_done = 1;
8210 }
8211 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8212 toc_inf->global_toc_syms = TRUE;
8213
8214 return TRUE;
8215}
8216
39eeab25
AM
8217/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8218 on a _LO variety toc/got reloc. */
560c8763
AM
8219
8220static bfd_boolean
39eeab25 8221ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8222{
39eeab25
AM
8223 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8224 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
8225 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8226 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8227 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8228 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8229 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8230 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8231 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8232 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8233 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8234 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8235 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8236 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8237 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
8238 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8239 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8240 /* Exclude lfqu by testing reloc. If relocs are ever
8241 defined for the reduced D field in psq_lu then those
8242 will need testing too. */
8243 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8244 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8245 && (insn & 1) == 0)
8246 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8247 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8248 /* Exclude stfqu. psq_stu as above for psq_lu. */
8249 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8250 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8251 && (insn & 1) == 0));
560c8763
AM
8252}
8253
4a421c53
AM
8254/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8255 pld ra,symbol@got@pcrel
dd9b12c2 8256 load/store rt,off(ra)
4a421c53 8257 or
d4b87b1e 8258 pla ra,symbol@pcrel
dd9b12c2 8259 load/store rt,off(ra)
4a421c53 8260 may be translated to
dd9b12c2 8261 pload/pstore rt,symbol+off@pcrel
4a421c53
AM
8262 nop.
8263 This function returns true if the optimization is possible, placing
dd9b12c2 8264 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
4a421c53
AM
8265
8266 On entry to this function, the linker has already determined that
d4b87b1e 8267 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8268 while *PINSN2 is the second instruction. */
8269
8270static bfd_boolean
dd9b12c2 8271xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
4a421c53 8272{
77486630
AM
8273 uint64_t insn1 = *pinsn1;
8274 uint64_t insn2 = *pinsn2;
dd9b12c2 8275 bfd_signed_vma off;
4a421c53 8276
77486630
AM
8277 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8278 {
8279 /* Check that regs match. */
8280 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8281 return FALSE;
8282
8283 /* P8LS or PMLS form, non-pcrel. */
8284 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8285 return FALSE;
8286
8287 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8288 *pinsn2 = PNOP;
8289 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8290 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8291 return TRUE;
8292 }
8293
8294 insn2 >>= 32;
8295
4a421c53 8296 /* Check that regs match. */
77486630 8297 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
4a421c53
AM
8298 return FALSE;
8299
8300 switch ((insn2 >> 26) & 63)
8301 {
8302 default:
8303 return FALSE;
8304
8305 case 32: /* lwz */
8306 case 34: /* lbz */
8307 case 36: /* stw */
8308 case 38: /* stb */
8309 case 40: /* lhz */
8310 case 42: /* lha */
8311 case 44: /* sth */
8312 case 48: /* lfs */
8313 case 50: /* lfd */
8314 case 52: /* stfs */
8315 case 54: /* stfd */
8316 /* These are the PMLS cases, where we just need to tack a prefix
dd9b12c2 8317 on the insn. */
77486630 8318 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
4a421c53 8319 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8320 off = insn2 & 0xffff;
4a421c53
AM
8321 break;
8322
8323 case 58: /* lwa, ld */
dd9b12c2 8324 if ((insn2 & 1) != 0)
4a421c53 8325 return FALSE;
77486630 8326 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8327 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8328 | (insn2 & (31ULL << 21)));
dd9b12c2 8329 off = insn2 & 0xfffc;
4a421c53
AM
8330 break;
8331
8332 case 57: /* lxsd, lxssp */
dd9b12c2 8333 if ((insn2 & 3) < 2)
4a421c53 8334 return FALSE;
77486630 8335 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8336 | ((40ULL | (insn2 & 3)) << 26)
8337 | (insn2 & (31ULL << 21)));
dd9b12c2 8338 off = insn2 & 0xfffc;
4a421c53
AM
8339 break;
8340
8341 case 61: /* stxsd, stxssp, lxv, stxv */
8342 if ((insn2 & 3) == 0)
8343 return FALSE;
8344 else if ((insn2 & 3) >= 2)
8345 {
77486630 8346 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8347 | ((44ULL | (insn2 & 3)) << 26)
8348 | (insn2 & (31ULL << 21)));
dd9b12c2 8349 off = insn2 & 0xfffc;
4a421c53
AM
8350 }
8351 else
8352 {
77486630 8353 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8354 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8355 | (insn2 & (31ULL << 21)));
dd9b12c2 8356 off = insn2 & 0xfff0;
4a421c53
AM
8357 }
8358 break;
8359
8360 case 56: /* lq */
77486630 8361 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53 8362 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8363 off = insn2 & 0xffff;
4a421c53
AM
8364 break;
8365
8366 case 62: /* std, stq */
dd9b12c2 8367 if ((insn2 & 1) != 0)
4a421c53 8368 return FALSE;
77486630 8369 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8370 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8371 | (insn2 & (31ULL << 21)));
dd9b12c2 8372 off = insn2 & 0xfffc;
4a421c53
AM
8373 break;
8374 }
8375
77486630 8376 *pinsn1 = insn1;
4a421c53 8377 *pinsn2 = (uint64_t) NOP << 32;
dd9b12c2 8378 *poff = (off ^ 0x8000) - 0x8000;
4a421c53
AM
8379 return TRUE;
8380}
8381
c5614fa4
AM
8382/* Examine all relocs referencing .toc sections in order to remove
8383 unused .toc entries. */
8384
8385bfd_boolean
33c0ec9d 8386ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8387{
8388 bfd *ibfd;
8389 struct adjust_toc_info toc_inf;
67f0cbdb 8390 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8391
67f0cbdb 8392 htab->do_toc_opt = 1;
c5614fa4 8393 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8394 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8395 {
8396 asection *toc, *sec;
8397 Elf_Internal_Shdr *symtab_hdr;
8398 Elf_Internal_Sym *local_syms;
425b145b 8399 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8400 unsigned long *skip, *drop;
8401 unsigned char *used;
8402 unsigned char *keep, last, some_unused;
8403
854b41e7
AM
8404 if (!is_ppc64_elf (ibfd))
8405 continue;
8406
c5614fa4
AM
8407 toc = bfd_get_section_by_name (ibfd, ".toc");
8408 if (toc == NULL
92b7a70f 8409 || toc->size == 0
dbaa2011
AM
8410 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8411 || discarded_section (toc))
c5614fa4
AM
8412 continue;
8413
425b145b 8414 toc_relocs = NULL;
c5614fa4 8415 local_syms = NULL;
0ffa91dd 8416 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8417
8418 /* Look at sections dropped from the final link. */
8419 skip = NULL;
8420 relstart = NULL;
8421 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8422 {
8423 if (sec->reloc_count == 0
dbaa2011 8424 || !discarded_section (sec)
c5614fa4
AM
8425 || get_opd_info (sec)
8426 || (sec->flags & SEC_ALLOC) == 0
8427 || (sec->flags & SEC_DEBUGGING) != 0)
8428 continue;
8429
8430 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8431 if (relstart == NULL)
8432 goto error_ret;
8433
8434 /* Run through the relocs to see which toc entries might be
8435 unused. */
8436 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8437 {
8438 enum elf_ppc64_reloc_type r_type;
8439 unsigned long r_symndx;
8440 asection *sym_sec;
8441 struct elf_link_hash_entry *h;
8442 Elf_Internal_Sym *sym;
8443 bfd_vma val;
8444
8445 r_type = ELF64_R_TYPE (rel->r_info);
8446 switch (r_type)
8447 {
8448 default:
8449 continue;
8450
8451 case R_PPC64_TOC16:
8452 case R_PPC64_TOC16_LO:
8453 case R_PPC64_TOC16_HI:
8454 case R_PPC64_TOC16_HA:
8455 case R_PPC64_TOC16_DS:
8456 case R_PPC64_TOC16_LO_DS:
8457 break;
8458 }
8459
8460 r_symndx = ELF64_R_SYM (rel->r_info);
8461 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8462 r_symndx, ibfd))
8463 goto error_ret;
8464
8465 if (sym_sec != toc)
8466 continue;
8467
8468 if (h != NULL)
8469 val = h->root.u.def.value;
8470 else
8471 val = sym->st_value;
8472 val += rel->r_addend;
8473
8474 if (val >= toc->size)
8475 continue;
8476
8477 /* Anything in the toc ought to be aligned to 8 bytes.
8478 If not, don't mark as unused. */
8479 if (val & 7)
8480 continue;
8481
8482 if (skip == NULL)
8483 {
854b41e7 8484 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8485 if (skip == NULL)
8486 goto error_ret;
8487 }
8488
ba761f19 8489 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8490 }
8491
8492 if (elf_section_data (sec)->relocs != relstart)
8493 free (relstart);
8494 }
8495
ba761f19
AM
8496 /* For largetoc loads of address constants, we can convert
8497 . addis rx,2,addr@got@ha
8498 . ld ry,addr@got@l(rx)
8499 to
8500 . addis rx,2,addr@toc@ha
8501 . addi ry,rx,addr@toc@l
8502 when addr is within 2G of the toc pointer. This then means
8503 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8504
ba761f19
AM
8505 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8506 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8507 && toc->reloc_count != 0)
8508 {
8509 /* Read toc relocs. */
425b145b
AM
8510 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8511 info->keep_memory);
8512 if (toc_relocs == NULL)
ba761f19
AM
8513 goto error_ret;
8514
425b145b 8515 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8516 {
8517 enum elf_ppc64_reloc_type r_type;
8518 unsigned long r_symndx;
8519 asection *sym_sec;
8520 struct elf_link_hash_entry *h;
8521 Elf_Internal_Sym *sym;
8522 bfd_vma val, addr;
8523
8524 r_type = ELF64_R_TYPE (rel->r_info);
8525 if (r_type != R_PPC64_ADDR64)
8526 continue;
8527
8528 r_symndx = ELF64_R_SYM (rel->r_info);
8529 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8530 r_symndx, ibfd))
8531 goto error_ret;
8532
425b145b 8533 if (sym_sec == NULL
c27b8c2a 8534 || sym_sec->output_section == NULL
dbaa2011 8535 || discarded_section (sym_sec))
425b145b
AM
8536 continue;
8537
afe397ea 8538 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8539 continue;
8540
8541 if (h != NULL)
bddc25c9
AM
8542 {
8543 if (h->type == STT_GNU_IFUNC)
8544 continue;
8545 val = h->root.u.def.value;
8546 }
ba761f19 8547 else
bddc25c9
AM
8548 {
8549 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8550 continue;
8551 val = sym->st_value;
8552 }
ba761f19
AM
8553 val += rel->r_addend;
8554 val += sym_sec->output_section->vma + sym_sec->output_offset;
8555
8556 /* We don't yet know the exact toc pointer value, but we
8557 know it will be somewhere in the toc section. Don't
8558 optimize if the difference from any possible toc
8559 pointer is outside [ff..f80008000, 7fff7fff]. */
8560 addr = toc->output_section->vma + TOC_BASE_OFF;
8561 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8562 continue;
8563
8564 addr = toc->output_section->vma + toc->output_section->rawsize;
8565 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8566 continue;
8567
8568 if (skip == NULL)
8569 {
8570 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8571 if (skip == NULL)
8572 goto error_ret;
8573 }
8574
8575 skip[rel->r_offset >> 3]
425b145b 8576 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8577 }
ba761f19
AM
8578 }
8579
c5614fa4
AM
8580 if (skip == NULL)
8581 continue;
8582
8583 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8584 if (used == NULL)
8585 {
8586 error_ret:
8587 if (local_syms != NULL
8588 && symtab_hdr->contents != (unsigned char *) local_syms)
8589 free (local_syms);
8590 if (sec != NULL
8591 && relstart != NULL
8592 && elf_section_data (sec)->relocs != relstart)
8593 free (relstart);
425b145b
AM
8594 if (toc_relocs != NULL
8595 && elf_section_data (toc)->relocs != toc_relocs)
8596 free (toc_relocs);
c5614fa4
AM
8597 if (skip != NULL)
8598 free (skip);
8599 return FALSE;
8600 }
8601
30038c59
AM
8602 /* Now check all kept sections that might reference the toc.
8603 Check the toc itself last. */
8604 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8605 : ibfd->sections);
c5614fa4 8606 sec != NULL;
c5614fa4 8607 sec = (sec == toc ? NULL
c5614fa4 8608 : sec->next == NULL ? toc
30038c59 8609 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8610 : sec->next))
8611 {
8612 int repeat;
8613
8614 if (sec->reloc_count == 0
dbaa2011 8615 || discarded_section (sec)
c5614fa4
AM
8616 || get_opd_info (sec)
8617 || (sec->flags & SEC_ALLOC) == 0
8618 || (sec->flags & SEC_DEBUGGING) != 0)
8619 continue;
8620
854b41e7
AM
8621 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8622 info->keep_memory);
c5614fa4 8623 if (relstart == NULL)
2915c55b
JK
8624 {
8625 free (used);
8626 goto error_ret;
8627 }
c5614fa4
AM
8628
8629 /* Mark toc entries referenced as used. */
c5614fa4 8630 do
d4f1ee75
AM
8631 {
8632 repeat = 0;
8633 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8634 {
8635 enum elf_ppc64_reloc_type r_type;
8636 unsigned long r_symndx;
8637 asection *sym_sec;
8638 struct elf_link_hash_entry *h;
8639 Elf_Internal_Sym *sym;
8640 bfd_vma val;
98528052 8641
d4f1ee75 8642 r_type = ELF64_R_TYPE (rel->r_info);
d4f1ee75
AM
8643 switch (r_type)
8644 {
8645 case R_PPC64_TOC16:
8646 case R_PPC64_TOC16_LO:
8647 case R_PPC64_TOC16_HI:
8648 case R_PPC64_TOC16_HA:
8649 case R_PPC64_TOC16_DS:
8650 case R_PPC64_TOC16_LO_DS:
8651 /* In case we're taking addresses of toc entries. */
8652 case R_PPC64_ADDR64:
8653 break;
c5614fa4 8654
d4f1ee75
AM
8655 default:
8656 continue;
8657 }
c5614fa4 8658
d4f1ee75
AM
8659 r_symndx = ELF64_R_SYM (rel->r_info);
8660 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8661 r_symndx, ibfd))
8662 {
8663 free (used);
8664 goto error_ret;
8665 }
c5614fa4 8666
d4f1ee75
AM
8667 if (sym_sec != toc)
8668 continue;
c5614fa4 8669
d4f1ee75
AM
8670 if (h != NULL)
8671 val = h->root.u.def.value;
8672 else
8673 val = sym->st_value;
8674 val += rel->r_addend;
ba761f19 8675
d4f1ee75
AM
8676 if (val >= toc->size)
8677 continue;
ba761f19 8678
d4f1ee75
AM
8679 if ((skip[val >> 3] & can_optimize) != 0)
8680 {
8681 bfd_vma off;
8682 unsigned char opc;
8683
8684 switch (r_type)
8685 {
8686 case R_PPC64_TOC16_HA:
ba761f19 8687 break;
ba761f19 8688
d4f1ee75
AM
8689 case R_PPC64_TOC16_LO_DS:
8690 off = rel->r_offset;
8691 off += (bfd_big_endian (ibfd) ? -2 : 3);
8692 if (!bfd_get_section_contents (ibfd, sec, &opc,
8693 off, 1))
8694 {
8695 free (used);
8696 goto error_ret;
8697 }
8698 if ((opc & (0x3f << 2)) == (58u << 2))
8699 break;
1a0670f3 8700 /* Fall through. */
ba761f19 8701
d4f1ee75
AM
8702 default:
8703 /* Wrong sort of reloc, or not a ld. We may
8704 as well clear ref_from_discarded too. */
8705 skip[val >> 3] = 0;
8706 }
8707 }
8708
8709 if (sec != toc)
8710 used[val >> 3] = 1;
8711 /* For the toc section, we only mark as used if this
8712 entry itself isn't unused. */
8713 else if ((used[rel->r_offset >> 3]
8714 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8715 && !used[val >> 3])
8716 {
8717 /* Do all the relocs again, to catch reference
8718 chains. */
8719 repeat = 1;
8720 used[val >> 3] = 1;
8721 }
8722 }
8723 }
c5614fa4 8724 while (repeat);
854b41e7
AM
8725
8726 if (elf_section_data (sec)->relocs != relstart)
8727 free (relstart);
c5614fa4
AM
8728 }
8729
8730 /* Merge the used and skip arrays. Assume that TOC
8731 doublewords not appearing as either used or unused belong
de194d85 8732 to an entry more than one doubleword in size. */
c5614fa4
AM
8733 for (drop = skip, keep = used, last = 0, some_unused = 0;
8734 drop < skip + (toc->size + 7) / 8;
8735 ++drop, ++keep)
8736 {
8737 if (*keep)
8738 {
ba761f19
AM
8739 *drop &= ~ref_from_discarded;
8740 if ((*drop & can_optimize) != 0)
8741 some_unused = 1;
c5614fa4
AM
8742 last = 0;
8743 }
b140b010 8744 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8745 {
8746 some_unused = 1;
ba761f19 8747 last = ref_from_discarded;
c5614fa4
AM
8748 }
8749 else
8750 *drop = last;
8751 }
8752
8753 free (used);
8754
8755 if (some_unused)
8756 {
8757 bfd_byte *contents, *src;
8758 unsigned long off;
d62b3684 8759 Elf_Internal_Sym *sym;
ba761f19 8760 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8761
8762 /* Shuffle the toc contents, and at the same time convert the
8763 skip array from booleans into offsets. */
8764 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8765 goto error_ret;
8766
8767 elf_section_data (toc)->this_hdr.contents = contents;
8768
8769 for (src = contents, off = 0, drop = skip;
8770 src < contents + toc->size;
8771 src += 8, ++drop)
8772 {
ba761f19
AM
8773 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8774 off += 8;
c5614fa4
AM
8775 else if (off != 0)
8776 {
8777 *drop = off;
8778 memcpy (src - off, src, 8);
8779 }
8780 }
854b41e7 8781 *drop = off;
c5614fa4
AM
8782 toc->rawsize = toc->size;
8783 toc->size = src - contents - off;
8784
ba761f19
AM
8785 /* Adjust addends for relocs against the toc section sym,
8786 and optimize any accesses we can. */
c5614fa4
AM
8787 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8788 {
8789 if (sec->reloc_count == 0
dbaa2011 8790 || discarded_section (sec))
c5614fa4
AM
8791 continue;
8792
8793 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8794 info->keep_memory);
c5614fa4
AM
8795 if (relstart == NULL)
8796 goto error_ret;
8797
8798 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8799 {
8800 enum elf_ppc64_reloc_type r_type;
8801 unsigned long r_symndx;
8802 asection *sym_sec;
8803 struct elf_link_hash_entry *h;
854b41e7 8804 bfd_vma val;
c5614fa4
AM
8805
8806 r_type = ELF64_R_TYPE (rel->r_info);
8807 switch (r_type)
8808 {
8809 default:
8810 continue;
8811
8812 case R_PPC64_TOC16:
8813 case R_PPC64_TOC16_LO:
8814 case R_PPC64_TOC16_HI:
8815 case R_PPC64_TOC16_HA:
8816 case R_PPC64_TOC16_DS:
8817 case R_PPC64_TOC16_LO_DS:
8818 case R_PPC64_ADDR64:
8819 break;
8820 }
8821
8822 r_symndx = ELF64_R_SYM (rel->r_info);
8823 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8824 r_symndx, ibfd))
8825 goto error_ret;
8826
ba761f19 8827 if (sym_sec != toc)
c5614fa4
AM
8828 continue;
8829
ba761f19
AM
8830 if (h != NULL)
8831 val = h->root.u.def.value;
8832 else
8833 {
8834 val = sym->st_value;
8835 if (val != 0)
8836 local_toc_syms = TRUE;
8837 }
8838
8839 val += rel->r_addend;
854b41e7
AM
8840
8841 if (val > toc->rawsize)
8842 val = toc->rawsize;
ba761f19
AM
8843 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8844 continue;
8845 else if ((skip[val >> 3] & can_optimize) != 0)
8846 {
8847 Elf_Internal_Rela *tocrel
425b145b 8848 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8849 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8850
8851 switch (r_type)
8852 {
8853 case R_PPC64_TOC16_HA:
8854 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8855 break;
8856
8857 case R_PPC64_TOC16_LO_DS:
8858 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8859 break;
8860
8861 default:
28942f62
AM
8862 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8863 ppc_howto_init ();
b140b010 8864 info->callbacks->einfo
695344c0 8865 /* xgettext:c-format */
174d0a74 8866 (_("%H: %s references "
b140b010
AM
8867 "optimized away TOC entry\n"),
8868 ibfd, sec, rel->r_offset,
8869 ppc64_elf_howto_table[r_type]->name);
8870 bfd_set_error (bfd_error_bad_value);
8871 goto error_ret;
ba761f19
AM
8872 }
8873 rel->r_addend = tocrel->r_addend;
8874 elf_section_data (sec)->relocs = relstart;
8875 continue;
8876 }
8877
8878 if (h != NULL || sym->st_value != 0)
8879 continue;
854b41e7
AM
8880
8881 rel->r_addend -= skip[val >> 3];
8882 elf_section_data (sec)->relocs = relstart;
c5614fa4 8883 }
854b41e7
AM
8884
8885 if (elf_section_data (sec)->relocs != relstart)
8886 free (relstart);
c5614fa4
AM
8887 }
8888
8889 /* We shouldn't have local or global symbols defined in the TOC,
8890 but handle them anyway. */
df22d223
AM
8891 if (local_syms != NULL)
8892 for (sym = local_syms;
8893 sym < local_syms + symtab_hdr->sh_info;
8894 ++sym)
8895 if (sym->st_value != 0
8896 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8897 {
8898 unsigned long i;
854b41e7 8899
df22d223
AM
8900 if (sym->st_value > toc->rawsize)
8901 i = toc->rawsize >> 3;
8902 else
8903 i = sym->st_value >> 3;
854b41e7 8904
df22d223
AM
8905 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8906 {
8907 if (local_toc_syms)
4eca0228 8908 _bfd_error_handler
df22d223
AM
8909 (_("%s defined on removed toc entry"),
8910 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8911 do
8912 ++i;
8913 while ((skip[i] & (ref_from_discarded | can_optimize)));
8914 sym->st_value = (bfd_vma) i << 3;
8915 }
d62b3684 8916
df22d223
AM
8917 sym->st_value -= skip[i];
8918 symtab_hdr->contents = (unsigned char *) local_syms;
8919 }
c5614fa4 8920
854b41e7 8921 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8922 if (toc_inf.global_toc_syms)
8923 {
8924 toc_inf.toc = toc;
8925 toc_inf.skip = skip;
8926 toc_inf.global_toc_syms = FALSE;
8927 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8928 &toc_inf);
8929 }
854b41e7
AM
8930
8931 if (toc->reloc_count != 0)
8932 {
d4730f92 8933 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8934 Elf_Internal_Rela *wrel;
8935 bfd_size_type sz;
8936
854b41e7 8937 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8938 if (toc_relocs == NULL)
8939 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8940 info->keep_memory);
8941 if (toc_relocs == NULL)
8942 goto error_ret;
8943
425b145b
AM
8944 wrel = toc_relocs;
8945 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8946 if ((skip[rel->r_offset >> 3]
8947 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8948 {
8949 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8950 wrel->r_info = rel->r_info;
8951 wrel->r_addend = rel->r_addend;
8952 ++wrel;
8953 }
8954 else if (!dec_dynrel_count (rel->r_info, toc, info,
8955 &local_syms, NULL, NULL))
8956 goto error_ret;
8957
425b145b
AM
8958 elf_section_data (toc)->relocs = toc_relocs;
8959 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8960 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8961 sz = rel_hdr->sh_entsize;
8962 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8963 }
c5614fa4 8964 }
28be611c
AM
8965 else if (toc_relocs != NULL
8966 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8967 free (toc_relocs);
c5614fa4
AM
8968
8969 if (local_syms != NULL
8970 && symtab_hdr->contents != (unsigned char *) local_syms)
8971 {
8972 if (!info->keep_memory)
8973 free (local_syms);
8974 else
8975 symtab_hdr->contents = (unsigned char *) local_syms;
8976 }
8977 free (skip);
8978 }
8979
066f4018 8980 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
8981 a GOT relative or PC relative access, possibly reducing the
8982 number of GOT entries. */
066f4018
AM
8983 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8984 {
8985 asection *sec;
8986 Elf_Internal_Shdr *symtab_hdr;
8987 Elf_Internal_Sym *local_syms;
8988 Elf_Internal_Rela *relstart, *rel;
8989 bfd_vma got;
8990
8991 if (!is_ppc64_elf (ibfd))
8992 continue;
8993
903b777d 8994 if (!ppc64_elf_tdata (ibfd)->has_optrel)
066f4018
AM
8995 continue;
8996
8997 sec = ppc64_elf_tdata (ibfd)->got;
903b777d
AM
8998 got = 0;
8999 if (sec != NULL)
9000 got = sec->output_section->vma + sec->output_offset + 0x8000;
066f4018
AM
9001
9002 local_syms = NULL;
9003 symtab_hdr = &elf_symtab_hdr (ibfd);
9004
9005 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9006 {
9007 if (sec->reloc_count == 0
903b777d 9008 || !ppc64_elf_section_data (sec)->has_optrel
066f4018
AM
9009 || discarded_section (sec))
9010 continue;
9011
9012 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9013 info->keep_memory);
9014 if (relstart == NULL)
9015 {
9016 got_error_ret:
9017 if (local_syms != NULL
9018 && symtab_hdr->contents != (unsigned char *) local_syms)
9019 free (local_syms);
9020 if (sec != NULL
9021 && relstart != NULL
9022 && elf_section_data (sec)->relocs != relstart)
9023 free (relstart);
9024 return FALSE;
9025 }
9026
9027 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9028 {
9029 enum elf_ppc64_reloc_type r_type;
9030 unsigned long r_symndx;
9031 Elf_Internal_Sym *sym;
9032 asection *sym_sec;
9033 struct elf_link_hash_entry *h;
9034 struct got_entry *ent;
133a1f60 9035 bfd_vma val, pc;
4a421c53 9036 unsigned char buf[8];
066f4018 9037 unsigned int insn;
903b777d 9038 enum {no_check, check_lo, check_ha} insn_check;
066f4018
AM
9039
9040 r_type = ELF64_R_TYPE (rel->r_info);
903b777d
AM
9041 switch (r_type)
9042 {
9043 default:
9044 insn_check = no_check;
9045 break;
9046
9047 case R_PPC64_PLT16_HA:
9048 case R_PPC64_GOT_TLSLD16_HA:
9049 case R_PPC64_GOT_TLSGD16_HA:
9050 case R_PPC64_GOT_TPREL16_HA:
9051 case R_PPC64_GOT_DTPREL16_HA:
9052 case R_PPC64_GOT16_HA:
9053 case R_PPC64_TOC16_HA:
9054 insn_check = check_ha;
9055 break;
9056
9057 case R_PPC64_PLT16_LO:
9058 case R_PPC64_PLT16_LO_DS:
9059 case R_PPC64_GOT_TLSLD16_LO:
9060 case R_PPC64_GOT_TLSGD16_LO:
9061 case R_PPC64_GOT_TPREL16_LO_DS:
9062 case R_PPC64_GOT_DTPREL16_LO_DS:
9063 case R_PPC64_GOT16_LO:
9064 case R_PPC64_GOT16_LO_DS:
9065 case R_PPC64_TOC16_LO:
9066 case R_PPC64_TOC16_LO_DS:
9067 insn_check = check_lo;
9068 break;
9069 }
9070
9071 if (insn_check != no_check)
9072 {
9073 bfd_vma off = rel->r_offset & ~3;
9074
9075 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9076 goto got_error_ret;
9077
9078 insn = bfd_get_32 (ibfd, buf);
9079 if (insn_check == check_lo
9080 ? !ok_lo_toc_insn (insn, r_type)
9081 : ((insn & ((0x3f << 26) | 0x1f << 16))
9082 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9083 {
9084 char str[12];
9085
9086 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9087 sprintf (str, "%#08x", insn);
9088 info->callbacks->einfo
9089 /* xgettext:c-format */
9090 (_("%H: got/toc optimization is not supported for"
9091 " %s instruction\n"),
9092 ibfd, sec, rel->r_offset & ~3, str);
9093 continue;
9094 }
9095 }
9096
066f4018
AM
9097 switch (r_type)
9098 {
bb22a418
AM
9099 /* Note that we don't delete GOT entries for
9100 R_PPC64_GOT16_DS since we'd need a lot more
9101 analysis. For starters, the preliminary layout is
9102 before the GOT, PLT, dynamic sections and stubs are
9103 laid out. Then we'd need to allow for changes in
9104 distance between sections caused by alignment. */
066f4018
AM
9105 default:
9106 continue;
9107
066f4018
AM
9108 case R_PPC64_GOT16_HA:
9109 case R_PPC64_GOT16_LO_DS:
4a421c53 9110 case R_PPC64_GOT_PCREL34:
066f4018
AM
9111 break;
9112 }
9113
9114 r_symndx = ELF64_R_SYM (rel->r_info);
9115 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9116 r_symndx, ibfd))
9117 goto got_error_ret;
9118
6d5554a6
AM
9119 if (sym_sec == NULL
9120 || sym_sec->output_section == NULL
9121 || discarded_section (sym_sec))
9122 continue;
9123
066f4018
AM
9124 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9125 continue;
9126
9127 if (h != NULL)
9128 val = h->root.u.def.value;
9129 else
9130 val = sym->st_value;
133a1f60 9131 val += rel->r_addend;
066f4018
AM
9132 val += sym_sec->output_section->vma + sym_sec->output_offset;
9133
bb22a418
AM
9134/* Fudge factor to allow for the fact that the preliminary layout
9135 isn't exact. Reduce limits by this factor. */
9136#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9137
066f4018
AM
9138 switch (r_type)
9139 {
9140 default:
9141 continue;
9142
066f4018 9143 case R_PPC64_GOT16_HA:
bb22a418
AM
9144 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9145 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9146 continue;
9147
9148 if (!bfd_get_section_contents (ibfd, sec, buf,
9149 rel->r_offset & ~3, 4))
9150 goto got_error_ret;
9151 insn = bfd_get_32 (ibfd, buf);
9152 if (((insn & ((0x3f << 26) | 0x1f << 16))
9153 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9154 continue;
9155 break;
9156
9157 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9158 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9159 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9160 continue;
9161 if (!bfd_get_section_contents (ibfd, sec, buf,
9162 rel->r_offset & ~3, 4))
9163 goto got_error_ret;
9164 insn = bfd_get_32 (ibfd, buf);
9165 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9166 continue;
9167 break;
4a421c53
AM
9168
9169 case R_PPC64_GOT_PCREL34:
9170 pc = rel->r_offset;
9171 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9172 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9173 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9174 continue;
9175 if (!bfd_get_section_contents (ibfd, sec, buf,
9176 rel->r_offset & ~3, 8))
9177 goto got_error_ret;
9178 insn = bfd_get_32 (ibfd, buf);
9179 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9180 continue;
9181 insn = bfd_get_32 (ibfd, buf + 4);
9182 if ((insn & (0x3f << 26)) != 57u << 26)
9183 continue;
9184 break;
066f4018 9185 }
bb22a418 9186#undef LIMIT_ADJUST
066f4018
AM
9187
9188 if (h != NULL)
9189 ent = h->got.glist;
9190 else
9191 {
9192 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9193 ent = local_got_ents[r_symndx];
9194 }
9195 for (; ent != NULL; ent = ent->next)
133a1f60 9196 if (ent->addend == rel->r_addend
066f4018
AM
9197 && ent->owner == ibfd
9198 && ent->tls_type == 0)
9199 break;
9200 BFD_ASSERT (ent && ent->got.refcount > 0);
9201 ent->got.refcount -= 1;
9202 }
9203
9204 if (elf_section_data (sec)->relocs != relstart)
9205 free (relstart);
9206 }
9207
9208 if (local_syms != NULL
9209 && symtab_hdr->contents != (unsigned char *) local_syms)
9210 {
9211 if (!info->keep_memory)
9212 free (local_syms);
9213 else
9214 symtab_hdr->contents = (unsigned char *) local_syms;
9215 }
9216 }
9217
c5614fa4
AM
9218 return TRUE;
9219}
9220
1bbe0902
AM
9221/* Return true iff input section I references the TOC using
9222 instructions limited to +/-32k offsets. */
9223
9224bfd_boolean
9225ppc64_elf_has_small_toc_reloc (asection *i)
9226{
9227 return (is_ppc64_elf (i->owner)
9228 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9229}
9230
927be08e
AM
9231/* Allocate space for one GOT entry. */
9232
9233static void
9234allocate_got (struct elf_link_hash_entry *h,
9235 struct bfd_link_info *info,
9236 struct got_entry *gent)
9237{
9238 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9239 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9240 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9241 ? 16 : 8);
9242 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9243 ? 2 : 1) * sizeof (Elf64_External_Rela);
9244 asection *got = ppc64_elf_tdata (gent->owner)->got;
9245
9246 gent->got.offset = got->size;
9247 got->size += entsize;
9248
19e08130 9249 if (h->type == STT_GNU_IFUNC)
927be08e 9250 {
33e44f2e 9251 htab->elf.irelplt->size += rentsize;
19e08130 9252 htab->got_reli_size += rentsize;
927be08e 9253 }
f15d0b54
AM
9254 else if (((bfd_link_pic (info)
9255 && !((gent->tls_type & TLS_TPREL) != 0
9256 && bfd_link_executable (info)
9257 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9258 || (htab->elf.dynamic_sections_created
9259 && h->dynindx != -1
9260 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9261 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9262 {
19e08130 9263 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9264 relgot->size += rentsize;
927be08e
AM
9265 }
9266}
9267
7865406b
AM
9268/* This function merges got entries in the same toc group. */
9269
9270static void
9271merge_got_entries (struct got_entry **pent)
9272{
9273 struct got_entry *ent, *ent2;
9274
9275 for (ent = *pent; ent != NULL; ent = ent->next)
9276 if (!ent->is_indirect)
9277 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9278 if (!ent2->is_indirect
9279 && ent2->addend == ent->addend
9280 && ent2->tls_type == ent->tls_type
9281 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9282 {
9283 ent2->is_indirect = TRUE;
9284 ent2->got.ent = ent;
9285 }
9286}
9287
46434633 9288/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9289
9290static bfd_boolean
46434633
AM
9291ensure_undef_dynamic (struct bfd_link_info *info,
9292 struct elf_link_hash_entry *h)
f0158f44
AM
9293{
9294 struct elf_link_hash_table *htab = elf_hash_table (info);
9295
9296 if (htab->dynamic_sections_created
46434633
AM
9297 && ((info->dynamic_undefined_weak != 0
9298 && h->root.type == bfd_link_hash_undefweak)
9299 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9300 && h->dynindx == -1
9301 && !h->forced_local
9302 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9303 return bfd_elf_link_record_dynamic_symbol (info, h);
9304 return TRUE;
9305}
9306
65f38f15
AM
9307/* Allocate space in .plt, .got and associated reloc sections for
9308 dynamic relocs. */
5bd4f169 9309
b34976b6 9310static bfd_boolean
4ce794b7 9311allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9312{
65f38f15
AM
9313 struct bfd_link_info *info;
9314 struct ppc_link_hash_table *htab;
5bd4f169 9315 asection *s;
65f38f15 9316 struct ppc_link_hash_entry *eh;
0b8bcf0d 9317 struct got_entry **pgent, *gent;
5bd4f169 9318
e92d460e 9319 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9320 return TRUE;
5bd4f169 9321
65f38f15
AM
9322 info = (struct bfd_link_info *) inf;
9323 htab = ppc_hash_table (info);
4dfe6ac6
NC
9324 if (htab == NULL)
9325 return FALSE;
5bd4f169 9326
951fd09b
AM
9327 eh = (struct ppc_link_hash_entry *) h;
9328 /* Run through the TLS GD got entries first if we're changing them
9329 to TPREL. */
b00a0a86 9330 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
951fd09b
AM
9331 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9332 if (gent->got.refcount > 0
9333 && (gent->tls_type & TLS_GD) != 0)
9334 {
9335 /* This was a GD entry that has been converted to TPREL. If
9336 there happens to be a TPREL entry we can use that one. */
9337 struct got_entry *ent;
9338 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9339 if (ent->got.refcount > 0
9340 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9341 && ent->addend == gent->addend
9342 && ent->owner == gent->owner)
951fd09b
AM
9343 {
9344 gent->got.refcount = 0;
9345 break;
9346 }
9347
9348 /* If not, then we'll be using our own TPREL entry. */
9349 if (gent->got.refcount != 0)
9350 gent->tls_type = TLS_TLS | TLS_TPREL;
9351 }
9352
7865406b
AM
9353 /* Remove any list entry that won't generate a word in the GOT before
9354 we call merge_got_entries. Otherwise we risk merging to empty
9355 entries. */
0b8bcf0d
AM
9356 pgent = &h->got.glist;
9357 while ((gent = *pgent) != NULL)
411e1bfb 9358 if (gent->got.refcount > 0)
7865406b
AM
9359 {
9360 if ((gent->tls_type & TLS_LD) != 0
9361 && !h->def_dynamic)
9362 {
9363 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9364 *pgent = gent->next;
9365 }
9366 else
9367 pgent = &gent->next;
9368 }
9369 else
9370 *pgent = gent->next;
9371
9372 if (!htab->do_multi_toc)
9373 merge_got_entries (&h->got.glist);
9374
9375 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9376 if (!gent->is_indirect)
411e1bfb 9377 {
46434633
AM
9378 /* Make sure this symbol is output as a dynamic symbol. */
9379 if (!ensure_undef_dynamic (info, h))
f0158f44 9380 return FALSE;
65f38f15 9381
0c8d6e5c 9382 if (!is_ppc64_elf (gent->owner))
927be08e 9383 abort ();
0ffa91dd 9384
927be08e 9385 allocate_got (h, info, gent);
411e1bfb 9386 }
65f38f15 9387
954b63d4
AM
9388 /* If no dynamic sections we can't have dynamic relocs, except for
9389 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9390 if (!htab->elf.dynamic_sections_created
9391 && h->type != STT_GNU_IFUNC)
9392 eh->dyn_relocs = NULL;
9393
529fe20e
AM
9394 /* Discard relocs on undefined symbols that must be local. */
9395 else if (h->root.type == bfd_link_hash_undefined
9396 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9397 eh->dyn_relocs = NULL;
9398
954b63d4
AM
9399 /* Also discard relocs on undefined weak syms with non-default
9400 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9401 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
9402 eh->dyn_relocs = NULL;
9403
8a2058b5 9404 if (eh->dyn_relocs != NULL)
65f38f15 9405 {
8a2058b5
AM
9406 struct elf_dyn_relocs *p, **pp;
9407
57e7d118
AM
9408 /* In the shared -Bsymbolic case, discard space allocated for
9409 dynamic pc-relative relocs against symbols which turn out to
9410 be defined in regular objects. For the normal shared case,
9411 discard space for relocs that have become local due to symbol
9412 visibility changes. */
9413
9414 if (bfd_link_pic (info))
65f38f15 9415 {
57e7d118
AM
9416 /* Relocs that use pc_count are those that appear on a call
9417 insn, or certain REL relocs (see must_be_dyn_reloc) that
9418 can be generated via assembly. We want calls to
9419 protected symbols to resolve directly to the function
9420 rather than going via the plt. If people want function
9421 pointer comparisons to work as expected then they should
9422 avoid writing weird assembly. */
9423 if (SYMBOL_CALLS_LOCAL (info, h))
9424 {
57e7d118
AM
9425 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9426 {
9427 p->count -= p->pc_count;
9428 p->pc_count = 0;
9429 if (p->count == 0)
9430 *pp = p->next;
9431 else
9432 pp = &p->next;
9433 }
9434 }
65f38f15 9435
954b63d4 9436 if (eh->dyn_relocs != NULL)
5bd4f169 9437 {
46434633
AM
9438 /* Make sure this symbol is output as a dynamic symbol. */
9439 if (!ensure_undef_dynamic (info, h))
f0158f44 9440 return FALSE;
5bd4f169 9441 }
65f38f15 9442 }
529fe20e 9443 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 9444 {
8a2058b5 9445 /* For the non-pic case, discard space for relocs against
57e7d118
AM
9446 symbols which turn out to need copy relocs or are not
9447 dynamic. */
529fe20e
AM
9448 if (h->dynamic_adjusted
9449 && !h->def_regular
9450 && !ELF_COMMON_DEF_P (h))
f0158f44 9451 {
46434633
AM
9452 /* Make sure this symbol is output as a dynamic symbol. */
9453 if (!ensure_undef_dynamic (info, h))
f0158f44 9454 return FALSE;
dfbb6ac9 9455
f0158f44
AM
9456 if (h->dynindx == -1)
9457 eh->dyn_relocs = NULL;
9458 }
9459 else
8a2058b5 9460 eh->dyn_relocs = NULL;
57e7d118
AM
9461 }
9462
9463 /* Finally, allocate space. */
9464 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9465 {
9466 asection *sreloc = elf_section_data (p->sec)->sreloc;
9467 if (eh->elf.type == STT_GNU_IFUNC)
9468 sreloc = htab->elf.irelplt;
9469 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9470 }
65f38f15 9471 }
57e7d118 9472
2d7ad24e
AM
9473 /* We might need a PLT entry when the symbol
9474 a) is dynamic, or
9475 b) is an ifunc, or
9476 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9477 d) has plt16 relocs and we are linking statically. */
9478 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9479 || h->type == STT_GNU_IFUNC
9480 || (h->needs_plt && h->dynamic_adjusted)
9481 || (h->needs_plt
9482 && h->def_regular
9483 && !htab->elf.dynamic_sections_created
3e04d765 9484 && !htab->can_convert_all_inline_plt
2d7ad24e
AM
9485 && (((struct ppc_link_hash_entry *) h)->tls_mask
9486 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 9487 {
57e7d118
AM
9488 struct plt_entry *pent;
9489 bfd_boolean doneone = FALSE;
9490 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9491 if (pent->plt.refcount > 0)
9492 {
9493 if (!htab->elf.dynamic_sections_created
9494 || h->dynindx == -1)
9495 {
2d7ad24e
AM
9496 if (h->type == STT_GNU_IFUNC)
9497 {
9498 s = htab->elf.iplt;
9499 pent->plt.offset = s->size;
9500 s->size += PLT_ENTRY_SIZE (htab);
9501 s = htab->elf.irelplt;
9502 }
9503 else
9504 {
9505 s = htab->pltlocal;
9506 pent->plt.offset = s->size;
9507 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9508 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9509 }
57e7d118
AM
9510 }
9511 else
9512 {
9513 /* If this is the first .plt entry, make room for the special
9514 first entry. */
9515 s = htab->elf.splt;
9516 if (s->size == 0)
9517 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9518
57e7d118 9519 pent->plt.offset = s->size;
65f38f15 9520
57e7d118
AM
9521 /* Make room for this entry. */
9522 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9523
57e7d118
AM
9524 /* Make room for the .glink code. */
9525 s = htab->glink;
9526 if (s->size == 0)
9e390558 9527 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9528 if (htab->opd_abi)
9529 {
9530 /* We need bigger stubs past index 32767. */
9e390558 9531 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9532 s->size += 4;
9533 s->size += 2*4;
9534 }
9535 else
9536 s->size += 4;
65f38f15 9537
57e7d118
AM
9538 /* We also need to make an entry in the .rela.plt section. */
9539 s = htab->elf.srelplt;
9540 }
2d7ad24e
AM
9541 if (s != NULL)
9542 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
9543 doneone = TRUE;
9544 }
9545 else
9546 pent->plt.offset = (bfd_vma) -1;
9547 if (!doneone)
9548 {
9549 h->plt.plist = NULL;
9550 h->needs_plt = 0;
9551 }
65f38f15 9552 }
57e7d118 9553 else
65f38f15 9554 {
57e7d118
AM
9555 h->plt.plist = NULL;
9556 h->needs_plt = 0;
65f38f15
AM
9557 }
9558
b34976b6 9559 return TRUE;
65f38f15
AM
9560}
9561
9e390558
AM
9562#define PPC_LO(v) ((v) & 0xffff)
9563#define PPC_HI(v) (((v) >> 16) & 0xffff)
9564#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
9565#define D34(v) \
9566 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9567#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 9568
a345bc8d
AM
9569/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9570 to set up space for global entry stubs. These are put in glink,
9571 after the branch table. */
65f38f15 9572
b34976b6 9573static bfd_boolean
a345bc8d 9574size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9575{
a345bc8d
AM
9576 struct bfd_link_info *info;
9577 struct ppc_link_hash_table *htab;
9578 struct plt_entry *pent;
9e390558 9579 asection *s, *plt;
65f38f15 9580
a345bc8d
AM
9581 if (h->root.type == bfd_link_hash_indirect)
9582 return TRUE;
65f38f15 9583
a345bc8d
AM
9584 if (!h->pointer_equality_needed)
9585 return TRUE;
65f38f15 9586
a345bc8d
AM
9587 if (h->def_regular)
9588 return TRUE;
65f38f15 9589
a345bc8d
AM
9590 info = inf;
9591 htab = ppc_hash_table (info);
9592 if (htab == NULL)
9593 return FALSE;
9594
9e390558
AM
9595 s = htab->global_entry;
9596 plt = htab->elf.splt;
a345bc8d
AM
9597 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9598 if (pent->plt.offset != (bfd_vma) -1
9599 && pent->addend == 0)
9600 {
afe397ea
AM
9601 /* For ELFv2, if this symbol is not defined in a regular file
9602 and we are not generating a shared library or pie, then we
9603 need to define the symbol in the executable on a call stub.
9604 This is to avoid text relocations. */
9e390558
AM
9605 bfd_vma off, stub_align, stub_off, stub_size;
9606 unsigned int align_power;
9607
9608 stub_size = 16;
9609 stub_off = s->size;
9610 if (htab->params->plt_stub_align >= 0)
9611 align_power = htab->params->plt_stub_align;
9612 else
9613 align_power = -htab->params->plt_stub_align;
9614 /* Setting section alignment is delayed until we know it is
9615 non-empty. Otherwise the .text output section will be
9616 aligned at least to plt_stub_align even when no global
9617 entry stubs are needed. */
9618 if (s->alignment_power < align_power)
9619 s->alignment_power = align_power;
9620 stub_align = (bfd_vma) 1 << align_power;
9621 if (htab->params->plt_stub_align >= 0
9622 || ((((stub_off + stub_size - 1) & -stub_align)
9623 - (stub_off & -stub_align))
9624 > ((stub_size - 1) & -stub_align)))
9625 stub_off = (stub_off + stub_align - 1) & -stub_align;
9626 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9627 off -= stub_off + s->output_offset + s->output_section->vma;
9628 /* Note that for --plt-stub-align negative we have a possible
9629 dependency between stub offset and size. Break that
9630 dependency by assuming the max stub size when calculating
9631 the stub offset. */
9632 if (PPC_HA (off) == 0)
9633 stub_size -= 4;
8a2058b5 9634 h->root.type = bfd_link_hash_defined;
afe397ea 9635 h->root.u.def.section = s;
9e390558
AM
9636 h->root.u.def.value = stub_off;
9637 s->size = stub_off + stub_size;
a345bc8d
AM
9638 break;
9639 }
9640 return TRUE;
9641}
9642
9643/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9644 read-only sections. */
9645
9646static bfd_boolean
98bbb1b8 9647maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 9648{
98bbb1b8
AM
9649 asection *sec;
9650
a345bc8d
AM
9651 if (h->root.type == bfd_link_hash_indirect)
9652 return TRUE;
9653
98bbb1b8
AM
9654 sec = readonly_dynrelocs (h);
9655 if (sec != NULL)
a345bc8d 9656 {
98bbb1b8
AM
9657 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9658
9659 info->flags |= DF_TEXTREL;
2cdcc330
AM
9660 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9661 " in read-only section `%pA'\n"),
9662 sec->owner, h->root.root.string, sec);
a345bc8d
AM
9663
9664 /* Not an error, just cut short the traversal. */
9665 return FALSE;
65f38f15 9666 }
b34976b6 9667 return TRUE;
65f38f15
AM
9668}
9669
9670/* Set the sizes of the dynamic sections. */
9671
b34976b6 9672static bfd_boolean
ee67d69a 9673ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9674 struct bfd_link_info *info)
65f38f15
AM
9675{
9676 struct ppc_link_hash_table *htab;
9677 bfd *dynobj;
9678 asection *s;
b34976b6 9679 bfd_boolean relocs;
65f38f15 9680 bfd *ibfd;
7865406b 9681 struct got_entry *first_tlsld;
65f38f15
AM
9682
9683 htab = ppc_hash_table (info);
4dfe6ac6
NC
9684 if (htab == NULL)
9685 return FALSE;
9686
65f38f15
AM
9687 dynobj = htab->elf.dynobj;
9688 if (dynobj == NULL)
9689 abort ();
9690
9691 if (htab->elf.dynamic_sections_created)
9692 {
9693 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9694 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9695 {
3d4d4302 9696 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9697 if (s == NULL)
9698 abort ();
eea6121a 9699 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9700 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9701 }
9702 }
9703
9704 /* Set up .got offsets for local syms, and space for local dynamic
9705 relocs. */
c72f2fb2 9706 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9707 {
411e1bfb
AM
9708 struct got_entry **lgot_ents;
9709 struct got_entry **end_lgot_ents;
e054468f
AM
9710 struct plt_entry **local_plt;
9711 struct plt_entry **end_local_plt;
f961d9dd 9712 unsigned char *lgot_masks;
65f38f15
AM
9713 bfd_size_type locsymcount;
9714 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9715
0c8d6e5c 9716 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9717 continue;
9718
9719 for (s = ibfd->sections; s != NULL; s = s->next)
9720 {
19e08130 9721 struct ppc_dyn_relocs *p;
65f38f15 9722
6edfbbad 9723 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9724 {
ec338859
AM
9725 if (!bfd_is_abs_section (p->sec)
9726 && bfd_is_abs_section (p->sec->output_section))
9727 {
9728 /* Input section has been discarded, either because
9729 it is a copy of a linkonce section or due to
9730 linker script /DISCARD/, so we'll be discarding
9731 the relocs too. */
9732 }
248866a8 9733 else if (p->count != 0)
ec338859 9734 {
19e08130
AM
9735 asection *srel = elf_section_data (p->sec)->sreloc;
9736 if (p->ifunc)
33e44f2e 9737 srel = htab->elf.irelplt;
eea6121a 9738 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9739 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9740 info->flags |= DF_TEXTREL;
ec338859 9741 }
65f38f15
AM
9742 }
9743 }
9744
411e1bfb
AM
9745 lgot_ents = elf_local_got_ents (ibfd);
9746 if (!lgot_ents)
65f38f15
AM
9747 continue;
9748
0ffa91dd 9749 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9750 locsymcount = symtab_hdr->sh_info;
411e1bfb 9751 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9752 local_plt = (struct plt_entry **) end_lgot_ents;
9753 end_local_plt = local_plt + locsymcount;
f961d9dd 9754 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9755 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9756 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9757 {
0b8bcf0d 9758 struct got_entry **pent, *ent;
411e1bfb 9759
0b8bcf0d
AM
9760 pent = lgot_ents;
9761 while ((ent = *pent) != NULL)
411e1bfb
AM
9762 if (ent->got.refcount > 0)
9763 {
e7b938ca 9764 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9765 {
927be08e 9766 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9767 *pent = ent->next;
411e1bfb
AM
9768 }
9769 else
9770 {
19e08130
AM
9771 unsigned int ent_size = 8;
9772 unsigned int rel_size = sizeof (Elf64_External_Rela);
9773
eea6121a 9774 ent->got.offset = s->size;
e7b938ca 9775 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9776 {
19e08130
AM
9777 ent_size *= 2;
9778 rel_size *= 2;
9779 }
9780 s->size += ent_size;
37da22e5 9781 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 9782 {
33e44f2e 9783 htab->elf.irelplt->size += rel_size;
19e08130
AM
9784 htab->got_reli_size += rel_size;
9785 }
f15d0b54
AM
9786 else if (bfd_link_pic (info)
9787 && !((ent->tls_type & TLS_TPREL) != 0
9788 && bfd_link_executable (info)))
19e08130
AM
9789 {
9790 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9791 srel->size += rel_size;
927be08e 9792 }
0b8bcf0d 9793 pent = &ent->next;
411e1bfb
AM
9794 }
9795 }
9796 else
0b8bcf0d 9797 *pent = ent->next;
65f38f15 9798 }
e054468f 9799
2d7ad24e
AM
9800 /* Allocate space for plt calls to local syms. */
9801 lgot_masks = (unsigned char *) end_local_plt;
9802 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
9803 {
9804 struct plt_entry *ent;
9805
9806 for (ent = *local_plt; ent != NULL; ent = ent->next)
9807 if (ent->plt.refcount > 0)
9808 {
2d7ad24e
AM
9809 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9810 {
9811 s = htab->elf.iplt;
9812 ent->plt.offset = s->size;
9813 s->size += PLT_ENTRY_SIZE (htab);
9814 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9815 }
3e04d765
AM
9816 else if (htab->can_convert_all_inline_plt
9817 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
9818 ent->plt.offset = (bfd_vma) -1;
9819 else
9820 {
9821 s = htab->pltlocal;
9822 ent->plt.offset = s->size;
9823 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9824 if (bfd_link_pic (info))
9825 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9826 }
e054468f
AM
9827 }
9828 else
9829 ent->plt.offset = (bfd_vma) -1;
9830 }
65f38f15
AM
9831 }
9832
9833 /* Allocate global sym .plt and .got entries, and space for global
9834 sym dynamic relocs. */
4ce794b7 9835 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 9836
0e1862bb 9837 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 9838 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 9839
7865406b 9840 first_tlsld = NULL;
c72f2fb2 9841 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 9842 {
7865406b
AM
9843 struct got_entry *ent;
9844
0c8d6e5c 9845 if (!is_ppc64_elf (ibfd))
102890f0
AM
9846 continue;
9847
7865406b
AM
9848 ent = ppc64_tlsld_got (ibfd);
9849 if (ent->got.refcount > 0)
102890f0 9850 {
7865406b 9851 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9852 {
7865406b
AM
9853 ent->is_indirect = TRUE;
9854 ent->got.ent = first_tlsld;
9855 }
9856 else
9857 {
9858 if (first_tlsld == NULL)
9859 first_tlsld = ent;
9860 s = ppc64_elf_tdata (ibfd)->got;
9861 ent->got.offset = s->size;
9862 ent->owner = ibfd;
9863 s->size += 16;
0e1862bb 9864 if (bfd_link_pic (info))
7865406b
AM
9865 {
9866 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9867 srel->size += sizeof (Elf64_External_Rela);
9868 }
102890f0
AM
9869 }
9870 }
9871 else
7865406b 9872 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9873 }
9874
65f38f15
AM
9875 /* We now have determined the sizes of the various dynamic sections.
9876 Allocate memory for them. */
b34976b6 9877 relocs = FALSE;
65f38f15
AM
9878 for (s = dynobj->sections; s != NULL; s = s->next)
9879 {
9880 if ((s->flags & SEC_LINKER_CREATED) == 0)
9881 continue;
9882
4ce794b7 9883 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9884 /* These haven't been allocated yet; don't strip. */
9885 continue;
33e44f2e
AM
9886 else if (s == htab->elf.sgot
9887 || s == htab->elf.splt
9888 || s == htab->elf.iplt
2d7ad24e 9889 || s == htab->pltlocal
c456f082 9890 || s == htab->glink
9e390558 9891 || s == htab->global_entry
5474d94f
AM
9892 || s == htab->elf.sdynbss
9893 || s == htab->elf.sdynrelro)
65f38f15
AM
9894 {
9895 /* Strip this section if we don't need it; see the
9896 comment below. */
5bd4f169 9897 }
58d180e8
AM
9898 else if (s == htab->glink_eh_frame)
9899 {
9900 if (!bfd_is_abs_section (s->output_section))
9901 /* Not sized yet. */
9902 continue;
9903 }
70cc837d 9904 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9905 {
c456f082 9906 if (s->size != 0)
5bd4f169 9907 {
33e44f2e 9908 if (s != htab->elf.srelplt)
b34976b6 9909 relocs = TRUE;
5bd4f169
AM
9910
9911 /* We use the reloc_count field as a counter if we need
9912 to copy relocs into the output file. */
9913 s->reloc_count = 0;
9914 }
9915 }
65f38f15 9916 else
5bd4f169
AM
9917 {
9918 /* It's not one of our sections, so don't allocate space. */
9919 continue;
9920 }
9921
eea6121a 9922 if (s->size == 0)
5bd4f169 9923 {
c456f082
AM
9924 /* If we don't need this section, strip it from the
9925 output file. This is mostly to handle .rela.bss and
9926 .rela.plt. We must create both sections in
9927 create_dynamic_sections, because they must be created
9928 before the linker maps input sections to output
9929 sections. The linker does that before
9930 adjust_dynamic_symbol is called, and it is that
9931 function which decides whether anything needs to go
9932 into these sections. */
8423293d 9933 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9934 continue;
9935 }
9936
06bcf541
AM
9937 if (bfd_is_abs_section (s->output_section))
9938 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9939 s->name);
9940
c456f082 9941 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9942 continue;
9943
65f38f15
AM
9944 /* Allocate memory for the section contents. We use bfd_zalloc
9945 here in case unused entries are not reclaimed before the
9946 section's contents are written out. This should not happen,
411e1bfb
AM
9947 but this way if it does we get a R_PPC64_NONE reloc in .rela
9948 sections instead of garbage.
9949 We also rely on the section contents being zero when writing
5474d94f 9950 the GOT and .dynrelro. */
eea6121a 9951 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9952 if (s->contents == NULL)
b34976b6 9953 return FALSE;
5bd4f169
AM
9954 }
9955
c72f2fb2 9956 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 9957 {
0c8d6e5c 9958 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9959 continue;
9960
e717da7e 9961 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 9962 if (s != NULL && s != htab->elf.sgot)
e717da7e 9963 {
eea6121a 9964 if (s->size == 0)
8423293d 9965 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9966 else
9967 {
eea6121a 9968 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9969 if (s->contents == NULL)
9970 return FALSE;
9971 }
9972 }
9973 s = ppc64_elf_tdata (ibfd)->relgot;
9974 if (s != NULL)
9975 {
eea6121a 9976 if (s->size == 0)
8423293d 9977 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9978 else
9979 {
eea6121a 9980 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9981 if (s->contents == NULL)
9982 return FALSE;
9983 relocs = TRUE;
9984 s->reloc_count = 0;
9985 }
9986 }
9987 }
9988
e86ce104 9989 if (htab->elf.dynamic_sections_created)
5bd4f169 9990 {
e8910a83
AM
9991 bfd_boolean tls_opt;
9992
5bd4f169
AM
9993 /* Add some entries to the .dynamic section. We fill in the
9994 values later, in ppc64_elf_finish_dynamic_sections, but we
9995 must add the entries now so that we get the correct size for
9996 the .dynamic section. The DT_DEBUG entry is filled in by the
9997 dynamic linker and used by the debugger. */
dc810e39 9998#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9999 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10000
0e1862bb 10001 if (bfd_link_executable (info))
5bd4f169 10002 {
dc810e39 10003 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10004 return FALSE;
5bd4f169
AM
10005 }
10006
33e44f2e 10007 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10008 {
dc810e39
AM
10009 if (!add_dynamic_entry (DT_PLTGOT, 0)
10010 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10011 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10012 || !add_dynamic_entry (DT_JMPREL, 0)
10013 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10014 return FALSE;
5bd4f169
AM
10015 }
10016
ee67d69a 10017 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10018 {
10019 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10020 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10021 return FALSE;
19397422
AM
10022 }
10023
7c9cf415 10024 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10025 && htab->tls_get_addr_fd != NULL
10026 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10027 if (tls_opt || !htab->opd_abi)
10028 {
10029 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10030 return FALSE;
10031 }
a7f2871e 10032
5bd4f169
AM
10033 if (relocs)
10034 {
dc810e39
AM
10035 if (!add_dynamic_entry (DT_RELA, 0)
10036 || !add_dynamic_entry (DT_RELASZ, 0)
10037 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10038 return FALSE;
5bd4f169 10039
65f38f15
AM
10040 /* If any dynamic relocs apply to a read-only section,
10041 then we need a DT_TEXTREL entry. */
248866a8 10042 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10043 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10044
65f38f15 10045 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10046 {
65f38f15 10047 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10048 return FALSE;
5bd4f169 10049 }
5bd4f169 10050 }
5bd4f169 10051 }
65f38f15 10052#undef add_dynamic_entry
5bd4f169 10053
b34976b6 10054 return TRUE;
5bd4f169
AM
10055}
10056
a345bc8d
AM
10057/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10058
10059static bfd_boolean
10060ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10061{
10062 if (h->plt.plist != NULL
10063 && !h->def_regular
10064 && !h->pointer_equality_needed)
10065 return FALSE;
10066
10067 return _bfd_elf_hash_symbol (h);
10068}
10069
721956f4 10070/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10071
4ce794b7
AM
10072static inline enum ppc_stub_type
10073ppc_type_of_stub (asection *input_sec,
10074 const Elf_Internal_Rela *rel,
10075 struct ppc_link_hash_entry **hash,
e054468f 10076 struct plt_entry **plt_ent,
6911b7dc
AM
10077 bfd_vma destination,
10078 unsigned long local_off)
5bd4f169 10079{
721956f4
AM
10080 struct ppc_link_hash_entry *h = *hash;
10081 bfd_vma location;
10082 bfd_vma branch_offset;
10083 bfd_vma max_branch_offset;
4ce794b7 10084 enum elf_ppc64_reloc_type r_type;
5bd4f169 10085
721956f4
AM
10086 if (h != NULL)
10087 {
e054468f 10088 struct plt_entry *ent;
7fe2b9a6 10089 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10090 if (h->oh != NULL
10091 && h->oh->is_func_descriptor)
7b8f6675
AM
10092 {
10093 fdh = ppc_follow_link (h->oh);
10094 *hash = fdh;
10095 }
8387904d 10096
e054468f
AM
10097 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10098 if (ent->addend == rel->r_addend
10099 && ent->plt.offset != (bfd_vma) -1)
10100 {
e054468f
AM
10101 *plt_ent = ent;
10102 return ppc_stub_plt_call;
10103 }
5bd4f169 10104
7fe2b9a6
AM
10105 /* Here, we know we don't have a plt entry. If we don't have a
10106 either a defined function descriptor or a defined entry symbol
10107 in a regular object file, then it is pointless trying to make
10108 any other type of stub. */
854b41e7
AM
10109 if (!is_static_defined (&fdh->elf)
10110 && !is_static_defined (&h->elf))
721956f4 10111 return ppc_stub_none;
5d1634d7 10112 }
e054468f
AM
10113 else if (elf_local_got_ents (input_sec->owner) != NULL)
10114 {
10115 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10116 struct plt_entry **local_plt = (struct plt_entry **)
10117 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10118 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10119
10120 if (local_plt[r_symndx] != NULL)
10121 {
10122 struct plt_entry *ent;
10123
10124 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10125 if (ent->addend == rel->r_addend
10126 && ent->plt.offset != (bfd_vma) -1)
10127 {
10128 *plt_ent = ent;
10129 return ppc_stub_plt_call;
10130 }
10131 }
10132 }
5d1634d7 10133
721956f4
AM
10134 /* Determine where the call point is. */
10135 location = (input_sec->output_offset
10136 + input_sec->output_section->vma
10137 + rel->r_offset);
5d1634d7 10138
721956f4
AM
10139 branch_offset = destination - location;
10140 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10141
721956f4
AM
10142 /* Determine if a long branch stub is needed. */
10143 max_branch_offset = 1 << 25;
23cedd1d
AM
10144 if (r_type == R_PPC64_REL14
10145 || r_type == R_PPC64_REL14_BRTAKEN
10146 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10147 max_branch_offset = 1 << 15;
5d1634d7 10148
6911b7dc 10149 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10150 /* We need a stub. Figure out whether a long_branch or plt_branch
10151 is needed later. */
10152 return ppc_stub_long_branch;
5d1634d7 10153
721956f4 10154 return ppc_stub_none;
5d1634d7
AM
10155}
10156
f891966f
AM
10157/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10158 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10159 . mflr %r12
10160 . bcl 20,31,1f
10161 .1: mflr %r11
10162 . mtlr %r12
05d0e962 10163 . lis %r12,xxx-1b@highest
f891966f 10164 . ori %r12,%r12,xxx-1b@higher
05d0e962 10165 . sldi %r12,%r12,32
f891966f 10166 . oris %r12,%r12,xxx-1b@high
05d0e962 10167 . ori %r12,%r12,xxx-1b@l
f891966f 10168 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10169
10170static bfd_byte *
10171build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10172{
f891966f
AM
10173 bfd_put_32 (abfd, MFLR_R12, p);
10174 p += 4;
10175 bfd_put_32 (abfd, BCL_20_31, p);
10176 p += 4;
10177 bfd_put_32 (abfd, MFLR_R11, p);
10178 p += 4;
10179 bfd_put_32 (abfd, MTLR_R12, p);
10180 p += 4;
05d0e962
AM
10181 if (off + 0x8000 < 0x10000)
10182 {
10183 if (load)
10184 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10185 else
10186 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10187 p += 4;
10188 }
10189 else if (off + 0x80008000ULL < 0x100000000ULL)
10190 {
10191 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10192 p += 4;
10193 if (load)
10194 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10195 else
10196 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10197 p += 4;
10198 }
10199 else
10200 {
10201 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10202 {
10203 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10204 p += 4;
10205 }
10206 else
10207 {
10208 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10209 p += 4;
10210 if (((off >> 32) & 0xffff) != 0)
10211 {
10212 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10213 p += 4;
10214 }
10215 }
10216 if (((off >> 32) & 0xffffffffULL) != 0)
10217 {
10218 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10219 p += 4;
10220 }
10221 if (PPC_HI (off) != 0)
10222 {
10223 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10224 p += 4;
10225 }
10226 if (PPC_LO (off) != 0)
10227 {
10228 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10229 p += 4;
10230 }
10231 if (load)
10232 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10233 else
10234 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10235 p += 4;
10236 }
10237 return p;
10238}
10239
10240static unsigned int
10241size_offset (bfd_vma off)
10242{
10243 unsigned int size;
10244 if (off + 0x8000 < 0x10000)
10245 size = 4;
10246 else if (off + 0x80008000ULL < 0x100000000ULL)
10247 size = 8;
10248 else
10249 {
10250 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10251 size = 4;
10252 else
10253 {
10254 size = 4;
10255 if (((off >> 32) & 0xffff) != 0)
10256 size += 4;
10257 }
10258 if (((off >> 32) & 0xffffffffULL) != 0)
10259 size += 4;
10260 if (PPC_HI (off) != 0)
10261 size += 4;
10262 if (PPC_LO (off) != 0)
10263 size += 4;
10264 size += 4;
10265 }
f891966f 10266 return size + 16;
05d0e962
AM
10267}
10268
3d58e1fc
AM
10269static unsigned int
10270num_relocs_for_offset (bfd_vma off)
10271{
10272 unsigned int num_rel;
10273 if (off + 0x8000 < 0x10000)
10274 num_rel = 1;
10275 else if (off + 0x80008000ULL < 0x100000000ULL)
10276 num_rel = 2;
10277 else
10278 {
10279 num_rel = 1;
10280 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10281 && ((off >> 32) & 0xffff) != 0)
10282 num_rel += 1;
10283 if (PPC_HI (off) != 0)
10284 num_rel += 1;
10285 if (PPC_LO (off) != 0)
10286 num_rel += 1;
10287 }
10288 return num_rel;
10289}
10290
10291static Elf_Internal_Rela *
10292emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10293 bfd_vma roff, bfd_vma targ, bfd_vma off)
10294{
10295 bfd_vma relative_targ = targ - (roff - 8);
10296 if (bfd_big_endian (info->output_bfd))
10297 roff += 2;
10298 r->r_offset = roff;
10299 r->r_addend = relative_targ + roff;
10300 if (off + 0x8000 < 0x10000)
10301 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10302 else if (off + 0x80008000ULL < 0x100000000ULL)
10303 {
10304 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10305 ++r;
10306 roff += 4;
10307 r->r_offset = roff;
10308 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10309 r->r_addend = relative_targ + roff;
10310 }
10311 else
10312 {
10313 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10314 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10315 else
10316 {
10317 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10318 if (((off >> 32) & 0xffff) != 0)
10319 {
10320 ++r;
10321 roff += 4;
10322 r->r_offset = roff;
10323 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10324 r->r_addend = relative_targ + roff;
10325 }
10326 }
10327 if (((off >> 32) & 0xffffffffULL) != 0)
10328 roff += 4;
10329 if (PPC_HI (off) != 0)
10330 {
10331 ++r;
10332 roff += 4;
10333 r->r_offset = roff;
10334 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10335 r->r_addend = relative_targ + roff;
10336 }
10337 if (PPC_LO (off) != 0)
10338 {
10339 ++r;
10340 roff += 4;
10341 r->r_offset = roff;
10342 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10343 r->r_addend = relative_targ + roff;
10344 }
10345 }
10346 return r;
10347}
10348
04bdff6a
AM
10349static bfd_byte *
10350build_powerxx_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10351 bfd_boolean load)
10352{
10353 uint64_t insn;
10354 if (off - odd + (1ULL << 33) < 1ULL << 34)
10355 {
10356 off -= odd;
10357 if (odd)
10358 {
10359 bfd_put_32 (abfd, NOP, p);
10360 p += 4;
10361 }
10362 if (load)
10363 insn = PLD_R12_PC;
10364 else
10365 insn = PADDI_R12_PC;
10366 insn |= D34 (off);
10367 bfd_put_32 (abfd, insn >> 32, p);
10368 p += 4;
10369 bfd_put_32 (abfd, insn, p);
10370 }
10371 /* The minimum value for paddi is -0x200000000. The minimum value
10372 for li is -0x8000, which when shifted by 34 and added gives a
10373 minimum value of -0x2000200000000. The maximum value is
10374 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10375 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10376 {
10377 off -= 8 - odd;
10378 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10379 p += 4;
10380 if (!odd)
10381 {
10382 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10383 p += 4;
10384 }
10385 insn = PADDI_R12_PC | D34 (off);
10386 bfd_put_32 (abfd, insn >> 32, p);
10387 p += 4;
10388 bfd_put_32 (abfd, insn, p);
10389 p += 4;
10390 if (odd)
10391 {
10392 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10393 p += 4;
10394 }
10395 if (load)
10396 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10397 else
10398 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10399 }
10400 else
10401 {
10402 off -= odd + 8;
10403 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10404 p += 4;
10405 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10406 p += 4;
10407 if (odd)
10408 {
10409 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10410 p += 4;
10411 }
10412 insn = PADDI_R12_PC | D34 (off);
10413 bfd_put_32 (abfd, insn >> 32, p);
10414 p += 4;
10415 bfd_put_32 (abfd, insn, p);
10416 p += 4;
10417 if (!odd)
10418 {
10419 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10420 p += 4;
10421 }
10422 if (load)
10423 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10424 else
10425 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10426 }
10427 p += 4;
10428 return p;
10429}
10430
10431static unsigned int
10432size_powerxx_offset (bfd_vma off, int odd)
10433{
10434 if (off - odd + (1ULL << 33) < 1ULL << 34)
10435 return odd + 8;
10436 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10437 return 20;
10438 else
10439 return 24;
10440}
10441
10442static unsigned int
10443num_relocs_for_powerxx_offset (bfd_vma off, int odd)
10444{
10445 if (off - odd + (1ULL << 33) < 1ULL << 34)
10446 return 1;
10447 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10448 return 2;
10449 else
10450 return 3;
10451}
10452
10453static Elf_Internal_Rela *
10454emit_relocs_for_powerxx_offset (struct bfd_link_info *info,
10455 Elf_Internal_Rela *r, bfd_vma roff,
10456 bfd_vma targ, bfd_vma off, int odd)
10457{
10458 if (off - odd + (1ULL << 33) < 1ULL << 34)
10459 roff += odd;
10460 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10461 {
10462 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10463 r->r_offset = roff + d_offset;
10464 r->r_addend = targ + 8 - odd - d_offset;
10465 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10466 ++r;
10467 roff += 8 - odd;
10468 }
10469 else
10470 {
10471 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10472 r->r_offset = roff + d_offset;
10473 r->r_addend = targ + 8 + odd - d_offset;
10474 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10475 ++r;
10476 roff += 4;
10477 r->r_offset = roff + d_offset;
10478 r->r_addend = targ + 4 + odd - d_offset;
10479 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10480 ++r;
10481 roff += 4 + odd;
10482 }
10483 r->r_offset = roff;
10484 r->r_addend = targ;
10485 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10486 return r;
10487}
10488
df136d64
AM
10489/* Emit .eh_frame opcode to advance pc by DELTA. */
10490
10491static bfd_byte *
10492eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10493{
10494 delta /= 4;
10495 if (delta < 64)
10496 *eh++ = DW_CFA_advance_loc + delta;
10497 else if (delta < 256)
10498 {
10499 *eh++ = DW_CFA_advance_loc1;
10500 *eh++ = delta;
10501 }
10502 else if (delta < 65536)
10503 {
10504 *eh++ = DW_CFA_advance_loc2;
10505 bfd_put_16 (abfd, delta, eh);
10506 eh += 2;
10507 }
10508 else
10509 {
10510 *eh++ = DW_CFA_advance_loc4;
10511 bfd_put_32 (abfd, delta, eh);
10512 eh += 4;
10513 }
10514 return eh;
10515}
10516
10517/* Size of required .eh_frame opcode to advance pc by DELTA. */
10518
10519static unsigned int
10520eh_advance_size (unsigned int delta)
10521{
10522 if (delta < 64 * 4)
10523 /* DW_CFA_advance_loc+[1..63]. */
10524 return 1;
10525 if (delta < 256 * 4)
10526 /* DW_CFA_advance_loc1, byte. */
10527 return 2;
10528 if (delta < 65536 * 4)
10529 /* DW_CFA_advance_loc2, 2 bytes. */
10530 return 3;
10531 /* DW_CFA_advance_loc4, 4 bytes. */
10532 return 5;
10533}
10534
794e51c0
AM
10535/* With power7 weakly ordered memory model, it is possible for ld.so
10536 to update a plt entry in one thread and have another thread see a
10537 stale zero toc entry. To avoid this we need some sort of acquire
10538 barrier in the call stub. One solution is to make the load of the
10539 toc word seem to appear to depend on the load of the function entry
10540 word. Another solution is to test for r2 being zero, and branch to
10541 the appropriate glink entry if so.
10542
10543 . fake dep barrier compare
71a39c98
AM
10544 . ld 12,xxx(2) ld 12,xxx(2)
10545 . mtctr 12 mtctr 12
10546 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10547 . add 2,2,11 cmpldi 2,0
10548 . ld 2,xxx+8(2) bnectr+
10549 . bctr b <glink_entry>
10550
10551 The solution involving the compare turns out to be faster, so
10552 that's what we use unless the branch won't reach. */
10553
10554#define ALWAYS_USE_FAKE_DEP 0
10555#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10556
794e51c0
AM
10557static inline unsigned int
10558plt_stub_size (struct ppc_link_hash_table *htab,
10559 struct ppc_stub_hash_entry *stub_entry,
10560 bfd_vma off)
10561{
05d0e962 10562 unsigned size;
b9e5796b 10563
05d0e962
AM
10564 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10565 {
04bdff6a
AM
10566 if (htab->powerxx_stubs)
10567 {
10568 bfd_vma start = (stub_entry->stub_offset
10569 + stub_entry->group->stub_sec->output_offset
10570 + stub_entry->group->stub_sec->output_section->vma);
10571 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10572 start += 4;
10573 size = 8 + size_powerxx_offset (off, start & 4);
10574 }
10575 else
10576 size = 8 + size_offset (off - 8);
05d0e962
AM
10577 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10578 size += 4;
10579 return size;
10580 }
10581
10582 size = 12;
b9e5796b
AM
10583 if (ALWAYS_EMIT_R2SAVE
10584 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10585 size += 4;
10586 if (PPC_HA (off) != 0)
794e51c0 10587 size += 4;
b9e5796b
AM
10588 if (htab->opd_abi)
10589 {
10590 size += 4;
e7d1c40c 10591 if (htab->params->plt_static_chain)
b9e5796b 10592 size += 4;
bd4d2eaa
AM
10593 if (htab->params->plt_thread_safe
10594 && htab->elf.dynamic_sections_created
10595 && stub_entry->h != NULL
10596 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10597 size += 8;
e7d1c40c 10598 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10599 size += 4;
10600 }
794e51c0
AM
10601 if (stub_entry->h != NULL
10602 && (stub_entry->h == htab->tls_get_addr_fd
10603 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10604 && htab->params->tls_get_addr_opt)
f378ab09
AM
10605 {
10606 size += 7 * 4;
e81b4c93 10607 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 10608 size += 6 * 4;
f378ab09 10609 }
794e51c0
AM
10610 return size;
10611}
10612
2420fff6
AM
10613/* Depending on the sign of plt_stub_align:
10614 If positive, return the padding to align to a 2**plt_stub_align
10615 boundary.
10616 If negative, if this stub would cross fewer 2**plt_stub_align
10617 boundaries if we align, then return the padding needed to do so. */
10618
794e51c0
AM
10619static inline unsigned int
10620plt_stub_pad (struct ppc_link_hash_table *htab,
10621 struct ppc_stub_hash_entry *stub_entry,
10622 bfd_vma plt_off)
10623{
2420fff6 10624 int stub_align;
1aa42141 10625 unsigned stub_size;
6f20ed8a 10626 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10627
2420fff6
AM
10628 if (htab->params->plt_stub_align >= 0)
10629 {
10630 stub_align = 1 << htab->params->plt_stub_align;
10631 if ((stub_off & (stub_align - 1)) != 0)
10632 return stub_align - (stub_off & (stub_align - 1));
10633 return 0;
10634 }
10635
10636 stub_align = 1 << -htab->params->plt_stub_align;
1aa42141 10637 stub_size = plt_stub_size (htab, stub_entry, plt_off);
794e51c0 10638 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10639 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10640 return stub_align - (stub_off & (stub_align - 1));
10641 return 0;
10642}
10643
10644/* Build a .plt call stub. */
10645
10646static inline bfd_byte *
10647build_plt_stub (struct ppc_link_hash_table *htab,
10648 struct ppc_stub_hash_entry *stub_entry,
10649 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10650{
e7d1c40c 10651 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10652 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10653 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10654 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10655 && htab->elf.dynamic_sections_created
10656 && stub_entry->h != NULL
10657 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10658 bfd_boolean use_fake_dep = plt_thread_safe;
10659 bfd_vma cmp_branch_off = 0;
10660
10661 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10662 && plt_load_toc
794e51c0 10663 && plt_thread_safe
bd4d2eaa
AM
10664 && !((stub_entry->h == htab->tls_get_addr_fd
10665 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10666 && htab->params->tls_get_addr_opt))
794e51c0
AM
10667 {
10668 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10669 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10670 / PLT_ENTRY_SIZE (htab));
9e390558 10671 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10672 bfd_vma to, from;
10673
68d62958
AM
10674 if (pltindex > 32768)
10675 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10676 to = (glinkoff
10677 + htab->glink->output_offset
10678 + htab->glink->output_section->vma);
6f20ed8a 10679 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10680 + 4 * (ALWAYS_EMIT_R2SAVE
10681 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10682 + 4 * (PPC_HA (offset) != 0)
10683 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10684 != PPC_HA (offset))
10685 + 4 * (plt_static_chain != 0)
10686 + 20
6f20ed8a
AM
10687 + stub_entry->group->stub_sec->output_offset
10688 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10689 cmp_branch_off = to - from;
10690 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10691 }
10692
ac2df442
AM
10693 if (PPC_HA (offset) != 0)
10694 {
176a0d42
AM
10695 if (r != NULL)
10696 {
794e51c0
AM
10697 if (ALWAYS_EMIT_R2SAVE
10698 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10699 r[0].r_offset += 4;
176a0d42 10700 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10701 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10702 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10703 r[1].r_addend = r[0].r_addend;
b9e5796b 10704 if (plt_load_toc)
176a0d42 10705 {
b9e5796b 10706 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10707 {
b9e5796b
AM
10708 r[2].r_offset = r[1].r_offset + 4;
10709 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10710 r[2].r_addend = r[0].r_addend;
10711 }
10712 else
10713 {
10714 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10715 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10716 r[2].r_addend = r[0].r_addend + 8;
10717 if (plt_static_chain)
10718 {
10719 r[3].r_offset = r[2].r_offset + 4;
10720 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10721 r[3].r_addend = r[0].r_addend + 16;
10722 }
c7131b65 10723 }
176a0d42
AM
10724 }
10725 }
794e51c0
AM
10726 if (ALWAYS_EMIT_R2SAVE
10727 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10728 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10729 if (plt_load_toc)
10730 {
10731 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10732 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10733 }
10734 else
10735 {
10736 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10737 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10738 }
b9e5796b
AM
10739 if (plt_load_toc
10740 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10741 {
71a39c98 10742 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10743 offset = 0;
10744 }
71a39c98 10745 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10746 if (plt_load_toc)
794e51c0 10747 {
b9e5796b
AM
10748 if (use_fake_dep)
10749 {
10750 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10751 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10752 }
10753 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10754 if (plt_static_chain)
10755 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10756 }
ac2df442
AM
10757 }
10758 else
10759 {
176a0d42
AM
10760 if (r != NULL)
10761 {
794e51c0
AM
10762 if (ALWAYS_EMIT_R2SAVE
10763 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10764 r[0].r_offset += 4;
176a0d42 10765 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10766 if (plt_load_toc)
176a0d42 10767 {
b9e5796b 10768 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10769 {
b9e5796b
AM
10770 r[1].r_offset = r[0].r_offset + 4;
10771 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10772 r[1].r_addend = r[0].r_addend;
10773 }
10774 else
10775 {
10776 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10777 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10778 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10779 if (plt_static_chain)
10780 {
10781 r[2].r_offset = r[1].r_offset + 4;
10782 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10783 r[2].r_addend = r[0].r_addend + 8;
10784 }
c7131b65 10785 }
176a0d42
AM
10786 }
10787 }
794e51c0
AM
10788 if (ALWAYS_EMIT_R2SAVE
10789 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10790 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10791 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10792 if (plt_load_toc
10793 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10794 {
10795 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10796 offset = 0;
10797 }
71a39c98 10798 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10799 if (plt_load_toc)
794e51c0 10800 {
b9e5796b
AM
10801 if (use_fake_dep)
10802 {
10803 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10804 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10805 }
10806 if (plt_static_chain)
10807 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10808 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10809 }
ac2df442 10810 }
b9e5796b 10811 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10812 {
10813 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10814 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10815 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10816 }
10817 else
407aa07c 10818 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10819 return p;
10820}
10821
a7f2871e
AM
10822/* Build a special .plt call stub for __tls_get_addr. */
10823
10824#define LD_R11_0R3 0xe9630000
10825#define LD_R12_0R3 0xe9830000
10826#define MR_R0_R3 0x7c601b78
10827#define CMPDI_R11_0 0x2c2b0000
10828#define ADD_R3_R12_R13 0x7c6c6a14
10829#define BEQLR 0x4d820020
10830#define MR_R3_R0 0x7c030378
a7f2871e
AM
10831#define STD_R11_0R1 0xf9610000
10832#define BCTRL 0x4e800421
10833#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10834#define MTLR_R11 0x7d6803a6
10835
10836static inline bfd_byte *
794e51c0
AM
10837build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10838 struct ppc_stub_hash_entry *stub_entry,
10839 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10840{
e7d1c40c 10841 bfd *obfd = htab->params->stub_bfd;
df136d64 10842 bfd_byte *loc = p;
794e51c0 10843
a7f2871e
AM
10844 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10845 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10846 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10847 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10848 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10849 bfd_put_32 (obfd, BEQLR, p), p += 4;
10850 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
10851 if (r != NULL)
10852 r[0].r_offset += 7 * 4;
e81b4c93 10853 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
f378ab09
AM
10854 return build_plt_stub (htab, stub_entry, p, offset, r);
10855
a7f2871e 10856 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10857 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10858
10859 if (r != NULL)
f378ab09 10860 r[0].r_offset += 2 * 4;
794e51c0 10861 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 10862 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 10863
a078d95a 10864 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10865 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10866 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10867 bfd_put_32 (obfd, BLR, p), p += 4;
10868
df136d64
AM
10869 if (htab->glink_eh_frame != NULL
10870 && htab->glink_eh_frame->size != 0)
10871 {
10872 bfd_byte *base, *eh;
10873 unsigned int lr_used, delta;
10874
10875 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10876 eh = base + stub_entry->group->eh_size;
10877 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10878 delta = lr_used - stub_entry->group->lr_restore;
10879 stub_entry->group->lr_restore = lr_used + 16;
10880 eh = eh_advance (htab->elf.dynobj, eh, delta);
10881 *eh++ = DW_CFA_offset_extended_sf;
10882 *eh++ = 65;
10883 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10884 *eh++ = DW_CFA_advance_loc + 4;
10885 *eh++ = DW_CFA_restore_extended;
10886 *eh++ = 65;
10887 stub_entry->group->eh_size = eh - base;
10888 }
a7f2871e
AM
10889 return p;
10890}
10891
176a0d42
AM
10892static Elf_Internal_Rela *
10893get_relocs (asection *sec, int count)
10894{
10895 Elf_Internal_Rela *relocs;
10896 struct bfd_elf_section_data *elfsec_data;
10897
10898 elfsec_data = elf_section_data (sec);
10899 relocs = elfsec_data->relocs;
10900 if (relocs == NULL)
10901 {
10902 bfd_size_type relsize;
10903 relsize = sec->reloc_count * sizeof (*relocs);
10904 relocs = bfd_alloc (sec->owner, relsize);
10905 if (relocs == NULL)
10906 return NULL;
10907 elfsec_data->relocs = relocs;
d4730f92
BS
10908 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10909 sizeof (Elf_Internal_Shdr));
10910 if (elfsec_data->rela.hdr == NULL)
10911 return NULL;
10912 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10913 * sizeof (Elf64_External_Rela));
10914 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10915 sec->reloc_count = 0;
10916 }
10917 relocs += sec->reloc_count;
10918 sec->reloc_count += count;
10919 return relocs;
10920}
10921
3d58e1fc
AM
10922/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10923 forms, to the equivalent relocs against the global symbol given by
10924 STUB_ENTRY->H. */
10925
10926static bfd_boolean
10927use_global_in_relocs (struct ppc_link_hash_table *htab,
10928 struct ppc_stub_hash_entry *stub_entry,
10929 Elf_Internal_Rela *r, unsigned int num_rel)
10930{
10931 struct elf_link_hash_entry **hashes;
10932 unsigned long symndx;
10933 struct ppc_link_hash_entry *h;
10934 bfd_vma symval;
10935
10936 /* Relocs are always against symbols in their own object file. Fake
10937 up global sym hashes for the stub bfd (which has no symbols). */
10938 hashes = elf_sym_hashes (htab->params->stub_bfd);
10939 if (hashes == NULL)
10940 {
10941 bfd_size_type hsize;
10942
10943 /* When called the first time, stub_globals will contain the
10944 total number of symbols seen during stub sizing. After
10945 allocating, stub_globals is used as an index to fill the
10946 hashes array. */
10947 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10948 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10949 if (hashes == NULL)
10950 return FALSE;
10951 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10952 htab->stub_globals = 1;
10953 }
10954 symndx = htab->stub_globals++;
10955 h = stub_entry->h;
10956 hashes[symndx] = &h->elf;
10957 if (h->oh != NULL && h->oh->is_func)
10958 h = ppc_follow_link (h->oh);
10959 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10960 || h->elf.root.type == bfd_link_hash_defweak);
10961 symval = (h->elf.root.u.def.value
10962 + h->elf.root.u.def.section->output_offset
10963 + h->elf.root.u.def.section->output_section->vma);
10964 while (num_rel-- != 0)
10965 {
10966 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10967 if (h->elf.root.u.def.section != stub_entry->target_section)
10968 {
10969 /* H is an opd symbol. The addend must be zero, and the
10970 branch reloc is the only one we can convert. */
10971 r->r_addend = 0;
10972 break;
10973 }
10974 else
10975 r->r_addend -= symval;
10976 --r;
10977 }
10978 return TRUE;
10979}
10980
aa374f67 10981static bfd_vma
25f53a85 10982get_r2off (struct bfd_link_info *info,
aa374f67
AM
10983 struct ppc_stub_hash_entry *stub_entry)
10984{
25f53a85 10985 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10986 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10987
10988 if (r2off == 0)
10989 {
10990 /* Support linking -R objects. Get the toc pointer from the
10991 opd entry. */
10992 char buf[8];
b9e5796b
AM
10993 if (!htab->opd_abi)
10994 return r2off;
aa374f67
AM
10995 asection *opd = stub_entry->h->elf.root.u.def.section;
10996 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10997
10998 if (strcmp (opd->name, ".opd") != 0
10999 || opd->reloc_count != 0)
11000 {
2cdcc330
AM
11001 info->callbacks->einfo
11002 (_("%P: cannot find opd entry toc for `%pT'\n"),
11003 stub_entry->h->elf.root.root.string);
aa374f67 11004 bfd_set_error (bfd_error_bad_value);
a7c49797 11005 return (bfd_vma) -1;
aa374f67
AM
11006 }
11007 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11008 return (bfd_vma) -1;
aa374f67 11009 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11010 r2off -= elf_gp (info->output_bfd);
aa374f67 11011 }
6f20ed8a 11012 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11013 return r2off;
11014}
11015
b34976b6 11016static bfd_boolean
4ce794b7 11017ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11018{
721956f4
AM
11019 struct ppc_stub_hash_entry *stub_entry;
11020 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11021 struct bfd_link_info *info;
11022 struct ppc_link_hash_table *htab;
721956f4 11023 bfd_byte *loc;
3d58e1fc 11024 bfd_byte *p, *relp;
1aa42141 11025 bfd_vma targ, off;
176a0d42 11026 Elf_Internal_Rela *r;
e054468f 11027 asection *plt;
3d58e1fc 11028 int num_rel;
04bdff6a 11029 int odd;
5d1634d7 11030
721956f4
AM
11031 /* Massage our args to the form they really have. */
11032 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11033 info = in_arg;
5d1634d7 11034
5d1634d7 11035 htab = ppc_hash_table (info);
4dfe6ac6
NC
11036 if (htab == NULL)
11037 return FALSE;
5d1634d7 11038
1aa42141 11039 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 11040 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11041
4ce794b7 11042 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 11043 switch (stub_entry->stub_type)
5d1634d7 11044 {
721956f4 11045 case ppc_stub_long_branch:
ad8e1ba5 11046 case ppc_stub_long_branch_r2off:
721956f4 11047 /* Branches are relative. This is where we are going to. */
1aa42141 11048 targ = (stub_entry->target_value
6911b7dc
AM
11049 + stub_entry->target_section->output_offset
11050 + stub_entry->target_section->output_section->vma);
1aa42141 11051 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11052
721956f4 11053 /* And this is where we are coming from. */
1aa42141
AM
11054 off = (stub_entry->stub_offset
11055 + stub_entry->group->stub_sec->output_offset
11056 + stub_entry->group->stub_sec->output_section->vma);
11057 off = targ - off;
e86ce104 11058
9e390558 11059 p = loc;
ac2df442 11060 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 11061 {
25f53a85 11062 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11063
a7c49797 11064 if (r2off == (bfd_vma) -1)
aa374f67
AM
11065 {
11066 htab->stub_error = TRUE;
11067 return FALSE;
11068 }
9e390558
AM
11069 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11070 p += 4;
ac2df442
AM
11071 if (PPC_HA (r2off) != 0)
11072 {
e7d1c40c 11073 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11074 ADDIS_R2_R2 | PPC_HA (r2off), p);
11075 p += 4;
a7c49797
AM
11076 }
11077 if (PPC_LO (r2off) != 0)
11078 {
11079 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11080 ADDI_R2_R2 | PPC_LO (r2off), p);
11081 p += 4;
ac2df442 11082 }
9e390558 11083 off -= p - loc;
ad8e1ba5 11084 }
9e390558
AM
11085 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11086 p += 4;
ad8e1ba5 11087
5c3dead3
AM
11088 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11089 {
cf97bcb0
AM
11090 _bfd_error_handler
11091 (_("long branch stub `%s' offset overflow"),
bc30df16 11092 stub_entry->root.string);
5c3dead3
AM
11093 htab->stub_error = TRUE;
11094 return FALSE;
11095 }
ee75fd95
AM
11096
11097 if (info->emitrelocations)
11098 {
6f20ed8a 11099 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
11100 if (r == NULL)
11101 return FALSE;
9e390558 11102 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11103 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11104 r->r_addend = targ;
3d58e1fc
AM
11105 if (stub_entry->h != NULL
11106 && !use_global_in_relocs (htab, stub_entry, r, 1))
11107 return FALSE;
ee75fd95 11108 }
721956f4 11109 break;
e86ce104 11110
721956f4 11111 case ppc_stub_plt_branch:
ad8e1ba5 11112 case ppc_stub_plt_branch_r2off:
721956f4
AM
11113 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11114 stub_entry->root.string + 9,
b34976b6 11115 FALSE, FALSE);
721956f4
AM
11116 if (br_entry == NULL)
11117 {
cf97bcb0
AM
11118 _bfd_error_handler (_("can't find branch stub `%s'"),
11119 stub_entry->root.string);
b34976b6
AM
11120 htab->stub_error = TRUE;
11121 return FALSE;
721956f4
AM
11122 }
11123
1aa42141 11124 targ = (stub_entry->target_value
176a0d42
AM
11125 + stub_entry->target_section->output_offset
11126 + stub_entry->target_section->output_section->vma);
6911b7dc 11127 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 11128 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11129
1aa42141 11130 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11131 htab->brlt->contents + br_entry->offset);
721956f4 11132
f94498ff 11133 if (br_entry->iter == htab->stub_iteration)
721956f4 11134 {
f94498ff 11135 br_entry->iter = 0;
84f5d08e 11136
f94498ff 11137 if (htab->relbrlt != NULL)
84f5d08e 11138 {
f94498ff
AM
11139 /* Create a reloc for the branch lookup table entry. */
11140 Elf_Internal_Rela rela;
11141 bfd_byte *rl;
11142
11143 rela.r_offset = (br_entry->offset
11144 + htab->brlt->output_offset
11145 + htab->brlt->output_section->vma);
11146 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11147 rela.r_addend = targ;
f94498ff
AM
11148
11149 rl = htab->relbrlt->contents;
11150 rl += (htab->relbrlt->reloc_count++
11151 * sizeof (Elf64_External_Rela));
11152 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11153 }
11154 else if (info->emitrelocations)
11155 {
176a0d42
AM
11156 r = get_relocs (htab->brlt, 1);
11157 if (r == NULL)
11158 return FALSE;
11159 /* brlt, being SEC_LINKER_CREATED does not go through the
11160 normal reloc processing. Symbols and offsets are not
11161 translated from input file to output file form, so
11162 set up the offset per the output file. */
f94498ff
AM
11163 r->r_offset = (br_entry->offset
11164 + htab->brlt->output_offset
11165 + htab->brlt->output_section->vma);
11166 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11167 r->r_addend = targ;
84f5d08e 11168 }
84f5d08e 11169 }
721956f4 11170
1aa42141 11171 targ = (br_entry->offset
176a0d42
AM
11172 + htab->brlt->output_offset
11173 + htab->brlt->output_section->vma);
11174
1aa42141
AM
11175 off = (elf_gp (info->output_bfd)
11176 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11177 off = targ - off;
721956f4 11178
ad8e1ba5 11179 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11180 {
25f53a85 11181 info->callbacks->einfo
c1c8c1ef 11182 (_("%P: linkage table error against `%pT'\n"),
721956f4 11183 stub_entry->root.string);
5d1634d7 11184 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11185 htab->stub_error = TRUE;
11186 return FALSE;
5d1634d7 11187 }
41bd81ab 11188
176a0d42
AM
11189 if (info->emitrelocations)
11190 {
6f20ed8a 11191 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11192 if (r == NULL)
11193 return FALSE;
6f20ed8a 11194 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11195 if (bfd_big_endian (info->output_bfd))
11196 r[0].r_offset += 2;
00f412ee 11197 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11198 r[0].r_offset += 4;
11199 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11200 r[0].r_addend = targ;
176a0d42
AM
11201 if (PPC_HA (off) != 0)
11202 {
11203 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11204 r[1].r_offset = r[0].r_offset + 4;
11205 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11206 r[1].r_addend = r[0].r_addend;
11207 }
11208 }
11209
9e390558 11210 p = loc;
00f412ee 11211 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11212 {
176a0d42 11213 if (PPC_HA (off) != 0)
ac2df442 11214 {
e7d1c40c 11215 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11216 ADDIS_R12_R2 | PPC_HA (off), p);
11217 p += 4;
e7d1c40c 11218 bfd_put_32 (htab->params->stub_bfd,
9e390558 11219 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11220 }
11221 else
9e390558
AM
11222 bfd_put_32 (htab->params->stub_bfd,
11223 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11224 }
11225 else
11226 {
25f53a85 11227 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11228
a7c49797 11229 if (r2off == (bfd_vma) -1)
aa374f67
AM
11230 {
11231 htab->stub_error = TRUE;
11232 return FALSE;
11233 }
ad8e1ba5 11234
9e390558
AM
11235 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11236 p += 4;
176a0d42 11237 if (PPC_HA (off) != 0)
ac2df442 11238 {
e7d1c40c 11239 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11240 ADDIS_R12_R2 | PPC_HA (off), p);
11241 p += 4;
e7d1c40c 11242 bfd_put_32 (htab->params->stub_bfd,
9e390558 11243 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11244 }
11245 else
9e390558 11246 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11247
11248 if (PPC_HA (r2off) != 0)
11249 {
9e390558 11250 p += 4;
e7d1c40c 11251 bfd_put_32 (htab->params->stub_bfd,
9e390558 11252 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11253 }
11254 if (PPC_LO (r2off) != 0)
11255 {
9e390558 11256 p += 4;
e7d1c40c 11257 bfd_put_32 (htab->params->stub_bfd,
9e390558 11258 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11259 }
ad8e1ba5 11260 }
9e390558
AM
11261 p += 4;
11262 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11263 p += 4;
407aa07c
AM
11264 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11265 p += 4;
721956f4 11266 break;
5d1634d7 11267
05d0e962
AM
11268 case ppc_stub_long_branch_notoc:
11269 case ppc_stub_long_branch_both:
11270 case ppc_stub_plt_branch_notoc:
11271 case ppc_stub_plt_branch_both:
11272 case ppc_stub_plt_call_notoc:
11273 case ppc_stub_plt_call_both:
11274 p = loc;
f891966f 11275 off = (stub_entry->stub_offset
05d0e962
AM
11276 + stub_entry->group->stub_sec->output_offset
11277 + stub_entry->group->stub_sec->output_section->vma);
11278 if (stub_entry->stub_type == ppc_stub_long_branch_both
11279 || stub_entry->stub_type == ppc_stub_plt_branch_both
11280 || stub_entry->stub_type == ppc_stub_plt_call_both)
11281 {
11282 off += 4;
11283 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11284 p += 4;
11285 }
11286 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11287 {
11288 targ = stub_entry->plt_ent->plt.offset & ~1;
11289 if (targ >= (bfd_vma) -2)
11290 abort ();
11291
11292 plt = htab->elf.splt;
11293 if (!htab->elf.dynamic_sections_created
11294 || stub_entry->h == NULL
11295 || stub_entry->h->elf.dynindx == -1)
11296 {
11297 if (stub_entry->symtype == STT_GNU_IFUNC)
11298 plt = htab->elf.iplt;
11299 else
11300 plt = htab->pltlocal;
11301 }
11302 targ += plt->output_offset + plt->output_section->vma;
11303 }
11304 else
11305 targ = (stub_entry->target_value
11306 + stub_entry->target_section->output_offset
11307 + stub_entry->target_section->output_section->vma);
04bdff6a 11308 odd = off & 4;
05d0e962 11309 off = targ - off;
f891966f 11310
3d58e1fc
AM
11311 relp = p;
11312 num_rel = 0;
04bdff6a
AM
11313 if (htab->powerxx_stubs)
11314 {
11315 bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
11316 p = build_powerxx_offset (htab->params->stub_bfd, p, off, odd, load);
11317 }
11318 else
11319 {
11320 /* The notoc stubs calculate their target (either a PLT entry or
11321 the global entry point of a function) relative to the PC
11322 returned by the "bcl" two instructions past the start of the
11323 sequence emitted by build_offset. The offset is therefore 8
11324 less than calculated from the start of the sequence. */
11325 off -= 8;
11326 p = build_offset (htab->params->stub_bfd, p, off,
11327 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11328 }
11329
f891966f 11330 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
05d0e962 11331 {
f891966f 11332 bfd_vma from;
3d58e1fc 11333 num_rel = 1;
f891966f
AM
11334 from = (stub_entry->stub_offset
11335 + stub_entry->group->stub_sec->output_offset
11336 + stub_entry->group->stub_sec->output_section->vma
11337 + (p - loc));
05d0e962 11338 bfd_put_32 (htab->params->stub_bfd,
f891966f 11339 B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
11340 }
11341 else
11342 {
11343 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11344 p += 4;
11345 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11346 }
11347 p += 4;
df136d64 11348
3d58e1fc
AM
11349 if (info->emitrelocations)
11350 {
04bdff6a
AM
11351 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
11352 if (htab->powerxx_stubs)
11353 num_rel += num_relocs_for_powerxx_offset (off, odd);
11354 else
11355 {
11356 num_rel += num_relocs_for_offset (off);
11357 roff += 16;
11358 }
3d58e1fc
AM
11359 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11360 if (r == NULL)
11361 return FALSE;
04bdff6a
AM
11362 if (htab->powerxx_stubs)
11363 r = emit_relocs_for_powerxx_offset (info, r, roff, targ, off, odd);
11364 else
11365 r = emit_relocs_for_offset (info, r, roff, targ, off);
3d58e1fc
AM
11366 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11367 || stub_entry->stub_type == ppc_stub_long_branch_both)
11368 {
11369 ++r;
11370 roff = p - 4 - stub_entry->group->stub_sec->contents;
11371 r->r_offset = roff;
11372 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11373 r->r_addend = targ;
11374 if (stub_entry->h != NULL
11375 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11376 return FALSE;
11377 }
11378 }
11379
04bdff6a
AM
11380 if (!htab->powerxx_stubs
11381 && htab->glink_eh_frame != NULL
11382 && htab->glink_eh_frame->size != 0)
df136d64
AM
11383 {
11384 bfd_byte *base, *eh;
11385 unsigned int lr_used, delta;
11386
11387 base = (htab->glink_eh_frame->contents
11388 + stub_entry->group->eh_base + 17);
11389 eh = base + stub_entry->group->eh_size;
11390 lr_used = stub_entry->stub_offset + 8;
11391 if (stub_entry->stub_type == ppc_stub_long_branch_both
11392 || stub_entry->stub_type == ppc_stub_plt_branch_both
11393 || stub_entry->stub_type == ppc_stub_plt_call_both)
11394 lr_used += 4;
11395 delta = lr_used - stub_entry->group->lr_restore;
11396 stub_entry->group->lr_restore = lr_used + 8;
11397 eh = eh_advance (htab->elf.dynobj, eh, delta);
11398 *eh++ = DW_CFA_register;
11399 *eh++ = 65;
11400 *eh++ = 12;
11401 *eh++ = DW_CFA_advance_loc + 2;
11402 *eh++ = DW_CFA_restore_extended;
11403 *eh++ = 65;
11404 stub_entry->group->eh_size = eh - base;
11405 }
05d0e962
AM
11406 break;
11407
721956f4 11408 case ppc_stub_plt_call:
794e51c0 11409 case ppc_stub_plt_call_r2save:
e054468f 11410 if (stub_entry->h != NULL
b31867b6
AM
11411 && stub_entry->h->is_func_descriptor
11412 && stub_entry->h->oh != NULL)
c862ae31 11413 {
b31867b6
AM
11414 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11415
11416 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11417 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11418 if (fh->elf.root.type == bfd_link_hash_undefined
11419 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11420 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11421 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11422 }
11423
721956f4 11424 /* Now build the stub. */
1aa42141
AM
11425 targ = stub_entry->plt_ent->plt.offset & ~1;
11426 if (targ >= (bfd_vma) -2)
721956f4
AM
11427 abort ();
11428
33e44f2e 11429 plt = htab->elf.splt;
25f23106
AM
11430 if (!htab->elf.dynamic_sections_created
11431 || stub_entry->h == NULL
11432 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11433 {
11434 if (stub_entry->symtype == STT_GNU_IFUNC)
11435 plt = htab->elf.iplt;
11436 else
11437 plt = htab->pltlocal;
11438 }
1aa42141 11439 targ += plt->output_offset + plt->output_section->vma;
e054468f 11440
1aa42141
AM
11441 off = (elf_gp (info->output_bfd)
11442 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11443 off = targ - off;
721956f4 11444
ad8e1ba5 11445 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11446 {
25f53a85 11447 info->callbacks->einfo
695344c0 11448 /* xgettext:c-format */
c1c8c1ef 11449 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11450 stub_entry->h != NULL
11451 ? stub_entry->h->elf.root.root.string
11452 : "<local sym>");
721956f4 11453 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11454 htab->stub_error = TRUE;
11455 return FALSE;
721956f4
AM
11456 }
11457
176a0d42
AM
11458 r = NULL;
11459 if (info->emitrelocations)
11460 {
6f20ed8a 11461 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11462 ((PPC_HA (off) != 0)
11463 + (htab->opd_abi
e7d1c40c 11464 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11465 && PPC_HA (off + 16) == PPC_HA (off))
11466 : 1)));
176a0d42
AM
11467 if (r == NULL)
11468 return FALSE;
6f20ed8a 11469 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11470 if (bfd_big_endian (info->output_bfd))
11471 r[0].r_offset += 2;
1aa42141 11472 r[0].r_addend = targ;
176a0d42 11473 }
a7f2871e
AM
11474 if (stub_entry->h != NULL
11475 && (stub_entry->h == htab->tls_get_addr_fd
11476 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11477 && htab->params->tls_get_addr_opt)
794e51c0 11478 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11479 else
794e51c0 11480 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11481 break;
11482
a4b6fadd
AM
11483 case ppc_stub_save_res:
11484 return TRUE;
11485
721956f4
AM
11486 default:
11487 BFD_FAIL ();
b34976b6 11488 return FALSE;
721956f4
AM
11489 }
11490
1aa42141 11491 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 11492
e7d1c40c 11493 if (htab->params->emit_stub_syms)
97b639ba
AM
11494 {
11495 struct elf_link_hash_entry *h;
ee75fd95
AM
11496 size_t len1, len2;
11497 char *name;
11498 const char *const stub_str[] = { "long_branch",
05d0e962
AM
11499 "long_branch",
11500 "long_branch",
3f6ff479
AM
11501 "long_branch",
11502 "plt_branch",
ee75fd95 11503 "plt_branch",
05d0e962
AM
11504 "plt_branch",
11505 "plt_branch",
11506 "plt_call",
11507 "plt_call",
794e51c0 11508 "plt_call",
ee75fd95
AM
11509 "plt_call" };
11510
11511 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11512 len2 = strlen (stub_entry->root.string);
11513 name = bfd_malloc (len1 + len2 + 2);
11514 if (name == NULL)
11515 return FALSE;
11516 memcpy (name, stub_entry->root.string, 9);
11517 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11518 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11519 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11520 if (h == NULL)
11521 return FALSE;
11522 if (h->root.type == bfd_link_hash_new)
11523 {
11524 h->root.type = bfd_link_hash_defined;
6f20ed8a 11525 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11526 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11527 h->ref_regular = 1;
11528 h->def_regular = 1;
11529 h->ref_regular_nonweak = 1;
11530 h->forced_local = 1;
11531 h->non_elf = 0;
2ec55de3 11532 h->root.linker_def = 1;
97b639ba
AM
11533 }
11534 }
11535
b34976b6 11536 return TRUE;
721956f4
AM
11537}
11538
11539/* As above, but don't actually build the stub. Just bump offset so
11540 we know stub section sizes, and select plt_branch stubs where
11541 long_branch stubs won't do. */
11542
b34976b6 11543static bfd_boolean
4ce794b7 11544ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11545{
11546 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11547 struct bfd_link_info *info;
721956f4 11548 struct ppc_link_hash_table *htab;
f891966f
AM
11549 asection *plt;
11550 bfd_vma targ, off, r2off;
04bdff6a 11551 unsigned int size, extra, lr_used, delta, odd;
721956f4
AM
11552
11553 /* Massage our args to the form they really have. */
11554 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11555 info = in_arg;
11556
11557 htab = ppc_hash_table (info);
4dfe6ac6
NC
11558 if (htab == NULL)
11559 return FALSE;
721956f4 11560
1aa42141
AM
11561 /* Make a note of the offset within the stubs for this entry. */
11562 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11563
a4b6fadd
AM
11564 if (stub_entry->h != NULL
11565 && stub_entry->h->save_res
11566 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11567 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11568 {
11569 /* Don't make stubs to out-of-line register save/restore
11570 functions. Instead, emit copies of the functions. */
11571 stub_entry->group->needs_save_res = 1;
11572 stub_entry->stub_type = ppc_stub_save_res;
11573 return TRUE;
11574 }
11575
f891966f 11576 switch (stub_entry->stub_type)
721956f4 11577 {
f891966f
AM
11578 case ppc_stub_plt_branch:
11579 case ppc_stub_plt_branch_r2off:
11580 /* Reset the stub type from the plt branch variant in case we now
11581 can reach with a shorter stub. */
11582 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11583 /* Fall through. */
11584 case ppc_stub_long_branch:
11585 case ppc_stub_long_branch_r2off:
1aa42141
AM
11586 targ = (stub_entry->target_value
11587 + stub_entry->target_section->output_offset
11588 + stub_entry->target_section->output_section->vma);
f891966f 11589 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
1aa42141
AM
11590 off = (stub_entry->stub_offset
11591 + stub_entry->group->stub_sec->output_offset
11592 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11593
ad8e1ba5 11594 size = 4;
f891966f 11595 r2off = 0;
ad8e1ba5
AM
11596 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11597 {
25f53a85 11598 r2off = get_r2off (info, stub_entry);
a7c49797 11599 if (r2off == (bfd_vma) -1)
aa374f67
AM
11600 {
11601 htab->stub_error = TRUE;
11602 return FALSE;
11603 }
a7c49797 11604 size = 8;
ac2df442 11605 if (PPC_HA (r2off) != 0)
a7c49797
AM
11606 size += 4;
11607 if (PPC_LO (r2off) != 0)
11608 size += 4;
1aa42141 11609 off += size - 4;
ad8e1ba5 11610 }
1aa42141 11611 off = targ - off;
ad8e1ba5 11612
f891966f
AM
11613 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11614 Do the same for -R objects without function descriptors. */
11615 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11616 && r2off == 0
11617 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11618 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 11619 {
f891966f 11620 struct ppc_branch_hash_entry *br_entry;
df136d64 11621
f891966f
AM
11622 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11623 stub_entry->root.string + 9,
11624 TRUE, FALSE);
11625 if (br_entry == NULL)
721956f4 11626 {
f891966f
AM
11627 _bfd_error_handler (_("can't build branch stub `%s'"),
11628 stub_entry->root.string);
11629 htab->stub_error = TRUE;
11630 return FALSE;
721956f4
AM
11631 }
11632
f891966f 11633 if (br_entry->iter != htab->stub_iteration)
721956f4 11634 {
f891966f
AM
11635 br_entry->iter = htab->stub_iteration;
11636 br_entry->offset = htab->brlt->size;
11637 htab->brlt->size += 8;
63bc6f6c 11638
f891966f
AM
11639 if (htab->relbrlt != NULL)
11640 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11641 else if (info->emitrelocations)
84f5d08e 11642 {
f891966f
AM
11643 htab->brlt->reloc_count += 1;
11644 htab->brlt->flags |= SEC_RELOC;
05d0e962 11645 }
f891966f 11646 }
ac2df442 11647
f891966f
AM
11648 targ = (br_entry->offset
11649 + htab->brlt->output_offset
11650 + htab->brlt->output_section->vma);
11651 off = (elf_gp (info->output_bfd)
11652 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11653 off = targ - off;
176a0d42 11654
f891966f
AM
11655 if (info->emitrelocations)
11656 {
11657 stub_entry->group->stub_sec->reloc_count
11658 += 1 + (PPC_HA (off) != 0);
11659 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11660 }
05d0e962 11661
f891966f
AM
11662 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11663 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11664 {
11665 size = 12;
11666 if (PPC_HA (off) != 0)
11667 size = 16;
ac2df442 11668 }
f891966f 11669 else
ac2df442 11670 {
f891966f
AM
11671 size = 16;
11672 if (PPC_HA (off) != 0)
11673 size += 4;
11674
11675 if (PPC_HA (r2off) != 0)
11676 size += 4;
11677 if (PPC_LO (r2off) != 0)
11678 size += 4;
ac2df442 11679 }
721956f4 11680 }
f891966f
AM
11681 else if (info->emitrelocations)
11682 {
11683 stub_entry->group->stub_sec->reloc_count += 1;
11684 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11685 }
11686 break;
11687
11688 case ppc_stub_plt_branch_notoc:
11689 case ppc_stub_plt_branch_both:
11690 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11691 /* Fall through. */
11692 case ppc_stub_long_branch_notoc:
11693 case ppc_stub_long_branch_both:
11694 off = (stub_entry->stub_offset
11695 + stub_entry->group->stub_sec->output_offset
11696 + stub_entry->group->stub_sec->output_section->vma);
11697 size = 0;
11698 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11699 size = 4;
11700 off += size;
11701 targ = (stub_entry->target_value
11702 + stub_entry->target_section->output_offset
11703 + stub_entry->target_section->output_section->vma);
04bdff6a 11704 odd = off & 4;
f891966f
AM
11705 off = targ - off;
11706
3d58e1fc
AM
11707 if (info->emitrelocations)
11708 {
04bdff6a
AM
11709 unsigned int num_rel;
11710 if (htab->powerxx_stubs)
11711 num_rel = num_relocs_for_powerxx_offset (off, odd);
11712 else
11713 num_rel = num_relocs_for_offset (off - 8);
11714 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
11715 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11716 }
11717
04bdff6a
AM
11718 if (htab->powerxx_stubs)
11719 extra = size_powerxx_offset (off, odd);
11720 else
11721 extra = size_offset (off - 8);
f891966f
AM
11722 /* Include branch insn plus those in the offset sequence. */
11723 size += 4 + extra;
11724 /* The branch insn is at the end, or "extra" bytes along. So
11725 its offset will be "extra" bytes less that that already
11726 calculated. */
11727 off -= extra;
11728
04bdff6a
AM
11729 if (!htab->powerxx_stubs)
11730 {
11731 /* After the bcl, lr has been modified so we need to emit
11732 .eh_frame info saying the return address is in r12. */
11733 lr_used = stub_entry->stub_offset + 8;
11734 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11735 lr_used += 4;
11736 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11737 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11738 DW_CFA_restore_extended 65. */
11739 delta = lr_used - stub_entry->group->lr_restore;
11740 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11741 stub_entry->group->lr_restore = lr_used + 8;
11742 }
f891966f
AM
11743
11744 /* If the branch can't reach, use a plt_branch. */
11745 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11746 {
11747 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11748 - ppc_stub_long_branch_notoc);
11749 size += 4;
11750 }
3d58e1fc
AM
11751 else if (info->emitrelocations)
11752 stub_entry->group->stub_sec->reloc_count +=1;
f891966f
AM
11753 break;
11754
11755 case ppc_stub_plt_call_notoc:
11756 case ppc_stub_plt_call_both:
11757 off = (stub_entry->stub_offset
11758 + stub_entry->group->stub_sec->output_offset
11759 + stub_entry->group->stub_sec->output_section->vma);
11760 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11761 off += 4;
11762 targ = stub_entry->plt_ent->plt.offset & ~1;
11763 if (targ >= (bfd_vma) -2)
11764 abort ();
11765
11766 plt = htab->elf.splt;
11767 if (!htab->elf.dynamic_sections_created
11768 || stub_entry->h == NULL
11769 || stub_entry->h->elf.dynindx == -1)
11770 {
11771 if (stub_entry->symtype == STT_GNU_IFUNC)
11772 plt = htab->elf.iplt;
11773 else
11774 plt = htab->pltlocal;
11775 }
11776 targ += plt->output_offset + plt->output_section->vma;
04bdff6a 11777 odd = off & 4;
f891966f
AM
11778 off = targ - off;
11779
11780 if (htab->params->plt_stub_align != 0)
11781 {
11782 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11783
11784 stub_entry->group->stub_sec->size += pad;
11785 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11786 off -= pad;
11787 }
11788
3d58e1fc
AM
11789 if (info->emitrelocations)
11790 {
04bdff6a
AM
11791 unsigned int num_rel;
11792 if (htab->powerxx_stubs)
11793 num_rel = num_relocs_for_powerxx_offset (off, odd);
11794 else
11795 num_rel = num_relocs_for_offset (off - 8);
11796 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
11797 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11798 }
11799
f891966f
AM
11800 size = plt_stub_size (htab, stub_entry, off);
11801
04bdff6a
AM
11802 if (!htab->powerxx_stubs)
11803 {
11804 /* After the bcl, lr has been modified so we need to emit
11805 .eh_frame info saying the return address is in r12. */
11806 lr_used = stub_entry->stub_offset + 8;
11807 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11808 lr_used += 4;
11809 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11810 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11811 DW_CFA_restore_extended 65. */
11812 delta = lr_used - stub_entry->group->lr_restore;
11813 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11814 stub_entry->group->lr_restore = lr_used + 8;
11815 }
f891966f
AM
11816 break;
11817
11818 case ppc_stub_plt_call:
11819 case ppc_stub_plt_call_r2save:
11820 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11821 if (targ >= (bfd_vma) -2)
11822 abort ();
11823 plt = htab->elf.splt;
11824 if (!htab->elf.dynamic_sections_created
11825 || stub_entry->h == NULL
11826 || stub_entry->h->elf.dynindx == -1)
11827 {
11828 if (stub_entry->symtype == STT_GNU_IFUNC)
11829 plt = htab->elf.iplt;
11830 else
11831 plt = htab->pltlocal;
11832 }
11833 targ += plt->output_offset + plt->output_section->vma;
11834
11835 off = (elf_gp (info->output_bfd)
11836 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11837 off = targ - off;
11838
11839 if (htab->params->plt_stub_align != 0)
11840 {
11841 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11842
11843 stub_entry->group->stub_sec->size += pad;
11844 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11845 }
11846
11847 if (info->emitrelocations)
11848 {
11849 stub_entry->group->stub_sec->reloc_count
11850 += ((PPC_HA (off) != 0)
11851 + (htab->opd_abi
11852 ? 2 + (htab->params->plt_static_chain
11853 && PPC_HA (off + 16) == PPC_HA (off))
11854 : 1));
11855 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11856 }
11857
11858 size = plt_stub_size (htab, stub_entry, off);
11859
11860 if (stub_entry->h != NULL
11861 && (stub_entry->h == htab->tls_get_addr_fd
11862 || stub_entry->h == htab->tls_get_addr)
11863 && htab->params->tls_get_addr_opt
11864 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11865 {
11866 /* After the bctrl, lr has been modified so we need to
11867 emit .eh_frame info saying the return address is
11868 on the stack. In fact we put the EH info specifying
11869 that the return address is on the stack *at* the
11870 call rather than after it, because the EH info for a
11871 call needs to be specified by that point.
11872 See libgcc/unwind-dw2.c execute_cfa_program. */
11873 lr_used = stub_entry->stub_offset + size - 20;
11874 /* The eh_frame info will consist of a DW_CFA_advance_loc
11875 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11876 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11877 delta = lr_used - stub_entry->group->lr_restore;
11878 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11879 stub_entry->group->lr_restore = size - 4;
11880 }
11881 break;
11882
11883 default:
11884 BFD_FAIL ();
11885 return FALSE;
721956f4
AM
11886 }
11887
6f20ed8a 11888 stub_entry->group->stub_sec->size += size;
b34976b6 11889 return TRUE;
721956f4
AM
11890}
11891
11892/* Set up various things so that we can make a list of input sections
11893 for each output section included in the link. Returns -1 on error,
cedb70c5 11894 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11895
11896int
e7d1c40c 11897ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11898{
6f20ed8a 11899 unsigned int id;
721956f4
AM
11900 bfd_size_type amt;
11901 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11902
4dfe6ac6
NC
11903 if (htab == NULL)
11904 return -1;
4c52953f 11905
7cf7fcc8 11906 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
11907 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11908 htab->sec_info = bfd_zmalloc (amt);
11909 if (htab->sec_info == NULL)
721956f4
AM
11910 return -1;
11911
3d6f9012
AM
11912 /* Set toc_off for com, und, abs and ind sections. */
11913 for (id = 0; id < 3; id++)
6f20ed8a 11914 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11915
721956f4
AM
11916 return 1;
11917}
11918
927be08e
AM
11919/* Set up for first pass at multitoc partitioning. */
11920
11921void
11922ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11923{
11924 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11925
1c865ab2 11926 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11927 htab->toc_bfd = NULL;
11928 htab->toc_first_sec = NULL;
11929}
11930
e717da7e
AM
11931/* The linker repeatedly calls this function for each TOC input section
11932 and linker generated GOT section. Group input bfds such that the toc
927be08e 11933 within a group is less than 64k in size. */
ad8e1ba5 11934
927be08e 11935bfd_boolean
4ce794b7 11936ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11937{
11938 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11939 bfd_vma addr, off, limit;
ad8e1ba5 11940
4dfe6ac6
NC
11941 if (htab == NULL)
11942 return FALSE;
11943
927be08e 11944 if (!htab->second_toc_pass)
4c52953f 11945 {
927be08e 11946 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11947 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11948
11949 if (new_bfd)
bf102f86
AM
11950 {
11951 htab->toc_bfd = isec->owner;
11952 htab->toc_first_sec = isec;
11953 }
927be08e 11954
bf102f86
AM
11955 addr = isec->output_offset + isec->output_section->vma;
11956 off = addr - htab->toc_curr;
d77c8a4b
AM
11957 limit = 0x80008000;
11958 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11959 limit = 0x10000;
11960 if (off + isec->size > limit)
bf102f86
AM
11961 {
11962 addr = (htab->toc_first_sec->output_offset
11963 + htab->toc_first_sec->output_section->vma);
11964 htab->toc_curr = addr;
a27e685f 11965 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11966 }
99877b66 11967
927be08e
AM
11968 /* toc_curr is the base address of this toc group. Set elf_gp
11969 for the input section to be the offset relative to the
11970 output toc base plus 0x8000. Making the input elf_gp an
11971 offset allows us to move the toc as a whole without
11972 recalculating input elf_gp. */
06bcf541 11973 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
11974 off += TOC_BASE_OFF;
11975
11976 /* Die if someone uses a linker script that doesn't keep input
11977 file .toc and .got together. */
a4fd3de5
AM
11978 if (new_bfd
11979 && elf_gp (isec->owner) != 0
927be08e
AM
11980 && elf_gp (isec->owner) != off)
11981 return FALSE;
11982
11983 elf_gp (isec->owner) = off;
11984 return TRUE;
4c52953f 11985 }
927be08e
AM
11986
11987 /* During the second pass toc_first_sec points to the start of
11988 a toc group, and toc_curr is used to track the old elf_gp.
11989 We use toc_bfd to ensure we only look at each bfd once. */
11990 if (htab->toc_bfd == isec->owner)
11991 return TRUE;
11992 htab->toc_bfd = isec->owner;
11993
11994 if (htab->toc_first_sec == NULL
11995 || htab->toc_curr != elf_gp (isec->owner))
11996 {
11997 htab->toc_curr = elf_gp (isec->owner);
11998 htab->toc_first_sec = isec;
11999 }
12000 addr = (htab->toc_first_sec->output_offset
12001 + htab->toc_first_sec->output_section->vma);
06bcf541 12002 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
12003 elf_gp (isec->owner) = off;
12004
12005 return TRUE;
ad8e1ba5
AM
12006}
12007
927be08e
AM
12008/* Called via elf_link_hash_traverse to merge GOT entries for global
12009 symbol H. */
12010
12011static bfd_boolean
12012merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12013{
12014 if (h->root.type == bfd_link_hash_indirect)
12015 return TRUE;
12016
927be08e
AM
12017 merge_got_entries (&h->got.glist);
12018
12019 return TRUE;
12020}
12021
12022/* Called via elf_link_hash_traverse to allocate GOT entries for global
12023 symbol H. */
12024
12025static bfd_boolean
12026reallocate_got (struct elf_link_hash_entry *h, void *inf)
12027{
12028 struct got_entry *gent;
12029
12030 if (h->root.type == bfd_link_hash_indirect)
12031 return TRUE;
12032
927be08e
AM
12033 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12034 if (!gent->is_indirect)
12035 allocate_got (h, (struct bfd_link_info *) inf, gent);
12036 return TRUE;
12037}
12038
12039/* Called on the first multitoc pass after the last call to
12040 ppc64_elf_next_toc_section. This function removes duplicate GOT
12041 entries. */
12042
12043bfd_boolean
12044ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12045{
12046 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
12047 struct bfd *ibfd, *ibfd2;
12048 bfd_boolean done_something;
12049
12050 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12051
7865406b
AM
12052 if (!htab->do_multi_toc)
12053 return FALSE;
12054
d0fae19d 12055 /* Merge global sym got entries within a toc group. */
927be08e
AM
12056 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12057
12058 /* And tlsld_got. */
c72f2fb2 12059 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12060 {
12061 struct got_entry *ent, *ent2;
12062
12063 if (!is_ppc64_elf (ibfd))
12064 continue;
12065
12066 ent = ppc64_tlsld_got (ibfd);
12067 if (!ent->is_indirect
12068 && ent->got.offset != (bfd_vma) -1)
12069 {
c72f2fb2 12070 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12071 {
12072 if (!is_ppc64_elf (ibfd2))
12073 continue;
12074
12075 ent2 = ppc64_tlsld_got (ibfd2);
12076 if (!ent2->is_indirect
12077 && ent2->got.offset != (bfd_vma) -1
12078 && elf_gp (ibfd2) == elf_gp (ibfd))
12079 {
12080 ent2->is_indirect = TRUE;
12081 ent2->got.ent = ent;
12082 }
12083 }
12084 }
12085 }
12086
12087 /* Zap sizes of got sections. */
33e44f2e
AM
12088 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12089 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12090 htab->got_reli_size = 0;
12091
c72f2fb2 12092 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12093 {
12094 asection *got, *relgot;
12095
12096 if (!is_ppc64_elf (ibfd))
12097 continue;
12098
12099 got = ppc64_elf_tdata (ibfd)->got;
12100 if (got != NULL)
12101 {
12102 got->rawsize = got->size;
12103 got->size = 0;
12104 relgot = ppc64_elf_tdata (ibfd)->relgot;
12105 relgot->rawsize = relgot->size;
12106 relgot->size = 0;
12107 }
12108 }
12109
12110 /* Now reallocate the got, local syms first. We don't need to
12111 allocate section contents again since we never increase size. */
c72f2fb2 12112 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12113 {
12114 struct got_entry **lgot_ents;
12115 struct got_entry **end_lgot_ents;
12116 struct plt_entry **local_plt;
12117 struct plt_entry **end_local_plt;
f961d9dd 12118 unsigned char *lgot_masks;
927be08e
AM
12119 bfd_size_type locsymcount;
12120 Elf_Internal_Shdr *symtab_hdr;
19e08130 12121 asection *s;
927be08e
AM
12122
12123 if (!is_ppc64_elf (ibfd))
12124 continue;
12125
12126 lgot_ents = elf_local_got_ents (ibfd);
12127 if (!lgot_ents)
12128 continue;
12129
12130 symtab_hdr = &elf_symtab_hdr (ibfd);
12131 locsymcount = symtab_hdr->sh_info;
12132 end_lgot_ents = lgot_ents + locsymcount;
12133 local_plt = (struct plt_entry **) end_lgot_ents;
12134 end_local_plt = local_plt + locsymcount;
f961d9dd 12135 lgot_masks = (unsigned char *) end_local_plt;
927be08e 12136 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
12137 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12138 {
12139 struct got_entry *ent;
12140
12141 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12142 {
19e08130
AM
12143 unsigned int ent_size = 8;
12144 unsigned int rel_size = sizeof (Elf64_External_Rela);
12145
d0fae19d
AM
12146 ent->got.offset = s->size;
12147 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12148 {
19e08130
AM
12149 ent_size *= 2;
12150 rel_size *= 2;
12151 }
12152 s->size += ent_size;
37da22e5 12153 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12154 {
33e44f2e 12155 htab->elf.irelplt->size += rel_size;
19e08130
AM
12156 htab->got_reli_size += rel_size;
12157 }
6a3858a6
AM
12158 else if (bfd_link_pic (info)
12159 && !((ent->tls_type & TLS_TPREL) != 0
12160 && bfd_link_executable (info)))
19e08130
AM
12161 {
12162 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12163 srel->size += rel_size;
d0fae19d
AM
12164 }
12165 }
927be08e
AM
12166 }
12167 }
12168
12169 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12170
c72f2fb2 12171 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12172 {
12173 struct got_entry *ent;
12174
12175 if (!is_ppc64_elf (ibfd))
12176 continue;
12177
12178 ent = ppc64_tlsld_got (ibfd);
12179 if (!ent->is_indirect
12180 && ent->got.offset != (bfd_vma) -1)
12181 {
12182 asection *s = ppc64_elf_tdata (ibfd)->got;
12183 ent->got.offset = s->size;
12184 s->size += 16;
0e1862bb 12185 if (bfd_link_pic (info))
927be08e
AM
12186 {
12187 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12188 srel->size += sizeof (Elf64_External_Rela);
12189 }
12190 }
12191 }
12192
33e44f2e 12193 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12194 if (!done_something)
c72f2fb2 12195 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12196 {
12197 asection *got;
12198
12199 if (!is_ppc64_elf (ibfd))
12200 continue;
12201
12202 got = ppc64_elf_tdata (ibfd)->got;
12203 if (got != NULL)
12204 {
12205 done_something = got->rawsize != got->size;
12206 if (done_something)
12207 break;
12208 }
12209 }
12210
12211 if (done_something)
e7d1c40c 12212 (*htab->params->layout_sections_again) ();
927be08e
AM
12213
12214 /* Set up for second pass over toc sections to recalculate elf_gp
12215 on input sections. */
12216 htab->toc_bfd = NULL;
12217 htab->toc_first_sec = NULL;
12218 htab->second_toc_pass = TRUE;
12219 return done_something;
12220}
12221
12222/* Called after second pass of multitoc partitioning. */
12223
12224void
12225ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12226{
12227 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12228
12229 /* After the second pass, toc_curr tracks the TOC offset used
12230 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12231 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12232}
12233
9b5ecbd0
AM
12234/* No toc references were found in ISEC. If the code in ISEC makes no
12235 calls, then there's no need to use toc adjusting stubs when branching
12236 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12237 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12238 needed, and 2 if a cyclical call-graph was found but no other reason
12239 for a stub was detected. If called from the top level, a return of
12240 2 means the same as a return of 0. */
9b5ecbd0
AM
12241
12242static int
4ce794b7 12243toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12244{
9b5ecbd0 12245 int ret;
70cc837d
AM
12246
12247 /* Mark this section as checked. */
12248 isec->call_check_done = 1;
9b5ecbd0 12249
772119ce
AM
12250 /* We know none of our code bearing sections will need toc stubs. */
12251 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12252 return 0;
12253
eea6121a 12254 if (isec->size == 0)
082c50f8
AM
12255 return 0;
12256
4c52953f
AM
12257 if (isec->output_section == NULL)
12258 return 0;
12259
4c52953f 12260 ret = 0;
70cc837d 12261 if (isec->reloc_count != 0)
9b5ecbd0 12262 {
70cc837d
AM
12263 Elf_Internal_Rela *relstart, *rel;
12264 Elf_Internal_Sym *local_syms;
12265 struct ppc_link_hash_table *htab;
2917689a 12266
70cc837d
AM
12267 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12268 info->keep_memory);
12269 if (relstart == NULL)
12270 return -1;
90aecf7a 12271
70cc837d
AM
12272 /* Look for branches to outside of this section. */
12273 local_syms = NULL;
12274 htab = ppc_hash_table (info);
12275 if (htab == NULL)
12276 return -1;
4c52953f 12277
70cc837d 12278 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12279 {
70cc837d
AM
12280 enum elf_ppc64_reloc_type r_type;
12281 unsigned long r_symndx;
12282 struct elf_link_hash_entry *h;
12283 struct ppc_link_hash_entry *eh;
12284 Elf_Internal_Sym *sym;
12285 asection *sym_sec;
12286 struct _opd_sec_data *opd;
12287 bfd_vma sym_value;
12288 bfd_vma dest;
12289
12290 r_type = ELF64_R_TYPE (rel->r_info);
12291 if (r_type != R_PPC64_REL24
05d0e962 12292 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
12293 && r_type != R_PPC64_REL14
12294 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 12295 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
12296 && r_type != R_PPC64_PLTCALL
12297 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 12298 continue;
4c52953f 12299
70cc837d
AM
12300 r_symndx = ELF64_R_SYM (rel->r_info);
12301 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12302 isec->owner))
4c52953f 12303 {
70cc837d
AM
12304 ret = -1;
12305 break;
12306 }
4c52953f 12307
70cc837d
AM
12308 /* Calls to dynamic lib functions go through a plt call stub
12309 that uses r2. */
12310 eh = (struct ppc_link_hash_entry *) h;
12311 if (eh != NULL
12312 && (eh->elf.plt.plist != NULL
12313 || (eh->oh != NULL
12314 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12315 {
12316 ret = 1;
12317 break;
4c52953f
AM
12318 }
12319
70cc837d
AM
12320 if (sym_sec == NULL)
12321 /* Ignore other undefined symbols. */
4c52953f 12322 continue;
4c52953f 12323
70cc837d
AM
12324 /* Assume branches to other sections not included in the
12325 link need stubs too, to cover -R and absolute syms. */
12326 if (sym_sec->output_section == NULL)
12327 {
12328 ret = 1;
12329 break;
12330 }
4c52953f 12331
70cc837d
AM
12332 if (h == NULL)
12333 sym_value = sym->st_value;
12334 else
12335 {
12336 if (h->root.type != bfd_link_hash_defined
12337 && h->root.type != bfd_link_hash_defweak)
12338 abort ();
12339 sym_value = h->root.u.def.value;
12340 }
12341 sym_value += rel->r_addend;
4c52953f 12342
70cc837d
AM
12343 /* If this branch reloc uses an opd sym, find the code section. */
12344 opd = get_opd_info (sym_sec);
12345 if (opd != NULL)
12346 {
12347 if (h == NULL && opd->adjust != NULL)
12348 {
12349 long adjust;
4c52953f 12350
92a9c616 12351 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12352 if (adjust == -1)
12353 /* Assume deleted functions won't ever be called. */
12354 continue;
12355 sym_value += adjust;
12356 }
4c52953f 12357
aef36ac1
AM
12358 dest = opd_entry_value (sym_sec, sym_value,
12359 &sym_sec, NULL, FALSE);
70cc837d
AM
12360 if (dest == (bfd_vma) -1)
12361 continue;
12362 }
12363 else
12364 dest = (sym_value
12365 + sym_sec->output_offset
12366 + sym_sec->output_section->vma);
4c52953f 12367
70cc837d
AM
12368 /* Ignore branch to self. */
12369 if (sym_sec == isec)
12370 continue;
4c52953f 12371
70cc837d
AM
12372 /* If the called function uses the toc, we need a stub. */
12373 if (sym_sec->has_toc_reloc
12374 || sym_sec->makes_toc_func_call)
4c52953f 12375 {
70cc837d 12376 ret = 1;
4c52953f
AM
12377 break;
12378 }
70cc837d
AM
12379
12380 /* Assume any branch that needs a long branch stub might in fact
12381 need a plt_branch stub. A plt_branch stub uses r2. */
12382 else if (dest - (isec->output_offset
12383 + isec->output_section->vma
6911b7dc
AM
12384 + rel->r_offset) + (1 << 25)
12385 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12386 ? h->other
12387 : sym->st_other))
4c52953f 12388 {
70cc837d
AM
12389 ret = 1;
12390 break;
12391 }
12392
12393 /* If calling back to a section in the process of being
12394 tested, we can't say for sure that no toc adjusting stubs
12395 are needed, so don't return zero. */
12396 else if (sym_sec->call_check_in_progress)
12397 ret = 2;
12398
12399 /* Branches to another section that itself doesn't have any TOC
12400 references are OK. Recursively call ourselves to check. */
12401 else if (!sym_sec->call_check_done)
12402 {
12403 int recur;
12404
12405 /* Mark current section as indeterminate, so that other
12406 sections that call back to current won't be marked as
12407 known. */
12408 isec->call_check_in_progress = 1;
12409 recur = toc_adjusting_stub_needed (info, sym_sec);
12410 isec->call_check_in_progress = 0;
12411
4c52953f
AM
12412 if (recur != 0)
12413 {
70cc837d
AM
12414 ret = recur;
12415 if (recur != 2)
12416 break;
4c52953f
AM
12417 }
12418 }
4c52953f 12419 }
70cc837d
AM
12420
12421 if (local_syms != NULL
12422 && (elf_symtab_hdr (isec->owner).contents
12423 != (unsigned char *) local_syms))
12424 free (local_syms);
12425 if (elf_section_data (isec)->relocs != relstart)
12426 free (relstart);
9b5ecbd0
AM
12427 }
12428
70cc837d
AM
12429 if ((ret & 1) == 0
12430 && isec->map_head.s != NULL
12431 && (strcmp (isec->output_section->name, ".init") == 0
12432 || strcmp (isec->output_section->name, ".fini") == 0))
12433 {
12434 if (isec->map_head.s->has_toc_reloc
12435 || isec->map_head.s->makes_toc_func_call)
12436 ret = 1;
12437 else if (!isec->map_head.s->call_check_done)
12438 {
12439 int recur;
12440 isec->call_check_in_progress = 1;
12441 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12442 isec->call_check_in_progress = 0;
12443 if (recur != 0)
12444 ret = recur;
12445 }
12446 }
12447
12448 if (ret == 1)
12449 isec->makes_toc_func_call = 1;
4c52953f 12450
9b5ecbd0
AM
12451 return ret;
12452}
12453
721956f4
AM
12454/* The linker repeatedly calls this function for each input section,
12455 in the order that input sections are linked into output sections.
12456 Build lists of input sections to determine groupings between which
12457 we may insert linker stubs. */
12458
9b5ecbd0 12459bfd_boolean
4ce794b7 12460ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12461{
12462 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12463
4dfe6ac6
NC
12464 if (htab == NULL)
12465 return FALSE;
12466
734b6cf9 12467 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12468 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12469 {
3d6f9012
AM
12470 /* This happens to make the list in reverse order,
12471 which is what we want. */
6f20ed8a
AM
12472 htab->sec_info[isec->id].u.list
12473 = htab->sec_info[isec->output_section->id].u.list;
12474 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12475 }
ad8e1ba5 12476
4c52953f 12477 if (htab->multi_toc_needed)
9b5ecbd0 12478 {
8b974ba3
AM
12479 /* Analyse sections that aren't already flagged as needing a
12480 valid toc pointer. Exclude .fixup for the linux kernel.
12481 .fixup contains branches, but only back to the function that
12482 hit an exception. */
12483 if (!(isec->has_toc_reloc
12484 || (isec->flags & SEC_CODE) == 0
12485 || strcmp (isec->name, ".fixup") == 0
12486 || isec->call_check_done))
12487 {
12488 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12489 return FALSE;
8b974ba3
AM
12490 }
12491 /* Make all sections use the TOC assigned for this object file.
12492 This will be wrong for pasted sections; We fix that in
12493 check_pasted_section(). */
12494 if (elf_gp (isec->owner) != 0)
12495 htab->toc_curr = elf_gp (isec->owner);
12496 }
12497
6f20ed8a 12498 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12499 return TRUE;
721956f4
AM
12500}
12501
70cc837d
AM
12502/* Check that all .init and .fini sections use the same toc, if they
12503 have toc relocs. */
12504
12505static bfd_boolean
12506check_pasted_section (struct bfd_link_info *info, const char *name)
12507{
12508 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12509
12510 if (o != NULL)
12511 {
12512 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12513 bfd_vma toc_off = 0;
12514 asection *i;
12515
12516 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12517 if (i->has_toc_reloc)
12518 {
12519 if (toc_off == 0)
6f20ed8a
AM
12520 toc_off = htab->sec_info[i->id].toc_off;
12521 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12522 return FALSE;
12523 }
6683a28d
AM
12524
12525 if (toc_off == 0)
12526 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12527 if (i->makes_toc_func_call)
12528 {
6f20ed8a 12529 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12530 break;
12531 }
12532
70cc837d
AM
12533 /* Make sure the whole pasted function uses the same toc offset. */
12534 if (toc_off != 0)
12535 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12536 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12537 }
12538 return TRUE;
12539}
12540
12541bfd_boolean
12542ppc64_elf_check_init_fini (struct bfd_link_info *info)
12543{
12544 return (check_pasted_section (info, ".init")
12545 & check_pasted_section (info, ".fini"));
12546}
12547
721956f4
AM
12548/* See whether we can group stub sections together. Grouping stub
12549 sections may result in fewer stubs. More importantly, we need to
12550 put all .init* and .fini* stubs at the beginning of the .init or
12551 .fini output sections respectively, because glibc splits the
12552 _init and _fini functions into multiple parts. Putting a stub in
12553 the middle of a function is not a good idea. */
12554
6f20ed8a
AM
12555static bfd_boolean
12556group_sections (struct bfd_link_info *info,
4ce794b7
AM
12557 bfd_size_type stub_group_size,
12558 bfd_boolean stubs_always_before_branch)
721956f4 12559{
6f20ed8a
AM
12560 struct ppc_link_hash_table *htab;
12561 asection *osec;
7c8fe5c4
AM
12562 bfd_boolean suppress_size_errors;
12563
6f20ed8a
AM
12564 htab = ppc_hash_table (info);
12565 if (htab == NULL)
12566 return FALSE;
12567
7c8fe5c4 12568 suppress_size_errors = FALSE;
7c8fe5c4
AM
12569 if (stub_group_size == 1)
12570 {
12571 /* Default values. */
12572 if (stubs_always_before_branch)
09f92717 12573 stub_group_size = 0x1e00000;
7c8fe5c4 12574 else
09f92717 12575 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12576 suppress_size_errors = TRUE;
12577 }
12578
6f20ed8a 12579 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12580 {
6f20ed8a
AM
12581 asection *tail;
12582
12583 if (osec->id >= htab->sec_info_arr_size)
12584 continue;
12585
12586 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12587 while (tail != NULL)
721956f4 12588 {
734b6cf9
AM
12589 asection *curr;
12590 asection *prev;
12591 bfd_size_type total;
12592 bfd_boolean big_sec;
12593 bfd_vma curr_toc;
6f20ed8a 12594 struct map_stub *group;
09f92717 12595 bfd_size_type group_size;
734b6cf9
AM
12596
12597 curr = tail;
eea6121a 12598 total = tail->size;
09f92717
AM
12599 group_size = (ppc64_elf_section_data (tail) != NULL
12600 && ppc64_elf_section_data (tail)->has_14bit_branch
12601 ? stub_group_size >> 10 : stub_group_size);
12602
12603 big_sec = total > group_size;
7c8fe5c4 12604 if (big_sec && !suppress_size_errors)
695344c0 12605 /* xgettext:c-format */
871b3ab2 12606 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 12607 tail->owner, tail);
6f20ed8a 12608 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12609
6f20ed8a 12610 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12611 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12612 < (ppc64_elf_section_data (prev) != NULL
12613 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12614 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12615 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12616 curr = prev;
12617
12618 /* OK, the size from the start of CURR to the end is less
09f92717 12619 than group_size and thus can be handled by one stub
734b6cf9 12620 section. (or the tail section is itself larger than
09f92717
AM
12621 group_size, in which case we may be toast.) We should
12622 really be keeping track of the total size of stubs added
12623 here, as stubs contribute to the final output section
12624 size. That's a little tricky, and this way will only
12625 break if stubs added make the total size more than 2^25,
12626 ie. for the default stub_group_size, if stubs total more
12627 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12628 group = bfd_alloc (curr->owner, sizeof (*group));
12629 if (group == NULL)
12630 return FALSE;
12631 group->link_sec = curr;
12632 group->stub_sec = NULL;
a4b6fadd 12633 group->needs_save_res = 0;
df136d64
AM
12634 group->lr_restore = 0;
12635 group->eh_size = 0;
12636 group->eh_base = 0;
a4b6fadd
AM
12637 group->next = htab->group;
12638 htab->group = group;
734b6cf9 12639 do
721956f4 12640 {
6f20ed8a 12641 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12642 /* Set up this stub group. */
6f20ed8a 12643 htab->sec_info[tail->id].u.group = group;
721956f4 12644 }
734b6cf9
AM
12645 while (tail != curr && (tail = prev) != NULL);
12646
09f92717 12647 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12648 bytes before the stub section can be handled by it too.
12649 Don't do this if we have a really large section after the
12650 stubs, as adding more stubs increases the chance that
12651 branches may not reach into the stub section. */
12652 if (!stubs_always_before_branch && !big_sec)
12653 {
12654 total = 0;
12655 while (prev != NULL
12656 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12657 < (ppc64_elf_section_data (prev) != NULL
12658 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
12659 ? (group_size = stub_group_size >> 10)
12660 : group_size))
6f20ed8a 12661 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12662 {
12663 tail = prev;
6f20ed8a
AM
12664 prev = htab->sec_info[tail->id].u.list;
12665 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12666 }
12667 }
12668 tail = prev;
721956f4
AM
12669 }
12670 }
6f20ed8a 12671 return TRUE;
721956f4
AM
12672}
12673
58d180e8
AM
12674static const unsigned char glink_eh_frame_cie[] =
12675{
12676 0, 0, 0, 16, /* length. */
12677 0, 0, 0, 0, /* id. */
12678 1, /* CIE version. */
12679 'z', 'R', 0, /* Augmentation string. */
12680 4, /* Code alignment. */
12681 0x78, /* Data alignment. */
12682 65, /* RA reg. */
12683 1, /* Augmentation size. */
12684 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12685 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12686};
12687
d969d15f
AM
12688/* Stripping output sections is normally done before dynamic section
12689 symbols have been allocated. This function is called later, and
12690 handles cases like htab->brlt which is mapped to its own output
12691 section. */
12692
12693static void
12694maybe_strip_output (struct bfd_link_info *info, asection *isec)
12695{
12696 if (isec->size == 0
12697 && isec->output_section->size == 0
53d8967a 12698 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12699 && !bfd_section_removed_from_list (info->output_bfd,
12700 isec->output_section)
12701 && elf_section_data (isec->output_section)->dynindx == 0)
12702 {
12703 isec->output_section->flags |= SEC_EXCLUDE;
12704 bfd_section_list_remove (info->output_bfd, isec->output_section);
12705 info->output_bfd->section_count--;
12706 }
12707}
12708
721956f4
AM
12709/* Determine and set the size of the stub section for a final link.
12710
12711 The basic idea here is to examine all the relocations looking for
12712 PC-relative calls to a target that is unreachable with a "bl"
12713 instruction. */
12714
b34976b6 12715bfd_boolean
e7d1c40c 12716ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12717{
12718 bfd_size_type stub_group_size;
b34976b6 12719 bfd_boolean stubs_always_before_branch;
721956f4
AM
12720 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12721
4dfe6ac6
NC
12722 if (htab == NULL)
12723 return FALSE;
12724
0e1862bb 12725 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12726 htab->params->plt_thread_safe = 1;
b9e5796b 12727 if (!htab->opd_abi)
e7d1c40c
AM
12728 htab->params->plt_thread_safe = 0;
12729 else if (htab->params->plt_thread_safe == -1)
794e51c0 12730 {
e2458743 12731 static const char *const thread_starter[] =
794e51c0
AM
12732 {
12733 "pthread_create",
12734 /* libstdc++ */
12735 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12736 /* librt */
12737 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12738 "mq_notify", "create_timer",
12739 /* libanl */
12740 "getaddrinfo_a",
12741 /* libgomp */
2300b5a1 12742 "GOMP_parallel",
794e51c0 12743 "GOMP_parallel_start",
2300b5a1 12744 "GOMP_parallel_loop_static",
794e51c0 12745 "GOMP_parallel_loop_static_start",
2300b5a1 12746 "GOMP_parallel_loop_dynamic",
794e51c0 12747 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12748 "GOMP_parallel_loop_guided",
794e51c0 12749 "GOMP_parallel_loop_guided_start",
2300b5a1 12750 "GOMP_parallel_loop_runtime",
794e51c0 12751 "GOMP_parallel_loop_runtime_start",
2300b5a1 12752 "GOMP_parallel_sections",
68ffbac6 12753 "GOMP_parallel_sections_start",
f9dffbf0
AM
12754 /* libgo */
12755 "__go_go",
794e51c0
AM
12756 };
12757 unsigned i;
12758
a4b6fadd 12759 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12760 {
12761 struct elf_link_hash_entry *h;
12762 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12763 FALSE, FALSE, TRUE);
e7d1c40c
AM
12764 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12765 if (htab->params->plt_thread_safe)
794e51c0
AM
12766 break;
12767 }
12768 }
e7d1c40c
AM
12769 stubs_always_before_branch = htab->params->group_size < 0;
12770 if (htab->params->group_size < 0)
12771 stub_group_size = -htab->params->group_size;
721956f4 12772 else
e7d1c40c 12773 stub_group_size = htab->params->group_size;
721956f4 12774
6f20ed8a
AM
12775 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12776 return FALSE;
721956f4 12777
c9301e31
AM
12778#define STUB_SHRINK_ITER 20
12779 /* Loop until no stubs added. After iteration 20 of this loop we may
12780 exit on a stub section shrinking. This is to break out of a
12781 pathological case where adding stubs on one iteration decreases
12782 section gaps (perhaps due to alignment), which then requires
12783 fewer or smaller stubs on the next iteration. */
12784
721956f4
AM
12785 while (1)
12786 {
12787 bfd *input_bfd;
12788 unsigned int bfd_indx;
a4b6fadd 12789 struct map_stub *group;
721956f4
AM
12790
12791 htab->stub_iteration += 1;
721956f4
AM
12792
12793 for (input_bfd = info->input_bfds, bfd_indx = 0;
12794 input_bfd != NULL;
c72f2fb2 12795 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12796 {
12797 Elf_Internal_Shdr *symtab_hdr;
12798 asection *section;
6cdc0ccc 12799 Elf_Internal_Sym *local_syms = NULL;
721956f4 12800
0c8d6e5c 12801 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12802 continue;
12803
721956f4 12804 /* We'll need the symbol table in a second. */
0ffa91dd 12805 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12806 if (symtab_hdr->sh_info == 0)
12807 continue;
12808
721956f4
AM
12809 /* Walk over each section attached to the input bfd. */
12810 for (section = input_bfd->sections;
12811 section != NULL;
12812 section = section->next)
12813 {
721956f4 12814 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12815
12816 /* If there aren't any relocs, then there's nothing more
12817 to do. */
12818 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12819 || (section->flags & SEC_ALLOC) == 0
12820 || (section->flags & SEC_LOAD) == 0
12821 || (section->flags & SEC_CODE) == 0
721956f4
AM
12822 || section->reloc_count == 0)
12823 continue;
12824
12825 /* If this section is a link-once section that will be
12826 discarded, then don't create any stubs. */
12827 if (section->output_section == NULL
927be08e 12828 || section->output_section->owner != info->output_bfd)
721956f4
AM
12829 continue;
12830
1e2f5b6e
AM
12831 /* Get the relocs. */
12832 internal_relocs
4ce794b7 12833 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12834 info->keep_memory);
721956f4 12835 if (internal_relocs == NULL)
1e2f5b6e 12836 goto error_ret_free_local;
721956f4
AM
12837
12838 /* Now examine each relocation. */
12839 irela = internal_relocs;
12840 irelaend = irela + section->reloc_count;
12841 for (; irela < irelaend; irela++)
12842 {
4ce794b7
AM
12843 enum elf_ppc64_reloc_type r_type;
12844 unsigned int r_indx;
721956f4
AM
12845 enum ppc_stub_type stub_type;
12846 struct ppc_stub_hash_entry *stub_entry;
8387904d 12847 asection *sym_sec, *code_sec;
e054468f 12848 bfd_vma sym_value, code_value;
721956f4 12849 bfd_vma destination;
6911b7dc 12850 unsigned long local_off;
8843416a 12851 bfd_boolean ok_dest;
721956f4 12852 struct ppc_link_hash_entry *hash;
8387904d 12853 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12854 struct elf_link_hash_entry *h;
12855 Elf_Internal_Sym *sym;
721956f4
AM
12856 char *stub_name;
12857 const asection *id_sec;
74f0fb50 12858 struct _opd_sec_data *opd;
e054468f 12859 struct plt_entry *plt_ent;
721956f4
AM
12860
12861 r_type = ELF64_R_TYPE (irela->r_info);
12862 r_indx = ELF64_R_SYM (irela->r_info);
12863
4ce794b7 12864 if (r_type >= R_PPC64_max)
721956f4
AM
12865 {
12866 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12867 goto error_ret_free_internal;
721956f4
AM
12868 }
12869
12870 /* Only look for stubs on branch instructions. */
4ce794b7 12871 if (r_type != R_PPC64_REL24
05d0e962 12872 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
12873 && r_type != R_PPC64_REL14
12874 && r_type != R_PPC64_REL14_BRTAKEN
12875 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12876 continue;
12877
12878 /* Now determine the call target, its name, value,
12879 section. */
411e1bfb
AM
12880 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12881 r_indx, input_bfd))
12882 goto error_ret_free_internal;
12883 hash = (struct ppc_link_hash_entry *) h;
12884
8843416a 12885 ok_dest = FALSE;
8387904d 12886 fdh = NULL;
7fe2b9a6 12887 sym_value = 0;
411e1bfb 12888 if (hash == NULL)
721956f4 12889 {
411e1bfb 12890 sym_value = sym->st_value;
c27b8c2a
AM
12891 if (sym_sec != NULL
12892 && sym_sec->output_section != NULL)
12893 ok_dest = TRUE;
721956f4 12894 }
7fe2b9a6
AM
12895 else if (hash->elf.root.type == bfd_link_hash_defined
12896 || hash->elf.root.type == bfd_link_hash_defweak)
12897 {
12898 sym_value = hash->elf.root.u.def.value;
12899 if (sym_sec->output_section != NULL)
12900 ok_dest = TRUE;
12901 }
12902 else if (hash->elf.root.type == bfd_link_hash_undefweak
12903 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12904 {
99877b66 12905 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12906 use the func descriptor sym instead if it is
12907 defined. */
ceb1f1ef 12908 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12909 && hash->oh != NULL)
8387904d 12910 {
8c5b4e52 12911 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12912 if (fdh->elf.root.type == bfd_link_hash_defined
12913 || fdh->elf.root.type == bfd_link_hash_defweak)
12914 {
12915 sym_sec = fdh->elf.root.u.def.section;
12916 sym_value = fdh->elf.root.u.def.value;
12917 if (sym_sec->output_section != NULL)
12918 ok_dest = TRUE;
12919 }
99877b66
AM
12920 else
12921 fdh = NULL;
8387904d 12922 }
7fe2b9a6
AM
12923 }
12924 else
12925 {
12926 bfd_set_error (bfd_error_bad_value);
12927 goto error_ret_free_internal;
721956f4
AM
12928 }
12929
8843416a 12930 destination = 0;
6911b7dc 12931 local_off = 0;
8843416a
AM
12932 if (ok_dest)
12933 {
12934 sym_value += irela->r_addend;
12935 destination = (sym_value
12936 + sym_sec->output_offset
12937 + sym_sec->output_section->vma);
6911b7dc
AM
12938 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12939 ? hash->elf.other
12940 : sym->st_other);
8843416a
AM
12941 }
12942
8387904d 12943 code_sec = sym_sec;
e054468f 12944 code_value = sym_value;
74f0fb50
AM
12945 opd = get_opd_info (sym_sec);
12946 if (opd != NULL)
8387904d
AM
12947 {
12948 bfd_vma dest;
12949
74f0fb50 12950 if (hash == NULL && opd->adjust != NULL)
8387904d 12951 {
51aecdc5 12952 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12953 if (adjust == -1)
12954 continue;
e054468f 12955 code_value += adjust;
8387904d
AM
12956 sym_value += adjust;
12957 }
12958 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12959 &code_sec, &code_value, FALSE);
8387904d
AM
12960 if (dest != (bfd_vma) -1)
12961 {
12962 destination = dest;
12963 if (fdh != NULL)
12964 {
12965 /* Fixup old ABI sym to point at code
12966 entry. */
99877b66 12967 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12968 hash->elf.root.u.def.section = code_sec;
e054468f 12969 hash->elf.root.u.def.value = code_value;
8387904d
AM
12970 }
12971 }
12972 }
12973
721956f4 12974 /* Determine what (if any) linker stub is needed. */
e054468f 12975 plt_ent = NULL;
721956f4 12976 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12977 &plt_ent, destination,
12978 local_off);
ad8e1ba5 12979
05d0e962
AM
12980 if (r_type == R_PPC64_REL24_NOTOC)
12981 {
12982 if (stub_type == ppc_stub_plt_call)
12983 stub_type = ppc_stub_plt_call_notoc;
12984 else if (stub_type == ppc_stub_long_branch
12985 || (code_sec != NULL
12986 && code_sec->output_section != NULL
12987 && (((hash ? hash->elf.other : sym->st_other)
12988 & STO_PPC64_LOCAL_MASK)
4a4e7361 12989 > 1 << STO_PPC64_LOCAL_BIT)))
05d0e962
AM
12990 stub_type = ppc_stub_long_branch_notoc;
12991 }
12992 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
12993 {
12994 /* Check whether we need a TOC adjusting stub.
12995 Since the linker pastes together pieces from
12996 different object files when creating the
12997 _init and _fini functions, it may be that a
12998 call to what looks like a local sym is in
12999 fact a call needing a TOC adjustment. */
33cb30a1
AM
13000 if ((code_sec != NULL
13001 && code_sec->output_section != NULL
13002 && (htab->sec_info[code_sec->id].toc_off
13003 != htab->sec_info[section->id].toc_off)
13004 && (code_sec->has_toc_reloc
13005 || code_sec->makes_toc_func_call))
13006 || (((hash ? hash->elf.other : sym->st_other)
13007 & STO_PPC64_LOCAL_MASK)
13008 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
13009 stub_type = ppc_stub_long_branch_r2off;
13010 }
13011
721956f4
AM
13012 if (stub_type == ppc_stub_none)
13013 continue;
13014
411e1bfb
AM
13015 /* __tls_get_addr calls might be eliminated. */
13016 if (stub_type != ppc_stub_plt_call
05d0e962 13017 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 13018 && hash != NULL
8387904d
AM
13019 && (hash == htab->tls_get_addr
13020 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
13021 && section->has_tls_reloc
13022 && irela != internal_relocs)
13023 {
13024 /* Get tls info. */
f961d9dd 13025 unsigned char *tls_mask;
411e1bfb 13026
3a71aa26 13027 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
13028 irela - 1, input_bfd))
13029 goto error_ret_free_internal;
37da22e5 13030 if ((*tls_mask & TLS_TLS) != 0)
411e1bfb
AM
13031 continue;
13032 }
13033
f378ab09 13034 if (stub_type == ppc_stub_plt_call)
794e51c0 13035 {
6e1816be
AM
13036 if (!htab->opd_abi
13037 && htab->params->plt_localentry0 != 0
13038 && is_elfv2_localentry0 (&hash->elf))
13039 htab->has_plt_localentry0 = 1;
13040 else if (irela + 1 < irelaend
13041 && irela[1].r_offset == irela->r_offset + 4
13042 && (ELF64_R_TYPE (irela[1].r_info)
13043 == R_PPC64_TOCSAVE))
f378ab09
AM
13044 {
13045 if (!tocsave_find (htab, INSERT,
13046 &local_syms, irela + 1, input_bfd))
13047 goto error_ret_free_internal;
13048 }
f378ab09
AM
13049 else
13050 stub_type = ppc_stub_plt_call_r2save;
794e51c0 13051 }
3b421ab3 13052
721956f4 13053 /* Support for grouping stub sections. */
6f20ed8a 13054 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
13055
13056 /* Get the name of this stub. */
13057 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13058 if (!stub_name)
13059 goto error_ret_free_internal;
13060
13061 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 13062 stub_name, FALSE, FALSE);
721956f4
AM
13063 if (stub_entry != NULL)
13064 {
05d0e962
AM
13065 enum ppc_stub_type old_type;
13066 /* A stub has already been created, but it may
13067 not be the required type. We shouldn't be
13068 transitioning from plt_call to long_branch
13069 stubs or vice versa, but we might be
13070 upgrading from plt_call to plt_call_r2save or
13071 from long_branch to long_branch_r2off. */
721956f4 13072 free (stub_name);
05d0e962
AM
13073 old_type = stub_entry->stub_type;
13074 switch (old_type)
13075 {
13076 default:
13077 abort ();
13078
13079 case ppc_stub_save_res:
13080 continue;
13081
13082 case ppc_stub_plt_call:
13083 case ppc_stub_plt_call_r2save:
13084 case ppc_stub_plt_call_notoc:
13085 case ppc_stub_plt_call_both:
13086 if (stub_type == ppc_stub_plt_call)
13087 continue;
13088 else if (stub_type == ppc_stub_plt_call_r2save)
13089 {
13090 if (old_type == ppc_stub_plt_call_notoc)
13091 stub_type = ppc_stub_plt_call_both;
13092 }
13093 else if (stub_type == ppc_stub_plt_call_notoc)
13094 {
13095 if (old_type == ppc_stub_plt_call_r2save)
13096 stub_type = ppc_stub_plt_call_both;
13097 }
13098 else
13099 abort ();
13100 break;
13101
13102 case ppc_stub_plt_branch:
13103 case ppc_stub_plt_branch_r2off:
13104 case ppc_stub_plt_branch_notoc:
13105 case ppc_stub_plt_branch_both:
13106 old_type += (ppc_stub_long_branch
13107 - ppc_stub_plt_branch);
13108 /* Fall through. */
13109 case ppc_stub_long_branch:
13110 case ppc_stub_long_branch_r2off:
13111 case ppc_stub_long_branch_notoc:
13112 case ppc_stub_long_branch_both:
13113 if (stub_type == ppc_stub_long_branch)
13114 continue;
13115 else if (stub_type == ppc_stub_long_branch_r2off)
13116 {
13117 if (old_type == ppc_stub_long_branch_notoc)
13118 stub_type = ppc_stub_long_branch_both;
13119 }
13120 else if (stub_type == ppc_stub_long_branch_notoc)
13121 {
13122 if (old_type == ppc_stub_long_branch_r2off)
13123 stub_type = ppc_stub_long_branch_both;
13124 }
13125 else
13126 abort ();
13127 break;
13128 }
13129 if (old_type < stub_type)
794e51c0 13130 stub_entry->stub_type = stub_type;
721956f4
AM
13131 continue;
13132 }
13133
25f53a85 13134 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
13135 if (stub_entry == NULL)
13136 {
13137 free (stub_name);
6cdc0ccc
AM
13138 error_ret_free_internal:
13139 if (elf_section_data (section)->relocs == NULL)
13140 free (internal_relocs);
13141 error_ret_free_local:
13142 if (local_syms != NULL
13143 && (symtab_hdr->contents
13144 != (unsigned char *) local_syms))
13145 free (local_syms);
b34976b6 13146 return FALSE;
721956f4
AM
13147 }
13148
ad8e1ba5 13149 stub_entry->stub_type = stub_type;
05d0e962
AM
13150 if (stub_type >= ppc_stub_plt_call
13151 && stub_type <= ppc_stub_plt_call_both)
e054468f 13152 {
05d0e962
AM
13153 stub_entry->target_value = sym_value;
13154 stub_entry->target_section = sym_sec;
e054468f
AM
13155 }
13156 else
13157 {
05d0e962
AM
13158 stub_entry->target_value = code_value;
13159 stub_entry->target_section = code_sec;
e054468f 13160 }
721956f4 13161 stub_entry->h = hash;
e054468f 13162 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
13163 stub_entry->symtype
13164 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 13165 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 13166
3d58e1fc
AM
13167 if (hash != NULL
13168 && (hash->elf.root.type == bfd_link_hash_defined
13169 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 13170 htab->stub_globals += 1;
721956f4
AM
13171 }
13172
13173 /* We're done with the internal relocs, free them. */
6cdc0ccc 13174 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 13175 free (internal_relocs);
721956f4 13176 }
6cdc0ccc
AM
13177
13178 if (local_syms != NULL
13179 && symtab_hdr->contents != (unsigned char *) local_syms)
13180 {
13181 if (!info->keep_memory)
13182 free (local_syms);
13183 else
13184 symtab_hdr->contents = (unsigned char *) local_syms;
13185 }
721956f4
AM
13186 }
13187
5c3dead3 13188 /* We may have added some stubs. Find out the new size of the
721956f4 13189 stub sections. */
d4aaa2a0 13190 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13191 {
13192 group->lr_restore = 0;
13193 group->eh_size = 0;
13194 if (group->stub_sec != NULL)
13195 {
13196 asection *stub_sec = group->stub_sec;
13197
13198 if (htab->stub_iteration <= STUB_SHRINK_ITER
13199 || stub_sec->rawsize < stub_sec->size)
13200 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13201 stub_sec->rawsize = stub_sec->size;
13202 stub_sec->size = 0;
13203 stub_sec->reloc_count = 0;
13204 stub_sec->flags &= ~SEC_RELOC;
13205 }
13206 }
eea6121a 13207
ba21f564
AM
13208 if (htab->stub_iteration <= STUB_SHRINK_ITER
13209 || htab->brlt->rawsize < htab->brlt->size)
13210 htab->brlt->rawsize = htab->brlt->size;
eea6121a 13211 htab->brlt->size = 0;
84f5d08e
AM
13212 htab->brlt->reloc_count = 0;
13213 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 13214 if (htab->relbrlt != NULL)
eea6121a 13215 htab->relbrlt->size = 0;
721956f4 13216
63bc6f6c 13217 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 13218
a4b6fadd
AM
13219 for (group = htab->group; group != NULL; group = group->next)
13220 if (group->needs_save_res)
13221 group->stub_sec->size += htab->sfpr->size;
13222
176a0d42
AM
13223 if (info->emitrelocations
13224 && htab->glink != NULL && htab->glink->size != 0)
13225 {
13226 htab->glink->reloc_count = 1;
13227 htab->glink->flags |= SEC_RELOC;
13228 }
13229
58d180e8
AM
13230 if (htab->glink_eh_frame != NULL
13231 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 13232 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 13233 {
2e0ce1c8 13234 size_t size = 0, align = 4;
58d180e8 13235
d4aaa2a0 13236 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13237 if (group->eh_size != 0)
13238 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 13239 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 13240 size += (24 + align - 1) & -align;
58d180e8 13241 if (size != 0)
2e0ce1c8
AM
13242 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13243 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13244 size = (size + align - 1) & -align;
58d180e8
AM
13245 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13246 htab->glink_eh_frame->size = size;
13247 }
13248
e7d1c40c 13249 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13250 for (group = htab->group; group != NULL; group = group->next)
13251 if (group->stub_sec != NULL)
691d2e9a
AM
13252 {
13253 int align = abs (htab->params->plt_stub_align);
13254 group->stub_sec->size
13255 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13256 }
d4aaa2a0
AM
13257
13258 for (group = htab->group; group != NULL; group = group->next)
13259 if (group->stub_sec != NULL
13260 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 13261 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 13262 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
13263 break;
13264
d4aaa2a0 13265 if (group == NULL
ba21f564
AM
13266 && (htab->brlt->rawsize == htab->brlt->size
13267 || (htab->stub_iteration > STUB_SHRINK_ITER
13268 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
13269 && (htab->glink_eh_frame == NULL
13270 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
13271 break;
13272
721956f4 13273 /* Ask the linker to do its stuff. */
e7d1c40c 13274 (*htab->params->layout_sections_again) ();
721956f4
AM
13275 }
13276
da44f4e5
AM
13277 if (htab->glink_eh_frame != NULL
13278 && htab->glink_eh_frame->size != 0)
13279 {
13280 bfd_vma val;
13281 bfd_byte *p, *last_fde;
13282 size_t last_fde_len, size, align, pad;
d4aaa2a0 13283 struct map_stub *group;
da44f4e5 13284
df136d64
AM
13285 /* It is necessary to at least have a rough outline of the
13286 linker generated CIEs and FDEs written before
13287 bfd_elf_discard_info is run, in order for these FDEs to be
13288 indexed in .eh_frame_hdr. */
da44f4e5
AM
13289 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13290 if (p == NULL)
13291 return FALSE;
13292 htab->glink_eh_frame->contents = p;
13293 last_fde = p;
2e0ce1c8 13294 align = 4;
da44f4e5
AM
13295
13296 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13297 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 13298 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 13299 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 13300 p += last_fde_len + 4;
da44f4e5 13301
d4aaa2a0 13302 for (group = htab->group; group != NULL; group = group->next)
df136d64 13303 if (group->eh_size != 0)
da44f4e5 13304 {
df136d64 13305 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 13306 last_fde = p;
df136d64 13307 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 13308 /* FDE length. */
2e0ce1c8 13309 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13310 p += 4;
13311 /* CIE pointer. */
13312 val = p - htab->glink_eh_frame->contents;
13313 bfd_put_32 (htab->elf.dynobj, val, p);
13314 p += 4;
13315 /* Offset to stub section, written later. */
13316 p += 4;
13317 /* stub section size. */
d4aaa2a0 13318 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
13319 p += 4;
13320 /* Augmentation. */
13321 p += 1;
df136d64
AM
13322 /* Make sure we don't have all nops. This is enough for
13323 elf-eh-frame.c to detect the last non-nop opcode. */
13324 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 13325 p = last_fde + last_fde_len + 4;
da44f4e5
AM
13326 }
13327 if (htab->glink != NULL && htab->glink->size != 0)
13328 {
13329 last_fde = p;
2e0ce1c8 13330 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13331 /* FDE length. */
2e0ce1c8 13332 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13333 p += 4;
13334 /* CIE pointer. */
13335 val = p - htab->glink_eh_frame->contents;
13336 bfd_put_32 (htab->elf.dynobj, val, p);
13337 p += 4;
13338 /* Offset to .glink, written later. */
13339 p += 4;
13340 /* .glink size. */
13341 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13342 p += 4;
13343 /* Augmentation. */
13344 p += 1;
13345
13346 *p++ = DW_CFA_advance_loc + 1;
13347 *p++ = DW_CFA_register;
13348 *p++ = 65;
9f08fa5c 13349 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 13350 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
13351 *p++ = DW_CFA_restore_extended;
13352 *p++ = 65;
2e0ce1c8 13353 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13354 }
13355 /* Subsume any padding into the last FDE if user .eh_frame
13356 sections are aligned more than glink_eh_frame. Otherwise any
13357 zero padding will be seen as a terminator. */
2e0ce1c8 13358 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13359 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13360 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13361 htab->glink_eh_frame->size = size + pad;
13362 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13363 }
13364
d969d15f
AM
13365 maybe_strip_output (info, htab->brlt);
13366 if (htab->glink_eh_frame != NULL)
13367 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13368
b34976b6 13369 return TRUE;
721956f4
AM
13370}
13371
13372/* Called after we have determined section placement. If sections
805fc799 13373 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13374
805fc799 13375bfd_vma
1c865ab2 13376ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13377{
805fc799 13378 asection *s;
a27e685f 13379 bfd_vma TOCstart, adjust;
721956f4 13380
43417696
AM
13381 if (info != NULL)
13382 {
13383 struct elf_link_hash_entry *h;
13384 struct elf_link_hash_table *htab = elf_hash_table (info);
13385
13386 if (is_elf_hash_table (htab)
13387 && htab->hgot != NULL)
13388 h = htab->hgot;
13389 else
13390 {
13391 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13392 if (is_elf_hash_table (htab))
13393 htab->hgot = h;
13394 }
13395 if (h != NULL
13396 && h->root.type == bfd_link_hash_defined
13397 && !h->root.linker_def
13398 && (!is_elf_hash_table (htab)
13399 || h->def_regular))
13400 {
13401 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13402 + h->root.u.def.section->output_offset
13403 + h->root.u.def.section->output_section->vma);
13404 _bfd_set_gp_value (obfd, TOCstart);
13405 return TOCstart;
13406 }
13407 }
13408
805fc799
AM
13409 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13410 order. The TOC starts where the first of these sections starts. */
13411 s = bfd_get_section_by_name (obfd, ".got");
e054468f 13412 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13413 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 13414 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13415 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 13416 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13417 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 13418 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
13419 {
13420 /* This may happen for
13421 o references to TOC base (SYM@toc / TOC[tc0]) without a
13422 .toc directive
13423 o bad linker script
13424 o --gc-sections and empty TOC sections
13425
13426 FIXME: Warn user? */
13427
13428 /* Look for a likely section. We probably won't even be
13429 using TOCstart. */
13430 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13431 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13432 | SEC_EXCLUDE))
805fc799
AM
13433 == (SEC_ALLOC | SEC_SMALL_DATA))
13434 break;
721956f4 13435 if (s == NULL)
805fc799 13436 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13437 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
13438 == (SEC_ALLOC | SEC_SMALL_DATA))
13439 break;
721956f4 13440 if (s == NULL)
805fc799 13441 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13442 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13443 == SEC_ALLOC)
805fc799 13444 break;
721956f4 13445 if (s == NULL)
805fc799 13446 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13447 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
13448 break;
13449 }
721956f4 13450
805fc799
AM
13451 TOCstart = 0;
13452 if (s != NULL)
13453 TOCstart = s->output_section->vma + s->output_offset;
721956f4 13454
a27e685f
AM
13455 /* Force alignment. */
13456 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13457 TOCstart -= adjust;
1c865ab2
AM
13458 _bfd_set_gp_value (obfd, TOCstart);
13459
810d4e75 13460 if (info != NULL && s != NULL)
1c865ab2
AM
13461 {
13462 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13463
810d4e75
AM
13464 if (htab != NULL)
13465 {
13466 if (htab->elf.hgot != NULL)
13467 {
a27e685f 13468 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
13469 htab->elf.hgot->root.u.def.section = s;
13470 }
13471 }
13472 else
1c865ab2 13473 {
810d4e75
AM
13474 struct bfd_link_hash_entry *bh = NULL;
13475 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
13476 s, TOC_BASE_OFF - adjust,
13477 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
13478 }
13479 }
805fc799 13480 return TOCstart;
721956f4
AM
13481}
13482
a345bc8d 13483/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 13484 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
13485
13486static bfd_boolean
49c09209 13487build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
13488{
13489 struct bfd_link_info *info;
13490 struct ppc_link_hash_table *htab;
49c09209 13491 struct plt_entry *ent;
a345bc8d
AM
13492 asection *s;
13493
13494 if (h->root.type == bfd_link_hash_indirect)
13495 return TRUE;
13496
49c09209
AM
13497 info = inf;
13498 htab = ppc_hash_table (info);
13499 if (htab == NULL)
13500 return FALSE;
13501
13502 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13503 if (ent->plt.offset != (bfd_vma) -1)
13504 {
13505 /* This symbol has an entry in the procedure linkage
13506 table. Set it up. */
13507 Elf_Internal_Rela rela;
2d7ad24e 13508 asection *plt, *relplt;
49c09209
AM
13509 bfd_byte *loc;
13510
13511 if (!htab->elf.dynamic_sections_created
13512 || h->dynindx == -1)
13513 {
13514 if (!(h->def_regular
13515 && (h->root.type == bfd_link_hash_defined
13516 || h->root.type == bfd_link_hash_defweak)))
13517 continue;
2d7ad24e
AM
13518 if (h->type == STT_GNU_IFUNC)
13519 {
13520 plt = htab->elf.iplt;
13521 relplt = htab->elf.irelplt;
13522 htab->local_ifunc_resolver = 1;
13523 if (htab->opd_abi)
13524 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13525 else
13526 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13527 }
49c09209 13528 else
2d7ad24e
AM
13529 {
13530 plt = htab->pltlocal;
13531 if (bfd_link_pic (info))
13532 {
13533 relplt = htab->relpltlocal;
13534 if (htab->opd_abi)
13535 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13536 else
13537 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13538 }
13539 else
13540 relplt = NULL;
13541 }
49c09209
AM
13542 rela.r_addend = (h->root.u.def.value
13543 + h->root.u.def.section->output_offset
13544 + h->root.u.def.section->output_section->vma
13545 + ent->addend);
2d7ad24e
AM
13546
13547 if (relplt == NULL)
13548 {
13549 loc = plt->contents + ent->plt.offset;
13550 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13551 if (htab->opd_abi)
13552 {
13553 bfd_vma toc = elf_gp (info->output_bfd);
13554 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13555 bfd_put_64 (info->output_bfd, toc, loc + 8);
13556 }
13557 }
13558 else
13559 {
13560 rela.r_offset = (plt->output_section->vma
13561 + plt->output_offset
13562 + ent->plt.offset);
13563 loc = relplt->contents + (relplt->reloc_count++
13564 * sizeof (Elf64_External_Rela));
13565 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13566 }
49c09209
AM
13567 }
13568 else
13569 {
13570 rela.r_offset = (htab->elf.splt->output_section->vma
13571 + htab->elf.splt->output_offset
13572 + ent->plt.offset);
13573 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13574 rela.r_addend = ent->addend;
13575 loc = (htab->elf.srelplt->contents
13576 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13577 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13578 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13579 htab->maybe_local_ifunc_resolver = 1;
2d7ad24e 13580 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 13581 }
49c09209
AM
13582 }
13583
a345bc8d
AM
13584 if (!h->pointer_equality_needed)
13585 return TRUE;
13586
13587 if (h->def_regular)
13588 return TRUE;
13589
9e390558 13590 s = htab->global_entry;
49c09209
AM
13591 if (s == NULL || s->size == 0)
13592 return TRUE;
13593
13594 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13595 if (ent->plt.offset != (bfd_vma) -1
13596 && ent->addend == 0)
a345bc8d
AM
13597 {
13598 bfd_byte *p;
13599 asection *plt;
13600 bfd_vma off;
13601
a345bc8d 13602 p = s->contents + h->root.u.def.value;
33e44f2e 13603 plt = htab->elf.splt;
a345bc8d
AM
13604 if (!htab->elf.dynamic_sections_created
13605 || h->dynindx == -1)
2d7ad24e
AM
13606 {
13607 if (h->type == STT_GNU_IFUNC)
13608 plt = htab->elf.iplt;
13609 else
13610 plt = htab->pltlocal;
13611 }
49c09209 13612 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
13613 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13614
13615 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13616 {
13617 info->callbacks->einfo
c1c8c1ef 13618 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
13619 h->root.root.string);
13620 bfd_set_error (bfd_error_bad_value);
13621 htab->stub_error = TRUE;
13622 }
13623
7341d5e2
AM
13624 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13625 if (htab->params->emit_stub_syms)
13626 {
13627 size_t len = strlen (h->root.root.string);
13628 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13629
13630 if (name == NULL)
13631 return FALSE;
13632
13633 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13634 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13635 if (h == NULL)
13636 return FALSE;
13637 if (h->root.type == bfd_link_hash_new)
13638 {
13639 h->root.type = bfd_link_hash_defined;
13640 h->root.u.def.section = s;
13641 h->root.u.def.value = p - s->contents;
13642 h->ref_regular = 1;
13643 h->def_regular = 1;
13644 h->ref_regular_nonweak = 1;
13645 h->forced_local = 1;
13646 h->non_elf = 0;
2ec55de3 13647 h->root.linker_def = 1;
7341d5e2
AM
13648 }
13649 }
13650
a345bc8d
AM
13651 if (PPC_HA (off) != 0)
13652 {
13653 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13654 p += 4;
13655 }
13656 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13657 p += 4;
13658 bfd_put_32 (s->owner, MTCTR_R12, p);
13659 p += 4;
407aa07c 13660 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
13661 break;
13662 }
13663 return TRUE;
13664}
13665
49c09209
AM
13666/* Write PLT relocs for locals. */
13667
13668static bfd_boolean
13669write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13670{
13671 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13672 bfd *ibfd;
13673
13674 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13675 {
13676 struct got_entry **lgot_ents, **end_lgot_ents;
13677 struct plt_entry **local_plt, **lplt, **end_local_plt;
13678 Elf_Internal_Shdr *symtab_hdr;
13679 bfd_size_type locsymcount;
13680 Elf_Internal_Sym *local_syms = NULL;
13681 struct plt_entry *ent;
13682
13683 if (!is_ppc64_elf (ibfd))
13684 continue;
13685
13686 lgot_ents = elf_local_got_ents (ibfd);
13687 if (!lgot_ents)
13688 continue;
13689
13690 symtab_hdr = &elf_symtab_hdr (ibfd);
13691 locsymcount = symtab_hdr->sh_info;
13692 end_lgot_ents = lgot_ents + locsymcount;
13693 local_plt = (struct plt_entry **) end_lgot_ents;
13694 end_local_plt = local_plt + locsymcount;
13695 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13696 for (ent = *lplt; ent != NULL; ent = ent->next)
13697 if (ent->plt.offset != (bfd_vma) -1)
13698 {
13699 Elf_Internal_Sym *sym;
13700 asection *sym_sec;
13701 asection *plt, *relplt;
13702 bfd_byte *loc;
13703 bfd_vma val;
49c09209
AM
13704
13705 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13706 lplt - local_plt, ibfd))
13707 {
13708 if (local_syms != NULL
13709 && symtab_hdr->contents != (unsigned char *) local_syms)
13710 free (local_syms);
13711 return FALSE;
13712 }
13713
13714 val = sym->st_value + ent->addend;
ba85c15d
AM
13715 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13716 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
49c09209
AM
13717 if (sym_sec != NULL && sym_sec->output_section != NULL)
13718 val += sym_sec->output_offset + sym_sec->output_section->vma;
13719
2d7ad24e
AM
13720 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13721 {
13722 htab->local_ifunc_resolver = 1;
13723 plt = htab->elf.iplt;
13724 relplt = htab->elf.irelplt;
13725 }
13726 else
13727 {
13728 plt = htab->pltlocal;
13729 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13730 }
49c09209 13731
2d7ad24e
AM
13732 if (relplt == NULL)
13733 {
13734 loc = plt->contents + ent->plt.offset;
13735 bfd_put_64 (info->output_bfd, val, loc);
13736 if (htab->opd_abi)
13737 {
13738 bfd_vma toc = elf_gp (ibfd);
13739 bfd_put_64 (info->output_bfd, toc, loc + 8);
13740 }
13741 }
49c09209 13742 else
2d7ad24e
AM
13743 {
13744 Elf_Internal_Rela rela;
13745 rela.r_offset = (ent->plt.offset
13746 + plt->output_offset
13747 + plt->output_section->vma);
13748 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13749 {
13750 if (htab->opd_abi)
13751 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13752 else
13753 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13754 }
13755 else
13756 {
13757 if (htab->opd_abi)
13758 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13759 else
13760 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13761 }
13762 rela.r_addend = val;
13763 loc = relplt->contents + (relplt->reloc_count++
13764 * sizeof (Elf64_External_Rela));
13765 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13766 }
49c09209
AM
13767 }
13768
13769 if (local_syms != NULL
13770 && symtab_hdr->contents != (unsigned char *) local_syms)
13771 {
13772 if (!info->keep_memory)
13773 free (local_syms);
13774 else
13775 symtab_hdr->contents = (unsigned char *) local_syms;
13776 }
13777 }
13778 return TRUE;
13779}
13780
721956f4
AM
13781/* Build all the stubs associated with the current output file.
13782 The stubs are kept in a hash table attached to the main linker
13783 hash table. This function is called via gldelf64ppc_finish. */
13784
b34976b6 13785bfd_boolean
e7d1c40c 13786ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13787 char **stats)
5d1634d7
AM
13788{
13789 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13790 struct map_stub *group;
721956f4 13791 asection *stub_sec;
5d1634d7 13792 bfd_byte *p;
e717da7e 13793 int stub_sec_count = 0;
5d1634d7 13794
4dfe6ac6
NC
13795 if (htab == NULL)
13796 return FALSE;
13797
eea6121a 13798 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 13799 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13800 {
13801 group->eh_size = 0;
13802 group->lr_restore = 0;
13803 if ((stub_sec = group->stub_sec) != NULL
13804 && stub_sec->size != 0)
13805 {
13806 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13807 stub_sec->size);
13808 if (stub_sec->contents == NULL)
13809 return FALSE;
13810 stub_sec->size = 0;
13811 }
13812 }
5d1634d7 13813
23eb7e01 13814 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13815 {
9f951329 13816 unsigned int indx;
ad8e1ba5 13817 bfd_vma plt0;
9f951329 13818
721956f4 13819 /* Build the .glink plt call stub. */
e7d1c40c 13820 if (htab->params->emit_stub_syms)
97b639ba
AM
13821 {
13822 struct elf_link_hash_entry *h;
468392fb
AM
13823 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13824 TRUE, FALSE, FALSE);
97b639ba
AM
13825 if (h == NULL)
13826 return FALSE;
13827 if (h->root.type == bfd_link_hash_new)
13828 {
13829 h->root.type = bfd_link_hash_defined;
13830 h->root.u.def.section = htab->glink;
ee4bf8d2 13831 h->root.u.def.value = 8;
f5385ebf
AM
13832 h->ref_regular = 1;
13833 h->def_regular = 1;
13834 h->ref_regular_nonweak = 1;
13835 h->forced_local = 1;
13836 h->non_elf = 0;
2ec55de3 13837 h->root.linker_def = 1;
97b639ba
AM
13838 }
13839 }
33e44f2e
AM
13840 plt0 = (htab->elf.splt->output_section->vma
13841 + htab->elf.splt->output_offset
13842 - 16);
176a0d42
AM
13843 if (info->emitrelocations)
13844 {
13845 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13846 if (r == NULL)
13847 return FALSE;
13848 r->r_offset = (htab->glink->output_offset
13849 + htab->glink->output_section->vma);
13850 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13851 r->r_addend = plt0;
13852 }
4ce794b7 13853 p = htab->glink->contents;
176a0d42 13854 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13855 bfd_put_64 (htab->glink->owner, plt0, p);
13856 p += 8;
b9e5796b
AM
13857 if (htab->opd_abi)
13858 {
13859 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13860 p += 4;
13861 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13862 p += 4;
13863 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13864 p += 4;
13865 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13866 p += 4;
13867 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13868 p += 4;
13869 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13870 p += 4;
13871 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13872 p += 4;
13873 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13874 p += 4;
13875 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13876 p += 4;
13877 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13878 p += 4;
13879 }
13880 else
13881 {
13882 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13883 p += 4;
13884 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13885 p += 4;
13886 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13887 p += 4;
f378ab09
AM
13888 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13889 p += 4;
b9e5796b
AM
13890 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13891 p += 4;
13892 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13893 p += 4;
13894 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13895 p += 4;
13896 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13897 p += 4;
13898 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13899 p += 4;
13900 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13901 p += 4;
13902 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13903 p += 4;
13904 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13905 p += 4;
13906 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13907 p += 4;
13908 }
407aa07c
AM
13909 bfd_put_32 (htab->glink->owner, BCTR, p);
13910 p += 4;
c75bc4f7 13911 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 13912
9f951329
AM
13913 /* Build the .glink lazy link call stubs. */
13914 indx = 0;
9e390558 13915 while (p < htab->glink->contents + htab->glink->size)
9f951329 13916 {
b9e5796b 13917 if (htab->opd_abi)
9f951329 13918 {
b9e5796b
AM
13919 if (indx < 0x8000)
13920 {
13921 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13922 p += 4;
13923 }
13924 else
13925 {
13926 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13927 p += 4;
13928 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13929 p);
13930 p += 4;
13931 }
9f951329 13932 }
4ce794b7 13933 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13934 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13935 indx++;
9f951329
AM
13936 p += 4;
13937 }
5d1634d7 13938 }
5d1634d7 13939
49c09209
AM
13940 /* Build .glink global entry stubs, and PLT relocs for globals. */
13941 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13942
13943 if (!write_plt_relocs_for_local_syms (info))
13944 return FALSE;
9e390558 13945
7341d5e2 13946 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13947 {
4ce794b7 13948 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13949 htab->brlt->size);
4ce794b7 13950 if (htab->brlt->contents == NULL)
b34976b6 13951 return FALSE;
721956f4 13952 }
ee75fd95 13953 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13954 {
13955 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13956 htab->relbrlt->size);
63bc6f6c
AM
13957 if (htab->relbrlt->contents == NULL)
13958 return FALSE;
13959 }
5d1634d7 13960
721956f4
AM
13961 /* Build the stubs as directed by the stub hash table. */
13962 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13963
a4b6fadd
AM
13964 for (group = htab->group; group != NULL; group = group->next)
13965 if (group->needs_save_res)
7dda8d3c 13966 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 13967
aa8a7074
AM
13968 if (htab->relbrlt != NULL)
13969 htab->relbrlt->reloc_count = 0;
13970
e7d1c40c 13971 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13972 for (group = htab->group; group != NULL; group = group->next)
13973 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
13974 {
13975 int align = abs (htab->params->plt_stub_align);
13976 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13977 }
794e51c0 13978
7dda8d3c
AM
13979 for (group = htab->group; group != NULL; group = group->next)
13980 if (group->needs_save_res)
13981 {
13982 stub_sec = group->stub_sec;
13983 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13984 htab->sfpr->contents, htab->sfpr->size);
13985 if (htab->params->emit_stub_syms)
13986 {
13987 unsigned int i;
13988
13989 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13990 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13991 return FALSE;
13992 }
13993 }
13994
df136d64
AM
13995 if (htab->glink_eh_frame != NULL
13996 && htab->glink_eh_frame->size != 0)
13997 {
13998 bfd_vma val;
13999 size_t align = 4;
14000
14001 p = htab->glink_eh_frame->contents;
14002 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14003
14004 for (group = htab->group; group != NULL; group = group->next)
14005 if (group->eh_size != 0)
14006 {
14007 /* Offset to stub section. */
14008 val = (group->stub_sec->output_section->vma
14009 + group->stub_sec->output_offset);
14010 val -= (htab->glink_eh_frame->output_section->vma
14011 + htab->glink_eh_frame->output_offset
14012 + (p + 8 - htab->glink_eh_frame->contents));
14013 if (val + 0x80000000 > 0xffffffff)
14014 {
14015 _bfd_error_handler
14016 (_("%s offset too large for .eh_frame sdata4 encoding"),
14017 group->stub_sec->name);
14018 return FALSE;
14019 }
14020 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14021 p += (group->eh_size + 17 + 3) & -4;
14022 }
14023 if (htab->glink != NULL && htab->glink->size != 0)
14024 {
14025 /* Offset to .glink. */
14026 val = (htab->glink->output_section->vma
14027 + htab->glink->output_offset
14028 + 8);
14029 val -= (htab->glink_eh_frame->output_section->vma
14030 + htab->glink_eh_frame->output_offset
14031 + (p + 8 - htab->glink_eh_frame->contents));
14032 if (val + 0x80000000 > 0xffffffff)
14033 {
14034 _bfd_error_handler
14035 (_("%s offset too large for .eh_frame sdata4 encoding"),
14036 htab->glink->name);
14037 return FALSE;
14038 }
14039 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14040 p += (24 + align - 1) & -align;
14041 }
14042 }
14043
d4aaa2a0
AM
14044 for (group = htab->group; group != NULL; group = group->next)
14045 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
14046 {
14047 stub_sec_count += 1;
c9301e31
AM
14048 if (stub_sec->rawsize != stub_sec->size
14049 && (htab->stub_iteration <= STUB_SHRINK_ITER
14050 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
14051 break;
14052 }
5d1634d7 14053
25516cc5 14054 if (group != NULL)
5d1634d7 14055 {
b34976b6 14056 htab->stub_error = TRUE;
cf97bcb0 14057 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 14058 }
721956f4 14059
d2a300cf
AM
14060 if (htab->stub_error)
14061 return FALSE;
14062
14063 if (stats != NULL)
14064 {
db84fff3 14065 size_t len;
d2a300cf
AM
14066 *stats = bfd_malloc (500);
14067 if (*stats == NULL)
14068 return FALSE;
14069
db84fff3
AM
14070 len = sprintf (*stats,
14071 ngettext ("linker stubs in %u group\n",
14072 "linker stubs in %u groups\n",
14073 stub_sec_count),
14074 stub_sec_count);
05d0e962
AM
14075 sprintf (*stats + len, _(" branch %lu\n"
14076 " branch toc adj %lu\n"
14077 " branch notoc %lu\n"
14078 " branch both %lu\n"
14079 " long branch %lu\n"
14080 " long toc adj %lu\n"
14081 " long notoc %lu\n"
14082 " long both %lu\n"
14083 " plt call %lu\n"
14084 " plt call save %lu\n"
14085 " plt call notoc %lu\n"
14086 " plt call both %lu\n"
14087 " global entry %lu"),
4ce794b7
AM
14088 htab->stub_count[ppc_stub_long_branch - 1],
14089 htab->stub_count[ppc_stub_long_branch_r2off - 1],
05d0e962
AM
14090 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14091 htab->stub_count[ppc_stub_long_branch_both - 1],
4ce794b7
AM
14092 htab->stub_count[ppc_stub_plt_branch - 1],
14093 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
05d0e962
AM
14094 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14095 htab->stub_count[ppc_stub_plt_branch_both - 1],
794e51c0 14096 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2 14097 htab->stub_count[ppc_stub_plt_call_r2save - 1],
05d0e962
AM
14098 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14099 htab->stub_count[ppc_stub_plt_call_both - 1],
7341d5e2 14100 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
14101 }
14102 return TRUE;
5bd4f169
AM
14103}
14104
60124e18
AM
14105/* What to do when ld finds relocations against symbols defined in
14106 discarded sections. */
14107
14108static unsigned int
14109ppc64_elf_action_discarded (asection *sec)
14110{
14111 if (strcmp (".opd", sec->name) == 0)
14112 return 0;
14113
14114 if (strcmp (".toc", sec->name) == 0)
14115 return 0;
14116
bce50a28
JJ
14117 if (strcmp (".toc1", sec->name) == 0)
14118 return 0;
14119
60124e18
AM
14120 return _bfd_elf_default_action_discarded (sec);
14121}
14122
5bd4f169
AM
14123/* The RELOCATE_SECTION function is called by the ELF backend linker
14124 to handle the relocations for a section.
14125
14126 The relocs are always passed as Rela structures; if the section
14127 actually uses Rel structures, the r_addend field will always be
14128 zero.
14129
14130 This function is responsible for adjust the section contents as
14131 necessary, and (if using Rela relocs and generating a
1049f94e 14132 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
14133 necessary.
14134
14135 This function does not have to worry about setting the reloc
14136 address or the reloc symbol index.
14137
14138 LOCAL_SYMS is a pointer to the swapped in local symbols.
14139
14140 LOCAL_SECTIONS is an array giving the section in the input file
14141 corresponding to the st_shndx field of each local symbol.
14142
14143 The global hash table entry for the global symbols can be found
14144 via elf_sym_hashes (input_bfd).
14145
1049f94e 14146 When generating relocatable output, this function must handle
5bd4f169
AM
14147 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14148 going to be the section symbol corresponding to the output
14149 section, which means that the addend must be adjusted
14150 accordingly. */
14151
b34976b6 14152static bfd_boolean
4ce794b7
AM
14153ppc64_elf_relocate_section (bfd *output_bfd,
14154 struct bfd_link_info *info,
14155 bfd *input_bfd,
14156 asection *input_section,
14157 bfd_byte *contents,
14158 Elf_Internal_Rela *relocs,
14159 Elf_Internal_Sym *local_syms,
14160 asection **local_sections)
5bd4f169 14161{
65f38f15 14162 struct ppc_link_hash_table *htab;
5bd4f169
AM
14163 Elf_Internal_Shdr *symtab_hdr;
14164 struct elf_link_hash_entry **sym_hashes;
5bd4f169 14165 Elf_Internal_Rela *rel;
c316a17c 14166 Elf_Internal_Rela *wrel;
5bd4f169 14167 Elf_Internal_Rela *relend;
411e1bfb
AM
14168 Elf_Internal_Rela outrel;
14169 bfd_byte *loc;
411e1bfb 14170 struct got_entry **local_got_ents;
5bd4f169 14171 bfd_vma TOCstart;
b34976b6
AM
14172 bfd_boolean ret = TRUE;
14173 bfd_boolean is_opd;
794e51c0
AM
14174 /* Assume 'at' branch hints. */
14175 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 14176 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 14177
65f38f15 14178 /* Initialize howto table if needed. */
5bd4f169 14179 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
14180 ppc_howto_init ();
14181
65f38f15 14182 htab = ppc_hash_table (info);
4dfe6ac6
NC
14183 if (htab == NULL)
14184 return FALSE;
ee75fd95
AM
14185
14186 /* Don't relocate stub sections. */
e7d1c40c 14187 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
14188 return TRUE;
14189
7af5d5c4
AM
14190 if (!is_ppc64_elf (input_bfd))
14191 {
14192 bfd_set_error (bfd_error_wrong_format);
14193 return FALSE;
14194 }
0ffa91dd 14195
411e1bfb 14196 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 14197 TOCstart = elf_gp (output_bfd);
0ffa91dd 14198 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 14199 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 14200 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 14201
c316a17c 14202 rel = wrel = relocs;
5bd4f169 14203 relend = relocs + input_section->reloc_count;
c316a17c 14204 for (; rel < relend; wrel++, rel++)
5bd4f169 14205 {
04c9666a 14206 enum elf_ppc64_reloc_type r_type;
31c76678 14207 bfd_vma addend;
5bd4f169
AM
14208 bfd_reloc_status_type r;
14209 Elf_Internal_Sym *sym;
14210 asection *sec;
039b3fef
AM
14211 struct elf_link_hash_entry *h_elf;
14212 struct ppc_link_hash_entry *h;
14213 struct ppc_link_hash_entry *fdh;
5bd4f169 14214 const char *sym_name;
0d4792f7 14215 unsigned long r_symndx, toc_symndx;
3a71aa26 14216 bfd_vma toc_addend;
f961d9dd
AM
14217 unsigned char tls_mask, tls_gd, tls_type;
14218 unsigned char sym_type;
5bd4f169 14219 bfd_vma relocation;
23cedd1d 14220 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 14221 bfd_boolean warned;
bc30df16 14222 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 14223 unsigned int insn;
e11840f9 14224 unsigned int mask;
721956f4
AM
14225 struct ppc_stub_hash_entry *stub_entry;
14226 bfd_vma max_br_offset;
14227 bfd_vma from;
c316a17c 14228 Elf_Internal_Rela orig_rel;
b80eed39
AM
14229 reloc_howto_type *howto;
14230 struct reloc_howto_struct alt_howto;
4a421c53
AM
14231 uint64_t pinsn;
14232 bfd_vma offset;
5bd4f169 14233
c316a17c
AM
14234 again:
14235 orig_rel = *rel;
14236
4ce794b7 14237 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 14238 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
14239
14240 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14241 symbol of the previous ADDR64 reloc. The symbol gives us the
14242 proper TOC base to use. */
14243 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
14244 && wrel != relocs
14245 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 14246 && is_opd)
c316a17c 14247 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 14248
4ce794b7
AM
14249 sym = NULL;
14250 sec = NULL;
039b3fef 14251 h_elf = NULL;
4ce794b7 14252 sym_name = NULL;
b34976b6
AM
14253 unresolved_reloc = FALSE;
14254 warned = FALSE;
65f38f15 14255
0b13192e 14256 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
14257 {
14258 /* It's a local symbol. */
74f0fb50 14259 struct _opd_sec_data *opd;
4025353c 14260
5bd4f169
AM
14261 sym = local_syms + r_symndx;
14262 sec = local_sections[r_symndx];
26c61ae5 14263 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 14264 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 14265 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
14266 opd = get_opd_info (sec);
14267 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 14268 {
51aecdc5
AM
14269 long adjust = opd->adjust[OPD_NDX (sym->st_value
14270 + rel->r_addend)];
4025353c
AM
14271 if (adjust == -1)
14272 relocation = 0;
14273 else
4cc603a5
AM
14274 {
14275 /* If this is a relocation against the opd section sym
14276 and we have edited .opd, adjust the reloc addend so
14277 that ld -r and ld --emit-relocs output is correct.
14278 If it is a reloc against some other .opd symbol,
14279 then the symbol value will be adjusted later. */
14280 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14281 rel->r_addend += adjust;
14282 else
14283 relocation += adjust;
14284 }
1e2f5b6e 14285 }
5bd4f169
AM
14286 }
14287 else
14288 {
62d887d4
L
14289 bfd_boolean ignored;
14290
b2a8e766
AM
14291 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14292 r_symndx, symtab_hdr, sym_hashes,
039b3fef 14293 h_elf, sec, relocation,
62d887d4 14294 unresolved_reloc, warned, ignored);
039b3fef
AM
14295 sym_name = h_elf->root.root.string;
14296 sym_type = h_elf->type;
b69fdb4e
AM
14297 if (sec != NULL
14298 && sec->owner == output_bfd
14299 && strcmp (sec->name, ".opd") == 0)
14300 {
14301 /* This is a symbol defined in a linker script. All
14302 such are defined in output sections, even those
14303 defined by simple assignment from a symbol defined in
14304 an input section. Transfer the symbol to an
14305 appropriate input .opd section, so that a branch to
14306 this symbol will be mapped to the location specified
14307 by the opd entry. */
14308 struct bfd_link_order *lo;
14309 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14310 if (lo->type == bfd_indirect_link_order)
14311 {
14312 asection *isec = lo->u.indirect.section;
14313 if (h_elf->root.u.def.value >= isec->output_offset
14314 && h_elf->root.u.def.value < (isec->output_offset
14315 + isec->size))
14316 {
14317 h_elf->root.u.def.value -= isec->output_offset;
14318 h_elf->root.u.def.section = isec;
14319 sec = isec;
14320 break;
14321 }
14322 }
14323 }
5bd4f169 14324 }
039b3fef 14325 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 14326
dbaa2011 14327 if (sec != NULL && discarded_section (sec))
c316a17c
AM
14328 {
14329 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14330 input_bfd, input_section,
0930cb30 14331 contents, rel->r_offset);
c316a17c
AM
14332 wrel->r_offset = rel->r_offset;
14333 wrel->r_info = 0;
14334 wrel->r_addend = 0;
14335
14336 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 14337 symbols defined in discarded sections. Not done for
c316a17c
AM
14338 non-debug to preserve relocs in .eh_frame which the
14339 eh_frame editing code expects to be present. */
14340 if (bfd_link_relocatable (info)
14341 && (input_section->flags & SEC_DEBUGGING))
14342 wrel--;
14343
14344 continue;
14345 }
ab96bf03 14346
0e1862bb 14347 if (bfd_link_relocatable (info))
c316a17c 14348 goto copy_reloc;
ab96bf03 14349
f40da81b
AM
14350 if (h != NULL && &h->elf == htab->elf.hgot)
14351 {
6f20ed8a 14352 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
14353 sec = bfd_abs_section_ptr;
14354 unresolved_reloc = FALSE;
14355 }
14356
951fd09b
AM
14357 /* TLS optimizations. Replace instruction sequences and relocs
14358 based on information we collected in tls_optimize. We edit
14359 RELOCS so that --emit-relocs will output something sensible
14360 for the final instruction stream. */
14361 tls_mask = 0;
14362 tls_gd = 0;
0d4792f7 14363 toc_symndx = 0;
727fc41e
AM
14364 if (h != NULL)
14365 tls_mask = h->tls_mask;
14366 else if (local_got_ents != NULL)
411e1bfb 14367 {
e054468f
AM
14368 struct plt_entry **local_plt = (struct plt_entry **)
14369 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 14370 unsigned char *lgot_masks = (unsigned char *)
e054468f 14371 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
14372 tls_mask = lgot_masks[r_symndx];
14373 }
37da22e5 14374 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
14375 && (r_type == R_PPC64_TLS
14376 || r_type == R_PPC64_TLSGD
14377 || r_type == R_PPC64_TLSLD))
14378 {
14379 /* Check for toc tls entries. */
f961d9dd 14380 unsigned char *toc_tls;
0d4792f7 14381
727fc41e
AM
14382 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14383 &local_syms, rel, input_bfd))
14384 return FALSE;
0d4792f7 14385
727fc41e
AM
14386 if (toc_tls)
14387 tls_mask = *toc_tls;
0d4792f7
AM
14388 }
14389
14390 /* Check that tls relocs are used with tls syms, and non-tls
14391 relocs are used with non-tls syms. */
cf35638d 14392 if (r_symndx != STN_UNDEF
0d4792f7
AM
14393 && r_type != R_PPC64_NONE
14394 && (h == NULL
039b3fef
AM
14395 || h->elf.root.type == bfd_link_hash_defined
14396 || h->elf.root.type == bfd_link_hash_defweak)
71c4e95a 14397 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
0d4792f7 14398 {
37da22e5 14399 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
14400 && (r_type == R_PPC64_TLS
14401 || r_type == R_PPC64_TLSGD
14402 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
14403 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14404 ;
14405 else
25f53a85 14406 info->callbacks->einfo
1d483afe 14407 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 14408 /* xgettext:c-format */
c1c8c1ef 14409 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 14410 /* xgettext:c-format */
c1c8c1ef 14411 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 14412 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
14413 ppc64_elf_howto_table[r_type]->name,
14414 sym_name);
411e1bfb
AM
14415 }
14416
14417 /* Ensure reloc mapping code below stays sane. */
14418 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14419 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14420 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14421 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14422 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14423 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14424 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14425 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14426 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14427 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14428 abort ();
0d4792f7 14429
411e1bfb
AM
14430 switch (r_type)
14431 {
14432 default:
411e1bfb
AM
14433 break;
14434
ba761f19 14435 case R_PPC64_LO_DS_OPT:
95f0d0d2 14436 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
14437 if ((insn & (0x3f << 26)) != 58u << 26)
14438 abort ();
14439 insn += (14u << 26) - (58u << 26);
95f0d0d2 14440 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
14441 r_type = R_PPC64_TOC16_LO;
14442 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14443 break;
14444
411e1bfb
AM
14445 case R_PPC64_TOC16:
14446 case R_PPC64_TOC16_LO:
14447 case R_PPC64_TOC16_DS:
14448 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
14449 {
14450 /* Check for toc tls entries. */
f961d9dd 14451 unsigned char *toc_tls;
951fd09b 14452 int retval;
411e1bfb 14453
3a71aa26
AM
14454 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14455 &local_syms, rel, input_bfd);
951fd09b 14456 if (retval == 0)
411e1bfb
AM
14457 return FALSE;
14458
14459 if (toc_tls)
14460 {
951fd09b 14461 tls_mask = *toc_tls;
411e1bfb
AM
14462 if (r_type == R_PPC64_TOC16_DS
14463 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 14464 {
37da22e5 14465 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
14466 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14467 goto toctprel;
14468 }
411e1bfb 14469 else
951fd09b
AM
14470 {
14471 /* If we found a GD reloc pair, then we might be
14472 doing a GD->IE transition. */
14473 if (retval == 2)
14474 {
b00a0a86 14475 tls_gd = TLS_GDIE;
37da22e5
AM
14476 if ((tls_mask & TLS_TLS) != 0
14477 && (tls_mask & TLS_GD) == 0)
102890f0 14478 goto tls_ldgd_opt;
951fd09b
AM
14479 }
14480 else if (retval == 3)
14481 {
37da22e5
AM
14482 if ((tls_mask & TLS_TLS) != 0
14483 && (tls_mask & TLS_LD) == 0)
102890f0 14484 goto tls_ldgd_opt;
951fd09b
AM
14485 }
14486 }
411e1bfb
AM
14487 }
14488 }
14489 break;
14490
9d6ded02
AM
14491 case R_PPC64_GOT_TPREL16_HI:
14492 case R_PPC64_GOT_TPREL16_HA:
37da22e5 14493 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
14494 && (tls_mask & TLS_TPREL) == 0)
14495 {
14496 rel->r_offset -= d_offset;
95f0d0d2 14497 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
14498 r_type = R_PPC64_NONE;
14499 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14500 }
14501 break;
14502
411e1bfb
AM
14503 case R_PPC64_GOT_TPREL16_DS:
14504 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 14505 if ((tls_mask & TLS_TLS) != 0
951fd09b 14506 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14507 {
81407a69 14508 toctprel:
95f0d0d2 14509 insn = bfd_get_32 (input_bfd,
c316a17c 14510 contents + rel->r_offset - d_offset);
411e1bfb
AM
14511 insn &= 31 << 21;
14512 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 14513 bfd_put_32 (input_bfd, insn,
c316a17c 14514 contents + rel->r_offset - d_offset);
411e1bfb 14515 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
14516 if (toc_symndx != 0)
14517 {
14518 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14519 rel->r_addend = toc_addend;
0d4792f7
AM
14520 /* We changed the symbol. Start over in order to
14521 get h, sym, sec etc. right. */
c316a17c 14522 goto again;
0d4792f7
AM
14523 }
14524 else
14525 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14526 }
14527 break;
14528
c213164a
AM
14529 case R_PPC64_GOT_TPREL34:
14530 if ((tls_mask & TLS_TLS) != 0
14531 && (tls_mask & TLS_TPREL) == 0)
14532 {
14533 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
14534 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14535 pinsn <<= 32;
14536 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14537 pinsn += ((2ULL << 56) + (-1ULL << 52)
14538 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
14539 bfd_put_32 (input_bfd, pinsn >> 32,
14540 contents + rel->r_offset);
14541 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14542 contents + rel->r_offset + 4);
14543 r_type = R_PPC64_TPREL34;
14544 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14545 }
14546 break;
14547
411e1bfb 14548 case R_PPC64_TLS:
37da22e5 14549 if ((tls_mask & TLS_TLS) != 0
951fd09b 14550 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14551 {
c213164a 14552 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2d0f3896
AM
14553 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14554 if (insn == 0)
c213164a
AM
14555 break;
14556 if ((rel->r_offset & 3) == 0)
0d4792f7 14557 {
c213164a
AM
14558 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14559 /* Was PPC64_TLS which sits on insn boundary, now
14560 PPC64_TPREL16_LO which is at low-order half-word. */
14561 rel->r_offset += d_offset;
14562 r_type = R_PPC64_TPREL16_LO;
14563 if (toc_symndx != 0)
14564 {
14565 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14566 rel->r_addend = toc_addend;
14567 /* We changed the symbol. Start over in order to
14568 get h, sym, sec etc. right. */
14569 goto again;
14570 }
14571 else
14572 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14573 }
14574 else if ((rel->r_offset & 3) == 1)
14575 {
14576 /* For pcrel IE to LE we already have the full
14577 offset and thus don't need an addi here. A nop
14578 or mr will do. */
14579 if ((insn & (0x3f << 26)) == 14 << 26)
14580 {
14581 /* Extract regs from addi rt,ra,si. */
14582 unsigned int rt = (insn >> 21) & 0x1f;
14583 unsigned int ra = (insn >> 16) & 0x1f;
14584 if (rt == ra)
14585 insn = NOP;
14586 else
14587 {
14588 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
14589 insn = (rt << 16) | (ra << 21) | (ra << 11);
14590 insn |= (31u << 26) | (444u << 1);
14591 }
14592 }
14593 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
0d4792f7 14594 }
411e1bfb
AM
14595 }
14596 break;
14597
411e1bfb
AM
14598 case R_PPC64_GOT_TLSGD16_HI:
14599 case R_PPC64_GOT_TLSGD16_HA:
b00a0a86 14600 tls_gd = TLS_GDIE;
37da22e5 14601 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
14602 goto tls_gdld_hi;
14603 break;
14604
411e1bfb
AM
14605 case R_PPC64_GOT_TLSLD16_HI:
14606 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 14607 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 14608 {
951fd09b
AM
14609 tls_gdld_hi:
14610 if ((tls_mask & tls_gd) != 0)
14611 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14612 + R_PPC64_GOT_TPREL16_DS);
14613 else
411e1bfb 14614 {
4fe5ca5b 14615 rel->r_offset -= d_offset;
95f0d0d2 14616 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 14617 r_type = R_PPC64_NONE;
411e1bfb 14618 }
951fd09b 14619 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14620 }
14621 break;
14622
951fd09b
AM
14623 case R_PPC64_GOT_TLSGD16:
14624 case R_PPC64_GOT_TLSGD16_LO:
b00a0a86 14625 tls_gd = TLS_GDIE;
37da22e5 14626 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 14627 goto tls_ldgd_opt;
951fd09b 14628 break;
411e1bfb 14629
951fd09b
AM
14630 case R_PPC64_GOT_TLSLD16:
14631 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 14632 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 14633 {
b9f04fe0 14634 unsigned int insn1, insn2;
102890f0
AM
14635
14636 tls_ldgd_opt:
727fc41e
AM
14637 offset = (bfd_vma) -1;
14638 /* If not using the newer R_PPC64_TLSGD/LD to mark
14639 __tls_get_addr calls, we must trust that the call
14640 stays with its arg setup insns, ie. that the next
14641 reloc is the __tls_get_addr call associated with
14642 the current reloc. Edit both insns. */
14643 if (input_section->has_tls_get_addr_call
14644 && rel + 1 < relend
14645 && branch_reloc_hash_match (input_bfd, rel + 1,
14646 htab->tls_get_addr,
14647 htab->tls_get_addr_fd))
14648 offset = rel[1].r_offset;
b86ac8e3
AM
14649 /* We read the low GOT_TLS (or TOC16) insn because we
14650 need to keep the destination reg. It may be
14651 something other than the usual r3, and moved to r3
14652 before the call by intervening code. */
95f0d0d2 14653 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 14654 contents + rel->r_offset - d_offset);
102890f0 14655 if ((tls_mask & tls_gd) != 0)
411e1bfb 14656 {
102890f0 14657 /* IE */
b86ac8e3 14658 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
14659 insn1 |= 58 << 26; /* ld */
14660 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 14661 if (offset != (bfd_vma) -1)
f58d5a2d 14662 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
46e9995a
AM
14663 if (r_type == R_PPC64_TOC16
14664 || r_type == R_PPC64_TOC16_LO)
102890f0 14665 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
46e9995a
AM
14666 else
14667 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
14668 + R_PPC64_GOT_TPREL16_DS);
102890f0
AM
14669 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14670 }
14671 else
14672 {
14673 /* LE */
b86ac8e3
AM
14674 insn1 &= 0x1f << 21;
14675 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
14676 insn2 = 0x38630000; /* addi 3,3,0 */
14677 if (tls_gd == 0)
951fd09b 14678 {
102890f0 14679 /* Was an LD reloc. */
71c4e95a 14680 r_symndx = STN_UNDEF;
102890f0 14681 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 14682 }
102890f0 14683 else if (toc_symndx != 0)
3a71aa26
AM
14684 {
14685 r_symndx = toc_symndx;
14686 rel->r_addend = toc_addend;
14687 }
102890f0
AM
14688 r_type = R_PPC64_TPREL16_HA;
14689 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
14690 if (offset != (bfd_vma) -1)
14691 {
14692 rel[1].r_info = ELF64_R_INFO (r_symndx,
14693 R_PPC64_TPREL16_LO);
14694 rel[1].r_offset = offset + d_offset;
14695 rel[1].r_addend = rel->r_addend;
14696 }
102890f0 14697 }
95f0d0d2 14698 bfd_put_32 (input_bfd, insn1,
3a71aa26 14699 contents + rel->r_offset - d_offset);
727fc41e 14700 if (offset != (bfd_vma) -1)
c96e0573
AM
14701 {
14702 bfd_put_32 (input_bfd, insn2, contents + offset);
14703 if (offset + 8 <= input_section->size)
14704 {
14705 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14706 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14707 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14708 }
14709 }
727fc41e
AM
14710 if ((tls_mask & tls_gd) == 0
14711 && (tls_gd == 0 || toc_symndx != 0))
14712 {
14713 /* We changed the symbol. Start over in order
14714 to get h, sym, sec etc. right. */
c316a17c 14715 goto again;
727fc41e
AM
14716 }
14717 }
14718 break;
14719
c213164a
AM
14720 case R_PPC64_GOT_TLSGD34:
14721 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14722 {
14723 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14724 pinsn <<= 32;
14725 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14726 if ((tls_mask & TLS_GDIE) != 0)
14727 {
14728 /* IE, pla -> pld */
14729 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
14730 r_type = R_PPC64_GOT_TPREL34;
14731 }
14732 else
14733 {
14734 /* LE, pla pcrel -> paddi r13 */
14735 pinsn += (-1ULL << 52) + (13ULL << 16);
14736 r_type = R_PPC64_TPREL34;
14737 }
14738 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14739 bfd_put_32 (input_bfd, pinsn >> 32,
14740 contents + rel->r_offset);
14741 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14742 contents + rel->r_offset + 4);
14743 }
14744 break;
14745
14746 case R_PPC64_GOT_TLSLD34:
14747 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14748 {
14749 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14750 pinsn <<= 32;
14751 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14752 pinsn += (-1ULL << 52) + (13ULL << 16);
14753 bfd_put_32 (input_bfd, pinsn >> 32,
14754 contents + rel->r_offset);
14755 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14756 contents + rel->r_offset + 4);
14757 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14758 r_symndx = STN_UNDEF;
14759 r_type = R_PPC64_TPREL34;
14760 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14761 goto again;
14762 }
14763 break;
14764
727fc41e 14765 case R_PPC64_TLSGD:
37da22e5 14766 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 14767 && rel + 1 < relend)
727fc41e 14768 {
b9f04fe0 14769 unsigned int insn2;
5663e321 14770 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 14771
4a421c53 14772 offset = rel->r_offset;
5663e321 14773 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
14774 {
14775 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
14776 if (r_type1 == R_PPC64_PLT_PCREL34
14777 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14778 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
14779 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14780 break;
14781 }
14782
14783 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14784 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14785
b00a0a86 14786 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
14787 {
14788 /* IE */
14789 r_type = R_PPC64_NONE;
14790 insn2 = 0x7c636a14; /* add 3,3,13 */
14791 }
14792 else
14793 {
14794 /* LE */
14795 if (toc_symndx != 0)
14796 {
14797 r_symndx = toc_symndx;
14798 rel->r_addend = toc_addend;
14799 }
c213164a
AM
14800 if (r_type1 == R_PPC64_REL24_NOTOC
14801 || r_type1 == R_PPC64_PLTCALL_NOTOC)
14802 {
14803 r_type = R_PPC64_NONE;
14804 insn2 = NOP;
14805 }
14806 else
14807 {
14808 rel->r_offset = offset + d_offset;
14809 r_type = R_PPC64_TPREL16_LO;
14810 insn2 = 0x38630000; /* addi 3,3,0 */
14811 }
727fc41e
AM
14812 }
14813 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14814 /* Zap the reloc on the _tls_get_addr call too. */
14815 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14816 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 14817 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
14818 if ((tls_mask & TLS_GDIE) == 0
14819 && toc_symndx != 0
14820 && r_type != R_PPC64_NONE)
c316a17c 14821 goto again;
411e1bfb 14822 }
411e1bfb
AM
14823 break;
14824
727fc41e 14825 case R_PPC64_TLSLD:
37da22e5 14826 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 14827 && rel + 1 < relend)
727fc41e 14828 {
b9f04fe0 14829 unsigned int insn2;
5663e321 14830 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 14831
4a421c53 14832 offset = rel->r_offset;
5663e321 14833 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
14834 {
14835 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
14836 if (r_type1 == R_PPC64_PLT_PCREL34
14837 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14838 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
14839 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14840 break;
14841 }
14842
14843 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14844 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14845
c213164a
AM
14846 if (r_type1 == R_PPC64_REL24_NOTOC
14847 || r_type1 == R_PPC64_PLTCALL_NOTOC)
14848 {
14849 r_type = R_PPC64_NONE;
14850 insn2 = NOP;
14851 }
14852 else
14853 {
14854 rel->r_offset = offset + d_offset;
14855 r_symndx = STN_UNDEF;
14856 r_type = R_PPC64_TPREL16_LO;
14857 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14858 insn2 = 0x38630000; /* addi 3,3,0 */
14859 }
727fc41e 14860 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
14861 /* Zap the reloc on the _tls_get_addr call too. */
14862 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14863 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 14864 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
14865 if (r_type != R_PPC64_NONE)
14866 goto again;
727fc41e
AM
14867 }
14868 break;
14869
411e1bfb 14870 case R_PPC64_DTPMOD64:
951fd09b
AM
14871 if (rel + 1 < relend
14872 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14873 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 14874 {
951fd09b
AM
14875 if ((tls_mask & TLS_GD) == 0)
14876 {
14877 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
b00a0a86 14878 if ((tls_mask & TLS_GDIE) != 0)
951fd09b
AM
14879 r_type = R_PPC64_TPREL64;
14880 else
14881 {
4ce794b7 14882 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
14883 r_type = R_PPC64_NONE;
14884 }
14885 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14886 }
14887 }
14888 else
14889 {
14890 if ((tls_mask & TLS_LD) == 0)
411e1bfb 14891 {
4ce794b7 14892 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 14893 r_type = R_PPC64_NONE;
951fd09b 14894 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 14895 }
411e1bfb
AM
14896 }
14897 break;
14898
14899 case R_PPC64_TPREL64:
951fd09b 14900 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
14901 {
14902 r_type = R_PPC64_NONE;
14903 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14904 }
14905 break;
52a82034 14906
006589cf
AM
14907 case R_PPC64_ENTRY:
14908 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14909 if (!bfd_link_pic (info)
14910 && !info->traditional_format
14911 && relocation + 0x80008000 <= 0xffffffff)
14912 {
14913 unsigned int insn1, insn2;
14914
14915 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14916 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14917 if ((insn1 & ~0xfffc) == LD_R2_0R12
14918 && insn2 == ADD_R2_R2_R12)
14919 {
95f0d0d2 14920 bfd_put_32 (input_bfd,
006589cf
AM
14921 LIS_R2 + PPC_HA (relocation),
14922 contents + rel->r_offset);
95f0d0d2 14923 bfd_put_32 (input_bfd,
006589cf
AM
14924 ADDI_R2_R2 + PPC_LO (relocation),
14925 contents + rel->r_offset + 4);
14926 }
14927 }
14928 else
14929 {
14930 relocation -= (rel->r_offset
14931 + input_section->output_offset
14932 + input_section->output_section->vma);
14933 if (relocation + 0x80008000 <= 0xffffffff)
14934 {
14935 unsigned int insn1, insn2;
14936
14937 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14938 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14939 if ((insn1 & ~0xfffc) == LD_R2_0R12
14940 && insn2 == ADD_R2_R2_R12)
14941 {
95f0d0d2 14942 bfd_put_32 (input_bfd,
006589cf
AM
14943 ADDIS_R2_R12 + PPC_HA (relocation),
14944 contents + rel->r_offset);
95f0d0d2 14945 bfd_put_32 (input_bfd,
006589cf
AM
14946 ADDI_R2_R2 + PPC_LO (relocation),
14947 contents + rel->r_offset + 4);
14948 }
14949 }
14950 }
14951 break;
14952
52a82034
AM
14953 case R_PPC64_REL16_HA:
14954 /* If we are generating a non-PIC executable, edit
14955 . 0: addis 2,12,.TOC.-0b@ha
14956 . addi 2,2,.TOC.-0b@l
14957 used by ELFv2 global entry points to set up r2, to
14958 . lis 2,.TOC.@ha
14959 . addi 2,2,.TOC.@l
14960 if .TOC. is in range. */
0e1862bb 14961 if (!bfd_link_pic (info)
810d4e75 14962 && !info->traditional_format
006589cf 14963 && !htab->opd_abi
4f038ee5 14964 && rel->r_addend == d_offset
52a82034
AM
14965 && h != NULL && &h->elf == htab->elf.hgot
14966 && rel + 1 < relend
14967 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14968 && rel[1].r_offset == rel->r_offset + 4
14969 && rel[1].r_addend == rel->r_addend + 4
14970 && relocation + 0x80008000 <= 0xffffffff)
14971 {
14972 unsigned int insn1, insn2;
4a421c53 14973 offset = rel->r_offset - d_offset;
95f0d0d2
AM
14974 insn1 = bfd_get_32 (input_bfd, contents + offset);
14975 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14976 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14977 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14978 {
14979 r_type = R_PPC64_ADDR16_HA;
14980 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14981 rel->r_addend -= d_offset;
14982 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14983 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14984 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14985 }
14986 }
14987 break;
411e1bfb
AM
14988 }
14989
14990 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14991 insn = 0;
b25116a9
AM
14992 max_br_offset = 1 << 25;
14993 addend = rel->r_addend;
bc30df16 14994 reloc_dest = DEST_NORMAL;
65f38f15 14995 switch (r_type)
5bd4f169
AM
14996 {
14997 default:
65f38f15 14998 break;
5bd4f169 14999
3b421ab3
AM
15000 case R_PPC64_TOCSAVE:
15001 if (relocation + addend == (rel->r_offset
15002 + input_section->output_offset
15003 + input_section->output_section->vma)
15004 && tocsave_find (htab, NO_INSERT,
15005 &local_syms, rel, input_bfd))
15006 {
15007 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15008 if (insn == NOP
15009 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
15010 bfd_put_32 (input_bfd,
15011 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
15012 contents + rel->r_offset);
15013 }
15014 break;
15015
65f38f15
AM
15016 /* Branch taken prediction relocations. */
15017 case R_PPC64_ADDR14_BRTAKEN:
15018 case R_PPC64_REL14_BRTAKEN:
cedb70c5 15019 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 15020 /* Fall through. */
65f38f15 15021
86c76c7b 15022 /* Branch not taken prediction relocations. */
65f38f15
AM
15023 case R_PPC64_ADDR14_BRNTAKEN:
15024 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 15025 insn |= bfd_get_32 (input_bfd,
411e1bfb 15026 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 15027 /* Fall through. */
86c76c7b 15028
b25116a9
AM
15029 case R_PPC64_REL14:
15030 max_br_offset = 1 << 15;
1a0670f3 15031 /* Fall through. */
5bd4f169 15032
65f38f15 15033 case R_PPC64_REL24:
05d0e962 15034 case R_PPC64_REL24_NOTOC:
23cedd1d 15035 case R_PPC64_PLTCALL:
5663e321 15036 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
15037 /* Calls to functions with a different TOC, such as calls to
15038 shared objects, need to alter the TOC pointer. This is
15039 done using a linkage stub. A REL24 branching to these
15040 linkage stubs needs to be followed by a nop, as the nop
15041 will be replaced with an instruction to restore the TOC
15042 base pointer. */
8387904d 15043 fdh = h;
b31867b6
AM
15044 if (h != NULL
15045 && h->oh != NULL
15046 && h->oh->is_func_descriptor)
15047 fdh = ppc_follow_link (h->oh);
31c76678
DK
15048 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15049 htab);
5663e321
AM
15050 if ((r_type == R_PPC64_PLTCALL
15051 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 15052 && stub_entry != NULL
05d0e962
AM
15053 && stub_entry->stub_type >= ppc_stub_plt_call
15054 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
15055 stub_entry = NULL;
15056
6abec6bc 15057 if (stub_entry != NULL
d0abeec8
AM
15058 && ((stub_entry->stub_type >= ppc_stub_plt_call
15059 && stub_entry->stub_type <= ppc_stub_plt_call_both)
ad8e1ba5 15060 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
15061 || stub_entry->stub_type == ppc_stub_plt_branch_both
15062 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15063 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 15064 {
b25116a9 15065 bfd_boolean can_plt_call = FALSE;
721956f4 15066
6e1816be
AM
15067 if (stub_entry->stub_type == ppc_stub_plt_call
15068 && !htab->opd_abi
15069 && htab->params->plt_localentry0 != 0
15070 && is_elfv2_localentry0 (&h->elf))
15071 {
15072 /* The function doesn't use or change r2. */
15073 can_plt_call = TRUE;
15074 }
05d0e962
AM
15075 else if (r_type == R_PPC64_REL24_NOTOC)
15076 {
15077 /* NOTOC calls don't need to restore r2. */
15078 can_plt_call = TRUE;
15079 }
6e1816be 15080
f378ab09 15081 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
15082 branch and link followed by a nop. The nop is
15083 replaced by an insn to restore r2. */
6e1816be 15084 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 15085 {
ba8ca3e7
AM
15086 unsigned long br;
15087
15088 br = bfd_get_32 (input_bfd,
15089 contents + rel->r_offset);
15090 if ((br & 1) != 0)
41bd81ab 15091 {
ba8ca3e7
AM
15092 unsigned long nop;
15093
15094 nop = bfd_get_32 (input_bfd,
15095 contents + rel->r_offset + 4);
23cedd1d
AM
15096 if (nop == LD_R2_0R1 + STK_TOC (htab))
15097 can_plt_call = TRUE;
15098 else if (nop == NOP
15099 || nop == CROR_151515
15100 || nop == CROR_313131)
a7f2871e 15101 {
ba8ca3e7
AM
15102 if (h != NULL
15103 && (h == htab->tls_get_addr_fd
15104 || h == htab->tls_get_addr)
7c9cf415 15105 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
15106 {
15107 /* Special stub used, leave nop alone. */
15108 }
15109 else
a078d95a
AM
15110 bfd_put_32 (input_bfd,
15111 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
15112 contents + rel->r_offset + 4);
15113 can_plt_call = TRUE;
a7f2871e 15114 }
41bd81ab 15115 }
5bd4f169 15116 }
721956f4 15117
ba8ca3e7 15118 if (!can_plt_call && h != NULL)
721956f4 15119 {
ba8ca3e7
AM
15120 const char *name = h->elf.root.root.string;
15121
15122 if (*name == '.')
15123 ++name;
15124
15125 if (strncmp (name, "__libc_start_main", 17) == 0
15126 && (name[17] == 0 || name[17] == '@'))
6ab189d5 15127 {
ba8ca3e7
AM
15128 /* Allow crt1 branch to go via a toc adjusting
15129 stub. Other calls that never return could do
15130 the same, if we could detect such. */
b25116a9 15131 can_plt_call = TRUE;
6ab189d5 15132 }
ba8ca3e7
AM
15133 }
15134
15135 if (!can_plt_call)
15136 {
15137 /* g++ as of 20130507 emits self-calls without a
15138 following nop. This is arguably wrong since we
15139 have conflicting information. On the one hand a
15140 global symbol and on the other a local call
15141 sequence, but don't error for this special case.
15142 It isn't possible to cheaply verify we have
15143 exactly such a call. Allow all calls to the same
15144 section. */
15145 asection *code_sec = sec;
15146
15147 if (get_opd_info (sec) != NULL)
ad8e1ba5 15148 {
ba8ca3e7
AM
15149 bfd_vma off = (relocation + addend
15150 - sec->output_section->vma
15151 - sec->output_offset);
bc30df16 15152
ba8ca3e7 15153 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 15154 }
ba8ca3e7
AM
15155 if (code_sec == input_section)
15156 can_plt_call = TRUE;
15157 }
15158
15159 if (!can_plt_call)
15160 {
05d0e962
AM
15161 if (stub_entry->stub_type >= ppc_stub_plt_call
15162 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 15163 info->callbacks->einfo
695344c0 15164 /* xgettext:c-format */
c1c8c1ef 15165 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15166 "(plt call stub)\n"),
4805fc55
AM
15167 input_bfd, input_section, rel->r_offset, sym_name);
15168 else
15169 info->callbacks->einfo
695344c0 15170 /* xgettext:c-format */
c1c8c1ef 15171 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15172 "(toc save/adjust stub)\n"),
4805fc55 15173 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
15174
15175 bfd_set_error (bfd_error_bad_value);
15176 ret = FALSE;
721956f4
AM
15177 }
15178
b25116a9 15179 if (can_plt_call
05d0e962
AM
15180 && stub_entry->stub_type >= ppc_stub_plt_call
15181 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
15182 unresolved_reloc = FALSE;
15183 }
15184
6abec6bc
AM
15185 if ((stub_entry == NULL
15186 || stub_entry->stub_type == ppc_stub_long_branch
15187 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
15188 && get_opd_info (sec) != NULL)
15189 {
15190 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
15191 bfd_vma off = (relocation + addend
15192 - sec->output_section->vma
15193 - sec->output_offset);
aef36ac1 15194 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
15195 if (dest != (bfd_vma) -1)
15196 {
15197 relocation = dest;
15198 addend = 0;
bc30df16 15199 reloc_dest = DEST_OPD;
8387904d
AM
15200 }
15201 }
15202
b25116a9
AM
15203 /* If the branch is out of reach we ought to have a long
15204 branch stub. */
15205 from = (rel->r_offset
15206 + input_section->output_offset
15207 + input_section->output_section->vma);
15208
6911b7dc
AM
15209 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15210 ? fdh->elf.other
15211 : sym->st_other);
15212
6abec6bc
AM
15213 if (stub_entry != NULL
15214 && (stub_entry->stub_type == ppc_stub_long_branch
15215 || stub_entry->stub_type == ppc_stub_plt_branch)
15216 && (r_type == R_PPC64_ADDR14_BRTAKEN
15217 || r_type == R_PPC64_ADDR14_BRNTAKEN
15218 || (relocation + addend - from + max_br_offset
15219 < 2 * max_br_offset)))
15220 /* Don't use the stub if this branch is in range. */
15221 stub_entry = NULL;
b25116a9 15222
05d0e962
AM
15223 if (stub_entry != NULL
15224 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15225 || stub_entry->stub_type == ppc_stub_long_branch_both
15226 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15227 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15228 && (r_type != R_PPC64_REL24_NOTOC
15229 || ((fdh ? fdh->elf.other : sym->st_other)
4a4e7361 15230 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
05d0e962
AM
15231 && (relocation + addend - from + max_br_offset
15232 < 2 * max_br_offset))
15233 stub_entry = NULL;
15234
15235 if (stub_entry != NULL
15236 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15237 || stub_entry->stub_type == ppc_stub_long_branch_both
15238 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15239 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15240 && r_type == R_PPC64_REL24_NOTOC
15241 && (relocation + addend - from + max_br_offset
15242 < 2 * max_br_offset))
15243 stub_entry = NULL;
15244
b25116a9
AM
15245 if (stub_entry != NULL)
15246 {
15247 /* Munge up the value and addend so that we call the stub
15248 rather than the procedure directly. */
a4b6fadd
AM
15249 asection *stub_sec = stub_entry->group->stub_sec;
15250
15251 if (stub_entry->stub_type == ppc_stub_save_res)
15252 relocation += (stub_sec->output_offset
15253 + stub_sec->output_section->vma
15254 + stub_sec->size - htab->sfpr->size
15255 - htab->sfpr->output_offset
15256 - htab->sfpr->output_section->vma);
15257 else
15258 relocation = (stub_entry->stub_offset
15259 + stub_sec->output_offset
15260 + stub_sec->output_section->vma);
b25116a9 15261 addend = 0;
bc30df16 15262 reloc_dest = DEST_STUB;
3b421ab3 15263
05d0e962
AM
15264 if (((stub_entry->stub_type == ppc_stub_plt_call
15265 && ALWAYS_EMIT_R2SAVE)
15266 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15267 || stub_entry->stub_type == ppc_stub_plt_call_both)
e81b4c93
AM
15268 && !(h != NULL
15269 && (h == htab->tls_get_addr_fd
15270 || h == htab->tls_get_addr)
15271 && htab->params->tls_get_addr_opt)
3b421ab3
AM
15272 && rel + 1 < relend
15273 && rel[1].r_offset == rel->r_offset + 4
15274 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15275 relocation += 4;
05d0e962
AM
15276 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15277 || stub_entry->stub_type == ppc_stub_plt_branch_both
15278 || stub_entry->stub_type == ppc_stub_plt_call_both)
15279 && r_type == R_PPC64_REL24_NOTOC)
15280 relocation += 4;
5663e321
AM
15281
15282 if (r_type == R_PPC64_REL24_NOTOC
15283 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
15284 || stub_entry->stub_type == ppc_stub_plt_call_both))
15285 htab->notoc_plt = 1;
b25116a9
AM
15286 }
15287
15288 if (insn != 0)
15289 {
794e51c0 15290 if (is_isa_v2)
721956f4 15291 {
b25116a9
AM
15292 /* Set 'a' bit. This is 0b00010 in BO field for branch
15293 on CR(BI) insns (BO == 001at or 011at), and 0b01000
15294 for branch on CTR insns (BO == 1a00t or 1a01t). */
15295 if ((insn & (0x14 << 21)) == (0x04 << 21))
15296 insn |= 0x02 << 21;
15297 else if ((insn & (0x14 << 21)) == (0x10 << 21))
15298 insn |= 0x08 << 21;
15299 else
15300 break;
15301 }
15302 else
15303 {
15304 /* Invert 'y' bit if not the default. */
4cc603a5 15305 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 15306 insn ^= 0x01 << 21;
721956f4 15307 }
b25116a9 15308
95f0d0d2 15309 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 15310 }
e86ce104 15311
06da1e8e
AM
15312 /* NOP out calls to undefined weak functions.
15313 We can thus call a weak function without first
15314 checking whether the function is defined. */
b25116a9 15315 else if (h != NULL
039b3fef 15316 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 15317 && h->elf.dynindx == -1
05d0e962
AM
15318 && (r_type == R_PPC64_REL24
15319 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 15320 && relocation == 0
4cc603a5 15321 && addend == 0)
e86ce104 15322 {
95f0d0d2 15323 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 15324 goto copy_reloc;
e86ce104 15325 }
65f38f15 15326 break;
066f4018
AM
15327
15328 case R_PPC64_GOT16_DS:
15329 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15330 if (relocation + addend - from + 0x8000 < 0x10000
15331 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15332 {
15333 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15334 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15335 {
15336 insn += (14u << 26) - (58u << 26);
15337 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15338 r_type = R_PPC64_TOC16;
15339 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15340 }
15341 }
15342 break;
15343
15344 case R_PPC64_GOT16_LO_DS:
15345 case R_PPC64_GOT16_HA:
15346 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15347 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
15348 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15349 {
15350 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15351 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15352 {
15353 insn += (14u << 26) - (58u << 26);
15354 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15355 r_type = R_PPC64_TOC16_LO;
15356 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15357 }
15358 else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
15359 {
15360 r_type = R_PPC64_TOC16_HA;
15361 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15362 }
15363 }
15364 break;
4a421c53
AM
15365
15366 case R_PPC64_GOT_PCREL34:
15367 from = (rel->r_offset
15368 + input_section->output_section->vma
15369 + input_section->output_offset);
15370 if (relocation - from + (1ULL << 33) < 1ULL << 34
15371 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15372 {
15373 offset = rel->r_offset;
15374 pinsn = bfd_get_32 (input_bfd, contents + offset);
15375 pinsn <<= 32;
15376 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15377 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15378 == ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
15379 {
15380 /* Replace with paddi. */
15381 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
15382 r_type = R_PPC64_PCREL34;
15383 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15384 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
15385 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
15386 goto pcrelopt;
15387 }
15388 }
15389 break;
15390
15391 case R_PPC64_PCREL34:
15392 if (SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15393 {
15394 offset = rel->r_offset;
15395 pinsn = bfd_get_32 (input_bfd, contents + offset);
15396 pinsn <<= 32;
15397 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15398 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15399 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
15400 | (14ULL << 26) /* paddi */))
15401 {
15402 pcrelopt:
15403 if (rel + 1 < relend
15404 && rel[1].r_offset == offset
15405 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT))
15406 {
15407 bfd_vma off2 = rel[1].r_addend;
15408 if (off2 == 0)
15409 /* zero means next insn. */
15410 off2 = 8;
15411 off2 += offset;
15412 if (off2 + 4 <= input_section->size)
15413 {
15414 uint64_t pinsn2;
dd9b12c2 15415 bfd_signed_vma addend_off;
4a421c53
AM
15416 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
15417 pinsn2 <<= 32;
15418 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
77486630
AM
15419 {
15420 if (off2 + 8 > input_section->size)
15421 break;
15422 pinsn2 |= bfd_get_32 (input_bfd,
15423 contents + off2 + 4);
15424 }
dd9b12c2 15425 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
4a421c53 15426 {
dd9b12c2
AM
15427 addend += addend_off;
15428 rel->r_addend = addend;
4a421c53
AM
15429 bfd_put_32 (input_bfd, pinsn >> 32,
15430 contents + offset);
15431 bfd_put_32 (input_bfd, pinsn,
15432 contents + offset + 4);
15433 bfd_put_32 (input_bfd, pinsn2 >> 32,
15434 contents + off2);
77486630
AM
15435 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15436 bfd_put_32 (input_bfd, pinsn2,
15437 contents + off2 + 4);
4a421c53
AM
15438 }
15439 }
15440 }
15441 }
15442 }
15443 break;
65f38f15 15444 }
5bd4f169 15445
411e1bfb 15446 tls_type = 0;
23cedd1d 15447 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
15448 switch (r_type)
15449 {
15450 default:
cf97bcb0
AM
15451 /* xgettext:c-format */
15452 _bfd_error_handler (_("%pB: %s unsupported"),
15453 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 15454
65f38f15 15455 bfd_set_error (bfd_error_bad_value);
b34976b6 15456 ret = FALSE;
c316a17c 15457 goto copy_reloc;
5bd4f169 15458
65f38f15 15459 case R_PPC64_NONE:
411e1bfb 15460 case R_PPC64_TLS:
727fc41e
AM
15461 case R_PPC64_TLSGD:
15462 case R_PPC64_TLSLD:
3b421ab3 15463 case R_PPC64_TOCSAVE:
04c9666a
AM
15464 case R_PPC64_GNU_VTINHERIT:
15465 case R_PPC64_GNU_VTENTRY:
006589cf 15466 case R_PPC64_ENTRY:
4a421c53 15467 case R_PPC64_PCREL_OPT:
c316a17c 15468 goto copy_reloc;
5bd4f169
AM
15469
15470 /* GOT16 relocations. Like an ADDR16 using the symbol's
15471 address in the GOT as relocation value instead of the
411e1bfb 15472 symbol's value itself. Also, create a GOT entry for the
5bd4f169 15473 symbol and put the symbol value there. */
411e1bfb
AM
15474 case R_PPC64_GOT_TLSGD16:
15475 case R_PPC64_GOT_TLSGD16_LO:
15476 case R_PPC64_GOT_TLSGD16_HI:
15477 case R_PPC64_GOT_TLSGD16_HA:
c213164a 15478 case R_PPC64_GOT_TLSGD34:
951fd09b 15479 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
15480 goto dogot;
15481
15482 case R_PPC64_GOT_TLSLD16:
15483 case R_PPC64_GOT_TLSLD16_LO:
15484 case R_PPC64_GOT_TLSLD16_HI:
15485 case R_PPC64_GOT_TLSLD16_HA:
c213164a 15486 case R_PPC64_GOT_TLSLD34:
951fd09b 15487 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
15488 goto dogot;
15489
15490 case R_PPC64_GOT_TPREL16_DS:
15491 case R_PPC64_GOT_TPREL16_LO_DS:
15492 case R_PPC64_GOT_TPREL16_HI:
15493 case R_PPC64_GOT_TPREL16_HA:
c213164a 15494 case R_PPC64_GOT_TPREL34:
411e1bfb
AM
15495 tls_type = TLS_TLS | TLS_TPREL;
15496 goto dogot;
15497
15498 case R_PPC64_GOT_DTPREL16_DS:
15499 case R_PPC64_GOT_DTPREL16_LO_DS:
15500 case R_PPC64_GOT_DTPREL16_HI:
15501 case R_PPC64_GOT_DTPREL16_HA:
c213164a 15502 case R_PPC64_GOT_DTPREL34:
411e1bfb
AM
15503 tls_type = TLS_TLS | TLS_DTPREL;
15504 goto dogot;
15505
65f38f15
AM
15506 case R_PPC64_GOT16:
15507 case R_PPC64_GOT16_LO:
15508 case R_PPC64_GOT16_HI:
15509 case R_PPC64_GOT16_HA:
15510 case R_PPC64_GOT16_DS:
15511 case R_PPC64_GOT16_LO_DS:
5663e321 15512 case R_PPC64_GOT_PCREL34:
411e1bfb 15513 dogot:
5bd4f169
AM
15514 {
15515 /* Relocation is to the entry for this symbol in the global
15516 offset table. */
e717da7e 15517 asection *got;
d881513a 15518 bfd_vma *offp;
5bd4f169 15519 bfd_vma off;
d881513a 15520 unsigned long indx = 0;
927be08e 15521 struct got_entry *ent;
65f38f15 15522
d881513a
AM
15523 if (tls_type == (TLS_TLS | TLS_LD)
15524 && (h == NULL
f5385ebf 15525 || !h->elf.def_dynamic))
927be08e 15526 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 15527 else
5bd4f169 15528 {
d881513a
AM
15529 if (h != NULL)
15530 {
f0158f44
AM
15531 if (!htab->elf.dynamic_sections_created
15532 || h->elf.dynindx == -1
15533 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 15534 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
15535 /* This is actually a static link, or it is a
15536 -Bsymbolic link and the symbol is defined
15537 locally, or the symbol was forced to be local
15538 because of a version file. */
15539 ;
15540 else
15541 {
039b3fef 15542 indx = h->elf.dynindx;
d881513a
AM
15543 unresolved_reloc = FALSE;
15544 }
039b3fef 15545 ent = h->elf.got.glist;
d881513a 15546 }
411e1bfb 15547 else
5bd4f169 15548 {
d881513a
AM
15549 if (local_got_ents == NULL)
15550 abort ();
15551 ent = local_got_ents[r_symndx];
5bd4f169 15552 }
d881513a
AM
15553
15554 for (; ent != NULL; ent = ent->next)
133a1f60 15555 if (ent->addend == orig_rel.r_addend
e717da7e 15556 && ent->owner == input_bfd
d881513a
AM
15557 && ent->tls_type == tls_type)
15558 break;
5bd4f169 15559 }
411e1bfb 15560
927be08e
AM
15561 if (ent == NULL)
15562 abort ();
15563 if (ent->is_indirect)
15564 ent = ent->got.ent;
15565 offp = &ent->got.offset;
15566 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
15567 if (got == NULL)
15568 abort ();
15569
411e1bfb
AM
15570 /* The offset must always be a multiple of 8. We use the
15571 least significant bit to record whether we have already
15572 processed this entry. */
d881513a 15573 off = *offp;
411e1bfb
AM
15574 if ((off & 1) != 0)
15575 off &= ~1;
5bd4f169
AM
15576 else
15577 {
411e1bfb
AM
15578 /* Generate relocs for the dynamic linker, except in
15579 the case of TLSLD where we'll use one entry per
15580 module. */
25f23106
AM
15581 asection *relgot;
15582 bfd_boolean ifunc;
e717da7e 15583
d881513a 15584 *offp = off | 1;
25f23106
AM
15585 relgot = NULL;
15586 ifunc = (h != NULL
15587 ? h->elf.type == STT_GNU_IFUNC
15588 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 15589 if (ifunc)
82e66161
AM
15590 {
15591 relgot = htab->elf.irelplt;
15592 if (indx == 0)
15593 htab->local_ifunc_resolver = 1;
15594 else if (is_static_defined (&h->elf))
15595 htab->maybe_local_ifunc_resolver = 1;
15596 }
f0158f44
AM
15597 else if (indx != 0
15598 || (bfd_link_pic (info)
15599 && (h == NULL
21d68fcd 15600 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 15601 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
15602 && !h->elf.def_dynamic))
15603 && !(tls_type == (TLS_TLS | TLS_TPREL)
15604 && bfd_link_executable (info)
15605 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 15606 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 15607 if (relgot != NULL)
5bd4f169 15608 {
e717da7e
AM
15609 outrel.r_offset = (got->output_section->vma
15610 + got->output_offset
411e1bfb 15611 + off);
133a1f60 15612 outrel.r_addend = orig_rel.r_addend;
d881513a 15613 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 15614 {
411e1bfb 15615 outrel.r_addend = 0;
e515b051 15616 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
15617 if (tls_type == (TLS_TLS | TLS_GD))
15618 {
e717da7e
AM
15619 loc = relgot->contents;
15620 loc += (relgot->reloc_count++
d881513a
AM
15621 * sizeof (Elf64_External_Rela));
15622 bfd_elf64_swap_reloca_out (output_bfd,
15623 &outrel, loc);
e515b051 15624 outrel.r_offset += 8;
133a1f60 15625 outrel.r_addend = orig_rel.r_addend;
d881513a
AM
15626 outrel.r_info
15627 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 15628 }
411e1bfb 15629 }
951fd09b 15630 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 15631 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 15632 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 15633 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
15634 else if (indx != 0)
15635 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15636 else
81407a69 15637 {
25f23106
AM
15638 if (ifunc)
15639 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15640 else
15641 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
15642
15643 /* Write the .got section contents for the sake
15644 of prelink. */
e717da7e 15645 loc = got->contents + off;
23fbd6fa
JJ
15646 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15647 loc);
81407a69 15648 }
81407a69
AM
15649
15650 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
15651 {
15652 outrel.r_addend += relocation;
15653 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
15654 {
15655 if (htab->elf.tls_sec == NULL)
15656 outrel.r_addend = 0;
15657 else
15658 outrel.r_addend -= htab->elf.tls_sec->vma;
15659 }
e515b051 15660 }
e717da7e
AM
15661 loc = relgot->contents;
15662 loc += (relgot->reloc_count++
411e1bfb
AM
15663 * sizeof (Elf64_External_Rela));
15664 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15665 }
15666
ad8e1ba5 15667 /* Init the .got section contents here if we're not
81407a69 15668 emitting a reloc. */
d881513a 15669 else
411e1bfb 15670 {
133a1f60 15671 relocation += orig_rel.r_addend;
f0158f44 15672 if (tls_type != 0)
411e1bfb 15673 {
989f9879
AM
15674 if (htab->elf.tls_sec == NULL)
15675 relocation = 0;
15676 else
15677 {
f0158f44
AM
15678 if (tls_type & TLS_LD)
15679 relocation = 0;
15680 else
15681 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 15682 if (tls_type & TLS_TPREL)
989f9879
AM
15683 relocation += DTP_OFFSET - TP_OFFSET;
15684 }
5bd4f169 15685
f0158f44 15686 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
15687 {
15688 bfd_put_64 (output_bfd, relocation,
e717da7e 15689 got->contents + off + 8);
676ee2b5 15690 relocation = 1;
7b609f53 15691 }
411e1bfb
AM
15692 }
15693 bfd_put_64 (output_bfd, relocation,
e717da7e 15694 got->contents + off);
5bd4f169
AM
15695 }
15696 }
15697
65f38f15
AM
15698 if (off >= (bfd_vma) -2)
15699 abort ();
15700
bf102f86 15701 relocation = got->output_section->vma + got->output_offset + off;
133a1f60 15702 addend = 0;
c213164a
AM
15703 if (!(r_type == R_PPC64_GOT_PCREL34
15704 || r_type == R_PPC64_GOT_TLSGD34
15705 || r_type == R_PPC64_GOT_TLSLD34
15706 || r_type == R_PPC64_GOT_TPREL34
15707 || r_type == R_PPC64_GOT_DTPREL34))
5663e321 15708 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 15709 }
65f38f15
AM
15710 break;
15711
15712 case R_PPC64_PLT16_HA:
15713 case R_PPC64_PLT16_HI:
15714 case R_PPC64_PLT16_LO:
08be3224 15715 case R_PPC64_PLT16_LO_DS:
5663e321
AM
15716 case R_PPC64_PLT_PCREL34:
15717 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
15718 case R_PPC64_PLT32:
15719 case R_PPC64_PLT64:
23cedd1d 15720 case R_PPC64_PLTSEQ:
5663e321 15721 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 15722 case R_PPC64_PLTCALL:
5663e321 15723 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
15724 /* Relocation is to the entry for this symbol in the
15725 procedure linkage table. */
23cedd1d 15726 unresolved_reloc = TRUE;
cbf95972
AM
15727 {
15728 struct plt_entry **plt_list = NULL;
15729 if (h != NULL)
15730 plt_list = &h->elf.plt.plist;
15731 else if (local_got_ents != NULL)
15732 {
15733 struct plt_entry **local_plt = (struct plt_entry **)
15734 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 15735 plt_list = local_plt + r_symndx;
cbf95972
AM
15736 }
15737 if (plt_list)
15738 {
15739 struct plt_entry *ent;
65f38f15 15740
cbf95972
AM
15741 for (ent = *plt_list; ent != NULL; ent = ent->next)
15742 if (ent->plt.offset != (bfd_vma) -1
133a1f60 15743 && ent->addend == orig_rel.r_addend)
cbf95972
AM
15744 {
15745 asection *plt;
08be3224 15746 bfd_vma got;
cbf95972
AM
15747
15748 plt = htab->elf.splt;
15749 if (!htab->elf.dynamic_sections_created
15750 || h == NULL
15751 || h->elf.dynindx == -1)
2d7ad24e
AM
15752 {
15753 if (h != NULL
15754 ? h->elf.type == STT_GNU_IFUNC
15755 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15756 plt = htab->elf.iplt;
15757 else
15758 plt = htab->pltlocal;
15759 }
15760 relocation = (plt->output_section->vma
15761 + plt->output_offset
15762 + ent->plt.offset);
08be3224 15763 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
15764 || r_type == R_PPC64_PLT16_HI
15765 || r_type == R_PPC64_PLT16_LO
15766 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
15767 {
15768 got = (elf_gp (output_bfd)
15769 + htab->sec_info[input_section->id].toc_off);
15770 relocation -= got;
15771 }
133a1f60 15772 addend = 0;
cbf95972
AM
15773 unresolved_reloc = FALSE;
15774 break;
15775 }
15776 }
15777 }
65f38f15 15778 break;
5bd4f169 15779
0b13192e
AM
15780 case R_PPC64_TOC:
15781 /* Relocation value is TOC base. */
15782 relocation = TOCstart;
cf35638d 15783 if (r_symndx == STN_UNDEF)
6f20ed8a 15784 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
15785 else if (unresolved_reloc)
15786 ;
6f20ed8a
AM
15787 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15788 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
15789 else
15790 unresolved_reloc = TRUE;
ab96bf03 15791 goto dodyn;
0b13192e 15792
5bd4f169
AM
15793 /* TOC16 relocs. We want the offset relative to the TOC base,
15794 which is the address of the start of the TOC plus 0x8000.
15795 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15796 in this order. */
65f38f15
AM
15797 case R_PPC64_TOC16:
15798 case R_PPC64_TOC16_LO:
15799 case R_PPC64_TOC16_HI:
15800 case R_PPC64_TOC16_DS:
15801 case R_PPC64_TOC16_LO_DS:
15802 case R_PPC64_TOC16_HA:
6f20ed8a 15803 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
15804 break;
15805
15806 /* Relocate against the beginning of the section. */
65f38f15
AM
15807 case R_PPC64_SECTOFF:
15808 case R_PPC64_SECTOFF_LO:
15809 case R_PPC64_SECTOFF_HI:
15810 case R_PPC64_SECTOFF_DS:
15811 case R_PPC64_SECTOFF_LO_DS:
15812 case R_PPC64_SECTOFF_HA:
4ce794b7 15813 if (sec != NULL)
65f38f15 15814 addend -= sec->output_section->vma;
5bd4f169
AM
15815 break;
15816
25f23106
AM
15817 case R_PPC64_REL16:
15818 case R_PPC64_REL16_LO:
15819 case R_PPC64_REL16_HI:
15820 case R_PPC64_REL16_HA:
4a969973
AM
15821 case R_PPC64_REL16_HIGH:
15822 case R_PPC64_REL16_HIGHA:
15823 case R_PPC64_REL16_HIGHER:
15824 case R_PPC64_REL16_HIGHERA:
15825 case R_PPC64_REL16_HIGHEST:
15826 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
15827 case R_PPC64_REL16_HIGHER34:
15828 case R_PPC64_REL16_HIGHERA34:
15829 case R_PPC64_REL16_HIGHEST34:
15830 case R_PPC64_REL16_HIGHESTA34:
a680de9a 15831 case R_PPC64_REL16DX_HA:
721956f4
AM
15832 case R_PPC64_REL14:
15833 case R_PPC64_REL14_BRNTAKEN:
15834 case R_PPC64_REL14_BRTAKEN:
5d1634d7 15835 case R_PPC64_REL24:
05d0e962 15836 case R_PPC64_REL24_NOTOC:
5663e321
AM
15837 case R_PPC64_PCREL34:
15838 case R_PPC64_PCREL28:
5d1634d7
AM
15839 break;
15840
411e1bfb
AM
15841 case R_PPC64_TPREL16:
15842 case R_PPC64_TPREL16_LO:
15843 case R_PPC64_TPREL16_HI:
15844 case R_PPC64_TPREL16_HA:
15845 case R_PPC64_TPREL16_DS:
15846 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
15847 case R_PPC64_TPREL16_HIGH:
15848 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
15849 case R_PPC64_TPREL16_HIGHER:
15850 case R_PPC64_TPREL16_HIGHERA:
15851 case R_PPC64_TPREL16_HIGHEST:
15852 case R_PPC64_TPREL16_HIGHESTA:
c213164a 15853 case R_PPC64_TPREL34:
766bc656
AM
15854 if (h != NULL
15855 && h->elf.root.type == bfd_link_hash_undefweak
15856 && h->elf.dynindx == -1)
15857 {
15858 /* Make this relocation against an undefined weak symbol
15859 resolve to zero. This is really just a tweak, since
15860 code using weak externs ought to check that they are
15861 defined before using them. */
15862 bfd_byte *p = contents + rel->r_offset - d_offset;
15863
95f0d0d2 15864 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
15865 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15866 if (insn != 0)
95f0d0d2 15867 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
15868 break;
15869 }
989f9879
AM
15870 if (htab->elf.tls_sec != NULL)
15871 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
15872 /* The TPREL16 relocs shouldn't really be used in shared
15873 libs or with non-local symbols as that will result in
15874 DT_TEXTREL being set, but support them anyway. */
15875 goto dodyn;
411e1bfb
AM
15876
15877 case R_PPC64_DTPREL16:
15878 case R_PPC64_DTPREL16_LO:
15879 case R_PPC64_DTPREL16_HI:
15880 case R_PPC64_DTPREL16_HA:
15881 case R_PPC64_DTPREL16_DS:
15882 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
15883 case R_PPC64_DTPREL16_HIGH:
15884 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
15885 case R_PPC64_DTPREL16_HIGHER:
15886 case R_PPC64_DTPREL16_HIGHERA:
15887 case R_PPC64_DTPREL16_HIGHEST:
15888 case R_PPC64_DTPREL16_HIGHESTA:
c213164a 15889 case R_PPC64_DTPREL34:
989f9879
AM
15890 if (htab->elf.tls_sec != NULL)
15891 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
15892 break;
15893
45965137
AM
15894 case R_PPC64_ADDR64_LOCAL:
15895 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15896 ? h->elf.other
15897 : sym->st_other);
15898 break;
15899
e515b051
AM
15900 case R_PPC64_DTPMOD64:
15901 relocation = 1;
15902 addend = 0;
15903 goto dodyn;
15904
411e1bfb 15905 case R_PPC64_TPREL64:
989f9879
AM
15906 if (htab->elf.tls_sec != NULL)
15907 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
15908 goto dodyn;
15909
15910 case R_PPC64_DTPREL64:
989f9879
AM
15911 if (htab->elf.tls_sec != NULL)
15912 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 15913 /* Fall through. */
411e1bfb 15914
65f38f15
AM
15915 /* Relocations that may need to be propagated if this is a
15916 dynamic object. */
04c9666a 15917 case R_PPC64_REL30:
65f38f15
AM
15918 case R_PPC64_REL32:
15919 case R_PPC64_REL64:
15920 case R_PPC64_ADDR14:
15921 case R_PPC64_ADDR14_BRNTAKEN:
15922 case R_PPC64_ADDR14_BRTAKEN:
15923 case R_PPC64_ADDR16:
15924 case R_PPC64_ADDR16_DS:
15925 case R_PPC64_ADDR16_HA:
15926 case R_PPC64_ADDR16_HI:
f9c6b907
AM
15927 case R_PPC64_ADDR16_HIGH:
15928 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15929 case R_PPC64_ADDR16_HIGHER:
15930 case R_PPC64_ADDR16_HIGHERA:
15931 case R_PPC64_ADDR16_HIGHEST:
15932 case R_PPC64_ADDR16_HIGHESTA:
15933 case R_PPC64_ADDR16_LO:
15934 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
15935 case R_PPC64_ADDR16_HIGHER34:
15936 case R_PPC64_ADDR16_HIGHERA34:
15937 case R_PPC64_ADDR16_HIGHEST34:
15938 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 15939 case R_PPC64_ADDR24:
65f38f15
AM
15940 case R_PPC64_ADDR32:
15941 case R_PPC64_ADDR64:
15942 case R_PPC64_UADDR16:
15943 case R_PPC64_UADDR32:
15944 case R_PPC64_UADDR64:
5663e321
AM
15945 case R_PPC64_D34:
15946 case R_PPC64_D34_LO:
15947 case R_PPC64_D34_HI30:
15948 case R_PPC64_D34_HA30:
15949 case R_PPC64_D28:
411e1bfb 15950 dodyn:
5d1634d7 15951 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
15952 break;
15953
41bd81ab
AM
15954 if (NO_OPD_RELOCS && is_opd)
15955 break;
15956
8a9e8e72 15957 if (bfd_link_pic (info)
b1b07054
AM
15958 ? ((h == NULL
15959 || h->dyn_relocs != NULL)
15960 && ((h != NULL && pc_dynrelocs (h))
15961 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
15962 : (h != NULL
15963 ? h->dyn_relocs != NULL
d311bc8b 15964 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 15965 {
b34976b6 15966 bfd_boolean skip, relocate;
65f38f15 15967 asection *sreloc;
1cf1f670 15968 bfd_vma out_off;
82e66161 15969 long indx = 0;
65f38f15
AM
15970
15971 /* When generating a dynamic object, these relocations
15972 are copied into the output file to be resolved at run
15973 time. */
15974
b34976b6
AM
15975 skip = FALSE;
15976 relocate = FALSE;
65f38f15 15977
1cf1f670
AM
15978 out_off = _bfd_elf_section_offset (output_bfd, info,
15979 input_section, rel->r_offset);
15980 if (out_off == (bfd_vma) -1)
b34976b6 15981 skip = TRUE;
1cf1f670 15982 else if (out_off == (bfd_vma) -2)
b34976b6 15983 skip = TRUE, relocate = TRUE;
1cf1f670
AM
15984 out_off += (input_section->output_section->vma
15985 + input_section->output_offset);
15986 outrel.r_offset = out_off;
411e1bfb 15987 outrel.r_addend = rel->r_addend;
65f38f15 15988
1cf1f670
AM
15989 /* Optimize unaligned reloc use. */
15990 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15991 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15992 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15993 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15994 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15995 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15996 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15997 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15998 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15999
65f38f15 16000 if (skip)
0bb2d96a 16001 memset (&outrel, 0, sizeof outrel);
afe397ea 16002 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
16003 && !is_opd
16004 && r_type != R_PPC64_TOC)
14acf4dc 16005 {
82e66161
AM
16006 indx = h->elf.dynindx;
16007 BFD_ASSERT (indx != -1);
16008 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 16009 }
65f38f15
AM
16010 else
16011 {
41bd81ab
AM
16012 /* This symbol is local, or marked to become local,
16013 or this is an opd section reloc which must point
16014 at a local function. */
65f38f15 16015 outrel.r_addend += relocation;
e86ce104 16016 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 16017 {
3fad3c7c 16018 if (is_opd && h != NULL)
afbe61cf
AM
16019 {
16020 /* Lie about opd entries. This case occurs
16021 when building shared libraries and we
16022 reference a function in another shared
3fad3c7c
AM
16023 lib. The same thing happens for a weak
16024 definition in an application that's
16025 overridden by a strong definition in a
16026 shared lib. (I believe this is a generic
16027 bug in binutils handling of weak syms.)
16028 In these cases we won't use the opd
1e2f5b6e 16029 entry in this lib. */
b34976b6 16030 unresolved_reloc = FALSE;
afbe61cf 16031 }
25f23106
AM
16032 if (!is_opd
16033 && r_type == R_PPC64_ADDR64
16034 && (h != NULL
16035 ? h->elf.type == STT_GNU_IFUNC
16036 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16037 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16038 else
16039 {
16040 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 16041
25f23106
AM
16042 /* We need to relocate .opd contents for ld.so.
16043 Prelink also wants simple and consistent rules
16044 for relocs. This make all RELATIVE relocs have
16045 *r_offset equal to r_addend. */
16046 relocate = TRUE;
16047 }
65f38f15
AM
16048 }
16049 else
16050 {
25f23106
AM
16051 if (h != NULL
16052 ? h->elf.type == STT_GNU_IFUNC
16053 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16054 {
25f53a85 16055 info->callbacks->einfo
695344c0 16056 /* xgettext:c-format */
174d0a74 16057 (_("%H: %s for indirect "
c1c8c1ef 16058 "function `%pT' unsupported\n"),
25f53a85 16059 input_bfd, input_section, rel->r_offset,
25f23106
AM
16060 ppc64_elf_howto_table[r_type]->name,
16061 sym_name);
16062 ret = FALSE;
16063 }
cf35638d 16064 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
16065 ;
16066 else if (sec == NULL || sec->owner == NULL)
16067 {
16068 bfd_set_error (bfd_error_bad_value);
b34976b6 16069 return FALSE;
65f38f15
AM
16070 }
16071 else
16072 {
f26a3287 16073 asection *osec = sec->output_section;
65f38f15 16074
f26a3287
AM
16075 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16076 {
16077 /* TLS symbol values are relative to the
16078 TLS segment. Dynamic relocations for
16079 local TLS symbols therefore can't be
16080 reduced to a relocation against their
16081 section symbol because it holds the
16082 address of the section, not a value
16083 relative to the TLS segment. We could
16084 change the .tdata dynamic section symbol
16085 to be zero value but STN_UNDEF works
16086 and is used elsewhere, eg. for TPREL64
16087 GOT relocs against local TLS symbols. */
16088 osec = htab->elf.tls_sec;
16089 indx = 0;
16090 }
16091 else
74541ad4 16092 {
74541ad4 16093 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
16094 if (indx == 0)
16095 {
16096 if ((osec->flags & SEC_READONLY) == 0
16097 && htab->elf.data_index_section != NULL)
16098 osec = htab->elf.data_index_section;
16099 else
16100 osec = htab->elf.text_index_section;
16101 indx = elf_section_data (osec)->dynindx;
16102 }
16103 BFD_ASSERT (indx != 0);
74541ad4 16104 }
74541ad4 16105
65f38f15
AM
16106 /* We are turning this relocation into one
16107 against a section symbol, so subtract out
16108 the output section's address but not the
16109 offset of the input section in the output
16110 section. */
16111 outrel.r_addend -= osec->vma;
16112 }
16113
16114 outrel.r_info = ELF64_R_INFO (indx, r_type);
16115 }
16116 }
16117
16118 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
16119 if (h != NULL
16120 ? h->elf.type == STT_GNU_IFUNC
16121 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
16122 {
16123 sreloc = htab->elf.irelplt;
16124 if (indx == 0)
16125 htab->local_ifunc_resolver = 1;
16126 else if (is_static_defined (&h->elf))
16127 htab->maybe_local_ifunc_resolver = 1;
16128 }
65f38f15
AM
16129 if (sreloc == NULL)
16130 abort ();
16131
dfbb6ac9
AM
16132 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16133 >= sreloc->size)
16134 abort ();
947216bf
AM
16135 loc = sreloc->contents;
16136 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
16137 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16138
16139 /* If this reloc is against an external symbol, it will
16140 be computed at runtime, so there's no need to do
81407a69
AM
16141 anything now. However, for the sake of prelink ensure
16142 that the section contents are a known value. */
2cdcc330 16143 if (!relocate)
81407a69
AM
16144 {
16145 unresolved_reloc = FALSE;
16146 /* The value chosen here is quite arbitrary as ld.so
16147 ignores section contents except for the special
16148 case of .opd where the contents might be accessed
16149 before relocation. Choose zero, as that won't
16150 cause reloc overflow. */
16151 relocation = 0;
16152 addend = 0;
16153 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16154 to improve backward compatibility with older
16155 versions of ld. */
16156 if (r_type == R_PPC64_ADDR64)
16157 addend = outrel.r_addend;
16158 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 16159 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 16160 addend = outrel.r_offset;
81407a69 16161 }
65f38f15 16162 }
5bd4f169
AM
16163 break;
16164
65f38f15
AM
16165 case R_PPC64_COPY:
16166 case R_PPC64_GLOB_DAT:
16167 case R_PPC64_JMP_SLOT:
25f23106 16168 case R_PPC64_JMP_IREL:
65f38f15
AM
16169 case R_PPC64_RELATIVE:
16170 /* We shouldn't ever see these dynamic relocs in relocatable
16171 files. */
ae9a127f 16172 /* Fall through. */
65f38f15
AM
16173
16174 case R_PPC64_PLTGOT16:
16175 case R_PPC64_PLTGOT16_DS:
16176 case R_PPC64_PLTGOT16_HA:
16177 case R_PPC64_PLTGOT16_HI:
16178 case R_PPC64_PLTGOT16_LO:
16179 case R_PPC64_PLTGOT16_LO_DS:
16180 case R_PPC64_PLTREL32:
16181 case R_PPC64_PLTREL64:
16182 /* These ones haven't been implemented yet. */
16183
25f53a85 16184 info->callbacks->einfo
695344c0 16185 /* xgettext:c-format */
c1c8c1ef 16186 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 16187 input_bfd,
4ce794b7 16188 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
16189
16190 bfd_set_error (bfd_error_invalid_operation);
b34976b6 16191 ret = FALSE;
c316a17c 16192 goto copy_reloc;
65f38f15 16193 }
5bd4f169 16194
67f0cbdb
AM
16195 /* Multi-instruction sequences that access the TOC can be
16196 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 16197 to nop; addi rb,r2,x; */
67f0cbdb
AM
16198 switch (r_type)
16199 {
16200 default:
16201 break;
16202
16203 case R_PPC64_GOT_TLSLD16_HI:
16204 case R_PPC64_GOT_TLSGD16_HI:
16205 case R_PPC64_GOT_TPREL16_HI:
16206 case R_PPC64_GOT_DTPREL16_HI:
16207 case R_PPC64_GOT16_HI:
16208 case R_PPC64_TOC16_HI:
16209 /* These relocs would only be useful if building up an
16210 offset to later add to r2, perhaps in an indexed
16211 addressing mode instruction. Don't try to optimize.
16212 Unfortunately, the possibility of someone building up an
16213 offset like this or even with the HA relocs, means that
16214 we need to check the high insn when optimizing the low
16215 insn. */
16216 break;
16217
5663e321
AM
16218 case R_PPC64_PLTCALL_NOTOC:
16219 if (!unresolved_reloc)
16220 htab->notoc_plt = 1;
16221 /* Fall through. */
23cedd1d
AM
16222 case R_PPC64_PLTCALL:
16223 if (unresolved_reloc)
16224 {
16225 /* No plt entry. Make this into a direct call. */
16226 bfd_byte *p = contents + rel->r_offset;
16227 insn = bfd_get_32 (input_bfd, p);
16228 insn &= 1;
16229 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
16230 if (r_type == R_PPC64_PLTCALL)
16231 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
16232 unresolved_reloc = save_unresolved_reloc;
16233 r_type = R_PPC64_REL24;
16234 }
16235 break;
16236
5663e321 16237 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
16238 case R_PPC64_PLTSEQ:
16239 if (unresolved_reloc)
16240 {
16241 unresolved_reloc = FALSE;
16242 goto nop_it;
16243 }
16244 break;
16245
5663e321
AM
16246 case R_PPC64_PLT_PCREL34_NOTOC:
16247 if (!unresolved_reloc)
16248 htab->notoc_plt = 1;
16249 /* Fall through. */
16250 case R_PPC64_PLT_PCREL34:
16251 if (unresolved_reloc)
16252 {
16253 bfd_byte *p = contents + rel->r_offset;
16254 bfd_put_32 (input_bfd, PNOP >> 32, p);
16255 bfd_put_32 (input_bfd, PNOP, p + 4);
16256 unresolved_reloc = FALSE;
16257 goto copy_reloc;
16258 }
16259 break;
16260
23cedd1d
AM
16261 case R_PPC64_PLT16_HA:
16262 if (unresolved_reloc)
16263 {
16264 unresolved_reloc = FALSE;
16265 goto nop_it;
16266 }
16267 /* Fall through. */
67f0cbdb
AM
16268 case R_PPC64_GOT_TLSLD16_HA:
16269 case R_PPC64_GOT_TLSGD16_HA:
16270 case R_PPC64_GOT_TPREL16_HA:
16271 case R_PPC64_GOT_DTPREL16_HA:
16272 case R_PPC64_GOT16_HA:
16273 case R_PPC64_TOC16_HA:
98528052 16274 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16275 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 16276 {
23cedd1d
AM
16277 bfd_byte *p;
16278 nop_it:
16279 p = contents + (rel->r_offset & ~3);
98528052 16280 bfd_put_32 (input_bfd, NOP, p);
d830549d 16281 goto copy_reloc;
98528052 16282 }
67f0cbdb
AM
16283 break;
16284
23cedd1d
AM
16285 case R_PPC64_PLT16_LO:
16286 case R_PPC64_PLT16_LO_DS:
16287 if (unresolved_reloc)
16288 {
16289 unresolved_reloc = FALSE;
16290 goto nop_it;
16291 }
16292 /* Fall through. */
67f0cbdb
AM
16293 case R_PPC64_GOT_TLSLD16_LO:
16294 case R_PPC64_GOT_TLSGD16_LO:
16295 case R_PPC64_GOT_TPREL16_LO_DS:
16296 case R_PPC64_GOT_DTPREL16_LO_DS:
16297 case R_PPC64_GOT16_LO:
16298 case R_PPC64_GOT16_LO_DS:
16299 case R_PPC64_TOC16_LO:
16300 case R_PPC64_TOC16_LO_DS:
98528052 16301 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16302 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
16303 {
16304 bfd_byte *p = contents + (rel->r_offset & ~3);
16305 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
16306 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16307 {
16308 /* Transform addic to addi when we change reg. */
16309 insn &= ~((0x3f << 26) | (0x1f << 16));
16310 insn |= (14u << 26) | (2 << 16);
16311 }
16312 else
67f0cbdb 16313 {
98528052
AM
16314 insn &= ~(0x1f << 16);
16315 insn |= 2 << 16;
67f0cbdb 16316 }
560c8763 16317 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
16318 }
16319 break;
9a23f96e
AM
16320
16321 case R_PPC64_TPREL16_HA:
16322 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16323 {
16324 bfd_byte *p = contents + (rel->r_offset & ~3);
16325 insn = bfd_get_32 (input_bfd, p);
16326 if ((insn & ((0x3f << 26) | 0x1f << 16))
16327 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16328 /* xgettext:c-format */
16329 info->callbacks->minfo
16330 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
16331 input_bfd, input_section, rel->r_offset,
16332 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 16333 else
d830549d
AM
16334 {
16335 bfd_put_32 (input_bfd, NOP, p);
16336 goto copy_reloc;
16337 }
9a23f96e
AM
16338 }
16339 break;
16340
16341 case R_PPC64_TPREL16_LO:
16342 case R_PPC64_TPREL16_LO_DS:
16343 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16344 {
16345 bfd_byte *p = contents + (rel->r_offset & ~3);
16346 insn = bfd_get_32 (input_bfd, p);
16347 insn &= ~(0x1f << 16);
16348 insn |= 13 << 16;
16349 bfd_put_32 (input_bfd, insn, p);
16350 }
16351 break;
67f0cbdb
AM
16352 }
16353
65f38f15
AM
16354 /* Do any further special processing. */
16355 switch (r_type)
16356 {
16357 default:
16358 break;
16359
25f23106 16360 case R_PPC64_REL16_HA:
4a969973
AM
16361 case R_PPC64_REL16_HIGHA:
16362 case R_PPC64_REL16_HIGHERA:
16363 case R_PPC64_REL16_HIGHESTA:
a680de9a 16364 case R_PPC64_REL16DX_HA:
f9c6b907
AM
16365 case R_PPC64_ADDR16_HA:
16366 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
16367 case R_PPC64_ADDR16_HIGHERA:
16368 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
16369 case R_PPC64_TOC16_HA:
16370 case R_PPC64_SECTOFF_HA:
411e1bfb 16371 case R_PPC64_TPREL16_HA:
f9c6b907 16372 case R_PPC64_TPREL16_HIGHA:
411e1bfb 16373 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 16374 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
16375 case R_PPC64_DTPREL16_HA:
16376 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 16377 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 16378 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
16379 /* It's just possible that this symbol is a weak symbol
16380 that's not actually defined anywhere. In that case,
16381 'sec' would be NULL, and we should leave the symbol
16382 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
16383 if (sec == NULL)
16384 break;
1a0670f3 16385 /* Fall through. */
5c5f6e17
AM
16386
16387 case R_PPC64_GOT16_HA:
16388 case R_PPC64_PLTGOT16_HA:
16389 case R_PPC64_PLT16_HA:
16390 case R_PPC64_GOT_TLSGD16_HA:
16391 case R_PPC64_GOT_TLSLD16_HA:
16392 case R_PPC64_GOT_TPREL16_HA:
16393 case R_PPC64_GOT_DTPREL16_HA:
16394 /* Add 0x10000 if sign bit in 0:15 is set.
16395 Bits 0:15 are not used. */
16396 addend += 0x8000;
65f38f15
AM
16397 break;
16398
5663e321
AM
16399 case R_PPC64_D34_HA30:
16400 case R_PPC64_ADDR16_HIGHERA34:
16401 case R_PPC64_ADDR16_HIGHESTA34:
16402 case R_PPC64_REL16_HIGHERA34:
16403 case R_PPC64_REL16_HIGHESTA34:
16404 if (sec != NULL)
16405 addend += 1ULL << 33;
16406 break;
16407
65f38f15
AM
16408 case R_PPC64_ADDR16_DS:
16409 case R_PPC64_ADDR16_LO_DS:
16410 case R_PPC64_GOT16_DS:
16411 case R_PPC64_GOT16_LO_DS:
16412 case R_PPC64_PLT16_LO_DS:
16413 case R_PPC64_SECTOFF_DS:
16414 case R_PPC64_SECTOFF_LO_DS:
16415 case R_PPC64_TOC16_DS:
16416 case R_PPC64_TOC16_LO_DS:
16417 case R_PPC64_PLTGOT16_DS:
16418 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
16419 case R_PPC64_GOT_TPREL16_DS:
16420 case R_PPC64_GOT_TPREL16_LO_DS:
16421 case R_PPC64_GOT_DTPREL16_DS:
16422 case R_PPC64_GOT_DTPREL16_LO_DS:
16423 case R_PPC64_TPREL16_DS:
16424 case R_PPC64_TPREL16_LO_DS:
16425 case R_PPC64_DTPREL16_DS:
16426 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
16427 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16428 mask = 3;
a680de9a
PB
16429 /* If this reloc is against an lq, lxv, or stxv insn, then
16430 the value must be a multiple of 16. This is somewhat of
16431 a hack, but the "correct" way to do this by defining _DQ
16432 forms of all the _DS relocs bloats all reloc switches in
16433 this file. It doesn't make much sense to use these
16434 relocs in data, so testing the insn should be safe. */
16435 if ((insn & (0x3f << 26)) == (56u << 26)
16436 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 16437 mask = 15;
a680de9a
PB
16438 relocation += addend;
16439 addend = insn & (mask ^ 3);
16440 if ((relocation & mask) != 0)
65f38f15 16441 {
a680de9a 16442 relocation ^= relocation & mask;
25f53a85 16443 info->callbacks->einfo
695344c0 16444 /* xgettext:c-format */
174d0a74 16445 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 16446 input_bfd, input_section, rel->r_offset,
d830549d 16447 ppc64_elf_howto_table[r_type]->name,
adadcc0c 16448 mask + 1);
65f38f15 16449 bfd_set_error (bfd_error_bad_value);
b34976b6 16450 ret = FALSE;
c316a17c 16451 goto copy_reloc;
65f38f15
AM
16452 }
16453 break;
5bd4f169
AM
16454 }
16455
239e1f3a
AM
16456 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16457 because such sections are not SEC_ALLOC and thus ld.so will
16458 not process them. */
d830549d 16459 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 16460 if (unresolved_reloc
239e1f3a 16461 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
16462 && h->elf.def_dynamic)
16463 && _bfd_elf_section_offset (output_bfd, info, input_section,
16464 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 16465 {
25f53a85 16466 info->callbacks->einfo
695344c0 16467 /* xgettext:c-format */
c1c8c1ef 16468 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 16469 input_bfd, input_section, rel->r_offset,
b80eed39 16470 howto->name,
039b3fef 16471 h->elf.root.root.string);
b34976b6 16472 ret = FALSE;
9c07fe7c 16473 }
5bd4f169 16474
b80eed39
AM
16475 /* 16-bit fields in insns mostly have signed values, but a
16476 few insns have 16-bit unsigned values. Really, we should
16477 have different reloc types. */
16478 if (howto->complain_on_overflow != complain_overflow_dont
16479 && howto->dst_mask == 0xffff
16480 && (input_section->flags & SEC_CODE) != 0)
16481 {
16482 enum complain_overflow complain = complain_overflow_signed;
16483
16484 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
16485 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16486 complain = complain_overflow_bitfield;
16487 else if (howto->rightshift == 0
16488 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16489 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16490 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16491 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16492 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16493 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
16494 complain = complain_overflow_unsigned;
16495 if (howto->complain_on_overflow != complain)
16496 {
16497 alt_howto = *howto;
16498 alt_howto.complain_on_overflow = complain;
16499 howto = &alt_howto;
16500 }
16501 }
16502
5663e321 16503 switch (r_type)
a680de9a 16504 {
5663e321
AM
16505 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
16506 case R_PPC64_D34:
16507 case R_PPC64_D34_LO:
16508 case R_PPC64_D34_HI30:
16509 case R_PPC64_D34_HA30:
16510 case R_PPC64_PCREL34:
16511 case R_PPC64_GOT_PCREL34:
c213164a
AM
16512 case R_PPC64_TPREL34:
16513 case R_PPC64_DTPREL34:
16514 case R_PPC64_GOT_TLSGD34:
16515 case R_PPC64_GOT_TLSLD34:
16516 case R_PPC64_GOT_TPREL34:
16517 case R_PPC64_GOT_DTPREL34:
5663e321
AM
16518 case R_PPC64_PLT_PCREL34:
16519 case R_PPC64_PLT_PCREL34_NOTOC:
16520 case R_PPC64_D28:
16521 case R_PPC64_PCREL28:
16522 if (rel->r_offset + 8 > input_section->size)
16523 r = bfd_reloc_outofrange;
16524 else
16525 {
5663e321
AM
16526 relocation += addend;
16527 if (howto->pc_relative)
16528 relocation -= (rel->r_offset
16529 + input_section->output_offset
16530 + input_section->output_section->vma);
16531 relocation >>= howto->rightshift;
16532
16533 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16534 pinsn <<= 32;
16535 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16536
16537 pinsn &= ~howto->dst_mask;
16538 pinsn |= (((relocation << 16) | (relocation & 0xffff))
16539 & howto->dst_mask);
16540 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
16541 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
16542 r = bfd_reloc_ok;
16543 if (howto->complain_on_overflow == complain_overflow_signed
16544 && (relocation + (1ULL << (howto->bitsize - 1))
16545 >= 1ULL << howto->bitsize))
16546 r = bfd_reloc_overflow;
16547 }
16548 break;
16549
16550 case R_PPC64_REL16DX_HA:
a680de9a
PB
16551 if (rel->r_offset + 4 > input_section->size)
16552 r = bfd_reloc_outofrange;
16553 else
16554 {
16555 relocation += addend;
16556 relocation -= (rel->r_offset
16557 + input_section->output_offset
16558 + input_section->output_section->vma);
3de43e7b 16559 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
16560 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16561 insn &= ~0x1fffc1;
3de43e7b 16562 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
16563 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16564 r = bfd_reloc_ok;
3de43e7b 16565 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
16566 r = bfd_reloc_overflow;
16567 }
5663e321
AM
16568 break;
16569
16570 default:
16571 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
16572 contents, rel->r_offset,
16573 relocation, addend);
a680de9a 16574 }
5bd4f169 16575
ef60b7ff 16576 if (r != bfd_reloc_ok)
5bd4f169 16577 {
bc30df16 16578 char *more_info = NULL;
b80eed39 16579 const char *reloc_name = howto->name;
bc30df16
AM
16580
16581 if (reloc_dest != DEST_NORMAL)
16582 {
16583 more_info = bfd_malloc (strlen (reloc_name) + 8);
16584 if (more_info != NULL)
16585 {
16586 strcpy (more_info, reloc_name);
16587 strcat (more_info, (reloc_dest == DEST_OPD
16588 ? " (OPD)" : " (stub)"));
16589 reloc_name = more_info;
16590 }
16591 }
16592
cd27b276 16593 if (r == bfd_reloc_overflow)
5bd4f169 16594 {
8131c122
AM
16595 /* On code like "if (foo) foo();" don't report overflow
16596 on a branch to zero when foo is undefined. */
16597 if (!warned
16598 && (reloc_dest == DEST_STUB
16599 || !(h != NULL
16600 && (h->elf.root.type == bfd_link_hash_undefweak
16601 || h->elf.root.type == bfd_link_hash_undefined)
16602 && is_branch_reloc (r_type))))
1a72702b
AM
16603 info->callbacks->reloc_overflow (info, &h->elf.root,
16604 sym_name, reloc_name,
16605 orig_rel.r_addend,
16606 input_bfd, input_section,
16607 rel->r_offset);
ef60b7ff
AM
16608 }
16609 else
16610 {
25f53a85 16611 info->callbacks->einfo
695344c0 16612 /* xgettext:c-format */
c1c8c1ef 16613 (_("%H: %s against `%pT': error %d\n"),
25f53a85 16614 input_bfd, input_section, rel->r_offset,
bc30df16 16615 reloc_name, sym_name, (int) r);
b34976b6 16616 ret = FALSE;
ef60b7ff 16617 }
bc30df16
AM
16618 if (more_info != NULL)
16619 free (more_info);
5bd4f169 16620 }
c316a17c
AM
16621 copy_reloc:
16622 if (wrel != rel)
16623 *wrel = *rel;
16624 }
16625
16626 if (wrel != rel)
16627 {
16628 Elf_Internal_Shdr *rel_hdr;
16629 size_t deleted = rel - wrel;
16630
16631 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16632 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16633 if (rel_hdr->sh_size == 0)
16634 {
16635 /* It is too late to remove an empty reloc section. Leave
16636 one NONE reloc.
16637 ??? What is wrong with an empty section??? */
16638 rel_hdr->sh_size = rel_hdr->sh_entsize;
16639 deleted -= 1;
16640 }
16641 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16642 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16643 input_section->reloc_count -= deleted;
5bd4f169
AM
16644 }
16645
645ea6a9
AM
16646 /* If we're emitting relocations, then shortly after this function
16647 returns, reloc offsets and addends for this section will be
16648 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
16649 file rather than the input. Save a copy of the relocs for
16650 opd_entry_value. */
0e1862bb 16651 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
16652 {
16653 bfd_size_type amt;
16654 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16655 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
16656 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16657 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
16658 if (rel == NULL)
16659 return FALSE;
16660 memcpy (rel, relocs, amt);
16661 }
5bd4f169
AM
16662 return ret;
16663}
16664
754021d0
AM
16665/* Adjust the value of any local symbols in opd sections. */
16666
6e0b88f1 16667static int
754021d0
AM
16668ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16669 const char *name ATTRIBUTE_UNUSED,
16670 Elf_Internal_Sym *elfsym,
16671 asection *input_sec,
16672 struct elf_link_hash_entry *h)
16673{
74f0fb50
AM
16674 struct _opd_sec_data *opd;
16675 long adjust;
754021d0
AM
16676 bfd_vma value;
16677
4025353c 16678 if (h != NULL)
6e0b88f1 16679 return 1;
4025353c 16680
74f0fb50
AM
16681 opd = get_opd_info (input_sec);
16682 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 16683 return 1;
754021d0
AM
16684
16685 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 16686 if (!bfd_link_relocatable (info))
754021d0
AM
16687 value -= input_sec->output_section->vma;
16688
51aecdc5 16689 adjust = opd->adjust[OPD_NDX (value)];
4025353c 16690 if (adjust == -1)
6e0b88f1
AM
16691 return 2;
16692
16693 elfsym->st_value += adjust;
16694 return 1;
754021d0
AM
16695}
16696
5bd4f169
AM
16697/* Finish up dynamic symbol handling. We set the contents of various
16698 dynamic sections here. */
16699
b34976b6 16700static bfd_boolean
4ce794b7
AM
16701ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16702 struct bfd_link_info *info,
16703 struct elf_link_hash_entry *h,
4aef7643 16704 Elf_Internal_Sym *sym)
5bd4f169 16705{
65f38f15 16706 struct ppc_link_hash_table *htab;
8387904d 16707 struct plt_entry *ent;
5bd4f169 16708
65f38f15 16709 htab = ppc_hash_table (info);
4dfe6ac6
NC
16710 if (htab == NULL)
16711 return FALSE;
5bd4f169 16712
49c09209
AM
16713 if (!htab->opd_abi && !h->def_regular)
16714 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16715 if (ent->plt.offset != (bfd_vma) -1)
16716 {
16717 /* Mark the symbol as undefined, rather than as
16718 defined in glink. Leave the value if there were
16719 any relocations where pointer equality matters
16720 (this is a clue for the dynamic linker, to make
16721 function pointer comparisons work between an
16722 application and shared library), otherwise set it
16723 to zero. */
16724 sym->st_shndx = SHN_UNDEF;
16725 if (!h->pointer_equality_needed)
16726 sym->st_value = 0;
16727 else if (!h->ref_regular_nonweak)
16728 {
16729 /* This breaks function pointer comparisons, but
16730 that is better than breaking tests for a NULL
16731 function pointer. */
16732 sym->st_value = 0;
16733 }
16734 break;
16735 }
5bd4f169 16736
f5385ebf 16737 if (h->needs_copy)
5bd4f169 16738 {
65f38f15 16739 /* This symbol needs a copy reloc. Set it up. */
49c09209 16740 Elf_Internal_Rela rela;
5474d94f 16741 asection *srel;
49c09209 16742 bfd_byte *loc;
5bd4f169 16743
65f38f15
AM
16744 if (h->dynindx == -1
16745 || (h->root.type != bfd_link_hash_defined
16746 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
16747 || htab->elf.srelbss == NULL
16748 || htab->elf.sreldynrelro == NULL)
65f38f15 16749 abort ();
5bd4f169
AM
16750
16751 rela.r_offset = (h->root.u.def.value
16752 + h->root.u.def.section->output_section->vma
16753 + h->root.u.def.section->output_offset);
16754 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16755 rela.r_addend = 0;
afbf7e8e 16756 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
16757 srel = htab->elf.sreldynrelro;
16758 else
16759 srel = htab->elf.srelbss;
16760 loc = srel->contents;
16761 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 16762 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
16763 }
16764
b34976b6 16765 return TRUE;
5bd4f169
AM
16766}
16767
65f38f15
AM
16768/* Used to decide how to sort relocs in an optimal manner for the
16769 dynamic linker, before writing them out. */
16770
16771static enum elf_reloc_type_class
7e612e98
AM
16772ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16773 const asection *rel_sec,
16774 const Elf_Internal_Rela *rela)
65f38f15 16775{
04c9666a 16776 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
16777 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16778
33e44f2e 16779 if (rel_sec == htab->elf.irelplt)
7e612e98 16780 return reloc_class_ifunc;
a33d1f77 16781
4ce794b7 16782 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 16783 switch (r_type)
65f38f15
AM
16784 {
16785 case R_PPC64_RELATIVE:
16786 return reloc_class_relative;
16787 case R_PPC64_JMP_SLOT:
16788 return reloc_class_plt;
16789 case R_PPC64_COPY:
16790 return reloc_class_copy;
16791 default:
16792 return reloc_class_normal;
16793 }
16794}
16795
5bd4f169
AM
16796/* Finish up the dynamic sections. */
16797
b34976b6 16798static bfd_boolean
4ce794b7
AM
16799ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16800 struct bfd_link_info *info)
5bd4f169 16801{
65f38f15
AM
16802 struct ppc_link_hash_table *htab;
16803 bfd *dynobj;
5bd4f169 16804 asection *sdyn;
5bd4f169 16805
65f38f15 16806 htab = ppc_hash_table (info);
4dfe6ac6
NC
16807 if (htab == NULL)
16808 return FALSE;
16809
65f38f15 16810 dynobj = htab->elf.dynobj;
3d4d4302 16811 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 16812
65f38f15 16813 if (htab->elf.dynamic_sections_created)
5bd4f169 16814 {
5bd4f169
AM
16815 Elf64_External_Dyn *dyncon, *dynconend;
16816
33e44f2e 16817 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 16818 abort ();
5bd4f169
AM
16819
16820 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 16821 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
16822 for (; dyncon < dynconend; dyncon++)
16823 {
16824 Elf_Internal_Dyn dyn;
19397422 16825 asection *s;
5bd4f169
AM
16826
16827 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16828
16829 switch (dyn.d_tag)
16830 {
65f38f15
AM
16831 default:
16832 continue;
5bd4f169 16833
5d1634d7 16834 case DT_PPC64_GLINK:
4ce794b7 16835 s = htab->glink;
6348e046 16836 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
16837 /* We stupidly defined DT_PPC64_GLINK to be the start
16838 of glink rather than the first entry point, which is
16839 what ld.so needs, and now have a bigger stub to
16840 support automatic multiple TOCs. */
9e390558 16841 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
16842 break;
16843
19397422
AM
16844 case DT_PPC64_OPD:
16845 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16846 if (s == NULL)
16847 continue;
16848 dyn.d_un.d_ptr = s->vma;
19397422
AM
16849 break;
16850
e8910a83 16851 case DT_PPC64_OPT:
5663e321
AM
16852 if ((htab->do_multi_toc && htab->multi_toc_needed)
16853 || htab->notoc_plt)
e8910a83 16854 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
16855 if (htab->has_plt_localentry0)
16856 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
16857 break;
16858
19397422
AM
16859 case DT_PPC64_OPDSZ:
16860 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16861 if (s == NULL)
16862 continue;
eea6121a 16863 dyn.d_un.d_val = s->size;
19397422
AM
16864 break;
16865
65f38f15 16866 case DT_PLTGOT:
33e44f2e 16867 s = htab->elf.splt;
6348e046 16868 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
16869 break;
16870
16871 case DT_JMPREL:
33e44f2e 16872 s = htab->elf.srelplt;
6348e046 16873 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 16874 break;
5bd4f169 16875
65f38f15 16876 case DT_PLTRELSZ:
33e44f2e 16877 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 16878 break;
82e66161
AM
16879
16880 case DT_TEXTREL:
16881 if (htab->local_ifunc_resolver)
16882 info->callbacks->einfo
16883 (_("%X%P: text relocations and GNU indirect "
16884 "functions will result in a segfault at runtime\n"));
16885 else if (htab->maybe_local_ifunc_resolver)
16886 info->callbacks->einfo
16887 (_("%P: warning: text relocations and GNU indirect "
16888 "functions may result in a segfault at runtime\n"));
16889 continue;
5bd4f169 16890 }
5bd4f169 16891
65f38f15 16892 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 16893 }
5bd4f169
AM
16894 }
16895
6528b6eb
AM
16896 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16897 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16898 {
16899 /* Fill in the first entry in the global offset table.
16900 We use it to hold the link-time TOCbase. */
16901 bfd_put_64 (output_bfd,
60ee0d4a 16902 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 16903 htab->elf.sgot->contents);
5d1634d7
AM
16904
16905 /* Set .got entry size. */
2cdcc330
AM
16906 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16907 = 8;
5d1634d7
AM
16908 }
16909
6528b6eb
AM
16910 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16911 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16912 {
16913 /* Set .plt entry size. */
33e44f2e 16914 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 16915 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
16916 }
16917
84f5d08e
AM
16918 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16919 brlt ourselves if emitrelocations. */
16920 if (htab->brlt != NULL
16921 && htab->brlt->reloc_count != 0
16922 && !_bfd_elf_link_output_relocs (output_bfd,
16923 htab->brlt,
d4730f92 16924 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
16925 elf_section_data (htab->brlt)->relocs,
16926 NULL))
16927 return FALSE;
16928
176a0d42
AM
16929 if (htab->glink != NULL
16930 && htab->glink->reloc_count != 0
16931 && !_bfd_elf_link_output_relocs (output_bfd,
16932 htab->glink,
d4730f92 16933 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
16934 elf_section_data (htab->glink)->relocs,
16935 NULL))
16936 return FALSE;
16937
da44f4e5 16938
df136d64
AM
16939 if (htab->glink_eh_frame != NULL
16940 && htab->glink_eh_frame->size != 0
16941 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16942 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16943 htab->glink_eh_frame,
16944 htab->glink_eh_frame->contents))
16945 return FALSE;
58d180e8 16946
e717da7e 16947 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
16948 since we didn't add them to DYNOBJ. We know dynobj is the first
16949 bfd. */
c72f2fb2 16950 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
16951 {
16952 asection *s;
7b53ace3 16953
0c8d6e5c 16954 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
16955 continue;
16956
e717da7e
AM
16957 s = ppc64_elf_tdata (dynobj)->got;
16958 if (s != NULL
eea6121a 16959 && s->size != 0
e717da7e
AM
16960 && s->output_section != bfd_abs_section_ptr
16961 && !bfd_set_section_contents (output_bfd, s->output_section,
16962 s->contents, s->output_offset,
eea6121a 16963 s->size))
e717da7e
AM
16964 return FALSE;
16965 s = ppc64_elf_tdata (dynobj)->relgot;
16966 if (s != NULL
eea6121a 16967 && s->size != 0
e717da7e
AM
16968 && s->output_section != bfd_abs_section_ptr
16969 && !bfd_set_section_contents (output_bfd, s->output_section,
16970 s->contents, s->output_offset,
eea6121a 16971 s->size))
e717da7e
AM
16972 return FALSE;
16973 }
f6c52c13 16974
b34976b6 16975 return TRUE;
5bd4f169
AM
16976}
16977
5bd4f169 16978#include "elf64-target.h"
7b8e7dad
AM
16979
16980/* FreeBSD support */
16981
16982#undef TARGET_LITTLE_SYM
16983#undef TARGET_LITTLE_NAME
16984
16985#undef TARGET_BIG_SYM
6d00b590 16986#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
16987#undef TARGET_BIG_NAME
16988#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16989
16990#undef ELF_OSABI
16991#define ELF_OSABI ELFOSABI_FREEBSD
16992
16993#undef elf64_bed
16994#define elf64_bed elf64_powerpc_fbsd_bed
16995
16996#include "elf64-target.h"
This page took 3.473994 seconds and 4 git commands to generate.