PR24955, libbfd terminating program on out of memory
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
82704155 2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
5bd4f169
AM
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 5 Largely rewritten by Alan Modra.
5bd4f169 6
ae9a127f 7 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 8
ae9a127f
NC
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
cd123cb7 11 the Free Software Foundation; either version 3 of the License, or
ae9a127f 12 (at your option) any later version.
5bd4f169 13
ae9a127f
NC
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
5bd4f169 18
4ce794b7
AM
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 22
cd123cb7 23
4ce794b7
AM
24/* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 27
3db64b00 28#include "sysdep.h"
183e98be 29#include <stdarg.h>
5bd4f169 30#include "bfd.h"
5bd4f169
AM
31#include "bfdlink.h"
32#include "libbfd.h"
33#include "elf-bfd.h"
04c9666a 34#include "elf/ppc64.h"
5d1634d7 35#include "elf64-ppc.h"
58d180e8 36#include "dwarf2.h"
5bd4f169 37
805fc799 38static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
40static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 42static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 44static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 46static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 48static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
5663e321
AM
54static bfd_reloc_status_type ppc64_elf_prefix_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 56static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 58static bfd_vma opd_entry_value
aef36ac1 59 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
5bd4f169 60
6d00b590 61#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
ad8e1ba5 62#define TARGET_LITTLE_NAME "elf64-powerpcle"
6d00b590 63#define TARGET_BIG_SYM powerpc_elf64_vec
ad8e1ba5
AM
64#define TARGET_BIG_NAME "elf64-powerpc"
65#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 66#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
67#define ELF_MACHINE_CODE EM_PPC64
68#define ELF_MAXPAGESIZE 0x10000
702d1671
AM
69#define ELF_COMMONPAGESIZE 0x1000
70#define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
ad8e1ba5
AM
71#define elf_info_to_howto ppc64_elf_info_to_howto
72
73#define elf_backend_want_got_sym 0
74#define elf_backend_want_plt_sym 0
75#define elf_backend_plt_alignment 3
76#define elf_backend_plt_not_loaded 1
ad8e1ba5 77#define elf_backend_got_header_size 8
5474d94f 78#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
79#define elf_backend_can_gc_sections 1
80#define elf_backend_can_refcount 1
81#define elf_backend_rela_normal 1
64f52338 82#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 83#define elf_backend_default_execstack 0
ad8e1ba5 84
e717da7e 85#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 86#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 87#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
88#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
89#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
90#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
91#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 92#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 93#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 94#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
95
96#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
97#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
98#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 99#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 100#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 101#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 102#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 103#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 104#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 105#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 106#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 107#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 108#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 109#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 110#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
111#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
112#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 113#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
ad8e1ba5
AM
114#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
115#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 116#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 117#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 118#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
119#define elf_backend_relocate_section ppc64_elf_relocate_section
120#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
121#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
122#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 123#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 124#define elf_backend_special_sections ppc64_elf_special_sections
6911b7dc 125#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 126#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 127#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 128
5bd4f169
AM
129/* The name of the dynamic interpreter. This is put in the .interp
130 section. */
131#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
132
133/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 134#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
2d7ad24e 135#define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
5bd4f169
AM
136
137/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 138#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 139
a078d95a
AM
140/* Offsets to some stack save slots. */
141#define STK_LR 16
142#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 143/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
144 CR save slot. Used only by optimised __tls_get_addr call stub,
145 relying on __tls_get_addr_opt not saving CR.. */
146#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
147
5bd4f169 148/* TOC base pointers offset from start of TOC. */
411e1bfb 149#define TOC_BASE_OFF 0x8000
a27e685f
AM
150/* TOC base alignment. */
151#define TOC_BASE_ALIGN 256
411e1bfb
AM
152
153/* Offset of tp and dtp pointers from start of TLS block. */
154#define TP_OFFSET 0x7000
155#define DTP_OFFSET 0x8000
5bd4f169 156
ad8e1ba5
AM
157/* .plt call stub instructions. The normal stub is like this, but
158 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 159 insert an addi to adjust r11. */
a078d95a 160#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
161#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
162#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
163#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
164#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
165#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
166#define BCTR 0x4e800420 /* bctr */
167
07d6d2b8 168#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
05d0e962
AM
169#define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
170#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
07d6d2b8
AM
171#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
172#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
173
174#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
175#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
176#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
177#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
178#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
179#define BNECTR 0x4ca20420 /* bnectr+ */
180#define BNECTR_P4 0x4ce20420 /* bnectr+ */
794e51c0 181
71a39c98 182#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 183#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 184#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 185
07d6d2b8
AM
186#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
187#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
188#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 189
04bdff6a 190#define LI_R11_0 0x39600000 /* li %r11,0 */
07d6d2b8 191#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
04bdff6a 192#define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
05d0e962 193#define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
006589cf
AM
194#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
195#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
05d0e962 196#define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
a345bc8d 197#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
05d0e962 198#define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
04bdff6a 199#define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
05d0e962 200#define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
07d6d2b8 201#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
04bdff6a 202#define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
05d0e962
AM
203#define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
204#define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
205#define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
04bdff6a
AM
206#define PADDI_R12_PC 0x0610000039800000ULL
207#define PLD_R12_PC 0x04100000e5800000ULL
5663e321 208#define PNOP 0x0700000000000000ULL
a345bc8d 209
9e390558
AM
210/* __glink_PLTresolve stub instructions. We enter with the index in R0. */
211#define GLINK_PLTRESOLVE_SIZE(htab) \
407aa07c 212 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
ee4bf8d2
AM
213 /* 0: */
214 /* .quad plt0-1f */
215 /* __glink: */
216#define MFLR_R12 0x7d8802a6 /* mflr %12 */
217#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
218 /* 1: */
219#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 220 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 221#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
222#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
223 /* ld %12,0(%11) */
224 /* ld %2,8(%11) */
225 /* mtctr %12 */
226 /* ld %11,16(%11) */
ee4bf8d2 227 /* bctr */
b9e5796b
AM
228#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
229#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
230#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
231#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
232#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
233
234/* Pad with this. */
235#define NOP 0x60000000
236
721956f4
AM
237/* Some other nops. */
238#define CROR_151515 0x4def7b82
239#define CROR_313131 0x4ffffb82
240
cedb70c5 241/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
242#define LI_R0_0 0x38000000 /* li %r0,0 */
243#define B_DOT 0x48000000 /* b . */
244
245/* After that, we need two instructions to load the index, followed by
246 a branch. */
247#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 248#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 249
deb0e272
AM
250/* Instructions used by the save and restore reg functions. */
251#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
252#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
253#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
254#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
255#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
256#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
257#define LI_R12_0 0x39800000 /* li %r12,0 */
258#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
259#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
260#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
261#define BLR 0x4e800020 /* blr */
262
41bd81ab
AM
263/* Since .opd is an array of descriptors and each entry will end up
264 with identical R_PPC64_RELATIVE relocs, there is really no need to
265 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 266 relocate .opd without reloc entries. */
41bd81ab
AM
267#ifndef NO_OPD_RELOCS
268#define NO_OPD_RELOCS 0
269#endif
810d4e75 270
a4b6fadd
AM
271#ifndef ARRAY_SIZE
272#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
273#endif
274
810d4e75
AM
275static inline int
276abiversion (bfd *abfd)
277{
278 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
279}
280
281static inline void
282set_abiversion (bfd *abfd, int ver)
283{
284 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
285 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
286}
5bd4f169
AM
287\f
288/* Relocation HOWTO's. */
46807bf4
AM
289/* Like other ELF RELA targets that don't apply multiple
290 field-altering relocations to the same localation, src_mask is
291 always zero and pcrel_offset is the same as pc_relative.
292 PowerPC can always use a zero bitpos, even when the field is not at
293 the LSB. For example, a REL24 could use rightshift=2, bisize=24
294 and bitpos=2 which matches the ABI description, or as we do here,
295 rightshift=0, bitsize=26 and bitpos=0. */
296#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
297 complain, special_func) \
298 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
299 complain_overflow_ ## complain, special_func, \
300 #type, FALSE, 0, mask, pc_relative)
301
04c9666a 302static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 303
f3185997
NC
304static reloc_howto_type ppc64_elf_howto_raw[] =
305{
5bd4f169 306 /* This reloc does nothing. */
46807bf4
AM
307 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
308 bfd_elf_generic_reloc),
5bd4f169
AM
309
310 /* A standard 32 bit relocation. */
46807bf4
AM
311 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
312 bfd_elf_generic_reloc),
5bd4f169
AM
313
314 /* An absolute 26 bit branch; the lower two bits must be zero.
315 FIXME: we don't check that, we just clear them. */
46807bf4
AM
316 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
317 bfd_elf_generic_reloc),
5bd4f169
AM
318
319 /* A standard 16 bit relocation. */
46807bf4
AM
320 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
321 bfd_elf_generic_reloc),
5bd4f169
AM
322
323 /* A 16 bit relocation without overflow. */
46807bf4
AM
324 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
325 bfd_elf_generic_reloc),
5bd4f169
AM
326
327 /* Bits 16-31 of an address. */
46807bf4
AM
328 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
329 bfd_elf_generic_reloc),
5bd4f169
AM
330
331 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
332 bits, treated as a signed number, is negative. */
46807bf4
AM
333 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
334 ppc64_elf_ha_reloc),
5bd4f169
AM
335
336 /* An absolute 16 bit branch; the lower two bits must be zero.
337 FIXME: we don't check that, we just clear them. */
46807bf4
AM
338 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
339 ppc64_elf_branch_reloc),
5bd4f169
AM
340
341 /* An absolute 16 bit branch, for which bit 10 should be set to
342 indicate that the branch is expected to be taken. The lower two
343 bits must be zero. */
46807bf4
AM
344 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
345 ppc64_elf_brtaken_reloc),
5bd4f169
AM
346
347 /* An absolute 16 bit branch, for which bit 10 should be set to
348 indicate that the branch is not expected to be taken. The lower
349 two bits must be zero. */
46807bf4
AM
350 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
351 ppc64_elf_brtaken_reloc),
5bd4f169
AM
352
353 /* A relative 26 bit branch; the lower two bits must be zero. */
46807bf4
AM
354 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
355 ppc64_elf_branch_reloc),
5bd4f169 356
05d0e962 357 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
46807bf4
AM
358 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
359 ppc64_elf_branch_reloc),
05d0e962 360
5bd4f169 361 /* A relative 16 bit branch; the lower two bits must be zero. */
46807bf4
AM
362 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
363 ppc64_elf_branch_reloc),
5bd4f169
AM
364
365 /* A relative 16 bit branch. Bit 10 should be set to indicate that
366 the branch is expected to be taken. The lower two bits must be
367 zero. */
46807bf4
AM
368 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
369 ppc64_elf_brtaken_reloc),
5bd4f169
AM
370
371 /* A relative 16 bit branch. Bit 10 should be set to indicate that
372 the branch is not expected to be taken. The lower two bits must
373 be zero. */
46807bf4
AM
374 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
375 ppc64_elf_brtaken_reloc),
5bd4f169
AM
376
377 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
378 symbol. */
46807bf4
AM
379 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
380 ppc64_elf_unhandled_reloc),
5bd4f169
AM
381
382 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
383 the symbol. */
46807bf4
AM
384 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
385 ppc64_elf_unhandled_reloc),
5bd4f169
AM
386
387 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
388 the symbol. */
46807bf4
AM
389 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
390 ppc64_elf_unhandled_reloc),
5bd4f169
AM
391
392 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
393 the symbol. */
46807bf4
AM
394 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
395 ppc64_elf_unhandled_reloc),
5bd4f169
AM
396
397 /* This is used only by the dynamic linker. The symbol should exist
398 both in the object being run and in some shared library. The
399 dynamic linker copies the data addressed by the symbol from the
400 shared library into the object, because the object being
401 run has to have the data at some particular address. */
46807bf4
AM
402 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
403 ppc64_elf_unhandled_reloc),
5bd4f169
AM
404
405 /* Like R_PPC64_ADDR64, but used when setting global offset table
406 entries. */
46807bf4
AM
407 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
408 ppc64_elf_unhandled_reloc),
5bd4f169
AM
409
410 /* Created by the link editor. Marks a procedure linkage table
411 entry for a symbol. */
46807bf4
AM
412 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
413 ppc64_elf_unhandled_reloc),
5bd4f169
AM
414
415 /* Used only by the dynamic linker. When the object is run, this
416 doubleword64 is set to the load address of the object, plus the
417 addend. */
46807bf4
AM
418 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
419 bfd_elf_generic_reloc),
5bd4f169
AM
420
421 /* Like R_PPC64_ADDR32, but may be unaligned. */
46807bf4
AM
422 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
423 bfd_elf_generic_reloc),
5bd4f169
AM
424
425 /* Like R_PPC64_ADDR16, but may be unaligned. */
46807bf4
AM
426 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
427 bfd_elf_generic_reloc),
5bd4f169
AM
428
429 /* 32-bit PC relative. */
46807bf4
AM
430 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
431 bfd_elf_generic_reloc),
5bd4f169 432
10ed1bba 433 /* 32-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
434 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
435 ppc64_elf_unhandled_reloc),
5bd4f169
AM
436
437 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
438 FIXME: R_PPC64_PLTREL32 not supported. */
46807bf4
AM
439 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
440 ppc64_elf_unhandled_reloc),
5bd4f169
AM
441
442 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
443 the symbol. */
46807bf4
AM
444 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
445 ppc64_elf_unhandled_reloc),
5bd4f169
AM
446
447 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
448 the symbol. */
46807bf4
AM
449 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
450 ppc64_elf_unhandled_reloc),
5bd4f169
AM
451
452 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
453 the symbol. */
46807bf4
AM
454 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
455 ppc64_elf_unhandled_reloc),
5bd4f169 456
c061c2d8 457 /* 16-bit section relative relocation. */
46807bf4
AM
458 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
459 ppc64_elf_sectoff_reloc),
5bd4f169 460
c061c2d8 461 /* Like R_PPC64_SECTOFF, but no overflow warning. */
46807bf4
AM
462 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
463 ppc64_elf_sectoff_reloc),
5bd4f169
AM
464
465 /* 16-bit upper half section relative relocation. */
46807bf4
AM
466 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
467 ppc64_elf_sectoff_reloc),
5bd4f169
AM
468
469 /* 16-bit upper half adjusted section relative relocation. */
46807bf4
AM
470 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
471 ppc64_elf_sectoff_ha_reloc),
5bd4f169 472
04c9666a 473 /* Like R_PPC64_REL24 without touching the two least significant bits. */
46807bf4
AM
474 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
475 bfd_elf_generic_reloc),
5bd4f169
AM
476
477 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
478
479 /* A standard 64-bit relocation. */
46807bf4
AM
480 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
481 bfd_elf_generic_reloc),
5bd4f169
AM
482
483 /* The bits 32-47 of an address. */
46807bf4
AM
484 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
485 bfd_elf_generic_reloc),
5bd4f169
AM
486
487 /* The bits 32-47 of an address, plus 1 if the contents of the low
488 16 bits, treated as a signed number, is negative. */
46807bf4
AM
489 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
490 ppc64_elf_ha_reloc),
5bd4f169
AM
491
492 /* The bits 48-63 of an address. */
46807bf4
AM
493 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
494 bfd_elf_generic_reloc),
5bd4f169
AM
495
496 /* The bits 48-63 of an address, plus 1 if the contents of the low
497 16 bits, treated as a signed number, is negative. */
46807bf4
AM
498 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
499 ppc64_elf_ha_reloc),
5bd4f169
AM
500
501 /* Like ADDR64, but may be unaligned. */
46807bf4
AM
502 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
503 bfd_elf_generic_reloc),
5bd4f169
AM
504
505 /* 64-bit relative relocation. */
46807bf4
AM
506 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
507 bfd_elf_generic_reloc),
5bd4f169 508
cedb70c5 509 /* 64-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
510 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
511 ppc64_elf_unhandled_reloc),
5bd4f169
AM
512
513 /* 64-bit PC relative relocation to the symbol's procedure linkage
514 table. */
515 /* FIXME: R_PPC64_PLTREL64 not supported. */
46807bf4
AM
516 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
517 ppc64_elf_unhandled_reloc),
5bd4f169
AM
518
519 /* 16 bit TOC-relative relocation. */
5bd4f169 520 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
46807bf4
AM
521 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
522 ppc64_elf_toc_reloc),
5bd4f169
AM
523
524 /* 16 bit TOC-relative relocation without overflow. */
5bd4f169 525 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
46807bf4
AM
526 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
527 ppc64_elf_toc_reloc),
5bd4f169
AM
528
529 /* 16 bit TOC-relative relocation, high 16 bits. */
5bd4f169 530 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
46807bf4
AM
531 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
532 ppc64_elf_toc_reloc),
5bd4f169
AM
533
534 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
535 contents of the low 16 bits, treated as a signed number, is
536 negative. */
5bd4f169 537 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
46807bf4
AM
538 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
539 ppc64_elf_toc_ha_reloc),
5bd4f169
AM
540
541 /* 64-bit relocation; insert value of TOC base (.TOC.). */
5bd4f169 542 /* R_PPC64_TOC 51 doubleword64 .TOC. */
46807bf4
AM
543 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
544 ppc64_elf_toc64_reloc),
5bd4f169
AM
545
546 /* Like R_PPC64_GOT16, but also informs the link editor that the
547 value to relocate may (!) refer to a PLT entry which the link
548 editor (a) may replace with the symbol value. If the link editor
549 is unable to fully resolve the symbol, it may (b) create a PLT
550 entry and store the address to the new PLT entry in the GOT.
551 This permits lazy resolution of function symbols at run time.
552 The link editor may also skip all of this and just (c) emit a
553 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
554 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
46807bf4
AM
555 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
556 ppc64_elf_unhandled_reloc),
411e1bfb
AM
557
558 /* Like R_PPC64_PLTGOT16, but without overflow. */
559 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
560 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
561 ppc64_elf_unhandled_reloc),
411e1bfb
AM
562
563 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
564 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
46807bf4
AM
565 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
566 ppc64_elf_unhandled_reloc),
411e1bfb
AM
567
568 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
569 1 if the contents of the low 16 bits, treated as a signed number,
570 is negative. */
571 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
46807bf4
AM
572 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
573 ppc64_elf_unhandled_reloc),
411e1bfb
AM
574
575 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
46807bf4
AM
576 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
577 bfd_elf_generic_reloc),
411e1bfb
AM
578
579 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
46807bf4
AM
580 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
581 bfd_elf_generic_reloc),
411e1bfb
AM
582
583 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
46807bf4
AM
584 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
585 ppc64_elf_unhandled_reloc),
411e1bfb
AM
586
587 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
46807bf4
AM
588 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
589 ppc64_elf_unhandled_reloc),
411e1bfb
AM
590
591 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
46807bf4
AM
592 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
593 ppc64_elf_unhandled_reloc),
411e1bfb
AM
594
595 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
46807bf4
AM
596 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
597 ppc64_elf_sectoff_reloc),
411e1bfb
AM
598
599 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
46807bf4
AM
600 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
601 ppc64_elf_sectoff_reloc),
411e1bfb
AM
602
603 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
46807bf4
AM
604 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
605 ppc64_elf_toc_reloc),
411e1bfb
AM
606
607 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
46807bf4
AM
608 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
609 ppc64_elf_toc_reloc),
411e1bfb
AM
610
611 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
612 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
46807bf4
AM
613 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
614 ppc64_elf_unhandled_reloc),
411e1bfb
AM
615
616 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
617 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
618 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
619 ppc64_elf_unhandled_reloc),
411e1bfb 620
727fc41e 621 /* Marker relocs for TLS. */
46807bf4
AM
622 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
623 bfd_elf_generic_reloc),
624
625 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
626 bfd_elf_generic_reloc),
627
628 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
629 bfd_elf_generic_reloc),
727fc41e 630
23cedd1d
AM
631 /* Marker reloc for optimizing r2 save in prologue rather than on
632 each plt call stub. */
46807bf4
AM
633 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
634 bfd_elf_generic_reloc),
3b421ab3 635
23cedd1d 636 /* Marker relocs on inline plt call instructions. */
46807bf4
AM
637 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
638 bfd_elf_generic_reloc),
639
640 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
641 bfd_elf_generic_reloc),
23cedd1d 642
411e1bfb
AM
643 /* Computes the load module index of the load module that contains the
644 definition of its TLS sym. */
46807bf4
AM
645 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
646 ppc64_elf_unhandled_reloc),
411e1bfb
AM
647
648 /* Computes a dtv-relative displacement, the difference between the value
649 of sym+add and the base address of the thread-local storage block that
650 contains the definition of sym, minus 0x8000. */
46807bf4
AM
651 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
652 ppc64_elf_unhandled_reloc),
411e1bfb
AM
653
654 /* A 16 bit dtprel reloc. */
46807bf4
AM
655 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
656 ppc64_elf_unhandled_reloc),
411e1bfb
AM
657
658 /* Like DTPREL16, but no overflow. */
46807bf4
AM
659 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
660 ppc64_elf_unhandled_reloc),
411e1bfb
AM
661
662 /* Like DTPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
663 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
664 ppc64_elf_unhandled_reloc),
411e1bfb
AM
665
666 /* Like DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
667 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
668 ppc64_elf_unhandled_reloc),
411e1bfb
AM
669
670 /* Like DTPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
671 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
672 ppc64_elf_unhandled_reloc),
411e1bfb
AM
673
674 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
675 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
676 ppc64_elf_unhandled_reloc),
411e1bfb
AM
677
678 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
679 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
680 ppc64_elf_unhandled_reloc),
411e1bfb
AM
681
682 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
683 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
684 ppc64_elf_unhandled_reloc),
411e1bfb
AM
685
686 /* Like DTPREL16, but for insns with a DS field. */
46807bf4
AM
687 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
688 ppc64_elf_unhandled_reloc),
411e1bfb
AM
689
690 /* Like DTPREL16_DS, but no overflow. */
46807bf4
AM
691 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
692 ppc64_elf_unhandled_reloc),
411e1bfb
AM
693
694 /* Computes a tp-relative displacement, the difference between the value of
695 sym+add and the value of the thread pointer (r13). */
46807bf4
AM
696 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
697 ppc64_elf_unhandled_reloc),
411e1bfb
AM
698
699 /* A 16 bit tprel reloc. */
46807bf4
AM
700 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
701 ppc64_elf_unhandled_reloc),
411e1bfb
AM
702
703 /* Like TPREL16, but no overflow. */
46807bf4
AM
704 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
705 ppc64_elf_unhandled_reloc),
411e1bfb
AM
706
707 /* Like TPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
708 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
709 ppc64_elf_unhandled_reloc),
411e1bfb
AM
710
711 /* Like TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
712 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
713 ppc64_elf_unhandled_reloc),
411e1bfb
AM
714
715 /* Like TPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
716 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
717 ppc64_elf_unhandled_reloc),
411e1bfb
AM
718
719 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
720 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
721 ppc64_elf_unhandled_reloc),
411e1bfb
AM
722
723 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
724 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
725 ppc64_elf_unhandled_reloc),
411e1bfb
AM
726
727 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
728 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
729 ppc64_elf_unhandled_reloc),
411e1bfb
AM
730
731 /* Like TPREL16, but for insns with a DS field. */
46807bf4
AM
732 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
733 ppc64_elf_unhandled_reloc),
411e1bfb
AM
734
735 /* Like TPREL16_DS, but no overflow. */
46807bf4
AM
736 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
737 ppc64_elf_unhandled_reloc),
411e1bfb
AM
738
739 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
740 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
741 to the first entry relative to the TOC base (r2). */
46807bf4
AM
742 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
743 ppc64_elf_unhandled_reloc),
5bd4f169 744
411e1bfb 745 /* Like GOT_TLSGD16, but no overflow. */
46807bf4
AM
746 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
747 ppc64_elf_unhandled_reloc),
5bd4f169 748
411e1bfb 749 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
46807bf4
AM
750 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
751 ppc64_elf_unhandled_reloc),
5bd4f169 752
411e1bfb 753 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
46807bf4
AM
754 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
755 ppc64_elf_unhandled_reloc),
5bd4f169 756
411e1bfb
AM
757 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
758 with values (sym+add)@dtpmod and zero, and computes the offset to the
759 first entry relative to the TOC base (r2). */
46807bf4
AM
760 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
761 ppc64_elf_unhandled_reloc),
5bd4f169 762
411e1bfb 763 /* Like GOT_TLSLD16, but no overflow. */
46807bf4
AM
764 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
765 ppc64_elf_unhandled_reloc),
5bd4f169 766
411e1bfb 767 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
46807bf4
AM
768 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
769 ppc64_elf_unhandled_reloc),
5bd4f169 770
411e1bfb 771 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
46807bf4
AM
772 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
773 ppc64_elf_unhandled_reloc),
5bd4f169 774
411e1bfb
AM
775 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
776 the offset to the entry relative to the TOC base (r2). */
46807bf4
AM
777 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
778 ppc64_elf_unhandled_reloc),
5bd4f169 779
411e1bfb 780 /* Like GOT_DTPREL16_DS, but no overflow. */
46807bf4
AM
781 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
782 ppc64_elf_unhandled_reloc),
5bd4f169 783
411e1bfb 784 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
785 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
786 ppc64_elf_unhandled_reloc),
5bd4f169 787
411e1bfb 788 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
789 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
790 ppc64_elf_unhandled_reloc),
411e1bfb
AM
791
792 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
793 offset to the entry relative to the TOC base (r2). */
46807bf4
AM
794 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
795 ppc64_elf_unhandled_reloc),
5bd4f169 796
411e1bfb 797 /* Like GOT_TPREL16_DS, but no overflow. */
46807bf4
AM
798 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
799 ppc64_elf_unhandled_reloc),
5bd4f169 800
411e1bfb 801 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
802 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
803 ppc64_elf_unhandled_reloc),
5bd4f169 804
411e1bfb 805 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
806 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
807 ppc64_elf_unhandled_reloc),
808
809 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
810 ppc64_elf_unhandled_reloc),
811
812 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
813 bfd_elf_generic_reloc),
e054468f 814
25f23106 815 /* A 16 bit relative relocation. */
46807bf4
AM
816 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
817 bfd_elf_generic_reloc),
25f23106
AM
818
819 /* A 16 bit relative relocation without overflow. */
46807bf4
AM
820 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
821 bfd_elf_generic_reloc),
25f23106
AM
822
823 /* The high order 16 bits of a relative address. */
46807bf4
AM
824 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
825 bfd_elf_generic_reloc),
25f23106
AM
826
827 /* The high order 16 bits of a relative address, plus 1 if the contents of
828 the low 16 bits, treated as a signed number, is negative. */
46807bf4
AM
829 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
830 ppc64_elf_ha_reloc),
25f23106 831
4a969973
AM
832 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
833 bfd_elf_generic_reloc),
834
835 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
836 ppc64_elf_ha_reloc),
837
838 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
839 bfd_elf_generic_reloc),
840
841 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
842 ppc64_elf_ha_reloc),
843
844 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
845 bfd_elf_generic_reloc),
846
847 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
848 ppc64_elf_ha_reloc),
849
a680de9a 850 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
46807bf4
AM
851 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
852 ppc64_elf_ha_reloc),
a680de9a 853
7ba71655 854 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
46807bf4
AM
855 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
856 ppc64_elf_ha_reloc),
7ba71655 857
f9c6b907 858 /* Like R_PPC64_ADDR16_HI, but no overflow. */
46807bf4
AM
859 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
860 bfd_elf_generic_reloc),
f9c6b907
AM
861
862 /* Like R_PPC64_ADDR16_HA, but no overflow. */
46807bf4
AM
863 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
864 ppc64_elf_ha_reloc),
f9c6b907
AM
865
866 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
46807bf4
AM
867 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
868 ppc64_elf_unhandled_reloc),
f9c6b907
AM
869
870 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
46807bf4
AM
871 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
872 ppc64_elf_unhandled_reloc),
f9c6b907
AM
873
874 /* Like R_PPC64_TPREL16_HI, but no overflow. */
46807bf4
AM
875 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
876 ppc64_elf_unhandled_reloc),
f9c6b907
AM
877
878 /* Like R_PPC64_TPREL16_HA, but no overflow. */
46807bf4
AM
879 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
880 ppc64_elf_unhandled_reloc),
f9c6b907 881
006589cf 882 /* Marker reloc on ELFv2 large-model function entry. */
46807bf4
AM
883 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
884 bfd_elf_generic_reloc),
006589cf 885
45965137 886 /* Like ADDR64, but use local entry point of function. */
46807bf4
AM
887 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
888 bfd_elf_generic_reloc),
45965137 889
5663e321
AM
890 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, FALSE, dont,
891 bfd_elf_generic_reloc),
892
893 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, FALSE, dont,
894 bfd_elf_generic_reloc),
895
896 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, FALSE, dont,
897 bfd_elf_generic_reloc),
898
899 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
900 ppc64_elf_prefix_reloc),
901
902 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, dont,
903 ppc64_elf_prefix_reloc),
904
905 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
906 ppc64_elf_prefix_reloc),
907
908 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
909 ppc64_elf_prefix_reloc),
910
911 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
912 ppc64_elf_prefix_reloc),
913
914 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
915 ppc64_elf_unhandled_reloc),
916
917 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
918 ppc64_elf_unhandled_reloc),
919
920 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
921 ppc64_elf_unhandled_reloc),
922
c213164a
AM
923 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
924 ppc64_elf_unhandled_reloc),
925
926 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
927 ppc64_elf_unhandled_reloc),
928
929 HOW (R_PPC64_GOT_TLSGD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
930 ppc64_elf_unhandled_reloc),
931
932 HOW (R_PPC64_GOT_TLSLD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
933 ppc64_elf_unhandled_reloc),
934
935 HOW (R_PPC64_GOT_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
936 ppc64_elf_unhandled_reloc),
937
938 HOW (R_PPC64_GOT_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
939 ppc64_elf_unhandled_reloc),
940
5663e321
AM
941 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
942 bfd_elf_generic_reloc),
943
944 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
945 ppc64_elf_ha_reloc),
946
947 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
948 bfd_elf_generic_reloc),
949
950 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
951 ppc64_elf_ha_reloc),
952
953 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
954 bfd_elf_generic_reloc),
955
956 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
957 ppc64_elf_ha_reloc),
958
959 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
960 bfd_elf_generic_reloc),
961
962 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
963 ppc64_elf_ha_reloc),
964
965 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
966 ppc64_elf_prefix_reloc),
967
968 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
969 ppc64_elf_prefix_reloc),
970
5bd4f169 971 /* GNU extension to record C++ vtable hierarchy. */
46807bf4
AM
972 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
973 NULL),
5bd4f169
AM
974
975 /* GNU extension to record C++ vtable member usage. */
46807bf4
AM
976 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
977 NULL),
5bd4f169
AM
978};
979
980\f
981/* Initialize the ppc64_elf_howto_table, so that linear accesses can
982 be done. */
983
984static void
4ce794b7 985ppc_howto_init (void)
5bd4f169
AM
986{
987 unsigned int i, type;
988
a4b6fadd 989 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
990 {
991 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 992 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
993 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
994 }
995}
996
997static reloc_howto_type *
f3185997 998ppc64_elf_reloc_type_lookup (bfd *abfd,
4ce794b7 999 bfd_reloc_code_real_type code)
5bd4f169 1000{
411e1bfb 1001 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
1002
1003 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1004 /* Initialize howto table if needed. */
1005 ppc_howto_init ();
1006
4ce794b7 1007 switch (code)
5bd4f169
AM
1008 {
1009 default:
f3185997 1010 /* xgettext:c-format */
2cdcc330
AM
1011 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1012 (int) code);
f3185997 1013 bfd_set_error (bfd_error_bad_value);
4ce794b7 1014 return NULL;
5bd4f169 1015
411e1bfb
AM
1016 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1017 break;
1018 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1019 break;
1020 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1021 break;
1022 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1023 break;
1024 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1025 break;
1026 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1027 break;
f9c6b907
AM
1028 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1029 break;
411e1bfb 1030 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 1031 break;
f9c6b907
AM
1032 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1033 break;
411e1bfb 1034 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 1035 break;
411e1bfb 1036 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 1037 break;
411e1bfb 1038 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 1039 break;
411e1bfb 1040 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 1041 break;
05d0e962
AM
1042 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1043 break;
411e1bfb 1044 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 1045 break;
411e1bfb 1046 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 1047 break;
411e1bfb 1048 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 1049 break;
411e1bfb 1050 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 1051 break;
411e1bfb 1052 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 1053 break;
411e1bfb 1054 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 1055 break;
411e1bfb 1056 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 1057 break;
411e1bfb 1058 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 1059 break;
411e1bfb 1060 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 1061 break;
411e1bfb 1062 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 1063 break;
411e1bfb 1064 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 1065 break;
411e1bfb 1066 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 1067 break;
411e1bfb 1068 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 1069 break;
411e1bfb 1070 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 1071 break;
411e1bfb 1072 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 1073 break;
411e1bfb 1074 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 1075 break;
411e1bfb 1076 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 1077 break;
411e1bfb 1078 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 1079 break;
411e1bfb 1080 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 1081 break;
411e1bfb 1082 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 1083 break;
411e1bfb 1084 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 1085 break;
411e1bfb 1086 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 1087 break;
411e1bfb 1088 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 1089 break;
411e1bfb 1090 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1091 break;
411e1bfb 1092 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1093 break;
411e1bfb 1094 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1095 break;
411e1bfb 1096 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1097 break;
411e1bfb 1098 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1099 break;
411e1bfb 1100 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1101 break;
411e1bfb 1102 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1103 break;
411e1bfb 1104 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1105 break;
411e1bfb 1106 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1107 break;
411e1bfb 1108 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1109 break;
411e1bfb 1110 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1111 break;
411e1bfb 1112 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1113 break;
411e1bfb 1114 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1115 break;
411e1bfb 1116 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1117 break;
411e1bfb 1118 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1119 break;
411e1bfb 1120 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1121 break;
411e1bfb 1122 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1123 break;
411e1bfb 1124 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1125 break;
411e1bfb 1126 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1127 break;
411e1bfb 1128 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1129 break;
411e1bfb 1130 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1131 break;
411e1bfb 1132 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1133 break;
411e1bfb 1134 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1135 break;
411e1bfb 1136 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1137 break;
411e1bfb 1138 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1139 break;
c213164a 1140 case BFD_RELOC_PPC64_TLS_PCREL:
411e1bfb 1141 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1142 break;
727fc41e
AM
1143 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1144 break;
1145 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1146 break;
411e1bfb 1147 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1148 break;
411e1bfb 1149 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1150 break;
411e1bfb 1151 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1152 break;
411e1bfb 1153 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1154 break;
f9c6b907
AM
1155 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1156 break;
411e1bfb 1157 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1158 break;
f9c6b907
AM
1159 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1160 break;
411e1bfb 1161 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1162 break;
411e1bfb
AM
1163 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1164 break;
1165 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1166 break;
1167 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1168 break;
f9c6b907
AM
1169 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1170 break;
411e1bfb
AM
1171 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1172 break;
f9c6b907
AM
1173 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1174 break;
411e1bfb
AM
1175 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1176 break;
1177 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1178 break;
1179 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1180 break;
1181 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1182 break;
1183 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1184 break;
1185 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1186 break;
1187 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1188 break;
1189 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1190 break;
1191 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1192 break;
1193 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1194 break;
1195 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1196 break;
1197 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1198 break;
1199 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1200 break;
1201 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1202 break;
1203 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1204 break;
1205 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1206 break;
1207 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1208 break;
1209 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1210 break;
1211 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1212 break;
1213 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1214 break;
1215 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1216 break;
1217 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1218 break;
1219 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1220 break;
1221 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1222 break;
1223 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1224 break;
1225 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1226 break;
1227 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1228 break;
1229 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1230 break;
1231 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1232 break;
25f23106
AM
1233 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1234 break;
1235 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1236 break;
1237 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1238 break;
1239 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1240 break;
4a969973
AM
1241 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1242 break;
1243 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1244 break;
1245 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1246 break;
1247 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1248 break;
1249 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1250 break;
1251 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1252 break;
7ba71655
AM
1253 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1254 break;
a680de9a
PB
1255 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1256 break;
006589cf
AM
1257 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1258 break;
45965137
AM
1259 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1260 break;
5663e321
AM
1261 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1262 break;
1263 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1264 break;
1265 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1266 break;
1267 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1268 break;
1269 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1270 break;
1271 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1272 break;
1273 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1274 break;
c213164a
AM
1275 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1276 break;
1277 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1278 break;
1279 case BFD_RELOC_PPC64_GOT_TLSGD34: r = R_PPC64_GOT_TLSGD34;
1280 break;
1281 case BFD_RELOC_PPC64_GOT_TLSLD34: r = R_PPC64_GOT_TLSLD34;
1282 break;
1283 case BFD_RELOC_PPC64_GOT_TPREL34: r = R_PPC64_GOT_TPREL34;
1284 break;
1285 case BFD_RELOC_PPC64_GOT_DTPREL34: r = R_PPC64_GOT_DTPREL34;
1286 break;
5663e321
AM
1287 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1288 break;
1289 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1290 break;
1291 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1292 break;
1293 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1294 break;
1295 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1296 break;
1297 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1298 break;
1299 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1300 break;
1301 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1302 break;
1303 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1304 break;
1305 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1306 break;
411e1bfb
AM
1307 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1308 break;
1309 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
1310 break;
1311 }
1312
4ce794b7 1313 return ppc64_elf_howto_table[r];
5bd4f169
AM
1314};
1315
157090f7
AM
1316static reloc_howto_type *
1317ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1318 const char *r_name)
1319{
1320 unsigned int i;
1321
a4b6fadd 1322 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
1323 if (ppc64_elf_howto_raw[i].name != NULL
1324 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1325 return &ppc64_elf_howto_raw[i];
1326
1327 return NULL;
1328}
1329
5bd4f169
AM
1330/* Set the howto pointer for a PowerPC ELF reloc. */
1331
f3185997 1332static bfd_boolean
4aef7643 1333ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 1334 Elf_Internal_Rela *dst)
5bd4f169 1335{
65f38f15
AM
1336 unsigned int type;
1337
ef60b7ff 1338 /* Initialize howto table if needed. */
5bd4f169 1339 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
1340 ppc_howto_init ();
1341
65f38f15 1342 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 1343 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 1344 {
695344c0 1345 /* xgettext:c-format */
0aa13fee 1346 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 1347 abfd, type);
f3185997
NC
1348 bfd_set_error (bfd_error_bad_value);
1349 return FALSE;
d0fb9a8d 1350 }
65f38f15 1351 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
1352 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1353 {
1354 /* xgettext:c-format */
1355 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1356 abfd, type);
1357 bfd_set_error (bfd_error_bad_value);
1358 return FALSE;
1359 }
2cdcc330 1360
f3185997 1361 return TRUE;
5bd4f169
AM
1362}
1363
04c9666a 1364/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
1365
1366static bfd_reloc_status_type
4ce794b7
AM
1367ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1368 void *data, asection *input_section,
1369 bfd *output_bfd, char **error_message)
5bd4f169 1370{
a680de9a
PB
1371 enum elf_ppc64_reloc_type r_type;
1372 long insn;
1373 bfd_size_type octets;
3de43e7b 1374 bfd_vma value;
a680de9a 1375
805fc799
AM
1376 /* If this is a relocatable link (output_bfd test tells us), just
1377 call the generic function. Any adjustment will be done at final
1378 link time. */
1379 if (output_bfd != NULL)
cedb70c5 1380 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1381 input_section, output_bfd, error_message);
1382
5663e321
AM
1383 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1384 We won't actually be using the low bits, so trashing them
805fc799 1385 doesn't matter. */
a680de9a 1386 r_type = reloc_entry->howto->type;
5663e321
AM
1387 if (r_type == R_PPC64_ADDR16_HIGHERA34
1388 || r_type == R_PPC64_ADDR16_HIGHESTA34
1389 || r_type == R_PPC64_REL16_HIGHERA34
1390 || r_type == R_PPC64_REL16_HIGHESTA34)
1391 reloc_entry->addend += 1ULL << 33;
1392 else
1393 reloc_entry->addend += 1U << 15;
a680de9a
PB
1394 if (r_type != R_PPC64_REL16DX_HA)
1395 return bfd_reloc_continue;
1396
1397 value = 0;
1398 if (!bfd_is_com_section (symbol->section))
1399 value = symbol->value;
1400 value += (reloc_entry->addend
1401 + symbol->section->output_offset
1402 + symbol->section->output_section->vma);
1403 value -= (reloc_entry->address
1404 + input_section->output_offset
1405 + input_section->output_section->vma);
3de43e7b 1406 value = (bfd_signed_vma) value >> 16;
a680de9a
PB
1407
1408 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1409 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1410 insn &= ~0x1fffc1;
3de43e7b 1411 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 1412 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 1413 if (value + 0x8000 > 0xffff)
a680de9a
PB
1414 return bfd_reloc_overflow;
1415 return bfd_reloc_ok;
805fc799 1416}
5bd4f169 1417
2441e016
AM
1418static bfd_reloc_status_type
1419ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1420 void *data, asection *input_section,
1421 bfd *output_bfd, char **error_message)
1422{
1423 if (output_bfd != NULL)
1424 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1425 input_section, output_bfd, error_message);
1426
699733f6
AM
1427 if (strcmp (symbol->section->name, ".opd") == 0
1428 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
1429 {
1430 bfd_vma dest = opd_entry_value (symbol->section,
1431 symbol->value + reloc_entry->addend,
aef36ac1 1432 NULL, NULL, FALSE);
2441e016
AM
1433 if (dest != (bfd_vma) -1)
1434 reloc_entry->addend = dest - (symbol->value
1435 + symbol->section->output_section->vma
1436 + symbol->section->output_offset);
1437 }
810d4e75
AM
1438 else
1439 {
1440 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1441
1442 if (symbol->section->owner != abfd
9f284bf9 1443 && symbol->section->owner != NULL
810d4e75
AM
1444 && abiversion (symbol->section->owner) >= 2)
1445 {
1446 unsigned int i;
1447
1448 for (i = 0; i < symbol->section->owner->symcount; ++i)
1449 {
1450 asymbol *symdef = symbol->section->owner->outsymbols[i];
1451
1452 if (strcmp (symdef->name, symbol->name) == 0)
1453 {
1454 elfsym = (elf_symbol_type *) symdef;
1455 break;
1456 }
1457 }
1458 }
1459 reloc_entry->addend
1460 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1461 }
2441e016
AM
1462 return bfd_reloc_continue;
1463}
1464
805fc799 1465static bfd_reloc_status_type
4ce794b7
AM
1466ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1467 void *data, asection *input_section,
1468 bfd *output_bfd, char **error_message)
805fc799
AM
1469{
1470 long insn;
04c9666a 1471 enum elf_ppc64_reloc_type r_type;
805fc799 1472 bfd_size_type octets;
794e51c0
AM
1473 /* Assume 'at' branch hints. */
1474 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
1475
1476 /* If this is a relocatable link (output_bfd test tells us), just
1477 call the generic function. Any adjustment will be done at final
1478 link time. */
5bd4f169 1479 if (output_bfd != NULL)
cedb70c5 1480 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1481 input_section, output_bfd, error_message);
1482
1483 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1484 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1485 insn &= ~(0x01 << 21);
4ce794b7 1486 r_type = reloc_entry->howto->type;
805fc799
AM
1487 if (r_type == R_PPC64_ADDR14_BRTAKEN
1488 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 1489 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 1490
794e51c0 1491 if (is_isa_v2)
5bd4f169 1492 {
805fc799
AM
1493 /* Set 'a' bit. This is 0b00010 in BO field for branch
1494 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1495 for branch on CTR insns (BO == 1a00t or 1a01t). */
1496 if ((insn & (0x14 << 21)) == (0x04 << 21))
1497 insn |= 0x02 << 21;
1498 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1499 insn |= 0x08 << 21;
1500 else
2441e016 1501 goto out;
5bd4f169 1502 }
805fc799
AM
1503 else
1504 {
1505 bfd_vma target = 0;
1506 bfd_vma from;
5bd4f169 1507
805fc799
AM
1508 if (!bfd_is_com_section (symbol->section))
1509 target = symbol->value;
1510 target += symbol->section->output_section->vma;
1511 target += symbol->section->output_offset;
1512 target += reloc_entry->addend;
5bd4f169 1513
805fc799
AM
1514 from = (reloc_entry->address
1515 + input_section->output_offset
1516 + input_section->output_section->vma);
5bd4f169 1517
805fc799
AM
1518 /* Invert 'y' bit if not the default. */
1519 if ((bfd_signed_vma) (target - from) < 0)
1520 insn ^= 0x01 << 21;
1521 }
4ce794b7 1522 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
1523 out:
1524 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1525 input_section, output_bfd, error_message);
805fc799 1526}
5bd4f169 1527
805fc799 1528static bfd_reloc_status_type
4ce794b7
AM
1529ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1530 void *data, asection *input_section,
1531 bfd *output_bfd, char **error_message)
805fc799
AM
1532{
1533 /* If this is a relocatable link (output_bfd test tells us), just
1534 call the generic function. Any adjustment will be done at final
1535 link time. */
1536 if (output_bfd != NULL)
cedb70c5 1537 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 1538 input_section, output_bfd, error_message);
5bd4f169 1539
805fc799
AM
1540 /* Subtract the symbol section base address. */
1541 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
1542 return bfd_reloc_continue;
1543}
1544
805fc799 1545static bfd_reloc_status_type
4ce794b7
AM
1546ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1547 void *data, asection *input_section,
1548 bfd *output_bfd, char **error_message)
805fc799
AM
1549{
1550 /* If this is a relocatable link (output_bfd test tells us), just
1551 call the generic function. Any adjustment will be done at final
1552 link time. */
1553 if (output_bfd != NULL)
cedb70c5 1554 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1555 input_section, output_bfd, error_message);
1556
1557 /* Subtract the symbol section base address. */
1558 reloc_entry->addend -= symbol->section->output_section->vma;
1559
1560 /* Adjust the addend for sign extension of the low 16 bits. */
1561 reloc_entry->addend += 0x8000;
1562 return bfd_reloc_continue;
1563}
1564
1565static bfd_reloc_status_type
4ce794b7
AM
1566ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1567 void *data, asection *input_section,
1568 bfd *output_bfd, char **error_message)
805fc799
AM
1569{
1570 bfd_vma TOCstart;
1571
1572 /* If this is a relocatable link (output_bfd test tells us), just
1573 call the generic function. Any adjustment will be done at final
1574 link time. */
1575 if (output_bfd != NULL)
cedb70c5 1576 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1577 input_section, output_bfd, error_message);
1578
1579 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1580 if (TOCstart == 0)
1c865ab2 1581 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1582
1583 /* Subtract the TOC base address. */
1584 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1585 return bfd_reloc_continue;
1586}
1587
1588static bfd_reloc_status_type
4ce794b7
AM
1589ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1590 void *data, asection *input_section,
1591 bfd *output_bfd, char **error_message)
805fc799
AM
1592{
1593 bfd_vma TOCstart;
1594
1595 /* If this is a relocatable link (output_bfd test tells us), just
1596 call the generic function. Any adjustment will be done at final
1597 link time. */
1598 if (output_bfd != NULL)
cedb70c5 1599 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1600 input_section, output_bfd, error_message);
1601
1602 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1603 if (TOCstart == 0)
1c865ab2 1604 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1605
1606 /* Subtract the TOC base address. */
1607 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1608
1609 /* Adjust the addend for sign extension of the low 16 bits. */
1610 reloc_entry->addend += 0x8000;
1611 return bfd_reloc_continue;
1612}
1613
1614static bfd_reloc_status_type
4ce794b7
AM
1615ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1616 void *data, asection *input_section,
1617 bfd *output_bfd, char **error_message)
805fc799
AM
1618{
1619 bfd_vma TOCstart;
1620 bfd_size_type octets;
1621
1622 /* If this is a relocatable link (output_bfd test tells us), just
1623 call the generic function. Any adjustment will be done at final
1624 link time. */
1625 if (output_bfd != NULL)
cedb70c5 1626 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1627 input_section, output_bfd, error_message);
1628
1629 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1630 if (TOCstart == 0)
1c865ab2 1631 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1632
1633 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1634 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1635 return bfd_reloc_ok;
1636}
1637
5663e321
AM
1638static bfd_reloc_status_type
1639ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1640 void *data, asection *input_section,
1641 bfd *output_bfd, char **error_message)
1642{
1643 uint64_t insn;
1644 bfd_vma targ;
1645
1646 if (output_bfd != NULL)
1647 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1648 input_section, output_bfd, error_message);
1649
1650 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1651 insn <<= 32;
1652 insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1653
1654 targ = (symbol->section->output_section->vma
1655 + symbol->section->output_offset
1656 + reloc_entry->addend);
1657 if (!bfd_is_com_section (symbol->section))
1658 targ += symbol->value;
1659 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1660 targ += 1ULL << 33;
1661 if (reloc_entry->howto->pc_relative)
1662 {
1663 bfd_vma from = (reloc_entry->address
1664 + input_section->output_offset
1665 + input_section->output_section->vma);
1666 targ -=from;
1667 }
1668 targ >>= reloc_entry->howto->rightshift;
1669 insn &= ~reloc_entry->howto->dst_mask;
1670 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1671 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1672 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1673 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1674 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1675 >= 1ULL << reloc_entry->howto->bitsize))
1676 return bfd_reloc_overflow;
1677 return bfd_reloc_ok;
1678}
1679
805fc799 1680static bfd_reloc_status_type
4ce794b7
AM
1681ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1682 void *data, asection *input_section,
1683 bfd *output_bfd, char **error_message)
805fc799
AM
1684{
1685 /* If this is a relocatable link (output_bfd test tells us), just
1686 call the generic function. Any adjustment will be done at final
1687 link time. */
1688 if (output_bfd != NULL)
cedb70c5 1689 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1690 input_section, output_bfd, error_message);
1691
1692 if (error_message != NULL)
1693 {
1694 static char buf[60];
1695 sprintf (buf, "generic linker can't handle %s",
1696 reloc_entry->howto->name);
1697 *error_message = buf;
1698 }
1699 return bfd_reloc_dangerous;
1700}
1701
927be08e
AM
1702/* Track GOT entries needed for a given symbol. We might need more
1703 than one got entry per symbol. */
1704struct got_entry
1705{
1706 struct got_entry *next;
1707
1708 /* The symbol addend that we'll be placing in the GOT. */
1709 bfd_vma addend;
1710
1711 /* Unlike other ELF targets, we use separate GOT entries for the same
1712 symbol referenced from different input files. This is to support
1713 automatic multiple TOC/GOT sections, where the TOC base can vary
1714 from one input file to another. After partitioning into TOC groups
1715 we merge entries within the group.
1716
1717 Point to the BFD owning this GOT entry. */
1718 bfd *owner;
1719
1720 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1721 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1722 unsigned char tls_type;
927be08e
AM
1723
1724 /* Non-zero if got.ent points to real entry. */
f961d9dd 1725 unsigned char is_indirect;
927be08e
AM
1726
1727 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1728 union
2cdcc330
AM
1729 {
1730 bfd_signed_vma refcount;
1731 bfd_vma offset;
1732 struct got_entry *ent;
1733 } got;
927be08e
AM
1734};
1735
1736/* The same for PLT. */
1737struct plt_entry
1738{
1739 struct plt_entry *next;
1740
1741 bfd_vma addend;
1742
1743 union
2cdcc330
AM
1744 {
1745 bfd_signed_vma refcount;
1746 bfd_vma offset;
1747 } plt;
927be08e
AM
1748};
1749
e717da7e
AM
1750struct ppc64_elf_obj_tdata
1751{
1752 struct elf_obj_tdata elf;
1753
1754 /* Shortcuts to dynamic linker sections. */
1755 asection *got;
1756 asection *relgot;
1757
b3fac117
AM
1758 /* Used during garbage collection. We attach global symbols defined
1759 on removed .opd entries to this section so that the sym is removed. */
1760 asection *deleted_section;
81688140 1761
927be08e 1762 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1763 sections means we potentially need one of these for each input bfd. */
927be08e 1764 struct got_entry tlsld_got;
8860955f 1765
2cdcc330
AM
1766 union
1767 {
729eabd5
AM
1768 /* A copy of relocs before they are modified for --emit-relocs. */
1769 Elf_Internal_Rela *relocs;
1770
1771 /* Section contents. */
1772 bfd_byte *contents;
1773 } opd;
d77c8a4b
AM
1774
1775 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1776 the reloc to be in the range -32768 to 32767. */
98528052
AM
1777 unsigned int has_small_toc_reloc : 1;
1778
560c8763
AM
1779 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1780 instruction not one we handle. */
1781 unsigned int unexpected_toc_insn : 1;
066f4018 1782
903b777d
AM
1783 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1784 this file. */
1785 unsigned int has_optrel : 1;
e717da7e
AM
1786};
1787
1788#define ppc64_elf_tdata(bfd) \
1789 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1790
1791#define ppc64_tlsld_got(bfd) \
1792 (&ppc64_elf_tdata (bfd)->tlsld_got)
1793
0c8d6e5c
AM
1794#define is_ppc64_elf(bfd) \
1795 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1796 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1797
e717da7e
AM
1798/* Override the generic function because we store some extras. */
1799
1800static bfd_boolean
1801ppc64_elf_mkobject (bfd *abfd)
1802{
0ffa91dd 1803 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1804 PPC64_ELF_DATA);
e717da7e
AM
1805}
1806
feee612b 1807/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1808 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1809
b34976b6 1810static bfd_boolean
4ce794b7 1811ppc64_elf_object_p (bfd *abfd)
feee612b 1812{
14b57c7c
AM
1813 if (!abfd->arch_info->the_default)
1814 return TRUE;
1815
1816 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1817 {
1818 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1819
1820 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1821 {
1822 /* Relies on arch after 32 bit default being 64 bit default. */
1823 abfd->arch_info = abfd->arch_info->next;
1824 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1825 }
1826 }
14b57c7c 1827 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1828}
1829
d37c89e5
AM
1830/* Support for core dump NOTE sections. */
1831
1832static bfd_boolean
1833ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1834{
eea6121a 1835 size_t offset, size;
d37c89e5
AM
1836
1837 if (note->descsz != 504)
1838 return FALSE;
1839
1840 /* pr_cursig */
228e534f 1841 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1842
1843 /* pr_pid */
228e534f 1844 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1845
1846 /* pr_reg */
1847 offset = 112;
eea6121a 1848 size = 384;
d37c89e5
AM
1849
1850 /* Make a ".reg/999" section. */
1851 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1852 size, note->descpos + offset);
d37c89e5
AM
1853}
1854
1855static bfd_boolean
1856ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1857{
1858 if (note->descsz != 136)
1859 return FALSE;
1860
228e534f 1861 elf_tdata (abfd)->core->pid
bc989cdc 1862 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1863 elf_tdata (abfd)->core->program
d37c89e5 1864 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1865 elf_tdata (abfd)->core->command
d37c89e5
AM
1866 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1867
1868 return TRUE;
1869}
1870
183e98be
AM
1871static char *
1872ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1873 ...)
1874{
1875 switch (note_type)
1876 {
1877 default:
1878 return NULL;
1879
1880 case NT_PRPSINFO:
1881 {
9ef6d1e3 1882 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1883 va_list ap;
1884
1885 va_start (ap, note_type);
75cd47ed 1886 memset (data, 0, sizeof (data));
183e98be 1887 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1888#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1889 DIAGNOSTIC_PUSH;
be3e27bb 1890 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1891 -Wstringop-truncation:
1892 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1893 */
95da9854
L
1894 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1895#endif
183e98be 1896 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1897#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1898 DIAGNOSTIC_POP;
fe75810f 1899#endif
183e98be
AM
1900 va_end (ap);
1901 return elfcore_write_note (abfd, buf, bufsiz,
1902 "CORE", note_type, data, sizeof (data));
1903 }
1904
1905 case NT_PRSTATUS:
1906 {
1907 char data[504];
1908 va_list ap;
1909 long pid;
1910 int cursig;
1911 const void *greg;
1912
1913 va_start (ap, note_type);
1914 memset (data, 0, 112);
1915 pid = va_arg (ap, long);
1916 bfd_put_32 (abfd, pid, data + 32);
1917 cursig = va_arg (ap, int);
1918 bfd_put_16 (abfd, cursig, data + 12);
1919 greg = va_arg (ap, const void *);
1920 memcpy (data + 112, greg, 384);
1921 memset (data + 496, 0, 8);
1922 va_end (ap);
1923 return elfcore_write_note (abfd, buf, bufsiz,
1924 "CORE", note_type, data, sizeof (data));
1925 }
1926 }
1927}
1928
5d35169e
AM
1929/* Add extra PPC sections. */
1930
2cdcc330 1931static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1932{
07d6d2b8
AM
1933 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1934 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1935 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1936 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1937 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1938 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1939 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1940};
1941
7c8fe5c4
AM
1942enum _ppc64_sec_type {
1943 sec_normal = 0,
1944 sec_opd = 1,
1945 sec_toc = 2
1946};
1947
f0abc2a1
AM
1948struct _ppc64_elf_section_data
1949{
1950 struct bfd_elf_section_data elf;
411e1bfb 1951
f0abc2a1
AM
1952 union
1953 {
51aecdc5
AM
1954 /* An array with one entry for each opd function descriptor,
1955 and some spares since opd entries may be either 16 or 24 bytes. */
1956#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
1957 struct _opd_sec_data
1958 {
1959 /* Points to the function code section for local opd entries. */
1960 asection **func_sec;
1961
1962 /* After editing .opd, adjust references to opd local syms. */
1963 long *adjust;
1964 } opd;
7c8fe5c4 1965
3a71aa26
AM
1966 /* An array for toc sections, indexed by offset/8. */
1967 struct _toc_sec_data
1968 {
1969 /* Specifies the relocation symbol index used at a given toc offset. */
1970 unsigned *symndx;
1971
1972 /* And the relocation addend. */
1973 bfd_vma *add;
1974 } toc;
7c8fe5c4
AM
1975 } u;
1976
1977 enum _ppc64_sec_type sec_type:2;
411e1bfb 1978
7c8fe5c4
AM
1979 /* Flag set when small branches are detected. Used to
1980 select suitable defaults for the stub group size. */
1981 unsigned int has_14bit_branch:1;
3e04d765
AM
1982
1983 /* Flag set when PLTCALL relocs are detected. */
1984 unsigned int has_pltcall:1;
066f4018 1985
903b777d
AM
1986 /* Flag set when section has PLT/GOT/TOC relocations that can be
1987 optimised. */
1988 unsigned int has_optrel:1;
f0abc2a1
AM
1989};
1990
1991#define ppc64_elf_section_data(sec) \
411e1bfb 1992 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
1993
1994static bfd_boolean
4ce794b7 1995ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1996{
f592407e
AM
1997 if (!sec->used_by_bfd)
1998 {
1999 struct _ppc64_elf_section_data *sdata;
2000 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 2001
f592407e
AM
2002 sdata = bfd_zalloc (abfd, amt);
2003 if (sdata == NULL)
2004 return FALSE;
2005 sec->used_by_bfd = sdata;
2006 }
f0abc2a1
AM
2007
2008 return _bfd_elf_new_section_hook (abfd, sec);
2009}
4025353c 2010
74f0fb50 2011static struct _opd_sec_data *
4025353c
AM
2012get_opd_info (asection * sec)
2013{
2014 if (sec != NULL
2015 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2016 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2017 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2018 return NULL;
2019}
90e3cdf2
JJ
2020\f
2021/* Parameters for the qsort hook. */
90e3cdf2 2022static bfd_boolean synthetic_relocatable;
cd285db5 2023static asection *synthetic_opd;
90e3cdf2 2024
699733f6 2025/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2026
2027static int
2028compare_symbols (const void *ap, const void *bp)
2029{
2cdcc330
AM
2030 const asymbol *a = *(const asymbol **) ap;
2031 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 2032
699733f6
AM
2033 /* Section symbols first. */
2034 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2035 return -1;
699733f6 2036 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2037 return 1;
2038
699733f6 2039 /* then .opd symbols. */
cd285db5
AM
2040 if (synthetic_opd != NULL)
2041 {
2042 if (strcmp (a->section->name, ".opd") == 0
2043 && strcmp (b->section->name, ".opd") != 0)
2044 return -1;
2045 if (strcmp (a->section->name, ".opd") != 0
2046 && strcmp (b->section->name, ".opd") == 0)
2047 return 1;
2048 }
90e3cdf2 2049
699733f6 2050 /* then other code symbols. */
2cdcc330
AM
2051 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2052 == (SEC_CODE | SEC_ALLOC))
2053 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2054 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2055 return -1;
2056
2cdcc330
AM
2057 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2058 != (SEC_CODE | SEC_ALLOC))
2059 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2060 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2061 return 1;
2062
2063 if (synthetic_relocatable)
2064 {
2065 if (a->section->id < b->section->id)
2066 return -1;
2067
2068 if (a->section->id > b->section->id)
2069 return 1;
2070 }
2071
2072 if (a->value + a->section->vma < b->value + b->section->vma)
2073 return -1;
2074
2075 if (a->value + a->section->vma > b->value + b->section->vma)
2076 return 1;
2077
4d35a0aa
AM
2078 /* For syms with the same value, prefer strong dynamic global function
2079 syms over other syms. */
2080 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2081 return -1;
2082
2083 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2084 return 1;
2085
2086 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2087 return -1;
2088
2089 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2090 return 1;
2091
2092 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2093 return -1;
2094
2095 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2096 return 1;
2097
2098 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2099 return -1;
2100
2101 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2102 return 1;
2103
aaed6f5b 2104 return a > b;
90e3cdf2
JJ
2105}
2106
699733f6 2107/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2108
699733f6 2109static asymbol *
7292b3ac 2110sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 2111{
699733f6 2112 long mid;
90e3cdf2 2113
7292b3ac 2114 if (id == (unsigned) -1)
699733f6
AM
2115 {
2116 while (lo < hi)
2117 {
2118 mid = (lo + hi) >> 1;
2119 if (syms[mid]->value + syms[mid]->section->vma < value)
2120 lo = mid + 1;
2121 else if (syms[mid]->value + syms[mid]->section->vma > value)
2122 hi = mid;
2123 else
2124 return syms[mid];
2125 }
2126 }
2127 else
2128 {
2129 while (lo < hi)
2130 {
2131 mid = (lo + hi) >> 1;
2132 if (syms[mid]->section->id < id)
2133 lo = mid + 1;
2134 else if (syms[mid]->section->id > id)
2135 hi = mid;
2136 else if (syms[mid]->value < value)
2137 lo = mid + 1;
2138 else if (syms[mid]->value > value)
2139 hi = mid;
2140 else
2141 return syms[mid];
2142 }
2143 }
2144 return NULL;
90e3cdf2
JJ
2145}
2146
468392fb
AM
2147static bfd_boolean
2148section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2149{
2150 bfd_vma vma = *(bfd_vma *) ptr;
2151 return ((section->flags & SEC_ALLOC) != 0
2152 && section->vma <= vma
2153 && vma < section->vma + section->size);
2154}
2155
699733f6 2156/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
2157 entry syms. Also generate @plt symbols for the glink branch table.
2158 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
2159
2160static long
a7535cf3
AM
2161ppc64_elf_get_synthetic_symtab (bfd *abfd,
2162 long static_count, asymbol **static_syms,
2163 long dyn_count, asymbol **dyn_syms,
c9727e01 2164 asymbol **ret)
90e3cdf2
JJ
2165{
2166 asymbol *s;
0ccf57bd 2167 size_t i, j, count;
90e3cdf2 2168 char *names;
0ccf57bd 2169 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 2170 asection *opd = NULL;
90e3cdf2 2171 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2172 asymbol **syms;
ee67d69a 2173 int abi = abiversion (abfd);
90e3cdf2
JJ
2174
2175 *ret = NULL;
2176
ee67d69a
AM
2177 if (abi < 2)
2178 {
2179 opd = bfd_get_section_by_name (abfd, ".opd");
2180 if (opd == NULL && abi == 1)
2181 return 0;
2182 }
90e3cdf2 2183
a5259595
AM
2184 syms = NULL;
2185 codesecsym = 0;
2186 codesecsymend = 0;
2187 secsymend = 0;
2188 opdsymend = 0;
2189 symcount = 0;
2190 if (opd != NULL)
c9727e01 2191 {
a5259595
AM
2192 symcount = static_count;
2193 if (!relocatable)
2194 symcount += dyn_count;
2195 if (symcount == 0)
2196 return 0;
c9727e01 2197
a5259595
AM
2198 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2199 if (syms == NULL)
2200 return -1;
90e3cdf2 2201
a5259595
AM
2202 if (!relocatable && static_count != 0 && dyn_count != 0)
2203 {
2204 /* Use both symbol tables. */
2205 memcpy (syms, static_syms, static_count * sizeof (*syms));
2206 memcpy (syms + static_count, dyn_syms,
2207 (dyn_count + 1) * sizeof (*syms));
2208 }
2209 else if (!relocatable && static_count == 0)
2210 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2211 else
2212 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2213
0ccf57bd
AM
2214 /* Trim uninteresting symbols. Interesting symbols are section,
2215 function, and notype symbols. */
2216 for (i = 0, j = 0; i < symcount; ++i)
2217 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2218 | BSF_RELC | BSF_SRELC)) == 0)
2219 syms[j++] = syms[i];
2220 symcount = j;
2221
a5259595
AM
2222 synthetic_relocatable = relocatable;
2223 synthetic_opd = opd;
2224 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2225
a5259595
AM
2226 if (!relocatable && symcount > 1)
2227 {
bfa5bd2a
PA
2228 /* Trim duplicate syms, since we may have merged the normal
2229 and dynamic symbols. Actually, we only care about syms
2230 that have different values, so trim any with the same
2231 value. Don't consider ifunc and ifunc resolver symbols
2232 duplicates however, because GDB wants to know whether a
2233 text symbol is an ifunc resolver. */
a5259595 2234 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2235 {
2236 const asymbol *s0 = syms[i - 1];
2237 const asymbol *s1 = syms[i];
2238
2239 if ((s0->value + s0->section->vma
2240 != s1->value + s1->section->vma)
2241 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2242 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2243 syms[j++] = syms[i];
2244 }
a5259595
AM
2245 symcount = j;
2246 }
699733f6 2247
a5259595
AM
2248 i = 0;
2249 /* Note that here and in compare_symbols we can't compare opd and
2250 sym->section directly. With separate debug info files, the
2251 symbols will be extracted from the debug file while abfd passed
2252 to this function is the real binary. */
0ccf57bd 2253 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2254 ++i;
2255 codesecsym = i;
2256
2257 for (; i < symcount; ++i)
2258 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2259 | SEC_THREAD_LOCAL))
2260 != (SEC_CODE | SEC_ALLOC))
2261 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2262 break;
2263 codesecsymend = i;
2264
2265 for (; i < symcount; ++i)
2266 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2267 break;
2268 secsymend = i;
2269
2270 for (; i < symcount; ++i)
2271 if (strcmp (syms[i]->section->name, ".opd") != 0)
2272 break;
2273 opdsymend = i;
2274
2275 for (; i < symcount; ++i)
2cdcc330
AM
2276 if (((syms[i]->section->flags
2277 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2278 != (SEC_CODE | SEC_ALLOC))
2279 break;
2280 symcount = i;
2281 }
c9727e01 2282 count = 0;
90e3cdf2 2283
699733f6 2284 if (relocatable)
90e3cdf2 2285 {
699733f6
AM
2286 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2287 arelent *r;
2288 size_t size;
0ccf57bd 2289 size_t relcount;
90e3cdf2 2290
468392fb
AM
2291 if (opdsymend == secsymend)
2292 goto done;
2293
699733f6 2294 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2295 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2296 if (relcount == 0)
c9727e01 2297 goto done;
90e3cdf2 2298
7356fed5
AM
2299 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2300 {
2301 count = -1;
2302 goto done;
2303 }
2304
699733f6 2305 size = 0;
595da8c5 2306 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2307 {
2308 asymbol *sym;
90e3cdf2 2309
595da8c5 2310 while (r < opd->relocation + relcount
699733f6
AM
2311 && r->address < syms[i]->value + opd->vma)
2312 ++r;
90e3cdf2 2313
595da8c5 2314 if (r == opd->relocation + relcount)
699733f6 2315 break;
90e3cdf2 2316
699733f6
AM
2317 if (r->address != syms[i]->value + opd->vma)
2318 continue;
90e3cdf2 2319
699733f6
AM
2320 if (r->howto->type != R_PPC64_ADDR64)
2321 continue;
90e3cdf2 2322
699733f6
AM
2323 sym = *r->sym_ptr_ptr;
2324 if (!sym_exists_at (syms, opdsymend, symcount,
2325 sym->section->id, sym->value + r->addend))
2326 {
2327 ++count;
2328 size += sizeof (asymbol);
2329 size += strlen (syms[i]->name) + 2;
2330 }
2331 }
90e3cdf2 2332
c4b0b099
AM
2333 if (size == 0)
2334 goto done;
699733f6
AM
2335 s = *ret = bfd_malloc (size);
2336 if (s == NULL)
2337 {
7356fed5 2338 count = -1;
c9727e01 2339 goto done;
699733f6 2340 }
90e3cdf2 2341
699733f6 2342 names = (char *) (s + count);
90e3cdf2 2343
595da8c5 2344 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2345 {
699733f6 2346 asymbol *sym;
90e3cdf2 2347
595da8c5 2348 while (r < opd->relocation + relcount
699733f6
AM
2349 && r->address < syms[i]->value + opd->vma)
2350 ++r;
90e3cdf2 2351
595da8c5 2352 if (r == opd->relocation + relcount)
699733f6
AM
2353 break;
2354
2355 if (r->address != syms[i]->value + opd->vma)
2356 continue;
2357
2358 if (r->howto->type != R_PPC64_ADDR64)
2359 continue;
90e3cdf2 2360
699733f6
AM
2361 sym = *r->sym_ptr_ptr;
2362 if (!sym_exists_at (syms, opdsymend, symcount,
2363 sym->section->id, sym->value + r->addend))
2364 {
2365 size_t len;
2366
2367 *s = *syms[i];
6ba2a415 2368 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2369 s->section = sym->section;
2370 s->value = sym->value + r->addend;
2371 s->name = names;
2372 *names++ = '.';
2373 len = strlen (syms[i]->name);
2374 memcpy (names, syms[i]->name, len + 1);
2375 names += len + 1;
6f610d07
UW
2376 /* Have udata.p point back to the original symbol this
2377 synthetic symbol was derived from. */
2378 s->udata.p = syms[i];
699733f6
AM
2379 s++;
2380 }
2381 }
2382 }
2383 else
90e3cdf2 2384 {
468392fb 2385 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 2386 bfd_byte *contents = NULL;
699733f6 2387 size_t size;
0ccf57bd 2388 size_t plt_count = 0;
468392fb
AM
2389 bfd_vma glink_vma = 0, resolv_vma = 0;
2390 asection *dynamic, *glink = NULL, *relplt = NULL;
2391 arelent *p;
90e3cdf2 2392
ee67d69a 2393 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2394 {
c4b0b099
AM
2395 free_contents_and_exit_err:
2396 count = -1;
ee67d69a 2397 free_contents_and_exit:
699733f6 2398 if (contents)
ee67d69a 2399 free (contents);
c9727e01 2400 goto done;
699733f6 2401 }
90e3cdf2 2402
699733f6
AM
2403 size = 0;
2404 for (i = secsymend; i < opdsymend; ++i)
2405 {
2406 bfd_vma ent;
90e3cdf2 2407
5ef11c02
AM
2408 /* Ignore bogus symbols. */
2409 if (syms[i]->value > opd->size - 8)
2410 continue;
2411
699733f6
AM
2412 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2413 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2414 {
2415 ++count;
2416 size += sizeof (asymbol);
2417 size += strlen (syms[i]->name) + 2;
2418 }
2419 }
90e3cdf2 2420
468392fb 2421 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2422 if (dyn_count != 0
2423 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2424 {
2425 bfd_byte *dynbuf, *extdyn, *extdynend;
2426 size_t extdynsize;
2427 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2428
2429 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2430 goto free_contents_and_exit_err;
468392fb
AM
2431
2432 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2433 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2434
2435 extdyn = dynbuf;
2436 extdynend = extdyn + dynamic->size;
2437 for (; extdyn < extdynend; extdyn += extdynsize)
2438 {
2439 Elf_Internal_Dyn dyn;
2440 (*swap_dyn_in) (abfd, extdyn, &dyn);
2441
2442 if (dyn.d_tag == DT_NULL)
2443 break;
2444
2445 if (dyn.d_tag == DT_PPC64_GLINK)
2446 {
9e390558
AM
2447 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2448 See comment in ppc64_elf_finish_dynamic_sections. */
2449 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2450 /* The .glink section usually does not survive the final
2451 link; search for the section (usually .text) where the
2452 glink stubs now reside. */
2453 glink = bfd_sections_find_if (abfd, section_covers_vma,
2454 &glink_vma);
2455 break;
2456 }
2457 }
2458
2459 free (dynbuf);
2460 }
2461
2462 if (glink != NULL)
2463 {
2464 /* Determine __glink trampoline by reading the relative branch
2465 from the first glink stub. */
2466 bfd_byte buf[4];
b9e5796b
AM
2467 unsigned int off = 0;
2468
2469 while (bfd_get_section_contents (abfd, glink, buf,
2470 glink_vma + off - glink->vma, 4))
468392fb
AM
2471 {
2472 unsigned int insn = bfd_get_32 (abfd, buf);
2473 insn ^= B_DOT;
2474 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2475 {
2cdcc330
AM
2476 resolv_vma
2477 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2478 break;
2479 }
2480 off += 4;
2481 if (off > 4)
2482 break;
468392fb
AM
2483 }
2484
2485 if (resolv_vma)
2486 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2487
066ee829
AM
2488 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2489 if (relplt != NULL)
2490 {
2491 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2cdcc330 2492 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 2493 goto free_contents_and_exit_err;
68ffbac6 2494
066ee829
AM
2495 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2496 size += plt_count * sizeof (asymbol);
468392fb 2497
066ee829
AM
2498 p = relplt->relocation;
2499 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2500 {
2501 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2502 if (p->addend != 0)
2503 size += sizeof ("+0x") - 1 + 16;
2504 }
066ee829 2505 }
468392fb
AM
2506 }
2507
c4b0b099
AM
2508 if (size == 0)
2509 goto free_contents_and_exit;
699733f6
AM
2510 s = *ret = bfd_malloc (size);
2511 if (s == NULL)
c4b0b099 2512 goto free_contents_and_exit_err;
90e3cdf2 2513
468392fb 2514 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2515
699733f6 2516 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2517 {
699733f6 2518 bfd_vma ent;
90e3cdf2 2519
5ef11c02
AM
2520 if (syms[i]->value > opd->size - 8)
2521 continue;
2522
699733f6
AM
2523 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2524 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2525 {
0ccf57bd 2526 size_t lo, hi;
699733f6 2527 size_t len;
c9727e01 2528 asection *sec = abfd->sections;
90e3cdf2 2529
699733f6
AM
2530 *s = *syms[i];
2531 lo = codesecsym;
2532 hi = codesecsymend;
2533 while (lo < hi)
2534 {
0ccf57bd 2535 size_t mid = (lo + hi) >> 1;
699733f6
AM
2536 if (syms[mid]->section->vma < ent)
2537 lo = mid + 1;
2538 else if (syms[mid]->section->vma > ent)
2539 hi = mid;
2540 else
c9727e01
AM
2541 {
2542 sec = syms[mid]->section;
2543 break;
2544 }
699733f6
AM
2545 }
2546
c9727e01 2547 if (lo >= hi && lo > codesecsym)
699733f6 2548 sec = syms[lo - 1]->section;
699733f6
AM
2549
2550 for (; sec != NULL; sec = sec->next)
2551 {
2552 if (sec->vma > ent)
2553 break;
63524580
JK
2554 /* SEC_LOAD may not be set if SEC is from a separate debug
2555 info file. */
2556 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2557 break;
2558 if ((sec->flags & SEC_CODE) != 0)
2559 s->section = sec;
2560 }
6ba2a415 2561 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2562 s->value = ent - s->section->vma;
2563 s->name = names;
2564 *names++ = '.';
2565 len = strlen (syms[i]->name);
2566 memcpy (names, syms[i]->name, len + 1);
2567 names += len + 1;
6f610d07
UW
2568 /* Have udata.p point back to the original symbol this
2569 synthetic symbol was derived from. */
2570 s->udata.p = syms[i];
699733f6 2571 s++;
90e3cdf2 2572 }
90e3cdf2 2573 }
699733f6 2574 free (contents);
468392fb
AM
2575
2576 if (glink != NULL && relplt != NULL)
2577 {
2578 if (resolv_vma)
2579 {
2580 /* Add a symbol for the main glink trampoline. */
86a4952b 2581 memset (s, 0, sizeof *s);
468392fb 2582 s->the_bfd = abfd;
6ba2a415 2583 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2584 s->section = glink;
2585 s->value = resolv_vma - glink->vma;
2586 s->name = names;
2cdcc330
AM
2587 memcpy (names, "__glink_PLTresolve",
2588 sizeof ("__glink_PLTresolve"));
468392fb
AM
2589 names += sizeof ("__glink_PLTresolve");
2590 s++;
2591 count++;
2592 }
2593
2594 /* FIXME: It would be very much nicer to put sym@plt on the
2595 stub rather than on the glink branch table entry. The
2596 objdump disassembler would then use a sensible symbol
2597 name on plt calls. The difficulty in doing so is
2598 a) finding the stubs, and,
2599 b) matching stubs against plt entries, and,
2600 c) there can be multiple stubs for a given plt entry.
2601
2602 Solving (a) could be done by code scanning, but older
2603 ppc64 binaries used different stubs to current code.
2604 (b) is the tricky one since you need to known the toc
2605 pointer for at least one function that uses a pic stub to
2606 be able to calculate the plt address referenced.
2607 (c) means gdb would need to set multiple breakpoints (or
2608 find the glink branch itself) when setting breakpoints
2609 for pending shared library loads. */
2610 p = relplt->relocation;
2611 for (i = 0; i < plt_count; i++, p++)
2612 {
2613 size_t len;
2614
2615 *s = **p->sym_ptr_ptr;
2616 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2617 we are defining a symbol, ensure one of them is set. */
2618 if ((s->flags & BSF_LOCAL) == 0)
2619 s->flags |= BSF_GLOBAL;
6ba2a415 2620 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2621 s->section = glink;
2622 s->value = glink_vma - glink->vma;
2623 s->name = names;
2624 s->udata.p = NULL;
2625 len = strlen ((*p->sym_ptr_ptr)->name);
2626 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2627 names += len;
e054468f
AM
2628 if (p->addend != 0)
2629 {
2630 memcpy (names, "+0x", sizeof ("+0x") - 1);
2631 names += sizeof ("+0x") - 1;
2632 bfd_sprintf_vma (abfd, names, p->addend);
2633 names += strlen (names);
2634 }
468392fb
AM
2635 memcpy (names, "@plt", sizeof ("@plt"));
2636 names += sizeof ("@plt");
2637 s++;
b9e5796b
AM
2638 if (abi < 2)
2639 {
2640 glink_vma += 8;
2641 if (i >= 0x8000)
2642 glink_vma += 4;
2643 }
2644 else
468392fb
AM
2645 glink_vma += 4;
2646 }
2647 count += plt_count;
2648 }
90e3cdf2
JJ
2649 }
2650
c9727e01 2651 done:
a7535cf3 2652 free (syms);
90e3cdf2
JJ
2653 return count;
2654}
5bd4f169 2655\f
65f38f15
AM
2656/* The following functions are specific to the ELF linker, while
2657 functions above are used generally. Those named ppc64_elf_* are
2658 called by the main ELF linker code. They appear in this file more
2659 or less in the order in which they are called. eg.
2660 ppc64_elf_check_relocs is called early in the link process,
2661 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2662 called.
2663
2664 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2665 functions have both a function code symbol and a function descriptor
2666 symbol. A call to foo in a relocatable object file looks like:
2667
2668 . .text
2669 . x:
2670 . bl .foo
2671 . nop
2672
2673 The function definition in another object file might be:
2674
2675 . .section .opd
2676 . foo: .quad .foo
2677 . .quad .TOC.@tocbase
2678 . .quad 0
2679 .
2680 . .text
2681 . .foo: blr
2682
2683 When the linker resolves the call during a static link, the branch
2684 unsurprisingly just goes to .foo and the .opd information is unused.
2685 If the function definition is in a shared library, things are a little
2686 different: The call goes via a plt call stub, the opd information gets
2687 copied to the plt, and the linker patches the nop.
2688
2689 . x:
2690 . bl .foo_stub
2691 . ld 2,40(1)
2692 .
2693 .
2694 . .foo_stub:
71a39c98
AM
2695 . std 2,40(1) # in practice, the call stub
2696 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2697 . addi 11,11,Lfoo@toc@l # this is the general idea
2698 . ld 12,0(11)
2699 . ld 2,8(11)
2700 . mtctr 12
2701 . ld 11,16(11)
e86ce104
AM
2702 . bctr
2703 .
2704 . .section .plt
2705 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2706
2707 The "reloc ()" notation is supposed to indicate that the linker emits
2708 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2709 copying.
2710
2711 What are the difficulties here? Well, firstly, the relocations
2712 examined by the linker in check_relocs are against the function code
2713 sym .foo, while the dynamic relocation in the plt is emitted against
2714 the function descriptor symbol, foo. Somewhere along the line, we need
2715 to carefully copy dynamic link information from one symbol to the other.
2716 Secondly, the generic part of the elf linker will make .foo a dynamic
2717 symbol as is normal for most other backends. We need foo dynamic
2718 instead, at least for an application final link. However, when
2719 creating a shared library containing foo, we need to have both symbols
2720 dynamic so that references to .foo are satisfied during the early
2721 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2722 definition from some other object, eg. a static library.
2723
2724 Update: As of August 2004, we support a new convention. Function
2725 calls may use the function descriptor symbol, ie. "bl foo". This
2726 behaves exactly as "bl .foo". */
65f38f15 2727
7c8bbca5
AM
2728/* Of those relocs that might be copied as dynamic relocs, this
2729 function selects those that must be copied when linking a shared
2730 library or PIE, even when the symbol is local. */
65f38f15 2731
1d483afe
AM
2732static int
2733must_be_dyn_reloc (struct bfd_link_info *info,
2734 enum elf_ppc64_reloc_type r_type)
2735{
2736 switch (r_type)
2737 {
2738 default:
7c8bbca5
AM
2739 /* Only relative relocs can be resolved when the object load
2740 address isn't fixed. DTPREL64 is excluded because the
2741 dynamic linker needs to differentiate global dynamic from
2742 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2743 return 1;
2744
2745 case R_PPC64_REL32:
2746 case R_PPC64_REL64:
2747 case R_PPC64_REL30:
2748 return 0;
2749
2750 case R_PPC64_TPREL16:
2751 case R_PPC64_TPREL16_LO:
2752 case R_PPC64_TPREL16_HI:
2753 case R_PPC64_TPREL16_HA:
2754 case R_PPC64_TPREL16_DS:
2755 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2756 case R_PPC64_TPREL16_HIGH:
2757 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2758 case R_PPC64_TPREL16_HIGHER:
2759 case R_PPC64_TPREL16_HIGHERA:
2760 case R_PPC64_TPREL16_HIGHEST:
2761 case R_PPC64_TPREL16_HIGHESTA:
2762 case R_PPC64_TPREL64:
c213164a 2763 case R_PPC64_TPREL34:
7c8bbca5
AM
2764 /* These relocations are relative but in a shared library the
2765 linker doesn't know the thread pointer base. */
2766 return bfd_link_dll (info);
1d483afe
AM
2767 }
2768}
65f38f15 2769
f4656909
AM
2770/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2771 copying dynamic variables from a shared lib into an app's dynbss
2772 section, and instead use a dynamic relocation to point into the
5d35169e
AM
2773 shared lib. With code that gcc generates, it's vital that this be
2774 enabled; In the PowerPC64 ABI, the address of a function is actually
2775 the address of a function descriptor, which resides in the .opd
2776 section. gcc uses the descriptor directly rather than going via the
2777 GOT as some other ABI's do, which means that initialized function
2778 pointers must reference the descriptor. Thus, a function pointer
2779 initialized to the address of a function in a shared library will
2780 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 2781 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
2782 presents a problem as a plt entry for that function is also
2783 initialized from the function descriptor symbol and the copy reloc
2784 may not be initialized first. */
a23b6845 2785#define ELIMINATE_COPY_RELOCS 1
f4656909 2786
721956f4 2787/* Section name for stubs is the associated section name plus this
29942be8
NC
2788 string. */
2789#define STUB_SUFFIX ".stub"
721956f4
AM
2790
2791/* Linker stubs.
2792 ppc_stub_long_branch:
2793 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2794 destination, but a 24 bit branch in a stub section will reach.
2795 . b dest
2796
2797 ppc_stub_plt_branch:
2798 Similar to the above, but a 24 bit branch in the stub section won't
2799 reach its destination.
71a39c98
AM
2800 . addis %r11,%r2,xxx@toc@ha
2801 . ld %r12,xxx@toc@l(%r11)
2802 . mtctr %r12
721956f4
AM
2803 . bctr
2804
2805 ppc_stub_plt_call:
2c66dc6c
AM
2806 Used to call a function in a shared library. If it so happens that
2807 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2808 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 2809 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
2810 . addis %r11,%r2,xxx@toc@ha
2811 . ld %r12,xxx+0@toc@l(%r11)
2812 . mtctr %r12
2813 . ld %r2,xxx+8@toc@l(%r11)
2814 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2815 . bctr
ad8e1ba5
AM
2816
2817 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2818 code to adjust the value and save r2 to support multiple toc sections.
2819 A ppc_stub_long_branch with an r2 offset looks like:
2820 . std %r2,40(%r1)
2821 . addis %r2,%r2,off@ha
2822 . addi %r2,%r2,off@l
2823 . b dest
2824
2825 A ppc_stub_plt_branch with an r2 offset looks like:
2826 . std %r2,40(%r1)
71a39c98
AM
2827 . addis %r11,%r2,xxx@toc@ha
2828 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
2829 . addis %r2,%r2,off@ha
2830 . addi %r2,%r2,off@l
71a39c98 2831 . mtctr %r12
ad8e1ba5 2832 . bctr
ac2df442 2833
05d0e962
AM
2834 All of the above stubs are shown as their ELFv1 variants. ELFv2
2835 variants exist too, simpler for plt calls since a new toc pointer
2836 and static chain are not loaded by the stub. In addition, ELFv2
2837 has some more complex stubs to handle calls marked with NOTOC
2838 relocs from functions where r2 is not a valid toc pointer. These
2839 come in two flavours, the ones shown below, and _both variants that
2840 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2841 one call is from a function where r2 is used as the toc pointer but
2842 needs a toc adjusting stub for small-model multi-toc, and another
2843 call is from a function where r2 is not valid.
2844 ppc_stub_long_branch_notoc:
2845 . mflr %r12
2846 . bcl 20,31,1f
2847 . 1:
2848 . mflr %r11
2849 . mtlr %r12
2cdcc330
AM
2850 . addis %r12,%r11,dest-1b@ha
2851 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2852 . b dest
2853
2854 ppc_stub_plt_branch_notoc:
2855 . mflr %r12
2856 . bcl 20,31,1f
2857 . 1:
2858 . mflr %r11
2859 . mtlr %r12
2860 . lis %r12,xxx-1b@highest
f891966f 2861 . ori %r12,%r12,xxx-1b@higher
05d0e962 2862 . sldi %r12,%r12,32
f891966f 2863 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2864 . ori %r12,%r12,xxx-1b@l
2865 . add %r12,%r11,%r12
2866 . mtctr %r12
2867 . bctr
2868
2869 ppc_stub_plt_call_notoc:
2870 . mflr %r12
2871 . bcl 20,31,1f
2872 . 1:
2873 . mflr %r11
2874 . mtlr %r12
2875 . lis %r12,xxx-1b@highest
f891966f 2876 . ori %r12,%r12,xxx-1b@higher
05d0e962 2877 . sldi %r12,%r12,32
f891966f 2878 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2879 . ori %r12,%r12,xxx-1b@l
2880 . ldx %r12,%r11,%r12
2881 . mtctr %r12
2882 . bctr
2883
04bdff6a
AM
2884 There are also ELFv1 powerxx variants of these stubs.
2885 ppc_stub_long_branch_notoc:
d4b87b1e 2886 . pla %r12,dest@pcrel
04bdff6a
AM
2887 . b dest
2888 ppc_stub_plt_branch_notoc:
2889 . lis %r11,(dest-1f)@highesta34
2890 . ori %r11,%r11,(dest-1f)@highera34
2891 . sldi %r11,%r11,34
d4b87b1e 2892 . 1: pla %r12,dest@pcrel
04bdff6a
AM
2893 . add %r12,%r11,%r12
2894 . mtctr %r12
2895 . bctr
2896 ppc_stub_plt_call_notoc:
2897 . lis %r11,(xxx-1f)@highesta34
2898 . ori %r11,%r11,(xxx-1f)@highera34
2899 . sldi %r11,%r11,34
d4b87b1e 2900 . 1: pla %r12,xxx@pcrel
04bdff6a
AM
2901 . ldx %r12,%r11,%r12
2902 . mtctr %r12
2903 . bctr
2904
05d0e962
AM
2905 In cases where the high instructions would add zero, they are
2906 omitted and following instructions modified in some cases.
04bdff6a
AM
2907 For example, a powerxx ppc_stub_plt_call_notoc might simplify down
2908 to
2909 . pld %r12,xxx@pcrel
2910 . mtctr %r12
2911 . bctr
05d0e962
AM
2912
2913 For a given stub group (a set of sections all using the same toc
2914 pointer value) there will be just one stub type used for any
2915 particular function symbol. For example, if printf is called from
2916 code with the tocsave optimization (ie. r2 saved in function
2917 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2918 and from other code without the tocsave optimization requiring a
2919 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2920 type will be created. Calls with the tocsave optimization will
2921 enter this stub after the instruction saving r2. A similar
2922 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2923 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2924 to call an external function like printf. If other calls to printf
2925 require a ppc_stub_plt_call linkage stub then a single
2926 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2927 call. If other calls to printf require a ppc_stub_plt_call_r2save
2928 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2929 be created and calls not requiring r2 to be saved will enter the
2930 stub after the r2 save instruction. There is an analogous
2931 hierarchy of long branch and plt branch stubs for local call
2932 linkage. */
721956f4 2933
2cdcc330
AM
2934enum ppc_stub_type
2935{
721956f4
AM
2936 ppc_stub_none,
2937 ppc_stub_long_branch,
ad8e1ba5 2938 ppc_stub_long_branch_r2off,
05d0e962
AM
2939 ppc_stub_long_branch_notoc,
2940 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 2941 ppc_stub_plt_branch,
ad8e1ba5 2942 ppc_stub_plt_branch_r2off,
05d0e962
AM
2943 ppc_stub_plt_branch_notoc,
2944 ppc_stub_plt_branch_both,
794e51c0 2945 ppc_stub_plt_call,
7341d5e2 2946 ppc_stub_plt_call_r2save,
05d0e962
AM
2947 ppc_stub_plt_call_notoc,
2948 ppc_stub_plt_call_both,
a4b6fadd
AM
2949 ppc_stub_global_entry,
2950 ppc_stub_save_res
721956f4
AM
2951};
2952
6f20ed8a
AM
2953/* Information on stub grouping. */
2954struct map_stub
2955{
2956 /* The stub section. */
2957 asection *stub_sec;
2958 /* This is the section to which stubs in the group will be attached. */
2959 asection *link_sec;
a4b6fadd
AM
2960 /* Next group. */
2961 struct map_stub *next;
2962 /* Whether to emit a copy of register save/restore functions in this
2963 group. */
2964 int needs_save_res;
df136d64
AM
2965 /* Current offset within stubs after the insn restoring lr in a
2966 _notoc or _both stub using bcl for pc-relative addressing, or
2967 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2968 unsigned int lr_restore;
2969 /* Accumulated size of EH info emitted to describe return address
2970 if stubs modify lr. Does not include 17 byte FDE header. */
2971 unsigned int eh_size;
2972 /* Offset in glink_eh_frame to the start of EH info for this group. */
2973 unsigned int eh_base;
6f20ed8a
AM
2974};
2975
2cdcc330
AM
2976struct ppc_stub_hash_entry
2977{
721956f4
AM
2978 /* Base hash table entry structure. */
2979 struct bfd_hash_entry root;
2980
ad8e1ba5
AM
2981 enum ppc_stub_type stub_type;
2982
6f20ed8a
AM
2983 /* Group information. */
2984 struct map_stub *group;
721956f4
AM
2985
2986 /* Offset within stub_sec of the beginning of this stub. */
2987 bfd_vma stub_offset;
2988
2989 /* Given the symbol's value and its section we can determine its final
2990 value when building the stubs (so the stub knows where to jump. */
2991 bfd_vma target_value;
2992 asection *target_section;
2993
721956f4
AM
2994 /* The symbol table entry, if any, that this was derived from. */
2995 struct ppc_link_hash_entry *h;
e054468f 2996 struct plt_entry *plt_ent;
721956f4 2997
2d7ad24e
AM
2998 /* Symbol type. */
2999 unsigned char symtype;
3000
6911b7dc
AM
3001 /* Symbol st_other. */
3002 unsigned char other;
721956f4
AM
3003};
3004
2cdcc330
AM
3005struct ppc_branch_hash_entry
3006{
721956f4
AM
3007 /* Base hash table entry structure. */
3008 struct bfd_hash_entry root;
3009
c456f082 3010 /* Offset within branch lookup table. */
721956f4
AM
3011 unsigned int offset;
3012
3013 /* Generation marker. */
3014 unsigned int iter;
3015};
65f38f15 3016
19e08130
AM
3017/* Used to track dynamic relocations for local symbols. */
3018struct ppc_dyn_relocs
3019{
3020 struct ppc_dyn_relocs *next;
3021
3022 /* The input section of the reloc. */
3023 asection *sec;
3024
3025 /* Total number of relocs copied for the input section. */
3026 unsigned int count : 31;
3027
3028 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3029 unsigned int ifunc : 1;
3030};
3031
65f38f15
AM
3032struct ppc_link_hash_entry
3033{
3034 struct elf_link_hash_entry elf;
3035
2cdcc330
AM
3036 union
3037 {
b3fac117
AM
3038 /* A pointer to the most recently used stub hash entry against this
3039 symbol. */
3040 struct ppc_stub_hash_entry *stub_cache;
3041
3042 /* A pointer to the next symbol starting with a '.' */
3043 struct ppc_link_hash_entry *next_dot_sym;
3044 } u;
721956f4 3045
65f38f15 3046 /* Track dynamic relocs copied for this symbol. */
6061a67d 3047 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3048
721956f4 3049 /* Link between function code and descriptor symbols. */
34814b9f 3050 struct ppc_link_hash_entry *oh;
721956f4 3051
e86ce104
AM
3052 /* Flag function code and descriptor symbols. */
3053 unsigned int is_func:1;
3054 unsigned int is_func_descriptor:1;
908b32fc 3055 unsigned int fake:1;
411e1bfb 3056
c5614fa4
AM
3057 /* Whether global opd/toc sym has been adjusted or not.
3058 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3059 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3060 unsigned int adjust_done:1;
3061
a4b6fadd
AM
3062 /* Set if this is an out-of-line register save/restore function,
3063 with non-standard calling convention. */
3064 unsigned int save_res:1;
3065
8b5f1ed8
AM
3066 /* Set if a duplicate symbol with non-zero localentry is detected,
3067 even when the duplicate symbol does not provide a definition. */
3068 unsigned int non_zero_localentry:1;
3069
411e1bfb 3070 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3071 Bits are or'd into the mask as the corresponding relocs are
3072 encountered during check_relocs, with TLS_TLS being set when any
3073 of the other TLS bits are set. tls_optimize clears bits when
3074 optimizing to indicate the corresponding GOT entry type is not
3075 needed. If set, TLS_TLS is never cleared. tls_optimize may also
b00a0a86 3076 set TLS_GDIE when a GD reloc turns into an IE one.
37da22e5
AM
3077 These flags are also kept for local symbols. */
3078#define TLS_TLS 1 /* Any TLS reloc. */
3079#define TLS_GD 2 /* GD reloc. */
3080#define TLS_LD 4 /* LD reloc. */
3081#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3082#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3083#define TLS_MARK 32 /* __tls_get_addr call marked. */
b00a0a86 3084#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
46e9995a 3085#define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
f961d9dd 3086 unsigned char tls_mask;
37da22e5
AM
3087
3088 /* The above field is also used to mark function symbols. In which
3089 case TLS_TLS will be 0. */
3090#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3091#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3092#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3093};
3094
3095/* ppc64 ELF linker hash table. */
3096
3097struct ppc_link_hash_table
3098{
3099 struct elf_link_hash_table elf;
3100
721956f4
AM
3101 /* The stub hash table. */
3102 struct bfd_hash_table stub_hash_table;
3103
3104 /* Another hash table for plt_branch stubs. */
3105 struct bfd_hash_table branch_hash_table;
3106
3b421ab3
AM
3107 /* Hash table for function prologue tocsave. */
3108 htab_t tocsave_htab;
3109
e7d1c40c
AM
3110 /* Various options and other info passed from the linker. */
3111 struct ppc64_elf_params *params;
721956f4 3112
6f20ed8a
AM
3113 /* The size of sec_info below. */
3114 unsigned int sec_info_arr_size;
3115
3116 /* Per-section array of extra section info. Done this way rather
3117 than as part of ppc64_elf_section_data so we have the info for
3118 non-ppc64 sections. */
3119 struct
3120 {
3121 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3122 bfd_vma toc_off;
6f20ed8a
AM
3123
3124 union
3125 {
3126 /* The section group that this section belongs to. */
3127 struct map_stub *group;
3128 /* A temp section list pointer. */
3129 asection *list;
3130 } u;
3131 } *sec_info;
721956f4 3132
a4b6fadd
AM
3133 /* Linked list of groups. */
3134 struct map_stub *group;
3135
ad8e1ba5
AM
3136 /* Temp used when calculating TOC pointers. */
3137 bfd_vma toc_curr;
bf102f86
AM
3138 bfd *toc_bfd;
3139 asection *toc_first_sec;
ad8e1ba5 3140
b3fac117
AM
3141 /* Used when adding symbols. */
3142 struct ppc_link_hash_entry *dot_syms;
3143
33e44f2e 3144 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3145 asection *glink;
9e390558 3146 asection *global_entry;
82bd7b59 3147 asection *sfpr;
2d7ad24e
AM
3148 asection *pltlocal;
3149 asection *relpltlocal;
4ce794b7
AM
3150 asection *brlt;
3151 asection *relbrlt;
58d180e8 3152 asection *glink_eh_frame;
ec338859 3153
8387904d
AM
3154 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3155 struct ppc_link_hash_entry *tls_get_addr;
3156 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3157
927be08e
AM
3158 /* The size of reliplt used by got entry relocs. */
3159 bfd_size_type got_reli_size;
3160
9b5ecbd0 3161 /* Statistics. */
7341d5e2 3162 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 3163
ee75fd95
AM
3164 /* Number of stubs against global syms. */
3165 unsigned long stub_globals;
3166
ee67d69a
AM
3167 /* Set if we're linking code with function descriptors. */
3168 unsigned int opd_abi:1;
3169
4c52953f 3170 /* Support for multiple toc sections. */
33c0ec9d 3171 unsigned int do_multi_toc:1;
4c52953f 3172 unsigned int multi_toc_needed:1;
927be08e 3173 unsigned int second_toc_pass:1;
67f0cbdb 3174 unsigned int do_toc_opt:1;
4c52953f 3175
9a23f96e
AM
3176 /* Set if tls optimization is enabled. */
3177 unsigned int do_tls_opt:1;
3178
3e04d765
AM
3179 /* Set if inline plt calls should be converted to direct calls. */
3180 unsigned int can_convert_all_inline_plt:1;
3181
5d1634d7 3182 /* Set on error. */
99877b66 3183 unsigned int stub_error:1;
721956f4 3184
8c5b4e52
AM
3185 /* Whether func_desc_adjust needs to be run over symbols. */
3186 unsigned int need_func_desc_adj:1;
721956f4 3187
82e66161
AM
3188 /* Whether there exist local gnu indirect function resolvers,
3189 referenced by dynamic relocations. */
3190 unsigned int local_ifunc_resolver:1;
3191 unsigned int maybe_local_ifunc_resolver:1;
3192
f378ab09
AM
3193 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3194 unsigned int has_plt_localentry0:1;
3195
5663e321
AM
3196 /* Whether calls are made via the PLT from NOTOC functions. */
3197 unsigned int notoc_plt:1;
3198
04bdff6a
AM
3199 /* Whether to use powerxx instructions in linkage stubs. */
3200 unsigned int powerxx_stubs:1;
3201
721956f4
AM
3202 /* Incremented every time we size stubs. */
3203 unsigned int stub_iteration;
5d1634d7 3204
87d72d41
AM
3205 /* Small local sym cache. */
3206 struct sym_cache sym_cache;
65f38f15
AM
3207};
3208
4c52953f
AM
3209/* Rename some of the generic section flags to better document how they
3210 are used here. */
b0dddeec
AM
3211
3212/* Nonzero if this section has TLS related relocations. */
3213#define has_tls_reloc sec_flg0
3214
23cedd1d 3215/* Nonzero if this section has an old-style call to __tls_get_addr. */
b0dddeec
AM
3216#define has_tls_get_addr_call sec_flg1
3217
3218/* Nonzero if this section has any toc or got relocs. */
3219#define has_toc_reloc sec_flg2
3220
3221/* Nonzero if this section has a call to another section that uses
3222 the toc or got. */
d77c8a4b 3223#define makes_toc_func_call sec_flg3
b0dddeec
AM
3224
3225/* Recursion protection when determining above flag. */
d77c8a4b 3226#define call_check_in_progress sec_flg4
70cc837d 3227#define call_check_done sec_flg5
4c52953f 3228
65f38f15
AM
3229/* Get the ppc64 ELF linker hash table from a link_info structure. */
3230
3231#define ppc_hash_table(p) \
4dfe6ac6
NC
3232 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3233 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3234
721956f4
AM
3235#define ppc_stub_hash_lookup(table, string, create, copy) \
3236 ((struct ppc_stub_hash_entry *) \
3237 bfd_hash_lookup ((table), (string), (create), (copy)))
3238
3239#define ppc_branch_hash_lookup(table, string, create, copy) \
3240 ((struct ppc_branch_hash_entry *) \
3241 bfd_hash_lookup ((table), (string), (create), (copy)))
3242
3243/* Create an entry in the stub hash table. */
3244
3245static struct bfd_hash_entry *
4ce794b7
AM
3246stub_hash_newfunc (struct bfd_hash_entry *entry,
3247 struct bfd_hash_table *table,
3248 const char *string)
721956f4
AM
3249{
3250 /* Allocate the structure if it has not already been allocated by a
3251 subclass. */
3252 if (entry == NULL)
3253 {
3254 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3255 if (entry == NULL)
3256 return entry;
3257 }
3258
3259 /* Call the allocation method of the superclass. */
3260 entry = bfd_hash_newfunc (entry, table, string);
3261 if (entry != NULL)
3262 {
3263 struct ppc_stub_hash_entry *eh;
3264
3265 /* Initialize the local fields. */
3266 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3267 eh->stub_type = ppc_stub_none;
6f20ed8a 3268 eh->group = NULL;
721956f4
AM
3269 eh->stub_offset = 0;
3270 eh->target_value = 0;
3271 eh->target_section = NULL;
721956f4 3272 eh->h = NULL;
6911b7dc 3273 eh->plt_ent = NULL;
6911b7dc 3274 eh->other = 0;
721956f4
AM
3275 }
3276
3277 return entry;
3278}
3279
3280/* Create an entry in the branch hash table. */
3281
3282static struct bfd_hash_entry *
4ce794b7
AM
3283branch_hash_newfunc (struct bfd_hash_entry *entry,
3284 struct bfd_hash_table *table,
3285 const char *string)
721956f4
AM
3286{
3287 /* Allocate the structure if it has not already been allocated by a
3288 subclass. */
3289 if (entry == NULL)
3290 {
3291 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3292 if (entry == NULL)
3293 return entry;
3294 }
3295
3296 /* Call the allocation method of the superclass. */
3297 entry = bfd_hash_newfunc (entry, table, string);
3298 if (entry != NULL)
3299 {
3300 struct ppc_branch_hash_entry *eh;
3301
3302 /* Initialize the local fields. */
3303 eh = (struct ppc_branch_hash_entry *) entry;
3304 eh->offset = 0;
3305 eh->iter = 0;
3306 }
3307
3308 return entry;
3309}
3310
65f38f15
AM
3311/* Create an entry in a ppc64 ELF linker hash table. */
3312
3313static struct bfd_hash_entry *
4ce794b7
AM
3314link_hash_newfunc (struct bfd_hash_entry *entry,
3315 struct bfd_hash_table *table,
3316 const char *string)
65f38f15
AM
3317{
3318 /* Allocate the structure if it has not already been allocated by a
3319 subclass. */
3320 if (entry == NULL)
3321 {
3322 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3323 if (entry == NULL)
3324 return entry;
3325 }
3326
3327 /* Call the allocation method of the superclass. */
3328 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3329 if (entry != NULL)
3330 {
3331 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3332
b3fac117 3333 memset (&eh->u.stub_cache, 0,
908b32fc 3334 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3335 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3336
3337 /* When making function calls, old ABI code references function entry
3338 points (dot symbols), while new ABI code references the function
3339 descriptor symbol. We need to make any combination of reference and
3340 definition work together, without breaking archive linking.
3341
3342 For a defined function "foo" and an undefined call to "bar":
3343 An old object defines "foo" and ".foo", references ".bar" (possibly
3344 "bar" too).
3345 A new object defines "foo" and references "bar".
3346
3347 A new object thus has no problem with its undefined symbols being
3348 satisfied by definitions in an old object. On the other hand, the
3349 old object won't have ".bar" satisfied by a new object.
3350
3351 Keep a list of newly added dot-symbols. */
3352
3353 if (string[0] == '.')
3354 {
3355 struct ppc_link_hash_table *htab;
3356
3357 htab = (struct ppc_link_hash_table *) table;
3358 eh->u.next_dot_sym = htab->dot_syms;
3359 htab->dot_syms = eh;
3360 }
65f38f15
AM
3361 }
3362
3363 return entry;
3364}
3365
2cdcc330
AM
3366struct tocsave_entry
3367{
3b421ab3
AM
3368 asection *sec;
3369 bfd_vma offset;
3370};
3371
3372static hashval_t
3373tocsave_htab_hash (const void *p)
3374{
3375 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3376 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3377}
3378
3379static int
3380tocsave_htab_eq (const void *p1, const void *p2)
3381{
3382 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3383 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3384 return e1->sec == e2->sec && e1->offset == e2->offset;
3385}
3386
68faa637
AM
3387/* Destroy a ppc64 ELF linker hash table. */
3388
3389static void
d495ab0d 3390ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3391{
d495ab0d 3392 struct ppc_link_hash_table *htab;
68faa637 3393
d495ab0d 3394 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3395 if (htab->tocsave_htab)
3396 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3397 bfd_hash_table_free (&htab->branch_hash_table);
3398 bfd_hash_table_free (&htab->stub_hash_table);
3399 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3400}
3401
65f38f15
AM
3402/* Create a ppc64 ELF linker hash table. */
3403
3404static struct bfd_link_hash_table *
4ce794b7 3405ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3406{
3407 struct ppc_link_hash_table *htab;
3408 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3409
4ce794b7 3410 htab = bfd_zmalloc (amt);
65f38f15
AM
3411 if (htab == NULL)
3412 return NULL;
3413
66eb6687 3414 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3415 sizeof (struct ppc_link_hash_entry),
3416 PPC64_ELF_DATA))
65f38f15 3417 {
e2d34d7d 3418 free (htab);
65f38f15
AM
3419 return NULL;
3420 }
3421
721956f4 3422 /* Init the stub hash table too. */
66eb6687
AM
3423 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3424 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3425 {
d495ab0d 3426 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3427 return NULL;
3428 }
721956f4
AM
3429
3430 /* And the branch hash table. */
66eb6687
AM
3431 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3432 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3433 {
3434 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3435 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3436 return NULL;
3437 }
721956f4 3438
3b421ab3
AM
3439 htab->tocsave_htab = htab_try_create (1024,
3440 tocsave_htab_hash,
3441 tocsave_htab_eq,
3442 NULL);
3443 if (htab->tocsave_htab == NULL)
2915c55b 3444 {
d495ab0d 3445 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3446 return NULL;
3447 }
d495ab0d 3448 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3449
3254fd24
AM
3450 /* Initializing two fields of the union is just cosmetic. We really
3451 only care about glist, but when compiled on a 32-bit host the
3452 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3453 debugger inspection of these fields look nicer. */
a6aa5195
AM
3454 htab->elf.init_got_refcount.refcount = 0;
3455 htab->elf.init_got_refcount.glist = NULL;
3456 htab->elf.init_plt_refcount.refcount = 0;
3457 htab->elf.init_plt_refcount.glist = NULL;
3458 htab->elf.init_got_offset.offset = 0;
3459 htab->elf.init_got_offset.glist = NULL;
3460 htab->elf.init_plt_offset.offset = 0;
3461 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3462
65f38f15
AM
3463 return &htab->elf.root;
3464}
3465
bfeb4a28
AM
3466/* Create sections for linker generated code. */
3467
3468static bfd_boolean
3469create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3470{
3471 struct ppc_link_hash_table *htab;
3472 flagword flags;
3473
3474 htab = ppc_hash_table (info);
3475
bfeb4a28
AM
3476 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3477 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3478 if (htab->params->save_restore_funcs)
3479 {
3480 /* Create .sfpr for code to save and restore fp regs. */
3481 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3482 flags);
3483 if (htab->sfpr == NULL
2cdcc330 3484 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b32547cd
AM
3485 return FALSE;
3486 }
3487
3488 if (bfd_link_relocatable (info))
3489 return TRUE;
bfeb4a28
AM
3490
3491 /* Create .glink for lazy dynamic linking support. */
3492 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3493 flags);
3494 if (htab->glink == NULL
2cdcc330 3495 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
bfeb4a28
AM
3496 return FALSE;
3497
9e390558
AM
3498 /* The part of .glink used by global entry stubs, separate so that
3499 it can be aligned appropriately without affecting htab->glink. */
3500 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3501 flags);
3502 if (htab->global_entry == NULL
2cdcc330 3503 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
9e390558
AM
3504 return FALSE;
3505
bfeb4a28
AM
3506 if (!info->no_ld_generated_unwind_info)
3507 {
3508 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3509 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3510 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3511 ".eh_frame",
3512 flags);
3513 if (htab->glink_eh_frame == NULL
3514 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3515 return FALSE;
3516 }
3517
3518 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3519 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3520 if (htab->elf.iplt == NULL
2cdcc330 3521 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
3522 return FALSE;
3523
3524 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3525 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3526 htab->elf.irelplt
3527 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3528 if (htab->elf.irelplt == NULL
2cdcc330 3529 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
3530 return FALSE;
3531
3532 /* Create branch lookup table for plt_branch stubs. */
3533 flags = (SEC_ALLOC | SEC_LOAD
3534 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3535 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3536 flags);
3537 if (htab->brlt == NULL
2cdcc330 3538 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
bfeb4a28
AM
3539 return FALSE;
3540
2d7ad24e
AM
3541 /* Local plt entries, put in .branch_lt but a separate section for
3542 convenience. */
3543 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3544 flags);
3545 if (htab->pltlocal == NULL
2cdcc330 3546 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
2d7ad24e
AM
3547 return FALSE;
3548
0e1862bb 3549 if (!bfd_link_pic (info))
bfeb4a28
AM
3550 return TRUE;
3551
3552 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3553 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3554 htab->relbrlt
3555 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3556 if (htab->relbrlt == NULL
2cdcc330 3557 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
bfeb4a28
AM
3558 return FALSE;
3559
2d7ad24e
AM
3560 htab->relpltlocal
3561 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3562 if (htab->relpltlocal == NULL
2cdcc330 3563 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
2d7ad24e
AM
3564 return FALSE;
3565
bfeb4a28
AM
3566 return TRUE;
3567}
3568
e717da7e
AM
3569/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3570
bfeb4a28 3571bfd_boolean
e7d1c40c
AM
3572ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3573 struct ppc64_elf_params *params)
e717da7e
AM
3574{
3575 struct ppc_link_hash_table *htab;
3576
e7d1c40c 3577 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3578
3579/* Always hook our dynamic sections into the first bfd, which is the
3580 linker created stub bfd. This ensures that the GOT header is at
3581 the start of the output TOC section. */
3582 htab = ppc_hash_table (info);
e7d1c40c
AM
3583 htab->elf.dynobj = params->stub_bfd;
3584 htab->params = params;
bfeb4a28 3585
bfeb4a28 3586 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3587}
3588
721956f4
AM
3589/* Build a name for an entry in the stub hash table. */
3590
3591static char *
4ce794b7
AM
3592ppc_stub_name (const asection *input_section,
3593 const asection *sym_sec,
3594 const struct ppc_link_hash_entry *h,
3595 const Elf_Internal_Rela *rel)
721956f4
AM
3596{
3597 char *stub_name;
bcaa2f82 3598 ssize_t len;
721956f4
AM
3599
3600 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3601 offsets from a sym as a branch target? In fact, we could
3602 probably assume the addend is always zero. */
3603 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3604
3605 if (h)
3606 {
3607 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3608 stub_name = bfd_malloc (len);
46de2a7c
AM
3609 if (stub_name == NULL)
3610 return stub_name;
3611
bcaa2f82
AM
3612 len = sprintf (stub_name, "%08x.%s+%x",
3613 input_section->id & 0xffffffff,
3614 h->elf.root.root.string,
3615 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3616 }
3617 else
3618 {
ad8e1ba5 3619 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3620 stub_name = bfd_malloc (len);
46de2a7c
AM
3621 if (stub_name == NULL)
3622 return stub_name;
3623
bcaa2f82
AM
3624 len = sprintf (stub_name, "%08x.%x:%x+%x",
3625 input_section->id & 0xffffffff,
3626 sym_sec->id & 0xffffffff,
3627 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3628 (int) rel->r_addend & 0xffffffff);
721956f4 3629 }
bcaa2f82 3630 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3631 stub_name[len - 2] = 0;
721956f4
AM
3632 return stub_name;
3633}
3634
3635/* Look up an entry in the stub hash. Stub entries are cached because
3636 creating the stub name takes a bit of time. */
3637
3638static struct ppc_stub_hash_entry *
4ce794b7
AM
3639ppc_get_stub_entry (const asection *input_section,
3640 const asection *sym_sec,
039b3fef 3641 struct ppc_link_hash_entry *h,
4ce794b7
AM
3642 const Elf_Internal_Rela *rel,
3643 struct ppc_link_hash_table *htab)
721956f4
AM
3644{
3645 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3646 struct map_stub *group;
721956f4
AM
3647
3648 /* If this input section is part of a group of sections sharing one
3649 stub section, then use the id of the first section in the group.
3650 Stub names need to include a section id, as there may well be
3651 more than one stub used to reach say, printf, and we need to
3652 distinguish between them. */
6f20ed8a 3653 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3654 if (group == NULL)
3655 return NULL;
721956f4 3656
b3fac117
AM
3657 if (h != NULL && h->u.stub_cache != NULL
3658 && h->u.stub_cache->h == h
6f20ed8a 3659 && h->u.stub_cache->group == group)
721956f4 3660 {
b3fac117 3661 stub_entry = h->u.stub_cache;
721956f4
AM
3662 }
3663 else
3664 {
3665 char *stub_name;
3666
6f20ed8a 3667 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3668 if (stub_name == NULL)
3669 return NULL;
3670
3671 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3672 stub_name, FALSE, FALSE);
721956f4 3673 if (h != NULL)
b3fac117 3674 h->u.stub_cache = stub_entry;
721956f4
AM
3675
3676 free (stub_name);
3677 }
3678
3679 return stub_entry;
3680}
3681
3682/* Add a new stub entry to the stub hash. Not all fields of the new
3683 stub entry are initialised. */
3684
3685static struct ppc_stub_hash_entry *
4ce794b7
AM
3686ppc_add_stub (const char *stub_name,
3687 asection *section,
25f53a85 3688 struct bfd_link_info *info)
721956f4 3689{
25f53a85 3690 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3691 struct map_stub *group;
721956f4
AM
3692 asection *link_sec;
3693 asection *stub_sec;
3694 struct ppc_stub_hash_entry *stub_entry;
3695
6f20ed8a
AM
3696 group = htab->sec_info[section->id].u.group;
3697 link_sec = group->link_sec;
3698 stub_sec = group->stub_sec;
721956f4
AM
3699 if (stub_sec == NULL)
3700 {
6f20ed8a
AM
3701 size_t namelen;
3702 bfd_size_type len;
3703 char *s_name;
721956f4 3704
6f20ed8a
AM
3705 namelen = strlen (link_sec->name);
3706 len = namelen + sizeof (STUB_SUFFIX);
3707 s_name = bfd_alloc (htab->params->stub_bfd, len);
3708 if (s_name == NULL)
3709 return NULL;
721956f4 3710
6f20ed8a
AM
3711 memcpy (s_name, link_sec->name, namelen);
3712 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3713 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3714 if (stub_sec == NULL)
3715 return NULL;
3716 group->stub_sec = stub_sec;
721956f4
AM
3717 }
3718
3719 /* Enter this entry into the linker stub hash table. */
3720 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3721 TRUE, FALSE);
721956f4
AM
3722 if (stub_entry == NULL)
3723 {
695344c0 3724 /* xgettext:c-format */
cf97bcb0
AM
3725 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3726 section->owner, stub_name);
721956f4
AM
3727 return NULL;
3728 }
3729
6f20ed8a 3730 stub_entry->group = group;
721956f4 3731 stub_entry->stub_offset = 0;
721956f4
AM
3732 return stub_entry;
3733}
3734
e717da7e
AM
3735/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3736 not already done. */
65f38f15 3737
b34976b6 3738static bfd_boolean
e717da7e 3739create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3740{
e717da7e
AM
3741 asection *got, *relgot;
3742 flagword flags;
3743 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3744
0c8d6e5c 3745 if (!is_ppc64_elf (abfd))
0ffa91dd 3746 return FALSE;
4dfe6ac6
NC
3747 if (htab == NULL)
3748 return FALSE;
0ffa91dd 3749
33e44f2e
AM
3750 if (!htab->elf.sgot
3751 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3752 return FALSE;
e717da7e
AM
3753
3754 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3755 | SEC_LINKER_CREATED);
3756
c456f082 3757 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3758 if (!got
e717da7e
AM
3759 || !bfd_set_section_alignment (abfd, got, 3))
3760 return FALSE;
65f38f15 3761
c456f082
AM
3762 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3763 flags | SEC_READONLY);
e717da7e 3764 if (!relgot
2cdcc330 3765 || !bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 3766 return FALSE;
e717da7e
AM
3767
3768 ppc64_elf_tdata (abfd)->got = got;
3769 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 3770 return TRUE;
65f38f15 3771}
5bd4f169 3772
b31867b6
AM
3773/* Follow indirect and warning symbol links. */
3774
3775static inline struct bfd_link_hash_entry *
3776follow_link (struct bfd_link_hash_entry *h)
3777{
3778 while (h->type == bfd_link_hash_indirect
3779 || h->type == bfd_link_hash_warning)
3780 h = h->u.i.link;
3781 return h;
3782}
3783
3784static inline struct elf_link_hash_entry *
3785elf_follow_link (struct elf_link_hash_entry *h)
3786{
3787 return (struct elf_link_hash_entry *) follow_link (&h->root);
3788}
3789
3790static inline struct ppc_link_hash_entry *
3791ppc_follow_link (struct ppc_link_hash_entry *h)
3792{
3793 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3794}
3795
40d16e0b
AM
3796/* Merge PLT info on FROM with that on TO. */
3797
3798static void
3799move_plt_plist (struct ppc_link_hash_entry *from,
3800 struct ppc_link_hash_entry *to)
3801{
3802 if (from->elf.plt.plist != NULL)
3803 {
3804 if (to->elf.plt.plist != NULL)
3805 {
3806 struct plt_entry **entp;
3807 struct plt_entry *ent;
3808
3809 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3810 {
3811 struct plt_entry *dent;
3812
3813 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3814 if (dent->addend == ent->addend)
3815 {
3816 dent->plt.refcount += ent->plt.refcount;
3817 *entp = ent->next;
3818 break;
3819 }
3820 if (dent == NULL)
3821 entp = &ent->next;
3822 }
3823 *entp = to->elf.plt.plist;
3824 }
3825
3826 to->elf.plt.plist = from->elf.plt.plist;
3827 from->elf.plt.plist = NULL;
3828 }
3829}
3830
65f38f15
AM
3831/* Copy the extra info we tack onto an elf_link_hash_entry. */
3832
3833static void
fcfa13d2
AM
3834ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3835 struct elf_link_hash_entry *dir,
3836 struct elf_link_hash_entry *ind)
65f38f15
AM
3837{
3838 struct ppc_link_hash_entry *edir, *eind;
3839
3840 edir = (struct ppc_link_hash_entry *) dir;
3841 eind = (struct ppc_link_hash_entry *) ind;
3842
c79d6685
AM
3843 edir->is_func |= eind->is_func;
3844 edir->is_func_descriptor |= eind->is_func_descriptor;
3845 edir->tls_mask |= eind->tls_mask;
3846 if (eind->oh != NULL)
3847 edir->oh = ppc_follow_link (eind->oh);
3848
474436e6 3849 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
3850 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3851 edir->elf.ref_regular |= eind->elf.ref_regular;
3852 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 3853 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
3854 edir->elf.needs_plt |= eind->elf.needs_plt;
3855 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 3856
d311bc8b
AM
3857 /* If we were called to copy over info for a weak sym, don't copy
3858 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3859 in order to simplify readonly_dynrelocs and save a field in the
3860 symbol hash entry, but that means dyn_relocs can't be used in any
3861 tests about a specific symbol, or affect other symbol flags which
ab2477e1 3862 are then tested. */
d311bc8b 3863 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 3864 return;
d311bc8b 3865
411e1bfb 3866 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 3867 if (eind->dyn_relocs != NULL)
65f38f15 3868 {
bbd7ec4a
AM
3869 if (edir->dyn_relocs != NULL)
3870 {
6061a67d
AM
3871 struct elf_dyn_relocs **pp;
3872 struct elf_dyn_relocs *p;
bbd7ec4a 3873
fcfa13d2 3874 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
3875 list. Merge any entries against the same section. */
3876 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3877 {
6061a67d 3878 struct elf_dyn_relocs *q;
bbd7ec4a
AM
3879
3880 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3881 if (q->sec == p->sec)
3882 {
3883 q->pc_count += p->pc_count;
3884 q->count += p->count;
3885 *pp = p->next;
3886 break;
3887 }
3888 if (q == NULL)
3889 pp = &p->next;
3890 }
3891 *pp = edir->dyn_relocs;
3892 }
3893
65f38f15
AM
3894 edir->dyn_relocs = eind->dyn_relocs;
3895 eind->dyn_relocs = NULL;
3896 }
65f38f15 3897
81848ca0
AM
3898 /* Copy over got entries that we may have already seen to the
3899 symbol which just became indirect. */
411e1bfb
AM
3900 if (eind->elf.got.glist != NULL)
3901 {
3902 if (edir->elf.got.glist != NULL)
3903 {
3904 struct got_entry **entp;
3905 struct got_entry *ent;
3906
3907 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3908 {
3909 struct got_entry *dent;
3910
3911 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3912 if (dent->addend == ent->addend
e717da7e 3913 && dent->owner == ent->owner
411e1bfb
AM
3914 && dent->tls_type == ent->tls_type)
3915 {
3916 dent->got.refcount += ent->got.refcount;
3917 *entp = ent->next;
3918 break;
3919 }
3920 if (dent == NULL)
3921 entp = &ent->next;
3922 }
3923 *entp = edir->elf.got.glist;
3924 }
3925
3926 edir->elf.got.glist = eind->elf.got.glist;
3927 eind->elf.got.glist = NULL;
3928 }
3929
3930 /* And plt entries. */
40d16e0b 3931 move_plt_plist (eind, edir);
411e1bfb 3932
fcfa13d2 3933 if (eind->elf.dynindx != -1)
411e1bfb 3934 {
fcfa13d2
AM
3935 if (edir->elf.dynindx != -1)
3936 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3937 edir->elf.dynstr_index);
411e1bfb
AM
3938 edir->elf.dynindx = eind->elf.dynindx;
3939 edir->elf.dynstr_index = eind->elf.dynstr_index;
3940 eind->elf.dynindx = -1;
3941 eind->elf.dynstr_index = 0;
3942 }
411e1bfb
AM
3943}
3944
8387904d
AM
3945/* Find the function descriptor hash entry from the given function code
3946 hash entry FH. Link the entries via their OH fields. */
3947
3948static struct ppc_link_hash_entry *
b31867b6 3949lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
3950{
3951 struct ppc_link_hash_entry *fdh = fh->oh;
3952
3953 if (fdh == NULL)
3954 {
3955 const char *fd_name = fh->elf.root.root.string + 1;
3956
3957 fdh = (struct ppc_link_hash_entry *)
3958 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
3959 if (fdh == NULL)
3960 return fdh;
3961
3962 fdh->is_func_descriptor = 1;
3963 fdh->oh = fh;
3964 fh->is_func = 1;
3965 fh->oh = fdh;
8387904d
AM
3966 }
3967
8c5b4e52
AM
3968 fdh = ppc_follow_link (fdh);
3969 fdh->is_func_descriptor = 1;
3970 fdh->oh = fh;
3971 return fdh;
8387904d
AM
3972}
3973
8c5b4e52 3974/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
3975
3976static struct ppc_link_hash_entry *
3977make_fdh (struct bfd_link_info *info,
908b32fc 3978 struct ppc_link_hash_entry *fh)
bb700d78 3979{
8c5b4e52
AM
3980 bfd *abfd = fh->elf.root.u.undef.abfd;
3981 struct bfd_link_hash_entry *bh = NULL;
bb700d78 3982 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
3983 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3984 ? BSF_WEAK
3985 : BSF_GLOBAL);
3986
3987 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3988 fh->elf.root.root.string + 1,
3989 flags, bfd_und_section_ptr, 0,
3990 NULL, FALSE, FALSE, &bh))
bb700d78
AM
3991 return NULL;
3992
3993 fdh = (struct ppc_link_hash_entry *) bh;
3994 fdh->elf.non_elf = 0;
908b32fc
AM
3995 fdh->fake = 1;
3996 fdh->is_func_descriptor = 1;
3997 fdh->oh = fh;
3998 fh->is_func = 1;
3999 fh->oh = fdh;
bb700d78
AM
4000 return fdh;
4001}
4002
8387904d
AM
4003/* Fix function descriptor symbols defined in .opd sections to be
4004 function type. */
555cd476
AM
4005
4006static bfd_boolean
c16153ae 4007ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4008 struct bfd_link_info *info,
555cd476 4009 Elf_Internal_Sym *isym,
6911b7dc 4010 const char **name,
555cd476
AM
4011 flagword *flags ATTRIBUTE_UNUSED,
4012 asection **sec,
b53dfeb2 4013 bfd_vma *value)
555cd476 4014{
b53dfeb2 4015 if (*sec != NULL
f1885d1e 4016 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4017 {
4018 asection *code_sec;
4019
4020 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4021 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4022 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4023
4024 /* If the symbol is a function defined in .opd, and the function
4025 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4026 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4027 && (*sec)->reloc_count != 0
4028 && opd_entry_value (*sec, *value, &code_sec, NULL,
4029 FALSE) != (bfd_vma) -1
4030 && discarded_section (code_sec))
4031 {
4032 *sec = bfd_und_section_ptr;
4033 isym->st_shndx = SHN_UNDEF;
4034 }
4035 }
dbd1e97e
AM
4036 else if (*sec != NULL
4037 && strcmp ((*sec)->name, ".toc") == 0
4038 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4039 {
4040 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4041 if (htab != NULL)
4042 htab->params->object_in_toc = 1;
4043 }
433817dd 4044
6911b7dc
AM
4045 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4046 {
4047 if (abiversion (ibfd) == 0)
4048 set_abiversion (ibfd, 2);
4049 else if (abiversion (ibfd) == 1)
4050 {
cf97bcb0
AM
4051 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4052 " for ABI version 1"), *name);
6911b7dc
AM
4053 bfd_set_error (bfd_error_bad_value);
4054 return FALSE;
4055 }
4056 }
4057
555cd476
AM
4058 return TRUE;
4059}
4060
6911b7dc
AM
4061/* Merge non-visibility st_other attributes: local entry point. */
4062
4063static void
4064ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4065 const Elf_Internal_Sym *isym,
4066 bfd_boolean definition,
4067 bfd_boolean dynamic)
4068{
f378ab09 4069 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
4070 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4071 | ELF_ST_VISIBILITY (h->other));
4072}
4073
8c5b4e52
AM
4074/* Hook called on merging a symbol. We use this to clear "fake" since
4075 we now have a real symbol. */
4076
4077static bfd_boolean
4078ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4079 const Elf_Internal_Sym *isym,
8c5b4e52
AM
4080 asection **psec ATTRIBUTE_UNUSED,
4081 bfd_boolean newdef ATTRIBUTE_UNUSED,
4082 bfd_boolean olddef ATTRIBUTE_UNUSED,
4083 bfd *oldbfd ATTRIBUTE_UNUSED,
4084 const asection *oldsec ATTRIBUTE_UNUSED)
4085{
4086 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
4087 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4088 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
4089 return TRUE;
4090}
4091
8387904d 4092/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4093 inclusion of a new ABI object archive that defines "bar".
4094 NAME is a symbol defined in an archive. Return a symbol in the hash
4095 table that might be satisfied by the archive symbols. */
8387904d
AM
4096
4097static struct elf_link_hash_entry *
4098ppc64_elf_archive_symbol_lookup (bfd *abfd,
4099 struct bfd_link_info *info,
4100 const char *name)
4101{
4102 struct elf_link_hash_entry *h;
4103 char *dot_name;
4104 size_t len;
4105
4106 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4107 if (h != NULL
4108 /* Don't return this sym if it is a fake function descriptor
4109 created by add_symbol_adjust. */
8c5b4e52 4110 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
4111 return h;
4112
4113 if (name[0] == '.')
4114 return h;
4115
4116 len = strlen (name);
4117 dot_name = bfd_alloc (abfd, len + 2);
4118 if (dot_name == NULL)
e99955cd 4119 return (struct elf_link_hash_entry *) -1;
8387904d
AM
4120 dot_name[0] = '.';
4121 memcpy (dot_name + 1, name, len + 1);
4122 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4123 bfd_release (abfd, dot_name);
4124 return h;
4125}
4126
4127/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4128 new ABI object defines "bar". Well, at least, undefined dot symbols
4129 are made weak. This stops later archive searches from including an
4130 object if we already have a function descriptor definition. It also
35b0ce59
AM
4131 prevents the linker complaining about undefined symbols.
4132 We also check and correct mismatched symbol visibility here. The
4133 most restrictive visibility of the function descriptor and the
4134 function entry symbol is used. */
8387904d
AM
4135
4136static bfd_boolean
b3fac117 4137add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4138{
8387904d
AM
4139 struct ppc_link_hash_table *htab;
4140 struct ppc_link_hash_entry *fdh;
4141
b3fac117
AM
4142 if (eh->elf.root.type == bfd_link_hash_warning)
4143 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4144
8400d40d
AM
4145 if (eh->elf.root.type == bfd_link_hash_indirect)
4146 return TRUE;
4147
b3fac117
AM
4148 if (eh->elf.root.root.string[0] != '.')
4149 abort ();
8387904d 4150
b3fac117 4151 htab = ppc_hash_table (info);
4dfe6ac6
NC
4152 if (htab == NULL)
4153 return FALSE;
4154
b31867b6 4155 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4156 if (fdh == NULL
4157 && !bfd_link_relocatable (info)
4158 && (eh->elf.root.type == bfd_link_hash_undefined
4159 || eh->elf.root.type == bfd_link_hash_undefweak)
4160 && eh->elf.ref_regular)
4161 {
4162 /* Make an undefined function descriptor sym, in order to
4163 pull in an --as-needed shared lib. Archives are handled
4164 elsewhere. */
4165 fdh = make_fdh (info, eh);
4166 if (fdh == NULL)
4167 return FALSE;
bb700d78 4168 }
8c5b4e52
AM
4169
4170 if (fdh != NULL)
8387904d 4171 {
35b0ce59
AM
4172 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4173 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4174
4175 /* Make both descriptor and entry symbol have the most
4176 constraining visibility of either symbol. */
35b0ce59
AM
4177 if (entry_vis < descr_vis)
4178 fdh->elf.other += entry_vis - descr_vis;
4179 else if (entry_vis > descr_vis)
4180 eh->elf.other += descr_vis - entry_vis;
4181
8c5b4e52
AM
4182 /* Propagate reference flags from entry symbol to function
4183 descriptor symbol. */
bc4e12de 4184 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4185 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4186 fdh->elf.ref_regular |= eh->elf.ref_regular;
4187 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4188
4189 if (!fdh->elf.forced_local
4190 && fdh->elf.dynindx == -1
4191 && fdh->elf.versioned != versioned_hidden
4192 && (bfd_link_dll (info)
4193 || fdh->elf.def_dynamic
4194 || fdh->elf.ref_dynamic)
4195 && (eh->elf.ref_regular
4196 || eh->elf.def_regular))
4197 {
2cdcc330 4198 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
8c5b4e52 4199 return FALSE;
35b0ce59 4200 }
8387904d 4201 }
99877b66 4202
8387904d
AM
4203 return TRUE;
4204}
4205
f6c7c3e8
AM
4206/* Set up opd section info and abiversion for IBFD, and process list
4207 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4208
8387904d 4209static bfd_boolean
f6c7c3e8 4210ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4211{
99877b66 4212 struct ppc_link_hash_table *htab;
b3fac117 4213 struct ppc_link_hash_entry **p, *eh;
459609d6 4214 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4215
459609d6 4216 if (opd != NULL && opd->size != 0)
b3fac117 4217 {
b9399fcf
AM
4218 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4219 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4220
459609d6
AM
4221 if (abiversion (ibfd) == 0)
4222 set_abiversion (ibfd, 1);
8a2058b5 4223 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4224 {
695344c0 4225 /* xgettext:c-format */
cf97bcb0
AM
4226 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4227 ibfd, abiversion (ibfd));
459609d6
AM
4228 bfd_set_error (bfd_error_bad_value);
4229 return FALSE;
f6c7c3e8 4230 }
b9399fcf 4231 }
f6c7c3e8 4232
b9399fcf
AM
4233 if (is_ppc64_elf (info->output_bfd))
4234 {
4235 /* For input files without an explicit abiversion in e_flags
4236 we should have flagged any with symbol st_other bits set
4237 as ELFv1 and above flagged those with .opd as ELFv2.
4238 Set the output abiversion if not yet set, and for any input
4239 still ambiguous, take its abiversion from the output.
4240 Differences in ABI are reported later. */
4241 if (abiversion (info->output_bfd) == 0)
4242 set_abiversion (info->output_bfd, abiversion (ibfd));
4243 else if (abiversion (ibfd) == 0)
4244 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4245 }
4246
459609d6
AM
4247 htab = ppc_hash_table (info);
4248 if (htab == NULL)
b9399fcf 4249 return TRUE;
459609d6 4250
b9399fcf
AM
4251 if (opd != NULL && opd->size != 0
4252 && (ibfd->flags & DYNAMIC) == 0
4253 && (opd->flags & SEC_RELOC) != 0
4254 && opd->reloc_count != 0
4255 && !bfd_is_abs_section (opd->output_section)
4256 && info->gc_sections)
4257 {
4258 /* Garbage collection needs some extra help with .opd sections.
4259 We don't want to necessarily keep everything referenced by
4260 relocs in .opd, as that would keep all functions. Instead,
4261 if we reference an .opd symbol (a function descriptor), we
4262 want to keep the function code symbol's section. This is
4263 easy for global symbols, but for local syms we need to keep
4264 information about the associated function section. */
4265 bfd_size_type amt;
4266 asection **opd_sym_map;
4267 Elf_Internal_Shdr *symtab_hdr;
4268 Elf_Internal_Rela *relocs, *rel_end, *rel;
4269
4270 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4271 opd_sym_map = bfd_zalloc (ibfd, amt);
4272 if (opd_sym_map == NULL)
4273 return FALSE;
4274 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4275 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4276 info->keep_memory);
4277 if (relocs == NULL)
4278 return FALSE;
4279 symtab_hdr = &elf_symtab_hdr (ibfd);
4280 rel_end = relocs + opd->reloc_count - 1;
4281 for (rel = relocs; rel < rel_end; rel++)
4282 {
4283 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4284 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4285
4286 if (r_type == R_PPC64_ADDR64
4287 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4288 && r_symndx < symtab_hdr->sh_info)
4289 {
4290 Elf_Internal_Sym *isym;
4291 asection *s;
4292
4293 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4294 if (isym == NULL)
4295 {
4296 if (elf_section_data (opd)->relocs != relocs)
4297 free (relocs);
4298 return FALSE;
4299 }
4300
4301 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4302 if (s != NULL && s != opd)
4303 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4304 }
4305 }
4306 if (elf_section_data (opd)->relocs != relocs)
4307 free (relocs);
4308 }
459609d6
AM
4309
4310 p = &htab->dot_syms;
4311 while ((eh = *p) != NULL)
4312 {
4313 *p = NULL;
4314 if (&eh->elf == htab->elf.hgot)
4315 ;
4316 else if (htab->elf.hgot == NULL
4317 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4318 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4319 else if (abiversion (ibfd) <= 1)
4320 {
4321 htab->need_func_desc_adj = 1;
4322 if (!add_symbol_adjust (eh, info))
4323 return FALSE;
4324 }
459609d6
AM
4325 p = &eh->u.next_dot_sym;
4326 }
b3fac117 4327 return TRUE;
8387904d
AM
4328}
4329
97fed1c9
JJ
4330/* Undo hash table changes when an --as-needed input file is determined
4331 not to be needed. */
4332
4333static bfd_boolean
e5034e59
AM
4334ppc64_elf_notice_as_needed (bfd *ibfd,
4335 struct bfd_link_info *info,
4336 enum notice_asneeded_action act)
97fed1c9 4337{
e5034e59
AM
4338 if (act == notice_not_needed)
4339 {
4340 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4341
e5034e59
AM
4342 if (htab == NULL)
4343 return FALSE;
4dfe6ac6 4344
e5034e59
AM
4345 htab->dot_syms = NULL;
4346 }
4347 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4348}
4349
aa374f67
AM
4350/* If --just-symbols against a final linked binary, then assume we need
4351 toc adjusting stubs when calling functions defined there. */
4352
4353static void
4354ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4355{
4356 if ((sec->flags & SEC_CODE) != 0
4357 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4358 && is_ppc64_elf (sec->owner))
4359 {
2c3f079f
AM
4360 if (abiversion (sec->owner) >= 2
4361 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4362 sec->has_toc_reloc = 1;
4363 }
4364 _bfd_elf_link_just_syms (sec, info);
4365}
4366
e054468f 4367static struct plt_entry **
4ce794b7
AM
4368update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4369 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4370{
4371 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4372 struct plt_entry **local_plt;
f961d9dd 4373 unsigned char *local_got_tls_masks;
411e1bfb
AM
4374
4375 if (local_got_ents == NULL)
4376 {
4377 bfd_size_type size = symtab_hdr->sh_info;
4378
e054468f
AM
4379 size *= (sizeof (*local_got_ents)
4380 + sizeof (*local_plt)
4381 + sizeof (*local_got_tls_masks));
4ce794b7 4382 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4383 if (local_got_ents == NULL)
e054468f 4384 return NULL;
411e1bfb
AM
4385 elf_local_got_ents (abfd) = local_got_ents;
4386 }
4387
37da22e5 4388 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4389 {
4390 struct got_entry *ent;
4391
4392 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4393 if (ent->addend == r_addend
4394 && ent->owner == abfd
4395 && ent->tls_type == tls_type)
411e1bfb
AM
4396 break;
4397 if (ent == NULL)
4398 {
4399 bfd_size_type amt = sizeof (*ent);
4ce794b7 4400 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4401 if (ent == NULL)
4402 return FALSE;
4403 ent->next = local_got_ents[r_symndx];
4404 ent->addend = r_addend;
e717da7e 4405 ent->owner = abfd;
411e1bfb 4406 ent->tls_type = tls_type;
927be08e 4407 ent->is_indirect = FALSE;
411e1bfb
AM
4408 ent->got.refcount = 0;
4409 local_got_ents[r_symndx] = ent;
4410 }
4411 ent->got.refcount += 1;
4412 }
4413
e054468f 4414 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4415 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4416 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4417
4418 return local_plt + r_symndx;
65f38f15
AM
4419}
4420
411e1bfb 4421static bfd_boolean
e054468f 4422update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4423{
411e1bfb 4424 struct plt_entry *ent;
1e2f5b6e 4425
e054468f 4426 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4427 if (ent->addend == addend)
4428 break;
4429 if (ent == NULL)
1e2f5b6e 4430 {
411e1bfb 4431 bfd_size_type amt = sizeof (*ent);
4ce794b7 4432 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4433 if (ent == NULL)
4434 return FALSE;
e054468f 4435 ent->next = *plist;
411e1bfb
AM
4436 ent->addend = addend;
4437 ent->plt.refcount = 0;
e054468f 4438 *plist = ent;
1e2f5b6e 4439 }
411e1bfb 4440 ent->plt.refcount += 1;
b34976b6 4441 return TRUE;
1e2f5b6e
AM
4442}
4443
e054468f
AM
4444static bfd_boolean
4445is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4446{
4447 return (r_type == R_PPC64_REL24
05d0e962 4448 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
4449 || r_type == R_PPC64_REL14
4450 || r_type == R_PPC64_REL14_BRTAKEN
4451 || r_type == R_PPC64_REL14_BRNTAKEN
4452 || r_type == R_PPC64_ADDR24
4453 || r_type == R_PPC64_ADDR14
4454 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4455 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4456 || r_type == R_PPC64_PLTCALL
4457 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4458}
4459
4460/* Relocs on inline plt call sequence insns prior to the call. */
4461
4462static bfd_boolean
4463is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4464{
4465 return (r_type == R_PPC64_PLT16_HA
4466 || r_type == R_PPC64_PLT16_HI
4467 || r_type == R_PPC64_PLT16_LO
4468 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4469 || r_type == R_PPC64_PLT_PCREL34
4470 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4471 || r_type == R_PPC64_PLTSEQ
4472 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4473}
4474
5bd4f169 4475/* Look through the relocs for a section during the first phase, and
65f38f15 4476 calculate needed space in the global offset table, procedure
5d1634d7 4477 linkage table, and dynamic reloc sections. */
5bd4f169 4478
b34976b6 4479static bfd_boolean
4ce794b7
AM
4480ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4481 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4482{
65f38f15 4483 struct ppc_link_hash_table *htab;
5bd4f169 4484 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4485 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4486 const Elf_Internal_Rela *rel;
4487 const Elf_Internal_Rela *rel_end;
5bd4f169 4488 asection *sreloc;
3a71aa26 4489 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 4490 bfd_boolean is_opd;
5bd4f169 4491
0e1862bb 4492 if (bfd_link_relocatable (info))
b34976b6 4493 return TRUE;
5bd4f169 4494
680a3378
AM
4495 /* Don't do anything special with non-loaded, non-alloced sections.
4496 In particular, any relocs in such sections should not affect GOT
4497 and PLT reference counting (ie. we don't allow them to create GOT
4498 or PLT entries), there's no possibility or desire to optimize TLS
4499 relocs, and there's not much point in propagating relocs to shared
4500 libs that the dynamic linker won't relocate. */
4501 if ((sec->flags & SEC_ALLOC) == 0)
4502 return TRUE;
4503
0c8d6e5c 4504 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4505
65f38f15 4506 htab = ppc_hash_table (info);
4dfe6ac6
NC
4507 if (htab == NULL)
4508 return FALSE;
4509
3a71aa26
AM
4510 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4511 FALSE, FALSE, TRUE);
4512 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4513 FALSE, FALSE, TRUE);
0ffa91dd 4514 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4515 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4516 sreloc = NULL;
b9399fcf 4517 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4518 rel_end = relocs + sec->reloc_count;
4519 for (rel = relocs; rel < rel_end; rel++)
4520 {
4521 unsigned long r_symndx;
4522 struct elf_link_hash_entry *h;
04c9666a 4523 enum elf_ppc64_reloc_type r_type;
727fc41e 4524 int tls_type;
7c8fe5c4 4525 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4526 struct plt_entry **ifunc, **plt_list;
5663e321 4527 bfd_vma sym_addend;
5bd4f169
AM
4528
4529 r_symndx = ELF64_R_SYM (rel->r_info);
4530 if (r_symndx < symtab_hdr->sh_info)
4531 h = NULL;
4532 else
973a3492
L
4533 {
4534 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4535 h = elf_follow_link (h);
1c865ab2
AM
4536
4537 if (h == htab->elf.hgot)
4538 sec->has_toc_reloc = 1;
973a3492 4539 }
5bd4f169 4540
727fc41e 4541 tls_type = 0;
e054468f 4542 ifunc = NULL;
5663e321
AM
4543 r_type = ELF64_R_TYPE (rel->r_info);
4544 switch (r_type)
4545 {
04bdff6a
AM
4546 case R_PPC64_D34:
4547 case R_PPC64_D34_LO:
4548 case R_PPC64_D34_HI30:
4549 case R_PPC64_D34_HA30:
4550 case R_PPC64_D28:
c213164a
AM
4551 case R_PPC64_TPREL34:
4552 case R_PPC64_DTPREL34:
04bdff6a
AM
4553 htab->powerxx_stubs = 1;
4554 /* Fall through. */
5663e321
AM
4555 default:
4556 /* Somewhat foolishly, because the ABIs don't specifically
4557 allow it, ppc64 gas and ld support GOT and PLT relocs
4558 with non-zero addends where the addend results in
4559 sym+addend being stored in the GOT or PLT entry. This
4560 can't be supported for pcrel relocs because the addend is
4561 used to specify the pcrel offset. */
4562 sym_addend = rel->r_addend;
4563 break;
04bdff6a
AM
4564
4565 case R_PPC64_PCREL34:
5663e321 4566 case R_PPC64_GOT_PCREL34:
c213164a
AM
4567 case R_PPC64_GOT_TLSGD34:
4568 case R_PPC64_GOT_TLSLD34:
4569 case R_PPC64_GOT_TPREL34:
4570 case R_PPC64_GOT_DTPREL34:
5663e321
AM
4571 case R_PPC64_PLT_PCREL34:
4572 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a
AM
4573 case R_PPC64_PCREL28:
4574 htab->powerxx_stubs = 1;
5663e321
AM
4575 sym_addend = 0;
4576 break;
4577 }
903b777d
AM
4578
4579 switch (r_type)
4580 {
4581 case R_PPC64_PLT16_HA:
4582 case R_PPC64_GOT_TLSLD16_HA:
4583 case R_PPC64_GOT_TLSGD16_HA:
4584 case R_PPC64_GOT_TPREL16_HA:
4585 case R_PPC64_GOT_DTPREL16_HA:
4586 case R_PPC64_GOT16_HA:
4587 case R_PPC64_TOC16_HA:
4588 case R_PPC64_PLT16_LO:
4589 case R_PPC64_PLT16_LO_DS:
4590 case R_PPC64_GOT_TLSLD16_LO:
4591 case R_PPC64_GOT_TLSGD16_LO:
4592 case R_PPC64_GOT_TPREL16_LO_DS:
4593 case R_PPC64_GOT_DTPREL16_LO_DS:
4594 case R_PPC64_GOT16_LO:
4595 case R_PPC64_GOT16_LO_DS:
4596 case R_PPC64_TOC16_LO:
4597 case R_PPC64_TOC16_LO_DS:
4598 case R_PPC64_GOT_PCREL34:
4599 ppc64_elf_tdata (abfd)->has_optrel = 1;
4600 ppc64_elf_section_data (sec)->has_optrel = 1;
4601 break;
4602 default:
4603 break;
4604 }
4605
25f23106
AM
4606 if (h != NULL)
4607 {
4608 if (h->type == STT_GNU_IFUNC)
4609 {
4610 h->needs_plt = 1;
4611 ifunc = &h->plt.plist;
4612 }
4613 }
4614 else
4615 {
4616 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4617 abfd, r_symndx);
4618 if (isym == NULL)
4619 return FALSE;
4620
4621 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4622 {
4623 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4624 sym_addend,
37da22e5 4625 NON_GOT | PLT_IFUNC);
25f23106
AM
4626 if (ifunc == NULL)
4627 return FALSE;
4628 }
4629 }
727fc41e 4630
a33d1f77 4631 switch (r_type)
5bd4f169 4632 {
727fc41e
AM
4633 case R_PPC64_TLSGD:
4634 case R_PPC64_TLSLD:
4635 /* These special tls relocs tie a call to __tls_get_addr with
4636 its parameter symbol. */
37da22e5
AM
4637 if (h != NULL)
4638 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4639 else
4640 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4641 sym_addend,
37da22e5
AM
4642 NON_GOT | TLS_TLS | TLS_MARK))
4643 return FALSE;
4644 sec->has_tls_reloc = 1;
727fc41e
AM
4645 break;
4646
411e1bfb
AM
4647 case R_PPC64_GOT_TLSLD16:
4648 case R_PPC64_GOT_TLSLD16_LO:
4649 case R_PPC64_GOT_TLSLD16_HI:
4650 case R_PPC64_GOT_TLSLD16_HA:
c213164a 4651 case R_PPC64_GOT_TLSLD34:
951fd09b 4652 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4653 goto dogottls;
4654
4655 case R_PPC64_GOT_TLSGD16:
4656 case R_PPC64_GOT_TLSGD16_LO:
4657 case R_PPC64_GOT_TLSGD16_HI:
4658 case R_PPC64_GOT_TLSGD16_HA:
c213164a 4659 case R_PPC64_GOT_TLSGD34:
951fd09b 4660 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4661 goto dogottls;
4662
4663 case R_PPC64_GOT_TPREL16_DS:
4664 case R_PPC64_GOT_TPREL16_LO_DS:
4665 case R_PPC64_GOT_TPREL16_HI:
4666 case R_PPC64_GOT_TPREL16_HA:
c213164a 4667 case R_PPC64_GOT_TPREL34:
7c8bbca5 4668 if (bfd_link_dll (info))
411e1bfb
AM
4669 info->flags |= DF_STATIC_TLS;
4670 tls_type = TLS_TLS | TLS_TPREL;
4671 goto dogottls;
4672
4673 case R_PPC64_GOT_DTPREL16_DS:
4674 case R_PPC64_GOT_DTPREL16_LO_DS:
4675 case R_PPC64_GOT_DTPREL16_HI:
4676 case R_PPC64_GOT_DTPREL16_HA:
c213164a 4677 case R_PPC64_GOT_DTPREL34:
411e1bfb
AM
4678 tls_type = TLS_TLS | TLS_DTPREL;
4679 dogottls:
4680 sec->has_tls_reloc = 1;
066f4018 4681 goto dogot;
411e1bfb 4682
903b777d
AM
4683 case R_PPC64_GOT16:
4684 case R_PPC64_GOT16_LO:
4685 case R_PPC64_GOT16_HI:
65f38f15 4686 case R_PPC64_GOT16_HA:
903b777d 4687 case R_PPC64_GOT16_DS:
066f4018 4688 case R_PPC64_GOT16_LO_DS:
4a421c53 4689 case R_PPC64_GOT_PCREL34:
066f4018 4690 dogot:
65f38f15 4691 /* This symbol requires a global offset table entry. */
4c52953f 4692 sec->has_toc_reloc = 1;
33c0ec9d
AM
4693 if (r_type == R_PPC64_GOT_TLSLD16
4694 || r_type == R_PPC64_GOT_TLSGD16
4695 || r_type == R_PPC64_GOT_TPREL16_DS
4696 || r_type == R_PPC64_GOT_DTPREL16_DS
4697 || r_type == R_PPC64_GOT16
4698 || r_type == R_PPC64_GOT16_DS)
4699 {
4700 htab->do_multi_toc = 1;
d77c8a4b 4701 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4702 }
4703
e717da7e
AM
4704 if (ppc64_elf_tdata (abfd)->got == NULL
4705 && !create_got_section (abfd, info))
b34976b6 4706 return FALSE;
5bd4f169
AM
4707
4708 if (h != NULL)
4709 {
411e1bfb
AM
4710 struct ppc_link_hash_entry *eh;
4711 struct got_entry *ent;
65f38f15 4712
411e1bfb
AM
4713 eh = (struct ppc_link_hash_entry *) h;
4714 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5663e321 4715 if (ent->addend == sym_addend
e717da7e 4716 && ent->owner == abfd
411e1bfb
AM
4717 && ent->tls_type == tls_type)
4718 break;
4719 if (ent == NULL)
5bd4f169 4720 {
411e1bfb 4721 bfd_size_type amt = sizeof (*ent);
4ce794b7 4722 ent = bfd_alloc (abfd, amt);
411e1bfb 4723 if (ent == NULL)
b34976b6 4724 return FALSE;
411e1bfb 4725 ent->next = eh->elf.got.glist;
5663e321 4726 ent->addend = sym_addend;
e717da7e 4727 ent->owner = abfd;
411e1bfb 4728 ent->tls_type = tls_type;
927be08e 4729 ent->is_indirect = FALSE;
411e1bfb
AM
4730 ent->got.refcount = 0;
4731 eh->elf.got.glist = ent;
5bd4f169 4732 }
411e1bfb 4733 ent->got.refcount += 1;
e7b938ca 4734 eh->tls_mask |= tls_type;
5bd4f169 4735 }
411e1bfb
AM
4736 else
4737 /* This is a global offset table entry for a local symbol. */
4738 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4739 sym_addend, tls_type))
411e1bfb 4740 return FALSE;
a345bc8d
AM
4741
4742 /* We may also need a plt entry if the symbol turns out to be
4743 an ifunc. */
0e1862bb 4744 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d 4745 {
5663e321 4746 if (!update_plt_info (abfd, &h->plt.plist, sym_addend))
a345bc8d
AM
4747 return FALSE;
4748 }
5bd4f169
AM
4749 break;
4750
5bd4f169 4751 case R_PPC64_PLT16_HA:
65f38f15
AM
4752 case R_PPC64_PLT16_HI:
4753 case R_PPC64_PLT16_LO:
08be3224 4754 case R_PPC64_PLT16_LO_DS:
5663e321
AM
4755 case R_PPC64_PLT_PCREL34:
4756 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
4757 case R_PPC64_PLT32:
4758 case R_PPC64_PLT64:
cbf95972
AM
4759 /* This symbol requires a procedure linkage table entry. */
4760 plt_list = ifunc;
4761 if (h != NULL)
e054468f 4762 {
e054468f
AM
4763 h->needs_plt = 1;
4764 if (h->root.root.string[0] == '.'
4765 && h->root.root.string[1] != '\0')
4766 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2d7ad24e 4767 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4768 plt_list = &h->plt.plist;
4769 }
4770 if (plt_list == NULL)
2d7ad24e 4771 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4772 sym_addend,
2d7ad24e 4773 NON_GOT | PLT_KEEP);
5663e321 4774 if (!update_plt_info (abfd, plt_list, sym_addend))
cbf95972 4775 return FALSE;
5bd4f169
AM
4776 break;
4777
4778 /* The following relocations don't need to propagate the
4779 relocation if linking a shared object since they are
4780 section relative. */
4781 case R_PPC64_SECTOFF:
4782 case R_PPC64_SECTOFF_LO:
4783 case R_PPC64_SECTOFF_HI:
4784 case R_PPC64_SECTOFF_HA:
4785 case R_PPC64_SECTOFF_DS:
4786 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4787 case R_PPC64_DTPREL16:
4788 case R_PPC64_DTPREL16_LO:
4789 case R_PPC64_DTPREL16_HI:
4790 case R_PPC64_DTPREL16_HA:
4791 case R_PPC64_DTPREL16_DS:
4792 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
4793 case R_PPC64_DTPREL16_HIGH:
4794 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
4795 case R_PPC64_DTPREL16_HIGHER:
4796 case R_PPC64_DTPREL16_HIGHERA:
4797 case R_PPC64_DTPREL16_HIGHEST:
4798 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4799 break;
4800
ad8e1ba5 4801 /* Nor do these. */
25f23106
AM
4802 case R_PPC64_REL16:
4803 case R_PPC64_REL16_LO:
4804 case R_PPC64_REL16_HI:
4805 case R_PPC64_REL16_HA:
4a969973
AM
4806 case R_PPC64_REL16_HIGH:
4807 case R_PPC64_REL16_HIGHA:
4808 case R_PPC64_REL16_HIGHER:
4809 case R_PPC64_REL16_HIGHERA:
4810 case R_PPC64_REL16_HIGHEST:
4811 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
4812 case R_PPC64_REL16_HIGHER34:
4813 case R_PPC64_REL16_HIGHERA34:
4814 case R_PPC64_REL16_HIGHEST34:
4815 case R_PPC64_REL16_HIGHESTA34:
a680de9a 4816 case R_PPC64_REL16DX_HA:
25f23106
AM
4817 break;
4818
45965137
AM
4819 /* Not supported as a dynamic relocation. */
4820 case R_PPC64_ADDR64_LOCAL:
0e1862bb 4821 if (bfd_link_pic (info))
45965137
AM
4822 {
4823 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4824 ppc_howto_init ();
695344c0 4825 /* xgettext:c-format */
174d0a74 4826 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 4827 "in shared libraries and PIEs\n"),
45965137
AM
4828 abfd, sec, rel->r_offset,
4829 ppc64_elf_howto_table[r_type]->name);
4830 bfd_set_error (bfd_error_bad_value);
4831 return FALSE;
4832 }
4833 break;
4834
ad8e1ba5 4835 case R_PPC64_TOC16:
33c0ec9d
AM
4836 case R_PPC64_TOC16_DS:
4837 htab->do_multi_toc = 1;
d77c8a4b 4838 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 4839 /* Fall through. */
ad8e1ba5
AM
4840 case R_PPC64_TOC16_LO:
4841 case R_PPC64_TOC16_HI:
4842 case R_PPC64_TOC16_HA:
ad8e1ba5 4843 case R_PPC64_TOC16_LO_DS:
4c52953f 4844 sec->has_toc_reloc = 1;
ad8e1ba5
AM
4845 break;
4846
006589cf
AM
4847 /* Marker reloc. */
4848 case R_PPC64_ENTRY:
4849 break;
4850
5bd4f169
AM
4851 /* This relocation describes the C++ object vtable hierarchy.
4852 Reconstruct it for later use during GC. */
4853 case R_PPC64_GNU_VTINHERIT:
c152c796 4854 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4855 return FALSE;
5bd4f169
AM
4856 break;
4857
4858 /* This relocation describes which C++ vtable entries are actually
4859 used. Record for later use during GC. */
4860 case R_PPC64_GNU_VTENTRY:
a0ea3a14 4861 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4862 return FALSE;
5bd4f169
AM
4863 break;
4864
721956f4
AM
4865 case R_PPC64_REL14:
4866 case R_PPC64_REL14_BRTAKEN:
4867 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4868 {
4869 asection *dest = NULL;
4870
4871 /* Heuristic: If jumping outside our section, chances are
4872 we are going to need a stub. */
4873 if (h != NULL)
4874 {
4875 /* If the sym is weak it may be overridden later, so
4876 don't assume we know where a weak sym lives. */
4877 if (h->root.type == bfd_link_hash_defined)
4878 dest = h->root.u.def.section;
4879 }
4880 else
87d72d41
AM
4881 {
4882 Elf_Internal_Sym *isym;
4883
4884 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4885 abfd, r_symndx);
4886 if (isym == NULL)
4887 return FALSE;
4888
4889 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4890 }
4891
220c76dd 4892 if (dest != sec)
7c8fe5c4 4893 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4894 }
3e04d765
AM
4895 goto rel24;
4896
4897 case R_PPC64_PLTCALL:
5663e321 4898 case R_PPC64_PLTCALL_NOTOC:
3e04d765 4899 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
4900 /* Fall through. */
4901
5d1634d7 4902 case R_PPC64_REL24:
05d0e962 4903 case R_PPC64_REL24_NOTOC:
3e04d765 4904 rel24:
cbf95972
AM
4905 plt_list = ifunc;
4906 if (h != NULL)
5d1634d7 4907 {
e054468f
AM
4908 h->needs_plt = 1;
4909 if (h->root.root.string[0] == '.'
4910 && h->root.root.string[1] != '\0')
4911 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 4912
3a71aa26 4913 if (h == tga || h == dottga)
cbf95972
AM
4914 {
4915 sec->has_tls_reloc = 1;
4916 if (rel != relocs
4917 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4918 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4919 /* We have a new-style __tls_get_addr call with
4920 a marker reloc. */
4921 ;
4922 else
4923 /* Mark this section as having an old-style call. */
4924 sec->has_tls_get_addr_call = 1;
4925 }
4926 plt_list = &h->plt.plist;
411e1bfb 4927 }
cbf95972
AM
4928
4929 /* We may need a .plt entry if the function this reloc
4930 refers to is in a shared lib. */
4931 if (plt_list
5663e321 4932 && !update_plt_info (abfd, plt_list, sym_addend))
cbf95972 4933 return FALSE;
411e1bfb
AM
4934 break;
4935
cbf95972
AM
4936 case R_PPC64_ADDR14:
4937 case R_PPC64_ADDR14_BRNTAKEN:
4938 case R_PPC64_ADDR14_BRTAKEN:
4939 case R_PPC64_ADDR24:
4940 goto dodyn;
4941
411e1bfb
AM
4942 case R_PPC64_TPREL64:
4943 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 4944 if (bfd_link_dll (info))
411e1bfb
AM
4945 info->flags |= DF_STATIC_TLS;
4946 goto dotlstoc;
4947
4948 case R_PPC64_DTPMOD64:
4949 if (rel + 1 < rel_end
4950 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4951 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 4952 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 4953 else
951fd09b 4954 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
4955 goto dotlstoc;
4956
4957 case R_PPC64_DTPREL64:
4958 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4959 if (rel != relocs
4960 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4961 && rel[-1].r_offset == rel->r_offset - 8)
4962 /* This is the second reloc of a dtpmod, dtprel pair.
4963 Don't mark with TLS_DTPREL. */
4964 goto dodyn;
4965
4966 dotlstoc:
4967 sec->has_tls_reloc = 1;
4968 if (h != NULL)
4969 {
4970 struct ppc_link_hash_entry *eh;
4971 eh = (struct ppc_link_hash_entry *) h;
46e9995a 4972 eh->tls_mask |= tls_type & 0xff;
411e1bfb
AM
4973 }
4974 else
4975 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4976 sym_addend, tls_type))
411e1bfb
AM
4977 return FALSE;
4978
7c8fe5c4
AM
4979 ppc64_sec = ppc64_elf_section_data (sec);
4980 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 4981 {
3a71aa26
AM
4982 bfd_size_type amt;
4983
e7b938ca 4984 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
4985 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4986 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4987 if (ppc64_sec->u.toc.symndx == NULL)
4988 return FALSE;
4989 amt = sec->size * sizeof (bfd_vma) / 8;
4990 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4991 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 4992 return FALSE;
7c8fe5c4
AM
4993 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4994 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
4995 }
4996 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26 4997 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5663e321 4998 ppc64_sec->u.toc.add[rel->r_offset / 8] = sym_addend;
951fd09b
AM
4999
5000 /* Mark the second slot of a GD or LD entry.
5001 -1 to indicate GD and -2 to indicate LD. */
5002 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5003 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5004 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5005 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5006 goto dodyn;
5007
5008 case R_PPC64_TPREL16:
5009 case R_PPC64_TPREL16_LO:
5010 case R_PPC64_TPREL16_HI:
5011 case R_PPC64_TPREL16_HA:
5012 case R_PPC64_TPREL16_DS:
5013 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5014 case R_PPC64_TPREL16_HIGH:
5015 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5016 case R_PPC64_TPREL16_HIGHER:
5017 case R_PPC64_TPREL16_HIGHERA:
5018 case R_PPC64_TPREL16_HIGHEST:
5019 case R_PPC64_TPREL16_HIGHESTA:
c213164a 5020 case R_PPC64_TPREL34:
7c8bbca5
AM
5021 if (bfd_link_dll (info))
5022 info->flags |= DF_STATIC_TLS;
5023 goto dodyn;
5d1634d7 5024
e86ce104 5025 case R_PPC64_ADDR64:
b9399fcf 5026 if (is_opd
1e2f5b6e 5027 && rel + 1 < rel_end
4ce794b7 5028 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5029 {
8387904d 5030 if (h != NULL)
8c5b4e52 5031 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 5032 }
e86ce104
AM
5033 /* Fall through. */
5034
65f38f15
AM
5035 case R_PPC64_ADDR16:
5036 case R_PPC64_ADDR16_DS:
5037 case R_PPC64_ADDR16_HA:
5038 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5039 case R_PPC64_ADDR16_HIGH:
5040 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5041 case R_PPC64_ADDR16_HIGHER:
5042 case R_PPC64_ADDR16_HIGHERA:
5043 case R_PPC64_ADDR16_HIGHEST:
5044 case R_PPC64_ADDR16_HIGHESTA:
5045 case R_PPC64_ADDR16_LO:
5046 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
5047 case R_PPC64_D34:
5048 case R_PPC64_D34_LO:
5049 case R_PPC64_D34_HI30:
5050 case R_PPC64_D34_HA30:
5051 case R_PPC64_ADDR16_HIGHER34:
5052 case R_PPC64_ADDR16_HIGHERA34:
5053 case R_PPC64_ADDR16_HIGHEST34:
5054 case R_PPC64_ADDR16_HIGHESTA34:
5055 case R_PPC64_D28:
0e1862bb 5056 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5057 && rel->r_addend == 0)
5058 {
5059 /* We may need a .plt entry if this reloc refers to a
5060 function in a shared lib. */
5663e321 5061 if (!update_plt_info (abfd, &h->plt.plist, 0))
a345bc8d
AM
5062 return FALSE;
5063 h->pointer_equality_needed = 1;
5064 }
5065 /* Fall through. */
5066
5067 case R_PPC64_REL30:
5068 case R_PPC64_REL32:
5069 case R_PPC64_REL64:
65f38f15 5070 case R_PPC64_ADDR32:
65f38f15
AM
5071 case R_PPC64_UADDR16:
5072 case R_PPC64_UADDR32:
5073 case R_PPC64_UADDR64:
5bd4f169 5074 case R_PPC64_TOC:
0e1862bb 5075 if (h != NULL && !bfd_link_pic (info))
81848ca0 5076 /* We may need a copy reloc. */
f5385ebf 5077 h->non_got_ref = 1;
81848ca0 5078
41bd81ab 5079 /* Don't propagate .opd relocs. */
b9399fcf 5080 if (NO_OPD_RELOCS && is_opd)
e86ce104 5081 break;
e86ce104 5082
65f38f15
AM
5083 /* If we are creating a shared library, and this is a reloc
5084 against a global symbol, or a non PC relative reloc
5085 against a local symbol, then we need to copy the reloc
5086 into the shared library. However, if we are linking with
5087 -Bsymbolic, we do not need to copy a reloc against a
5088 global symbol which is defined in an object we are
5089 including in the link (i.e., DEF_REGULAR is set). At
5090 this point we have not seen all the input files, so it is
5091 possible that DEF_REGULAR is not set now but will be set
5092 later (it is never cleared). In case of a weak definition,
5093 DEF_REGULAR may be cleared later by a strong definition in
5094 a shared library. We account for that possibility below by
f4656909 5095 storing information in the dyn_relocs field of the hash
65f38f15
AM
5096 table entry. A similar situation occurs when creating
5097 shared libraries and symbol visibility changes render the
5098 symbol local.
5099
5100 If on the other hand, we are creating an executable, we
5101 may need to keep relocations for symbols satisfied by a
5102 dynamic library if we manage to avoid copy relocs for the
5103 symbol. */
411e1bfb 5104 dodyn:
0e1862bb 5105 if ((bfd_link_pic (info)
1d483afe 5106 && (must_be_dyn_reloc (info, r_type)
65f38f15 5107 || (h != NULL
198f1157 5108 && (!SYMBOLIC_BIND (info, h)
65f38f15 5109 || h->root.type == bfd_link_hash_defweak
f5385ebf 5110 || !h->def_regular))))
f4656909 5111 || (ELIMINATE_COPY_RELOCS
0e1862bb 5112 && !bfd_link_pic (info)
65f38f15
AM
5113 && h != NULL
5114 && (h->root.type == bfd_link_hash_defweak
25f23106 5115 || !h->def_regular))
0e1862bb 5116 || (!bfd_link_pic (info)
25f23106 5117 && ifunc != NULL))
5bd4f169 5118 {
65f38f15
AM
5119 /* We must copy these reloc types into the output file.
5120 Create a reloc section in dynobj and make room for
5121 this reloc. */
5bd4f169
AM
5122 if (sreloc == NULL)
5123 {
83bac4b0
NC
5124 sreloc = _bfd_elf_make_dynamic_reloc_section
5125 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5126
5bd4f169 5127 if (sreloc == NULL)
83bac4b0 5128 return FALSE;
5bd4f169
AM
5129 }
5130
65f38f15
AM
5131 /* If this is a global symbol, we count the number of
5132 relocations we need for this symbol. */
5133 if (h != NULL)
5134 {
19e08130
AM
5135 struct elf_dyn_relocs *p;
5136 struct elf_dyn_relocs **head;
5137
ec338859 5138 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5139 p = *head;
5140 if (p == NULL || p->sec != sec)
5141 {
5142 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5143 if (p == NULL)
5144 return FALSE;
5145 p->next = *head;
5146 *head = p;
5147 p->sec = sec;
5148 p->count = 0;
5149 p->pc_count = 0;
5150 }
5151 p->count += 1;
5152 if (!must_be_dyn_reloc (info, r_type))
5153 p->pc_count += 1;
65f38f15
AM
5154 }
5155 else
5156 {
ec338859
AM
5157 /* Track dynamic relocs needed for local syms too.
5158 We really need local syms available to do this
5159 easily. Oh well. */
19e08130
AM
5160 struct ppc_dyn_relocs *p;
5161 struct ppc_dyn_relocs **head;
5162 bfd_boolean is_ifunc;
ec338859 5163 asection *s;
6edfbbad 5164 void *vpp;
87d72d41 5165 Elf_Internal_Sym *isym;
6edfbbad 5166
87d72d41
AM
5167 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5168 abfd, r_symndx);
5169 if (isym == NULL)
b34976b6 5170 return FALSE;
ec338859 5171
87d72d41
AM
5172 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5173 if (s == NULL)
5174 s = sec;
5175
6edfbbad 5176 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5177 head = (struct ppc_dyn_relocs **) vpp;
5178 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5179 p = *head;
5180 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5181 p = p->next;
5182 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5183 {
5184 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5185 if (p == NULL)
5186 return FALSE;
5187 p->next = *head;
5188 *head = p;
5189 p->sec = sec;
5190 p->ifunc = is_ifunc;
5191 p->count = 0;
5192 }
5193 p->count += 1;
ec338859 5194 }
65f38f15 5195 }
5bd4f169 5196 break;
65f38f15
AM
5197
5198 default:
96e0dda4 5199 break;
5bd4f169
AM
5200 }
5201 }
5202
b34976b6 5203 return TRUE;
5bd4f169
AM
5204}
5205
ee67d69a
AM
5206/* Merge backend specific data from an object file to the output
5207 object file when linking. */
5208
5209static bfd_boolean
50e03d47 5210ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5211{
50e03d47 5212 bfd *obfd = info->output_bfd;
ee67d69a
AM
5213 unsigned long iflags, oflags;
5214
5215 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5216 return TRUE;
5217
5218 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5219 return TRUE;
5220
50e03d47 5221 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
5222 return FALSE;
5223
5224 iflags = elf_elfheader (ibfd)->e_flags;
5225 oflags = elf_elfheader (obfd)->e_flags;
5226
f6c7c3e8 5227 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5228 {
4eca0228 5229 _bfd_error_handler
695344c0 5230 /* xgettext:c-format */
871b3ab2 5231 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
5232 bfd_set_error (bfd_error_bad_value);
5233 return FALSE;
5234 }
f6c7c3e8 5235 else if (iflags != oflags && iflags != 0)
ee67d69a 5236 {
4eca0228 5237 _bfd_error_handler
695344c0 5238 /* xgettext:c-format */
871b3ab2 5239 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5240 ibfd, iflags, oflags);
5241 bfd_set_error (bfd_error_bad_value);
5242 return FALSE;
5243 }
5244
4a91d0ba
AM
5245 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5246 return FALSE;
005d79fd 5247
ee67d69a 5248 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5249 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5250}
5251
5252static bfd_boolean
5253ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5254{
5255 /* Print normal ELF private data. */
5256 _bfd_elf_print_private_bfd_data (abfd, ptr);
5257
5258 if (elf_elfheader (abfd)->e_flags != 0)
5259 {
5260 FILE *file = ptr;
5261
ee67d69a
AM
5262 fprintf (file, _("private flags = 0x%lx:"),
5263 elf_elfheader (abfd)->e_flags);
5264
5265 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5266 fprintf (file, _(" [abiv%ld]"),
5267 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5268 fputc ('\n', file);
5269 }
5270
5271 return TRUE;
5272}
5273
8387904d 5274/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5275 of the code entry point, and its section, which must be in the same
5276 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5277
5278static bfd_vma
5279opd_entry_value (asection *opd_sec,
5280 bfd_vma offset,
5281 asection **code_sec,
aef36ac1
AM
5282 bfd_vma *code_off,
5283 bfd_boolean in_code_sec)
8387904d
AM
5284{
5285 bfd *opd_bfd = opd_sec->owner;
8860955f 5286 Elf_Internal_Rela *relocs;
8387904d 5287 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5288 bfd_vma val;
8387904d 5289
9f296da3
AM
5290 /* No relocs implies we are linking a --just-symbols object, or looking
5291 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5292 if (opd_sec->reloc_count == 0)
5293 {
729eabd5 5294 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5295
729eabd5
AM
5296 if (contents == NULL)
5297 {
5298 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5299 return (bfd_vma) -1;
5300 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5301 }
ee1e4ede 5302
dbb3fbbb 5303 /* PR 17512: file: 64b9dfbb. */
451dfd38 5304 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5305 return (bfd_vma) -1;
5306
729eabd5 5307 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5308 if (code_sec != NULL)
5309 {
5310 asection *sec, *likely = NULL;
ee1e4ede 5311
aef36ac1 5312 if (in_code_sec)
4b85d634 5313 {
aef36ac1
AM
5314 sec = *code_sec;
5315 if (sec->vma <= val
5316 && val < sec->vma + sec->size)
5317 likely = sec;
5318 else
5319 val = -1;
5320 }
5321 else
5322 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5323 if (sec->vma <= val
5324 && (sec->flags & SEC_LOAD) != 0
5325 && (sec->flags & SEC_ALLOC) != 0)
5326 likely = sec;
5327 if (likely != NULL)
5328 {
5329 *code_sec = likely;
5330 if (code_off != NULL)
5331 *code_off = val - likely->vma;
4b85d634
AM
5332 }
5333 }
aef36ac1 5334 return val;
4b85d634
AM
5335 }
5336
0c8d6e5c 5337 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5338
729eabd5 5339 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5340 if (relocs == NULL)
5341 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
5342 /* PR 17512: file: df8e1fd6. */
5343 if (relocs == NULL)
5344 return (bfd_vma) -1;
645ea6a9 5345
8387904d 5346 /* Go find the opd reloc at the sym address. */
8860955f 5347 lo = relocs;
8387904d 5348 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5349 val = (bfd_vma) -1;
8387904d
AM
5350 while (lo < hi)
5351 {
5352 look = lo + (hi - lo) / 2;
5353 if (look->r_offset < offset)
5354 lo = look + 1;
5355 else if (look->r_offset > offset)
5356 hi = look;
5357 else
5358 {
0ffa91dd
NC
5359 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5360
8387904d
AM
5361 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5362 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5363 {
5364 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5365 asection *sec = NULL;
8387904d 5366
b53dfeb2
AM
5367 if (symndx >= symtab_hdr->sh_info
5368 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5369 {
5370 struct elf_link_hash_entry **sym_hashes;
5371 struct elf_link_hash_entry *rh;
5372
5373 sym_hashes = elf_sym_hashes (opd_bfd);
5374 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5375 if (rh != NULL)
5376 {
5377 rh = elf_follow_link (rh);
bb854a36
AM
5378 if (rh->root.type != bfd_link_hash_defined
5379 && rh->root.type != bfd_link_hash_defweak)
5380 break;
5381 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5382 {
bb854a36
AM
5383 val = rh->root.u.def.value;
5384 sec = rh->root.u.def.section;
b53dfeb2
AM
5385 }
5386 }
5387 }
5388
5389 if (sec == NULL)
5390 {
5391 Elf_Internal_Sym *sym;
5392
5393 if (symndx < symtab_hdr->sh_info)
5394 {
5395 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5396 if (sym == NULL)
5397 {
5398 size_t symcnt = symtab_hdr->sh_info;
5399 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5400 symcnt, 0,
5401 NULL, NULL, NULL);
5402 if (sym == NULL)
5403 break;
5404 symtab_hdr->contents = (bfd_byte *) sym;
5405 }
5406 sym += symndx;
128205bb
AM
5407 }
5408 else
5409 {
b53dfeb2
AM
5410 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5411 1, symndx,
5412 NULL, NULL, NULL);
128205bb
AM
5413 if (sym == NULL)
5414 break;
128205bb 5415 }
b53dfeb2
AM
5416 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5417 if (sec == NULL)
5418 break;
5419 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5420 val = sym->st_value;
8387904d 5421 }
b53dfeb2 5422
8387904d
AM
5423 val += look->r_addend;
5424 if (code_off != NULL)
5425 *code_off = val;
5426 if (code_sec != NULL)
aef36ac1
AM
5427 {
5428 if (in_code_sec && *code_sec != sec)
5429 return -1;
5430 else
5431 *code_sec = sec;
5432 }
b53dfeb2 5433 if (sec->output_section != NULL)
8387904d 5434 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5435 }
5436 break;
5437 }
5438 }
645ea6a9 5439
645ea6a9 5440 return val;
8387904d
AM
5441}
5442
aef36ac1
AM
5443/* If the ELF symbol SYM might be a function in SEC, return the
5444 function size and set *CODE_OFF to the function's entry point,
5445 otherwise return zero. */
9f296da3 5446
aef36ac1
AM
5447static bfd_size_type
5448ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5449 bfd_vma *code_off)
9f296da3 5450{
aef36ac1
AM
5451 bfd_size_type size;
5452
5453 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5454 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5455 return 0;
5456
5457 size = 0;
5458 if (!(sym->flags & BSF_SYNTHETIC))
5459 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5460
5461 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5462 {
b07bca4e
AM
5463 struct _opd_sec_data *opd = get_opd_info (sym->section);
5464 bfd_vma symval = sym->value;
5465
5466 if (opd != NULL
5467 && opd->adjust != NULL
5468 && elf_section_data (sym->section)->relocs != NULL)
5469 {
5470 /* opd_entry_value will use cached relocs that have been
5471 adjusted, but with raw symbols. That means both local
5472 and global symbols need adjusting. */
5473 long adjust = opd->adjust[OPD_NDX (symval)];
5474 if (adjust == -1)
5475 return 0;
5476 symval += adjust;
5477 }
5478
5479 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
5480 &sec, code_off, TRUE) == (bfd_vma) -1)
5481 return 0;
5482 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5483 symbol. This size has nothing to do with the code size of the
5484 function, which is what we're supposed to return, but the
5485 code size isn't available without looking up the dot-sym.
5486 However, doing that would be a waste of time particularly
5487 since elf_find_function will look at the dot-sym anyway.
5488 Now, elf_find_function will keep the largest size of any
5489 function sym found at the code address of interest, so return
5490 1 here to avoid it incorrectly caching a larger function size
5491 for a small function. This does mean we return the wrong
5492 size for a new-ABI function of size 24, but all that does is
5493 disable caching for such functions. */
5494 if (size == 24)
5495 size = 1;
9f296da3 5496 }
aef36ac1
AM
5497 else
5498 {
5499 if (sym->section != sec)
5500 return 0;
5501 *code_off = sym->value;
5502 }
5503 if (size == 0)
5504 size = 1;
5505 return size;
9f296da3
AM
5506}
5507
f378ab09
AM
5508/* Return true if symbol is a strong function defined in an ELFv2
5509 object with st_other localentry bits of zero, ie. its local entry
5510 point coincides with its global entry point. */
5511
5512static bfd_boolean
5513is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5514{
5515 return (h != NULL
5516 && h->type == STT_FUNC
5517 && h->root.type == bfd_link_hash_defined
5518 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 5519 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
5520 && is_ppc64_elf (h->root.u.def.section->owner)
5521 && abiversion (h->root.u.def.section->owner) >= 2);
5522}
5523
854b41e7
AM
5524/* Return true if symbol is defined in a regular object file. */
5525
5526static bfd_boolean
5527is_static_defined (struct elf_link_hash_entry *h)
5528{
5529 return ((h->root.type == bfd_link_hash_defined
5530 || h->root.type == bfd_link_hash_defweak)
5531 && h->root.u.def.section != NULL
5532 && h->root.u.def.section->output_section != NULL);
5533}
5534
b31867b6
AM
5535/* If FDH is a function descriptor symbol, return the associated code
5536 entry symbol if it is defined. Return NULL otherwise. */
5537
5538static struct ppc_link_hash_entry *
5539defined_code_entry (struct ppc_link_hash_entry *fdh)
5540{
5541 if (fdh->is_func_descriptor)
5542 {
5543 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5544 if (fh->elf.root.type == bfd_link_hash_defined
5545 || fh->elf.root.type == bfd_link_hash_defweak)
5546 return fh;
5547 }
5548 return NULL;
5549}
5550
5551/* If FH is a function code entry symbol, return the associated
5552 function descriptor symbol if it is defined. Return NULL otherwise. */
5553
5554static struct ppc_link_hash_entry *
5555defined_func_desc (struct ppc_link_hash_entry *fh)
5556{
5557 if (fh->oh != NULL
5558 && fh->oh->is_func_descriptor)
5559 {
5560 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5561 if (fdh->elf.root.type == bfd_link_hash_defined
5562 || fdh->elf.root.type == bfd_link_hash_defweak)
5563 return fdh;
5564 }
5565 return NULL;
5566}
5567
8c5b4e52
AM
5568static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5569
5570/* Garbage collect sections, after first dealing with dot-symbols. */
5571
5572static bfd_boolean
5573ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5574{
5575 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5576
5577 if (htab != NULL && htab->need_func_desc_adj)
5578 {
5579 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5580 htab->need_func_desc_adj = 0;
5581 }
5582 return bfd_elf_gc_sections (abfd, info);
5583}
5584
74f0fb50
AM
5585/* Mark all our entry sym sections, both opd and code section. */
5586
5587static void
5588ppc64_elf_gc_keep (struct bfd_link_info *info)
5589{
5590 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5591 struct bfd_sym_chain *sym;
5592
4dfe6ac6
NC
5593 if (htab == NULL)
5594 return;
5595
74f0fb50
AM
5596 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5597 {
b31867b6 5598 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5599 asection *sec;
5600
5601 eh = (struct ppc_link_hash_entry *)
b31867b6 5602 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5603 if (eh == NULL)
5604 continue;
5605 if (eh->elf.root.type != bfd_link_hash_defined
5606 && eh->elf.root.type != bfd_link_hash_defweak)
5607 continue;
5608
b31867b6
AM
5609 fh = defined_code_entry (eh);
5610 if (fh != NULL)
74f0fb50 5611 {
b31867b6 5612 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5613 sec->flags |= SEC_KEEP;
5614 }
5615 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5616 && opd_entry_value (eh->elf.root.u.def.section,
5617 eh->elf.root.u.def.value,
aef36ac1 5618 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
5619 sec->flags |= SEC_KEEP;
5620
5621 sec = eh->elf.root.u.def.section;
5622 sec->flags |= SEC_KEEP;
5623 }
5624}
5625
64d03ab5
AM
5626/* Mark sections containing dynamically referenced symbols. When
5627 building shared libraries, we must assume that any visible symbol is
5628 referenced. */
5629
5630static bfd_boolean
5631ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5632{
5633 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5634 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5635 struct ppc_link_hash_entry *fdh;
b407645f 5636 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5637
64d03ab5 5638 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5639 fdh = defined_func_desc (eh);
5640 if (fdh != NULL)
5641 eh = fdh;
64d03ab5
AM
5642
5643 if ((eh->elf.root.type == bfd_link_hash_defined
5644 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 5645 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5646 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5647 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5648 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5649 && (!bfd_link_executable (info)
e278ae05 5650 || info->gc_keep_exported
b407645f
AM
5651 || info->export_dynamic
5652 || (eh->elf.dynamic
5653 && d != NULL
2cdcc330
AM
5654 && (*d->match) (&d->head, NULL,
5655 eh->elf.root.root.string)))
e278ae05 5656 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5657 || !bfd_hide_sym_by_version (info->version_info,
5658 eh->elf.root.root.string)))))
64d03ab5
AM
5659 {
5660 asection *code_sec;
b31867b6 5661 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5662
5663 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5664
5665 /* Function descriptor syms cause the associated
5666 function code sym section to be marked. */
b31867b6
AM
5667 fh = defined_code_entry (eh);
5668 if (fh != NULL)
5669 {
5670 code_sec = fh->elf.root.u.def.section;
5671 code_sec->flags |= SEC_KEEP;
5672 }
64d03ab5
AM
5673 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5674 && opd_entry_value (eh->elf.root.u.def.section,
5675 eh->elf.root.u.def.value,
aef36ac1 5676 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
5677 code_sec->flags |= SEC_KEEP;
5678 }
5679
5680 return TRUE;
5681}
5682
5bd4f169
AM
5683/* Return the section that should be marked against GC for a given
5684 relocation. */
5685
5686static asection *
4ce794b7 5687ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5688 struct bfd_link_info *info,
4ce794b7
AM
5689 Elf_Internal_Rela *rel,
5690 struct elf_link_hash_entry *h,
5691 Elf_Internal_Sym *sym)
5bd4f169 5692{
ccfa59ea
AM
5693 asection *rsec;
5694
ccfa59ea
AM
5695 /* Syms return NULL if we're marking .opd, so we avoid marking all
5696 function sections, as all functions are referenced in .opd. */
5697 rsec = NULL;
5698 if (get_opd_info (sec) != NULL)
5699 return rsec;
1e2f5b6e 5700
5bd4f169
AM
5701 if (h != NULL)
5702 {
04c9666a 5703 enum elf_ppc64_reloc_type r_type;
b31867b6 5704 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5705
4ce794b7 5706 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5707 switch (r_type)
5bd4f169
AM
5708 {
5709 case R_PPC64_GNU_VTINHERIT:
5710 case R_PPC64_GNU_VTENTRY:
5711 break;
5712
5713 default:
5714 switch (h->root.type)
5715 {
5716 case bfd_link_hash_defined:
5717 case bfd_link_hash_defweak:
ccfa59ea 5718 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5719 fdh = defined_func_desc (eh);
5720 if (fdh != NULL)
8c5b4e52
AM
5721 {
5722 /* -mcall-aixdesc code references the dot-symbol on
5723 a call reloc. Mark the function descriptor too
5724 against garbage collection. */
5725 fdh->elf.mark = 1;
60d67dc8
AM
5726 if (fdh->elf.is_weakalias)
5727 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
5728 eh = fdh;
5729 }
1e2f5b6e
AM
5730
5731 /* Function descriptor syms cause the associated
5732 function code sym section to be marked. */
b31867b6
AM
5733 fh = defined_code_entry (eh);
5734 if (fh != NULL)
ccfa59ea
AM
5735 {
5736 /* They also mark their opd section. */
74f0fb50 5737 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5738
b31867b6 5739 rsec = fh->elf.root.u.def.section;
ccfa59ea 5740 }
8387904d
AM
5741 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5742 && opd_entry_value (eh->elf.root.u.def.section,
5743 eh->elf.root.u.def.value,
aef36ac1 5744 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 5745 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5746 else
1e2f5b6e
AM
5747 rsec = h->root.u.def.section;
5748 break;
5bd4f169
AM
5749
5750 case bfd_link_hash_common:
1e2f5b6e
AM
5751 rsec = h->root.u.c.p->section;
5752 break;
5bd4f169
AM
5753
5754 default:
fb34365b 5755 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5756 }
5757 }
5758 }
5759 else
5760 {
74f0fb50 5761 struct _opd_sec_data *opd;
1e2f5b6e
AM
5762
5763 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5764 opd = get_opd_info (rsec);
5765 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5766 {
74f0fb50 5767 rsec->gc_mark = 1;
ccfa59ea 5768
51aecdc5 5769 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 5770 }
5bd4f169
AM
5771 }
5772
1e2f5b6e 5773 return rsec;
5bd4f169
AM
5774}
5775
deb0e272
AM
5776/* The maximum size of .sfpr. */
5777#define SFPR_MAX (218*4)
5778
5779struct sfpr_def_parms
5780{
699733f6
AM
5781 const char name[12];
5782 unsigned char lo, hi;
2cdcc330
AM
5783 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5784 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
5785};
5786
a4b6fadd
AM
5787/* Auto-generate _save*, _rest* functions in .sfpr.
5788 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5789 instead. */
deb0e272 5790
4dfe6ac6 5791static bfd_boolean
a4b6fadd
AM
5792sfpr_define (struct bfd_link_info *info,
5793 const struct sfpr_def_parms *parm,
5794 asection *stub_sec)
deb0e272
AM
5795{
5796 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5797 unsigned int i;
5798 size_t len = strlen (parm->name);
5799 bfd_boolean writing = FALSE;
699733f6 5800 char sym[16];
deb0e272 5801
4dfe6ac6
NC
5802 if (htab == NULL)
5803 return FALSE;
5804
deb0e272
AM
5805 memcpy (sym, parm->name, len);
5806 sym[len + 2] = 0;
5807
5808 for (i = parm->lo; i <= parm->hi; i++)
5809 {
a4b6fadd 5810 struct ppc_link_hash_entry *h;
deb0e272
AM
5811
5812 sym[len + 0] = i / 10 + '0';
5813 sym[len + 1] = i % 10 + '0';
a4b6fadd 5814 h = (struct ppc_link_hash_entry *)
b32547cd 5815 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 5816 if (stub_sec != NULL)
deb0e272 5817 {
a4b6fadd
AM
5818 if (h != NULL
5819 && h->elf.root.type == bfd_link_hash_defined
5820 && h->elf.root.u.def.section == htab->sfpr)
5821 {
5822 struct elf_link_hash_entry *s;
5823 char buf[32];
5824 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5825 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5826 if (s == NULL)
5827 return FALSE;
5828 if (s->root.type == bfd_link_hash_new
5829 || (s->root.type = bfd_link_hash_defined
5830 && s->root.u.def.section == stub_sec))
5831 {
5832 s->root.type = bfd_link_hash_defined;
5833 s->root.u.def.section = stub_sec;
7dda8d3c 5834 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
5835 + h->elf.root.u.def.value);
5836 s->ref_regular = 1;
5837 s->def_regular = 1;
5838 s->ref_regular_nonweak = 1;
5839 s->forced_local = 1;
5840 s->non_elf = 0;
5841 s->root.linker_def = 1;
5842 }
5843 }
5844 continue;
5845 }
5846 if (h != NULL)
5847 {
5848 h->save_res = 1;
5849 if (!h->elf.def_regular)
deb0e272 5850 {
a4b6fadd
AM
5851 h->elf.root.type = bfd_link_hash_defined;
5852 h->elf.root.u.def.section = htab->sfpr;
5853 h->elf.root.u.def.value = htab->sfpr->size;
5854 h->elf.type = STT_FUNC;
5855 h->elf.def_regular = 1;
b32547cd 5856 h->elf.non_elf = 0;
a4b6fadd
AM
5857 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5858 writing = TRUE;
deb0e272 5859 if (htab->sfpr->contents == NULL)
a4b6fadd 5860 {
2cdcc330
AM
5861 htab->sfpr->contents
5862 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd
AM
5863 if (htab->sfpr->contents == NULL)
5864 return FALSE;
5865 }
deb0e272
AM
5866 }
5867 }
5868 if (writing)
5869 {
5870 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5871 if (i != parm->hi)
5872 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5873 else
5874 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5875 htab->sfpr->size = p - htab->sfpr->contents;
5876 }
5877 }
5878
5879 return TRUE;
5880}
5881
5882static bfd_byte *
5883savegpr0 (bfd *abfd, bfd_byte *p, int r)
5884{
5885 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5886 return p + 4;
5887}
5888
5889static bfd_byte *
5890savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5891{
5892 p = savegpr0 (abfd, p, r);
a078d95a 5893 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5894 p = p + 4;
5895 bfd_put_32 (abfd, BLR, p);
5896 return p + 4;
5897}
5898
5899static bfd_byte *
5900restgpr0 (bfd *abfd, bfd_byte *p, int r)
5901{
5902 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5903 return p + 4;
5904}
5905
5906static bfd_byte *
5907restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5908{
a078d95a 5909 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5910 p = p + 4;
5911 p = restgpr0 (abfd, p, r);
5912 bfd_put_32 (abfd, MTLR_R0, p);
5913 p = p + 4;
5914 if (r == 29)
5915 {
5916 p = restgpr0 (abfd, p, 30);
5917 p = restgpr0 (abfd, p, 31);
5918 }
5919 bfd_put_32 (abfd, BLR, p);
5920 return p + 4;
5921}
5922
5923static bfd_byte *
5924savegpr1 (bfd *abfd, bfd_byte *p, int r)
5925{
5926 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5927 return p + 4;
5928}
5929
5930static bfd_byte *
5931savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5932{
5933 p = savegpr1 (abfd, p, r);
5934 bfd_put_32 (abfd, BLR, p);
5935 return p + 4;
5936}
5937
5938static bfd_byte *
5939restgpr1 (bfd *abfd, bfd_byte *p, int r)
5940{
5941 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5942 return p + 4;
5943}
5944
5945static bfd_byte *
5946restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5947{
5948 p = restgpr1 (abfd, p, r);
5949 bfd_put_32 (abfd, BLR, p);
5950 return p + 4;
5951}
5952
5953static bfd_byte *
5954savefpr (bfd *abfd, bfd_byte *p, int r)
5955{
5956 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5957 return p + 4;
5958}
5959
5960static bfd_byte *
5961savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5962{
5963 p = savefpr (abfd, p, r);
a078d95a 5964 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5965 p = p + 4;
5966 bfd_put_32 (abfd, BLR, p);
5967 return p + 4;
5968}
5969
5970static bfd_byte *
5971restfpr (bfd *abfd, bfd_byte *p, int r)
5972{
5973 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5974 return p + 4;
5975}
5976
5977static bfd_byte *
5978restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5979{
a078d95a 5980 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5981 p = p + 4;
5982 p = restfpr (abfd, p, r);
5983 bfd_put_32 (abfd, MTLR_R0, p);
5984 p = p + 4;
5985 if (r == 29)
5986 {
5987 p = restfpr (abfd, p, 30);
5988 p = restfpr (abfd, p, 31);
5989 }
5990 bfd_put_32 (abfd, BLR, p);
5991 return p + 4;
5992}
5993
5994static bfd_byte *
5995savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5996{
5997 p = savefpr (abfd, p, r);
5998 bfd_put_32 (abfd, BLR, p);
5999 return p + 4;
6000}
6001
6002static bfd_byte *
6003restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6004{
6005 p = restfpr (abfd, p, r);
6006 bfd_put_32 (abfd, BLR, p);
6007 return p + 4;
6008}
6009
6010static bfd_byte *
6011savevr (bfd *abfd, bfd_byte *p, int r)
6012{
6013 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6014 p = p + 4;
6015 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6016 return p + 4;
6017}
6018
6019static bfd_byte *
6020savevr_tail (bfd *abfd, bfd_byte *p, int r)
6021{
6022 p = savevr (abfd, p, r);
6023 bfd_put_32 (abfd, BLR, p);
6024 return p + 4;
6025}
6026
6027static bfd_byte *
6028restvr (bfd *abfd, bfd_byte *p, int r)
6029{
6030 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6031 p = p + 4;
6032 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6033 return p + 4;
6034}
6035
6036static bfd_byte *
6037restvr_tail (bfd *abfd, bfd_byte *p, int r)
6038{
6039 p = restvr (abfd, p, r);
6040 bfd_put_32 (abfd, BLR, p);
6041 return p + 4;
6042}
6043
e86ce104
AM
6044/* Called via elf_link_hash_traverse to transfer dynamic linking
6045 information on function code symbol entries to their corresponding
6046 function descriptor symbol entries. */
deb0e272 6047
b34976b6 6048static bfd_boolean
4ce794b7 6049func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6050{
e86ce104 6051 struct bfd_link_info *info;
65f38f15 6052 struct ppc_link_hash_table *htab;
50bc7936
AM
6053 struct ppc_link_hash_entry *fh;
6054 struct ppc_link_hash_entry *fdh;
6055 bfd_boolean force_local;
5bd4f169 6056
50bc7936
AM
6057 fh = (struct ppc_link_hash_entry *) h;
6058 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6059 return TRUE;
e86ce104 6060
8c5b4e52
AM
6061 if (!fh->is_func)
6062 return TRUE;
6063
6064 if (fh->elf.root.root.string[0] != '.'
6065 || fh->elf.root.root.string[1] == '\0')
6066 return TRUE;
6067
4ce794b7 6068 info = inf;
65f38f15 6069 htab = ppc_hash_table (info);
4dfe6ac6
NC
6070 if (htab == NULL)
6071 return FALSE;
5bd4f169 6072
8c5b4e52
AM
6073 /* Find the corresponding function descriptor symbol. */
6074 fdh = lookup_fdh (fh, htab);
6075
c09bdfe5
AM
6076 /* Resolve undefined references to dot-symbols as the value
6077 in the function descriptor, if we have one in a regular object.
6078 This is to satisfy cases like ".quad .foo". Calls to functions
6079 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6080 if ((fh->elf.root.type == bfd_link_hash_undefined
6081 || fh->elf.root.type == bfd_link_hash_undefweak)
6082 && (fdh->elf.root.type == bfd_link_hash_defined
6083 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6084 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6085 && opd_entry_value (fdh->elf.root.u.def.section,
6086 fdh->elf.root.u.def.value,
c09bdfe5 6087 &fh->elf.root.u.def.section,
aef36ac1 6088 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6089 {
b31867b6 6090 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6091 fh->elf.forced_local = 1;
b31867b6
AM
6092 fh->elf.def_regular = fdh->elf.def_regular;
6093 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6094 }
6095
8c5b4e52
AM
6096 if (!fh->elf.dynamic)
6097 {
6098 struct plt_entry *ent;
5bd4f169 6099
8c5b4e52
AM
6100 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6101 if (ent->plt.refcount > 0)
6102 break;
6103 if (ent == NULL)
6104 return TRUE;
6105 }
5bd4f169 6106
8c5b4e52 6107 /* Create a descriptor as undefined if necessary. */
50bc7936 6108 if (fdh == NULL
0e1862bb 6109 && !bfd_link_executable (info)
50bc7936
AM
6110 && (fh->elf.root.type == bfd_link_hash_undefined
6111 || fh->elf.root.type == bfd_link_hash_undefweak))
6112 {
908b32fc 6113 fdh = make_fdh (info, fh);
bb700d78
AM
6114 if (fdh == NULL)
6115 return FALSE;
50bc7936 6116 }
648cca2c 6117
8c5b4e52 6118 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6119 if (fdh != NULL
6120 && fdh->fake
8c5b4e52
AM
6121 && (fh->elf.root.type == bfd_link_hash_defined
6122 || fh->elf.root.type == bfd_link_hash_defweak))
6123 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 6124
8c5b4e52
AM
6125 /* Transfer dynamic linking information to the function descriptor. */
6126 if (fdh != NULL)
6127 {
f5385ebf
AM
6128 fdh->elf.ref_regular |= fh->elf.ref_regular;
6129 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6130 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6131 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6132 fdh->elf.dynamic |= fh->elf.dynamic;
6133 fdh->elf.needs_plt |= (fh->elf.needs_plt
6134 || fh->elf.type == STT_FUNC
6135 || fh->elf.type == STT_GNU_IFUNC);
6136 move_plt_plist (fh, fdh);
6137
6138 if (!fdh->elf.forced_local
6139 && fh->elf.dynindx != -1)
6140 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6141 return FALSE;
e86ce104
AM
6142 }
6143
50bc7936
AM
6144 /* Now that the info is on the function descriptor, clear the
6145 function code sym info. Any function code syms for which we
6146 don't have a definition in a regular file, we force local.
6147 This prevents a shared library from exporting syms that have
6148 been imported from another library. Function code syms that
6149 are really in the library we must leave global to prevent the
6150 linker dragging in a definition from a static library. */
93f3fa99
AM
6151 force_local = (!fh->elf.def_regular
6152 || fdh == NULL
6153 || !fdh->elf.def_regular
6154 || fdh->elf.forced_local);
50bc7936
AM
6155 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6156
b34976b6 6157 return TRUE;
e86ce104 6158}
40b8271b 6159
a4b6fadd
AM
6160static const struct sfpr_def_parms save_res_funcs[] =
6161 {
6162 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6163 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6164 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6165 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6166 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6167 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6168 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6169 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6170 { "._savef", 14, 31, savefpr, savefpr1_tail },
6171 { "._restf", 14, 31, restfpr, restfpr1_tail },
6172 { "_savevr_", 20, 31, savevr, savevr_tail },
6173 { "_restvr_", 20, 31, restvr, restvr_tail }
6174 };
6175
e86ce104 6176/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6177 this hook to a) provide some gcc support functions, and b) transfer
6178 dynamic linking information gathered so far on function code symbol
6179 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6180
b34976b6 6181static bfd_boolean
4ce794b7
AM
6182ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6183 struct bfd_link_info *info)
e86ce104
AM
6184{
6185 struct ppc_link_hash_table *htab;
6186
6187 htab = ppc_hash_table (info);
4dfe6ac6
NC
6188 if (htab == NULL)
6189 return FALSE;
6190
b32547cd
AM
6191 /* Provide any missing _save* and _rest* functions. */
6192 if (htab->sfpr != NULL)
6193 {
6194 unsigned int i;
6195
6196 htab->sfpr->size = 0;
6197 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6198 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6199 return FALSE;
6200 if (htab->sfpr->size == 0)
6201 htab->sfpr->flags |= SEC_EXCLUDE;
6202 }
6203
6204 if (bfd_link_relocatable (info))
6205 return TRUE;
6206
6207 if (htab->elf.hgot != NULL)
dba6fa9b
AM
6208 {
6209 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6210 /* Make .TOC. defined so as to prevent it being made dynamic.
6211 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6212 if (!htab->elf.hgot->def_regular
6213 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6214 {
6215 htab->elf.hgot->root.type = bfd_link_hash_defined;
6216 htab->elf.hgot->root.u.def.value = 0;
6217 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6218 htab->elf.hgot->def_regular = 1;
6219 htab->elf.hgot->root.linker_def = 1;
6220 }
dba6fa9b 6221 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6222 htab->elf.hgot->other
6223 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6224 }
c66bb0ee 6225
8c5b4e52
AM
6226 if (htab->need_func_desc_adj)
6227 {
6228 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6229 htab->need_func_desc_adj = 0;
6230 }
805fc799 6231
b34976b6 6232 return TRUE;
e86ce104
AM
6233}
6234
98bbb1b8 6235/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 6236
98bbb1b8 6237static asection *
a345bc8d
AM
6238readonly_dynrelocs (struct elf_link_hash_entry *h)
6239{
6240 struct ppc_link_hash_entry *eh;
6241 struct elf_dyn_relocs *p;
6242
6243 eh = (struct ppc_link_hash_entry *) h;
6244 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6245 {
6246 asection *s = p->sec->output_section;
6247
6248 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 6249 return p->sec;
a345bc8d 6250 }
98bbb1b8 6251 return NULL;
a345bc8d
AM
6252}
6253
d311bc8b 6254/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6255 aliases, that apply to read-only sections. Cannot be used after
6256 size_dynamic_sections. */
d311bc8b
AM
6257
6258static bfd_boolean
6259alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6260{
6261 struct ppc_link_hash_entry *eh;
6262
6263 eh = (struct ppc_link_hash_entry *) h;
6264 do
6265 {
6266 if (readonly_dynrelocs (&eh->elf))
6267 return TRUE;
ab2477e1 6268 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
2cdcc330
AM
6269 }
6270 while (eh != NULL && &eh->elf != h);
d311bc8b
AM
6271
6272 return FALSE;
6273}
8a2058b5 6274
8a9e8e72
AM
6275/* Return whether EH has pc-relative dynamic relocs. */
6276
6277static bfd_boolean
6278pc_dynrelocs (struct ppc_link_hash_entry *eh)
6279{
6280 struct elf_dyn_relocs *p;
6281
6282 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6283 if (p->pc_count != 0)
6284 return TRUE;
6285 return FALSE;
6286}
6287
8a2058b5
AM
6288/* Return true if a global entry stub will be created for H. Valid
6289 for ELFv2 before plt entries have been allocated. */
6290
6291static bfd_boolean
6292global_entry_stub (struct elf_link_hash_entry *h)
6293{
6294 struct plt_entry *pent;
6295
6296 if (!h->pointer_equality_needed
6297 || h->def_regular)
6298 return FALSE;
6299
6300 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6301 if (pent->plt.refcount > 0
6302 && pent->addend == 0)
6303 return TRUE;
6304
6305 return FALSE;
6306}
6307
e86ce104
AM
6308/* Adjust a symbol defined by a dynamic object and referenced by a
6309 regular object. The current definition is in some section of the
6310 dynamic object, but we're not including those sections. We have to
6311 change the definition to something the rest of the link can
6312 understand. */
6313
b34976b6 6314static bfd_boolean
4ce794b7
AM
6315ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6316 struct elf_link_hash_entry *h)
e86ce104
AM
6317{
6318 struct ppc_link_hash_table *htab;
5474d94f 6319 asection *s, *srel;
e86ce104
AM
6320
6321 htab = ppc_hash_table (info);
4dfe6ac6
NC
6322 if (htab == NULL)
6323 return FALSE;
e86ce104
AM
6324
6325 /* Deal with function syms. */
6326 if (h->type == STT_FUNC
e054468f 6327 || h->type == STT_GNU_IFUNC
f5385ebf 6328 || h->needs_plt)
e86ce104 6329 {
529fe20e
AM
6330 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6331 || SYMBOL_CALLS_LOCAL (info, h)
6332 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6333 /* Discard dyn_relocs when non-pic if we've decided that a
6334 function symbol is local and not an ifunc. We keep dynamic
6335 relocs for ifuncs when local rather than always emitting a
6336 plt call stub for them and defining the symbol on the call
6337 stub. We can't do that for ELFv1 anyway (a function symbol
6338 is defined on a descriptor, not code) and it can be faster at
6339 run-time due to not needing to bounce through a stub. The
6340 dyn_relocs for ifuncs will be applied even in a static
6341 executable. */
6342 if (!bfd_link_pic (info)
6343 && h->type != STT_GNU_IFUNC
6344 && local)
6345 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6346
e86ce104
AM
6347 /* Clear procedure linkage table information for any symbol that
6348 won't need a .plt entry. */
411e1bfb
AM
6349 struct plt_entry *ent;
6350 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6351 if (ent->plt.refcount > 0)
6352 break;
8387904d 6353 if (ent == NULL
2d7ad24e
AM
6354 || (h->type != STT_GNU_IFUNC
6355 && local
3e04d765
AM
6356 && (htab->can_convert_all_inline_plt
6357 || (((struct ppc_link_hash_entry *) h)->tls_mask
6358 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6359 {
411e1bfb 6360 h->plt.plist = NULL;
f5385ebf 6361 h->needs_plt = 0;
d1eca1e4 6362 h->pointer_equality_needed = 0;
40b8271b 6363 }
8a2058b5 6364 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6365 {
d1eca1e4
AM
6366 /* Taking a function's address in a read/write section
6367 doesn't require us to define the function symbol in the
6368 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6369 be used instead. The reason we prefer a few more dynamic
6370 relocs is that calling via a global entry stub costs a
6371 few more instructions, and pointer_equality_needed causes
6372 extra work in ld.so when resolving these symbols. */
529fe20e 6373 if (global_entry_stub (h))
d1eca1e4 6374 {
ab2477e1 6375 if (!readonly_dynrelocs (h))
529fe20e
AM
6376 {
6377 h->pointer_equality_needed = 0;
04383fd1
AM
6378 /* If we haven't seen a branch reloc and the symbol
6379 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6380 if (!h->needs_plt)
6381 h->plt.plist = NULL;
6382 }
6383 else if (!bfd_link_pic (info))
6384 /* We are going to be defining the function symbol on the
6385 plt stub, so no dyn_relocs needed when non-pic. */
6386 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
6387 }
6388
3988aed5
AM
6389 /* ELFv2 function symbols can't have copy relocs. */
6390 return TRUE;
6391 }
6392 else if (!h->needs_plt
ab2477e1 6393 && !readonly_dynrelocs (h))
3988aed5 6394 {
04383fd1
AM
6395 /* If we haven't seen a branch reloc and the symbol isn't an
6396 ifunc then we don't need a plt entry. */
3988aed5
AM
6397 h->plt.plist = NULL;
6398 h->pointer_equality_needed = 0;
a345bc8d
AM
6399 return TRUE;
6400 }
5bd4f169 6401 }
bbd7ec4a 6402 else
411e1bfb 6403 h->plt.plist = NULL;
5bd4f169
AM
6404
6405 /* If this is a weak symbol, and there is a real definition, the
6406 processor independent code will have arranged for us to see the
6407 real definition first, and we can just use the same value. */
60d67dc8 6408 if (h->is_weakalias)
5bd4f169 6409 {
60d67dc8
AM
6410 struct elf_link_hash_entry *def = weakdef (h);
6411 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6412 h->root.u.def.section = def->root.u.def.section;
6413 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6414 if (def->root.u.def.section == htab->elf.sdynbss
6415 || def->root.u.def.section == htab->elf.sdynrelro)
6416 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 6417 return TRUE;
5bd4f169
AM
6418 }
6419
5bd4f169
AM
6420 /* If we are creating a shared library, we must presume that the
6421 only references to the symbol are via the global offset table.
6422 For such cases we need not do anything here; the relocations will
6423 be handled correctly by relocate_section. */
0e1862bb 6424 if (bfd_link_pic (info))
b34976b6 6425 return TRUE;
5bd4f169 6426
65f38f15
AM
6427 /* If there are no references to this symbol that do not use the
6428 GOT, we don't need to generate a copy reloc. */
f5385ebf 6429 if (!h->non_got_ref)
b34976b6 6430 return TRUE;
65f38f15 6431
b186458a 6432 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6433 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6434
d93d1c80
AM
6435 /* If -z nocopyreloc was given, don't generate them either. */
6436 || info->nocopyreloc
a127494f 6437
dce2246a 6438 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6439 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 6440 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 6441
d93d1c80
AM
6442 /* Protected variables do not work with .dynbss. The copy in
6443 .dynbss won't be used by the shared library with the protected
6444 definition for the variable. Text relocations are preferable
6445 to an incorrect program. */
6446 || h->protected_def)
529fe20e 6447 return TRUE;
a127494f 6448
5d35169e 6449 if (h->plt.plist != NULL)
97b639ba
AM
6450 {
6451 /* We should never get here, but unfortunately there are versions
6452 of gcc out there that improperly (for this ABI) put initialized
6453 function pointers, vtable refs and suchlike in read-only
6454 sections. Allow them to proceed, but warn that this might
6455 break at runtime. */
25f53a85 6456 info->callbacks->einfo
c1c8c1ef 6457 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6458 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6459 h->root.root.string);
6460 }
5d35169e
AM
6461
6462 /* This is a reference to a symbol defined by a dynamic object which
6463 is not a function. */
6464
5bd4f169
AM
6465 /* We must allocate the symbol in our .dynbss section, which will
6466 become part of the .bss section of the executable. There will be
6467 an entry for this symbol in the .dynsym section. The dynamic
6468 object will contain position independent code, so all references
6469 from the dynamic object to this symbol will go through the global
6470 offset table. The dynamic linker will use the .dynsym entry to
6471 determine the address it must put in the global offset table, so
6472 both the dynamic object and the regular object will refer to the
6473 same memory location for the variable. */
5474d94f
AM
6474 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6475 {
6476 s = htab->elf.sdynrelro;
6477 srel = htab->elf.sreldynrelro;
6478 }
6479 else
6480 {
6481 s = htab->elf.sdynbss;
6482 srel = htab->elf.srelbss;
6483 }
1d7e9d18 6484 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6485 {
4a7e5234
AM
6486 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6487 linker to copy the initial value out of the dynamic object
6488 and into the runtime process image. */
5474d94f 6489 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6490 h->needs_copy = 1;
5bd4f169
AM
6491 }
6492
529fe20e
AM
6493 /* We no longer want dyn_relocs. */
6494 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 6495 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6496}
6497
e86ce104
AM
6498/* If given a function descriptor symbol, hide both the function code
6499 sym and the descriptor. */
6500static void
4ce794b7
AM
6501ppc64_elf_hide_symbol (struct bfd_link_info *info,
6502 struct elf_link_hash_entry *h,
6503 bfd_boolean force_local)
e86ce104 6504{
34814b9f 6505 struct ppc_link_hash_entry *eh;
e86ce104
AM
6506 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6507
87469ba2
AM
6508 if (ppc_hash_table (info) == NULL)
6509 return;
6510
34814b9f
AM
6511 eh = (struct ppc_link_hash_entry *) h;
6512 if (eh->is_func_descriptor)
e86ce104 6513 {
34814b9f 6514 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6515
721956f4 6516 if (fh == NULL)
d1329ca3
AM
6517 {
6518 const char *p, *q;
b8ac2841 6519 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6520 char save;
6521
6522 /* We aren't supposed to use alloca in BFD because on
6523 systems which do not have alloca the version in libiberty
6524 calls xmalloc, which might cause the program to crash
6525 when it runs out of memory. This function doesn't have a
6526 return status, so there's no way to gracefully return an
6527 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6528 accessed; It's either a string in an ELF string table,
6529 or allocated in an objalloc structure. */
d1329ca3 6530
34814b9f 6531 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6532 save = *p;
6533 *(char *) p = '.';
34814b9f 6534 fh = (struct ppc_link_hash_entry *)
b8ac2841 6535 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6536 *(char *) p = save;
6537
6538 /* Unfortunately, if it so happens that the string we were
6539 looking for was allocated immediately before this string,
6540 then we overwrote the string terminator. That's the only
6541 reason the lookup should fail. */
6542 if (fh == NULL)
6543 {
34814b9f
AM
6544 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6545 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6546 --q, --p;
34814b9f
AM
6547 if (q < eh->elf.root.root.string && *p == '.')
6548 fh = (struct ppc_link_hash_entry *)
b8ac2841 6549 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6550 }
6551 if (fh != NULL)
6552 {
34814b9f
AM
6553 eh->oh = fh;
6554 fh->oh = eh;
d1329ca3
AM
6555 }
6556 }
e86ce104 6557 if (fh != NULL)
34814b9f 6558 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6559 }
6560}
6561
411e1bfb 6562static bfd_boolean
8843416a
AM
6563get_sym_h (struct elf_link_hash_entry **hp,
6564 Elf_Internal_Sym **symp,
6565 asection **symsecp,
f961d9dd 6566 unsigned char **tls_maskp,
8843416a
AM
6567 Elf_Internal_Sym **locsymsp,
6568 unsigned long r_symndx,
6569 bfd *ibfd)
411e1bfb 6570{
0ffa91dd 6571 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6572
6573 if (r_symndx >= symtab_hdr->sh_info)
6574 {
6575 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6576 struct elf_link_hash_entry *h;
6577
6578 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6579 h = elf_follow_link (h);
411e1bfb
AM
6580
6581 if (hp != NULL)
6582 *hp = h;
6583
6584 if (symp != NULL)
6585 *symp = NULL;
6586
6587 if (symsecp != NULL)
6588 {
6589 asection *symsec = NULL;
6590 if (h->root.type == bfd_link_hash_defined
6591 || h->root.type == bfd_link_hash_defweak)
6592 symsec = h->root.u.def.section;
6593 *symsecp = symsec;
6594 }
6595
e7b938ca 6596 if (tls_maskp != NULL)
411e1bfb
AM
6597 {
6598 struct ppc_link_hash_entry *eh;
6599
6600 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6601 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6602 }
6603 }
6604 else
6605 {
6606 Elf_Internal_Sym *sym;
6607 Elf_Internal_Sym *locsyms = *locsymsp;
6608
6609 if (locsyms == NULL)
6610 {
6611 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6612 if (locsyms == NULL)
6613 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6614 symtab_hdr->sh_info,
6615 0, NULL, NULL, NULL);
6616 if (locsyms == NULL)
6617 return FALSE;
6618 *locsymsp = locsyms;
6619 }
6620 sym = locsyms + r_symndx;
6621
6622 if (hp != NULL)
6623 *hp = NULL;
6624
6625 if (symp != NULL)
6626 *symp = sym;
6627
6628 if (symsecp != NULL)
cb33740c 6629 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6630
e7b938ca 6631 if (tls_maskp != NULL)
411e1bfb
AM
6632 {
6633 struct got_entry **lgot_ents;
f961d9dd 6634 unsigned char *tls_mask;
411e1bfb 6635
e7b938ca 6636 tls_mask = NULL;
411e1bfb
AM
6637 lgot_ents = elf_local_got_ents (ibfd);
6638 if (lgot_ents != NULL)
6639 {
e054468f
AM
6640 struct plt_entry **local_plt = (struct plt_entry **)
6641 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6642 unsigned char *lgot_masks = (unsigned char *)
e054468f 6643 (local_plt + symtab_hdr->sh_info);
e7b938ca 6644 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6645 }
e7b938ca 6646 *tls_maskp = tls_mask;
411e1bfb
AM
6647 }
6648 }
6649 return TRUE;
6650}
6651
e7b938ca 6652/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6653 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6654 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6655
6656static int
f961d9dd 6657get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6658 unsigned long *toc_symndx,
6659 bfd_vma *toc_addend,
0d4792f7 6660 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6661 const Elf_Internal_Rela *rel,
6662 bfd *ibfd)
411e1bfb
AM
6663{
6664 unsigned long r_symndx;
0d4792f7 6665 int next_r;
411e1bfb
AM
6666 struct elf_link_hash_entry *h;
6667 Elf_Internal_Sym *sym;
6668 asection *sec;
6669 bfd_vma off;
6670
6671 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6672 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6673 return 0;
411e1bfb 6674
37da22e5
AM
6675 if ((*tls_maskp != NULL
6676 && (**tls_maskp & TLS_TLS) != 0
6677 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 6678 || sec == NULL
6bee8834 6679 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6680 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6681 return 1;
411e1bfb
AM
6682
6683 /* Look inside a TOC section too. */
6684 if (h != NULL)
6685 {
6686 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6687 off = h->root.u.def.value;
6688 }
6689 else
6690 off = sym->st_value;
6691 off += rel->r_addend;
6692 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6693 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6694 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6695 if (toc_symndx != NULL)
6696 *toc_symndx = r_symndx;
3a71aa26
AM
6697 if (toc_addend != NULL)
6698 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6699 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6700 return 0;
854b41e7 6701 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6702 && (next_r == -1 || next_r == -2))
6703 return 1 - next_r;
951fd09b 6704 return 1;
411e1bfb
AM
6705}
6706
3b421ab3
AM
6707/* Find (or create) an entry in the tocsave hash table. */
6708
6709static struct tocsave_entry *
6710tocsave_find (struct ppc_link_hash_table *htab,
6711 enum insert_option insert,
6712 Elf_Internal_Sym **local_syms,
6713 const Elf_Internal_Rela *irela,
6714 bfd *ibfd)
6715{
6716 unsigned long r_indx;
6717 struct elf_link_hash_entry *h;
6718 Elf_Internal_Sym *sym;
6719 struct tocsave_entry ent, *p;
6720 hashval_t hash;
6721 struct tocsave_entry **slot;
6722
6723 r_indx = ELF64_R_SYM (irela->r_info);
6724 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6725 return NULL;
6726 if (ent.sec == NULL || ent.sec->output_section == NULL)
6727 {
4eca0228 6728 _bfd_error_handler
871b3ab2 6729 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
6730 return NULL;
6731 }
6732
6733 if (h != NULL)
6734 ent.offset = h->root.u.def.value;
6735 else
6736 ent.offset = sym->st_value;
6737 ent.offset += irela->r_addend;
6738
6739 hash = tocsave_htab_hash (&ent);
6740 slot = ((struct tocsave_entry **)
6741 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6742 if (slot == NULL)
6743 return NULL;
6744
6745 if (*slot == NULL)
6746 {
6747 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6748 if (p == NULL)
6749 return NULL;
6750 *p = ent;
6751 *slot = p;
6752 }
6753 return *slot;
6754}
6755
754021d0 6756/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6757 code for the old ABI, these will already have been done. */
754021d0
AM
6758
6759static bfd_boolean
6760adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6761{
6762 struct ppc_link_hash_entry *eh;
6763 asection *sym_sec;
74f0fb50 6764 struct _opd_sec_data *opd;
754021d0
AM
6765
6766 if (h->root.type == bfd_link_hash_indirect)
6767 return TRUE;
6768
754021d0
AM
6769 if (h->root.type != bfd_link_hash_defined
6770 && h->root.type != bfd_link_hash_defweak)
6771 return TRUE;
6772
6773 eh = (struct ppc_link_hash_entry *) h;
6774 if (eh->adjust_done)
6775 return TRUE;
6776
6777 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6778 opd = get_opd_info (sym_sec);
6779 if (opd != NULL && opd->adjust != NULL)
754021d0 6780 {
51aecdc5 6781 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
6782 if (adjust == -1)
6783 {
6784 /* This entry has been deleted. */
b3fac117 6785 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6786 if (dsec == NULL)
6787 {
6788 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 6789 if (discarded_section (dsec))
81688140 6790 {
b3fac117 6791 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6792 break;
6793 }
6794 }
4025353c 6795 eh->elf.root.u.def.value = 0;
81688140 6796 eh->elf.root.u.def.section = dsec;
4025353c
AM
6797 }
6798 else
6799 eh->elf.root.u.def.value += adjust;
754021d0
AM
6800 eh->adjust_done = 1;
6801 }
6802 return TRUE;
6803}
6804
8c1d1bb8 6805/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 6806 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
6807 have already been determined. */
6808
6809static bfd_boolean
6810dec_dynrel_count (bfd_vma r_info,
6811 asection *sec,
6812 struct bfd_link_info *info,
6813 Elf_Internal_Sym **local_syms,
6814 struct elf_link_hash_entry *h,
19e08130 6815 Elf_Internal_Sym *sym)
8c1d1bb8
AM
6816{
6817 enum elf_ppc64_reloc_type r_type;
19e08130 6818 asection *sym_sec = NULL;
8c1d1bb8
AM
6819
6820 /* Can this reloc be dynamic? This switch, and later tests here
6821 should be kept in sync with the code in check_relocs. */
6822 r_type = ELF64_R_TYPE (r_info);
6823 switch (r_type)
6824 {
6825 default:
6826 return TRUE;
6827
6828 case R_PPC64_TPREL16:
6829 case R_PPC64_TPREL16_LO:
6830 case R_PPC64_TPREL16_HI:
6831 case R_PPC64_TPREL16_HA:
6832 case R_PPC64_TPREL16_DS:
6833 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
6834 case R_PPC64_TPREL16_HIGH:
6835 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
6836 case R_PPC64_TPREL16_HIGHER:
6837 case R_PPC64_TPREL16_HIGHERA:
6838 case R_PPC64_TPREL16_HIGHEST:
6839 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8 6840 case R_PPC64_TPREL64:
c213164a 6841 case R_PPC64_TPREL34:
8c1d1bb8
AM
6842 case R_PPC64_DTPMOD64:
6843 case R_PPC64_DTPREL64:
6844 case R_PPC64_ADDR64:
6845 case R_PPC64_REL30:
6846 case R_PPC64_REL32:
6847 case R_PPC64_REL64:
6848 case R_PPC64_ADDR14:
6849 case R_PPC64_ADDR14_BRNTAKEN:
6850 case R_PPC64_ADDR14_BRTAKEN:
6851 case R_PPC64_ADDR16:
6852 case R_PPC64_ADDR16_DS:
6853 case R_PPC64_ADDR16_HA:
6854 case R_PPC64_ADDR16_HI:
f9c6b907
AM
6855 case R_PPC64_ADDR16_HIGH:
6856 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
6857 case R_PPC64_ADDR16_HIGHER:
6858 case R_PPC64_ADDR16_HIGHERA:
6859 case R_PPC64_ADDR16_HIGHEST:
6860 case R_PPC64_ADDR16_HIGHESTA:
6861 case R_PPC64_ADDR16_LO:
6862 case R_PPC64_ADDR16_LO_DS:
6863 case R_PPC64_ADDR24:
6864 case R_PPC64_ADDR32:
6865 case R_PPC64_UADDR16:
6866 case R_PPC64_UADDR32:
6867 case R_PPC64_UADDR64:
6868 case R_PPC64_TOC:
5663e321
AM
6869 case R_PPC64_D34:
6870 case R_PPC64_D34_LO:
6871 case R_PPC64_D34_HI30:
6872 case R_PPC64_D34_HA30:
6873 case R_PPC64_ADDR16_HIGHER34:
6874 case R_PPC64_ADDR16_HIGHERA34:
6875 case R_PPC64_ADDR16_HIGHEST34:
6876 case R_PPC64_ADDR16_HIGHESTA34:
6877 case R_PPC64_D28:
8c1d1bb8
AM
6878 break;
6879 }
6880
6881 if (local_syms != NULL)
6882 {
6883 unsigned long r_symndx;
8c1d1bb8
AM
6884 bfd *ibfd = sec->owner;
6885
6886 r_symndx = ELF64_R_SYM (r_info);
6887 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6888 return FALSE;
6889 }
6890
0e1862bb 6891 if ((bfd_link_pic (info)
1d483afe 6892 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 6893 || (h != NULL
198f1157 6894 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
6895 || h->root.type == bfd_link_hash_defweak
6896 || !h->def_regular))))
6897 || (ELIMINATE_COPY_RELOCS
0e1862bb 6898 && !bfd_link_pic (info)
8c1d1bb8
AM
6899 && h != NULL
6900 && (h->root.type == bfd_link_hash_defweak
6901 || !h->def_regular)))
6902 ;
6903 else
6904 return TRUE;
6905
6906 if (h != NULL)
6edfbbad 6907 {
19e08130
AM
6908 struct elf_dyn_relocs *p;
6909 struct elf_dyn_relocs **pp;
6910 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6911
6912 /* elf_gc_sweep may have already removed all dyn relocs associated
6913 with local syms for a given section. Also, symbol flags are
6914 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6915 report a dynreloc miscount. */
6916 if (*pp == NULL && info->gc_sections)
6917 return TRUE;
6918
6919 while ((p = *pp) != NULL)
60124e18 6920 {
19e08130
AM
6921 if (p->sec == sec)
6922 {
6923 if (!must_be_dyn_reloc (info, r_type))
6924 p->pc_count -= 1;
6925 p->count -= 1;
6926 if (p->count == 0)
6927 *pp = p->next;
6928 return TRUE;
6929 }
6930 pp = &p->next;
60124e18 6931 }
6edfbbad 6932 }
19e08130
AM
6933 else
6934 {
6935 struct ppc_dyn_relocs *p;
6936 struct ppc_dyn_relocs **pp;
6937 void *vpp;
6938 bfd_boolean is_ifunc;
8c1d1bb8 6939
19e08130
AM
6940 if (local_syms == NULL)
6941 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6942 if (sym_sec == NULL)
6943 sym_sec = sec;
c57da1a7 6944
19e08130
AM
6945 vpp = &elf_section_data (sym_sec)->local_dynrel;
6946 pp = (struct ppc_dyn_relocs **) vpp;
6947
6948 if (*pp == NULL && info->gc_sections)
6949 return TRUE;
6950
6951 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6952 while ((p = *pp) != NULL)
8c1d1bb8 6953 {
19e08130
AM
6954 if (p->sec == sec && p->ifunc == is_ifunc)
6955 {
6956 p->count -= 1;
6957 if (p->count == 0)
6958 *pp = p->next;
6959 return TRUE;
6960 }
6961 pp = &p->next;
8c1d1bb8 6962 }
8c1d1bb8
AM
6963 }
6964
695344c0 6965 /* xgettext:c-format */
cf97bcb0
AM
6966 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6967 sec->owner, sec);
8c1d1bb8
AM
6968 bfd_set_error (bfd_error_bad_value);
6969 return FALSE;
6970}
6971
754021d0
AM
6972/* Remove unused Official Procedure Descriptor entries. Currently we
6973 only remove those associated with functions in discarded link-once
6974 sections, or weakly defined functions that have been overridden. It
6975 would be possible to remove many more entries for statically linked
6976 applications. */
6977
b34976b6 6978bfd_boolean
e7d1c40c 6979ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
6980{
6981 bfd *ibfd;
754021d0 6982 bfd_boolean some_edited = FALSE;
3f764659 6983 asection *need_pad = NULL;
e7d1c40c
AM
6984 struct ppc_link_hash_table *htab;
6985
6986 htab = ppc_hash_table (info);
6987 if (htab == NULL)
6988 return FALSE;
1e2f5b6e 6989
c72f2fb2 6990 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
6991 {
6992 asection *sec;
6993 Elf_Internal_Rela *relstart, *rel, *relend;
6994 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 6995 Elf_Internal_Sym *local_syms;
74f0fb50 6996 struct _opd_sec_data *opd;
51aecdc5 6997 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 6998 bfd_size_type cnt_16b = 0;
1e2f5b6e 6999
854b41e7
AM
7000 if (!is_ppc64_elf (ibfd))
7001 continue;
7002
1e2f5b6e 7003 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7004 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7005 continue;
7006
dbaa2011 7007 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7008 continue;
7009
1e2f5b6e
AM
7010 if (sec->output_section == bfd_abs_section_ptr)
7011 continue;
7012
7013 /* Look through the section relocs. */
7014 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7015 continue;
7016
6cdc0ccc 7017 local_syms = NULL;
0ffa91dd 7018 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7019
7020 /* Read the relocations. */
4ce794b7 7021 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7022 info->keep_memory);
1e2f5b6e 7023 if (relstart == NULL)
b34976b6 7024 return FALSE;
1e2f5b6e
AM
7025
7026 /* First run through the relocs to check they are sane, and to
7027 determine whether we need to edit this opd section. */
b34976b6 7028 need_edit = FALSE;
51aecdc5 7029 broken = FALSE;
3f764659 7030 need_pad = sec;
1e2f5b6e 7031 relend = relstart + sec->reloc_count;
50bc7936 7032 for (rel = relstart; rel < relend; )
1e2f5b6e 7033 {
04c9666a 7034 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7035 unsigned long r_symndx;
7036 asection *sym_sec;
7037 struct elf_link_hash_entry *h;
7038 Elf_Internal_Sym *sym;
51aecdc5 7039 bfd_vma offset;
1e2f5b6e 7040
51aecdc5 7041 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7042 only interested in the reloc pointing to a function entry
7043 point. */
51aecdc5
AM
7044 offset = rel->r_offset;
7045 if (rel + 1 == relend
7046 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7047 {
7048 /* If someone messes with .opd alignment then after a
7049 "ld -r" we might have padding in the middle of .opd.
7050 Also, there's nothing to prevent someone putting
7051 something silly in .opd with the assembler. No .opd
b34976b6 7052 optimization for them! */
3f764659 7053 broken_opd:
4eca0228 7054 _bfd_error_handler
871b3ab2 7055 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7056 broken = TRUE;
1e2f5b6e
AM
7057 break;
7058 }
7059
50bc7936
AM
7060 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7061 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7062 {
4eca0228 7063 _bfd_error_handler
695344c0 7064 /* xgettext:c-format */
871b3ab2 7065 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7066 ibfd, r_type);
51aecdc5 7067 broken = TRUE;
50bc7936
AM
7068 break;
7069 }
7070
1e2f5b6e 7071 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7072 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7073 r_symndx, ibfd))
50bc7936 7074 goto error_ret;
1e2f5b6e
AM
7075
7076 if (sym_sec == NULL || sym_sec->owner == NULL)
7077 {
411e1bfb
AM
7078 const char *sym_name;
7079 if (h != NULL)
7080 sym_name = h->root.root.string;
7081 else
26c61ae5
L
7082 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7083 sym_sec);
411e1bfb 7084
4eca0228 7085 _bfd_error_handler
695344c0 7086 /* xgettext:c-format */
871b3ab2 7087 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7088 ibfd, sym_name);
51aecdc5 7089 broken = TRUE;
1e2f5b6e
AM
7090 break;
7091 }
7092
51020317
AM
7093 /* opd entries are always for functions defined in the
7094 current input bfd. If the symbol isn't defined in the
7095 input bfd, then we won't be using the function in this
7096 bfd; It must be defined in a linkonce section in another
7097 bfd, or is weak. It's also possible that we are
7098 discarding the function due to a linker script /DISCARD/,
7099 which we test for via the output_section. */
7100 if (sym_sec->owner != ibfd
7101 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7102 need_edit = TRUE;
1e2f5b6e 7103
50bc7936 7104 rel += 2;
51aecdc5
AM
7105 if (rel + 1 == relend
7106 || (rel + 2 < relend
7107 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7108 ++rel;
7109
7110 if (rel == relend)
3f764659
JJ
7111 {
7112 if (sec->size == offset + 24)
7113 {
7114 need_pad = NULL;
7115 break;
7116 }
51aecdc5 7117 if (sec->size == offset + 16)
3f764659
JJ
7118 {
7119 cnt_16b++;
7120 break;
7121 }
7122 goto broken_opd;
7123 }
3f764659
JJ
7124 else if (rel + 1 < relend
7125 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7126 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7127 {
51aecdc5
AM
7128 if (rel[0].r_offset == offset + 16)
7129 cnt_16b++;
7130 else if (rel[0].r_offset != offset + 24)
7131 goto broken_opd;
3f764659
JJ
7132 }
7133 else
7134 goto broken_opd;
1e2f5b6e
AM
7135 }
7136
e7d1c40c 7137 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7138
51aecdc5 7139 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7140 {
7141 Elf_Internal_Rela *write_rel;
d4730f92 7142 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7143 bfd_byte *rptr, *wptr;
983bddc8 7144 bfd_byte *new_contents;
74f0fb50
AM
7145 bfd_size_type amt;
7146
983bddc8 7147 new_contents = NULL;
51aecdc5 7148 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7149 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7150 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7151 if (opd->adjust == NULL)
7152 return FALSE;
1e2f5b6e
AM
7153
7154 /* This seems a waste of time as input .opd sections are all
7155 zeros as generated by gcc, but I suppose there's no reason
7156 this will always be so. We might start putting something in
7157 the third word of .opd entries. */
7158 if ((sec->flags & SEC_IN_MEMORY) == 0)
7159 {
eea6121a
AM
7160 bfd_byte *loc;
7161 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7162 {
eea6121a
AM
7163 if (loc != NULL)
7164 free (loc);
50bc7936 7165 error_ret:
6cdc0ccc
AM
7166 if (local_syms != NULL
7167 && symtab_hdr->contents != (unsigned char *) local_syms)
7168 free (local_syms);
6cdc0ccc
AM
7169 if (elf_section_data (sec)->relocs != relstart)
7170 free (relstart);
b34976b6 7171 return FALSE;
6cdc0ccc 7172 }
1e2f5b6e
AM
7173 sec->contents = loc;
7174 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7175 }
7176
7177 elf_section_data (sec)->relocs = relstart;
7178
3f764659 7179 new_contents = sec->contents;
3f764659
JJ
7180 if (add_aux_fields)
7181 {
7182 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7183 if (new_contents == NULL)
7184 return FALSE;
51aecdc5 7185 need_pad = NULL;
3f764659 7186 }
b4f4e59f
AM
7187 wptr = new_contents;
7188 rptr = sec->contents;
1e2f5b6e 7189 write_rel = relstart;
51aecdc5 7190 for (rel = relstart; rel < relend; )
1e2f5b6e 7191 {
50bc7936
AM
7192 unsigned long r_symndx;
7193 asection *sym_sec;
7194 struct elf_link_hash_entry *h;
51aecdc5 7195 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7196 Elf_Internal_Sym *sym;
51aecdc5
AM
7197 long opd_ent_size;
7198 Elf_Internal_Rela *next_rel;
7199 bfd_boolean skip;
50bc7936
AM
7200
7201 r_symndx = ELF64_R_SYM (rel->r_info);
7202 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7203 r_symndx, ibfd))
50bc7936
AM
7204 goto error_ret;
7205
51aecdc5
AM
7206 next_rel = rel + 2;
7207 if (next_rel + 1 == relend
7208 || (next_rel + 2 < relend
7209 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7210 ++next_rel;
7211
7212 /* See if the .opd entry is full 24 byte or
7213 16 byte (with fd_aux entry overlapped with next
7214 fd_func). */
7215 opd_ent_size = 24;
7216 if (next_rel == relend)
1e2f5b6e 7217 {
51aecdc5 7218 if (sec->size == rel->r_offset + 16)
3f764659 7219 opd_ent_size = 16;
51aecdc5
AM
7220 }
7221 else if (next_rel->r_offset == rel->r_offset + 16)
7222 opd_ent_size = 16;
3f764659 7223
51aecdc5
AM
7224 if (h != NULL
7225 && h->root.root.string[0] == '.')
7226 {
8c5b4e52
AM
7227 fdh = ((struct ppc_link_hash_entry *) h)->oh;
7228 if (fdh != NULL)
7229 {
7230 fdh = ppc_follow_link (fdh);
7231 if (fdh->elf.root.type != bfd_link_hash_defined
7232 && fdh->elf.root.type != bfd_link_hash_defweak)
7233 fdh = NULL;
7234 }
51aecdc5 7235 }
1e2f5b6e 7236
51aecdc5
AM
7237 skip = (sym_sec->owner != ibfd
7238 || sym_sec->output_section == bfd_abs_section_ptr);
7239 if (skip)
7240 {
7241 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7242 {
51aecdc5
AM
7243 /* Arrange for the function descriptor sym
7244 to be dropped. */
7245 fdh->elf.root.u.def.value = 0;
7246 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7247 }
51aecdc5 7248 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7249
0e1862bb 7250 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7251 rel = next_rel;
7252 else
7253 while (1)
7254 {
7255 if (!dec_dynrel_count (rel->r_info, sec, info,
7256 NULL, h, sym))
7257 goto error_ret;
754021d0 7258
51aecdc5
AM
7259 if (++rel == next_rel)
7260 break;
1e2f5b6e 7261
51aecdc5
AM
7262 r_symndx = ELF64_R_SYM (rel->r_info);
7263 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7264 r_symndx, ibfd))
7265 goto error_ret;
7266 }
50bc7936
AM
7267 }
7268 else
1e2f5b6e 7269 {
51aecdc5
AM
7270 /* We'll be keeping this opd entry. */
7271 long adjust;
7272
7273 if (fdh != NULL)
7274 {
7275 /* Redefine the function descriptor symbol to
7276 this location in the opd section. It is
7277 necessary to update the value here rather
7278 than using an array of adjustments as we do
7279 for local symbols, because various places
7280 in the generic ELF code use the value
7281 stored in u.def.value. */
7282 fdh->elf.root.u.def.value = wptr - new_contents;
7283 fdh->adjust_done = 1;
7284 }
7285
7286 /* Local syms are a bit tricky. We could
7287 tweak them as they can be cached, but
7288 we'd need to look through the local syms
7289 for the function descriptor sym which we
7290 don't have at the moment. So keep an
7291 array of adjustments. */
7292 adjust = (wptr - new_contents) - (rptr - sec->contents);
7293 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7294
7295 if (wptr != rptr)
7296 memcpy (wptr, rptr, opd_ent_size);
7297 wptr += opd_ent_size;
7298 if (add_aux_fields && opd_ent_size == 16)
7299 {
7300 memset (wptr, '\0', 8);
7301 wptr += 8;
7302 }
7303
50bc7936 7304 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7305 new opd entries. */
7306 for ( ; rel != next_rel; ++rel)
7307 {
7308 rel->r_offset += adjust;
7309 if (write_rel != rel)
7310 memcpy (write_rel, rel, sizeof (*rel));
7311 ++write_rel;
7312 }
1e2f5b6e 7313 }
51aecdc5
AM
7314
7315 rptr += opd_ent_size;
1e2f5b6e
AM
7316 }
7317
3f764659 7318 sec->size = wptr - new_contents;
1e2f5b6e 7319 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7320 if (add_aux_fields)
7321 {
7322 free (sec->contents);
7323 sec->contents = new_contents;
7324 }
7325
05bf9422 7326 /* Fudge the header size too, as this is used later in
cdcf6e38 7327 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7328 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7329 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7330 some_edited = TRUE;
1e2f5b6e 7331 }
6cdc0ccc 7332 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7333 free (relstart);
6cdc0ccc 7334
411e1bfb
AM
7335 if (local_syms != NULL
7336 && symtab_hdr->contents != (unsigned char *) local_syms)
7337 {
7338 if (!info->keep_memory)
7339 free (local_syms);
7340 else
7341 symtab_hdr->contents = (unsigned char *) local_syms;
7342 }
7343 }
7344
754021d0
AM
7345 if (some_edited)
7346 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7347
3f764659
JJ
7348 /* If we are doing a final link and the last .opd entry is just 16 byte
7349 long, add a 8 byte padding after it. */
0e1862bb 7350 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7351 {
7352 bfd_byte *p;
7353
7354 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7355 {
7356 BFD_ASSERT (need_pad->size > 0);
7357
7358 p = bfd_malloc (need_pad->size + 8);
7359 if (p == NULL)
7360 return FALSE;
699733f6 7361
2cdcc330
AM
7362 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7363 p, 0, need_pad->size))
3f764659
JJ
7364 return FALSE;
7365
7366 need_pad->contents = p;
7367 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7368 }
7369 else
7370 {
7371 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7372 if (p == NULL)
7373 return FALSE;
7374
7375 need_pad->contents = p;
7376 }
7377
7378 memset (need_pad->contents + need_pad->size, 0, 8);
7379 need_pad->size += 8;
7380 }
7381
411e1bfb
AM
7382 return TRUE;
7383}
7384
3e04d765
AM
7385/* Analyze inline PLT call relocations to see whether calls to locally
7386 defined functions can be converted to direct calls. */
7387
7388bfd_boolean
7389ppc64_elf_inline_plt (struct bfd_link_info *info)
7390{
7391 struct ppc_link_hash_table *htab;
7392 bfd *ibfd;
7393 asection *sec;
7394 bfd_vma low_vma, high_vma, limit;
7395
7396 htab = ppc_hash_table (info);
7397 if (htab == NULL)
7398 return FALSE;
7399
7400 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7401 reduced somewhat to cater for possible stubs that might be added
7402 between the call and its destination. */
7403 if (htab->params->group_size < 0)
7404 {
7405 limit = -htab->params->group_size;
7406 if (limit == 1)
7407 limit = 0x1e00000;
7408 }
7409 else
7410 {
7411 limit = htab->params->group_size;
7412 if (limit == 1)
7413 limit = 0x1c00000;
7414 }
7415
7416 low_vma = -1;
7417 high_vma = 0;
7418 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7419 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7420 {
7421 if (low_vma > sec->vma)
7422 low_vma = sec->vma;
7423 if (high_vma < sec->vma + sec->size)
7424 high_vma = sec->vma + sec->size;
7425 }
7426
7427 /* If a "bl" can reach anywhere in local code sections, then we can
7428 convert all inline PLT sequences to direct calls when the symbol
7429 is local. */
7430 if (high_vma - low_vma < limit)
7431 {
7432 htab->can_convert_all_inline_plt = 1;
7433 return TRUE;
7434 }
7435
7436 /* Otherwise, go looking through relocs for cases where a direct
7437 call won't reach. Mark the symbol on any such reloc to disable
7438 the optimization and keep the PLT entry as it seems likely that
7439 this will be better than creating trampolines. Note that this
7440 will disable the optimization for all inline PLT calls to a
7441 particular symbol, not just those that won't reach. The
7442 difficulty in doing a more precise optimization is that the
7443 linker needs to make a decision depending on whether a
7444 particular R_PPC64_PLTCALL insn can be turned into a direct
7445 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7446 the sequence, and there is nothing that ties those relocs
7447 together except their symbol. */
7448
7449 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7450 {
7451 Elf_Internal_Shdr *symtab_hdr;
7452 Elf_Internal_Sym *local_syms;
7453
7454 if (!is_ppc64_elf (ibfd))
7455 continue;
7456
7457 local_syms = NULL;
7458 symtab_hdr = &elf_symtab_hdr (ibfd);
7459
7460 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7461 if (ppc64_elf_section_data (sec)->has_pltcall
7462 && !bfd_is_abs_section (sec->output_section))
7463 {
7464 Elf_Internal_Rela *relstart, *rel, *relend;
7465
7466 /* Read the relocations. */
7467 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7468 info->keep_memory);
7469 if (relstart == NULL)
7470 return FALSE;
7471
7472 relend = relstart + sec->reloc_count;
7473 for (rel = relstart; rel < relend; )
7474 {
7475 enum elf_ppc64_reloc_type r_type;
7476 unsigned long r_symndx;
7477 asection *sym_sec;
7478 struct elf_link_hash_entry *h;
7479 Elf_Internal_Sym *sym;
7480 unsigned char *tls_maskp;
7481
7482 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7483 if (r_type != R_PPC64_PLTCALL
7484 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7485 continue;
7486
7487 r_symndx = ELF64_R_SYM (rel->r_info);
7488 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7489 r_symndx, ibfd))
7490 {
7491 if (elf_section_data (sec)->relocs != relstart)
7492 free (relstart);
7493 if (local_syms != NULL
2cdcc330 7494 && symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765
AM
7495 free (local_syms);
7496 return FALSE;
7497 }
7498
7499 if (sym_sec != NULL && sym_sec->output_section != NULL)
7500 {
7501 bfd_vma from, to;
7502 if (h != NULL)
7503 to = h->root.u.def.value;
7504 else
7505 to = sym->st_value;
7506 to += (rel->r_addend
7507 + sym_sec->output_offset
7508 + sym_sec->output_section->vma);
7509 from = (rel->r_offset
7510 + sec->output_offset
7511 + sec->output_section->vma);
5663e321
AM
7512 if (to - from + limit < 2 * limit
7513 && !(r_type == R_PPC64_PLTCALL_NOTOC
7514 && (((h ? h->other : sym->st_other)
7515 & STO_PPC64_LOCAL_MASK)
4a4e7361 7516 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7517 *tls_maskp &= ~PLT_KEEP;
7518 }
7519 }
7520 if (elf_section_data (sec)->relocs != relstart)
7521 free (relstart);
7522 }
7523
7524 if (local_syms != NULL
7525 && symtab_hdr->contents != (unsigned char *) local_syms)
7526 {
7527 if (!info->keep_memory)
7528 free (local_syms);
7529 else
7530 symtab_hdr->contents = (unsigned char *) local_syms;
7531 }
7532 }
7533
7534 return TRUE;
7535}
7536
e1918d23 7537/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7538
e1918d23 7539asection *
e7d1c40c 7540ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7541{
411e1bfb
AM
7542 struct ppc_link_hash_table *htab;
7543
411e1bfb 7544 htab = ppc_hash_table (info);
4dfe6ac6
NC
7545 if (htab == NULL)
7546 return NULL;
7547
ee67d69a
AM
7548 if (abiversion (info->output_bfd) == 1)
7549 htab->opd_abi = 1;
7550
e7d1c40c 7551 if (htab->params->no_multi_toc)
33c0ec9d
AM
7552 htab->do_multi_toc = 0;
7553 else if (!htab->do_multi_toc)
e7d1c40c 7554 htab->params->no_multi_toc = 1;
33c0ec9d 7555
8b5f1ed8
AM
7556 /* Default to --no-plt-localentry, as this option can cause problems
7557 with symbol interposition. For example, glibc libpthread.so and
7558 libc.so duplicate many pthread symbols, with a fallback
7559 implementation in libc.so. In some cases the fallback does more
7560 work than the pthread implementation. __pthread_condattr_destroy
7561 is one such symbol: the libpthread.so implementation is
7562 localentry:0 while the libc.so implementation is localentry:8.
7563 An app that "cleverly" uses dlopen to only load necessary
7564 libraries at runtime may omit loading libpthread.so when not
7565 running multi-threaded, which then results in the libc.so
7566 fallback symbols being used and ld.so complaining. Now there
7567 are workarounds in ld (see non_zero_localentry) to detect the
7568 pthread situation, but that may not be the only case where
7569 --plt-localentry can cause trouble. */
f378ab09 7570 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7571 htab->params->plt_localentry0 = 0;
d44c746a
AM
7572 if (htab->params->plt_localentry0
7573 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7574 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
7575 _bfd_error_handler
7576 (_("warning: --plt-localentry is especially dangerous without "
7577 "ld.so support to detect ABI violations"));
f378ab09 7578
3a71aa26
AM
7579 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7580 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7581 FALSE, FALSE, TRUE));
a7f2871e
AM
7582 /* Move dynamic linking info to the function descriptor sym. */
7583 if (htab->tls_get_addr != NULL)
7584 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7585 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7586 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7587 FALSE, FALSE, TRUE));
7c9cf415 7588 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
7589 {
7590 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7591
7592 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7593 FALSE, FALSE, TRUE);
7594 if (opt != NULL)
7595 func_desc_adjust (opt, info);
7596 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7597 FALSE, FALSE, TRUE);
7598 if (opt_fd != NULL
7599 && (opt_fd->root.type == bfd_link_hash_defined
7600 || opt_fd->root.type == bfd_link_hash_defweak))
7601 {
7602 /* If glibc supports an optimized __tls_get_addr call stub,
7603 signalled by the presence of __tls_get_addr_opt, and we'll
7604 be calling __tls_get_addr via a plt call stub, then
7605 make __tls_get_addr point to __tls_get_addr_opt. */
7606 tga_fd = &htab->tls_get_addr_fd->elf;
7607 if (htab->elf.dynamic_sections_created
7608 && tga_fd != NULL
7609 && (tga_fd->type == STT_FUNC
7610 || tga_fd->needs_plt)
7611 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 7612 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
7613 {
7614 struct plt_entry *ent;
7615
7616 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7617 if (ent->plt.refcount > 0)
7618 break;
7619 if (ent != NULL)
7620 {
7621 tga_fd->root.type = bfd_link_hash_indirect;
7622 tga_fd->root.u.i.link = &opt_fd->root;
7623 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 7624 opt_fd->mark = 1;
a7f2871e
AM
7625 if (opt_fd->dynindx != -1)
7626 {
7627 /* Use __tls_get_addr_opt in dynamic relocations. */
7628 opt_fd->dynindx = -1;
7629 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7630 opt_fd->dynstr_index);
7631 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7632 return NULL;
a7f2871e 7633 }
2cdcc330
AM
7634 htab->tls_get_addr_fd
7635 = (struct ppc_link_hash_entry *) opt_fd;
a7f2871e
AM
7636 tga = &htab->tls_get_addr->elf;
7637 if (opt != NULL && tga != NULL)
7638 {
7639 tga->root.type = bfd_link_hash_indirect;
7640 tga->root.u.i.link = &opt->root;
7641 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 7642 opt->mark = 1;
a7f2871e
AM
7643 _bfd_elf_link_hash_hide_symbol (info, opt,
7644 tga->forced_local);
7645 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7646 }
7647 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7648 htab->tls_get_addr_fd->is_func_descriptor = 1;
7649 if (htab->tls_get_addr != NULL)
7650 {
7651 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7652 htab->tls_get_addr->is_func = 1;
7653 }
7654 }
7655 }
7656 }
7c9cf415
AM
7657 else if (htab->params->tls_get_addr_opt < 0)
7658 htab->params->tls_get_addr_opt = 0;
a7f2871e 7659 }
33c0ec9d 7660 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7661}
8387904d 7662
3a71aa26
AM
7663/* Return TRUE iff REL is a branch reloc with a global symbol matching
7664 HASH1 or HASH2. */
8387904d 7665
3a71aa26
AM
7666static bfd_boolean
7667branch_reloc_hash_match (const bfd *ibfd,
7668 const Elf_Internal_Rela *rel,
7669 const struct ppc_link_hash_entry *hash1,
7670 const struct ppc_link_hash_entry *hash2)
7671{
7672 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7673 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7674 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7675
e054468f 7676 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7677 {
3a71aa26
AM
7678 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7679 struct elf_link_hash_entry *h;
8387904d 7680
3a71aa26 7681 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7682 h = elf_follow_link (h);
3a71aa26
AM
7683 if (h == &hash1->elf || h == &hash2->elf)
7684 return TRUE;
a48ebf4d 7685 }
3a71aa26 7686 return FALSE;
951fd09b 7687}
411e1bfb 7688
951fd09b
AM
7689/* Run through all the TLS relocs looking for optimization
7690 opportunities. The linker has been hacked (see ppc64elf.em) to do
7691 a preliminary section layout so that we know the TLS segment
7692 offsets. We can't optimize earlier because some optimizations need
7693 to know the tp offset, and we need to optimize before allocating
7694 dynamic relocations. */
7695
7696bfd_boolean
33c0ec9d 7697ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7698{
7699 bfd *ibfd;
7700 asection *sec;
7701 struct ppc_link_hash_table *htab;
663a1470 7702 unsigned char *toc_ref;
102890f0 7703 int pass;
951fd09b 7704
3cbc1e5e 7705 if (!bfd_link_executable (info))
411e1bfb
AM
7706 return TRUE;
7707
951fd09b 7708 htab = ppc_hash_table (info);
4dfe6ac6
NC
7709 if (htab == NULL)
7710 return FALSE;
7711
663a1470
AM
7712 /* Make two passes over the relocs. On the first pass, mark toc
7713 entries involved with tls relocs, and check that tls relocs
7714 involved in setting up a tls_get_addr call are indeed followed by
7715 such a call. If they are not, we can't do any tls optimization.
7716 On the second pass twiddle tls_mask flags to notify
7717 relocate_section that optimization can be done, and adjust got
7718 and plt refcounts. */
7719 toc_ref = NULL;
7720 for (pass = 0; pass < 2; ++pass)
c72f2fb2 7721 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
7722 {
7723 Elf_Internal_Sym *locsyms = NULL;
7724 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7725
102890f0
AM
7726 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7727 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7728 {
7729 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7730 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7731
102890f0
AM
7732 /* Read the relocations. */
7733 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7734 info->keep_memory);
7735 if (relstart == NULL)
2915c55b
JK
7736 {
7737 free (toc_ref);
7738 return FALSE;
7739 }
411e1bfb 7740
102890f0
AM
7741 relend = relstart + sec->reloc_count;
7742 for (rel = relstart; rel < relend; rel++)
7743 {
7744 enum elf_ppc64_reloc_type r_type;
7745 unsigned long r_symndx;
7746 struct elf_link_hash_entry *h;
7747 Elf_Internal_Sym *sym;
7748 asection *sym_sec;
f961d9dd 7749 unsigned char *tls_mask;
46e9995a 7750 unsigned int tls_set, tls_clear, tls_type = 0;
102890f0
AM
7751 bfd_vma value;
7752 bfd_boolean ok_tprel, is_local;
7753 long toc_ref_index = 0;
7754 int expecting_tls_get_addr = 0;
663a1470 7755 bfd_boolean ret = FALSE;
411e1bfb 7756
102890f0
AM
7757 r_symndx = ELF64_R_SYM (rel->r_info);
7758 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7759 r_symndx, ibfd))
7760 {
7761 err_free_rel:
7762 if (elf_section_data (sec)->relocs != relstart)
7763 free (relstart);
7764 if (toc_ref != NULL)
7765 free (toc_ref);
7766 if (locsyms != NULL
0ffa91dd 7767 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7768 != (unsigned char *) locsyms))
7769 free (locsyms);
663a1470 7770 return ret;
102890f0 7771 }
411e1bfb 7772
102890f0
AM
7773 if (h != NULL)
7774 {
766bc656
AM
7775 if (h->root.type == bfd_link_hash_defined
7776 || h->root.type == bfd_link_hash_defweak)
7777 value = h->root.u.def.value;
7778 else if (h->root.type == bfd_link_hash_undefweak)
7779 value = 0;
7780 else
663a1470
AM
7781 {
7782 found_tls_get_addr_arg = 0;
7783 continue;
7784 }
102890f0
AM
7785 }
7786 else
7787 /* Symbols referenced by TLS relocs must be of type
7788 STT_TLS. So no need for .opd local sym adjust. */
7789 value = sym->st_value;
7790
7791 ok_tprel = FALSE;
7792 is_local = FALSE;
7793 if (h == NULL
7794 || !h->def_dynamic)
7795 {
7796 is_local = TRUE;
766bc656
AM
7797 if (h != NULL
7798 && h->root.type == bfd_link_hash_undefweak)
7799 ok_tprel = TRUE;
c27b8c2a
AM
7800 else if (sym_sec != NULL
7801 && sym_sec->output_section != NULL)
766bc656
AM
7802 {
7803 value += sym_sec->output_offset;
7804 value += sym_sec->output_section->vma;
0b147428 7805 value -= htab->elf.tls_sec->vma + TP_OFFSET;
c213164a
AM
7806 /* Note that even though the prefix insns
7807 allow a 1<<33 offset we use the same test
7808 as for addis;addi. There may be a mix of
7809 pcrel and non-pcrel code and the decision
7810 to optimise is per symbol, not per TLS
7811 sequence. */
0b147428 7812 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
766bc656 7813 }
102890f0 7814 }
951fd09b 7815
102890f0 7816 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7817 /* If this section has old-style __tls_get_addr calls
7818 without marker relocs, then check that each
7819 __tls_get_addr call reloc is preceded by a reloc
7820 that conceivably belongs to the __tls_get_addr arg
7821 setup insn. If we don't find matching arg setup
7822 relocs, don't do any tls optimization. */
7823 if (pass == 0
7824 && sec->has_tls_get_addr_call
7825 && h != NULL
7826 && (h == &htab->tls_get_addr->elf
7827 || h == &htab->tls_get_addr_fd->elf)
7828 && !found_tls_get_addr_arg
7829 && is_branch_reloc (r_type))
7830 {
25f53a85 7831 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
7832 "TLS optimization disabled\n"),
7833 ibfd, sec, rel->r_offset);
7834 ret = TRUE;
7835 goto err_free_rel;
7836 }
7837
7838 found_tls_get_addr_arg = 0;
102890f0
AM
7839 switch (r_type)
7840 {
7841 case R_PPC64_GOT_TLSLD16:
7842 case R_PPC64_GOT_TLSLD16_LO:
c213164a 7843 case R_PPC64_GOT_TLSLD34:
102890f0 7844 expecting_tls_get_addr = 1;
663a1470 7845 found_tls_get_addr_arg = 1;
1a0670f3 7846 /* Fall through. */
102890f0
AM
7847
7848 case R_PPC64_GOT_TLSLD16_HI:
7849 case R_PPC64_GOT_TLSLD16_HA:
7850 /* These relocs should never be against a symbol
7851 defined in a shared lib. Leave them alone if
7852 that turns out to be the case. */
7853 if (!is_local)
7854 continue;
411e1bfb 7855
102890f0 7856 /* LD -> LE */
411e1bfb 7857 tls_set = 0;
102890f0
AM
7858 tls_clear = TLS_LD;
7859 tls_type = TLS_TLS | TLS_LD;
7860 break;
411e1bfb 7861
102890f0
AM
7862 case R_PPC64_GOT_TLSGD16:
7863 case R_PPC64_GOT_TLSGD16_LO:
c213164a 7864 case R_PPC64_GOT_TLSGD34:
102890f0 7865 expecting_tls_get_addr = 1;
663a1470 7866 found_tls_get_addr_arg = 1;
1a0670f3 7867 /* Fall through. */
102890f0
AM
7868
7869 case R_PPC64_GOT_TLSGD16_HI:
7870 case R_PPC64_GOT_TLSGD16_HA:
7871 if (ok_tprel)
7872 /* GD -> LE */
411e1bfb 7873 tls_set = 0;
102890f0
AM
7874 else
7875 /* GD -> IE */
b00a0a86 7876 tls_set = TLS_TLS | TLS_GDIE;
102890f0
AM
7877 tls_clear = TLS_GD;
7878 tls_type = TLS_TLS | TLS_GD;
7879 break;
7880
c213164a 7881 case R_PPC64_GOT_TPREL34:
102890f0
AM
7882 case R_PPC64_GOT_TPREL16_DS:
7883 case R_PPC64_GOT_TPREL16_LO_DS:
7884 case R_PPC64_GOT_TPREL16_HI:
7885 case R_PPC64_GOT_TPREL16_HA:
7886 if (ok_tprel)
7887 {
7888 /* IE -> LE */
7889 tls_set = 0;
7890 tls_clear = TLS_TPREL;
7891 tls_type = TLS_TLS | TLS_TPREL;
7892 break;
7893 }
411e1bfb
AM
7894 continue;
7895
727fc41e
AM
7896 case R_PPC64_TLSGD:
7897 case R_PPC64_TLSLD:
23cedd1d
AM
7898 if (rel + 1 < relend
7899 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7900 {
7901 if (pass != 0
2cdcc330 7902 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
7903 != R_PPC64_PLTSEQ)
7904 && (ELF64_R_TYPE (rel[1].r_info)
7905 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
7906 {
7907 r_symndx = ELF64_R_SYM (rel[1].r_info);
7908 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 7909 r_symndx, ibfd))
23cedd1d
AM
7910 goto err_free_rel;
7911 if (h != NULL)
7912 {
7913 struct plt_entry *ent = NULL;
7914
7915 for (ent = h->plt.plist;
7916 ent != NULL;
7917 ent = ent->next)
7918 if (ent->addend == rel[1].r_addend)
7919 break;
7920
7921 if (ent != NULL
7922 && ent->plt.refcount > 0)
7923 ent->plt.refcount -= 1;
7924 }
7925 }
7926 continue;
7927 }
663a1470 7928 found_tls_get_addr_arg = 1;
1a0670f3 7929 /* Fall through. */
663a1470
AM
7930
7931 case R_PPC64_TLS:
7932 case R_PPC64_TOC16:
7933 case R_PPC64_TOC16_LO:
102890f0
AM
7934 if (sym_sec == NULL || sym_sec != toc)
7935 continue;
7936
7937 /* Mark this toc entry as referenced by a TLS
7938 code sequence. We can do that now in the
7939 case of R_PPC64_TLS, and after checking for
7940 tls_get_addr for the TOC16 relocs. */
7941 if (toc_ref == NULL)
2cdcc330
AM
7942 toc_ref
7943 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
7944 if (toc_ref == NULL)
7945 goto err_free_rel;
7946
102890f0
AM
7947 if (h != NULL)
7948 value = h->root.u.def.value;
7949 else
7950 value = sym->st_value;
7951 value += rel->r_addend;
73242275
AM
7952 if (value % 8 != 0)
7953 continue;
7954 BFD_ASSERT (value < toc->size
7955 && toc->output_offset % 8 == 0);
663a1470 7956 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7957 if (r_type == R_PPC64_TLS
7958 || r_type == R_PPC64_TLSGD
7959 || r_type == R_PPC64_TLSLD)
102890f0
AM
7960 {
7961 toc_ref[toc_ref_index] = 1;
7962 continue;
7963 }
7964
7965 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7966 continue;
7967
7968 tls_set = 0;
7969 tls_clear = 0;
7970 expecting_tls_get_addr = 2;
7971 break;
7972
7973 case R_PPC64_TPREL64:
7974 if (pass == 0
7975 || sec != toc
7976 || toc_ref == NULL
663a1470 7977 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7978 continue;
7979 if (ok_tprel)
7980 {
7981 /* IE -> LE */
7982 tls_set = TLS_EXPLICIT;
7983 tls_clear = TLS_TPREL;
7984 break;
7985 }
7986 continue;
7987
7988 case R_PPC64_DTPMOD64:
7989 if (pass == 0
7990 || sec != toc
7991 || toc_ref == NULL
663a1470 7992 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7993 continue;
7994 if (rel + 1 < relend
7995 && (rel[1].r_info
7996 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7997 && rel[1].r_offset == rel->r_offset + 8)
7998 {
7999 if (ok_tprel)
8000 /* GD -> LE */
8001 tls_set = TLS_EXPLICIT | TLS_GD;
8002 else
8003 /* GD -> IE */
b00a0a86 8004 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
102890f0
AM
8005 tls_clear = TLS_GD;
8006 }
8007 else
8008 {
8009 if (!is_local)
8010 continue;
8011
8012 /* LD -> LE */
8013 tls_set = TLS_EXPLICIT;
8014 tls_clear = TLS_LD;
8015 }
8016 break;
8017
8018 default:
8019 continue;
8020 }
8021
8022 if (pass == 0)
8023 {
727fc41e
AM
8024 if (!expecting_tls_get_addr
8025 || !sec->has_tls_get_addr_call)
102890f0
AM
8026 continue;
8027
3a71aa26
AM
8028 if (rel + 1 < relend
8029 && branch_reloc_hash_match (ibfd, rel + 1,
8030 htab->tls_get_addr,
8031 htab->tls_get_addr_fd))
102890f0 8032 {
3a71aa26 8033 if (expecting_tls_get_addr == 2)
102890f0 8034 {
3a71aa26 8035 /* Check for toc tls entries. */
f961d9dd 8036 unsigned char *toc_tls;
3a71aa26
AM
8037 int retval;
8038
8039 retval = get_tls_mask (&toc_tls, NULL, NULL,
8040 &locsyms,
8041 rel, ibfd);
8042 if (retval == 0)
8043 goto err_free_rel;
663a1470
AM
8044 if (toc_tls != NULL)
8045 {
37da22e5
AM
8046 if ((*toc_tls & TLS_TLS) != 0
8047 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8048 found_tls_get_addr_arg = 1;
8049 if (retval > 1)
8050 toc_ref[toc_ref_index] = 1;
8051 }
102890f0 8052 }
3a71aa26 8053 continue;
102890f0
AM
8054 }
8055
102890f0
AM
8056 /* Uh oh, we didn't find the expected call. We
8057 could just mark this symbol to exclude it
8058 from tls optimization but it's safer to skip
663a1470 8059 the entire optimization. */
695344c0 8060 /* xgettext:c-format */
25f53a85 8061 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8062 "TLS optimization disabled\n"),
8063 ibfd, sec, rel->r_offset);
8064 ret = TRUE;
8065 goto err_free_rel;
102890f0
AM
8066 }
8067
37da22e5
AM
8068 /* If we don't have old-style __tls_get_addr calls
8069 without TLSGD/TLSLD marker relocs, and we haven't
8070 found a new-style __tls_get_addr call with a
8071 marker for this symbol, then we either have a
8072 broken object file or an -mlongcall style
8073 indirect call to __tls_get_addr without a marker.
8074 Disable optimization in this case. */
8075 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8076 && (tls_set & TLS_EXPLICIT) == 0
8077 && !sec->has_tls_get_addr_call
8078 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8079 != (TLS_TLS | TLS_MARK)))
8080 continue;
8081
23cedd1d 8082 if (expecting_tls_get_addr)
102890f0 8083 {
23cedd1d
AM
8084 struct plt_entry *ent = NULL;
8085
8086 if (htab->tls_get_addr != NULL)
8087 for (ent = htab->tls_get_addr->elf.plt.plist;
8088 ent != NULL;
8089 ent = ent->next)
8090 if (ent->addend == 0)
102890f0 8091 break;
411e1bfb 8092
23cedd1d
AM
8093 if (ent == NULL && htab->tls_get_addr_fd != NULL)
8094 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8095 ent != NULL;
8096 ent = ent->next)
8097 if (ent->addend == 0)
102890f0 8098 break;
23cedd1d
AM
8099
8100 if (ent != NULL
8101 && ent->plt.refcount > 0)
8102 ent->plt.refcount -= 1;
102890f0 8103 }
411e1bfb 8104
102890f0 8105 if (tls_clear == 0)
30038c59
AM
8106 continue;
8107
102890f0
AM
8108 if ((tls_set & TLS_EXPLICIT) == 0)
8109 {
8110 struct got_entry *ent;
411e1bfb 8111
102890f0
AM
8112 /* Adjust got entry for this reloc. */
8113 if (h != NULL)
8114 ent = h->got.glist;
8115 else
8116 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8117
102890f0
AM
8118 for (; ent != NULL; ent = ent->next)
8119 if (ent->addend == rel->r_addend
8120 && ent->owner == ibfd
8121 && ent->tls_type == tls_type)
8122 break;
8123 if (ent == NULL)
8124 abort ();
411e1bfb 8125
102890f0
AM
8126 if (tls_set == 0)
8127 {
8128 /* We managed to get rid of a got entry. */
8129 if (ent->got.refcount > 0)
8130 ent->got.refcount -= 1;
8131 }
8132 }
8133 else
8134 {
8135 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8136 we'll lose one or two dyn relocs. */
8137 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8138 NULL, h, sym))
102890f0 8139 return FALSE;
411e1bfb 8140
102890f0
AM
8141 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8142 {
8143 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8144 NULL, h, sym))
102890f0
AM
8145 return FALSE;
8146 }
8147 }
411e1bfb 8148
46e9995a 8149 *tls_mask |= tls_set & 0xff;
102890f0
AM
8150 *tls_mask &= ~tls_clear;
8151 }
8c1d1bb8 8152
102890f0
AM
8153 if (elf_section_data (sec)->relocs != relstart)
8154 free (relstart);
8155 }
411e1bfb 8156
663a1470
AM
8157 if (locsyms != NULL
8158 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8159 {
8160 if (!info->keep_memory)
8161 free (locsyms);
8162 else
8163 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8164 }
8165 }
411e1bfb 8166
663a1470
AM
8167 if (toc_ref != NULL)
8168 free (toc_ref);
9a23f96e 8169 htab->do_tls_opt = 1;
b34976b6 8170 return TRUE;
1e2f5b6e 8171}
b34976b6 8172
c5614fa4
AM
8173/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8174 the values of any global symbols in a toc section that has been
8175 edited. Globals in toc sections should be a rarity, so this function
8176 sets a flag if any are found in toc sections other than the one just
de194d85 8177 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8178
8179struct adjust_toc_info
8180{
8181 asection *toc;
8182 unsigned long *skip;
8183 bfd_boolean global_toc_syms;
8184};
8185
ba761f19
AM
8186enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8187
c5614fa4
AM
8188static bfd_boolean
8189adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8190{
8191 struct ppc_link_hash_entry *eh;
8192 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8193 unsigned long i;
c5614fa4 8194
c5614fa4
AM
8195 if (h->root.type != bfd_link_hash_defined
8196 && h->root.type != bfd_link_hash_defweak)
8197 return TRUE;
8198
8199 eh = (struct ppc_link_hash_entry *) h;
8200 if (eh->adjust_done)
8201 return TRUE;
8202
8203 if (eh->elf.root.u.def.section == toc_inf->toc)
8204 {
854b41e7
AM
8205 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8206 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8207 else
854b41e7
AM
8208 i = eh->elf.root.u.def.value >> 3;
8209
ba761f19 8210 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8211 {
4eca0228 8212 _bfd_error_handler
854b41e7
AM
8213 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8214 do
8215 ++i;
ba761f19 8216 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8217 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8218 }
854b41e7
AM
8219
8220 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8221 eh->adjust_done = 1;
8222 }
8223 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8224 toc_inf->global_toc_syms = TRUE;
8225
8226 return TRUE;
8227}
8228
39eeab25
AM
8229/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8230 on a _LO variety toc/got reloc. */
560c8763
AM
8231
8232static bfd_boolean
39eeab25 8233ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8234{
39eeab25
AM
8235 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8236 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
8237 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8238 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8239 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8240 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8241 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8242 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8243 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8244 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8245 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8246 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8247 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8248 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8249 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
8250 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8251 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8252 /* Exclude lfqu by testing reloc. If relocs are ever
8253 defined for the reduced D field in psq_lu then those
8254 will need testing too. */
8255 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8256 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8257 && (insn & 1) == 0)
8258 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8259 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8260 /* Exclude stfqu. psq_stu as above for psq_lu. */
8261 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8262 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8263 && (insn & 1) == 0));
560c8763
AM
8264}
8265
4a421c53
AM
8266/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8267 pld ra,symbol@got@pcrel
dd9b12c2 8268 load/store rt,off(ra)
4a421c53 8269 or
d4b87b1e 8270 pla ra,symbol@pcrel
dd9b12c2 8271 load/store rt,off(ra)
4a421c53 8272 may be translated to
dd9b12c2 8273 pload/pstore rt,symbol+off@pcrel
4a421c53
AM
8274 nop.
8275 This function returns true if the optimization is possible, placing
dd9b12c2 8276 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
4a421c53
AM
8277
8278 On entry to this function, the linker has already determined that
d4b87b1e 8279 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8280 while *PINSN2 is the second instruction. */
8281
8282static bfd_boolean
dd9b12c2 8283xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
4a421c53
AM
8284{
8285 uint32_t insn2 = *pinsn2 >> 32;
8286 uint64_t i1new;
dd9b12c2 8287 bfd_signed_vma off;
4a421c53
AM
8288
8289 /* Check that regs match. */
8290 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
8291 return FALSE;
8292
8293 switch ((insn2 >> 26) & 63)
8294 {
8295 default:
8296 return FALSE;
8297
8298 case 32: /* lwz */
8299 case 34: /* lbz */
8300 case 36: /* stw */
8301 case 38: /* stb */
8302 case 40: /* lhz */
8303 case 42: /* lha */
8304 case 44: /* sth */
8305 case 48: /* lfs */
8306 case 50: /* lfd */
8307 case 52: /* stfs */
8308 case 54: /* stfd */
8309 /* These are the PMLS cases, where we just need to tack a prefix
dd9b12c2 8310 on the insn. */
4a421c53
AM
8311 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8312 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8313 off = insn2 & 0xffff;
4a421c53
AM
8314 break;
8315
8316 case 58: /* lwa, ld */
dd9b12c2 8317 if ((insn2 & 1) != 0)
4a421c53
AM
8318 return FALSE;
8319 i1new = ((1ULL << 58) | (1ULL << 52)
8320 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8321 | (insn2 & (31ULL << 21)));
dd9b12c2 8322 off = insn2 & 0xfffc;
4a421c53
AM
8323 break;
8324
8325 case 57: /* lxsd, lxssp */
dd9b12c2 8326 if ((insn2 & 3) < 2)
4a421c53
AM
8327 return FALSE;
8328 i1new = ((1ULL << 58) | (1ULL << 52)
8329 | ((40ULL | (insn2 & 3)) << 26)
8330 | (insn2 & (31ULL << 21)));
dd9b12c2 8331 off = insn2 & 0xfffc;
4a421c53
AM
8332 break;
8333
8334 case 61: /* stxsd, stxssp, lxv, stxv */
8335 if ((insn2 & 3) == 0)
8336 return FALSE;
8337 else if ((insn2 & 3) >= 2)
8338 {
4a421c53
AM
8339 i1new = ((1ULL << 58) | (1ULL << 52)
8340 | ((44ULL | (insn2 & 3)) << 26)
8341 | (insn2 & (31ULL << 21)));
dd9b12c2 8342 off = insn2 & 0xfffc;
4a421c53
AM
8343 }
8344 else
8345 {
4a421c53
AM
8346 i1new = ((1ULL << 58) | (1ULL << 52)
8347 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8348 | (insn2 & (31ULL << 21)));
dd9b12c2 8349 off = insn2 & 0xfff0;
4a421c53
AM
8350 }
8351 break;
8352
8353 case 56: /* lq */
4a421c53
AM
8354 i1new = ((1ULL << 58) | (1ULL << 52)
8355 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8356 off = insn2 & 0xffff;
4a421c53
AM
8357 break;
8358
8359 case 62: /* std, stq */
dd9b12c2 8360 if ((insn2 & 1) != 0)
4a421c53
AM
8361 return FALSE;
8362 i1new = ((1ULL << 58) | (1ULL << 52)
8363 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8364 | (insn2 & (31ULL << 21)));
dd9b12c2 8365 off = insn2 & 0xfffc;
4a421c53
AM
8366 break;
8367 }
8368
8369 *pinsn1 = i1new;
8370 *pinsn2 = (uint64_t) NOP << 32;
dd9b12c2 8371 *poff = (off ^ 0x8000) - 0x8000;
4a421c53
AM
8372 return TRUE;
8373}
8374
c5614fa4
AM
8375/* Examine all relocs referencing .toc sections in order to remove
8376 unused .toc entries. */
8377
8378bfd_boolean
33c0ec9d 8379ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8380{
8381 bfd *ibfd;
8382 struct adjust_toc_info toc_inf;
67f0cbdb 8383 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8384
67f0cbdb 8385 htab->do_toc_opt = 1;
c5614fa4 8386 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8387 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8388 {
8389 asection *toc, *sec;
8390 Elf_Internal_Shdr *symtab_hdr;
8391 Elf_Internal_Sym *local_syms;
425b145b 8392 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8393 unsigned long *skip, *drop;
8394 unsigned char *used;
8395 unsigned char *keep, last, some_unused;
8396
854b41e7
AM
8397 if (!is_ppc64_elf (ibfd))
8398 continue;
8399
c5614fa4
AM
8400 toc = bfd_get_section_by_name (ibfd, ".toc");
8401 if (toc == NULL
92b7a70f 8402 || toc->size == 0
dbaa2011
AM
8403 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8404 || discarded_section (toc))
c5614fa4
AM
8405 continue;
8406
425b145b 8407 toc_relocs = NULL;
c5614fa4 8408 local_syms = NULL;
0ffa91dd 8409 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8410
8411 /* Look at sections dropped from the final link. */
8412 skip = NULL;
8413 relstart = NULL;
8414 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8415 {
8416 if (sec->reloc_count == 0
dbaa2011 8417 || !discarded_section (sec)
c5614fa4
AM
8418 || get_opd_info (sec)
8419 || (sec->flags & SEC_ALLOC) == 0
8420 || (sec->flags & SEC_DEBUGGING) != 0)
8421 continue;
8422
8423 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8424 if (relstart == NULL)
8425 goto error_ret;
8426
8427 /* Run through the relocs to see which toc entries might be
8428 unused. */
8429 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8430 {
8431 enum elf_ppc64_reloc_type r_type;
8432 unsigned long r_symndx;
8433 asection *sym_sec;
8434 struct elf_link_hash_entry *h;
8435 Elf_Internal_Sym *sym;
8436 bfd_vma val;
8437
8438 r_type = ELF64_R_TYPE (rel->r_info);
8439 switch (r_type)
8440 {
8441 default:
8442 continue;
8443
8444 case R_PPC64_TOC16:
8445 case R_PPC64_TOC16_LO:
8446 case R_PPC64_TOC16_HI:
8447 case R_PPC64_TOC16_HA:
8448 case R_PPC64_TOC16_DS:
8449 case R_PPC64_TOC16_LO_DS:
8450 break;
8451 }
8452
8453 r_symndx = ELF64_R_SYM (rel->r_info);
8454 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8455 r_symndx, ibfd))
8456 goto error_ret;
8457
8458 if (sym_sec != toc)
8459 continue;
8460
8461 if (h != NULL)
8462 val = h->root.u.def.value;
8463 else
8464 val = sym->st_value;
8465 val += rel->r_addend;
8466
8467 if (val >= toc->size)
8468 continue;
8469
8470 /* Anything in the toc ought to be aligned to 8 bytes.
8471 If not, don't mark as unused. */
8472 if (val & 7)
8473 continue;
8474
8475 if (skip == NULL)
8476 {
854b41e7 8477 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8478 if (skip == NULL)
8479 goto error_ret;
8480 }
8481
ba761f19 8482 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8483 }
8484
8485 if (elf_section_data (sec)->relocs != relstart)
8486 free (relstart);
8487 }
8488
ba761f19
AM
8489 /* For largetoc loads of address constants, we can convert
8490 . addis rx,2,addr@got@ha
8491 . ld ry,addr@got@l(rx)
8492 to
8493 . addis rx,2,addr@toc@ha
8494 . addi ry,rx,addr@toc@l
8495 when addr is within 2G of the toc pointer. This then means
8496 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8497
ba761f19
AM
8498 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8499 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8500 && toc->reloc_count != 0)
8501 {
8502 /* Read toc relocs. */
425b145b
AM
8503 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8504 info->keep_memory);
8505 if (toc_relocs == NULL)
ba761f19
AM
8506 goto error_ret;
8507
425b145b 8508 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8509 {
8510 enum elf_ppc64_reloc_type r_type;
8511 unsigned long r_symndx;
8512 asection *sym_sec;
8513 struct elf_link_hash_entry *h;
8514 Elf_Internal_Sym *sym;
8515 bfd_vma val, addr;
8516
8517 r_type = ELF64_R_TYPE (rel->r_info);
8518 if (r_type != R_PPC64_ADDR64)
8519 continue;
8520
8521 r_symndx = ELF64_R_SYM (rel->r_info);
8522 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8523 r_symndx, ibfd))
8524 goto error_ret;
8525
425b145b 8526 if (sym_sec == NULL
c27b8c2a 8527 || sym_sec->output_section == NULL
dbaa2011 8528 || discarded_section (sym_sec))
425b145b
AM
8529 continue;
8530
afe397ea 8531 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8532 continue;
8533
8534 if (h != NULL)
bddc25c9
AM
8535 {
8536 if (h->type == STT_GNU_IFUNC)
8537 continue;
8538 val = h->root.u.def.value;
8539 }
ba761f19 8540 else
bddc25c9
AM
8541 {
8542 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8543 continue;
8544 val = sym->st_value;
8545 }
ba761f19
AM
8546 val += rel->r_addend;
8547 val += sym_sec->output_section->vma + sym_sec->output_offset;
8548
8549 /* We don't yet know the exact toc pointer value, but we
8550 know it will be somewhere in the toc section. Don't
8551 optimize if the difference from any possible toc
8552 pointer is outside [ff..f80008000, 7fff7fff]. */
8553 addr = toc->output_section->vma + TOC_BASE_OFF;
8554 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8555 continue;
8556
8557 addr = toc->output_section->vma + toc->output_section->rawsize;
8558 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8559 continue;
8560
8561 if (skip == NULL)
8562 {
8563 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8564 if (skip == NULL)
8565 goto error_ret;
8566 }
8567
8568 skip[rel->r_offset >> 3]
425b145b 8569 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8570 }
ba761f19
AM
8571 }
8572
c5614fa4
AM
8573 if (skip == NULL)
8574 continue;
8575
8576 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8577 if (used == NULL)
8578 {
8579 error_ret:
8580 if (local_syms != NULL
8581 && symtab_hdr->contents != (unsigned char *) local_syms)
8582 free (local_syms);
8583 if (sec != NULL
8584 && relstart != NULL
8585 && elf_section_data (sec)->relocs != relstart)
8586 free (relstart);
425b145b
AM
8587 if (toc_relocs != NULL
8588 && elf_section_data (toc)->relocs != toc_relocs)
8589 free (toc_relocs);
c5614fa4
AM
8590 if (skip != NULL)
8591 free (skip);
8592 return FALSE;
8593 }
8594
30038c59
AM
8595 /* Now check all kept sections that might reference the toc.
8596 Check the toc itself last. */
8597 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8598 : ibfd->sections);
c5614fa4 8599 sec != NULL;
c5614fa4 8600 sec = (sec == toc ? NULL
c5614fa4 8601 : sec->next == NULL ? toc
30038c59 8602 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8603 : sec->next))
8604 {
8605 int repeat;
8606
8607 if (sec->reloc_count == 0
dbaa2011 8608 || discarded_section (sec)
c5614fa4
AM
8609 || get_opd_info (sec)
8610 || (sec->flags & SEC_ALLOC) == 0
8611 || (sec->flags & SEC_DEBUGGING) != 0)
8612 continue;
8613
854b41e7
AM
8614 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8615 info->keep_memory);
c5614fa4 8616 if (relstart == NULL)
2915c55b
JK
8617 {
8618 free (used);
8619 goto error_ret;
8620 }
c5614fa4
AM
8621
8622 /* Mark toc entries referenced as used. */
c5614fa4 8623 do
d4f1ee75
AM
8624 {
8625 repeat = 0;
8626 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8627 {
8628 enum elf_ppc64_reloc_type r_type;
8629 unsigned long r_symndx;
8630 asection *sym_sec;
8631 struct elf_link_hash_entry *h;
8632 Elf_Internal_Sym *sym;
8633 bfd_vma val;
98528052 8634
d4f1ee75 8635 r_type = ELF64_R_TYPE (rel->r_info);
d4f1ee75
AM
8636 switch (r_type)
8637 {
8638 case R_PPC64_TOC16:
8639 case R_PPC64_TOC16_LO:
8640 case R_PPC64_TOC16_HI:
8641 case R_PPC64_TOC16_HA:
8642 case R_PPC64_TOC16_DS:
8643 case R_PPC64_TOC16_LO_DS:
8644 /* In case we're taking addresses of toc entries. */
8645 case R_PPC64_ADDR64:
8646 break;
c5614fa4 8647
d4f1ee75
AM
8648 default:
8649 continue;
8650 }
c5614fa4 8651
d4f1ee75
AM
8652 r_symndx = ELF64_R_SYM (rel->r_info);
8653 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8654 r_symndx, ibfd))
8655 {
8656 free (used);
8657 goto error_ret;
8658 }
c5614fa4 8659
d4f1ee75
AM
8660 if (sym_sec != toc)
8661 continue;
c5614fa4 8662
d4f1ee75
AM
8663 if (h != NULL)
8664 val = h->root.u.def.value;
8665 else
8666 val = sym->st_value;
8667 val += rel->r_addend;
ba761f19 8668
d4f1ee75
AM
8669 if (val >= toc->size)
8670 continue;
ba761f19 8671
d4f1ee75
AM
8672 if ((skip[val >> 3] & can_optimize) != 0)
8673 {
8674 bfd_vma off;
8675 unsigned char opc;
8676
8677 switch (r_type)
8678 {
8679 case R_PPC64_TOC16_HA:
ba761f19 8680 break;
ba761f19 8681
d4f1ee75
AM
8682 case R_PPC64_TOC16_LO_DS:
8683 off = rel->r_offset;
8684 off += (bfd_big_endian (ibfd) ? -2 : 3);
8685 if (!bfd_get_section_contents (ibfd, sec, &opc,
8686 off, 1))
8687 {
8688 free (used);
8689 goto error_ret;
8690 }
8691 if ((opc & (0x3f << 2)) == (58u << 2))
8692 break;
1a0670f3 8693 /* Fall through. */
ba761f19 8694
d4f1ee75
AM
8695 default:
8696 /* Wrong sort of reloc, or not a ld. We may
8697 as well clear ref_from_discarded too. */
8698 skip[val >> 3] = 0;
8699 }
8700 }
8701
8702 if (sec != toc)
8703 used[val >> 3] = 1;
8704 /* For the toc section, we only mark as used if this
8705 entry itself isn't unused. */
8706 else if ((used[rel->r_offset >> 3]
8707 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8708 && !used[val >> 3])
8709 {
8710 /* Do all the relocs again, to catch reference
8711 chains. */
8712 repeat = 1;
8713 used[val >> 3] = 1;
8714 }
8715 }
8716 }
c5614fa4 8717 while (repeat);
854b41e7
AM
8718
8719 if (elf_section_data (sec)->relocs != relstart)
8720 free (relstart);
c5614fa4
AM
8721 }
8722
8723 /* Merge the used and skip arrays. Assume that TOC
8724 doublewords not appearing as either used or unused belong
de194d85 8725 to an entry more than one doubleword in size. */
c5614fa4
AM
8726 for (drop = skip, keep = used, last = 0, some_unused = 0;
8727 drop < skip + (toc->size + 7) / 8;
8728 ++drop, ++keep)
8729 {
8730 if (*keep)
8731 {
ba761f19
AM
8732 *drop &= ~ref_from_discarded;
8733 if ((*drop & can_optimize) != 0)
8734 some_unused = 1;
c5614fa4
AM
8735 last = 0;
8736 }
b140b010 8737 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8738 {
8739 some_unused = 1;
ba761f19 8740 last = ref_from_discarded;
c5614fa4
AM
8741 }
8742 else
8743 *drop = last;
8744 }
8745
8746 free (used);
8747
8748 if (some_unused)
8749 {
8750 bfd_byte *contents, *src;
8751 unsigned long off;
d62b3684 8752 Elf_Internal_Sym *sym;
ba761f19 8753 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8754
8755 /* Shuffle the toc contents, and at the same time convert the
8756 skip array from booleans into offsets. */
8757 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8758 goto error_ret;
8759
8760 elf_section_data (toc)->this_hdr.contents = contents;
8761
8762 for (src = contents, off = 0, drop = skip;
8763 src < contents + toc->size;
8764 src += 8, ++drop)
8765 {
ba761f19
AM
8766 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8767 off += 8;
c5614fa4
AM
8768 else if (off != 0)
8769 {
8770 *drop = off;
8771 memcpy (src - off, src, 8);
8772 }
8773 }
854b41e7 8774 *drop = off;
c5614fa4
AM
8775 toc->rawsize = toc->size;
8776 toc->size = src - contents - off;
8777
ba761f19
AM
8778 /* Adjust addends for relocs against the toc section sym,
8779 and optimize any accesses we can. */
c5614fa4
AM
8780 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8781 {
8782 if (sec->reloc_count == 0
dbaa2011 8783 || discarded_section (sec))
c5614fa4
AM
8784 continue;
8785
8786 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8787 info->keep_memory);
c5614fa4
AM
8788 if (relstart == NULL)
8789 goto error_ret;
8790
8791 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8792 {
8793 enum elf_ppc64_reloc_type r_type;
8794 unsigned long r_symndx;
8795 asection *sym_sec;
8796 struct elf_link_hash_entry *h;
854b41e7 8797 bfd_vma val;
c5614fa4
AM
8798
8799 r_type = ELF64_R_TYPE (rel->r_info);
8800 switch (r_type)
8801 {
8802 default:
8803 continue;
8804
8805 case R_PPC64_TOC16:
8806 case R_PPC64_TOC16_LO:
8807 case R_PPC64_TOC16_HI:
8808 case R_PPC64_TOC16_HA:
8809 case R_PPC64_TOC16_DS:
8810 case R_PPC64_TOC16_LO_DS:
8811 case R_PPC64_ADDR64:
8812 break;
8813 }
8814
8815 r_symndx = ELF64_R_SYM (rel->r_info);
8816 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8817 r_symndx, ibfd))
8818 goto error_ret;
8819
ba761f19 8820 if (sym_sec != toc)
c5614fa4
AM
8821 continue;
8822
ba761f19
AM
8823 if (h != NULL)
8824 val = h->root.u.def.value;
8825 else
8826 {
8827 val = sym->st_value;
8828 if (val != 0)
8829 local_toc_syms = TRUE;
8830 }
8831
8832 val += rel->r_addend;
854b41e7
AM
8833
8834 if (val > toc->rawsize)
8835 val = toc->rawsize;
ba761f19
AM
8836 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8837 continue;
8838 else if ((skip[val >> 3] & can_optimize) != 0)
8839 {
8840 Elf_Internal_Rela *tocrel
425b145b 8841 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8842 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8843
8844 switch (r_type)
8845 {
8846 case R_PPC64_TOC16_HA:
8847 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8848 break;
8849
8850 case R_PPC64_TOC16_LO_DS:
8851 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8852 break;
8853
8854 default:
28942f62
AM
8855 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8856 ppc_howto_init ();
b140b010 8857 info->callbacks->einfo
695344c0 8858 /* xgettext:c-format */
174d0a74 8859 (_("%H: %s references "
b140b010
AM
8860 "optimized away TOC entry\n"),
8861 ibfd, sec, rel->r_offset,
8862 ppc64_elf_howto_table[r_type]->name);
8863 bfd_set_error (bfd_error_bad_value);
8864 goto error_ret;
ba761f19
AM
8865 }
8866 rel->r_addend = tocrel->r_addend;
8867 elf_section_data (sec)->relocs = relstart;
8868 continue;
8869 }
8870
8871 if (h != NULL || sym->st_value != 0)
8872 continue;
854b41e7
AM
8873
8874 rel->r_addend -= skip[val >> 3];
8875 elf_section_data (sec)->relocs = relstart;
c5614fa4 8876 }
854b41e7
AM
8877
8878 if (elf_section_data (sec)->relocs != relstart)
8879 free (relstart);
c5614fa4
AM
8880 }
8881
8882 /* We shouldn't have local or global symbols defined in the TOC,
8883 but handle them anyway. */
df22d223
AM
8884 if (local_syms != NULL)
8885 for (sym = local_syms;
8886 sym < local_syms + symtab_hdr->sh_info;
8887 ++sym)
8888 if (sym->st_value != 0
8889 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8890 {
8891 unsigned long i;
854b41e7 8892
df22d223
AM
8893 if (sym->st_value > toc->rawsize)
8894 i = toc->rawsize >> 3;
8895 else
8896 i = sym->st_value >> 3;
854b41e7 8897
df22d223
AM
8898 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8899 {
8900 if (local_toc_syms)
4eca0228 8901 _bfd_error_handler
df22d223
AM
8902 (_("%s defined on removed toc entry"),
8903 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8904 do
8905 ++i;
8906 while ((skip[i] & (ref_from_discarded | can_optimize)));
8907 sym->st_value = (bfd_vma) i << 3;
8908 }
d62b3684 8909
df22d223
AM
8910 sym->st_value -= skip[i];
8911 symtab_hdr->contents = (unsigned char *) local_syms;
8912 }
c5614fa4 8913
854b41e7 8914 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8915 if (toc_inf.global_toc_syms)
8916 {
8917 toc_inf.toc = toc;
8918 toc_inf.skip = skip;
8919 toc_inf.global_toc_syms = FALSE;
8920 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8921 &toc_inf);
8922 }
854b41e7
AM
8923
8924 if (toc->reloc_count != 0)
8925 {
d4730f92 8926 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8927 Elf_Internal_Rela *wrel;
8928 bfd_size_type sz;
8929
854b41e7 8930 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8931 if (toc_relocs == NULL)
8932 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8933 info->keep_memory);
8934 if (toc_relocs == NULL)
8935 goto error_ret;
8936
425b145b
AM
8937 wrel = toc_relocs;
8938 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8939 if ((skip[rel->r_offset >> 3]
8940 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8941 {
8942 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8943 wrel->r_info = rel->r_info;
8944 wrel->r_addend = rel->r_addend;
8945 ++wrel;
8946 }
8947 else if (!dec_dynrel_count (rel->r_info, toc, info,
8948 &local_syms, NULL, NULL))
8949 goto error_ret;
8950
425b145b
AM
8951 elf_section_data (toc)->relocs = toc_relocs;
8952 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8953 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8954 sz = rel_hdr->sh_entsize;
8955 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8956 }
c5614fa4 8957 }
28be611c
AM
8958 else if (toc_relocs != NULL
8959 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8960 free (toc_relocs);
c5614fa4
AM
8961
8962 if (local_syms != NULL
8963 && symtab_hdr->contents != (unsigned char *) local_syms)
8964 {
8965 if (!info->keep_memory)
8966 free (local_syms);
8967 else
8968 symtab_hdr->contents = (unsigned char *) local_syms;
8969 }
8970 free (skip);
8971 }
8972
066f4018 8973 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
8974 a GOT relative or PC relative access, possibly reducing the
8975 number of GOT entries. */
066f4018
AM
8976 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8977 {
8978 asection *sec;
8979 Elf_Internal_Shdr *symtab_hdr;
8980 Elf_Internal_Sym *local_syms;
8981 Elf_Internal_Rela *relstart, *rel;
8982 bfd_vma got;
8983
8984 if (!is_ppc64_elf (ibfd))
8985 continue;
8986
903b777d 8987 if (!ppc64_elf_tdata (ibfd)->has_optrel)
066f4018
AM
8988 continue;
8989
8990 sec = ppc64_elf_tdata (ibfd)->got;
903b777d
AM
8991 got = 0;
8992 if (sec != NULL)
8993 got = sec->output_section->vma + sec->output_offset + 0x8000;
066f4018
AM
8994
8995 local_syms = NULL;
8996 symtab_hdr = &elf_symtab_hdr (ibfd);
8997
8998 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8999 {
9000 if (sec->reloc_count == 0
903b777d 9001 || !ppc64_elf_section_data (sec)->has_optrel
066f4018
AM
9002 || discarded_section (sec))
9003 continue;
9004
9005 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9006 info->keep_memory);
9007 if (relstart == NULL)
9008 {
9009 got_error_ret:
9010 if (local_syms != NULL
9011 && symtab_hdr->contents != (unsigned char *) local_syms)
9012 free (local_syms);
9013 if (sec != NULL
9014 && relstart != NULL
9015 && elf_section_data (sec)->relocs != relstart)
9016 free (relstart);
9017 return FALSE;
9018 }
9019
9020 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9021 {
9022 enum elf_ppc64_reloc_type r_type;
9023 unsigned long r_symndx;
9024 Elf_Internal_Sym *sym;
9025 asection *sym_sec;
9026 struct elf_link_hash_entry *h;
9027 struct got_entry *ent;
4a421c53
AM
9028 bfd_vma sym_addend, val, pc;
9029 unsigned char buf[8];
066f4018 9030 unsigned int insn;
903b777d 9031 enum {no_check, check_lo, check_ha} insn_check;
066f4018
AM
9032
9033 r_type = ELF64_R_TYPE (rel->r_info);
903b777d
AM
9034 switch (r_type)
9035 {
9036 default:
9037 insn_check = no_check;
9038 break;
9039
9040 case R_PPC64_PLT16_HA:
9041 case R_PPC64_GOT_TLSLD16_HA:
9042 case R_PPC64_GOT_TLSGD16_HA:
9043 case R_PPC64_GOT_TPREL16_HA:
9044 case R_PPC64_GOT_DTPREL16_HA:
9045 case R_PPC64_GOT16_HA:
9046 case R_PPC64_TOC16_HA:
9047 insn_check = check_ha;
9048 break;
9049
9050 case R_PPC64_PLT16_LO:
9051 case R_PPC64_PLT16_LO_DS:
9052 case R_PPC64_GOT_TLSLD16_LO:
9053 case R_PPC64_GOT_TLSGD16_LO:
9054 case R_PPC64_GOT_TPREL16_LO_DS:
9055 case R_PPC64_GOT_DTPREL16_LO_DS:
9056 case R_PPC64_GOT16_LO:
9057 case R_PPC64_GOT16_LO_DS:
9058 case R_PPC64_TOC16_LO:
9059 case R_PPC64_TOC16_LO_DS:
9060 insn_check = check_lo;
9061 break;
9062 }
9063
9064 if (insn_check != no_check)
9065 {
9066 bfd_vma off = rel->r_offset & ~3;
9067
9068 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9069 goto got_error_ret;
9070
9071 insn = bfd_get_32 (ibfd, buf);
9072 if (insn_check == check_lo
9073 ? !ok_lo_toc_insn (insn, r_type)
9074 : ((insn & ((0x3f << 26) | 0x1f << 16))
9075 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9076 {
9077 char str[12];
9078
9079 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9080 sprintf (str, "%#08x", insn);
9081 info->callbacks->einfo
9082 /* xgettext:c-format */
9083 (_("%H: got/toc optimization is not supported for"
9084 " %s instruction\n"),
9085 ibfd, sec, rel->r_offset & ~3, str);
9086 continue;
9087 }
9088 }
9089
066f4018
AM
9090 switch (r_type)
9091 {
bb22a418
AM
9092 /* Note that we don't delete GOT entries for
9093 R_PPC64_GOT16_DS since we'd need a lot more
9094 analysis. For starters, the preliminary layout is
9095 before the GOT, PLT, dynamic sections and stubs are
9096 laid out. Then we'd need to allow for changes in
9097 distance between sections caused by alignment. */
066f4018
AM
9098 default:
9099 continue;
9100
066f4018
AM
9101 case R_PPC64_GOT16_HA:
9102 case R_PPC64_GOT16_LO_DS:
4a421c53
AM
9103 sym_addend = rel->r_addend;
9104 break;
9105
9106 case R_PPC64_GOT_PCREL34:
9107 sym_addend = 0;
066f4018
AM
9108 break;
9109 }
9110
9111 r_symndx = ELF64_R_SYM (rel->r_info);
9112 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9113 r_symndx, ibfd))
9114 goto got_error_ret;
9115
6d5554a6
AM
9116 if (sym_sec == NULL
9117 || sym_sec->output_section == NULL
9118 || discarded_section (sym_sec))
9119 continue;
9120
066f4018
AM
9121 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9122 continue;
9123
9124 if (h != NULL)
9125 val = h->root.u.def.value;
9126 else
9127 val = sym->st_value;
4a421c53 9128 val += sym_addend;
066f4018
AM
9129 val += sym_sec->output_section->vma + sym_sec->output_offset;
9130
bb22a418
AM
9131/* Fudge factor to allow for the fact that the preliminary layout
9132 isn't exact. Reduce limits by this factor. */
9133#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9134
066f4018
AM
9135 switch (r_type)
9136 {
9137 default:
9138 continue;
9139
066f4018 9140 case R_PPC64_GOT16_HA:
bb22a418
AM
9141 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9142 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9143 continue;
9144
9145 if (!bfd_get_section_contents (ibfd, sec, buf,
9146 rel->r_offset & ~3, 4))
9147 goto got_error_ret;
9148 insn = bfd_get_32 (ibfd, buf);
9149 if (((insn & ((0x3f << 26) | 0x1f << 16))
9150 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9151 continue;
9152 break;
9153
9154 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9155 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9156 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9157 continue;
9158 if (!bfd_get_section_contents (ibfd, sec, buf,
9159 rel->r_offset & ~3, 4))
9160 goto got_error_ret;
9161 insn = bfd_get_32 (ibfd, buf);
9162 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9163 continue;
9164 break;
4a421c53
AM
9165
9166 case R_PPC64_GOT_PCREL34:
9167 pc = rel->r_offset;
9168 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9169 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9170 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9171 continue;
9172 if (!bfd_get_section_contents (ibfd, sec, buf,
9173 rel->r_offset & ~3, 8))
9174 goto got_error_ret;
9175 insn = bfd_get_32 (ibfd, buf);
9176 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9177 continue;
9178 insn = bfd_get_32 (ibfd, buf + 4);
9179 if ((insn & (0x3f << 26)) != 57u << 26)
9180 continue;
9181 break;
066f4018 9182 }
bb22a418 9183#undef LIMIT_ADJUST
066f4018
AM
9184
9185 if (h != NULL)
9186 ent = h->got.glist;
9187 else
9188 {
9189 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9190 ent = local_got_ents[r_symndx];
9191 }
9192 for (; ent != NULL; ent = ent->next)
4a421c53 9193 if (ent->addend == sym_addend
066f4018
AM
9194 && ent->owner == ibfd
9195 && ent->tls_type == 0)
9196 break;
9197 BFD_ASSERT (ent && ent->got.refcount > 0);
9198 ent->got.refcount -= 1;
9199 }
9200
9201 if (elf_section_data (sec)->relocs != relstart)
9202 free (relstart);
9203 }
9204
9205 if (local_syms != NULL
9206 && symtab_hdr->contents != (unsigned char *) local_syms)
9207 {
9208 if (!info->keep_memory)
9209 free (local_syms);
9210 else
9211 symtab_hdr->contents = (unsigned char *) local_syms;
9212 }
9213 }
9214
c5614fa4
AM
9215 return TRUE;
9216}
9217
1bbe0902
AM
9218/* Return true iff input section I references the TOC using
9219 instructions limited to +/-32k offsets. */
9220
9221bfd_boolean
9222ppc64_elf_has_small_toc_reloc (asection *i)
9223{
9224 return (is_ppc64_elf (i->owner)
9225 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9226}
9227
927be08e
AM
9228/* Allocate space for one GOT entry. */
9229
9230static void
9231allocate_got (struct elf_link_hash_entry *h,
9232 struct bfd_link_info *info,
9233 struct got_entry *gent)
9234{
9235 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9236 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9237 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9238 ? 16 : 8);
9239 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9240 ? 2 : 1) * sizeof (Elf64_External_Rela);
9241 asection *got = ppc64_elf_tdata (gent->owner)->got;
9242
9243 gent->got.offset = got->size;
9244 got->size += entsize;
9245
19e08130 9246 if (h->type == STT_GNU_IFUNC)
927be08e 9247 {
33e44f2e 9248 htab->elf.irelplt->size += rentsize;
19e08130 9249 htab->got_reli_size += rentsize;
927be08e 9250 }
f15d0b54
AM
9251 else if (((bfd_link_pic (info)
9252 && !((gent->tls_type & TLS_TPREL) != 0
9253 && bfd_link_executable (info)
9254 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9255 || (htab->elf.dynamic_sections_created
9256 && h->dynindx != -1
9257 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9258 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9259 {
19e08130 9260 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9261 relgot->size += rentsize;
927be08e
AM
9262 }
9263}
9264
7865406b
AM
9265/* This function merges got entries in the same toc group. */
9266
9267static void
9268merge_got_entries (struct got_entry **pent)
9269{
9270 struct got_entry *ent, *ent2;
9271
9272 for (ent = *pent; ent != NULL; ent = ent->next)
9273 if (!ent->is_indirect)
9274 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9275 if (!ent2->is_indirect
9276 && ent2->addend == ent->addend
9277 && ent2->tls_type == ent->tls_type
9278 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9279 {
9280 ent2->is_indirect = TRUE;
9281 ent2->got.ent = ent;
9282 }
9283}
9284
46434633 9285/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9286
9287static bfd_boolean
46434633
AM
9288ensure_undef_dynamic (struct bfd_link_info *info,
9289 struct elf_link_hash_entry *h)
f0158f44
AM
9290{
9291 struct elf_link_hash_table *htab = elf_hash_table (info);
9292
9293 if (htab->dynamic_sections_created
46434633
AM
9294 && ((info->dynamic_undefined_weak != 0
9295 && h->root.type == bfd_link_hash_undefweak)
9296 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9297 && h->dynindx == -1
9298 && !h->forced_local
9299 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9300 return bfd_elf_link_record_dynamic_symbol (info, h);
9301 return TRUE;
9302}
9303
65f38f15
AM
9304/* Allocate space in .plt, .got and associated reloc sections for
9305 dynamic relocs. */
5bd4f169 9306
b34976b6 9307static bfd_boolean
4ce794b7 9308allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9309{
65f38f15
AM
9310 struct bfd_link_info *info;
9311 struct ppc_link_hash_table *htab;
5bd4f169 9312 asection *s;
65f38f15 9313 struct ppc_link_hash_entry *eh;
0b8bcf0d 9314 struct got_entry **pgent, *gent;
5bd4f169 9315
e92d460e 9316 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9317 return TRUE;
5bd4f169 9318
65f38f15
AM
9319 info = (struct bfd_link_info *) inf;
9320 htab = ppc_hash_table (info);
4dfe6ac6
NC
9321 if (htab == NULL)
9322 return FALSE;
5bd4f169 9323
951fd09b
AM
9324 eh = (struct ppc_link_hash_entry *) h;
9325 /* Run through the TLS GD got entries first if we're changing them
9326 to TPREL. */
b00a0a86 9327 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
951fd09b
AM
9328 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9329 if (gent->got.refcount > 0
9330 && (gent->tls_type & TLS_GD) != 0)
9331 {
9332 /* This was a GD entry that has been converted to TPREL. If
9333 there happens to be a TPREL entry we can use that one. */
9334 struct got_entry *ent;
9335 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9336 if (ent->got.refcount > 0
9337 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9338 && ent->addend == gent->addend
9339 && ent->owner == gent->owner)
951fd09b
AM
9340 {
9341 gent->got.refcount = 0;
9342 break;
9343 }
9344
9345 /* If not, then we'll be using our own TPREL entry. */
9346 if (gent->got.refcount != 0)
9347 gent->tls_type = TLS_TLS | TLS_TPREL;
9348 }
9349
7865406b
AM
9350 /* Remove any list entry that won't generate a word in the GOT before
9351 we call merge_got_entries. Otherwise we risk merging to empty
9352 entries. */
0b8bcf0d
AM
9353 pgent = &h->got.glist;
9354 while ((gent = *pgent) != NULL)
411e1bfb 9355 if (gent->got.refcount > 0)
7865406b
AM
9356 {
9357 if ((gent->tls_type & TLS_LD) != 0
9358 && !h->def_dynamic)
9359 {
9360 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9361 *pgent = gent->next;
9362 }
9363 else
9364 pgent = &gent->next;
9365 }
9366 else
9367 *pgent = gent->next;
9368
9369 if (!htab->do_multi_toc)
9370 merge_got_entries (&h->got.glist);
9371
9372 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9373 if (!gent->is_indirect)
411e1bfb 9374 {
46434633
AM
9375 /* Make sure this symbol is output as a dynamic symbol. */
9376 if (!ensure_undef_dynamic (info, h))
f0158f44 9377 return FALSE;
65f38f15 9378
0c8d6e5c 9379 if (!is_ppc64_elf (gent->owner))
927be08e 9380 abort ();
0ffa91dd 9381
927be08e 9382 allocate_got (h, info, gent);
411e1bfb 9383 }
65f38f15 9384
954b63d4
AM
9385 /* If no dynamic sections we can't have dynamic relocs, except for
9386 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9387 if (!htab->elf.dynamic_sections_created
9388 && h->type != STT_GNU_IFUNC)
9389 eh->dyn_relocs = NULL;
9390
529fe20e
AM
9391 /* Discard relocs on undefined symbols that must be local. */
9392 else if (h->root.type == bfd_link_hash_undefined
9393 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9394 eh->dyn_relocs = NULL;
9395
954b63d4
AM
9396 /* Also discard relocs on undefined weak syms with non-default
9397 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9398 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
9399 eh->dyn_relocs = NULL;
9400
8a2058b5 9401 if (eh->dyn_relocs != NULL)
65f38f15 9402 {
8a2058b5
AM
9403 struct elf_dyn_relocs *p, **pp;
9404
57e7d118
AM
9405 /* In the shared -Bsymbolic case, discard space allocated for
9406 dynamic pc-relative relocs against symbols which turn out to
9407 be defined in regular objects. For the normal shared case,
9408 discard space for relocs that have become local due to symbol
9409 visibility changes. */
9410
9411 if (bfd_link_pic (info))
65f38f15 9412 {
57e7d118
AM
9413 /* Relocs that use pc_count are those that appear on a call
9414 insn, or certain REL relocs (see must_be_dyn_reloc) that
9415 can be generated via assembly. We want calls to
9416 protected symbols to resolve directly to the function
9417 rather than going via the plt. If people want function
9418 pointer comparisons to work as expected then they should
9419 avoid writing weird assembly. */
9420 if (SYMBOL_CALLS_LOCAL (info, h))
9421 {
57e7d118
AM
9422 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9423 {
9424 p->count -= p->pc_count;
9425 p->pc_count = 0;
9426 if (p->count == 0)
9427 *pp = p->next;
9428 else
9429 pp = &p->next;
9430 }
9431 }
65f38f15 9432
954b63d4 9433 if (eh->dyn_relocs != NULL)
5bd4f169 9434 {
46434633
AM
9435 /* Make sure this symbol is output as a dynamic symbol. */
9436 if (!ensure_undef_dynamic (info, h))
f0158f44 9437 return FALSE;
5bd4f169 9438 }
65f38f15 9439 }
529fe20e 9440 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 9441 {
8a2058b5 9442 /* For the non-pic case, discard space for relocs against
57e7d118
AM
9443 symbols which turn out to need copy relocs or are not
9444 dynamic. */
529fe20e
AM
9445 if (h->dynamic_adjusted
9446 && !h->def_regular
9447 && !ELF_COMMON_DEF_P (h))
f0158f44 9448 {
46434633
AM
9449 /* Make sure this symbol is output as a dynamic symbol. */
9450 if (!ensure_undef_dynamic (info, h))
f0158f44 9451 return FALSE;
dfbb6ac9 9452
f0158f44
AM
9453 if (h->dynindx == -1)
9454 eh->dyn_relocs = NULL;
9455 }
9456 else
8a2058b5 9457 eh->dyn_relocs = NULL;
57e7d118
AM
9458 }
9459
9460 /* Finally, allocate space. */
9461 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9462 {
9463 asection *sreloc = elf_section_data (p->sec)->sreloc;
9464 if (eh->elf.type == STT_GNU_IFUNC)
9465 sreloc = htab->elf.irelplt;
9466 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9467 }
65f38f15 9468 }
57e7d118 9469
2d7ad24e
AM
9470 /* We might need a PLT entry when the symbol
9471 a) is dynamic, or
9472 b) is an ifunc, or
9473 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9474 d) has plt16 relocs and we are linking statically. */
9475 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9476 || h->type == STT_GNU_IFUNC
9477 || (h->needs_plt && h->dynamic_adjusted)
9478 || (h->needs_plt
9479 && h->def_regular
9480 && !htab->elf.dynamic_sections_created
3e04d765 9481 && !htab->can_convert_all_inline_plt
2d7ad24e
AM
9482 && (((struct ppc_link_hash_entry *) h)->tls_mask
9483 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 9484 {
57e7d118
AM
9485 struct plt_entry *pent;
9486 bfd_boolean doneone = FALSE;
9487 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9488 if (pent->plt.refcount > 0)
9489 {
9490 if (!htab->elf.dynamic_sections_created
9491 || h->dynindx == -1)
9492 {
2d7ad24e
AM
9493 if (h->type == STT_GNU_IFUNC)
9494 {
9495 s = htab->elf.iplt;
9496 pent->plt.offset = s->size;
9497 s->size += PLT_ENTRY_SIZE (htab);
9498 s = htab->elf.irelplt;
9499 }
9500 else
9501 {
9502 s = htab->pltlocal;
9503 pent->plt.offset = s->size;
9504 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9505 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9506 }
57e7d118
AM
9507 }
9508 else
9509 {
9510 /* If this is the first .plt entry, make room for the special
9511 first entry. */
9512 s = htab->elf.splt;
9513 if (s->size == 0)
9514 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9515
57e7d118 9516 pent->plt.offset = s->size;
65f38f15 9517
57e7d118
AM
9518 /* Make room for this entry. */
9519 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9520
57e7d118
AM
9521 /* Make room for the .glink code. */
9522 s = htab->glink;
9523 if (s->size == 0)
9e390558 9524 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9525 if (htab->opd_abi)
9526 {
9527 /* We need bigger stubs past index 32767. */
9e390558 9528 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9529 s->size += 4;
9530 s->size += 2*4;
9531 }
9532 else
9533 s->size += 4;
65f38f15 9534
57e7d118
AM
9535 /* We also need to make an entry in the .rela.plt section. */
9536 s = htab->elf.srelplt;
9537 }
2d7ad24e
AM
9538 if (s != NULL)
9539 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
9540 doneone = TRUE;
9541 }
9542 else
9543 pent->plt.offset = (bfd_vma) -1;
9544 if (!doneone)
9545 {
9546 h->plt.plist = NULL;
9547 h->needs_plt = 0;
9548 }
65f38f15 9549 }
57e7d118 9550 else
65f38f15 9551 {
57e7d118
AM
9552 h->plt.plist = NULL;
9553 h->needs_plt = 0;
65f38f15
AM
9554 }
9555
b34976b6 9556 return TRUE;
65f38f15
AM
9557}
9558
9e390558
AM
9559#define PPC_LO(v) ((v) & 0xffff)
9560#define PPC_HI(v) (((v) >> 16) & 0xffff)
9561#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
9562#define D34(v) \
9563 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9564#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 9565
a345bc8d
AM
9566/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9567 to set up space for global entry stubs. These are put in glink,
9568 after the branch table. */
65f38f15 9569
b34976b6 9570static bfd_boolean
a345bc8d 9571size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9572{
a345bc8d
AM
9573 struct bfd_link_info *info;
9574 struct ppc_link_hash_table *htab;
9575 struct plt_entry *pent;
9e390558 9576 asection *s, *plt;
65f38f15 9577
a345bc8d
AM
9578 if (h->root.type == bfd_link_hash_indirect)
9579 return TRUE;
65f38f15 9580
a345bc8d
AM
9581 if (!h->pointer_equality_needed)
9582 return TRUE;
65f38f15 9583
a345bc8d
AM
9584 if (h->def_regular)
9585 return TRUE;
65f38f15 9586
a345bc8d
AM
9587 info = inf;
9588 htab = ppc_hash_table (info);
9589 if (htab == NULL)
9590 return FALSE;
9591
9e390558
AM
9592 s = htab->global_entry;
9593 plt = htab->elf.splt;
a345bc8d
AM
9594 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9595 if (pent->plt.offset != (bfd_vma) -1
9596 && pent->addend == 0)
9597 {
afe397ea
AM
9598 /* For ELFv2, if this symbol is not defined in a regular file
9599 and we are not generating a shared library or pie, then we
9600 need to define the symbol in the executable on a call stub.
9601 This is to avoid text relocations. */
9e390558
AM
9602 bfd_vma off, stub_align, stub_off, stub_size;
9603 unsigned int align_power;
9604
9605 stub_size = 16;
9606 stub_off = s->size;
9607 if (htab->params->plt_stub_align >= 0)
9608 align_power = htab->params->plt_stub_align;
9609 else
9610 align_power = -htab->params->plt_stub_align;
9611 /* Setting section alignment is delayed until we know it is
9612 non-empty. Otherwise the .text output section will be
9613 aligned at least to plt_stub_align even when no global
9614 entry stubs are needed. */
9615 if (s->alignment_power < align_power)
9616 s->alignment_power = align_power;
9617 stub_align = (bfd_vma) 1 << align_power;
9618 if (htab->params->plt_stub_align >= 0
9619 || ((((stub_off + stub_size - 1) & -stub_align)
9620 - (stub_off & -stub_align))
9621 > ((stub_size - 1) & -stub_align)))
9622 stub_off = (stub_off + stub_align - 1) & -stub_align;
9623 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9624 off -= stub_off + s->output_offset + s->output_section->vma;
9625 /* Note that for --plt-stub-align negative we have a possible
9626 dependency between stub offset and size. Break that
9627 dependency by assuming the max stub size when calculating
9628 the stub offset. */
9629 if (PPC_HA (off) == 0)
9630 stub_size -= 4;
8a2058b5 9631 h->root.type = bfd_link_hash_defined;
afe397ea 9632 h->root.u.def.section = s;
9e390558
AM
9633 h->root.u.def.value = stub_off;
9634 s->size = stub_off + stub_size;
a345bc8d
AM
9635 break;
9636 }
9637 return TRUE;
9638}
9639
9640/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9641 read-only sections. */
9642
9643static bfd_boolean
98bbb1b8 9644maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 9645{
98bbb1b8
AM
9646 asection *sec;
9647
a345bc8d
AM
9648 if (h->root.type == bfd_link_hash_indirect)
9649 return TRUE;
9650
98bbb1b8
AM
9651 sec = readonly_dynrelocs (h);
9652 if (sec != NULL)
a345bc8d 9653 {
98bbb1b8
AM
9654 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9655
9656 info->flags |= DF_TEXTREL;
2cdcc330
AM
9657 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9658 " in read-only section `%pA'\n"),
9659 sec->owner, h->root.root.string, sec);
a345bc8d
AM
9660
9661 /* Not an error, just cut short the traversal. */
9662 return FALSE;
65f38f15 9663 }
b34976b6 9664 return TRUE;
65f38f15
AM
9665}
9666
9667/* Set the sizes of the dynamic sections. */
9668
b34976b6 9669static bfd_boolean
ee67d69a 9670ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9671 struct bfd_link_info *info)
65f38f15
AM
9672{
9673 struct ppc_link_hash_table *htab;
9674 bfd *dynobj;
9675 asection *s;
b34976b6 9676 bfd_boolean relocs;
65f38f15 9677 bfd *ibfd;
7865406b 9678 struct got_entry *first_tlsld;
65f38f15
AM
9679
9680 htab = ppc_hash_table (info);
4dfe6ac6
NC
9681 if (htab == NULL)
9682 return FALSE;
9683
65f38f15
AM
9684 dynobj = htab->elf.dynobj;
9685 if (dynobj == NULL)
9686 abort ();
9687
9688 if (htab->elf.dynamic_sections_created)
9689 {
9690 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9691 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9692 {
3d4d4302 9693 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9694 if (s == NULL)
9695 abort ();
eea6121a 9696 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9697 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9698 }
9699 }
9700
9701 /* Set up .got offsets for local syms, and space for local dynamic
9702 relocs. */
c72f2fb2 9703 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9704 {
411e1bfb
AM
9705 struct got_entry **lgot_ents;
9706 struct got_entry **end_lgot_ents;
e054468f
AM
9707 struct plt_entry **local_plt;
9708 struct plt_entry **end_local_plt;
f961d9dd 9709 unsigned char *lgot_masks;
65f38f15
AM
9710 bfd_size_type locsymcount;
9711 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9712
0c8d6e5c 9713 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9714 continue;
9715
9716 for (s = ibfd->sections; s != NULL; s = s->next)
9717 {
19e08130 9718 struct ppc_dyn_relocs *p;
65f38f15 9719
6edfbbad 9720 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9721 {
ec338859
AM
9722 if (!bfd_is_abs_section (p->sec)
9723 && bfd_is_abs_section (p->sec->output_section))
9724 {
9725 /* Input section has been discarded, either because
9726 it is a copy of a linkonce section or due to
9727 linker script /DISCARD/, so we'll be discarding
9728 the relocs too. */
9729 }
248866a8 9730 else if (p->count != 0)
ec338859 9731 {
19e08130
AM
9732 asection *srel = elf_section_data (p->sec)->sreloc;
9733 if (p->ifunc)
33e44f2e 9734 srel = htab->elf.irelplt;
eea6121a 9735 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9736 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9737 info->flags |= DF_TEXTREL;
ec338859 9738 }
65f38f15
AM
9739 }
9740 }
9741
411e1bfb
AM
9742 lgot_ents = elf_local_got_ents (ibfd);
9743 if (!lgot_ents)
65f38f15
AM
9744 continue;
9745
0ffa91dd 9746 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9747 locsymcount = symtab_hdr->sh_info;
411e1bfb 9748 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9749 local_plt = (struct plt_entry **) end_lgot_ents;
9750 end_local_plt = local_plt + locsymcount;
f961d9dd 9751 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9752 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9753 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9754 {
0b8bcf0d 9755 struct got_entry **pent, *ent;
411e1bfb 9756
0b8bcf0d
AM
9757 pent = lgot_ents;
9758 while ((ent = *pent) != NULL)
411e1bfb
AM
9759 if (ent->got.refcount > 0)
9760 {
e7b938ca 9761 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9762 {
927be08e 9763 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9764 *pent = ent->next;
411e1bfb
AM
9765 }
9766 else
9767 {
19e08130
AM
9768 unsigned int ent_size = 8;
9769 unsigned int rel_size = sizeof (Elf64_External_Rela);
9770
eea6121a 9771 ent->got.offset = s->size;
e7b938ca 9772 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9773 {
19e08130
AM
9774 ent_size *= 2;
9775 rel_size *= 2;
9776 }
9777 s->size += ent_size;
37da22e5 9778 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 9779 {
33e44f2e 9780 htab->elf.irelplt->size += rel_size;
19e08130
AM
9781 htab->got_reli_size += rel_size;
9782 }
f15d0b54
AM
9783 else if (bfd_link_pic (info)
9784 && !((ent->tls_type & TLS_TPREL) != 0
9785 && bfd_link_executable (info)))
19e08130
AM
9786 {
9787 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9788 srel->size += rel_size;
927be08e 9789 }
0b8bcf0d 9790 pent = &ent->next;
411e1bfb
AM
9791 }
9792 }
9793 else
0b8bcf0d 9794 *pent = ent->next;
65f38f15 9795 }
e054468f 9796
2d7ad24e
AM
9797 /* Allocate space for plt calls to local syms. */
9798 lgot_masks = (unsigned char *) end_local_plt;
9799 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
9800 {
9801 struct plt_entry *ent;
9802
9803 for (ent = *local_plt; ent != NULL; ent = ent->next)
9804 if (ent->plt.refcount > 0)
9805 {
2d7ad24e
AM
9806 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9807 {
9808 s = htab->elf.iplt;
9809 ent->plt.offset = s->size;
9810 s->size += PLT_ENTRY_SIZE (htab);
9811 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9812 }
3e04d765
AM
9813 else if (htab->can_convert_all_inline_plt
9814 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
9815 ent->plt.offset = (bfd_vma) -1;
9816 else
9817 {
9818 s = htab->pltlocal;
9819 ent->plt.offset = s->size;
9820 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9821 if (bfd_link_pic (info))
9822 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9823 }
e054468f
AM
9824 }
9825 else
9826 ent->plt.offset = (bfd_vma) -1;
9827 }
65f38f15
AM
9828 }
9829
9830 /* Allocate global sym .plt and .got entries, and space for global
9831 sym dynamic relocs. */
4ce794b7 9832 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 9833
0e1862bb 9834 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 9835 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 9836
7865406b 9837 first_tlsld = NULL;
c72f2fb2 9838 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 9839 {
7865406b
AM
9840 struct got_entry *ent;
9841
0c8d6e5c 9842 if (!is_ppc64_elf (ibfd))
102890f0
AM
9843 continue;
9844
7865406b
AM
9845 ent = ppc64_tlsld_got (ibfd);
9846 if (ent->got.refcount > 0)
102890f0 9847 {
7865406b 9848 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9849 {
7865406b
AM
9850 ent->is_indirect = TRUE;
9851 ent->got.ent = first_tlsld;
9852 }
9853 else
9854 {
9855 if (first_tlsld == NULL)
9856 first_tlsld = ent;
9857 s = ppc64_elf_tdata (ibfd)->got;
9858 ent->got.offset = s->size;
9859 ent->owner = ibfd;
9860 s->size += 16;
0e1862bb 9861 if (bfd_link_pic (info))
7865406b
AM
9862 {
9863 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9864 srel->size += sizeof (Elf64_External_Rela);
9865 }
102890f0
AM
9866 }
9867 }
9868 else
7865406b 9869 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9870 }
9871
65f38f15
AM
9872 /* We now have determined the sizes of the various dynamic sections.
9873 Allocate memory for them. */
b34976b6 9874 relocs = FALSE;
65f38f15
AM
9875 for (s = dynobj->sections; s != NULL; s = s->next)
9876 {
9877 if ((s->flags & SEC_LINKER_CREATED) == 0)
9878 continue;
9879
4ce794b7 9880 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9881 /* These haven't been allocated yet; don't strip. */
9882 continue;
33e44f2e
AM
9883 else if (s == htab->elf.sgot
9884 || s == htab->elf.splt
9885 || s == htab->elf.iplt
2d7ad24e 9886 || s == htab->pltlocal
c456f082 9887 || s == htab->glink
9e390558 9888 || s == htab->global_entry
5474d94f
AM
9889 || s == htab->elf.sdynbss
9890 || s == htab->elf.sdynrelro)
65f38f15
AM
9891 {
9892 /* Strip this section if we don't need it; see the
9893 comment below. */
5bd4f169 9894 }
58d180e8
AM
9895 else if (s == htab->glink_eh_frame)
9896 {
9897 if (!bfd_is_abs_section (s->output_section))
9898 /* Not sized yet. */
9899 continue;
9900 }
70cc837d 9901 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9902 {
c456f082 9903 if (s->size != 0)
5bd4f169 9904 {
33e44f2e 9905 if (s != htab->elf.srelplt)
b34976b6 9906 relocs = TRUE;
5bd4f169
AM
9907
9908 /* We use the reloc_count field as a counter if we need
9909 to copy relocs into the output file. */
9910 s->reloc_count = 0;
9911 }
9912 }
65f38f15 9913 else
5bd4f169
AM
9914 {
9915 /* It's not one of our sections, so don't allocate space. */
9916 continue;
9917 }
9918
eea6121a 9919 if (s->size == 0)
5bd4f169 9920 {
c456f082
AM
9921 /* If we don't need this section, strip it from the
9922 output file. This is mostly to handle .rela.bss and
9923 .rela.plt. We must create both sections in
9924 create_dynamic_sections, because they must be created
9925 before the linker maps input sections to output
9926 sections. The linker does that before
9927 adjust_dynamic_symbol is called, and it is that
9928 function which decides whether anything needs to go
9929 into these sections. */
8423293d 9930 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9931 continue;
9932 }
9933
06bcf541
AM
9934 if (bfd_is_abs_section (s->output_section))
9935 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9936 s->name);
9937
c456f082 9938 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9939 continue;
9940
65f38f15
AM
9941 /* Allocate memory for the section contents. We use bfd_zalloc
9942 here in case unused entries are not reclaimed before the
9943 section's contents are written out. This should not happen,
411e1bfb
AM
9944 but this way if it does we get a R_PPC64_NONE reloc in .rela
9945 sections instead of garbage.
9946 We also rely on the section contents being zero when writing
5474d94f 9947 the GOT and .dynrelro. */
eea6121a 9948 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9949 if (s->contents == NULL)
b34976b6 9950 return FALSE;
5bd4f169
AM
9951 }
9952
c72f2fb2 9953 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 9954 {
0c8d6e5c 9955 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9956 continue;
9957
e717da7e 9958 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 9959 if (s != NULL && s != htab->elf.sgot)
e717da7e 9960 {
eea6121a 9961 if (s->size == 0)
8423293d 9962 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9963 else
9964 {
eea6121a 9965 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9966 if (s->contents == NULL)
9967 return FALSE;
9968 }
9969 }
9970 s = ppc64_elf_tdata (ibfd)->relgot;
9971 if (s != NULL)
9972 {
eea6121a 9973 if (s->size == 0)
8423293d 9974 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9975 else
9976 {
eea6121a 9977 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9978 if (s->contents == NULL)
9979 return FALSE;
9980 relocs = TRUE;
9981 s->reloc_count = 0;
9982 }
9983 }
9984 }
9985
e86ce104 9986 if (htab->elf.dynamic_sections_created)
5bd4f169 9987 {
e8910a83
AM
9988 bfd_boolean tls_opt;
9989
5bd4f169
AM
9990 /* Add some entries to the .dynamic section. We fill in the
9991 values later, in ppc64_elf_finish_dynamic_sections, but we
9992 must add the entries now so that we get the correct size for
9993 the .dynamic section. The DT_DEBUG entry is filled in by the
9994 dynamic linker and used by the debugger. */
dc810e39 9995#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9996 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9997
0e1862bb 9998 if (bfd_link_executable (info))
5bd4f169 9999 {
dc810e39 10000 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10001 return FALSE;
5bd4f169
AM
10002 }
10003
33e44f2e 10004 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10005 {
dc810e39
AM
10006 if (!add_dynamic_entry (DT_PLTGOT, 0)
10007 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10008 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10009 || !add_dynamic_entry (DT_JMPREL, 0)
10010 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10011 return FALSE;
5bd4f169
AM
10012 }
10013
ee67d69a 10014 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10015 {
10016 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10017 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10018 return FALSE;
19397422
AM
10019 }
10020
7c9cf415 10021 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10022 && htab->tls_get_addr_fd != NULL
10023 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10024 if (tls_opt || !htab->opd_abi)
10025 {
10026 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10027 return FALSE;
10028 }
a7f2871e 10029
5bd4f169
AM
10030 if (relocs)
10031 {
dc810e39
AM
10032 if (!add_dynamic_entry (DT_RELA, 0)
10033 || !add_dynamic_entry (DT_RELASZ, 0)
10034 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10035 return FALSE;
5bd4f169 10036
65f38f15
AM
10037 /* If any dynamic relocs apply to a read-only section,
10038 then we need a DT_TEXTREL entry. */
248866a8 10039 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10040 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10041
65f38f15 10042 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10043 {
65f38f15 10044 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10045 return FALSE;
5bd4f169 10046 }
5bd4f169 10047 }
5bd4f169 10048 }
65f38f15 10049#undef add_dynamic_entry
5bd4f169 10050
b34976b6 10051 return TRUE;
5bd4f169
AM
10052}
10053
a345bc8d
AM
10054/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10055
10056static bfd_boolean
10057ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10058{
10059 if (h->plt.plist != NULL
10060 && !h->def_regular
10061 && !h->pointer_equality_needed)
10062 return FALSE;
10063
10064 return _bfd_elf_hash_symbol (h);
10065}
10066
721956f4 10067/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10068
4ce794b7
AM
10069static inline enum ppc_stub_type
10070ppc_type_of_stub (asection *input_sec,
10071 const Elf_Internal_Rela *rel,
10072 struct ppc_link_hash_entry **hash,
e054468f 10073 struct plt_entry **plt_ent,
6911b7dc
AM
10074 bfd_vma destination,
10075 unsigned long local_off)
5bd4f169 10076{
721956f4
AM
10077 struct ppc_link_hash_entry *h = *hash;
10078 bfd_vma location;
10079 bfd_vma branch_offset;
10080 bfd_vma max_branch_offset;
4ce794b7 10081 enum elf_ppc64_reloc_type r_type;
5bd4f169 10082
721956f4
AM
10083 if (h != NULL)
10084 {
e054468f 10085 struct plt_entry *ent;
7fe2b9a6 10086 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10087 if (h->oh != NULL
10088 && h->oh->is_func_descriptor)
7b8f6675
AM
10089 {
10090 fdh = ppc_follow_link (h->oh);
10091 *hash = fdh;
10092 }
8387904d 10093
e054468f
AM
10094 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10095 if (ent->addend == rel->r_addend
10096 && ent->plt.offset != (bfd_vma) -1)
10097 {
e054468f
AM
10098 *plt_ent = ent;
10099 return ppc_stub_plt_call;
10100 }
5bd4f169 10101
7fe2b9a6
AM
10102 /* Here, we know we don't have a plt entry. If we don't have a
10103 either a defined function descriptor or a defined entry symbol
10104 in a regular object file, then it is pointless trying to make
10105 any other type of stub. */
854b41e7
AM
10106 if (!is_static_defined (&fdh->elf)
10107 && !is_static_defined (&h->elf))
721956f4 10108 return ppc_stub_none;
5d1634d7 10109 }
e054468f
AM
10110 else if (elf_local_got_ents (input_sec->owner) != NULL)
10111 {
10112 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10113 struct plt_entry **local_plt = (struct plt_entry **)
10114 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10115 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10116
10117 if (local_plt[r_symndx] != NULL)
10118 {
10119 struct plt_entry *ent;
10120
10121 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10122 if (ent->addend == rel->r_addend
10123 && ent->plt.offset != (bfd_vma) -1)
10124 {
10125 *plt_ent = ent;
10126 return ppc_stub_plt_call;
10127 }
10128 }
10129 }
5d1634d7 10130
721956f4
AM
10131 /* Determine where the call point is. */
10132 location = (input_sec->output_offset
10133 + input_sec->output_section->vma
10134 + rel->r_offset);
5d1634d7 10135
721956f4
AM
10136 branch_offset = destination - location;
10137 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10138
721956f4
AM
10139 /* Determine if a long branch stub is needed. */
10140 max_branch_offset = 1 << 25;
23cedd1d
AM
10141 if (r_type == R_PPC64_REL14
10142 || r_type == R_PPC64_REL14_BRTAKEN
10143 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10144 max_branch_offset = 1 << 15;
5d1634d7 10145
6911b7dc 10146 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10147 /* We need a stub. Figure out whether a long_branch or plt_branch
10148 is needed later. */
10149 return ppc_stub_long_branch;
5d1634d7 10150
721956f4 10151 return ppc_stub_none;
5d1634d7
AM
10152}
10153
f891966f
AM
10154/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10155 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10156 . mflr %r12
10157 . bcl 20,31,1f
10158 .1: mflr %r11
10159 . mtlr %r12
05d0e962 10160 . lis %r12,xxx-1b@highest
f891966f 10161 . ori %r12,%r12,xxx-1b@higher
05d0e962 10162 . sldi %r12,%r12,32
f891966f 10163 . oris %r12,%r12,xxx-1b@high
05d0e962 10164 . ori %r12,%r12,xxx-1b@l
f891966f 10165 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10166
10167static bfd_byte *
10168build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10169{
f891966f
AM
10170 bfd_put_32 (abfd, MFLR_R12, p);
10171 p += 4;
10172 bfd_put_32 (abfd, BCL_20_31, p);
10173 p += 4;
10174 bfd_put_32 (abfd, MFLR_R11, p);
10175 p += 4;
10176 bfd_put_32 (abfd, MTLR_R12, p);
10177 p += 4;
05d0e962
AM
10178 if (off + 0x8000 < 0x10000)
10179 {
10180 if (load)
10181 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10182 else
10183 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10184 p += 4;
10185 }
10186 else if (off + 0x80008000ULL < 0x100000000ULL)
10187 {
10188 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10189 p += 4;
10190 if (load)
10191 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10192 else
10193 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10194 p += 4;
10195 }
10196 else
10197 {
10198 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10199 {
10200 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10201 p += 4;
10202 }
10203 else
10204 {
10205 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10206 p += 4;
10207 if (((off >> 32) & 0xffff) != 0)
10208 {
10209 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10210 p += 4;
10211 }
10212 }
10213 if (((off >> 32) & 0xffffffffULL) != 0)
10214 {
10215 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10216 p += 4;
10217 }
10218 if (PPC_HI (off) != 0)
10219 {
10220 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10221 p += 4;
10222 }
10223 if (PPC_LO (off) != 0)
10224 {
10225 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10226 p += 4;
10227 }
10228 if (load)
10229 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10230 else
10231 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10232 p += 4;
10233 }
10234 return p;
10235}
10236
10237static unsigned int
10238size_offset (bfd_vma off)
10239{
10240 unsigned int size;
10241 if (off + 0x8000 < 0x10000)
10242 size = 4;
10243 else if (off + 0x80008000ULL < 0x100000000ULL)
10244 size = 8;
10245 else
10246 {
10247 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10248 size = 4;
10249 else
10250 {
10251 size = 4;
10252 if (((off >> 32) & 0xffff) != 0)
10253 size += 4;
10254 }
10255 if (((off >> 32) & 0xffffffffULL) != 0)
10256 size += 4;
10257 if (PPC_HI (off) != 0)
10258 size += 4;
10259 if (PPC_LO (off) != 0)
10260 size += 4;
10261 size += 4;
10262 }
f891966f 10263 return size + 16;
05d0e962
AM
10264}
10265
3d58e1fc
AM
10266static unsigned int
10267num_relocs_for_offset (bfd_vma off)
10268{
10269 unsigned int num_rel;
10270 if (off + 0x8000 < 0x10000)
10271 num_rel = 1;
10272 else if (off + 0x80008000ULL < 0x100000000ULL)
10273 num_rel = 2;
10274 else
10275 {
10276 num_rel = 1;
10277 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10278 && ((off >> 32) & 0xffff) != 0)
10279 num_rel += 1;
10280 if (PPC_HI (off) != 0)
10281 num_rel += 1;
10282 if (PPC_LO (off) != 0)
10283 num_rel += 1;
10284 }
10285 return num_rel;
10286}
10287
10288static Elf_Internal_Rela *
10289emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10290 bfd_vma roff, bfd_vma targ, bfd_vma off)
10291{
10292 bfd_vma relative_targ = targ - (roff - 8);
10293 if (bfd_big_endian (info->output_bfd))
10294 roff += 2;
10295 r->r_offset = roff;
10296 r->r_addend = relative_targ + roff;
10297 if (off + 0x8000 < 0x10000)
10298 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10299 else if (off + 0x80008000ULL < 0x100000000ULL)
10300 {
10301 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
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 else
10309 {
10310 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10311 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10312 else
10313 {
10314 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10315 if (((off >> 32) & 0xffff) != 0)
10316 {
10317 ++r;
10318 roff += 4;
10319 r->r_offset = roff;
10320 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10321 r->r_addend = relative_targ + roff;
10322 }
10323 }
10324 if (((off >> 32) & 0xffffffffULL) != 0)
10325 roff += 4;
10326 if (PPC_HI (off) != 0)
10327 {
10328 ++r;
10329 roff += 4;
10330 r->r_offset = roff;
10331 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10332 r->r_addend = relative_targ + roff;
10333 }
10334 if (PPC_LO (off) != 0)
10335 {
10336 ++r;
10337 roff += 4;
10338 r->r_offset = roff;
10339 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10340 r->r_addend = relative_targ + roff;
10341 }
10342 }
10343 return r;
10344}
10345
04bdff6a
AM
10346static bfd_byte *
10347build_powerxx_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10348 bfd_boolean load)
10349{
10350 uint64_t insn;
10351 if (off - odd + (1ULL << 33) < 1ULL << 34)
10352 {
10353 off -= odd;
10354 if (odd)
10355 {
10356 bfd_put_32 (abfd, NOP, p);
10357 p += 4;
10358 }
10359 if (load)
10360 insn = PLD_R12_PC;
10361 else
10362 insn = PADDI_R12_PC;
10363 insn |= D34 (off);
10364 bfd_put_32 (abfd, insn >> 32, p);
10365 p += 4;
10366 bfd_put_32 (abfd, insn, p);
10367 }
10368 /* The minimum value for paddi is -0x200000000. The minimum value
10369 for li is -0x8000, which when shifted by 34 and added gives a
10370 minimum value of -0x2000200000000. The maximum value is
10371 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10372 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10373 {
10374 off -= 8 - odd;
10375 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10376 p += 4;
10377 if (!odd)
10378 {
10379 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10380 p += 4;
10381 }
10382 insn = PADDI_R12_PC | D34 (off);
10383 bfd_put_32 (abfd, insn >> 32, p);
10384 p += 4;
10385 bfd_put_32 (abfd, insn, p);
10386 p += 4;
10387 if (odd)
10388 {
10389 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10390 p += 4;
10391 }
10392 if (load)
10393 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10394 else
10395 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10396 }
10397 else
10398 {
10399 off -= odd + 8;
10400 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10401 p += 4;
10402 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10403 p += 4;
10404 if (odd)
10405 {
10406 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10407 p += 4;
10408 }
10409 insn = PADDI_R12_PC | D34 (off);
10410 bfd_put_32 (abfd, insn >> 32, p);
10411 p += 4;
10412 bfd_put_32 (abfd, insn, p);
10413 p += 4;
10414 if (!odd)
10415 {
10416 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10417 p += 4;
10418 }
10419 if (load)
10420 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10421 else
10422 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10423 }
10424 p += 4;
10425 return p;
10426}
10427
10428static unsigned int
10429size_powerxx_offset (bfd_vma off, int odd)
10430{
10431 if (off - odd + (1ULL << 33) < 1ULL << 34)
10432 return odd + 8;
10433 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10434 return 20;
10435 else
10436 return 24;
10437}
10438
10439static unsigned int
10440num_relocs_for_powerxx_offset (bfd_vma off, int odd)
10441{
10442 if (off - odd + (1ULL << 33) < 1ULL << 34)
10443 return 1;
10444 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10445 return 2;
10446 else
10447 return 3;
10448}
10449
10450static Elf_Internal_Rela *
10451emit_relocs_for_powerxx_offset (struct bfd_link_info *info,
10452 Elf_Internal_Rela *r, bfd_vma roff,
10453 bfd_vma targ, bfd_vma off, int odd)
10454{
10455 if (off - odd + (1ULL << 33) < 1ULL << 34)
10456 roff += odd;
10457 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10458 {
10459 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10460 r->r_offset = roff + d_offset;
10461 r->r_addend = targ + 8 - odd - d_offset;
10462 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10463 ++r;
10464 roff += 8 - odd;
10465 }
10466 else
10467 {
10468 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10469 r->r_offset = roff + d_offset;
10470 r->r_addend = targ + 8 + odd - d_offset;
10471 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10472 ++r;
10473 roff += 4;
10474 r->r_offset = roff + d_offset;
10475 r->r_addend = targ + 4 + odd - d_offset;
10476 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10477 ++r;
10478 roff += 4 + odd;
10479 }
10480 r->r_offset = roff;
10481 r->r_addend = targ;
10482 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10483 return r;
10484}
10485
df136d64
AM
10486/* Emit .eh_frame opcode to advance pc by DELTA. */
10487
10488static bfd_byte *
10489eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10490{
10491 delta /= 4;
10492 if (delta < 64)
10493 *eh++ = DW_CFA_advance_loc + delta;
10494 else if (delta < 256)
10495 {
10496 *eh++ = DW_CFA_advance_loc1;
10497 *eh++ = delta;
10498 }
10499 else if (delta < 65536)
10500 {
10501 *eh++ = DW_CFA_advance_loc2;
10502 bfd_put_16 (abfd, delta, eh);
10503 eh += 2;
10504 }
10505 else
10506 {
10507 *eh++ = DW_CFA_advance_loc4;
10508 bfd_put_32 (abfd, delta, eh);
10509 eh += 4;
10510 }
10511 return eh;
10512}
10513
10514/* Size of required .eh_frame opcode to advance pc by DELTA. */
10515
10516static unsigned int
10517eh_advance_size (unsigned int delta)
10518{
10519 if (delta < 64 * 4)
10520 /* DW_CFA_advance_loc+[1..63]. */
10521 return 1;
10522 if (delta < 256 * 4)
10523 /* DW_CFA_advance_loc1, byte. */
10524 return 2;
10525 if (delta < 65536 * 4)
10526 /* DW_CFA_advance_loc2, 2 bytes. */
10527 return 3;
10528 /* DW_CFA_advance_loc4, 4 bytes. */
10529 return 5;
10530}
10531
794e51c0
AM
10532/* With power7 weakly ordered memory model, it is possible for ld.so
10533 to update a plt entry in one thread and have another thread see a
10534 stale zero toc entry. To avoid this we need some sort of acquire
10535 barrier in the call stub. One solution is to make the load of the
10536 toc word seem to appear to depend on the load of the function entry
10537 word. Another solution is to test for r2 being zero, and branch to
10538 the appropriate glink entry if so.
10539
10540 . fake dep barrier compare
71a39c98
AM
10541 . ld 12,xxx(2) ld 12,xxx(2)
10542 . mtctr 12 mtctr 12
10543 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10544 . add 2,2,11 cmpldi 2,0
10545 . ld 2,xxx+8(2) bnectr+
10546 . bctr b <glink_entry>
10547
10548 The solution involving the compare turns out to be faster, so
10549 that's what we use unless the branch won't reach. */
10550
10551#define ALWAYS_USE_FAKE_DEP 0
10552#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10553
794e51c0
AM
10554static inline unsigned int
10555plt_stub_size (struct ppc_link_hash_table *htab,
10556 struct ppc_stub_hash_entry *stub_entry,
10557 bfd_vma off)
10558{
05d0e962 10559 unsigned size;
b9e5796b 10560
05d0e962
AM
10561 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10562 {
04bdff6a
AM
10563 if (htab->powerxx_stubs)
10564 {
10565 bfd_vma start = (stub_entry->stub_offset
10566 + stub_entry->group->stub_sec->output_offset
10567 + stub_entry->group->stub_sec->output_section->vma);
10568 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10569 start += 4;
10570 size = 8 + size_powerxx_offset (off, start & 4);
10571 }
10572 else
10573 size = 8 + size_offset (off - 8);
05d0e962
AM
10574 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10575 size += 4;
10576 return size;
10577 }
10578
10579 size = 12;
b9e5796b
AM
10580 if (ALWAYS_EMIT_R2SAVE
10581 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10582 size += 4;
10583 if (PPC_HA (off) != 0)
794e51c0 10584 size += 4;
b9e5796b
AM
10585 if (htab->opd_abi)
10586 {
10587 size += 4;
e7d1c40c 10588 if (htab->params->plt_static_chain)
b9e5796b 10589 size += 4;
bd4d2eaa
AM
10590 if (htab->params->plt_thread_safe
10591 && htab->elf.dynamic_sections_created
10592 && stub_entry->h != NULL
10593 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10594 size += 8;
e7d1c40c 10595 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10596 size += 4;
10597 }
794e51c0
AM
10598 if (stub_entry->h != NULL
10599 && (stub_entry->h == htab->tls_get_addr_fd
10600 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10601 && htab->params->tls_get_addr_opt)
f378ab09
AM
10602 {
10603 size += 7 * 4;
e81b4c93 10604 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 10605 size += 6 * 4;
f378ab09 10606 }
794e51c0
AM
10607 return size;
10608}
10609
2420fff6
AM
10610/* Depending on the sign of plt_stub_align:
10611 If positive, return the padding to align to a 2**plt_stub_align
10612 boundary.
10613 If negative, if this stub would cross fewer 2**plt_stub_align
10614 boundaries if we align, then return the padding needed to do so. */
10615
794e51c0
AM
10616static inline unsigned int
10617plt_stub_pad (struct ppc_link_hash_table *htab,
10618 struct ppc_stub_hash_entry *stub_entry,
10619 bfd_vma plt_off)
10620{
2420fff6 10621 int stub_align;
1aa42141 10622 unsigned stub_size;
6f20ed8a 10623 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10624
2420fff6
AM
10625 if (htab->params->plt_stub_align >= 0)
10626 {
10627 stub_align = 1 << htab->params->plt_stub_align;
10628 if ((stub_off & (stub_align - 1)) != 0)
10629 return stub_align - (stub_off & (stub_align - 1));
10630 return 0;
10631 }
10632
10633 stub_align = 1 << -htab->params->plt_stub_align;
1aa42141 10634 stub_size = plt_stub_size (htab, stub_entry, plt_off);
794e51c0 10635 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10636 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10637 return stub_align - (stub_off & (stub_align - 1));
10638 return 0;
10639}
10640
10641/* Build a .plt call stub. */
10642
10643static inline bfd_byte *
10644build_plt_stub (struct ppc_link_hash_table *htab,
10645 struct ppc_stub_hash_entry *stub_entry,
10646 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10647{
e7d1c40c 10648 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10649 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10650 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10651 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10652 && htab->elf.dynamic_sections_created
10653 && stub_entry->h != NULL
10654 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10655 bfd_boolean use_fake_dep = plt_thread_safe;
10656 bfd_vma cmp_branch_off = 0;
10657
10658 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10659 && plt_load_toc
794e51c0 10660 && plt_thread_safe
bd4d2eaa
AM
10661 && !((stub_entry->h == htab->tls_get_addr_fd
10662 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10663 && htab->params->tls_get_addr_opt))
794e51c0
AM
10664 {
10665 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10666 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10667 / PLT_ENTRY_SIZE (htab));
9e390558 10668 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10669 bfd_vma to, from;
10670
68d62958
AM
10671 if (pltindex > 32768)
10672 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10673 to = (glinkoff
10674 + htab->glink->output_offset
10675 + htab->glink->output_section->vma);
6f20ed8a 10676 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10677 + 4 * (ALWAYS_EMIT_R2SAVE
10678 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10679 + 4 * (PPC_HA (offset) != 0)
10680 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10681 != PPC_HA (offset))
10682 + 4 * (plt_static_chain != 0)
10683 + 20
6f20ed8a
AM
10684 + stub_entry->group->stub_sec->output_offset
10685 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10686 cmp_branch_off = to - from;
10687 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10688 }
10689
ac2df442
AM
10690 if (PPC_HA (offset) != 0)
10691 {
176a0d42
AM
10692 if (r != NULL)
10693 {
794e51c0
AM
10694 if (ALWAYS_EMIT_R2SAVE
10695 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10696 r[0].r_offset += 4;
176a0d42 10697 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10698 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10699 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10700 r[1].r_addend = r[0].r_addend;
b9e5796b 10701 if (plt_load_toc)
176a0d42 10702 {
b9e5796b 10703 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10704 {
b9e5796b
AM
10705 r[2].r_offset = r[1].r_offset + 4;
10706 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10707 r[2].r_addend = r[0].r_addend;
10708 }
10709 else
10710 {
10711 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10712 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10713 r[2].r_addend = r[0].r_addend + 8;
10714 if (plt_static_chain)
10715 {
10716 r[3].r_offset = r[2].r_offset + 4;
10717 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10718 r[3].r_addend = r[0].r_addend + 16;
10719 }
c7131b65 10720 }
176a0d42
AM
10721 }
10722 }
794e51c0
AM
10723 if (ALWAYS_EMIT_R2SAVE
10724 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10725 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10726 if (plt_load_toc)
10727 {
10728 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10729 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10730 }
10731 else
10732 {
10733 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10734 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10735 }
b9e5796b
AM
10736 if (plt_load_toc
10737 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10738 {
71a39c98 10739 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10740 offset = 0;
10741 }
71a39c98 10742 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10743 if (plt_load_toc)
794e51c0 10744 {
b9e5796b
AM
10745 if (use_fake_dep)
10746 {
10747 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10748 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10749 }
10750 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10751 if (plt_static_chain)
10752 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10753 }
ac2df442
AM
10754 }
10755 else
10756 {
176a0d42
AM
10757 if (r != NULL)
10758 {
794e51c0
AM
10759 if (ALWAYS_EMIT_R2SAVE
10760 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10761 r[0].r_offset += 4;
176a0d42 10762 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10763 if (plt_load_toc)
176a0d42 10764 {
b9e5796b 10765 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10766 {
b9e5796b
AM
10767 r[1].r_offset = r[0].r_offset + 4;
10768 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10769 r[1].r_addend = r[0].r_addend;
10770 }
10771 else
10772 {
10773 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10774 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10775 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10776 if (plt_static_chain)
10777 {
10778 r[2].r_offset = r[1].r_offset + 4;
10779 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10780 r[2].r_addend = r[0].r_addend + 8;
10781 }
c7131b65 10782 }
176a0d42
AM
10783 }
10784 }
794e51c0
AM
10785 if (ALWAYS_EMIT_R2SAVE
10786 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10787 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10788 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10789 if (plt_load_toc
10790 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10791 {
10792 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10793 offset = 0;
10794 }
71a39c98 10795 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10796 if (plt_load_toc)
794e51c0 10797 {
b9e5796b
AM
10798 if (use_fake_dep)
10799 {
10800 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10801 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10802 }
10803 if (plt_static_chain)
10804 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10805 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10806 }
ac2df442 10807 }
b9e5796b 10808 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10809 {
10810 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10811 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10812 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10813 }
10814 else
407aa07c 10815 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10816 return p;
10817}
10818
a7f2871e
AM
10819/* Build a special .plt call stub for __tls_get_addr. */
10820
10821#define LD_R11_0R3 0xe9630000
10822#define LD_R12_0R3 0xe9830000
10823#define MR_R0_R3 0x7c601b78
10824#define CMPDI_R11_0 0x2c2b0000
10825#define ADD_R3_R12_R13 0x7c6c6a14
10826#define BEQLR 0x4d820020
10827#define MR_R3_R0 0x7c030378
a7f2871e
AM
10828#define STD_R11_0R1 0xf9610000
10829#define BCTRL 0x4e800421
10830#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10831#define MTLR_R11 0x7d6803a6
10832
10833static inline bfd_byte *
794e51c0
AM
10834build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10835 struct ppc_stub_hash_entry *stub_entry,
10836 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10837{
e7d1c40c 10838 bfd *obfd = htab->params->stub_bfd;
df136d64 10839 bfd_byte *loc = p;
794e51c0 10840
a7f2871e
AM
10841 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10842 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10843 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10844 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10845 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10846 bfd_put_32 (obfd, BEQLR, p), p += 4;
10847 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
10848 if (r != NULL)
10849 r[0].r_offset += 7 * 4;
e81b4c93 10850 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
f378ab09
AM
10851 return build_plt_stub (htab, stub_entry, p, offset, r);
10852
a7f2871e 10853 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10854 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10855
10856 if (r != NULL)
f378ab09 10857 r[0].r_offset += 2 * 4;
794e51c0 10858 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 10859 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 10860
a078d95a 10861 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10862 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10863 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10864 bfd_put_32 (obfd, BLR, p), p += 4;
10865
df136d64
AM
10866 if (htab->glink_eh_frame != NULL
10867 && htab->glink_eh_frame->size != 0)
10868 {
10869 bfd_byte *base, *eh;
10870 unsigned int lr_used, delta;
10871
10872 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10873 eh = base + stub_entry->group->eh_size;
10874 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10875 delta = lr_used - stub_entry->group->lr_restore;
10876 stub_entry->group->lr_restore = lr_used + 16;
10877 eh = eh_advance (htab->elf.dynobj, eh, delta);
10878 *eh++ = DW_CFA_offset_extended_sf;
10879 *eh++ = 65;
10880 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10881 *eh++ = DW_CFA_advance_loc + 4;
10882 *eh++ = DW_CFA_restore_extended;
10883 *eh++ = 65;
10884 stub_entry->group->eh_size = eh - base;
10885 }
a7f2871e
AM
10886 return p;
10887}
10888
176a0d42
AM
10889static Elf_Internal_Rela *
10890get_relocs (asection *sec, int count)
10891{
10892 Elf_Internal_Rela *relocs;
10893 struct bfd_elf_section_data *elfsec_data;
10894
10895 elfsec_data = elf_section_data (sec);
10896 relocs = elfsec_data->relocs;
10897 if (relocs == NULL)
10898 {
10899 bfd_size_type relsize;
10900 relsize = sec->reloc_count * sizeof (*relocs);
10901 relocs = bfd_alloc (sec->owner, relsize);
10902 if (relocs == NULL)
10903 return NULL;
10904 elfsec_data->relocs = relocs;
d4730f92
BS
10905 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10906 sizeof (Elf_Internal_Shdr));
10907 if (elfsec_data->rela.hdr == NULL)
10908 return NULL;
10909 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10910 * sizeof (Elf64_External_Rela));
10911 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10912 sec->reloc_count = 0;
10913 }
10914 relocs += sec->reloc_count;
10915 sec->reloc_count += count;
10916 return relocs;
10917}
10918
3d58e1fc
AM
10919/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10920 forms, to the equivalent relocs against the global symbol given by
10921 STUB_ENTRY->H. */
10922
10923static bfd_boolean
10924use_global_in_relocs (struct ppc_link_hash_table *htab,
10925 struct ppc_stub_hash_entry *stub_entry,
10926 Elf_Internal_Rela *r, unsigned int num_rel)
10927{
10928 struct elf_link_hash_entry **hashes;
10929 unsigned long symndx;
10930 struct ppc_link_hash_entry *h;
10931 bfd_vma symval;
10932
10933 /* Relocs are always against symbols in their own object file. Fake
10934 up global sym hashes for the stub bfd (which has no symbols). */
10935 hashes = elf_sym_hashes (htab->params->stub_bfd);
10936 if (hashes == NULL)
10937 {
10938 bfd_size_type hsize;
10939
10940 /* When called the first time, stub_globals will contain the
10941 total number of symbols seen during stub sizing. After
10942 allocating, stub_globals is used as an index to fill the
10943 hashes array. */
10944 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10945 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10946 if (hashes == NULL)
10947 return FALSE;
10948 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10949 htab->stub_globals = 1;
10950 }
10951 symndx = htab->stub_globals++;
10952 h = stub_entry->h;
10953 hashes[symndx] = &h->elf;
10954 if (h->oh != NULL && h->oh->is_func)
10955 h = ppc_follow_link (h->oh);
10956 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10957 || h->elf.root.type == bfd_link_hash_defweak);
10958 symval = (h->elf.root.u.def.value
10959 + h->elf.root.u.def.section->output_offset
10960 + h->elf.root.u.def.section->output_section->vma);
10961 while (num_rel-- != 0)
10962 {
10963 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10964 if (h->elf.root.u.def.section != stub_entry->target_section)
10965 {
10966 /* H is an opd symbol. The addend must be zero, and the
10967 branch reloc is the only one we can convert. */
10968 r->r_addend = 0;
10969 break;
10970 }
10971 else
10972 r->r_addend -= symval;
10973 --r;
10974 }
10975 return TRUE;
10976}
10977
aa374f67 10978static bfd_vma
25f53a85 10979get_r2off (struct bfd_link_info *info,
aa374f67
AM
10980 struct ppc_stub_hash_entry *stub_entry)
10981{
25f53a85 10982 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10983 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10984
10985 if (r2off == 0)
10986 {
10987 /* Support linking -R objects. Get the toc pointer from the
10988 opd entry. */
10989 char buf[8];
b9e5796b
AM
10990 if (!htab->opd_abi)
10991 return r2off;
aa374f67
AM
10992 asection *opd = stub_entry->h->elf.root.u.def.section;
10993 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10994
10995 if (strcmp (opd->name, ".opd") != 0
10996 || opd->reloc_count != 0)
10997 {
2cdcc330
AM
10998 info->callbacks->einfo
10999 (_("%P: cannot find opd entry toc for `%pT'\n"),
11000 stub_entry->h->elf.root.root.string);
aa374f67 11001 bfd_set_error (bfd_error_bad_value);
a7c49797 11002 return (bfd_vma) -1;
aa374f67
AM
11003 }
11004 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11005 return (bfd_vma) -1;
aa374f67 11006 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11007 r2off -= elf_gp (info->output_bfd);
aa374f67 11008 }
6f20ed8a 11009 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11010 return r2off;
11011}
11012
b34976b6 11013static bfd_boolean
4ce794b7 11014ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11015{
721956f4
AM
11016 struct ppc_stub_hash_entry *stub_entry;
11017 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11018 struct bfd_link_info *info;
11019 struct ppc_link_hash_table *htab;
721956f4 11020 bfd_byte *loc;
3d58e1fc 11021 bfd_byte *p, *relp;
1aa42141 11022 bfd_vma targ, off;
176a0d42 11023 Elf_Internal_Rela *r;
e054468f 11024 asection *plt;
3d58e1fc 11025 int num_rel;
04bdff6a 11026 int odd;
5d1634d7 11027
721956f4
AM
11028 /* Massage our args to the form they really have. */
11029 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11030 info = in_arg;
5d1634d7 11031
5d1634d7 11032 htab = ppc_hash_table (info);
4dfe6ac6
NC
11033 if (htab == NULL)
11034 return FALSE;
5d1634d7 11035
1aa42141 11036 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 11037 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11038
4ce794b7 11039 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 11040 switch (stub_entry->stub_type)
5d1634d7 11041 {
721956f4 11042 case ppc_stub_long_branch:
ad8e1ba5 11043 case ppc_stub_long_branch_r2off:
721956f4 11044 /* Branches are relative. This is where we are going to. */
1aa42141 11045 targ = (stub_entry->target_value
6911b7dc
AM
11046 + stub_entry->target_section->output_offset
11047 + stub_entry->target_section->output_section->vma);
1aa42141 11048 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11049
721956f4 11050 /* And this is where we are coming from. */
1aa42141
AM
11051 off = (stub_entry->stub_offset
11052 + stub_entry->group->stub_sec->output_offset
11053 + stub_entry->group->stub_sec->output_section->vma);
11054 off = targ - off;
e86ce104 11055
9e390558 11056 p = loc;
ac2df442 11057 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 11058 {
25f53a85 11059 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11060
a7c49797 11061 if (r2off == (bfd_vma) -1)
aa374f67
AM
11062 {
11063 htab->stub_error = TRUE;
11064 return FALSE;
11065 }
9e390558
AM
11066 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11067 p += 4;
ac2df442
AM
11068 if (PPC_HA (r2off) != 0)
11069 {
e7d1c40c 11070 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11071 ADDIS_R2_R2 | PPC_HA (r2off), p);
11072 p += 4;
a7c49797
AM
11073 }
11074 if (PPC_LO (r2off) != 0)
11075 {
11076 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11077 ADDI_R2_R2 | PPC_LO (r2off), p);
11078 p += 4;
ac2df442 11079 }
9e390558 11080 off -= p - loc;
ad8e1ba5 11081 }
9e390558
AM
11082 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11083 p += 4;
ad8e1ba5 11084
5c3dead3
AM
11085 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11086 {
cf97bcb0
AM
11087 _bfd_error_handler
11088 (_("long branch stub `%s' offset overflow"),
bc30df16 11089 stub_entry->root.string);
5c3dead3
AM
11090 htab->stub_error = TRUE;
11091 return FALSE;
11092 }
ee75fd95
AM
11093
11094 if (info->emitrelocations)
11095 {
6f20ed8a 11096 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
11097 if (r == NULL)
11098 return FALSE;
9e390558 11099 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11100 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11101 r->r_addend = targ;
3d58e1fc
AM
11102 if (stub_entry->h != NULL
11103 && !use_global_in_relocs (htab, stub_entry, r, 1))
11104 return FALSE;
ee75fd95 11105 }
721956f4 11106 break;
e86ce104 11107
721956f4 11108 case ppc_stub_plt_branch:
ad8e1ba5 11109 case ppc_stub_plt_branch_r2off:
721956f4
AM
11110 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11111 stub_entry->root.string + 9,
b34976b6 11112 FALSE, FALSE);
721956f4
AM
11113 if (br_entry == NULL)
11114 {
cf97bcb0
AM
11115 _bfd_error_handler (_("can't find branch stub `%s'"),
11116 stub_entry->root.string);
b34976b6
AM
11117 htab->stub_error = TRUE;
11118 return FALSE;
721956f4
AM
11119 }
11120
1aa42141 11121 targ = (stub_entry->target_value
176a0d42
AM
11122 + stub_entry->target_section->output_offset
11123 + stub_entry->target_section->output_section->vma);
6911b7dc 11124 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 11125 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11126
1aa42141 11127 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11128 htab->brlt->contents + br_entry->offset);
721956f4 11129
f94498ff 11130 if (br_entry->iter == htab->stub_iteration)
721956f4 11131 {
f94498ff 11132 br_entry->iter = 0;
84f5d08e 11133
f94498ff 11134 if (htab->relbrlt != NULL)
84f5d08e 11135 {
f94498ff
AM
11136 /* Create a reloc for the branch lookup table entry. */
11137 Elf_Internal_Rela rela;
11138 bfd_byte *rl;
11139
11140 rela.r_offset = (br_entry->offset
11141 + htab->brlt->output_offset
11142 + htab->brlt->output_section->vma);
11143 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11144 rela.r_addend = targ;
f94498ff
AM
11145
11146 rl = htab->relbrlt->contents;
11147 rl += (htab->relbrlt->reloc_count++
11148 * sizeof (Elf64_External_Rela));
11149 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11150 }
11151 else if (info->emitrelocations)
11152 {
176a0d42
AM
11153 r = get_relocs (htab->brlt, 1);
11154 if (r == NULL)
11155 return FALSE;
11156 /* brlt, being SEC_LINKER_CREATED does not go through the
11157 normal reloc processing. Symbols and offsets are not
11158 translated from input file to output file form, so
11159 set up the offset per the output file. */
f94498ff
AM
11160 r->r_offset = (br_entry->offset
11161 + htab->brlt->output_offset
11162 + htab->brlt->output_section->vma);
11163 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11164 r->r_addend = targ;
84f5d08e 11165 }
84f5d08e 11166 }
721956f4 11167
1aa42141 11168 targ = (br_entry->offset
176a0d42
AM
11169 + htab->brlt->output_offset
11170 + htab->brlt->output_section->vma);
11171
1aa42141
AM
11172 off = (elf_gp (info->output_bfd)
11173 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11174 off = targ - off;
721956f4 11175
ad8e1ba5 11176 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11177 {
25f53a85 11178 info->callbacks->einfo
c1c8c1ef 11179 (_("%P: linkage table error against `%pT'\n"),
721956f4 11180 stub_entry->root.string);
5d1634d7 11181 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11182 htab->stub_error = TRUE;
11183 return FALSE;
5d1634d7 11184 }
41bd81ab 11185
176a0d42
AM
11186 if (info->emitrelocations)
11187 {
6f20ed8a 11188 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11189 if (r == NULL)
11190 return FALSE;
6f20ed8a 11191 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11192 if (bfd_big_endian (info->output_bfd))
11193 r[0].r_offset += 2;
00f412ee 11194 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11195 r[0].r_offset += 4;
11196 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11197 r[0].r_addend = targ;
176a0d42
AM
11198 if (PPC_HA (off) != 0)
11199 {
11200 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11201 r[1].r_offset = r[0].r_offset + 4;
11202 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11203 r[1].r_addend = r[0].r_addend;
11204 }
11205 }
11206
9e390558 11207 p = loc;
00f412ee 11208 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11209 {
176a0d42 11210 if (PPC_HA (off) != 0)
ac2df442 11211 {
e7d1c40c 11212 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11213 ADDIS_R12_R2 | PPC_HA (off), p);
11214 p += 4;
e7d1c40c 11215 bfd_put_32 (htab->params->stub_bfd,
9e390558 11216 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11217 }
11218 else
9e390558
AM
11219 bfd_put_32 (htab->params->stub_bfd,
11220 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11221 }
11222 else
11223 {
25f53a85 11224 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11225
a7c49797 11226 if (r2off == (bfd_vma) -1)
aa374f67
AM
11227 {
11228 htab->stub_error = TRUE;
11229 return FALSE;
11230 }
ad8e1ba5 11231
9e390558
AM
11232 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11233 p += 4;
176a0d42 11234 if (PPC_HA (off) != 0)
ac2df442 11235 {
e7d1c40c 11236 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11237 ADDIS_R12_R2 | PPC_HA (off), p);
11238 p += 4;
e7d1c40c 11239 bfd_put_32 (htab->params->stub_bfd,
9e390558 11240 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11241 }
11242 else
9e390558 11243 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11244
11245 if (PPC_HA (r2off) != 0)
11246 {
9e390558 11247 p += 4;
e7d1c40c 11248 bfd_put_32 (htab->params->stub_bfd,
9e390558 11249 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11250 }
11251 if (PPC_LO (r2off) != 0)
11252 {
9e390558 11253 p += 4;
e7d1c40c 11254 bfd_put_32 (htab->params->stub_bfd,
9e390558 11255 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11256 }
ad8e1ba5 11257 }
9e390558
AM
11258 p += 4;
11259 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11260 p += 4;
407aa07c
AM
11261 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11262 p += 4;
721956f4 11263 break;
5d1634d7 11264
05d0e962
AM
11265 case ppc_stub_long_branch_notoc:
11266 case ppc_stub_long_branch_both:
11267 case ppc_stub_plt_branch_notoc:
11268 case ppc_stub_plt_branch_both:
11269 case ppc_stub_plt_call_notoc:
11270 case ppc_stub_plt_call_both:
11271 p = loc;
f891966f 11272 off = (stub_entry->stub_offset
05d0e962
AM
11273 + stub_entry->group->stub_sec->output_offset
11274 + stub_entry->group->stub_sec->output_section->vma);
11275 if (stub_entry->stub_type == ppc_stub_long_branch_both
11276 || stub_entry->stub_type == ppc_stub_plt_branch_both
11277 || stub_entry->stub_type == ppc_stub_plt_call_both)
11278 {
11279 off += 4;
11280 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11281 p += 4;
11282 }
11283 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11284 {
11285 targ = stub_entry->plt_ent->plt.offset & ~1;
11286 if (targ >= (bfd_vma) -2)
11287 abort ();
11288
11289 plt = htab->elf.splt;
11290 if (!htab->elf.dynamic_sections_created
11291 || stub_entry->h == NULL
11292 || stub_entry->h->elf.dynindx == -1)
11293 {
11294 if (stub_entry->symtype == STT_GNU_IFUNC)
11295 plt = htab->elf.iplt;
11296 else
11297 plt = htab->pltlocal;
11298 }
11299 targ += plt->output_offset + plt->output_section->vma;
11300 }
11301 else
11302 targ = (stub_entry->target_value
11303 + stub_entry->target_section->output_offset
11304 + stub_entry->target_section->output_section->vma);
04bdff6a 11305 odd = off & 4;
05d0e962 11306 off = targ - off;
f891966f 11307
3d58e1fc
AM
11308 relp = p;
11309 num_rel = 0;
04bdff6a
AM
11310 if (htab->powerxx_stubs)
11311 {
11312 bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
11313 p = build_powerxx_offset (htab->params->stub_bfd, p, off, odd, load);
11314 }
11315 else
11316 {
11317 /* The notoc stubs calculate their target (either a PLT entry or
11318 the global entry point of a function) relative to the PC
11319 returned by the "bcl" two instructions past the start of the
11320 sequence emitted by build_offset. The offset is therefore 8
11321 less than calculated from the start of the sequence. */
11322 off -= 8;
11323 p = build_offset (htab->params->stub_bfd, p, off,
11324 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11325 }
11326
f891966f 11327 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
05d0e962 11328 {
f891966f 11329 bfd_vma from;
3d58e1fc 11330 num_rel = 1;
f891966f
AM
11331 from = (stub_entry->stub_offset
11332 + stub_entry->group->stub_sec->output_offset
11333 + stub_entry->group->stub_sec->output_section->vma
11334 + (p - loc));
05d0e962 11335 bfd_put_32 (htab->params->stub_bfd,
f891966f 11336 B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
11337 }
11338 else
11339 {
11340 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11341 p += 4;
11342 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11343 }
11344 p += 4;
df136d64 11345
3d58e1fc
AM
11346 if (info->emitrelocations)
11347 {
04bdff6a
AM
11348 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
11349 if (htab->powerxx_stubs)
11350 num_rel += num_relocs_for_powerxx_offset (off, odd);
11351 else
11352 {
11353 num_rel += num_relocs_for_offset (off);
11354 roff += 16;
11355 }
3d58e1fc
AM
11356 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11357 if (r == NULL)
11358 return FALSE;
04bdff6a
AM
11359 if (htab->powerxx_stubs)
11360 r = emit_relocs_for_powerxx_offset (info, r, roff, targ, off, odd);
11361 else
11362 r = emit_relocs_for_offset (info, r, roff, targ, off);
3d58e1fc
AM
11363 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11364 || stub_entry->stub_type == ppc_stub_long_branch_both)
11365 {
11366 ++r;
11367 roff = p - 4 - stub_entry->group->stub_sec->contents;
11368 r->r_offset = roff;
11369 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11370 r->r_addend = targ;
11371 if (stub_entry->h != NULL
11372 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11373 return FALSE;
11374 }
11375 }
11376
04bdff6a
AM
11377 if (!htab->powerxx_stubs
11378 && htab->glink_eh_frame != NULL
11379 && htab->glink_eh_frame->size != 0)
df136d64
AM
11380 {
11381 bfd_byte *base, *eh;
11382 unsigned int lr_used, delta;
11383
11384 base = (htab->glink_eh_frame->contents
11385 + stub_entry->group->eh_base + 17);
11386 eh = base + stub_entry->group->eh_size;
11387 lr_used = stub_entry->stub_offset + 8;
11388 if (stub_entry->stub_type == ppc_stub_long_branch_both
11389 || stub_entry->stub_type == ppc_stub_plt_branch_both
11390 || stub_entry->stub_type == ppc_stub_plt_call_both)
11391 lr_used += 4;
11392 delta = lr_used - stub_entry->group->lr_restore;
11393 stub_entry->group->lr_restore = lr_used + 8;
11394 eh = eh_advance (htab->elf.dynobj, eh, delta);
11395 *eh++ = DW_CFA_register;
11396 *eh++ = 65;
11397 *eh++ = 12;
11398 *eh++ = DW_CFA_advance_loc + 2;
11399 *eh++ = DW_CFA_restore_extended;
11400 *eh++ = 65;
11401 stub_entry->group->eh_size = eh - base;
11402 }
05d0e962
AM
11403 break;
11404
721956f4 11405 case ppc_stub_plt_call:
794e51c0 11406 case ppc_stub_plt_call_r2save:
e054468f 11407 if (stub_entry->h != NULL
b31867b6
AM
11408 && stub_entry->h->is_func_descriptor
11409 && stub_entry->h->oh != NULL)
c862ae31 11410 {
b31867b6
AM
11411 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11412
11413 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11414 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11415 if (fh->elf.root.type == bfd_link_hash_undefined
11416 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11417 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11418 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11419 }
11420
721956f4 11421 /* Now build the stub. */
1aa42141
AM
11422 targ = stub_entry->plt_ent->plt.offset & ~1;
11423 if (targ >= (bfd_vma) -2)
721956f4
AM
11424 abort ();
11425
33e44f2e 11426 plt = htab->elf.splt;
25f23106
AM
11427 if (!htab->elf.dynamic_sections_created
11428 || stub_entry->h == NULL
11429 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11430 {
11431 if (stub_entry->symtype == STT_GNU_IFUNC)
11432 plt = htab->elf.iplt;
11433 else
11434 plt = htab->pltlocal;
11435 }
1aa42141 11436 targ += plt->output_offset + plt->output_section->vma;
e054468f 11437
1aa42141
AM
11438 off = (elf_gp (info->output_bfd)
11439 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11440 off = targ - off;
721956f4 11441
ad8e1ba5 11442 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11443 {
25f53a85 11444 info->callbacks->einfo
695344c0 11445 /* xgettext:c-format */
c1c8c1ef 11446 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11447 stub_entry->h != NULL
11448 ? stub_entry->h->elf.root.root.string
11449 : "<local sym>");
721956f4 11450 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11451 htab->stub_error = TRUE;
11452 return FALSE;
721956f4
AM
11453 }
11454
176a0d42
AM
11455 r = NULL;
11456 if (info->emitrelocations)
11457 {
6f20ed8a 11458 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11459 ((PPC_HA (off) != 0)
11460 + (htab->opd_abi
e7d1c40c 11461 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11462 && PPC_HA (off + 16) == PPC_HA (off))
11463 : 1)));
176a0d42
AM
11464 if (r == NULL)
11465 return FALSE;
6f20ed8a 11466 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11467 if (bfd_big_endian (info->output_bfd))
11468 r[0].r_offset += 2;
1aa42141 11469 r[0].r_addend = targ;
176a0d42 11470 }
a7f2871e
AM
11471 if (stub_entry->h != NULL
11472 && (stub_entry->h == htab->tls_get_addr_fd
11473 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11474 && htab->params->tls_get_addr_opt)
794e51c0 11475 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11476 else
794e51c0 11477 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11478 break;
11479
a4b6fadd
AM
11480 case ppc_stub_save_res:
11481 return TRUE;
11482
721956f4
AM
11483 default:
11484 BFD_FAIL ();
b34976b6 11485 return FALSE;
721956f4
AM
11486 }
11487
1aa42141 11488 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 11489
e7d1c40c 11490 if (htab->params->emit_stub_syms)
97b639ba
AM
11491 {
11492 struct elf_link_hash_entry *h;
ee75fd95
AM
11493 size_t len1, len2;
11494 char *name;
11495 const char *const stub_str[] = { "long_branch",
05d0e962
AM
11496 "long_branch",
11497 "long_branch",
3f6ff479
AM
11498 "long_branch",
11499 "plt_branch",
ee75fd95 11500 "plt_branch",
05d0e962
AM
11501 "plt_branch",
11502 "plt_branch",
11503 "plt_call",
11504 "plt_call",
794e51c0 11505 "plt_call",
ee75fd95
AM
11506 "plt_call" };
11507
11508 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11509 len2 = strlen (stub_entry->root.string);
11510 name = bfd_malloc (len1 + len2 + 2);
11511 if (name == NULL)
11512 return FALSE;
11513 memcpy (name, stub_entry->root.string, 9);
11514 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11515 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11516 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11517 if (h == NULL)
11518 return FALSE;
11519 if (h->root.type == bfd_link_hash_new)
11520 {
11521 h->root.type = bfd_link_hash_defined;
6f20ed8a 11522 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11523 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11524 h->ref_regular = 1;
11525 h->def_regular = 1;
11526 h->ref_regular_nonweak = 1;
11527 h->forced_local = 1;
11528 h->non_elf = 0;
2ec55de3 11529 h->root.linker_def = 1;
97b639ba
AM
11530 }
11531 }
11532
b34976b6 11533 return TRUE;
721956f4
AM
11534}
11535
11536/* As above, but don't actually build the stub. Just bump offset so
11537 we know stub section sizes, and select plt_branch stubs where
11538 long_branch stubs won't do. */
11539
b34976b6 11540static bfd_boolean
4ce794b7 11541ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11542{
11543 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11544 struct bfd_link_info *info;
721956f4 11545 struct ppc_link_hash_table *htab;
f891966f
AM
11546 asection *plt;
11547 bfd_vma targ, off, r2off;
04bdff6a 11548 unsigned int size, extra, lr_used, delta, odd;
721956f4
AM
11549
11550 /* Massage our args to the form they really have. */
11551 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11552 info = in_arg;
11553
11554 htab = ppc_hash_table (info);
4dfe6ac6
NC
11555 if (htab == NULL)
11556 return FALSE;
721956f4 11557
1aa42141
AM
11558 /* Make a note of the offset within the stubs for this entry. */
11559 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11560
a4b6fadd
AM
11561 if (stub_entry->h != NULL
11562 && stub_entry->h->save_res
11563 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11564 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11565 {
11566 /* Don't make stubs to out-of-line register save/restore
11567 functions. Instead, emit copies of the functions. */
11568 stub_entry->group->needs_save_res = 1;
11569 stub_entry->stub_type = ppc_stub_save_res;
11570 return TRUE;
11571 }
11572
f891966f 11573 switch (stub_entry->stub_type)
721956f4 11574 {
f891966f
AM
11575 case ppc_stub_plt_branch:
11576 case ppc_stub_plt_branch_r2off:
11577 /* Reset the stub type from the plt branch variant in case we now
11578 can reach with a shorter stub. */
11579 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11580 /* Fall through. */
11581 case ppc_stub_long_branch:
11582 case ppc_stub_long_branch_r2off:
1aa42141
AM
11583 targ = (stub_entry->target_value
11584 + stub_entry->target_section->output_offset
11585 + stub_entry->target_section->output_section->vma);
f891966f 11586 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
1aa42141
AM
11587 off = (stub_entry->stub_offset
11588 + stub_entry->group->stub_sec->output_offset
11589 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11590
ad8e1ba5 11591 size = 4;
f891966f 11592 r2off = 0;
ad8e1ba5
AM
11593 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11594 {
25f53a85 11595 r2off = get_r2off (info, stub_entry);
a7c49797 11596 if (r2off == (bfd_vma) -1)
aa374f67
AM
11597 {
11598 htab->stub_error = TRUE;
11599 return FALSE;
11600 }
a7c49797 11601 size = 8;
ac2df442 11602 if (PPC_HA (r2off) != 0)
a7c49797
AM
11603 size += 4;
11604 if (PPC_LO (r2off) != 0)
11605 size += 4;
1aa42141 11606 off += size - 4;
ad8e1ba5 11607 }
1aa42141 11608 off = targ - off;
ad8e1ba5 11609
f891966f
AM
11610 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11611 Do the same for -R objects without function descriptors. */
11612 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11613 && r2off == 0
11614 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11615 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 11616 {
f891966f 11617 struct ppc_branch_hash_entry *br_entry;
df136d64 11618
f891966f
AM
11619 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11620 stub_entry->root.string + 9,
11621 TRUE, FALSE);
11622 if (br_entry == NULL)
721956f4 11623 {
f891966f
AM
11624 _bfd_error_handler (_("can't build branch stub `%s'"),
11625 stub_entry->root.string);
11626 htab->stub_error = TRUE;
11627 return FALSE;
721956f4
AM
11628 }
11629
f891966f 11630 if (br_entry->iter != htab->stub_iteration)
721956f4 11631 {
f891966f
AM
11632 br_entry->iter = htab->stub_iteration;
11633 br_entry->offset = htab->brlt->size;
11634 htab->brlt->size += 8;
63bc6f6c 11635
f891966f
AM
11636 if (htab->relbrlt != NULL)
11637 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11638 else if (info->emitrelocations)
84f5d08e 11639 {
f891966f
AM
11640 htab->brlt->reloc_count += 1;
11641 htab->brlt->flags |= SEC_RELOC;
05d0e962 11642 }
f891966f 11643 }
ac2df442 11644
f891966f
AM
11645 targ = (br_entry->offset
11646 + htab->brlt->output_offset
11647 + htab->brlt->output_section->vma);
11648 off = (elf_gp (info->output_bfd)
11649 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11650 off = targ - off;
176a0d42 11651
f891966f
AM
11652 if (info->emitrelocations)
11653 {
11654 stub_entry->group->stub_sec->reloc_count
11655 += 1 + (PPC_HA (off) != 0);
11656 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11657 }
05d0e962 11658
f891966f
AM
11659 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11660 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11661 {
11662 size = 12;
11663 if (PPC_HA (off) != 0)
11664 size = 16;
ac2df442 11665 }
f891966f 11666 else
ac2df442 11667 {
f891966f
AM
11668 size = 16;
11669 if (PPC_HA (off) != 0)
11670 size += 4;
11671
11672 if (PPC_HA (r2off) != 0)
11673 size += 4;
11674 if (PPC_LO (r2off) != 0)
11675 size += 4;
ac2df442 11676 }
721956f4 11677 }
f891966f
AM
11678 else if (info->emitrelocations)
11679 {
11680 stub_entry->group->stub_sec->reloc_count += 1;
11681 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11682 }
11683 break;
11684
11685 case ppc_stub_plt_branch_notoc:
11686 case ppc_stub_plt_branch_both:
11687 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11688 /* Fall through. */
11689 case ppc_stub_long_branch_notoc:
11690 case ppc_stub_long_branch_both:
11691 off = (stub_entry->stub_offset
11692 + stub_entry->group->stub_sec->output_offset
11693 + stub_entry->group->stub_sec->output_section->vma);
11694 size = 0;
11695 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11696 size = 4;
11697 off += size;
11698 targ = (stub_entry->target_value
11699 + stub_entry->target_section->output_offset
11700 + stub_entry->target_section->output_section->vma);
04bdff6a 11701 odd = off & 4;
f891966f
AM
11702 off = targ - off;
11703
3d58e1fc
AM
11704 if (info->emitrelocations)
11705 {
04bdff6a
AM
11706 unsigned int num_rel;
11707 if (htab->powerxx_stubs)
11708 num_rel = num_relocs_for_powerxx_offset (off, odd);
11709 else
11710 num_rel = num_relocs_for_offset (off - 8);
11711 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
11712 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11713 }
11714
04bdff6a
AM
11715 if (htab->powerxx_stubs)
11716 extra = size_powerxx_offset (off, odd);
11717 else
11718 extra = size_offset (off - 8);
f891966f
AM
11719 /* Include branch insn plus those in the offset sequence. */
11720 size += 4 + extra;
11721 /* The branch insn is at the end, or "extra" bytes along. So
11722 its offset will be "extra" bytes less that that already
11723 calculated. */
11724 off -= extra;
11725
04bdff6a
AM
11726 if (!htab->powerxx_stubs)
11727 {
11728 /* After the bcl, lr has been modified so we need to emit
11729 .eh_frame info saying the return address is in r12. */
11730 lr_used = stub_entry->stub_offset + 8;
11731 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11732 lr_used += 4;
11733 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11734 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11735 DW_CFA_restore_extended 65. */
11736 delta = lr_used - stub_entry->group->lr_restore;
11737 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11738 stub_entry->group->lr_restore = lr_used + 8;
11739 }
f891966f
AM
11740
11741 /* If the branch can't reach, use a plt_branch. */
11742 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11743 {
11744 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11745 - ppc_stub_long_branch_notoc);
11746 size += 4;
11747 }
3d58e1fc
AM
11748 else if (info->emitrelocations)
11749 stub_entry->group->stub_sec->reloc_count +=1;
f891966f
AM
11750 break;
11751
11752 case ppc_stub_plt_call_notoc:
11753 case ppc_stub_plt_call_both:
11754 off = (stub_entry->stub_offset
11755 + stub_entry->group->stub_sec->output_offset
11756 + stub_entry->group->stub_sec->output_section->vma);
11757 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11758 off += 4;
11759 targ = stub_entry->plt_ent->plt.offset & ~1;
11760 if (targ >= (bfd_vma) -2)
11761 abort ();
11762
11763 plt = htab->elf.splt;
11764 if (!htab->elf.dynamic_sections_created
11765 || stub_entry->h == NULL
11766 || stub_entry->h->elf.dynindx == -1)
11767 {
11768 if (stub_entry->symtype == STT_GNU_IFUNC)
11769 plt = htab->elf.iplt;
11770 else
11771 plt = htab->pltlocal;
11772 }
11773 targ += plt->output_offset + plt->output_section->vma;
04bdff6a 11774 odd = off & 4;
f891966f
AM
11775 off = targ - off;
11776
11777 if (htab->params->plt_stub_align != 0)
11778 {
11779 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11780
11781 stub_entry->group->stub_sec->size += pad;
11782 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11783 off -= pad;
11784 }
11785
3d58e1fc
AM
11786 if (info->emitrelocations)
11787 {
04bdff6a
AM
11788 unsigned int num_rel;
11789 if (htab->powerxx_stubs)
11790 num_rel = num_relocs_for_powerxx_offset (off, odd);
11791 else
11792 num_rel = num_relocs_for_offset (off - 8);
11793 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
11794 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11795 }
11796
f891966f
AM
11797 size = plt_stub_size (htab, stub_entry, off);
11798
04bdff6a
AM
11799 if (!htab->powerxx_stubs)
11800 {
11801 /* After the bcl, lr has been modified so we need to emit
11802 .eh_frame info saying the return address is in r12. */
11803 lr_used = stub_entry->stub_offset + 8;
11804 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11805 lr_used += 4;
11806 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11807 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11808 DW_CFA_restore_extended 65. */
11809 delta = lr_used - stub_entry->group->lr_restore;
11810 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11811 stub_entry->group->lr_restore = lr_used + 8;
11812 }
f891966f
AM
11813 break;
11814
11815 case ppc_stub_plt_call:
11816 case ppc_stub_plt_call_r2save:
11817 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11818 if (targ >= (bfd_vma) -2)
11819 abort ();
11820 plt = htab->elf.splt;
11821 if (!htab->elf.dynamic_sections_created
11822 || stub_entry->h == NULL
11823 || stub_entry->h->elf.dynindx == -1)
11824 {
11825 if (stub_entry->symtype == STT_GNU_IFUNC)
11826 plt = htab->elf.iplt;
11827 else
11828 plt = htab->pltlocal;
11829 }
11830 targ += plt->output_offset + plt->output_section->vma;
11831
11832 off = (elf_gp (info->output_bfd)
11833 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11834 off = targ - off;
11835
11836 if (htab->params->plt_stub_align != 0)
11837 {
11838 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11839
11840 stub_entry->group->stub_sec->size += pad;
11841 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11842 }
11843
11844 if (info->emitrelocations)
11845 {
11846 stub_entry->group->stub_sec->reloc_count
11847 += ((PPC_HA (off) != 0)
11848 + (htab->opd_abi
11849 ? 2 + (htab->params->plt_static_chain
11850 && PPC_HA (off + 16) == PPC_HA (off))
11851 : 1));
11852 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11853 }
11854
11855 size = plt_stub_size (htab, stub_entry, off);
11856
11857 if (stub_entry->h != NULL
11858 && (stub_entry->h == htab->tls_get_addr_fd
11859 || stub_entry->h == htab->tls_get_addr)
11860 && htab->params->tls_get_addr_opt
11861 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11862 {
11863 /* After the bctrl, lr has been modified so we need to
11864 emit .eh_frame info saying the return address is
11865 on the stack. In fact we put the EH info specifying
11866 that the return address is on the stack *at* the
11867 call rather than after it, because the EH info for a
11868 call needs to be specified by that point.
11869 See libgcc/unwind-dw2.c execute_cfa_program. */
11870 lr_used = stub_entry->stub_offset + size - 20;
11871 /* The eh_frame info will consist of a DW_CFA_advance_loc
11872 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11873 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11874 delta = lr_used - stub_entry->group->lr_restore;
11875 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11876 stub_entry->group->lr_restore = size - 4;
11877 }
11878 break;
11879
11880 default:
11881 BFD_FAIL ();
11882 return FALSE;
721956f4
AM
11883 }
11884
6f20ed8a 11885 stub_entry->group->stub_sec->size += size;
b34976b6 11886 return TRUE;
721956f4
AM
11887}
11888
11889/* Set up various things so that we can make a list of input sections
11890 for each output section included in the link. Returns -1 on error,
cedb70c5 11891 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11892
11893int
e7d1c40c 11894ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11895{
6f20ed8a 11896 unsigned int id;
721956f4
AM
11897 bfd_size_type amt;
11898 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11899
4dfe6ac6
NC
11900 if (htab == NULL)
11901 return -1;
4c52953f 11902
7cf7fcc8 11903 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
11904 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11905 htab->sec_info = bfd_zmalloc (amt);
11906 if (htab->sec_info == NULL)
721956f4
AM
11907 return -1;
11908
3d6f9012
AM
11909 /* Set toc_off for com, und, abs and ind sections. */
11910 for (id = 0; id < 3; id++)
6f20ed8a 11911 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11912
721956f4
AM
11913 return 1;
11914}
11915
927be08e
AM
11916/* Set up for first pass at multitoc partitioning. */
11917
11918void
11919ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11920{
11921 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11922
1c865ab2 11923 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11924 htab->toc_bfd = NULL;
11925 htab->toc_first_sec = NULL;
11926}
11927
e717da7e
AM
11928/* The linker repeatedly calls this function for each TOC input section
11929 and linker generated GOT section. Group input bfds such that the toc
927be08e 11930 within a group is less than 64k in size. */
ad8e1ba5 11931
927be08e 11932bfd_boolean
4ce794b7 11933ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11934{
11935 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11936 bfd_vma addr, off, limit;
ad8e1ba5 11937
4dfe6ac6
NC
11938 if (htab == NULL)
11939 return FALSE;
11940
927be08e 11941 if (!htab->second_toc_pass)
4c52953f 11942 {
927be08e 11943 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11944 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11945
11946 if (new_bfd)
bf102f86
AM
11947 {
11948 htab->toc_bfd = isec->owner;
11949 htab->toc_first_sec = isec;
11950 }
927be08e 11951
bf102f86
AM
11952 addr = isec->output_offset + isec->output_section->vma;
11953 off = addr - htab->toc_curr;
d77c8a4b
AM
11954 limit = 0x80008000;
11955 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11956 limit = 0x10000;
11957 if (off + isec->size > limit)
bf102f86
AM
11958 {
11959 addr = (htab->toc_first_sec->output_offset
11960 + htab->toc_first_sec->output_section->vma);
11961 htab->toc_curr = addr;
a27e685f 11962 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11963 }
99877b66 11964
927be08e
AM
11965 /* toc_curr is the base address of this toc group. Set elf_gp
11966 for the input section to be the offset relative to the
11967 output toc base plus 0x8000. Making the input elf_gp an
11968 offset allows us to move the toc as a whole without
11969 recalculating input elf_gp. */
06bcf541 11970 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
11971 off += TOC_BASE_OFF;
11972
11973 /* Die if someone uses a linker script that doesn't keep input
11974 file .toc and .got together. */
a4fd3de5
AM
11975 if (new_bfd
11976 && elf_gp (isec->owner) != 0
927be08e
AM
11977 && elf_gp (isec->owner) != off)
11978 return FALSE;
11979
11980 elf_gp (isec->owner) = off;
11981 return TRUE;
4c52953f 11982 }
927be08e
AM
11983
11984 /* During the second pass toc_first_sec points to the start of
11985 a toc group, and toc_curr is used to track the old elf_gp.
11986 We use toc_bfd to ensure we only look at each bfd once. */
11987 if (htab->toc_bfd == isec->owner)
11988 return TRUE;
11989 htab->toc_bfd = isec->owner;
11990
11991 if (htab->toc_first_sec == NULL
11992 || htab->toc_curr != elf_gp (isec->owner))
11993 {
11994 htab->toc_curr = elf_gp (isec->owner);
11995 htab->toc_first_sec = isec;
11996 }
11997 addr = (htab->toc_first_sec->output_offset
11998 + htab->toc_first_sec->output_section->vma);
06bcf541 11999 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
12000 elf_gp (isec->owner) = off;
12001
12002 return TRUE;
ad8e1ba5
AM
12003}
12004
927be08e
AM
12005/* Called via elf_link_hash_traverse to merge GOT entries for global
12006 symbol H. */
12007
12008static bfd_boolean
12009merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12010{
12011 if (h->root.type == bfd_link_hash_indirect)
12012 return TRUE;
12013
927be08e
AM
12014 merge_got_entries (&h->got.glist);
12015
12016 return TRUE;
12017}
12018
12019/* Called via elf_link_hash_traverse to allocate GOT entries for global
12020 symbol H. */
12021
12022static bfd_boolean
12023reallocate_got (struct elf_link_hash_entry *h, void *inf)
12024{
12025 struct got_entry *gent;
12026
12027 if (h->root.type == bfd_link_hash_indirect)
12028 return TRUE;
12029
927be08e
AM
12030 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12031 if (!gent->is_indirect)
12032 allocate_got (h, (struct bfd_link_info *) inf, gent);
12033 return TRUE;
12034}
12035
12036/* Called on the first multitoc pass after the last call to
12037 ppc64_elf_next_toc_section. This function removes duplicate GOT
12038 entries. */
12039
12040bfd_boolean
12041ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12042{
12043 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
12044 struct bfd *ibfd, *ibfd2;
12045 bfd_boolean done_something;
12046
12047 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12048
7865406b
AM
12049 if (!htab->do_multi_toc)
12050 return FALSE;
12051
d0fae19d 12052 /* Merge global sym got entries within a toc group. */
927be08e
AM
12053 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12054
12055 /* And tlsld_got. */
c72f2fb2 12056 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12057 {
12058 struct got_entry *ent, *ent2;
12059
12060 if (!is_ppc64_elf (ibfd))
12061 continue;
12062
12063 ent = ppc64_tlsld_got (ibfd);
12064 if (!ent->is_indirect
12065 && ent->got.offset != (bfd_vma) -1)
12066 {
c72f2fb2 12067 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12068 {
12069 if (!is_ppc64_elf (ibfd2))
12070 continue;
12071
12072 ent2 = ppc64_tlsld_got (ibfd2);
12073 if (!ent2->is_indirect
12074 && ent2->got.offset != (bfd_vma) -1
12075 && elf_gp (ibfd2) == elf_gp (ibfd))
12076 {
12077 ent2->is_indirect = TRUE;
12078 ent2->got.ent = ent;
12079 }
12080 }
12081 }
12082 }
12083
12084 /* Zap sizes of got sections. */
33e44f2e
AM
12085 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12086 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12087 htab->got_reli_size = 0;
12088
c72f2fb2 12089 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12090 {
12091 asection *got, *relgot;
12092
12093 if (!is_ppc64_elf (ibfd))
12094 continue;
12095
12096 got = ppc64_elf_tdata (ibfd)->got;
12097 if (got != NULL)
12098 {
12099 got->rawsize = got->size;
12100 got->size = 0;
12101 relgot = ppc64_elf_tdata (ibfd)->relgot;
12102 relgot->rawsize = relgot->size;
12103 relgot->size = 0;
12104 }
12105 }
12106
12107 /* Now reallocate the got, local syms first. We don't need to
12108 allocate section contents again since we never increase size. */
c72f2fb2 12109 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12110 {
12111 struct got_entry **lgot_ents;
12112 struct got_entry **end_lgot_ents;
12113 struct plt_entry **local_plt;
12114 struct plt_entry **end_local_plt;
f961d9dd 12115 unsigned char *lgot_masks;
927be08e
AM
12116 bfd_size_type locsymcount;
12117 Elf_Internal_Shdr *symtab_hdr;
19e08130 12118 asection *s;
927be08e
AM
12119
12120 if (!is_ppc64_elf (ibfd))
12121 continue;
12122
12123 lgot_ents = elf_local_got_ents (ibfd);
12124 if (!lgot_ents)
12125 continue;
12126
12127 symtab_hdr = &elf_symtab_hdr (ibfd);
12128 locsymcount = symtab_hdr->sh_info;
12129 end_lgot_ents = lgot_ents + locsymcount;
12130 local_plt = (struct plt_entry **) end_lgot_ents;
12131 end_local_plt = local_plt + locsymcount;
f961d9dd 12132 lgot_masks = (unsigned char *) end_local_plt;
927be08e 12133 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
12134 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12135 {
12136 struct got_entry *ent;
12137
12138 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12139 {
19e08130
AM
12140 unsigned int ent_size = 8;
12141 unsigned int rel_size = sizeof (Elf64_External_Rela);
12142
d0fae19d
AM
12143 ent->got.offset = s->size;
12144 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12145 {
19e08130
AM
12146 ent_size *= 2;
12147 rel_size *= 2;
12148 }
12149 s->size += ent_size;
37da22e5 12150 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12151 {
33e44f2e 12152 htab->elf.irelplt->size += rel_size;
19e08130
AM
12153 htab->got_reli_size += rel_size;
12154 }
6a3858a6
AM
12155 else if (bfd_link_pic (info)
12156 && !((ent->tls_type & TLS_TPREL) != 0
12157 && bfd_link_executable (info)))
19e08130
AM
12158 {
12159 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12160 srel->size += rel_size;
d0fae19d
AM
12161 }
12162 }
927be08e
AM
12163 }
12164 }
12165
12166 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12167
c72f2fb2 12168 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12169 {
12170 struct got_entry *ent;
12171
12172 if (!is_ppc64_elf (ibfd))
12173 continue;
12174
12175 ent = ppc64_tlsld_got (ibfd);
12176 if (!ent->is_indirect
12177 && ent->got.offset != (bfd_vma) -1)
12178 {
12179 asection *s = ppc64_elf_tdata (ibfd)->got;
12180 ent->got.offset = s->size;
12181 s->size += 16;
0e1862bb 12182 if (bfd_link_pic (info))
927be08e
AM
12183 {
12184 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12185 srel->size += sizeof (Elf64_External_Rela);
12186 }
12187 }
12188 }
12189
33e44f2e 12190 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12191 if (!done_something)
c72f2fb2 12192 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12193 {
12194 asection *got;
12195
12196 if (!is_ppc64_elf (ibfd))
12197 continue;
12198
12199 got = ppc64_elf_tdata (ibfd)->got;
12200 if (got != NULL)
12201 {
12202 done_something = got->rawsize != got->size;
12203 if (done_something)
12204 break;
12205 }
12206 }
12207
12208 if (done_something)
e7d1c40c 12209 (*htab->params->layout_sections_again) ();
927be08e
AM
12210
12211 /* Set up for second pass over toc sections to recalculate elf_gp
12212 on input sections. */
12213 htab->toc_bfd = NULL;
12214 htab->toc_first_sec = NULL;
12215 htab->second_toc_pass = TRUE;
12216 return done_something;
12217}
12218
12219/* Called after second pass of multitoc partitioning. */
12220
12221void
12222ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12223{
12224 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12225
12226 /* After the second pass, toc_curr tracks the TOC offset used
12227 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12228 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12229}
12230
9b5ecbd0
AM
12231/* No toc references were found in ISEC. If the code in ISEC makes no
12232 calls, then there's no need to use toc adjusting stubs when branching
12233 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12234 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12235 needed, and 2 if a cyclical call-graph was found but no other reason
12236 for a stub was detected. If called from the top level, a return of
12237 2 means the same as a return of 0. */
9b5ecbd0
AM
12238
12239static int
4ce794b7 12240toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12241{
9b5ecbd0 12242 int ret;
70cc837d
AM
12243
12244 /* Mark this section as checked. */
12245 isec->call_check_done = 1;
9b5ecbd0 12246
772119ce
AM
12247 /* We know none of our code bearing sections will need toc stubs. */
12248 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12249 return 0;
12250
eea6121a 12251 if (isec->size == 0)
082c50f8
AM
12252 return 0;
12253
4c52953f
AM
12254 if (isec->output_section == NULL)
12255 return 0;
12256
4c52953f 12257 ret = 0;
70cc837d 12258 if (isec->reloc_count != 0)
9b5ecbd0 12259 {
70cc837d
AM
12260 Elf_Internal_Rela *relstart, *rel;
12261 Elf_Internal_Sym *local_syms;
12262 struct ppc_link_hash_table *htab;
2917689a 12263
70cc837d
AM
12264 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12265 info->keep_memory);
12266 if (relstart == NULL)
12267 return -1;
90aecf7a 12268
70cc837d
AM
12269 /* Look for branches to outside of this section. */
12270 local_syms = NULL;
12271 htab = ppc_hash_table (info);
12272 if (htab == NULL)
12273 return -1;
4c52953f 12274
70cc837d 12275 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12276 {
70cc837d
AM
12277 enum elf_ppc64_reloc_type r_type;
12278 unsigned long r_symndx;
12279 struct elf_link_hash_entry *h;
12280 struct ppc_link_hash_entry *eh;
12281 Elf_Internal_Sym *sym;
12282 asection *sym_sec;
12283 struct _opd_sec_data *opd;
12284 bfd_vma sym_value;
12285 bfd_vma dest;
12286
12287 r_type = ELF64_R_TYPE (rel->r_info);
12288 if (r_type != R_PPC64_REL24
05d0e962 12289 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
12290 && r_type != R_PPC64_REL14
12291 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 12292 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
12293 && r_type != R_PPC64_PLTCALL
12294 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 12295 continue;
4c52953f 12296
70cc837d
AM
12297 r_symndx = ELF64_R_SYM (rel->r_info);
12298 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12299 isec->owner))
4c52953f 12300 {
70cc837d
AM
12301 ret = -1;
12302 break;
12303 }
4c52953f 12304
70cc837d
AM
12305 /* Calls to dynamic lib functions go through a plt call stub
12306 that uses r2. */
12307 eh = (struct ppc_link_hash_entry *) h;
12308 if (eh != NULL
12309 && (eh->elf.plt.plist != NULL
12310 || (eh->oh != NULL
12311 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12312 {
12313 ret = 1;
12314 break;
4c52953f
AM
12315 }
12316
70cc837d
AM
12317 if (sym_sec == NULL)
12318 /* Ignore other undefined symbols. */
4c52953f 12319 continue;
4c52953f 12320
70cc837d
AM
12321 /* Assume branches to other sections not included in the
12322 link need stubs too, to cover -R and absolute syms. */
12323 if (sym_sec->output_section == NULL)
12324 {
12325 ret = 1;
12326 break;
12327 }
4c52953f 12328
70cc837d
AM
12329 if (h == NULL)
12330 sym_value = sym->st_value;
12331 else
12332 {
12333 if (h->root.type != bfd_link_hash_defined
12334 && h->root.type != bfd_link_hash_defweak)
12335 abort ();
12336 sym_value = h->root.u.def.value;
12337 }
12338 sym_value += rel->r_addend;
4c52953f 12339
70cc837d
AM
12340 /* If this branch reloc uses an opd sym, find the code section. */
12341 opd = get_opd_info (sym_sec);
12342 if (opd != NULL)
12343 {
12344 if (h == NULL && opd->adjust != NULL)
12345 {
12346 long adjust;
4c52953f 12347
92a9c616 12348 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12349 if (adjust == -1)
12350 /* Assume deleted functions won't ever be called. */
12351 continue;
12352 sym_value += adjust;
12353 }
4c52953f 12354
aef36ac1
AM
12355 dest = opd_entry_value (sym_sec, sym_value,
12356 &sym_sec, NULL, FALSE);
70cc837d
AM
12357 if (dest == (bfd_vma) -1)
12358 continue;
12359 }
12360 else
12361 dest = (sym_value
12362 + sym_sec->output_offset
12363 + sym_sec->output_section->vma);
4c52953f 12364
70cc837d
AM
12365 /* Ignore branch to self. */
12366 if (sym_sec == isec)
12367 continue;
4c52953f 12368
70cc837d
AM
12369 /* If the called function uses the toc, we need a stub. */
12370 if (sym_sec->has_toc_reloc
12371 || sym_sec->makes_toc_func_call)
4c52953f 12372 {
70cc837d 12373 ret = 1;
4c52953f
AM
12374 break;
12375 }
70cc837d
AM
12376
12377 /* Assume any branch that needs a long branch stub might in fact
12378 need a plt_branch stub. A plt_branch stub uses r2. */
12379 else if (dest - (isec->output_offset
12380 + isec->output_section->vma
6911b7dc
AM
12381 + rel->r_offset) + (1 << 25)
12382 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12383 ? h->other
12384 : sym->st_other))
4c52953f 12385 {
70cc837d
AM
12386 ret = 1;
12387 break;
12388 }
12389
12390 /* If calling back to a section in the process of being
12391 tested, we can't say for sure that no toc adjusting stubs
12392 are needed, so don't return zero. */
12393 else if (sym_sec->call_check_in_progress)
12394 ret = 2;
12395
12396 /* Branches to another section that itself doesn't have any TOC
12397 references are OK. Recursively call ourselves to check. */
12398 else if (!sym_sec->call_check_done)
12399 {
12400 int recur;
12401
12402 /* Mark current section as indeterminate, so that other
12403 sections that call back to current won't be marked as
12404 known. */
12405 isec->call_check_in_progress = 1;
12406 recur = toc_adjusting_stub_needed (info, sym_sec);
12407 isec->call_check_in_progress = 0;
12408
4c52953f
AM
12409 if (recur != 0)
12410 {
70cc837d
AM
12411 ret = recur;
12412 if (recur != 2)
12413 break;
4c52953f
AM
12414 }
12415 }
4c52953f 12416 }
70cc837d
AM
12417
12418 if (local_syms != NULL
12419 && (elf_symtab_hdr (isec->owner).contents
12420 != (unsigned char *) local_syms))
12421 free (local_syms);
12422 if (elf_section_data (isec)->relocs != relstart)
12423 free (relstart);
9b5ecbd0
AM
12424 }
12425
70cc837d
AM
12426 if ((ret & 1) == 0
12427 && isec->map_head.s != NULL
12428 && (strcmp (isec->output_section->name, ".init") == 0
12429 || strcmp (isec->output_section->name, ".fini") == 0))
12430 {
12431 if (isec->map_head.s->has_toc_reloc
12432 || isec->map_head.s->makes_toc_func_call)
12433 ret = 1;
12434 else if (!isec->map_head.s->call_check_done)
12435 {
12436 int recur;
12437 isec->call_check_in_progress = 1;
12438 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12439 isec->call_check_in_progress = 0;
12440 if (recur != 0)
12441 ret = recur;
12442 }
12443 }
12444
12445 if (ret == 1)
12446 isec->makes_toc_func_call = 1;
4c52953f 12447
9b5ecbd0
AM
12448 return ret;
12449}
12450
721956f4
AM
12451/* The linker repeatedly calls this function for each input section,
12452 in the order that input sections are linked into output sections.
12453 Build lists of input sections to determine groupings between which
12454 we may insert linker stubs. */
12455
9b5ecbd0 12456bfd_boolean
4ce794b7 12457ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12458{
12459 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12460
4dfe6ac6
NC
12461 if (htab == NULL)
12462 return FALSE;
12463
734b6cf9 12464 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12465 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12466 {
3d6f9012
AM
12467 /* This happens to make the list in reverse order,
12468 which is what we want. */
6f20ed8a
AM
12469 htab->sec_info[isec->id].u.list
12470 = htab->sec_info[isec->output_section->id].u.list;
12471 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12472 }
ad8e1ba5 12473
4c52953f 12474 if (htab->multi_toc_needed)
9b5ecbd0 12475 {
8b974ba3
AM
12476 /* Analyse sections that aren't already flagged as needing a
12477 valid toc pointer. Exclude .fixup for the linux kernel.
12478 .fixup contains branches, but only back to the function that
12479 hit an exception. */
12480 if (!(isec->has_toc_reloc
12481 || (isec->flags & SEC_CODE) == 0
12482 || strcmp (isec->name, ".fixup") == 0
12483 || isec->call_check_done))
12484 {
12485 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12486 return FALSE;
8b974ba3
AM
12487 }
12488 /* Make all sections use the TOC assigned for this object file.
12489 This will be wrong for pasted sections; We fix that in
12490 check_pasted_section(). */
12491 if (elf_gp (isec->owner) != 0)
12492 htab->toc_curr = elf_gp (isec->owner);
12493 }
12494
6f20ed8a 12495 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12496 return TRUE;
721956f4
AM
12497}
12498
70cc837d
AM
12499/* Check that all .init and .fini sections use the same toc, if they
12500 have toc relocs. */
12501
12502static bfd_boolean
12503check_pasted_section (struct bfd_link_info *info, const char *name)
12504{
12505 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12506
12507 if (o != NULL)
12508 {
12509 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12510 bfd_vma toc_off = 0;
12511 asection *i;
12512
12513 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12514 if (i->has_toc_reloc)
12515 {
12516 if (toc_off == 0)
6f20ed8a
AM
12517 toc_off = htab->sec_info[i->id].toc_off;
12518 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12519 return FALSE;
12520 }
6683a28d
AM
12521
12522 if (toc_off == 0)
12523 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12524 if (i->makes_toc_func_call)
12525 {
6f20ed8a 12526 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12527 break;
12528 }
12529
70cc837d
AM
12530 /* Make sure the whole pasted function uses the same toc offset. */
12531 if (toc_off != 0)
12532 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12533 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12534 }
12535 return TRUE;
12536}
12537
12538bfd_boolean
12539ppc64_elf_check_init_fini (struct bfd_link_info *info)
12540{
12541 return (check_pasted_section (info, ".init")
12542 & check_pasted_section (info, ".fini"));
12543}
12544
721956f4
AM
12545/* See whether we can group stub sections together. Grouping stub
12546 sections may result in fewer stubs. More importantly, we need to
12547 put all .init* and .fini* stubs at the beginning of the .init or
12548 .fini output sections respectively, because glibc splits the
12549 _init and _fini functions into multiple parts. Putting a stub in
12550 the middle of a function is not a good idea. */
12551
6f20ed8a
AM
12552static bfd_boolean
12553group_sections (struct bfd_link_info *info,
4ce794b7
AM
12554 bfd_size_type stub_group_size,
12555 bfd_boolean stubs_always_before_branch)
721956f4 12556{
6f20ed8a
AM
12557 struct ppc_link_hash_table *htab;
12558 asection *osec;
7c8fe5c4
AM
12559 bfd_boolean suppress_size_errors;
12560
6f20ed8a
AM
12561 htab = ppc_hash_table (info);
12562 if (htab == NULL)
12563 return FALSE;
12564
7c8fe5c4 12565 suppress_size_errors = FALSE;
7c8fe5c4
AM
12566 if (stub_group_size == 1)
12567 {
12568 /* Default values. */
12569 if (stubs_always_before_branch)
09f92717 12570 stub_group_size = 0x1e00000;
7c8fe5c4 12571 else
09f92717 12572 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12573 suppress_size_errors = TRUE;
12574 }
12575
6f20ed8a 12576 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12577 {
6f20ed8a
AM
12578 asection *tail;
12579
12580 if (osec->id >= htab->sec_info_arr_size)
12581 continue;
12582
12583 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12584 while (tail != NULL)
721956f4 12585 {
734b6cf9
AM
12586 asection *curr;
12587 asection *prev;
12588 bfd_size_type total;
12589 bfd_boolean big_sec;
12590 bfd_vma curr_toc;
6f20ed8a 12591 struct map_stub *group;
09f92717 12592 bfd_size_type group_size;
734b6cf9
AM
12593
12594 curr = tail;
eea6121a 12595 total = tail->size;
09f92717
AM
12596 group_size = (ppc64_elf_section_data (tail) != NULL
12597 && ppc64_elf_section_data (tail)->has_14bit_branch
12598 ? stub_group_size >> 10 : stub_group_size);
12599
12600 big_sec = total > group_size;
7c8fe5c4 12601 if (big_sec && !suppress_size_errors)
695344c0 12602 /* xgettext:c-format */
871b3ab2 12603 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 12604 tail->owner, tail);
6f20ed8a 12605 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12606
6f20ed8a 12607 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12608 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12609 < (ppc64_elf_section_data (prev) != NULL
12610 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12611 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12612 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12613 curr = prev;
12614
12615 /* OK, the size from the start of CURR to the end is less
09f92717 12616 than group_size and thus can be handled by one stub
734b6cf9 12617 section. (or the tail section is itself larger than
09f92717
AM
12618 group_size, in which case we may be toast.) We should
12619 really be keeping track of the total size of stubs added
12620 here, as stubs contribute to the final output section
12621 size. That's a little tricky, and this way will only
12622 break if stubs added make the total size more than 2^25,
12623 ie. for the default stub_group_size, if stubs total more
12624 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12625 group = bfd_alloc (curr->owner, sizeof (*group));
12626 if (group == NULL)
12627 return FALSE;
12628 group->link_sec = curr;
12629 group->stub_sec = NULL;
a4b6fadd 12630 group->needs_save_res = 0;
df136d64
AM
12631 group->lr_restore = 0;
12632 group->eh_size = 0;
12633 group->eh_base = 0;
a4b6fadd
AM
12634 group->next = htab->group;
12635 htab->group = group;
734b6cf9 12636 do
721956f4 12637 {
6f20ed8a 12638 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12639 /* Set up this stub group. */
6f20ed8a 12640 htab->sec_info[tail->id].u.group = group;
721956f4 12641 }
734b6cf9
AM
12642 while (tail != curr && (tail = prev) != NULL);
12643
09f92717 12644 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12645 bytes before the stub section can be handled by it too.
12646 Don't do this if we have a really large section after the
12647 stubs, as adding more stubs increases the chance that
12648 branches may not reach into the stub section. */
12649 if (!stubs_always_before_branch && !big_sec)
12650 {
12651 total = 0;
12652 while (prev != NULL
12653 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12654 < (ppc64_elf_section_data (prev) != NULL
12655 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
12656 ? (group_size = stub_group_size >> 10)
12657 : group_size))
6f20ed8a 12658 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12659 {
12660 tail = prev;
6f20ed8a
AM
12661 prev = htab->sec_info[tail->id].u.list;
12662 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12663 }
12664 }
12665 tail = prev;
721956f4
AM
12666 }
12667 }
6f20ed8a 12668 return TRUE;
721956f4
AM
12669}
12670
58d180e8
AM
12671static const unsigned char glink_eh_frame_cie[] =
12672{
12673 0, 0, 0, 16, /* length. */
12674 0, 0, 0, 0, /* id. */
12675 1, /* CIE version. */
12676 'z', 'R', 0, /* Augmentation string. */
12677 4, /* Code alignment. */
12678 0x78, /* Data alignment. */
12679 65, /* RA reg. */
12680 1, /* Augmentation size. */
12681 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12682 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12683};
12684
d969d15f
AM
12685/* Stripping output sections is normally done before dynamic section
12686 symbols have been allocated. This function is called later, and
12687 handles cases like htab->brlt which is mapped to its own output
12688 section. */
12689
12690static void
12691maybe_strip_output (struct bfd_link_info *info, asection *isec)
12692{
12693 if (isec->size == 0
12694 && isec->output_section->size == 0
53d8967a 12695 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12696 && !bfd_section_removed_from_list (info->output_bfd,
12697 isec->output_section)
12698 && elf_section_data (isec->output_section)->dynindx == 0)
12699 {
12700 isec->output_section->flags |= SEC_EXCLUDE;
12701 bfd_section_list_remove (info->output_bfd, isec->output_section);
12702 info->output_bfd->section_count--;
12703 }
12704}
12705
721956f4
AM
12706/* Determine and set the size of the stub section for a final link.
12707
12708 The basic idea here is to examine all the relocations looking for
12709 PC-relative calls to a target that is unreachable with a "bl"
12710 instruction. */
12711
b34976b6 12712bfd_boolean
e7d1c40c 12713ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12714{
12715 bfd_size_type stub_group_size;
b34976b6 12716 bfd_boolean stubs_always_before_branch;
721956f4
AM
12717 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12718
4dfe6ac6
NC
12719 if (htab == NULL)
12720 return FALSE;
12721
0e1862bb 12722 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12723 htab->params->plt_thread_safe = 1;
b9e5796b 12724 if (!htab->opd_abi)
e7d1c40c
AM
12725 htab->params->plt_thread_safe = 0;
12726 else if (htab->params->plt_thread_safe == -1)
794e51c0 12727 {
e2458743 12728 static const char *const thread_starter[] =
794e51c0
AM
12729 {
12730 "pthread_create",
12731 /* libstdc++ */
12732 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12733 /* librt */
12734 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12735 "mq_notify", "create_timer",
12736 /* libanl */
12737 "getaddrinfo_a",
12738 /* libgomp */
2300b5a1 12739 "GOMP_parallel",
794e51c0 12740 "GOMP_parallel_start",
2300b5a1 12741 "GOMP_parallel_loop_static",
794e51c0 12742 "GOMP_parallel_loop_static_start",
2300b5a1 12743 "GOMP_parallel_loop_dynamic",
794e51c0 12744 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12745 "GOMP_parallel_loop_guided",
794e51c0 12746 "GOMP_parallel_loop_guided_start",
2300b5a1 12747 "GOMP_parallel_loop_runtime",
794e51c0 12748 "GOMP_parallel_loop_runtime_start",
2300b5a1 12749 "GOMP_parallel_sections",
68ffbac6 12750 "GOMP_parallel_sections_start",
f9dffbf0
AM
12751 /* libgo */
12752 "__go_go",
794e51c0
AM
12753 };
12754 unsigned i;
12755
a4b6fadd 12756 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12757 {
12758 struct elf_link_hash_entry *h;
12759 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12760 FALSE, FALSE, TRUE);
e7d1c40c
AM
12761 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12762 if (htab->params->plt_thread_safe)
794e51c0
AM
12763 break;
12764 }
12765 }
e7d1c40c
AM
12766 stubs_always_before_branch = htab->params->group_size < 0;
12767 if (htab->params->group_size < 0)
12768 stub_group_size = -htab->params->group_size;
721956f4 12769 else
e7d1c40c 12770 stub_group_size = htab->params->group_size;
721956f4 12771
6f20ed8a
AM
12772 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12773 return FALSE;
721956f4 12774
c9301e31
AM
12775#define STUB_SHRINK_ITER 20
12776 /* Loop until no stubs added. After iteration 20 of this loop we may
12777 exit on a stub section shrinking. This is to break out of a
12778 pathological case where adding stubs on one iteration decreases
12779 section gaps (perhaps due to alignment), which then requires
12780 fewer or smaller stubs on the next iteration. */
12781
721956f4
AM
12782 while (1)
12783 {
12784 bfd *input_bfd;
12785 unsigned int bfd_indx;
a4b6fadd 12786 struct map_stub *group;
721956f4
AM
12787
12788 htab->stub_iteration += 1;
721956f4
AM
12789
12790 for (input_bfd = info->input_bfds, bfd_indx = 0;
12791 input_bfd != NULL;
c72f2fb2 12792 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12793 {
12794 Elf_Internal_Shdr *symtab_hdr;
12795 asection *section;
6cdc0ccc 12796 Elf_Internal_Sym *local_syms = NULL;
721956f4 12797
0c8d6e5c 12798 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12799 continue;
12800
721956f4 12801 /* We'll need the symbol table in a second. */
0ffa91dd 12802 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12803 if (symtab_hdr->sh_info == 0)
12804 continue;
12805
721956f4
AM
12806 /* Walk over each section attached to the input bfd. */
12807 for (section = input_bfd->sections;
12808 section != NULL;
12809 section = section->next)
12810 {
721956f4 12811 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12812
12813 /* If there aren't any relocs, then there's nothing more
12814 to do. */
12815 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12816 || (section->flags & SEC_ALLOC) == 0
12817 || (section->flags & SEC_LOAD) == 0
12818 || (section->flags & SEC_CODE) == 0
721956f4
AM
12819 || section->reloc_count == 0)
12820 continue;
12821
12822 /* If this section is a link-once section that will be
12823 discarded, then don't create any stubs. */
12824 if (section->output_section == NULL
927be08e 12825 || section->output_section->owner != info->output_bfd)
721956f4
AM
12826 continue;
12827
1e2f5b6e
AM
12828 /* Get the relocs. */
12829 internal_relocs
4ce794b7 12830 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12831 info->keep_memory);
721956f4 12832 if (internal_relocs == NULL)
1e2f5b6e 12833 goto error_ret_free_local;
721956f4
AM
12834
12835 /* Now examine each relocation. */
12836 irela = internal_relocs;
12837 irelaend = irela + section->reloc_count;
12838 for (; irela < irelaend; irela++)
12839 {
4ce794b7
AM
12840 enum elf_ppc64_reloc_type r_type;
12841 unsigned int r_indx;
721956f4
AM
12842 enum ppc_stub_type stub_type;
12843 struct ppc_stub_hash_entry *stub_entry;
8387904d 12844 asection *sym_sec, *code_sec;
e054468f 12845 bfd_vma sym_value, code_value;
721956f4 12846 bfd_vma destination;
6911b7dc 12847 unsigned long local_off;
8843416a 12848 bfd_boolean ok_dest;
721956f4 12849 struct ppc_link_hash_entry *hash;
8387904d 12850 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12851 struct elf_link_hash_entry *h;
12852 Elf_Internal_Sym *sym;
721956f4
AM
12853 char *stub_name;
12854 const asection *id_sec;
74f0fb50 12855 struct _opd_sec_data *opd;
e054468f 12856 struct plt_entry *plt_ent;
721956f4
AM
12857
12858 r_type = ELF64_R_TYPE (irela->r_info);
12859 r_indx = ELF64_R_SYM (irela->r_info);
12860
4ce794b7 12861 if (r_type >= R_PPC64_max)
721956f4
AM
12862 {
12863 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12864 goto error_ret_free_internal;
721956f4
AM
12865 }
12866
12867 /* Only look for stubs on branch instructions. */
4ce794b7 12868 if (r_type != R_PPC64_REL24
05d0e962 12869 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
12870 && r_type != R_PPC64_REL14
12871 && r_type != R_PPC64_REL14_BRTAKEN
12872 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12873 continue;
12874
12875 /* Now determine the call target, its name, value,
12876 section. */
411e1bfb
AM
12877 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12878 r_indx, input_bfd))
12879 goto error_ret_free_internal;
12880 hash = (struct ppc_link_hash_entry *) h;
12881
8843416a 12882 ok_dest = FALSE;
8387904d 12883 fdh = NULL;
7fe2b9a6 12884 sym_value = 0;
411e1bfb 12885 if (hash == NULL)
721956f4 12886 {
411e1bfb 12887 sym_value = sym->st_value;
c27b8c2a
AM
12888 if (sym_sec != NULL
12889 && sym_sec->output_section != NULL)
12890 ok_dest = TRUE;
721956f4 12891 }
7fe2b9a6
AM
12892 else if (hash->elf.root.type == bfd_link_hash_defined
12893 || hash->elf.root.type == bfd_link_hash_defweak)
12894 {
12895 sym_value = hash->elf.root.u.def.value;
12896 if (sym_sec->output_section != NULL)
12897 ok_dest = TRUE;
12898 }
12899 else if (hash->elf.root.type == bfd_link_hash_undefweak
12900 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12901 {
99877b66 12902 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12903 use the func descriptor sym instead if it is
12904 defined. */
ceb1f1ef 12905 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12906 && hash->oh != NULL)
8387904d 12907 {
8c5b4e52 12908 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12909 if (fdh->elf.root.type == bfd_link_hash_defined
12910 || fdh->elf.root.type == bfd_link_hash_defweak)
12911 {
12912 sym_sec = fdh->elf.root.u.def.section;
12913 sym_value = fdh->elf.root.u.def.value;
12914 if (sym_sec->output_section != NULL)
12915 ok_dest = TRUE;
12916 }
99877b66
AM
12917 else
12918 fdh = NULL;
8387904d 12919 }
7fe2b9a6
AM
12920 }
12921 else
12922 {
12923 bfd_set_error (bfd_error_bad_value);
12924 goto error_ret_free_internal;
721956f4
AM
12925 }
12926
8843416a 12927 destination = 0;
6911b7dc 12928 local_off = 0;
8843416a
AM
12929 if (ok_dest)
12930 {
12931 sym_value += irela->r_addend;
12932 destination = (sym_value
12933 + sym_sec->output_offset
12934 + sym_sec->output_section->vma);
6911b7dc
AM
12935 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12936 ? hash->elf.other
12937 : sym->st_other);
8843416a
AM
12938 }
12939
8387904d 12940 code_sec = sym_sec;
e054468f 12941 code_value = sym_value;
74f0fb50
AM
12942 opd = get_opd_info (sym_sec);
12943 if (opd != NULL)
8387904d
AM
12944 {
12945 bfd_vma dest;
12946
74f0fb50 12947 if (hash == NULL && opd->adjust != NULL)
8387904d 12948 {
51aecdc5 12949 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12950 if (adjust == -1)
12951 continue;
e054468f 12952 code_value += adjust;
8387904d
AM
12953 sym_value += adjust;
12954 }
12955 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12956 &code_sec, &code_value, FALSE);
8387904d
AM
12957 if (dest != (bfd_vma) -1)
12958 {
12959 destination = dest;
12960 if (fdh != NULL)
12961 {
12962 /* Fixup old ABI sym to point at code
12963 entry. */
99877b66 12964 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12965 hash->elf.root.u.def.section = code_sec;
e054468f 12966 hash->elf.root.u.def.value = code_value;
8387904d
AM
12967 }
12968 }
12969 }
12970
721956f4 12971 /* Determine what (if any) linker stub is needed. */
e054468f 12972 plt_ent = NULL;
721956f4 12973 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12974 &plt_ent, destination,
12975 local_off);
ad8e1ba5 12976
05d0e962
AM
12977 if (r_type == R_PPC64_REL24_NOTOC)
12978 {
12979 if (stub_type == ppc_stub_plt_call)
12980 stub_type = ppc_stub_plt_call_notoc;
12981 else if (stub_type == ppc_stub_long_branch
12982 || (code_sec != NULL
12983 && code_sec->output_section != NULL
12984 && (((hash ? hash->elf.other : sym->st_other)
12985 & STO_PPC64_LOCAL_MASK)
4a4e7361 12986 > 1 << STO_PPC64_LOCAL_BIT)))
05d0e962
AM
12987 stub_type = ppc_stub_long_branch_notoc;
12988 }
12989 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
12990 {
12991 /* Check whether we need a TOC adjusting stub.
12992 Since the linker pastes together pieces from
12993 different object files when creating the
12994 _init and _fini functions, it may be that a
12995 call to what looks like a local sym is in
12996 fact a call needing a TOC adjustment. */
33cb30a1
AM
12997 if ((code_sec != NULL
12998 && code_sec->output_section != NULL
12999 && (htab->sec_info[code_sec->id].toc_off
13000 != htab->sec_info[section->id].toc_off)
13001 && (code_sec->has_toc_reloc
13002 || code_sec->makes_toc_func_call))
13003 || (((hash ? hash->elf.other : sym->st_other)
13004 & STO_PPC64_LOCAL_MASK)
13005 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
13006 stub_type = ppc_stub_long_branch_r2off;
13007 }
13008
721956f4
AM
13009 if (stub_type == ppc_stub_none)
13010 continue;
13011
411e1bfb
AM
13012 /* __tls_get_addr calls might be eliminated. */
13013 if (stub_type != ppc_stub_plt_call
05d0e962 13014 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 13015 && hash != NULL
8387904d
AM
13016 && (hash == htab->tls_get_addr
13017 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
13018 && section->has_tls_reloc
13019 && irela != internal_relocs)
13020 {
13021 /* Get tls info. */
f961d9dd 13022 unsigned char *tls_mask;
411e1bfb 13023
3a71aa26 13024 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
13025 irela - 1, input_bfd))
13026 goto error_ret_free_internal;
37da22e5 13027 if ((*tls_mask & TLS_TLS) != 0)
411e1bfb
AM
13028 continue;
13029 }
13030
f378ab09 13031 if (stub_type == ppc_stub_plt_call)
794e51c0 13032 {
6e1816be
AM
13033 if (!htab->opd_abi
13034 && htab->params->plt_localentry0 != 0
13035 && is_elfv2_localentry0 (&hash->elf))
13036 htab->has_plt_localentry0 = 1;
13037 else if (irela + 1 < irelaend
13038 && irela[1].r_offset == irela->r_offset + 4
13039 && (ELF64_R_TYPE (irela[1].r_info)
13040 == R_PPC64_TOCSAVE))
f378ab09
AM
13041 {
13042 if (!tocsave_find (htab, INSERT,
13043 &local_syms, irela + 1, input_bfd))
13044 goto error_ret_free_internal;
13045 }
f378ab09
AM
13046 else
13047 stub_type = ppc_stub_plt_call_r2save;
794e51c0 13048 }
3b421ab3 13049
721956f4 13050 /* Support for grouping stub sections. */
6f20ed8a 13051 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
13052
13053 /* Get the name of this stub. */
13054 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13055 if (!stub_name)
13056 goto error_ret_free_internal;
13057
13058 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 13059 stub_name, FALSE, FALSE);
721956f4
AM
13060 if (stub_entry != NULL)
13061 {
05d0e962
AM
13062 enum ppc_stub_type old_type;
13063 /* A stub has already been created, but it may
13064 not be the required type. We shouldn't be
13065 transitioning from plt_call to long_branch
13066 stubs or vice versa, but we might be
13067 upgrading from plt_call to plt_call_r2save or
13068 from long_branch to long_branch_r2off. */
721956f4 13069 free (stub_name);
05d0e962
AM
13070 old_type = stub_entry->stub_type;
13071 switch (old_type)
13072 {
13073 default:
13074 abort ();
13075
13076 case ppc_stub_save_res:
13077 continue;
13078
13079 case ppc_stub_plt_call:
13080 case ppc_stub_plt_call_r2save:
13081 case ppc_stub_plt_call_notoc:
13082 case ppc_stub_plt_call_both:
13083 if (stub_type == ppc_stub_plt_call)
13084 continue;
13085 else if (stub_type == ppc_stub_plt_call_r2save)
13086 {
13087 if (old_type == ppc_stub_plt_call_notoc)
13088 stub_type = ppc_stub_plt_call_both;
13089 }
13090 else if (stub_type == ppc_stub_plt_call_notoc)
13091 {
13092 if (old_type == ppc_stub_plt_call_r2save)
13093 stub_type = ppc_stub_plt_call_both;
13094 }
13095 else
13096 abort ();
13097 break;
13098
13099 case ppc_stub_plt_branch:
13100 case ppc_stub_plt_branch_r2off:
13101 case ppc_stub_plt_branch_notoc:
13102 case ppc_stub_plt_branch_both:
13103 old_type += (ppc_stub_long_branch
13104 - ppc_stub_plt_branch);
13105 /* Fall through. */
13106 case ppc_stub_long_branch:
13107 case ppc_stub_long_branch_r2off:
13108 case ppc_stub_long_branch_notoc:
13109 case ppc_stub_long_branch_both:
13110 if (stub_type == ppc_stub_long_branch)
13111 continue;
13112 else if (stub_type == ppc_stub_long_branch_r2off)
13113 {
13114 if (old_type == ppc_stub_long_branch_notoc)
13115 stub_type = ppc_stub_long_branch_both;
13116 }
13117 else if (stub_type == ppc_stub_long_branch_notoc)
13118 {
13119 if (old_type == ppc_stub_long_branch_r2off)
13120 stub_type = ppc_stub_long_branch_both;
13121 }
13122 else
13123 abort ();
13124 break;
13125 }
13126 if (old_type < stub_type)
794e51c0 13127 stub_entry->stub_type = stub_type;
721956f4
AM
13128 continue;
13129 }
13130
25f53a85 13131 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
13132 if (stub_entry == NULL)
13133 {
13134 free (stub_name);
6cdc0ccc
AM
13135 error_ret_free_internal:
13136 if (elf_section_data (section)->relocs == NULL)
13137 free (internal_relocs);
13138 error_ret_free_local:
13139 if (local_syms != NULL
13140 && (symtab_hdr->contents
13141 != (unsigned char *) local_syms))
13142 free (local_syms);
b34976b6 13143 return FALSE;
721956f4
AM
13144 }
13145
ad8e1ba5 13146 stub_entry->stub_type = stub_type;
05d0e962
AM
13147 if (stub_type >= ppc_stub_plt_call
13148 && stub_type <= ppc_stub_plt_call_both)
e054468f 13149 {
05d0e962
AM
13150 stub_entry->target_value = sym_value;
13151 stub_entry->target_section = sym_sec;
e054468f
AM
13152 }
13153 else
13154 {
05d0e962
AM
13155 stub_entry->target_value = code_value;
13156 stub_entry->target_section = code_sec;
e054468f 13157 }
721956f4 13158 stub_entry->h = hash;
e054468f 13159 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
13160 stub_entry->symtype
13161 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 13162 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 13163
3d58e1fc
AM
13164 if (hash != NULL
13165 && (hash->elf.root.type == bfd_link_hash_defined
13166 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 13167 htab->stub_globals += 1;
721956f4
AM
13168 }
13169
13170 /* We're done with the internal relocs, free them. */
6cdc0ccc 13171 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 13172 free (internal_relocs);
721956f4 13173 }
6cdc0ccc
AM
13174
13175 if (local_syms != NULL
13176 && symtab_hdr->contents != (unsigned char *) local_syms)
13177 {
13178 if (!info->keep_memory)
13179 free (local_syms);
13180 else
13181 symtab_hdr->contents = (unsigned char *) local_syms;
13182 }
721956f4
AM
13183 }
13184
5c3dead3 13185 /* We may have added some stubs. Find out the new size of the
721956f4 13186 stub sections. */
d4aaa2a0 13187 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13188 {
13189 group->lr_restore = 0;
13190 group->eh_size = 0;
13191 if (group->stub_sec != NULL)
13192 {
13193 asection *stub_sec = group->stub_sec;
13194
13195 if (htab->stub_iteration <= STUB_SHRINK_ITER
13196 || stub_sec->rawsize < stub_sec->size)
13197 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13198 stub_sec->rawsize = stub_sec->size;
13199 stub_sec->size = 0;
13200 stub_sec->reloc_count = 0;
13201 stub_sec->flags &= ~SEC_RELOC;
13202 }
13203 }
eea6121a 13204
ba21f564
AM
13205 if (htab->stub_iteration <= STUB_SHRINK_ITER
13206 || htab->brlt->rawsize < htab->brlt->size)
13207 htab->brlt->rawsize = htab->brlt->size;
eea6121a 13208 htab->brlt->size = 0;
84f5d08e
AM
13209 htab->brlt->reloc_count = 0;
13210 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 13211 if (htab->relbrlt != NULL)
eea6121a 13212 htab->relbrlt->size = 0;
721956f4 13213
63bc6f6c 13214 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 13215
a4b6fadd
AM
13216 for (group = htab->group; group != NULL; group = group->next)
13217 if (group->needs_save_res)
13218 group->stub_sec->size += htab->sfpr->size;
13219
176a0d42
AM
13220 if (info->emitrelocations
13221 && htab->glink != NULL && htab->glink->size != 0)
13222 {
13223 htab->glink->reloc_count = 1;
13224 htab->glink->flags |= SEC_RELOC;
13225 }
13226
58d180e8
AM
13227 if (htab->glink_eh_frame != NULL
13228 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 13229 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 13230 {
2e0ce1c8 13231 size_t size = 0, align = 4;
58d180e8 13232
d4aaa2a0 13233 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13234 if (group->eh_size != 0)
13235 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 13236 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 13237 size += (24 + align - 1) & -align;
58d180e8 13238 if (size != 0)
2e0ce1c8
AM
13239 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13240 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13241 size = (size + align - 1) & -align;
58d180e8
AM
13242 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13243 htab->glink_eh_frame->size = size;
13244 }
13245
e7d1c40c 13246 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13247 for (group = htab->group; group != NULL; group = group->next)
13248 if (group->stub_sec != NULL)
691d2e9a
AM
13249 {
13250 int align = abs (htab->params->plt_stub_align);
13251 group->stub_sec->size
13252 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13253 }
d4aaa2a0
AM
13254
13255 for (group = htab->group; group != NULL; group = group->next)
13256 if (group->stub_sec != NULL
13257 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 13258 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 13259 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
13260 break;
13261
d4aaa2a0 13262 if (group == NULL
ba21f564
AM
13263 && (htab->brlt->rawsize == htab->brlt->size
13264 || (htab->stub_iteration > STUB_SHRINK_ITER
13265 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
13266 && (htab->glink_eh_frame == NULL
13267 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
13268 break;
13269
721956f4 13270 /* Ask the linker to do its stuff. */
e7d1c40c 13271 (*htab->params->layout_sections_again) ();
721956f4
AM
13272 }
13273
da44f4e5
AM
13274 if (htab->glink_eh_frame != NULL
13275 && htab->glink_eh_frame->size != 0)
13276 {
13277 bfd_vma val;
13278 bfd_byte *p, *last_fde;
13279 size_t last_fde_len, size, align, pad;
d4aaa2a0 13280 struct map_stub *group;
da44f4e5 13281
df136d64
AM
13282 /* It is necessary to at least have a rough outline of the
13283 linker generated CIEs and FDEs written before
13284 bfd_elf_discard_info is run, in order for these FDEs to be
13285 indexed in .eh_frame_hdr. */
da44f4e5
AM
13286 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13287 if (p == NULL)
13288 return FALSE;
13289 htab->glink_eh_frame->contents = p;
13290 last_fde = p;
2e0ce1c8 13291 align = 4;
da44f4e5
AM
13292
13293 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13294 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 13295 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 13296 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 13297 p += last_fde_len + 4;
da44f4e5 13298
d4aaa2a0 13299 for (group = htab->group; group != NULL; group = group->next)
df136d64 13300 if (group->eh_size != 0)
da44f4e5 13301 {
df136d64 13302 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 13303 last_fde = p;
df136d64 13304 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 13305 /* FDE length. */
2e0ce1c8 13306 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13307 p += 4;
13308 /* CIE pointer. */
13309 val = p - htab->glink_eh_frame->contents;
13310 bfd_put_32 (htab->elf.dynobj, val, p);
13311 p += 4;
13312 /* Offset to stub section, written later. */
13313 p += 4;
13314 /* stub section size. */
d4aaa2a0 13315 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
13316 p += 4;
13317 /* Augmentation. */
13318 p += 1;
df136d64
AM
13319 /* Make sure we don't have all nops. This is enough for
13320 elf-eh-frame.c to detect the last non-nop opcode. */
13321 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 13322 p = last_fde + last_fde_len + 4;
da44f4e5
AM
13323 }
13324 if (htab->glink != NULL && htab->glink->size != 0)
13325 {
13326 last_fde = p;
2e0ce1c8 13327 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13328 /* FDE length. */
2e0ce1c8 13329 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13330 p += 4;
13331 /* CIE pointer. */
13332 val = p - htab->glink_eh_frame->contents;
13333 bfd_put_32 (htab->elf.dynobj, val, p);
13334 p += 4;
13335 /* Offset to .glink, written later. */
13336 p += 4;
13337 /* .glink size. */
13338 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13339 p += 4;
13340 /* Augmentation. */
13341 p += 1;
13342
13343 *p++ = DW_CFA_advance_loc + 1;
13344 *p++ = DW_CFA_register;
13345 *p++ = 65;
9f08fa5c 13346 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 13347 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
13348 *p++ = DW_CFA_restore_extended;
13349 *p++ = 65;
2e0ce1c8 13350 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13351 }
13352 /* Subsume any padding into the last FDE if user .eh_frame
13353 sections are aligned more than glink_eh_frame. Otherwise any
13354 zero padding will be seen as a terminator. */
2e0ce1c8 13355 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13356 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13357 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13358 htab->glink_eh_frame->size = size + pad;
13359 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13360 }
13361
d969d15f
AM
13362 maybe_strip_output (info, htab->brlt);
13363 if (htab->glink_eh_frame != NULL)
13364 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13365
b34976b6 13366 return TRUE;
721956f4
AM
13367}
13368
13369/* Called after we have determined section placement. If sections
805fc799 13370 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13371
805fc799 13372bfd_vma
1c865ab2 13373ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13374{
805fc799 13375 asection *s;
a27e685f 13376 bfd_vma TOCstart, adjust;
721956f4 13377
43417696
AM
13378 if (info != NULL)
13379 {
13380 struct elf_link_hash_entry *h;
13381 struct elf_link_hash_table *htab = elf_hash_table (info);
13382
13383 if (is_elf_hash_table (htab)
13384 && htab->hgot != NULL)
13385 h = htab->hgot;
13386 else
13387 {
13388 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13389 if (is_elf_hash_table (htab))
13390 htab->hgot = h;
13391 }
13392 if (h != NULL
13393 && h->root.type == bfd_link_hash_defined
13394 && !h->root.linker_def
13395 && (!is_elf_hash_table (htab)
13396 || h->def_regular))
13397 {
13398 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13399 + h->root.u.def.section->output_offset
13400 + h->root.u.def.section->output_section->vma);
13401 _bfd_set_gp_value (obfd, TOCstart);
13402 return TOCstart;
13403 }
13404 }
13405
805fc799
AM
13406 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13407 order. The TOC starts where the first of these sections starts. */
13408 s = bfd_get_section_by_name (obfd, ".got");
e054468f 13409 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13410 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 13411 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13412 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 13413 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13414 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 13415 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
13416 {
13417 /* This may happen for
13418 o references to TOC base (SYM@toc / TOC[tc0]) without a
13419 .toc directive
13420 o bad linker script
13421 o --gc-sections and empty TOC sections
13422
13423 FIXME: Warn user? */
13424
13425 /* Look for a likely section. We probably won't even be
13426 using TOCstart. */
13427 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13428 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13429 | SEC_EXCLUDE))
805fc799
AM
13430 == (SEC_ALLOC | SEC_SMALL_DATA))
13431 break;
721956f4 13432 if (s == NULL)
805fc799 13433 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13434 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
13435 == (SEC_ALLOC | SEC_SMALL_DATA))
13436 break;
721956f4 13437 if (s == NULL)
805fc799 13438 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13439 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13440 == SEC_ALLOC)
805fc799 13441 break;
721956f4 13442 if (s == NULL)
805fc799 13443 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13444 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
13445 break;
13446 }
721956f4 13447
805fc799
AM
13448 TOCstart = 0;
13449 if (s != NULL)
13450 TOCstart = s->output_section->vma + s->output_offset;
721956f4 13451
a27e685f
AM
13452 /* Force alignment. */
13453 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13454 TOCstart -= adjust;
1c865ab2
AM
13455 _bfd_set_gp_value (obfd, TOCstart);
13456
810d4e75 13457 if (info != NULL && s != NULL)
1c865ab2
AM
13458 {
13459 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13460
810d4e75
AM
13461 if (htab != NULL)
13462 {
13463 if (htab->elf.hgot != NULL)
13464 {
a27e685f 13465 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
13466 htab->elf.hgot->root.u.def.section = s;
13467 }
13468 }
13469 else
1c865ab2 13470 {
810d4e75
AM
13471 struct bfd_link_hash_entry *bh = NULL;
13472 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
13473 s, TOC_BASE_OFF - adjust,
13474 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
13475 }
13476 }
805fc799 13477 return TOCstart;
721956f4
AM
13478}
13479
a345bc8d 13480/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 13481 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
13482
13483static bfd_boolean
49c09209 13484build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
13485{
13486 struct bfd_link_info *info;
13487 struct ppc_link_hash_table *htab;
49c09209 13488 struct plt_entry *ent;
a345bc8d
AM
13489 asection *s;
13490
13491 if (h->root.type == bfd_link_hash_indirect)
13492 return TRUE;
13493
49c09209
AM
13494 info = inf;
13495 htab = ppc_hash_table (info);
13496 if (htab == NULL)
13497 return FALSE;
13498
13499 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13500 if (ent->plt.offset != (bfd_vma) -1)
13501 {
13502 /* This symbol has an entry in the procedure linkage
13503 table. Set it up. */
13504 Elf_Internal_Rela rela;
2d7ad24e 13505 asection *plt, *relplt;
49c09209
AM
13506 bfd_byte *loc;
13507
13508 if (!htab->elf.dynamic_sections_created
13509 || h->dynindx == -1)
13510 {
13511 if (!(h->def_regular
13512 && (h->root.type == bfd_link_hash_defined
13513 || h->root.type == bfd_link_hash_defweak)))
13514 continue;
2d7ad24e
AM
13515 if (h->type == STT_GNU_IFUNC)
13516 {
13517 plt = htab->elf.iplt;
13518 relplt = htab->elf.irelplt;
13519 htab->local_ifunc_resolver = 1;
13520 if (htab->opd_abi)
13521 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13522 else
13523 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13524 }
49c09209 13525 else
2d7ad24e
AM
13526 {
13527 plt = htab->pltlocal;
13528 if (bfd_link_pic (info))
13529 {
13530 relplt = htab->relpltlocal;
13531 if (htab->opd_abi)
13532 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13533 else
13534 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13535 }
13536 else
13537 relplt = NULL;
13538 }
49c09209
AM
13539 rela.r_addend = (h->root.u.def.value
13540 + h->root.u.def.section->output_offset
13541 + h->root.u.def.section->output_section->vma
13542 + ent->addend);
2d7ad24e
AM
13543
13544 if (relplt == NULL)
13545 {
13546 loc = plt->contents + ent->plt.offset;
13547 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13548 if (htab->opd_abi)
13549 {
13550 bfd_vma toc = elf_gp (info->output_bfd);
13551 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13552 bfd_put_64 (info->output_bfd, toc, loc + 8);
13553 }
13554 }
13555 else
13556 {
13557 rela.r_offset = (plt->output_section->vma
13558 + plt->output_offset
13559 + ent->plt.offset);
13560 loc = relplt->contents + (relplt->reloc_count++
13561 * sizeof (Elf64_External_Rela));
13562 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13563 }
49c09209
AM
13564 }
13565 else
13566 {
13567 rela.r_offset = (htab->elf.splt->output_section->vma
13568 + htab->elf.splt->output_offset
13569 + ent->plt.offset);
13570 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13571 rela.r_addend = ent->addend;
13572 loc = (htab->elf.srelplt->contents
13573 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13574 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13575 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13576 htab->maybe_local_ifunc_resolver = 1;
2d7ad24e 13577 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 13578 }
49c09209
AM
13579 }
13580
a345bc8d
AM
13581 if (!h->pointer_equality_needed)
13582 return TRUE;
13583
13584 if (h->def_regular)
13585 return TRUE;
13586
9e390558 13587 s = htab->global_entry;
49c09209
AM
13588 if (s == NULL || s->size == 0)
13589 return TRUE;
13590
13591 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13592 if (ent->plt.offset != (bfd_vma) -1
13593 && ent->addend == 0)
a345bc8d
AM
13594 {
13595 bfd_byte *p;
13596 asection *plt;
13597 bfd_vma off;
13598
a345bc8d 13599 p = s->contents + h->root.u.def.value;
33e44f2e 13600 plt = htab->elf.splt;
a345bc8d
AM
13601 if (!htab->elf.dynamic_sections_created
13602 || h->dynindx == -1)
2d7ad24e
AM
13603 {
13604 if (h->type == STT_GNU_IFUNC)
13605 plt = htab->elf.iplt;
13606 else
13607 plt = htab->pltlocal;
13608 }
49c09209 13609 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
13610 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13611
13612 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13613 {
13614 info->callbacks->einfo
c1c8c1ef 13615 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
13616 h->root.root.string);
13617 bfd_set_error (bfd_error_bad_value);
13618 htab->stub_error = TRUE;
13619 }
13620
7341d5e2
AM
13621 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13622 if (htab->params->emit_stub_syms)
13623 {
13624 size_t len = strlen (h->root.root.string);
13625 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13626
13627 if (name == NULL)
13628 return FALSE;
13629
13630 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13631 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13632 if (h == NULL)
13633 return FALSE;
13634 if (h->root.type == bfd_link_hash_new)
13635 {
13636 h->root.type = bfd_link_hash_defined;
13637 h->root.u.def.section = s;
13638 h->root.u.def.value = p - s->contents;
13639 h->ref_regular = 1;
13640 h->def_regular = 1;
13641 h->ref_regular_nonweak = 1;
13642 h->forced_local = 1;
13643 h->non_elf = 0;
2ec55de3 13644 h->root.linker_def = 1;
7341d5e2
AM
13645 }
13646 }
13647
a345bc8d
AM
13648 if (PPC_HA (off) != 0)
13649 {
13650 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13651 p += 4;
13652 }
13653 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13654 p += 4;
13655 bfd_put_32 (s->owner, MTCTR_R12, p);
13656 p += 4;
407aa07c 13657 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
13658 break;
13659 }
13660 return TRUE;
13661}
13662
49c09209
AM
13663/* Write PLT relocs for locals. */
13664
13665static bfd_boolean
13666write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13667{
13668 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13669 bfd *ibfd;
13670
13671 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13672 {
13673 struct got_entry **lgot_ents, **end_lgot_ents;
13674 struct plt_entry **local_plt, **lplt, **end_local_plt;
13675 Elf_Internal_Shdr *symtab_hdr;
13676 bfd_size_type locsymcount;
13677 Elf_Internal_Sym *local_syms = NULL;
13678 struct plt_entry *ent;
13679
13680 if (!is_ppc64_elf (ibfd))
13681 continue;
13682
13683 lgot_ents = elf_local_got_ents (ibfd);
13684 if (!lgot_ents)
13685 continue;
13686
13687 symtab_hdr = &elf_symtab_hdr (ibfd);
13688 locsymcount = symtab_hdr->sh_info;
13689 end_lgot_ents = lgot_ents + locsymcount;
13690 local_plt = (struct plt_entry **) end_lgot_ents;
13691 end_local_plt = local_plt + locsymcount;
13692 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13693 for (ent = *lplt; ent != NULL; ent = ent->next)
13694 if (ent->plt.offset != (bfd_vma) -1)
13695 {
13696 Elf_Internal_Sym *sym;
13697 asection *sym_sec;
13698 asection *plt, *relplt;
13699 bfd_byte *loc;
13700 bfd_vma val;
49c09209
AM
13701
13702 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13703 lplt - local_plt, ibfd))
13704 {
13705 if (local_syms != NULL
13706 && symtab_hdr->contents != (unsigned char *) local_syms)
13707 free (local_syms);
13708 return FALSE;
13709 }
13710
13711 val = sym->st_value + ent->addend;
ba85c15d
AM
13712 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13713 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
49c09209
AM
13714 if (sym_sec != NULL && sym_sec->output_section != NULL)
13715 val += sym_sec->output_offset + sym_sec->output_section->vma;
13716
2d7ad24e
AM
13717 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13718 {
13719 htab->local_ifunc_resolver = 1;
13720 plt = htab->elf.iplt;
13721 relplt = htab->elf.irelplt;
13722 }
13723 else
13724 {
13725 plt = htab->pltlocal;
13726 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13727 }
49c09209 13728
2d7ad24e
AM
13729 if (relplt == NULL)
13730 {
13731 loc = plt->contents + ent->plt.offset;
13732 bfd_put_64 (info->output_bfd, val, loc);
13733 if (htab->opd_abi)
13734 {
13735 bfd_vma toc = elf_gp (ibfd);
13736 bfd_put_64 (info->output_bfd, toc, loc + 8);
13737 }
13738 }
49c09209 13739 else
2d7ad24e
AM
13740 {
13741 Elf_Internal_Rela rela;
13742 rela.r_offset = (ent->plt.offset
13743 + plt->output_offset
13744 + plt->output_section->vma);
13745 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13746 {
13747 if (htab->opd_abi)
13748 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13749 else
13750 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13751 }
13752 else
13753 {
13754 if (htab->opd_abi)
13755 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13756 else
13757 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13758 }
13759 rela.r_addend = val;
13760 loc = relplt->contents + (relplt->reloc_count++
13761 * sizeof (Elf64_External_Rela));
13762 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13763 }
49c09209
AM
13764 }
13765
13766 if (local_syms != NULL
13767 && symtab_hdr->contents != (unsigned char *) local_syms)
13768 {
13769 if (!info->keep_memory)
13770 free (local_syms);
13771 else
13772 symtab_hdr->contents = (unsigned char *) local_syms;
13773 }
13774 }
13775 return TRUE;
13776}
13777
721956f4
AM
13778/* Build all the stubs associated with the current output file.
13779 The stubs are kept in a hash table attached to the main linker
13780 hash table. This function is called via gldelf64ppc_finish. */
13781
b34976b6 13782bfd_boolean
e7d1c40c 13783ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13784 char **stats)
5d1634d7
AM
13785{
13786 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13787 struct map_stub *group;
721956f4 13788 asection *stub_sec;
5d1634d7 13789 bfd_byte *p;
e717da7e 13790 int stub_sec_count = 0;
5d1634d7 13791
4dfe6ac6
NC
13792 if (htab == NULL)
13793 return FALSE;
13794
eea6121a 13795 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 13796 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13797 {
13798 group->eh_size = 0;
13799 group->lr_restore = 0;
13800 if ((stub_sec = group->stub_sec) != NULL
13801 && stub_sec->size != 0)
13802 {
13803 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13804 stub_sec->size);
13805 if (stub_sec->contents == NULL)
13806 return FALSE;
13807 stub_sec->size = 0;
13808 }
13809 }
5d1634d7 13810
23eb7e01 13811 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13812 {
9f951329 13813 unsigned int indx;
ad8e1ba5 13814 bfd_vma plt0;
9f951329 13815
721956f4 13816 /* Build the .glink plt call stub. */
e7d1c40c 13817 if (htab->params->emit_stub_syms)
97b639ba
AM
13818 {
13819 struct elf_link_hash_entry *h;
468392fb
AM
13820 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13821 TRUE, FALSE, FALSE);
97b639ba
AM
13822 if (h == NULL)
13823 return FALSE;
13824 if (h->root.type == bfd_link_hash_new)
13825 {
13826 h->root.type = bfd_link_hash_defined;
13827 h->root.u.def.section = htab->glink;
ee4bf8d2 13828 h->root.u.def.value = 8;
f5385ebf
AM
13829 h->ref_regular = 1;
13830 h->def_regular = 1;
13831 h->ref_regular_nonweak = 1;
13832 h->forced_local = 1;
13833 h->non_elf = 0;
2ec55de3 13834 h->root.linker_def = 1;
97b639ba
AM
13835 }
13836 }
33e44f2e
AM
13837 plt0 = (htab->elf.splt->output_section->vma
13838 + htab->elf.splt->output_offset
13839 - 16);
176a0d42
AM
13840 if (info->emitrelocations)
13841 {
13842 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13843 if (r == NULL)
13844 return FALSE;
13845 r->r_offset = (htab->glink->output_offset
13846 + htab->glink->output_section->vma);
13847 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13848 r->r_addend = plt0;
13849 }
4ce794b7 13850 p = htab->glink->contents;
176a0d42 13851 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13852 bfd_put_64 (htab->glink->owner, plt0, p);
13853 p += 8;
b9e5796b
AM
13854 if (htab->opd_abi)
13855 {
13856 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13857 p += 4;
13858 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13859 p += 4;
13860 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13861 p += 4;
13862 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13863 p += 4;
13864 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13865 p += 4;
13866 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13867 p += 4;
13868 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13869 p += 4;
13870 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13871 p += 4;
13872 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13873 p += 4;
13874 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13875 p += 4;
13876 }
13877 else
13878 {
13879 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13880 p += 4;
13881 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13882 p += 4;
13883 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13884 p += 4;
f378ab09
AM
13885 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13886 p += 4;
b9e5796b
AM
13887 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13888 p += 4;
13889 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13890 p += 4;
13891 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13892 p += 4;
13893 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13894 p += 4;
13895 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13896 p += 4;
13897 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13898 p += 4;
13899 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13900 p += 4;
13901 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13902 p += 4;
13903 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13904 p += 4;
13905 }
407aa07c
AM
13906 bfd_put_32 (htab->glink->owner, BCTR, p);
13907 p += 4;
c75bc4f7 13908 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 13909
9f951329
AM
13910 /* Build the .glink lazy link call stubs. */
13911 indx = 0;
9e390558 13912 while (p < htab->glink->contents + htab->glink->size)
9f951329 13913 {
b9e5796b 13914 if (htab->opd_abi)
9f951329 13915 {
b9e5796b
AM
13916 if (indx < 0x8000)
13917 {
13918 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13919 p += 4;
13920 }
13921 else
13922 {
13923 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13924 p += 4;
13925 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13926 p);
13927 p += 4;
13928 }
9f951329 13929 }
4ce794b7 13930 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13931 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13932 indx++;
9f951329
AM
13933 p += 4;
13934 }
5d1634d7 13935 }
5d1634d7 13936
49c09209
AM
13937 /* Build .glink global entry stubs, and PLT relocs for globals. */
13938 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13939
13940 if (!write_plt_relocs_for_local_syms (info))
13941 return FALSE;
9e390558 13942
7341d5e2 13943 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13944 {
4ce794b7 13945 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13946 htab->brlt->size);
4ce794b7 13947 if (htab->brlt->contents == NULL)
b34976b6 13948 return FALSE;
721956f4 13949 }
ee75fd95 13950 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13951 {
13952 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13953 htab->relbrlt->size);
63bc6f6c
AM
13954 if (htab->relbrlt->contents == NULL)
13955 return FALSE;
13956 }
5d1634d7 13957
721956f4
AM
13958 /* Build the stubs as directed by the stub hash table. */
13959 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13960
a4b6fadd
AM
13961 for (group = htab->group; group != NULL; group = group->next)
13962 if (group->needs_save_res)
7dda8d3c 13963 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 13964
aa8a7074
AM
13965 if (htab->relbrlt != NULL)
13966 htab->relbrlt->reloc_count = 0;
13967
e7d1c40c 13968 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13969 for (group = htab->group; group != NULL; group = group->next)
13970 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
13971 {
13972 int align = abs (htab->params->plt_stub_align);
13973 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13974 }
794e51c0 13975
7dda8d3c
AM
13976 for (group = htab->group; group != NULL; group = group->next)
13977 if (group->needs_save_res)
13978 {
13979 stub_sec = group->stub_sec;
13980 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13981 htab->sfpr->contents, htab->sfpr->size);
13982 if (htab->params->emit_stub_syms)
13983 {
13984 unsigned int i;
13985
13986 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13987 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13988 return FALSE;
13989 }
13990 }
13991
df136d64
AM
13992 if (htab->glink_eh_frame != NULL
13993 && htab->glink_eh_frame->size != 0)
13994 {
13995 bfd_vma val;
13996 size_t align = 4;
13997
13998 p = htab->glink_eh_frame->contents;
13999 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14000
14001 for (group = htab->group; group != NULL; group = group->next)
14002 if (group->eh_size != 0)
14003 {
14004 /* Offset to stub section. */
14005 val = (group->stub_sec->output_section->vma
14006 + group->stub_sec->output_offset);
14007 val -= (htab->glink_eh_frame->output_section->vma
14008 + htab->glink_eh_frame->output_offset
14009 + (p + 8 - htab->glink_eh_frame->contents));
14010 if (val + 0x80000000 > 0xffffffff)
14011 {
14012 _bfd_error_handler
14013 (_("%s offset too large for .eh_frame sdata4 encoding"),
14014 group->stub_sec->name);
14015 return FALSE;
14016 }
14017 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14018 p += (group->eh_size + 17 + 3) & -4;
14019 }
14020 if (htab->glink != NULL && htab->glink->size != 0)
14021 {
14022 /* Offset to .glink. */
14023 val = (htab->glink->output_section->vma
14024 + htab->glink->output_offset
14025 + 8);
14026 val -= (htab->glink_eh_frame->output_section->vma
14027 + htab->glink_eh_frame->output_offset
14028 + (p + 8 - htab->glink_eh_frame->contents));
14029 if (val + 0x80000000 > 0xffffffff)
14030 {
14031 _bfd_error_handler
14032 (_("%s offset too large for .eh_frame sdata4 encoding"),
14033 htab->glink->name);
14034 return FALSE;
14035 }
14036 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14037 p += (24 + align - 1) & -align;
14038 }
14039 }
14040
d4aaa2a0
AM
14041 for (group = htab->group; group != NULL; group = group->next)
14042 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
14043 {
14044 stub_sec_count += 1;
c9301e31
AM
14045 if (stub_sec->rawsize != stub_sec->size
14046 && (htab->stub_iteration <= STUB_SHRINK_ITER
14047 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
14048 break;
14049 }
5d1634d7 14050
25516cc5 14051 if (group != NULL)
5d1634d7 14052 {
b34976b6 14053 htab->stub_error = TRUE;
cf97bcb0 14054 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 14055 }
721956f4 14056
d2a300cf
AM
14057 if (htab->stub_error)
14058 return FALSE;
14059
14060 if (stats != NULL)
14061 {
db84fff3 14062 size_t len;
d2a300cf
AM
14063 *stats = bfd_malloc (500);
14064 if (*stats == NULL)
14065 return FALSE;
14066
db84fff3
AM
14067 len = sprintf (*stats,
14068 ngettext ("linker stubs in %u group\n",
14069 "linker stubs in %u groups\n",
14070 stub_sec_count),
14071 stub_sec_count);
05d0e962
AM
14072 sprintf (*stats + len, _(" branch %lu\n"
14073 " branch toc adj %lu\n"
14074 " branch notoc %lu\n"
14075 " branch both %lu\n"
14076 " long branch %lu\n"
14077 " long toc adj %lu\n"
14078 " long notoc %lu\n"
14079 " long both %lu\n"
14080 " plt call %lu\n"
14081 " plt call save %lu\n"
14082 " plt call notoc %lu\n"
14083 " plt call both %lu\n"
14084 " global entry %lu"),
4ce794b7
AM
14085 htab->stub_count[ppc_stub_long_branch - 1],
14086 htab->stub_count[ppc_stub_long_branch_r2off - 1],
05d0e962
AM
14087 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14088 htab->stub_count[ppc_stub_long_branch_both - 1],
4ce794b7
AM
14089 htab->stub_count[ppc_stub_plt_branch - 1],
14090 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
05d0e962
AM
14091 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14092 htab->stub_count[ppc_stub_plt_branch_both - 1],
794e51c0 14093 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2 14094 htab->stub_count[ppc_stub_plt_call_r2save - 1],
05d0e962
AM
14095 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14096 htab->stub_count[ppc_stub_plt_call_both - 1],
7341d5e2 14097 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
14098 }
14099 return TRUE;
5bd4f169
AM
14100}
14101
60124e18
AM
14102/* What to do when ld finds relocations against symbols defined in
14103 discarded sections. */
14104
14105static unsigned int
14106ppc64_elf_action_discarded (asection *sec)
14107{
14108 if (strcmp (".opd", sec->name) == 0)
14109 return 0;
14110
14111 if (strcmp (".toc", sec->name) == 0)
14112 return 0;
14113
bce50a28
JJ
14114 if (strcmp (".toc1", sec->name) == 0)
14115 return 0;
14116
60124e18
AM
14117 return _bfd_elf_default_action_discarded (sec);
14118}
14119
5bd4f169
AM
14120/* The RELOCATE_SECTION function is called by the ELF backend linker
14121 to handle the relocations for a section.
14122
14123 The relocs are always passed as Rela structures; if the section
14124 actually uses Rel structures, the r_addend field will always be
14125 zero.
14126
14127 This function is responsible for adjust the section contents as
14128 necessary, and (if using Rela relocs and generating a
1049f94e 14129 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
14130 necessary.
14131
14132 This function does not have to worry about setting the reloc
14133 address or the reloc symbol index.
14134
14135 LOCAL_SYMS is a pointer to the swapped in local symbols.
14136
14137 LOCAL_SECTIONS is an array giving the section in the input file
14138 corresponding to the st_shndx field of each local symbol.
14139
14140 The global hash table entry for the global symbols can be found
14141 via elf_sym_hashes (input_bfd).
14142
1049f94e 14143 When generating relocatable output, this function must handle
5bd4f169
AM
14144 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14145 going to be the section symbol corresponding to the output
14146 section, which means that the addend must be adjusted
14147 accordingly. */
14148
b34976b6 14149static bfd_boolean
4ce794b7
AM
14150ppc64_elf_relocate_section (bfd *output_bfd,
14151 struct bfd_link_info *info,
14152 bfd *input_bfd,
14153 asection *input_section,
14154 bfd_byte *contents,
14155 Elf_Internal_Rela *relocs,
14156 Elf_Internal_Sym *local_syms,
14157 asection **local_sections)
5bd4f169 14158{
65f38f15 14159 struct ppc_link_hash_table *htab;
5bd4f169
AM
14160 Elf_Internal_Shdr *symtab_hdr;
14161 struct elf_link_hash_entry **sym_hashes;
5bd4f169 14162 Elf_Internal_Rela *rel;
c316a17c 14163 Elf_Internal_Rela *wrel;
5bd4f169 14164 Elf_Internal_Rela *relend;
411e1bfb
AM
14165 Elf_Internal_Rela outrel;
14166 bfd_byte *loc;
411e1bfb 14167 struct got_entry **local_got_ents;
5bd4f169 14168 bfd_vma TOCstart;
b34976b6
AM
14169 bfd_boolean ret = TRUE;
14170 bfd_boolean is_opd;
794e51c0
AM
14171 /* Assume 'at' branch hints. */
14172 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 14173 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 14174
65f38f15 14175 /* Initialize howto table if needed. */
5bd4f169 14176 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
14177 ppc_howto_init ();
14178
65f38f15 14179 htab = ppc_hash_table (info);
4dfe6ac6
NC
14180 if (htab == NULL)
14181 return FALSE;
ee75fd95
AM
14182
14183 /* Don't relocate stub sections. */
e7d1c40c 14184 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
14185 return TRUE;
14186
7af5d5c4
AM
14187 if (!is_ppc64_elf (input_bfd))
14188 {
14189 bfd_set_error (bfd_error_wrong_format);
14190 return FALSE;
14191 }
0ffa91dd 14192
411e1bfb 14193 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 14194 TOCstart = elf_gp (output_bfd);
0ffa91dd 14195 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 14196 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 14197 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 14198
c316a17c 14199 rel = wrel = relocs;
5bd4f169 14200 relend = relocs + input_section->reloc_count;
c316a17c 14201 for (; rel < relend; wrel++, rel++)
5bd4f169 14202 {
04c9666a 14203 enum elf_ppc64_reloc_type r_type;
31c76678 14204 bfd_vma addend;
5bd4f169
AM
14205 bfd_reloc_status_type r;
14206 Elf_Internal_Sym *sym;
14207 asection *sec;
039b3fef
AM
14208 struct elf_link_hash_entry *h_elf;
14209 struct ppc_link_hash_entry *h;
14210 struct ppc_link_hash_entry *fdh;
5bd4f169 14211 const char *sym_name;
0d4792f7 14212 unsigned long r_symndx, toc_symndx;
3a71aa26 14213 bfd_vma toc_addend;
f961d9dd
AM
14214 unsigned char tls_mask, tls_gd, tls_type;
14215 unsigned char sym_type;
5bd4f169 14216 bfd_vma relocation;
23cedd1d 14217 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 14218 bfd_boolean warned;
bc30df16 14219 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 14220 unsigned int insn;
e11840f9 14221 unsigned int mask;
721956f4
AM
14222 struct ppc_stub_hash_entry *stub_entry;
14223 bfd_vma max_br_offset;
14224 bfd_vma from;
c316a17c 14225 Elf_Internal_Rela orig_rel;
b80eed39
AM
14226 reloc_howto_type *howto;
14227 struct reloc_howto_struct alt_howto;
4a421c53
AM
14228 uint64_t pinsn;
14229 bfd_vma offset;
5bd4f169 14230
c316a17c
AM
14231 again:
14232 orig_rel = *rel;
14233
4ce794b7 14234 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 14235 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
14236
14237 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14238 symbol of the previous ADDR64 reloc. The symbol gives us the
14239 proper TOC base to use. */
14240 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
14241 && wrel != relocs
14242 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 14243 && is_opd)
c316a17c 14244 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 14245
4ce794b7
AM
14246 sym = NULL;
14247 sec = NULL;
039b3fef 14248 h_elf = NULL;
4ce794b7 14249 sym_name = NULL;
b34976b6
AM
14250 unresolved_reloc = FALSE;
14251 warned = FALSE;
65f38f15 14252
0b13192e 14253 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
14254 {
14255 /* It's a local symbol. */
74f0fb50 14256 struct _opd_sec_data *opd;
4025353c 14257
5bd4f169
AM
14258 sym = local_syms + r_symndx;
14259 sec = local_sections[r_symndx];
26c61ae5 14260 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 14261 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 14262 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
14263 opd = get_opd_info (sec);
14264 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 14265 {
51aecdc5
AM
14266 long adjust = opd->adjust[OPD_NDX (sym->st_value
14267 + rel->r_addend)];
4025353c
AM
14268 if (adjust == -1)
14269 relocation = 0;
14270 else
4cc603a5
AM
14271 {
14272 /* If this is a relocation against the opd section sym
14273 and we have edited .opd, adjust the reloc addend so
14274 that ld -r and ld --emit-relocs output is correct.
14275 If it is a reloc against some other .opd symbol,
14276 then the symbol value will be adjusted later. */
14277 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14278 rel->r_addend += adjust;
14279 else
14280 relocation += adjust;
14281 }
1e2f5b6e 14282 }
5bd4f169
AM
14283 }
14284 else
14285 {
62d887d4
L
14286 bfd_boolean ignored;
14287
b2a8e766
AM
14288 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14289 r_symndx, symtab_hdr, sym_hashes,
039b3fef 14290 h_elf, sec, relocation,
62d887d4 14291 unresolved_reloc, warned, ignored);
039b3fef
AM
14292 sym_name = h_elf->root.root.string;
14293 sym_type = h_elf->type;
b69fdb4e
AM
14294 if (sec != NULL
14295 && sec->owner == output_bfd
14296 && strcmp (sec->name, ".opd") == 0)
14297 {
14298 /* This is a symbol defined in a linker script. All
14299 such are defined in output sections, even those
14300 defined by simple assignment from a symbol defined in
14301 an input section. Transfer the symbol to an
14302 appropriate input .opd section, so that a branch to
14303 this symbol will be mapped to the location specified
14304 by the opd entry. */
14305 struct bfd_link_order *lo;
14306 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14307 if (lo->type == bfd_indirect_link_order)
14308 {
14309 asection *isec = lo->u.indirect.section;
14310 if (h_elf->root.u.def.value >= isec->output_offset
14311 && h_elf->root.u.def.value < (isec->output_offset
14312 + isec->size))
14313 {
14314 h_elf->root.u.def.value -= isec->output_offset;
14315 h_elf->root.u.def.section = isec;
14316 sec = isec;
14317 break;
14318 }
14319 }
14320 }
5bd4f169 14321 }
039b3fef 14322 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 14323
dbaa2011 14324 if (sec != NULL && discarded_section (sec))
c316a17c
AM
14325 {
14326 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14327 input_bfd, input_section,
0930cb30 14328 contents, rel->r_offset);
c316a17c
AM
14329 wrel->r_offset = rel->r_offset;
14330 wrel->r_info = 0;
14331 wrel->r_addend = 0;
14332
14333 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 14334 symbols defined in discarded sections. Not done for
c316a17c
AM
14335 non-debug to preserve relocs in .eh_frame which the
14336 eh_frame editing code expects to be present. */
14337 if (bfd_link_relocatable (info)
14338 && (input_section->flags & SEC_DEBUGGING))
14339 wrel--;
14340
14341 continue;
14342 }
ab96bf03 14343
0e1862bb 14344 if (bfd_link_relocatable (info))
c316a17c 14345 goto copy_reloc;
ab96bf03 14346
f40da81b
AM
14347 if (h != NULL && &h->elf == htab->elf.hgot)
14348 {
6f20ed8a 14349 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
14350 sec = bfd_abs_section_ptr;
14351 unresolved_reloc = FALSE;
14352 }
14353
951fd09b
AM
14354 /* TLS optimizations. Replace instruction sequences and relocs
14355 based on information we collected in tls_optimize. We edit
14356 RELOCS so that --emit-relocs will output something sensible
14357 for the final instruction stream. */
14358 tls_mask = 0;
14359 tls_gd = 0;
0d4792f7 14360 toc_symndx = 0;
727fc41e
AM
14361 if (h != NULL)
14362 tls_mask = h->tls_mask;
14363 else if (local_got_ents != NULL)
411e1bfb 14364 {
e054468f
AM
14365 struct plt_entry **local_plt = (struct plt_entry **)
14366 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 14367 unsigned char *lgot_masks = (unsigned char *)
e054468f 14368 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
14369 tls_mask = lgot_masks[r_symndx];
14370 }
37da22e5 14371 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
14372 && (r_type == R_PPC64_TLS
14373 || r_type == R_PPC64_TLSGD
14374 || r_type == R_PPC64_TLSLD))
14375 {
14376 /* Check for toc tls entries. */
f961d9dd 14377 unsigned char *toc_tls;
0d4792f7 14378
727fc41e
AM
14379 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14380 &local_syms, rel, input_bfd))
14381 return FALSE;
0d4792f7 14382
727fc41e
AM
14383 if (toc_tls)
14384 tls_mask = *toc_tls;
0d4792f7
AM
14385 }
14386
14387 /* Check that tls relocs are used with tls syms, and non-tls
14388 relocs are used with non-tls syms. */
cf35638d 14389 if (r_symndx != STN_UNDEF
0d4792f7
AM
14390 && r_type != R_PPC64_NONE
14391 && (h == NULL
039b3fef
AM
14392 || h->elf.root.type == bfd_link_hash_defined
14393 || h->elf.root.type == bfd_link_hash_defweak)
71c4e95a 14394 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
0d4792f7 14395 {
37da22e5 14396 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
14397 && (r_type == R_PPC64_TLS
14398 || r_type == R_PPC64_TLSGD
14399 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
14400 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14401 ;
14402 else
25f53a85 14403 info->callbacks->einfo
1d483afe 14404 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 14405 /* xgettext:c-format */
c1c8c1ef 14406 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 14407 /* xgettext:c-format */
c1c8c1ef 14408 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 14409 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
14410 ppc64_elf_howto_table[r_type]->name,
14411 sym_name);
411e1bfb
AM
14412 }
14413
14414 /* Ensure reloc mapping code below stays sane. */
14415 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14416 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14417 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14418 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14419 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14420 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14421 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14422 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14423 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14424 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14425 abort ();
0d4792f7 14426
411e1bfb
AM
14427 switch (r_type)
14428 {
14429 default:
411e1bfb
AM
14430 break;
14431
ba761f19 14432 case R_PPC64_LO_DS_OPT:
95f0d0d2 14433 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
14434 if ((insn & (0x3f << 26)) != 58u << 26)
14435 abort ();
14436 insn += (14u << 26) - (58u << 26);
95f0d0d2 14437 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
14438 r_type = R_PPC64_TOC16_LO;
14439 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14440 break;
14441
411e1bfb
AM
14442 case R_PPC64_TOC16:
14443 case R_PPC64_TOC16_LO:
14444 case R_PPC64_TOC16_DS:
14445 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
14446 {
14447 /* Check for toc tls entries. */
f961d9dd 14448 unsigned char *toc_tls;
951fd09b 14449 int retval;
411e1bfb 14450
3a71aa26
AM
14451 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14452 &local_syms, rel, input_bfd);
951fd09b 14453 if (retval == 0)
411e1bfb
AM
14454 return FALSE;
14455
14456 if (toc_tls)
14457 {
951fd09b 14458 tls_mask = *toc_tls;
411e1bfb
AM
14459 if (r_type == R_PPC64_TOC16_DS
14460 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 14461 {
37da22e5 14462 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
14463 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14464 goto toctprel;
14465 }
411e1bfb 14466 else
951fd09b
AM
14467 {
14468 /* If we found a GD reloc pair, then we might be
14469 doing a GD->IE transition. */
14470 if (retval == 2)
14471 {
b00a0a86 14472 tls_gd = TLS_GDIE;
37da22e5
AM
14473 if ((tls_mask & TLS_TLS) != 0
14474 && (tls_mask & TLS_GD) == 0)
102890f0 14475 goto tls_ldgd_opt;
951fd09b
AM
14476 }
14477 else if (retval == 3)
14478 {
37da22e5
AM
14479 if ((tls_mask & TLS_TLS) != 0
14480 && (tls_mask & TLS_LD) == 0)
102890f0 14481 goto tls_ldgd_opt;
951fd09b
AM
14482 }
14483 }
411e1bfb
AM
14484 }
14485 }
14486 break;
14487
9d6ded02
AM
14488 case R_PPC64_GOT_TPREL16_HI:
14489 case R_PPC64_GOT_TPREL16_HA:
37da22e5 14490 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
14491 && (tls_mask & TLS_TPREL) == 0)
14492 {
14493 rel->r_offset -= d_offset;
95f0d0d2 14494 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
14495 r_type = R_PPC64_NONE;
14496 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14497 }
14498 break;
14499
411e1bfb
AM
14500 case R_PPC64_GOT_TPREL16_DS:
14501 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 14502 if ((tls_mask & TLS_TLS) != 0
951fd09b 14503 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14504 {
81407a69 14505 toctprel:
95f0d0d2 14506 insn = bfd_get_32 (input_bfd,
c316a17c 14507 contents + rel->r_offset - d_offset);
411e1bfb
AM
14508 insn &= 31 << 21;
14509 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 14510 bfd_put_32 (input_bfd, insn,
c316a17c 14511 contents + rel->r_offset - d_offset);
411e1bfb 14512 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
14513 if (toc_symndx != 0)
14514 {
14515 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14516 rel->r_addend = toc_addend;
0d4792f7
AM
14517 /* We changed the symbol. Start over in order to
14518 get h, sym, sec etc. right. */
c316a17c 14519 goto again;
0d4792f7
AM
14520 }
14521 else
14522 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14523 }
14524 break;
14525
c213164a
AM
14526 case R_PPC64_GOT_TPREL34:
14527 if ((tls_mask & TLS_TLS) != 0
14528 && (tls_mask & TLS_TPREL) == 0)
14529 {
14530 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
14531 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14532 pinsn <<= 32;
14533 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14534 pinsn += ((2ULL << 56) + (-1ULL << 52)
14535 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
14536 bfd_put_32 (input_bfd, pinsn >> 32,
14537 contents + rel->r_offset);
14538 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14539 contents + rel->r_offset + 4);
14540 r_type = R_PPC64_TPREL34;
14541 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14542 }
14543 break;
14544
411e1bfb 14545 case R_PPC64_TLS:
37da22e5 14546 if ((tls_mask & TLS_TLS) != 0
951fd09b 14547 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14548 {
c213164a 14549 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2d0f3896
AM
14550 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14551 if (insn == 0)
c213164a
AM
14552 break;
14553 if ((rel->r_offset & 3) == 0)
0d4792f7 14554 {
c213164a
AM
14555 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14556 /* Was PPC64_TLS which sits on insn boundary, now
14557 PPC64_TPREL16_LO which is at low-order half-word. */
14558 rel->r_offset += d_offset;
14559 r_type = R_PPC64_TPREL16_LO;
14560 if (toc_symndx != 0)
14561 {
14562 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14563 rel->r_addend = toc_addend;
14564 /* We changed the symbol. Start over in order to
14565 get h, sym, sec etc. right. */
14566 goto again;
14567 }
14568 else
14569 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14570 }
14571 else if ((rel->r_offset & 3) == 1)
14572 {
14573 /* For pcrel IE to LE we already have the full
14574 offset and thus don't need an addi here. A nop
14575 or mr will do. */
14576 if ((insn & (0x3f << 26)) == 14 << 26)
14577 {
14578 /* Extract regs from addi rt,ra,si. */
14579 unsigned int rt = (insn >> 21) & 0x1f;
14580 unsigned int ra = (insn >> 16) & 0x1f;
14581 if (rt == ra)
14582 insn = NOP;
14583 else
14584 {
14585 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
14586 insn = (rt << 16) | (ra << 21) | (ra << 11);
14587 insn |= (31u << 26) | (444u << 1);
14588 }
14589 }
14590 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
0d4792f7 14591 }
411e1bfb
AM
14592 }
14593 break;
14594
411e1bfb
AM
14595 case R_PPC64_GOT_TLSGD16_HI:
14596 case R_PPC64_GOT_TLSGD16_HA:
b00a0a86 14597 tls_gd = TLS_GDIE;
37da22e5 14598 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
14599 goto tls_gdld_hi;
14600 break;
14601
411e1bfb
AM
14602 case R_PPC64_GOT_TLSLD16_HI:
14603 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 14604 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 14605 {
951fd09b
AM
14606 tls_gdld_hi:
14607 if ((tls_mask & tls_gd) != 0)
14608 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14609 + R_PPC64_GOT_TPREL16_DS);
14610 else
411e1bfb 14611 {
4fe5ca5b 14612 rel->r_offset -= d_offset;
95f0d0d2 14613 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 14614 r_type = R_PPC64_NONE;
411e1bfb 14615 }
951fd09b 14616 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14617 }
14618 break;
14619
951fd09b
AM
14620 case R_PPC64_GOT_TLSGD16:
14621 case R_PPC64_GOT_TLSGD16_LO:
b00a0a86 14622 tls_gd = TLS_GDIE;
37da22e5 14623 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 14624 goto tls_ldgd_opt;
951fd09b 14625 break;
411e1bfb 14626
951fd09b
AM
14627 case R_PPC64_GOT_TLSLD16:
14628 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 14629 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 14630 {
b9f04fe0 14631 unsigned int insn1, insn2;
102890f0
AM
14632
14633 tls_ldgd_opt:
727fc41e
AM
14634 offset = (bfd_vma) -1;
14635 /* If not using the newer R_PPC64_TLSGD/LD to mark
14636 __tls_get_addr calls, we must trust that the call
14637 stays with its arg setup insns, ie. that the next
14638 reloc is the __tls_get_addr call associated with
14639 the current reloc. Edit both insns. */
14640 if (input_section->has_tls_get_addr_call
14641 && rel + 1 < relend
14642 && branch_reloc_hash_match (input_bfd, rel + 1,
14643 htab->tls_get_addr,
14644 htab->tls_get_addr_fd))
14645 offset = rel[1].r_offset;
b86ac8e3
AM
14646 /* We read the low GOT_TLS (or TOC16) insn because we
14647 need to keep the destination reg. It may be
14648 something other than the usual r3, and moved to r3
14649 before the call by intervening code. */
95f0d0d2 14650 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 14651 contents + rel->r_offset - d_offset);
102890f0 14652 if ((tls_mask & tls_gd) != 0)
411e1bfb 14653 {
102890f0 14654 /* IE */
b86ac8e3 14655 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
14656 insn1 |= 58 << 26; /* ld */
14657 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 14658 if (offset != (bfd_vma) -1)
f58d5a2d 14659 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
46e9995a
AM
14660 if (r_type == R_PPC64_TOC16
14661 || r_type == R_PPC64_TOC16_LO)
102890f0 14662 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
46e9995a
AM
14663 else
14664 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
14665 + R_PPC64_GOT_TPREL16_DS);
102890f0
AM
14666 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14667 }
14668 else
14669 {
14670 /* LE */
b86ac8e3
AM
14671 insn1 &= 0x1f << 21;
14672 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
14673 insn2 = 0x38630000; /* addi 3,3,0 */
14674 if (tls_gd == 0)
951fd09b 14675 {
102890f0 14676 /* Was an LD reloc. */
71c4e95a 14677 r_symndx = STN_UNDEF;
102890f0 14678 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 14679 }
102890f0 14680 else if (toc_symndx != 0)
3a71aa26
AM
14681 {
14682 r_symndx = toc_symndx;
14683 rel->r_addend = toc_addend;
14684 }
102890f0
AM
14685 r_type = R_PPC64_TPREL16_HA;
14686 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
14687 if (offset != (bfd_vma) -1)
14688 {
14689 rel[1].r_info = ELF64_R_INFO (r_symndx,
14690 R_PPC64_TPREL16_LO);
14691 rel[1].r_offset = offset + d_offset;
14692 rel[1].r_addend = rel->r_addend;
14693 }
102890f0 14694 }
95f0d0d2 14695 bfd_put_32 (input_bfd, insn1,
3a71aa26 14696 contents + rel->r_offset - d_offset);
727fc41e 14697 if (offset != (bfd_vma) -1)
c96e0573
AM
14698 {
14699 bfd_put_32 (input_bfd, insn2, contents + offset);
14700 if (offset + 8 <= input_section->size)
14701 {
14702 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14703 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14704 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14705 }
14706 }
727fc41e
AM
14707 if ((tls_mask & tls_gd) == 0
14708 && (tls_gd == 0 || toc_symndx != 0))
14709 {
14710 /* We changed the symbol. Start over in order
14711 to get h, sym, sec etc. right. */
c316a17c 14712 goto again;
727fc41e
AM
14713 }
14714 }
14715 break;
14716
c213164a
AM
14717 case R_PPC64_GOT_TLSGD34:
14718 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14719 {
14720 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14721 pinsn <<= 32;
14722 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14723 if ((tls_mask & TLS_GDIE) != 0)
14724 {
14725 /* IE, pla -> pld */
14726 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
14727 r_type = R_PPC64_GOT_TPREL34;
14728 }
14729 else
14730 {
14731 /* LE, pla pcrel -> paddi r13 */
14732 pinsn += (-1ULL << 52) + (13ULL << 16);
14733 r_type = R_PPC64_TPREL34;
14734 }
14735 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14736 bfd_put_32 (input_bfd, pinsn >> 32,
14737 contents + rel->r_offset);
14738 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14739 contents + rel->r_offset + 4);
14740 }
14741 break;
14742
14743 case R_PPC64_GOT_TLSLD34:
14744 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14745 {
14746 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14747 pinsn <<= 32;
14748 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14749 pinsn += (-1ULL << 52) + (13ULL << 16);
14750 bfd_put_32 (input_bfd, pinsn >> 32,
14751 contents + rel->r_offset);
14752 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14753 contents + rel->r_offset + 4);
14754 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14755 r_symndx = STN_UNDEF;
14756 r_type = R_PPC64_TPREL34;
14757 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14758 goto again;
14759 }
14760 break;
14761
727fc41e 14762 case R_PPC64_TLSGD:
37da22e5 14763 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 14764 && rel + 1 < relend)
727fc41e 14765 {
b9f04fe0 14766 unsigned int insn2;
5663e321 14767 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 14768
4a421c53 14769 offset = rel->r_offset;
5663e321 14770 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
14771 {
14772 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
14773 if (r_type1 == R_PPC64_PLT_PCREL34
14774 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14775 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
14776 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14777 break;
14778 }
14779
14780 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14781 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14782
b00a0a86 14783 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
14784 {
14785 /* IE */
14786 r_type = R_PPC64_NONE;
14787 insn2 = 0x7c636a14; /* add 3,3,13 */
14788 }
14789 else
14790 {
14791 /* LE */
14792 if (toc_symndx != 0)
14793 {
14794 r_symndx = toc_symndx;
14795 rel->r_addend = toc_addend;
14796 }
c213164a
AM
14797 if (r_type1 == R_PPC64_REL24_NOTOC
14798 || r_type1 == R_PPC64_PLTCALL_NOTOC)
14799 {
14800 r_type = R_PPC64_NONE;
14801 insn2 = NOP;
14802 }
14803 else
14804 {
14805 rel->r_offset = offset + d_offset;
14806 r_type = R_PPC64_TPREL16_LO;
14807 insn2 = 0x38630000; /* addi 3,3,0 */
14808 }
727fc41e
AM
14809 }
14810 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14811 /* Zap the reloc on the _tls_get_addr call too. */
14812 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14813 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 14814 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
14815 if ((tls_mask & TLS_GDIE) == 0
14816 && toc_symndx != 0
14817 && r_type != R_PPC64_NONE)
c316a17c 14818 goto again;
411e1bfb 14819 }
411e1bfb
AM
14820 break;
14821
727fc41e 14822 case R_PPC64_TLSLD:
37da22e5 14823 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 14824 && rel + 1 < relend)
727fc41e 14825 {
b9f04fe0 14826 unsigned int insn2;
5663e321 14827 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 14828
4a421c53 14829 offset = rel->r_offset;
5663e321 14830 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
14831 {
14832 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
14833 if (r_type1 == R_PPC64_PLT_PCREL34
14834 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14835 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
14836 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14837 break;
14838 }
14839
14840 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14841 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14842
c213164a
AM
14843 if (r_type1 == R_PPC64_REL24_NOTOC
14844 || r_type1 == R_PPC64_PLTCALL_NOTOC)
14845 {
14846 r_type = R_PPC64_NONE;
14847 insn2 = NOP;
14848 }
14849 else
14850 {
14851 rel->r_offset = offset + d_offset;
14852 r_symndx = STN_UNDEF;
14853 r_type = R_PPC64_TPREL16_LO;
14854 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14855 insn2 = 0x38630000; /* addi 3,3,0 */
14856 }
727fc41e 14857 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
14858 /* Zap the reloc on the _tls_get_addr call too. */
14859 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14860 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 14861 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
14862 if (r_type != R_PPC64_NONE)
14863 goto again;
727fc41e
AM
14864 }
14865 break;
14866
411e1bfb 14867 case R_PPC64_DTPMOD64:
951fd09b
AM
14868 if (rel + 1 < relend
14869 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14870 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 14871 {
951fd09b
AM
14872 if ((tls_mask & TLS_GD) == 0)
14873 {
14874 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
b00a0a86 14875 if ((tls_mask & TLS_GDIE) != 0)
951fd09b
AM
14876 r_type = R_PPC64_TPREL64;
14877 else
14878 {
4ce794b7 14879 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
14880 r_type = R_PPC64_NONE;
14881 }
14882 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14883 }
14884 }
14885 else
14886 {
14887 if ((tls_mask & TLS_LD) == 0)
411e1bfb 14888 {
4ce794b7 14889 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 14890 r_type = R_PPC64_NONE;
951fd09b 14891 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 14892 }
411e1bfb
AM
14893 }
14894 break;
14895
14896 case R_PPC64_TPREL64:
951fd09b 14897 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
14898 {
14899 r_type = R_PPC64_NONE;
14900 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14901 }
14902 break;
52a82034 14903
006589cf
AM
14904 case R_PPC64_ENTRY:
14905 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14906 if (!bfd_link_pic (info)
14907 && !info->traditional_format
14908 && relocation + 0x80008000 <= 0xffffffff)
14909 {
14910 unsigned int insn1, insn2;
14911
14912 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14913 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14914 if ((insn1 & ~0xfffc) == LD_R2_0R12
14915 && insn2 == ADD_R2_R2_R12)
14916 {
95f0d0d2 14917 bfd_put_32 (input_bfd,
006589cf
AM
14918 LIS_R2 + PPC_HA (relocation),
14919 contents + rel->r_offset);
95f0d0d2 14920 bfd_put_32 (input_bfd,
006589cf
AM
14921 ADDI_R2_R2 + PPC_LO (relocation),
14922 contents + rel->r_offset + 4);
14923 }
14924 }
14925 else
14926 {
14927 relocation -= (rel->r_offset
14928 + input_section->output_offset
14929 + input_section->output_section->vma);
14930 if (relocation + 0x80008000 <= 0xffffffff)
14931 {
14932 unsigned int insn1, insn2;
14933
14934 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14935 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14936 if ((insn1 & ~0xfffc) == LD_R2_0R12
14937 && insn2 == ADD_R2_R2_R12)
14938 {
95f0d0d2 14939 bfd_put_32 (input_bfd,
006589cf
AM
14940 ADDIS_R2_R12 + PPC_HA (relocation),
14941 contents + rel->r_offset);
95f0d0d2 14942 bfd_put_32 (input_bfd,
006589cf
AM
14943 ADDI_R2_R2 + PPC_LO (relocation),
14944 contents + rel->r_offset + 4);
14945 }
14946 }
14947 }
14948 break;
14949
52a82034
AM
14950 case R_PPC64_REL16_HA:
14951 /* If we are generating a non-PIC executable, edit
14952 . 0: addis 2,12,.TOC.-0b@ha
14953 . addi 2,2,.TOC.-0b@l
14954 used by ELFv2 global entry points to set up r2, to
14955 . lis 2,.TOC.@ha
14956 . addi 2,2,.TOC.@l
14957 if .TOC. is in range. */
0e1862bb 14958 if (!bfd_link_pic (info)
810d4e75 14959 && !info->traditional_format
006589cf 14960 && !htab->opd_abi
4f038ee5 14961 && rel->r_addend == d_offset
52a82034
AM
14962 && h != NULL && &h->elf == htab->elf.hgot
14963 && rel + 1 < relend
14964 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14965 && rel[1].r_offset == rel->r_offset + 4
14966 && rel[1].r_addend == rel->r_addend + 4
14967 && relocation + 0x80008000 <= 0xffffffff)
14968 {
14969 unsigned int insn1, insn2;
4a421c53 14970 offset = rel->r_offset - d_offset;
95f0d0d2
AM
14971 insn1 = bfd_get_32 (input_bfd, contents + offset);
14972 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14973 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14974 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14975 {
14976 r_type = R_PPC64_ADDR16_HA;
14977 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14978 rel->r_addend -= d_offset;
14979 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14980 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14981 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14982 }
14983 }
14984 break;
411e1bfb
AM
14985 }
14986
14987 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14988 insn = 0;
b25116a9
AM
14989 max_br_offset = 1 << 25;
14990 addend = rel->r_addend;
bc30df16 14991 reloc_dest = DEST_NORMAL;
65f38f15 14992 switch (r_type)
5bd4f169
AM
14993 {
14994 default:
65f38f15 14995 break;
5bd4f169 14996
3b421ab3
AM
14997 case R_PPC64_TOCSAVE:
14998 if (relocation + addend == (rel->r_offset
14999 + input_section->output_offset
15000 + input_section->output_section->vma)
15001 && tocsave_find (htab, NO_INSERT,
15002 &local_syms, rel, input_bfd))
15003 {
15004 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15005 if (insn == NOP
15006 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
15007 bfd_put_32 (input_bfd,
15008 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
15009 contents + rel->r_offset);
15010 }
15011 break;
15012
65f38f15
AM
15013 /* Branch taken prediction relocations. */
15014 case R_PPC64_ADDR14_BRTAKEN:
15015 case R_PPC64_REL14_BRTAKEN:
cedb70c5 15016 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 15017 /* Fall through. */
65f38f15 15018
86c76c7b 15019 /* Branch not taken prediction relocations. */
65f38f15
AM
15020 case R_PPC64_ADDR14_BRNTAKEN:
15021 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 15022 insn |= bfd_get_32 (input_bfd,
411e1bfb 15023 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 15024 /* Fall through. */
86c76c7b 15025
b25116a9
AM
15026 case R_PPC64_REL14:
15027 max_br_offset = 1 << 15;
1a0670f3 15028 /* Fall through. */
5bd4f169 15029
65f38f15 15030 case R_PPC64_REL24:
05d0e962 15031 case R_PPC64_REL24_NOTOC:
23cedd1d 15032 case R_PPC64_PLTCALL:
5663e321 15033 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
15034 /* Calls to functions with a different TOC, such as calls to
15035 shared objects, need to alter the TOC pointer. This is
15036 done using a linkage stub. A REL24 branching to these
15037 linkage stubs needs to be followed by a nop, as the nop
15038 will be replaced with an instruction to restore the TOC
15039 base pointer. */
8387904d 15040 fdh = h;
b31867b6
AM
15041 if (h != NULL
15042 && h->oh != NULL
15043 && h->oh->is_func_descriptor)
15044 fdh = ppc_follow_link (h->oh);
31c76678
DK
15045 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15046 htab);
5663e321
AM
15047 if ((r_type == R_PPC64_PLTCALL
15048 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 15049 && stub_entry != NULL
05d0e962
AM
15050 && stub_entry->stub_type >= ppc_stub_plt_call
15051 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
15052 stub_entry = NULL;
15053
6abec6bc 15054 if (stub_entry != NULL
d0abeec8
AM
15055 && ((stub_entry->stub_type >= ppc_stub_plt_call
15056 && stub_entry->stub_type <= ppc_stub_plt_call_both)
ad8e1ba5 15057 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
15058 || stub_entry->stub_type == ppc_stub_plt_branch_both
15059 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15060 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 15061 {
b25116a9 15062 bfd_boolean can_plt_call = FALSE;
721956f4 15063
6e1816be
AM
15064 if (stub_entry->stub_type == ppc_stub_plt_call
15065 && !htab->opd_abi
15066 && htab->params->plt_localentry0 != 0
15067 && is_elfv2_localentry0 (&h->elf))
15068 {
15069 /* The function doesn't use or change r2. */
15070 can_plt_call = TRUE;
15071 }
05d0e962
AM
15072 else if (r_type == R_PPC64_REL24_NOTOC)
15073 {
15074 /* NOTOC calls don't need to restore r2. */
15075 can_plt_call = TRUE;
15076 }
6e1816be 15077
f378ab09 15078 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
15079 branch and link followed by a nop. The nop is
15080 replaced by an insn to restore r2. */
6e1816be 15081 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 15082 {
ba8ca3e7
AM
15083 unsigned long br;
15084
15085 br = bfd_get_32 (input_bfd,
15086 contents + rel->r_offset);
15087 if ((br & 1) != 0)
41bd81ab 15088 {
ba8ca3e7
AM
15089 unsigned long nop;
15090
15091 nop = bfd_get_32 (input_bfd,
15092 contents + rel->r_offset + 4);
23cedd1d
AM
15093 if (nop == LD_R2_0R1 + STK_TOC (htab))
15094 can_plt_call = TRUE;
15095 else if (nop == NOP
15096 || nop == CROR_151515
15097 || nop == CROR_313131)
a7f2871e 15098 {
ba8ca3e7
AM
15099 if (h != NULL
15100 && (h == htab->tls_get_addr_fd
15101 || h == htab->tls_get_addr)
7c9cf415 15102 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
15103 {
15104 /* Special stub used, leave nop alone. */
15105 }
15106 else
a078d95a
AM
15107 bfd_put_32 (input_bfd,
15108 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
15109 contents + rel->r_offset + 4);
15110 can_plt_call = TRUE;
a7f2871e 15111 }
41bd81ab 15112 }
5bd4f169 15113 }
721956f4 15114
ba8ca3e7 15115 if (!can_plt_call && h != NULL)
721956f4 15116 {
ba8ca3e7
AM
15117 const char *name = h->elf.root.root.string;
15118
15119 if (*name == '.')
15120 ++name;
15121
15122 if (strncmp (name, "__libc_start_main", 17) == 0
15123 && (name[17] == 0 || name[17] == '@'))
6ab189d5 15124 {
ba8ca3e7
AM
15125 /* Allow crt1 branch to go via a toc adjusting
15126 stub. Other calls that never return could do
15127 the same, if we could detect such. */
b25116a9 15128 can_plt_call = TRUE;
6ab189d5 15129 }
ba8ca3e7
AM
15130 }
15131
15132 if (!can_plt_call)
15133 {
15134 /* g++ as of 20130507 emits self-calls without a
15135 following nop. This is arguably wrong since we
15136 have conflicting information. On the one hand a
15137 global symbol and on the other a local call
15138 sequence, but don't error for this special case.
15139 It isn't possible to cheaply verify we have
15140 exactly such a call. Allow all calls to the same
15141 section. */
15142 asection *code_sec = sec;
15143
15144 if (get_opd_info (sec) != NULL)
ad8e1ba5 15145 {
ba8ca3e7
AM
15146 bfd_vma off = (relocation + addend
15147 - sec->output_section->vma
15148 - sec->output_offset);
bc30df16 15149
ba8ca3e7 15150 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 15151 }
ba8ca3e7
AM
15152 if (code_sec == input_section)
15153 can_plt_call = TRUE;
15154 }
15155
15156 if (!can_plt_call)
15157 {
05d0e962
AM
15158 if (stub_entry->stub_type >= ppc_stub_plt_call
15159 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 15160 info->callbacks->einfo
695344c0 15161 /* xgettext:c-format */
c1c8c1ef 15162 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15163 "(plt call stub)\n"),
4805fc55
AM
15164 input_bfd, input_section, rel->r_offset, sym_name);
15165 else
15166 info->callbacks->einfo
695344c0 15167 /* xgettext:c-format */
c1c8c1ef 15168 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15169 "(toc save/adjust stub)\n"),
4805fc55 15170 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
15171
15172 bfd_set_error (bfd_error_bad_value);
15173 ret = FALSE;
721956f4
AM
15174 }
15175
b25116a9 15176 if (can_plt_call
05d0e962
AM
15177 && stub_entry->stub_type >= ppc_stub_plt_call
15178 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
15179 unresolved_reloc = FALSE;
15180 }
15181
6abec6bc
AM
15182 if ((stub_entry == NULL
15183 || stub_entry->stub_type == ppc_stub_long_branch
15184 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
15185 && get_opd_info (sec) != NULL)
15186 {
15187 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
15188 bfd_vma off = (relocation + addend
15189 - sec->output_section->vma
15190 - sec->output_offset);
aef36ac1 15191 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
15192 if (dest != (bfd_vma) -1)
15193 {
15194 relocation = dest;
15195 addend = 0;
bc30df16 15196 reloc_dest = DEST_OPD;
8387904d
AM
15197 }
15198 }
15199
b25116a9
AM
15200 /* If the branch is out of reach we ought to have a long
15201 branch stub. */
15202 from = (rel->r_offset
15203 + input_section->output_offset
15204 + input_section->output_section->vma);
15205
6911b7dc
AM
15206 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15207 ? fdh->elf.other
15208 : sym->st_other);
15209
6abec6bc
AM
15210 if (stub_entry != NULL
15211 && (stub_entry->stub_type == ppc_stub_long_branch
15212 || stub_entry->stub_type == ppc_stub_plt_branch)
15213 && (r_type == R_PPC64_ADDR14_BRTAKEN
15214 || r_type == R_PPC64_ADDR14_BRNTAKEN
15215 || (relocation + addend - from + max_br_offset
15216 < 2 * max_br_offset)))
15217 /* Don't use the stub if this branch is in range. */
15218 stub_entry = NULL;
b25116a9 15219
05d0e962
AM
15220 if (stub_entry != NULL
15221 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15222 || stub_entry->stub_type == ppc_stub_long_branch_both
15223 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15224 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15225 && (r_type != R_PPC64_REL24_NOTOC
15226 || ((fdh ? fdh->elf.other : sym->st_other)
4a4e7361 15227 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
05d0e962
AM
15228 && (relocation + addend - from + max_br_offset
15229 < 2 * max_br_offset))
15230 stub_entry = NULL;
15231
15232 if (stub_entry != NULL
15233 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15234 || stub_entry->stub_type == ppc_stub_long_branch_both
15235 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15236 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15237 && r_type == R_PPC64_REL24_NOTOC
15238 && (relocation + addend - from + max_br_offset
15239 < 2 * max_br_offset))
15240 stub_entry = NULL;
15241
b25116a9
AM
15242 if (stub_entry != NULL)
15243 {
15244 /* Munge up the value and addend so that we call the stub
15245 rather than the procedure directly. */
a4b6fadd
AM
15246 asection *stub_sec = stub_entry->group->stub_sec;
15247
15248 if (stub_entry->stub_type == ppc_stub_save_res)
15249 relocation += (stub_sec->output_offset
15250 + stub_sec->output_section->vma
15251 + stub_sec->size - htab->sfpr->size
15252 - htab->sfpr->output_offset
15253 - htab->sfpr->output_section->vma);
15254 else
15255 relocation = (stub_entry->stub_offset
15256 + stub_sec->output_offset
15257 + stub_sec->output_section->vma);
b25116a9 15258 addend = 0;
bc30df16 15259 reloc_dest = DEST_STUB;
3b421ab3 15260
05d0e962
AM
15261 if (((stub_entry->stub_type == ppc_stub_plt_call
15262 && ALWAYS_EMIT_R2SAVE)
15263 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15264 || stub_entry->stub_type == ppc_stub_plt_call_both)
e81b4c93
AM
15265 && !(h != NULL
15266 && (h == htab->tls_get_addr_fd
15267 || h == htab->tls_get_addr)
15268 && htab->params->tls_get_addr_opt)
3b421ab3
AM
15269 && rel + 1 < relend
15270 && rel[1].r_offset == rel->r_offset + 4
15271 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15272 relocation += 4;
05d0e962
AM
15273 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15274 || stub_entry->stub_type == ppc_stub_plt_branch_both
15275 || stub_entry->stub_type == ppc_stub_plt_call_both)
15276 && r_type == R_PPC64_REL24_NOTOC)
15277 relocation += 4;
5663e321
AM
15278
15279 if (r_type == R_PPC64_REL24_NOTOC
15280 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
15281 || stub_entry->stub_type == ppc_stub_plt_call_both))
15282 htab->notoc_plt = 1;
b25116a9
AM
15283 }
15284
15285 if (insn != 0)
15286 {
794e51c0 15287 if (is_isa_v2)
721956f4 15288 {
b25116a9
AM
15289 /* Set 'a' bit. This is 0b00010 in BO field for branch
15290 on CR(BI) insns (BO == 001at or 011at), and 0b01000
15291 for branch on CTR insns (BO == 1a00t or 1a01t). */
15292 if ((insn & (0x14 << 21)) == (0x04 << 21))
15293 insn |= 0x02 << 21;
15294 else if ((insn & (0x14 << 21)) == (0x10 << 21))
15295 insn |= 0x08 << 21;
15296 else
15297 break;
15298 }
15299 else
15300 {
15301 /* Invert 'y' bit if not the default. */
4cc603a5 15302 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 15303 insn ^= 0x01 << 21;
721956f4 15304 }
b25116a9 15305
95f0d0d2 15306 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 15307 }
e86ce104 15308
06da1e8e
AM
15309 /* NOP out calls to undefined weak functions.
15310 We can thus call a weak function without first
15311 checking whether the function is defined. */
b25116a9 15312 else if (h != NULL
039b3fef 15313 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 15314 && h->elf.dynindx == -1
05d0e962
AM
15315 && (r_type == R_PPC64_REL24
15316 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 15317 && relocation == 0
4cc603a5 15318 && addend == 0)
e86ce104 15319 {
95f0d0d2 15320 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 15321 goto copy_reloc;
e86ce104 15322 }
65f38f15 15323 break;
066f4018
AM
15324
15325 case R_PPC64_GOT16_DS:
15326 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15327 if (relocation + addend - from + 0x8000 < 0x10000
15328 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15329 {
15330 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15331 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15332 {
15333 insn += (14u << 26) - (58u << 26);
15334 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15335 r_type = R_PPC64_TOC16;
15336 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15337 }
15338 }
15339 break;
15340
15341 case R_PPC64_GOT16_LO_DS:
15342 case R_PPC64_GOT16_HA:
15343 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15344 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
15345 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15346 {
15347 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15348 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15349 {
15350 insn += (14u << 26) - (58u << 26);
15351 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15352 r_type = R_PPC64_TOC16_LO;
15353 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15354 }
15355 else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
15356 {
15357 r_type = R_PPC64_TOC16_HA;
15358 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15359 }
15360 }
15361 break;
4a421c53
AM
15362
15363 case R_PPC64_GOT_PCREL34:
15364 from = (rel->r_offset
15365 + input_section->output_section->vma
15366 + input_section->output_offset);
15367 if (relocation - from + (1ULL << 33) < 1ULL << 34
15368 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15369 {
15370 offset = rel->r_offset;
15371 pinsn = bfd_get_32 (input_bfd, contents + offset);
15372 pinsn <<= 32;
15373 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15374 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15375 == ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
15376 {
15377 /* Replace with paddi. */
15378 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
15379 r_type = R_PPC64_PCREL34;
15380 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15381 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
15382 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
15383 goto pcrelopt;
15384 }
15385 }
15386 break;
15387
15388 case R_PPC64_PCREL34:
15389 if (SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15390 {
15391 offset = rel->r_offset;
15392 pinsn = bfd_get_32 (input_bfd, contents + offset);
15393 pinsn <<= 32;
15394 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15395 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15396 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
15397 | (14ULL << 26) /* paddi */))
15398 {
15399 pcrelopt:
15400 if (rel + 1 < relend
15401 && rel[1].r_offset == offset
15402 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT))
15403 {
15404 bfd_vma off2 = rel[1].r_addend;
15405 if (off2 == 0)
15406 /* zero means next insn. */
15407 off2 = 8;
15408 off2 += offset;
15409 if (off2 + 4 <= input_section->size)
15410 {
15411 uint64_t pinsn2;
dd9b12c2 15412 bfd_signed_vma addend_off;
4a421c53
AM
15413 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
15414 pinsn2 <<= 32;
15415 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15416 break;
dd9b12c2 15417 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
4a421c53 15418 {
dd9b12c2
AM
15419 addend += addend_off;
15420 rel->r_addend = addend;
4a421c53
AM
15421 bfd_put_32 (input_bfd, pinsn >> 32,
15422 contents + offset);
15423 bfd_put_32 (input_bfd, pinsn,
15424 contents + offset + 4);
15425 bfd_put_32 (input_bfd, pinsn2 >> 32,
15426 contents + off2);
15427 }
15428 }
15429 }
15430 }
15431 }
15432 break;
65f38f15 15433 }
5bd4f169 15434
411e1bfb 15435 tls_type = 0;
23cedd1d 15436 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
15437 switch (r_type)
15438 {
15439 default:
cf97bcb0
AM
15440 /* xgettext:c-format */
15441 _bfd_error_handler (_("%pB: %s unsupported"),
15442 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 15443
65f38f15 15444 bfd_set_error (bfd_error_bad_value);
b34976b6 15445 ret = FALSE;
c316a17c 15446 goto copy_reloc;
5bd4f169 15447
65f38f15 15448 case R_PPC64_NONE:
411e1bfb 15449 case R_PPC64_TLS:
727fc41e
AM
15450 case R_PPC64_TLSGD:
15451 case R_PPC64_TLSLD:
3b421ab3 15452 case R_PPC64_TOCSAVE:
04c9666a
AM
15453 case R_PPC64_GNU_VTINHERIT:
15454 case R_PPC64_GNU_VTENTRY:
006589cf 15455 case R_PPC64_ENTRY:
4a421c53 15456 case R_PPC64_PCREL_OPT:
c316a17c 15457 goto copy_reloc;
5bd4f169
AM
15458
15459 /* GOT16 relocations. Like an ADDR16 using the symbol's
15460 address in the GOT as relocation value instead of the
411e1bfb 15461 symbol's value itself. Also, create a GOT entry for the
5bd4f169 15462 symbol and put the symbol value there. */
411e1bfb
AM
15463 case R_PPC64_GOT_TLSGD16:
15464 case R_PPC64_GOT_TLSGD16_LO:
15465 case R_PPC64_GOT_TLSGD16_HI:
15466 case R_PPC64_GOT_TLSGD16_HA:
c213164a 15467 case R_PPC64_GOT_TLSGD34:
951fd09b 15468 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
15469 goto dogot;
15470
15471 case R_PPC64_GOT_TLSLD16:
15472 case R_PPC64_GOT_TLSLD16_LO:
15473 case R_PPC64_GOT_TLSLD16_HI:
15474 case R_PPC64_GOT_TLSLD16_HA:
c213164a 15475 case R_PPC64_GOT_TLSLD34:
951fd09b 15476 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
15477 goto dogot;
15478
15479 case R_PPC64_GOT_TPREL16_DS:
15480 case R_PPC64_GOT_TPREL16_LO_DS:
15481 case R_PPC64_GOT_TPREL16_HI:
15482 case R_PPC64_GOT_TPREL16_HA:
c213164a 15483 case R_PPC64_GOT_TPREL34:
411e1bfb
AM
15484 tls_type = TLS_TLS | TLS_TPREL;
15485 goto dogot;
15486
15487 case R_PPC64_GOT_DTPREL16_DS:
15488 case R_PPC64_GOT_DTPREL16_LO_DS:
15489 case R_PPC64_GOT_DTPREL16_HI:
15490 case R_PPC64_GOT_DTPREL16_HA:
c213164a 15491 case R_PPC64_GOT_DTPREL34:
411e1bfb
AM
15492 tls_type = TLS_TLS | TLS_DTPREL;
15493 goto dogot;
15494
65f38f15
AM
15495 case R_PPC64_GOT16:
15496 case R_PPC64_GOT16_LO:
15497 case R_PPC64_GOT16_HI:
15498 case R_PPC64_GOT16_HA:
15499 case R_PPC64_GOT16_DS:
15500 case R_PPC64_GOT16_LO_DS:
5663e321 15501 case R_PPC64_GOT_PCREL34:
411e1bfb 15502 dogot:
5bd4f169
AM
15503 {
15504 /* Relocation is to the entry for this symbol in the global
15505 offset table. */
e717da7e 15506 asection *got;
d881513a 15507 bfd_vma *offp;
5bd4f169 15508 bfd_vma off;
d881513a 15509 unsigned long indx = 0;
927be08e 15510 struct got_entry *ent;
5663e321
AM
15511 bfd_vma sym_addend = orig_rel.r_addend;
15512
c213164a
AM
15513 if (r_type == R_PPC64_GOT_PCREL34
15514 || r_type == R_PPC64_GOT_TLSGD34
15515 || r_type == R_PPC64_GOT_TLSLD34
15516 || r_type == R_PPC64_GOT_TPREL34
15517 || r_type == R_PPC64_GOT_DTPREL34)
5663e321 15518 sym_addend = 0;
65f38f15 15519
d881513a
AM
15520 if (tls_type == (TLS_TLS | TLS_LD)
15521 && (h == NULL
f5385ebf 15522 || !h->elf.def_dynamic))
927be08e 15523 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 15524 else
5bd4f169 15525 {
d881513a
AM
15526 if (h != NULL)
15527 {
f0158f44
AM
15528 if (!htab->elf.dynamic_sections_created
15529 || h->elf.dynindx == -1
15530 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 15531 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
15532 /* This is actually a static link, or it is a
15533 -Bsymbolic link and the symbol is defined
15534 locally, or the symbol was forced to be local
15535 because of a version file. */
15536 ;
15537 else
15538 {
039b3fef 15539 indx = h->elf.dynindx;
d881513a
AM
15540 unresolved_reloc = FALSE;
15541 }
039b3fef 15542 ent = h->elf.got.glist;
d881513a 15543 }
411e1bfb 15544 else
5bd4f169 15545 {
d881513a
AM
15546 if (local_got_ents == NULL)
15547 abort ();
15548 ent = local_got_ents[r_symndx];
5bd4f169 15549 }
d881513a
AM
15550
15551 for (; ent != NULL; ent = ent->next)
5663e321 15552 if (ent->addend == sym_addend
e717da7e 15553 && ent->owner == input_bfd
d881513a
AM
15554 && ent->tls_type == tls_type)
15555 break;
5bd4f169 15556 }
411e1bfb 15557
927be08e
AM
15558 if (ent == NULL)
15559 abort ();
15560 if (ent->is_indirect)
15561 ent = ent->got.ent;
15562 offp = &ent->got.offset;
15563 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
15564 if (got == NULL)
15565 abort ();
15566
411e1bfb
AM
15567 /* The offset must always be a multiple of 8. We use the
15568 least significant bit to record whether we have already
15569 processed this entry. */
d881513a 15570 off = *offp;
411e1bfb
AM
15571 if ((off & 1) != 0)
15572 off &= ~1;
5bd4f169
AM
15573 else
15574 {
411e1bfb
AM
15575 /* Generate relocs for the dynamic linker, except in
15576 the case of TLSLD where we'll use one entry per
15577 module. */
25f23106
AM
15578 asection *relgot;
15579 bfd_boolean ifunc;
e717da7e 15580
d881513a 15581 *offp = off | 1;
25f23106
AM
15582 relgot = NULL;
15583 ifunc = (h != NULL
15584 ? h->elf.type == STT_GNU_IFUNC
15585 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 15586 if (ifunc)
82e66161
AM
15587 {
15588 relgot = htab->elf.irelplt;
15589 if (indx == 0)
15590 htab->local_ifunc_resolver = 1;
15591 else if (is_static_defined (&h->elf))
15592 htab->maybe_local_ifunc_resolver = 1;
15593 }
f0158f44
AM
15594 else if (indx != 0
15595 || (bfd_link_pic (info)
15596 && (h == NULL
21d68fcd 15597 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 15598 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
15599 && !h->elf.def_dynamic))
15600 && !(tls_type == (TLS_TLS | TLS_TPREL)
15601 && bfd_link_executable (info)
15602 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 15603 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 15604 if (relgot != NULL)
5bd4f169 15605 {
e717da7e
AM
15606 outrel.r_offset = (got->output_section->vma
15607 + got->output_offset
411e1bfb 15608 + off);
5663e321 15609 outrel.r_addend = sym_addend;
d881513a 15610 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 15611 {
411e1bfb 15612 outrel.r_addend = 0;
e515b051 15613 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
15614 if (tls_type == (TLS_TLS | TLS_GD))
15615 {
e717da7e
AM
15616 loc = relgot->contents;
15617 loc += (relgot->reloc_count++
d881513a
AM
15618 * sizeof (Elf64_External_Rela));
15619 bfd_elf64_swap_reloca_out (output_bfd,
15620 &outrel, loc);
e515b051 15621 outrel.r_offset += 8;
5663e321 15622 outrel.r_addend = sym_addend;
d881513a
AM
15623 outrel.r_info
15624 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 15625 }
411e1bfb 15626 }
951fd09b 15627 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 15628 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 15629 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 15630 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
15631 else if (indx != 0)
15632 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15633 else
81407a69 15634 {
25f23106
AM
15635 if (ifunc)
15636 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15637 else
15638 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
15639
15640 /* Write the .got section contents for the sake
15641 of prelink. */
e717da7e 15642 loc = got->contents + off;
23fbd6fa
JJ
15643 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15644 loc);
81407a69 15645 }
81407a69
AM
15646
15647 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
15648 {
15649 outrel.r_addend += relocation;
15650 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
15651 {
15652 if (htab->elf.tls_sec == NULL)
15653 outrel.r_addend = 0;
15654 else
15655 outrel.r_addend -= htab->elf.tls_sec->vma;
15656 }
e515b051 15657 }
e717da7e
AM
15658 loc = relgot->contents;
15659 loc += (relgot->reloc_count++
411e1bfb
AM
15660 * sizeof (Elf64_External_Rela));
15661 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15662 }
15663
ad8e1ba5 15664 /* Init the .got section contents here if we're not
81407a69 15665 emitting a reloc. */
d881513a 15666 else
411e1bfb 15667 {
5663e321 15668 relocation += sym_addend;
f0158f44 15669 if (tls_type != 0)
411e1bfb 15670 {
989f9879
AM
15671 if (htab->elf.tls_sec == NULL)
15672 relocation = 0;
15673 else
15674 {
f0158f44
AM
15675 if (tls_type & TLS_LD)
15676 relocation = 0;
15677 else
15678 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 15679 if (tls_type & TLS_TPREL)
989f9879
AM
15680 relocation += DTP_OFFSET - TP_OFFSET;
15681 }
5bd4f169 15682
f0158f44 15683 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
15684 {
15685 bfd_put_64 (output_bfd, relocation,
e717da7e 15686 got->contents + off + 8);
676ee2b5 15687 relocation = 1;
7b609f53 15688 }
411e1bfb
AM
15689 }
15690 bfd_put_64 (output_bfd, relocation,
e717da7e 15691 got->contents + off);
5bd4f169
AM
15692 }
15693 }
15694
65f38f15
AM
15695 if (off >= (bfd_vma) -2)
15696 abort ();
15697
bf102f86 15698 relocation = got->output_section->vma + got->output_offset + off;
c213164a
AM
15699 if (!(r_type == R_PPC64_GOT_PCREL34
15700 || r_type == R_PPC64_GOT_TLSGD34
15701 || r_type == R_PPC64_GOT_TLSLD34
15702 || r_type == R_PPC64_GOT_TPREL34
15703 || r_type == R_PPC64_GOT_DTPREL34))
5663e321 15704 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 15705 }
65f38f15
AM
15706 break;
15707
15708 case R_PPC64_PLT16_HA:
15709 case R_PPC64_PLT16_HI:
15710 case R_PPC64_PLT16_LO:
08be3224 15711 case R_PPC64_PLT16_LO_DS:
5663e321
AM
15712 case R_PPC64_PLT_PCREL34:
15713 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
15714 case R_PPC64_PLT32:
15715 case R_PPC64_PLT64:
23cedd1d 15716 case R_PPC64_PLTSEQ:
5663e321 15717 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 15718 case R_PPC64_PLTCALL:
5663e321 15719 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
15720 /* Relocation is to the entry for this symbol in the
15721 procedure linkage table. */
23cedd1d 15722 unresolved_reloc = TRUE;
cbf95972
AM
15723 {
15724 struct plt_entry **plt_list = NULL;
15725 if (h != NULL)
15726 plt_list = &h->elf.plt.plist;
15727 else if (local_got_ents != NULL)
15728 {
15729 struct plt_entry **local_plt = (struct plt_entry **)
15730 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 15731 plt_list = local_plt + r_symndx;
cbf95972
AM
15732 }
15733 if (plt_list)
15734 {
15735 struct plt_entry *ent;
5663e321
AM
15736 bfd_vma sym_addend = orig_rel.r_addend;
15737
15738 if (r_type == R_PPC64_PLT_PCREL34
15739 || r_type == R_PPC64_PLT_PCREL34_NOTOC)
15740 sym_addend = 0;
65f38f15 15741
cbf95972
AM
15742 for (ent = *plt_list; ent != NULL; ent = ent->next)
15743 if (ent->plt.offset != (bfd_vma) -1
5663e321 15744 && ent->addend == sym_addend)
cbf95972
AM
15745 {
15746 asection *plt;
08be3224 15747 bfd_vma got;
cbf95972
AM
15748
15749 plt = htab->elf.splt;
15750 if (!htab->elf.dynamic_sections_created
15751 || h == NULL
15752 || h->elf.dynindx == -1)
2d7ad24e
AM
15753 {
15754 if (h != NULL
15755 ? h->elf.type == STT_GNU_IFUNC
15756 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15757 plt = htab->elf.iplt;
15758 else
15759 plt = htab->pltlocal;
15760 }
15761 relocation = (plt->output_section->vma
15762 + plt->output_offset
15763 + ent->plt.offset);
08be3224 15764 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
15765 || r_type == R_PPC64_PLT16_HI
15766 || r_type == R_PPC64_PLT16_LO
15767 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
15768 {
15769 got = (elf_gp (output_bfd)
15770 + htab->sec_info[input_section->id].toc_off);
15771 relocation -= got;
15772 }
5663e321
AM
15773 if (r_type != R_PPC64_PLT_PCREL34
15774 && r_type != R_PPC64_PLT_PCREL34_NOTOC)
15775 addend = 0;
cbf95972
AM
15776 unresolved_reloc = FALSE;
15777 break;
15778 }
15779 }
15780 }
65f38f15 15781 break;
5bd4f169 15782
0b13192e
AM
15783 case R_PPC64_TOC:
15784 /* Relocation value is TOC base. */
15785 relocation = TOCstart;
cf35638d 15786 if (r_symndx == STN_UNDEF)
6f20ed8a 15787 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
15788 else if (unresolved_reloc)
15789 ;
6f20ed8a
AM
15790 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15791 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
15792 else
15793 unresolved_reloc = TRUE;
ab96bf03 15794 goto dodyn;
0b13192e 15795
5bd4f169
AM
15796 /* TOC16 relocs. We want the offset relative to the TOC base,
15797 which is the address of the start of the TOC plus 0x8000.
15798 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15799 in this order. */
65f38f15
AM
15800 case R_PPC64_TOC16:
15801 case R_PPC64_TOC16_LO:
15802 case R_PPC64_TOC16_HI:
15803 case R_PPC64_TOC16_DS:
15804 case R_PPC64_TOC16_LO_DS:
15805 case R_PPC64_TOC16_HA:
6f20ed8a 15806 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
15807 break;
15808
15809 /* Relocate against the beginning of the section. */
65f38f15
AM
15810 case R_PPC64_SECTOFF:
15811 case R_PPC64_SECTOFF_LO:
15812 case R_PPC64_SECTOFF_HI:
15813 case R_PPC64_SECTOFF_DS:
15814 case R_PPC64_SECTOFF_LO_DS:
15815 case R_PPC64_SECTOFF_HA:
4ce794b7 15816 if (sec != NULL)
65f38f15 15817 addend -= sec->output_section->vma;
5bd4f169
AM
15818 break;
15819
25f23106
AM
15820 case R_PPC64_REL16:
15821 case R_PPC64_REL16_LO:
15822 case R_PPC64_REL16_HI:
15823 case R_PPC64_REL16_HA:
4a969973
AM
15824 case R_PPC64_REL16_HIGH:
15825 case R_PPC64_REL16_HIGHA:
15826 case R_PPC64_REL16_HIGHER:
15827 case R_PPC64_REL16_HIGHERA:
15828 case R_PPC64_REL16_HIGHEST:
15829 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
15830 case R_PPC64_REL16_HIGHER34:
15831 case R_PPC64_REL16_HIGHERA34:
15832 case R_PPC64_REL16_HIGHEST34:
15833 case R_PPC64_REL16_HIGHESTA34:
a680de9a 15834 case R_PPC64_REL16DX_HA:
721956f4
AM
15835 case R_PPC64_REL14:
15836 case R_PPC64_REL14_BRNTAKEN:
15837 case R_PPC64_REL14_BRTAKEN:
5d1634d7 15838 case R_PPC64_REL24:
05d0e962 15839 case R_PPC64_REL24_NOTOC:
5663e321
AM
15840 case R_PPC64_PCREL34:
15841 case R_PPC64_PCREL28:
5d1634d7
AM
15842 break;
15843
411e1bfb
AM
15844 case R_PPC64_TPREL16:
15845 case R_PPC64_TPREL16_LO:
15846 case R_PPC64_TPREL16_HI:
15847 case R_PPC64_TPREL16_HA:
15848 case R_PPC64_TPREL16_DS:
15849 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
15850 case R_PPC64_TPREL16_HIGH:
15851 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
15852 case R_PPC64_TPREL16_HIGHER:
15853 case R_PPC64_TPREL16_HIGHERA:
15854 case R_PPC64_TPREL16_HIGHEST:
15855 case R_PPC64_TPREL16_HIGHESTA:
c213164a 15856 case R_PPC64_TPREL34:
766bc656
AM
15857 if (h != NULL
15858 && h->elf.root.type == bfd_link_hash_undefweak
15859 && h->elf.dynindx == -1)
15860 {
15861 /* Make this relocation against an undefined weak symbol
15862 resolve to zero. This is really just a tweak, since
15863 code using weak externs ought to check that they are
15864 defined before using them. */
15865 bfd_byte *p = contents + rel->r_offset - d_offset;
15866
95f0d0d2 15867 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
15868 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15869 if (insn != 0)
95f0d0d2 15870 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
15871 break;
15872 }
989f9879
AM
15873 if (htab->elf.tls_sec != NULL)
15874 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
15875 /* The TPREL16 relocs shouldn't really be used in shared
15876 libs or with non-local symbols as that will result in
15877 DT_TEXTREL being set, but support them anyway. */
15878 goto dodyn;
411e1bfb
AM
15879
15880 case R_PPC64_DTPREL16:
15881 case R_PPC64_DTPREL16_LO:
15882 case R_PPC64_DTPREL16_HI:
15883 case R_PPC64_DTPREL16_HA:
15884 case R_PPC64_DTPREL16_DS:
15885 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
15886 case R_PPC64_DTPREL16_HIGH:
15887 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
15888 case R_PPC64_DTPREL16_HIGHER:
15889 case R_PPC64_DTPREL16_HIGHERA:
15890 case R_PPC64_DTPREL16_HIGHEST:
15891 case R_PPC64_DTPREL16_HIGHESTA:
c213164a 15892 case R_PPC64_DTPREL34:
989f9879
AM
15893 if (htab->elf.tls_sec != NULL)
15894 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
15895 break;
15896
45965137
AM
15897 case R_PPC64_ADDR64_LOCAL:
15898 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15899 ? h->elf.other
15900 : sym->st_other);
15901 break;
15902
e515b051
AM
15903 case R_PPC64_DTPMOD64:
15904 relocation = 1;
15905 addend = 0;
15906 goto dodyn;
15907
411e1bfb 15908 case R_PPC64_TPREL64:
989f9879
AM
15909 if (htab->elf.tls_sec != NULL)
15910 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
15911 goto dodyn;
15912
15913 case R_PPC64_DTPREL64:
989f9879
AM
15914 if (htab->elf.tls_sec != NULL)
15915 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 15916 /* Fall through. */
411e1bfb 15917
65f38f15
AM
15918 /* Relocations that may need to be propagated if this is a
15919 dynamic object. */
04c9666a 15920 case R_PPC64_REL30:
65f38f15
AM
15921 case R_PPC64_REL32:
15922 case R_PPC64_REL64:
15923 case R_PPC64_ADDR14:
15924 case R_PPC64_ADDR14_BRNTAKEN:
15925 case R_PPC64_ADDR14_BRTAKEN:
15926 case R_PPC64_ADDR16:
15927 case R_PPC64_ADDR16_DS:
15928 case R_PPC64_ADDR16_HA:
15929 case R_PPC64_ADDR16_HI:
f9c6b907
AM
15930 case R_PPC64_ADDR16_HIGH:
15931 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15932 case R_PPC64_ADDR16_HIGHER:
15933 case R_PPC64_ADDR16_HIGHERA:
15934 case R_PPC64_ADDR16_HIGHEST:
15935 case R_PPC64_ADDR16_HIGHESTA:
15936 case R_PPC64_ADDR16_LO:
15937 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
15938 case R_PPC64_ADDR16_HIGHER34:
15939 case R_PPC64_ADDR16_HIGHERA34:
15940 case R_PPC64_ADDR16_HIGHEST34:
15941 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 15942 case R_PPC64_ADDR24:
65f38f15
AM
15943 case R_PPC64_ADDR32:
15944 case R_PPC64_ADDR64:
15945 case R_PPC64_UADDR16:
15946 case R_PPC64_UADDR32:
15947 case R_PPC64_UADDR64:
5663e321
AM
15948 case R_PPC64_D34:
15949 case R_PPC64_D34_LO:
15950 case R_PPC64_D34_HI30:
15951 case R_PPC64_D34_HA30:
15952 case R_PPC64_D28:
411e1bfb 15953 dodyn:
5d1634d7 15954 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
15955 break;
15956
41bd81ab
AM
15957 if (NO_OPD_RELOCS && is_opd)
15958 break;
15959
8a9e8e72 15960 if (bfd_link_pic (info)
b1b07054
AM
15961 ? ((h == NULL
15962 || h->dyn_relocs != NULL)
15963 && ((h != NULL && pc_dynrelocs (h))
15964 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
15965 : (h != NULL
15966 ? h->dyn_relocs != NULL
d311bc8b 15967 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 15968 {
b34976b6 15969 bfd_boolean skip, relocate;
65f38f15 15970 asection *sreloc;
1cf1f670 15971 bfd_vma out_off;
82e66161 15972 long indx = 0;
65f38f15
AM
15973
15974 /* When generating a dynamic object, these relocations
15975 are copied into the output file to be resolved at run
15976 time. */
15977
b34976b6
AM
15978 skip = FALSE;
15979 relocate = FALSE;
65f38f15 15980
1cf1f670
AM
15981 out_off = _bfd_elf_section_offset (output_bfd, info,
15982 input_section, rel->r_offset);
15983 if (out_off == (bfd_vma) -1)
b34976b6 15984 skip = TRUE;
1cf1f670 15985 else if (out_off == (bfd_vma) -2)
b34976b6 15986 skip = TRUE, relocate = TRUE;
1cf1f670
AM
15987 out_off += (input_section->output_section->vma
15988 + input_section->output_offset);
15989 outrel.r_offset = out_off;
411e1bfb 15990 outrel.r_addend = rel->r_addend;
65f38f15 15991
1cf1f670
AM
15992 /* Optimize unaligned reloc use. */
15993 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15994 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15995 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15996 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15997 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15998 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15999 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16000 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16001 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16002
65f38f15 16003 if (skip)
0bb2d96a 16004 memset (&outrel, 0, sizeof outrel);
afe397ea 16005 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
16006 && !is_opd
16007 && r_type != R_PPC64_TOC)
14acf4dc 16008 {
82e66161
AM
16009 indx = h->elf.dynindx;
16010 BFD_ASSERT (indx != -1);
16011 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 16012 }
65f38f15
AM
16013 else
16014 {
41bd81ab
AM
16015 /* This symbol is local, or marked to become local,
16016 or this is an opd section reloc which must point
16017 at a local function. */
65f38f15 16018 outrel.r_addend += relocation;
e86ce104 16019 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 16020 {
3fad3c7c 16021 if (is_opd && h != NULL)
afbe61cf
AM
16022 {
16023 /* Lie about opd entries. This case occurs
16024 when building shared libraries and we
16025 reference a function in another shared
3fad3c7c
AM
16026 lib. The same thing happens for a weak
16027 definition in an application that's
16028 overridden by a strong definition in a
16029 shared lib. (I believe this is a generic
16030 bug in binutils handling of weak syms.)
16031 In these cases we won't use the opd
1e2f5b6e 16032 entry in this lib. */
b34976b6 16033 unresolved_reloc = FALSE;
afbe61cf 16034 }
25f23106
AM
16035 if (!is_opd
16036 && r_type == R_PPC64_ADDR64
16037 && (h != NULL
16038 ? h->elf.type == STT_GNU_IFUNC
16039 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16040 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16041 else
16042 {
16043 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 16044
25f23106
AM
16045 /* We need to relocate .opd contents for ld.so.
16046 Prelink also wants simple and consistent rules
16047 for relocs. This make all RELATIVE relocs have
16048 *r_offset equal to r_addend. */
16049 relocate = TRUE;
16050 }
65f38f15
AM
16051 }
16052 else
16053 {
25f23106
AM
16054 if (h != NULL
16055 ? h->elf.type == STT_GNU_IFUNC
16056 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16057 {
25f53a85 16058 info->callbacks->einfo
695344c0 16059 /* xgettext:c-format */
174d0a74 16060 (_("%H: %s for indirect "
c1c8c1ef 16061 "function `%pT' unsupported\n"),
25f53a85 16062 input_bfd, input_section, rel->r_offset,
25f23106
AM
16063 ppc64_elf_howto_table[r_type]->name,
16064 sym_name);
16065 ret = FALSE;
16066 }
cf35638d 16067 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
16068 ;
16069 else if (sec == NULL || sec->owner == NULL)
16070 {
16071 bfd_set_error (bfd_error_bad_value);
b34976b6 16072 return FALSE;
65f38f15
AM
16073 }
16074 else
16075 {
f26a3287 16076 asection *osec = sec->output_section;
65f38f15 16077
f26a3287
AM
16078 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16079 {
16080 /* TLS symbol values are relative to the
16081 TLS segment. Dynamic relocations for
16082 local TLS symbols therefore can't be
16083 reduced to a relocation against their
16084 section symbol because it holds the
16085 address of the section, not a value
16086 relative to the TLS segment. We could
16087 change the .tdata dynamic section symbol
16088 to be zero value but STN_UNDEF works
16089 and is used elsewhere, eg. for TPREL64
16090 GOT relocs against local TLS symbols. */
16091 osec = htab->elf.tls_sec;
16092 indx = 0;
16093 }
16094 else
74541ad4 16095 {
74541ad4 16096 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
16097 if (indx == 0)
16098 {
16099 if ((osec->flags & SEC_READONLY) == 0
16100 && htab->elf.data_index_section != NULL)
16101 osec = htab->elf.data_index_section;
16102 else
16103 osec = htab->elf.text_index_section;
16104 indx = elf_section_data (osec)->dynindx;
16105 }
16106 BFD_ASSERT (indx != 0);
74541ad4 16107 }
74541ad4 16108
65f38f15
AM
16109 /* We are turning this relocation into one
16110 against a section symbol, so subtract out
16111 the output section's address but not the
16112 offset of the input section in the output
16113 section. */
16114 outrel.r_addend -= osec->vma;
16115 }
16116
16117 outrel.r_info = ELF64_R_INFO (indx, r_type);
16118 }
16119 }
16120
16121 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
16122 if (h != NULL
16123 ? h->elf.type == STT_GNU_IFUNC
16124 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
16125 {
16126 sreloc = htab->elf.irelplt;
16127 if (indx == 0)
16128 htab->local_ifunc_resolver = 1;
16129 else if (is_static_defined (&h->elf))
16130 htab->maybe_local_ifunc_resolver = 1;
16131 }
65f38f15
AM
16132 if (sreloc == NULL)
16133 abort ();
16134
dfbb6ac9
AM
16135 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16136 >= sreloc->size)
16137 abort ();
947216bf
AM
16138 loc = sreloc->contents;
16139 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
16140 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16141
16142 /* If this reloc is against an external symbol, it will
16143 be computed at runtime, so there's no need to do
81407a69
AM
16144 anything now. However, for the sake of prelink ensure
16145 that the section contents are a known value. */
2cdcc330 16146 if (!relocate)
81407a69
AM
16147 {
16148 unresolved_reloc = FALSE;
16149 /* The value chosen here is quite arbitrary as ld.so
16150 ignores section contents except for the special
16151 case of .opd where the contents might be accessed
16152 before relocation. Choose zero, as that won't
16153 cause reloc overflow. */
16154 relocation = 0;
16155 addend = 0;
16156 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16157 to improve backward compatibility with older
16158 versions of ld. */
16159 if (r_type == R_PPC64_ADDR64)
16160 addend = outrel.r_addend;
16161 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 16162 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 16163 addend = outrel.r_offset;
81407a69 16164 }
65f38f15 16165 }
5bd4f169
AM
16166 break;
16167
65f38f15
AM
16168 case R_PPC64_COPY:
16169 case R_PPC64_GLOB_DAT:
16170 case R_PPC64_JMP_SLOT:
25f23106 16171 case R_PPC64_JMP_IREL:
65f38f15
AM
16172 case R_PPC64_RELATIVE:
16173 /* We shouldn't ever see these dynamic relocs in relocatable
16174 files. */
ae9a127f 16175 /* Fall through. */
65f38f15
AM
16176
16177 case R_PPC64_PLTGOT16:
16178 case R_PPC64_PLTGOT16_DS:
16179 case R_PPC64_PLTGOT16_HA:
16180 case R_PPC64_PLTGOT16_HI:
16181 case R_PPC64_PLTGOT16_LO:
16182 case R_PPC64_PLTGOT16_LO_DS:
16183 case R_PPC64_PLTREL32:
16184 case R_PPC64_PLTREL64:
16185 /* These ones haven't been implemented yet. */
16186
25f53a85 16187 info->callbacks->einfo
695344c0 16188 /* xgettext:c-format */
c1c8c1ef 16189 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 16190 input_bfd,
4ce794b7 16191 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
16192
16193 bfd_set_error (bfd_error_invalid_operation);
b34976b6 16194 ret = FALSE;
c316a17c 16195 goto copy_reloc;
65f38f15 16196 }
5bd4f169 16197
67f0cbdb
AM
16198 /* Multi-instruction sequences that access the TOC can be
16199 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 16200 to nop; addi rb,r2,x; */
67f0cbdb
AM
16201 switch (r_type)
16202 {
16203 default:
16204 break;
16205
16206 case R_PPC64_GOT_TLSLD16_HI:
16207 case R_PPC64_GOT_TLSGD16_HI:
16208 case R_PPC64_GOT_TPREL16_HI:
16209 case R_PPC64_GOT_DTPREL16_HI:
16210 case R_PPC64_GOT16_HI:
16211 case R_PPC64_TOC16_HI:
16212 /* These relocs would only be useful if building up an
16213 offset to later add to r2, perhaps in an indexed
16214 addressing mode instruction. Don't try to optimize.
16215 Unfortunately, the possibility of someone building up an
16216 offset like this or even with the HA relocs, means that
16217 we need to check the high insn when optimizing the low
16218 insn. */
16219 break;
16220
5663e321
AM
16221 case R_PPC64_PLTCALL_NOTOC:
16222 if (!unresolved_reloc)
16223 htab->notoc_plt = 1;
16224 /* Fall through. */
23cedd1d
AM
16225 case R_PPC64_PLTCALL:
16226 if (unresolved_reloc)
16227 {
16228 /* No plt entry. Make this into a direct call. */
16229 bfd_byte *p = contents + rel->r_offset;
16230 insn = bfd_get_32 (input_bfd, p);
16231 insn &= 1;
16232 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
16233 if (r_type == R_PPC64_PLTCALL)
16234 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
16235 unresolved_reloc = save_unresolved_reloc;
16236 r_type = R_PPC64_REL24;
16237 }
16238 break;
16239
5663e321 16240 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
16241 case R_PPC64_PLTSEQ:
16242 if (unresolved_reloc)
16243 {
16244 unresolved_reloc = FALSE;
16245 goto nop_it;
16246 }
16247 break;
16248
5663e321
AM
16249 case R_PPC64_PLT_PCREL34_NOTOC:
16250 if (!unresolved_reloc)
16251 htab->notoc_plt = 1;
16252 /* Fall through. */
16253 case R_PPC64_PLT_PCREL34:
16254 if (unresolved_reloc)
16255 {
16256 bfd_byte *p = contents + rel->r_offset;
16257 bfd_put_32 (input_bfd, PNOP >> 32, p);
16258 bfd_put_32 (input_bfd, PNOP, p + 4);
16259 unresolved_reloc = FALSE;
16260 goto copy_reloc;
16261 }
16262 break;
16263
23cedd1d
AM
16264 case R_PPC64_PLT16_HA:
16265 if (unresolved_reloc)
16266 {
16267 unresolved_reloc = FALSE;
16268 goto nop_it;
16269 }
16270 /* Fall through. */
67f0cbdb
AM
16271 case R_PPC64_GOT_TLSLD16_HA:
16272 case R_PPC64_GOT_TLSGD16_HA:
16273 case R_PPC64_GOT_TPREL16_HA:
16274 case R_PPC64_GOT_DTPREL16_HA:
16275 case R_PPC64_GOT16_HA:
16276 case R_PPC64_TOC16_HA:
98528052 16277 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16278 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 16279 {
23cedd1d
AM
16280 bfd_byte *p;
16281 nop_it:
16282 p = contents + (rel->r_offset & ~3);
98528052 16283 bfd_put_32 (input_bfd, NOP, p);
d830549d 16284 goto copy_reloc;
98528052 16285 }
67f0cbdb
AM
16286 break;
16287
23cedd1d
AM
16288 case R_PPC64_PLT16_LO:
16289 case R_PPC64_PLT16_LO_DS:
16290 if (unresolved_reloc)
16291 {
16292 unresolved_reloc = FALSE;
16293 goto nop_it;
16294 }
16295 /* Fall through. */
67f0cbdb
AM
16296 case R_PPC64_GOT_TLSLD16_LO:
16297 case R_PPC64_GOT_TLSGD16_LO:
16298 case R_PPC64_GOT_TPREL16_LO_DS:
16299 case R_PPC64_GOT_DTPREL16_LO_DS:
16300 case R_PPC64_GOT16_LO:
16301 case R_PPC64_GOT16_LO_DS:
16302 case R_PPC64_TOC16_LO:
16303 case R_PPC64_TOC16_LO_DS:
98528052 16304 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16305 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
16306 {
16307 bfd_byte *p = contents + (rel->r_offset & ~3);
16308 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
16309 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16310 {
16311 /* Transform addic to addi when we change reg. */
16312 insn &= ~((0x3f << 26) | (0x1f << 16));
16313 insn |= (14u << 26) | (2 << 16);
16314 }
16315 else
67f0cbdb 16316 {
98528052
AM
16317 insn &= ~(0x1f << 16);
16318 insn |= 2 << 16;
67f0cbdb 16319 }
560c8763 16320 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
16321 }
16322 break;
9a23f96e
AM
16323
16324 case R_PPC64_TPREL16_HA:
16325 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16326 {
16327 bfd_byte *p = contents + (rel->r_offset & ~3);
16328 insn = bfd_get_32 (input_bfd, p);
16329 if ((insn & ((0x3f << 26) | 0x1f << 16))
16330 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16331 /* xgettext:c-format */
16332 info->callbacks->minfo
16333 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
16334 input_bfd, input_section, rel->r_offset,
16335 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 16336 else
d830549d
AM
16337 {
16338 bfd_put_32 (input_bfd, NOP, p);
16339 goto copy_reloc;
16340 }
9a23f96e
AM
16341 }
16342 break;
16343
16344 case R_PPC64_TPREL16_LO:
16345 case R_PPC64_TPREL16_LO_DS:
16346 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16347 {
16348 bfd_byte *p = contents + (rel->r_offset & ~3);
16349 insn = bfd_get_32 (input_bfd, p);
16350 insn &= ~(0x1f << 16);
16351 insn |= 13 << 16;
16352 bfd_put_32 (input_bfd, insn, p);
16353 }
16354 break;
67f0cbdb
AM
16355 }
16356
65f38f15
AM
16357 /* Do any further special processing. */
16358 switch (r_type)
16359 {
16360 default:
16361 break;
16362
25f23106 16363 case R_PPC64_REL16_HA:
4a969973
AM
16364 case R_PPC64_REL16_HIGHA:
16365 case R_PPC64_REL16_HIGHERA:
16366 case R_PPC64_REL16_HIGHESTA:
a680de9a 16367 case R_PPC64_REL16DX_HA:
f9c6b907
AM
16368 case R_PPC64_ADDR16_HA:
16369 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
16370 case R_PPC64_ADDR16_HIGHERA:
16371 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
16372 case R_PPC64_TOC16_HA:
16373 case R_PPC64_SECTOFF_HA:
411e1bfb 16374 case R_PPC64_TPREL16_HA:
f9c6b907 16375 case R_PPC64_TPREL16_HIGHA:
411e1bfb 16376 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 16377 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
16378 case R_PPC64_DTPREL16_HA:
16379 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 16380 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 16381 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
16382 /* It's just possible that this symbol is a weak symbol
16383 that's not actually defined anywhere. In that case,
16384 'sec' would be NULL, and we should leave the symbol
16385 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
16386 if (sec == NULL)
16387 break;
1a0670f3 16388 /* Fall through. */
5c5f6e17
AM
16389
16390 case R_PPC64_GOT16_HA:
16391 case R_PPC64_PLTGOT16_HA:
16392 case R_PPC64_PLT16_HA:
16393 case R_PPC64_GOT_TLSGD16_HA:
16394 case R_PPC64_GOT_TLSLD16_HA:
16395 case R_PPC64_GOT_TPREL16_HA:
16396 case R_PPC64_GOT_DTPREL16_HA:
16397 /* Add 0x10000 if sign bit in 0:15 is set.
16398 Bits 0:15 are not used. */
16399 addend += 0x8000;
65f38f15
AM
16400 break;
16401
5663e321
AM
16402 case R_PPC64_D34_HA30:
16403 case R_PPC64_ADDR16_HIGHERA34:
16404 case R_PPC64_ADDR16_HIGHESTA34:
16405 case R_PPC64_REL16_HIGHERA34:
16406 case R_PPC64_REL16_HIGHESTA34:
16407 if (sec != NULL)
16408 addend += 1ULL << 33;
16409 break;
16410
65f38f15
AM
16411 case R_PPC64_ADDR16_DS:
16412 case R_PPC64_ADDR16_LO_DS:
16413 case R_PPC64_GOT16_DS:
16414 case R_PPC64_GOT16_LO_DS:
16415 case R_PPC64_PLT16_LO_DS:
16416 case R_PPC64_SECTOFF_DS:
16417 case R_PPC64_SECTOFF_LO_DS:
16418 case R_PPC64_TOC16_DS:
16419 case R_PPC64_TOC16_LO_DS:
16420 case R_PPC64_PLTGOT16_DS:
16421 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
16422 case R_PPC64_GOT_TPREL16_DS:
16423 case R_PPC64_GOT_TPREL16_LO_DS:
16424 case R_PPC64_GOT_DTPREL16_DS:
16425 case R_PPC64_GOT_DTPREL16_LO_DS:
16426 case R_PPC64_TPREL16_DS:
16427 case R_PPC64_TPREL16_LO_DS:
16428 case R_PPC64_DTPREL16_DS:
16429 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
16430 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16431 mask = 3;
a680de9a
PB
16432 /* If this reloc is against an lq, lxv, or stxv insn, then
16433 the value must be a multiple of 16. This is somewhat of
16434 a hack, but the "correct" way to do this by defining _DQ
16435 forms of all the _DS relocs bloats all reloc switches in
16436 this file. It doesn't make much sense to use these
16437 relocs in data, so testing the insn should be safe. */
16438 if ((insn & (0x3f << 26)) == (56u << 26)
16439 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 16440 mask = 15;
a680de9a
PB
16441 relocation += addend;
16442 addend = insn & (mask ^ 3);
16443 if ((relocation & mask) != 0)
65f38f15 16444 {
a680de9a 16445 relocation ^= relocation & mask;
25f53a85 16446 info->callbacks->einfo
695344c0 16447 /* xgettext:c-format */
174d0a74 16448 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 16449 input_bfd, input_section, rel->r_offset,
d830549d 16450 ppc64_elf_howto_table[r_type]->name,
adadcc0c 16451 mask + 1);
65f38f15 16452 bfd_set_error (bfd_error_bad_value);
b34976b6 16453 ret = FALSE;
c316a17c 16454 goto copy_reloc;
65f38f15
AM
16455 }
16456 break;
5bd4f169
AM
16457 }
16458
239e1f3a
AM
16459 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16460 because such sections are not SEC_ALLOC and thus ld.so will
16461 not process them. */
d830549d 16462 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 16463 if (unresolved_reloc
239e1f3a 16464 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
16465 && h->elf.def_dynamic)
16466 && _bfd_elf_section_offset (output_bfd, info, input_section,
16467 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 16468 {
25f53a85 16469 info->callbacks->einfo
695344c0 16470 /* xgettext:c-format */
c1c8c1ef 16471 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 16472 input_bfd, input_section, rel->r_offset,
b80eed39 16473 howto->name,
039b3fef 16474 h->elf.root.root.string);
b34976b6 16475 ret = FALSE;
9c07fe7c 16476 }
5bd4f169 16477
b80eed39
AM
16478 /* 16-bit fields in insns mostly have signed values, but a
16479 few insns have 16-bit unsigned values. Really, we should
16480 have different reloc types. */
16481 if (howto->complain_on_overflow != complain_overflow_dont
16482 && howto->dst_mask == 0xffff
16483 && (input_section->flags & SEC_CODE) != 0)
16484 {
16485 enum complain_overflow complain = complain_overflow_signed;
16486
16487 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
16488 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16489 complain = complain_overflow_bitfield;
16490 else if (howto->rightshift == 0
16491 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16492 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16493 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16494 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16495 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16496 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
16497 complain = complain_overflow_unsigned;
16498 if (howto->complain_on_overflow != complain)
16499 {
16500 alt_howto = *howto;
16501 alt_howto.complain_on_overflow = complain;
16502 howto = &alt_howto;
16503 }
16504 }
16505
5663e321 16506 switch (r_type)
a680de9a 16507 {
5663e321
AM
16508 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
16509 case R_PPC64_D34:
16510 case R_PPC64_D34_LO:
16511 case R_PPC64_D34_HI30:
16512 case R_PPC64_D34_HA30:
16513 case R_PPC64_PCREL34:
16514 case R_PPC64_GOT_PCREL34:
c213164a
AM
16515 case R_PPC64_TPREL34:
16516 case R_PPC64_DTPREL34:
16517 case R_PPC64_GOT_TLSGD34:
16518 case R_PPC64_GOT_TLSLD34:
16519 case R_PPC64_GOT_TPREL34:
16520 case R_PPC64_GOT_DTPREL34:
5663e321
AM
16521 case R_PPC64_PLT_PCREL34:
16522 case R_PPC64_PLT_PCREL34_NOTOC:
16523 case R_PPC64_D28:
16524 case R_PPC64_PCREL28:
16525 if (rel->r_offset + 8 > input_section->size)
16526 r = bfd_reloc_outofrange;
16527 else
16528 {
5663e321
AM
16529 relocation += addend;
16530 if (howto->pc_relative)
16531 relocation -= (rel->r_offset
16532 + input_section->output_offset
16533 + input_section->output_section->vma);
16534 relocation >>= howto->rightshift;
16535
16536 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16537 pinsn <<= 32;
16538 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16539
16540 pinsn &= ~howto->dst_mask;
16541 pinsn |= (((relocation << 16) | (relocation & 0xffff))
16542 & howto->dst_mask);
16543 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
16544 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
16545 r = bfd_reloc_ok;
16546 if (howto->complain_on_overflow == complain_overflow_signed
16547 && (relocation + (1ULL << (howto->bitsize - 1))
16548 >= 1ULL << howto->bitsize))
16549 r = bfd_reloc_overflow;
16550 }
16551 break;
16552
16553 case R_PPC64_REL16DX_HA:
a680de9a
PB
16554 if (rel->r_offset + 4 > input_section->size)
16555 r = bfd_reloc_outofrange;
16556 else
16557 {
16558 relocation += addend;
16559 relocation -= (rel->r_offset
16560 + input_section->output_offset
16561 + input_section->output_section->vma);
3de43e7b 16562 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
16563 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16564 insn &= ~0x1fffc1;
3de43e7b 16565 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
16566 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16567 r = bfd_reloc_ok;
3de43e7b 16568 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
16569 r = bfd_reloc_overflow;
16570 }
5663e321
AM
16571 break;
16572
16573 default:
16574 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
16575 contents, rel->r_offset,
16576 relocation, addend);
a680de9a 16577 }
5bd4f169 16578
ef60b7ff 16579 if (r != bfd_reloc_ok)
5bd4f169 16580 {
bc30df16 16581 char *more_info = NULL;
b80eed39 16582 const char *reloc_name = howto->name;
bc30df16
AM
16583
16584 if (reloc_dest != DEST_NORMAL)
16585 {
16586 more_info = bfd_malloc (strlen (reloc_name) + 8);
16587 if (more_info != NULL)
16588 {
16589 strcpy (more_info, reloc_name);
16590 strcat (more_info, (reloc_dest == DEST_OPD
16591 ? " (OPD)" : " (stub)"));
16592 reloc_name = more_info;
16593 }
16594 }
16595
cd27b276 16596 if (r == bfd_reloc_overflow)
5bd4f169 16597 {
8131c122
AM
16598 /* On code like "if (foo) foo();" don't report overflow
16599 on a branch to zero when foo is undefined. */
16600 if (!warned
16601 && (reloc_dest == DEST_STUB
16602 || !(h != NULL
16603 && (h->elf.root.type == bfd_link_hash_undefweak
16604 || h->elf.root.type == bfd_link_hash_undefined)
16605 && is_branch_reloc (r_type))))
1a72702b
AM
16606 info->callbacks->reloc_overflow (info, &h->elf.root,
16607 sym_name, reloc_name,
16608 orig_rel.r_addend,
16609 input_bfd, input_section,
16610 rel->r_offset);
ef60b7ff
AM
16611 }
16612 else
16613 {
25f53a85 16614 info->callbacks->einfo
695344c0 16615 /* xgettext:c-format */
c1c8c1ef 16616 (_("%H: %s against `%pT': error %d\n"),
25f53a85 16617 input_bfd, input_section, rel->r_offset,
bc30df16 16618 reloc_name, sym_name, (int) r);
b34976b6 16619 ret = FALSE;
ef60b7ff 16620 }
bc30df16
AM
16621 if (more_info != NULL)
16622 free (more_info);
5bd4f169 16623 }
c316a17c
AM
16624 copy_reloc:
16625 if (wrel != rel)
16626 *wrel = *rel;
16627 }
16628
16629 if (wrel != rel)
16630 {
16631 Elf_Internal_Shdr *rel_hdr;
16632 size_t deleted = rel - wrel;
16633
16634 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16635 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16636 if (rel_hdr->sh_size == 0)
16637 {
16638 /* It is too late to remove an empty reloc section. Leave
16639 one NONE reloc.
16640 ??? What is wrong with an empty section??? */
16641 rel_hdr->sh_size = rel_hdr->sh_entsize;
16642 deleted -= 1;
16643 }
16644 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16645 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16646 input_section->reloc_count -= deleted;
5bd4f169
AM
16647 }
16648
645ea6a9
AM
16649 /* If we're emitting relocations, then shortly after this function
16650 returns, reloc offsets and addends for this section will be
16651 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
16652 file rather than the input. Save a copy of the relocs for
16653 opd_entry_value. */
0e1862bb 16654 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
16655 {
16656 bfd_size_type amt;
16657 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16658 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
16659 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16660 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
16661 if (rel == NULL)
16662 return FALSE;
16663 memcpy (rel, relocs, amt);
16664 }
5bd4f169
AM
16665 return ret;
16666}
16667
754021d0
AM
16668/* Adjust the value of any local symbols in opd sections. */
16669
6e0b88f1 16670static int
754021d0
AM
16671ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16672 const char *name ATTRIBUTE_UNUSED,
16673 Elf_Internal_Sym *elfsym,
16674 asection *input_sec,
16675 struct elf_link_hash_entry *h)
16676{
74f0fb50
AM
16677 struct _opd_sec_data *opd;
16678 long adjust;
754021d0
AM
16679 bfd_vma value;
16680
4025353c 16681 if (h != NULL)
6e0b88f1 16682 return 1;
4025353c 16683
74f0fb50
AM
16684 opd = get_opd_info (input_sec);
16685 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 16686 return 1;
754021d0
AM
16687
16688 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 16689 if (!bfd_link_relocatable (info))
754021d0
AM
16690 value -= input_sec->output_section->vma;
16691
51aecdc5 16692 adjust = opd->adjust[OPD_NDX (value)];
4025353c 16693 if (adjust == -1)
6e0b88f1
AM
16694 return 2;
16695
16696 elfsym->st_value += adjust;
16697 return 1;
754021d0
AM
16698}
16699
5bd4f169
AM
16700/* Finish up dynamic symbol handling. We set the contents of various
16701 dynamic sections here. */
16702
b34976b6 16703static bfd_boolean
4ce794b7
AM
16704ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16705 struct bfd_link_info *info,
16706 struct elf_link_hash_entry *h,
4aef7643 16707 Elf_Internal_Sym *sym)
5bd4f169 16708{
65f38f15 16709 struct ppc_link_hash_table *htab;
8387904d 16710 struct plt_entry *ent;
5bd4f169 16711
65f38f15 16712 htab = ppc_hash_table (info);
4dfe6ac6
NC
16713 if (htab == NULL)
16714 return FALSE;
5bd4f169 16715
49c09209
AM
16716 if (!htab->opd_abi && !h->def_regular)
16717 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16718 if (ent->plt.offset != (bfd_vma) -1)
16719 {
16720 /* Mark the symbol as undefined, rather than as
16721 defined in glink. Leave the value if there were
16722 any relocations where pointer equality matters
16723 (this is a clue for the dynamic linker, to make
16724 function pointer comparisons work between an
16725 application and shared library), otherwise set it
16726 to zero. */
16727 sym->st_shndx = SHN_UNDEF;
16728 if (!h->pointer_equality_needed)
16729 sym->st_value = 0;
16730 else if (!h->ref_regular_nonweak)
16731 {
16732 /* This breaks function pointer comparisons, but
16733 that is better than breaking tests for a NULL
16734 function pointer. */
16735 sym->st_value = 0;
16736 }
16737 break;
16738 }
5bd4f169 16739
f5385ebf 16740 if (h->needs_copy)
5bd4f169 16741 {
65f38f15 16742 /* This symbol needs a copy reloc. Set it up. */
49c09209 16743 Elf_Internal_Rela rela;
5474d94f 16744 asection *srel;
49c09209 16745 bfd_byte *loc;
5bd4f169 16746
65f38f15
AM
16747 if (h->dynindx == -1
16748 || (h->root.type != bfd_link_hash_defined
16749 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
16750 || htab->elf.srelbss == NULL
16751 || htab->elf.sreldynrelro == NULL)
65f38f15 16752 abort ();
5bd4f169
AM
16753
16754 rela.r_offset = (h->root.u.def.value
16755 + h->root.u.def.section->output_section->vma
16756 + h->root.u.def.section->output_offset);
16757 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16758 rela.r_addend = 0;
afbf7e8e 16759 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
16760 srel = htab->elf.sreldynrelro;
16761 else
16762 srel = htab->elf.srelbss;
16763 loc = srel->contents;
16764 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 16765 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
16766 }
16767
b34976b6 16768 return TRUE;
5bd4f169
AM
16769}
16770
65f38f15
AM
16771/* Used to decide how to sort relocs in an optimal manner for the
16772 dynamic linker, before writing them out. */
16773
16774static enum elf_reloc_type_class
7e612e98
AM
16775ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16776 const asection *rel_sec,
16777 const Elf_Internal_Rela *rela)
65f38f15 16778{
04c9666a 16779 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
16780 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16781
33e44f2e 16782 if (rel_sec == htab->elf.irelplt)
7e612e98 16783 return reloc_class_ifunc;
a33d1f77 16784
4ce794b7 16785 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 16786 switch (r_type)
65f38f15
AM
16787 {
16788 case R_PPC64_RELATIVE:
16789 return reloc_class_relative;
16790 case R_PPC64_JMP_SLOT:
16791 return reloc_class_plt;
16792 case R_PPC64_COPY:
16793 return reloc_class_copy;
16794 default:
16795 return reloc_class_normal;
16796 }
16797}
16798
5bd4f169
AM
16799/* Finish up the dynamic sections. */
16800
b34976b6 16801static bfd_boolean
4ce794b7
AM
16802ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16803 struct bfd_link_info *info)
5bd4f169 16804{
65f38f15
AM
16805 struct ppc_link_hash_table *htab;
16806 bfd *dynobj;
5bd4f169 16807 asection *sdyn;
5bd4f169 16808
65f38f15 16809 htab = ppc_hash_table (info);
4dfe6ac6
NC
16810 if (htab == NULL)
16811 return FALSE;
16812
65f38f15 16813 dynobj = htab->elf.dynobj;
3d4d4302 16814 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 16815
65f38f15 16816 if (htab->elf.dynamic_sections_created)
5bd4f169 16817 {
5bd4f169
AM
16818 Elf64_External_Dyn *dyncon, *dynconend;
16819
33e44f2e 16820 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 16821 abort ();
5bd4f169
AM
16822
16823 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 16824 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
16825 for (; dyncon < dynconend; dyncon++)
16826 {
16827 Elf_Internal_Dyn dyn;
19397422 16828 asection *s;
5bd4f169
AM
16829
16830 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16831
16832 switch (dyn.d_tag)
16833 {
65f38f15
AM
16834 default:
16835 continue;
5bd4f169 16836
5d1634d7 16837 case DT_PPC64_GLINK:
4ce794b7 16838 s = htab->glink;
6348e046 16839 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
16840 /* We stupidly defined DT_PPC64_GLINK to be the start
16841 of glink rather than the first entry point, which is
16842 what ld.so needs, and now have a bigger stub to
16843 support automatic multiple TOCs. */
9e390558 16844 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
16845 break;
16846
19397422
AM
16847 case DT_PPC64_OPD:
16848 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16849 if (s == NULL)
16850 continue;
16851 dyn.d_un.d_ptr = s->vma;
19397422
AM
16852 break;
16853
e8910a83 16854 case DT_PPC64_OPT:
5663e321
AM
16855 if ((htab->do_multi_toc && htab->multi_toc_needed)
16856 || htab->notoc_plt)
e8910a83 16857 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
16858 if (htab->has_plt_localentry0)
16859 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
16860 break;
16861
19397422
AM
16862 case DT_PPC64_OPDSZ:
16863 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16864 if (s == NULL)
16865 continue;
eea6121a 16866 dyn.d_un.d_val = s->size;
19397422
AM
16867 break;
16868
65f38f15 16869 case DT_PLTGOT:
33e44f2e 16870 s = htab->elf.splt;
6348e046 16871 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
16872 break;
16873
16874 case DT_JMPREL:
33e44f2e 16875 s = htab->elf.srelplt;
6348e046 16876 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 16877 break;
5bd4f169 16878
65f38f15 16879 case DT_PLTRELSZ:
33e44f2e 16880 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 16881 break;
82e66161
AM
16882
16883 case DT_TEXTREL:
16884 if (htab->local_ifunc_resolver)
16885 info->callbacks->einfo
16886 (_("%X%P: text relocations and GNU indirect "
16887 "functions will result in a segfault at runtime\n"));
16888 else if (htab->maybe_local_ifunc_resolver)
16889 info->callbacks->einfo
16890 (_("%P: warning: text relocations and GNU indirect "
16891 "functions may result in a segfault at runtime\n"));
16892 continue;
5bd4f169 16893 }
5bd4f169 16894
65f38f15 16895 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 16896 }
5bd4f169
AM
16897 }
16898
6528b6eb
AM
16899 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16900 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16901 {
16902 /* Fill in the first entry in the global offset table.
16903 We use it to hold the link-time TOCbase. */
16904 bfd_put_64 (output_bfd,
60ee0d4a 16905 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 16906 htab->elf.sgot->contents);
5d1634d7
AM
16907
16908 /* Set .got entry size. */
2cdcc330
AM
16909 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16910 = 8;
5d1634d7
AM
16911 }
16912
6528b6eb
AM
16913 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16914 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16915 {
16916 /* Set .plt entry size. */
33e44f2e 16917 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 16918 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
16919 }
16920
84f5d08e
AM
16921 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16922 brlt ourselves if emitrelocations. */
16923 if (htab->brlt != NULL
16924 && htab->brlt->reloc_count != 0
16925 && !_bfd_elf_link_output_relocs (output_bfd,
16926 htab->brlt,
d4730f92 16927 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
16928 elf_section_data (htab->brlt)->relocs,
16929 NULL))
16930 return FALSE;
16931
176a0d42
AM
16932 if (htab->glink != NULL
16933 && htab->glink->reloc_count != 0
16934 && !_bfd_elf_link_output_relocs (output_bfd,
16935 htab->glink,
d4730f92 16936 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
16937 elf_section_data (htab->glink)->relocs,
16938 NULL))
16939 return FALSE;
16940
da44f4e5 16941
df136d64
AM
16942 if (htab->glink_eh_frame != NULL
16943 && htab->glink_eh_frame->size != 0
16944 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16945 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16946 htab->glink_eh_frame,
16947 htab->glink_eh_frame->contents))
16948 return FALSE;
58d180e8 16949
e717da7e 16950 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
16951 since we didn't add them to DYNOBJ. We know dynobj is the first
16952 bfd. */
c72f2fb2 16953 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
16954 {
16955 asection *s;
7b53ace3 16956
0c8d6e5c 16957 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
16958 continue;
16959
e717da7e
AM
16960 s = ppc64_elf_tdata (dynobj)->got;
16961 if (s != NULL
eea6121a 16962 && s->size != 0
e717da7e
AM
16963 && s->output_section != bfd_abs_section_ptr
16964 && !bfd_set_section_contents (output_bfd, s->output_section,
16965 s->contents, s->output_offset,
eea6121a 16966 s->size))
e717da7e
AM
16967 return FALSE;
16968 s = ppc64_elf_tdata (dynobj)->relgot;
16969 if (s != NULL
eea6121a 16970 && s->size != 0
e717da7e
AM
16971 && s->output_section != bfd_abs_section_ptr
16972 && !bfd_set_section_contents (output_bfd, s->output_section,
16973 s->contents, s->output_offset,
eea6121a 16974 s->size))
e717da7e
AM
16975 return FALSE;
16976 }
f6c52c13 16977
b34976b6 16978 return TRUE;
5bd4f169
AM
16979}
16980
5bd4f169 16981#include "elf64-target.h"
7b8e7dad
AM
16982
16983/* FreeBSD support */
16984
16985#undef TARGET_LITTLE_SYM
16986#undef TARGET_LITTLE_NAME
16987
16988#undef TARGET_BIG_SYM
6d00b590 16989#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
16990#undef TARGET_BIG_NAME
16991#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16992
16993#undef ELF_OSABI
16994#define ELF_OSABI ELFOSABI_FREEBSD
16995
16996#undef elf64_bed
16997#define elf64_bed elf64_powerpc_fbsd_bed
16998
16999#include "elf64-target.h"
This page took 4.362998 seconds and 4 git commands to generate.