Improve the formatting of the title strings of the binutils manual pages.
[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
AM
1782
1783 /* Set if got relocs that can be optimised are present in this file. */
1784 unsigned int has_gotrel : 1;
e717da7e
AM
1785};
1786
1787#define ppc64_elf_tdata(bfd) \
1788 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1789
1790#define ppc64_tlsld_got(bfd) \
1791 (&ppc64_elf_tdata (bfd)->tlsld_got)
1792
0c8d6e5c
AM
1793#define is_ppc64_elf(bfd) \
1794 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1795 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1796
e717da7e
AM
1797/* Override the generic function because we store some extras. */
1798
1799static bfd_boolean
1800ppc64_elf_mkobject (bfd *abfd)
1801{
0ffa91dd 1802 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1803 PPC64_ELF_DATA);
e717da7e
AM
1804}
1805
feee612b 1806/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1807 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1808
b34976b6 1809static bfd_boolean
4ce794b7 1810ppc64_elf_object_p (bfd *abfd)
feee612b 1811{
14b57c7c
AM
1812 if (!abfd->arch_info->the_default)
1813 return TRUE;
1814
1815 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1816 {
1817 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1818
1819 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1820 {
1821 /* Relies on arch after 32 bit default being 64 bit default. */
1822 abfd->arch_info = abfd->arch_info->next;
1823 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1824 }
1825 }
14b57c7c 1826 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1827}
1828
d37c89e5
AM
1829/* Support for core dump NOTE sections. */
1830
1831static bfd_boolean
1832ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1833{
eea6121a 1834 size_t offset, size;
d37c89e5
AM
1835
1836 if (note->descsz != 504)
1837 return FALSE;
1838
1839 /* pr_cursig */
228e534f 1840 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1841
1842 /* pr_pid */
228e534f 1843 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1844
1845 /* pr_reg */
1846 offset = 112;
eea6121a 1847 size = 384;
d37c89e5
AM
1848
1849 /* Make a ".reg/999" section. */
1850 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1851 size, note->descpos + offset);
d37c89e5
AM
1852}
1853
1854static bfd_boolean
1855ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1856{
1857 if (note->descsz != 136)
1858 return FALSE;
1859
228e534f 1860 elf_tdata (abfd)->core->pid
bc989cdc 1861 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1862 elf_tdata (abfd)->core->program
d37c89e5 1863 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1864 elf_tdata (abfd)->core->command
d37c89e5
AM
1865 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1866
1867 return TRUE;
1868}
1869
183e98be
AM
1870static char *
1871ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1872 ...)
1873{
1874 switch (note_type)
1875 {
1876 default:
1877 return NULL;
1878
1879 case NT_PRPSINFO:
1880 {
9ef6d1e3 1881 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1882 va_list ap;
1883
1884 va_start (ap, note_type);
75cd47ed 1885 memset (data, 0, sizeof (data));
183e98be 1886 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1887#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1888 DIAGNOSTIC_PUSH;
be3e27bb 1889 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1890 -Wstringop-truncation:
1891 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1892 */
95da9854
L
1893 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1894#endif
183e98be 1895 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1896#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1897 DIAGNOSTIC_POP;
fe75810f 1898#endif
183e98be
AM
1899 va_end (ap);
1900 return elfcore_write_note (abfd, buf, bufsiz,
1901 "CORE", note_type, data, sizeof (data));
1902 }
1903
1904 case NT_PRSTATUS:
1905 {
1906 char data[504];
1907 va_list ap;
1908 long pid;
1909 int cursig;
1910 const void *greg;
1911
1912 va_start (ap, note_type);
1913 memset (data, 0, 112);
1914 pid = va_arg (ap, long);
1915 bfd_put_32 (abfd, pid, data + 32);
1916 cursig = va_arg (ap, int);
1917 bfd_put_16 (abfd, cursig, data + 12);
1918 greg = va_arg (ap, const void *);
1919 memcpy (data + 112, greg, 384);
1920 memset (data + 496, 0, 8);
1921 va_end (ap);
1922 return elfcore_write_note (abfd, buf, bufsiz,
1923 "CORE", note_type, data, sizeof (data));
1924 }
1925 }
1926}
1927
5d35169e
AM
1928/* Add extra PPC sections. */
1929
2cdcc330 1930static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1931{
07d6d2b8
AM
1932 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1933 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1934 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1935 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1936 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1937 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1938 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1939};
1940
7c8fe5c4
AM
1941enum _ppc64_sec_type {
1942 sec_normal = 0,
1943 sec_opd = 1,
1944 sec_toc = 2
1945};
1946
f0abc2a1
AM
1947struct _ppc64_elf_section_data
1948{
1949 struct bfd_elf_section_data elf;
411e1bfb 1950
f0abc2a1
AM
1951 union
1952 {
51aecdc5
AM
1953 /* An array with one entry for each opd function descriptor,
1954 and some spares since opd entries may be either 16 or 24 bytes. */
1955#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
1956 struct _opd_sec_data
1957 {
1958 /* Points to the function code section for local opd entries. */
1959 asection **func_sec;
1960
1961 /* After editing .opd, adjust references to opd local syms. */
1962 long *adjust;
1963 } opd;
7c8fe5c4 1964
3a71aa26
AM
1965 /* An array for toc sections, indexed by offset/8. */
1966 struct _toc_sec_data
1967 {
1968 /* Specifies the relocation symbol index used at a given toc offset. */
1969 unsigned *symndx;
1970
1971 /* And the relocation addend. */
1972 bfd_vma *add;
1973 } toc;
7c8fe5c4
AM
1974 } u;
1975
1976 enum _ppc64_sec_type sec_type:2;
411e1bfb 1977
7c8fe5c4
AM
1978 /* Flag set when small branches are detected. Used to
1979 select suitable defaults for the stub group size. */
1980 unsigned int has_14bit_branch:1;
3e04d765
AM
1981
1982 /* Flag set when PLTCALL relocs are detected. */
1983 unsigned int has_pltcall:1;
066f4018
AM
1984
1985 /* Flag set when section has GOT relocations that can be optimised. */
1986 unsigned int has_gotrel:1;
f0abc2a1
AM
1987};
1988
1989#define ppc64_elf_section_data(sec) \
411e1bfb 1990 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
1991
1992static bfd_boolean
4ce794b7 1993ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1994{
f592407e
AM
1995 if (!sec->used_by_bfd)
1996 {
1997 struct _ppc64_elf_section_data *sdata;
1998 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1999
f592407e
AM
2000 sdata = bfd_zalloc (abfd, amt);
2001 if (sdata == NULL)
2002 return FALSE;
2003 sec->used_by_bfd = sdata;
2004 }
f0abc2a1
AM
2005
2006 return _bfd_elf_new_section_hook (abfd, sec);
2007}
4025353c 2008
74f0fb50 2009static struct _opd_sec_data *
4025353c
AM
2010get_opd_info (asection * sec)
2011{
2012 if (sec != NULL
2013 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2014 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2015 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2016 return NULL;
2017}
90e3cdf2
JJ
2018\f
2019/* Parameters for the qsort hook. */
90e3cdf2 2020static bfd_boolean synthetic_relocatable;
cd285db5 2021static asection *synthetic_opd;
90e3cdf2 2022
699733f6 2023/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2024
2025static int
2026compare_symbols (const void *ap, const void *bp)
2027{
2cdcc330
AM
2028 const asymbol *a = *(const asymbol **) ap;
2029 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 2030
699733f6
AM
2031 /* Section symbols first. */
2032 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2033 return -1;
699733f6 2034 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2035 return 1;
2036
699733f6 2037 /* then .opd symbols. */
cd285db5
AM
2038 if (synthetic_opd != NULL)
2039 {
2040 if (strcmp (a->section->name, ".opd") == 0
2041 && strcmp (b->section->name, ".opd") != 0)
2042 return -1;
2043 if (strcmp (a->section->name, ".opd") != 0
2044 && strcmp (b->section->name, ".opd") == 0)
2045 return 1;
2046 }
90e3cdf2 2047
699733f6 2048 /* then other code symbols. */
2cdcc330
AM
2049 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2050 == (SEC_CODE | SEC_ALLOC))
2051 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2052 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2053 return -1;
2054
2cdcc330
AM
2055 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2056 != (SEC_CODE | SEC_ALLOC))
2057 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2058 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2059 return 1;
2060
2061 if (synthetic_relocatable)
2062 {
2063 if (a->section->id < b->section->id)
2064 return -1;
2065
2066 if (a->section->id > b->section->id)
2067 return 1;
2068 }
2069
2070 if (a->value + a->section->vma < b->value + b->section->vma)
2071 return -1;
2072
2073 if (a->value + a->section->vma > b->value + b->section->vma)
2074 return 1;
2075
4d35a0aa
AM
2076 /* For syms with the same value, prefer strong dynamic global function
2077 syms over other syms. */
2078 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2079 return -1;
2080
2081 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2082 return 1;
2083
2084 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2085 return -1;
2086
2087 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2088 return 1;
2089
2090 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2091 return -1;
2092
2093 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2094 return 1;
2095
2096 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2097 return -1;
2098
2099 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2100 return 1;
2101
aaed6f5b 2102 return a > b;
90e3cdf2
JJ
2103}
2104
699733f6 2105/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2106
699733f6 2107static asymbol *
7292b3ac 2108sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 2109{
699733f6 2110 long mid;
90e3cdf2 2111
7292b3ac 2112 if (id == (unsigned) -1)
699733f6
AM
2113 {
2114 while (lo < hi)
2115 {
2116 mid = (lo + hi) >> 1;
2117 if (syms[mid]->value + syms[mid]->section->vma < value)
2118 lo = mid + 1;
2119 else if (syms[mid]->value + syms[mid]->section->vma > value)
2120 hi = mid;
2121 else
2122 return syms[mid];
2123 }
2124 }
2125 else
2126 {
2127 while (lo < hi)
2128 {
2129 mid = (lo + hi) >> 1;
2130 if (syms[mid]->section->id < id)
2131 lo = mid + 1;
2132 else if (syms[mid]->section->id > id)
2133 hi = mid;
2134 else if (syms[mid]->value < value)
2135 lo = mid + 1;
2136 else if (syms[mid]->value > value)
2137 hi = mid;
2138 else
2139 return syms[mid];
2140 }
2141 }
2142 return NULL;
90e3cdf2
JJ
2143}
2144
468392fb
AM
2145static bfd_boolean
2146section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2147{
2148 bfd_vma vma = *(bfd_vma *) ptr;
2149 return ((section->flags & SEC_ALLOC) != 0
2150 && section->vma <= vma
2151 && vma < section->vma + section->size);
2152}
2153
699733f6 2154/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
2155 entry syms. Also generate @plt symbols for the glink branch table.
2156 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
2157
2158static long
a7535cf3
AM
2159ppc64_elf_get_synthetic_symtab (bfd *abfd,
2160 long static_count, asymbol **static_syms,
2161 long dyn_count, asymbol **dyn_syms,
c9727e01 2162 asymbol **ret)
90e3cdf2
JJ
2163{
2164 asymbol *s;
0ccf57bd 2165 size_t i, j, count;
90e3cdf2 2166 char *names;
0ccf57bd 2167 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 2168 asection *opd = NULL;
90e3cdf2 2169 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2170 asymbol **syms;
ee67d69a 2171 int abi = abiversion (abfd);
90e3cdf2
JJ
2172
2173 *ret = NULL;
2174
ee67d69a
AM
2175 if (abi < 2)
2176 {
2177 opd = bfd_get_section_by_name (abfd, ".opd");
2178 if (opd == NULL && abi == 1)
2179 return 0;
2180 }
90e3cdf2 2181
a5259595
AM
2182 syms = NULL;
2183 codesecsym = 0;
2184 codesecsymend = 0;
2185 secsymend = 0;
2186 opdsymend = 0;
2187 symcount = 0;
2188 if (opd != NULL)
c9727e01 2189 {
a5259595
AM
2190 symcount = static_count;
2191 if (!relocatable)
2192 symcount += dyn_count;
2193 if (symcount == 0)
2194 return 0;
c9727e01 2195
a5259595
AM
2196 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2197 if (syms == NULL)
2198 return -1;
90e3cdf2 2199
a5259595
AM
2200 if (!relocatable && static_count != 0 && dyn_count != 0)
2201 {
2202 /* Use both symbol tables. */
2203 memcpy (syms, static_syms, static_count * sizeof (*syms));
2204 memcpy (syms + static_count, dyn_syms,
2205 (dyn_count + 1) * sizeof (*syms));
2206 }
2207 else if (!relocatable && static_count == 0)
2208 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2209 else
2210 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2211
0ccf57bd
AM
2212 /* Trim uninteresting symbols. Interesting symbols are section,
2213 function, and notype symbols. */
2214 for (i = 0, j = 0; i < symcount; ++i)
2215 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2216 | BSF_RELC | BSF_SRELC)) == 0)
2217 syms[j++] = syms[i];
2218 symcount = j;
2219
a5259595
AM
2220 synthetic_relocatable = relocatable;
2221 synthetic_opd = opd;
2222 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2223
a5259595
AM
2224 if (!relocatable && symcount > 1)
2225 {
bfa5bd2a
PA
2226 /* Trim duplicate syms, since we may have merged the normal
2227 and dynamic symbols. Actually, we only care about syms
2228 that have different values, so trim any with the same
2229 value. Don't consider ifunc and ifunc resolver symbols
2230 duplicates however, because GDB wants to know whether a
2231 text symbol is an ifunc resolver. */
a5259595 2232 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2233 {
2234 const asymbol *s0 = syms[i - 1];
2235 const asymbol *s1 = syms[i];
2236
2237 if ((s0->value + s0->section->vma
2238 != s1->value + s1->section->vma)
2239 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2240 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2241 syms[j++] = syms[i];
2242 }
a5259595
AM
2243 symcount = j;
2244 }
699733f6 2245
a5259595
AM
2246 i = 0;
2247 /* Note that here and in compare_symbols we can't compare opd and
2248 sym->section directly. With separate debug info files, the
2249 symbols will be extracted from the debug file while abfd passed
2250 to this function is the real binary. */
0ccf57bd 2251 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2252 ++i;
2253 codesecsym = i;
2254
2255 for (; i < symcount; ++i)
2256 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2257 | SEC_THREAD_LOCAL))
2258 != (SEC_CODE | SEC_ALLOC))
2259 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2260 break;
2261 codesecsymend = i;
2262
2263 for (; i < symcount; ++i)
2264 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2265 break;
2266 secsymend = i;
2267
2268 for (; i < symcount; ++i)
2269 if (strcmp (syms[i]->section->name, ".opd") != 0)
2270 break;
2271 opdsymend = i;
2272
2273 for (; i < symcount; ++i)
2cdcc330
AM
2274 if (((syms[i]->section->flags
2275 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2276 != (SEC_CODE | SEC_ALLOC))
2277 break;
2278 symcount = i;
2279 }
c9727e01 2280 count = 0;
90e3cdf2 2281
699733f6 2282 if (relocatable)
90e3cdf2 2283 {
699733f6
AM
2284 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2285 arelent *r;
2286 size_t size;
0ccf57bd 2287 size_t relcount;
90e3cdf2 2288
468392fb
AM
2289 if (opdsymend == secsymend)
2290 goto done;
2291
699733f6 2292 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2293 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2294 if (relcount == 0)
c9727e01 2295 goto done;
90e3cdf2 2296
7356fed5
AM
2297 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2298 {
2299 count = -1;
2300 goto done;
2301 }
2302
699733f6 2303 size = 0;
595da8c5 2304 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2305 {
2306 asymbol *sym;
90e3cdf2 2307
595da8c5 2308 while (r < opd->relocation + relcount
699733f6
AM
2309 && r->address < syms[i]->value + opd->vma)
2310 ++r;
90e3cdf2 2311
595da8c5 2312 if (r == opd->relocation + relcount)
699733f6 2313 break;
90e3cdf2 2314
699733f6
AM
2315 if (r->address != syms[i]->value + opd->vma)
2316 continue;
90e3cdf2 2317
699733f6
AM
2318 if (r->howto->type != R_PPC64_ADDR64)
2319 continue;
90e3cdf2 2320
699733f6
AM
2321 sym = *r->sym_ptr_ptr;
2322 if (!sym_exists_at (syms, opdsymend, symcount,
2323 sym->section->id, sym->value + r->addend))
2324 {
2325 ++count;
2326 size += sizeof (asymbol);
2327 size += strlen (syms[i]->name) + 2;
2328 }
2329 }
90e3cdf2 2330
c4b0b099
AM
2331 if (size == 0)
2332 goto done;
699733f6
AM
2333 s = *ret = bfd_malloc (size);
2334 if (s == NULL)
2335 {
7356fed5 2336 count = -1;
c9727e01 2337 goto done;
699733f6 2338 }
90e3cdf2 2339
699733f6 2340 names = (char *) (s + count);
90e3cdf2 2341
595da8c5 2342 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2343 {
699733f6 2344 asymbol *sym;
90e3cdf2 2345
595da8c5 2346 while (r < opd->relocation + relcount
699733f6
AM
2347 && r->address < syms[i]->value + opd->vma)
2348 ++r;
90e3cdf2 2349
595da8c5 2350 if (r == opd->relocation + relcount)
699733f6
AM
2351 break;
2352
2353 if (r->address != syms[i]->value + opd->vma)
2354 continue;
2355
2356 if (r->howto->type != R_PPC64_ADDR64)
2357 continue;
90e3cdf2 2358
699733f6
AM
2359 sym = *r->sym_ptr_ptr;
2360 if (!sym_exists_at (syms, opdsymend, symcount,
2361 sym->section->id, sym->value + r->addend))
2362 {
2363 size_t len;
2364
2365 *s = *syms[i];
6ba2a415 2366 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2367 s->section = sym->section;
2368 s->value = sym->value + r->addend;
2369 s->name = names;
2370 *names++ = '.';
2371 len = strlen (syms[i]->name);
2372 memcpy (names, syms[i]->name, len + 1);
2373 names += len + 1;
6f610d07
UW
2374 /* Have udata.p point back to the original symbol this
2375 synthetic symbol was derived from. */
2376 s->udata.p = syms[i];
699733f6
AM
2377 s++;
2378 }
2379 }
2380 }
2381 else
90e3cdf2 2382 {
468392fb 2383 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 2384 bfd_byte *contents = NULL;
699733f6 2385 size_t size;
0ccf57bd 2386 size_t plt_count = 0;
468392fb
AM
2387 bfd_vma glink_vma = 0, resolv_vma = 0;
2388 asection *dynamic, *glink = NULL, *relplt = NULL;
2389 arelent *p;
90e3cdf2 2390
ee67d69a 2391 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2392 {
c4b0b099
AM
2393 free_contents_and_exit_err:
2394 count = -1;
ee67d69a 2395 free_contents_and_exit:
699733f6 2396 if (contents)
ee67d69a 2397 free (contents);
c9727e01 2398 goto done;
699733f6 2399 }
90e3cdf2 2400
699733f6
AM
2401 size = 0;
2402 for (i = secsymend; i < opdsymend; ++i)
2403 {
2404 bfd_vma ent;
90e3cdf2 2405
5ef11c02
AM
2406 /* Ignore bogus symbols. */
2407 if (syms[i]->value > opd->size - 8)
2408 continue;
2409
699733f6
AM
2410 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2411 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2412 {
2413 ++count;
2414 size += sizeof (asymbol);
2415 size += strlen (syms[i]->name) + 2;
2416 }
2417 }
90e3cdf2 2418
468392fb 2419 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2420 if (dyn_count != 0
2421 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2422 {
2423 bfd_byte *dynbuf, *extdyn, *extdynend;
2424 size_t extdynsize;
2425 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2426
2427 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2428 goto free_contents_and_exit_err;
468392fb
AM
2429
2430 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2431 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2432
2433 extdyn = dynbuf;
2434 extdynend = extdyn + dynamic->size;
2435 for (; extdyn < extdynend; extdyn += extdynsize)
2436 {
2437 Elf_Internal_Dyn dyn;
2438 (*swap_dyn_in) (abfd, extdyn, &dyn);
2439
2440 if (dyn.d_tag == DT_NULL)
2441 break;
2442
2443 if (dyn.d_tag == DT_PPC64_GLINK)
2444 {
9e390558
AM
2445 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2446 See comment in ppc64_elf_finish_dynamic_sections. */
2447 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2448 /* The .glink section usually does not survive the final
2449 link; search for the section (usually .text) where the
2450 glink stubs now reside. */
2451 glink = bfd_sections_find_if (abfd, section_covers_vma,
2452 &glink_vma);
2453 break;
2454 }
2455 }
2456
2457 free (dynbuf);
2458 }
2459
2460 if (glink != NULL)
2461 {
2462 /* Determine __glink trampoline by reading the relative branch
2463 from the first glink stub. */
2464 bfd_byte buf[4];
b9e5796b
AM
2465 unsigned int off = 0;
2466
2467 while (bfd_get_section_contents (abfd, glink, buf,
2468 glink_vma + off - glink->vma, 4))
468392fb
AM
2469 {
2470 unsigned int insn = bfd_get_32 (abfd, buf);
2471 insn ^= B_DOT;
2472 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2473 {
2cdcc330
AM
2474 resolv_vma
2475 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2476 break;
2477 }
2478 off += 4;
2479 if (off > 4)
2480 break;
468392fb
AM
2481 }
2482
2483 if (resolv_vma)
2484 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2485
066ee829
AM
2486 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2487 if (relplt != NULL)
2488 {
2489 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2cdcc330 2490 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 2491 goto free_contents_and_exit_err;
68ffbac6 2492
066ee829
AM
2493 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2494 size += plt_count * sizeof (asymbol);
468392fb 2495
066ee829
AM
2496 p = relplt->relocation;
2497 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2498 {
2499 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2500 if (p->addend != 0)
2501 size += sizeof ("+0x") - 1 + 16;
2502 }
066ee829 2503 }
468392fb
AM
2504 }
2505
c4b0b099
AM
2506 if (size == 0)
2507 goto free_contents_and_exit;
699733f6
AM
2508 s = *ret = bfd_malloc (size);
2509 if (s == NULL)
c4b0b099 2510 goto free_contents_and_exit_err;
90e3cdf2 2511
468392fb 2512 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2513
699733f6 2514 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2515 {
699733f6 2516 bfd_vma ent;
90e3cdf2 2517
5ef11c02
AM
2518 if (syms[i]->value > opd->size - 8)
2519 continue;
2520
699733f6
AM
2521 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2522 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2523 {
0ccf57bd 2524 size_t lo, hi;
699733f6 2525 size_t len;
c9727e01 2526 asection *sec = abfd->sections;
90e3cdf2 2527
699733f6
AM
2528 *s = *syms[i];
2529 lo = codesecsym;
2530 hi = codesecsymend;
2531 while (lo < hi)
2532 {
0ccf57bd 2533 size_t mid = (lo + hi) >> 1;
699733f6
AM
2534 if (syms[mid]->section->vma < ent)
2535 lo = mid + 1;
2536 else if (syms[mid]->section->vma > ent)
2537 hi = mid;
2538 else
c9727e01
AM
2539 {
2540 sec = syms[mid]->section;
2541 break;
2542 }
699733f6
AM
2543 }
2544
c9727e01 2545 if (lo >= hi && lo > codesecsym)
699733f6 2546 sec = syms[lo - 1]->section;
699733f6
AM
2547
2548 for (; sec != NULL; sec = sec->next)
2549 {
2550 if (sec->vma > ent)
2551 break;
63524580
JK
2552 /* SEC_LOAD may not be set if SEC is from a separate debug
2553 info file. */
2554 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2555 break;
2556 if ((sec->flags & SEC_CODE) != 0)
2557 s->section = sec;
2558 }
6ba2a415 2559 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2560 s->value = ent - s->section->vma;
2561 s->name = names;
2562 *names++ = '.';
2563 len = strlen (syms[i]->name);
2564 memcpy (names, syms[i]->name, len + 1);
2565 names += len + 1;
6f610d07
UW
2566 /* Have udata.p point back to the original symbol this
2567 synthetic symbol was derived from. */
2568 s->udata.p = syms[i];
699733f6 2569 s++;
90e3cdf2 2570 }
90e3cdf2 2571 }
699733f6 2572 free (contents);
468392fb
AM
2573
2574 if (glink != NULL && relplt != NULL)
2575 {
2576 if (resolv_vma)
2577 {
2578 /* Add a symbol for the main glink trampoline. */
86a4952b 2579 memset (s, 0, sizeof *s);
468392fb 2580 s->the_bfd = abfd;
6ba2a415 2581 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2582 s->section = glink;
2583 s->value = resolv_vma - glink->vma;
2584 s->name = names;
2cdcc330
AM
2585 memcpy (names, "__glink_PLTresolve",
2586 sizeof ("__glink_PLTresolve"));
468392fb
AM
2587 names += sizeof ("__glink_PLTresolve");
2588 s++;
2589 count++;
2590 }
2591
2592 /* FIXME: It would be very much nicer to put sym@plt on the
2593 stub rather than on the glink branch table entry. The
2594 objdump disassembler would then use a sensible symbol
2595 name on plt calls. The difficulty in doing so is
2596 a) finding the stubs, and,
2597 b) matching stubs against plt entries, and,
2598 c) there can be multiple stubs for a given plt entry.
2599
2600 Solving (a) could be done by code scanning, but older
2601 ppc64 binaries used different stubs to current code.
2602 (b) is the tricky one since you need to known the toc
2603 pointer for at least one function that uses a pic stub to
2604 be able to calculate the plt address referenced.
2605 (c) means gdb would need to set multiple breakpoints (or
2606 find the glink branch itself) when setting breakpoints
2607 for pending shared library loads. */
2608 p = relplt->relocation;
2609 for (i = 0; i < plt_count; i++, p++)
2610 {
2611 size_t len;
2612
2613 *s = **p->sym_ptr_ptr;
2614 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2615 we are defining a symbol, ensure one of them is set. */
2616 if ((s->flags & BSF_LOCAL) == 0)
2617 s->flags |= BSF_GLOBAL;
6ba2a415 2618 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2619 s->section = glink;
2620 s->value = glink_vma - glink->vma;
2621 s->name = names;
2622 s->udata.p = NULL;
2623 len = strlen ((*p->sym_ptr_ptr)->name);
2624 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2625 names += len;
e054468f
AM
2626 if (p->addend != 0)
2627 {
2628 memcpy (names, "+0x", sizeof ("+0x") - 1);
2629 names += sizeof ("+0x") - 1;
2630 bfd_sprintf_vma (abfd, names, p->addend);
2631 names += strlen (names);
2632 }
468392fb
AM
2633 memcpy (names, "@plt", sizeof ("@plt"));
2634 names += sizeof ("@plt");
2635 s++;
b9e5796b
AM
2636 if (abi < 2)
2637 {
2638 glink_vma += 8;
2639 if (i >= 0x8000)
2640 glink_vma += 4;
2641 }
2642 else
468392fb
AM
2643 glink_vma += 4;
2644 }
2645 count += plt_count;
2646 }
90e3cdf2
JJ
2647 }
2648
c9727e01 2649 done:
a7535cf3 2650 free (syms);
90e3cdf2
JJ
2651 return count;
2652}
5bd4f169 2653\f
65f38f15
AM
2654/* The following functions are specific to the ELF linker, while
2655 functions above are used generally. Those named ppc64_elf_* are
2656 called by the main ELF linker code. They appear in this file more
2657 or less in the order in which they are called. eg.
2658 ppc64_elf_check_relocs is called early in the link process,
2659 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2660 called.
2661
2662 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2663 functions have both a function code symbol and a function descriptor
2664 symbol. A call to foo in a relocatable object file looks like:
2665
2666 . .text
2667 . x:
2668 . bl .foo
2669 . nop
2670
2671 The function definition in another object file might be:
2672
2673 . .section .opd
2674 . foo: .quad .foo
2675 . .quad .TOC.@tocbase
2676 . .quad 0
2677 .
2678 . .text
2679 . .foo: blr
2680
2681 When the linker resolves the call during a static link, the branch
2682 unsurprisingly just goes to .foo and the .opd information is unused.
2683 If the function definition is in a shared library, things are a little
2684 different: The call goes via a plt call stub, the opd information gets
2685 copied to the plt, and the linker patches the nop.
2686
2687 . x:
2688 . bl .foo_stub
2689 . ld 2,40(1)
2690 .
2691 .
2692 . .foo_stub:
71a39c98
AM
2693 . std 2,40(1) # in practice, the call stub
2694 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2695 . addi 11,11,Lfoo@toc@l # this is the general idea
2696 . ld 12,0(11)
2697 . ld 2,8(11)
2698 . mtctr 12
2699 . ld 11,16(11)
e86ce104
AM
2700 . bctr
2701 .
2702 . .section .plt
2703 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2704
2705 The "reloc ()" notation is supposed to indicate that the linker emits
2706 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2707 copying.
2708
2709 What are the difficulties here? Well, firstly, the relocations
2710 examined by the linker in check_relocs are against the function code
2711 sym .foo, while the dynamic relocation in the plt is emitted against
2712 the function descriptor symbol, foo. Somewhere along the line, we need
2713 to carefully copy dynamic link information from one symbol to the other.
2714 Secondly, the generic part of the elf linker will make .foo a dynamic
2715 symbol as is normal for most other backends. We need foo dynamic
2716 instead, at least for an application final link. However, when
2717 creating a shared library containing foo, we need to have both symbols
2718 dynamic so that references to .foo are satisfied during the early
2719 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2720 definition from some other object, eg. a static library.
2721
2722 Update: As of August 2004, we support a new convention. Function
2723 calls may use the function descriptor symbol, ie. "bl foo". This
2724 behaves exactly as "bl .foo". */
65f38f15 2725
7c8bbca5
AM
2726/* Of those relocs that might be copied as dynamic relocs, this
2727 function selects those that must be copied when linking a shared
2728 library or PIE, even when the symbol is local. */
65f38f15 2729
1d483afe
AM
2730static int
2731must_be_dyn_reloc (struct bfd_link_info *info,
2732 enum elf_ppc64_reloc_type r_type)
2733{
2734 switch (r_type)
2735 {
2736 default:
7c8bbca5
AM
2737 /* Only relative relocs can be resolved when the object load
2738 address isn't fixed. DTPREL64 is excluded because the
2739 dynamic linker needs to differentiate global dynamic from
2740 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2741 return 1;
2742
2743 case R_PPC64_REL32:
2744 case R_PPC64_REL64:
2745 case R_PPC64_REL30:
2746 return 0;
2747
2748 case R_PPC64_TPREL16:
2749 case R_PPC64_TPREL16_LO:
2750 case R_PPC64_TPREL16_HI:
2751 case R_PPC64_TPREL16_HA:
2752 case R_PPC64_TPREL16_DS:
2753 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2754 case R_PPC64_TPREL16_HIGH:
2755 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2756 case R_PPC64_TPREL16_HIGHER:
2757 case R_PPC64_TPREL16_HIGHERA:
2758 case R_PPC64_TPREL16_HIGHEST:
2759 case R_PPC64_TPREL16_HIGHESTA:
2760 case R_PPC64_TPREL64:
c213164a 2761 case R_PPC64_TPREL34:
7c8bbca5
AM
2762 /* These relocations are relative but in a shared library the
2763 linker doesn't know the thread pointer base. */
2764 return bfd_link_dll (info);
1d483afe
AM
2765 }
2766}
65f38f15 2767
f4656909
AM
2768/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2769 copying dynamic variables from a shared lib into an app's dynbss
2770 section, and instead use a dynamic relocation to point into the
5d35169e
AM
2771 shared lib. With code that gcc generates, it's vital that this be
2772 enabled; In the PowerPC64 ABI, the address of a function is actually
2773 the address of a function descriptor, which resides in the .opd
2774 section. gcc uses the descriptor directly rather than going via the
2775 GOT as some other ABI's do, which means that initialized function
2776 pointers must reference the descriptor. Thus, a function pointer
2777 initialized to the address of a function in a shared library will
2778 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 2779 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
2780 presents a problem as a plt entry for that function is also
2781 initialized from the function descriptor symbol and the copy reloc
2782 may not be initialized first. */
a23b6845 2783#define ELIMINATE_COPY_RELOCS 1
f4656909 2784
721956f4 2785/* Section name for stubs is the associated section name plus this
29942be8
NC
2786 string. */
2787#define STUB_SUFFIX ".stub"
721956f4
AM
2788
2789/* Linker stubs.
2790 ppc_stub_long_branch:
2791 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2792 destination, but a 24 bit branch in a stub section will reach.
2793 . b dest
2794
2795 ppc_stub_plt_branch:
2796 Similar to the above, but a 24 bit branch in the stub section won't
2797 reach its destination.
71a39c98
AM
2798 . addis %r11,%r2,xxx@toc@ha
2799 . ld %r12,xxx@toc@l(%r11)
2800 . mtctr %r12
721956f4
AM
2801 . bctr
2802
2803 ppc_stub_plt_call:
2c66dc6c
AM
2804 Used to call a function in a shared library. If it so happens that
2805 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2806 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 2807 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
2808 . addis %r11,%r2,xxx@toc@ha
2809 . ld %r12,xxx+0@toc@l(%r11)
2810 . mtctr %r12
2811 . ld %r2,xxx+8@toc@l(%r11)
2812 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2813 . bctr
ad8e1ba5
AM
2814
2815 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2816 code to adjust the value and save r2 to support multiple toc sections.
2817 A ppc_stub_long_branch with an r2 offset looks like:
2818 . std %r2,40(%r1)
2819 . addis %r2,%r2,off@ha
2820 . addi %r2,%r2,off@l
2821 . b dest
2822
2823 A ppc_stub_plt_branch with an r2 offset looks like:
2824 . std %r2,40(%r1)
71a39c98
AM
2825 . addis %r11,%r2,xxx@toc@ha
2826 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
2827 . addis %r2,%r2,off@ha
2828 . addi %r2,%r2,off@l
71a39c98 2829 . mtctr %r12
ad8e1ba5 2830 . bctr
ac2df442 2831
05d0e962
AM
2832 All of the above stubs are shown as their ELFv1 variants. ELFv2
2833 variants exist too, simpler for plt calls since a new toc pointer
2834 and static chain are not loaded by the stub. In addition, ELFv2
2835 has some more complex stubs to handle calls marked with NOTOC
2836 relocs from functions where r2 is not a valid toc pointer. These
2837 come in two flavours, the ones shown below, and _both variants that
2838 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2839 one call is from a function where r2 is used as the toc pointer but
2840 needs a toc adjusting stub for small-model multi-toc, and another
2841 call is from a function where r2 is not valid.
2842 ppc_stub_long_branch_notoc:
2843 . mflr %r12
2844 . bcl 20,31,1f
2845 . 1:
2846 . mflr %r11
2847 . mtlr %r12
2cdcc330
AM
2848 . addis %r12,%r11,dest-1b@ha
2849 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2850 . b dest
2851
2852 ppc_stub_plt_branch_notoc:
2853 . mflr %r12
2854 . bcl 20,31,1f
2855 . 1:
2856 . mflr %r11
2857 . mtlr %r12
2858 . lis %r12,xxx-1b@highest
f891966f 2859 . ori %r12,%r12,xxx-1b@higher
05d0e962 2860 . sldi %r12,%r12,32
f891966f 2861 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2862 . ori %r12,%r12,xxx-1b@l
2863 . add %r12,%r11,%r12
2864 . mtctr %r12
2865 . bctr
2866
2867 ppc_stub_plt_call_notoc:
2868 . mflr %r12
2869 . bcl 20,31,1f
2870 . 1:
2871 . mflr %r11
2872 . mtlr %r12
2873 . lis %r12,xxx-1b@highest
f891966f 2874 . ori %r12,%r12,xxx-1b@higher
05d0e962 2875 . sldi %r12,%r12,32
f891966f 2876 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2877 . ori %r12,%r12,xxx-1b@l
2878 . ldx %r12,%r11,%r12
2879 . mtctr %r12
2880 . bctr
2881
04bdff6a
AM
2882 There are also ELFv1 powerxx variants of these stubs.
2883 ppc_stub_long_branch_notoc:
d4b87b1e 2884 . pla %r12,dest@pcrel
04bdff6a
AM
2885 . b dest
2886 ppc_stub_plt_branch_notoc:
2887 . lis %r11,(dest-1f)@highesta34
2888 . ori %r11,%r11,(dest-1f)@highera34
2889 . sldi %r11,%r11,34
d4b87b1e 2890 . 1: pla %r12,dest@pcrel
04bdff6a
AM
2891 . add %r12,%r11,%r12
2892 . mtctr %r12
2893 . bctr
2894 ppc_stub_plt_call_notoc:
2895 . lis %r11,(xxx-1f)@highesta34
2896 . ori %r11,%r11,(xxx-1f)@highera34
2897 . sldi %r11,%r11,34
d4b87b1e 2898 . 1: pla %r12,xxx@pcrel
04bdff6a
AM
2899 . ldx %r12,%r11,%r12
2900 . mtctr %r12
2901 . bctr
2902
05d0e962
AM
2903 In cases where the high instructions would add zero, they are
2904 omitted and following instructions modified in some cases.
04bdff6a
AM
2905 For example, a powerxx ppc_stub_plt_call_notoc might simplify down
2906 to
2907 . pld %r12,xxx@pcrel
2908 . mtctr %r12
2909 . bctr
05d0e962
AM
2910
2911 For a given stub group (a set of sections all using the same toc
2912 pointer value) there will be just one stub type used for any
2913 particular function symbol. For example, if printf is called from
2914 code with the tocsave optimization (ie. r2 saved in function
2915 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2916 and from other code without the tocsave optimization requiring a
2917 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2918 type will be created. Calls with the tocsave optimization will
2919 enter this stub after the instruction saving r2. A similar
2920 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2921 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2922 to call an external function like printf. If other calls to printf
2923 require a ppc_stub_plt_call linkage stub then a single
2924 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2925 call. If other calls to printf require a ppc_stub_plt_call_r2save
2926 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2927 be created and calls not requiring r2 to be saved will enter the
2928 stub after the r2 save instruction. There is an analogous
2929 hierarchy of long branch and plt branch stubs for local call
2930 linkage. */
721956f4 2931
2cdcc330
AM
2932enum ppc_stub_type
2933{
721956f4
AM
2934 ppc_stub_none,
2935 ppc_stub_long_branch,
ad8e1ba5 2936 ppc_stub_long_branch_r2off,
05d0e962
AM
2937 ppc_stub_long_branch_notoc,
2938 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 2939 ppc_stub_plt_branch,
ad8e1ba5 2940 ppc_stub_plt_branch_r2off,
05d0e962
AM
2941 ppc_stub_plt_branch_notoc,
2942 ppc_stub_plt_branch_both,
794e51c0 2943 ppc_stub_plt_call,
7341d5e2 2944 ppc_stub_plt_call_r2save,
05d0e962
AM
2945 ppc_stub_plt_call_notoc,
2946 ppc_stub_plt_call_both,
a4b6fadd
AM
2947 ppc_stub_global_entry,
2948 ppc_stub_save_res
721956f4
AM
2949};
2950
6f20ed8a
AM
2951/* Information on stub grouping. */
2952struct map_stub
2953{
2954 /* The stub section. */
2955 asection *stub_sec;
2956 /* This is the section to which stubs in the group will be attached. */
2957 asection *link_sec;
a4b6fadd
AM
2958 /* Next group. */
2959 struct map_stub *next;
2960 /* Whether to emit a copy of register save/restore functions in this
2961 group. */
2962 int needs_save_res;
df136d64
AM
2963 /* Current offset within stubs after the insn restoring lr in a
2964 _notoc or _both stub using bcl for pc-relative addressing, or
2965 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2966 unsigned int lr_restore;
2967 /* Accumulated size of EH info emitted to describe return address
2968 if stubs modify lr. Does not include 17 byte FDE header. */
2969 unsigned int eh_size;
2970 /* Offset in glink_eh_frame to the start of EH info for this group. */
2971 unsigned int eh_base;
6f20ed8a
AM
2972};
2973
2cdcc330
AM
2974struct ppc_stub_hash_entry
2975{
721956f4
AM
2976 /* Base hash table entry structure. */
2977 struct bfd_hash_entry root;
2978
ad8e1ba5
AM
2979 enum ppc_stub_type stub_type;
2980
6f20ed8a
AM
2981 /* Group information. */
2982 struct map_stub *group;
721956f4
AM
2983
2984 /* Offset within stub_sec of the beginning of this stub. */
2985 bfd_vma stub_offset;
2986
2987 /* Given the symbol's value and its section we can determine its final
2988 value when building the stubs (so the stub knows where to jump. */
2989 bfd_vma target_value;
2990 asection *target_section;
2991
721956f4
AM
2992 /* The symbol table entry, if any, that this was derived from. */
2993 struct ppc_link_hash_entry *h;
e054468f 2994 struct plt_entry *plt_ent;
721956f4 2995
2d7ad24e
AM
2996 /* Symbol type. */
2997 unsigned char symtype;
2998
6911b7dc
AM
2999 /* Symbol st_other. */
3000 unsigned char other;
721956f4
AM
3001};
3002
2cdcc330
AM
3003struct ppc_branch_hash_entry
3004{
721956f4
AM
3005 /* Base hash table entry structure. */
3006 struct bfd_hash_entry root;
3007
c456f082 3008 /* Offset within branch lookup table. */
721956f4
AM
3009 unsigned int offset;
3010
3011 /* Generation marker. */
3012 unsigned int iter;
3013};
65f38f15 3014
19e08130
AM
3015/* Used to track dynamic relocations for local symbols. */
3016struct ppc_dyn_relocs
3017{
3018 struct ppc_dyn_relocs *next;
3019
3020 /* The input section of the reloc. */
3021 asection *sec;
3022
3023 /* Total number of relocs copied for the input section. */
3024 unsigned int count : 31;
3025
3026 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3027 unsigned int ifunc : 1;
3028};
3029
65f38f15
AM
3030struct ppc_link_hash_entry
3031{
3032 struct elf_link_hash_entry elf;
3033
2cdcc330
AM
3034 union
3035 {
b3fac117
AM
3036 /* A pointer to the most recently used stub hash entry against this
3037 symbol. */
3038 struct ppc_stub_hash_entry *stub_cache;
3039
3040 /* A pointer to the next symbol starting with a '.' */
3041 struct ppc_link_hash_entry *next_dot_sym;
3042 } u;
721956f4 3043
65f38f15 3044 /* Track dynamic relocs copied for this symbol. */
6061a67d 3045 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3046
721956f4 3047 /* Link between function code and descriptor symbols. */
34814b9f 3048 struct ppc_link_hash_entry *oh;
721956f4 3049
e86ce104
AM
3050 /* Flag function code and descriptor symbols. */
3051 unsigned int is_func:1;
3052 unsigned int is_func_descriptor:1;
908b32fc 3053 unsigned int fake:1;
411e1bfb 3054
c5614fa4
AM
3055 /* Whether global opd/toc sym has been adjusted or not.
3056 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3057 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3058 unsigned int adjust_done:1;
3059
a4b6fadd
AM
3060 /* Set if this is an out-of-line register save/restore function,
3061 with non-standard calling convention. */
3062 unsigned int save_res:1;
3063
8b5f1ed8
AM
3064 /* Set if a duplicate symbol with non-zero localentry is detected,
3065 even when the duplicate symbol does not provide a definition. */
3066 unsigned int non_zero_localentry:1;
3067
411e1bfb 3068 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3069 Bits are or'd into the mask as the corresponding relocs are
3070 encountered during check_relocs, with TLS_TLS being set when any
3071 of the other TLS bits are set. tls_optimize clears bits when
3072 optimizing to indicate the corresponding GOT entry type is not
3073 needed. If set, TLS_TLS is never cleared. tls_optimize may also
b00a0a86 3074 set TLS_GDIE when a GD reloc turns into an IE one.
37da22e5
AM
3075 These flags are also kept for local symbols. */
3076#define TLS_TLS 1 /* Any TLS reloc. */
3077#define TLS_GD 2 /* GD reloc. */
3078#define TLS_LD 4 /* LD reloc. */
3079#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3080#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3081#define TLS_MARK 32 /* __tls_get_addr call marked. */
b00a0a86 3082#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
46e9995a 3083#define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
f961d9dd 3084 unsigned char tls_mask;
37da22e5
AM
3085
3086 /* The above field is also used to mark function symbols. In which
3087 case TLS_TLS will be 0. */
3088#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3089#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3090#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3091};
3092
3093/* ppc64 ELF linker hash table. */
3094
3095struct ppc_link_hash_table
3096{
3097 struct elf_link_hash_table elf;
3098
721956f4
AM
3099 /* The stub hash table. */
3100 struct bfd_hash_table stub_hash_table;
3101
3102 /* Another hash table for plt_branch stubs. */
3103 struct bfd_hash_table branch_hash_table;
3104
3b421ab3
AM
3105 /* Hash table for function prologue tocsave. */
3106 htab_t tocsave_htab;
3107
e7d1c40c
AM
3108 /* Various options and other info passed from the linker. */
3109 struct ppc64_elf_params *params;
721956f4 3110
6f20ed8a
AM
3111 /* The size of sec_info below. */
3112 unsigned int sec_info_arr_size;
3113
3114 /* Per-section array of extra section info. Done this way rather
3115 than as part of ppc64_elf_section_data so we have the info for
3116 non-ppc64 sections. */
3117 struct
3118 {
3119 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3120 bfd_vma toc_off;
6f20ed8a
AM
3121
3122 union
3123 {
3124 /* The section group that this section belongs to. */
3125 struct map_stub *group;
3126 /* A temp section list pointer. */
3127 asection *list;
3128 } u;
3129 } *sec_info;
721956f4 3130
a4b6fadd
AM
3131 /* Linked list of groups. */
3132 struct map_stub *group;
3133
ad8e1ba5
AM
3134 /* Temp used when calculating TOC pointers. */
3135 bfd_vma toc_curr;
bf102f86
AM
3136 bfd *toc_bfd;
3137 asection *toc_first_sec;
ad8e1ba5 3138
b3fac117
AM
3139 /* Used when adding symbols. */
3140 struct ppc_link_hash_entry *dot_syms;
3141
33e44f2e 3142 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3143 asection *glink;
9e390558 3144 asection *global_entry;
82bd7b59 3145 asection *sfpr;
2d7ad24e
AM
3146 asection *pltlocal;
3147 asection *relpltlocal;
4ce794b7
AM
3148 asection *brlt;
3149 asection *relbrlt;
58d180e8 3150 asection *glink_eh_frame;
ec338859 3151
8387904d
AM
3152 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3153 struct ppc_link_hash_entry *tls_get_addr;
3154 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3155
927be08e
AM
3156 /* The size of reliplt used by got entry relocs. */
3157 bfd_size_type got_reli_size;
3158
9b5ecbd0 3159 /* Statistics. */
7341d5e2 3160 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 3161
ee75fd95
AM
3162 /* Number of stubs against global syms. */
3163 unsigned long stub_globals;
3164
ee67d69a
AM
3165 /* Set if we're linking code with function descriptors. */
3166 unsigned int opd_abi:1;
3167
4c52953f 3168 /* Support for multiple toc sections. */
33c0ec9d 3169 unsigned int do_multi_toc:1;
4c52953f 3170 unsigned int multi_toc_needed:1;
927be08e 3171 unsigned int second_toc_pass:1;
67f0cbdb 3172 unsigned int do_toc_opt:1;
4c52953f 3173
9a23f96e
AM
3174 /* Set if tls optimization is enabled. */
3175 unsigned int do_tls_opt:1;
3176
3e04d765
AM
3177 /* Set if inline plt calls should be converted to direct calls. */
3178 unsigned int can_convert_all_inline_plt:1;
3179
5d1634d7 3180 /* Set on error. */
99877b66 3181 unsigned int stub_error:1;
721956f4 3182
8c5b4e52
AM
3183 /* Whether func_desc_adjust needs to be run over symbols. */
3184 unsigned int need_func_desc_adj:1;
721956f4 3185
82e66161
AM
3186 /* Whether there exist local gnu indirect function resolvers,
3187 referenced by dynamic relocations. */
3188 unsigned int local_ifunc_resolver:1;
3189 unsigned int maybe_local_ifunc_resolver:1;
3190
f378ab09
AM
3191 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3192 unsigned int has_plt_localentry0:1;
3193
5663e321
AM
3194 /* Whether calls are made via the PLT from NOTOC functions. */
3195 unsigned int notoc_plt:1;
3196
04bdff6a
AM
3197 /* Whether to use powerxx instructions in linkage stubs. */
3198 unsigned int powerxx_stubs:1;
3199
721956f4
AM
3200 /* Incremented every time we size stubs. */
3201 unsigned int stub_iteration;
5d1634d7 3202
87d72d41
AM
3203 /* Small local sym cache. */
3204 struct sym_cache sym_cache;
65f38f15
AM
3205};
3206
4c52953f
AM
3207/* Rename some of the generic section flags to better document how they
3208 are used here. */
b0dddeec
AM
3209
3210/* Nonzero if this section has TLS related relocations. */
3211#define has_tls_reloc sec_flg0
3212
23cedd1d 3213/* Nonzero if this section has an old-style call to __tls_get_addr. */
b0dddeec
AM
3214#define has_tls_get_addr_call sec_flg1
3215
3216/* Nonzero if this section has any toc or got relocs. */
3217#define has_toc_reloc sec_flg2
3218
3219/* Nonzero if this section has a call to another section that uses
3220 the toc or got. */
d77c8a4b 3221#define makes_toc_func_call sec_flg3
b0dddeec
AM
3222
3223/* Recursion protection when determining above flag. */
d77c8a4b 3224#define call_check_in_progress sec_flg4
70cc837d 3225#define call_check_done sec_flg5
4c52953f 3226
65f38f15
AM
3227/* Get the ppc64 ELF linker hash table from a link_info structure. */
3228
3229#define ppc_hash_table(p) \
4dfe6ac6
NC
3230 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3231 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3232
721956f4
AM
3233#define ppc_stub_hash_lookup(table, string, create, copy) \
3234 ((struct ppc_stub_hash_entry *) \
3235 bfd_hash_lookup ((table), (string), (create), (copy)))
3236
3237#define ppc_branch_hash_lookup(table, string, create, copy) \
3238 ((struct ppc_branch_hash_entry *) \
3239 bfd_hash_lookup ((table), (string), (create), (copy)))
3240
3241/* Create an entry in the stub hash table. */
3242
3243static struct bfd_hash_entry *
4ce794b7
AM
3244stub_hash_newfunc (struct bfd_hash_entry *entry,
3245 struct bfd_hash_table *table,
3246 const char *string)
721956f4
AM
3247{
3248 /* Allocate the structure if it has not already been allocated by a
3249 subclass. */
3250 if (entry == NULL)
3251 {
3252 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3253 if (entry == NULL)
3254 return entry;
3255 }
3256
3257 /* Call the allocation method of the superclass. */
3258 entry = bfd_hash_newfunc (entry, table, string);
3259 if (entry != NULL)
3260 {
3261 struct ppc_stub_hash_entry *eh;
3262
3263 /* Initialize the local fields. */
3264 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3265 eh->stub_type = ppc_stub_none;
6f20ed8a 3266 eh->group = NULL;
721956f4
AM
3267 eh->stub_offset = 0;
3268 eh->target_value = 0;
3269 eh->target_section = NULL;
721956f4 3270 eh->h = NULL;
6911b7dc 3271 eh->plt_ent = NULL;
6911b7dc 3272 eh->other = 0;
721956f4
AM
3273 }
3274
3275 return entry;
3276}
3277
3278/* Create an entry in the branch hash table. */
3279
3280static struct bfd_hash_entry *
4ce794b7
AM
3281branch_hash_newfunc (struct bfd_hash_entry *entry,
3282 struct bfd_hash_table *table,
3283 const char *string)
721956f4
AM
3284{
3285 /* Allocate the structure if it has not already been allocated by a
3286 subclass. */
3287 if (entry == NULL)
3288 {
3289 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3290 if (entry == NULL)
3291 return entry;
3292 }
3293
3294 /* Call the allocation method of the superclass. */
3295 entry = bfd_hash_newfunc (entry, table, string);
3296 if (entry != NULL)
3297 {
3298 struct ppc_branch_hash_entry *eh;
3299
3300 /* Initialize the local fields. */
3301 eh = (struct ppc_branch_hash_entry *) entry;
3302 eh->offset = 0;
3303 eh->iter = 0;
3304 }
3305
3306 return entry;
3307}
3308
65f38f15
AM
3309/* Create an entry in a ppc64 ELF linker hash table. */
3310
3311static struct bfd_hash_entry *
4ce794b7
AM
3312link_hash_newfunc (struct bfd_hash_entry *entry,
3313 struct bfd_hash_table *table,
3314 const char *string)
65f38f15
AM
3315{
3316 /* Allocate the structure if it has not already been allocated by a
3317 subclass. */
3318 if (entry == NULL)
3319 {
3320 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3321 if (entry == NULL)
3322 return entry;
3323 }
3324
3325 /* Call the allocation method of the superclass. */
3326 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3327 if (entry != NULL)
3328 {
3329 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3330
b3fac117 3331 memset (&eh->u.stub_cache, 0,
908b32fc 3332 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3333 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3334
3335 /* When making function calls, old ABI code references function entry
3336 points (dot symbols), while new ABI code references the function
3337 descriptor symbol. We need to make any combination of reference and
3338 definition work together, without breaking archive linking.
3339
3340 For a defined function "foo" and an undefined call to "bar":
3341 An old object defines "foo" and ".foo", references ".bar" (possibly
3342 "bar" too).
3343 A new object defines "foo" and references "bar".
3344
3345 A new object thus has no problem with its undefined symbols being
3346 satisfied by definitions in an old object. On the other hand, the
3347 old object won't have ".bar" satisfied by a new object.
3348
3349 Keep a list of newly added dot-symbols. */
3350
3351 if (string[0] == '.')
3352 {
3353 struct ppc_link_hash_table *htab;
3354
3355 htab = (struct ppc_link_hash_table *) table;
3356 eh->u.next_dot_sym = htab->dot_syms;
3357 htab->dot_syms = eh;
3358 }
65f38f15
AM
3359 }
3360
3361 return entry;
3362}
3363
2cdcc330
AM
3364struct tocsave_entry
3365{
3b421ab3
AM
3366 asection *sec;
3367 bfd_vma offset;
3368};
3369
3370static hashval_t
3371tocsave_htab_hash (const void *p)
3372{
3373 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3374 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3375}
3376
3377static int
3378tocsave_htab_eq (const void *p1, const void *p2)
3379{
3380 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3381 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3382 return e1->sec == e2->sec && e1->offset == e2->offset;
3383}
3384
68faa637
AM
3385/* Destroy a ppc64 ELF linker hash table. */
3386
3387static void
d495ab0d 3388ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3389{
d495ab0d 3390 struct ppc_link_hash_table *htab;
68faa637 3391
d495ab0d 3392 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3393 if (htab->tocsave_htab)
3394 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3395 bfd_hash_table_free (&htab->branch_hash_table);
3396 bfd_hash_table_free (&htab->stub_hash_table);
3397 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3398}
3399
65f38f15
AM
3400/* Create a ppc64 ELF linker hash table. */
3401
3402static struct bfd_link_hash_table *
4ce794b7 3403ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3404{
3405 struct ppc_link_hash_table *htab;
3406 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3407
4ce794b7 3408 htab = bfd_zmalloc (amt);
65f38f15
AM
3409 if (htab == NULL)
3410 return NULL;
3411
66eb6687 3412 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3413 sizeof (struct ppc_link_hash_entry),
3414 PPC64_ELF_DATA))
65f38f15 3415 {
e2d34d7d 3416 free (htab);
65f38f15
AM
3417 return NULL;
3418 }
3419
721956f4 3420 /* Init the stub hash table too. */
66eb6687
AM
3421 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3422 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3423 {
d495ab0d 3424 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3425 return NULL;
3426 }
721956f4
AM
3427
3428 /* And the branch hash table. */
66eb6687
AM
3429 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3430 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3431 {
3432 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3433 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3434 return NULL;
3435 }
721956f4 3436
3b421ab3
AM
3437 htab->tocsave_htab = htab_try_create (1024,
3438 tocsave_htab_hash,
3439 tocsave_htab_eq,
3440 NULL);
3441 if (htab->tocsave_htab == NULL)
2915c55b 3442 {
d495ab0d 3443 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3444 return NULL;
3445 }
d495ab0d 3446 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3447
3254fd24
AM
3448 /* Initializing two fields of the union is just cosmetic. We really
3449 only care about glist, but when compiled on a 32-bit host the
3450 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3451 debugger inspection of these fields look nicer. */
a6aa5195
AM
3452 htab->elf.init_got_refcount.refcount = 0;
3453 htab->elf.init_got_refcount.glist = NULL;
3454 htab->elf.init_plt_refcount.refcount = 0;
3455 htab->elf.init_plt_refcount.glist = NULL;
3456 htab->elf.init_got_offset.offset = 0;
3457 htab->elf.init_got_offset.glist = NULL;
3458 htab->elf.init_plt_offset.offset = 0;
3459 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3460
65f38f15
AM
3461 return &htab->elf.root;
3462}
3463
bfeb4a28
AM
3464/* Create sections for linker generated code. */
3465
3466static bfd_boolean
3467create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3468{
3469 struct ppc_link_hash_table *htab;
3470 flagword flags;
3471
3472 htab = ppc_hash_table (info);
3473
bfeb4a28
AM
3474 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3475 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3476 if (htab->params->save_restore_funcs)
3477 {
3478 /* Create .sfpr for code to save and restore fp regs. */
3479 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3480 flags);
3481 if (htab->sfpr == NULL
2cdcc330 3482 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b32547cd
AM
3483 return FALSE;
3484 }
3485
3486 if (bfd_link_relocatable (info))
3487 return TRUE;
bfeb4a28
AM
3488
3489 /* Create .glink for lazy dynamic linking support. */
3490 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3491 flags);
3492 if (htab->glink == NULL
2cdcc330 3493 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
bfeb4a28
AM
3494 return FALSE;
3495
9e390558
AM
3496 /* The part of .glink used by global entry stubs, separate so that
3497 it can be aligned appropriately without affecting htab->glink. */
3498 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3499 flags);
3500 if (htab->global_entry == NULL
2cdcc330 3501 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
9e390558
AM
3502 return FALSE;
3503
bfeb4a28
AM
3504 if (!info->no_ld_generated_unwind_info)
3505 {
3506 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3507 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3508 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3509 ".eh_frame",
3510 flags);
3511 if (htab->glink_eh_frame == NULL
3512 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3513 return FALSE;
3514 }
3515
3516 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3517 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3518 if (htab->elf.iplt == NULL
2cdcc330 3519 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
3520 return FALSE;
3521
3522 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3523 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3524 htab->elf.irelplt
3525 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3526 if (htab->elf.irelplt == NULL
2cdcc330 3527 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
3528 return FALSE;
3529
3530 /* Create branch lookup table for plt_branch stubs. */
3531 flags = (SEC_ALLOC | SEC_LOAD
3532 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3533 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3534 flags);
3535 if (htab->brlt == NULL
2cdcc330 3536 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
bfeb4a28
AM
3537 return FALSE;
3538
2d7ad24e
AM
3539 /* Local plt entries, put in .branch_lt but a separate section for
3540 convenience. */
3541 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3542 flags);
3543 if (htab->pltlocal == NULL
2cdcc330 3544 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
2d7ad24e
AM
3545 return FALSE;
3546
0e1862bb 3547 if (!bfd_link_pic (info))
bfeb4a28
AM
3548 return TRUE;
3549
3550 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3551 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3552 htab->relbrlt
3553 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3554 if (htab->relbrlt == NULL
2cdcc330 3555 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
bfeb4a28
AM
3556 return FALSE;
3557
2d7ad24e
AM
3558 htab->relpltlocal
3559 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3560 if (htab->relpltlocal == NULL
2cdcc330 3561 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
2d7ad24e
AM
3562 return FALSE;
3563
bfeb4a28
AM
3564 return TRUE;
3565}
3566
e717da7e
AM
3567/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3568
bfeb4a28 3569bfd_boolean
e7d1c40c
AM
3570ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3571 struct ppc64_elf_params *params)
e717da7e
AM
3572{
3573 struct ppc_link_hash_table *htab;
3574
e7d1c40c 3575 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3576
3577/* Always hook our dynamic sections into the first bfd, which is the
3578 linker created stub bfd. This ensures that the GOT header is at
3579 the start of the output TOC section. */
3580 htab = ppc_hash_table (info);
e7d1c40c
AM
3581 htab->elf.dynobj = params->stub_bfd;
3582 htab->params = params;
bfeb4a28 3583
bfeb4a28 3584 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3585}
3586
721956f4
AM
3587/* Build a name for an entry in the stub hash table. */
3588
3589static char *
4ce794b7
AM
3590ppc_stub_name (const asection *input_section,
3591 const asection *sym_sec,
3592 const struct ppc_link_hash_entry *h,
3593 const Elf_Internal_Rela *rel)
721956f4
AM
3594{
3595 char *stub_name;
bcaa2f82 3596 ssize_t len;
721956f4
AM
3597
3598 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3599 offsets from a sym as a branch target? In fact, we could
3600 probably assume the addend is always zero. */
3601 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3602
3603 if (h)
3604 {
3605 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3606 stub_name = bfd_malloc (len);
46de2a7c
AM
3607 if (stub_name == NULL)
3608 return stub_name;
3609
bcaa2f82
AM
3610 len = sprintf (stub_name, "%08x.%s+%x",
3611 input_section->id & 0xffffffff,
3612 h->elf.root.root.string,
3613 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3614 }
3615 else
3616 {
ad8e1ba5 3617 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3618 stub_name = bfd_malloc (len);
46de2a7c
AM
3619 if (stub_name == NULL)
3620 return stub_name;
3621
bcaa2f82
AM
3622 len = sprintf (stub_name, "%08x.%x:%x+%x",
3623 input_section->id & 0xffffffff,
3624 sym_sec->id & 0xffffffff,
3625 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3626 (int) rel->r_addend & 0xffffffff);
721956f4 3627 }
bcaa2f82 3628 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3629 stub_name[len - 2] = 0;
721956f4
AM
3630 return stub_name;
3631}
3632
3633/* Look up an entry in the stub hash. Stub entries are cached because
3634 creating the stub name takes a bit of time. */
3635
3636static struct ppc_stub_hash_entry *
4ce794b7
AM
3637ppc_get_stub_entry (const asection *input_section,
3638 const asection *sym_sec,
039b3fef 3639 struct ppc_link_hash_entry *h,
4ce794b7
AM
3640 const Elf_Internal_Rela *rel,
3641 struct ppc_link_hash_table *htab)
721956f4
AM
3642{
3643 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3644 struct map_stub *group;
721956f4
AM
3645
3646 /* If this input section is part of a group of sections sharing one
3647 stub section, then use the id of the first section in the group.
3648 Stub names need to include a section id, as there may well be
3649 more than one stub used to reach say, printf, and we need to
3650 distinguish between them. */
6f20ed8a 3651 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3652 if (group == NULL)
3653 return NULL;
721956f4 3654
b3fac117
AM
3655 if (h != NULL && h->u.stub_cache != NULL
3656 && h->u.stub_cache->h == h
6f20ed8a 3657 && h->u.stub_cache->group == group)
721956f4 3658 {
b3fac117 3659 stub_entry = h->u.stub_cache;
721956f4
AM
3660 }
3661 else
3662 {
3663 char *stub_name;
3664
6f20ed8a 3665 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3666 if (stub_name == NULL)
3667 return NULL;
3668
3669 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3670 stub_name, FALSE, FALSE);
721956f4 3671 if (h != NULL)
b3fac117 3672 h->u.stub_cache = stub_entry;
721956f4
AM
3673
3674 free (stub_name);
3675 }
3676
3677 return stub_entry;
3678}
3679
3680/* Add a new stub entry to the stub hash. Not all fields of the new
3681 stub entry are initialised. */
3682
3683static struct ppc_stub_hash_entry *
4ce794b7
AM
3684ppc_add_stub (const char *stub_name,
3685 asection *section,
25f53a85 3686 struct bfd_link_info *info)
721956f4 3687{
25f53a85 3688 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3689 struct map_stub *group;
721956f4
AM
3690 asection *link_sec;
3691 asection *stub_sec;
3692 struct ppc_stub_hash_entry *stub_entry;
3693
6f20ed8a
AM
3694 group = htab->sec_info[section->id].u.group;
3695 link_sec = group->link_sec;
3696 stub_sec = group->stub_sec;
721956f4
AM
3697 if (stub_sec == NULL)
3698 {
6f20ed8a
AM
3699 size_t namelen;
3700 bfd_size_type len;
3701 char *s_name;
721956f4 3702
6f20ed8a
AM
3703 namelen = strlen (link_sec->name);
3704 len = namelen + sizeof (STUB_SUFFIX);
3705 s_name = bfd_alloc (htab->params->stub_bfd, len);
3706 if (s_name == NULL)
3707 return NULL;
721956f4 3708
6f20ed8a
AM
3709 memcpy (s_name, link_sec->name, namelen);
3710 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3711 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3712 if (stub_sec == NULL)
3713 return NULL;
3714 group->stub_sec = stub_sec;
721956f4
AM
3715 }
3716
3717 /* Enter this entry into the linker stub hash table. */
3718 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3719 TRUE, FALSE);
721956f4
AM
3720 if (stub_entry == NULL)
3721 {
695344c0 3722 /* xgettext:c-format */
cf97bcb0
AM
3723 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3724 section->owner, stub_name);
721956f4
AM
3725 return NULL;
3726 }
3727
6f20ed8a 3728 stub_entry->group = group;
721956f4 3729 stub_entry->stub_offset = 0;
721956f4
AM
3730 return stub_entry;
3731}
3732
e717da7e
AM
3733/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3734 not already done. */
65f38f15 3735
b34976b6 3736static bfd_boolean
e717da7e 3737create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3738{
e717da7e
AM
3739 asection *got, *relgot;
3740 flagword flags;
3741 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3742
0c8d6e5c 3743 if (!is_ppc64_elf (abfd))
0ffa91dd 3744 return FALSE;
4dfe6ac6
NC
3745 if (htab == NULL)
3746 return FALSE;
0ffa91dd 3747
33e44f2e
AM
3748 if (!htab->elf.sgot
3749 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3750 return FALSE;
e717da7e
AM
3751
3752 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3753 | SEC_LINKER_CREATED);
3754
c456f082 3755 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3756 if (!got
e717da7e
AM
3757 || !bfd_set_section_alignment (abfd, got, 3))
3758 return FALSE;
65f38f15 3759
c456f082
AM
3760 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3761 flags | SEC_READONLY);
e717da7e 3762 if (!relgot
2cdcc330 3763 || !bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 3764 return FALSE;
e717da7e
AM
3765
3766 ppc64_elf_tdata (abfd)->got = got;
3767 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 3768 return TRUE;
65f38f15 3769}
5bd4f169 3770
b31867b6
AM
3771/* Follow indirect and warning symbol links. */
3772
3773static inline struct bfd_link_hash_entry *
3774follow_link (struct bfd_link_hash_entry *h)
3775{
3776 while (h->type == bfd_link_hash_indirect
3777 || h->type == bfd_link_hash_warning)
3778 h = h->u.i.link;
3779 return h;
3780}
3781
3782static inline struct elf_link_hash_entry *
3783elf_follow_link (struct elf_link_hash_entry *h)
3784{
3785 return (struct elf_link_hash_entry *) follow_link (&h->root);
3786}
3787
3788static inline struct ppc_link_hash_entry *
3789ppc_follow_link (struct ppc_link_hash_entry *h)
3790{
3791 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3792}
3793
40d16e0b
AM
3794/* Merge PLT info on FROM with that on TO. */
3795
3796static void
3797move_plt_plist (struct ppc_link_hash_entry *from,
3798 struct ppc_link_hash_entry *to)
3799{
3800 if (from->elf.plt.plist != NULL)
3801 {
3802 if (to->elf.plt.plist != NULL)
3803 {
3804 struct plt_entry **entp;
3805 struct plt_entry *ent;
3806
3807 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3808 {
3809 struct plt_entry *dent;
3810
3811 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3812 if (dent->addend == ent->addend)
3813 {
3814 dent->plt.refcount += ent->plt.refcount;
3815 *entp = ent->next;
3816 break;
3817 }
3818 if (dent == NULL)
3819 entp = &ent->next;
3820 }
3821 *entp = to->elf.plt.plist;
3822 }
3823
3824 to->elf.plt.plist = from->elf.plt.plist;
3825 from->elf.plt.plist = NULL;
3826 }
3827}
3828
65f38f15
AM
3829/* Copy the extra info we tack onto an elf_link_hash_entry. */
3830
3831static void
fcfa13d2
AM
3832ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3833 struct elf_link_hash_entry *dir,
3834 struct elf_link_hash_entry *ind)
65f38f15
AM
3835{
3836 struct ppc_link_hash_entry *edir, *eind;
3837
3838 edir = (struct ppc_link_hash_entry *) dir;
3839 eind = (struct ppc_link_hash_entry *) ind;
3840
c79d6685
AM
3841 edir->is_func |= eind->is_func;
3842 edir->is_func_descriptor |= eind->is_func_descriptor;
3843 edir->tls_mask |= eind->tls_mask;
3844 if (eind->oh != NULL)
3845 edir->oh = ppc_follow_link (eind->oh);
3846
474436e6 3847 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
3848 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3849 edir->elf.ref_regular |= eind->elf.ref_regular;
3850 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 3851 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
3852 edir->elf.needs_plt |= eind->elf.needs_plt;
3853 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 3854
d311bc8b
AM
3855 /* If we were called to copy over info for a weak sym, don't copy
3856 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3857 in order to simplify readonly_dynrelocs and save a field in the
3858 symbol hash entry, but that means dyn_relocs can't be used in any
3859 tests about a specific symbol, or affect other symbol flags which
ab2477e1 3860 are then tested. */
d311bc8b 3861 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 3862 return;
d311bc8b 3863
411e1bfb 3864 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 3865 if (eind->dyn_relocs != NULL)
65f38f15 3866 {
bbd7ec4a
AM
3867 if (edir->dyn_relocs != NULL)
3868 {
6061a67d
AM
3869 struct elf_dyn_relocs **pp;
3870 struct elf_dyn_relocs *p;
bbd7ec4a 3871
fcfa13d2 3872 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
3873 list. Merge any entries against the same section. */
3874 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3875 {
6061a67d 3876 struct elf_dyn_relocs *q;
bbd7ec4a
AM
3877
3878 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3879 if (q->sec == p->sec)
3880 {
3881 q->pc_count += p->pc_count;
3882 q->count += p->count;
3883 *pp = p->next;
3884 break;
3885 }
3886 if (q == NULL)
3887 pp = &p->next;
3888 }
3889 *pp = edir->dyn_relocs;
3890 }
3891
65f38f15
AM
3892 edir->dyn_relocs = eind->dyn_relocs;
3893 eind->dyn_relocs = NULL;
3894 }
65f38f15 3895
81848ca0
AM
3896 /* Copy over got entries that we may have already seen to the
3897 symbol which just became indirect. */
411e1bfb
AM
3898 if (eind->elf.got.glist != NULL)
3899 {
3900 if (edir->elf.got.glist != NULL)
3901 {
3902 struct got_entry **entp;
3903 struct got_entry *ent;
3904
3905 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3906 {
3907 struct got_entry *dent;
3908
3909 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3910 if (dent->addend == ent->addend
e717da7e 3911 && dent->owner == ent->owner
411e1bfb
AM
3912 && dent->tls_type == ent->tls_type)
3913 {
3914 dent->got.refcount += ent->got.refcount;
3915 *entp = ent->next;
3916 break;
3917 }
3918 if (dent == NULL)
3919 entp = &ent->next;
3920 }
3921 *entp = edir->elf.got.glist;
3922 }
3923
3924 edir->elf.got.glist = eind->elf.got.glist;
3925 eind->elf.got.glist = NULL;
3926 }
3927
3928 /* And plt entries. */
40d16e0b 3929 move_plt_plist (eind, edir);
411e1bfb 3930
fcfa13d2 3931 if (eind->elf.dynindx != -1)
411e1bfb 3932 {
fcfa13d2
AM
3933 if (edir->elf.dynindx != -1)
3934 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3935 edir->elf.dynstr_index);
411e1bfb
AM
3936 edir->elf.dynindx = eind->elf.dynindx;
3937 edir->elf.dynstr_index = eind->elf.dynstr_index;
3938 eind->elf.dynindx = -1;
3939 eind->elf.dynstr_index = 0;
3940 }
411e1bfb
AM
3941}
3942
8387904d
AM
3943/* Find the function descriptor hash entry from the given function code
3944 hash entry FH. Link the entries via their OH fields. */
3945
3946static struct ppc_link_hash_entry *
b31867b6 3947lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
3948{
3949 struct ppc_link_hash_entry *fdh = fh->oh;
3950
3951 if (fdh == NULL)
3952 {
3953 const char *fd_name = fh->elf.root.root.string + 1;
3954
3955 fdh = (struct ppc_link_hash_entry *)
3956 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
3957 if (fdh == NULL)
3958 return fdh;
3959
3960 fdh->is_func_descriptor = 1;
3961 fdh->oh = fh;
3962 fh->is_func = 1;
3963 fh->oh = fdh;
8387904d
AM
3964 }
3965
8c5b4e52
AM
3966 fdh = ppc_follow_link (fdh);
3967 fdh->is_func_descriptor = 1;
3968 fdh->oh = fh;
3969 return fdh;
8387904d
AM
3970}
3971
8c5b4e52 3972/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
3973
3974static struct ppc_link_hash_entry *
3975make_fdh (struct bfd_link_info *info,
908b32fc 3976 struct ppc_link_hash_entry *fh)
bb700d78 3977{
8c5b4e52
AM
3978 bfd *abfd = fh->elf.root.u.undef.abfd;
3979 struct bfd_link_hash_entry *bh = NULL;
bb700d78 3980 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
3981 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3982 ? BSF_WEAK
3983 : BSF_GLOBAL);
3984
3985 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3986 fh->elf.root.root.string + 1,
3987 flags, bfd_und_section_ptr, 0,
3988 NULL, FALSE, FALSE, &bh))
bb700d78
AM
3989 return NULL;
3990
3991 fdh = (struct ppc_link_hash_entry *) bh;
3992 fdh->elf.non_elf = 0;
908b32fc
AM
3993 fdh->fake = 1;
3994 fdh->is_func_descriptor = 1;
3995 fdh->oh = fh;
3996 fh->is_func = 1;
3997 fh->oh = fdh;
bb700d78
AM
3998 return fdh;
3999}
4000
8387904d
AM
4001/* Fix function descriptor symbols defined in .opd sections to be
4002 function type. */
555cd476
AM
4003
4004static bfd_boolean
c16153ae 4005ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4006 struct bfd_link_info *info,
555cd476 4007 Elf_Internal_Sym *isym,
6911b7dc 4008 const char **name,
555cd476
AM
4009 flagword *flags ATTRIBUTE_UNUSED,
4010 asection **sec,
b53dfeb2 4011 bfd_vma *value)
555cd476 4012{
b53dfeb2 4013 if (*sec != NULL
f1885d1e 4014 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4015 {
4016 asection *code_sec;
4017
4018 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4019 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4020 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4021
4022 /* If the symbol is a function defined in .opd, and the function
4023 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4024 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4025 && (*sec)->reloc_count != 0
4026 && opd_entry_value (*sec, *value, &code_sec, NULL,
4027 FALSE) != (bfd_vma) -1
4028 && discarded_section (code_sec))
4029 {
4030 *sec = bfd_und_section_ptr;
4031 isym->st_shndx = SHN_UNDEF;
4032 }
4033 }
dbd1e97e
AM
4034 else if (*sec != NULL
4035 && strcmp ((*sec)->name, ".toc") == 0
4036 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4037 {
4038 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4039 if (htab != NULL)
4040 htab->params->object_in_toc = 1;
4041 }
433817dd 4042
6911b7dc
AM
4043 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4044 {
4045 if (abiversion (ibfd) == 0)
4046 set_abiversion (ibfd, 2);
4047 else if (abiversion (ibfd) == 1)
4048 {
cf97bcb0
AM
4049 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4050 " for ABI version 1"), *name);
6911b7dc
AM
4051 bfd_set_error (bfd_error_bad_value);
4052 return FALSE;
4053 }
4054 }
4055
555cd476
AM
4056 return TRUE;
4057}
4058
6911b7dc
AM
4059/* Merge non-visibility st_other attributes: local entry point. */
4060
4061static void
4062ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4063 const Elf_Internal_Sym *isym,
4064 bfd_boolean definition,
4065 bfd_boolean dynamic)
4066{
f378ab09 4067 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
4068 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4069 | ELF_ST_VISIBILITY (h->other));
4070}
4071
8c5b4e52
AM
4072/* Hook called on merging a symbol. We use this to clear "fake" since
4073 we now have a real symbol. */
4074
4075static bfd_boolean
4076ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4077 const Elf_Internal_Sym *isym,
8c5b4e52
AM
4078 asection **psec ATTRIBUTE_UNUSED,
4079 bfd_boolean newdef ATTRIBUTE_UNUSED,
4080 bfd_boolean olddef ATTRIBUTE_UNUSED,
4081 bfd *oldbfd ATTRIBUTE_UNUSED,
4082 const asection *oldsec ATTRIBUTE_UNUSED)
4083{
4084 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
4085 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4086 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
4087 return TRUE;
4088}
4089
8387904d 4090/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4091 inclusion of a new ABI object archive that defines "bar".
4092 NAME is a symbol defined in an archive. Return a symbol in the hash
4093 table that might be satisfied by the archive symbols. */
8387904d
AM
4094
4095static struct elf_link_hash_entry *
4096ppc64_elf_archive_symbol_lookup (bfd *abfd,
4097 struct bfd_link_info *info,
4098 const char *name)
4099{
4100 struct elf_link_hash_entry *h;
4101 char *dot_name;
4102 size_t len;
4103
4104 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4105 if (h != NULL
4106 /* Don't return this sym if it is a fake function descriptor
4107 created by add_symbol_adjust. */
8c5b4e52 4108 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
4109 return h;
4110
4111 if (name[0] == '.')
4112 return h;
4113
4114 len = strlen (name);
4115 dot_name = bfd_alloc (abfd, len + 2);
4116 if (dot_name == NULL)
e99955cd 4117 return (struct elf_link_hash_entry *) -1;
8387904d
AM
4118 dot_name[0] = '.';
4119 memcpy (dot_name + 1, name, len + 1);
4120 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4121 bfd_release (abfd, dot_name);
4122 return h;
4123}
4124
4125/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4126 new ABI object defines "bar". Well, at least, undefined dot symbols
4127 are made weak. This stops later archive searches from including an
4128 object if we already have a function descriptor definition. It also
35b0ce59
AM
4129 prevents the linker complaining about undefined symbols.
4130 We also check and correct mismatched symbol visibility here. The
4131 most restrictive visibility of the function descriptor and the
4132 function entry symbol is used. */
8387904d
AM
4133
4134static bfd_boolean
b3fac117 4135add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4136{
8387904d
AM
4137 struct ppc_link_hash_table *htab;
4138 struct ppc_link_hash_entry *fdh;
4139
b3fac117
AM
4140 if (eh->elf.root.type == bfd_link_hash_warning)
4141 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4142
8400d40d
AM
4143 if (eh->elf.root.type == bfd_link_hash_indirect)
4144 return TRUE;
4145
b3fac117
AM
4146 if (eh->elf.root.root.string[0] != '.')
4147 abort ();
8387904d 4148
b3fac117 4149 htab = ppc_hash_table (info);
4dfe6ac6
NC
4150 if (htab == NULL)
4151 return FALSE;
4152
b31867b6 4153 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4154 if (fdh == NULL
4155 && !bfd_link_relocatable (info)
4156 && (eh->elf.root.type == bfd_link_hash_undefined
4157 || eh->elf.root.type == bfd_link_hash_undefweak)
4158 && eh->elf.ref_regular)
4159 {
4160 /* Make an undefined function descriptor sym, in order to
4161 pull in an --as-needed shared lib. Archives are handled
4162 elsewhere. */
4163 fdh = make_fdh (info, eh);
4164 if (fdh == NULL)
4165 return FALSE;
bb700d78 4166 }
8c5b4e52
AM
4167
4168 if (fdh != NULL)
8387904d 4169 {
35b0ce59
AM
4170 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4171 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4172
4173 /* Make both descriptor and entry symbol have the most
4174 constraining visibility of either symbol. */
35b0ce59
AM
4175 if (entry_vis < descr_vis)
4176 fdh->elf.other += entry_vis - descr_vis;
4177 else if (entry_vis > descr_vis)
4178 eh->elf.other += descr_vis - entry_vis;
4179
8c5b4e52
AM
4180 /* Propagate reference flags from entry symbol to function
4181 descriptor symbol. */
bc4e12de 4182 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4183 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4184 fdh->elf.ref_regular |= eh->elf.ref_regular;
4185 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4186
4187 if (!fdh->elf.forced_local
4188 && fdh->elf.dynindx == -1
4189 && fdh->elf.versioned != versioned_hidden
4190 && (bfd_link_dll (info)
4191 || fdh->elf.def_dynamic
4192 || fdh->elf.ref_dynamic)
4193 && (eh->elf.ref_regular
4194 || eh->elf.def_regular))
4195 {
2cdcc330 4196 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
8c5b4e52 4197 return FALSE;
35b0ce59 4198 }
8387904d 4199 }
99877b66 4200
8387904d
AM
4201 return TRUE;
4202}
4203
f6c7c3e8
AM
4204/* Set up opd section info and abiversion for IBFD, and process list
4205 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4206
8387904d 4207static bfd_boolean
f6c7c3e8 4208ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4209{
99877b66 4210 struct ppc_link_hash_table *htab;
b3fac117 4211 struct ppc_link_hash_entry **p, *eh;
459609d6 4212 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4213
459609d6 4214 if (opd != NULL && opd->size != 0)
b3fac117 4215 {
b9399fcf
AM
4216 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4217 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4218
459609d6
AM
4219 if (abiversion (ibfd) == 0)
4220 set_abiversion (ibfd, 1);
8a2058b5 4221 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4222 {
695344c0 4223 /* xgettext:c-format */
cf97bcb0
AM
4224 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4225 ibfd, abiversion (ibfd));
459609d6
AM
4226 bfd_set_error (bfd_error_bad_value);
4227 return FALSE;
f6c7c3e8 4228 }
b9399fcf 4229 }
f6c7c3e8 4230
b9399fcf
AM
4231 if (is_ppc64_elf (info->output_bfd))
4232 {
4233 /* For input files without an explicit abiversion in e_flags
4234 we should have flagged any with symbol st_other bits set
4235 as ELFv1 and above flagged those with .opd as ELFv2.
4236 Set the output abiversion if not yet set, and for any input
4237 still ambiguous, take its abiversion from the output.
4238 Differences in ABI are reported later. */
4239 if (abiversion (info->output_bfd) == 0)
4240 set_abiversion (info->output_bfd, abiversion (ibfd));
4241 else if (abiversion (ibfd) == 0)
4242 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4243 }
4244
459609d6
AM
4245 htab = ppc_hash_table (info);
4246 if (htab == NULL)
b9399fcf 4247 return TRUE;
459609d6 4248
b9399fcf
AM
4249 if (opd != NULL && opd->size != 0
4250 && (ibfd->flags & DYNAMIC) == 0
4251 && (opd->flags & SEC_RELOC) != 0
4252 && opd->reloc_count != 0
4253 && !bfd_is_abs_section (opd->output_section)
4254 && info->gc_sections)
4255 {
4256 /* Garbage collection needs some extra help with .opd sections.
4257 We don't want to necessarily keep everything referenced by
4258 relocs in .opd, as that would keep all functions. Instead,
4259 if we reference an .opd symbol (a function descriptor), we
4260 want to keep the function code symbol's section. This is
4261 easy for global symbols, but for local syms we need to keep
4262 information about the associated function section. */
4263 bfd_size_type amt;
4264 asection **opd_sym_map;
4265 Elf_Internal_Shdr *symtab_hdr;
4266 Elf_Internal_Rela *relocs, *rel_end, *rel;
4267
4268 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4269 opd_sym_map = bfd_zalloc (ibfd, amt);
4270 if (opd_sym_map == NULL)
4271 return FALSE;
4272 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4273 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4274 info->keep_memory);
4275 if (relocs == NULL)
4276 return FALSE;
4277 symtab_hdr = &elf_symtab_hdr (ibfd);
4278 rel_end = relocs + opd->reloc_count - 1;
4279 for (rel = relocs; rel < rel_end; rel++)
4280 {
4281 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4282 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4283
4284 if (r_type == R_PPC64_ADDR64
4285 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4286 && r_symndx < symtab_hdr->sh_info)
4287 {
4288 Elf_Internal_Sym *isym;
4289 asection *s;
4290
4291 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4292 if (isym == NULL)
4293 {
4294 if (elf_section_data (opd)->relocs != relocs)
4295 free (relocs);
4296 return FALSE;
4297 }
4298
4299 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4300 if (s != NULL && s != opd)
4301 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4302 }
4303 }
4304 if (elf_section_data (opd)->relocs != relocs)
4305 free (relocs);
4306 }
459609d6
AM
4307
4308 p = &htab->dot_syms;
4309 while ((eh = *p) != NULL)
4310 {
4311 *p = NULL;
4312 if (&eh->elf == htab->elf.hgot)
4313 ;
4314 else if (htab->elf.hgot == NULL
4315 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4316 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4317 else if (abiversion (ibfd) <= 1)
4318 {
4319 htab->need_func_desc_adj = 1;
4320 if (!add_symbol_adjust (eh, info))
4321 return FALSE;
4322 }
459609d6
AM
4323 p = &eh->u.next_dot_sym;
4324 }
b3fac117 4325 return TRUE;
8387904d
AM
4326}
4327
97fed1c9
JJ
4328/* Undo hash table changes when an --as-needed input file is determined
4329 not to be needed. */
4330
4331static bfd_boolean
e5034e59
AM
4332ppc64_elf_notice_as_needed (bfd *ibfd,
4333 struct bfd_link_info *info,
4334 enum notice_asneeded_action act)
97fed1c9 4335{
e5034e59
AM
4336 if (act == notice_not_needed)
4337 {
4338 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4339
e5034e59
AM
4340 if (htab == NULL)
4341 return FALSE;
4dfe6ac6 4342
e5034e59
AM
4343 htab->dot_syms = NULL;
4344 }
4345 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4346}
4347
aa374f67
AM
4348/* If --just-symbols against a final linked binary, then assume we need
4349 toc adjusting stubs when calling functions defined there. */
4350
4351static void
4352ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4353{
4354 if ((sec->flags & SEC_CODE) != 0
4355 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4356 && is_ppc64_elf (sec->owner))
4357 {
2c3f079f
AM
4358 if (abiversion (sec->owner) >= 2
4359 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4360 sec->has_toc_reloc = 1;
4361 }
4362 _bfd_elf_link_just_syms (sec, info);
4363}
4364
e054468f 4365static struct plt_entry **
4ce794b7
AM
4366update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4367 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4368{
4369 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4370 struct plt_entry **local_plt;
f961d9dd 4371 unsigned char *local_got_tls_masks;
411e1bfb
AM
4372
4373 if (local_got_ents == NULL)
4374 {
4375 bfd_size_type size = symtab_hdr->sh_info;
4376
e054468f
AM
4377 size *= (sizeof (*local_got_ents)
4378 + sizeof (*local_plt)
4379 + sizeof (*local_got_tls_masks));
4ce794b7 4380 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4381 if (local_got_ents == NULL)
e054468f 4382 return NULL;
411e1bfb
AM
4383 elf_local_got_ents (abfd) = local_got_ents;
4384 }
4385
37da22e5 4386 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4387 {
4388 struct got_entry *ent;
4389
4390 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4391 if (ent->addend == r_addend
4392 && ent->owner == abfd
4393 && ent->tls_type == tls_type)
411e1bfb
AM
4394 break;
4395 if (ent == NULL)
4396 {
4397 bfd_size_type amt = sizeof (*ent);
4ce794b7 4398 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4399 if (ent == NULL)
4400 return FALSE;
4401 ent->next = local_got_ents[r_symndx];
4402 ent->addend = r_addend;
e717da7e 4403 ent->owner = abfd;
411e1bfb 4404 ent->tls_type = tls_type;
927be08e 4405 ent->is_indirect = FALSE;
411e1bfb
AM
4406 ent->got.refcount = 0;
4407 local_got_ents[r_symndx] = ent;
4408 }
4409 ent->got.refcount += 1;
4410 }
4411
e054468f 4412 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4413 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4414 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4415
4416 return local_plt + r_symndx;
65f38f15
AM
4417}
4418
411e1bfb 4419static bfd_boolean
e054468f 4420update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4421{
411e1bfb 4422 struct plt_entry *ent;
1e2f5b6e 4423
e054468f 4424 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4425 if (ent->addend == addend)
4426 break;
4427 if (ent == NULL)
1e2f5b6e 4428 {
411e1bfb 4429 bfd_size_type amt = sizeof (*ent);
4ce794b7 4430 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4431 if (ent == NULL)
4432 return FALSE;
e054468f 4433 ent->next = *plist;
411e1bfb
AM
4434 ent->addend = addend;
4435 ent->plt.refcount = 0;
e054468f 4436 *plist = ent;
1e2f5b6e 4437 }
411e1bfb 4438 ent->plt.refcount += 1;
b34976b6 4439 return TRUE;
1e2f5b6e
AM
4440}
4441
e054468f
AM
4442static bfd_boolean
4443is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4444{
4445 return (r_type == R_PPC64_REL24
05d0e962 4446 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
4447 || r_type == R_PPC64_REL14
4448 || r_type == R_PPC64_REL14_BRTAKEN
4449 || r_type == R_PPC64_REL14_BRNTAKEN
4450 || r_type == R_PPC64_ADDR24
4451 || r_type == R_PPC64_ADDR14
4452 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4453 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4454 || r_type == R_PPC64_PLTCALL
4455 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4456}
4457
4458/* Relocs on inline plt call sequence insns prior to the call. */
4459
4460static bfd_boolean
4461is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4462{
4463 return (r_type == R_PPC64_PLT16_HA
4464 || r_type == R_PPC64_PLT16_HI
4465 || r_type == R_PPC64_PLT16_LO
4466 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4467 || r_type == R_PPC64_PLT_PCREL34
4468 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4469 || r_type == R_PPC64_PLTSEQ
4470 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4471}
4472
5bd4f169 4473/* Look through the relocs for a section during the first phase, and
65f38f15 4474 calculate needed space in the global offset table, procedure
5d1634d7 4475 linkage table, and dynamic reloc sections. */
5bd4f169 4476
b34976b6 4477static bfd_boolean
4ce794b7
AM
4478ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4479 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4480{
65f38f15 4481 struct ppc_link_hash_table *htab;
5bd4f169 4482 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4483 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4484 const Elf_Internal_Rela *rel;
4485 const Elf_Internal_Rela *rel_end;
5bd4f169 4486 asection *sreloc;
3a71aa26 4487 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 4488 bfd_boolean is_opd;
5bd4f169 4489
0e1862bb 4490 if (bfd_link_relocatable (info))
b34976b6 4491 return TRUE;
5bd4f169 4492
680a3378
AM
4493 /* Don't do anything special with non-loaded, non-alloced sections.
4494 In particular, any relocs in such sections should not affect GOT
4495 and PLT reference counting (ie. we don't allow them to create GOT
4496 or PLT entries), there's no possibility or desire to optimize TLS
4497 relocs, and there's not much point in propagating relocs to shared
4498 libs that the dynamic linker won't relocate. */
4499 if ((sec->flags & SEC_ALLOC) == 0)
4500 return TRUE;
4501
0c8d6e5c 4502 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4503
65f38f15 4504 htab = ppc_hash_table (info);
4dfe6ac6
NC
4505 if (htab == NULL)
4506 return FALSE;
4507
3a71aa26
AM
4508 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4509 FALSE, FALSE, TRUE);
4510 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4511 FALSE, FALSE, TRUE);
0ffa91dd 4512 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4513 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4514 sreloc = NULL;
b9399fcf 4515 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4516 rel_end = relocs + sec->reloc_count;
4517 for (rel = relocs; rel < rel_end; rel++)
4518 {
4519 unsigned long r_symndx;
4520 struct elf_link_hash_entry *h;
04c9666a 4521 enum elf_ppc64_reloc_type r_type;
727fc41e 4522 int tls_type;
7c8fe5c4 4523 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4524 struct plt_entry **ifunc, **plt_list;
5663e321 4525 bfd_vma sym_addend;
5bd4f169
AM
4526
4527 r_symndx = ELF64_R_SYM (rel->r_info);
4528 if (r_symndx < symtab_hdr->sh_info)
4529 h = NULL;
4530 else
973a3492
L
4531 {
4532 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4533 h = elf_follow_link (h);
1c865ab2
AM
4534
4535 if (h == htab->elf.hgot)
4536 sec->has_toc_reloc = 1;
973a3492 4537 }
5bd4f169 4538
727fc41e 4539 tls_type = 0;
e054468f 4540 ifunc = NULL;
5663e321
AM
4541 r_type = ELF64_R_TYPE (rel->r_info);
4542 switch (r_type)
4543 {
04bdff6a
AM
4544 case R_PPC64_D34:
4545 case R_PPC64_D34_LO:
4546 case R_PPC64_D34_HI30:
4547 case R_PPC64_D34_HA30:
4548 case R_PPC64_D28:
c213164a
AM
4549 case R_PPC64_TPREL34:
4550 case R_PPC64_DTPREL34:
04bdff6a
AM
4551 htab->powerxx_stubs = 1;
4552 /* Fall through. */
5663e321
AM
4553 default:
4554 /* Somewhat foolishly, because the ABIs don't specifically
4555 allow it, ppc64 gas and ld support GOT and PLT relocs
4556 with non-zero addends where the addend results in
4557 sym+addend being stored in the GOT or PLT entry. This
4558 can't be supported for pcrel relocs because the addend is
4559 used to specify the pcrel offset. */
4560 sym_addend = rel->r_addend;
4561 break;
04bdff6a
AM
4562
4563 case R_PPC64_PCREL34:
5663e321 4564 case R_PPC64_GOT_PCREL34:
c213164a
AM
4565 case R_PPC64_GOT_TLSGD34:
4566 case R_PPC64_GOT_TLSLD34:
4567 case R_PPC64_GOT_TPREL34:
4568 case R_PPC64_GOT_DTPREL34:
5663e321
AM
4569 case R_PPC64_PLT_PCREL34:
4570 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a
AM
4571 case R_PPC64_PCREL28:
4572 htab->powerxx_stubs = 1;
5663e321
AM
4573 sym_addend = 0;
4574 break;
4575 }
25f23106
AM
4576 if (h != NULL)
4577 {
4578 if (h->type == STT_GNU_IFUNC)
4579 {
4580 h->needs_plt = 1;
4581 ifunc = &h->plt.plist;
4582 }
4583 }
4584 else
4585 {
4586 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4587 abfd, r_symndx);
4588 if (isym == NULL)
4589 return FALSE;
4590
4591 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4592 {
4593 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4594 sym_addend,
37da22e5 4595 NON_GOT | PLT_IFUNC);
25f23106
AM
4596 if (ifunc == NULL)
4597 return FALSE;
4598 }
4599 }
727fc41e 4600
a33d1f77 4601 switch (r_type)
5bd4f169 4602 {
727fc41e
AM
4603 case R_PPC64_TLSGD:
4604 case R_PPC64_TLSLD:
4605 /* These special tls relocs tie a call to __tls_get_addr with
4606 its parameter symbol. */
37da22e5
AM
4607 if (h != NULL)
4608 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4609 else
4610 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4611 sym_addend,
37da22e5
AM
4612 NON_GOT | TLS_TLS | TLS_MARK))
4613 return FALSE;
4614 sec->has_tls_reloc = 1;
727fc41e
AM
4615 break;
4616
411e1bfb
AM
4617 case R_PPC64_GOT_TLSLD16:
4618 case R_PPC64_GOT_TLSLD16_LO:
4619 case R_PPC64_GOT_TLSLD16_HI:
4620 case R_PPC64_GOT_TLSLD16_HA:
c213164a 4621 case R_PPC64_GOT_TLSLD34:
951fd09b 4622 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4623 goto dogottls;
4624
4625 case R_PPC64_GOT_TLSGD16:
4626 case R_PPC64_GOT_TLSGD16_LO:
4627 case R_PPC64_GOT_TLSGD16_HI:
4628 case R_PPC64_GOT_TLSGD16_HA:
c213164a 4629 case R_PPC64_GOT_TLSGD34:
951fd09b 4630 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4631 goto dogottls;
4632
4633 case R_PPC64_GOT_TPREL16_DS:
4634 case R_PPC64_GOT_TPREL16_LO_DS:
4635 case R_PPC64_GOT_TPREL16_HI:
4636 case R_PPC64_GOT_TPREL16_HA:
c213164a 4637 case R_PPC64_GOT_TPREL34:
7c8bbca5 4638 if (bfd_link_dll (info))
411e1bfb
AM
4639 info->flags |= DF_STATIC_TLS;
4640 tls_type = TLS_TLS | TLS_TPREL;
4641 goto dogottls;
4642
4643 case R_PPC64_GOT_DTPREL16_DS:
4644 case R_PPC64_GOT_DTPREL16_LO_DS:
4645 case R_PPC64_GOT_DTPREL16_HI:
4646 case R_PPC64_GOT_DTPREL16_HA:
c213164a 4647 case R_PPC64_GOT_DTPREL34:
411e1bfb
AM
4648 tls_type = TLS_TLS | TLS_DTPREL;
4649 dogottls:
4650 sec->has_tls_reloc = 1;
066f4018 4651 goto dogot;
411e1bfb 4652
65f38f15 4653 case R_PPC64_GOT16_HA:
066f4018 4654 case R_PPC64_GOT16_LO_DS:
4a421c53 4655 case R_PPC64_GOT_PCREL34:
066f4018
AM
4656 ppc64_elf_tdata (abfd)->has_gotrel = 1;
4657 ppc64_elf_section_data (sec)->has_gotrel = 1;
4658 /* Fall through. */
4659
bb22a418 4660 case R_PPC64_GOT16_DS:
066f4018 4661 case R_PPC64_GOT16:
65f38f15
AM
4662 case R_PPC64_GOT16_HI:
4663 case R_PPC64_GOT16_LO:
066f4018 4664 dogot:
65f38f15 4665 /* This symbol requires a global offset table entry. */
4c52953f 4666 sec->has_toc_reloc = 1;
33c0ec9d
AM
4667 if (r_type == R_PPC64_GOT_TLSLD16
4668 || r_type == R_PPC64_GOT_TLSGD16
4669 || r_type == R_PPC64_GOT_TPREL16_DS
4670 || r_type == R_PPC64_GOT_DTPREL16_DS
4671 || r_type == R_PPC64_GOT16
4672 || r_type == R_PPC64_GOT16_DS)
4673 {
4674 htab->do_multi_toc = 1;
d77c8a4b 4675 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4676 }
4677
e717da7e
AM
4678 if (ppc64_elf_tdata (abfd)->got == NULL
4679 && !create_got_section (abfd, info))
b34976b6 4680 return FALSE;
5bd4f169
AM
4681
4682 if (h != NULL)
4683 {
411e1bfb
AM
4684 struct ppc_link_hash_entry *eh;
4685 struct got_entry *ent;
65f38f15 4686
411e1bfb
AM
4687 eh = (struct ppc_link_hash_entry *) h;
4688 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5663e321 4689 if (ent->addend == sym_addend
e717da7e 4690 && ent->owner == abfd
411e1bfb
AM
4691 && ent->tls_type == tls_type)
4692 break;
4693 if (ent == NULL)
5bd4f169 4694 {
411e1bfb 4695 bfd_size_type amt = sizeof (*ent);
4ce794b7 4696 ent = bfd_alloc (abfd, amt);
411e1bfb 4697 if (ent == NULL)
b34976b6 4698 return FALSE;
411e1bfb 4699 ent->next = eh->elf.got.glist;
5663e321 4700 ent->addend = sym_addend;
e717da7e 4701 ent->owner = abfd;
411e1bfb 4702 ent->tls_type = tls_type;
927be08e 4703 ent->is_indirect = FALSE;
411e1bfb
AM
4704 ent->got.refcount = 0;
4705 eh->elf.got.glist = ent;
5bd4f169 4706 }
411e1bfb 4707 ent->got.refcount += 1;
e7b938ca 4708 eh->tls_mask |= tls_type;
5bd4f169 4709 }
411e1bfb
AM
4710 else
4711 /* This is a global offset table entry for a local symbol. */
4712 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4713 sym_addend, tls_type))
411e1bfb 4714 return FALSE;
a345bc8d
AM
4715
4716 /* We may also need a plt entry if the symbol turns out to be
4717 an ifunc. */
0e1862bb 4718 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d 4719 {
5663e321 4720 if (!update_plt_info (abfd, &h->plt.plist, sym_addend))
a345bc8d
AM
4721 return FALSE;
4722 }
5bd4f169
AM
4723 break;
4724
5bd4f169 4725 case R_PPC64_PLT16_HA:
65f38f15
AM
4726 case R_PPC64_PLT16_HI:
4727 case R_PPC64_PLT16_LO:
08be3224 4728 case R_PPC64_PLT16_LO_DS:
5663e321
AM
4729 case R_PPC64_PLT_PCREL34:
4730 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
4731 case R_PPC64_PLT32:
4732 case R_PPC64_PLT64:
cbf95972
AM
4733 /* This symbol requires a procedure linkage table entry. */
4734 plt_list = ifunc;
4735 if (h != NULL)
e054468f 4736 {
e054468f
AM
4737 h->needs_plt = 1;
4738 if (h->root.root.string[0] == '.'
4739 && h->root.root.string[1] != '\0')
4740 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2d7ad24e 4741 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4742 plt_list = &h->plt.plist;
4743 }
4744 if (plt_list == NULL)
2d7ad24e 4745 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4746 sym_addend,
2d7ad24e 4747 NON_GOT | PLT_KEEP);
5663e321 4748 if (!update_plt_info (abfd, plt_list, sym_addend))
cbf95972 4749 return FALSE;
5bd4f169
AM
4750 break;
4751
4752 /* The following relocations don't need to propagate the
4753 relocation if linking a shared object since they are
4754 section relative. */
4755 case R_PPC64_SECTOFF:
4756 case R_PPC64_SECTOFF_LO:
4757 case R_PPC64_SECTOFF_HI:
4758 case R_PPC64_SECTOFF_HA:
4759 case R_PPC64_SECTOFF_DS:
4760 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4761 case R_PPC64_DTPREL16:
4762 case R_PPC64_DTPREL16_LO:
4763 case R_PPC64_DTPREL16_HI:
4764 case R_PPC64_DTPREL16_HA:
4765 case R_PPC64_DTPREL16_DS:
4766 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
4767 case R_PPC64_DTPREL16_HIGH:
4768 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
4769 case R_PPC64_DTPREL16_HIGHER:
4770 case R_PPC64_DTPREL16_HIGHERA:
4771 case R_PPC64_DTPREL16_HIGHEST:
4772 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4773 break;
4774
ad8e1ba5 4775 /* Nor do these. */
25f23106
AM
4776 case R_PPC64_REL16:
4777 case R_PPC64_REL16_LO:
4778 case R_PPC64_REL16_HI:
4779 case R_PPC64_REL16_HA:
4a969973
AM
4780 case R_PPC64_REL16_HIGH:
4781 case R_PPC64_REL16_HIGHA:
4782 case R_PPC64_REL16_HIGHER:
4783 case R_PPC64_REL16_HIGHERA:
4784 case R_PPC64_REL16_HIGHEST:
4785 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
4786 case R_PPC64_REL16_HIGHER34:
4787 case R_PPC64_REL16_HIGHERA34:
4788 case R_PPC64_REL16_HIGHEST34:
4789 case R_PPC64_REL16_HIGHESTA34:
a680de9a 4790 case R_PPC64_REL16DX_HA:
25f23106
AM
4791 break;
4792
45965137
AM
4793 /* Not supported as a dynamic relocation. */
4794 case R_PPC64_ADDR64_LOCAL:
0e1862bb 4795 if (bfd_link_pic (info))
45965137
AM
4796 {
4797 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4798 ppc_howto_init ();
695344c0 4799 /* xgettext:c-format */
174d0a74 4800 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 4801 "in shared libraries and PIEs\n"),
45965137
AM
4802 abfd, sec, rel->r_offset,
4803 ppc64_elf_howto_table[r_type]->name);
4804 bfd_set_error (bfd_error_bad_value);
4805 return FALSE;
4806 }
4807 break;
4808
ad8e1ba5 4809 case R_PPC64_TOC16:
33c0ec9d
AM
4810 case R_PPC64_TOC16_DS:
4811 htab->do_multi_toc = 1;
d77c8a4b 4812 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 4813 /* Fall through. */
ad8e1ba5
AM
4814 case R_PPC64_TOC16_LO:
4815 case R_PPC64_TOC16_HI:
4816 case R_PPC64_TOC16_HA:
ad8e1ba5 4817 case R_PPC64_TOC16_LO_DS:
4c52953f 4818 sec->has_toc_reloc = 1;
ad8e1ba5
AM
4819 break;
4820
006589cf
AM
4821 /* Marker reloc. */
4822 case R_PPC64_ENTRY:
4823 break;
4824
5bd4f169
AM
4825 /* This relocation describes the C++ object vtable hierarchy.
4826 Reconstruct it for later use during GC. */
4827 case R_PPC64_GNU_VTINHERIT:
c152c796 4828 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4829 return FALSE;
5bd4f169
AM
4830 break;
4831
4832 /* This relocation describes which C++ vtable entries are actually
4833 used. Record for later use during GC. */
4834 case R_PPC64_GNU_VTENTRY:
a0ea3a14 4835 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4836 return FALSE;
5bd4f169
AM
4837 break;
4838
721956f4
AM
4839 case R_PPC64_REL14:
4840 case R_PPC64_REL14_BRTAKEN:
4841 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4842 {
4843 asection *dest = NULL;
4844
4845 /* Heuristic: If jumping outside our section, chances are
4846 we are going to need a stub. */
4847 if (h != NULL)
4848 {
4849 /* If the sym is weak it may be overridden later, so
4850 don't assume we know where a weak sym lives. */
4851 if (h->root.type == bfd_link_hash_defined)
4852 dest = h->root.u.def.section;
4853 }
4854 else
87d72d41
AM
4855 {
4856 Elf_Internal_Sym *isym;
4857
4858 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4859 abfd, r_symndx);
4860 if (isym == NULL)
4861 return FALSE;
4862
4863 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4864 }
4865
220c76dd 4866 if (dest != sec)
7c8fe5c4 4867 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4868 }
3e04d765
AM
4869 goto rel24;
4870
4871 case R_PPC64_PLTCALL:
5663e321 4872 case R_PPC64_PLTCALL_NOTOC:
3e04d765 4873 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
4874 /* Fall through. */
4875
5d1634d7 4876 case R_PPC64_REL24:
05d0e962 4877 case R_PPC64_REL24_NOTOC:
3e04d765 4878 rel24:
cbf95972
AM
4879 plt_list = ifunc;
4880 if (h != NULL)
5d1634d7 4881 {
e054468f
AM
4882 h->needs_plt = 1;
4883 if (h->root.root.string[0] == '.'
4884 && h->root.root.string[1] != '\0')
4885 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 4886
3a71aa26 4887 if (h == tga || h == dottga)
cbf95972
AM
4888 {
4889 sec->has_tls_reloc = 1;
4890 if (rel != relocs
4891 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4892 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4893 /* We have a new-style __tls_get_addr call with
4894 a marker reloc. */
4895 ;
4896 else
4897 /* Mark this section as having an old-style call. */
4898 sec->has_tls_get_addr_call = 1;
4899 }
4900 plt_list = &h->plt.plist;
411e1bfb 4901 }
cbf95972
AM
4902
4903 /* We may need a .plt entry if the function this reloc
4904 refers to is in a shared lib. */
4905 if (plt_list
5663e321 4906 && !update_plt_info (abfd, plt_list, sym_addend))
cbf95972 4907 return FALSE;
411e1bfb
AM
4908 break;
4909
cbf95972
AM
4910 case R_PPC64_ADDR14:
4911 case R_PPC64_ADDR14_BRNTAKEN:
4912 case R_PPC64_ADDR14_BRTAKEN:
4913 case R_PPC64_ADDR24:
4914 goto dodyn;
4915
411e1bfb
AM
4916 case R_PPC64_TPREL64:
4917 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 4918 if (bfd_link_dll (info))
411e1bfb
AM
4919 info->flags |= DF_STATIC_TLS;
4920 goto dotlstoc;
4921
4922 case R_PPC64_DTPMOD64:
4923 if (rel + 1 < rel_end
4924 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4925 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 4926 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 4927 else
951fd09b 4928 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
4929 goto dotlstoc;
4930
4931 case R_PPC64_DTPREL64:
4932 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4933 if (rel != relocs
4934 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4935 && rel[-1].r_offset == rel->r_offset - 8)
4936 /* This is the second reloc of a dtpmod, dtprel pair.
4937 Don't mark with TLS_DTPREL. */
4938 goto dodyn;
4939
4940 dotlstoc:
4941 sec->has_tls_reloc = 1;
4942 if (h != NULL)
4943 {
4944 struct ppc_link_hash_entry *eh;
4945 eh = (struct ppc_link_hash_entry *) h;
46e9995a 4946 eh->tls_mask |= tls_type & 0xff;
411e1bfb
AM
4947 }
4948 else
4949 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4950 sym_addend, tls_type))
411e1bfb
AM
4951 return FALSE;
4952
7c8fe5c4
AM
4953 ppc64_sec = ppc64_elf_section_data (sec);
4954 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 4955 {
3a71aa26
AM
4956 bfd_size_type amt;
4957
e7b938ca 4958 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
4959 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4960 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4961 if (ppc64_sec->u.toc.symndx == NULL)
4962 return FALSE;
4963 amt = sec->size * sizeof (bfd_vma) / 8;
4964 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4965 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 4966 return FALSE;
7c8fe5c4
AM
4967 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4968 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
4969 }
4970 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26 4971 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5663e321 4972 ppc64_sec->u.toc.add[rel->r_offset / 8] = sym_addend;
951fd09b
AM
4973
4974 /* Mark the second slot of a GD or LD entry.
4975 -1 to indicate GD and -2 to indicate LD. */
4976 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 4977 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 4978 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 4979 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
4980 goto dodyn;
4981
4982 case R_PPC64_TPREL16:
4983 case R_PPC64_TPREL16_LO:
4984 case R_PPC64_TPREL16_HI:
4985 case R_PPC64_TPREL16_HA:
4986 case R_PPC64_TPREL16_DS:
4987 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
4988 case R_PPC64_TPREL16_HIGH:
4989 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
4990 case R_PPC64_TPREL16_HIGHER:
4991 case R_PPC64_TPREL16_HIGHERA:
4992 case R_PPC64_TPREL16_HIGHEST:
4993 case R_PPC64_TPREL16_HIGHESTA:
c213164a 4994 case R_PPC64_TPREL34:
7c8bbca5
AM
4995 if (bfd_link_dll (info))
4996 info->flags |= DF_STATIC_TLS;
4997 goto dodyn;
5d1634d7 4998
e86ce104 4999 case R_PPC64_ADDR64:
b9399fcf 5000 if (is_opd
1e2f5b6e 5001 && rel + 1 < rel_end
4ce794b7 5002 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5003 {
8387904d 5004 if (h != NULL)
8c5b4e52 5005 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 5006 }
e86ce104
AM
5007 /* Fall through. */
5008
65f38f15
AM
5009 case R_PPC64_ADDR16:
5010 case R_PPC64_ADDR16_DS:
5011 case R_PPC64_ADDR16_HA:
5012 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5013 case R_PPC64_ADDR16_HIGH:
5014 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5015 case R_PPC64_ADDR16_HIGHER:
5016 case R_PPC64_ADDR16_HIGHERA:
5017 case R_PPC64_ADDR16_HIGHEST:
5018 case R_PPC64_ADDR16_HIGHESTA:
5019 case R_PPC64_ADDR16_LO:
5020 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
5021 case R_PPC64_D34:
5022 case R_PPC64_D34_LO:
5023 case R_PPC64_D34_HI30:
5024 case R_PPC64_D34_HA30:
5025 case R_PPC64_ADDR16_HIGHER34:
5026 case R_PPC64_ADDR16_HIGHERA34:
5027 case R_PPC64_ADDR16_HIGHEST34:
5028 case R_PPC64_ADDR16_HIGHESTA34:
5029 case R_PPC64_D28:
0e1862bb 5030 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5031 && rel->r_addend == 0)
5032 {
5033 /* We may need a .plt entry if this reloc refers to a
5034 function in a shared lib. */
5663e321 5035 if (!update_plt_info (abfd, &h->plt.plist, 0))
a345bc8d
AM
5036 return FALSE;
5037 h->pointer_equality_needed = 1;
5038 }
5039 /* Fall through. */
5040
5041 case R_PPC64_REL30:
5042 case R_PPC64_REL32:
5043 case R_PPC64_REL64:
65f38f15 5044 case R_PPC64_ADDR32:
65f38f15
AM
5045 case R_PPC64_UADDR16:
5046 case R_PPC64_UADDR32:
5047 case R_PPC64_UADDR64:
5bd4f169 5048 case R_PPC64_TOC:
0e1862bb 5049 if (h != NULL && !bfd_link_pic (info))
81848ca0 5050 /* We may need a copy reloc. */
f5385ebf 5051 h->non_got_ref = 1;
81848ca0 5052
41bd81ab 5053 /* Don't propagate .opd relocs. */
b9399fcf 5054 if (NO_OPD_RELOCS && is_opd)
e86ce104 5055 break;
e86ce104 5056
65f38f15
AM
5057 /* If we are creating a shared library, and this is a reloc
5058 against a global symbol, or a non PC relative reloc
5059 against a local symbol, then we need to copy the reloc
5060 into the shared library. However, if we are linking with
5061 -Bsymbolic, we do not need to copy a reloc against a
5062 global symbol which is defined in an object we are
5063 including in the link (i.e., DEF_REGULAR is set). At
5064 this point we have not seen all the input files, so it is
5065 possible that DEF_REGULAR is not set now but will be set
5066 later (it is never cleared). In case of a weak definition,
5067 DEF_REGULAR may be cleared later by a strong definition in
5068 a shared library. We account for that possibility below by
f4656909 5069 storing information in the dyn_relocs field of the hash
65f38f15
AM
5070 table entry. A similar situation occurs when creating
5071 shared libraries and symbol visibility changes render the
5072 symbol local.
5073
5074 If on the other hand, we are creating an executable, we
5075 may need to keep relocations for symbols satisfied by a
5076 dynamic library if we manage to avoid copy relocs for the
5077 symbol. */
411e1bfb 5078 dodyn:
0e1862bb 5079 if ((bfd_link_pic (info)
1d483afe 5080 && (must_be_dyn_reloc (info, r_type)
65f38f15 5081 || (h != NULL
198f1157 5082 && (!SYMBOLIC_BIND (info, h)
65f38f15 5083 || h->root.type == bfd_link_hash_defweak
f5385ebf 5084 || !h->def_regular))))
f4656909 5085 || (ELIMINATE_COPY_RELOCS
0e1862bb 5086 && !bfd_link_pic (info)
65f38f15
AM
5087 && h != NULL
5088 && (h->root.type == bfd_link_hash_defweak
25f23106 5089 || !h->def_regular))
0e1862bb 5090 || (!bfd_link_pic (info)
25f23106 5091 && ifunc != NULL))
5bd4f169 5092 {
65f38f15
AM
5093 /* We must copy these reloc types into the output file.
5094 Create a reloc section in dynobj and make room for
5095 this reloc. */
5bd4f169
AM
5096 if (sreloc == NULL)
5097 {
83bac4b0
NC
5098 sreloc = _bfd_elf_make_dynamic_reloc_section
5099 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5100
5bd4f169 5101 if (sreloc == NULL)
83bac4b0 5102 return FALSE;
5bd4f169
AM
5103 }
5104
65f38f15
AM
5105 /* If this is a global symbol, we count the number of
5106 relocations we need for this symbol. */
5107 if (h != NULL)
5108 {
19e08130
AM
5109 struct elf_dyn_relocs *p;
5110 struct elf_dyn_relocs **head;
5111
ec338859 5112 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5113 p = *head;
5114 if (p == NULL || p->sec != sec)
5115 {
5116 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5117 if (p == NULL)
5118 return FALSE;
5119 p->next = *head;
5120 *head = p;
5121 p->sec = sec;
5122 p->count = 0;
5123 p->pc_count = 0;
5124 }
5125 p->count += 1;
5126 if (!must_be_dyn_reloc (info, r_type))
5127 p->pc_count += 1;
65f38f15
AM
5128 }
5129 else
5130 {
ec338859
AM
5131 /* Track dynamic relocs needed for local syms too.
5132 We really need local syms available to do this
5133 easily. Oh well. */
19e08130
AM
5134 struct ppc_dyn_relocs *p;
5135 struct ppc_dyn_relocs **head;
5136 bfd_boolean is_ifunc;
ec338859 5137 asection *s;
6edfbbad 5138 void *vpp;
87d72d41 5139 Elf_Internal_Sym *isym;
6edfbbad 5140
87d72d41
AM
5141 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5142 abfd, r_symndx);
5143 if (isym == NULL)
b34976b6 5144 return FALSE;
ec338859 5145
87d72d41
AM
5146 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5147 if (s == NULL)
5148 s = sec;
5149
6edfbbad 5150 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5151 head = (struct ppc_dyn_relocs **) vpp;
5152 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5153 p = *head;
5154 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5155 p = p->next;
5156 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5157 {
5158 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5159 if (p == NULL)
5160 return FALSE;
5161 p->next = *head;
5162 *head = p;
5163 p->sec = sec;
5164 p->ifunc = is_ifunc;
5165 p->count = 0;
5166 }
5167 p->count += 1;
ec338859 5168 }
65f38f15 5169 }
5bd4f169 5170 break;
65f38f15
AM
5171
5172 default:
96e0dda4 5173 break;
5bd4f169
AM
5174 }
5175 }
5176
b34976b6 5177 return TRUE;
5bd4f169
AM
5178}
5179
ee67d69a
AM
5180/* Merge backend specific data from an object file to the output
5181 object file when linking. */
5182
5183static bfd_boolean
50e03d47 5184ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5185{
50e03d47 5186 bfd *obfd = info->output_bfd;
ee67d69a
AM
5187 unsigned long iflags, oflags;
5188
5189 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5190 return TRUE;
5191
5192 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5193 return TRUE;
5194
50e03d47 5195 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
5196 return FALSE;
5197
5198 iflags = elf_elfheader (ibfd)->e_flags;
5199 oflags = elf_elfheader (obfd)->e_flags;
5200
f6c7c3e8 5201 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5202 {
4eca0228 5203 _bfd_error_handler
695344c0 5204 /* xgettext:c-format */
871b3ab2 5205 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
5206 bfd_set_error (bfd_error_bad_value);
5207 return FALSE;
5208 }
f6c7c3e8 5209 else if (iflags != oflags && iflags != 0)
ee67d69a 5210 {
4eca0228 5211 _bfd_error_handler
695344c0 5212 /* xgettext:c-format */
871b3ab2 5213 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5214 ibfd, iflags, oflags);
5215 bfd_set_error (bfd_error_bad_value);
5216 return FALSE;
5217 }
5218
4a91d0ba
AM
5219 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5220 return FALSE;
005d79fd 5221
ee67d69a 5222 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5223 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5224}
5225
5226static bfd_boolean
5227ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5228{
5229 /* Print normal ELF private data. */
5230 _bfd_elf_print_private_bfd_data (abfd, ptr);
5231
5232 if (elf_elfheader (abfd)->e_flags != 0)
5233 {
5234 FILE *file = ptr;
5235
ee67d69a
AM
5236 fprintf (file, _("private flags = 0x%lx:"),
5237 elf_elfheader (abfd)->e_flags);
5238
5239 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5240 fprintf (file, _(" [abiv%ld]"),
5241 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5242 fputc ('\n', file);
5243 }
5244
5245 return TRUE;
5246}
5247
8387904d 5248/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5249 of the code entry point, and its section, which must be in the same
5250 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5251
5252static bfd_vma
5253opd_entry_value (asection *opd_sec,
5254 bfd_vma offset,
5255 asection **code_sec,
aef36ac1
AM
5256 bfd_vma *code_off,
5257 bfd_boolean in_code_sec)
8387904d
AM
5258{
5259 bfd *opd_bfd = opd_sec->owner;
8860955f 5260 Elf_Internal_Rela *relocs;
8387904d 5261 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5262 bfd_vma val;
8387904d 5263
9f296da3
AM
5264 /* No relocs implies we are linking a --just-symbols object, or looking
5265 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5266 if (opd_sec->reloc_count == 0)
5267 {
729eabd5 5268 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5269
729eabd5
AM
5270 if (contents == NULL)
5271 {
5272 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5273 return (bfd_vma) -1;
5274 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5275 }
ee1e4ede 5276
dbb3fbbb 5277 /* PR 17512: file: 64b9dfbb. */
451dfd38 5278 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5279 return (bfd_vma) -1;
5280
729eabd5 5281 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5282 if (code_sec != NULL)
5283 {
5284 asection *sec, *likely = NULL;
ee1e4ede 5285
aef36ac1 5286 if (in_code_sec)
4b85d634 5287 {
aef36ac1
AM
5288 sec = *code_sec;
5289 if (sec->vma <= val
5290 && val < sec->vma + sec->size)
5291 likely = sec;
5292 else
5293 val = -1;
5294 }
5295 else
5296 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5297 if (sec->vma <= val
5298 && (sec->flags & SEC_LOAD) != 0
5299 && (sec->flags & SEC_ALLOC) != 0)
5300 likely = sec;
5301 if (likely != NULL)
5302 {
5303 *code_sec = likely;
5304 if (code_off != NULL)
5305 *code_off = val - likely->vma;
4b85d634
AM
5306 }
5307 }
aef36ac1 5308 return val;
4b85d634
AM
5309 }
5310
0c8d6e5c 5311 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5312
729eabd5 5313 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5314 if (relocs == NULL)
5315 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
5316 /* PR 17512: file: df8e1fd6. */
5317 if (relocs == NULL)
5318 return (bfd_vma) -1;
645ea6a9 5319
8387904d 5320 /* Go find the opd reloc at the sym address. */
8860955f 5321 lo = relocs;
8387904d 5322 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5323 val = (bfd_vma) -1;
8387904d
AM
5324 while (lo < hi)
5325 {
5326 look = lo + (hi - lo) / 2;
5327 if (look->r_offset < offset)
5328 lo = look + 1;
5329 else if (look->r_offset > offset)
5330 hi = look;
5331 else
5332 {
0ffa91dd
NC
5333 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5334
8387904d
AM
5335 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5336 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5337 {
5338 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5339 asection *sec = NULL;
8387904d 5340
b53dfeb2
AM
5341 if (symndx >= symtab_hdr->sh_info
5342 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5343 {
5344 struct elf_link_hash_entry **sym_hashes;
5345 struct elf_link_hash_entry *rh;
5346
5347 sym_hashes = elf_sym_hashes (opd_bfd);
5348 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5349 if (rh != NULL)
5350 {
5351 rh = elf_follow_link (rh);
bb854a36
AM
5352 if (rh->root.type != bfd_link_hash_defined
5353 && rh->root.type != bfd_link_hash_defweak)
5354 break;
5355 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5356 {
bb854a36
AM
5357 val = rh->root.u.def.value;
5358 sec = rh->root.u.def.section;
b53dfeb2
AM
5359 }
5360 }
5361 }
5362
5363 if (sec == NULL)
5364 {
5365 Elf_Internal_Sym *sym;
5366
5367 if (symndx < symtab_hdr->sh_info)
5368 {
5369 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5370 if (sym == NULL)
5371 {
5372 size_t symcnt = symtab_hdr->sh_info;
5373 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5374 symcnt, 0,
5375 NULL, NULL, NULL);
5376 if (sym == NULL)
5377 break;
5378 symtab_hdr->contents = (bfd_byte *) sym;
5379 }
5380 sym += symndx;
128205bb
AM
5381 }
5382 else
5383 {
b53dfeb2
AM
5384 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5385 1, symndx,
5386 NULL, NULL, NULL);
128205bb
AM
5387 if (sym == NULL)
5388 break;
128205bb 5389 }
b53dfeb2
AM
5390 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5391 if (sec == NULL)
5392 break;
5393 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5394 val = sym->st_value;
8387904d 5395 }
b53dfeb2 5396
8387904d
AM
5397 val += look->r_addend;
5398 if (code_off != NULL)
5399 *code_off = val;
5400 if (code_sec != NULL)
aef36ac1
AM
5401 {
5402 if (in_code_sec && *code_sec != sec)
5403 return -1;
5404 else
5405 *code_sec = sec;
5406 }
b53dfeb2 5407 if (sec->output_section != NULL)
8387904d 5408 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5409 }
5410 break;
5411 }
5412 }
645ea6a9 5413
645ea6a9 5414 return val;
8387904d
AM
5415}
5416
aef36ac1
AM
5417/* If the ELF symbol SYM might be a function in SEC, return the
5418 function size and set *CODE_OFF to the function's entry point,
5419 otherwise return zero. */
9f296da3 5420
aef36ac1
AM
5421static bfd_size_type
5422ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5423 bfd_vma *code_off)
9f296da3 5424{
aef36ac1
AM
5425 bfd_size_type size;
5426
5427 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5428 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5429 return 0;
5430
5431 size = 0;
5432 if (!(sym->flags & BSF_SYNTHETIC))
5433 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5434
5435 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5436 {
b07bca4e
AM
5437 struct _opd_sec_data *opd = get_opd_info (sym->section);
5438 bfd_vma symval = sym->value;
5439
5440 if (opd != NULL
5441 && opd->adjust != NULL
5442 && elf_section_data (sym->section)->relocs != NULL)
5443 {
5444 /* opd_entry_value will use cached relocs that have been
5445 adjusted, but with raw symbols. That means both local
5446 and global symbols need adjusting. */
5447 long adjust = opd->adjust[OPD_NDX (symval)];
5448 if (adjust == -1)
5449 return 0;
5450 symval += adjust;
5451 }
5452
5453 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
5454 &sec, code_off, TRUE) == (bfd_vma) -1)
5455 return 0;
5456 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5457 symbol. This size has nothing to do with the code size of the
5458 function, which is what we're supposed to return, but the
5459 code size isn't available without looking up the dot-sym.
5460 However, doing that would be a waste of time particularly
5461 since elf_find_function will look at the dot-sym anyway.
5462 Now, elf_find_function will keep the largest size of any
5463 function sym found at the code address of interest, so return
5464 1 here to avoid it incorrectly caching a larger function size
5465 for a small function. This does mean we return the wrong
5466 size for a new-ABI function of size 24, but all that does is
5467 disable caching for such functions. */
5468 if (size == 24)
5469 size = 1;
9f296da3 5470 }
aef36ac1
AM
5471 else
5472 {
5473 if (sym->section != sec)
5474 return 0;
5475 *code_off = sym->value;
5476 }
5477 if (size == 0)
5478 size = 1;
5479 return size;
9f296da3
AM
5480}
5481
f378ab09
AM
5482/* Return true if symbol is a strong function defined in an ELFv2
5483 object with st_other localentry bits of zero, ie. its local entry
5484 point coincides with its global entry point. */
5485
5486static bfd_boolean
5487is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5488{
5489 return (h != NULL
5490 && h->type == STT_FUNC
5491 && h->root.type == bfd_link_hash_defined
5492 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 5493 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
5494 && is_ppc64_elf (h->root.u.def.section->owner)
5495 && abiversion (h->root.u.def.section->owner) >= 2);
5496}
5497
854b41e7
AM
5498/* Return true if symbol is defined in a regular object file. */
5499
5500static bfd_boolean
5501is_static_defined (struct elf_link_hash_entry *h)
5502{
5503 return ((h->root.type == bfd_link_hash_defined
5504 || h->root.type == bfd_link_hash_defweak)
5505 && h->root.u.def.section != NULL
5506 && h->root.u.def.section->output_section != NULL);
5507}
5508
b31867b6
AM
5509/* If FDH is a function descriptor symbol, return the associated code
5510 entry symbol if it is defined. Return NULL otherwise. */
5511
5512static struct ppc_link_hash_entry *
5513defined_code_entry (struct ppc_link_hash_entry *fdh)
5514{
5515 if (fdh->is_func_descriptor)
5516 {
5517 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5518 if (fh->elf.root.type == bfd_link_hash_defined
5519 || fh->elf.root.type == bfd_link_hash_defweak)
5520 return fh;
5521 }
5522 return NULL;
5523}
5524
5525/* If FH is a function code entry symbol, return the associated
5526 function descriptor symbol if it is defined. Return NULL otherwise. */
5527
5528static struct ppc_link_hash_entry *
5529defined_func_desc (struct ppc_link_hash_entry *fh)
5530{
5531 if (fh->oh != NULL
5532 && fh->oh->is_func_descriptor)
5533 {
5534 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5535 if (fdh->elf.root.type == bfd_link_hash_defined
5536 || fdh->elf.root.type == bfd_link_hash_defweak)
5537 return fdh;
5538 }
5539 return NULL;
5540}
5541
8c5b4e52
AM
5542static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5543
5544/* Garbage collect sections, after first dealing with dot-symbols. */
5545
5546static bfd_boolean
5547ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5548{
5549 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5550
5551 if (htab != NULL && htab->need_func_desc_adj)
5552 {
5553 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5554 htab->need_func_desc_adj = 0;
5555 }
5556 return bfd_elf_gc_sections (abfd, info);
5557}
5558
74f0fb50
AM
5559/* Mark all our entry sym sections, both opd and code section. */
5560
5561static void
5562ppc64_elf_gc_keep (struct bfd_link_info *info)
5563{
5564 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5565 struct bfd_sym_chain *sym;
5566
4dfe6ac6
NC
5567 if (htab == NULL)
5568 return;
5569
74f0fb50
AM
5570 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5571 {
b31867b6 5572 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5573 asection *sec;
5574
5575 eh = (struct ppc_link_hash_entry *)
b31867b6 5576 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5577 if (eh == NULL)
5578 continue;
5579 if (eh->elf.root.type != bfd_link_hash_defined
5580 && eh->elf.root.type != bfd_link_hash_defweak)
5581 continue;
5582
b31867b6
AM
5583 fh = defined_code_entry (eh);
5584 if (fh != NULL)
74f0fb50 5585 {
b31867b6 5586 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5587 sec->flags |= SEC_KEEP;
5588 }
5589 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5590 && opd_entry_value (eh->elf.root.u.def.section,
5591 eh->elf.root.u.def.value,
aef36ac1 5592 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
5593 sec->flags |= SEC_KEEP;
5594
5595 sec = eh->elf.root.u.def.section;
5596 sec->flags |= SEC_KEEP;
5597 }
5598}
5599
64d03ab5
AM
5600/* Mark sections containing dynamically referenced symbols. When
5601 building shared libraries, we must assume that any visible symbol is
5602 referenced. */
5603
5604static bfd_boolean
5605ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5606{
5607 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5608 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5609 struct ppc_link_hash_entry *fdh;
b407645f 5610 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5611
64d03ab5 5612 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5613 fdh = defined_func_desc (eh);
5614 if (fdh != NULL)
5615 eh = fdh;
64d03ab5
AM
5616
5617 if ((eh->elf.root.type == bfd_link_hash_defined
5618 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 5619 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5620 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5621 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5622 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5623 && (!bfd_link_executable (info)
e278ae05 5624 || info->gc_keep_exported
b407645f
AM
5625 || info->export_dynamic
5626 || (eh->elf.dynamic
5627 && d != NULL
2cdcc330
AM
5628 && (*d->match) (&d->head, NULL,
5629 eh->elf.root.root.string)))
e278ae05 5630 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5631 || !bfd_hide_sym_by_version (info->version_info,
5632 eh->elf.root.root.string)))))
64d03ab5
AM
5633 {
5634 asection *code_sec;
b31867b6 5635 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5636
5637 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5638
5639 /* Function descriptor syms cause the associated
5640 function code sym section to be marked. */
b31867b6
AM
5641 fh = defined_code_entry (eh);
5642 if (fh != NULL)
5643 {
5644 code_sec = fh->elf.root.u.def.section;
5645 code_sec->flags |= SEC_KEEP;
5646 }
64d03ab5
AM
5647 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5648 && opd_entry_value (eh->elf.root.u.def.section,
5649 eh->elf.root.u.def.value,
aef36ac1 5650 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
5651 code_sec->flags |= SEC_KEEP;
5652 }
5653
5654 return TRUE;
5655}
5656
5bd4f169
AM
5657/* Return the section that should be marked against GC for a given
5658 relocation. */
5659
5660static asection *
4ce794b7 5661ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5662 struct bfd_link_info *info,
4ce794b7
AM
5663 Elf_Internal_Rela *rel,
5664 struct elf_link_hash_entry *h,
5665 Elf_Internal_Sym *sym)
5bd4f169 5666{
ccfa59ea
AM
5667 asection *rsec;
5668
ccfa59ea
AM
5669 /* Syms return NULL if we're marking .opd, so we avoid marking all
5670 function sections, as all functions are referenced in .opd. */
5671 rsec = NULL;
5672 if (get_opd_info (sec) != NULL)
5673 return rsec;
1e2f5b6e 5674
5bd4f169
AM
5675 if (h != NULL)
5676 {
04c9666a 5677 enum elf_ppc64_reloc_type r_type;
b31867b6 5678 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5679
4ce794b7 5680 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5681 switch (r_type)
5bd4f169
AM
5682 {
5683 case R_PPC64_GNU_VTINHERIT:
5684 case R_PPC64_GNU_VTENTRY:
5685 break;
5686
5687 default:
5688 switch (h->root.type)
5689 {
5690 case bfd_link_hash_defined:
5691 case bfd_link_hash_defweak:
ccfa59ea 5692 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5693 fdh = defined_func_desc (eh);
5694 if (fdh != NULL)
8c5b4e52
AM
5695 {
5696 /* -mcall-aixdesc code references the dot-symbol on
5697 a call reloc. Mark the function descriptor too
5698 against garbage collection. */
5699 fdh->elf.mark = 1;
60d67dc8
AM
5700 if (fdh->elf.is_weakalias)
5701 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
5702 eh = fdh;
5703 }
1e2f5b6e
AM
5704
5705 /* Function descriptor syms cause the associated
5706 function code sym section to be marked. */
b31867b6
AM
5707 fh = defined_code_entry (eh);
5708 if (fh != NULL)
ccfa59ea
AM
5709 {
5710 /* They also mark their opd section. */
74f0fb50 5711 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5712
b31867b6 5713 rsec = fh->elf.root.u.def.section;
ccfa59ea 5714 }
8387904d
AM
5715 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5716 && opd_entry_value (eh->elf.root.u.def.section,
5717 eh->elf.root.u.def.value,
aef36ac1 5718 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 5719 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5720 else
1e2f5b6e
AM
5721 rsec = h->root.u.def.section;
5722 break;
5bd4f169
AM
5723
5724 case bfd_link_hash_common:
1e2f5b6e
AM
5725 rsec = h->root.u.c.p->section;
5726 break;
5bd4f169
AM
5727
5728 default:
fb34365b 5729 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5730 }
5731 }
5732 }
5733 else
5734 {
74f0fb50 5735 struct _opd_sec_data *opd;
1e2f5b6e
AM
5736
5737 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5738 opd = get_opd_info (rsec);
5739 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5740 {
74f0fb50 5741 rsec->gc_mark = 1;
ccfa59ea 5742
51aecdc5 5743 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 5744 }
5bd4f169
AM
5745 }
5746
1e2f5b6e 5747 return rsec;
5bd4f169
AM
5748}
5749
deb0e272
AM
5750/* The maximum size of .sfpr. */
5751#define SFPR_MAX (218*4)
5752
5753struct sfpr_def_parms
5754{
699733f6
AM
5755 const char name[12];
5756 unsigned char lo, hi;
2cdcc330
AM
5757 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5758 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
5759};
5760
a4b6fadd
AM
5761/* Auto-generate _save*, _rest* functions in .sfpr.
5762 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5763 instead. */
deb0e272 5764
4dfe6ac6 5765static bfd_boolean
a4b6fadd
AM
5766sfpr_define (struct bfd_link_info *info,
5767 const struct sfpr_def_parms *parm,
5768 asection *stub_sec)
deb0e272
AM
5769{
5770 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5771 unsigned int i;
5772 size_t len = strlen (parm->name);
5773 bfd_boolean writing = FALSE;
699733f6 5774 char sym[16];
deb0e272 5775
4dfe6ac6
NC
5776 if (htab == NULL)
5777 return FALSE;
5778
deb0e272
AM
5779 memcpy (sym, parm->name, len);
5780 sym[len + 2] = 0;
5781
5782 for (i = parm->lo; i <= parm->hi; i++)
5783 {
a4b6fadd 5784 struct ppc_link_hash_entry *h;
deb0e272
AM
5785
5786 sym[len + 0] = i / 10 + '0';
5787 sym[len + 1] = i % 10 + '0';
a4b6fadd 5788 h = (struct ppc_link_hash_entry *)
b32547cd 5789 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 5790 if (stub_sec != NULL)
deb0e272 5791 {
a4b6fadd
AM
5792 if (h != NULL
5793 && h->elf.root.type == bfd_link_hash_defined
5794 && h->elf.root.u.def.section == htab->sfpr)
5795 {
5796 struct elf_link_hash_entry *s;
5797 char buf[32];
5798 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5799 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5800 if (s == NULL)
5801 return FALSE;
5802 if (s->root.type == bfd_link_hash_new
5803 || (s->root.type = bfd_link_hash_defined
5804 && s->root.u.def.section == stub_sec))
5805 {
5806 s->root.type = bfd_link_hash_defined;
5807 s->root.u.def.section = stub_sec;
7dda8d3c 5808 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
5809 + h->elf.root.u.def.value);
5810 s->ref_regular = 1;
5811 s->def_regular = 1;
5812 s->ref_regular_nonweak = 1;
5813 s->forced_local = 1;
5814 s->non_elf = 0;
5815 s->root.linker_def = 1;
5816 }
5817 }
5818 continue;
5819 }
5820 if (h != NULL)
5821 {
5822 h->save_res = 1;
5823 if (!h->elf.def_regular)
deb0e272 5824 {
a4b6fadd
AM
5825 h->elf.root.type = bfd_link_hash_defined;
5826 h->elf.root.u.def.section = htab->sfpr;
5827 h->elf.root.u.def.value = htab->sfpr->size;
5828 h->elf.type = STT_FUNC;
5829 h->elf.def_regular = 1;
b32547cd 5830 h->elf.non_elf = 0;
a4b6fadd
AM
5831 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5832 writing = TRUE;
deb0e272 5833 if (htab->sfpr->contents == NULL)
a4b6fadd 5834 {
2cdcc330
AM
5835 htab->sfpr->contents
5836 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd
AM
5837 if (htab->sfpr->contents == NULL)
5838 return FALSE;
5839 }
deb0e272
AM
5840 }
5841 }
5842 if (writing)
5843 {
5844 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5845 if (i != parm->hi)
5846 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5847 else
5848 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5849 htab->sfpr->size = p - htab->sfpr->contents;
5850 }
5851 }
5852
5853 return TRUE;
5854}
5855
5856static bfd_byte *
5857savegpr0 (bfd *abfd, bfd_byte *p, int r)
5858{
5859 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5860 return p + 4;
5861}
5862
5863static bfd_byte *
5864savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5865{
5866 p = savegpr0 (abfd, p, r);
a078d95a 5867 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5868 p = p + 4;
5869 bfd_put_32 (abfd, BLR, p);
5870 return p + 4;
5871}
5872
5873static bfd_byte *
5874restgpr0 (bfd *abfd, bfd_byte *p, int r)
5875{
5876 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5877 return p + 4;
5878}
5879
5880static bfd_byte *
5881restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5882{
a078d95a 5883 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5884 p = p + 4;
5885 p = restgpr0 (abfd, p, r);
5886 bfd_put_32 (abfd, MTLR_R0, p);
5887 p = p + 4;
5888 if (r == 29)
5889 {
5890 p = restgpr0 (abfd, p, 30);
5891 p = restgpr0 (abfd, p, 31);
5892 }
5893 bfd_put_32 (abfd, BLR, p);
5894 return p + 4;
5895}
5896
5897static bfd_byte *
5898savegpr1 (bfd *abfd, bfd_byte *p, int r)
5899{
5900 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5901 return p + 4;
5902}
5903
5904static bfd_byte *
5905savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5906{
5907 p = savegpr1 (abfd, p, r);
5908 bfd_put_32 (abfd, BLR, p);
5909 return p + 4;
5910}
5911
5912static bfd_byte *
5913restgpr1 (bfd *abfd, bfd_byte *p, int r)
5914{
5915 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5916 return p + 4;
5917}
5918
5919static bfd_byte *
5920restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5921{
5922 p = restgpr1 (abfd, p, r);
5923 bfd_put_32 (abfd, BLR, p);
5924 return p + 4;
5925}
5926
5927static bfd_byte *
5928savefpr (bfd *abfd, bfd_byte *p, int r)
5929{
5930 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5931 return p + 4;
5932}
5933
5934static bfd_byte *
5935savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5936{
5937 p = savefpr (abfd, p, r);
a078d95a 5938 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5939 p = p + 4;
5940 bfd_put_32 (abfd, BLR, p);
5941 return p + 4;
5942}
5943
5944static bfd_byte *
5945restfpr (bfd *abfd, bfd_byte *p, int r)
5946{
5947 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5948 return p + 4;
5949}
5950
5951static bfd_byte *
5952restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5953{
a078d95a 5954 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5955 p = p + 4;
5956 p = restfpr (abfd, p, r);
5957 bfd_put_32 (abfd, MTLR_R0, p);
5958 p = p + 4;
5959 if (r == 29)
5960 {
5961 p = restfpr (abfd, p, 30);
5962 p = restfpr (abfd, p, 31);
5963 }
5964 bfd_put_32 (abfd, BLR, p);
5965 return p + 4;
5966}
5967
5968static bfd_byte *
5969savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5970{
5971 p = savefpr (abfd, p, r);
5972 bfd_put_32 (abfd, BLR, p);
5973 return p + 4;
5974}
5975
5976static bfd_byte *
5977restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5978{
5979 p = restfpr (abfd, p, r);
5980 bfd_put_32 (abfd, BLR, p);
5981 return p + 4;
5982}
5983
5984static bfd_byte *
5985savevr (bfd *abfd, bfd_byte *p, int r)
5986{
5987 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5988 p = p + 4;
5989 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5990 return p + 4;
5991}
5992
5993static bfd_byte *
5994savevr_tail (bfd *abfd, bfd_byte *p, int r)
5995{
5996 p = savevr (abfd, p, r);
5997 bfd_put_32 (abfd, BLR, p);
5998 return p + 4;
5999}
6000
6001static bfd_byte *
6002restvr (bfd *abfd, bfd_byte *p, int r)
6003{
6004 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6005 p = p + 4;
6006 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6007 return p + 4;
6008}
6009
6010static bfd_byte *
6011restvr_tail (bfd *abfd, bfd_byte *p, int r)
6012{
6013 p = restvr (abfd, p, r);
6014 bfd_put_32 (abfd, BLR, p);
6015 return p + 4;
6016}
6017
e86ce104
AM
6018/* Called via elf_link_hash_traverse to transfer dynamic linking
6019 information on function code symbol entries to their corresponding
6020 function descriptor symbol entries. */
deb0e272 6021
b34976b6 6022static bfd_boolean
4ce794b7 6023func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6024{
e86ce104 6025 struct bfd_link_info *info;
65f38f15 6026 struct ppc_link_hash_table *htab;
50bc7936
AM
6027 struct ppc_link_hash_entry *fh;
6028 struct ppc_link_hash_entry *fdh;
6029 bfd_boolean force_local;
5bd4f169 6030
50bc7936
AM
6031 fh = (struct ppc_link_hash_entry *) h;
6032 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6033 return TRUE;
e86ce104 6034
8c5b4e52
AM
6035 if (!fh->is_func)
6036 return TRUE;
6037
6038 if (fh->elf.root.root.string[0] != '.'
6039 || fh->elf.root.root.string[1] == '\0')
6040 return TRUE;
6041
4ce794b7 6042 info = inf;
65f38f15 6043 htab = ppc_hash_table (info);
4dfe6ac6
NC
6044 if (htab == NULL)
6045 return FALSE;
5bd4f169 6046
8c5b4e52
AM
6047 /* Find the corresponding function descriptor symbol. */
6048 fdh = lookup_fdh (fh, htab);
6049
c09bdfe5
AM
6050 /* Resolve undefined references to dot-symbols as the value
6051 in the function descriptor, if we have one in a regular object.
6052 This is to satisfy cases like ".quad .foo". Calls to functions
6053 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6054 if ((fh->elf.root.type == bfd_link_hash_undefined
6055 || fh->elf.root.type == bfd_link_hash_undefweak)
6056 && (fdh->elf.root.type == bfd_link_hash_defined
6057 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6058 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6059 && opd_entry_value (fdh->elf.root.u.def.section,
6060 fdh->elf.root.u.def.value,
c09bdfe5 6061 &fh->elf.root.u.def.section,
aef36ac1 6062 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6063 {
b31867b6 6064 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6065 fh->elf.forced_local = 1;
b31867b6
AM
6066 fh->elf.def_regular = fdh->elf.def_regular;
6067 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6068 }
6069
8c5b4e52
AM
6070 if (!fh->elf.dynamic)
6071 {
6072 struct plt_entry *ent;
5bd4f169 6073
8c5b4e52
AM
6074 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6075 if (ent->plt.refcount > 0)
6076 break;
6077 if (ent == NULL)
6078 return TRUE;
6079 }
5bd4f169 6080
8c5b4e52 6081 /* Create a descriptor as undefined if necessary. */
50bc7936 6082 if (fdh == NULL
0e1862bb 6083 && !bfd_link_executable (info)
50bc7936
AM
6084 && (fh->elf.root.type == bfd_link_hash_undefined
6085 || fh->elf.root.type == bfd_link_hash_undefweak))
6086 {
908b32fc 6087 fdh = make_fdh (info, fh);
bb700d78
AM
6088 if (fdh == NULL)
6089 return FALSE;
50bc7936 6090 }
648cca2c 6091
8c5b4e52 6092 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6093 if (fdh != NULL
6094 && fdh->fake
8c5b4e52
AM
6095 && (fh->elf.root.type == bfd_link_hash_defined
6096 || fh->elf.root.type == bfd_link_hash_defweak))
6097 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 6098
8c5b4e52
AM
6099 /* Transfer dynamic linking information to the function descriptor. */
6100 if (fdh != NULL)
6101 {
f5385ebf
AM
6102 fdh->elf.ref_regular |= fh->elf.ref_regular;
6103 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6104 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6105 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6106 fdh->elf.dynamic |= fh->elf.dynamic;
6107 fdh->elf.needs_plt |= (fh->elf.needs_plt
6108 || fh->elf.type == STT_FUNC
6109 || fh->elf.type == STT_GNU_IFUNC);
6110 move_plt_plist (fh, fdh);
6111
6112 if (!fdh->elf.forced_local
6113 && fh->elf.dynindx != -1)
6114 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6115 return FALSE;
e86ce104
AM
6116 }
6117
50bc7936
AM
6118 /* Now that the info is on the function descriptor, clear the
6119 function code sym info. Any function code syms for which we
6120 don't have a definition in a regular file, we force local.
6121 This prevents a shared library from exporting syms that have
6122 been imported from another library. Function code syms that
6123 are really in the library we must leave global to prevent the
6124 linker dragging in a definition from a static library. */
93f3fa99
AM
6125 force_local = (!fh->elf.def_regular
6126 || fdh == NULL
6127 || !fdh->elf.def_regular
6128 || fdh->elf.forced_local);
50bc7936
AM
6129 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6130
b34976b6 6131 return TRUE;
e86ce104 6132}
40b8271b 6133
a4b6fadd
AM
6134static const struct sfpr_def_parms save_res_funcs[] =
6135 {
6136 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6137 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6138 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6139 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6140 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6141 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6142 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6143 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6144 { "._savef", 14, 31, savefpr, savefpr1_tail },
6145 { "._restf", 14, 31, restfpr, restfpr1_tail },
6146 { "_savevr_", 20, 31, savevr, savevr_tail },
6147 { "_restvr_", 20, 31, restvr, restvr_tail }
6148 };
6149
e86ce104 6150/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6151 this hook to a) provide some gcc support functions, and b) transfer
6152 dynamic linking information gathered so far on function code symbol
6153 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6154
b34976b6 6155static bfd_boolean
4ce794b7
AM
6156ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6157 struct bfd_link_info *info)
e86ce104
AM
6158{
6159 struct ppc_link_hash_table *htab;
6160
6161 htab = ppc_hash_table (info);
4dfe6ac6
NC
6162 if (htab == NULL)
6163 return FALSE;
6164
b32547cd
AM
6165 /* Provide any missing _save* and _rest* functions. */
6166 if (htab->sfpr != NULL)
6167 {
6168 unsigned int i;
6169
6170 htab->sfpr->size = 0;
6171 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6172 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6173 return FALSE;
6174 if (htab->sfpr->size == 0)
6175 htab->sfpr->flags |= SEC_EXCLUDE;
6176 }
6177
6178 if (bfd_link_relocatable (info))
6179 return TRUE;
6180
6181 if (htab->elf.hgot != NULL)
dba6fa9b
AM
6182 {
6183 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6184 /* Make .TOC. defined so as to prevent it being made dynamic.
6185 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6186 if (!htab->elf.hgot->def_regular
6187 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6188 {
6189 htab->elf.hgot->root.type = bfd_link_hash_defined;
6190 htab->elf.hgot->root.u.def.value = 0;
6191 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6192 htab->elf.hgot->def_regular = 1;
6193 htab->elf.hgot->root.linker_def = 1;
6194 }
dba6fa9b 6195 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6196 htab->elf.hgot->other
6197 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6198 }
c66bb0ee 6199
8c5b4e52
AM
6200 if (htab->need_func_desc_adj)
6201 {
6202 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6203 htab->need_func_desc_adj = 0;
6204 }
805fc799 6205
b34976b6 6206 return TRUE;
e86ce104
AM
6207}
6208
98bbb1b8 6209/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 6210
98bbb1b8 6211static asection *
a345bc8d
AM
6212readonly_dynrelocs (struct elf_link_hash_entry *h)
6213{
6214 struct ppc_link_hash_entry *eh;
6215 struct elf_dyn_relocs *p;
6216
6217 eh = (struct ppc_link_hash_entry *) h;
6218 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6219 {
6220 asection *s = p->sec->output_section;
6221
6222 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 6223 return p->sec;
a345bc8d 6224 }
98bbb1b8 6225 return NULL;
a345bc8d
AM
6226}
6227
d311bc8b 6228/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6229 aliases, that apply to read-only sections. Cannot be used after
6230 size_dynamic_sections. */
d311bc8b
AM
6231
6232static bfd_boolean
6233alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6234{
6235 struct ppc_link_hash_entry *eh;
6236
6237 eh = (struct ppc_link_hash_entry *) h;
6238 do
6239 {
6240 if (readonly_dynrelocs (&eh->elf))
6241 return TRUE;
ab2477e1 6242 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
2cdcc330
AM
6243 }
6244 while (eh != NULL && &eh->elf != h);
d311bc8b
AM
6245
6246 return FALSE;
6247}
8a2058b5 6248
8a9e8e72
AM
6249/* Return whether EH has pc-relative dynamic relocs. */
6250
6251static bfd_boolean
6252pc_dynrelocs (struct ppc_link_hash_entry *eh)
6253{
6254 struct elf_dyn_relocs *p;
6255
6256 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6257 if (p->pc_count != 0)
6258 return TRUE;
6259 return FALSE;
6260}
6261
8a2058b5
AM
6262/* Return true if a global entry stub will be created for H. Valid
6263 for ELFv2 before plt entries have been allocated. */
6264
6265static bfd_boolean
6266global_entry_stub (struct elf_link_hash_entry *h)
6267{
6268 struct plt_entry *pent;
6269
6270 if (!h->pointer_equality_needed
6271 || h->def_regular)
6272 return FALSE;
6273
6274 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6275 if (pent->plt.refcount > 0
6276 && pent->addend == 0)
6277 return TRUE;
6278
6279 return FALSE;
6280}
6281
e86ce104
AM
6282/* Adjust a symbol defined by a dynamic object and referenced by a
6283 regular object. The current definition is in some section of the
6284 dynamic object, but we're not including those sections. We have to
6285 change the definition to something the rest of the link can
6286 understand. */
6287
b34976b6 6288static bfd_boolean
4ce794b7
AM
6289ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6290 struct elf_link_hash_entry *h)
e86ce104
AM
6291{
6292 struct ppc_link_hash_table *htab;
5474d94f 6293 asection *s, *srel;
e86ce104
AM
6294
6295 htab = ppc_hash_table (info);
4dfe6ac6
NC
6296 if (htab == NULL)
6297 return FALSE;
e86ce104
AM
6298
6299 /* Deal with function syms. */
6300 if (h->type == STT_FUNC
e054468f 6301 || h->type == STT_GNU_IFUNC
f5385ebf 6302 || h->needs_plt)
e86ce104 6303 {
529fe20e
AM
6304 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6305 || SYMBOL_CALLS_LOCAL (info, h)
6306 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6307 /* Discard dyn_relocs when non-pic if we've decided that a
6308 function symbol is local and not an ifunc. We keep dynamic
6309 relocs for ifuncs when local rather than always emitting a
6310 plt call stub for them and defining the symbol on the call
6311 stub. We can't do that for ELFv1 anyway (a function symbol
6312 is defined on a descriptor, not code) and it can be faster at
6313 run-time due to not needing to bounce through a stub. The
6314 dyn_relocs for ifuncs will be applied even in a static
6315 executable. */
6316 if (!bfd_link_pic (info)
6317 && h->type != STT_GNU_IFUNC
6318 && local)
6319 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6320
e86ce104
AM
6321 /* Clear procedure linkage table information for any symbol that
6322 won't need a .plt entry. */
411e1bfb
AM
6323 struct plt_entry *ent;
6324 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6325 if (ent->plt.refcount > 0)
6326 break;
8387904d 6327 if (ent == NULL
2d7ad24e
AM
6328 || (h->type != STT_GNU_IFUNC
6329 && local
3e04d765
AM
6330 && (htab->can_convert_all_inline_plt
6331 || (((struct ppc_link_hash_entry *) h)->tls_mask
6332 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6333 {
411e1bfb 6334 h->plt.plist = NULL;
f5385ebf 6335 h->needs_plt = 0;
d1eca1e4 6336 h->pointer_equality_needed = 0;
40b8271b 6337 }
8a2058b5 6338 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6339 {
d1eca1e4
AM
6340 /* Taking a function's address in a read/write section
6341 doesn't require us to define the function symbol in the
6342 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6343 be used instead. The reason we prefer a few more dynamic
6344 relocs is that calling via a global entry stub costs a
6345 few more instructions, and pointer_equality_needed causes
6346 extra work in ld.so when resolving these symbols. */
529fe20e 6347 if (global_entry_stub (h))
d1eca1e4 6348 {
ab2477e1 6349 if (!readonly_dynrelocs (h))
529fe20e
AM
6350 {
6351 h->pointer_equality_needed = 0;
04383fd1
AM
6352 /* If we haven't seen a branch reloc and the symbol
6353 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6354 if (!h->needs_plt)
6355 h->plt.plist = NULL;
6356 }
6357 else if (!bfd_link_pic (info))
6358 /* We are going to be defining the function symbol on the
6359 plt stub, so no dyn_relocs needed when non-pic. */
6360 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
6361 }
6362
3988aed5
AM
6363 /* ELFv2 function symbols can't have copy relocs. */
6364 return TRUE;
6365 }
6366 else if (!h->needs_plt
ab2477e1 6367 && !readonly_dynrelocs (h))
3988aed5 6368 {
04383fd1
AM
6369 /* If we haven't seen a branch reloc and the symbol isn't an
6370 ifunc then we don't need a plt entry. */
3988aed5
AM
6371 h->plt.plist = NULL;
6372 h->pointer_equality_needed = 0;
a345bc8d
AM
6373 return TRUE;
6374 }
5bd4f169 6375 }
bbd7ec4a 6376 else
411e1bfb 6377 h->plt.plist = NULL;
5bd4f169
AM
6378
6379 /* If this is a weak symbol, and there is a real definition, the
6380 processor independent code will have arranged for us to see the
6381 real definition first, and we can just use the same value. */
60d67dc8 6382 if (h->is_weakalias)
5bd4f169 6383 {
60d67dc8
AM
6384 struct elf_link_hash_entry *def = weakdef (h);
6385 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6386 h->root.u.def.section = def->root.u.def.section;
6387 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6388 if (def->root.u.def.section == htab->elf.sdynbss
6389 || def->root.u.def.section == htab->elf.sdynrelro)
6390 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 6391 return TRUE;
5bd4f169
AM
6392 }
6393
5bd4f169
AM
6394 /* If we are creating a shared library, we must presume that the
6395 only references to the symbol are via the global offset table.
6396 For such cases we need not do anything here; the relocations will
6397 be handled correctly by relocate_section. */
0e1862bb 6398 if (bfd_link_pic (info))
b34976b6 6399 return TRUE;
5bd4f169 6400
65f38f15
AM
6401 /* If there are no references to this symbol that do not use the
6402 GOT, we don't need to generate a copy reloc. */
f5385ebf 6403 if (!h->non_got_ref)
b34976b6 6404 return TRUE;
65f38f15 6405
b186458a 6406 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6407 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6408
d93d1c80
AM
6409 /* If -z nocopyreloc was given, don't generate them either. */
6410 || info->nocopyreloc
a127494f 6411
dce2246a 6412 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6413 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 6414 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 6415
d93d1c80
AM
6416 /* Protected variables do not work with .dynbss. The copy in
6417 .dynbss won't be used by the shared library with the protected
6418 definition for the variable. Text relocations are preferable
6419 to an incorrect program. */
6420 || h->protected_def)
529fe20e 6421 return TRUE;
a127494f 6422
5d35169e 6423 if (h->plt.plist != NULL)
97b639ba
AM
6424 {
6425 /* We should never get here, but unfortunately there are versions
6426 of gcc out there that improperly (for this ABI) put initialized
6427 function pointers, vtable refs and suchlike in read-only
6428 sections. Allow them to proceed, but warn that this might
6429 break at runtime. */
25f53a85 6430 info->callbacks->einfo
c1c8c1ef 6431 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6432 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6433 h->root.root.string);
6434 }
5d35169e
AM
6435
6436 /* This is a reference to a symbol defined by a dynamic object which
6437 is not a function. */
6438
5bd4f169
AM
6439 /* We must allocate the symbol in our .dynbss section, which will
6440 become part of the .bss section of the executable. There will be
6441 an entry for this symbol in the .dynsym section. The dynamic
6442 object will contain position independent code, so all references
6443 from the dynamic object to this symbol will go through the global
6444 offset table. The dynamic linker will use the .dynsym entry to
6445 determine the address it must put in the global offset table, so
6446 both the dynamic object and the regular object will refer to the
6447 same memory location for the variable. */
5474d94f
AM
6448 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6449 {
6450 s = htab->elf.sdynrelro;
6451 srel = htab->elf.sreldynrelro;
6452 }
6453 else
6454 {
6455 s = htab->elf.sdynbss;
6456 srel = htab->elf.srelbss;
6457 }
1d7e9d18 6458 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6459 {
4a7e5234
AM
6460 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6461 linker to copy the initial value out of the dynamic object
6462 and into the runtime process image. */
5474d94f 6463 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6464 h->needs_copy = 1;
5bd4f169
AM
6465 }
6466
529fe20e
AM
6467 /* We no longer want dyn_relocs. */
6468 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 6469 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6470}
6471
e86ce104
AM
6472/* If given a function descriptor symbol, hide both the function code
6473 sym and the descriptor. */
6474static void
4ce794b7
AM
6475ppc64_elf_hide_symbol (struct bfd_link_info *info,
6476 struct elf_link_hash_entry *h,
6477 bfd_boolean force_local)
e86ce104 6478{
34814b9f 6479 struct ppc_link_hash_entry *eh;
e86ce104
AM
6480 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6481
87469ba2
AM
6482 if (ppc_hash_table (info) == NULL)
6483 return;
6484
34814b9f
AM
6485 eh = (struct ppc_link_hash_entry *) h;
6486 if (eh->is_func_descriptor)
e86ce104 6487 {
34814b9f 6488 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6489
721956f4 6490 if (fh == NULL)
d1329ca3
AM
6491 {
6492 const char *p, *q;
b8ac2841 6493 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6494 char save;
6495
6496 /* We aren't supposed to use alloca in BFD because on
6497 systems which do not have alloca the version in libiberty
6498 calls xmalloc, which might cause the program to crash
6499 when it runs out of memory. This function doesn't have a
6500 return status, so there's no way to gracefully return an
6501 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6502 accessed; It's either a string in an ELF string table,
6503 or allocated in an objalloc structure. */
d1329ca3 6504
34814b9f 6505 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6506 save = *p;
6507 *(char *) p = '.';
34814b9f 6508 fh = (struct ppc_link_hash_entry *)
b8ac2841 6509 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6510 *(char *) p = save;
6511
6512 /* Unfortunately, if it so happens that the string we were
6513 looking for was allocated immediately before this string,
6514 then we overwrote the string terminator. That's the only
6515 reason the lookup should fail. */
6516 if (fh == NULL)
6517 {
34814b9f
AM
6518 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6519 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6520 --q, --p;
34814b9f
AM
6521 if (q < eh->elf.root.root.string && *p == '.')
6522 fh = (struct ppc_link_hash_entry *)
b8ac2841 6523 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6524 }
6525 if (fh != NULL)
6526 {
34814b9f
AM
6527 eh->oh = fh;
6528 fh->oh = eh;
d1329ca3
AM
6529 }
6530 }
e86ce104 6531 if (fh != NULL)
34814b9f 6532 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6533 }
6534}
6535
411e1bfb 6536static bfd_boolean
8843416a
AM
6537get_sym_h (struct elf_link_hash_entry **hp,
6538 Elf_Internal_Sym **symp,
6539 asection **symsecp,
f961d9dd 6540 unsigned char **tls_maskp,
8843416a
AM
6541 Elf_Internal_Sym **locsymsp,
6542 unsigned long r_symndx,
6543 bfd *ibfd)
411e1bfb 6544{
0ffa91dd 6545 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6546
6547 if (r_symndx >= symtab_hdr->sh_info)
6548 {
6549 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6550 struct elf_link_hash_entry *h;
6551
6552 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6553 h = elf_follow_link (h);
411e1bfb
AM
6554
6555 if (hp != NULL)
6556 *hp = h;
6557
6558 if (symp != NULL)
6559 *symp = NULL;
6560
6561 if (symsecp != NULL)
6562 {
6563 asection *symsec = NULL;
6564 if (h->root.type == bfd_link_hash_defined
6565 || h->root.type == bfd_link_hash_defweak)
6566 symsec = h->root.u.def.section;
6567 *symsecp = symsec;
6568 }
6569
e7b938ca 6570 if (tls_maskp != NULL)
411e1bfb
AM
6571 {
6572 struct ppc_link_hash_entry *eh;
6573
6574 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6575 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6576 }
6577 }
6578 else
6579 {
6580 Elf_Internal_Sym *sym;
6581 Elf_Internal_Sym *locsyms = *locsymsp;
6582
6583 if (locsyms == NULL)
6584 {
6585 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6586 if (locsyms == NULL)
6587 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6588 symtab_hdr->sh_info,
6589 0, NULL, NULL, NULL);
6590 if (locsyms == NULL)
6591 return FALSE;
6592 *locsymsp = locsyms;
6593 }
6594 sym = locsyms + r_symndx;
6595
6596 if (hp != NULL)
6597 *hp = NULL;
6598
6599 if (symp != NULL)
6600 *symp = sym;
6601
6602 if (symsecp != NULL)
cb33740c 6603 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6604
e7b938ca 6605 if (tls_maskp != NULL)
411e1bfb
AM
6606 {
6607 struct got_entry **lgot_ents;
f961d9dd 6608 unsigned char *tls_mask;
411e1bfb 6609
e7b938ca 6610 tls_mask = NULL;
411e1bfb
AM
6611 lgot_ents = elf_local_got_ents (ibfd);
6612 if (lgot_ents != NULL)
6613 {
e054468f
AM
6614 struct plt_entry **local_plt = (struct plt_entry **)
6615 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6616 unsigned char *lgot_masks = (unsigned char *)
e054468f 6617 (local_plt + symtab_hdr->sh_info);
e7b938ca 6618 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6619 }
e7b938ca 6620 *tls_maskp = tls_mask;
411e1bfb
AM
6621 }
6622 }
6623 return TRUE;
6624}
6625
e7b938ca 6626/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6627 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6628 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6629
6630static int
f961d9dd 6631get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6632 unsigned long *toc_symndx,
6633 bfd_vma *toc_addend,
0d4792f7 6634 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6635 const Elf_Internal_Rela *rel,
6636 bfd *ibfd)
411e1bfb
AM
6637{
6638 unsigned long r_symndx;
0d4792f7 6639 int next_r;
411e1bfb
AM
6640 struct elf_link_hash_entry *h;
6641 Elf_Internal_Sym *sym;
6642 asection *sec;
6643 bfd_vma off;
6644
6645 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6646 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6647 return 0;
411e1bfb 6648
37da22e5
AM
6649 if ((*tls_maskp != NULL
6650 && (**tls_maskp & TLS_TLS) != 0
6651 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 6652 || sec == NULL
6bee8834 6653 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6654 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6655 return 1;
411e1bfb
AM
6656
6657 /* Look inside a TOC section too. */
6658 if (h != NULL)
6659 {
6660 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6661 off = h->root.u.def.value;
6662 }
6663 else
6664 off = sym->st_value;
6665 off += rel->r_addend;
6666 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6667 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6668 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6669 if (toc_symndx != NULL)
6670 *toc_symndx = r_symndx;
3a71aa26
AM
6671 if (toc_addend != NULL)
6672 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6673 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6674 return 0;
854b41e7 6675 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6676 && (next_r == -1 || next_r == -2))
6677 return 1 - next_r;
951fd09b 6678 return 1;
411e1bfb
AM
6679}
6680
3b421ab3
AM
6681/* Find (or create) an entry in the tocsave hash table. */
6682
6683static struct tocsave_entry *
6684tocsave_find (struct ppc_link_hash_table *htab,
6685 enum insert_option insert,
6686 Elf_Internal_Sym **local_syms,
6687 const Elf_Internal_Rela *irela,
6688 bfd *ibfd)
6689{
6690 unsigned long r_indx;
6691 struct elf_link_hash_entry *h;
6692 Elf_Internal_Sym *sym;
6693 struct tocsave_entry ent, *p;
6694 hashval_t hash;
6695 struct tocsave_entry **slot;
6696
6697 r_indx = ELF64_R_SYM (irela->r_info);
6698 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6699 return NULL;
6700 if (ent.sec == NULL || ent.sec->output_section == NULL)
6701 {
4eca0228 6702 _bfd_error_handler
871b3ab2 6703 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
6704 return NULL;
6705 }
6706
6707 if (h != NULL)
6708 ent.offset = h->root.u.def.value;
6709 else
6710 ent.offset = sym->st_value;
6711 ent.offset += irela->r_addend;
6712
6713 hash = tocsave_htab_hash (&ent);
6714 slot = ((struct tocsave_entry **)
6715 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6716 if (slot == NULL)
6717 return NULL;
6718
6719 if (*slot == NULL)
6720 {
6721 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6722 if (p == NULL)
6723 return NULL;
6724 *p = ent;
6725 *slot = p;
6726 }
6727 return *slot;
6728}
6729
754021d0 6730/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6731 code for the old ABI, these will already have been done. */
754021d0
AM
6732
6733static bfd_boolean
6734adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6735{
6736 struct ppc_link_hash_entry *eh;
6737 asection *sym_sec;
74f0fb50 6738 struct _opd_sec_data *opd;
754021d0
AM
6739
6740 if (h->root.type == bfd_link_hash_indirect)
6741 return TRUE;
6742
754021d0
AM
6743 if (h->root.type != bfd_link_hash_defined
6744 && h->root.type != bfd_link_hash_defweak)
6745 return TRUE;
6746
6747 eh = (struct ppc_link_hash_entry *) h;
6748 if (eh->adjust_done)
6749 return TRUE;
6750
6751 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6752 opd = get_opd_info (sym_sec);
6753 if (opd != NULL && opd->adjust != NULL)
754021d0 6754 {
51aecdc5 6755 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
6756 if (adjust == -1)
6757 {
6758 /* This entry has been deleted. */
b3fac117 6759 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6760 if (dsec == NULL)
6761 {
6762 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 6763 if (discarded_section (dsec))
81688140 6764 {
b3fac117 6765 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6766 break;
6767 }
6768 }
4025353c 6769 eh->elf.root.u.def.value = 0;
81688140 6770 eh->elf.root.u.def.section = dsec;
4025353c
AM
6771 }
6772 else
6773 eh->elf.root.u.def.value += adjust;
754021d0
AM
6774 eh->adjust_done = 1;
6775 }
6776 return TRUE;
6777}
6778
8c1d1bb8 6779/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 6780 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
6781 have already been determined. */
6782
6783static bfd_boolean
6784dec_dynrel_count (bfd_vma r_info,
6785 asection *sec,
6786 struct bfd_link_info *info,
6787 Elf_Internal_Sym **local_syms,
6788 struct elf_link_hash_entry *h,
19e08130 6789 Elf_Internal_Sym *sym)
8c1d1bb8
AM
6790{
6791 enum elf_ppc64_reloc_type r_type;
19e08130 6792 asection *sym_sec = NULL;
8c1d1bb8
AM
6793
6794 /* Can this reloc be dynamic? This switch, and later tests here
6795 should be kept in sync with the code in check_relocs. */
6796 r_type = ELF64_R_TYPE (r_info);
6797 switch (r_type)
6798 {
6799 default:
6800 return TRUE;
6801
6802 case R_PPC64_TPREL16:
6803 case R_PPC64_TPREL16_LO:
6804 case R_PPC64_TPREL16_HI:
6805 case R_PPC64_TPREL16_HA:
6806 case R_PPC64_TPREL16_DS:
6807 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
6808 case R_PPC64_TPREL16_HIGH:
6809 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
6810 case R_PPC64_TPREL16_HIGHER:
6811 case R_PPC64_TPREL16_HIGHERA:
6812 case R_PPC64_TPREL16_HIGHEST:
6813 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8 6814 case R_PPC64_TPREL64:
c213164a 6815 case R_PPC64_TPREL34:
8c1d1bb8
AM
6816 case R_PPC64_DTPMOD64:
6817 case R_PPC64_DTPREL64:
6818 case R_PPC64_ADDR64:
6819 case R_PPC64_REL30:
6820 case R_PPC64_REL32:
6821 case R_PPC64_REL64:
6822 case R_PPC64_ADDR14:
6823 case R_PPC64_ADDR14_BRNTAKEN:
6824 case R_PPC64_ADDR14_BRTAKEN:
6825 case R_PPC64_ADDR16:
6826 case R_PPC64_ADDR16_DS:
6827 case R_PPC64_ADDR16_HA:
6828 case R_PPC64_ADDR16_HI:
f9c6b907
AM
6829 case R_PPC64_ADDR16_HIGH:
6830 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
6831 case R_PPC64_ADDR16_HIGHER:
6832 case R_PPC64_ADDR16_HIGHERA:
6833 case R_PPC64_ADDR16_HIGHEST:
6834 case R_PPC64_ADDR16_HIGHESTA:
6835 case R_PPC64_ADDR16_LO:
6836 case R_PPC64_ADDR16_LO_DS:
6837 case R_PPC64_ADDR24:
6838 case R_PPC64_ADDR32:
6839 case R_PPC64_UADDR16:
6840 case R_PPC64_UADDR32:
6841 case R_PPC64_UADDR64:
6842 case R_PPC64_TOC:
5663e321
AM
6843 case R_PPC64_D34:
6844 case R_PPC64_D34_LO:
6845 case R_PPC64_D34_HI30:
6846 case R_PPC64_D34_HA30:
6847 case R_PPC64_ADDR16_HIGHER34:
6848 case R_PPC64_ADDR16_HIGHERA34:
6849 case R_PPC64_ADDR16_HIGHEST34:
6850 case R_PPC64_ADDR16_HIGHESTA34:
6851 case R_PPC64_D28:
8c1d1bb8
AM
6852 break;
6853 }
6854
6855 if (local_syms != NULL)
6856 {
6857 unsigned long r_symndx;
8c1d1bb8
AM
6858 bfd *ibfd = sec->owner;
6859
6860 r_symndx = ELF64_R_SYM (r_info);
6861 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6862 return FALSE;
6863 }
6864
0e1862bb 6865 if ((bfd_link_pic (info)
1d483afe 6866 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 6867 || (h != NULL
198f1157 6868 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
6869 || h->root.type == bfd_link_hash_defweak
6870 || !h->def_regular))))
6871 || (ELIMINATE_COPY_RELOCS
0e1862bb 6872 && !bfd_link_pic (info)
8c1d1bb8
AM
6873 && h != NULL
6874 && (h->root.type == bfd_link_hash_defweak
6875 || !h->def_regular)))
6876 ;
6877 else
6878 return TRUE;
6879
6880 if (h != NULL)
6edfbbad 6881 {
19e08130
AM
6882 struct elf_dyn_relocs *p;
6883 struct elf_dyn_relocs **pp;
6884 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6885
6886 /* elf_gc_sweep may have already removed all dyn relocs associated
6887 with local syms for a given section. Also, symbol flags are
6888 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6889 report a dynreloc miscount. */
6890 if (*pp == NULL && info->gc_sections)
6891 return TRUE;
6892
6893 while ((p = *pp) != NULL)
60124e18 6894 {
19e08130
AM
6895 if (p->sec == sec)
6896 {
6897 if (!must_be_dyn_reloc (info, r_type))
6898 p->pc_count -= 1;
6899 p->count -= 1;
6900 if (p->count == 0)
6901 *pp = p->next;
6902 return TRUE;
6903 }
6904 pp = &p->next;
60124e18 6905 }
6edfbbad 6906 }
19e08130
AM
6907 else
6908 {
6909 struct ppc_dyn_relocs *p;
6910 struct ppc_dyn_relocs **pp;
6911 void *vpp;
6912 bfd_boolean is_ifunc;
8c1d1bb8 6913
19e08130
AM
6914 if (local_syms == NULL)
6915 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6916 if (sym_sec == NULL)
6917 sym_sec = sec;
c57da1a7 6918
19e08130
AM
6919 vpp = &elf_section_data (sym_sec)->local_dynrel;
6920 pp = (struct ppc_dyn_relocs **) vpp;
6921
6922 if (*pp == NULL && info->gc_sections)
6923 return TRUE;
6924
6925 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6926 while ((p = *pp) != NULL)
8c1d1bb8 6927 {
19e08130
AM
6928 if (p->sec == sec && p->ifunc == is_ifunc)
6929 {
6930 p->count -= 1;
6931 if (p->count == 0)
6932 *pp = p->next;
6933 return TRUE;
6934 }
6935 pp = &p->next;
8c1d1bb8 6936 }
8c1d1bb8
AM
6937 }
6938
695344c0 6939 /* xgettext:c-format */
cf97bcb0
AM
6940 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6941 sec->owner, sec);
8c1d1bb8
AM
6942 bfd_set_error (bfd_error_bad_value);
6943 return FALSE;
6944}
6945
754021d0
AM
6946/* Remove unused Official Procedure Descriptor entries. Currently we
6947 only remove those associated with functions in discarded link-once
6948 sections, or weakly defined functions that have been overridden. It
6949 would be possible to remove many more entries for statically linked
6950 applications. */
6951
b34976b6 6952bfd_boolean
e7d1c40c 6953ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
6954{
6955 bfd *ibfd;
754021d0 6956 bfd_boolean some_edited = FALSE;
3f764659 6957 asection *need_pad = NULL;
e7d1c40c
AM
6958 struct ppc_link_hash_table *htab;
6959
6960 htab = ppc_hash_table (info);
6961 if (htab == NULL)
6962 return FALSE;
1e2f5b6e 6963
c72f2fb2 6964 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
6965 {
6966 asection *sec;
6967 Elf_Internal_Rela *relstart, *rel, *relend;
6968 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 6969 Elf_Internal_Sym *local_syms;
74f0fb50 6970 struct _opd_sec_data *opd;
51aecdc5 6971 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 6972 bfd_size_type cnt_16b = 0;
1e2f5b6e 6973
854b41e7
AM
6974 if (!is_ppc64_elf (ibfd))
6975 continue;
6976
1e2f5b6e 6977 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 6978 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
6979 continue;
6980
dbaa2011 6981 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
6982 continue;
6983
1e2f5b6e
AM
6984 if (sec->output_section == bfd_abs_section_ptr)
6985 continue;
6986
6987 /* Look through the section relocs. */
6988 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6989 continue;
6990
6cdc0ccc 6991 local_syms = NULL;
0ffa91dd 6992 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
6993
6994 /* Read the relocations. */
4ce794b7 6995 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 6996 info->keep_memory);
1e2f5b6e 6997 if (relstart == NULL)
b34976b6 6998 return FALSE;
1e2f5b6e
AM
6999
7000 /* First run through the relocs to check they are sane, and to
7001 determine whether we need to edit this opd section. */
b34976b6 7002 need_edit = FALSE;
51aecdc5 7003 broken = FALSE;
3f764659 7004 need_pad = sec;
1e2f5b6e 7005 relend = relstart + sec->reloc_count;
50bc7936 7006 for (rel = relstart; rel < relend; )
1e2f5b6e 7007 {
04c9666a 7008 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7009 unsigned long r_symndx;
7010 asection *sym_sec;
7011 struct elf_link_hash_entry *h;
7012 Elf_Internal_Sym *sym;
51aecdc5 7013 bfd_vma offset;
1e2f5b6e 7014
51aecdc5 7015 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7016 only interested in the reloc pointing to a function entry
7017 point. */
51aecdc5
AM
7018 offset = rel->r_offset;
7019 if (rel + 1 == relend
7020 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7021 {
7022 /* If someone messes with .opd alignment then after a
7023 "ld -r" we might have padding in the middle of .opd.
7024 Also, there's nothing to prevent someone putting
7025 something silly in .opd with the assembler. No .opd
b34976b6 7026 optimization for them! */
3f764659 7027 broken_opd:
4eca0228 7028 _bfd_error_handler
871b3ab2 7029 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7030 broken = TRUE;
1e2f5b6e
AM
7031 break;
7032 }
7033
50bc7936
AM
7034 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7035 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7036 {
4eca0228 7037 _bfd_error_handler
695344c0 7038 /* xgettext:c-format */
871b3ab2 7039 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7040 ibfd, r_type);
51aecdc5 7041 broken = TRUE;
50bc7936
AM
7042 break;
7043 }
7044
1e2f5b6e 7045 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7046 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7047 r_symndx, ibfd))
50bc7936 7048 goto error_ret;
1e2f5b6e
AM
7049
7050 if (sym_sec == NULL || sym_sec->owner == NULL)
7051 {
411e1bfb
AM
7052 const char *sym_name;
7053 if (h != NULL)
7054 sym_name = h->root.root.string;
7055 else
26c61ae5
L
7056 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7057 sym_sec);
411e1bfb 7058
4eca0228 7059 _bfd_error_handler
695344c0 7060 /* xgettext:c-format */
871b3ab2 7061 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7062 ibfd, sym_name);
51aecdc5 7063 broken = TRUE;
1e2f5b6e
AM
7064 break;
7065 }
7066
51020317
AM
7067 /* opd entries are always for functions defined in the
7068 current input bfd. If the symbol isn't defined in the
7069 input bfd, then we won't be using the function in this
7070 bfd; It must be defined in a linkonce section in another
7071 bfd, or is weak. It's also possible that we are
7072 discarding the function due to a linker script /DISCARD/,
7073 which we test for via the output_section. */
7074 if (sym_sec->owner != ibfd
7075 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7076 need_edit = TRUE;
1e2f5b6e 7077
50bc7936 7078 rel += 2;
51aecdc5
AM
7079 if (rel + 1 == relend
7080 || (rel + 2 < relend
7081 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7082 ++rel;
7083
7084 if (rel == relend)
3f764659
JJ
7085 {
7086 if (sec->size == offset + 24)
7087 {
7088 need_pad = NULL;
7089 break;
7090 }
51aecdc5 7091 if (sec->size == offset + 16)
3f764659
JJ
7092 {
7093 cnt_16b++;
7094 break;
7095 }
7096 goto broken_opd;
7097 }
3f764659
JJ
7098 else if (rel + 1 < relend
7099 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7100 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7101 {
51aecdc5
AM
7102 if (rel[0].r_offset == offset + 16)
7103 cnt_16b++;
7104 else if (rel[0].r_offset != offset + 24)
7105 goto broken_opd;
3f764659
JJ
7106 }
7107 else
7108 goto broken_opd;
1e2f5b6e
AM
7109 }
7110
e7d1c40c 7111 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7112
51aecdc5 7113 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7114 {
7115 Elf_Internal_Rela *write_rel;
d4730f92 7116 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7117 bfd_byte *rptr, *wptr;
983bddc8 7118 bfd_byte *new_contents;
74f0fb50
AM
7119 bfd_size_type amt;
7120
983bddc8 7121 new_contents = NULL;
51aecdc5 7122 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7123 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7124 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7125 if (opd->adjust == NULL)
7126 return FALSE;
1e2f5b6e
AM
7127
7128 /* This seems a waste of time as input .opd sections are all
7129 zeros as generated by gcc, but I suppose there's no reason
7130 this will always be so. We might start putting something in
7131 the third word of .opd entries. */
7132 if ((sec->flags & SEC_IN_MEMORY) == 0)
7133 {
eea6121a
AM
7134 bfd_byte *loc;
7135 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7136 {
eea6121a
AM
7137 if (loc != NULL)
7138 free (loc);
50bc7936 7139 error_ret:
6cdc0ccc
AM
7140 if (local_syms != NULL
7141 && symtab_hdr->contents != (unsigned char *) local_syms)
7142 free (local_syms);
6cdc0ccc
AM
7143 if (elf_section_data (sec)->relocs != relstart)
7144 free (relstart);
b34976b6 7145 return FALSE;
6cdc0ccc 7146 }
1e2f5b6e
AM
7147 sec->contents = loc;
7148 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7149 }
7150
7151 elf_section_data (sec)->relocs = relstart;
7152
3f764659 7153 new_contents = sec->contents;
3f764659
JJ
7154 if (add_aux_fields)
7155 {
7156 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7157 if (new_contents == NULL)
7158 return FALSE;
51aecdc5 7159 need_pad = NULL;
3f764659 7160 }
b4f4e59f
AM
7161 wptr = new_contents;
7162 rptr = sec->contents;
1e2f5b6e 7163 write_rel = relstart;
51aecdc5 7164 for (rel = relstart; rel < relend; )
1e2f5b6e 7165 {
50bc7936
AM
7166 unsigned long r_symndx;
7167 asection *sym_sec;
7168 struct elf_link_hash_entry *h;
51aecdc5 7169 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7170 Elf_Internal_Sym *sym;
51aecdc5
AM
7171 long opd_ent_size;
7172 Elf_Internal_Rela *next_rel;
7173 bfd_boolean skip;
50bc7936
AM
7174
7175 r_symndx = ELF64_R_SYM (rel->r_info);
7176 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7177 r_symndx, ibfd))
50bc7936
AM
7178 goto error_ret;
7179
51aecdc5
AM
7180 next_rel = rel + 2;
7181 if (next_rel + 1 == relend
7182 || (next_rel + 2 < relend
7183 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7184 ++next_rel;
7185
7186 /* See if the .opd entry is full 24 byte or
7187 16 byte (with fd_aux entry overlapped with next
7188 fd_func). */
7189 opd_ent_size = 24;
7190 if (next_rel == relend)
1e2f5b6e 7191 {
51aecdc5 7192 if (sec->size == rel->r_offset + 16)
3f764659 7193 opd_ent_size = 16;
51aecdc5
AM
7194 }
7195 else if (next_rel->r_offset == rel->r_offset + 16)
7196 opd_ent_size = 16;
3f764659 7197
51aecdc5
AM
7198 if (h != NULL
7199 && h->root.root.string[0] == '.')
7200 {
8c5b4e52
AM
7201 fdh = ((struct ppc_link_hash_entry *) h)->oh;
7202 if (fdh != NULL)
7203 {
7204 fdh = ppc_follow_link (fdh);
7205 if (fdh->elf.root.type != bfd_link_hash_defined
7206 && fdh->elf.root.type != bfd_link_hash_defweak)
7207 fdh = NULL;
7208 }
51aecdc5 7209 }
1e2f5b6e 7210
51aecdc5
AM
7211 skip = (sym_sec->owner != ibfd
7212 || sym_sec->output_section == bfd_abs_section_ptr);
7213 if (skip)
7214 {
7215 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7216 {
51aecdc5
AM
7217 /* Arrange for the function descriptor sym
7218 to be dropped. */
7219 fdh->elf.root.u.def.value = 0;
7220 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7221 }
51aecdc5 7222 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7223
0e1862bb 7224 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7225 rel = next_rel;
7226 else
7227 while (1)
7228 {
7229 if (!dec_dynrel_count (rel->r_info, sec, info,
7230 NULL, h, sym))
7231 goto error_ret;
754021d0 7232
51aecdc5
AM
7233 if (++rel == next_rel)
7234 break;
1e2f5b6e 7235
51aecdc5
AM
7236 r_symndx = ELF64_R_SYM (rel->r_info);
7237 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7238 r_symndx, ibfd))
7239 goto error_ret;
7240 }
50bc7936
AM
7241 }
7242 else
1e2f5b6e 7243 {
51aecdc5
AM
7244 /* We'll be keeping this opd entry. */
7245 long adjust;
7246
7247 if (fdh != NULL)
7248 {
7249 /* Redefine the function descriptor symbol to
7250 this location in the opd section. It is
7251 necessary to update the value here rather
7252 than using an array of adjustments as we do
7253 for local symbols, because various places
7254 in the generic ELF code use the value
7255 stored in u.def.value. */
7256 fdh->elf.root.u.def.value = wptr - new_contents;
7257 fdh->adjust_done = 1;
7258 }
7259
7260 /* Local syms are a bit tricky. We could
7261 tweak them as they can be cached, but
7262 we'd need to look through the local syms
7263 for the function descriptor sym which we
7264 don't have at the moment. So keep an
7265 array of adjustments. */
7266 adjust = (wptr - new_contents) - (rptr - sec->contents);
7267 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7268
7269 if (wptr != rptr)
7270 memcpy (wptr, rptr, opd_ent_size);
7271 wptr += opd_ent_size;
7272 if (add_aux_fields && opd_ent_size == 16)
7273 {
7274 memset (wptr, '\0', 8);
7275 wptr += 8;
7276 }
7277
50bc7936 7278 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7279 new opd entries. */
7280 for ( ; rel != next_rel; ++rel)
7281 {
7282 rel->r_offset += adjust;
7283 if (write_rel != rel)
7284 memcpy (write_rel, rel, sizeof (*rel));
7285 ++write_rel;
7286 }
1e2f5b6e 7287 }
51aecdc5
AM
7288
7289 rptr += opd_ent_size;
1e2f5b6e
AM
7290 }
7291
3f764659 7292 sec->size = wptr - new_contents;
1e2f5b6e 7293 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7294 if (add_aux_fields)
7295 {
7296 free (sec->contents);
7297 sec->contents = new_contents;
7298 }
7299
05bf9422 7300 /* Fudge the header size too, as this is used later in
cdcf6e38 7301 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7302 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7303 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7304 some_edited = TRUE;
1e2f5b6e 7305 }
6cdc0ccc 7306 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7307 free (relstart);
6cdc0ccc 7308
411e1bfb
AM
7309 if (local_syms != NULL
7310 && symtab_hdr->contents != (unsigned char *) local_syms)
7311 {
7312 if (!info->keep_memory)
7313 free (local_syms);
7314 else
7315 symtab_hdr->contents = (unsigned char *) local_syms;
7316 }
7317 }
7318
754021d0
AM
7319 if (some_edited)
7320 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7321
3f764659
JJ
7322 /* If we are doing a final link and the last .opd entry is just 16 byte
7323 long, add a 8 byte padding after it. */
0e1862bb 7324 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7325 {
7326 bfd_byte *p;
7327
7328 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7329 {
7330 BFD_ASSERT (need_pad->size > 0);
7331
7332 p = bfd_malloc (need_pad->size + 8);
7333 if (p == NULL)
7334 return FALSE;
699733f6 7335
2cdcc330
AM
7336 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7337 p, 0, need_pad->size))
3f764659
JJ
7338 return FALSE;
7339
7340 need_pad->contents = p;
7341 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7342 }
7343 else
7344 {
7345 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7346 if (p == NULL)
7347 return FALSE;
7348
7349 need_pad->contents = p;
7350 }
7351
7352 memset (need_pad->contents + need_pad->size, 0, 8);
7353 need_pad->size += 8;
7354 }
7355
411e1bfb
AM
7356 return TRUE;
7357}
7358
3e04d765
AM
7359/* Analyze inline PLT call relocations to see whether calls to locally
7360 defined functions can be converted to direct calls. */
7361
7362bfd_boolean
7363ppc64_elf_inline_plt (struct bfd_link_info *info)
7364{
7365 struct ppc_link_hash_table *htab;
7366 bfd *ibfd;
7367 asection *sec;
7368 bfd_vma low_vma, high_vma, limit;
7369
7370 htab = ppc_hash_table (info);
7371 if (htab == NULL)
7372 return FALSE;
7373
7374 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7375 reduced somewhat to cater for possible stubs that might be added
7376 between the call and its destination. */
7377 if (htab->params->group_size < 0)
7378 {
7379 limit = -htab->params->group_size;
7380 if (limit == 1)
7381 limit = 0x1e00000;
7382 }
7383 else
7384 {
7385 limit = htab->params->group_size;
7386 if (limit == 1)
7387 limit = 0x1c00000;
7388 }
7389
7390 low_vma = -1;
7391 high_vma = 0;
7392 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7393 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7394 {
7395 if (low_vma > sec->vma)
7396 low_vma = sec->vma;
7397 if (high_vma < sec->vma + sec->size)
7398 high_vma = sec->vma + sec->size;
7399 }
7400
7401 /* If a "bl" can reach anywhere in local code sections, then we can
7402 convert all inline PLT sequences to direct calls when the symbol
7403 is local. */
7404 if (high_vma - low_vma < limit)
7405 {
7406 htab->can_convert_all_inline_plt = 1;
7407 return TRUE;
7408 }
7409
7410 /* Otherwise, go looking through relocs for cases where a direct
7411 call won't reach. Mark the symbol on any such reloc to disable
7412 the optimization and keep the PLT entry as it seems likely that
7413 this will be better than creating trampolines. Note that this
7414 will disable the optimization for all inline PLT calls to a
7415 particular symbol, not just those that won't reach. The
7416 difficulty in doing a more precise optimization is that the
7417 linker needs to make a decision depending on whether a
7418 particular R_PPC64_PLTCALL insn can be turned into a direct
7419 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7420 the sequence, and there is nothing that ties those relocs
7421 together except their symbol. */
7422
7423 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7424 {
7425 Elf_Internal_Shdr *symtab_hdr;
7426 Elf_Internal_Sym *local_syms;
7427
7428 if (!is_ppc64_elf (ibfd))
7429 continue;
7430
7431 local_syms = NULL;
7432 symtab_hdr = &elf_symtab_hdr (ibfd);
7433
7434 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7435 if (ppc64_elf_section_data (sec)->has_pltcall
7436 && !bfd_is_abs_section (sec->output_section))
7437 {
7438 Elf_Internal_Rela *relstart, *rel, *relend;
7439
7440 /* Read the relocations. */
7441 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7442 info->keep_memory);
7443 if (relstart == NULL)
7444 return FALSE;
7445
7446 relend = relstart + sec->reloc_count;
7447 for (rel = relstart; rel < relend; )
7448 {
7449 enum elf_ppc64_reloc_type r_type;
7450 unsigned long r_symndx;
7451 asection *sym_sec;
7452 struct elf_link_hash_entry *h;
7453 Elf_Internal_Sym *sym;
7454 unsigned char *tls_maskp;
7455
7456 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7457 if (r_type != R_PPC64_PLTCALL
7458 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7459 continue;
7460
7461 r_symndx = ELF64_R_SYM (rel->r_info);
7462 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7463 r_symndx, ibfd))
7464 {
7465 if (elf_section_data (sec)->relocs != relstart)
7466 free (relstart);
7467 if (local_syms != NULL
2cdcc330 7468 && symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765
AM
7469 free (local_syms);
7470 return FALSE;
7471 }
7472
7473 if (sym_sec != NULL && sym_sec->output_section != NULL)
7474 {
7475 bfd_vma from, to;
7476 if (h != NULL)
7477 to = h->root.u.def.value;
7478 else
7479 to = sym->st_value;
7480 to += (rel->r_addend
7481 + sym_sec->output_offset
7482 + sym_sec->output_section->vma);
7483 from = (rel->r_offset
7484 + sec->output_offset
7485 + sec->output_section->vma);
5663e321
AM
7486 if (to - from + limit < 2 * limit
7487 && !(r_type == R_PPC64_PLTCALL_NOTOC
7488 && (((h ? h->other : sym->st_other)
7489 & STO_PPC64_LOCAL_MASK)
4a4e7361 7490 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7491 *tls_maskp &= ~PLT_KEEP;
7492 }
7493 }
7494 if (elf_section_data (sec)->relocs != relstart)
7495 free (relstart);
7496 }
7497
7498 if (local_syms != NULL
7499 && symtab_hdr->contents != (unsigned char *) local_syms)
7500 {
7501 if (!info->keep_memory)
7502 free (local_syms);
7503 else
7504 symtab_hdr->contents = (unsigned char *) local_syms;
7505 }
7506 }
7507
7508 return TRUE;
7509}
7510
e1918d23 7511/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7512
e1918d23 7513asection *
e7d1c40c 7514ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7515{
411e1bfb
AM
7516 struct ppc_link_hash_table *htab;
7517
411e1bfb 7518 htab = ppc_hash_table (info);
4dfe6ac6
NC
7519 if (htab == NULL)
7520 return NULL;
7521
ee67d69a
AM
7522 if (abiversion (info->output_bfd) == 1)
7523 htab->opd_abi = 1;
7524
e7d1c40c 7525 if (htab->params->no_multi_toc)
33c0ec9d
AM
7526 htab->do_multi_toc = 0;
7527 else if (!htab->do_multi_toc)
e7d1c40c 7528 htab->params->no_multi_toc = 1;
33c0ec9d 7529
8b5f1ed8
AM
7530 /* Default to --no-plt-localentry, as this option can cause problems
7531 with symbol interposition. For example, glibc libpthread.so and
7532 libc.so duplicate many pthread symbols, with a fallback
7533 implementation in libc.so. In some cases the fallback does more
7534 work than the pthread implementation. __pthread_condattr_destroy
7535 is one such symbol: the libpthread.so implementation is
7536 localentry:0 while the libc.so implementation is localentry:8.
7537 An app that "cleverly" uses dlopen to only load necessary
7538 libraries at runtime may omit loading libpthread.so when not
7539 running multi-threaded, which then results in the libc.so
7540 fallback symbols being used and ld.so complaining. Now there
7541 are workarounds in ld (see non_zero_localentry) to detect the
7542 pthread situation, but that may not be the only case where
7543 --plt-localentry can cause trouble. */
f378ab09 7544 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7545 htab->params->plt_localentry0 = 0;
d44c746a
AM
7546 if (htab->params->plt_localentry0
7547 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7548 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
7549 _bfd_error_handler
7550 (_("warning: --plt-localentry is especially dangerous without "
7551 "ld.so support to detect ABI violations"));
f378ab09 7552
3a71aa26
AM
7553 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7554 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7555 FALSE, FALSE, TRUE));
a7f2871e
AM
7556 /* Move dynamic linking info to the function descriptor sym. */
7557 if (htab->tls_get_addr != NULL)
7558 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7559 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7560 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7561 FALSE, FALSE, TRUE));
7c9cf415 7562 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
7563 {
7564 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7565
7566 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7567 FALSE, FALSE, TRUE);
7568 if (opt != NULL)
7569 func_desc_adjust (opt, info);
7570 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7571 FALSE, FALSE, TRUE);
7572 if (opt_fd != NULL
7573 && (opt_fd->root.type == bfd_link_hash_defined
7574 || opt_fd->root.type == bfd_link_hash_defweak))
7575 {
7576 /* If glibc supports an optimized __tls_get_addr call stub,
7577 signalled by the presence of __tls_get_addr_opt, and we'll
7578 be calling __tls_get_addr via a plt call stub, then
7579 make __tls_get_addr point to __tls_get_addr_opt. */
7580 tga_fd = &htab->tls_get_addr_fd->elf;
7581 if (htab->elf.dynamic_sections_created
7582 && tga_fd != NULL
7583 && (tga_fd->type == STT_FUNC
7584 || tga_fd->needs_plt)
7585 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 7586 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
7587 {
7588 struct plt_entry *ent;
7589
7590 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7591 if (ent->plt.refcount > 0)
7592 break;
7593 if (ent != NULL)
7594 {
7595 tga_fd->root.type = bfd_link_hash_indirect;
7596 tga_fd->root.u.i.link = &opt_fd->root;
7597 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 7598 opt_fd->mark = 1;
a7f2871e
AM
7599 if (opt_fd->dynindx != -1)
7600 {
7601 /* Use __tls_get_addr_opt in dynamic relocations. */
7602 opt_fd->dynindx = -1;
7603 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7604 opt_fd->dynstr_index);
7605 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7606 return NULL;
a7f2871e 7607 }
2cdcc330
AM
7608 htab->tls_get_addr_fd
7609 = (struct ppc_link_hash_entry *) opt_fd;
a7f2871e
AM
7610 tga = &htab->tls_get_addr->elf;
7611 if (opt != NULL && tga != NULL)
7612 {
7613 tga->root.type = bfd_link_hash_indirect;
7614 tga->root.u.i.link = &opt->root;
7615 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 7616 opt->mark = 1;
a7f2871e
AM
7617 _bfd_elf_link_hash_hide_symbol (info, opt,
7618 tga->forced_local);
7619 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7620 }
7621 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7622 htab->tls_get_addr_fd->is_func_descriptor = 1;
7623 if (htab->tls_get_addr != NULL)
7624 {
7625 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7626 htab->tls_get_addr->is_func = 1;
7627 }
7628 }
7629 }
7630 }
7c9cf415
AM
7631 else if (htab->params->tls_get_addr_opt < 0)
7632 htab->params->tls_get_addr_opt = 0;
a7f2871e 7633 }
33c0ec9d 7634 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7635}
8387904d 7636
3a71aa26
AM
7637/* Return TRUE iff REL is a branch reloc with a global symbol matching
7638 HASH1 or HASH2. */
8387904d 7639
3a71aa26
AM
7640static bfd_boolean
7641branch_reloc_hash_match (const bfd *ibfd,
7642 const Elf_Internal_Rela *rel,
7643 const struct ppc_link_hash_entry *hash1,
7644 const struct ppc_link_hash_entry *hash2)
7645{
7646 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7647 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7648 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7649
e054468f 7650 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7651 {
3a71aa26
AM
7652 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7653 struct elf_link_hash_entry *h;
8387904d 7654
3a71aa26 7655 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7656 h = elf_follow_link (h);
3a71aa26
AM
7657 if (h == &hash1->elf || h == &hash2->elf)
7658 return TRUE;
a48ebf4d 7659 }
3a71aa26 7660 return FALSE;
951fd09b 7661}
411e1bfb 7662
951fd09b
AM
7663/* Run through all the TLS relocs looking for optimization
7664 opportunities. The linker has been hacked (see ppc64elf.em) to do
7665 a preliminary section layout so that we know the TLS segment
7666 offsets. We can't optimize earlier because some optimizations need
7667 to know the tp offset, and we need to optimize before allocating
7668 dynamic relocations. */
7669
7670bfd_boolean
33c0ec9d 7671ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7672{
7673 bfd *ibfd;
7674 asection *sec;
7675 struct ppc_link_hash_table *htab;
663a1470 7676 unsigned char *toc_ref;
102890f0 7677 int pass;
951fd09b 7678
3cbc1e5e 7679 if (!bfd_link_executable (info))
411e1bfb
AM
7680 return TRUE;
7681
951fd09b 7682 htab = ppc_hash_table (info);
4dfe6ac6
NC
7683 if (htab == NULL)
7684 return FALSE;
7685
663a1470
AM
7686 /* Make two passes over the relocs. On the first pass, mark toc
7687 entries involved with tls relocs, and check that tls relocs
7688 involved in setting up a tls_get_addr call are indeed followed by
7689 such a call. If they are not, we can't do any tls optimization.
7690 On the second pass twiddle tls_mask flags to notify
7691 relocate_section that optimization can be done, and adjust got
7692 and plt refcounts. */
7693 toc_ref = NULL;
7694 for (pass = 0; pass < 2; ++pass)
c72f2fb2 7695 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
7696 {
7697 Elf_Internal_Sym *locsyms = NULL;
7698 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7699
102890f0
AM
7700 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7701 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7702 {
7703 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7704 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7705
102890f0
AM
7706 /* Read the relocations. */
7707 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7708 info->keep_memory);
7709 if (relstart == NULL)
2915c55b
JK
7710 {
7711 free (toc_ref);
7712 return FALSE;
7713 }
411e1bfb 7714
102890f0
AM
7715 relend = relstart + sec->reloc_count;
7716 for (rel = relstart; rel < relend; rel++)
7717 {
7718 enum elf_ppc64_reloc_type r_type;
7719 unsigned long r_symndx;
7720 struct elf_link_hash_entry *h;
7721 Elf_Internal_Sym *sym;
7722 asection *sym_sec;
f961d9dd 7723 unsigned char *tls_mask;
46e9995a 7724 unsigned int tls_set, tls_clear, tls_type = 0;
102890f0
AM
7725 bfd_vma value;
7726 bfd_boolean ok_tprel, is_local;
7727 long toc_ref_index = 0;
7728 int expecting_tls_get_addr = 0;
663a1470 7729 bfd_boolean ret = FALSE;
411e1bfb 7730
102890f0
AM
7731 r_symndx = ELF64_R_SYM (rel->r_info);
7732 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7733 r_symndx, ibfd))
7734 {
7735 err_free_rel:
7736 if (elf_section_data (sec)->relocs != relstart)
7737 free (relstart);
7738 if (toc_ref != NULL)
7739 free (toc_ref);
7740 if (locsyms != NULL
0ffa91dd 7741 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7742 != (unsigned char *) locsyms))
7743 free (locsyms);
663a1470 7744 return ret;
102890f0 7745 }
411e1bfb 7746
102890f0
AM
7747 if (h != NULL)
7748 {
766bc656
AM
7749 if (h->root.type == bfd_link_hash_defined
7750 || h->root.type == bfd_link_hash_defweak)
7751 value = h->root.u.def.value;
7752 else if (h->root.type == bfd_link_hash_undefweak)
7753 value = 0;
7754 else
663a1470
AM
7755 {
7756 found_tls_get_addr_arg = 0;
7757 continue;
7758 }
102890f0
AM
7759 }
7760 else
7761 /* Symbols referenced by TLS relocs must be of type
7762 STT_TLS. So no need for .opd local sym adjust. */
7763 value = sym->st_value;
7764
7765 ok_tprel = FALSE;
7766 is_local = FALSE;
7767 if (h == NULL
7768 || !h->def_dynamic)
7769 {
7770 is_local = TRUE;
766bc656
AM
7771 if (h != NULL
7772 && h->root.type == bfd_link_hash_undefweak)
7773 ok_tprel = TRUE;
c27b8c2a
AM
7774 else if (sym_sec != NULL
7775 && sym_sec->output_section != NULL)
766bc656
AM
7776 {
7777 value += sym_sec->output_offset;
7778 value += sym_sec->output_section->vma;
0b147428 7779 value -= htab->elf.tls_sec->vma + TP_OFFSET;
c213164a
AM
7780 /* Note that even though the prefix insns
7781 allow a 1<<33 offset we use the same test
7782 as for addis;addi. There may be a mix of
7783 pcrel and non-pcrel code and the decision
7784 to optimise is per symbol, not per TLS
7785 sequence. */
0b147428 7786 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
766bc656 7787 }
102890f0 7788 }
951fd09b 7789
102890f0 7790 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7791 /* If this section has old-style __tls_get_addr calls
7792 without marker relocs, then check that each
7793 __tls_get_addr call reloc is preceded by a reloc
7794 that conceivably belongs to the __tls_get_addr arg
7795 setup insn. If we don't find matching arg setup
7796 relocs, don't do any tls optimization. */
7797 if (pass == 0
7798 && sec->has_tls_get_addr_call
7799 && h != NULL
7800 && (h == &htab->tls_get_addr->elf
7801 || h == &htab->tls_get_addr_fd->elf)
7802 && !found_tls_get_addr_arg
7803 && is_branch_reloc (r_type))
7804 {
25f53a85 7805 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
7806 "TLS optimization disabled\n"),
7807 ibfd, sec, rel->r_offset);
7808 ret = TRUE;
7809 goto err_free_rel;
7810 }
7811
7812 found_tls_get_addr_arg = 0;
102890f0
AM
7813 switch (r_type)
7814 {
7815 case R_PPC64_GOT_TLSLD16:
7816 case R_PPC64_GOT_TLSLD16_LO:
c213164a 7817 case R_PPC64_GOT_TLSLD34:
102890f0 7818 expecting_tls_get_addr = 1;
663a1470 7819 found_tls_get_addr_arg = 1;
1a0670f3 7820 /* Fall through. */
102890f0
AM
7821
7822 case R_PPC64_GOT_TLSLD16_HI:
7823 case R_PPC64_GOT_TLSLD16_HA:
7824 /* These relocs should never be against a symbol
7825 defined in a shared lib. Leave them alone if
7826 that turns out to be the case. */
7827 if (!is_local)
7828 continue;
411e1bfb 7829
102890f0 7830 /* LD -> LE */
411e1bfb 7831 tls_set = 0;
102890f0
AM
7832 tls_clear = TLS_LD;
7833 tls_type = TLS_TLS | TLS_LD;
7834 break;
411e1bfb 7835
102890f0
AM
7836 case R_PPC64_GOT_TLSGD16:
7837 case R_PPC64_GOT_TLSGD16_LO:
c213164a 7838 case R_PPC64_GOT_TLSGD34:
102890f0 7839 expecting_tls_get_addr = 1;
663a1470 7840 found_tls_get_addr_arg = 1;
1a0670f3 7841 /* Fall through. */
102890f0
AM
7842
7843 case R_PPC64_GOT_TLSGD16_HI:
7844 case R_PPC64_GOT_TLSGD16_HA:
7845 if (ok_tprel)
7846 /* GD -> LE */
411e1bfb 7847 tls_set = 0;
102890f0
AM
7848 else
7849 /* GD -> IE */
b00a0a86 7850 tls_set = TLS_TLS | TLS_GDIE;
102890f0
AM
7851 tls_clear = TLS_GD;
7852 tls_type = TLS_TLS | TLS_GD;
7853 break;
7854
c213164a 7855 case R_PPC64_GOT_TPREL34:
102890f0
AM
7856 case R_PPC64_GOT_TPREL16_DS:
7857 case R_PPC64_GOT_TPREL16_LO_DS:
7858 case R_PPC64_GOT_TPREL16_HI:
7859 case R_PPC64_GOT_TPREL16_HA:
7860 if (ok_tprel)
7861 {
7862 /* IE -> LE */
7863 tls_set = 0;
7864 tls_clear = TLS_TPREL;
7865 tls_type = TLS_TLS | TLS_TPREL;
7866 break;
7867 }
411e1bfb
AM
7868 continue;
7869
727fc41e
AM
7870 case R_PPC64_TLSGD:
7871 case R_PPC64_TLSLD:
23cedd1d
AM
7872 if (rel + 1 < relend
7873 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7874 {
7875 if (pass != 0
2cdcc330 7876 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
7877 != R_PPC64_PLTSEQ)
7878 && (ELF64_R_TYPE (rel[1].r_info)
7879 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
7880 {
7881 r_symndx = ELF64_R_SYM (rel[1].r_info);
7882 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 7883 r_symndx, ibfd))
23cedd1d
AM
7884 goto err_free_rel;
7885 if (h != NULL)
7886 {
7887 struct plt_entry *ent = NULL;
7888
7889 for (ent = h->plt.plist;
7890 ent != NULL;
7891 ent = ent->next)
7892 if (ent->addend == rel[1].r_addend)
7893 break;
7894
7895 if (ent != NULL
7896 && ent->plt.refcount > 0)
7897 ent->plt.refcount -= 1;
7898 }
7899 }
7900 continue;
7901 }
663a1470 7902 found_tls_get_addr_arg = 1;
1a0670f3 7903 /* Fall through. */
663a1470
AM
7904
7905 case R_PPC64_TLS:
7906 case R_PPC64_TOC16:
7907 case R_PPC64_TOC16_LO:
102890f0
AM
7908 if (sym_sec == NULL || sym_sec != toc)
7909 continue;
7910
7911 /* Mark this toc entry as referenced by a TLS
7912 code sequence. We can do that now in the
7913 case of R_PPC64_TLS, and after checking for
7914 tls_get_addr for the TOC16 relocs. */
7915 if (toc_ref == NULL)
2cdcc330
AM
7916 toc_ref
7917 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
7918 if (toc_ref == NULL)
7919 goto err_free_rel;
7920
102890f0
AM
7921 if (h != NULL)
7922 value = h->root.u.def.value;
7923 else
7924 value = sym->st_value;
7925 value += rel->r_addend;
73242275
AM
7926 if (value % 8 != 0)
7927 continue;
7928 BFD_ASSERT (value < toc->size
7929 && toc->output_offset % 8 == 0);
663a1470 7930 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7931 if (r_type == R_PPC64_TLS
7932 || r_type == R_PPC64_TLSGD
7933 || r_type == R_PPC64_TLSLD)
102890f0
AM
7934 {
7935 toc_ref[toc_ref_index] = 1;
7936 continue;
7937 }
7938
7939 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7940 continue;
7941
7942 tls_set = 0;
7943 tls_clear = 0;
7944 expecting_tls_get_addr = 2;
7945 break;
7946
7947 case R_PPC64_TPREL64:
7948 if (pass == 0
7949 || sec != toc
7950 || toc_ref == NULL
663a1470 7951 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7952 continue;
7953 if (ok_tprel)
7954 {
7955 /* IE -> LE */
7956 tls_set = TLS_EXPLICIT;
7957 tls_clear = TLS_TPREL;
7958 break;
7959 }
7960 continue;
7961
7962 case R_PPC64_DTPMOD64:
7963 if (pass == 0
7964 || sec != toc
7965 || toc_ref == NULL
663a1470 7966 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7967 continue;
7968 if (rel + 1 < relend
7969 && (rel[1].r_info
7970 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7971 && rel[1].r_offset == rel->r_offset + 8)
7972 {
7973 if (ok_tprel)
7974 /* GD -> LE */
7975 tls_set = TLS_EXPLICIT | TLS_GD;
7976 else
7977 /* GD -> IE */
b00a0a86 7978 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
102890f0
AM
7979 tls_clear = TLS_GD;
7980 }
7981 else
7982 {
7983 if (!is_local)
7984 continue;
7985
7986 /* LD -> LE */
7987 tls_set = TLS_EXPLICIT;
7988 tls_clear = TLS_LD;
7989 }
7990 break;
7991
7992 default:
7993 continue;
7994 }
7995
7996 if (pass == 0)
7997 {
727fc41e
AM
7998 if (!expecting_tls_get_addr
7999 || !sec->has_tls_get_addr_call)
102890f0
AM
8000 continue;
8001
3a71aa26
AM
8002 if (rel + 1 < relend
8003 && branch_reloc_hash_match (ibfd, rel + 1,
8004 htab->tls_get_addr,
8005 htab->tls_get_addr_fd))
102890f0 8006 {
3a71aa26 8007 if (expecting_tls_get_addr == 2)
102890f0 8008 {
3a71aa26 8009 /* Check for toc tls entries. */
f961d9dd 8010 unsigned char *toc_tls;
3a71aa26
AM
8011 int retval;
8012
8013 retval = get_tls_mask (&toc_tls, NULL, NULL,
8014 &locsyms,
8015 rel, ibfd);
8016 if (retval == 0)
8017 goto err_free_rel;
663a1470
AM
8018 if (toc_tls != NULL)
8019 {
37da22e5
AM
8020 if ((*toc_tls & TLS_TLS) != 0
8021 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8022 found_tls_get_addr_arg = 1;
8023 if (retval > 1)
8024 toc_ref[toc_ref_index] = 1;
8025 }
102890f0 8026 }
3a71aa26 8027 continue;
102890f0
AM
8028 }
8029
102890f0
AM
8030 /* Uh oh, we didn't find the expected call. We
8031 could just mark this symbol to exclude it
8032 from tls optimization but it's safer to skip
663a1470 8033 the entire optimization. */
695344c0 8034 /* xgettext:c-format */
25f53a85 8035 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8036 "TLS optimization disabled\n"),
8037 ibfd, sec, rel->r_offset);
8038 ret = TRUE;
8039 goto err_free_rel;
102890f0
AM
8040 }
8041
37da22e5
AM
8042 /* If we don't have old-style __tls_get_addr calls
8043 without TLSGD/TLSLD marker relocs, and we haven't
8044 found a new-style __tls_get_addr call with a
8045 marker for this symbol, then we either have a
8046 broken object file or an -mlongcall style
8047 indirect call to __tls_get_addr without a marker.
8048 Disable optimization in this case. */
8049 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8050 && (tls_set & TLS_EXPLICIT) == 0
8051 && !sec->has_tls_get_addr_call
8052 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8053 != (TLS_TLS | TLS_MARK)))
8054 continue;
8055
23cedd1d 8056 if (expecting_tls_get_addr)
102890f0 8057 {
23cedd1d
AM
8058 struct plt_entry *ent = NULL;
8059
8060 if (htab->tls_get_addr != NULL)
8061 for (ent = htab->tls_get_addr->elf.plt.plist;
8062 ent != NULL;
8063 ent = ent->next)
8064 if (ent->addend == 0)
102890f0 8065 break;
411e1bfb 8066
23cedd1d
AM
8067 if (ent == NULL && htab->tls_get_addr_fd != NULL)
8068 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8069 ent != NULL;
8070 ent = ent->next)
8071 if (ent->addend == 0)
102890f0 8072 break;
23cedd1d
AM
8073
8074 if (ent != NULL
8075 && ent->plt.refcount > 0)
8076 ent->plt.refcount -= 1;
102890f0 8077 }
411e1bfb 8078
102890f0 8079 if (tls_clear == 0)
30038c59
AM
8080 continue;
8081
102890f0
AM
8082 if ((tls_set & TLS_EXPLICIT) == 0)
8083 {
8084 struct got_entry *ent;
411e1bfb 8085
102890f0
AM
8086 /* Adjust got entry for this reloc. */
8087 if (h != NULL)
8088 ent = h->got.glist;
8089 else
8090 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8091
102890f0
AM
8092 for (; ent != NULL; ent = ent->next)
8093 if (ent->addend == rel->r_addend
8094 && ent->owner == ibfd
8095 && ent->tls_type == tls_type)
8096 break;
8097 if (ent == NULL)
8098 abort ();
411e1bfb 8099
102890f0
AM
8100 if (tls_set == 0)
8101 {
8102 /* We managed to get rid of a got entry. */
8103 if (ent->got.refcount > 0)
8104 ent->got.refcount -= 1;
8105 }
8106 }
8107 else
8108 {
8109 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8110 we'll lose one or two dyn relocs. */
8111 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8112 NULL, h, sym))
102890f0 8113 return FALSE;
411e1bfb 8114
102890f0
AM
8115 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8116 {
8117 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8118 NULL, h, sym))
102890f0
AM
8119 return FALSE;
8120 }
8121 }
411e1bfb 8122
46e9995a 8123 *tls_mask |= tls_set & 0xff;
102890f0
AM
8124 *tls_mask &= ~tls_clear;
8125 }
8c1d1bb8 8126
102890f0
AM
8127 if (elf_section_data (sec)->relocs != relstart)
8128 free (relstart);
8129 }
411e1bfb 8130
663a1470
AM
8131 if (locsyms != NULL
8132 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8133 {
8134 if (!info->keep_memory)
8135 free (locsyms);
8136 else
8137 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8138 }
8139 }
411e1bfb 8140
663a1470
AM
8141 if (toc_ref != NULL)
8142 free (toc_ref);
9a23f96e 8143 htab->do_tls_opt = 1;
b34976b6 8144 return TRUE;
1e2f5b6e 8145}
b34976b6 8146
c5614fa4
AM
8147/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8148 the values of any global symbols in a toc section that has been
8149 edited. Globals in toc sections should be a rarity, so this function
8150 sets a flag if any are found in toc sections other than the one just
de194d85 8151 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8152
8153struct adjust_toc_info
8154{
8155 asection *toc;
8156 unsigned long *skip;
8157 bfd_boolean global_toc_syms;
8158};
8159
ba761f19
AM
8160enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8161
c5614fa4
AM
8162static bfd_boolean
8163adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8164{
8165 struct ppc_link_hash_entry *eh;
8166 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8167 unsigned long i;
c5614fa4 8168
c5614fa4
AM
8169 if (h->root.type != bfd_link_hash_defined
8170 && h->root.type != bfd_link_hash_defweak)
8171 return TRUE;
8172
8173 eh = (struct ppc_link_hash_entry *) h;
8174 if (eh->adjust_done)
8175 return TRUE;
8176
8177 if (eh->elf.root.u.def.section == toc_inf->toc)
8178 {
854b41e7
AM
8179 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8180 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8181 else
854b41e7
AM
8182 i = eh->elf.root.u.def.value >> 3;
8183
ba761f19 8184 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8185 {
4eca0228 8186 _bfd_error_handler
854b41e7
AM
8187 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8188 do
8189 ++i;
ba761f19 8190 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8191 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8192 }
854b41e7
AM
8193
8194 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8195 eh->adjust_done = 1;
8196 }
8197 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8198 toc_inf->global_toc_syms = TRUE;
8199
8200 return TRUE;
8201}
8202
39eeab25
AM
8203/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8204 on a _LO variety toc/got reloc. */
560c8763
AM
8205
8206static bfd_boolean
39eeab25 8207ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8208{
39eeab25
AM
8209 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8210 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
8211 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8212 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8213 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8214 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8215 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8216 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8217 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8218 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8219 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8220 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8221 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8222 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8223 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
8224 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8225 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8226 /* Exclude lfqu by testing reloc. If relocs are ever
8227 defined for the reduced D field in psq_lu then those
8228 will need testing too. */
8229 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8230 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8231 && (insn & 1) == 0)
8232 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8233 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8234 /* Exclude stfqu. psq_stu as above for psq_lu. */
8235 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8236 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8237 && (insn & 1) == 0));
560c8763
AM
8238}
8239
4a421c53
AM
8240/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8241 pld ra,symbol@got@pcrel
8242 load/store rt,0(ra)
8243 or
d4b87b1e 8244 pla ra,symbol@pcrel
4a421c53
AM
8245 load/store rt,0(ra)
8246 may be translated to
8247 pload/pstore rt,symbol@pcrel
8248 nop.
8249 This function returns true if the optimization is possible, placing
8250 the prefix insn in *PINSN1 and a NOP in *PINSN2.
8251
8252 On entry to this function, the linker has already determined that
d4b87b1e 8253 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8254 while *PINSN2 is the second instruction. */
8255
8256static bfd_boolean
8257xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2)
8258{
8259 uint32_t insn2 = *pinsn2 >> 32;
8260 uint64_t i1new;
8261
8262 /* Check that regs match. */
8263 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
8264 return FALSE;
8265
8266 switch ((insn2 >> 26) & 63)
8267 {
8268 default:
8269 return FALSE;
8270
8271 case 32: /* lwz */
8272 case 34: /* lbz */
8273 case 36: /* stw */
8274 case 38: /* stb */
8275 case 40: /* lhz */
8276 case 42: /* lha */
8277 case 44: /* sth */
8278 case 48: /* lfs */
8279 case 50: /* lfd */
8280 case 52: /* stfs */
8281 case 54: /* stfd */
8282 /* These are the PMLS cases, where we just need to tack a prefix
8283 on the insn. Check that the D field is zero. */
8284 if ((insn2 & 0xffff) != 0)
8285 return FALSE;
8286 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8287 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8288 break;
8289
8290 case 58: /* lwa, ld */
8291 if ((insn2 & 0xfffd) != 0)
8292 return FALSE;
8293 i1new = ((1ULL << 58) | (1ULL << 52)
8294 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8295 | (insn2 & (31ULL << 21)));
8296 break;
8297
8298 case 57: /* lxsd, lxssp */
8299 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
8300 return FALSE;
8301 i1new = ((1ULL << 58) | (1ULL << 52)
8302 | ((40ULL | (insn2 & 3)) << 26)
8303 | (insn2 & (31ULL << 21)));
8304 break;
8305
8306 case 61: /* stxsd, stxssp, lxv, stxv */
8307 if ((insn2 & 3) == 0)
8308 return FALSE;
8309 else if ((insn2 & 3) >= 2)
8310 {
8311 if ((insn2 & 0xfffc) != 0)
8312 return FALSE;
8313 i1new = ((1ULL << 58) | (1ULL << 52)
8314 | ((44ULL | (insn2 & 3)) << 26)
8315 | (insn2 & (31ULL << 21)));
8316 }
8317 else
8318 {
8319 if ((insn2 & 0xfff0) != 0)
8320 return FALSE;
8321 i1new = ((1ULL << 58) | (1ULL << 52)
8322 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8323 | (insn2 & (31ULL << 21)));
8324 }
8325 break;
8326
8327 case 56: /* lq */
8328 if ((insn2 & 0xffff) != 0)
8329 return FALSE;
8330 i1new = ((1ULL << 58) | (1ULL << 52)
8331 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8332 break;
8333
8334 case 62: /* std, stq */
8335 if ((insn2 & 0xfffd) != 0)
8336 return FALSE;
8337 i1new = ((1ULL << 58) | (1ULL << 52)
8338 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8339 | (insn2 & (31ULL << 21)));
8340 break;
8341 }
8342
8343 *pinsn1 = i1new;
8344 *pinsn2 = (uint64_t) NOP << 32;
8345 return TRUE;
8346}
8347
c5614fa4
AM
8348/* Examine all relocs referencing .toc sections in order to remove
8349 unused .toc entries. */
8350
8351bfd_boolean
33c0ec9d 8352ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8353{
8354 bfd *ibfd;
8355 struct adjust_toc_info toc_inf;
67f0cbdb 8356 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8357
67f0cbdb 8358 htab->do_toc_opt = 1;
c5614fa4 8359 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8360 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8361 {
8362 asection *toc, *sec;
8363 Elf_Internal_Shdr *symtab_hdr;
8364 Elf_Internal_Sym *local_syms;
425b145b 8365 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8366 unsigned long *skip, *drop;
8367 unsigned char *used;
8368 unsigned char *keep, last, some_unused;
8369
854b41e7
AM
8370 if (!is_ppc64_elf (ibfd))
8371 continue;
8372
c5614fa4
AM
8373 toc = bfd_get_section_by_name (ibfd, ".toc");
8374 if (toc == NULL
92b7a70f 8375 || toc->size == 0
dbaa2011
AM
8376 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8377 || discarded_section (toc))
c5614fa4
AM
8378 continue;
8379
425b145b 8380 toc_relocs = NULL;
c5614fa4 8381 local_syms = NULL;
0ffa91dd 8382 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8383
8384 /* Look at sections dropped from the final link. */
8385 skip = NULL;
8386 relstart = NULL;
8387 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8388 {
8389 if (sec->reloc_count == 0
dbaa2011 8390 || !discarded_section (sec)
c5614fa4
AM
8391 || get_opd_info (sec)
8392 || (sec->flags & SEC_ALLOC) == 0
8393 || (sec->flags & SEC_DEBUGGING) != 0)
8394 continue;
8395
8396 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8397 if (relstart == NULL)
8398 goto error_ret;
8399
8400 /* Run through the relocs to see which toc entries might be
8401 unused. */
8402 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8403 {
8404 enum elf_ppc64_reloc_type r_type;
8405 unsigned long r_symndx;
8406 asection *sym_sec;
8407 struct elf_link_hash_entry *h;
8408 Elf_Internal_Sym *sym;
8409 bfd_vma val;
8410
8411 r_type = ELF64_R_TYPE (rel->r_info);
8412 switch (r_type)
8413 {
8414 default:
8415 continue;
8416
8417 case R_PPC64_TOC16:
8418 case R_PPC64_TOC16_LO:
8419 case R_PPC64_TOC16_HI:
8420 case R_PPC64_TOC16_HA:
8421 case R_PPC64_TOC16_DS:
8422 case R_PPC64_TOC16_LO_DS:
8423 break;
8424 }
8425
8426 r_symndx = ELF64_R_SYM (rel->r_info);
8427 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8428 r_symndx, ibfd))
8429 goto error_ret;
8430
8431 if (sym_sec != toc)
8432 continue;
8433
8434 if (h != NULL)
8435 val = h->root.u.def.value;
8436 else
8437 val = sym->st_value;
8438 val += rel->r_addend;
8439
8440 if (val >= toc->size)
8441 continue;
8442
8443 /* Anything in the toc ought to be aligned to 8 bytes.
8444 If not, don't mark as unused. */
8445 if (val & 7)
8446 continue;
8447
8448 if (skip == NULL)
8449 {
854b41e7 8450 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8451 if (skip == NULL)
8452 goto error_ret;
8453 }
8454
ba761f19 8455 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8456 }
8457
8458 if (elf_section_data (sec)->relocs != relstart)
8459 free (relstart);
8460 }
8461
ba761f19
AM
8462 /* For largetoc loads of address constants, we can convert
8463 . addis rx,2,addr@got@ha
8464 . ld ry,addr@got@l(rx)
8465 to
8466 . addis rx,2,addr@toc@ha
8467 . addi ry,rx,addr@toc@l
8468 when addr is within 2G of the toc pointer. This then means
8469 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8470
ba761f19
AM
8471 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8472 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8473 && toc->reloc_count != 0)
8474 {
8475 /* Read toc relocs. */
425b145b
AM
8476 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8477 info->keep_memory);
8478 if (toc_relocs == NULL)
ba761f19
AM
8479 goto error_ret;
8480
425b145b 8481 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8482 {
8483 enum elf_ppc64_reloc_type r_type;
8484 unsigned long r_symndx;
8485 asection *sym_sec;
8486 struct elf_link_hash_entry *h;
8487 Elf_Internal_Sym *sym;
8488 bfd_vma val, addr;
8489
8490 r_type = ELF64_R_TYPE (rel->r_info);
8491 if (r_type != R_PPC64_ADDR64)
8492 continue;
8493
8494 r_symndx = ELF64_R_SYM (rel->r_info);
8495 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8496 r_symndx, ibfd))
8497 goto error_ret;
8498
425b145b 8499 if (sym_sec == NULL
c27b8c2a 8500 || sym_sec->output_section == NULL
dbaa2011 8501 || discarded_section (sym_sec))
425b145b
AM
8502 continue;
8503
afe397ea 8504 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8505 continue;
8506
8507 if (h != NULL)
bddc25c9
AM
8508 {
8509 if (h->type == STT_GNU_IFUNC)
8510 continue;
8511 val = h->root.u.def.value;
8512 }
ba761f19 8513 else
bddc25c9
AM
8514 {
8515 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8516 continue;
8517 val = sym->st_value;
8518 }
ba761f19
AM
8519 val += rel->r_addend;
8520 val += sym_sec->output_section->vma + sym_sec->output_offset;
8521
8522 /* We don't yet know the exact toc pointer value, but we
8523 know it will be somewhere in the toc section. Don't
8524 optimize if the difference from any possible toc
8525 pointer is outside [ff..f80008000, 7fff7fff]. */
8526 addr = toc->output_section->vma + TOC_BASE_OFF;
8527 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8528 continue;
8529
8530 addr = toc->output_section->vma + toc->output_section->rawsize;
8531 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8532 continue;
8533
8534 if (skip == NULL)
8535 {
8536 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8537 if (skip == NULL)
8538 goto error_ret;
8539 }
8540
8541 skip[rel->r_offset >> 3]
425b145b 8542 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8543 }
ba761f19
AM
8544 }
8545
c5614fa4
AM
8546 if (skip == NULL)
8547 continue;
8548
8549 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8550 if (used == NULL)
8551 {
8552 error_ret:
8553 if (local_syms != NULL
8554 && symtab_hdr->contents != (unsigned char *) local_syms)
8555 free (local_syms);
8556 if (sec != NULL
8557 && relstart != NULL
8558 && elf_section_data (sec)->relocs != relstart)
8559 free (relstart);
425b145b
AM
8560 if (toc_relocs != NULL
8561 && elf_section_data (toc)->relocs != toc_relocs)
8562 free (toc_relocs);
c5614fa4
AM
8563 if (skip != NULL)
8564 free (skip);
8565 return FALSE;
8566 }
8567
30038c59
AM
8568 /* Now check all kept sections that might reference the toc.
8569 Check the toc itself last. */
8570 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8571 : ibfd->sections);
c5614fa4 8572 sec != NULL;
c5614fa4 8573 sec = (sec == toc ? NULL
c5614fa4 8574 : sec->next == NULL ? toc
30038c59 8575 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8576 : sec->next))
8577 {
8578 int repeat;
8579
8580 if (sec->reloc_count == 0
dbaa2011 8581 || discarded_section (sec)
c5614fa4
AM
8582 || get_opd_info (sec)
8583 || (sec->flags & SEC_ALLOC) == 0
8584 || (sec->flags & SEC_DEBUGGING) != 0)
8585 continue;
8586
854b41e7
AM
8587 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8588 info->keep_memory);
c5614fa4 8589 if (relstart == NULL)
2915c55b
JK
8590 {
8591 free (used);
8592 goto error_ret;
8593 }
c5614fa4
AM
8594
8595 /* Mark toc entries referenced as used. */
c5614fa4 8596 do
d4f1ee75
AM
8597 {
8598 repeat = 0;
8599 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8600 {
8601 enum elf_ppc64_reloc_type r_type;
8602 unsigned long r_symndx;
8603 asection *sym_sec;
8604 struct elf_link_hash_entry *h;
8605 Elf_Internal_Sym *sym;
8606 bfd_vma val;
8607 enum {no_check, check_lo, check_ha} insn_check;
98528052 8608
d4f1ee75
AM
8609 r_type = ELF64_R_TYPE (rel->r_info);
8610 switch (r_type)
8611 {
8612 default:
8613 insn_check = no_check;
8614 break;
98528052 8615
d4f1ee75
AM
8616 case R_PPC64_GOT_TLSLD16_HA:
8617 case R_PPC64_GOT_TLSGD16_HA:
8618 case R_PPC64_GOT_TPREL16_HA:
8619 case R_PPC64_GOT_DTPREL16_HA:
8620 case R_PPC64_GOT16_HA:
8621 case R_PPC64_TOC16_HA:
8622 insn_check = check_ha;
8623 break;
98528052 8624
d4f1ee75
AM
8625 case R_PPC64_GOT_TLSLD16_LO:
8626 case R_PPC64_GOT_TLSGD16_LO:
8627 case R_PPC64_GOT_TPREL16_LO_DS:
8628 case R_PPC64_GOT_DTPREL16_LO_DS:
8629 case R_PPC64_GOT16_LO:
8630 case R_PPC64_GOT16_LO_DS:
8631 case R_PPC64_TOC16_LO:
8632 case R_PPC64_TOC16_LO_DS:
8633 insn_check = check_lo;
8634 break;
8635 }
560c8763 8636
d4f1ee75
AM
8637 if (insn_check != no_check)
8638 {
8639 bfd_vma off = rel->r_offset & ~3;
8640 unsigned char buf[4];
8641 unsigned int insn;
c5614fa4 8642
d4f1ee75
AM
8643 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8644 {
8645 free (used);
8646 goto error_ret;
8647 }
8648 insn = bfd_get_32 (ibfd, buf);
8649 if (insn_check == check_lo
39eeab25 8650 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
8651 : ((insn & ((0x3f << 26) | 0x1f << 16))
8652 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8653 {
8654 char str[12];
8655
8656 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8657 sprintf (str, "%#08x", insn);
8658 info->callbacks->einfo
695344c0 8659 /* xgettext:c-format */
174d0a74 8660 (_("%H: toc optimization is not supported for"
cf97bcb0 8661 " %s instruction\n"),
d4f1ee75
AM
8662 ibfd, sec, rel->r_offset & ~3, str);
8663 }
8664 }
c5614fa4 8665
d4f1ee75
AM
8666 switch (r_type)
8667 {
8668 case R_PPC64_TOC16:
8669 case R_PPC64_TOC16_LO:
8670 case R_PPC64_TOC16_HI:
8671 case R_PPC64_TOC16_HA:
8672 case R_PPC64_TOC16_DS:
8673 case R_PPC64_TOC16_LO_DS:
8674 /* In case we're taking addresses of toc entries. */
8675 case R_PPC64_ADDR64:
8676 break;
c5614fa4 8677
d4f1ee75
AM
8678 default:
8679 continue;
8680 }
c5614fa4 8681
d4f1ee75
AM
8682 r_symndx = ELF64_R_SYM (rel->r_info);
8683 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8684 r_symndx, ibfd))
8685 {
8686 free (used);
8687 goto error_ret;
8688 }
c5614fa4 8689
d4f1ee75
AM
8690 if (sym_sec != toc)
8691 continue;
c5614fa4 8692
d4f1ee75
AM
8693 if (h != NULL)
8694 val = h->root.u.def.value;
8695 else
8696 val = sym->st_value;
8697 val += rel->r_addend;
ba761f19 8698
d4f1ee75
AM
8699 if (val >= toc->size)
8700 continue;
ba761f19 8701
d4f1ee75
AM
8702 if ((skip[val >> 3] & can_optimize) != 0)
8703 {
8704 bfd_vma off;
8705 unsigned char opc;
8706
8707 switch (r_type)
8708 {
8709 case R_PPC64_TOC16_HA:
ba761f19 8710 break;
ba761f19 8711
d4f1ee75
AM
8712 case R_PPC64_TOC16_LO_DS:
8713 off = rel->r_offset;
8714 off += (bfd_big_endian (ibfd) ? -2 : 3);
8715 if (!bfd_get_section_contents (ibfd, sec, &opc,
8716 off, 1))
8717 {
8718 free (used);
8719 goto error_ret;
8720 }
8721 if ((opc & (0x3f << 2)) == (58u << 2))
8722 break;
1a0670f3 8723 /* Fall through. */
ba761f19 8724
d4f1ee75
AM
8725 default:
8726 /* Wrong sort of reloc, or not a ld. We may
8727 as well clear ref_from_discarded too. */
8728 skip[val >> 3] = 0;
8729 }
8730 }
8731
8732 if (sec != toc)
8733 used[val >> 3] = 1;
8734 /* For the toc section, we only mark as used if this
8735 entry itself isn't unused. */
8736 else if ((used[rel->r_offset >> 3]
8737 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8738 && !used[val >> 3])
8739 {
8740 /* Do all the relocs again, to catch reference
8741 chains. */
8742 repeat = 1;
8743 used[val >> 3] = 1;
8744 }
8745 }
8746 }
c5614fa4 8747 while (repeat);
854b41e7
AM
8748
8749 if (elf_section_data (sec)->relocs != relstart)
8750 free (relstart);
c5614fa4
AM
8751 }
8752
8753 /* Merge the used and skip arrays. Assume that TOC
8754 doublewords not appearing as either used or unused belong
de194d85 8755 to an entry more than one doubleword in size. */
c5614fa4
AM
8756 for (drop = skip, keep = used, last = 0, some_unused = 0;
8757 drop < skip + (toc->size + 7) / 8;
8758 ++drop, ++keep)
8759 {
8760 if (*keep)
8761 {
ba761f19
AM
8762 *drop &= ~ref_from_discarded;
8763 if ((*drop & can_optimize) != 0)
8764 some_unused = 1;
c5614fa4
AM
8765 last = 0;
8766 }
b140b010 8767 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8768 {
8769 some_unused = 1;
ba761f19 8770 last = ref_from_discarded;
c5614fa4
AM
8771 }
8772 else
8773 *drop = last;
8774 }
8775
8776 free (used);
8777
8778 if (some_unused)
8779 {
8780 bfd_byte *contents, *src;
8781 unsigned long off;
d62b3684 8782 Elf_Internal_Sym *sym;
ba761f19 8783 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8784
8785 /* Shuffle the toc contents, and at the same time convert the
8786 skip array from booleans into offsets. */
8787 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8788 goto error_ret;
8789
8790 elf_section_data (toc)->this_hdr.contents = contents;
8791
8792 for (src = contents, off = 0, drop = skip;
8793 src < contents + toc->size;
8794 src += 8, ++drop)
8795 {
ba761f19
AM
8796 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8797 off += 8;
c5614fa4
AM
8798 else if (off != 0)
8799 {
8800 *drop = off;
8801 memcpy (src - off, src, 8);
8802 }
8803 }
854b41e7 8804 *drop = off;
c5614fa4
AM
8805 toc->rawsize = toc->size;
8806 toc->size = src - contents - off;
8807
ba761f19
AM
8808 /* Adjust addends for relocs against the toc section sym,
8809 and optimize any accesses we can. */
c5614fa4
AM
8810 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8811 {
8812 if (sec->reloc_count == 0
dbaa2011 8813 || discarded_section (sec))
c5614fa4
AM
8814 continue;
8815
8816 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8817 info->keep_memory);
c5614fa4
AM
8818 if (relstart == NULL)
8819 goto error_ret;
8820
8821 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8822 {
8823 enum elf_ppc64_reloc_type r_type;
8824 unsigned long r_symndx;
8825 asection *sym_sec;
8826 struct elf_link_hash_entry *h;
854b41e7 8827 bfd_vma val;
c5614fa4
AM
8828
8829 r_type = ELF64_R_TYPE (rel->r_info);
8830 switch (r_type)
8831 {
8832 default:
8833 continue;
8834
8835 case R_PPC64_TOC16:
8836 case R_PPC64_TOC16_LO:
8837 case R_PPC64_TOC16_HI:
8838 case R_PPC64_TOC16_HA:
8839 case R_PPC64_TOC16_DS:
8840 case R_PPC64_TOC16_LO_DS:
8841 case R_PPC64_ADDR64:
8842 break;
8843 }
8844
8845 r_symndx = ELF64_R_SYM (rel->r_info);
8846 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8847 r_symndx, ibfd))
8848 goto error_ret;
8849
ba761f19 8850 if (sym_sec != toc)
c5614fa4
AM
8851 continue;
8852
ba761f19
AM
8853 if (h != NULL)
8854 val = h->root.u.def.value;
8855 else
8856 {
8857 val = sym->st_value;
8858 if (val != 0)
8859 local_toc_syms = TRUE;
8860 }
8861
8862 val += rel->r_addend;
854b41e7
AM
8863
8864 if (val > toc->rawsize)
8865 val = toc->rawsize;
ba761f19
AM
8866 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8867 continue;
8868 else if ((skip[val >> 3] & can_optimize) != 0)
8869 {
8870 Elf_Internal_Rela *tocrel
425b145b 8871 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8872 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8873
8874 switch (r_type)
8875 {
8876 case R_PPC64_TOC16_HA:
8877 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8878 break;
8879
8880 case R_PPC64_TOC16_LO_DS:
8881 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8882 break;
8883
8884 default:
28942f62
AM
8885 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8886 ppc_howto_init ();
b140b010 8887 info->callbacks->einfo
695344c0 8888 /* xgettext:c-format */
174d0a74 8889 (_("%H: %s references "
b140b010
AM
8890 "optimized away TOC entry\n"),
8891 ibfd, sec, rel->r_offset,
8892 ppc64_elf_howto_table[r_type]->name);
8893 bfd_set_error (bfd_error_bad_value);
8894 goto error_ret;
ba761f19
AM
8895 }
8896 rel->r_addend = tocrel->r_addend;
8897 elf_section_data (sec)->relocs = relstart;
8898 continue;
8899 }
8900
8901 if (h != NULL || sym->st_value != 0)
8902 continue;
854b41e7
AM
8903
8904 rel->r_addend -= skip[val >> 3];
8905 elf_section_data (sec)->relocs = relstart;
c5614fa4 8906 }
854b41e7
AM
8907
8908 if (elf_section_data (sec)->relocs != relstart)
8909 free (relstart);
c5614fa4
AM
8910 }
8911
8912 /* We shouldn't have local or global symbols defined in the TOC,
8913 but handle them anyway. */
df22d223
AM
8914 if (local_syms != NULL)
8915 for (sym = local_syms;
8916 sym < local_syms + symtab_hdr->sh_info;
8917 ++sym)
8918 if (sym->st_value != 0
8919 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8920 {
8921 unsigned long i;
854b41e7 8922
df22d223
AM
8923 if (sym->st_value > toc->rawsize)
8924 i = toc->rawsize >> 3;
8925 else
8926 i = sym->st_value >> 3;
854b41e7 8927
df22d223
AM
8928 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8929 {
8930 if (local_toc_syms)
4eca0228 8931 _bfd_error_handler
df22d223
AM
8932 (_("%s defined on removed toc entry"),
8933 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8934 do
8935 ++i;
8936 while ((skip[i] & (ref_from_discarded | can_optimize)));
8937 sym->st_value = (bfd_vma) i << 3;
8938 }
d62b3684 8939
df22d223
AM
8940 sym->st_value -= skip[i];
8941 symtab_hdr->contents = (unsigned char *) local_syms;
8942 }
c5614fa4 8943
854b41e7 8944 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8945 if (toc_inf.global_toc_syms)
8946 {
8947 toc_inf.toc = toc;
8948 toc_inf.skip = skip;
8949 toc_inf.global_toc_syms = FALSE;
8950 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8951 &toc_inf);
8952 }
854b41e7
AM
8953
8954 if (toc->reloc_count != 0)
8955 {
d4730f92 8956 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8957 Elf_Internal_Rela *wrel;
8958 bfd_size_type sz;
8959
854b41e7 8960 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8961 if (toc_relocs == NULL)
8962 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8963 info->keep_memory);
8964 if (toc_relocs == NULL)
8965 goto error_ret;
8966
425b145b
AM
8967 wrel = toc_relocs;
8968 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8969 if ((skip[rel->r_offset >> 3]
8970 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8971 {
8972 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8973 wrel->r_info = rel->r_info;
8974 wrel->r_addend = rel->r_addend;
8975 ++wrel;
8976 }
8977 else if (!dec_dynrel_count (rel->r_info, toc, info,
8978 &local_syms, NULL, NULL))
8979 goto error_ret;
8980
425b145b
AM
8981 elf_section_data (toc)->relocs = toc_relocs;
8982 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8983 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8984 sz = rel_hdr->sh_entsize;
8985 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8986 }
c5614fa4 8987 }
28be611c
AM
8988 else if (toc_relocs != NULL
8989 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8990 free (toc_relocs);
c5614fa4
AM
8991
8992 if (local_syms != NULL
8993 && symtab_hdr->contents != (unsigned char *) local_syms)
8994 {
8995 if (!info->keep_memory)
8996 free (local_syms);
8997 else
8998 symtab_hdr->contents = (unsigned char *) local_syms;
8999 }
9000 free (skip);
9001 }
9002
066f4018 9003 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
9004 a GOT relative or PC relative access, possibly reducing the
9005 number of GOT entries. */
066f4018
AM
9006 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9007 {
9008 asection *sec;
9009 Elf_Internal_Shdr *symtab_hdr;
9010 Elf_Internal_Sym *local_syms;
9011 Elf_Internal_Rela *relstart, *rel;
9012 bfd_vma got;
9013
9014 if (!is_ppc64_elf (ibfd))
9015 continue;
9016
9017 if (!ppc64_elf_tdata (ibfd)->has_gotrel)
9018 continue;
9019
9020 sec = ppc64_elf_tdata (ibfd)->got;
9021 got = sec->output_section->vma + sec->output_offset + 0x8000;
9022
9023 local_syms = NULL;
9024 symtab_hdr = &elf_symtab_hdr (ibfd);
9025
9026 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9027 {
9028 if (sec->reloc_count == 0
9029 || !ppc64_elf_section_data (sec)->has_gotrel
9030 || discarded_section (sec))
9031 continue;
9032
9033 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9034 info->keep_memory);
9035 if (relstart == NULL)
9036 {
9037 got_error_ret:
9038 if (local_syms != NULL
9039 && symtab_hdr->contents != (unsigned char *) local_syms)
9040 free (local_syms);
9041 if (sec != NULL
9042 && relstart != NULL
9043 && elf_section_data (sec)->relocs != relstart)
9044 free (relstart);
9045 return FALSE;
9046 }
9047
9048 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9049 {
9050 enum elf_ppc64_reloc_type r_type;
9051 unsigned long r_symndx;
9052 Elf_Internal_Sym *sym;
9053 asection *sym_sec;
9054 struct elf_link_hash_entry *h;
9055 struct got_entry *ent;
4a421c53
AM
9056 bfd_vma sym_addend, val, pc;
9057 unsigned char buf[8];
066f4018
AM
9058 unsigned int insn;
9059
9060 r_type = ELF64_R_TYPE (rel->r_info);
9061 switch (r_type)
9062 {
bb22a418
AM
9063 /* Note that we don't delete GOT entries for
9064 R_PPC64_GOT16_DS since we'd need a lot more
9065 analysis. For starters, the preliminary layout is
9066 before the GOT, PLT, dynamic sections and stubs are
9067 laid out. Then we'd need to allow for changes in
9068 distance between sections caused by alignment. */
066f4018
AM
9069 default:
9070 continue;
9071
066f4018
AM
9072 case R_PPC64_GOT16_HA:
9073 case R_PPC64_GOT16_LO_DS:
4a421c53
AM
9074 sym_addend = rel->r_addend;
9075 break;
9076
9077 case R_PPC64_GOT_PCREL34:
9078 sym_addend = 0;
066f4018
AM
9079 break;
9080 }
9081
9082 r_symndx = ELF64_R_SYM (rel->r_info);
9083 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9084 r_symndx, ibfd))
9085 goto got_error_ret;
9086
9087 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9088 continue;
9089
9090 if (h != NULL)
9091 val = h->root.u.def.value;
9092 else
9093 val = sym->st_value;
4a421c53 9094 val += sym_addend;
066f4018
AM
9095 val += sym_sec->output_section->vma + sym_sec->output_offset;
9096
bb22a418
AM
9097/* Fudge factor to allow for the fact that the preliminary layout
9098 isn't exact. Reduce limits by this factor. */
9099#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9100
066f4018
AM
9101 switch (r_type)
9102 {
9103 default:
9104 continue;
9105
066f4018 9106 case R_PPC64_GOT16_HA:
bb22a418
AM
9107 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9108 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9109 continue;
9110
9111 if (!bfd_get_section_contents (ibfd, sec, buf,
9112 rel->r_offset & ~3, 4))
9113 goto got_error_ret;
9114 insn = bfd_get_32 (ibfd, buf);
9115 if (((insn & ((0x3f << 26) | 0x1f << 16))
9116 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9117 continue;
9118 break;
9119
9120 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9121 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9122 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9123 continue;
9124 if (!bfd_get_section_contents (ibfd, sec, buf,
9125 rel->r_offset & ~3, 4))
9126 goto got_error_ret;
9127 insn = bfd_get_32 (ibfd, buf);
9128 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9129 continue;
9130 break;
4a421c53
AM
9131
9132 case R_PPC64_GOT_PCREL34:
9133 pc = rel->r_offset;
9134 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9135 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9136 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9137 continue;
9138 if (!bfd_get_section_contents (ibfd, sec, buf,
9139 rel->r_offset & ~3, 8))
9140 goto got_error_ret;
9141 insn = bfd_get_32 (ibfd, buf);
9142 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9143 continue;
9144 insn = bfd_get_32 (ibfd, buf + 4);
9145 if ((insn & (0x3f << 26)) != 57u << 26)
9146 continue;
9147 break;
066f4018 9148 }
bb22a418 9149#undef LIMIT_ADJUST
066f4018
AM
9150
9151 if (h != NULL)
9152 ent = h->got.glist;
9153 else
9154 {
9155 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9156 ent = local_got_ents[r_symndx];
9157 }
9158 for (; ent != NULL; ent = ent->next)
4a421c53 9159 if (ent->addend == sym_addend
066f4018
AM
9160 && ent->owner == ibfd
9161 && ent->tls_type == 0)
9162 break;
9163 BFD_ASSERT (ent && ent->got.refcount > 0);
9164 ent->got.refcount -= 1;
9165 }
9166
9167 if (elf_section_data (sec)->relocs != relstart)
9168 free (relstart);
9169 }
9170
9171 if (local_syms != NULL
9172 && symtab_hdr->contents != (unsigned char *) local_syms)
9173 {
9174 if (!info->keep_memory)
9175 free (local_syms);
9176 else
9177 symtab_hdr->contents = (unsigned char *) local_syms;
9178 }
9179 }
9180
c5614fa4
AM
9181 return TRUE;
9182}
9183
1bbe0902
AM
9184/* Return true iff input section I references the TOC using
9185 instructions limited to +/-32k offsets. */
9186
9187bfd_boolean
9188ppc64_elf_has_small_toc_reloc (asection *i)
9189{
9190 return (is_ppc64_elf (i->owner)
9191 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9192}
9193
927be08e
AM
9194/* Allocate space for one GOT entry. */
9195
9196static void
9197allocate_got (struct elf_link_hash_entry *h,
9198 struct bfd_link_info *info,
9199 struct got_entry *gent)
9200{
9201 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9202 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9203 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9204 ? 16 : 8);
9205 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9206 ? 2 : 1) * sizeof (Elf64_External_Rela);
9207 asection *got = ppc64_elf_tdata (gent->owner)->got;
9208
9209 gent->got.offset = got->size;
9210 got->size += entsize;
9211
19e08130 9212 if (h->type == STT_GNU_IFUNC)
927be08e 9213 {
33e44f2e 9214 htab->elf.irelplt->size += rentsize;
19e08130 9215 htab->got_reli_size += rentsize;
927be08e 9216 }
f15d0b54
AM
9217 else if (((bfd_link_pic (info)
9218 && !((gent->tls_type & TLS_TPREL) != 0
9219 && bfd_link_executable (info)
9220 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9221 || (htab->elf.dynamic_sections_created
9222 && h->dynindx != -1
9223 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9224 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9225 {
19e08130 9226 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9227 relgot->size += rentsize;
927be08e
AM
9228 }
9229}
9230
7865406b
AM
9231/* This function merges got entries in the same toc group. */
9232
9233static void
9234merge_got_entries (struct got_entry **pent)
9235{
9236 struct got_entry *ent, *ent2;
9237
9238 for (ent = *pent; ent != NULL; ent = ent->next)
9239 if (!ent->is_indirect)
9240 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9241 if (!ent2->is_indirect
9242 && ent2->addend == ent->addend
9243 && ent2->tls_type == ent->tls_type
9244 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9245 {
9246 ent2->is_indirect = TRUE;
9247 ent2->got.ent = ent;
9248 }
9249}
9250
46434633 9251/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9252
9253static bfd_boolean
46434633
AM
9254ensure_undef_dynamic (struct bfd_link_info *info,
9255 struct elf_link_hash_entry *h)
f0158f44
AM
9256{
9257 struct elf_link_hash_table *htab = elf_hash_table (info);
9258
9259 if (htab->dynamic_sections_created
46434633
AM
9260 && ((info->dynamic_undefined_weak != 0
9261 && h->root.type == bfd_link_hash_undefweak)
9262 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9263 && h->dynindx == -1
9264 && !h->forced_local
9265 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9266 return bfd_elf_link_record_dynamic_symbol (info, h);
9267 return TRUE;
9268}
9269
65f38f15
AM
9270/* Allocate space in .plt, .got and associated reloc sections for
9271 dynamic relocs. */
5bd4f169 9272
b34976b6 9273static bfd_boolean
4ce794b7 9274allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9275{
65f38f15
AM
9276 struct bfd_link_info *info;
9277 struct ppc_link_hash_table *htab;
5bd4f169 9278 asection *s;
65f38f15 9279 struct ppc_link_hash_entry *eh;
0b8bcf0d 9280 struct got_entry **pgent, *gent;
5bd4f169 9281
e92d460e 9282 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9283 return TRUE;
5bd4f169 9284
65f38f15
AM
9285 info = (struct bfd_link_info *) inf;
9286 htab = ppc_hash_table (info);
4dfe6ac6
NC
9287 if (htab == NULL)
9288 return FALSE;
5bd4f169 9289
951fd09b
AM
9290 eh = (struct ppc_link_hash_entry *) h;
9291 /* Run through the TLS GD got entries first if we're changing them
9292 to TPREL. */
b00a0a86 9293 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
951fd09b
AM
9294 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9295 if (gent->got.refcount > 0
9296 && (gent->tls_type & TLS_GD) != 0)
9297 {
9298 /* This was a GD entry that has been converted to TPREL. If
9299 there happens to be a TPREL entry we can use that one. */
9300 struct got_entry *ent;
9301 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9302 if (ent->got.refcount > 0
9303 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9304 && ent->addend == gent->addend
9305 && ent->owner == gent->owner)
951fd09b
AM
9306 {
9307 gent->got.refcount = 0;
9308 break;
9309 }
9310
9311 /* If not, then we'll be using our own TPREL entry. */
9312 if (gent->got.refcount != 0)
9313 gent->tls_type = TLS_TLS | TLS_TPREL;
9314 }
9315
7865406b
AM
9316 /* Remove any list entry that won't generate a word in the GOT before
9317 we call merge_got_entries. Otherwise we risk merging to empty
9318 entries. */
0b8bcf0d
AM
9319 pgent = &h->got.glist;
9320 while ((gent = *pgent) != NULL)
411e1bfb 9321 if (gent->got.refcount > 0)
7865406b
AM
9322 {
9323 if ((gent->tls_type & TLS_LD) != 0
9324 && !h->def_dynamic)
9325 {
9326 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9327 *pgent = gent->next;
9328 }
9329 else
9330 pgent = &gent->next;
9331 }
9332 else
9333 *pgent = gent->next;
9334
9335 if (!htab->do_multi_toc)
9336 merge_got_entries (&h->got.glist);
9337
9338 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9339 if (!gent->is_indirect)
411e1bfb 9340 {
46434633
AM
9341 /* Make sure this symbol is output as a dynamic symbol. */
9342 if (!ensure_undef_dynamic (info, h))
f0158f44 9343 return FALSE;
65f38f15 9344
0c8d6e5c 9345 if (!is_ppc64_elf (gent->owner))
927be08e 9346 abort ();
0ffa91dd 9347
927be08e 9348 allocate_got (h, info, gent);
411e1bfb 9349 }
65f38f15 9350
954b63d4
AM
9351 /* If no dynamic sections we can't have dynamic relocs, except for
9352 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9353 if (!htab->elf.dynamic_sections_created
9354 && h->type != STT_GNU_IFUNC)
9355 eh->dyn_relocs = NULL;
9356
529fe20e
AM
9357 /* Discard relocs on undefined symbols that must be local. */
9358 else if (h->root.type == bfd_link_hash_undefined
9359 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9360 eh->dyn_relocs = NULL;
9361
954b63d4
AM
9362 /* Also discard relocs on undefined weak syms with non-default
9363 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9364 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
9365 eh->dyn_relocs = NULL;
9366
8a2058b5 9367 if (eh->dyn_relocs != NULL)
65f38f15 9368 {
8a2058b5
AM
9369 struct elf_dyn_relocs *p, **pp;
9370
57e7d118
AM
9371 /* In the shared -Bsymbolic case, discard space allocated for
9372 dynamic pc-relative relocs against symbols which turn out to
9373 be defined in regular objects. For the normal shared case,
9374 discard space for relocs that have become local due to symbol
9375 visibility changes. */
9376
9377 if (bfd_link_pic (info))
65f38f15 9378 {
57e7d118
AM
9379 /* Relocs that use pc_count are those that appear on a call
9380 insn, or certain REL relocs (see must_be_dyn_reloc) that
9381 can be generated via assembly. We want calls to
9382 protected symbols to resolve directly to the function
9383 rather than going via the plt. If people want function
9384 pointer comparisons to work as expected then they should
9385 avoid writing weird assembly. */
9386 if (SYMBOL_CALLS_LOCAL (info, h))
9387 {
57e7d118
AM
9388 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9389 {
9390 p->count -= p->pc_count;
9391 p->pc_count = 0;
9392 if (p->count == 0)
9393 *pp = p->next;
9394 else
9395 pp = &p->next;
9396 }
9397 }
65f38f15 9398
954b63d4 9399 if (eh->dyn_relocs != NULL)
5bd4f169 9400 {
46434633
AM
9401 /* Make sure this symbol is output as a dynamic symbol. */
9402 if (!ensure_undef_dynamic (info, h))
f0158f44 9403 return FALSE;
5bd4f169 9404 }
65f38f15 9405 }
529fe20e 9406 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 9407 {
8a2058b5 9408 /* For the non-pic case, discard space for relocs against
57e7d118
AM
9409 symbols which turn out to need copy relocs or are not
9410 dynamic. */
529fe20e
AM
9411 if (h->dynamic_adjusted
9412 && !h->def_regular
9413 && !ELF_COMMON_DEF_P (h))
f0158f44 9414 {
46434633
AM
9415 /* Make sure this symbol is output as a dynamic symbol. */
9416 if (!ensure_undef_dynamic (info, h))
f0158f44 9417 return FALSE;
dfbb6ac9 9418
f0158f44
AM
9419 if (h->dynindx == -1)
9420 eh->dyn_relocs = NULL;
9421 }
9422 else
8a2058b5 9423 eh->dyn_relocs = NULL;
57e7d118
AM
9424 }
9425
9426 /* Finally, allocate space. */
9427 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9428 {
9429 asection *sreloc = elf_section_data (p->sec)->sreloc;
9430 if (eh->elf.type == STT_GNU_IFUNC)
9431 sreloc = htab->elf.irelplt;
9432 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9433 }
65f38f15 9434 }
57e7d118 9435
2d7ad24e
AM
9436 /* We might need a PLT entry when the symbol
9437 a) is dynamic, or
9438 b) is an ifunc, or
9439 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9440 d) has plt16 relocs and we are linking statically. */
9441 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9442 || h->type == STT_GNU_IFUNC
9443 || (h->needs_plt && h->dynamic_adjusted)
9444 || (h->needs_plt
9445 && h->def_regular
9446 && !htab->elf.dynamic_sections_created
3e04d765 9447 && !htab->can_convert_all_inline_plt
2d7ad24e
AM
9448 && (((struct ppc_link_hash_entry *) h)->tls_mask
9449 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 9450 {
57e7d118
AM
9451 struct plt_entry *pent;
9452 bfd_boolean doneone = FALSE;
9453 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9454 if (pent->plt.refcount > 0)
9455 {
9456 if (!htab->elf.dynamic_sections_created
9457 || h->dynindx == -1)
9458 {
2d7ad24e
AM
9459 if (h->type == STT_GNU_IFUNC)
9460 {
9461 s = htab->elf.iplt;
9462 pent->plt.offset = s->size;
9463 s->size += PLT_ENTRY_SIZE (htab);
9464 s = htab->elf.irelplt;
9465 }
9466 else
9467 {
9468 s = htab->pltlocal;
9469 pent->plt.offset = s->size;
9470 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9471 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9472 }
57e7d118
AM
9473 }
9474 else
9475 {
9476 /* If this is the first .plt entry, make room for the special
9477 first entry. */
9478 s = htab->elf.splt;
9479 if (s->size == 0)
9480 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9481
57e7d118 9482 pent->plt.offset = s->size;
65f38f15 9483
57e7d118
AM
9484 /* Make room for this entry. */
9485 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9486
57e7d118
AM
9487 /* Make room for the .glink code. */
9488 s = htab->glink;
9489 if (s->size == 0)
9e390558 9490 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9491 if (htab->opd_abi)
9492 {
9493 /* We need bigger stubs past index 32767. */
9e390558 9494 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9495 s->size += 4;
9496 s->size += 2*4;
9497 }
9498 else
9499 s->size += 4;
65f38f15 9500
57e7d118
AM
9501 /* We also need to make an entry in the .rela.plt section. */
9502 s = htab->elf.srelplt;
9503 }
2d7ad24e
AM
9504 if (s != NULL)
9505 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
9506 doneone = TRUE;
9507 }
9508 else
9509 pent->plt.offset = (bfd_vma) -1;
9510 if (!doneone)
9511 {
9512 h->plt.plist = NULL;
9513 h->needs_plt = 0;
9514 }
65f38f15 9515 }
57e7d118 9516 else
65f38f15 9517 {
57e7d118
AM
9518 h->plt.plist = NULL;
9519 h->needs_plt = 0;
65f38f15
AM
9520 }
9521
b34976b6 9522 return TRUE;
65f38f15
AM
9523}
9524
9e390558
AM
9525#define PPC_LO(v) ((v) & 0xffff)
9526#define PPC_HI(v) (((v) >> 16) & 0xffff)
9527#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
9528#define D34(v) \
9529 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9530#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 9531
a345bc8d
AM
9532/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9533 to set up space for global entry stubs. These are put in glink,
9534 after the branch table. */
65f38f15 9535
b34976b6 9536static bfd_boolean
a345bc8d 9537size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9538{
a345bc8d
AM
9539 struct bfd_link_info *info;
9540 struct ppc_link_hash_table *htab;
9541 struct plt_entry *pent;
9e390558 9542 asection *s, *plt;
65f38f15 9543
a345bc8d
AM
9544 if (h->root.type == bfd_link_hash_indirect)
9545 return TRUE;
65f38f15 9546
a345bc8d
AM
9547 if (!h->pointer_equality_needed)
9548 return TRUE;
65f38f15 9549
a345bc8d
AM
9550 if (h->def_regular)
9551 return TRUE;
65f38f15 9552
a345bc8d
AM
9553 info = inf;
9554 htab = ppc_hash_table (info);
9555 if (htab == NULL)
9556 return FALSE;
9557
9e390558
AM
9558 s = htab->global_entry;
9559 plt = htab->elf.splt;
a345bc8d
AM
9560 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9561 if (pent->plt.offset != (bfd_vma) -1
9562 && pent->addend == 0)
9563 {
afe397ea
AM
9564 /* For ELFv2, if this symbol is not defined in a regular file
9565 and we are not generating a shared library or pie, then we
9566 need to define the symbol in the executable on a call stub.
9567 This is to avoid text relocations. */
9e390558
AM
9568 bfd_vma off, stub_align, stub_off, stub_size;
9569 unsigned int align_power;
9570
9571 stub_size = 16;
9572 stub_off = s->size;
9573 if (htab->params->plt_stub_align >= 0)
9574 align_power = htab->params->plt_stub_align;
9575 else
9576 align_power = -htab->params->plt_stub_align;
9577 /* Setting section alignment is delayed until we know it is
9578 non-empty. Otherwise the .text output section will be
9579 aligned at least to plt_stub_align even when no global
9580 entry stubs are needed. */
9581 if (s->alignment_power < align_power)
9582 s->alignment_power = align_power;
9583 stub_align = (bfd_vma) 1 << align_power;
9584 if (htab->params->plt_stub_align >= 0
9585 || ((((stub_off + stub_size - 1) & -stub_align)
9586 - (stub_off & -stub_align))
9587 > ((stub_size - 1) & -stub_align)))
9588 stub_off = (stub_off + stub_align - 1) & -stub_align;
9589 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9590 off -= stub_off + s->output_offset + s->output_section->vma;
9591 /* Note that for --plt-stub-align negative we have a possible
9592 dependency between stub offset and size. Break that
9593 dependency by assuming the max stub size when calculating
9594 the stub offset. */
9595 if (PPC_HA (off) == 0)
9596 stub_size -= 4;
8a2058b5 9597 h->root.type = bfd_link_hash_defined;
afe397ea 9598 h->root.u.def.section = s;
9e390558
AM
9599 h->root.u.def.value = stub_off;
9600 s->size = stub_off + stub_size;
a345bc8d
AM
9601 break;
9602 }
9603 return TRUE;
9604}
9605
9606/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9607 read-only sections. */
9608
9609static bfd_boolean
98bbb1b8 9610maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 9611{
98bbb1b8
AM
9612 asection *sec;
9613
a345bc8d
AM
9614 if (h->root.type == bfd_link_hash_indirect)
9615 return TRUE;
9616
98bbb1b8
AM
9617 sec = readonly_dynrelocs (h);
9618 if (sec != NULL)
a345bc8d 9619 {
98bbb1b8
AM
9620 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9621
9622 info->flags |= DF_TEXTREL;
2cdcc330
AM
9623 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9624 " in read-only section `%pA'\n"),
9625 sec->owner, h->root.root.string, sec);
a345bc8d
AM
9626
9627 /* Not an error, just cut short the traversal. */
9628 return FALSE;
65f38f15 9629 }
b34976b6 9630 return TRUE;
65f38f15
AM
9631}
9632
9633/* Set the sizes of the dynamic sections. */
9634
b34976b6 9635static bfd_boolean
ee67d69a 9636ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9637 struct bfd_link_info *info)
65f38f15
AM
9638{
9639 struct ppc_link_hash_table *htab;
9640 bfd *dynobj;
9641 asection *s;
b34976b6 9642 bfd_boolean relocs;
65f38f15 9643 bfd *ibfd;
7865406b 9644 struct got_entry *first_tlsld;
65f38f15
AM
9645
9646 htab = ppc_hash_table (info);
4dfe6ac6
NC
9647 if (htab == NULL)
9648 return FALSE;
9649
65f38f15
AM
9650 dynobj = htab->elf.dynobj;
9651 if (dynobj == NULL)
9652 abort ();
9653
9654 if (htab->elf.dynamic_sections_created)
9655 {
9656 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9657 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9658 {
3d4d4302 9659 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9660 if (s == NULL)
9661 abort ();
eea6121a 9662 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9663 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9664 }
9665 }
9666
9667 /* Set up .got offsets for local syms, and space for local dynamic
9668 relocs. */
c72f2fb2 9669 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9670 {
411e1bfb
AM
9671 struct got_entry **lgot_ents;
9672 struct got_entry **end_lgot_ents;
e054468f
AM
9673 struct plt_entry **local_plt;
9674 struct plt_entry **end_local_plt;
f961d9dd 9675 unsigned char *lgot_masks;
65f38f15
AM
9676 bfd_size_type locsymcount;
9677 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9678
0c8d6e5c 9679 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9680 continue;
9681
9682 for (s = ibfd->sections; s != NULL; s = s->next)
9683 {
19e08130 9684 struct ppc_dyn_relocs *p;
65f38f15 9685
6edfbbad 9686 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9687 {
ec338859
AM
9688 if (!bfd_is_abs_section (p->sec)
9689 && bfd_is_abs_section (p->sec->output_section))
9690 {
9691 /* Input section has been discarded, either because
9692 it is a copy of a linkonce section or due to
9693 linker script /DISCARD/, so we'll be discarding
9694 the relocs too. */
9695 }
248866a8 9696 else if (p->count != 0)
ec338859 9697 {
19e08130
AM
9698 asection *srel = elf_section_data (p->sec)->sreloc;
9699 if (p->ifunc)
33e44f2e 9700 srel = htab->elf.irelplt;
eea6121a 9701 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9702 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9703 info->flags |= DF_TEXTREL;
ec338859 9704 }
65f38f15
AM
9705 }
9706 }
9707
411e1bfb
AM
9708 lgot_ents = elf_local_got_ents (ibfd);
9709 if (!lgot_ents)
65f38f15
AM
9710 continue;
9711
0ffa91dd 9712 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9713 locsymcount = symtab_hdr->sh_info;
411e1bfb 9714 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9715 local_plt = (struct plt_entry **) end_lgot_ents;
9716 end_local_plt = local_plt + locsymcount;
f961d9dd 9717 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9718 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9719 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9720 {
0b8bcf0d 9721 struct got_entry **pent, *ent;
411e1bfb 9722
0b8bcf0d
AM
9723 pent = lgot_ents;
9724 while ((ent = *pent) != NULL)
411e1bfb
AM
9725 if (ent->got.refcount > 0)
9726 {
e7b938ca 9727 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9728 {
927be08e 9729 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9730 *pent = ent->next;
411e1bfb
AM
9731 }
9732 else
9733 {
19e08130
AM
9734 unsigned int ent_size = 8;
9735 unsigned int rel_size = sizeof (Elf64_External_Rela);
9736
eea6121a 9737 ent->got.offset = s->size;
e7b938ca 9738 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9739 {
19e08130
AM
9740 ent_size *= 2;
9741 rel_size *= 2;
9742 }
9743 s->size += ent_size;
37da22e5 9744 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 9745 {
33e44f2e 9746 htab->elf.irelplt->size += rel_size;
19e08130
AM
9747 htab->got_reli_size += rel_size;
9748 }
f15d0b54
AM
9749 else if (bfd_link_pic (info)
9750 && !((ent->tls_type & TLS_TPREL) != 0
9751 && bfd_link_executable (info)))
19e08130
AM
9752 {
9753 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9754 srel->size += rel_size;
927be08e 9755 }
0b8bcf0d 9756 pent = &ent->next;
411e1bfb
AM
9757 }
9758 }
9759 else
0b8bcf0d 9760 *pent = ent->next;
65f38f15 9761 }
e054468f 9762
2d7ad24e
AM
9763 /* Allocate space for plt calls to local syms. */
9764 lgot_masks = (unsigned char *) end_local_plt;
9765 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
9766 {
9767 struct plt_entry *ent;
9768
9769 for (ent = *local_plt; ent != NULL; ent = ent->next)
9770 if (ent->plt.refcount > 0)
9771 {
2d7ad24e
AM
9772 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9773 {
9774 s = htab->elf.iplt;
9775 ent->plt.offset = s->size;
9776 s->size += PLT_ENTRY_SIZE (htab);
9777 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9778 }
3e04d765
AM
9779 else if (htab->can_convert_all_inline_plt
9780 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
9781 ent->plt.offset = (bfd_vma) -1;
9782 else
9783 {
9784 s = htab->pltlocal;
9785 ent->plt.offset = s->size;
9786 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9787 if (bfd_link_pic (info))
9788 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9789 }
e054468f
AM
9790 }
9791 else
9792 ent->plt.offset = (bfd_vma) -1;
9793 }
65f38f15
AM
9794 }
9795
9796 /* Allocate global sym .plt and .got entries, and space for global
9797 sym dynamic relocs. */
4ce794b7 9798 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 9799
0e1862bb 9800 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 9801 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 9802
7865406b 9803 first_tlsld = NULL;
c72f2fb2 9804 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 9805 {
7865406b
AM
9806 struct got_entry *ent;
9807
0c8d6e5c 9808 if (!is_ppc64_elf (ibfd))
102890f0
AM
9809 continue;
9810
7865406b
AM
9811 ent = ppc64_tlsld_got (ibfd);
9812 if (ent->got.refcount > 0)
102890f0 9813 {
7865406b 9814 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9815 {
7865406b
AM
9816 ent->is_indirect = TRUE;
9817 ent->got.ent = first_tlsld;
9818 }
9819 else
9820 {
9821 if (first_tlsld == NULL)
9822 first_tlsld = ent;
9823 s = ppc64_elf_tdata (ibfd)->got;
9824 ent->got.offset = s->size;
9825 ent->owner = ibfd;
9826 s->size += 16;
0e1862bb 9827 if (bfd_link_pic (info))
7865406b
AM
9828 {
9829 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9830 srel->size += sizeof (Elf64_External_Rela);
9831 }
102890f0
AM
9832 }
9833 }
9834 else
7865406b 9835 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9836 }
9837
65f38f15
AM
9838 /* We now have determined the sizes of the various dynamic sections.
9839 Allocate memory for them. */
b34976b6 9840 relocs = FALSE;
65f38f15
AM
9841 for (s = dynobj->sections; s != NULL; s = s->next)
9842 {
9843 if ((s->flags & SEC_LINKER_CREATED) == 0)
9844 continue;
9845
4ce794b7 9846 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9847 /* These haven't been allocated yet; don't strip. */
9848 continue;
33e44f2e
AM
9849 else if (s == htab->elf.sgot
9850 || s == htab->elf.splt
9851 || s == htab->elf.iplt
2d7ad24e 9852 || s == htab->pltlocal
c456f082 9853 || s == htab->glink
9e390558 9854 || s == htab->global_entry
5474d94f
AM
9855 || s == htab->elf.sdynbss
9856 || s == htab->elf.sdynrelro)
65f38f15
AM
9857 {
9858 /* Strip this section if we don't need it; see the
9859 comment below. */
5bd4f169 9860 }
58d180e8
AM
9861 else if (s == htab->glink_eh_frame)
9862 {
9863 if (!bfd_is_abs_section (s->output_section))
9864 /* Not sized yet. */
9865 continue;
9866 }
70cc837d 9867 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9868 {
c456f082 9869 if (s->size != 0)
5bd4f169 9870 {
33e44f2e 9871 if (s != htab->elf.srelplt)
b34976b6 9872 relocs = TRUE;
5bd4f169
AM
9873
9874 /* We use the reloc_count field as a counter if we need
9875 to copy relocs into the output file. */
9876 s->reloc_count = 0;
9877 }
9878 }
65f38f15 9879 else
5bd4f169
AM
9880 {
9881 /* It's not one of our sections, so don't allocate space. */
9882 continue;
9883 }
9884
eea6121a 9885 if (s->size == 0)
5bd4f169 9886 {
c456f082
AM
9887 /* If we don't need this section, strip it from the
9888 output file. This is mostly to handle .rela.bss and
9889 .rela.plt. We must create both sections in
9890 create_dynamic_sections, because they must be created
9891 before the linker maps input sections to output
9892 sections. The linker does that before
9893 adjust_dynamic_symbol is called, and it is that
9894 function which decides whether anything needs to go
9895 into these sections. */
8423293d 9896 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9897 continue;
9898 }
9899
06bcf541
AM
9900 if (bfd_is_abs_section (s->output_section))
9901 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9902 s->name);
9903
c456f082 9904 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9905 continue;
9906
65f38f15
AM
9907 /* Allocate memory for the section contents. We use bfd_zalloc
9908 here in case unused entries are not reclaimed before the
9909 section's contents are written out. This should not happen,
411e1bfb
AM
9910 but this way if it does we get a R_PPC64_NONE reloc in .rela
9911 sections instead of garbage.
9912 We also rely on the section contents being zero when writing
5474d94f 9913 the GOT and .dynrelro. */
eea6121a 9914 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9915 if (s->contents == NULL)
b34976b6 9916 return FALSE;
5bd4f169
AM
9917 }
9918
c72f2fb2 9919 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 9920 {
0c8d6e5c 9921 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9922 continue;
9923
e717da7e 9924 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 9925 if (s != NULL && s != htab->elf.sgot)
e717da7e 9926 {
eea6121a 9927 if (s->size == 0)
8423293d 9928 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9929 else
9930 {
eea6121a 9931 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9932 if (s->contents == NULL)
9933 return FALSE;
9934 }
9935 }
9936 s = ppc64_elf_tdata (ibfd)->relgot;
9937 if (s != NULL)
9938 {
eea6121a 9939 if (s->size == 0)
8423293d 9940 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9941 else
9942 {
eea6121a 9943 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9944 if (s->contents == NULL)
9945 return FALSE;
9946 relocs = TRUE;
9947 s->reloc_count = 0;
9948 }
9949 }
9950 }
9951
e86ce104 9952 if (htab->elf.dynamic_sections_created)
5bd4f169 9953 {
e8910a83
AM
9954 bfd_boolean tls_opt;
9955
5bd4f169
AM
9956 /* Add some entries to the .dynamic section. We fill in the
9957 values later, in ppc64_elf_finish_dynamic_sections, but we
9958 must add the entries now so that we get the correct size for
9959 the .dynamic section. The DT_DEBUG entry is filled in by the
9960 dynamic linker and used by the debugger. */
dc810e39 9961#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9962 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9963
0e1862bb 9964 if (bfd_link_executable (info))
5bd4f169 9965 {
dc810e39 9966 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9967 return FALSE;
5bd4f169
AM
9968 }
9969
33e44f2e 9970 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 9971 {
dc810e39
AM
9972 if (!add_dynamic_entry (DT_PLTGOT, 0)
9973 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9974 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9975 || !add_dynamic_entry (DT_JMPREL, 0)
9976 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9977 return FALSE;
5bd4f169
AM
9978 }
9979
ee67d69a 9980 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
9981 {
9982 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9983 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9984 return FALSE;
19397422
AM
9985 }
9986
7c9cf415 9987 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
9988 && htab->tls_get_addr_fd != NULL
9989 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9990 if (tls_opt || !htab->opd_abi)
9991 {
9992 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9993 return FALSE;
9994 }
a7f2871e 9995
5bd4f169
AM
9996 if (relocs)
9997 {
dc810e39
AM
9998 if (!add_dynamic_entry (DT_RELA, 0)
9999 || !add_dynamic_entry (DT_RELASZ, 0)
10000 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10001 return FALSE;
5bd4f169 10002
65f38f15
AM
10003 /* If any dynamic relocs apply to a read-only section,
10004 then we need a DT_TEXTREL entry. */
248866a8 10005 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10006 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10007
65f38f15 10008 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10009 {
65f38f15 10010 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10011 return FALSE;
5bd4f169 10012 }
5bd4f169 10013 }
5bd4f169 10014 }
65f38f15 10015#undef add_dynamic_entry
5bd4f169 10016
b34976b6 10017 return TRUE;
5bd4f169
AM
10018}
10019
a345bc8d
AM
10020/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10021
10022static bfd_boolean
10023ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10024{
10025 if (h->plt.plist != NULL
10026 && !h->def_regular
10027 && !h->pointer_equality_needed)
10028 return FALSE;
10029
10030 return _bfd_elf_hash_symbol (h);
10031}
10032
721956f4 10033/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10034
4ce794b7
AM
10035static inline enum ppc_stub_type
10036ppc_type_of_stub (asection *input_sec,
10037 const Elf_Internal_Rela *rel,
10038 struct ppc_link_hash_entry **hash,
e054468f 10039 struct plt_entry **plt_ent,
6911b7dc
AM
10040 bfd_vma destination,
10041 unsigned long local_off)
5bd4f169 10042{
721956f4
AM
10043 struct ppc_link_hash_entry *h = *hash;
10044 bfd_vma location;
10045 bfd_vma branch_offset;
10046 bfd_vma max_branch_offset;
4ce794b7 10047 enum elf_ppc64_reloc_type r_type;
5bd4f169 10048
721956f4
AM
10049 if (h != NULL)
10050 {
e054468f 10051 struct plt_entry *ent;
7fe2b9a6 10052 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10053 if (h->oh != NULL
10054 && h->oh->is_func_descriptor)
7b8f6675
AM
10055 {
10056 fdh = ppc_follow_link (h->oh);
10057 *hash = fdh;
10058 }
8387904d 10059
e054468f
AM
10060 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10061 if (ent->addend == rel->r_addend
10062 && ent->plt.offset != (bfd_vma) -1)
10063 {
e054468f
AM
10064 *plt_ent = ent;
10065 return ppc_stub_plt_call;
10066 }
5bd4f169 10067
7fe2b9a6
AM
10068 /* Here, we know we don't have a plt entry. If we don't have a
10069 either a defined function descriptor or a defined entry symbol
10070 in a regular object file, then it is pointless trying to make
10071 any other type of stub. */
854b41e7
AM
10072 if (!is_static_defined (&fdh->elf)
10073 && !is_static_defined (&h->elf))
721956f4 10074 return ppc_stub_none;
5d1634d7 10075 }
e054468f
AM
10076 else if (elf_local_got_ents (input_sec->owner) != NULL)
10077 {
10078 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10079 struct plt_entry **local_plt = (struct plt_entry **)
10080 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10081 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10082
10083 if (local_plt[r_symndx] != NULL)
10084 {
10085 struct plt_entry *ent;
10086
10087 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10088 if (ent->addend == rel->r_addend
10089 && ent->plt.offset != (bfd_vma) -1)
10090 {
10091 *plt_ent = ent;
10092 return ppc_stub_plt_call;
10093 }
10094 }
10095 }
5d1634d7 10096
721956f4
AM
10097 /* Determine where the call point is. */
10098 location = (input_sec->output_offset
10099 + input_sec->output_section->vma
10100 + rel->r_offset);
5d1634d7 10101
721956f4
AM
10102 branch_offset = destination - location;
10103 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10104
721956f4
AM
10105 /* Determine if a long branch stub is needed. */
10106 max_branch_offset = 1 << 25;
23cedd1d
AM
10107 if (r_type == R_PPC64_REL14
10108 || r_type == R_PPC64_REL14_BRTAKEN
10109 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10110 max_branch_offset = 1 << 15;
5d1634d7 10111
6911b7dc 10112 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10113 /* We need a stub. Figure out whether a long_branch or plt_branch
10114 is needed later. */
10115 return ppc_stub_long_branch;
5d1634d7 10116
721956f4 10117 return ppc_stub_none;
5d1634d7
AM
10118}
10119
f891966f
AM
10120/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10121 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10122 . mflr %r12
10123 . bcl 20,31,1f
10124 .1: mflr %r11
10125 . mtlr %r12
05d0e962 10126 . lis %r12,xxx-1b@highest
f891966f 10127 . ori %r12,%r12,xxx-1b@higher
05d0e962 10128 . sldi %r12,%r12,32
f891966f 10129 . oris %r12,%r12,xxx-1b@high
05d0e962 10130 . ori %r12,%r12,xxx-1b@l
f891966f 10131 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10132
10133static bfd_byte *
10134build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10135{
f891966f
AM
10136 bfd_put_32 (abfd, MFLR_R12, p);
10137 p += 4;
10138 bfd_put_32 (abfd, BCL_20_31, p);
10139 p += 4;
10140 bfd_put_32 (abfd, MFLR_R11, p);
10141 p += 4;
10142 bfd_put_32 (abfd, MTLR_R12, p);
10143 p += 4;
05d0e962
AM
10144 if (off + 0x8000 < 0x10000)
10145 {
10146 if (load)
10147 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10148 else
10149 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10150 p += 4;
10151 }
10152 else if (off + 0x80008000ULL < 0x100000000ULL)
10153 {
10154 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10155 p += 4;
10156 if (load)
10157 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10158 else
10159 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10160 p += 4;
10161 }
10162 else
10163 {
10164 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10165 {
10166 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10167 p += 4;
10168 }
10169 else
10170 {
10171 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10172 p += 4;
10173 if (((off >> 32) & 0xffff) != 0)
10174 {
10175 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10176 p += 4;
10177 }
10178 }
10179 if (((off >> 32) & 0xffffffffULL) != 0)
10180 {
10181 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10182 p += 4;
10183 }
10184 if (PPC_HI (off) != 0)
10185 {
10186 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10187 p += 4;
10188 }
10189 if (PPC_LO (off) != 0)
10190 {
10191 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10192 p += 4;
10193 }
10194 if (load)
10195 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10196 else
10197 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10198 p += 4;
10199 }
10200 return p;
10201}
10202
10203static unsigned int
10204size_offset (bfd_vma off)
10205{
10206 unsigned int size;
10207 if (off + 0x8000 < 0x10000)
10208 size = 4;
10209 else if (off + 0x80008000ULL < 0x100000000ULL)
10210 size = 8;
10211 else
10212 {
10213 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10214 size = 4;
10215 else
10216 {
10217 size = 4;
10218 if (((off >> 32) & 0xffff) != 0)
10219 size += 4;
10220 }
10221 if (((off >> 32) & 0xffffffffULL) != 0)
10222 size += 4;
10223 if (PPC_HI (off) != 0)
10224 size += 4;
10225 if (PPC_LO (off) != 0)
10226 size += 4;
10227 size += 4;
10228 }
f891966f 10229 return size + 16;
05d0e962
AM
10230}
10231
3d58e1fc
AM
10232static unsigned int
10233num_relocs_for_offset (bfd_vma off)
10234{
10235 unsigned int num_rel;
10236 if (off + 0x8000 < 0x10000)
10237 num_rel = 1;
10238 else if (off + 0x80008000ULL < 0x100000000ULL)
10239 num_rel = 2;
10240 else
10241 {
10242 num_rel = 1;
10243 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10244 && ((off >> 32) & 0xffff) != 0)
10245 num_rel += 1;
10246 if (PPC_HI (off) != 0)
10247 num_rel += 1;
10248 if (PPC_LO (off) != 0)
10249 num_rel += 1;
10250 }
10251 return num_rel;
10252}
10253
10254static Elf_Internal_Rela *
10255emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10256 bfd_vma roff, bfd_vma targ, bfd_vma off)
10257{
10258 bfd_vma relative_targ = targ - (roff - 8);
10259 if (bfd_big_endian (info->output_bfd))
10260 roff += 2;
10261 r->r_offset = roff;
10262 r->r_addend = relative_targ + roff;
10263 if (off + 0x8000 < 0x10000)
10264 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10265 else if (off + 0x80008000ULL < 0x100000000ULL)
10266 {
10267 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10268 ++r;
10269 roff += 4;
10270 r->r_offset = roff;
10271 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10272 r->r_addend = relative_targ + roff;
10273 }
10274 else
10275 {
10276 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10277 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10278 else
10279 {
10280 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10281 if (((off >> 32) & 0xffff) != 0)
10282 {
10283 ++r;
10284 roff += 4;
10285 r->r_offset = roff;
10286 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10287 r->r_addend = relative_targ + roff;
10288 }
10289 }
10290 if (((off >> 32) & 0xffffffffULL) != 0)
10291 roff += 4;
10292 if (PPC_HI (off) != 0)
10293 {
10294 ++r;
10295 roff += 4;
10296 r->r_offset = roff;
10297 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10298 r->r_addend = relative_targ + roff;
10299 }
10300 if (PPC_LO (off) != 0)
10301 {
10302 ++r;
10303 roff += 4;
10304 r->r_offset = roff;
10305 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10306 r->r_addend = relative_targ + roff;
10307 }
10308 }
10309 return r;
10310}
10311
04bdff6a
AM
10312static bfd_byte *
10313build_powerxx_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10314 bfd_boolean load)
10315{
10316 uint64_t insn;
10317 if (off - odd + (1ULL << 33) < 1ULL << 34)
10318 {
10319 off -= odd;
10320 if (odd)
10321 {
10322 bfd_put_32 (abfd, NOP, p);
10323 p += 4;
10324 }
10325 if (load)
10326 insn = PLD_R12_PC;
10327 else
10328 insn = PADDI_R12_PC;
10329 insn |= D34 (off);
10330 bfd_put_32 (abfd, insn >> 32, p);
10331 p += 4;
10332 bfd_put_32 (abfd, insn, p);
10333 }
10334 /* The minimum value for paddi is -0x200000000. The minimum value
10335 for li is -0x8000, which when shifted by 34 and added gives a
10336 minimum value of -0x2000200000000. The maximum value is
10337 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10338 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10339 {
10340 off -= 8 - odd;
10341 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10342 p += 4;
10343 if (!odd)
10344 {
10345 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10346 p += 4;
10347 }
10348 insn = PADDI_R12_PC | D34 (off);
10349 bfd_put_32 (abfd, insn >> 32, p);
10350 p += 4;
10351 bfd_put_32 (abfd, insn, p);
10352 p += 4;
10353 if (odd)
10354 {
10355 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10356 p += 4;
10357 }
10358 if (load)
10359 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10360 else
10361 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10362 }
10363 else
10364 {
10365 off -= odd + 8;
10366 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10367 p += 4;
10368 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10369 p += 4;
10370 if (odd)
10371 {
10372 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10373 p += 4;
10374 }
10375 insn = PADDI_R12_PC | D34 (off);
10376 bfd_put_32 (abfd, insn >> 32, p);
10377 p += 4;
10378 bfd_put_32 (abfd, insn, p);
10379 p += 4;
10380 if (!odd)
10381 {
10382 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10383 p += 4;
10384 }
10385 if (load)
10386 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10387 else
10388 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10389 }
10390 p += 4;
10391 return p;
10392}
10393
10394static unsigned int
10395size_powerxx_offset (bfd_vma off, int odd)
10396{
10397 if (off - odd + (1ULL << 33) < 1ULL << 34)
10398 return odd + 8;
10399 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10400 return 20;
10401 else
10402 return 24;
10403}
10404
10405static unsigned int
10406num_relocs_for_powerxx_offset (bfd_vma off, int odd)
10407{
10408 if (off - odd + (1ULL << 33) < 1ULL << 34)
10409 return 1;
10410 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10411 return 2;
10412 else
10413 return 3;
10414}
10415
10416static Elf_Internal_Rela *
10417emit_relocs_for_powerxx_offset (struct bfd_link_info *info,
10418 Elf_Internal_Rela *r, bfd_vma roff,
10419 bfd_vma targ, bfd_vma off, int odd)
10420{
10421 if (off - odd + (1ULL << 33) < 1ULL << 34)
10422 roff += odd;
10423 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10424 {
10425 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10426 r->r_offset = roff + d_offset;
10427 r->r_addend = targ + 8 - odd - d_offset;
10428 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10429 ++r;
10430 roff += 8 - odd;
10431 }
10432 else
10433 {
10434 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10435 r->r_offset = roff + d_offset;
10436 r->r_addend = targ + 8 + odd - d_offset;
10437 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10438 ++r;
10439 roff += 4;
10440 r->r_offset = roff + d_offset;
10441 r->r_addend = targ + 4 + odd - d_offset;
10442 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10443 ++r;
10444 roff += 4 + odd;
10445 }
10446 r->r_offset = roff;
10447 r->r_addend = targ;
10448 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10449 return r;
10450}
10451
df136d64
AM
10452/* Emit .eh_frame opcode to advance pc by DELTA. */
10453
10454static bfd_byte *
10455eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10456{
10457 delta /= 4;
10458 if (delta < 64)
10459 *eh++ = DW_CFA_advance_loc + delta;
10460 else if (delta < 256)
10461 {
10462 *eh++ = DW_CFA_advance_loc1;
10463 *eh++ = delta;
10464 }
10465 else if (delta < 65536)
10466 {
10467 *eh++ = DW_CFA_advance_loc2;
10468 bfd_put_16 (abfd, delta, eh);
10469 eh += 2;
10470 }
10471 else
10472 {
10473 *eh++ = DW_CFA_advance_loc4;
10474 bfd_put_32 (abfd, delta, eh);
10475 eh += 4;
10476 }
10477 return eh;
10478}
10479
10480/* Size of required .eh_frame opcode to advance pc by DELTA. */
10481
10482static unsigned int
10483eh_advance_size (unsigned int delta)
10484{
10485 if (delta < 64 * 4)
10486 /* DW_CFA_advance_loc+[1..63]. */
10487 return 1;
10488 if (delta < 256 * 4)
10489 /* DW_CFA_advance_loc1, byte. */
10490 return 2;
10491 if (delta < 65536 * 4)
10492 /* DW_CFA_advance_loc2, 2 bytes. */
10493 return 3;
10494 /* DW_CFA_advance_loc4, 4 bytes. */
10495 return 5;
10496}
10497
794e51c0
AM
10498/* With power7 weakly ordered memory model, it is possible for ld.so
10499 to update a plt entry in one thread and have another thread see a
10500 stale zero toc entry. To avoid this we need some sort of acquire
10501 barrier in the call stub. One solution is to make the load of the
10502 toc word seem to appear to depend on the load of the function entry
10503 word. Another solution is to test for r2 being zero, and branch to
10504 the appropriate glink entry if so.
10505
10506 . fake dep barrier compare
71a39c98
AM
10507 . ld 12,xxx(2) ld 12,xxx(2)
10508 . mtctr 12 mtctr 12
10509 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10510 . add 2,2,11 cmpldi 2,0
10511 . ld 2,xxx+8(2) bnectr+
10512 . bctr b <glink_entry>
10513
10514 The solution involving the compare turns out to be faster, so
10515 that's what we use unless the branch won't reach. */
10516
10517#define ALWAYS_USE_FAKE_DEP 0
10518#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10519
794e51c0
AM
10520static inline unsigned int
10521plt_stub_size (struct ppc_link_hash_table *htab,
10522 struct ppc_stub_hash_entry *stub_entry,
10523 bfd_vma off)
10524{
05d0e962 10525 unsigned size;
b9e5796b 10526
05d0e962
AM
10527 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10528 {
04bdff6a
AM
10529 if (htab->powerxx_stubs)
10530 {
10531 bfd_vma start = (stub_entry->stub_offset
10532 + stub_entry->group->stub_sec->output_offset
10533 + stub_entry->group->stub_sec->output_section->vma);
10534 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10535 start += 4;
10536 size = 8 + size_powerxx_offset (off, start & 4);
10537 }
10538 else
10539 size = 8 + size_offset (off - 8);
05d0e962
AM
10540 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10541 size += 4;
10542 return size;
10543 }
10544
10545 size = 12;
b9e5796b
AM
10546 if (ALWAYS_EMIT_R2SAVE
10547 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10548 size += 4;
10549 if (PPC_HA (off) != 0)
794e51c0 10550 size += 4;
b9e5796b
AM
10551 if (htab->opd_abi)
10552 {
10553 size += 4;
e7d1c40c 10554 if (htab->params->plt_static_chain)
b9e5796b 10555 size += 4;
bd4d2eaa
AM
10556 if (htab->params->plt_thread_safe
10557 && htab->elf.dynamic_sections_created
10558 && stub_entry->h != NULL
10559 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10560 size += 8;
e7d1c40c 10561 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10562 size += 4;
10563 }
794e51c0
AM
10564 if (stub_entry->h != NULL
10565 && (stub_entry->h == htab->tls_get_addr_fd
10566 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10567 && htab->params->tls_get_addr_opt)
f378ab09
AM
10568 {
10569 size += 7 * 4;
e81b4c93 10570 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 10571 size += 6 * 4;
f378ab09 10572 }
794e51c0
AM
10573 return size;
10574}
10575
2420fff6
AM
10576/* Depending on the sign of plt_stub_align:
10577 If positive, return the padding to align to a 2**plt_stub_align
10578 boundary.
10579 If negative, if this stub would cross fewer 2**plt_stub_align
10580 boundaries if we align, then return the padding needed to do so. */
10581
794e51c0
AM
10582static inline unsigned int
10583plt_stub_pad (struct ppc_link_hash_table *htab,
10584 struct ppc_stub_hash_entry *stub_entry,
10585 bfd_vma plt_off)
10586{
2420fff6 10587 int stub_align;
1aa42141 10588 unsigned stub_size;
6f20ed8a 10589 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10590
2420fff6
AM
10591 if (htab->params->plt_stub_align >= 0)
10592 {
10593 stub_align = 1 << htab->params->plt_stub_align;
10594 if ((stub_off & (stub_align - 1)) != 0)
10595 return stub_align - (stub_off & (stub_align - 1));
10596 return 0;
10597 }
10598
10599 stub_align = 1 << -htab->params->plt_stub_align;
1aa42141 10600 stub_size = plt_stub_size (htab, stub_entry, plt_off);
794e51c0 10601 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10602 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10603 return stub_align - (stub_off & (stub_align - 1));
10604 return 0;
10605}
10606
10607/* Build a .plt call stub. */
10608
10609static inline bfd_byte *
10610build_plt_stub (struct ppc_link_hash_table *htab,
10611 struct ppc_stub_hash_entry *stub_entry,
10612 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10613{
e7d1c40c 10614 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10615 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10616 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10617 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10618 && htab->elf.dynamic_sections_created
10619 && stub_entry->h != NULL
10620 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10621 bfd_boolean use_fake_dep = plt_thread_safe;
10622 bfd_vma cmp_branch_off = 0;
10623
10624 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10625 && plt_load_toc
794e51c0 10626 && plt_thread_safe
bd4d2eaa
AM
10627 && !((stub_entry->h == htab->tls_get_addr_fd
10628 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10629 && htab->params->tls_get_addr_opt))
794e51c0
AM
10630 {
10631 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10632 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10633 / PLT_ENTRY_SIZE (htab));
9e390558 10634 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10635 bfd_vma to, from;
10636
68d62958
AM
10637 if (pltindex > 32768)
10638 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10639 to = (glinkoff
10640 + htab->glink->output_offset
10641 + htab->glink->output_section->vma);
6f20ed8a 10642 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10643 + 4 * (ALWAYS_EMIT_R2SAVE
10644 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10645 + 4 * (PPC_HA (offset) != 0)
10646 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10647 != PPC_HA (offset))
10648 + 4 * (plt_static_chain != 0)
10649 + 20
6f20ed8a
AM
10650 + stub_entry->group->stub_sec->output_offset
10651 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10652 cmp_branch_off = to - from;
10653 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10654 }
10655
ac2df442
AM
10656 if (PPC_HA (offset) != 0)
10657 {
176a0d42
AM
10658 if (r != NULL)
10659 {
794e51c0
AM
10660 if (ALWAYS_EMIT_R2SAVE
10661 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10662 r[0].r_offset += 4;
176a0d42 10663 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10664 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10665 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10666 r[1].r_addend = r[0].r_addend;
b9e5796b 10667 if (plt_load_toc)
176a0d42 10668 {
b9e5796b 10669 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10670 {
b9e5796b
AM
10671 r[2].r_offset = r[1].r_offset + 4;
10672 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10673 r[2].r_addend = r[0].r_addend;
10674 }
10675 else
10676 {
10677 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10678 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10679 r[2].r_addend = r[0].r_addend + 8;
10680 if (plt_static_chain)
10681 {
10682 r[3].r_offset = r[2].r_offset + 4;
10683 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10684 r[3].r_addend = r[0].r_addend + 16;
10685 }
c7131b65 10686 }
176a0d42
AM
10687 }
10688 }
794e51c0
AM
10689 if (ALWAYS_EMIT_R2SAVE
10690 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10691 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10692 if (plt_load_toc)
10693 {
10694 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10695 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10696 }
10697 else
10698 {
10699 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10700 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10701 }
b9e5796b
AM
10702 if (plt_load_toc
10703 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10704 {
71a39c98 10705 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10706 offset = 0;
10707 }
71a39c98 10708 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10709 if (plt_load_toc)
794e51c0 10710 {
b9e5796b
AM
10711 if (use_fake_dep)
10712 {
10713 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10714 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10715 }
10716 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10717 if (plt_static_chain)
10718 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10719 }
ac2df442
AM
10720 }
10721 else
10722 {
176a0d42
AM
10723 if (r != NULL)
10724 {
794e51c0
AM
10725 if (ALWAYS_EMIT_R2SAVE
10726 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10727 r[0].r_offset += 4;
176a0d42 10728 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10729 if (plt_load_toc)
176a0d42 10730 {
b9e5796b 10731 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10732 {
b9e5796b
AM
10733 r[1].r_offset = r[0].r_offset + 4;
10734 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10735 r[1].r_addend = r[0].r_addend;
10736 }
10737 else
10738 {
10739 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10740 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10741 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10742 if (plt_static_chain)
10743 {
10744 r[2].r_offset = r[1].r_offset + 4;
10745 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10746 r[2].r_addend = r[0].r_addend + 8;
10747 }
c7131b65 10748 }
176a0d42
AM
10749 }
10750 }
794e51c0
AM
10751 if (ALWAYS_EMIT_R2SAVE
10752 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10753 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10754 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10755 if (plt_load_toc
10756 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10757 {
10758 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10759 offset = 0;
10760 }
71a39c98 10761 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10762 if (plt_load_toc)
794e51c0 10763 {
b9e5796b
AM
10764 if (use_fake_dep)
10765 {
10766 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10767 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10768 }
10769 if (plt_static_chain)
10770 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10771 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10772 }
ac2df442 10773 }
b9e5796b 10774 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10775 {
10776 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10777 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10778 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10779 }
10780 else
407aa07c 10781 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10782 return p;
10783}
10784
a7f2871e
AM
10785/* Build a special .plt call stub for __tls_get_addr. */
10786
10787#define LD_R11_0R3 0xe9630000
10788#define LD_R12_0R3 0xe9830000
10789#define MR_R0_R3 0x7c601b78
10790#define CMPDI_R11_0 0x2c2b0000
10791#define ADD_R3_R12_R13 0x7c6c6a14
10792#define BEQLR 0x4d820020
10793#define MR_R3_R0 0x7c030378
a7f2871e
AM
10794#define STD_R11_0R1 0xf9610000
10795#define BCTRL 0x4e800421
10796#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10797#define MTLR_R11 0x7d6803a6
10798
10799static inline bfd_byte *
794e51c0
AM
10800build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10801 struct ppc_stub_hash_entry *stub_entry,
10802 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10803{
e7d1c40c 10804 bfd *obfd = htab->params->stub_bfd;
df136d64 10805 bfd_byte *loc = p;
794e51c0 10806
a7f2871e
AM
10807 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10808 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10809 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10810 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10811 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10812 bfd_put_32 (obfd, BEQLR, p), p += 4;
10813 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
10814 if (r != NULL)
10815 r[0].r_offset += 7 * 4;
e81b4c93 10816 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
f378ab09
AM
10817 return build_plt_stub (htab, stub_entry, p, offset, r);
10818
a7f2871e 10819 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10820 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10821
10822 if (r != NULL)
f378ab09 10823 r[0].r_offset += 2 * 4;
794e51c0 10824 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 10825 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 10826
a078d95a 10827 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10828 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10829 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10830 bfd_put_32 (obfd, BLR, p), p += 4;
10831
df136d64
AM
10832 if (htab->glink_eh_frame != NULL
10833 && htab->glink_eh_frame->size != 0)
10834 {
10835 bfd_byte *base, *eh;
10836 unsigned int lr_used, delta;
10837
10838 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10839 eh = base + stub_entry->group->eh_size;
10840 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10841 delta = lr_used - stub_entry->group->lr_restore;
10842 stub_entry->group->lr_restore = lr_used + 16;
10843 eh = eh_advance (htab->elf.dynobj, eh, delta);
10844 *eh++ = DW_CFA_offset_extended_sf;
10845 *eh++ = 65;
10846 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10847 *eh++ = DW_CFA_advance_loc + 4;
10848 *eh++ = DW_CFA_restore_extended;
10849 *eh++ = 65;
10850 stub_entry->group->eh_size = eh - base;
10851 }
a7f2871e
AM
10852 return p;
10853}
10854
176a0d42
AM
10855static Elf_Internal_Rela *
10856get_relocs (asection *sec, int count)
10857{
10858 Elf_Internal_Rela *relocs;
10859 struct bfd_elf_section_data *elfsec_data;
10860
10861 elfsec_data = elf_section_data (sec);
10862 relocs = elfsec_data->relocs;
10863 if (relocs == NULL)
10864 {
10865 bfd_size_type relsize;
10866 relsize = sec->reloc_count * sizeof (*relocs);
10867 relocs = bfd_alloc (sec->owner, relsize);
10868 if (relocs == NULL)
10869 return NULL;
10870 elfsec_data->relocs = relocs;
d4730f92
BS
10871 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10872 sizeof (Elf_Internal_Shdr));
10873 if (elfsec_data->rela.hdr == NULL)
10874 return NULL;
10875 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10876 * sizeof (Elf64_External_Rela));
10877 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10878 sec->reloc_count = 0;
10879 }
10880 relocs += sec->reloc_count;
10881 sec->reloc_count += count;
10882 return relocs;
10883}
10884
3d58e1fc
AM
10885/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10886 forms, to the equivalent relocs against the global symbol given by
10887 STUB_ENTRY->H. */
10888
10889static bfd_boolean
10890use_global_in_relocs (struct ppc_link_hash_table *htab,
10891 struct ppc_stub_hash_entry *stub_entry,
10892 Elf_Internal_Rela *r, unsigned int num_rel)
10893{
10894 struct elf_link_hash_entry **hashes;
10895 unsigned long symndx;
10896 struct ppc_link_hash_entry *h;
10897 bfd_vma symval;
10898
10899 /* Relocs are always against symbols in their own object file. Fake
10900 up global sym hashes for the stub bfd (which has no symbols). */
10901 hashes = elf_sym_hashes (htab->params->stub_bfd);
10902 if (hashes == NULL)
10903 {
10904 bfd_size_type hsize;
10905
10906 /* When called the first time, stub_globals will contain the
10907 total number of symbols seen during stub sizing. After
10908 allocating, stub_globals is used as an index to fill the
10909 hashes array. */
10910 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10911 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10912 if (hashes == NULL)
10913 return FALSE;
10914 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10915 htab->stub_globals = 1;
10916 }
10917 symndx = htab->stub_globals++;
10918 h = stub_entry->h;
10919 hashes[symndx] = &h->elf;
10920 if (h->oh != NULL && h->oh->is_func)
10921 h = ppc_follow_link (h->oh);
10922 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10923 || h->elf.root.type == bfd_link_hash_defweak);
10924 symval = (h->elf.root.u.def.value
10925 + h->elf.root.u.def.section->output_offset
10926 + h->elf.root.u.def.section->output_section->vma);
10927 while (num_rel-- != 0)
10928 {
10929 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10930 if (h->elf.root.u.def.section != stub_entry->target_section)
10931 {
10932 /* H is an opd symbol. The addend must be zero, and the
10933 branch reloc is the only one we can convert. */
10934 r->r_addend = 0;
10935 break;
10936 }
10937 else
10938 r->r_addend -= symval;
10939 --r;
10940 }
10941 return TRUE;
10942}
10943
aa374f67 10944static bfd_vma
25f53a85 10945get_r2off (struct bfd_link_info *info,
aa374f67
AM
10946 struct ppc_stub_hash_entry *stub_entry)
10947{
25f53a85 10948 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10949 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10950
10951 if (r2off == 0)
10952 {
10953 /* Support linking -R objects. Get the toc pointer from the
10954 opd entry. */
10955 char buf[8];
b9e5796b
AM
10956 if (!htab->opd_abi)
10957 return r2off;
aa374f67
AM
10958 asection *opd = stub_entry->h->elf.root.u.def.section;
10959 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10960
10961 if (strcmp (opd->name, ".opd") != 0
10962 || opd->reloc_count != 0)
10963 {
2cdcc330
AM
10964 info->callbacks->einfo
10965 (_("%P: cannot find opd entry toc for `%pT'\n"),
10966 stub_entry->h->elf.root.root.string);
aa374f67 10967 bfd_set_error (bfd_error_bad_value);
a7c49797 10968 return (bfd_vma) -1;
aa374f67
AM
10969 }
10970 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10971 return (bfd_vma) -1;
aa374f67 10972 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10973 r2off -= elf_gp (info->output_bfd);
aa374f67 10974 }
6f20ed8a 10975 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10976 return r2off;
10977}
10978
b34976b6 10979static bfd_boolean
4ce794b7 10980ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10981{
721956f4
AM
10982 struct ppc_stub_hash_entry *stub_entry;
10983 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10984 struct bfd_link_info *info;
10985 struct ppc_link_hash_table *htab;
721956f4 10986 bfd_byte *loc;
3d58e1fc 10987 bfd_byte *p, *relp;
1aa42141 10988 bfd_vma targ, off;
176a0d42 10989 Elf_Internal_Rela *r;
e054468f 10990 asection *plt;
3d58e1fc 10991 int num_rel;
04bdff6a 10992 int odd;
5d1634d7 10993
721956f4
AM
10994 /* Massage our args to the form they really have. */
10995 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10996 info = in_arg;
5d1634d7 10997
5d1634d7 10998 htab = ppc_hash_table (info);
4dfe6ac6
NC
10999 if (htab == NULL)
11000 return FALSE;
5d1634d7 11001
1aa42141 11002 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 11003 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11004
4ce794b7 11005 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 11006 switch (stub_entry->stub_type)
5d1634d7 11007 {
721956f4 11008 case ppc_stub_long_branch:
ad8e1ba5 11009 case ppc_stub_long_branch_r2off:
721956f4 11010 /* Branches are relative. This is where we are going to. */
1aa42141 11011 targ = (stub_entry->target_value
6911b7dc
AM
11012 + stub_entry->target_section->output_offset
11013 + stub_entry->target_section->output_section->vma);
1aa42141 11014 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11015
721956f4 11016 /* And this is where we are coming from. */
1aa42141
AM
11017 off = (stub_entry->stub_offset
11018 + stub_entry->group->stub_sec->output_offset
11019 + stub_entry->group->stub_sec->output_section->vma);
11020 off = targ - off;
e86ce104 11021
9e390558 11022 p = loc;
ac2df442 11023 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 11024 {
25f53a85 11025 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11026
a7c49797 11027 if (r2off == (bfd_vma) -1)
aa374f67
AM
11028 {
11029 htab->stub_error = TRUE;
11030 return FALSE;
11031 }
9e390558
AM
11032 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11033 p += 4;
ac2df442
AM
11034 if (PPC_HA (r2off) != 0)
11035 {
e7d1c40c 11036 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11037 ADDIS_R2_R2 | PPC_HA (r2off), p);
11038 p += 4;
a7c49797
AM
11039 }
11040 if (PPC_LO (r2off) != 0)
11041 {
11042 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11043 ADDI_R2_R2 | PPC_LO (r2off), p);
11044 p += 4;
ac2df442 11045 }
9e390558 11046 off -= p - loc;
ad8e1ba5 11047 }
9e390558
AM
11048 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11049 p += 4;
ad8e1ba5 11050
5c3dead3
AM
11051 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11052 {
cf97bcb0
AM
11053 _bfd_error_handler
11054 (_("long branch stub `%s' offset overflow"),
bc30df16 11055 stub_entry->root.string);
5c3dead3
AM
11056 htab->stub_error = TRUE;
11057 return FALSE;
11058 }
ee75fd95
AM
11059
11060 if (info->emitrelocations)
11061 {
6f20ed8a 11062 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
11063 if (r == NULL)
11064 return FALSE;
9e390558 11065 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11066 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11067 r->r_addend = targ;
3d58e1fc
AM
11068 if (stub_entry->h != NULL
11069 && !use_global_in_relocs (htab, stub_entry, r, 1))
11070 return FALSE;
ee75fd95 11071 }
721956f4 11072 break;
e86ce104 11073
721956f4 11074 case ppc_stub_plt_branch:
ad8e1ba5 11075 case ppc_stub_plt_branch_r2off:
721956f4
AM
11076 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11077 stub_entry->root.string + 9,
b34976b6 11078 FALSE, FALSE);
721956f4
AM
11079 if (br_entry == NULL)
11080 {
cf97bcb0
AM
11081 _bfd_error_handler (_("can't find branch stub `%s'"),
11082 stub_entry->root.string);
b34976b6
AM
11083 htab->stub_error = TRUE;
11084 return FALSE;
721956f4
AM
11085 }
11086
1aa42141 11087 targ = (stub_entry->target_value
176a0d42
AM
11088 + stub_entry->target_section->output_offset
11089 + stub_entry->target_section->output_section->vma);
6911b7dc 11090 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 11091 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11092
1aa42141 11093 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11094 htab->brlt->contents + br_entry->offset);
721956f4 11095
f94498ff 11096 if (br_entry->iter == htab->stub_iteration)
721956f4 11097 {
f94498ff 11098 br_entry->iter = 0;
84f5d08e 11099
f94498ff 11100 if (htab->relbrlt != NULL)
84f5d08e 11101 {
f94498ff
AM
11102 /* Create a reloc for the branch lookup table entry. */
11103 Elf_Internal_Rela rela;
11104 bfd_byte *rl;
11105
11106 rela.r_offset = (br_entry->offset
11107 + htab->brlt->output_offset
11108 + htab->brlt->output_section->vma);
11109 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11110 rela.r_addend = targ;
f94498ff
AM
11111
11112 rl = htab->relbrlt->contents;
11113 rl += (htab->relbrlt->reloc_count++
11114 * sizeof (Elf64_External_Rela));
11115 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11116 }
11117 else if (info->emitrelocations)
11118 {
176a0d42
AM
11119 r = get_relocs (htab->brlt, 1);
11120 if (r == NULL)
11121 return FALSE;
11122 /* brlt, being SEC_LINKER_CREATED does not go through the
11123 normal reloc processing. Symbols and offsets are not
11124 translated from input file to output file form, so
11125 set up the offset per the output file. */
f94498ff
AM
11126 r->r_offset = (br_entry->offset
11127 + htab->brlt->output_offset
11128 + htab->brlt->output_section->vma);
11129 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11130 r->r_addend = targ;
84f5d08e 11131 }
84f5d08e 11132 }
721956f4 11133
1aa42141 11134 targ = (br_entry->offset
176a0d42
AM
11135 + htab->brlt->output_offset
11136 + htab->brlt->output_section->vma);
11137
1aa42141
AM
11138 off = (elf_gp (info->output_bfd)
11139 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11140 off = targ - off;
721956f4 11141
ad8e1ba5 11142 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11143 {
25f53a85 11144 info->callbacks->einfo
c1c8c1ef 11145 (_("%P: linkage table error against `%pT'\n"),
721956f4 11146 stub_entry->root.string);
5d1634d7 11147 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11148 htab->stub_error = TRUE;
11149 return FALSE;
5d1634d7 11150 }
41bd81ab 11151
176a0d42
AM
11152 if (info->emitrelocations)
11153 {
6f20ed8a 11154 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11155 if (r == NULL)
11156 return FALSE;
6f20ed8a 11157 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11158 if (bfd_big_endian (info->output_bfd))
11159 r[0].r_offset += 2;
00f412ee 11160 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11161 r[0].r_offset += 4;
11162 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11163 r[0].r_addend = targ;
176a0d42
AM
11164 if (PPC_HA (off) != 0)
11165 {
11166 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11167 r[1].r_offset = r[0].r_offset + 4;
11168 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11169 r[1].r_addend = r[0].r_addend;
11170 }
11171 }
11172
9e390558 11173 p = loc;
00f412ee 11174 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11175 {
176a0d42 11176 if (PPC_HA (off) != 0)
ac2df442 11177 {
e7d1c40c 11178 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11179 ADDIS_R12_R2 | PPC_HA (off), p);
11180 p += 4;
e7d1c40c 11181 bfd_put_32 (htab->params->stub_bfd,
9e390558 11182 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11183 }
11184 else
9e390558
AM
11185 bfd_put_32 (htab->params->stub_bfd,
11186 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11187 }
11188 else
11189 {
25f53a85 11190 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11191
a7c49797 11192 if (r2off == (bfd_vma) -1)
aa374f67
AM
11193 {
11194 htab->stub_error = TRUE;
11195 return FALSE;
11196 }
ad8e1ba5 11197
9e390558
AM
11198 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11199 p += 4;
176a0d42 11200 if (PPC_HA (off) != 0)
ac2df442 11201 {
e7d1c40c 11202 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11203 ADDIS_R12_R2 | PPC_HA (off), p);
11204 p += 4;
e7d1c40c 11205 bfd_put_32 (htab->params->stub_bfd,
9e390558 11206 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11207 }
11208 else
9e390558 11209 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11210
11211 if (PPC_HA (r2off) != 0)
11212 {
9e390558 11213 p += 4;
e7d1c40c 11214 bfd_put_32 (htab->params->stub_bfd,
9e390558 11215 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11216 }
11217 if (PPC_LO (r2off) != 0)
11218 {
9e390558 11219 p += 4;
e7d1c40c 11220 bfd_put_32 (htab->params->stub_bfd,
9e390558 11221 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11222 }
ad8e1ba5 11223 }
9e390558
AM
11224 p += 4;
11225 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11226 p += 4;
407aa07c
AM
11227 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11228 p += 4;
721956f4 11229 break;
5d1634d7 11230
05d0e962
AM
11231 case ppc_stub_long_branch_notoc:
11232 case ppc_stub_long_branch_both:
11233 case ppc_stub_plt_branch_notoc:
11234 case ppc_stub_plt_branch_both:
11235 case ppc_stub_plt_call_notoc:
11236 case ppc_stub_plt_call_both:
11237 p = loc;
f891966f 11238 off = (stub_entry->stub_offset
05d0e962
AM
11239 + stub_entry->group->stub_sec->output_offset
11240 + stub_entry->group->stub_sec->output_section->vma);
11241 if (stub_entry->stub_type == ppc_stub_long_branch_both
11242 || stub_entry->stub_type == ppc_stub_plt_branch_both
11243 || stub_entry->stub_type == ppc_stub_plt_call_both)
11244 {
11245 off += 4;
11246 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11247 p += 4;
11248 }
11249 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11250 {
11251 targ = stub_entry->plt_ent->plt.offset & ~1;
11252 if (targ >= (bfd_vma) -2)
11253 abort ();
11254
11255 plt = htab->elf.splt;
11256 if (!htab->elf.dynamic_sections_created
11257 || stub_entry->h == NULL
11258 || stub_entry->h->elf.dynindx == -1)
11259 {
11260 if (stub_entry->symtype == STT_GNU_IFUNC)
11261 plt = htab->elf.iplt;
11262 else
11263 plt = htab->pltlocal;
11264 }
11265 targ += plt->output_offset + plt->output_section->vma;
11266 }
11267 else
11268 targ = (stub_entry->target_value
11269 + stub_entry->target_section->output_offset
11270 + stub_entry->target_section->output_section->vma);
04bdff6a 11271 odd = off & 4;
05d0e962 11272 off = targ - off;
f891966f 11273
3d58e1fc
AM
11274 relp = p;
11275 num_rel = 0;
04bdff6a
AM
11276 if (htab->powerxx_stubs)
11277 {
11278 bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
11279 p = build_powerxx_offset (htab->params->stub_bfd, p, off, odd, load);
11280 }
11281 else
11282 {
11283 /* The notoc stubs calculate their target (either a PLT entry or
11284 the global entry point of a function) relative to the PC
11285 returned by the "bcl" two instructions past the start of the
11286 sequence emitted by build_offset. The offset is therefore 8
11287 less than calculated from the start of the sequence. */
11288 off -= 8;
11289 p = build_offset (htab->params->stub_bfd, p, off,
11290 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11291 }
11292
f891966f 11293 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
05d0e962 11294 {
f891966f 11295 bfd_vma from;
3d58e1fc 11296 num_rel = 1;
f891966f
AM
11297 from = (stub_entry->stub_offset
11298 + stub_entry->group->stub_sec->output_offset
11299 + stub_entry->group->stub_sec->output_section->vma
11300 + (p - loc));
05d0e962 11301 bfd_put_32 (htab->params->stub_bfd,
f891966f 11302 B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
11303 }
11304 else
11305 {
11306 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11307 p += 4;
11308 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11309 }
11310 p += 4;
df136d64 11311
3d58e1fc
AM
11312 if (info->emitrelocations)
11313 {
04bdff6a
AM
11314 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
11315 if (htab->powerxx_stubs)
11316 num_rel += num_relocs_for_powerxx_offset (off, odd);
11317 else
11318 {
11319 num_rel += num_relocs_for_offset (off);
11320 roff += 16;
11321 }
3d58e1fc
AM
11322 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11323 if (r == NULL)
11324 return FALSE;
04bdff6a
AM
11325 if (htab->powerxx_stubs)
11326 r = emit_relocs_for_powerxx_offset (info, r, roff, targ, off, odd);
11327 else
11328 r = emit_relocs_for_offset (info, r, roff, targ, off);
3d58e1fc
AM
11329 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11330 || stub_entry->stub_type == ppc_stub_long_branch_both)
11331 {
11332 ++r;
11333 roff = p - 4 - stub_entry->group->stub_sec->contents;
11334 r->r_offset = roff;
11335 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11336 r->r_addend = targ;
11337 if (stub_entry->h != NULL
11338 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11339 return FALSE;
11340 }
11341 }
11342
04bdff6a
AM
11343 if (!htab->powerxx_stubs
11344 && htab->glink_eh_frame != NULL
11345 && htab->glink_eh_frame->size != 0)
df136d64
AM
11346 {
11347 bfd_byte *base, *eh;
11348 unsigned int lr_used, delta;
11349
11350 base = (htab->glink_eh_frame->contents
11351 + stub_entry->group->eh_base + 17);
11352 eh = base + stub_entry->group->eh_size;
11353 lr_used = stub_entry->stub_offset + 8;
11354 if (stub_entry->stub_type == ppc_stub_long_branch_both
11355 || stub_entry->stub_type == ppc_stub_plt_branch_both
11356 || stub_entry->stub_type == ppc_stub_plt_call_both)
11357 lr_used += 4;
11358 delta = lr_used - stub_entry->group->lr_restore;
11359 stub_entry->group->lr_restore = lr_used + 8;
11360 eh = eh_advance (htab->elf.dynobj, eh, delta);
11361 *eh++ = DW_CFA_register;
11362 *eh++ = 65;
11363 *eh++ = 12;
11364 *eh++ = DW_CFA_advance_loc + 2;
11365 *eh++ = DW_CFA_restore_extended;
11366 *eh++ = 65;
11367 stub_entry->group->eh_size = eh - base;
11368 }
05d0e962
AM
11369 break;
11370
721956f4 11371 case ppc_stub_plt_call:
794e51c0 11372 case ppc_stub_plt_call_r2save:
e054468f 11373 if (stub_entry->h != NULL
b31867b6
AM
11374 && stub_entry->h->is_func_descriptor
11375 && stub_entry->h->oh != NULL)
c862ae31 11376 {
b31867b6
AM
11377 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11378
11379 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11380 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11381 if (fh->elf.root.type == bfd_link_hash_undefined
11382 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11383 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11384 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11385 }
11386
721956f4 11387 /* Now build the stub. */
1aa42141
AM
11388 targ = stub_entry->plt_ent->plt.offset & ~1;
11389 if (targ >= (bfd_vma) -2)
721956f4
AM
11390 abort ();
11391
33e44f2e 11392 plt = htab->elf.splt;
25f23106
AM
11393 if (!htab->elf.dynamic_sections_created
11394 || stub_entry->h == NULL
11395 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11396 {
11397 if (stub_entry->symtype == STT_GNU_IFUNC)
11398 plt = htab->elf.iplt;
11399 else
11400 plt = htab->pltlocal;
11401 }
1aa42141 11402 targ += plt->output_offset + plt->output_section->vma;
e054468f 11403
1aa42141
AM
11404 off = (elf_gp (info->output_bfd)
11405 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11406 off = targ - off;
721956f4 11407
ad8e1ba5 11408 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11409 {
25f53a85 11410 info->callbacks->einfo
695344c0 11411 /* xgettext:c-format */
c1c8c1ef 11412 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11413 stub_entry->h != NULL
11414 ? stub_entry->h->elf.root.root.string
11415 : "<local sym>");
721956f4 11416 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11417 htab->stub_error = TRUE;
11418 return FALSE;
721956f4
AM
11419 }
11420
176a0d42
AM
11421 r = NULL;
11422 if (info->emitrelocations)
11423 {
6f20ed8a 11424 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11425 ((PPC_HA (off) != 0)
11426 + (htab->opd_abi
e7d1c40c 11427 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11428 && PPC_HA (off + 16) == PPC_HA (off))
11429 : 1)));
176a0d42
AM
11430 if (r == NULL)
11431 return FALSE;
6f20ed8a 11432 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11433 if (bfd_big_endian (info->output_bfd))
11434 r[0].r_offset += 2;
1aa42141 11435 r[0].r_addend = targ;
176a0d42 11436 }
a7f2871e
AM
11437 if (stub_entry->h != NULL
11438 && (stub_entry->h == htab->tls_get_addr_fd
11439 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11440 && htab->params->tls_get_addr_opt)
794e51c0 11441 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11442 else
794e51c0 11443 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11444 break;
11445
a4b6fadd
AM
11446 case ppc_stub_save_res:
11447 return TRUE;
11448
721956f4
AM
11449 default:
11450 BFD_FAIL ();
b34976b6 11451 return FALSE;
721956f4
AM
11452 }
11453
1aa42141 11454 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 11455
e7d1c40c 11456 if (htab->params->emit_stub_syms)
97b639ba
AM
11457 {
11458 struct elf_link_hash_entry *h;
ee75fd95
AM
11459 size_t len1, len2;
11460 char *name;
11461 const char *const stub_str[] = { "long_branch",
05d0e962
AM
11462 "long_branch",
11463 "long_branch",
3f6ff479
AM
11464 "long_branch",
11465 "plt_branch",
ee75fd95 11466 "plt_branch",
05d0e962
AM
11467 "plt_branch",
11468 "plt_branch",
11469 "plt_call",
11470 "plt_call",
794e51c0 11471 "plt_call",
ee75fd95
AM
11472 "plt_call" };
11473
11474 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11475 len2 = strlen (stub_entry->root.string);
11476 name = bfd_malloc (len1 + len2 + 2);
11477 if (name == NULL)
11478 return FALSE;
11479 memcpy (name, stub_entry->root.string, 9);
11480 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11481 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11482 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11483 if (h == NULL)
11484 return FALSE;
11485 if (h->root.type == bfd_link_hash_new)
11486 {
11487 h->root.type = bfd_link_hash_defined;
6f20ed8a 11488 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11489 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11490 h->ref_regular = 1;
11491 h->def_regular = 1;
11492 h->ref_regular_nonweak = 1;
11493 h->forced_local = 1;
11494 h->non_elf = 0;
2ec55de3 11495 h->root.linker_def = 1;
97b639ba
AM
11496 }
11497 }
11498
b34976b6 11499 return TRUE;
721956f4
AM
11500}
11501
11502/* As above, but don't actually build the stub. Just bump offset so
11503 we know stub section sizes, and select plt_branch stubs where
11504 long_branch stubs won't do. */
11505
b34976b6 11506static bfd_boolean
4ce794b7 11507ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11508{
11509 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11510 struct bfd_link_info *info;
721956f4 11511 struct ppc_link_hash_table *htab;
f891966f
AM
11512 asection *plt;
11513 bfd_vma targ, off, r2off;
04bdff6a 11514 unsigned int size, extra, lr_used, delta, odd;
721956f4
AM
11515
11516 /* Massage our args to the form they really have. */
11517 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11518 info = in_arg;
11519
11520 htab = ppc_hash_table (info);
4dfe6ac6
NC
11521 if (htab == NULL)
11522 return FALSE;
721956f4 11523
1aa42141
AM
11524 /* Make a note of the offset within the stubs for this entry. */
11525 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11526
a4b6fadd
AM
11527 if (stub_entry->h != NULL
11528 && stub_entry->h->save_res
11529 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11530 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11531 {
11532 /* Don't make stubs to out-of-line register save/restore
11533 functions. Instead, emit copies of the functions. */
11534 stub_entry->group->needs_save_res = 1;
11535 stub_entry->stub_type = ppc_stub_save_res;
11536 return TRUE;
11537 }
11538
f891966f 11539 switch (stub_entry->stub_type)
721956f4 11540 {
f891966f
AM
11541 case ppc_stub_plt_branch:
11542 case ppc_stub_plt_branch_r2off:
11543 /* Reset the stub type from the plt branch variant in case we now
11544 can reach with a shorter stub. */
11545 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11546 /* Fall through. */
11547 case ppc_stub_long_branch:
11548 case ppc_stub_long_branch_r2off:
1aa42141
AM
11549 targ = (stub_entry->target_value
11550 + stub_entry->target_section->output_offset
11551 + stub_entry->target_section->output_section->vma);
f891966f 11552 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
1aa42141
AM
11553 off = (stub_entry->stub_offset
11554 + stub_entry->group->stub_sec->output_offset
11555 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11556
ad8e1ba5 11557 size = 4;
f891966f 11558 r2off = 0;
ad8e1ba5
AM
11559 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11560 {
25f53a85 11561 r2off = get_r2off (info, stub_entry);
a7c49797 11562 if (r2off == (bfd_vma) -1)
aa374f67
AM
11563 {
11564 htab->stub_error = TRUE;
11565 return FALSE;
11566 }
a7c49797 11567 size = 8;
ac2df442 11568 if (PPC_HA (r2off) != 0)
a7c49797
AM
11569 size += 4;
11570 if (PPC_LO (r2off) != 0)
11571 size += 4;
1aa42141 11572 off += size - 4;
ad8e1ba5 11573 }
1aa42141 11574 off = targ - off;
ad8e1ba5 11575
f891966f
AM
11576 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11577 Do the same for -R objects without function descriptors. */
11578 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11579 && r2off == 0
11580 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11581 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 11582 {
f891966f 11583 struct ppc_branch_hash_entry *br_entry;
df136d64 11584
f891966f
AM
11585 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11586 stub_entry->root.string + 9,
11587 TRUE, FALSE);
11588 if (br_entry == NULL)
721956f4 11589 {
f891966f
AM
11590 _bfd_error_handler (_("can't build branch stub `%s'"),
11591 stub_entry->root.string);
11592 htab->stub_error = TRUE;
11593 return FALSE;
721956f4
AM
11594 }
11595
f891966f 11596 if (br_entry->iter != htab->stub_iteration)
721956f4 11597 {
f891966f
AM
11598 br_entry->iter = htab->stub_iteration;
11599 br_entry->offset = htab->brlt->size;
11600 htab->brlt->size += 8;
63bc6f6c 11601
f891966f
AM
11602 if (htab->relbrlt != NULL)
11603 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11604 else if (info->emitrelocations)
84f5d08e 11605 {
f891966f
AM
11606 htab->brlt->reloc_count += 1;
11607 htab->brlt->flags |= SEC_RELOC;
05d0e962 11608 }
f891966f 11609 }
ac2df442 11610
f891966f
AM
11611 targ = (br_entry->offset
11612 + htab->brlt->output_offset
11613 + htab->brlt->output_section->vma);
11614 off = (elf_gp (info->output_bfd)
11615 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11616 off = targ - off;
176a0d42 11617
f891966f
AM
11618 if (info->emitrelocations)
11619 {
11620 stub_entry->group->stub_sec->reloc_count
11621 += 1 + (PPC_HA (off) != 0);
11622 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11623 }
05d0e962 11624
f891966f
AM
11625 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11626 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11627 {
11628 size = 12;
11629 if (PPC_HA (off) != 0)
11630 size = 16;
ac2df442 11631 }
f891966f 11632 else
ac2df442 11633 {
f891966f
AM
11634 size = 16;
11635 if (PPC_HA (off) != 0)
11636 size += 4;
11637
11638 if (PPC_HA (r2off) != 0)
11639 size += 4;
11640 if (PPC_LO (r2off) != 0)
11641 size += 4;
ac2df442 11642 }
721956f4 11643 }
f891966f
AM
11644 else if (info->emitrelocations)
11645 {
11646 stub_entry->group->stub_sec->reloc_count += 1;
11647 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11648 }
11649 break;
11650
11651 case ppc_stub_plt_branch_notoc:
11652 case ppc_stub_plt_branch_both:
11653 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11654 /* Fall through. */
11655 case ppc_stub_long_branch_notoc:
11656 case ppc_stub_long_branch_both:
11657 off = (stub_entry->stub_offset
11658 + stub_entry->group->stub_sec->output_offset
11659 + stub_entry->group->stub_sec->output_section->vma);
11660 size = 0;
11661 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11662 size = 4;
11663 off += size;
11664 targ = (stub_entry->target_value
11665 + stub_entry->target_section->output_offset
11666 + stub_entry->target_section->output_section->vma);
04bdff6a 11667 odd = off & 4;
f891966f
AM
11668 off = targ - off;
11669
3d58e1fc
AM
11670 if (info->emitrelocations)
11671 {
04bdff6a
AM
11672 unsigned int num_rel;
11673 if (htab->powerxx_stubs)
11674 num_rel = num_relocs_for_powerxx_offset (off, odd);
11675 else
11676 num_rel = num_relocs_for_offset (off - 8);
11677 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
11678 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11679 }
11680
04bdff6a
AM
11681 if (htab->powerxx_stubs)
11682 extra = size_powerxx_offset (off, odd);
11683 else
11684 extra = size_offset (off - 8);
f891966f
AM
11685 /* Include branch insn plus those in the offset sequence. */
11686 size += 4 + extra;
11687 /* The branch insn is at the end, or "extra" bytes along. So
11688 its offset will be "extra" bytes less that that already
11689 calculated. */
11690 off -= extra;
11691
04bdff6a
AM
11692 if (!htab->powerxx_stubs)
11693 {
11694 /* After the bcl, lr has been modified so we need to emit
11695 .eh_frame info saying the return address is in r12. */
11696 lr_used = stub_entry->stub_offset + 8;
11697 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11698 lr_used += 4;
11699 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11700 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11701 DW_CFA_restore_extended 65. */
11702 delta = lr_used - stub_entry->group->lr_restore;
11703 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11704 stub_entry->group->lr_restore = lr_used + 8;
11705 }
f891966f
AM
11706
11707 /* If the branch can't reach, use a plt_branch. */
11708 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11709 {
11710 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11711 - ppc_stub_long_branch_notoc);
11712 size += 4;
11713 }
3d58e1fc
AM
11714 else if (info->emitrelocations)
11715 stub_entry->group->stub_sec->reloc_count +=1;
f891966f
AM
11716 break;
11717
11718 case ppc_stub_plt_call_notoc:
11719 case ppc_stub_plt_call_both:
11720 off = (stub_entry->stub_offset
11721 + stub_entry->group->stub_sec->output_offset
11722 + stub_entry->group->stub_sec->output_section->vma);
11723 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11724 off += 4;
11725 targ = stub_entry->plt_ent->plt.offset & ~1;
11726 if (targ >= (bfd_vma) -2)
11727 abort ();
11728
11729 plt = htab->elf.splt;
11730 if (!htab->elf.dynamic_sections_created
11731 || stub_entry->h == NULL
11732 || stub_entry->h->elf.dynindx == -1)
11733 {
11734 if (stub_entry->symtype == STT_GNU_IFUNC)
11735 plt = htab->elf.iplt;
11736 else
11737 plt = htab->pltlocal;
11738 }
11739 targ += plt->output_offset + plt->output_section->vma;
04bdff6a 11740 odd = off & 4;
f891966f
AM
11741 off = targ - off;
11742
11743 if (htab->params->plt_stub_align != 0)
11744 {
11745 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11746
11747 stub_entry->group->stub_sec->size += pad;
11748 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11749 off -= pad;
11750 }
11751
3d58e1fc
AM
11752 if (info->emitrelocations)
11753 {
04bdff6a
AM
11754 unsigned int num_rel;
11755 if (htab->powerxx_stubs)
11756 num_rel = num_relocs_for_powerxx_offset (off, odd);
11757 else
11758 num_rel = num_relocs_for_offset (off - 8);
11759 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
11760 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11761 }
11762
f891966f
AM
11763 size = plt_stub_size (htab, stub_entry, off);
11764
04bdff6a
AM
11765 if (!htab->powerxx_stubs)
11766 {
11767 /* After the bcl, lr has been modified so we need to emit
11768 .eh_frame info saying the return address is in r12. */
11769 lr_used = stub_entry->stub_offset + 8;
11770 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11771 lr_used += 4;
11772 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11773 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11774 DW_CFA_restore_extended 65. */
11775 delta = lr_used - stub_entry->group->lr_restore;
11776 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11777 stub_entry->group->lr_restore = lr_used + 8;
11778 }
f891966f
AM
11779 break;
11780
11781 case ppc_stub_plt_call:
11782 case ppc_stub_plt_call_r2save:
11783 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11784 if (targ >= (bfd_vma) -2)
11785 abort ();
11786 plt = htab->elf.splt;
11787 if (!htab->elf.dynamic_sections_created
11788 || stub_entry->h == NULL
11789 || stub_entry->h->elf.dynindx == -1)
11790 {
11791 if (stub_entry->symtype == STT_GNU_IFUNC)
11792 plt = htab->elf.iplt;
11793 else
11794 plt = htab->pltlocal;
11795 }
11796 targ += plt->output_offset + plt->output_section->vma;
11797
11798 off = (elf_gp (info->output_bfd)
11799 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11800 off = targ - off;
11801
11802 if (htab->params->plt_stub_align != 0)
11803 {
11804 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11805
11806 stub_entry->group->stub_sec->size += pad;
11807 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11808 }
11809
11810 if (info->emitrelocations)
11811 {
11812 stub_entry->group->stub_sec->reloc_count
11813 += ((PPC_HA (off) != 0)
11814 + (htab->opd_abi
11815 ? 2 + (htab->params->plt_static_chain
11816 && PPC_HA (off + 16) == PPC_HA (off))
11817 : 1));
11818 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11819 }
11820
11821 size = plt_stub_size (htab, stub_entry, off);
11822
11823 if (stub_entry->h != NULL
11824 && (stub_entry->h == htab->tls_get_addr_fd
11825 || stub_entry->h == htab->tls_get_addr)
11826 && htab->params->tls_get_addr_opt
11827 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11828 {
11829 /* After the bctrl, lr has been modified so we need to
11830 emit .eh_frame info saying the return address is
11831 on the stack. In fact we put the EH info specifying
11832 that the return address is on the stack *at* the
11833 call rather than after it, because the EH info for a
11834 call needs to be specified by that point.
11835 See libgcc/unwind-dw2.c execute_cfa_program. */
11836 lr_used = stub_entry->stub_offset + size - 20;
11837 /* The eh_frame info will consist of a DW_CFA_advance_loc
11838 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11839 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11840 delta = lr_used - stub_entry->group->lr_restore;
11841 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11842 stub_entry->group->lr_restore = size - 4;
11843 }
11844 break;
11845
11846 default:
11847 BFD_FAIL ();
11848 return FALSE;
721956f4
AM
11849 }
11850
6f20ed8a 11851 stub_entry->group->stub_sec->size += size;
b34976b6 11852 return TRUE;
721956f4
AM
11853}
11854
11855/* Set up various things so that we can make a list of input sections
11856 for each output section included in the link. Returns -1 on error,
cedb70c5 11857 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11858
11859int
e7d1c40c 11860ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11861{
6f20ed8a 11862 unsigned int id;
721956f4
AM
11863 bfd_size_type amt;
11864 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11865
4dfe6ac6
NC
11866 if (htab == NULL)
11867 return -1;
4c52953f 11868
7cf7fcc8 11869 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
11870 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11871 htab->sec_info = bfd_zmalloc (amt);
11872 if (htab->sec_info == NULL)
721956f4
AM
11873 return -1;
11874
3d6f9012
AM
11875 /* Set toc_off for com, und, abs and ind sections. */
11876 for (id = 0; id < 3; id++)
6f20ed8a 11877 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11878
721956f4
AM
11879 return 1;
11880}
11881
927be08e
AM
11882/* Set up for first pass at multitoc partitioning. */
11883
11884void
11885ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11886{
11887 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11888
1c865ab2 11889 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11890 htab->toc_bfd = NULL;
11891 htab->toc_first_sec = NULL;
11892}
11893
e717da7e
AM
11894/* The linker repeatedly calls this function for each TOC input section
11895 and linker generated GOT section. Group input bfds such that the toc
927be08e 11896 within a group is less than 64k in size. */
ad8e1ba5 11897
927be08e 11898bfd_boolean
4ce794b7 11899ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11900{
11901 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11902 bfd_vma addr, off, limit;
ad8e1ba5 11903
4dfe6ac6
NC
11904 if (htab == NULL)
11905 return FALSE;
11906
927be08e 11907 if (!htab->second_toc_pass)
4c52953f 11908 {
927be08e 11909 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11910 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11911
11912 if (new_bfd)
bf102f86
AM
11913 {
11914 htab->toc_bfd = isec->owner;
11915 htab->toc_first_sec = isec;
11916 }
927be08e 11917
bf102f86
AM
11918 addr = isec->output_offset + isec->output_section->vma;
11919 off = addr - htab->toc_curr;
d77c8a4b
AM
11920 limit = 0x80008000;
11921 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11922 limit = 0x10000;
11923 if (off + isec->size > limit)
bf102f86
AM
11924 {
11925 addr = (htab->toc_first_sec->output_offset
11926 + htab->toc_first_sec->output_section->vma);
11927 htab->toc_curr = addr;
a27e685f 11928 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11929 }
99877b66 11930
927be08e
AM
11931 /* toc_curr is the base address of this toc group. Set elf_gp
11932 for the input section to be the offset relative to the
11933 output toc base plus 0x8000. Making the input elf_gp an
11934 offset allows us to move the toc as a whole without
11935 recalculating input elf_gp. */
06bcf541 11936 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
11937 off += TOC_BASE_OFF;
11938
11939 /* Die if someone uses a linker script that doesn't keep input
11940 file .toc and .got together. */
a4fd3de5
AM
11941 if (new_bfd
11942 && elf_gp (isec->owner) != 0
927be08e
AM
11943 && elf_gp (isec->owner) != off)
11944 return FALSE;
11945
11946 elf_gp (isec->owner) = off;
11947 return TRUE;
4c52953f 11948 }
927be08e
AM
11949
11950 /* During the second pass toc_first_sec points to the start of
11951 a toc group, and toc_curr is used to track the old elf_gp.
11952 We use toc_bfd to ensure we only look at each bfd once. */
11953 if (htab->toc_bfd == isec->owner)
11954 return TRUE;
11955 htab->toc_bfd = isec->owner;
11956
11957 if (htab->toc_first_sec == NULL
11958 || htab->toc_curr != elf_gp (isec->owner))
11959 {
11960 htab->toc_curr = elf_gp (isec->owner);
11961 htab->toc_first_sec = isec;
11962 }
11963 addr = (htab->toc_first_sec->output_offset
11964 + htab->toc_first_sec->output_section->vma);
06bcf541 11965 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
11966 elf_gp (isec->owner) = off;
11967
11968 return TRUE;
ad8e1ba5
AM
11969}
11970
927be08e
AM
11971/* Called via elf_link_hash_traverse to merge GOT entries for global
11972 symbol H. */
11973
11974static bfd_boolean
11975merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11976{
11977 if (h->root.type == bfd_link_hash_indirect)
11978 return TRUE;
11979
927be08e
AM
11980 merge_got_entries (&h->got.glist);
11981
11982 return TRUE;
11983}
11984
11985/* Called via elf_link_hash_traverse to allocate GOT entries for global
11986 symbol H. */
11987
11988static bfd_boolean
11989reallocate_got (struct elf_link_hash_entry *h, void *inf)
11990{
11991 struct got_entry *gent;
11992
11993 if (h->root.type == bfd_link_hash_indirect)
11994 return TRUE;
11995
927be08e
AM
11996 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11997 if (!gent->is_indirect)
11998 allocate_got (h, (struct bfd_link_info *) inf, gent);
11999 return TRUE;
12000}
12001
12002/* Called on the first multitoc pass after the last call to
12003 ppc64_elf_next_toc_section. This function removes duplicate GOT
12004 entries. */
12005
12006bfd_boolean
12007ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12008{
12009 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
12010 struct bfd *ibfd, *ibfd2;
12011 bfd_boolean done_something;
12012
12013 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12014
7865406b
AM
12015 if (!htab->do_multi_toc)
12016 return FALSE;
12017
d0fae19d 12018 /* Merge global sym got entries within a toc group. */
927be08e
AM
12019 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12020
12021 /* And tlsld_got. */
c72f2fb2 12022 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12023 {
12024 struct got_entry *ent, *ent2;
12025
12026 if (!is_ppc64_elf (ibfd))
12027 continue;
12028
12029 ent = ppc64_tlsld_got (ibfd);
12030 if (!ent->is_indirect
12031 && ent->got.offset != (bfd_vma) -1)
12032 {
c72f2fb2 12033 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12034 {
12035 if (!is_ppc64_elf (ibfd2))
12036 continue;
12037
12038 ent2 = ppc64_tlsld_got (ibfd2);
12039 if (!ent2->is_indirect
12040 && ent2->got.offset != (bfd_vma) -1
12041 && elf_gp (ibfd2) == elf_gp (ibfd))
12042 {
12043 ent2->is_indirect = TRUE;
12044 ent2->got.ent = ent;
12045 }
12046 }
12047 }
12048 }
12049
12050 /* Zap sizes of got sections. */
33e44f2e
AM
12051 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12052 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12053 htab->got_reli_size = 0;
12054
c72f2fb2 12055 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12056 {
12057 asection *got, *relgot;
12058
12059 if (!is_ppc64_elf (ibfd))
12060 continue;
12061
12062 got = ppc64_elf_tdata (ibfd)->got;
12063 if (got != NULL)
12064 {
12065 got->rawsize = got->size;
12066 got->size = 0;
12067 relgot = ppc64_elf_tdata (ibfd)->relgot;
12068 relgot->rawsize = relgot->size;
12069 relgot->size = 0;
12070 }
12071 }
12072
12073 /* Now reallocate the got, local syms first. We don't need to
12074 allocate section contents again since we never increase size. */
c72f2fb2 12075 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12076 {
12077 struct got_entry **lgot_ents;
12078 struct got_entry **end_lgot_ents;
12079 struct plt_entry **local_plt;
12080 struct plt_entry **end_local_plt;
f961d9dd 12081 unsigned char *lgot_masks;
927be08e
AM
12082 bfd_size_type locsymcount;
12083 Elf_Internal_Shdr *symtab_hdr;
19e08130 12084 asection *s;
927be08e
AM
12085
12086 if (!is_ppc64_elf (ibfd))
12087 continue;
12088
12089 lgot_ents = elf_local_got_ents (ibfd);
12090 if (!lgot_ents)
12091 continue;
12092
12093 symtab_hdr = &elf_symtab_hdr (ibfd);
12094 locsymcount = symtab_hdr->sh_info;
12095 end_lgot_ents = lgot_ents + locsymcount;
12096 local_plt = (struct plt_entry **) end_lgot_ents;
12097 end_local_plt = local_plt + locsymcount;
f961d9dd 12098 lgot_masks = (unsigned char *) end_local_plt;
927be08e 12099 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
12100 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12101 {
12102 struct got_entry *ent;
12103
12104 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12105 {
19e08130
AM
12106 unsigned int ent_size = 8;
12107 unsigned int rel_size = sizeof (Elf64_External_Rela);
12108
d0fae19d
AM
12109 ent->got.offset = s->size;
12110 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12111 {
19e08130
AM
12112 ent_size *= 2;
12113 rel_size *= 2;
12114 }
12115 s->size += ent_size;
37da22e5 12116 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12117 {
33e44f2e 12118 htab->elf.irelplt->size += rel_size;
19e08130
AM
12119 htab->got_reli_size += rel_size;
12120 }
6a3858a6
AM
12121 else if (bfd_link_pic (info)
12122 && !((ent->tls_type & TLS_TPREL) != 0
12123 && bfd_link_executable (info)))
19e08130
AM
12124 {
12125 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12126 srel->size += rel_size;
d0fae19d
AM
12127 }
12128 }
927be08e
AM
12129 }
12130 }
12131
12132 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12133
c72f2fb2 12134 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12135 {
12136 struct got_entry *ent;
12137
12138 if (!is_ppc64_elf (ibfd))
12139 continue;
12140
12141 ent = ppc64_tlsld_got (ibfd);
12142 if (!ent->is_indirect
12143 && ent->got.offset != (bfd_vma) -1)
12144 {
12145 asection *s = ppc64_elf_tdata (ibfd)->got;
12146 ent->got.offset = s->size;
12147 s->size += 16;
0e1862bb 12148 if (bfd_link_pic (info))
927be08e
AM
12149 {
12150 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12151 srel->size += sizeof (Elf64_External_Rela);
12152 }
12153 }
12154 }
12155
33e44f2e 12156 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12157 if (!done_something)
c72f2fb2 12158 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12159 {
12160 asection *got;
12161
12162 if (!is_ppc64_elf (ibfd))
12163 continue;
12164
12165 got = ppc64_elf_tdata (ibfd)->got;
12166 if (got != NULL)
12167 {
12168 done_something = got->rawsize != got->size;
12169 if (done_something)
12170 break;
12171 }
12172 }
12173
12174 if (done_something)
e7d1c40c 12175 (*htab->params->layout_sections_again) ();
927be08e
AM
12176
12177 /* Set up for second pass over toc sections to recalculate elf_gp
12178 on input sections. */
12179 htab->toc_bfd = NULL;
12180 htab->toc_first_sec = NULL;
12181 htab->second_toc_pass = TRUE;
12182 return done_something;
12183}
12184
12185/* Called after second pass of multitoc partitioning. */
12186
12187void
12188ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12189{
12190 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12191
12192 /* After the second pass, toc_curr tracks the TOC offset used
12193 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12194 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12195}
12196
9b5ecbd0
AM
12197/* No toc references were found in ISEC. If the code in ISEC makes no
12198 calls, then there's no need to use toc adjusting stubs when branching
12199 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12200 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12201 needed, and 2 if a cyclical call-graph was found but no other reason
12202 for a stub was detected. If called from the top level, a return of
12203 2 means the same as a return of 0. */
9b5ecbd0
AM
12204
12205static int
4ce794b7 12206toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12207{
9b5ecbd0 12208 int ret;
70cc837d
AM
12209
12210 /* Mark this section as checked. */
12211 isec->call_check_done = 1;
9b5ecbd0 12212
772119ce
AM
12213 /* We know none of our code bearing sections will need toc stubs. */
12214 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12215 return 0;
12216
eea6121a 12217 if (isec->size == 0)
082c50f8
AM
12218 return 0;
12219
4c52953f
AM
12220 if (isec->output_section == NULL)
12221 return 0;
12222
4c52953f 12223 ret = 0;
70cc837d 12224 if (isec->reloc_count != 0)
9b5ecbd0 12225 {
70cc837d
AM
12226 Elf_Internal_Rela *relstart, *rel;
12227 Elf_Internal_Sym *local_syms;
12228 struct ppc_link_hash_table *htab;
2917689a 12229
70cc837d
AM
12230 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12231 info->keep_memory);
12232 if (relstart == NULL)
12233 return -1;
90aecf7a 12234
70cc837d
AM
12235 /* Look for branches to outside of this section. */
12236 local_syms = NULL;
12237 htab = ppc_hash_table (info);
12238 if (htab == NULL)
12239 return -1;
4c52953f 12240
70cc837d 12241 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12242 {
70cc837d
AM
12243 enum elf_ppc64_reloc_type r_type;
12244 unsigned long r_symndx;
12245 struct elf_link_hash_entry *h;
12246 struct ppc_link_hash_entry *eh;
12247 Elf_Internal_Sym *sym;
12248 asection *sym_sec;
12249 struct _opd_sec_data *opd;
12250 bfd_vma sym_value;
12251 bfd_vma dest;
12252
12253 r_type = ELF64_R_TYPE (rel->r_info);
12254 if (r_type != R_PPC64_REL24
05d0e962 12255 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
12256 && r_type != R_PPC64_REL14
12257 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 12258 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
12259 && r_type != R_PPC64_PLTCALL
12260 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 12261 continue;
4c52953f 12262
70cc837d
AM
12263 r_symndx = ELF64_R_SYM (rel->r_info);
12264 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12265 isec->owner))
4c52953f 12266 {
70cc837d
AM
12267 ret = -1;
12268 break;
12269 }
4c52953f 12270
70cc837d
AM
12271 /* Calls to dynamic lib functions go through a plt call stub
12272 that uses r2. */
12273 eh = (struct ppc_link_hash_entry *) h;
12274 if (eh != NULL
12275 && (eh->elf.plt.plist != NULL
12276 || (eh->oh != NULL
12277 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12278 {
12279 ret = 1;
12280 break;
4c52953f
AM
12281 }
12282
70cc837d
AM
12283 if (sym_sec == NULL)
12284 /* Ignore other undefined symbols. */
4c52953f 12285 continue;
4c52953f 12286
70cc837d
AM
12287 /* Assume branches to other sections not included in the
12288 link need stubs too, to cover -R and absolute syms. */
12289 if (sym_sec->output_section == NULL)
12290 {
12291 ret = 1;
12292 break;
12293 }
4c52953f 12294
70cc837d
AM
12295 if (h == NULL)
12296 sym_value = sym->st_value;
12297 else
12298 {
12299 if (h->root.type != bfd_link_hash_defined
12300 && h->root.type != bfd_link_hash_defweak)
12301 abort ();
12302 sym_value = h->root.u.def.value;
12303 }
12304 sym_value += rel->r_addend;
4c52953f 12305
70cc837d
AM
12306 /* If this branch reloc uses an opd sym, find the code section. */
12307 opd = get_opd_info (sym_sec);
12308 if (opd != NULL)
12309 {
12310 if (h == NULL && opd->adjust != NULL)
12311 {
12312 long adjust;
4c52953f 12313
92a9c616 12314 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12315 if (adjust == -1)
12316 /* Assume deleted functions won't ever be called. */
12317 continue;
12318 sym_value += adjust;
12319 }
4c52953f 12320
aef36ac1
AM
12321 dest = opd_entry_value (sym_sec, sym_value,
12322 &sym_sec, NULL, FALSE);
70cc837d
AM
12323 if (dest == (bfd_vma) -1)
12324 continue;
12325 }
12326 else
12327 dest = (sym_value
12328 + sym_sec->output_offset
12329 + sym_sec->output_section->vma);
4c52953f 12330
70cc837d
AM
12331 /* Ignore branch to self. */
12332 if (sym_sec == isec)
12333 continue;
4c52953f 12334
70cc837d
AM
12335 /* If the called function uses the toc, we need a stub. */
12336 if (sym_sec->has_toc_reloc
12337 || sym_sec->makes_toc_func_call)
4c52953f 12338 {
70cc837d 12339 ret = 1;
4c52953f
AM
12340 break;
12341 }
70cc837d
AM
12342
12343 /* Assume any branch that needs a long branch stub might in fact
12344 need a plt_branch stub. A plt_branch stub uses r2. */
12345 else if (dest - (isec->output_offset
12346 + isec->output_section->vma
6911b7dc
AM
12347 + rel->r_offset) + (1 << 25)
12348 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12349 ? h->other
12350 : sym->st_other))
4c52953f 12351 {
70cc837d
AM
12352 ret = 1;
12353 break;
12354 }
12355
12356 /* If calling back to a section in the process of being
12357 tested, we can't say for sure that no toc adjusting stubs
12358 are needed, so don't return zero. */
12359 else if (sym_sec->call_check_in_progress)
12360 ret = 2;
12361
12362 /* Branches to another section that itself doesn't have any TOC
12363 references are OK. Recursively call ourselves to check. */
12364 else if (!sym_sec->call_check_done)
12365 {
12366 int recur;
12367
12368 /* Mark current section as indeterminate, so that other
12369 sections that call back to current won't be marked as
12370 known. */
12371 isec->call_check_in_progress = 1;
12372 recur = toc_adjusting_stub_needed (info, sym_sec);
12373 isec->call_check_in_progress = 0;
12374
4c52953f
AM
12375 if (recur != 0)
12376 {
70cc837d
AM
12377 ret = recur;
12378 if (recur != 2)
12379 break;
4c52953f
AM
12380 }
12381 }
4c52953f 12382 }
70cc837d
AM
12383
12384 if (local_syms != NULL
12385 && (elf_symtab_hdr (isec->owner).contents
12386 != (unsigned char *) local_syms))
12387 free (local_syms);
12388 if (elf_section_data (isec)->relocs != relstart)
12389 free (relstart);
9b5ecbd0
AM
12390 }
12391
70cc837d
AM
12392 if ((ret & 1) == 0
12393 && isec->map_head.s != NULL
12394 && (strcmp (isec->output_section->name, ".init") == 0
12395 || strcmp (isec->output_section->name, ".fini") == 0))
12396 {
12397 if (isec->map_head.s->has_toc_reloc
12398 || isec->map_head.s->makes_toc_func_call)
12399 ret = 1;
12400 else if (!isec->map_head.s->call_check_done)
12401 {
12402 int recur;
12403 isec->call_check_in_progress = 1;
12404 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12405 isec->call_check_in_progress = 0;
12406 if (recur != 0)
12407 ret = recur;
12408 }
12409 }
12410
12411 if (ret == 1)
12412 isec->makes_toc_func_call = 1;
4c52953f 12413
9b5ecbd0
AM
12414 return ret;
12415}
12416
721956f4
AM
12417/* The linker repeatedly calls this function for each input section,
12418 in the order that input sections are linked into output sections.
12419 Build lists of input sections to determine groupings between which
12420 we may insert linker stubs. */
12421
9b5ecbd0 12422bfd_boolean
4ce794b7 12423ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12424{
12425 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12426
4dfe6ac6
NC
12427 if (htab == NULL)
12428 return FALSE;
12429
734b6cf9 12430 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12431 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12432 {
3d6f9012
AM
12433 /* This happens to make the list in reverse order,
12434 which is what we want. */
6f20ed8a
AM
12435 htab->sec_info[isec->id].u.list
12436 = htab->sec_info[isec->output_section->id].u.list;
12437 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12438 }
ad8e1ba5 12439
4c52953f 12440 if (htab->multi_toc_needed)
9b5ecbd0 12441 {
8b974ba3
AM
12442 /* Analyse sections that aren't already flagged as needing a
12443 valid toc pointer. Exclude .fixup for the linux kernel.
12444 .fixup contains branches, but only back to the function that
12445 hit an exception. */
12446 if (!(isec->has_toc_reloc
12447 || (isec->flags & SEC_CODE) == 0
12448 || strcmp (isec->name, ".fixup") == 0
12449 || isec->call_check_done))
12450 {
12451 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12452 return FALSE;
8b974ba3
AM
12453 }
12454 /* Make all sections use the TOC assigned for this object file.
12455 This will be wrong for pasted sections; We fix that in
12456 check_pasted_section(). */
12457 if (elf_gp (isec->owner) != 0)
12458 htab->toc_curr = elf_gp (isec->owner);
12459 }
12460
6f20ed8a 12461 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12462 return TRUE;
721956f4
AM
12463}
12464
70cc837d
AM
12465/* Check that all .init and .fini sections use the same toc, if they
12466 have toc relocs. */
12467
12468static bfd_boolean
12469check_pasted_section (struct bfd_link_info *info, const char *name)
12470{
12471 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12472
12473 if (o != NULL)
12474 {
12475 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12476 bfd_vma toc_off = 0;
12477 asection *i;
12478
12479 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12480 if (i->has_toc_reloc)
12481 {
12482 if (toc_off == 0)
6f20ed8a
AM
12483 toc_off = htab->sec_info[i->id].toc_off;
12484 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12485 return FALSE;
12486 }
6683a28d
AM
12487
12488 if (toc_off == 0)
12489 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12490 if (i->makes_toc_func_call)
12491 {
6f20ed8a 12492 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12493 break;
12494 }
12495
70cc837d
AM
12496 /* Make sure the whole pasted function uses the same toc offset. */
12497 if (toc_off != 0)
12498 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12499 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12500 }
12501 return TRUE;
12502}
12503
12504bfd_boolean
12505ppc64_elf_check_init_fini (struct bfd_link_info *info)
12506{
12507 return (check_pasted_section (info, ".init")
12508 & check_pasted_section (info, ".fini"));
12509}
12510
721956f4
AM
12511/* See whether we can group stub sections together. Grouping stub
12512 sections may result in fewer stubs. More importantly, we need to
12513 put all .init* and .fini* stubs at the beginning of the .init or
12514 .fini output sections respectively, because glibc splits the
12515 _init and _fini functions into multiple parts. Putting a stub in
12516 the middle of a function is not a good idea. */
12517
6f20ed8a
AM
12518static bfd_boolean
12519group_sections (struct bfd_link_info *info,
4ce794b7
AM
12520 bfd_size_type stub_group_size,
12521 bfd_boolean stubs_always_before_branch)
721956f4 12522{
6f20ed8a
AM
12523 struct ppc_link_hash_table *htab;
12524 asection *osec;
7c8fe5c4
AM
12525 bfd_boolean suppress_size_errors;
12526
6f20ed8a
AM
12527 htab = ppc_hash_table (info);
12528 if (htab == NULL)
12529 return FALSE;
12530
7c8fe5c4 12531 suppress_size_errors = FALSE;
7c8fe5c4
AM
12532 if (stub_group_size == 1)
12533 {
12534 /* Default values. */
12535 if (stubs_always_before_branch)
09f92717 12536 stub_group_size = 0x1e00000;
7c8fe5c4 12537 else
09f92717 12538 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12539 suppress_size_errors = TRUE;
12540 }
12541
6f20ed8a 12542 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12543 {
6f20ed8a
AM
12544 asection *tail;
12545
12546 if (osec->id >= htab->sec_info_arr_size)
12547 continue;
12548
12549 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12550 while (tail != NULL)
721956f4 12551 {
734b6cf9
AM
12552 asection *curr;
12553 asection *prev;
12554 bfd_size_type total;
12555 bfd_boolean big_sec;
12556 bfd_vma curr_toc;
6f20ed8a 12557 struct map_stub *group;
09f92717 12558 bfd_size_type group_size;
734b6cf9
AM
12559
12560 curr = tail;
eea6121a 12561 total = tail->size;
09f92717
AM
12562 group_size = (ppc64_elf_section_data (tail) != NULL
12563 && ppc64_elf_section_data (tail)->has_14bit_branch
12564 ? stub_group_size >> 10 : stub_group_size);
12565
12566 big_sec = total > group_size;
7c8fe5c4 12567 if (big_sec && !suppress_size_errors)
695344c0 12568 /* xgettext:c-format */
871b3ab2 12569 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 12570 tail->owner, tail);
6f20ed8a 12571 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12572
6f20ed8a 12573 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12574 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12575 < (ppc64_elf_section_data (prev) != NULL
12576 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12577 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12578 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12579 curr = prev;
12580
12581 /* OK, the size from the start of CURR to the end is less
09f92717 12582 than group_size and thus can be handled by one stub
734b6cf9 12583 section. (or the tail section is itself larger than
09f92717
AM
12584 group_size, in which case we may be toast.) We should
12585 really be keeping track of the total size of stubs added
12586 here, as stubs contribute to the final output section
12587 size. That's a little tricky, and this way will only
12588 break if stubs added make the total size more than 2^25,
12589 ie. for the default stub_group_size, if stubs total more
12590 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12591 group = bfd_alloc (curr->owner, sizeof (*group));
12592 if (group == NULL)
12593 return FALSE;
12594 group->link_sec = curr;
12595 group->stub_sec = NULL;
a4b6fadd 12596 group->needs_save_res = 0;
df136d64
AM
12597 group->lr_restore = 0;
12598 group->eh_size = 0;
12599 group->eh_base = 0;
a4b6fadd
AM
12600 group->next = htab->group;
12601 htab->group = group;
734b6cf9 12602 do
721956f4 12603 {
6f20ed8a 12604 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12605 /* Set up this stub group. */
6f20ed8a 12606 htab->sec_info[tail->id].u.group = group;
721956f4 12607 }
734b6cf9
AM
12608 while (tail != curr && (tail = prev) != NULL);
12609
09f92717 12610 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12611 bytes before the stub section can be handled by it too.
12612 Don't do this if we have a really large section after the
12613 stubs, as adding more stubs increases the chance that
12614 branches may not reach into the stub section. */
12615 if (!stubs_always_before_branch && !big_sec)
12616 {
12617 total = 0;
12618 while (prev != NULL
12619 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12620 < (ppc64_elf_section_data (prev) != NULL
12621 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
12622 ? (group_size = stub_group_size >> 10)
12623 : group_size))
6f20ed8a 12624 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12625 {
12626 tail = prev;
6f20ed8a
AM
12627 prev = htab->sec_info[tail->id].u.list;
12628 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12629 }
12630 }
12631 tail = prev;
721956f4
AM
12632 }
12633 }
6f20ed8a 12634 return TRUE;
721956f4
AM
12635}
12636
58d180e8
AM
12637static const unsigned char glink_eh_frame_cie[] =
12638{
12639 0, 0, 0, 16, /* length. */
12640 0, 0, 0, 0, /* id. */
12641 1, /* CIE version. */
12642 'z', 'R', 0, /* Augmentation string. */
12643 4, /* Code alignment. */
12644 0x78, /* Data alignment. */
12645 65, /* RA reg. */
12646 1, /* Augmentation size. */
12647 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12648 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12649};
12650
d969d15f
AM
12651/* Stripping output sections is normally done before dynamic section
12652 symbols have been allocated. This function is called later, and
12653 handles cases like htab->brlt which is mapped to its own output
12654 section. */
12655
12656static void
12657maybe_strip_output (struct bfd_link_info *info, asection *isec)
12658{
12659 if (isec->size == 0
12660 && isec->output_section->size == 0
53d8967a 12661 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12662 && !bfd_section_removed_from_list (info->output_bfd,
12663 isec->output_section)
12664 && elf_section_data (isec->output_section)->dynindx == 0)
12665 {
12666 isec->output_section->flags |= SEC_EXCLUDE;
12667 bfd_section_list_remove (info->output_bfd, isec->output_section);
12668 info->output_bfd->section_count--;
12669 }
12670}
12671
721956f4
AM
12672/* Determine and set the size of the stub section for a final link.
12673
12674 The basic idea here is to examine all the relocations looking for
12675 PC-relative calls to a target that is unreachable with a "bl"
12676 instruction. */
12677
b34976b6 12678bfd_boolean
e7d1c40c 12679ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12680{
12681 bfd_size_type stub_group_size;
b34976b6 12682 bfd_boolean stubs_always_before_branch;
721956f4
AM
12683 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12684
4dfe6ac6
NC
12685 if (htab == NULL)
12686 return FALSE;
12687
0e1862bb 12688 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12689 htab->params->plt_thread_safe = 1;
b9e5796b 12690 if (!htab->opd_abi)
e7d1c40c
AM
12691 htab->params->plt_thread_safe = 0;
12692 else if (htab->params->plt_thread_safe == -1)
794e51c0 12693 {
e2458743 12694 static const char *const thread_starter[] =
794e51c0
AM
12695 {
12696 "pthread_create",
12697 /* libstdc++ */
12698 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12699 /* librt */
12700 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12701 "mq_notify", "create_timer",
12702 /* libanl */
12703 "getaddrinfo_a",
12704 /* libgomp */
2300b5a1 12705 "GOMP_parallel",
794e51c0 12706 "GOMP_parallel_start",
2300b5a1 12707 "GOMP_parallel_loop_static",
794e51c0 12708 "GOMP_parallel_loop_static_start",
2300b5a1 12709 "GOMP_parallel_loop_dynamic",
794e51c0 12710 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12711 "GOMP_parallel_loop_guided",
794e51c0 12712 "GOMP_parallel_loop_guided_start",
2300b5a1 12713 "GOMP_parallel_loop_runtime",
794e51c0 12714 "GOMP_parallel_loop_runtime_start",
2300b5a1 12715 "GOMP_parallel_sections",
68ffbac6 12716 "GOMP_parallel_sections_start",
f9dffbf0
AM
12717 /* libgo */
12718 "__go_go",
794e51c0
AM
12719 };
12720 unsigned i;
12721
a4b6fadd 12722 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12723 {
12724 struct elf_link_hash_entry *h;
12725 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12726 FALSE, FALSE, TRUE);
e7d1c40c
AM
12727 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12728 if (htab->params->plt_thread_safe)
794e51c0
AM
12729 break;
12730 }
12731 }
e7d1c40c
AM
12732 stubs_always_before_branch = htab->params->group_size < 0;
12733 if (htab->params->group_size < 0)
12734 stub_group_size = -htab->params->group_size;
721956f4 12735 else
e7d1c40c 12736 stub_group_size = htab->params->group_size;
721956f4 12737
6f20ed8a
AM
12738 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12739 return FALSE;
721956f4 12740
c9301e31
AM
12741#define STUB_SHRINK_ITER 20
12742 /* Loop until no stubs added. After iteration 20 of this loop we may
12743 exit on a stub section shrinking. This is to break out of a
12744 pathological case where adding stubs on one iteration decreases
12745 section gaps (perhaps due to alignment), which then requires
12746 fewer or smaller stubs on the next iteration. */
12747
721956f4
AM
12748 while (1)
12749 {
12750 bfd *input_bfd;
12751 unsigned int bfd_indx;
a4b6fadd 12752 struct map_stub *group;
721956f4
AM
12753
12754 htab->stub_iteration += 1;
721956f4
AM
12755
12756 for (input_bfd = info->input_bfds, bfd_indx = 0;
12757 input_bfd != NULL;
c72f2fb2 12758 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12759 {
12760 Elf_Internal_Shdr *symtab_hdr;
12761 asection *section;
6cdc0ccc 12762 Elf_Internal_Sym *local_syms = NULL;
721956f4 12763
0c8d6e5c 12764 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12765 continue;
12766
721956f4 12767 /* We'll need the symbol table in a second. */
0ffa91dd 12768 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12769 if (symtab_hdr->sh_info == 0)
12770 continue;
12771
721956f4
AM
12772 /* Walk over each section attached to the input bfd. */
12773 for (section = input_bfd->sections;
12774 section != NULL;
12775 section = section->next)
12776 {
721956f4 12777 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12778
12779 /* If there aren't any relocs, then there's nothing more
12780 to do. */
12781 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12782 || (section->flags & SEC_ALLOC) == 0
12783 || (section->flags & SEC_LOAD) == 0
12784 || (section->flags & SEC_CODE) == 0
721956f4
AM
12785 || section->reloc_count == 0)
12786 continue;
12787
12788 /* If this section is a link-once section that will be
12789 discarded, then don't create any stubs. */
12790 if (section->output_section == NULL
927be08e 12791 || section->output_section->owner != info->output_bfd)
721956f4
AM
12792 continue;
12793
1e2f5b6e
AM
12794 /* Get the relocs. */
12795 internal_relocs
4ce794b7 12796 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12797 info->keep_memory);
721956f4 12798 if (internal_relocs == NULL)
1e2f5b6e 12799 goto error_ret_free_local;
721956f4
AM
12800
12801 /* Now examine each relocation. */
12802 irela = internal_relocs;
12803 irelaend = irela + section->reloc_count;
12804 for (; irela < irelaend; irela++)
12805 {
4ce794b7
AM
12806 enum elf_ppc64_reloc_type r_type;
12807 unsigned int r_indx;
721956f4
AM
12808 enum ppc_stub_type stub_type;
12809 struct ppc_stub_hash_entry *stub_entry;
8387904d 12810 asection *sym_sec, *code_sec;
e054468f 12811 bfd_vma sym_value, code_value;
721956f4 12812 bfd_vma destination;
6911b7dc 12813 unsigned long local_off;
8843416a 12814 bfd_boolean ok_dest;
721956f4 12815 struct ppc_link_hash_entry *hash;
8387904d 12816 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12817 struct elf_link_hash_entry *h;
12818 Elf_Internal_Sym *sym;
721956f4
AM
12819 char *stub_name;
12820 const asection *id_sec;
74f0fb50 12821 struct _opd_sec_data *opd;
e054468f 12822 struct plt_entry *plt_ent;
721956f4
AM
12823
12824 r_type = ELF64_R_TYPE (irela->r_info);
12825 r_indx = ELF64_R_SYM (irela->r_info);
12826
4ce794b7 12827 if (r_type >= R_PPC64_max)
721956f4
AM
12828 {
12829 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12830 goto error_ret_free_internal;
721956f4
AM
12831 }
12832
12833 /* Only look for stubs on branch instructions. */
4ce794b7 12834 if (r_type != R_PPC64_REL24
05d0e962 12835 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
12836 && r_type != R_PPC64_REL14
12837 && r_type != R_PPC64_REL14_BRTAKEN
12838 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12839 continue;
12840
12841 /* Now determine the call target, its name, value,
12842 section. */
411e1bfb
AM
12843 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12844 r_indx, input_bfd))
12845 goto error_ret_free_internal;
12846 hash = (struct ppc_link_hash_entry *) h;
12847
8843416a 12848 ok_dest = FALSE;
8387904d 12849 fdh = NULL;
7fe2b9a6 12850 sym_value = 0;
411e1bfb 12851 if (hash == NULL)
721956f4 12852 {
411e1bfb 12853 sym_value = sym->st_value;
c27b8c2a
AM
12854 if (sym_sec != NULL
12855 && sym_sec->output_section != NULL)
12856 ok_dest = TRUE;
721956f4 12857 }
7fe2b9a6
AM
12858 else if (hash->elf.root.type == bfd_link_hash_defined
12859 || hash->elf.root.type == bfd_link_hash_defweak)
12860 {
12861 sym_value = hash->elf.root.u.def.value;
12862 if (sym_sec->output_section != NULL)
12863 ok_dest = TRUE;
12864 }
12865 else if (hash->elf.root.type == bfd_link_hash_undefweak
12866 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12867 {
99877b66 12868 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12869 use the func descriptor sym instead if it is
12870 defined. */
ceb1f1ef 12871 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12872 && hash->oh != NULL)
8387904d 12873 {
8c5b4e52 12874 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12875 if (fdh->elf.root.type == bfd_link_hash_defined
12876 || fdh->elf.root.type == bfd_link_hash_defweak)
12877 {
12878 sym_sec = fdh->elf.root.u.def.section;
12879 sym_value = fdh->elf.root.u.def.value;
12880 if (sym_sec->output_section != NULL)
12881 ok_dest = TRUE;
12882 }
99877b66
AM
12883 else
12884 fdh = NULL;
8387904d 12885 }
7fe2b9a6
AM
12886 }
12887 else
12888 {
12889 bfd_set_error (bfd_error_bad_value);
12890 goto error_ret_free_internal;
721956f4
AM
12891 }
12892
8843416a 12893 destination = 0;
6911b7dc 12894 local_off = 0;
8843416a
AM
12895 if (ok_dest)
12896 {
12897 sym_value += irela->r_addend;
12898 destination = (sym_value
12899 + sym_sec->output_offset
12900 + sym_sec->output_section->vma);
6911b7dc
AM
12901 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12902 ? hash->elf.other
12903 : sym->st_other);
8843416a
AM
12904 }
12905
8387904d 12906 code_sec = sym_sec;
e054468f 12907 code_value = sym_value;
74f0fb50
AM
12908 opd = get_opd_info (sym_sec);
12909 if (opd != NULL)
8387904d
AM
12910 {
12911 bfd_vma dest;
12912
74f0fb50 12913 if (hash == NULL && opd->adjust != NULL)
8387904d 12914 {
51aecdc5 12915 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12916 if (adjust == -1)
12917 continue;
e054468f 12918 code_value += adjust;
8387904d
AM
12919 sym_value += adjust;
12920 }
12921 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12922 &code_sec, &code_value, FALSE);
8387904d
AM
12923 if (dest != (bfd_vma) -1)
12924 {
12925 destination = dest;
12926 if (fdh != NULL)
12927 {
12928 /* Fixup old ABI sym to point at code
12929 entry. */
99877b66 12930 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12931 hash->elf.root.u.def.section = code_sec;
e054468f 12932 hash->elf.root.u.def.value = code_value;
8387904d
AM
12933 }
12934 }
12935 }
12936
721956f4 12937 /* Determine what (if any) linker stub is needed. */
e054468f 12938 plt_ent = NULL;
721956f4 12939 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12940 &plt_ent, destination,
12941 local_off);
ad8e1ba5 12942
05d0e962
AM
12943 if (r_type == R_PPC64_REL24_NOTOC)
12944 {
12945 if (stub_type == ppc_stub_plt_call)
12946 stub_type = ppc_stub_plt_call_notoc;
12947 else if (stub_type == ppc_stub_long_branch
12948 || (code_sec != NULL
12949 && code_sec->output_section != NULL
12950 && (((hash ? hash->elf.other : sym->st_other)
12951 & STO_PPC64_LOCAL_MASK)
4a4e7361 12952 > 1 << STO_PPC64_LOCAL_BIT)))
05d0e962
AM
12953 stub_type = ppc_stub_long_branch_notoc;
12954 }
12955 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
12956 {
12957 /* Check whether we need a TOC adjusting stub.
12958 Since the linker pastes together pieces from
12959 different object files when creating the
12960 _init and _fini functions, it may be that a
12961 call to what looks like a local sym is in
12962 fact a call needing a TOC adjustment. */
33cb30a1
AM
12963 if ((code_sec != NULL
12964 && code_sec->output_section != NULL
12965 && (htab->sec_info[code_sec->id].toc_off
12966 != htab->sec_info[section->id].toc_off)
12967 && (code_sec->has_toc_reloc
12968 || code_sec->makes_toc_func_call))
12969 || (((hash ? hash->elf.other : sym->st_other)
12970 & STO_PPC64_LOCAL_MASK)
12971 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
12972 stub_type = ppc_stub_long_branch_r2off;
12973 }
12974
721956f4
AM
12975 if (stub_type == ppc_stub_none)
12976 continue;
12977
411e1bfb
AM
12978 /* __tls_get_addr calls might be eliminated. */
12979 if (stub_type != ppc_stub_plt_call
05d0e962 12980 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 12981 && hash != NULL
8387904d
AM
12982 && (hash == htab->tls_get_addr
12983 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12984 && section->has_tls_reloc
12985 && irela != internal_relocs)
12986 {
12987 /* Get tls info. */
f961d9dd 12988 unsigned char *tls_mask;
411e1bfb 12989
3a71aa26 12990 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12991 irela - 1, input_bfd))
12992 goto error_ret_free_internal;
37da22e5 12993 if ((*tls_mask & TLS_TLS) != 0)
411e1bfb
AM
12994 continue;
12995 }
12996
f378ab09 12997 if (stub_type == ppc_stub_plt_call)
794e51c0 12998 {
6e1816be
AM
12999 if (!htab->opd_abi
13000 && htab->params->plt_localentry0 != 0
13001 && is_elfv2_localentry0 (&hash->elf))
13002 htab->has_plt_localentry0 = 1;
13003 else if (irela + 1 < irelaend
13004 && irela[1].r_offset == irela->r_offset + 4
13005 && (ELF64_R_TYPE (irela[1].r_info)
13006 == R_PPC64_TOCSAVE))
f378ab09
AM
13007 {
13008 if (!tocsave_find (htab, INSERT,
13009 &local_syms, irela + 1, input_bfd))
13010 goto error_ret_free_internal;
13011 }
f378ab09
AM
13012 else
13013 stub_type = ppc_stub_plt_call_r2save;
794e51c0 13014 }
3b421ab3 13015
721956f4 13016 /* Support for grouping stub sections. */
6f20ed8a 13017 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
13018
13019 /* Get the name of this stub. */
13020 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13021 if (!stub_name)
13022 goto error_ret_free_internal;
13023
13024 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 13025 stub_name, FALSE, FALSE);
721956f4
AM
13026 if (stub_entry != NULL)
13027 {
05d0e962
AM
13028 enum ppc_stub_type old_type;
13029 /* A stub has already been created, but it may
13030 not be the required type. We shouldn't be
13031 transitioning from plt_call to long_branch
13032 stubs or vice versa, but we might be
13033 upgrading from plt_call to plt_call_r2save or
13034 from long_branch to long_branch_r2off. */
721956f4 13035 free (stub_name);
05d0e962
AM
13036 old_type = stub_entry->stub_type;
13037 switch (old_type)
13038 {
13039 default:
13040 abort ();
13041
13042 case ppc_stub_save_res:
13043 continue;
13044
13045 case ppc_stub_plt_call:
13046 case ppc_stub_plt_call_r2save:
13047 case ppc_stub_plt_call_notoc:
13048 case ppc_stub_plt_call_both:
13049 if (stub_type == ppc_stub_plt_call)
13050 continue;
13051 else if (stub_type == ppc_stub_plt_call_r2save)
13052 {
13053 if (old_type == ppc_stub_plt_call_notoc)
13054 stub_type = ppc_stub_plt_call_both;
13055 }
13056 else if (stub_type == ppc_stub_plt_call_notoc)
13057 {
13058 if (old_type == ppc_stub_plt_call_r2save)
13059 stub_type = ppc_stub_plt_call_both;
13060 }
13061 else
13062 abort ();
13063 break;
13064
13065 case ppc_stub_plt_branch:
13066 case ppc_stub_plt_branch_r2off:
13067 case ppc_stub_plt_branch_notoc:
13068 case ppc_stub_plt_branch_both:
13069 old_type += (ppc_stub_long_branch
13070 - ppc_stub_plt_branch);
13071 /* Fall through. */
13072 case ppc_stub_long_branch:
13073 case ppc_stub_long_branch_r2off:
13074 case ppc_stub_long_branch_notoc:
13075 case ppc_stub_long_branch_both:
13076 if (stub_type == ppc_stub_long_branch)
13077 continue;
13078 else if (stub_type == ppc_stub_long_branch_r2off)
13079 {
13080 if (old_type == ppc_stub_long_branch_notoc)
13081 stub_type = ppc_stub_long_branch_both;
13082 }
13083 else if (stub_type == ppc_stub_long_branch_notoc)
13084 {
13085 if (old_type == ppc_stub_long_branch_r2off)
13086 stub_type = ppc_stub_long_branch_both;
13087 }
13088 else
13089 abort ();
13090 break;
13091 }
13092 if (old_type < stub_type)
794e51c0 13093 stub_entry->stub_type = stub_type;
721956f4
AM
13094 continue;
13095 }
13096
25f53a85 13097 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
13098 if (stub_entry == NULL)
13099 {
13100 free (stub_name);
6cdc0ccc
AM
13101 error_ret_free_internal:
13102 if (elf_section_data (section)->relocs == NULL)
13103 free (internal_relocs);
13104 error_ret_free_local:
13105 if (local_syms != NULL
13106 && (symtab_hdr->contents
13107 != (unsigned char *) local_syms))
13108 free (local_syms);
b34976b6 13109 return FALSE;
721956f4
AM
13110 }
13111
ad8e1ba5 13112 stub_entry->stub_type = stub_type;
05d0e962
AM
13113 if (stub_type >= ppc_stub_plt_call
13114 && stub_type <= ppc_stub_plt_call_both)
e054468f 13115 {
05d0e962
AM
13116 stub_entry->target_value = sym_value;
13117 stub_entry->target_section = sym_sec;
e054468f
AM
13118 }
13119 else
13120 {
05d0e962
AM
13121 stub_entry->target_value = code_value;
13122 stub_entry->target_section = code_sec;
e054468f 13123 }
721956f4 13124 stub_entry->h = hash;
e054468f 13125 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
13126 stub_entry->symtype
13127 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 13128 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 13129
3d58e1fc
AM
13130 if (hash != NULL
13131 && (hash->elf.root.type == bfd_link_hash_defined
13132 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 13133 htab->stub_globals += 1;
721956f4
AM
13134 }
13135
13136 /* We're done with the internal relocs, free them. */
6cdc0ccc 13137 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 13138 free (internal_relocs);
721956f4 13139 }
6cdc0ccc
AM
13140
13141 if (local_syms != NULL
13142 && symtab_hdr->contents != (unsigned char *) local_syms)
13143 {
13144 if (!info->keep_memory)
13145 free (local_syms);
13146 else
13147 symtab_hdr->contents = (unsigned char *) local_syms;
13148 }
721956f4
AM
13149 }
13150
5c3dead3 13151 /* We may have added some stubs. Find out the new size of the
721956f4 13152 stub sections. */
d4aaa2a0 13153 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13154 {
13155 group->lr_restore = 0;
13156 group->eh_size = 0;
13157 if (group->stub_sec != NULL)
13158 {
13159 asection *stub_sec = group->stub_sec;
13160
13161 if (htab->stub_iteration <= STUB_SHRINK_ITER
13162 || stub_sec->rawsize < stub_sec->size)
13163 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13164 stub_sec->rawsize = stub_sec->size;
13165 stub_sec->size = 0;
13166 stub_sec->reloc_count = 0;
13167 stub_sec->flags &= ~SEC_RELOC;
13168 }
13169 }
eea6121a 13170
ba21f564
AM
13171 if (htab->stub_iteration <= STUB_SHRINK_ITER
13172 || htab->brlt->rawsize < htab->brlt->size)
13173 htab->brlt->rawsize = htab->brlt->size;
eea6121a 13174 htab->brlt->size = 0;
84f5d08e
AM
13175 htab->brlt->reloc_count = 0;
13176 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 13177 if (htab->relbrlt != NULL)
eea6121a 13178 htab->relbrlt->size = 0;
721956f4 13179
63bc6f6c 13180 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 13181
a4b6fadd
AM
13182 for (group = htab->group; group != NULL; group = group->next)
13183 if (group->needs_save_res)
13184 group->stub_sec->size += htab->sfpr->size;
13185
176a0d42
AM
13186 if (info->emitrelocations
13187 && htab->glink != NULL && htab->glink->size != 0)
13188 {
13189 htab->glink->reloc_count = 1;
13190 htab->glink->flags |= SEC_RELOC;
13191 }
13192
58d180e8
AM
13193 if (htab->glink_eh_frame != NULL
13194 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 13195 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 13196 {
2e0ce1c8 13197 size_t size = 0, align = 4;
58d180e8 13198
d4aaa2a0 13199 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13200 if (group->eh_size != 0)
13201 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 13202 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 13203 size += (24 + align - 1) & -align;
58d180e8 13204 if (size != 0)
2e0ce1c8
AM
13205 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13206 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13207 size = (size + align - 1) & -align;
58d180e8
AM
13208 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13209 htab->glink_eh_frame->size = size;
13210 }
13211
e7d1c40c 13212 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13213 for (group = htab->group; group != NULL; group = group->next)
13214 if (group->stub_sec != NULL)
691d2e9a
AM
13215 {
13216 int align = abs (htab->params->plt_stub_align);
13217 group->stub_sec->size
13218 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13219 }
d4aaa2a0
AM
13220
13221 for (group = htab->group; group != NULL; group = group->next)
13222 if (group->stub_sec != NULL
13223 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 13224 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 13225 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
13226 break;
13227
d4aaa2a0 13228 if (group == NULL
ba21f564
AM
13229 && (htab->brlt->rawsize == htab->brlt->size
13230 || (htab->stub_iteration > STUB_SHRINK_ITER
13231 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
13232 && (htab->glink_eh_frame == NULL
13233 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
13234 break;
13235
721956f4 13236 /* Ask the linker to do its stuff. */
e7d1c40c 13237 (*htab->params->layout_sections_again) ();
721956f4
AM
13238 }
13239
da44f4e5
AM
13240 if (htab->glink_eh_frame != NULL
13241 && htab->glink_eh_frame->size != 0)
13242 {
13243 bfd_vma val;
13244 bfd_byte *p, *last_fde;
13245 size_t last_fde_len, size, align, pad;
d4aaa2a0 13246 struct map_stub *group;
da44f4e5 13247
df136d64
AM
13248 /* It is necessary to at least have a rough outline of the
13249 linker generated CIEs and FDEs written before
13250 bfd_elf_discard_info is run, in order for these FDEs to be
13251 indexed in .eh_frame_hdr. */
da44f4e5
AM
13252 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13253 if (p == NULL)
13254 return FALSE;
13255 htab->glink_eh_frame->contents = p;
13256 last_fde = p;
2e0ce1c8 13257 align = 4;
da44f4e5
AM
13258
13259 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13260 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 13261 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 13262 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 13263 p += last_fde_len + 4;
da44f4e5 13264
d4aaa2a0 13265 for (group = htab->group; group != NULL; group = group->next)
df136d64 13266 if (group->eh_size != 0)
da44f4e5 13267 {
df136d64 13268 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 13269 last_fde = p;
df136d64 13270 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 13271 /* FDE length. */
2e0ce1c8 13272 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13273 p += 4;
13274 /* CIE pointer. */
13275 val = p - htab->glink_eh_frame->contents;
13276 bfd_put_32 (htab->elf.dynobj, val, p);
13277 p += 4;
13278 /* Offset to stub section, written later. */
13279 p += 4;
13280 /* stub section size. */
d4aaa2a0 13281 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
13282 p += 4;
13283 /* Augmentation. */
13284 p += 1;
df136d64
AM
13285 /* Make sure we don't have all nops. This is enough for
13286 elf-eh-frame.c to detect the last non-nop opcode. */
13287 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 13288 p = last_fde + last_fde_len + 4;
da44f4e5
AM
13289 }
13290 if (htab->glink != NULL && htab->glink->size != 0)
13291 {
13292 last_fde = p;
2e0ce1c8 13293 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13294 /* FDE length. */
2e0ce1c8 13295 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13296 p += 4;
13297 /* CIE pointer. */
13298 val = p - htab->glink_eh_frame->contents;
13299 bfd_put_32 (htab->elf.dynobj, val, p);
13300 p += 4;
13301 /* Offset to .glink, written later. */
13302 p += 4;
13303 /* .glink size. */
13304 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13305 p += 4;
13306 /* Augmentation. */
13307 p += 1;
13308
13309 *p++ = DW_CFA_advance_loc + 1;
13310 *p++ = DW_CFA_register;
13311 *p++ = 65;
9f08fa5c 13312 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 13313 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
13314 *p++ = DW_CFA_restore_extended;
13315 *p++ = 65;
2e0ce1c8 13316 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13317 }
13318 /* Subsume any padding into the last FDE if user .eh_frame
13319 sections are aligned more than glink_eh_frame. Otherwise any
13320 zero padding will be seen as a terminator. */
2e0ce1c8 13321 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13322 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13323 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13324 htab->glink_eh_frame->size = size + pad;
13325 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13326 }
13327
d969d15f
AM
13328 maybe_strip_output (info, htab->brlt);
13329 if (htab->glink_eh_frame != NULL)
13330 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13331
b34976b6 13332 return TRUE;
721956f4
AM
13333}
13334
13335/* Called after we have determined section placement. If sections
805fc799 13336 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13337
805fc799 13338bfd_vma
1c865ab2 13339ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13340{
805fc799 13341 asection *s;
a27e685f 13342 bfd_vma TOCstart, adjust;
721956f4 13343
43417696
AM
13344 if (info != NULL)
13345 {
13346 struct elf_link_hash_entry *h;
13347 struct elf_link_hash_table *htab = elf_hash_table (info);
13348
13349 if (is_elf_hash_table (htab)
13350 && htab->hgot != NULL)
13351 h = htab->hgot;
13352 else
13353 {
13354 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13355 if (is_elf_hash_table (htab))
13356 htab->hgot = h;
13357 }
13358 if (h != NULL
13359 && h->root.type == bfd_link_hash_defined
13360 && !h->root.linker_def
13361 && (!is_elf_hash_table (htab)
13362 || h->def_regular))
13363 {
13364 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13365 + h->root.u.def.section->output_offset
13366 + h->root.u.def.section->output_section->vma);
13367 _bfd_set_gp_value (obfd, TOCstart);
13368 return TOCstart;
13369 }
13370 }
13371
805fc799
AM
13372 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13373 order. The TOC starts where the first of these sections starts. */
13374 s = bfd_get_section_by_name (obfd, ".got");
e054468f 13375 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13376 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 13377 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13378 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 13379 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13380 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 13381 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
13382 {
13383 /* This may happen for
13384 o references to TOC base (SYM@toc / TOC[tc0]) without a
13385 .toc directive
13386 o bad linker script
13387 o --gc-sections and empty TOC sections
13388
13389 FIXME: Warn user? */
13390
13391 /* Look for a likely section. We probably won't even be
13392 using TOCstart. */
13393 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13394 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13395 | SEC_EXCLUDE))
805fc799
AM
13396 == (SEC_ALLOC | SEC_SMALL_DATA))
13397 break;
721956f4 13398 if (s == NULL)
805fc799 13399 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13400 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
13401 == (SEC_ALLOC | SEC_SMALL_DATA))
13402 break;
721956f4 13403 if (s == NULL)
805fc799 13404 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13405 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13406 == SEC_ALLOC)
805fc799 13407 break;
721956f4 13408 if (s == NULL)
805fc799 13409 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13410 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
13411 break;
13412 }
721956f4 13413
805fc799
AM
13414 TOCstart = 0;
13415 if (s != NULL)
13416 TOCstart = s->output_section->vma + s->output_offset;
721956f4 13417
a27e685f
AM
13418 /* Force alignment. */
13419 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13420 TOCstart -= adjust;
1c865ab2
AM
13421 _bfd_set_gp_value (obfd, TOCstart);
13422
810d4e75 13423 if (info != NULL && s != NULL)
1c865ab2
AM
13424 {
13425 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13426
810d4e75
AM
13427 if (htab != NULL)
13428 {
13429 if (htab->elf.hgot != NULL)
13430 {
a27e685f 13431 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
13432 htab->elf.hgot->root.u.def.section = s;
13433 }
13434 }
13435 else
1c865ab2 13436 {
810d4e75
AM
13437 struct bfd_link_hash_entry *bh = NULL;
13438 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
13439 s, TOC_BASE_OFF - adjust,
13440 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
13441 }
13442 }
805fc799 13443 return TOCstart;
721956f4
AM
13444}
13445
a345bc8d 13446/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 13447 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
13448
13449static bfd_boolean
49c09209 13450build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
13451{
13452 struct bfd_link_info *info;
13453 struct ppc_link_hash_table *htab;
49c09209 13454 struct plt_entry *ent;
a345bc8d
AM
13455 asection *s;
13456
13457 if (h->root.type == bfd_link_hash_indirect)
13458 return TRUE;
13459
49c09209
AM
13460 info = inf;
13461 htab = ppc_hash_table (info);
13462 if (htab == NULL)
13463 return FALSE;
13464
13465 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13466 if (ent->plt.offset != (bfd_vma) -1)
13467 {
13468 /* This symbol has an entry in the procedure linkage
13469 table. Set it up. */
13470 Elf_Internal_Rela rela;
2d7ad24e 13471 asection *plt, *relplt;
49c09209
AM
13472 bfd_byte *loc;
13473
13474 if (!htab->elf.dynamic_sections_created
13475 || h->dynindx == -1)
13476 {
13477 if (!(h->def_regular
13478 && (h->root.type == bfd_link_hash_defined
13479 || h->root.type == bfd_link_hash_defweak)))
13480 continue;
2d7ad24e
AM
13481 if (h->type == STT_GNU_IFUNC)
13482 {
13483 plt = htab->elf.iplt;
13484 relplt = htab->elf.irelplt;
13485 htab->local_ifunc_resolver = 1;
13486 if (htab->opd_abi)
13487 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13488 else
13489 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13490 }
49c09209 13491 else
2d7ad24e
AM
13492 {
13493 plt = htab->pltlocal;
13494 if (bfd_link_pic (info))
13495 {
13496 relplt = htab->relpltlocal;
13497 if (htab->opd_abi)
13498 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13499 else
13500 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13501 }
13502 else
13503 relplt = NULL;
13504 }
49c09209
AM
13505 rela.r_addend = (h->root.u.def.value
13506 + h->root.u.def.section->output_offset
13507 + h->root.u.def.section->output_section->vma
13508 + ent->addend);
2d7ad24e
AM
13509
13510 if (relplt == NULL)
13511 {
13512 loc = plt->contents + ent->plt.offset;
13513 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13514 if (htab->opd_abi)
13515 {
13516 bfd_vma toc = elf_gp (info->output_bfd);
13517 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13518 bfd_put_64 (info->output_bfd, toc, loc + 8);
13519 }
13520 }
13521 else
13522 {
13523 rela.r_offset = (plt->output_section->vma
13524 + plt->output_offset
13525 + ent->plt.offset);
13526 loc = relplt->contents + (relplt->reloc_count++
13527 * sizeof (Elf64_External_Rela));
13528 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13529 }
49c09209
AM
13530 }
13531 else
13532 {
13533 rela.r_offset = (htab->elf.splt->output_section->vma
13534 + htab->elf.splt->output_offset
13535 + ent->plt.offset);
13536 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13537 rela.r_addend = ent->addend;
13538 loc = (htab->elf.srelplt->contents
13539 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13540 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13541 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13542 htab->maybe_local_ifunc_resolver = 1;
2d7ad24e 13543 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 13544 }
49c09209
AM
13545 }
13546
a345bc8d
AM
13547 if (!h->pointer_equality_needed)
13548 return TRUE;
13549
13550 if (h->def_regular)
13551 return TRUE;
13552
9e390558 13553 s = htab->global_entry;
49c09209
AM
13554 if (s == NULL || s->size == 0)
13555 return TRUE;
13556
13557 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13558 if (ent->plt.offset != (bfd_vma) -1
13559 && ent->addend == 0)
a345bc8d
AM
13560 {
13561 bfd_byte *p;
13562 asection *plt;
13563 bfd_vma off;
13564
a345bc8d 13565 p = s->contents + h->root.u.def.value;
33e44f2e 13566 plt = htab->elf.splt;
a345bc8d
AM
13567 if (!htab->elf.dynamic_sections_created
13568 || h->dynindx == -1)
2d7ad24e
AM
13569 {
13570 if (h->type == STT_GNU_IFUNC)
13571 plt = htab->elf.iplt;
13572 else
13573 plt = htab->pltlocal;
13574 }
49c09209 13575 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
13576 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13577
13578 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13579 {
13580 info->callbacks->einfo
c1c8c1ef 13581 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
13582 h->root.root.string);
13583 bfd_set_error (bfd_error_bad_value);
13584 htab->stub_error = TRUE;
13585 }
13586
7341d5e2
AM
13587 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13588 if (htab->params->emit_stub_syms)
13589 {
13590 size_t len = strlen (h->root.root.string);
13591 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13592
13593 if (name == NULL)
13594 return FALSE;
13595
13596 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13597 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13598 if (h == NULL)
13599 return FALSE;
13600 if (h->root.type == bfd_link_hash_new)
13601 {
13602 h->root.type = bfd_link_hash_defined;
13603 h->root.u.def.section = s;
13604 h->root.u.def.value = p - s->contents;
13605 h->ref_regular = 1;
13606 h->def_regular = 1;
13607 h->ref_regular_nonweak = 1;
13608 h->forced_local = 1;
13609 h->non_elf = 0;
2ec55de3 13610 h->root.linker_def = 1;
7341d5e2
AM
13611 }
13612 }
13613
a345bc8d
AM
13614 if (PPC_HA (off) != 0)
13615 {
13616 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13617 p += 4;
13618 }
13619 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13620 p += 4;
13621 bfd_put_32 (s->owner, MTCTR_R12, p);
13622 p += 4;
407aa07c 13623 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
13624 break;
13625 }
13626 return TRUE;
13627}
13628
49c09209
AM
13629/* Write PLT relocs for locals. */
13630
13631static bfd_boolean
13632write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13633{
13634 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13635 bfd *ibfd;
13636
13637 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13638 {
13639 struct got_entry **lgot_ents, **end_lgot_ents;
13640 struct plt_entry **local_plt, **lplt, **end_local_plt;
13641 Elf_Internal_Shdr *symtab_hdr;
13642 bfd_size_type locsymcount;
13643 Elf_Internal_Sym *local_syms = NULL;
13644 struct plt_entry *ent;
13645
13646 if (!is_ppc64_elf (ibfd))
13647 continue;
13648
13649 lgot_ents = elf_local_got_ents (ibfd);
13650 if (!lgot_ents)
13651 continue;
13652
13653 symtab_hdr = &elf_symtab_hdr (ibfd);
13654 locsymcount = symtab_hdr->sh_info;
13655 end_lgot_ents = lgot_ents + locsymcount;
13656 local_plt = (struct plt_entry **) end_lgot_ents;
13657 end_local_plt = local_plt + locsymcount;
13658 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13659 for (ent = *lplt; ent != NULL; ent = ent->next)
13660 if (ent->plt.offset != (bfd_vma) -1)
13661 {
13662 Elf_Internal_Sym *sym;
13663 asection *sym_sec;
13664 asection *plt, *relplt;
13665 bfd_byte *loc;
13666 bfd_vma val;
49c09209
AM
13667
13668 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13669 lplt - local_plt, ibfd))
13670 {
13671 if (local_syms != NULL
13672 && symtab_hdr->contents != (unsigned char *) local_syms)
13673 free (local_syms);
13674 return FALSE;
13675 }
13676
13677 val = sym->st_value + ent->addend;
ba85c15d
AM
13678 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13679 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
49c09209
AM
13680 if (sym_sec != NULL && sym_sec->output_section != NULL)
13681 val += sym_sec->output_offset + sym_sec->output_section->vma;
13682
2d7ad24e
AM
13683 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13684 {
13685 htab->local_ifunc_resolver = 1;
13686 plt = htab->elf.iplt;
13687 relplt = htab->elf.irelplt;
13688 }
13689 else
13690 {
13691 plt = htab->pltlocal;
13692 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13693 }
49c09209 13694
2d7ad24e
AM
13695 if (relplt == NULL)
13696 {
13697 loc = plt->contents + ent->plt.offset;
13698 bfd_put_64 (info->output_bfd, val, loc);
13699 if (htab->opd_abi)
13700 {
13701 bfd_vma toc = elf_gp (ibfd);
13702 bfd_put_64 (info->output_bfd, toc, loc + 8);
13703 }
13704 }
49c09209 13705 else
2d7ad24e
AM
13706 {
13707 Elf_Internal_Rela rela;
13708 rela.r_offset = (ent->plt.offset
13709 + plt->output_offset
13710 + plt->output_section->vma);
13711 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13712 {
13713 if (htab->opd_abi)
13714 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13715 else
13716 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13717 }
13718 else
13719 {
13720 if (htab->opd_abi)
13721 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13722 else
13723 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13724 }
13725 rela.r_addend = val;
13726 loc = relplt->contents + (relplt->reloc_count++
13727 * sizeof (Elf64_External_Rela));
13728 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13729 }
49c09209
AM
13730 }
13731
13732 if (local_syms != NULL
13733 && symtab_hdr->contents != (unsigned char *) local_syms)
13734 {
13735 if (!info->keep_memory)
13736 free (local_syms);
13737 else
13738 symtab_hdr->contents = (unsigned char *) local_syms;
13739 }
13740 }
13741 return TRUE;
13742}
13743
721956f4
AM
13744/* Build all the stubs associated with the current output file.
13745 The stubs are kept in a hash table attached to the main linker
13746 hash table. This function is called via gldelf64ppc_finish. */
13747
b34976b6 13748bfd_boolean
e7d1c40c 13749ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13750 char **stats)
5d1634d7
AM
13751{
13752 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13753 struct map_stub *group;
721956f4 13754 asection *stub_sec;
5d1634d7 13755 bfd_byte *p;
e717da7e 13756 int stub_sec_count = 0;
5d1634d7 13757
4dfe6ac6
NC
13758 if (htab == NULL)
13759 return FALSE;
13760
eea6121a 13761 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 13762 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13763 {
13764 group->eh_size = 0;
13765 group->lr_restore = 0;
13766 if ((stub_sec = group->stub_sec) != NULL
13767 && stub_sec->size != 0)
13768 {
13769 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13770 stub_sec->size);
13771 if (stub_sec->contents == NULL)
13772 return FALSE;
13773 stub_sec->size = 0;
13774 }
13775 }
5d1634d7 13776
23eb7e01 13777 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13778 {
9f951329 13779 unsigned int indx;
ad8e1ba5 13780 bfd_vma plt0;
9f951329 13781
721956f4 13782 /* Build the .glink plt call stub. */
e7d1c40c 13783 if (htab->params->emit_stub_syms)
97b639ba
AM
13784 {
13785 struct elf_link_hash_entry *h;
468392fb
AM
13786 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13787 TRUE, FALSE, FALSE);
97b639ba
AM
13788 if (h == NULL)
13789 return FALSE;
13790 if (h->root.type == bfd_link_hash_new)
13791 {
13792 h->root.type = bfd_link_hash_defined;
13793 h->root.u.def.section = htab->glink;
ee4bf8d2 13794 h->root.u.def.value = 8;
f5385ebf
AM
13795 h->ref_regular = 1;
13796 h->def_regular = 1;
13797 h->ref_regular_nonweak = 1;
13798 h->forced_local = 1;
13799 h->non_elf = 0;
2ec55de3 13800 h->root.linker_def = 1;
97b639ba
AM
13801 }
13802 }
33e44f2e
AM
13803 plt0 = (htab->elf.splt->output_section->vma
13804 + htab->elf.splt->output_offset
13805 - 16);
176a0d42
AM
13806 if (info->emitrelocations)
13807 {
13808 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13809 if (r == NULL)
13810 return FALSE;
13811 r->r_offset = (htab->glink->output_offset
13812 + htab->glink->output_section->vma);
13813 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13814 r->r_addend = plt0;
13815 }
4ce794b7 13816 p = htab->glink->contents;
176a0d42 13817 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13818 bfd_put_64 (htab->glink->owner, plt0, p);
13819 p += 8;
b9e5796b
AM
13820 if (htab->opd_abi)
13821 {
13822 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13823 p += 4;
13824 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13825 p += 4;
13826 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13827 p += 4;
13828 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13829 p += 4;
13830 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13831 p += 4;
13832 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13833 p += 4;
13834 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13835 p += 4;
13836 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13837 p += 4;
13838 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13839 p += 4;
13840 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13841 p += 4;
13842 }
13843 else
13844 {
13845 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13846 p += 4;
13847 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13848 p += 4;
13849 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13850 p += 4;
f378ab09
AM
13851 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13852 p += 4;
b9e5796b
AM
13853 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13854 p += 4;
13855 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13856 p += 4;
13857 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13858 p += 4;
13859 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13860 p += 4;
13861 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13862 p += 4;
13863 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13864 p += 4;
13865 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13866 p += 4;
13867 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13868 p += 4;
13869 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13870 p += 4;
13871 }
407aa07c
AM
13872 bfd_put_32 (htab->glink->owner, BCTR, p);
13873 p += 4;
c75bc4f7 13874 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 13875
9f951329
AM
13876 /* Build the .glink lazy link call stubs. */
13877 indx = 0;
9e390558 13878 while (p < htab->glink->contents + htab->glink->size)
9f951329 13879 {
b9e5796b 13880 if (htab->opd_abi)
9f951329 13881 {
b9e5796b
AM
13882 if (indx < 0x8000)
13883 {
13884 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13885 p += 4;
13886 }
13887 else
13888 {
13889 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13890 p += 4;
13891 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13892 p);
13893 p += 4;
13894 }
9f951329 13895 }
4ce794b7 13896 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13897 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13898 indx++;
9f951329
AM
13899 p += 4;
13900 }
5d1634d7 13901 }
5d1634d7 13902
49c09209
AM
13903 /* Build .glink global entry stubs, and PLT relocs for globals. */
13904 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13905
13906 if (!write_plt_relocs_for_local_syms (info))
13907 return FALSE;
9e390558 13908
7341d5e2 13909 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13910 {
4ce794b7 13911 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13912 htab->brlt->size);
4ce794b7 13913 if (htab->brlt->contents == NULL)
b34976b6 13914 return FALSE;
721956f4 13915 }
ee75fd95 13916 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13917 {
13918 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13919 htab->relbrlt->size);
63bc6f6c
AM
13920 if (htab->relbrlt->contents == NULL)
13921 return FALSE;
13922 }
5d1634d7 13923
721956f4
AM
13924 /* Build the stubs as directed by the stub hash table. */
13925 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13926
a4b6fadd
AM
13927 for (group = htab->group; group != NULL; group = group->next)
13928 if (group->needs_save_res)
7dda8d3c 13929 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 13930
aa8a7074
AM
13931 if (htab->relbrlt != NULL)
13932 htab->relbrlt->reloc_count = 0;
13933
e7d1c40c 13934 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13935 for (group = htab->group; group != NULL; group = group->next)
13936 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
13937 {
13938 int align = abs (htab->params->plt_stub_align);
13939 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13940 }
794e51c0 13941
7dda8d3c
AM
13942 for (group = htab->group; group != NULL; group = group->next)
13943 if (group->needs_save_res)
13944 {
13945 stub_sec = group->stub_sec;
13946 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13947 htab->sfpr->contents, htab->sfpr->size);
13948 if (htab->params->emit_stub_syms)
13949 {
13950 unsigned int i;
13951
13952 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13953 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13954 return FALSE;
13955 }
13956 }
13957
df136d64
AM
13958 if (htab->glink_eh_frame != NULL
13959 && htab->glink_eh_frame->size != 0)
13960 {
13961 bfd_vma val;
13962 size_t align = 4;
13963
13964 p = htab->glink_eh_frame->contents;
13965 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13966
13967 for (group = htab->group; group != NULL; group = group->next)
13968 if (group->eh_size != 0)
13969 {
13970 /* Offset to stub section. */
13971 val = (group->stub_sec->output_section->vma
13972 + group->stub_sec->output_offset);
13973 val -= (htab->glink_eh_frame->output_section->vma
13974 + htab->glink_eh_frame->output_offset
13975 + (p + 8 - htab->glink_eh_frame->contents));
13976 if (val + 0x80000000 > 0xffffffff)
13977 {
13978 _bfd_error_handler
13979 (_("%s offset too large for .eh_frame sdata4 encoding"),
13980 group->stub_sec->name);
13981 return FALSE;
13982 }
13983 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13984 p += (group->eh_size + 17 + 3) & -4;
13985 }
13986 if (htab->glink != NULL && htab->glink->size != 0)
13987 {
13988 /* Offset to .glink. */
13989 val = (htab->glink->output_section->vma
13990 + htab->glink->output_offset
13991 + 8);
13992 val -= (htab->glink_eh_frame->output_section->vma
13993 + htab->glink_eh_frame->output_offset
13994 + (p + 8 - htab->glink_eh_frame->contents));
13995 if (val + 0x80000000 > 0xffffffff)
13996 {
13997 _bfd_error_handler
13998 (_("%s offset too large for .eh_frame sdata4 encoding"),
13999 htab->glink->name);
14000 return FALSE;
14001 }
14002 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14003 p += (24 + align - 1) & -align;
14004 }
14005 }
14006
d4aaa2a0
AM
14007 for (group = htab->group; group != NULL; group = group->next)
14008 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
14009 {
14010 stub_sec_count += 1;
c9301e31
AM
14011 if (stub_sec->rawsize != stub_sec->size
14012 && (htab->stub_iteration <= STUB_SHRINK_ITER
14013 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
14014 break;
14015 }
5d1634d7 14016
25516cc5 14017 if (group != NULL)
5d1634d7 14018 {
b34976b6 14019 htab->stub_error = TRUE;
cf97bcb0 14020 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 14021 }
721956f4 14022
d2a300cf
AM
14023 if (htab->stub_error)
14024 return FALSE;
14025
14026 if (stats != NULL)
14027 {
db84fff3 14028 size_t len;
d2a300cf
AM
14029 *stats = bfd_malloc (500);
14030 if (*stats == NULL)
14031 return FALSE;
14032
db84fff3
AM
14033 len = sprintf (*stats,
14034 ngettext ("linker stubs in %u group\n",
14035 "linker stubs in %u groups\n",
14036 stub_sec_count),
14037 stub_sec_count);
05d0e962
AM
14038 sprintf (*stats + len, _(" branch %lu\n"
14039 " branch toc adj %lu\n"
14040 " branch notoc %lu\n"
14041 " branch both %lu\n"
14042 " long branch %lu\n"
14043 " long toc adj %lu\n"
14044 " long notoc %lu\n"
14045 " long both %lu\n"
14046 " plt call %lu\n"
14047 " plt call save %lu\n"
14048 " plt call notoc %lu\n"
14049 " plt call both %lu\n"
14050 " global entry %lu"),
4ce794b7
AM
14051 htab->stub_count[ppc_stub_long_branch - 1],
14052 htab->stub_count[ppc_stub_long_branch_r2off - 1],
05d0e962
AM
14053 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14054 htab->stub_count[ppc_stub_long_branch_both - 1],
4ce794b7
AM
14055 htab->stub_count[ppc_stub_plt_branch - 1],
14056 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
05d0e962
AM
14057 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14058 htab->stub_count[ppc_stub_plt_branch_both - 1],
794e51c0 14059 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2 14060 htab->stub_count[ppc_stub_plt_call_r2save - 1],
05d0e962
AM
14061 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14062 htab->stub_count[ppc_stub_plt_call_both - 1],
7341d5e2 14063 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
14064 }
14065 return TRUE;
5bd4f169
AM
14066}
14067
60124e18
AM
14068/* What to do when ld finds relocations against symbols defined in
14069 discarded sections. */
14070
14071static unsigned int
14072ppc64_elf_action_discarded (asection *sec)
14073{
14074 if (strcmp (".opd", sec->name) == 0)
14075 return 0;
14076
14077 if (strcmp (".toc", sec->name) == 0)
14078 return 0;
14079
bce50a28
JJ
14080 if (strcmp (".toc1", sec->name) == 0)
14081 return 0;
14082
60124e18
AM
14083 return _bfd_elf_default_action_discarded (sec);
14084}
14085
5bd4f169
AM
14086/* The RELOCATE_SECTION function is called by the ELF backend linker
14087 to handle the relocations for a section.
14088
14089 The relocs are always passed as Rela structures; if the section
14090 actually uses Rel structures, the r_addend field will always be
14091 zero.
14092
14093 This function is responsible for adjust the section contents as
14094 necessary, and (if using Rela relocs and generating a
1049f94e 14095 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
14096 necessary.
14097
14098 This function does not have to worry about setting the reloc
14099 address or the reloc symbol index.
14100
14101 LOCAL_SYMS is a pointer to the swapped in local symbols.
14102
14103 LOCAL_SECTIONS is an array giving the section in the input file
14104 corresponding to the st_shndx field of each local symbol.
14105
14106 The global hash table entry for the global symbols can be found
14107 via elf_sym_hashes (input_bfd).
14108
1049f94e 14109 When generating relocatable output, this function must handle
5bd4f169
AM
14110 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14111 going to be the section symbol corresponding to the output
14112 section, which means that the addend must be adjusted
14113 accordingly. */
14114
b34976b6 14115static bfd_boolean
4ce794b7
AM
14116ppc64_elf_relocate_section (bfd *output_bfd,
14117 struct bfd_link_info *info,
14118 bfd *input_bfd,
14119 asection *input_section,
14120 bfd_byte *contents,
14121 Elf_Internal_Rela *relocs,
14122 Elf_Internal_Sym *local_syms,
14123 asection **local_sections)
5bd4f169 14124{
65f38f15 14125 struct ppc_link_hash_table *htab;
5bd4f169
AM
14126 Elf_Internal_Shdr *symtab_hdr;
14127 struct elf_link_hash_entry **sym_hashes;
5bd4f169 14128 Elf_Internal_Rela *rel;
c316a17c 14129 Elf_Internal_Rela *wrel;
5bd4f169 14130 Elf_Internal_Rela *relend;
411e1bfb
AM
14131 Elf_Internal_Rela outrel;
14132 bfd_byte *loc;
411e1bfb 14133 struct got_entry **local_got_ents;
5bd4f169 14134 bfd_vma TOCstart;
b34976b6
AM
14135 bfd_boolean ret = TRUE;
14136 bfd_boolean is_opd;
794e51c0
AM
14137 /* Assume 'at' branch hints. */
14138 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 14139 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 14140
65f38f15 14141 /* Initialize howto table if needed. */
5bd4f169 14142 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
14143 ppc_howto_init ();
14144
65f38f15 14145 htab = ppc_hash_table (info);
4dfe6ac6
NC
14146 if (htab == NULL)
14147 return FALSE;
ee75fd95
AM
14148
14149 /* Don't relocate stub sections. */
e7d1c40c 14150 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
14151 return TRUE;
14152
7af5d5c4
AM
14153 if (!is_ppc64_elf (input_bfd))
14154 {
14155 bfd_set_error (bfd_error_wrong_format);
14156 return FALSE;
14157 }
0ffa91dd 14158
411e1bfb 14159 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 14160 TOCstart = elf_gp (output_bfd);
0ffa91dd 14161 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 14162 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 14163 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 14164
c316a17c 14165 rel = wrel = relocs;
5bd4f169 14166 relend = relocs + input_section->reloc_count;
c316a17c 14167 for (; rel < relend; wrel++, rel++)
5bd4f169 14168 {
04c9666a 14169 enum elf_ppc64_reloc_type r_type;
31c76678 14170 bfd_vma addend;
5bd4f169
AM
14171 bfd_reloc_status_type r;
14172 Elf_Internal_Sym *sym;
14173 asection *sec;
039b3fef
AM
14174 struct elf_link_hash_entry *h_elf;
14175 struct ppc_link_hash_entry *h;
14176 struct ppc_link_hash_entry *fdh;
5bd4f169 14177 const char *sym_name;
0d4792f7 14178 unsigned long r_symndx, toc_symndx;
3a71aa26 14179 bfd_vma toc_addend;
f961d9dd
AM
14180 unsigned char tls_mask, tls_gd, tls_type;
14181 unsigned char sym_type;
5bd4f169 14182 bfd_vma relocation;
23cedd1d 14183 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 14184 bfd_boolean warned;
bc30df16 14185 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 14186 unsigned int insn;
e11840f9 14187 unsigned int mask;
721956f4
AM
14188 struct ppc_stub_hash_entry *stub_entry;
14189 bfd_vma max_br_offset;
14190 bfd_vma from;
c316a17c 14191 Elf_Internal_Rela orig_rel;
b80eed39
AM
14192 reloc_howto_type *howto;
14193 struct reloc_howto_struct alt_howto;
4a421c53
AM
14194 uint64_t pinsn;
14195 bfd_vma offset;
5bd4f169 14196
c316a17c
AM
14197 again:
14198 orig_rel = *rel;
14199
4ce794b7 14200 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 14201 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
14202
14203 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14204 symbol of the previous ADDR64 reloc. The symbol gives us the
14205 proper TOC base to use. */
14206 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
14207 && wrel != relocs
14208 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 14209 && is_opd)
c316a17c 14210 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 14211
4ce794b7
AM
14212 sym = NULL;
14213 sec = NULL;
039b3fef 14214 h_elf = NULL;
4ce794b7 14215 sym_name = NULL;
b34976b6
AM
14216 unresolved_reloc = FALSE;
14217 warned = FALSE;
65f38f15 14218
0b13192e 14219 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
14220 {
14221 /* It's a local symbol. */
74f0fb50 14222 struct _opd_sec_data *opd;
4025353c 14223
5bd4f169
AM
14224 sym = local_syms + r_symndx;
14225 sec = local_sections[r_symndx];
26c61ae5 14226 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 14227 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 14228 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
14229 opd = get_opd_info (sec);
14230 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 14231 {
51aecdc5
AM
14232 long adjust = opd->adjust[OPD_NDX (sym->st_value
14233 + rel->r_addend)];
4025353c
AM
14234 if (adjust == -1)
14235 relocation = 0;
14236 else
4cc603a5
AM
14237 {
14238 /* If this is a relocation against the opd section sym
14239 and we have edited .opd, adjust the reloc addend so
14240 that ld -r and ld --emit-relocs output is correct.
14241 If it is a reloc against some other .opd symbol,
14242 then the symbol value will be adjusted later. */
14243 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14244 rel->r_addend += adjust;
14245 else
14246 relocation += adjust;
14247 }
1e2f5b6e 14248 }
5bd4f169
AM
14249 }
14250 else
14251 {
62d887d4
L
14252 bfd_boolean ignored;
14253
b2a8e766
AM
14254 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14255 r_symndx, symtab_hdr, sym_hashes,
039b3fef 14256 h_elf, sec, relocation,
62d887d4 14257 unresolved_reloc, warned, ignored);
039b3fef
AM
14258 sym_name = h_elf->root.root.string;
14259 sym_type = h_elf->type;
b69fdb4e
AM
14260 if (sec != NULL
14261 && sec->owner == output_bfd
14262 && strcmp (sec->name, ".opd") == 0)
14263 {
14264 /* This is a symbol defined in a linker script. All
14265 such are defined in output sections, even those
14266 defined by simple assignment from a symbol defined in
14267 an input section. Transfer the symbol to an
14268 appropriate input .opd section, so that a branch to
14269 this symbol will be mapped to the location specified
14270 by the opd entry. */
14271 struct bfd_link_order *lo;
14272 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14273 if (lo->type == bfd_indirect_link_order)
14274 {
14275 asection *isec = lo->u.indirect.section;
14276 if (h_elf->root.u.def.value >= isec->output_offset
14277 && h_elf->root.u.def.value < (isec->output_offset
14278 + isec->size))
14279 {
14280 h_elf->root.u.def.value -= isec->output_offset;
14281 h_elf->root.u.def.section = isec;
14282 sec = isec;
14283 break;
14284 }
14285 }
14286 }
5bd4f169 14287 }
039b3fef 14288 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 14289
dbaa2011 14290 if (sec != NULL && discarded_section (sec))
c316a17c
AM
14291 {
14292 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14293 input_bfd, input_section,
0930cb30 14294 contents, rel->r_offset);
c316a17c
AM
14295 wrel->r_offset = rel->r_offset;
14296 wrel->r_info = 0;
14297 wrel->r_addend = 0;
14298
14299 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 14300 symbols defined in discarded sections. Not done for
c316a17c
AM
14301 non-debug to preserve relocs in .eh_frame which the
14302 eh_frame editing code expects to be present. */
14303 if (bfd_link_relocatable (info)
14304 && (input_section->flags & SEC_DEBUGGING))
14305 wrel--;
14306
14307 continue;
14308 }
ab96bf03 14309
0e1862bb 14310 if (bfd_link_relocatable (info))
c316a17c 14311 goto copy_reloc;
ab96bf03 14312
f40da81b
AM
14313 if (h != NULL && &h->elf == htab->elf.hgot)
14314 {
6f20ed8a 14315 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
14316 sec = bfd_abs_section_ptr;
14317 unresolved_reloc = FALSE;
14318 }
14319
951fd09b
AM
14320 /* TLS optimizations. Replace instruction sequences and relocs
14321 based on information we collected in tls_optimize. We edit
14322 RELOCS so that --emit-relocs will output something sensible
14323 for the final instruction stream. */
14324 tls_mask = 0;
14325 tls_gd = 0;
0d4792f7 14326 toc_symndx = 0;
727fc41e
AM
14327 if (h != NULL)
14328 tls_mask = h->tls_mask;
14329 else if (local_got_ents != NULL)
411e1bfb 14330 {
e054468f
AM
14331 struct plt_entry **local_plt = (struct plt_entry **)
14332 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 14333 unsigned char *lgot_masks = (unsigned char *)
e054468f 14334 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
14335 tls_mask = lgot_masks[r_symndx];
14336 }
37da22e5 14337 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
14338 && (r_type == R_PPC64_TLS
14339 || r_type == R_PPC64_TLSGD
14340 || r_type == R_PPC64_TLSLD))
14341 {
14342 /* Check for toc tls entries. */
f961d9dd 14343 unsigned char *toc_tls;
0d4792f7 14344
727fc41e
AM
14345 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14346 &local_syms, rel, input_bfd))
14347 return FALSE;
0d4792f7 14348
727fc41e
AM
14349 if (toc_tls)
14350 tls_mask = *toc_tls;
0d4792f7
AM
14351 }
14352
14353 /* Check that tls relocs are used with tls syms, and non-tls
14354 relocs are used with non-tls syms. */
cf35638d 14355 if (r_symndx != STN_UNDEF
0d4792f7
AM
14356 && r_type != R_PPC64_NONE
14357 && (h == NULL
039b3fef
AM
14358 || h->elf.root.type == bfd_link_hash_defined
14359 || h->elf.root.type == bfd_link_hash_defweak)
71c4e95a 14360 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
0d4792f7 14361 {
37da22e5 14362 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
14363 && (r_type == R_PPC64_TLS
14364 || r_type == R_PPC64_TLSGD
14365 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
14366 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14367 ;
14368 else
25f53a85 14369 info->callbacks->einfo
1d483afe 14370 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 14371 /* xgettext:c-format */
c1c8c1ef 14372 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 14373 /* xgettext:c-format */
c1c8c1ef 14374 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 14375 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
14376 ppc64_elf_howto_table[r_type]->name,
14377 sym_name);
411e1bfb
AM
14378 }
14379
14380 /* Ensure reloc mapping code below stays sane. */
14381 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14382 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14383 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14384 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14385 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14386 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14387 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14388 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14389 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14390 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14391 abort ();
0d4792f7 14392
411e1bfb
AM
14393 switch (r_type)
14394 {
14395 default:
411e1bfb
AM
14396 break;
14397
ba761f19 14398 case R_PPC64_LO_DS_OPT:
95f0d0d2 14399 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
14400 if ((insn & (0x3f << 26)) != 58u << 26)
14401 abort ();
14402 insn += (14u << 26) - (58u << 26);
95f0d0d2 14403 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
14404 r_type = R_PPC64_TOC16_LO;
14405 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14406 break;
14407
411e1bfb
AM
14408 case R_PPC64_TOC16:
14409 case R_PPC64_TOC16_LO:
14410 case R_PPC64_TOC16_DS:
14411 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
14412 {
14413 /* Check for toc tls entries. */
f961d9dd 14414 unsigned char *toc_tls;
951fd09b 14415 int retval;
411e1bfb 14416
3a71aa26
AM
14417 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14418 &local_syms, rel, input_bfd);
951fd09b 14419 if (retval == 0)
411e1bfb
AM
14420 return FALSE;
14421
14422 if (toc_tls)
14423 {
951fd09b 14424 tls_mask = *toc_tls;
411e1bfb
AM
14425 if (r_type == R_PPC64_TOC16_DS
14426 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 14427 {
37da22e5 14428 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
14429 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14430 goto toctprel;
14431 }
411e1bfb 14432 else
951fd09b
AM
14433 {
14434 /* If we found a GD reloc pair, then we might be
14435 doing a GD->IE transition. */
14436 if (retval == 2)
14437 {
b00a0a86 14438 tls_gd = TLS_GDIE;
37da22e5
AM
14439 if ((tls_mask & TLS_TLS) != 0
14440 && (tls_mask & TLS_GD) == 0)
102890f0 14441 goto tls_ldgd_opt;
951fd09b
AM
14442 }
14443 else if (retval == 3)
14444 {
37da22e5
AM
14445 if ((tls_mask & TLS_TLS) != 0
14446 && (tls_mask & TLS_LD) == 0)
102890f0 14447 goto tls_ldgd_opt;
951fd09b
AM
14448 }
14449 }
411e1bfb
AM
14450 }
14451 }
14452 break;
14453
9d6ded02
AM
14454 case R_PPC64_GOT_TPREL16_HI:
14455 case R_PPC64_GOT_TPREL16_HA:
37da22e5 14456 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
14457 && (tls_mask & TLS_TPREL) == 0)
14458 {
14459 rel->r_offset -= d_offset;
95f0d0d2 14460 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
14461 r_type = R_PPC64_NONE;
14462 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14463 }
14464 break;
14465
411e1bfb
AM
14466 case R_PPC64_GOT_TPREL16_DS:
14467 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 14468 if ((tls_mask & TLS_TLS) != 0
951fd09b 14469 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14470 {
81407a69 14471 toctprel:
95f0d0d2 14472 insn = bfd_get_32 (input_bfd,
c316a17c 14473 contents + rel->r_offset - d_offset);
411e1bfb
AM
14474 insn &= 31 << 21;
14475 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 14476 bfd_put_32 (input_bfd, insn,
c316a17c 14477 contents + rel->r_offset - d_offset);
411e1bfb 14478 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
14479 if (toc_symndx != 0)
14480 {
14481 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14482 rel->r_addend = toc_addend;
0d4792f7
AM
14483 /* We changed the symbol. Start over in order to
14484 get h, sym, sec etc. right. */
c316a17c 14485 goto again;
0d4792f7
AM
14486 }
14487 else
14488 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14489 }
14490 break;
14491
c213164a
AM
14492 case R_PPC64_GOT_TPREL34:
14493 if ((tls_mask & TLS_TLS) != 0
14494 && (tls_mask & TLS_TPREL) == 0)
14495 {
14496 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
14497 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14498 pinsn <<= 32;
14499 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14500 pinsn += ((2ULL << 56) + (-1ULL << 52)
14501 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
14502 bfd_put_32 (input_bfd, pinsn >> 32,
14503 contents + rel->r_offset);
14504 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14505 contents + rel->r_offset + 4);
14506 r_type = R_PPC64_TPREL34;
14507 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14508 }
14509 break;
14510
411e1bfb 14511 case R_PPC64_TLS:
37da22e5 14512 if ((tls_mask & TLS_TLS) != 0
951fd09b 14513 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14514 {
c213164a 14515 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2d0f3896
AM
14516 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14517 if (insn == 0)
c213164a
AM
14518 break;
14519 if ((rel->r_offset & 3) == 0)
0d4792f7 14520 {
c213164a
AM
14521 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14522 /* Was PPC64_TLS which sits on insn boundary, now
14523 PPC64_TPREL16_LO which is at low-order half-word. */
14524 rel->r_offset += d_offset;
14525 r_type = R_PPC64_TPREL16_LO;
14526 if (toc_symndx != 0)
14527 {
14528 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14529 rel->r_addend = toc_addend;
14530 /* We changed the symbol. Start over in order to
14531 get h, sym, sec etc. right. */
14532 goto again;
14533 }
14534 else
14535 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14536 }
14537 else if ((rel->r_offset & 3) == 1)
14538 {
14539 /* For pcrel IE to LE we already have the full
14540 offset and thus don't need an addi here. A nop
14541 or mr will do. */
14542 if ((insn & (0x3f << 26)) == 14 << 26)
14543 {
14544 /* Extract regs from addi rt,ra,si. */
14545 unsigned int rt = (insn >> 21) & 0x1f;
14546 unsigned int ra = (insn >> 16) & 0x1f;
14547 if (rt == ra)
14548 insn = NOP;
14549 else
14550 {
14551 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
14552 insn = (rt << 16) | (ra << 21) | (ra << 11);
14553 insn |= (31u << 26) | (444u << 1);
14554 }
14555 }
14556 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
0d4792f7 14557 }
411e1bfb
AM
14558 }
14559 break;
14560
411e1bfb
AM
14561 case R_PPC64_GOT_TLSGD16_HI:
14562 case R_PPC64_GOT_TLSGD16_HA:
b00a0a86 14563 tls_gd = TLS_GDIE;
37da22e5 14564 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
14565 goto tls_gdld_hi;
14566 break;
14567
411e1bfb
AM
14568 case R_PPC64_GOT_TLSLD16_HI:
14569 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 14570 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 14571 {
951fd09b
AM
14572 tls_gdld_hi:
14573 if ((tls_mask & tls_gd) != 0)
14574 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14575 + R_PPC64_GOT_TPREL16_DS);
14576 else
411e1bfb 14577 {
4fe5ca5b 14578 rel->r_offset -= d_offset;
95f0d0d2 14579 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 14580 r_type = R_PPC64_NONE;
411e1bfb 14581 }
951fd09b 14582 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14583 }
14584 break;
14585
951fd09b
AM
14586 case R_PPC64_GOT_TLSGD16:
14587 case R_PPC64_GOT_TLSGD16_LO:
b00a0a86 14588 tls_gd = TLS_GDIE;
37da22e5 14589 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 14590 goto tls_ldgd_opt;
951fd09b 14591 break;
411e1bfb 14592
951fd09b
AM
14593 case R_PPC64_GOT_TLSLD16:
14594 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 14595 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 14596 {
b9f04fe0 14597 unsigned int insn1, insn2;
102890f0
AM
14598
14599 tls_ldgd_opt:
727fc41e
AM
14600 offset = (bfd_vma) -1;
14601 /* If not using the newer R_PPC64_TLSGD/LD to mark
14602 __tls_get_addr calls, we must trust that the call
14603 stays with its arg setup insns, ie. that the next
14604 reloc is the __tls_get_addr call associated with
14605 the current reloc. Edit both insns. */
14606 if (input_section->has_tls_get_addr_call
14607 && rel + 1 < relend
14608 && branch_reloc_hash_match (input_bfd, rel + 1,
14609 htab->tls_get_addr,
14610 htab->tls_get_addr_fd))
14611 offset = rel[1].r_offset;
b86ac8e3
AM
14612 /* We read the low GOT_TLS (or TOC16) insn because we
14613 need to keep the destination reg. It may be
14614 something other than the usual r3, and moved to r3
14615 before the call by intervening code. */
95f0d0d2 14616 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 14617 contents + rel->r_offset - d_offset);
102890f0 14618 if ((tls_mask & tls_gd) != 0)
411e1bfb 14619 {
102890f0 14620 /* IE */
b86ac8e3 14621 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
14622 insn1 |= 58 << 26; /* ld */
14623 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 14624 if (offset != (bfd_vma) -1)
f58d5a2d 14625 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
46e9995a
AM
14626 if (r_type == R_PPC64_TOC16
14627 || r_type == R_PPC64_TOC16_LO)
102890f0 14628 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
46e9995a
AM
14629 else
14630 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
14631 + R_PPC64_GOT_TPREL16_DS);
102890f0
AM
14632 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14633 }
14634 else
14635 {
14636 /* LE */
b86ac8e3
AM
14637 insn1 &= 0x1f << 21;
14638 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
14639 insn2 = 0x38630000; /* addi 3,3,0 */
14640 if (tls_gd == 0)
951fd09b 14641 {
102890f0 14642 /* Was an LD reloc. */
71c4e95a 14643 r_symndx = STN_UNDEF;
102890f0 14644 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 14645 }
102890f0 14646 else if (toc_symndx != 0)
3a71aa26
AM
14647 {
14648 r_symndx = toc_symndx;
14649 rel->r_addend = toc_addend;
14650 }
102890f0
AM
14651 r_type = R_PPC64_TPREL16_HA;
14652 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
14653 if (offset != (bfd_vma) -1)
14654 {
14655 rel[1].r_info = ELF64_R_INFO (r_symndx,
14656 R_PPC64_TPREL16_LO);
14657 rel[1].r_offset = offset + d_offset;
14658 rel[1].r_addend = rel->r_addend;
14659 }
102890f0 14660 }
95f0d0d2 14661 bfd_put_32 (input_bfd, insn1,
3a71aa26 14662 contents + rel->r_offset - d_offset);
727fc41e 14663 if (offset != (bfd_vma) -1)
c96e0573
AM
14664 {
14665 bfd_put_32 (input_bfd, insn2, contents + offset);
14666 if (offset + 8 <= input_section->size)
14667 {
14668 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14669 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14670 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14671 }
14672 }
727fc41e
AM
14673 if ((tls_mask & tls_gd) == 0
14674 && (tls_gd == 0 || toc_symndx != 0))
14675 {
14676 /* We changed the symbol. Start over in order
14677 to get h, sym, sec etc. right. */
c316a17c 14678 goto again;
727fc41e
AM
14679 }
14680 }
14681 break;
14682
c213164a
AM
14683 case R_PPC64_GOT_TLSGD34:
14684 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14685 {
14686 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14687 pinsn <<= 32;
14688 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14689 if ((tls_mask & TLS_GDIE) != 0)
14690 {
14691 /* IE, pla -> pld */
14692 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
14693 r_type = R_PPC64_GOT_TPREL34;
14694 }
14695 else
14696 {
14697 /* LE, pla pcrel -> paddi r13 */
14698 pinsn += (-1ULL << 52) + (13ULL << 16);
14699 r_type = R_PPC64_TPREL34;
14700 }
14701 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14702 bfd_put_32 (input_bfd, pinsn >> 32,
14703 contents + rel->r_offset);
14704 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14705 contents + rel->r_offset + 4);
14706 }
14707 break;
14708
14709 case R_PPC64_GOT_TLSLD34:
14710 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14711 {
14712 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14713 pinsn <<= 32;
14714 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14715 pinsn += (-1ULL << 52) + (13ULL << 16);
14716 bfd_put_32 (input_bfd, pinsn >> 32,
14717 contents + rel->r_offset);
14718 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14719 contents + rel->r_offset + 4);
14720 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14721 r_symndx = STN_UNDEF;
14722 r_type = R_PPC64_TPREL34;
14723 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14724 goto again;
14725 }
14726 break;
14727
727fc41e 14728 case R_PPC64_TLSGD:
37da22e5 14729 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 14730 && rel + 1 < relend)
727fc41e 14731 {
b9f04fe0 14732 unsigned int insn2;
5663e321 14733 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 14734
4a421c53 14735 offset = rel->r_offset;
5663e321 14736 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
14737 {
14738 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
14739 if (r_type1 == R_PPC64_PLT_PCREL34
14740 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14741 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
14742 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14743 break;
14744 }
14745
14746 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14747 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14748
b00a0a86 14749 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
14750 {
14751 /* IE */
14752 r_type = R_PPC64_NONE;
14753 insn2 = 0x7c636a14; /* add 3,3,13 */
14754 }
14755 else
14756 {
14757 /* LE */
14758 if (toc_symndx != 0)
14759 {
14760 r_symndx = toc_symndx;
14761 rel->r_addend = toc_addend;
14762 }
c213164a
AM
14763 if (r_type1 == R_PPC64_REL24_NOTOC
14764 || r_type1 == R_PPC64_PLTCALL_NOTOC)
14765 {
14766 r_type = R_PPC64_NONE;
14767 insn2 = NOP;
14768 }
14769 else
14770 {
14771 rel->r_offset = offset + d_offset;
14772 r_type = R_PPC64_TPREL16_LO;
14773 insn2 = 0x38630000; /* addi 3,3,0 */
14774 }
727fc41e
AM
14775 }
14776 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14777 /* Zap the reloc on the _tls_get_addr call too. */
14778 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14779 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 14780 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
14781 if ((tls_mask & TLS_GDIE) == 0
14782 && toc_symndx != 0
14783 && r_type != R_PPC64_NONE)
c316a17c 14784 goto again;
411e1bfb 14785 }
411e1bfb
AM
14786 break;
14787
727fc41e 14788 case R_PPC64_TLSLD:
37da22e5 14789 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 14790 && rel + 1 < relend)
727fc41e 14791 {
b9f04fe0 14792 unsigned int insn2;
5663e321 14793 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 14794
4a421c53 14795 offset = rel->r_offset;
5663e321 14796 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
14797 {
14798 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
14799 if (r_type1 == R_PPC64_PLT_PCREL34
14800 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14801 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
14802 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14803 break;
14804 }
14805
14806 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14807 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14808
c213164a
AM
14809 if (r_type1 == R_PPC64_REL24_NOTOC
14810 || r_type1 == R_PPC64_PLTCALL_NOTOC)
14811 {
14812 r_type = R_PPC64_NONE;
14813 insn2 = NOP;
14814 }
14815 else
14816 {
14817 rel->r_offset = offset + d_offset;
14818 r_symndx = STN_UNDEF;
14819 r_type = R_PPC64_TPREL16_LO;
14820 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14821 insn2 = 0x38630000; /* addi 3,3,0 */
14822 }
727fc41e 14823 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
14824 /* Zap the reloc on the _tls_get_addr call too. */
14825 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14826 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 14827 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
14828 if (r_type != R_PPC64_NONE)
14829 goto again;
727fc41e
AM
14830 }
14831 break;
14832
411e1bfb 14833 case R_PPC64_DTPMOD64:
951fd09b
AM
14834 if (rel + 1 < relend
14835 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14836 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 14837 {
951fd09b
AM
14838 if ((tls_mask & TLS_GD) == 0)
14839 {
14840 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
b00a0a86 14841 if ((tls_mask & TLS_GDIE) != 0)
951fd09b
AM
14842 r_type = R_PPC64_TPREL64;
14843 else
14844 {
4ce794b7 14845 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
14846 r_type = R_PPC64_NONE;
14847 }
14848 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14849 }
14850 }
14851 else
14852 {
14853 if ((tls_mask & TLS_LD) == 0)
411e1bfb 14854 {
4ce794b7 14855 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 14856 r_type = R_PPC64_NONE;
951fd09b 14857 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 14858 }
411e1bfb
AM
14859 }
14860 break;
14861
14862 case R_PPC64_TPREL64:
951fd09b 14863 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
14864 {
14865 r_type = R_PPC64_NONE;
14866 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14867 }
14868 break;
52a82034 14869
006589cf
AM
14870 case R_PPC64_ENTRY:
14871 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14872 if (!bfd_link_pic (info)
14873 && !info->traditional_format
14874 && relocation + 0x80008000 <= 0xffffffff)
14875 {
14876 unsigned int insn1, insn2;
14877
14878 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14879 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14880 if ((insn1 & ~0xfffc) == LD_R2_0R12
14881 && insn2 == ADD_R2_R2_R12)
14882 {
95f0d0d2 14883 bfd_put_32 (input_bfd,
006589cf
AM
14884 LIS_R2 + PPC_HA (relocation),
14885 contents + rel->r_offset);
95f0d0d2 14886 bfd_put_32 (input_bfd,
006589cf
AM
14887 ADDI_R2_R2 + PPC_LO (relocation),
14888 contents + rel->r_offset + 4);
14889 }
14890 }
14891 else
14892 {
14893 relocation -= (rel->r_offset
14894 + input_section->output_offset
14895 + input_section->output_section->vma);
14896 if (relocation + 0x80008000 <= 0xffffffff)
14897 {
14898 unsigned int insn1, insn2;
14899
14900 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14901 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14902 if ((insn1 & ~0xfffc) == LD_R2_0R12
14903 && insn2 == ADD_R2_R2_R12)
14904 {
95f0d0d2 14905 bfd_put_32 (input_bfd,
006589cf
AM
14906 ADDIS_R2_R12 + PPC_HA (relocation),
14907 contents + rel->r_offset);
95f0d0d2 14908 bfd_put_32 (input_bfd,
006589cf
AM
14909 ADDI_R2_R2 + PPC_LO (relocation),
14910 contents + rel->r_offset + 4);
14911 }
14912 }
14913 }
14914 break;
14915
52a82034
AM
14916 case R_PPC64_REL16_HA:
14917 /* If we are generating a non-PIC executable, edit
14918 . 0: addis 2,12,.TOC.-0b@ha
14919 . addi 2,2,.TOC.-0b@l
14920 used by ELFv2 global entry points to set up r2, to
14921 . lis 2,.TOC.@ha
14922 . addi 2,2,.TOC.@l
14923 if .TOC. is in range. */
0e1862bb 14924 if (!bfd_link_pic (info)
810d4e75 14925 && !info->traditional_format
006589cf 14926 && !htab->opd_abi
4f038ee5 14927 && rel->r_addend == d_offset
52a82034
AM
14928 && h != NULL && &h->elf == htab->elf.hgot
14929 && rel + 1 < relend
14930 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14931 && rel[1].r_offset == rel->r_offset + 4
14932 && rel[1].r_addend == rel->r_addend + 4
14933 && relocation + 0x80008000 <= 0xffffffff)
14934 {
14935 unsigned int insn1, insn2;
4a421c53 14936 offset = rel->r_offset - d_offset;
95f0d0d2
AM
14937 insn1 = bfd_get_32 (input_bfd, contents + offset);
14938 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14939 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14940 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14941 {
14942 r_type = R_PPC64_ADDR16_HA;
14943 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14944 rel->r_addend -= d_offset;
14945 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14946 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14947 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14948 }
14949 }
14950 break;
411e1bfb
AM
14951 }
14952
14953 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14954 insn = 0;
b25116a9
AM
14955 max_br_offset = 1 << 25;
14956 addend = rel->r_addend;
bc30df16 14957 reloc_dest = DEST_NORMAL;
65f38f15 14958 switch (r_type)
5bd4f169
AM
14959 {
14960 default:
65f38f15 14961 break;
5bd4f169 14962
3b421ab3
AM
14963 case R_PPC64_TOCSAVE:
14964 if (relocation + addend == (rel->r_offset
14965 + input_section->output_offset
14966 + input_section->output_section->vma)
14967 && tocsave_find (htab, NO_INSERT,
14968 &local_syms, rel, input_bfd))
14969 {
14970 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14971 if (insn == NOP
14972 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14973 bfd_put_32 (input_bfd,
14974 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14975 contents + rel->r_offset);
14976 }
14977 break;
14978
65f38f15
AM
14979 /* Branch taken prediction relocations. */
14980 case R_PPC64_ADDR14_BRTAKEN:
14981 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14982 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14983 /* Fall through. */
65f38f15 14984
86c76c7b 14985 /* Branch not taken prediction relocations. */
65f38f15
AM
14986 case R_PPC64_ADDR14_BRNTAKEN:
14987 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14988 insn |= bfd_get_32 (input_bfd,
411e1bfb 14989 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14990 /* Fall through. */
86c76c7b 14991
b25116a9
AM
14992 case R_PPC64_REL14:
14993 max_br_offset = 1 << 15;
1a0670f3 14994 /* Fall through. */
5bd4f169 14995
65f38f15 14996 case R_PPC64_REL24:
05d0e962 14997 case R_PPC64_REL24_NOTOC:
23cedd1d 14998 case R_PPC64_PLTCALL:
5663e321 14999 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
15000 /* Calls to functions with a different TOC, such as calls to
15001 shared objects, need to alter the TOC pointer. This is
15002 done using a linkage stub. A REL24 branching to these
15003 linkage stubs needs to be followed by a nop, as the nop
15004 will be replaced with an instruction to restore the TOC
15005 base pointer. */
8387904d 15006 fdh = h;
b31867b6
AM
15007 if (h != NULL
15008 && h->oh != NULL
15009 && h->oh->is_func_descriptor)
15010 fdh = ppc_follow_link (h->oh);
31c76678
DK
15011 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15012 htab);
5663e321
AM
15013 if ((r_type == R_PPC64_PLTCALL
15014 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 15015 && stub_entry != NULL
05d0e962
AM
15016 && stub_entry->stub_type >= ppc_stub_plt_call
15017 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
15018 stub_entry = NULL;
15019
6abec6bc 15020 if (stub_entry != NULL
d0abeec8
AM
15021 && ((stub_entry->stub_type >= ppc_stub_plt_call
15022 && stub_entry->stub_type <= ppc_stub_plt_call_both)
ad8e1ba5 15023 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
15024 || stub_entry->stub_type == ppc_stub_plt_branch_both
15025 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15026 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 15027 {
b25116a9 15028 bfd_boolean can_plt_call = FALSE;
721956f4 15029
6e1816be
AM
15030 if (stub_entry->stub_type == ppc_stub_plt_call
15031 && !htab->opd_abi
15032 && htab->params->plt_localentry0 != 0
15033 && is_elfv2_localentry0 (&h->elf))
15034 {
15035 /* The function doesn't use or change r2. */
15036 can_plt_call = TRUE;
15037 }
05d0e962
AM
15038 else if (r_type == R_PPC64_REL24_NOTOC)
15039 {
15040 /* NOTOC calls don't need to restore r2. */
15041 can_plt_call = TRUE;
15042 }
6e1816be 15043
f378ab09 15044 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
15045 branch and link followed by a nop. The nop is
15046 replaced by an insn to restore r2. */
6e1816be 15047 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 15048 {
ba8ca3e7
AM
15049 unsigned long br;
15050
15051 br = bfd_get_32 (input_bfd,
15052 contents + rel->r_offset);
15053 if ((br & 1) != 0)
41bd81ab 15054 {
ba8ca3e7
AM
15055 unsigned long nop;
15056
15057 nop = bfd_get_32 (input_bfd,
15058 contents + rel->r_offset + 4);
23cedd1d
AM
15059 if (nop == LD_R2_0R1 + STK_TOC (htab))
15060 can_plt_call = TRUE;
15061 else if (nop == NOP
15062 || nop == CROR_151515
15063 || nop == CROR_313131)
a7f2871e 15064 {
ba8ca3e7
AM
15065 if (h != NULL
15066 && (h == htab->tls_get_addr_fd
15067 || h == htab->tls_get_addr)
7c9cf415 15068 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
15069 {
15070 /* Special stub used, leave nop alone. */
15071 }
15072 else
a078d95a
AM
15073 bfd_put_32 (input_bfd,
15074 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
15075 contents + rel->r_offset + 4);
15076 can_plt_call = TRUE;
a7f2871e 15077 }
41bd81ab 15078 }
5bd4f169 15079 }
721956f4 15080
ba8ca3e7 15081 if (!can_plt_call && h != NULL)
721956f4 15082 {
ba8ca3e7
AM
15083 const char *name = h->elf.root.root.string;
15084
15085 if (*name == '.')
15086 ++name;
15087
15088 if (strncmp (name, "__libc_start_main", 17) == 0
15089 && (name[17] == 0 || name[17] == '@'))
6ab189d5 15090 {
ba8ca3e7
AM
15091 /* Allow crt1 branch to go via a toc adjusting
15092 stub. Other calls that never return could do
15093 the same, if we could detect such. */
b25116a9 15094 can_plt_call = TRUE;
6ab189d5 15095 }
ba8ca3e7
AM
15096 }
15097
15098 if (!can_plt_call)
15099 {
15100 /* g++ as of 20130507 emits self-calls without a
15101 following nop. This is arguably wrong since we
15102 have conflicting information. On the one hand a
15103 global symbol and on the other a local call
15104 sequence, but don't error for this special case.
15105 It isn't possible to cheaply verify we have
15106 exactly such a call. Allow all calls to the same
15107 section. */
15108 asection *code_sec = sec;
15109
15110 if (get_opd_info (sec) != NULL)
ad8e1ba5 15111 {
ba8ca3e7
AM
15112 bfd_vma off = (relocation + addend
15113 - sec->output_section->vma
15114 - sec->output_offset);
bc30df16 15115
ba8ca3e7 15116 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 15117 }
ba8ca3e7
AM
15118 if (code_sec == input_section)
15119 can_plt_call = TRUE;
15120 }
15121
15122 if (!can_plt_call)
15123 {
05d0e962
AM
15124 if (stub_entry->stub_type >= ppc_stub_plt_call
15125 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 15126 info->callbacks->einfo
695344c0 15127 /* xgettext:c-format */
c1c8c1ef 15128 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15129 "(plt call stub)\n"),
4805fc55
AM
15130 input_bfd, input_section, rel->r_offset, sym_name);
15131 else
15132 info->callbacks->einfo
695344c0 15133 /* xgettext:c-format */
c1c8c1ef 15134 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15135 "(toc save/adjust stub)\n"),
4805fc55 15136 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
15137
15138 bfd_set_error (bfd_error_bad_value);
15139 ret = FALSE;
721956f4
AM
15140 }
15141
b25116a9 15142 if (can_plt_call
05d0e962
AM
15143 && stub_entry->stub_type >= ppc_stub_plt_call
15144 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
15145 unresolved_reloc = FALSE;
15146 }
15147
6abec6bc
AM
15148 if ((stub_entry == NULL
15149 || stub_entry->stub_type == ppc_stub_long_branch
15150 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
15151 && get_opd_info (sec) != NULL)
15152 {
15153 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
15154 bfd_vma off = (relocation + addend
15155 - sec->output_section->vma
15156 - sec->output_offset);
aef36ac1 15157 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
15158 if (dest != (bfd_vma) -1)
15159 {
15160 relocation = dest;
15161 addend = 0;
bc30df16 15162 reloc_dest = DEST_OPD;
8387904d
AM
15163 }
15164 }
15165
b25116a9
AM
15166 /* If the branch is out of reach we ought to have a long
15167 branch stub. */
15168 from = (rel->r_offset
15169 + input_section->output_offset
15170 + input_section->output_section->vma);
15171
6911b7dc
AM
15172 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15173 ? fdh->elf.other
15174 : sym->st_other);
15175
6abec6bc
AM
15176 if (stub_entry != NULL
15177 && (stub_entry->stub_type == ppc_stub_long_branch
15178 || stub_entry->stub_type == ppc_stub_plt_branch)
15179 && (r_type == R_PPC64_ADDR14_BRTAKEN
15180 || r_type == R_PPC64_ADDR14_BRNTAKEN
15181 || (relocation + addend - from + max_br_offset
15182 < 2 * max_br_offset)))
15183 /* Don't use the stub if this branch is in range. */
15184 stub_entry = NULL;
b25116a9 15185
05d0e962
AM
15186 if (stub_entry != NULL
15187 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15188 || stub_entry->stub_type == ppc_stub_long_branch_both
15189 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15190 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15191 && (r_type != R_PPC64_REL24_NOTOC
15192 || ((fdh ? fdh->elf.other : sym->st_other)
4a4e7361 15193 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
05d0e962
AM
15194 && (relocation + addend - from + max_br_offset
15195 < 2 * max_br_offset))
15196 stub_entry = NULL;
15197
15198 if (stub_entry != NULL
15199 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15200 || stub_entry->stub_type == ppc_stub_long_branch_both
15201 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15202 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15203 && r_type == R_PPC64_REL24_NOTOC
15204 && (relocation + addend - from + max_br_offset
15205 < 2 * max_br_offset))
15206 stub_entry = NULL;
15207
b25116a9
AM
15208 if (stub_entry != NULL)
15209 {
15210 /* Munge up the value and addend so that we call the stub
15211 rather than the procedure directly. */
a4b6fadd
AM
15212 asection *stub_sec = stub_entry->group->stub_sec;
15213
15214 if (stub_entry->stub_type == ppc_stub_save_res)
15215 relocation += (stub_sec->output_offset
15216 + stub_sec->output_section->vma
15217 + stub_sec->size - htab->sfpr->size
15218 - htab->sfpr->output_offset
15219 - htab->sfpr->output_section->vma);
15220 else
15221 relocation = (stub_entry->stub_offset
15222 + stub_sec->output_offset
15223 + stub_sec->output_section->vma);
b25116a9 15224 addend = 0;
bc30df16 15225 reloc_dest = DEST_STUB;
3b421ab3 15226
05d0e962
AM
15227 if (((stub_entry->stub_type == ppc_stub_plt_call
15228 && ALWAYS_EMIT_R2SAVE)
15229 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15230 || stub_entry->stub_type == ppc_stub_plt_call_both)
e81b4c93
AM
15231 && !(h != NULL
15232 && (h == htab->tls_get_addr_fd
15233 || h == htab->tls_get_addr)
15234 && htab->params->tls_get_addr_opt)
3b421ab3
AM
15235 && rel + 1 < relend
15236 && rel[1].r_offset == rel->r_offset + 4
15237 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15238 relocation += 4;
05d0e962
AM
15239 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15240 || stub_entry->stub_type == ppc_stub_plt_branch_both
15241 || stub_entry->stub_type == ppc_stub_plt_call_both)
15242 && r_type == R_PPC64_REL24_NOTOC)
15243 relocation += 4;
5663e321
AM
15244
15245 if (r_type == R_PPC64_REL24_NOTOC
15246 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
15247 || stub_entry->stub_type == ppc_stub_plt_call_both))
15248 htab->notoc_plt = 1;
b25116a9
AM
15249 }
15250
15251 if (insn != 0)
15252 {
794e51c0 15253 if (is_isa_v2)
721956f4 15254 {
b25116a9
AM
15255 /* Set 'a' bit. This is 0b00010 in BO field for branch
15256 on CR(BI) insns (BO == 001at or 011at), and 0b01000
15257 for branch on CTR insns (BO == 1a00t or 1a01t). */
15258 if ((insn & (0x14 << 21)) == (0x04 << 21))
15259 insn |= 0x02 << 21;
15260 else if ((insn & (0x14 << 21)) == (0x10 << 21))
15261 insn |= 0x08 << 21;
15262 else
15263 break;
15264 }
15265 else
15266 {
15267 /* Invert 'y' bit if not the default. */
4cc603a5 15268 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 15269 insn ^= 0x01 << 21;
721956f4 15270 }
b25116a9 15271
95f0d0d2 15272 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 15273 }
e86ce104 15274
06da1e8e
AM
15275 /* NOP out calls to undefined weak functions.
15276 We can thus call a weak function without first
15277 checking whether the function is defined. */
b25116a9 15278 else if (h != NULL
039b3fef 15279 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 15280 && h->elf.dynindx == -1
05d0e962
AM
15281 && (r_type == R_PPC64_REL24
15282 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 15283 && relocation == 0
4cc603a5 15284 && addend == 0)
e86ce104 15285 {
95f0d0d2 15286 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 15287 goto copy_reloc;
e86ce104 15288 }
65f38f15 15289 break;
066f4018
AM
15290
15291 case R_PPC64_GOT16_DS:
15292 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15293 if (relocation + addend - from + 0x8000 < 0x10000
15294 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15295 {
15296 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15297 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15298 {
15299 insn += (14u << 26) - (58u << 26);
15300 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15301 r_type = R_PPC64_TOC16;
15302 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15303 }
15304 }
15305 break;
15306
15307 case R_PPC64_GOT16_LO_DS:
15308 case R_PPC64_GOT16_HA:
15309 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15310 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
15311 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15312 {
15313 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15314 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15315 {
15316 insn += (14u << 26) - (58u << 26);
15317 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15318 r_type = R_PPC64_TOC16_LO;
15319 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15320 }
15321 else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
15322 {
15323 r_type = R_PPC64_TOC16_HA;
15324 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15325 }
15326 }
15327 break;
4a421c53
AM
15328
15329 case R_PPC64_GOT_PCREL34:
15330 from = (rel->r_offset
15331 + input_section->output_section->vma
15332 + input_section->output_offset);
15333 if (relocation - from + (1ULL << 33) < 1ULL << 34
15334 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15335 {
15336 offset = rel->r_offset;
15337 pinsn = bfd_get_32 (input_bfd, contents + offset);
15338 pinsn <<= 32;
15339 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15340 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15341 == ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
15342 {
15343 /* Replace with paddi. */
15344 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
15345 r_type = R_PPC64_PCREL34;
15346 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15347 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
15348 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
15349 goto pcrelopt;
15350 }
15351 }
15352 break;
15353
15354 case R_PPC64_PCREL34:
15355 if (SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15356 {
15357 offset = rel->r_offset;
15358 pinsn = bfd_get_32 (input_bfd, contents + offset);
15359 pinsn <<= 32;
15360 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15361 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15362 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
15363 | (14ULL << 26) /* paddi */))
15364 {
15365 pcrelopt:
15366 if (rel + 1 < relend
15367 && rel[1].r_offset == offset
15368 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT))
15369 {
15370 bfd_vma off2 = rel[1].r_addend;
15371 if (off2 == 0)
15372 /* zero means next insn. */
15373 off2 = 8;
15374 off2 += offset;
15375 if (off2 + 4 <= input_section->size)
15376 {
15377 uint64_t pinsn2;
15378 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
15379 pinsn2 <<= 32;
15380 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15381 break;
15382 if (xlate_pcrel_opt (&pinsn, &pinsn2))
15383 {
15384 bfd_put_32 (input_bfd, pinsn >> 32,
15385 contents + offset);
15386 bfd_put_32 (input_bfd, pinsn,
15387 contents + offset + 4);
15388 bfd_put_32 (input_bfd, pinsn2 >> 32,
15389 contents + off2);
15390 }
15391 }
15392 }
15393 }
15394 }
15395 break;
65f38f15 15396 }
5bd4f169 15397
65f38f15 15398 /* Set `addend'. */
411e1bfb 15399 tls_type = 0;
23cedd1d 15400 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
15401 switch (r_type)
15402 {
15403 default:
cf97bcb0
AM
15404 /* xgettext:c-format */
15405 _bfd_error_handler (_("%pB: %s unsupported"),
15406 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 15407
65f38f15 15408 bfd_set_error (bfd_error_bad_value);
b34976b6 15409 ret = FALSE;
c316a17c 15410 goto copy_reloc;
5bd4f169 15411
65f38f15 15412 case R_PPC64_NONE:
411e1bfb 15413 case R_PPC64_TLS:
727fc41e
AM
15414 case R_PPC64_TLSGD:
15415 case R_PPC64_TLSLD:
3b421ab3 15416 case R_PPC64_TOCSAVE:
04c9666a
AM
15417 case R_PPC64_GNU_VTINHERIT:
15418 case R_PPC64_GNU_VTENTRY:
006589cf 15419 case R_PPC64_ENTRY:
4a421c53 15420 case R_PPC64_PCREL_OPT:
c316a17c 15421 goto copy_reloc;
5bd4f169
AM
15422
15423 /* GOT16 relocations. Like an ADDR16 using the symbol's
15424 address in the GOT as relocation value instead of the
411e1bfb 15425 symbol's value itself. Also, create a GOT entry for the
5bd4f169 15426 symbol and put the symbol value there. */
411e1bfb
AM
15427 case R_PPC64_GOT_TLSGD16:
15428 case R_PPC64_GOT_TLSGD16_LO:
15429 case R_PPC64_GOT_TLSGD16_HI:
15430 case R_PPC64_GOT_TLSGD16_HA:
c213164a 15431 case R_PPC64_GOT_TLSGD34:
951fd09b 15432 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
15433 goto dogot;
15434
15435 case R_PPC64_GOT_TLSLD16:
15436 case R_PPC64_GOT_TLSLD16_LO:
15437 case R_PPC64_GOT_TLSLD16_HI:
15438 case R_PPC64_GOT_TLSLD16_HA:
c213164a 15439 case R_PPC64_GOT_TLSLD34:
951fd09b 15440 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
15441 goto dogot;
15442
15443 case R_PPC64_GOT_TPREL16_DS:
15444 case R_PPC64_GOT_TPREL16_LO_DS:
15445 case R_PPC64_GOT_TPREL16_HI:
15446 case R_PPC64_GOT_TPREL16_HA:
c213164a 15447 case R_PPC64_GOT_TPREL34:
411e1bfb
AM
15448 tls_type = TLS_TLS | TLS_TPREL;
15449 goto dogot;
15450
15451 case R_PPC64_GOT_DTPREL16_DS:
15452 case R_PPC64_GOT_DTPREL16_LO_DS:
15453 case R_PPC64_GOT_DTPREL16_HI:
15454 case R_PPC64_GOT_DTPREL16_HA:
c213164a 15455 case R_PPC64_GOT_DTPREL34:
411e1bfb
AM
15456 tls_type = TLS_TLS | TLS_DTPREL;
15457 goto dogot;
15458
65f38f15
AM
15459 case R_PPC64_GOT16:
15460 case R_PPC64_GOT16_LO:
15461 case R_PPC64_GOT16_HI:
15462 case R_PPC64_GOT16_HA:
15463 case R_PPC64_GOT16_DS:
15464 case R_PPC64_GOT16_LO_DS:
5663e321 15465 case R_PPC64_GOT_PCREL34:
411e1bfb 15466 dogot:
5bd4f169
AM
15467 {
15468 /* Relocation is to the entry for this symbol in the global
15469 offset table. */
e717da7e 15470 asection *got;
d881513a 15471 bfd_vma *offp;
5bd4f169 15472 bfd_vma off;
d881513a 15473 unsigned long indx = 0;
927be08e 15474 struct got_entry *ent;
5663e321
AM
15475 bfd_vma sym_addend = orig_rel.r_addend;
15476
c213164a
AM
15477 if (r_type == R_PPC64_GOT_PCREL34
15478 || r_type == R_PPC64_GOT_TLSGD34
15479 || r_type == R_PPC64_GOT_TLSLD34
15480 || r_type == R_PPC64_GOT_TPREL34
15481 || r_type == R_PPC64_GOT_DTPREL34)
5663e321 15482 sym_addend = 0;
65f38f15 15483
d881513a
AM
15484 if (tls_type == (TLS_TLS | TLS_LD)
15485 && (h == NULL
f5385ebf 15486 || !h->elf.def_dynamic))
927be08e 15487 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 15488 else
5bd4f169 15489 {
d881513a
AM
15490 if (h != NULL)
15491 {
f0158f44
AM
15492 if (!htab->elf.dynamic_sections_created
15493 || h->elf.dynindx == -1
15494 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 15495 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
15496 /* This is actually a static link, or it is a
15497 -Bsymbolic link and the symbol is defined
15498 locally, or the symbol was forced to be local
15499 because of a version file. */
15500 ;
15501 else
15502 {
039b3fef 15503 indx = h->elf.dynindx;
d881513a
AM
15504 unresolved_reloc = FALSE;
15505 }
039b3fef 15506 ent = h->elf.got.glist;
d881513a 15507 }
411e1bfb 15508 else
5bd4f169 15509 {
d881513a
AM
15510 if (local_got_ents == NULL)
15511 abort ();
15512 ent = local_got_ents[r_symndx];
5bd4f169 15513 }
d881513a
AM
15514
15515 for (; ent != NULL; ent = ent->next)
5663e321 15516 if (ent->addend == sym_addend
e717da7e 15517 && ent->owner == input_bfd
d881513a
AM
15518 && ent->tls_type == tls_type)
15519 break;
5bd4f169 15520 }
411e1bfb 15521
927be08e
AM
15522 if (ent == NULL)
15523 abort ();
15524 if (ent->is_indirect)
15525 ent = ent->got.ent;
15526 offp = &ent->got.offset;
15527 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
15528 if (got == NULL)
15529 abort ();
15530
411e1bfb
AM
15531 /* The offset must always be a multiple of 8. We use the
15532 least significant bit to record whether we have already
15533 processed this entry. */
d881513a 15534 off = *offp;
411e1bfb
AM
15535 if ((off & 1) != 0)
15536 off &= ~1;
5bd4f169
AM
15537 else
15538 {
411e1bfb
AM
15539 /* Generate relocs for the dynamic linker, except in
15540 the case of TLSLD where we'll use one entry per
15541 module. */
25f23106
AM
15542 asection *relgot;
15543 bfd_boolean ifunc;
e717da7e 15544
d881513a 15545 *offp = off | 1;
25f23106
AM
15546 relgot = NULL;
15547 ifunc = (h != NULL
15548 ? h->elf.type == STT_GNU_IFUNC
15549 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 15550 if (ifunc)
82e66161
AM
15551 {
15552 relgot = htab->elf.irelplt;
15553 if (indx == 0)
15554 htab->local_ifunc_resolver = 1;
15555 else if (is_static_defined (&h->elf))
15556 htab->maybe_local_ifunc_resolver = 1;
15557 }
f0158f44
AM
15558 else if (indx != 0
15559 || (bfd_link_pic (info)
15560 && (h == NULL
21d68fcd 15561 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 15562 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
15563 && !h->elf.def_dynamic))
15564 && !(tls_type == (TLS_TLS | TLS_TPREL)
15565 && bfd_link_executable (info)
15566 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 15567 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 15568 if (relgot != NULL)
5bd4f169 15569 {
e717da7e
AM
15570 outrel.r_offset = (got->output_section->vma
15571 + got->output_offset
411e1bfb 15572 + off);
5663e321 15573 outrel.r_addend = sym_addend;
d881513a 15574 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 15575 {
411e1bfb 15576 outrel.r_addend = 0;
e515b051 15577 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
15578 if (tls_type == (TLS_TLS | TLS_GD))
15579 {
e717da7e
AM
15580 loc = relgot->contents;
15581 loc += (relgot->reloc_count++
d881513a
AM
15582 * sizeof (Elf64_External_Rela));
15583 bfd_elf64_swap_reloca_out (output_bfd,
15584 &outrel, loc);
e515b051 15585 outrel.r_offset += 8;
5663e321 15586 outrel.r_addend = sym_addend;
d881513a
AM
15587 outrel.r_info
15588 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 15589 }
411e1bfb 15590 }
951fd09b 15591 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 15592 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 15593 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 15594 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
15595 else if (indx != 0)
15596 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15597 else
81407a69 15598 {
25f23106
AM
15599 if (ifunc)
15600 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15601 else
15602 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
15603
15604 /* Write the .got section contents for the sake
15605 of prelink. */
e717da7e 15606 loc = got->contents + off;
23fbd6fa
JJ
15607 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15608 loc);
81407a69 15609 }
81407a69
AM
15610
15611 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
15612 {
15613 outrel.r_addend += relocation;
15614 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
15615 {
15616 if (htab->elf.tls_sec == NULL)
15617 outrel.r_addend = 0;
15618 else
15619 outrel.r_addend -= htab->elf.tls_sec->vma;
15620 }
e515b051 15621 }
e717da7e
AM
15622 loc = relgot->contents;
15623 loc += (relgot->reloc_count++
411e1bfb
AM
15624 * sizeof (Elf64_External_Rela));
15625 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15626 }
15627
ad8e1ba5 15628 /* Init the .got section contents here if we're not
81407a69 15629 emitting a reloc. */
d881513a 15630 else
411e1bfb 15631 {
5663e321 15632 relocation += sym_addend;
f0158f44 15633 if (tls_type != 0)
411e1bfb 15634 {
989f9879
AM
15635 if (htab->elf.tls_sec == NULL)
15636 relocation = 0;
15637 else
15638 {
f0158f44
AM
15639 if (tls_type & TLS_LD)
15640 relocation = 0;
15641 else
15642 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 15643 if (tls_type & TLS_TPREL)
989f9879
AM
15644 relocation += DTP_OFFSET - TP_OFFSET;
15645 }
5bd4f169 15646
f0158f44 15647 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
15648 {
15649 bfd_put_64 (output_bfd, relocation,
e717da7e 15650 got->contents + off + 8);
676ee2b5 15651 relocation = 1;
7b609f53 15652 }
411e1bfb
AM
15653 }
15654 bfd_put_64 (output_bfd, relocation,
e717da7e 15655 got->contents + off);
5bd4f169
AM
15656 }
15657 }
15658
65f38f15
AM
15659 if (off >= (bfd_vma) -2)
15660 abort ();
15661
bf102f86 15662 relocation = got->output_section->vma + got->output_offset + off;
c213164a
AM
15663 if (!(r_type == R_PPC64_GOT_PCREL34
15664 || r_type == R_PPC64_GOT_TLSGD34
15665 || r_type == R_PPC64_GOT_TLSLD34
15666 || r_type == R_PPC64_GOT_TPREL34
15667 || r_type == R_PPC64_GOT_DTPREL34))
5663e321 15668 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 15669 }
65f38f15
AM
15670 break;
15671
15672 case R_PPC64_PLT16_HA:
15673 case R_PPC64_PLT16_HI:
15674 case R_PPC64_PLT16_LO:
08be3224 15675 case R_PPC64_PLT16_LO_DS:
5663e321
AM
15676 case R_PPC64_PLT_PCREL34:
15677 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
15678 case R_PPC64_PLT32:
15679 case R_PPC64_PLT64:
23cedd1d 15680 case R_PPC64_PLTSEQ:
5663e321 15681 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 15682 case R_PPC64_PLTCALL:
5663e321 15683 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
15684 /* Relocation is to the entry for this symbol in the
15685 procedure linkage table. */
23cedd1d 15686 unresolved_reloc = TRUE;
cbf95972
AM
15687 {
15688 struct plt_entry **plt_list = NULL;
15689 if (h != NULL)
15690 plt_list = &h->elf.plt.plist;
15691 else if (local_got_ents != NULL)
15692 {
15693 struct plt_entry **local_plt = (struct plt_entry **)
15694 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 15695 plt_list = local_plt + r_symndx;
cbf95972
AM
15696 }
15697 if (plt_list)
15698 {
15699 struct plt_entry *ent;
5663e321
AM
15700 bfd_vma sym_addend = orig_rel.r_addend;
15701
15702 if (r_type == R_PPC64_PLT_PCREL34
15703 || r_type == R_PPC64_PLT_PCREL34_NOTOC)
15704 sym_addend = 0;
65f38f15 15705
cbf95972
AM
15706 for (ent = *plt_list; ent != NULL; ent = ent->next)
15707 if (ent->plt.offset != (bfd_vma) -1
5663e321 15708 && ent->addend == sym_addend)
cbf95972
AM
15709 {
15710 asection *plt;
08be3224 15711 bfd_vma got;
cbf95972
AM
15712
15713 plt = htab->elf.splt;
15714 if (!htab->elf.dynamic_sections_created
15715 || h == NULL
15716 || h->elf.dynindx == -1)
2d7ad24e
AM
15717 {
15718 if (h != NULL
15719 ? h->elf.type == STT_GNU_IFUNC
15720 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15721 plt = htab->elf.iplt;
15722 else
15723 plt = htab->pltlocal;
15724 }
15725 relocation = (plt->output_section->vma
15726 + plt->output_offset
15727 + ent->plt.offset);
08be3224 15728 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
15729 || r_type == R_PPC64_PLT16_HI
15730 || r_type == R_PPC64_PLT16_LO
15731 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
15732 {
15733 got = (elf_gp (output_bfd)
15734 + htab->sec_info[input_section->id].toc_off);
15735 relocation -= got;
15736 }
5663e321
AM
15737 if (r_type != R_PPC64_PLT_PCREL34
15738 && r_type != R_PPC64_PLT_PCREL34_NOTOC)
15739 addend = 0;
cbf95972
AM
15740 unresolved_reloc = FALSE;
15741 break;
15742 }
15743 }
15744 }
65f38f15 15745 break;
5bd4f169 15746
0b13192e
AM
15747 case R_PPC64_TOC:
15748 /* Relocation value is TOC base. */
15749 relocation = TOCstart;
cf35638d 15750 if (r_symndx == STN_UNDEF)
6f20ed8a 15751 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
15752 else if (unresolved_reloc)
15753 ;
6f20ed8a
AM
15754 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15755 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
15756 else
15757 unresolved_reloc = TRUE;
ab96bf03 15758 goto dodyn;
0b13192e 15759
5bd4f169
AM
15760 /* TOC16 relocs. We want the offset relative to the TOC base,
15761 which is the address of the start of the TOC plus 0x8000.
15762 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15763 in this order. */
65f38f15
AM
15764 case R_PPC64_TOC16:
15765 case R_PPC64_TOC16_LO:
15766 case R_PPC64_TOC16_HI:
15767 case R_PPC64_TOC16_DS:
15768 case R_PPC64_TOC16_LO_DS:
15769 case R_PPC64_TOC16_HA:
6f20ed8a 15770 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
15771 break;
15772
15773 /* Relocate against the beginning of the section. */
65f38f15
AM
15774 case R_PPC64_SECTOFF:
15775 case R_PPC64_SECTOFF_LO:
15776 case R_PPC64_SECTOFF_HI:
15777 case R_PPC64_SECTOFF_DS:
15778 case R_PPC64_SECTOFF_LO_DS:
15779 case R_PPC64_SECTOFF_HA:
4ce794b7 15780 if (sec != NULL)
65f38f15 15781 addend -= sec->output_section->vma;
5bd4f169
AM
15782 break;
15783
25f23106
AM
15784 case R_PPC64_REL16:
15785 case R_PPC64_REL16_LO:
15786 case R_PPC64_REL16_HI:
15787 case R_PPC64_REL16_HA:
4a969973
AM
15788 case R_PPC64_REL16_HIGH:
15789 case R_PPC64_REL16_HIGHA:
15790 case R_PPC64_REL16_HIGHER:
15791 case R_PPC64_REL16_HIGHERA:
15792 case R_PPC64_REL16_HIGHEST:
15793 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
15794 case R_PPC64_REL16_HIGHER34:
15795 case R_PPC64_REL16_HIGHERA34:
15796 case R_PPC64_REL16_HIGHEST34:
15797 case R_PPC64_REL16_HIGHESTA34:
a680de9a 15798 case R_PPC64_REL16DX_HA:
721956f4
AM
15799 case R_PPC64_REL14:
15800 case R_PPC64_REL14_BRNTAKEN:
15801 case R_PPC64_REL14_BRTAKEN:
5d1634d7 15802 case R_PPC64_REL24:
05d0e962 15803 case R_PPC64_REL24_NOTOC:
5663e321
AM
15804 case R_PPC64_PCREL34:
15805 case R_PPC64_PCREL28:
5d1634d7
AM
15806 break;
15807
411e1bfb
AM
15808 case R_PPC64_TPREL16:
15809 case R_PPC64_TPREL16_LO:
15810 case R_PPC64_TPREL16_HI:
15811 case R_PPC64_TPREL16_HA:
15812 case R_PPC64_TPREL16_DS:
15813 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
15814 case R_PPC64_TPREL16_HIGH:
15815 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
15816 case R_PPC64_TPREL16_HIGHER:
15817 case R_PPC64_TPREL16_HIGHERA:
15818 case R_PPC64_TPREL16_HIGHEST:
15819 case R_PPC64_TPREL16_HIGHESTA:
c213164a 15820 case R_PPC64_TPREL34:
766bc656
AM
15821 if (h != NULL
15822 && h->elf.root.type == bfd_link_hash_undefweak
15823 && h->elf.dynindx == -1)
15824 {
15825 /* Make this relocation against an undefined weak symbol
15826 resolve to zero. This is really just a tweak, since
15827 code using weak externs ought to check that they are
15828 defined before using them. */
15829 bfd_byte *p = contents + rel->r_offset - d_offset;
15830
95f0d0d2 15831 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
15832 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15833 if (insn != 0)
95f0d0d2 15834 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
15835 break;
15836 }
989f9879
AM
15837 if (htab->elf.tls_sec != NULL)
15838 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
15839 /* The TPREL16 relocs shouldn't really be used in shared
15840 libs or with non-local symbols as that will result in
15841 DT_TEXTREL being set, but support them anyway. */
15842 goto dodyn;
411e1bfb
AM
15843
15844 case R_PPC64_DTPREL16:
15845 case R_PPC64_DTPREL16_LO:
15846 case R_PPC64_DTPREL16_HI:
15847 case R_PPC64_DTPREL16_HA:
15848 case R_PPC64_DTPREL16_DS:
15849 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
15850 case R_PPC64_DTPREL16_HIGH:
15851 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
15852 case R_PPC64_DTPREL16_HIGHER:
15853 case R_PPC64_DTPREL16_HIGHERA:
15854 case R_PPC64_DTPREL16_HIGHEST:
15855 case R_PPC64_DTPREL16_HIGHESTA:
c213164a 15856 case R_PPC64_DTPREL34:
989f9879
AM
15857 if (htab->elf.tls_sec != NULL)
15858 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
15859 break;
15860
45965137
AM
15861 case R_PPC64_ADDR64_LOCAL:
15862 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15863 ? h->elf.other
15864 : sym->st_other);
15865 break;
15866
e515b051
AM
15867 case R_PPC64_DTPMOD64:
15868 relocation = 1;
15869 addend = 0;
15870 goto dodyn;
15871
411e1bfb 15872 case R_PPC64_TPREL64:
989f9879
AM
15873 if (htab->elf.tls_sec != NULL)
15874 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
15875 goto dodyn;
15876
15877 case R_PPC64_DTPREL64:
989f9879
AM
15878 if (htab->elf.tls_sec != NULL)
15879 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 15880 /* Fall through. */
411e1bfb 15881
65f38f15
AM
15882 /* Relocations that may need to be propagated if this is a
15883 dynamic object. */
04c9666a 15884 case R_PPC64_REL30:
65f38f15
AM
15885 case R_PPC64_REL32:
15886 case R_PPC64_REL64:
15887 case R_PPC64_ADDR14:
15888 case R_PPC64_ADDR14_BRNTAKEN:
15889 case R_PPC64_ADDR14_BRTAKEN:
15890 case R_PPC64_ADDR16:
15891 case R_PPC64_ADDR16_DS:
15892 case R_PPC64_ADDR16_HA:
15893 case R_PPC64_ADDR16_HI:
f9c6b907
AM
15894 case R_PPC64_ADDR16_HIGH:
15895 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15896 case R_PPC64_ADDR16_HIGHER:
15897 case R_PPC64_ADDR16_HIGHERA:
15898 case R_PPC64_ADDR16_HIGHEST:
15899 case R_PPC64_ADDR16_HIGHESTA:
15900 case R_PPC64_ADDR16_LO:
15901 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
15902 case R_PPC64_ADDR16_HIGHER34:
15903 case R_PPC64_ADDR16_HIGHERA34:
15904 case R_PPC64_ADDR16_HIGHEST34:
15905 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 15906 case R_PPC64_ADDR24:
65f38f15
AM
15907 case R_PPC64_ADDR32:
15908 case R_PPC64_ADDR64:
15909 case R_PPC64_UADDR16:
15910 case R_PPC64_UADDR32:
15911 case R_PPC64_UADDR64:
5663e321
AM
15912 case R_PPC64_D34:
15913 case R_PPC64_D34_LO:
15914 case R_PPC64_D34_HI30:
15915 case R_PPC64_D34_HA30:
15916 case R_PPC64_D28:
411e1bfb 15917 dodyn:
5d1634d7 15918 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
15919 break;
15920
41bd81ab
AM
15921 if (NO_OPD_RELOCS && is_opd)
15922 break;
15923
8a9e8e72 15924 if (bfd_link_pic (info)
b1b07054
AM
15925 ? ((h == NULL
15926 || h->dyn_relocs != NULL)
15927 && ((h != NULL && pc_dynrelocs (h))
15928 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
15929 : (h != NULL
15930 ? h->dyn_relocs != NULL
d311bc8b 15931 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 15932 {
b34976b6 15933 bfd_boolean skip, relocate;
65f38f15 15934 asection *sreloc;
1cf1f670 15935 bfd_vma out_off;
82e66161 15936 long indx = 0;
65f38f15
AM
15937
15938 /* When generating a dynamic object, these relocations
15939 are copied into the output file to be resolved at run
15940 time. */
15941
b34976b6
AM
15942 skip = FALSE;
15943 relocate = FALSE;
65f38f15 15944
1cf1f670
AM
15945 out_off = _bfd_elf_section_offset (output_bfd, info,
15946 input_section, rel->r_offset);
15947 if (out_off == (bfd_vma) -1)
b34976b6 15948 skip = TRUE;
1cf1f670 15949 else if (out_off == (bfd_vma) -2)
b34976b6 15950 skip = TRUE, relocate = TRUE;
1cf1f670
AM
15951 out_off += (input_section->output_section->vma
15952 + input_section->output_offset);
15953 outrel.r_offset = out_off;
411e1bfb 15954 outrel.r_addend = rel->r_addend;
65f38f15 15955
1cf1f670
AM
15956 /* Optimize unaligned reloc use. */
15957 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15958 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15959 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15960 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15961 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15962 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15963 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15964 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15965 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15966
65f38f15 15967 if (skip)
0bb2d96a 15968 memset (&outrel, 0, sizeof outrel);
afe397ea 15969 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
15970 && !is_opd
15971 && r_type != R_PPC64_TOC)
14acf4dc 15972 {
82e66161
AM
15973 indx = h->elf.dynindx;
15974 BFD_ASSERT (indx != -1);
15975 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 15976 }
65f38f15
AM
15977 else
15978 {
41bd81ab
AM
15979 /* This symbol is local, or marked to become local,
15980 or this is an opd section reloc which must point
15981 at a local function. */
65f38f15 15982 outrel.r_addend += relocation;
e86ce104 15983 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 15984 {
3fad3c7c 15985 if (is_opd && h != NULL)
afbe61cf
AM
15986 {
15987 /* Lie about opd entries. This case occurs
15988 when building shared libraries and we
15989 reference a function in another shared
3fad3c7c
AM
15990 lib. The same thing happens for a weak
15991 definition in an application that's
15992 overridden by a strong definition in a
15993 shared lib. (I believe this is a generic
15994 bug in binutils handling of weak syms.)
15995 In these cases we won't use the opd
1e2f5b6e 15996 entry in this lib. */
b34976b6 15997 unresolved_reloc = FALSE;
afbe61cf 15998 }
25f23106
AM
15999 if (!is_opd
16000 && r_type == R_PPC64_ADDR64
16001 && (h != NULL
16002 ? h->elf.type == STT_GNU_IFUNC
16003 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16004 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16005 else
16006 {
16007 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 16008
25f23106
AM
16009 /* We need to relocate .opd contents for ld.so.
16010 Prelink also wants simple and consistent rules
16011 for relocs. This make all RELATIVE relocs have
16012 *r_offset equal to r_addend. */
16013 relocate = TRUE;
16014 }
65f38f15
AM
16015 }
16016 else
16017 {
25f23106
AM
16018 if (h != NULL
16019 ? h->elf.type == STT_GNU_IFUNC
16020 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16021 {
25f53a85 16022 info->callbacks->einfo
695344c0 16023 /* xgettext:c-format */
174d0a74 16024 (_("%H: %s for indirect "
c1c8c1ef 16025 "function `%pT' unsupported\n"),
25f53a85 16026 input_bfd, input_section, rel->r_offset,
25f23106
AM
16027 ppc64_elf_howto_table[r_type]->name,
16028 sym_name);
16029 ret = FALSE;
16030 }
cf35638d 16031 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
16032 ;
16033 else if (sec == NULL || sec->owner == NULL)
16034 {
16035 bfd_set_error (bfd_error_bad_value);
b34976b6 16036 return FALSE;
65f38f15
AM
16037 }
16038 else
16039 {
f26a3287 16040 asection *osec = sec->output_section;
65f38f15 16041
f26a3287
AM
16042 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16043 {
16044 /* TLS symbol values are relative to the
16045 TLS segment. Dynamic relocations for
16046 local TLS symbols therefore can't be
16047 reduced to a relocation against their
16048 section symbol because it holds the
16049 address of the section, not a value
16050 relative to the TLS segment. We could
16051 change the .tdata dynamic section symbol
16052 to be zero value but STN_UNDEF works
16053 and is used elsewhere, eg. for TPREL64
16054 GOT relocs against local TLS symbols. */
16055 osec = htab->elf.tls_sec;
16056 indx = 0;
16057 }
16058 else
74541ad4 16059 {
74541ad4 16060 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
16061 if (indx == 0)
16062 {
16063 if ((osec->flags & SEC_READONLY) == 0
16064 && htab->elf.data_index_section != NULL)
16065 osec = htab->elf.data_index_section;
16066 else
16067 osec = htab->elf.text_index_section;
16068 indx = elf_section_data (osec)->dynindx;
16069 }
16070 BFD_ASSERT (indx != 0);
74541ad4 16071 }
74541ad4 16072
65f38f15
AM
16073 /* We are turning this relocation into one
16074 against a section symbol, so subtract out
16075 the output section's address but not the
16076 offset of the input section in the output
16077 section. */
16078 outrel.r_addend -= osec->vma;
16079 }
16080
16081 outrel.r_info = ELF64_R_INFO (indx, r_type);
16082 }
16083 }
16084
16085 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
16086 if (h != NULL
16087 ? h->elf.type == STT_GNU_IFUNC
16088 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
16089 {
16090 sreloc = htab->elf.irelplt;
16091 if (indx == 0)
16092 htab->local_ifunc_resolver = 1;
16093 else if (is_static_defined (&h->elf))
16094 htab->maybe_local_ifunc_resolver = 1;
16095 }
65f38f15
AM
16096 if (sreloc == NULL)
16097 abort ();
16098
dfbb6ac9
AM
16099 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16100 >= sreloc->size)
16101 abort ();
947216bf
AM
16102 loc = sreloc->contents;
16103 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
16104 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16105
16106 /* If this reloc is against an external symbol, it will
16107 be computed at runtime, so there's no need to do
81407a69
AM
16108 anything now. However, for the sake of prelink ensure
16109 that the section contents are a known value. */
2cdcc330 16110 if (!relocate)
81407a69
AM
16111 {
16112 unresolved_reloc = FALSE;
16113 /* The value chosen here is quite arbitrary as ld.so
16114 ignores section contents except for the special
16115 case of .opd where the contents might be accessed
16116 before relocation. Choose zero, as that won't
16117 cause reloc overflow. */
16118 relocation = 0;
16119 addend = 0;
16120 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16121 to improve backward compatibility with older
16122 versions of ld. */
16123 if (r_type == R_PPC64_ADDR64)
16124 addend = outrel.r_addend;
16125 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 16126 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 16127 addend = outrel.r_offset;
81407a69 16128 }
65f38f15 16129 }
5bd4f169
AM
16130 break;
16131
65f38f15
AM
16132 case R_PPC64_COPY:
16133 case R_PPC64_GLOB_DAT:
16134 case R_PPC64_JMP_SLOT:
25f23106 16135 case R_PPC64_JMP_IREL:
65f38f15
AM
16136 case R_PPC64_RELATIVE:
16137 /* We shouldn't ever see these dynamic relocs in relocatable
16138 files. */
ae9a127f 16139 /* Fall through. */
65f38f15
AM
16140
16141 case R_PPC64_PLTGOT16:
16142 case R_PPC64_PLTGOT16_DS:
16143 case R_PPC64_PLTGOT16_HA:
16144 case R_PPC64_PLTGOT16_HI:
16145 case R_PPC64_PLTGOT16_LO:
16146 case R_PPC64_PLTGOT16_LO_DS:
16147 case R_PPC64_PLTREL32:
16148 case R_PPC64_PLTREL64:
16149 /* These ones haven't been implemented yet. */
16150
25f53a85 16151 info->callbacks->einfo
695344c0 16152 /* xgettext:c-format */
c1c8c1ef 16153 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 16154 input_bfd,
4ce794b7 16155 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
16156
16157 bfd_set_error (bfd_error_invalid_operation);
b34976b6 16158 ret = FALSE;
c316a17c 16159 goto copy_reloc;
65f38f15 16160 }
5bd4f169 16161
67f0cbdb
AM
16162 /* Multi-instruction sequences that access the TOC can be
16163 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 16164 to nop; addi rb,r2,x; */
67f0cbdb
AM
16165 switch (r_type)
16166 {
16167 default:
16168 break;
16169
16170 case R_PPC64_GOT_TLSLD16_HI:
16171 case R_PPC64_GOT_TLSGD16_HI:
16172 case R_PPC64_GOT_TPREL16_HI:
16173 case R_PPC64_GOT_DTPREL16_HI:
16174 case R_PPC64_GOT16_HI:
16175 case R_PPC64_TOC16_HI:
16176 /* These relocs would only be useful if building up an
16177 offset to later add to r2, perhaps in an indexed
16178 addressing mode instruction. Don't try to optimize.
16179 Unfortunately, the possibility of someone building up an
16180 offset like this or even with the HA relocs, means that
16181 we need to check the high insn when optimizing the low
16182 insn. */
16183 break;
16184
5663e321
AM
16185 case R_PPC64_PLTCALL_NOTOC:
16186 if (!unresolved_reloc)
16187 htab->notoc_plt = 1;
16188 /* Fall through. */
23cedd1d
AM
16189 case R_PPC64_PLTCALL:
16190 if (unresolved_reloc)
16191 {
16192 /* No plt entry. Make this into a direct call. */
16193 bfd_byte *p = contents + rel->r_offset;
16194 insn = bfd_get_32 (input_bfd, p);
16195 insn &= 1;
16196 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
16197 if (r_type == R_PPC64_PLTCALL)
16198 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
16199 unresolved_reloc = save_unresolved_reloc;
16200 r_type = R_PPC64_REL24;
16201 }
16202 break;
16203
5663e321 16204 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
16205 case R_PPC64_PLTSEQ:
16206 if (unresolved_reloc)
16207 {
16208 unresolved_reloc = FALSE;
16209 goto nop_it;
16210 }
16211 break;
16212
5663e321
AM
16213 case R_PPC64_PLT_PCREL34_NOTOC:
16214 if (!unresolved_reloc)
16215 htab->notoc_plt = 1;
16216 /* Fall through. */
16217 case R_PPC64_PLT_PCREL34:
16218 if (unresolved_reloc)
16219 {
16220 bfd_byte *p = contents + rel->r_offset;
16221 bfd_put_32 (input_bfd, PNOP >> 32, p);
16222 bfd_put_32 (input_bfd, PNOP, p + 4);
16223 unresolved_reloc = FALSE;
16224 goto copy_reloc;
16225 }
16226 break;
16227
23cedd1d
AM
16228 case R_PPC64_PLT16_HA:
16229 if (unresolved_reloc)
16230 {
16231 unresolved_reloc = FALSE;
16232 goto nop_it;
16233 }
16234 /* Fall through. */
67f0cbdb
AM
16235 case R_PPC64_GOT_TLSLD16_HA:
16236 case R_PPC64_GOT_TLSGD16_HA:
16237 case R_PPC64_GOT_TPREL16_HA:
16238 case R_PPC64_GOT_DTPREL16_HA:
16239 case R_PPC64_GOT16_HA:
16240 case R_PPC64_TOC16_HA:
98528052 16241 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16242 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 16243 {
23cedd1d
AM
16244 bfd_byte *p;
16245 nop_it:
16246 p = contents + (rel->r_offset & ~3);
98528052 16247 bfd_put_32 (input_bfd, NOP, p);
d830549d 16248 goto copy_reloc;
98528052 16249 }
67f0cbdb
AM
16250 break;
16251
23cedd1d
AM
16252 case R_PPC64_PLT16_LO:
16253 case R_PPC64_PLT16_LO_DS:
16254 if (unresolved_reloc)
16255 {
16256 unresolved_reloc = FALSE;
16257 goto nop_it;
16258 }
16259 /* Fall through. */
67f0cbdb
AM
16260 case R_PPC64_GOT_TLSLD16_LO:
16261 case R_PPC64_GOT_TLSGD16_LO:
16262 case R_PPC64_GOT_TPREL16_LO_DS:
16263 case R_PPC64_GOT_DTPREL16_LO_DS:
16264 case R_PPC64_GOT16_LO:
16265 case R_PPC64_GOT16_LO_DS:
16266 case R_PPC64_TOC16_LO:
16267 case R_PPC64_TOC16_LO_DS:
98528052 16268 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16269 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
16270 {
16271 bfd_byte *p = contents + (rel->r_offset & ~3);
16272 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
16273 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16274 {
16275 /* Transform addic to addi when we change reg. */
16276 insn &= ~((0x3f << 26) | (0x1f << 16));
16277 insn |= (14u << 26) | (2 << 16);
16278 }
16279 else
67f0cbdb 16280 {
98528052
AM
16281 insn &= ~(0x1f << 16);
16282 insn |= 2 << 16;
67f0cbdb 16283 }
560c8763 16284 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
16285 }
16286 break;
9a23f96e
AM
16287
16288 case R_PPC64_TPREL16_HA:
16289 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16290 {
16291 bfd_byte *p = contents + (rel->r_offset & ~3);
16292 insn = bfd_get_32 (input_bfd, p);
16293 if ((insn & ((0x3f << 26) | 0x1f << 16))
16294 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16295 /* xgettext:c-format */
16296 info->callbacks->minfo
16297 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
16298 input_bfd, input_section, rel->r_offset,
16299 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 16300 else
d830549d
AM
16301 {
16302 bfd_put_32 (input_bfd, NOP, p);
16303 goto copy_reloc;
16304 }
9a23f96e
AM
16305 }
16306 break;
16307
16308 case R_PPC64_TPREL16_LO:
16309 case R_PPC64_TPREL16_LO_DS:
16310 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16311 {
16312 bfd_byte *p = contents + (rel->r_offset & ~3);
16313 insn = bfd_get_32 (input_bfd, p);
16314 insn &= ~(0x1f << 16);
16315 insn |= 13 << 16;
16316 bfd_put_32 (input_bfd, insn, p);
16317 }
16318 break;
67f0cbdb
AM
16319 }
16320
65f38f15
AM
16321 /* Do any further special processing. */
16322 switch (r_type)
16323 {
16324 default:
16325 break;
16326
25f23106 16327 case R_PPC64_REL16_HA:
4a969973
AM
16328 case R_PPC64_REL16_HIGHA:
16329 case R_PPC64_REL16_HIGHERA:
16330 case R_PPC64_REL16_HIGHESTA:
a680de9a 16331 case R_PPC64_REL16DX_HA:
f9c6b907
AM
16332 case R_PPC64_ADDR16_HA:
16333 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
16334 case R_PPC64_ADDR16_HIGHERA:
16335 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
16336 case R_PPC64_TOC16_HA:
16337 case R_PPC64_SECTOFF_HA:
411e1bfb 16338 case R_PPC64_TPREL16_HA:
f9c6b907 16339 case R_PPC64_TPREL16_HIGHA:
411e1bfb 16340 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 16341 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
16342 case R_PPC64_DTPREL16_HA:
16343 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 16344 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 16345 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
16346 /* It's just possible that this symbol is a weak symbol
16347 that's not actually defined anywhere. In that case,
16348 'sec' would be NULL, and we should leave the symbol
16349 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
16350 if (sec == NULL)
16351 break;
1a0670f3 16352 /* Fall through. */
5c5f6e17
AM
16353
16354 case R_PPC64_GOT16_HA:
16355 case R_PPC64_PLTGOT16_HA:
16356 case R_PPC64_PLT16_HA:
16357 case R_PPC64_GOT_TLSGD16_HA:
16358 case R_PPC64_GOT_TLSLD16_HA:
16359 case R_PPC64_GOT_TPREL16_HA:
16360 case R_PPC64_GOT_DTPREL16_HA:
16361 /* Add 0x10000 if sign bit in 0:15 is set.
16362 Bits 0:15 are not used. */
16363 addend += 0x8000;
65f38f15
AM
16364 break;
16365
5663e321
AM
16366 case R_PPC64_D34_HA30:
16367 case R_PPC64_ADDR16_HIGHERA34:
16368 case R_PPC64_ADDR16_HIGHESTA34:
16369 case R_PPC64_REL16_HIGHERA34:
16370 case R_PPC64_REL16_HIGHESTA34:
16371 if (sec != NULL)
16372 addend += 1ULL << 33;
16373 break;
16374
65f38f15
AM
16375 case R_PPC64_ADDR16_DS:
16376 case R_PPC64_ADDR16_LO_DS:
16377 case R_PPC64_GOT16_DS:
16378 case R_PPC64_GOT16_LO_DS:
16379 case R_PPC64_PLT16_LO_DS:
16380 case R_PPC64_SECTOFF_DS:
16381 case R_PPC64_SECTOFF_LO_DS:
16382 case R_PPC64_TOC16_DS:
16383 case R_PPC64_TOC16_LO_DS:
16384 case R_PPC64_PLTGOT16_DS:
16385 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
16386 case R_PPC64_GOT_TPREL16_DS:
16387 case R_PPC64_GOT_TPREL16_LO_DS:
16388 case R_PPC64_GOT_DTPREL16_DS:
16389 case R_PPC64_GOT_DTPREL16_LO_DS:
16390 case R_PPC64_TPREL16_DS:
16391 case R_PPC64_TPREL16_LO_DS:
16392 case R_PPC64_DTPREL16_DS:
16393 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
16394 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16395 mask = 3;
a680de9a
PB
16396 /* If this reloc is against an lq, lxv, or stxv insn, then
16397 the value must be a multiple of 16. This is somewhat of
16398 a hack, but the "correct" way to do this by defining _DQ
16399 forms of all the _DS relocs bloats all reloc switches in
16400 this file. It doesn't make much sense to use these
16401 relocs in data, so testing the insn should be safe. */
16402 if ((insn & (0x3f << 26)) == (56u << 26)
16403 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 16404 mask = 15;
a680de9a
PB
16405 relocation += addend;
16406 addend = insn & (mask ^ 3);
16407 if ((relocation & mask) != 0)
65f38f15 16408 {
a680de9a 16409 relocation ^= relocation & mask;
25f53a85 16410 info->callbacks->einfo
695344c0 16411 /* xgettext:c-format */
174d0a74 16412 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 16413 input_bfd, input_section, rel->r_offset,
d830549d 16414 ppc64_elf_howto_table[r_type]->name,
adadcc0c 16415 mask + 1);
65f38f15 16416 bfd_set_error (bfd_error_bad_value);
b34976b6 16417 ret = FALSE;
c316a17c 16418 goto copy_reloc;
65f38f15
AM
16419 }
16420 break;
5bd4f169
AM
16421 }
16422
239e1f3a
AM
16423 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16424 because such sections are not SEC_ALLOC and thus ld.so will
16425 not process them. */
d830549d 16426 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 16427 if (unresolved_reloc
239e1f3a 16428 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
16429 && h->elf.def_dynamic)
16430 && _bfd_elf_section_offset (output_bfd, info, input_section,
16431 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 16432 {
25f53a85 16433 info->callbacks->einfo
695344c0 16434 /* xgettext:c-format */
c1c8c1ef 16435 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 16436 input_bfd, input_section, rel->r_offset,
b80eed39 16437 howto->name,
039b3fef 16438 h->elf.root.root.string);
b34976b6 16439 ret = FALSE;
9c07fe7c 16440 }
5bd4f169 16441
b80eed39
AM
16442 /* 16-bit fields in insns mostly have signed values, but a
16443 few insns have 16-bit unsigned values. Really, we should
16444 have different reloc types. */
16445 if (howto->complain_on_overflow != complain_overflow_dont
16446 && howto->dst_mask == 0xffff
16447 && (input_section->flags & SEC_CODE) != 0)
16448 {
16449 enum complain_overflow complain = complain_overflow_signed;
16450
16451 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
16452 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16453 complain = complain_overflow_bitfield;
16454 else if (howto->rightshift == 0
16455 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16456 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16457 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16458 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16459 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16460 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
16461 complain = complain_overflow_unsigned;
16462 if (howto->complain_on_overflow != complain)
16463 {
16464 alt_howto = *howto;
16465 alt_howto.complain_on_overflow = complain;
16466 howto = &alt_howto;
16467 }
16468 }
16469
5663e321 16470 switch (r_type)
a680de9a 16471 {
5663e321
AM
16472 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
16473 case R_PPC64_D34:
16474 case R_PPC64_D34_LO:
16475 case R_PPC64_D34_HI30:
16476 case R_PPC64_D34_HA30:
16477 case R_PPC64_PCREL34:
16478 case R_PPC64_GOT_PCREL34:
c213164a
AM
16479 case R_PPC64_TPREL34:
16480 case R_PPC64_DTPREL34:
16481 case R_PPC64_GOT_TLSGD34:
16482 case R_PPC64_GOT_TLSLD34:
16483 case R_PPC64_GOT_TPREL34:
16484 case R_PPC64_GOT_DTPREL34:
5663e321
AM
16485 case R_PPC64_PLT_PCREL34:
16486 case R_PPC64_PLT_PCREL34_NOTOC:
16487 case R_PPC64_D28:
16488 case R_PPC64_PCREL28:
16489 if (rel->r_offset + 8 > input_section->size)
16490 r = bfd_reloc_outofrange;
16491 else
16492 {
5663e321
AM
16493 relocation += addend;
16494 if (howto->pc_relative)
16495 relocation -= (rel->r_offset
16496 + input_section->output_offset
16497 + input_section->output_section->vma);
16498 relocation >>= howto->rightshift;
16499
16500 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16501 pinsn <<= 32;
16502 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16503
16504 pinsn &= ~howto->dst_mask;
16505 pinsn |= (((relocation << 16) | (relocation & 0xffff))
16506 & howto->dst_mask);
16507 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
16508 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
16509 r = bfd_reloc_ok;
16510 if (howto->complain_on_overflow == complain_overflow_signed
16511 && (relocation + (1ULL << (howto->bitsize - 1))
16512 >= 1ULL << howto->bitsize))
16513 r = bfd_reloc_overflow;
16514 }
16515 break;
16516
16517 case R_PPC64_REL16DX_HA:
a680de9a
PB
16518 if (rel->r_offset + 4 > input_section->size)
16519 r = bfd_reloc_outofrange;
16520 else
16521 {
16522 relocation += addend;
16523 relocation -= (rel->r_offset
16524 + input_section->output_offset
16525 + input_section->output_section->vma);
3de43e7b 16526 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
16527 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16528 insn &= ~0x1fffc1;
3de43e7b 16529 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
16530 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16531 r = bfd_reloc_ok;
3de43e7b 16532 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
16533 r = bfd_reloc_overflow;
16534 }
5663e321
AM
16535 break;
16536
16537 default:
16538 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
16539 contents, rel->r_offset,
16540 relocation, addend);
a680de9a 16541 }
5bd4f169 16542
ef60b7ff 16543 if (r != bfd_reloc_ok)
5bd4f169 16544 {
bc30df16 16545 char *more_info = NULL;
b80eed39 16546 const char *reloc_name = howto->name;
bc30df16
AM
16547
16548 if (reloc_dest != DEST_NORMAL)
16549 {
16550 more_info = bfd_malloc (strlen (reloc_name) + 8);
16551 if (more_info != NULL)
16552 {
16553 strcpy (more_info, reloc_name);
16554 strcat (more_info, (reloc_dest == DEST_OPD
16555 ? " (OPD)" : " (stub)"));
16556 reloc_name = more_info;
16557 }
16558 }
16559
cd27b276 16560 if (r == bfd_reloc_overflow)
5bd4f169 16561 {
8131c122
AM
16562 /* On code like "if (foo) foo();" don't report overflow
16563 on a branch to zero when foo is undefined. */
16564 if (!warned
16565 && (reloc_dest == DEST_STUB
16566 || !(h != NULL
16567 && (h->elf.root.type == bfd_link_hash_undefweak
16568 || h->elf.root.type == bfd_link_hash_undefined)
16569 && is_branch_reloc (r_type))))
1a72702b
AM
16570 info->callbacks->reloc_overflow (info, &h->elf.root,
16571 sym_name, reloc_name,
16572 orig_rel.r_addend,
16573 input_bfd, input_section,
16574 rel->r_offset);
ef60b7ff
AM
16575 }
16576 else
16577 {
25f53a85 16578 info->callbacks->einfo
695344c0 16579 /* xgettext:c-format */
c1c8c1ef 16580 (_("%H: %s against `%pT': error %d\n"),
25f53a85 16581 input_bfd, input_section, rel->r_offset,
bc30df16 16582 reloc_name, sym_name, (int) r);
b34976b6 16583 ret = FALSE;
ef60b7ff 16584 }
bc30df16
AM
16585 if (more_info != NULL)
16586 free (more_info);
5bd4f169 16587 }
c316a17c
AM
16588 copy_reloc:
16589 if (wrel != rel)
16590 *wrel = *rel;
16591 }
16592
16593 if (wrel != rel)
16594 {
16595 Elf_Internal_Shdr *rel_hdr;
16596 size_t deleted = rel - wrel;
16597
16598 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16599 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16600 if (rel_hdr->sh_size == 0)
16601 {
16602 /* It is too late to remove an empty reloc section. Leave
16603 one NONE reloc.
16604 ??? What is wrong with an empty section??? */
16605 rel_hdr->sh_size = rel_hdr->sh_entsize;
16606 deleted -= 1;
16607 }
16608 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16609 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16610 input_section->reloc_count -= deleted;
5bd4f169
AM
16611 }
16612
645ea6a9
AM
16613 /* If we're emitting relocations, then shortly after this function
16614 returns, reloc offsets and addends for this section will be
16615 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
16616 file rather than the input. Save a copy of the relocs for
16617 opd_entry_value. */
0e1862bb 16618 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
16619 {
16620 bfd_size_type amt;
16621 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16622 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
16623 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16624 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
16625 if (rel == NULL)
16626 return FALSE;
16627 memcpy (rel, relocs, amt);
16628 }
5bd4f169
AM
16629 return ret;
16630}
16631
754021d0
AM
16632/* Adjust the value of any local symbols in opd sections. */
16633
6e0b88f1 16634static int
754021d0
AM
16635ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16636 const char *name ATTRIBUTE_UNUSED,
16637 Elf_Internal_Sym *elfsym,
16638 asection *input_sec,
16639 struct elf_link_hash_entry *h)
16640{
74f0fb50
AM
16641 struct _opd_sec_data *opd;
16642 long adjust;
754021d0
AM
16643 bfd_vma value;
16644
4025353c 16645 if (h != NULL)
6e0b88f1 16646 return 1;
4025353c 16647
74f0fb50
AM
16648 opd = get_opd_info (input_sec);
16649 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 16650 return 1;
754021d0
AM
16651
16652 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 16653 if (!bfd_link_relocatable (info))
754021d0
AM
16654 value -= input_sec->output_section->vma;
16655
51aecdc5 16656 adjust = opd->adjust[OPD_NDX (value)];
4025353c 16657 if (adjust == -1)
6e0b88f1
AM
16658 return 2;
16659
16660 elfsym->st_value += adjust;
16661 return 1;
754021d0
AM
16662}
16663
5bd4f169
AM
16664/* Finish up dynamic symbol handling. We set the contents of various
16665 dynamic sections here. */
16666
b34976b6 16667static bfd_boolean
4ce794b7
AM
16668ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16669 struct bfd_link_info *info,
16670 struct elf_link_hash_entry *h,
4aef7643 16671 Elf_Internal_Sym *sym)
5bd4f169 16672{
65f38f15 16673 struct ppc_link_hash_table *htab;
8387904d 16674 struct plt_entry *ent;
5bd4f169 16675
65f38f15 16676 htab = ppc_hash_table (info);
4dfe6ac6
NC
16677 if (htab == NULL)
16678 return FALSE;
5bd4f169 16679
49c09209
AM
16680 if (!htab->opd_abi && !h->def_regular)
16681 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16682 if (ent->plt.offset != (bfd_vma) -1)
16683 {
16684 /* Mark the symbol as undefined, rather than as
16685 defined in glink. Leave the value if there were
16686 any relocations where pointer equality matters
16687 (this is a clue for the dynamic linker, to make
16688 function pointer comparisons work between an
16689 application and shared library), otherwise set it
16690 to zero. */
16691 sym->st_shndx = SHN_UNDEF;
16692 if (!h->pointer_equality_needed)
16693 sym->st_value = 0;
16694 else if (!h->ref_regular_nonweak)
16695 {
16696 /* This breaks function pointer comparisons, but
16697 that is better than breaking tests for a NULL
16698 function pointer. */
16699 sym->st_value = 0;
16700 }
16701 break;
16702 }
5bd4f169 16703
f5385ebf 16704 if (h->needs_copy)
5bd4f169 16705 {
65f38f15 16706 /* This symbol needs a copy reloc. Set it up. */
49c09209 16707 Elf_Internal_Rela rela;
5474d94f 16708 asection *srel;
49c09209 16709 bfd_byte *loc;
5bd4f169 16710
65f38f15
AM
16711 if (h->dynindx == -1
16712 || (h->root.type != bfd_link_hash_defined
16713 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
16714 || htab->elf.srelbss == NULL
16715 || htab->elf.sreldynrelro == NULL)
65f38f15 16716 abort ();
5bd4f169
AM
16717
16718 rela.r_offset = (h->root.u.def.value
16719 + h->root.u.def.section->output_section->vma
16720 + h->root.u.def.section->output_offset);
16721 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16722 rela.r_addend = 0;
afbf7e8e 16723 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
16724 srel = htab->elf.sreldynrelro;
16725 else
16726 srel = htab->elf.srelbss;
16727 loc = srel->contents;
16728 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 16729 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
16730 }
16731
b34976b6 16732 return TRUE;
5bd4f169
AM
16733}
16734
65f38f15
AM
16735/* Used to decide how to sort relocs in an optimal manner for the
16736 dynamic linker, before writing them out. */
16737
16738static enum elf_reloc_type_class
7e612e98
AM
16739ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16740 const asection *rel_sec,
16741 const Elf_Internal_Rela *rela)
65f38f15 16742{
04c9666a 16743 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
16744 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16745
33e44f2e 16746 if (rel_sec == htab->elf.irelplt)
7e612e98 16747 return reloc_class_ifunc;
a33d1f77 16748
4ce794b7 16749 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 16750 switch (r_type)
65f38f15
AM
16751 {
16752 case R_PPC64_RELATIVE:
16753 return reloc_class_relative;
16754 case R_PPC64_JMP_SLOT:
16755 return reloc_class_plt;
16756 case R_PPC64_COPY:
16757 return reloc_class_copy;
16758 default:
16759 return reloc_class_normal;
16760 }
16761}
16762
5bd4f169
AM
16763/* Finish up the dynamic sections. */
16764
b34976b6 16765static bfd_boolean
4ce794b7
AM
16766ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16767 struct bfd_link_info *info)
5bd4f169 16768{
65f38f15
AM
16769 struct ppc_link_hash_table *htab;
16770 bfd *dynobj;
5bd4f169 16771 asection *sdyn;
5bd4f169 16772
65f38f15 16773 htab = ppc_hash_table (info);
4dfe6ac6
NC
16774 if (htab == NULL)
16775 return FALSE;
16776
65f38f15 16777 dynobj = htab->elf.dynobj;
3d4d4302 16778 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 16779
65f38f15 16780 if (htab->elf.dynamic_sections_created)
5bd4f169 16781 {
5bd4f169
AM
16782 Elf64_External_Dyn *dyncon, *dynconend;
16783
33e44f2e 16784 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 16785 abort ();
5bd4f169
AM
16786
16787 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 16788 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
16789 for (; dyncon < dynconend; dyncon++)
16790 {
16791 Elf_Internal_Dyn dyn;
19397422 16792 asection *s;
5bd4f169
AM
16793
16794 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16795
16796 switch (dyn.d_tag)
16797 {
65f38f15
AM
16798 default:
16799 continue;
5bd4f169 16800
5d1634d7 16801 case DT_PPC64_GLINK:
4ce794b7 16802 s = htab->glink;
6348e046 16803 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
16804 /* We stupidly defined DT_PPC64_GLINK to be the start
16805 of glink rather than the first entry point, which is
16806 what ld.so needs, and now have a bigger stub to
16807 support automatic multiple TOCs. */
9e390558 16808 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
16809 break;
16810
19397422
AM
16811 case DT_PPC64_OPD:
16812 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16813 if (s == NULL)
16814 continue;
16815 dyn.d_un.d_ptr = s->vma;
19397422
AM
16816 break;
16817
e8910a83 16818 case DT_PPC64_OPT:
5663e321
AM
16819 if ((htab->do_multi_toc && htab->multi_toc_needed)
16820 || htab->notoc_plt)
e8910a83 16821 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
16822 if (htab->has_plt_localentry0)
16823 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
16824 break;
16825
19397422
AM
16826 case DT_PPC64_OPDSZ:
16827 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16828 if (s == NULL)
16829 continue;
eea6121a 16830 dyn.d_un.d_val = s->size;
19397422
AM
16831 break;
16832
65f38f15 16833 case DT_PLTGOT:
33e44f2e 16834 s = htab->elf.splt;
6348e046 16835 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
16836 break;
16837
16838 case DT_JMPREL:
33e44f2e 16839 s = htab->elf.srelplt;
6348e046 16840 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 16841 break;
5bd4f169 16842
65f38f15 16843 case DT_PLTRELSZ:
33e44f2e 16844 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 16845 break;
82e66161
AM
16846
16847 case DT_TEXTREL:
16848 if (htab->local_ifunc_resolver)
16849 info->callbacks->einfo
16850 (_("%X%P: text relocations and GNU indirect "
16851 "functions will result in a segfault at runtime\n"));
16852 else if (htab->maybe_local_ifunc_resolver)
16853 info->callbacks->einfo
16854 (_("%P: warning: text relocations and GNU indirect "
16855 "functions may result in a segfault at runtime\n"));
16856 continue;
5bd4f169 16857 }
5bd4f169 16858
65f38f15 16859 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 16860 }
5bd4f169
AM
16861 }
16862
6528b6eb
AM
16863 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16864 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16865 {
16866 /* Fill in the first entry in the global offset table.
16867 We use it to hold the link-time TOCbase. */
16868 bfd_put_64 (output_bfd,
60ee0d4a 16869 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 16870 htab->elf.sgot->contents);
5d1634d7
AM
16871
16872 /* Set .got entry size. */
2cdcc330
AM
16873 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16874 = 8;
5d1634d7
AM
16875 }
16876
6528b6eb
AM
16877 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16878 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16879 {
16880 /* Set .plt entry size. */
33e44f2e 16881 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 16882 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
16883 }
16884
84f5d08e
AM
16885 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16886 brlt ourselves if emitrelocations. */
16887 if (htab->brlt != NULL
16888 && htab->brlt->reloc_count != 0
16889 && !_bfd_elf_link_output_relocs (output_bfd,
16890 htab->brlt,
d4730f92 16891 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
16892 elf_section_data (htab->brlt)->relocs,
16893 NULL))
16894 return FALSE;
16895
176a0d42
AM
16896 if (htab->glink != NULL
16897 && htab->glink->reloc_count != 0
16898 && !_bfd_elf_link_output_relocs (output_bfd,
16899 htab->glink,
d4730f92 16900 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
16901 elf_section_data (htab->glink)->relocs,
16902 NULL))
16903 return FALSE;
16904
da44f4e5 16905
df136d64
AM
16906 if (htab->glink_eh_frame != NULL
16907 && htab->glink_eh_frame->size != 0
16908 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16909 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16910 htab->glink_eh_frame,
16911 htab->glink_eh_frame->contents))
16912 return FALSE;
58d180e8 16913
e717da7e 16914 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
16915 since we didn't add them to DYNOBJ. We know dynobj is the first
16916 bfd. */
c72f2fb2 16917 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
16918 {
16919 asection *s;
7b53ace3 16920
0c8d6e5c 16921 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
16922 continue;
16923
e717da7e
AM
16924 s = ppc64_elf_tdata (dynobj)->got;
16925 if (s != NULL
eea6121a 16926 && s->size != 0
e717da7e
AM
16927 && s->output_section != bfd_abs_section_ptr
16928 && !bfd_set_section_contents (output_bfd, s->output_section,
16929 s->contents, s->output_offset,
eea6121a 16930 s->size))
e717da7e
AM
16931 return FALSE;
16932 s = ppc64_elf_tdata (dynobj)->relgot;
16933 if (s != NULL
eea6121a 16934 && s->size != 0
e717da7e
AM
16935 && s->output_section != bfd_abs_section_ptr
16936 && !bfd_set_section_contents (output_bfd, s->output_section,
16937 s->contents, s->output_offset,
eea6121a 16938 s->size))
e717da7e
AM
16939 return FALSE;
16940 }
f6c52c13 16941
b34976b6 16942 return TRUE;
5bd4f169
AM
16943}
16944
5bd4f169 16945#include "elf64-target.h"
7b8e7dad
AM
16946
16947/* FreeBSD support */
16948
16949#undef TARGET_LITTLE_SYM
16950#undef TARGET_LITTLE_NAME
16951
16952#undef TARGET_BIG_SYM
6d00b590 16953#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
16954#undef TARGET_BIG_NAME
16955#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16956
16957#undef ELF_OSABI
16958#define ELF_OSABI ELFOSABI_FREEBSD
16959
16960#undef elf64_bed
16961#define elf64_bed elf64_powerpc_fbsd_bed
16962
16963#include "elf64-target.h"
This page took 4.031307 seconds and 4 git commands to generate.