Dynamic TLS section symbols
[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
923 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
924 bfd_elf_generic_reloc),
925
926 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
927 ppc64_elf_ha_reloc),
928
929 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
930 bfd_elf_generic_reloc),
931
932 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
933 ppc64_elf_ha_reloc),
934
935 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
936 bfd_elf_generic_reloc),
937
938 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
939 ppc64_elf_ha_reloc),
940
941 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
942 bfd_elf_generic_reloc),
943
944 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
945 ppc64_elf_ha_reloc),
946
947 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
948 ppc64_elf_prefix_reloc),
949
950 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
951 ppc64_elf_prefix_reloc),
952
5bd4f169 953 /* GNU extension to record C++ vtable hierarchy. */
46807bf4
AM
954 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
955 NULL),
5bd4f169
AM
956
957 /* GNU extension to record C++ vtable member usage. */
46807bf4
AM
958 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
959 NULL),
5bd4f169
AM
960};
961
962\f
963/* Initialize the ppc64_elf_howto_table, so that linear accesses can
964 be done. */
965
966static void
4ce794b7 967ppc_howto_init (void)
5bd4f169
AM
968{
969 unsigned int i, type;
970
a4b6fadd 971 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
972 {
973 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 974 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
975 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
976 }
977}
978
979static reloc_howto_type *
f3185997 980ppc64_elf_reloc_type_lookup (bfd *abfd,
4ce794b7 981 bfd_reloc_code_real_type code)
5bd4f169 982{
411e1bfb 983 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
984
985 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
986 /* Initialize howto table if needed. */
987 ppc_howto_init ();
988
4ce794b7 989 switch (code)
5bd4f169
AM
990 {
991 default:
f3185997 992 /* xgettext:c-format */
2cdcc330
AM
993 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
994 (int) code);
f3185997 995 bfd_set_error (bfd_error_bad_value);
4ce794b7 996 return NULL;
5bd4f169 997
411e1bfb
AM
998 case BFD_RELOC_NONE: r = R_PPC64_NONE;
999 break;
1000 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1001 break;
1002 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1003 break;
1004 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1005 break;
1006 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1007 break;
1008 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1009 break;
f9c6b907
AM
1010 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1011 break;
411e1bfb 1012 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 1013 break;
f9c6b907
AM
1014 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1015 break;
411e1bfb 1016 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 1017 break;
411e1bfb 1018 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 1019 break;
411e1bfb 1020 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 1021 break;
411e1bfb 1022 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 1023 break;
05d0e962
AM
1024 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1025 break;
411e1bfb 1026 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 1027 break;
411e1bfb 1028 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 1029 break;
411e1bfb 1030 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 1031 break;
411e1bfb 1032 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 1033 break;
411e1bfb 1034 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 1035 break;
411e1bfb 1036 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 1037 break;
411e1bfb 1038 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 1039 break;
411e1bfb 1040 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 1041 break;
411e1bfb 1042 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 1043 break;
411e1bfb 1044 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 1045 break;
411e1bfb 1046 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 1047 break;
411e1bfb 1048 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 1049 break;
411e1bfb 1050 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 1051 break;
411e1bfb 1052 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 1053 break;
411e1bfb 1054 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 1055 break;
411e1bfb 1056 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 1057 break;
411e1bfb 1058 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 1059 break;
411e1bfb 1060 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 1061 break;
411e1bfb 1062 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 1063 break;
411e1bfb 1064 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 1065 break;
411e1bfb 1066 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 1067 break;
411e1bfb 1068 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 1069 break;
411e1bfb 1070 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 1071 break;
411e1bfb 1072 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1073 break;
411e1bfb 1074 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1075 break;
411e1bfb 1076 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1077 break;
411e1bfb 1078 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1079 break;
411e1bfb 1080 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1081 break;
411e1bfb 1082 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1083 break;
411e1bfb 1084 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1085 break;
411e1bfb 1086 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1087 break;
411e1bfb 1088 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1089 break;
411e1bfb 1090 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1091 break;
411e1bfb 1092 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1093 break;
411e1bfb 1094 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1095 break;
411e1bfb 1096 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1097 break;
411e1bfb 1098 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1099 break;
411e1bfb 1100 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1101 break;
411e1bfb 1102 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1103 break;
411e1bfb 1104 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1105 break;
411e1bfb 1106 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1107 break;
411e1bfb 1108 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1109 break;
411e1bfb 1110 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1111 break;
411e1bfb 1112 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1113 break;
411e1bfb 1114 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1115 break;
411e1bfb 1116 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1117 break;
411e1bfb 1118 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1119 break;
411e1bfb 1120 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1121 break;
411e1bfb 1122 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1123 break;
727fc41e
AM
1124 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1125 break;
1126 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1127 break;
411e1bfb 1128 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1129 break;
411e1bfb 1130 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1131 break;
411e1bfb 1132 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1133 break;
411e1bfb 1134 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1135 break;
f9c6b907
AM
1136 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1137 break;
411e1bfb 1138 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1139 break;
f9c6b907
AM
1140 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1141 break;
411e1bfb 1142 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1143 break;
411e1bfb
AM
1144 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1145 break;
1146 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1147 break;
1148 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1149 break;
f9c6b907
AM
1150 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1151 break;
411e1bfb
AM
1152 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1153 break;
f9c6b907
AM
1154 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1155 break;
411e1bfb
AM
1156 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1157 break;
1158 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1159 break;
1160 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1161 break;
1162 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1163 break;
1164 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1165 break;
1166 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1167 break;
1168 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1169 break;
1170 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1171 break;
1172 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1173 break;
1174 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1175 break;
1176 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1177 break;
1178 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1179 break;
1180 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1181 break;
1182 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1183 break;
1184 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1185 break;
1186 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1187 break;
1188 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1189 break;
1190 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1191 break;
1192 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1193 break;
1194 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1195 break;
1196 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1197 break;
1198 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1199 break;
1200 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1201 break;
1202 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1203 break;
1204 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1205 break;
1206 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1207 break;
1208 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1209 break;
1210 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1211 break;
1212 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1213 break;
25f23106
AM
1214 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1215 break;
1216 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1217 break;
1218 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1219 break;
1220 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1221 break;
4a969973
AM
1222 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1223 break;
1224 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1225 break;
1226 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1227 break;
1228 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1229 break;
1230 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1231 break;
1232 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1233 break;
7ba71655
AM
1234 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1235 break;
a680de9a
PB
1236 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1237 break;
006589cf
AM
1238 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1239 break;
45965137
AM
1240 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1241 break;
5663e321
AM
1242 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1243 break;
1244 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1245 break;
1246 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1247 break;
1248 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1249 break;
1250 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1251 break;
1252 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1253 break;
1254 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1255 break;
1256 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1257 break;
1258 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1259 break;
1260 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1261 break;
1262 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1263 break;
1264 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1265 break;
1266 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1267 break;
1268 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1269 break;
1270 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1271 break;
1272 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1273 break;
1274 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1275 break;
411e1bfb
AM
1276 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1277 break;
1278 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
1279 break;
1280 }
1281
4ce794b7 1282 return ppc64_elf_howto_table[r];
5bd4f169
AM
1283};
1284
157090f7
AM
1285static reloc_howto_type *
1286ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1287 const char *r_name)
1288{
1289 unsigned int i;
1290
a4b6fadd 1291 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
1292 if (ppc64_elf_howto_raw[i].name != NULL
1293 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1294 return &ppc64_elf_howto_raw[i];
1295
1296 return NULL;
1297}
1298
5bd4f169
AM
1299/* Set the howto pointer for a PowerPC ELF reloc. */
1300
f3185997 1301static bfd_boolean
4aef7643 1302ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 1303 Elf_Internal_Rela *dst)
5bd4f169 1304{
65f38f15
AM
1305 unsigned int type;
1306
ef60b7ff 1307 /* Initialize howto table if needed. */
5bd4f169 1308 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
1309 ppc_howto_init ();
1310
65f38f15 1311 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 1312 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 1313 {
695344c0 1314 /* xgettext:c-format */
0aa13fee 1315 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 1316 abfd, type);
f3185997
NC
1317 bfd_set_error (bfd_error_bad_value);
1318 return FALSE;
d0fb9a8d 1319 }
65f38f15 1320 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
1321 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1322 {
1323 /* xgettext:c-format */
1324 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1325 abfd, type);
1326 bfd_set_error (bfd_error_bad_value);
1327 return FALSE;
1328 }
2cdcc330 1329
f3185997 1330 return TRUE;
5bd4f169
AM
1331}
1332
04c9666a 1333/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
1334
1335static bfd_reloc_status_type
4ce794b7
AM
1336ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1337 void *data, asection *input_section,
1338 bfd *output_bfd, char **error_message)
5bd4f169 1339{
a680de9a
PB
1340 enum elf_ppc64_reloc_type r_type;
1341 long insn;
1342 bfd_size_type octets;
3de43e7b 1343 bfd_vma value;
a680de9a 1344
805fc799
AM
1345 /* If this is a relocatable link (output_bfd test tells us), just
1346 call the generic function. Any adjustment will be done at final
1347 link time. */
1348 if (output_bfd != NULL)
cedb70c5 1349 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1350 input_section, output_bfd, error_message);
1351
5663e321
AM
1352 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1353 We won't actually be using the low bits, so trashing them
805fc799 1354 doesn't matter. */
a680de9a 1355 r_type = reloc_entry->howto->type;
5663e321
AM
1356 if (r_type == R_PPC64_ADDR16_HIGHERA34
1357 || r_type == R_PPC64_ADDR16_HIGHESTA34
1358 || r_type == R_PPC64_REL16_HIGHERA34
1359 || r_type == R_PPC64_REL16_HIGHESTA34)
1360 reloc_entry->addend += 1ULL << 33;
1361 else
1362 reloc_entry->addend += 1U << 15;
a680de9a
PB
1363 if (r_type != R_PPC64_REL16DX_HA)
1364 return bfd_reloc_continue;
1365
1366 value = 0;
1367 if (!bfd_is_com_section (symbol->section))
1368 value = symbol->value;
1369 value += (reloc_entry->addend
1370 + symbol->section->output_offset
1371 + symbol->section->output_section->vma);
1372 value -= (reloc_entry->address
1373 + input_section->output_offset
1374 + input_section->output_section->vma);
3de43e7b 1375 value = (bfd_signed_vma) value >> 16;
a680de9a
PB
1376
1377 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1378 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1379 insn &= ~0x1fffc1;
3de43e7b 1380 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 1381 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 1382 if (value + 0x8000 > 0xffff)
a680de9a
PB
1383 return bfd_reloc_overflow;
1384 return bfd_reloc_ok;
805fc799 1385}
5bd4f169 1386
2441e016
AM
1387static bfd_reloc_status_type
1388ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1389 void *data, asection *input_section,
1390 bfd *output_bfd, char **error_message)
1391{
1392 if (output_bfd != NULL)
1393 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1394 input_section, output_bfd, error_message);
1395
699733f6
AM
1396 if (strcmp (symbol->section->name, ".opd") == 0
1397 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
1398 {
1399 bfd_vma dest = opd_entry_value (symbol->section,
1400 symbol->value + reloc_entry->addend,
aef36ac1 1401 NULL, NULL, FALSE);
2441e016
AM
1402 if (dest != (bfd_vma) -1)
1403 reloc_entry->addend = dest - (symbol->value
1404 + symbol->section->output_section->vma
1405 + symbol->section->output_offset);
1406 }
810d4e75
AM
1407 else
1408 {
1409 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1410
1411 if (symbol->section->owner != abfd
9f284bf9 1412 && symbol->section->owner != NULL
810d4e75
AM
1413 && abiversion (symbol->section->owner) >= 2)
1414 {
1415 unsigned int i;
1416
1417 for (i = 0; i < symbol->section->owner->symcount; ++i)
1418 {
1419 asymbol *symdef = symbol->section->owner->outsymbols[i];
1420
1421 if (strcmp (symdef->name, symbol->name) == 0)
1422 {
1423 elfsym = (elf_symbol_type *) symdef;
1424 break;
1425 }
1426 }
1427 }
1428 reloc_entry->addend
1429 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1430 }
2441e016
AM
1431 return bfd_reloc_continue;
1432}
1433
805fc799 1434static bfd_reloc_status_type
4ce794b7
AM
1435ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1436 void *data, asection *input_section,
1437 bfd *output_bfd, char **error_message)
805fc799
AM
1438{
1439 long insn;
04c9666a 1440 enum elf_ppc64_reloc_type r_type;
805fc799 1441 bfd_size_type octets;
794e51c0
AM
1442 /* Assume 'at' branch hints. */
1443 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
1444
1445 /* If this is a relocatable link (output_bfd test tells us), just
1446 call the generic function. Any adjustment will be done at final
1447 link time. */
5bd4f169 1448 if (output_bfd != NULL)
cedb70c5 1449 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1450 input_section, output_bfd, error_message);
1451
1452 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1453 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1454 insn &= ~(0x01 << 21);
4ce794b7 1455 r_type = reloc_entry->howto->type;
805fc799
AM
1456 if (r_type == R_PPC64_ADDR14_BRTAKEN
1457 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 1458 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 1459
794e51c0 1460 if (is_isa_v2)
5bd4f169 1461 {
805fc799
AM
1462 /* Set 'a' bit. This is 0b00010 in BO field for branch
1463 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1464 for branch on CTR insns (BO == 1a00t or 1a01t). */
1465 if ((insn & (0x14 << 21)) == (0x04 << 21))
1466 insn |= 0x02 << 21;
1467 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1468 insn |= 0x08 << 21;
1469 else
2441e016 1470 goto out;
5bd4f169 1471 }
805fc799
AM
1472 else
1473 {
1474 bfd_vma target = 0;
1475 bfd_vma from;
5bd4f169 1476
805fc799
AM
1477 if (!bfd_is_com_section (symbol->section))
1478 target = symbol->value;
1479 target += symbol->section->output_section->vma;
1480 target += symbol->section->output_offset;
1481 target += reloc_entry->addend;
5bd4f169 1482
805fc799
AM
1483 from = (reloc_entry->address
1484 + input_section->output_offset
1485 + input_section->output_section->vma);
5bd4f169 1486
805fc799
AM
1487 /* Invert 'y' bit if not the default. */
1488 if ((bfd_signed_vma) (target - from) < 0)
1489 insn ^= 0x01 << 21;
1490 }
4ce794b7 1491 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
1492 out:
1493 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1494 input_section, output_bfd, error_message);
805fc799 1495}
5bd4f169 1496
805fc799 1497static bfd_reloc_status_type
4ce794b7
AM
1498ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1499 void *data, asection *input_section,
1500 bfd *output_bfd, char **error_message)
805fc799
AM
1501{
1502 /* If this is a relocatable link (output_bfd test tells us), just
1503 call the generic function. Any adjustment will be done at final
1504 link time. */
1505 if (output_bfd != NULL)
cedb70c5 1506 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 1507 input_section, output_bfd, error_message);
5bd4f169 1508
805fc799
AM
1509 /* Subtract the symbol section base address. */
1510 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
1511 return bfd_reloc_continue;
1512}
1513
805fc799 1514static bfd_reloc_status_type
4ce794b7
AM
1515ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1516 void *data, asection *input_section,
1517 bfd *output_bfd, char **error_message)
805fc799
AM
1518{
1519 /* If this is a relocatable link (output_bfd test tells us), just
1520 call the generic function. Any adjustment will be done at final
1521 link time. */
1522 if (output_bfd != NULL)
cedb70c5 1523 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1524 input_section, output_bfd, error_message);
1525
1526 /* Subtract the symbol section base address. */
1527 reloc_entry->addend -= symbol->section->output_section->vma;
1528
1529 /* Adjust the addend for sign extension of the low 16 bits. */
1530 reloc_entry->addend += 0x8000;
1531 return bfd_reloc_continue;
1532}
1533
1534static bfd_reloc_status_type
4ce794b7
AM
1535ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1536 void *data, asection *input_section,
1537 bfd *output_bfd, char **error_message)
805fc799
AM
1538{
1539 bfd_vma TOCstart;
1540
1541 /* If this is a relocatable link (output_bfd test tells us), just
1542 call the generic function. Any adjustment will be done at final
1543 link time. */
1544 if (output_bfd != NULL)
cedb70c5 1545 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1546 input_section, output_bfd, error_message);
1547
1548 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1549 if (TOCstart == 0)
1c865ab2 1550 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1551
1552 /* Subtract the TOC base address. */
1553 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1554 return bfd_reloc_continue;
1555}
1556
1557static bfd_reloc_status_type
4ce794b7
AM
1558ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1559 void *data, asection *input_section,
1560 bfd *output_bfd, char **error_message)
805fc799
AM
1561{
1562 bfd_vma TOCstart;
1563
1564 /* If this is a relocatable link (output_bfd test tells us), just
1565 call the generic function. Any adjustment will be done at final
1566 link time. */
1567 if (output_bfd != NULL)
cedb70c5 1568 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1569 input_section, output_bfd, error_message);
1570
1571 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1572 if (TOCstart == 0)
1c865ab2 1573 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1574
1575 /* Subtract the TOC base address. */
1576 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1577
1578 /* Adjust the addend for sign extension of the low 16 bits. */
1579 reloc_entry->addend += 0x8000;
1580 return bfd_reloc_continue;
1581}
1582
1583static bfd_reloc_status_type
4ce794b7
AM
1584ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1585 void *data, asection *input_section,
1586 bfd *output_bfd, char **error_message)
805fc799
AM
1587{
1588 bfd_vma TOCstart;
1589 bfd_size_type octets;
1590
1591 /* If this is a relocatable link (output_bfd test tells us), just
1592 call the generic function. Any adjustment will be done at final
1593 link time. */
1594 if (output_bfd != NULL)
cedb70c5 1595 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1596 input_section, output_bfd, error_message);
1597
1598 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1599 if (TOCstart == 0)
1c865ab2 1600 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1601
1602 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1603 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1604 return bfd_reloc_ok;
1605}
1606
5663e321
AM
1607static bfd_reloc_status_type
1608ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1609 void *data, asection *input_section,
1610 bfd *output_bfd, char **error_message)
1611{
1612 uint64_t insn;
1613 bfd_vma targ;
1614
1615 if (output_bfd != NULL)
1616 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1617 input_section, output_bfd, error_message);
1618
1619 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1620 insn <<= 32;
1621 insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1622
1623 targ = (symbol->section->output_section->vma
1624 + symbol->section->output_offset
1625 + reloc_entry->addend);
1626 if (!bfd_is_com_section (symbol->section))
1627 targ += symbol->value;
1628 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1629 targ += 1ULL << 33;
1630 if (reloc_entry->howto->pc_relative)
1631 {
1632 bfd_vma from = (reloc_entry->address
1633 + input_section->output_offset
1634 + input_section->output_section->vma);
1635 targ -=from;
1636 }
1637 targ >>= reloc_entry->howto->rightshift;
1638 insn &= ~reloc_entry->howto->dst_mask;
1639 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1640 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1641 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1642 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1643 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1644 >= 1ULL << reloc_entry->howto->bitsize))
1645 return bfd_reloc_overflow;
1646 return bfd_reloc_ok;
1647}
1648
805fc799 1649static bfd_reloc_status_type
4ce794b7
AM
1650ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1651 void *data, asection *input_section,
1652 bfd *output_bfd, char **error_message)
805fc799
AM
1653{
1654 /* If this is a relocatable link (output_bfd test tells us), just
1655 call the generic function. Any adjustment will be done at final
1656 link time. */
1657 if (output_bfd != NULL)
cedb70c5 1658 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1659 input_section, output_bfd, error_message);
1660
1661 if (error_message != NULL)
1662 {
1663 static char buf[60];
1664 sprintf (buf, "generic linker can't handle %s",
1665 reloc_entry->howto->name);
1666 *error_message = buf;
1667 }
1668 return bfd_reloc_dangerous;
1669}
1670
927be08e
AM
1671/* Track GOT entries needed for a given symbol. We might need more
1672 than one got entry per symbol. */
1673struct got_entry
1674{
1675 struct got_entry *next;
1676
1677 /* The symbol addend that we'll be placing in the GOT. */
1678 bfd_vma addend;
1679
1680 /* Unlike other ELF targets, we use separate GOT entries for the same
1681 symbol referenced from different input files. This is to support
1682 automatic multiple TOC/GOT sections, where the TOC base can vary
1683 from one input file to another. After partitioning into TOC groups
1684 we merge entries within the group.
1685
1686 Point to the BFD owning this GOT entry. */
1687 bfd *owner;
1688
1689 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1690 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1691 unsigned char tls_type;
927be08e
AM
1692
1693 /* Non-zero if got.ent points to real entry. */
f961d9dd 1694 unsigned char is_indirect;
927be08e
AM
1695
1696 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1697 union
2cdcc330
AM
1698 {
1699 bfd_signed_vma refcount;
1700 bfd_vma offset;
1701 struct got_entry *ent;
1702 } got;
927be08e
AM
1703};
1704
1705/* The same for PLT. */
1706struct plt_entry
1707{
1708 struct plt_entry *next;
1709
1710 bfd_vma addend;
1711
1712 union
2cdcc330
AM
1713 {
1714 bfd_signed_vma refcount;
1715 bfd_vma offset;
1716 } plt;
927be08e
AM
1717};
1718
e717da7e
AM
1719struct ppc64_elf_obj_tdata
1720{
1721 struct elf_obj_tdata elf;
1722
1723 /* Shortcuts to dynamic linker sections. */
1724 asection *got;
1725 asection *relgot;
1726
b3fac117
AM
1727 /* Used during garbage collection. We attach global symbols defined
1728 on removed .opd entries to this section so that the sym is removed. */
1729 asection *deleted_section;
81688140 1730
927be08e 1731 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1732 sections means we potentially need one of these for each input bfd. */
927be08e 1733 struct got_entry tlsld_got;
8860955f 1734
2cdcc330
AM
1735 union
1736 {
729eabd5
AM
1737 /* A copy of relocs before they are modified for --emit-relocs. */
1738 Elf_Internal_Rela *relocs;
1739
1740 /* Section contents. */
1741 bfd_byte *contents;
1742 } opd;
d77c8a4b
AM
1743
1744 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1745 the reloc to be in the range -32768 to 32767. */
98528052
AM
1746 unsigned int has_small_toc_reloc : 1;
1747
560c8763
AM
1748 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1749 instruction not one we handle. */
1750 unsigned int unexpected_toc_insn : 1;
066f4018
AM
1751
1752 /* Set if got relocs that can be optimised are present in this file. */
1753 unsigned int has_gotrel : 1;
e717da7e
AM
1754};
1755
1756#define ppc64_elf_tdata(bfd) \
1757 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1758
1759#define ppc64_tlsld_got(bfd) \
1760 (&ppc64_elf_tdata (bfd)->tlsld_got)
1761
0c8d6e5c
AM
1762#define is_ppc64_elf(bfd) \
1763 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1764 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1765
e717da7e
AM
1766/* Override the generic function because we store some extras. */
1767
1768static bfd_boolean
1769ppc64_elf_mkobject (bfd *abfd)
1770{
0ffa91dd 1771 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1772 PPC64_ELF_DATA);
e717da7e
AM
1773}
1774
feee612b 1775/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1776 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1777
b34976b6 1778static bfd_boolean
4ce794b7 1779ppc64_elf_object_p (bfd *abfd)
feee612b 1780{
14b57c7c
AM
1781 if (!abfd->arch_info->the_default)
1782 return TRUE;
1783
1784 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1785 {
1786 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1787
1788 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1789 {
1790 /* Relies on arch after 32 bit default being 64 bit default. */
1791 abfd->arch_info = abfd->arch_info->next;
1792 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1793 }
1794 }
14b57c7c 1795 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1796}
1797
d37c89e5
AM
1798/* Support for core dump NOTE sections. */
1799
1800static bfd_boolean
1801ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1802{
eea6121a 1803 size_t offset, size;
d37c89e5
AM
1804
1805 if (note->descsz != 504)
1806 return FALSE;
1807
1808 /* pr_cursig */
228e534f 1809 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1810
1811 /* pr_pid */
228e534f 1812 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1813
1814 /* pr_reg */
1815 offset = 112;
eea6121a 1816 size = 384;
d37c89e5
AM
1817
1818 /* Make a ".reg/999" section. */
1819 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1820 size, note->descpos + offset);
d37c89e5
AM
1821}
1822
1823static bfd_boolean
1824ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1825{
1826 if (note->descsz != 136)
1827 return FALSE;
1828
228e534f 1829 elf_tdata (abfd)->core->pid
bc989cdc 1830 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1831 elf_tdata (abfd)->core->program
d37c89e5 1832 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1833 elf_tdata (abfd)->core->command
d37c89e5
AM
1834 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1835
1836 return TRUE;
1837}
1838
183e98be
AM
1839static char *
1840ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1841 ...)
1842{
1843 switch (note_type)
1844 {
1845 default:
1846 return NULL;
1847
1848 case NT_PRPSINFO:
1849 {
9ef6d1e3 1850 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1851 va_list ap;
1852
1853 va_start (ap, note_type);
75cd47ed 1854 memset (data, 0, sizeof (data));
183e98be 1855 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1856#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1857 DIAGNOSTIC_PUSH;
be3e27bb 1858 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1859 -Wstringop-truncation:
1860 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1861 */
95da9854
L
1862 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1863#endif
183e98be 1864 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1865#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1866 DIAGNOSTIC_POP;
fe75810f 1867#endif
183e98be
AM
1868 va_end (ap);
1869 return elfcore_write_note (abfd, buf, bufsiz,
1870 "CORE", note_type, data, sizeof (data));
1871 }
1872
1873 case NT_PRSTATUS:
1874 {
1875 char data[504];
1876 va_list ap;
1877 long pid;
1878 int cursig;
1879 const void *greg;
1880
1881 va_start (ap, note_type);
1882 memset (data, 0, 112);
1883 pid = va_arg (ap, long);
1884 bfd_put_32 (abfd, pid, data + 32);
1885 cursig = va_arg (ap, int);
1886 bfd_put_16 (abfd, cursig, data + 12);
1887 greg = va_arg (ap, const void *);
1888 memcpy (data + 112, greg, 384);
1889 memset (data + 496, 0, 8);
1890 va_end (ap);
1891 return elfcore_write_note (abfd, buf, bufsiz,
1892 "CORE", note_type, data, sizeof (data));
1893 }
1894 }
1895}
1896
5d35169e
AM
1897/* Add extra PPC sections. */
1898
2cdcc330 1899static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1900{
07d6d2b8
AM
1901 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1902 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1903 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1904 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1905 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1906 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1907 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1908};
1909
7c8fe5c4
AM
1910enum _ppc64_sec_type {
1911 sec_normal = 0,
1912 sec_opd = 1,
1913 sec_toc = 2
1914};
1915
f0abc2a1
AM
1916struct _ppc64_elf_section_data
1917{
1918 struct bfd_elf_section_data elf;
411e1bfb 1919
f0abc2a1
AM
1920 union
1921 {
51aecdc5
AM
1922 /* An array with one entry for each opd function descriptor,
1923 and some spares since opd entries may be either 16 or 24 bytes. */
1924#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
1925 struct _opd_sec_data
1926 {
1927 /* Points to the function code section for local opd entries. */
1928 asection **func_sec;
1929
1930 /* After editing .opd, adjust references to opd local syms. */
1931 long *adjust;
1932 } opd;
7c8fe5c4 1933
3a71aa26
AM
1934 /* An array for toc sections, indexed by offset/8. */
1935 struct _toc_sec_data
1936 {
1937 /* Specifies the relocation symbol index used at a given toc offset. */
1938 unsigned *symndx;
1939
1940 /* And the relocation addend. */
1941 bfd_vma *add;
1942 } toc;
7c8fe5c4
AM
1943 } u;
1944
1945 enum _ppc64_sec_type sec_type:2;
411e1bfb 1946
7c8fe5c4
AM
1947 /* Flag set when small branches are detected. Used to
1948 select suitable defaults for the stub group size. */
1949 unsigned int has_14bit_branch:1;
3e04d765
AM
1950
1951 /* Flag set when PLTCALL relocs are detected. */
1952 unsigned int has_pltcall:1;
066f4018
AM
1953
1954 /* Flag set when section has GOT relocations that can be optimised. */
1955 unsigned int has_gotrel:1;
f0abc2a1
AM
1956};
1957
1958#define ppc64_elf_section_data(sec) \
411e1bfb 1959 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
1960
1961static bfd_boolean
4ce794b7 1962ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1963{
f592407e
AM
1964 if (!sec->used_by_bfd)
1965 {
1966 struct _ppc64_elf_section_data *sdata;
1967 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1968
f592407e
AM
1969 sdata = bfd_zalloc (abfd, amt);
1970 if (sdata == NULL)
1971 return FALSE;
1972 sec->used_by_bfd = sdata;
1973 }
f0abc2a1
AM
1974
1975 return _bfd_elf_new_section_hook (abfd, sec);
1976}
4025353c 1977
74f0fb50 1978static struct _opd_sec_data *
4025353c
AM
1979get_opd_info (asection * sec)
1980{
1981 if (sec != NULL
1982 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 1983 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 1984 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
1985 return NULL;
1986}
90e3cdf2
JJ
1987\f
1988/* Parameters for the qsort hook. */
90e3cdf2 1989static bfd_boolean synthetic_relocatable;
cd285db5 1990static asection *synthetic_opd;
90e3cdf2 1991
699733f6 1992/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
1993
1994static int
1995compare_symbols (const void *ap, const void *bp)
1996{
2cdcc330
AM
1997 const asymbol *a = *(const asymbol **) ap;
1998 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 1999
699733f6
AM
2000 /* Section symbols first. */
2001 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2002 return -1;
699733f6 2003 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2004 return 1;
2005
699733f6 2006 /* then .opd symbols. */
cd285db5
AM
2007 if (synthetic_opd != NULL)
2008 {
2009 if (strcmp (a->section->name, ".opd") == 0
2010 && strcmp (b->section->name, ".opd") != 0)
2011 return -1;
2012 if (strcmp (a->section->name, ".opd") != 0
2013 && strcmp (b->section->name, ".opd") == 0)
2014 return 1;
2015 }
90e3cdf2 2016
699733f6 2017 /* then other code symbols. */
2cdcc330
AM
2018 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2019 == (SEC_CODE | SEC_ALLOC))
2020 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2021 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2022 return -1;
2023
2cdcc330
AM
2024 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2025 != (SEC_CODE | SEC_ALLOC))
2026 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2027 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2028 return 1;
2029
2030 if (synthetic_relocatable)
2031 {
2032 if (a->section->id < b->section->id)
2033 return -1;
2034
2035 if (a->section->id > b->section->id)
2036 return 1;
2037 }
2038
2039 if (a->value + a->section->vma < b->value + b->section->vma)
2040 return -1;
2041
2042 if (a->value + a->section->vma > b->value + b->section->vma)
2043 return 1;
2044
4d35a0aa
AM
2045 /* For syms with the same value, prefer strong dynamic global function
2046 syms over other syms. */
2047 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2048 return -1;
2049
2050 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2051 return 1;
2052
2053 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2054 return -1;
2055
2056 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2057 return 1;
2058
2059 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2060 return -1;
2061
2062 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2063 return 1;
2064
2065 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2066 return -1;
2067
2068 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2069 return 1;
2070
aaed6f5b 2071 return a > b;
90e3cdf2
JJ
2072}
2073
699733f6 2074/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2075
699733f6 2076static asymbol *
7292b3ac 2077sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 2078{
699733f6 2079 long mid;
90e3cdf2 2080
7292b3ac 2081 if (id == (unsigned) -1)
699733f6
AM
2082 {
2083 while (lo < hi)
2084 {
2085 mid = (lo + hi) >> 1;
2086 if (syms[mid]->value + syms[mid]->section->vma < value)
2087 lo = mid + 1;
2088 else if (syms[mid]->value + syms[mid]->section->vma > value)
2089 hi = mid;
2090 else
2091 return syms[mid];
2092 }
2093 }
2094 else
2095 {
2096 while (lo < hi)
2097 {
2098 mid = (lo + hi) >> 1;
2099 if (syms[mid]->section->id < id)
2100 lo = mid + 1;
2101 else if (syms[mid]->section->id > id)
2102 hi = mid;
2103 else if (syms[mid]->value < value)
2104 lo = mid + 1;
2105 else if (syms[mid]->value > value)
2106 hi = mid;
2107 else
2108 return syms[mid];
2109 }
2110 }
2111 return NULL;
90e3cdf2
JJ
2112}
2113
468392fb
AM
2114static bfd_boolean
2115section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2116{
2117 bfd_vma vma = *(bfd_vma *) ptr;
2118 return ((section->flags & SEC_ALLOC) != 0
2119 && section->vma <= vma
2120 && vma < section->vma + section->size);
2121}
2122
699733f6 2123/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
2124 entry syms. Also generate @plt symbols for the glink branch table.
2125 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
2126
2127static long
a7535cf3
AM
2128ppc64_elf_get_synthetic_symtab (bfd *abfd,
2129 long static_count, asymbol **static_syms,
2130 long dyn_count, asymbol **dyn_syms,
c9727e01 2131 asymbol **ret)
90e3cdf2
JJ
2132{
2133 asymbol *s;
0ccf57bd 2134 size_t i, j, count;
90e3cdf2 2135 char *names;
0ccf57bd 2136 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 2137 asection *opd = NULL;
90e3cdf2 2138 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2139 asymbol **syms;
ee67d69a 2140 int abi = abiversion (abfd);
90e3cdf2
JJ
2141
2142 *ret = NULL;
2143
ee67d69a
AM
2144 if (abi < 2)
2145 {
2146 opd = bfd_get_section_by_name (abfd, ".opd");
2147 if (opd == NULL && abi == 1)
2148 return 0;
2149 }
90e3cdf2 2150
a5259595
AM
2151 syms = NULL;
2152 codesecsym = 0;
2153 codesecsymend = 0;
2154 secsymend = 0;
2155 opdsymend = 0;
2156 symcount = 0;
2157 if (opd != NULL)
c9727e01 2158 {
a5259595
AM
2159 symcount = static_count;
2160 if (!relocatable)
2161 symcount += dyn_count;
2162 if (symcount == 0)
2163 return 0;
c9727e01 2164
a5259595
AM
2165 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2166 if (syms == NULL)
2167 return -1;
90e3cdf2 2168
a5259595
AM
2169 if (!relocatable && static_count != 0 && dyn_count != 0)
2170 {
2171 /* Use both symbol tables. */
2172 memcpy (syms, static_syms, static_count * sizeof (*syms));
2173 memcpy (syms + static_count, dyn_syms,
2174 (dyn_count + 1) * sizeof (*syms));
2175 }
2176 else if (!relocatable && static_count == 0)
2177 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2178 else
2179 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2180
0ccf57bd
AM
2181 /* Trim uninteresting symbols. Interesting symbols are section,
2182 function, and notype symbols. */
2183 for (i = 0, j = 0; i < symcount; ++i)
2184 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2185 | BSF_RELC | BSF_SRELC)) == 0)
2186 syms[j++] = syms[i];
2187 symcount = j;
2188
a5259595
AM
2189 synthetic_relocatable = relocatable;
2190 synthetic_opd = opd;
2191 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2192
a5259595
AM
2193 if (!relocatable && symcount > 1)
2194 {
bfa5bd2a
PA
2195 /* Trim duplicate syms, since we may have merged the normal
2196 and dynamic symbols. Actually, we only care about syms
2197 that have different values, so trim any with the same
2198 value. Don't consider ifunc and ifunc resolver symbols
2199 duplicates however, because GDB wants to know whether a
2200 text symbol is an ifunc resolver. */
a5259595 2201 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2202 {
2203 const asymbol *s0 = syms[i - 1];
2204 const asymbol *s1 = syms[i];
2205
2206 if ((s0->value + s0->section->vma
2207 != s1->value + s1->section->vma)
2208 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2209 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2210 syms[j++] = syms[i];
2211 }
a5259595
AM
2212 symcount = j;
2213 }
699733f6 2214
a5259595
AM
2215 i = 0;
2216 /* Note that here and in compare_symbols we can't compare opd and
2217 sym->section directly. With separate debug info files, the
2218 symbols will be extracted from the debug file while abfd passed
2219 to this function is the real binary. */
0ccf57bd 2220 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2221 ++i;
2222 codesecsym = i;
2223
2224 for (; i < symcount; ++i)
2225 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2226 | SEC_THREAD_LOCAL))
2227 != (SEC_CODE | SEC_ALLOC))
2228 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2229 break;
2230 codesecsymend = i;
2231
2232 for (; i < symcount; ++i)
2233 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2234 break;
2235 secsymend = i;
2236
2237 for (; i < symcount; ++i)
2238 if (strcmp (syms[i]->section->name, ".opd") != 0)
2239 break;
2240 opdsymend = i;
2241
2242 for (; i < symcount; ++i)
2cdcc330
AM
2243 if (((syms[i]->section->flags
2244 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2245 != (SEC_CODE | SEC_ALLOC))
2246 break;
2247 symcount = i;
2248 }
c9727e01 2249 count = 0;
90e3cdf2 2250
699733f6 2251 if (relocatable)
90e3cdf2 2252 {
699733f6
AM
2253 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2254 arelent *r;
2255 size_t size;
0ccf57bd 2256 size_t relcount;
90e3cdf2 2257
468392fb
AM
2258 if (opdsymend == secsymend)
2259 goto done;
2260
699733f6 2261 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2262 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2263 if (relcount == 0)
c9727e01 2264 goto done;
90e3cdf2 2265
7356fed5
AM
2266 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2267 {
2268 count = -1;
2269 goto done;
2270 }
2271
699733f6 2272 size = 0;
595da8c5 2273 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2274 {
2275 asymbol *sym;
90e3cdf2 2276
595da8c5 2277 while (r < opd->relocation + relcount
699733f6
AM
2278 && r->address < syms[i]->value + opd->vma)
2279 ++r;
90e3cdf2 2280
595da8c5 2281 if (r == opd->relocation + relcount)
699733f6 2282 break;
90e3cdf2 2283
699733f6
AM
2284 if (r->address != syms[i]->value + opd->vma)
2285 continue;
90e3cdf2 2286
699733f6
AM
2287 if (r->howto->type != R_PPC64_ADDR64)
2288 continue;
90e3cdf2 2289
699733f6
AM
2290 sym = *r->sym_ptr_ptr;
2291 if (!sym_exists_at (syms, opdsymend, symcount,
2292 sym->section->id, sym->value + r->addend))
2293 {
2294 ++count;
2295 size += sizeof (asymbol);
2296 size += strlen (syms[i]->name) + 2;
2297 }
2298 }
90e3cdf2 2299
c4b0b099
AM
2300 if (size == 0)
2301 goto done;
699733f6
AM
2302 s = *ret = bfd_malloc (size);
2303 if (s == NULL)
2304 {
7356fed5 2305 count = -1;
c9727e01 2306 goto done;
699733f6 2307 }
90e3cdf2 2308
699733f6 2309 names = (char *) (s + count);
90e3cdf2 2310
595da8c5 2311 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2312 {
699733f6 2313 asymbol *sym;
90e3cdf2 2314
595da8c5 2315 while (r < opd->relocation + relcount
699733f6
AM
2316 && r->address < syms[i]->value + opd->vma)
2317 ++r;
90e3cdf2 2318
595da8c5 2319 if (r == opd->relocation + relcount)
699733f6
AM
2320 break;
2321
2322 if (r->address != syms[i]->value + opd->vma)
2323 continue;
2324
2325 if (r->howto->type != R_PPC64_ADDR64)
2326 continue;
90e3cdf2 2327
699733f6
AM
2328 sym = *r->sym_ptr_ptr;
2329 if (!sym_exists_at (syms, opdsymend, symcount,
2330 sym->section->id, sym->value + r->addend))
2331 {
2332 size_t len;
2333
2334 *s = *syms[i];
6ba2a415 2335 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2336 s->section = sym->section;
2337 s->value = sym->value + r->addend;
2338 s->name = names;
2339 *names++ = '.';
2340 len = strlen (syms[i]->name);
2341 memcpy (names, syms[i]->name, len + 1);
2342 names += len + 1;
6f610d07
UW
2343 /* Have udata.p point back to the original symbol this
2344 synthetic symbol was derived from. */
2345 s->udata.p = syms[i];
699733f6
AM
2346 s++;
2347 }
2348 }
2349 }
2350 else
90e3cdf2 2351 {
468392fb 2352 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 2353 bfd_byte *contents = NULL;
699733f6 2354 size_t size;
0ccf57bd 2355 size_t plt_count = 0;
468392fb
AM
2356 bfd_vma glink_vma = 0, resolv_vma = 0;
2357 asection *dynamic, *glink = NULL, *relplt = NULL;
2358 arelent *p;
90e3cdf2 2359
ee67d69a 2360 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2361 {
c4b0b099
AM
2362 free_contents_and_exit_err:
2363 count = -1;
ee67d69a 2364 free_contents_and_exit:
699733f6 2365 if (contents)
ee67d69a 2366 free (contents);
c9727e01 2367 goto done;
699733f6 2368 }
90e3cdf2 2369
699733f6
AM
2370 size = 0;
2371 for (i = secsymend; i < opdsymend; ++i)
2372 {
2373 bfd_vma ent;
90e3cdf2 2374
5ef11c02
AM
2375 /* Ignore bogus symbols. */
2376 if (syms[i]->value > opd->size - 8)
2377 continue;
2378
699733f6
AM
2379 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2380 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2381 {
2382 ++count;
2383 size += sizeof (asymbol);
2384 size += strlen (syms[i]->name) + 2;
2385 }
2386 }
90e3cdf2 2387
468392fb 2388 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2389 if (dyn_count != 0
2390 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2391 {
2392 bfd_byte *dynbuf, *extdyn, *extdynend;
2393 size_t extdynsize;
2394 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2395
2396 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2397 goto free_contents_and_exit_err;
468392fb
AM
2398
2399 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2400 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2401
2402 extdyn = dynbuf;
2403 extdynend = extdyn + dynamic->size;
2404 for (; extdyn < extdynend; extdyn += extdynsize)
2405 {
2406 Elf_Internal_Dyn dyn;
2407 (*swap_dyn_in) (abfd, extdyn, &dyn);
2408
2409 if (dyn.d_tag == DT_NULL)
2410 break;
2411
2412 if (dyn.d_tag == DT_PPC64_GLINK)
2413 {
9e390558
AM
2414 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2415 See comment in ppc64_elf_finish_dynamic_sections. */
2416 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2417 /* The .glink section usually does not survive the final
2418 link; search for the section (usually .text) where the
2419 glink stubs now reside. */
2420 glink = bfd_sections_find_if (abfd, section_covers_vma,
2421 &glink_vma);
2422 break;
2423 }
2424 }
2425
2426 free (dynbuf);
2427 }
2428
2429 if (glink != NULL)
2430 {
2431 /* Determine __glink trampoline by reading the relative branch
2432 from the first glink stub. */
2433 bfd_byte buf[4];
b9e5796b
AM
2434 unsigned int off = 0;
2435
2436 while (bfd_get_section_contents (abfd, glink, buf,
2437 glink_vma + off - glink->vma, 4))
468392fb
AM
2438 {
2439 unsigned int insn = bfd_get_32 (abfd, buf);
2440 insn ^= B_DOT;
2441 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2442 {
2cdcc330
AM
2443 resolv_vma
2444 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2445 break;
2446 }
2447 off += 4;
2448 if (off > 4)
2449 break;
468392fb
AM
2450 }
2451
2452 if (resolv_vma)
2453 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2454
066ee829
AM
2455 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2456 if (relplt != NULL)
2457 {
2458 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2cdcc330 2459 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 2460 goto free_contents_and_exit_err;
68ffbac6 2461
066ee829
AM
2462 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2463 size += plt_count * sizeof (asymbol);
468392fb 2464
066ee829
AM
2465 p = relplt->relocation;
2466 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2467 {
2468 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2469 if (p->addend != 0)
2470 size += sizeof ("+0x") - 1 + 16;
2471 }
066ee829 2472 }
468392fb
AM
2473 }
2474
c4b0b099
AM
2475 if (size == 0)
2476 goto free_contents_and_exit;
699733f6
AM
2477 s = *ret = bfd_malloc (size);
2478 if (s == NULL)
c4b0b099 2479 goto free_contents_and_exit_err;
90e3cdf2 2480
468392fb 2481 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2482
699733f6 2483 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2484 {
699733f6 2485 bfd_vma ent;
90e3cdf2 2486
5ef11c02
AM
2487 if (syms[i]->value > opd->size - 8)
2488 continue;
2489
699733f6
AM
2490 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2491 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2492 {
0ccf57bd 2493 size_t lo, hi;
699733f6 2494 size_t len;
c9727e01 2495 asection *sec = abfd->sections;
90e3cdf2 2496
699733f6
AM
2497 *s = *syms[i];
2498 lo = codesecsym;
2499 hi = codesecsymend;
2500 while (lo < hi)
2501 {
0ccf57bd 2502 size_t mid = (lo + hi) >> 1;
699733f6
AM
2503 if (syms[mid]->section->vma < ent)
2504 lo = mid + 1;
2505 else if (syms[mid]->section->vma > ent)
2506 hi = mid;
2507 else
c9727e01
AM
2508 {
2509 sec = syms[mid]->section;
2510 break;
2511 }
699733f6
AM
2512 }
2513
c9727e01 2514 if (lo >= hi && lo > codesecsym)
699733f6 2515 sec = syms[lo - 1]->section;
699733f6
AM
2516
2517 for (; sec != NULL; sec = sec->next)
2518 {
2519 if (sec->vma > ent)
2520 break;
63524580
JK
2521 /* SEC_LOAD may not be set if SEC is from a separate debug
2522 info file. */
2523 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2524 break;
2525 if ((sec->flags & SEC_CODE) != 0)
2526 s->section = sec;
2527 }
6ba2a415 2528 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2529 s->value = ent - s->section->vma;
2530 s->name = names;
2531 *names++ = '.';
2532 len = strlen (syms[i]->name);
2533 memcpy (names, syms[i]->name, len + 1);
2534 names += len + 1;
6f610d07
UW
2535 /* Have udata.p point back to the original symbol this
2536 synthetic symbol was derived from. */
2537 s->udata.p = syms[i];
699733f6 2538 s++;
90e3cdf2 2539 }
90e3cdf2 2540 }
699733f6 2541 free (contents);
468392fb
AM
2542
2543 if (glink != NULL && relplt != NULL)
2544 {
2545 if (resolv_vma)
2546 {
2547 /* Add a symbol for the main glink trampoline. */
86a4952b 2548 memset (s, 0, sizeof *s);
468392fb 2549 s->the_bfd = abfd;
6ba2a415 2550 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2551 s->section = glink;
2552 s->value = resolv_vma - glink->vma;
2553 s->name = names;
2cdcc330
AM
2554 memcpy (names, "__glink_PLTresolve",
2555 sizeof ("__glink_PLTresolve"));
468392fb
AM
2556 names += sizeof ("__glink_PLTresolve");
2557 s++;
2558 count++;
2559 }
2560
2561 /* FIXME: It would be very much nicer to put sym@plt on the
2562 stub rather than on the glink branch table entry. The
2563 objdump disassembler would then use a sensible symbol
2564 name on plt calls. The difficulty in doing so is
2565 a) finding the stubs, and,
2566 b) matching stubs against plt entries, and,
2567 c) there can be multiple stubs for a given plt entry.
2568
2569 Solving (a) could be done by code scanning, but older
2570 ppc64 binaries used different stubs to current code.
2571 (b) is the tricky one since you need to known the toc
2572 pointer for at least one function that uses a pic stub to
2573 be able to calculate the plt address referenced.
2574 (c) means gdb would need to set multiple breakpoints (or
2575 find the glink branch itself) when setting breakpoints
2576 for pending shared library loads. */
2577 p = relplt->relocation;
2578 for (i = 0; i < plt_count; i++, p++)
2579 {
2580 size_t len;
2581
2582 *s = **p->sym_ptr_ptr;
2583 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2584 we are defining a symbol, ensure one of them is set. */
2585 if ((s->flags & BSF_LOCAL) == 0)
2586 s->flags |= BSF_GLOBAL;
6ba2a415 2587 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2588 s->section = glink;
2589 s->value = glink_vma - glink->vma;
2590 s->name = names;
2591 s->udata.p = NULL;
2592 len = strlen ((*p->sym_ptr_ptr)->name);
2593 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2594 names += len;
e054468f
AM
2595 if (p->addend != 0)
2596 {
2597 memcpy (names, "+0x", sizeof ("+0x") - 1);
2598 names += sizeof ("+0x") - 1;
2599 bfd_sprintf_vma (abfd, names, p->addend);
2600 names += strlen (names);
2601 }
468392fb
AM
2602 memcpy (names, "@plt", sizeof ("@plt"));
2603 names += sizeof ("@plt");
2604 s++;
b9e5796b
AM
2605 if (abi < 2)
2606 {
2607 glink_vma += 8;
2608 if (i >= 0x8000)
2609 glink_vma += 4;
2610 }
2611 else
468392fb
AM
2612 glink_vma += 4;
2613 }
2614 count += plt_count;
2615 }
90e3cdf2
JJ
2616 }
2617
c9727e01 2618 done:
a7535cf3 2619 free (syms);
90e3cdf2
JJ
2620 return count;
2621}
5bd4f169 2622\f
65f38f15
AM
2623/* The following functions are specific to the ELF linker, while
2624 functions above are used generally. Those named ppc64_elf_* are
2625 called by the main ELF linker code. They appear in this file more
2626 or less in the order in which they are called. eg.
2627 ppc64_elf_check_relocs is called early in the link process,
2628 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2629 called.
2630
2631 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2632 functions have both a function code symbol and a function descriptor
2633 symbol. A call to foo in a relocatable object file looks like:
2634
2635 . .text
2636 . x:
2637 . bl .foo
2638 . nop
2639
2640 The function definition in another object file might be:
2641
2642 . .section .opd
2643 . foo: .quad .foo
2644 . .quad .TOC.@tocbase
2645 . .quad 0
2646 .
2647 . .text
2648 . .foo: blr
2649
2650 When the linker resolves the call during a static link, the branch
2651 unsurprisingly just goes to .foo and the .opd information is unused.
2652 If the function definition is in a shared library, things are a little
2653 different: The call goes via a plt call stub, the opd information gets
2654 copied to the plt, and the linker patches the nop.
2655
2656 . x:
2657 . bl .foo_stub
2658 . ld 2,40(1)
2659 .
2660 .
2661 . .foo_stub:
71a39c98
AM
2662 . std 2,40(1) # in practice, the call stub
2663 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2664 . addi 11,11,Lfoo@toc@l # this is the general idea
2665 . ld 12,0(11)
2666 . ld 2,8(11)
2667 . mtctr 12
2668 . ld 11,16(11)
e86ce104
AM
2669 . bctr
2670 .
2671 . .section .plt
2672 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2673
2674 The "reloc ()" notation is supposed to indicate that the linker emits
2675 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2676 copying.
2677
2678 What are the difficulties here? Well, firstly, the relocations
2679 examined by the linker in check_relocs are against the function code
2680 sym .foo, while the dynamic relocation in the plt is emitted against
2681 the function descriptor symbol, foo. Somewhere along the line, we need
2682 to carefully copy dynamic link information from one symbol to the other.
2683 Secondly, the generic part of the elf linker will make .foo a dynamic
2684 symbol as is normal for most other backends. We need foo dynamic
2685 instead, at least for an application final link. However, when
2686 creating a shared library containing foo, we need to have both symbols
2687 dynamic so that references to .foo are satisfied during the early
2688 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2689 definition from some other object, eg. a static library.
2690
2691 Update: As of August 2004, we support a new convention. Function
2692 calls may use the function descriptor symbol, ie. "bl foo". This
2693 behaves exactly as "bl .foo". */
65f38f15 2694
7c8bbca5
AM
2695/* Of those relocs that might be copied as dynamic relocs, this
2696 function selects those that must be copied when linking a shared
2697 library or PIE, even when the symbol is local. */
65f38f15 2698
1d483afe
AM
2699static int
2700must_be_dyn_reloc (struct bfd_link_info *info,
2701 enum elf_ppc64_reloc_type r_type)
2702{
2703 switch (r_type)
2704 {
2705 default:
7c8bbca5
AM
2706 /* Only relative relocs can be resolved when the object load
2707 address isn't fixed. DTPREL64 is excluded because the
2708 dynamic linker needs to differentiate global dynamic from
2709 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2710 return 1;
2711
2712 case R_PPC64_REL32:
2713 case R_PPC64_REL64:
2714 case R_PPC64_REL30:
2715 return 0;
2716
2717 case R_PPC64_TPREL16:
2718 case R_PPC64_TPREL16_LO:
2719 case R_PPC64_TPREL16_HI:
2720 case R_PPC64_TPREL16_HA:
2721 case R_PPC64_TPREL16_DS:
2722 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2723 case R_PPC64_TPREL16_HIGH:
2724 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2725 case R_PPC64_TPREL16_HIGHER:
2726 case R_PPC64_TPREL16_HIGHERA:
2727 case R_PPC64_TPREL16_HIGHEST:
2728 case R_PPC64_TPREL16_HIGHESTA:
2729 case R_PPC64_TPREL64:
7c8bbca5
AM
2730 /* These relocations are relative but in a shared library the
2731 linker doesn't know the thread pointer base. */
2732 return bfd_link_dll (info);
1d483afe
AM
2733 }
2734}
65f38f15 2735
f4656909
AM
2736/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2737 copying dynamic variables from a shared lib into an app's dynbss
2738 section, and instead use a dynamic relocation to point into the
5d35169e
AM
2739 shared lib. With code that gcc generates, it's vital that this be
2740 enabled; In the PowerPC64 ABI, the address of a function is actually
2741 the address of a function descriptor, which resides in the .opd
2742 section. gcc uses the descriptor directly rather than going via the
2743 GOT as some other ABI's do, which means that initialized function
2744 pointers must reference the descriptor. Thus, a function pointer
2745 initialized to the address of a function in a shared library will
2746 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 2747 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
2748 presents a problem as a plt entry for that function is also
2749 initialized from the function descriptor symbol and the copy reloc
2750 may not be initialized first. */
a23b6845 2751#define ELIMINATE_COPY_RELOCS 1
f4656909 2752
721956f4 2753/* Section name for stubs is the associated section name plus this
29942be8
NC
2754 string. */
2755#define STUB_SUFFIX ".stub"
721956f4
AM
2756
2757/* Linker stubs.
2758 ppc_stub_long_branch:
2759 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2760 destination, but a 24 bit branch in a stub section will reach.
2761 . b dest
2762
2763 ppc_stub_plt_branch:
2764 Similar to the above, but a 24 bit branch in the stub section won't
2765 reach its destination.
71a39c98
AM
2766 . addis %r11,%r2,xxx@toc@ha
2767 . ld %r12,xxx@toc@l(%r11)
2768 . mtctr %r12
721956f4
AM
2769 . bctr
2770
2771 ppc_stub_plt_call:
2c66dc6c
AM
2772 Used to call a function in a shared library. If it so happens that
2773 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2774 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 2775 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
2776 . addis %r11,%r2,xxx@toc@ha
2777 . ld %r12,xxx+0@toc@l(%r11)
2778 . mtctr %r12
2779 . ld %r2,xxx+8@toc@l(%r11)
2780 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2781 . bctr
ad8e1ba5
AM
2782
2783 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2784 code to adjust the value and save r2 to support multiple toc sections.
2785 A ppc_stub_long_branch with an r2 offset looks like:
2786 . std %r2,40(%r1)
2787 . addis %r2,%r2,off@ha
2788 . addi %r2,%r2,off@l
2789 . b dest
2790
2791 A ppc_stub_plt_branch with an r2 offset looks like:
2792 . std %r2,40(%r1)
71a39c98
AM
2793 . addis %r11,%r2,xxx@toc@ha
2794 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
2795 . addis %r2,%r2,off@ha
2796 . addi %r2,%r2,off@l
71a39c98 2797 . mtctr %r12
ad8e1ba5 2798 . bctr
ac2df442 2799
05d0e962
AM
2800 All of the above stubs are shown as their ELFv1 variants. ELFv2
2801 variants exist too, simpler for plt calls since a new toc pointer
2802 and static chain are not loaded by the stub. In addition, ELFv2
2803 has some more complex stubs to handle calls marked with NOTOC
2804 relocs from functions where r2 is not a valid toc pointer. These
2805 come in two flavours, the ones shown below, and _both variants that
2806 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2807 one call is from a function where r2 is used as the toc pointer but
2808 needs a toc adjusting stub for small-model multi-toc, and another
2809 call is from a function where r2 is not valid.
2810 ppc_stub_long_branch_notoc:
2811 . mflr %r12
2812 . bcl 20,31,1f
2813 . 1:
2814 . mflr %r11
2815 . mtlr %r12
2cdcc330
AM
2816 . addis %r12,%r11,dest-1b@ha
2817 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2818 . b dest
2819
2820 ppc_stub_plt_branch_notoc:
2821 . mflr %r12
2822 . bcl 20,31,1f
2823 . 1:
2824 . mflr %r11
2825 . mtlr %r12
2826 . lis %r12,xxx-1b@highest
f891966f 2827 . ori %r12,%r12,xxx-1b@higher
05d0e962 2828 . sldi %r12,%r12,32
f891966f 2829 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2830 . ori %r12,%r12,xxx-1b@l
2831 . add %r12,%r11,%r12
2832 . mtctr %r12
2833 . bctr
2834
2835 ppc_stub_plt_call_notoc:
2836 . mflr %r12
2837 . bcl 20,31,1f
2838 . 1:
2839 . mflr %r11
2840 . mtlr %r12
2841 . lis %r12,xxx-1b@highest
f891966f 2842 . ori %r12,%r12,xxx-1b@higher
05d0e962 2843 . sldi %r12,%r12,32
f891966f 2844 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2845 . ori %r12,%r12,xxx-1b@l
2846 . ldx %r12,%r11,%r12
2847 . mtctr %r12
2848 . bctr
2849
04bdff6a
AM
2850 There are also ELFv1 powerxx variants of these stubs.
2851 ppc_stub_long_branch_notoc:
d4b87b1e 2852 . pla %r12,dest@pcrel
04bdff6a
AM
2853 . b dest
2854 ppc_stub_plt_branch_notoc:
2855 . lis %r11,(dest-1f)@highesta34
2856 . ori %r11,%r11,(dest-1f)@highera34
2857 . sldi %r11,%r11,34
d4b87b1e 2858 . 1: pla %r12,dest@pcrel
04bdff6a
AM
2859 . add %r12,%r11,%r12
2860 . mtctr %r12
2861 . bctr
2862 ppc_stub_plt_call_notoc:
2863 . lis %r11,(xxx-1f)@highesta34
2864 . ori %r11,%r11,(xxx-1f)@highera34
2865 . sldi %r11,%r11,34
d4b87b1e 2866 . 1: pla %r12,xxx@pcrel
04bdff6a
AM
2867 . ldx %r12,%r11,%r12
2868 . mtctr %r12
2869 . bctr
2870
05d0e962
AM
2871 In cases where the high instructions would add zero, they are
2872 omitted and following instructions modified in some cases.
04bdff6a
AM
2873 For example, a powerxx ppc_stub_plt_call_notoc might simplify down
2874 to
2875 . pld %r12,xxx@pcrel
2876 . mtctr %r12
2877 . bctr
05d0e962
AM
2878
2879 For a given stub group (a set of sections all using the same toc
2880 pointer value) there will be just one stub type used for any
2881 particular function symbol. For example, if printf is called from
2882 code with the tocsave optimization (ie. r2 saved in function
2883 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2884 and from other code without the tocsave optimization requiring a
2885 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2886 type will be created. Calls with the tocsave optimization will
2887 enter this stub after the instruction saving r2. A similar
2888 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2889 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2890 to call an external function like printf. If other calls to printf
2891 require a ppc_stub_plt_call linkage stub then a single
2892 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2893 call. If other calls to printf require a ppc_stub_plt_call_r2save
2894 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2895 be created and calls not requiring r2 to be saved will enter the
2896 stub after the r2 save instruction. There is an analogous
2897 hierarchy of long branch and plt branch stubs for local call
2898 linkage. */
721956f4 2899
2cdcc330
AM
2900enum ppc_stub_type
2901{
721956f4
AM
2902 ppc_stub_none,
2903 ppc_stub_long_branch,
ad8e1ba5 2904 ppc_stub_long_branch_r2off,
05d0e962
AM
2905 ppc_stub_long_branch_notoc,
2906 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 2907 ppc_stub_plt_branch,
ad8e1ba5 2908 ppc_stub_plt_branch_r2off,
05d0e962
AM
2909 ppc_stub_plt_branch_notoc,
2910 ppc_stub_plt_branch_both,
794e51c0 2911 ppc_stub_plt_call,
7341d5e2 2912 ppc_stub_plt_call_r2save,
05d0e962
AM
2913 ppc_stub_plt_call_notoc,
2914 ppc_stub_plt_call_both,
a4b6fadd
AM
2915 ppc_stub_global_entry,
2916 ppc_stub_save_res
721956f4
AM
2917};
2918
6f20ed8a
AM
2919/* Information on stub grouping. */
2920struct map_stub
2921{
2922 /* The stub section. */
2923 asection *stub_sec;
2924 /* This is the section to which stubs in the group will be attached. */
2925 asection *link_sec;
a4b6fadd
AM
2926 /* Next group. */
2927 struct map_stub *next;
2928 /* Whether to emit a copy of register save/restore functions in this
2929 group. */
2930 int needs_save_res;
df136d64
AM
2931 /* Current offset within stubs after the insn restoring lr in a
2932 _notoc or _both stub using bcl for pc-relative addressing, or
2933 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2934 unsigned int lr_restore;
2935 /* Accumulated size of EH info emitted to describe return address
2936 if stubs modify lr. Does not include 17 byte FDE header. */
2937 unsigned int eh_size;
2938 /* Offset in glink_eh_frame to the start of EH info for this group. */
2939 unsigned int eh_base;
6f20ed8a
AM
2940};
2941
2cdcc330
AM
2942struct ppc_stub_hash_entry
2943{
721956f4
AM
2944 /* Base hash table entry structure. */
2945 struct bfd_hash_entry root;
2946
ad8e1ba5
AM
2947 enum ppc_stub_type stub_type;
2948
6f20ed8a
AM
2949 /* Group information. */
2950 struct map_stub *group;
721956f4
AM
2951
2952 /* Offset within stub_sec of the beginning of this stub. */
2953 bfd_vma stub_offset;
2954
2955 /* Given the symbol's value and its section we can determine its final
2956 value when building the stubs (so the stub knows where to jump. */
2957 bfd_vma target_value;
2958 asection *target_section;
2959
721956f4
AM
2960 /* The symbol table entry, if any, that this was derived from. */
2961 struct ppc_link_hash_entry *h;
e054468f 2962 struct plt_entry *plt_ent;
721956f4 2963
2d7ad24e
AM
2964 /* Symbol type. */
2965 unsigned char symtype;
2966
6911b7dc
AM
2967 /* Symbol st_other. */
2968 unsigned char other;
721956f4
AM
2969};
2970
2cdcc330
AM
2971struct ppc_branch_hash_entry
2972{
721956f4
AM
2973 /* Base hash table entry structure. */
2974 struct bfd_hash_entry root;
2975
c456f082 2976 /* Offset within branch lookup table. */
721956f4
AM
2977 unsigned int offset;
2978
2979 /* Generation marker. */
2980 unsigned int iter;
2981};
65f38f15 2982
19e08130
AM
2983/* Used to track dynamic relocations for local symbols. */
2984struct ppc_dyn_relocs
2985{
2986 struct ppc_dyn_relocs *next;
2987
2988 /* The input section of the reloc. */
2989 asection *sec;
2990
2991 /* Total number of relocs copied for the input section. */
2992 unsigned int count : 31;
2993
2994 /* Whether this entry is for STT_GNU_IFUNC symbols. */
2995 unsigned int ifunc : 1;
2996};
2997
65f38f15
AM
2998struct ppc_link_hash_entry
2999{
3000 struct elf_link_hash_entry elf;
3001
2cdcc330
AM
3002 union
3003 {
b3fac117
AM
3004 /* A pointer to the most recently used stub hash entry against this
3005 symbol. */
3006 struct ppc_stub_hash_entry *stub_cache;
3007
3008 /* A pointer to the next symbol starting with a '.' */
3009 struct ppc_link_hash_entry *next_dot_sym;
3010 } u;
721956f4 3011
65f38f15 3012 /* Track dynamic relocs copied for this symbol. */
6061a67d 3013 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3014
721956f4 3015 /* Link between function code and descriptor symbols. */
34814b9f 3016 struct ppc_link_hash_entry *oh;
721956f4 3017
e86ce104
AM
3018 /* Flag function code and descriptor symbols. */
3019 unsigned int is_func:1;
3020 unsigned int is_func_descriptor:1;
908b32fc 3021 unsigned int fake:1;
411e1bfb 3022
c5614fa4
AM
3023 /* Whether global opd/toc sym has been adjusted or not.
3024 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3025 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3026 unsigned int adjust_done:1;
3027
a4b6fadd
AM
3028 /* Set if this is an out-of-line register save/restore function,
3029 with non-standard calling convention. */
3030 unsigned int save_res:1;
3031
8b5f1ed8
AM
3032 /* Set if a duplicate symbol with non-zero localentry is detected,
3033 even when the duplicate symbol does not provide a definition. */
3034 unsigned int non_zero_localentry:1;
3035
411e1bfb 3036 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3037 Bits are or'd into the mask as the corresponding relocs are
3038 encountered during check_relocs, with TLS_TLS being set when any
3039 of the other TLS bits are set. tls_optimize clears bits when
3040 optimizing to indicate the corresponding GOT entry type is not
3041 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3042 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
3043 separate flag rather than setting TPREL just for convenience in
3044 distinguishing the two cases.
3045 These flags are also kept for local symbols. */
3046#define TLS_TLS 1 /* Any TLS reloc. */
3047#define TLS_GD 2 /* GD reloc. */
3048#define TLS_LD 4 /* LD reloc. */
3049#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3050#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3051#define TLS_MARK 32 /* __tls_get_addr call marked. */
e7b938ca 3052#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
37da22e5 3053#define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
f961d9dd 3054 unsigned char tls_mask;
37da22e5
AM
3055
3056 /* The above field is also used to mark function symbols. In which
3057 case TLS_TLS will be 0. */
3058#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3059#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3060#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3061};
3062
3063/* ppc64 ELF linker hash table. */
3064
3065struct ppc_link_hash_table
3066{
3067 struct elf_link_hash_table elf;
3068
721956f4
AM
3069 /* The stub hash table. */
3070 struct bfd_hash_table stub_hash_table;
3071
3072 /* Another hash table for plt_branch stubs. */
3073 struct bfd_hash_table branch_hash_table;
3074
3b421ab3
AM
3075 /* Hash table for function prologue tocsave. */
3076 htab_t tocsave_htab;
3077
e7d1c40c
AM
3078 /* Various options and other info passed from the linker. */
3079 struct ppc64_elf_params *params;
721956f4 3080
6f20ed8a
AM
3081 /* The size of sec_info below. */
3082 unsigned int sec_info_arr_size;
3083
3084 /* Per-section array of extra section info. Done this way rather
3085 than as part of ppc64_elf_section_data so we have the info for
3086 non-ppc64 sections. */
3087 struct
3088 {
3089 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3090 bfd_vma toc_off;
6f20ed8a
AM
3091
3092 union
3093 {
3094 /* The section group that this section belongs to. */
3095 struct map_stub *group;
3096 /* A temp section list pointer. */
3097 asection *list;
3098 } u;
3099 } *sec_info;
721956f4 3100
a4b6fadd
AM
3101 /* Linked list of groups. */
3102 struct map_stub *group;
3103
ad8e1ba5
AM
3104 /* Temp used when calculating TOC pointers. */
3105 bfd_vma toc_curr;
bf102f86
AM
3106 bfd *toc_bfd;
3107 asection *toc_first_sec;
ad8e1ba5 3108
b3fac117
AM
3109 /* Used when adding symbols. */
3110 struct ppc_link_hash_entry *dot_syms;
3111
33e44f2e 3112 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3113 asection *glink;
9e390558 3114 asection *global_entry;
82bd7b59 3115 asection *sfpr;
2d7ad24e
AM
3116 asection *pltlocal;
3117 asection *relpltlocal;
4ce794b7
AM
3118 asection *brlt;
3119 asection *relbrlt;
58d180e8 3120 asection *glink_eh_frame;
ec338859 3121
8387904d
AM
3122 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3123 struct ppc_link_hash_entry *tls_get_addr;
3124 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3125
927be08e
AM
3126 /* The size of reliplt used by got entry relocs. */
3127 bfd_size_type got_reli_size;
3128
9b5ecbd0 3129 /* Statistics. */
7341d5e2 3130 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 3131
ee75fd95
AM
3132 /* Number of stubs against global syms. */
3133 unsigned long stub_globals;
3134
ee67d69a
AM
3135 /* Set if we're linking code with function descriptors. */
3136 unsigned int opd_abi:1;
3137
4c52953f 3138 /* Support for multiple toc sections. */
33c0ec9d 3139 unsigned int do_multi_toc:1;
4c52953f 3140 unsigned int multi_toc_needed:1;
927be08e 3141 unsigned int second_toc_pass:1;
67f0cbdb 3142 unsigned int do_toc_opt:1;
4c52953f 3143
9a23f96e
AM
3144 /* Set if tls optimization is enabled. */
3145 unsigned int do_tls_opt:1;
3146
3e04d765
AM
3147 /* Set if inline plt calls should be converted to direct calls. */
3148 unsigned int can_convert_all_inline_plt:1;
3149
5d1634d7 3150 /* Set on error. */
99877b66 3151 unsigned int stub_error:1;
721956f4 3152
8c5b4e52
AM
3153 /* Whether func_desc_adjust needs to be run over symbols. */
3154 unsigned int need_func_desc_adj:1;
721956f4 3155
82e66161
AM
3156 /* Whether there exist local gnu indirect function resolvers,
3157 referenced by dynamic relocations. */
3158 unsigned int local_ifunc_resolver:1;
3159 unsigned int maybe_local_ifunc_resolver:1;
3160
f378ab09
AM
3161 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3162 unsigned int has_plt_localentry0:1;
3163
5663e321
AM
3164 /* Whether calls are made via the PLT from NOTOC functions. */
3165 unsigned int notoc_plt:1;
3166
04bdff6a
AM
3167 /* Whether to use powerxx instructions in linkage stubs. */
3168 unsigned int powerxx_stubs:1;
3169
721956f4
AM
3170 /* Incremented every time we size stubs. */
3171 unsigned int stub_iteration;
5d1634d7 3172
87d72d41
AM
3173 /* Small local sym cache. */
3174 struct sym_cache sym_cache;
65f38f15
AM
3175};
3176
4c52953f
AM
3177/* Rename some of the generic section flags to better document how they
3178 are used here. */
b0dddeec
AM
3179
3180/* Nonzero if this section has TLS related relocations. */
3181#define has_tls_reloc sec_flg0
3182
23cedd1d 3183/* Nonzero if this section has an old-style call to __tls_get_addr. */
b0dddeec
AM
3184#define has_tls_get_addr_call sec_flg1
3185
3186/* Nonzero if this section has any toc or got relocs. */
3187#define has_toc_reloc sec_flg2
3188
3189/* Nonzero if this section has a call to another section that uses
3190 the toc or got. */
d77c8a4b 3191#define makes_toc_func_call sec_flg3
b0dddeec
AM
3192
3193/* Recursion protection when determining above flag. */
d77c8a4b 3194#define call_check_in_progress sec_flg4
70cc837d 3195#define call_check_done sec_flg5
4c52953f 3196
65f38f15
AM
3197/* Get the ppc64 ELF linker hash table from a link_info structure. */
3198
3199#define ppc_hash_table(p) \
4dfe6ac6
NC
3200 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3201 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3202
721956f4
AM
3203#define ppc_stub_hash_lookup(table, string, create, copy) \
3204 ((struct ppc_stub_hash_entry *) \
3205 bfd_hash_lookup ((table), (string), (create), (copy)))
3206
3207#define ppc_branch_hash_lookup(table, string, create, copy) \
3208 ((struct ppc_branch_hash_entry *) \
3209 bfd_hash_lookup ((table), (string), (create), (copy)))
3210
3211/* Create an entry in the stub hash table. */
3212
3213static struct bfd_hash_entry *
4ce794b7
AM
3214stub_hash_newfunc (struct bfd_hash_entry *entry,
3215 struct bfd_hash_table *table,
3216 const char *string)
721956f4
AM
3217{
3218 /* Allocate the structure if it has not already been allocated by a
3219 subclass. */
3220 if (entry == NULL)
3221 {
3222 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3223 if (entry == NULL)
3224 return entry;
3225 }
3226
3227 /* Call the allocation method of the superclass. */
3228 entry = bfd_hash_newfunc (entry, table, string);
3229 if (entry != NULL)
3230 {
3231 struct ppc_stub_hash_entry *eh;
3232
3233 /* Initialize the local fields. */
3234 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3235 eh->stub_type = ppc_stub_none;
6f20ed8a 3236 eh->group = NULL;
721956f4
AM
3237 eh->stub_offset = 0;
3238 eh->target_value = 0;
3239 eh->target_section = NULL;
721956f4 3240 eh->h = NULL;
6911b7dc 3241 eh->plt_ent = NULL;
6911b7dc 3242 eh->other = 0;
721956f4
AM
3243 }
3244
3245 return entry;
3246}
3247
3248/* Create an entry in the branch hash table. */
3249
3250static struct bfd_hash_entry *
4ce794b7
AM
3251branch_hash_newfunc (struct bfd_hash_entry *entry,
3252 struct bfd_hash_table *table,
3253 const char *string)
721956f4
AM
3254{
3255 /* Allocate the structure if it has not already been allocated by a
3256 subclass. */
3257 if (entry == NULL)
3258 {
3259 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3260 if (entry == NULL)
3261 return entry;
3262 }
3263
3264 /* Call the allocation method of the superclass. */
3265 entry = bfd_hash_newfunc (entry, table, string);
3266 if (entry != NULL)
3267 {
3268 struct ppc_branch_hash_entry *eh;
3269
3270 /* Initialize the local fields. */
3271 eh = (struct ppc_branch_hash_entry *) entry;
3272 eh->offset = 0;
3273 eh->iter = 0;
3274 }
3275
3276 return entry;
3277}
3278
65f38f15
AM
3279/* Create an entry in a ppc64 ELF linker hash table. */
3280
3281static struct bfd_hash_entry *
4ce794b7
AM
3282link_hash_newfunc (struct bfd_hash_entry *entry,
3283 struct bfd_hash_table *table,
3284 const char *string)
65f38f15
AM
3285{
3286 /* Allocate the structure if it has not already been allocated by a
3287 subclass. */
3288 if (entry == NULL)
3289 {
3290 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3291 if (entry == NULL)
3292 return entry;
3293 }
3294
3295 /* Call the allocation method of the superclass. */
3296 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3297 if (entry != NULL)
3298 {
3299 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3300
b3fac117 3301 memset (&eh->u.stub_cache, 0,
908b32fc 3302 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3303 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3304
3305 /* When making function calls, old ABI code references function entry
3306 points (dot symbols), while new ABI code references the function
3307 descriptor symbol. We need to make any combination of reference and
3308 definition work together, without breaking archive linking.
3309
3310 For a defined function "foo" and an undefined call to "bar":
3311 An old object defines "foo" and ".foo", references ".bar" (possibly
3312 "bar" too).
3313 A new object defines "foo" and references "bar".
3314
3315 A new object thus has no problem with its undefined symbols being
3316 satisfied by definitions in an old object. On the other hand, the
3317 old object won't have ".bar" satisfied by a new object.
3318
3319 Keep a list of newly added dot-symbols. */
3320
3321 if (string[0] == '.')
3322 {
3323 struct ppc_link_hash_table *htab;
3324
3325 htab = (struct ppc_link_hash_table *) table;
3326 eh->u.next_dot_sym = htab->dot_syms;
3327 htab->dot_syms = eh;
3328 }
65f38f15
AM
3329 }
3330
3331 return entry;
3332}
3333
2cdcc330
AM
3334struct tocsave_entry
3335{
3b421ab3
AM
3336 asection *sec;
3337 bfd_vma offset;
3338};
3339
3340static hashval_t
3341tocsave_htab_hash (const void *p)
3342{
3343 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3344 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3345}
3346
3347static int
3348tocsave_htab_eq (const void *p1, const void *p2)
3349{
3350 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3351 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3352 return e1->sec == e2->sec && e1->offset == e2->offset;
3353}
3354
68faa637
AM
3355/* Destroy a ppc64 ELF linker hash table. */
3356
3357static void
d495ab0d 3358ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3359{
d495ab0d 3360 struct ppc_link_hash_table *htab;
68faa637 3361
d495ab0d 3362 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3363 if (htab->tocsave_htab)
3364 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3365 bfd_hash_table_free (&htab->branch_hash_table);
3366 bfd_hash_table_free (&htab->stub_hash_table);
3367 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3368}
3369
65f38f15
AM
3370/* Create a ppc64 ELF linker hash table. */
3371
3372static struct bfd_link_hash_table *
4ce794b7 3373ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3374{
3375 struct ppc_link_hash_table *htab;
3376 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3377
4ce794b7 3378 htab = bfd_zmalloc (amt);
65f38f15
AM
3379 if (htab == NULL)
3380 return NULL;
3381
66eb6687 3382 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3383 sizeof (struct ppc_link_hash_entry),
3384 PPC64_ELF_DATA))
65f38f15 3385 {
e2d34d7d 3386 free (htab);
65f38f15
AM
3387 return NULL;
3388 }
3389
721956f4 3390 /* Init the stub hash table too. */
66eb6687
AM
3391 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3392 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3393 {
d495ab0d 3394 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3395 return NULL;
3396 }
721956f4
AM
3397
3398 /* And the branch hash table. */
66eb6687
AM
3399 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3400 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3401 {
3402 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3403 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3404 return NULL;
3405 }
721956f4 3406
3b421ab3
AM
3407 htab->tocsave_htab = htab_try_create (1024,
3408 tocsave_htab_hash,
3409 tocsave_htab_eq,
3410 NULL);
3411 if (htab->tocsave_htab == NULL)
2915c55b 3412 {
d495ab0d 3413 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3414 return NULL;
3415 }
d495ab0d 3416 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3417
3254fd24
AM
3418 /* Initializing two fields of the union is just cosmetic. We really
3419 only care about glist, but when compiled on a 32-bit host the
3420 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3421 debugger inspection of these fields look nicer. */
a6aa5195
AM
3422 htab->elf.init_got_refcount.refcount = 0;
3423 htab->elf.init_got_refcount.glist = NULL;
3424 htab->elf.init_plt_refcount.refcount = 0;
3425 htab->elf.init_plt_refcount.glist = NULL;
3426 htab->elf.init_got_offset.offset = 0;
3427 htab->elf.init_got_offset.glist = NULL;
3428 htab->elf.init_plt_offset.offset = 0;
3429 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3430
65f38f15
AM
3431 return &htab->elf.root;
3432}
3433
bfeb4a28
AM
3434/* Create sections for linker generated code. */
3435
3436static bfd_boolean
3437create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3438{
3439 struct ppc_link_hash_table *htab;
3440 flagword flags;
3441
3442 htab = ppc_hash_table (info);
3443
bfeb4a28
AM
3444 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3445 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3446 if (htab->params->save_restore_funcs)
3447 {
3448 /* Create .sfpr for code to save and restore fp regs. */
3449 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3450 flags);
3451 if (htab->sfpr == NULL
2cdcc330 3452 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b32547cd
AM
3453 return FALSE;
3454 }
3455
3456 if (bfd_link_relocatable (info))
3457 return TRUE;
bfeb4a28
AM
3458
3459 /* Create .glink for lazy dynamic linking support. */
3460 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3461 flags);
3462 if (htab->glink == NULL
2cdcc330 3463 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
bfeb4a28
AM
3464 return FALSE;
3465
9e390558
AM
3466 /* The part of .glink used by global entry stubs, separate so that
3467 it can be aligned appropriately without affecting htab->glink. */
3468 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3469 flags);
3470 if (htab->global_entry == NULL
2cdcc330 3471 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
9e390558
AM
3472 return FALSE;
3473
bfeb4a28
AM
3474 if (!info->no_ld_generated_unwind_info)
3475 {
3476 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3477 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3478 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3479 ".eh_frame",
3480 flags);
3481 if (htab->glink_eh_frame == NULL
3482 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3483 return FALSE;
3484 }
3485
3486 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3487 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3488 if (htab->elf.iplt == NULL
2cdcc330 3489 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
3490 return FALSE;
3491
3492 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3493 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3494 htab->elf.irelplt
3495 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3496 if (htab->elf.irelplt == NULL
2cdcc330 3497 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
3498 return FALSE;
3499
3500 /* Create branch lookup table for plt_branch stubs. */
3501 flags = (SEC_ALLOC | SEC_LOAD
3502 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3503 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3504 flags);
3505 if (htab->brlt == NULL
2cdcc330 3506 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
bfeb4a28
AM
3507 return FALSE;
3508
2d7ad24e
AM
3509 /* Local plt entries, put in .branch_lt but a separate section for
3510 convenience. */
3511 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3512 flags);
3513 if (htab->pltlocal == NULL
2cdcc330 3514 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
2d7ad24e
AM
3515 return FALSE;
3516
0e1862bb 3517 if (!bfd_link_pic (info))
bfeb4a28
AM
3518 return TRUE;
3519
3520 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3521 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3522 htab->relbrlt
3523 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3524 if (htab->relbrlt == NULL
2cdcc330 3525 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
bfeb4a28
AM
3526 return FALSE;
3527
2d7ad24e
AM
3528 htab->relpltlocal
3529 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3530 if (htab->relpltlocal == NULL
2cdcc330 3531 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
2d7ad24e
AM
3532 return FALSE;
3533
bfeb4a28
AM
3534 return TRUE;
3535}
3536
e717da7e
AM
3537/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3538
bfeb4a28 3539bfd_boolean
e7d1c40c
AM
3540ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3541 struct ppc64_elf_params *params)
e717da7e
AM
3542{
3543 struct ppc_link_hash_table *htab;
3544
e7d1c40c 3545 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3546
3547/* Always hook our dynamic sections into the first bfd, which is the
3548 linker created stub bfd. This ensures that the GOT header is at
3549 the start of the output TOC section. */
3550 htab = ppc_hash_table (info);
e7d1c40c
AM
3551 htab->elf.dynobj = params->stub_bfd;
3552 htab->params = params;
bfeb4a28 3553
bfeb4a28 3554 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3555}
3556
721956f4
AM
3557/* Build a name for an entry in the stub hash table. */
3558
3559static char *
4ce794b7
AM
3560ppc_stub_name (const asection *input_section,
3561 const asection *sym_sec,
3562 const struct ppc_link_hash_entry *h,
3563 const Elf_Internal_Rela *rel)
721956f4
AM
3564{
3565 char *stub_name;
bcaa2f82 3566 ssize_t len;
721956f4
AM
3567
3568 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3569 offsets from a sym as a branch target? In fact, we could
3570 probably assume the addend is always zero. */
3571 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3572
3573 if (h)
3574 {
3575 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3576 stub_name = bfd_malloc (len);
46de2a7c
AM
3577 if (stub_name == NULL)
3578 return stub_name;
3579
bcaa2f82
AM
3580 len = sprintf (stub_name, "%08x.%s+%x",
3581 input_section->id & 0xffffffff,
3582 h->elf.root.root.string,
3583 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3584 }
3585 else
3586 {
ad8e1ba5 3587 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3588 stub_name = bfd_malloc (len);
46de2a7c
AM
3589 if (stub_name == NULL)
3590 return stub_name;
3591
bcaa2f82
AM
3592 len = sprintf (stub_name, "%08x.%x:%x+%x",
3593 input_section->id & 0xffffffff,
3594 sym_sec->id & 0xffffffff,
3595 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3596 (int) rel->r_addend & 0xffffffff);
721956f4 3597 }
bcaa2f82 3598 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3599 stub_name[len - 2] = 0;
721956f4
AM
3600 return stub_name;
3601}
3602
3603/* Look up an entry in the stub hash. Stub entries are cached because
3604 creating the stub name takes a bit of time. */
3605
3606static struct ppc_stub_hash_entry *
4ce794b7
AM
3607ppc_get_stub_entry (const asection *input_section,
3608 const asection *sym_sec,
039b3fef 3609 struct ppc_link_hash_entry *h,
4ce794b7
AM
3610 const Elf_Internal_Rela *rel,
3611 struct ppc_link_hash_table *htab)
721956f4
AM
3612{
3613 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3614 struct map_stub *group;
721956f4
AM
3615
3616 /* If this input section is part of a group of sections sharing one
3617 stub section, then use the id of the first section in the group.
3618 Stub names need to include a section id, as there may well be
3619 more than one stub used to reach say, printf, and we need to
3620 distinguish between them. */
6f20ed8a 3621 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3622 if (group == NULL)
3623 return NULL;
721956f4 3624
b3fac117
AM
3625 if (h != NULL && h->u.stub_cache != NULL
3626 && h->u.stub_cache->h == h
6f20ed8a 3627 && h->u.stub_cache->group == group)
721956f4 3628 {
b3fac117 3629 stub_entry = h->u.stub_cache;
721956f4
AM
3630 }
3631 else
3632 {
3633 char *stub_name;
3634
6f20ed8a 3635 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3636 if (stub_name == NULL)
3637 return NULL;
3638
3639 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3640 stub_name, FALSE, FALSE);
721956f4 3641 if (h != NULL)
b3fac117 3642 h->u.stub_cache = stub_entry;
721956f4
AM
3643
3644 free (stub_name);
3645 }
3646
3647 return stub_entry;
3648}
3649
3650/* Add a new stub entry to the stub hash. Not all fields of the new
3651 stub entry are initialised. */
3652
3653static struct ppc_stub_hash_entry *
4ce794b7
AM
3654ppc_add_stub (const char *stub_name,
3655 asection *section,
25f53a85 3656 struct bfd_link_info *info)
721956f4 3657{
25f53a85 3658 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3659 struct map_stub *group;
721956f4
AM
3660 asection *link_sec;
3661 asection *stub_sec;
3662 struct ppc_stub_hash_entry *stub_entry;
3663
6f20ed8a
AM
3664 group = htab->sec_info[section->id].u.group;
3665 link_sec = group->link_sec;
3666 stub_sec = group->stub_sec;
721956f4
AM
3667 if (stub_sec == NULL)
3668 {
6f20ed8a
AM
3669 size_t namelen;
3670 bfd_size_type len;
3671 char *s_name;
721956f4 3672
6f20ed8a
AM
3673 namelen = strlen (link_sec->name);
3674 len = namelen + sizeof (STUB_SUFFIX);
3675 s_name = bfd_alloc (htab->params->stub_bfd, len);
3676 if (s_name == NULL)
3677 return NULL;
721956f4 3678
6f20ed8a
AM
3679 memcpy (s_name, link_sec->name, namelen);
3680 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3681 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3682 if (stub_sec == NULL)
3683 return NULL;
3684 group->stub_sec = stub_sec;
721956f4
AM
3685 }
3686
3687 /* Enter this entry into the linker stub hash table. */
3688 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3689 TRUE, FALSE);
721956f4
AM
3690 if (stub_entry == NULL)
3691 {
695344c0 3692 /* xgettext:c-format */
cf97bcb0
AM
3693 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3694 section->owner, stub_name);
721956f4
AM
3695 return NULL;
3696 }
3697
6f20ed8a 3698 stub_entry->group = group;
721956f4 3699 stub_entry->stub_offset = 0;
721956f4
AM
3700 return stub_entry;
3701}
3702
e717da7e
AM
3703/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3704 not already done. */
65f38f15 3705
b34976b6 3706static bfd_boolean
e717da7e 3707create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3708{
e717da7e
AM
3709 asection *got, *relgot;
3710 flagword flags;
3711 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3712
0c8d6e5c 3713 if (!is_ppc64_elf (abfd))
0ffa91dd 3714 return FALSE;
4dfe6ac6
NC
3715 if (htab == NULL)
3716 return FALSE;
0ffa91dd 3717
33e44f2e
AM
3718 if (!htab->elf.sgot
3719 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3720 return FALSE;
e717da7e
AM
3721
3722 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3723 | SEC_LINKER_CREATED);
3724
c456f082 3725 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3726 if (!got
e717da7e
AM
3727 || !bfd_set_section_alignment (abfd, got, 3))
3728 return FALSE;
65f38f15 3729
c456f082
AM
3730 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3731 flags | SEC_READONLY);
e717da7e 3732 if (!relgot
2cdcc330 3733 || !bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 3734 return FALSE;
e717da7e
AM
3735
3736 ppc64_elf_tdata (abfd)->got = got;
3737 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 3738 return TRUE;
65f38f15 3739}
5bd4f169 3740
b31867b6
AM
3741/* Follow indirect and warning symbol links. */
3742
3743static inline struct bfd_link_hash_entry *
3744follow_link (struct bfd_link_hash_entry *h)
3745{
3746 while (h->type == bfd_link_hash_indirect
3747 || h->type == bfd_link_hash_warning)
3748 h = h->u.i.link;
3749 return h;
3750}
3751
3752static inline struct elf_link_hash_entry *
3753elf_follow_link (struct elf_link_hash_entry *h)
3754{
3755 return (struct elf_link_hash_entry *) follow_link (&h->root);
3756}
3757
3758static inline struct ppc_link_hash_entry *
3759ppc_follow_link (struct ppc_link_hash_entry *h)
3760{
3761 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3762}
3763
40d16e0b
AM
3764/* Merge PLT info on FROM with that on TO. */
3765
3766static void
3767move_plt_plist (struct ppc_link_hash_entry *from,
3768 struct ppc_link_hash_entry *to)
3769{
3770 if (from->elf.plt.plist != NULL)
3771 {
3772 if (to->elf.plt.plist != NULL)
3773 {
3774 struct plt_entry **entp;
3775 struct plt_entry *ent;
3776
3777 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3778 {
3779 struct plt_entry *dent;
3780
3781 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3782 if (dent->addend == ent->addend)
3783 {
3784 dent->plt.refcount += ent->plt.refcount;
3785 *entp = ent->next;
3786 break;
3787 }
3788 if (dent == NULL)
3789 entp = &ent->next;
3790 }
3791 *entp = to->elf.plt.plist;
3792 }
3793
3794 to->elf.plt.plist = from->elf.plt.plist;
3795 from->elf.plt.plist = NULL;
3796 }
3797}
3798
65f38f15
AM
3799/* Copy the extra info we tack onto an elf_link_hash_entry. */
3800
3801static void
fcfa13d2
AM
3802ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3803 struct elf_link_hash_entry *dir,
3804 struct elf_link_hash_entry *ind)
65f38f15
AM
3805{
3806 struct ppc_link_hash_entry *edir, *eind;
3807
3808 edir = (struct ppc_link_hash_entry *) dir;
3809 eind = (struct ppc_link_hash_entry *) ind;
3810
c79d6685
AM
3811 edir->is_func |= eind->is_func;
3812 edir->is_func_descriptor |= eind->is_func_descriptor;
3813 edir->tls_mask |= eind->tls_mask;
3814 if (eind->oh != NULL)
3815 edir->oh = ppc_follow_link (eind->oh);
3816
474436e6 3817 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
3818 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3819 edir->elf.ref_regular |= eind->elf.ref_regular;
3820 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 3821 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
3822 edir->elf.needs_plt |= eind->elf.needs_plt;
3823 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 3824
d311bc8b
AM
3825 /* If we were called to copy over info for a weak sym, don't copy
3826 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3827 in order to simplify readonly_dynrelocs and save a field in the
3828 symbol hash entry, but that means dyn_relocs can't be used in any
3829 tests about a specific symbol, or affect other symbol flags which
ab2477e1 3830 are then tested. */
d311bc8b 3831 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 3832 return;
d311bc8b 3833
411e1bfb 3834 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 3835 if (eind->dyn_relocs != NULL)
65f38f15 3836 {
bbd7ec4a
AM
3837 if (edir->dyn_relocs != NULL)
3838 {
6061a67d
AM
3839 struct elf_dyn_relocs **pp;
3840 struct elf_dyn_relocs *p;
bbd7ec4a 3841
fcfa13d2 3842 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
3843 list. Merge any entries against the same section. */
3844 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3845 {
6061a67d 3846 struct elf_dyn_relocs *q;
bbd7ec4a
AM
3847
3848 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3849 if (q->sec == p->sec)
3850 {
3851 q->pc_count += p->pc_count;
3852 q->count += p->count;
3853 *pp = p->next;
3854 break;
3855 }
3856 if (q == NULL)
3857 pp = &p->next;
3858 }
3859 *pp = edir->dyn_relocs;
3860 }
3861
65f38f15
AM
3862 edir->dyn_relocs = eind->dyn_relocs;
3863 eind->dyn_relocs = NULL;
3864 }
65f38f15 3865
81848ca0
AM
3866 /* Copy over got entries that we may have already seen to the
3867 symbol which just became indirect. */
411e1bfb
AM
3868 if (eind->elf.got.glist != NULL)
3869 {
3870 if (edir->elf.got.glist != NULL)
3871 {
3872 struct got_entry **entp;
3873 struct got_entry *ent;
3874
3875 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3876 {
3877 struct got_entry *dent;
3878
3879 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3880 if (dent->addend == ent->addend
e717da7e 3881 && dent->owner == ent->owner
411e1bfb
AM
3882 && dent->tls_type == ent->tls_type)
3883 {
3884 dent->got.refcount += ent->got.refcount;
3885 *entp = ent->next;
3886 break;
3887 }
3888 if (dent == NULL)
3889 entp = &ent->next;
3890 }
3891 *entp = edir->elf.got.glist;
3892 }
3893
3894 edir->elf.got.glist = eind->elf.got.glist;
3895 eind->elf.got.glist = NULL;
3896 }
3897
3898 /* And plt entries. */
40d16e0b 3899 move_plt_plist (eind, edir);
411e1bfb 3900
fcfa13d2 3901 if (eind->elf.dynindx != -1)
411e1bfb 3902 {
fcfa13d2
AM
3903 if (edir->elf.dynindx != -1)
3904 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3905 edir->elf.dynstr_index);
411e1bfb
AM
3906 edir->elf.dynindx = eind->elf.dynindx;
3907 edir->elf.dynstr_index = eind->elf.dynstr_index;
3908 eind->elf.dynindx = -1;
3909 eind->elf.dynstr_index = 0;
3910 }
411e1bfb
AM
3911}
3912
8387904d
AM
3913/* Find the function descriptor hash entry from the given function code
3914 hash entry FH. Link the entries via their OH fields. */
3915
3916static struct ppc_link_hash_entry *
b31867b6 3917lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
3918{
3919 struct ppc_link_hash_entry *fdh = fh->oh;
3920
3921 if (fdh == NULL)
3922 {
3923 const char *fd_name = fh->elf.root.root.string + 1;
3924
3925 fdh = (struct ppc_link_hash_entry *)
3926 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
3927 if (fdh == NULL)
3928 return fdh;
3929
3930 fdh->is_func_descriptor = 1;
3931 fdh->oh = fh;
3932 fh->is_func = 1;
3933 fh->oh = fdh;
8387904d
AM
3934 }
3935
8c5b4e52
AM
3936 fdh = ppc_follow_link (fdh);
3937 fdh->is_func_descriptor = 1;
3938 fdh->oh = fh;
3939 return fdh;
8387904d
AM
3940}
3941
8c5b4e52 3942/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
3943
3944static struct ppc_link_hash_entry *
3945make_fdh (struct bfd_link_info *info,
908b32fc 3946 struct ppc_link_hash_entry *fh)
bb700d78 3947{
8c5b4e52
AM
3948 bfd *abfd = fh->elf.root.u.undef.abfd;
3949 struct bfd_link_hash_entry *bh = NULL;
bb700d78 3950 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
3951 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3952 ? BSF_WEAK
3953 : BSF_GLOBAL);
3954
3955 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3956 fh->elf.root.root.string + 1,
3957 flags, bfd_und_section_ptr, 0,
3958 NULL, FALSE, FALSE, &bh))
bb700d78
AM
3959 return NULL;
3960
3961 fdh = (struct ppc_link_hash_entry *) bh;
3962 fdh->elf.non_elf = 0;
908b32fc
AM
3963 fdh->fake = 1;
3964 fdh->is_func_descriptor = 1;
3965 fdh->oh = fh;
3966 fh->is_func = 1;
3967 fh->oh = fdh;
bb700d78
AM
3968 return fdh;
3969}
3970
8387904d
AM
3971/* Fix function descriptor symbols defined in .opd sections to be
3972 function type. */
555cd476
AM
3973
3974static bfd_boolean
c16153ae 3975ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 3976 struct bfd_link_info *info,
555cd476 3977 Elf_Internal_Sym *isym,
6911b7dc 3978 const char **name,
555cd476
AM
3979 flagword *flags ATTRIBUTE_UNUSED,
3980 asection **sec,
b53dfeb2 3981 bfd_vma *value)
555cd476 3982{
b53dfeb2 3983 if (*sec != NULL
f1885d1e 3984 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
3985 {
3986 asection *code_sec;
3987
3988 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3989 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
3990 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
3991
3992 /* If the symbol is a function defined in .opd, and the function
3993 code is in a discarded group, let it appear to be undefined. */
0e1862bb 3994 if (!bfd_link_relocatable (info)
b53dfeb2
AM
3995 && (*sec)->reloc_count != 0
3996 && opd_entry_value (*sec, *value, &code_sec, NULL,
3997 FALSE) != (bfd_vma) -1
3998 && discarded_section (code_sec))
3999 {
4000 *sec = bfd_und_section_ptr;
4001 isym->st_shndx = SHN_UNDEF;
4002 }
4003 }
dbd1e97e
AM
4004 else if (*sec != NULL
4005 && strcmp ((*sec)->name, ".toc") == 0
4006 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4007 {
4008 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4009 if (htab != NULL)
4010 htab->params->object_in_toc = 1;
4011 }
433817dd 4012
6911b7dc
AM
4013 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4014 {
4015 if (abiversion (ibfd) == 0)
4016 set_abiversion (ibfd, 2);
4017 else if (abiversion (ibfd) == 1)
4018 {
cf97bcb0
AM
4019 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4020 " for ABI version 1"), *name);
6911b7dc
AM
4021 bfd_set_error (bfd_error_bad_value);
4022 return FALSE;
4023 }
4024 }
4025
555cd476
AM
4026 return TRUE;
4027}
4028
6911b7dc
AM
4029/* Merge non-visibility st_other attributes: local entry point. */
4030
4031static void
4032ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4033 const Elf_Internal_Sym *isym,
4034 bfd_boolean definition,
4035 bfd_boolean dynamic)
4036{
f378ab09 4037 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
4038 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4039 | ELF_ST_VISIBILITY (h->other));
4040}
4041
8c5b4e52
AM
4042/* Hook called on merging a symbol. We use this to clear "fake" since
4043 we now have a real symbol. */
4044
4045static bfd_boolean
4046ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4047 const Elf_Internal_Sym *isym,
8c5b4e52
AM
4048 asection **psec ATTRIBUTE_UNUSED,
4049 bfd_boolean newdef ATTRIBUTE_UNUSED,
4050 bfd_boolean olddef ATTRIBUTE_UNUSED,
4051 bfd *oldbfd ATTRIBUTE_UNUSED,
4052 const asection *oldsec ATTRIBUTE_UNUSED)
4053{
4054 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
4055 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4056 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
4057 return TRUE;
4058}
4059
8387904d 4060/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4061 inclusion of a new ABI object archive that defines "bar".
4062 NAME is a symbol defined in an archive. Return a symbol in the hash
4063 table that might be satisfied by the archive symbols. */
8387904d
AM
4064
4065static struct elf_link_hash_entry *
4066ppc64_elf_archive_symbol_lookup (bfd *abfd,
4067 struct bfd_link_info *info,
4068 const char *name)
4069{
4070 struct elf_link_hash_entry *h;
4071 char *dot_name;
4072 size_t len;
4073
4074 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4075 if (h != NULL
4076 /* Don't return this sym if it is a fake function descriptor
4077 created by add_symbol_adjust. */
8c5b4e52 4078 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
4079 return h;
4080
4081 if (name[0] == '.')
4082 return h;
4083
4084 len = strlen (name);
4085 dot_name = bfd_alloc (abfd, len + 2);
4086 if (dot_name == NULL)
e99955cd 4087 return (struct elf_link_hash_entry *) -1;
8387904d
AM
4088 dot_name[0] = '.';
4089 memcpy (dot_name + 1, name, len + 1);
4090 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4091 bfd_release (abfd, dot_name);
4092 return h;
4093}
4094
4095/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4096 new ABI object defines "bar". Well, at least, undefined dot symbols
4097 are made weak. This stops later archive searches from including an
4098 object if we already have a function descriptor definition. It also
35b0ce59
AM
4099 prevents the linker complaining about undefined symbols.
4100 We also check and correct mismatched symbol visibility here. The
4101 most restrictive visibility of the function descriptor and the
4102 function entry symbol is used. */
8387904d
AM
4103
4104static bfd_boolean
b3fac117 4105add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4106{
8387904d
AM
4107 struct ppc_link_hash_table *htab;
4108 struct ppc_link_hash_entry *fdh;
4109
b3fac117
AM
4110 if (eh->elf.root.type == bfd_link_hash_warning)
4111 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4112
8400d40d
AM
4113 if (eh->elf.root.type == bfd_link_hash_indirect)
4114 return TRUE;
4115
b3fac117
AM
4116 if (eh->elf.root.root.string[0] != '.')
4117 abort ();
8387904d 4118
b3fac117 4119 htab = ppc_hash_table (info);
4dfe6ac6
NC
4120 if (htab == NULL)
4121 return FALSE;
4122
b31867b6 4123 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4124 if (fdh == NULL
4125 && !bfd_link_relocatable (info)
4126 && (eh->elf.root.type == bfd_link_hash_undefined
4127 || eh->elf.root.type == bfd_link_hash_undefweak)
4128 && eh->elf.ref_regular)
4129 {
4130 /* Make an undefined function descriptor sym, in order to
4131 pull in an --as-needed shared lib. Archives are handled
4132 elsewhere. */
4133 fdh = make_fdh (info, eh);
4134 if (fdh == NULL)
4135 return FALSE;
bb700d78 4136 }
8c5b4e52
AM
4137
4138 if (fdh != NULL)
8387904d 4139 {
35b0ce59
AM
4140 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4141 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4142
4143 /* Make both descriptor and entry symbol have the most
4144 constraining visibility of either symbol. */
35b0ce59
AM
4145 if (entry_vis < descr_vis)
4146 fdh->elf.other += entry_vis - descr_vis;
4147 else if (entry_vis > descr_vis)
4148 eh->elf.other += descr_vis - entry_vis;
4149
8c5b4e52
AM
4150 /* Propagate reference flags from entry symbol to function
4151 descriptor symbol. */
bc4e12de 4152 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4153 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4154 fdh->elf.ref_regular |= eh->elf.ref_regular;
4155 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4156
4157 if (!fdh->elf.forced_local
4158 && fdh->elf.dynindx == -1
4159 && fdh->elf.versioned != versioned_hidden
4160 && (bfd_link_dll (info)
4161 || fdh->elf.def_dynamic
4162 || fdh->elf.ref_dynamic)
4163 && (eh->elf.ref_regular
4164 || eh->elf.def_regular))
4165 {
2cdcc330 4166 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
8c5b4e52 4167 return FALSE;
35b0ce59 4168 }
8387904d 4169 }
99877b66 4170
8387904d
AM
4171 return TRUE;
4172}
4173
f6c7c3e8
AM
4174/* Set up opd section info and abiversion for IBFD, and process list
4175 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4176
8387904d 4177static bfd_boolean
f6c7c3e8 4178ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4179{
99877b66 4180 struct ppc_link_hash_table *htab;
b3fac117 4181 struct ppc_link_hash_entry **p, *eh;
459609d6 4182 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4183
459609d6 4184 if (opd != NULL && opd->size != 0)
b3fac117 4185 {
b9399fcf
AM
4186 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4187 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4188
459609d6
AM
4189 if (abiversion (ibfd) == 0)
4190 set_abiversion (ibfd, 1);
8a2058b5 4191 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4192 {
695344c0 4193 /* xgettext:c-format */
cf97bcb0
AM
4194 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4195 ibfd, abiversion (ibfd));
459609d6
AM
4196 bfd_set_error (bfd_error_bad_value);
4197 return FALSE;
f6c7c3e8 4198 }
b9399fcf 4199 }
f6c7c3e8 4200
b9399fcf
AM
4201 if (is_ppc64_elf (info->output_bfd))
4202 {
4203 /* For input files without an explicit abiversion in e_flags
4204 we should have flagged any with symbol st_other bits set
4205 as ELFv1 and above flagged those with .opd as ELFv2.
4206 Set the output abiversion if not yet set, and for any input
4207 still ambiguous, take its abiversion from the output.
4208 Differences in ABI are reported later. */
4209 if (abiversion (info->output_bfd) == 0)
4210 set_abiversion (info->output_bfd, abiversion (ibfd));
4211 else if (abiversion (ibfd) == 0)
4212 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4213 }
4214
459609d6
AM
4215 htab = ppc_hash_table (info);
4216 if (htab == NULL)
b9399fcf 4217 return TRUE;
459609d6 4218
b9399fcf
AM
4219 if (opd != NULL && opd->size != 0
4220 && (ibfd->flags & DYNAMIC) == 0
4221 && (opd->flags & SEC_RELOC) != 0
4222 && opd->reloc_count != 0
4223 && !bfd_is_abs_section (opd->output_section)
4224 && info->gc_sections)
4225 {
4226 /* Garbage collection needs some extra help with .opd sections.
4227 We don't want to necessarily keep everything referenced by
4228 relocs in .opd, as that would keep all functions. Instead,
4229 if we reference an .opd symbol (a function descriptor), we
4230 want to keep the function code symbol's section. This is
4231 easy for global symbols, but for local syms we need to keep
4232 information about the associated function section. */
4233 bfd_size_type amt;
4234 asection **opd_sym_map;
4235 Elf_Internal_Shdr *symtab_hdr;
4236 Elf_Internal_Rela *relocs, *rel_end, *rel;
4237
4238 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4239 opd_sym_map = bfd_zalloc (ibfd, amt);
4240 if (opd_sym_map == NULL)
4241 return FALSE;
4242 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4243 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4244 info->keep_memory);
4245 if (relocs == NULL)
4246 return FALSE;
4247 symtab_hdr = &elf_symtab_hdr (ibfd);
4248 rel_end = relocs + opd->reloc_count - 1;
4249 for (rel = relocs; rel < rel_end; rel++)
4250 {
4251 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4252 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4253
4254 if (r_type == R_PPC64_ADDR64
4255 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4256 && r_symndx < symtab_hdr->sh_info)
4257 {
4258 Elf_Internal_Sym *isym;
4259 asection *s;
4260
4261 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4262 if (isym == NULL)
4263 {
4264 if (elf_section_data (opd)->relocs != relocs)
4265 free (relocs);
4266 return FALSE;
4267 }
4268
4269 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4270 if (s != NULL && s != opd)
4271 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4272 }
4273 }
4274 if (elf_section_data (opd)->relocs != relocs)
4275 free (relocs);
4276 }
459609d6
AM
4277
4278 p = &htab->dot_syms;
4279 while ((eh = *p) != NULL)
4280 {
4281 *p = NULL;
4282 if (&eh->elf == htab->elf.hgot)
4283 ;
4284 else if (htab->elf.hgot == NULL
4285 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4286 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4287 else if (abiversion (ibfd) <= 1)
4288 {
4289 htab->need_func_desc_adj = 1;
4290 if (!add_symbol_adjust (eh, info))
4291 return FALSE;
4292 }
459609d6
AM
4293 p = &eh->u.next_dot_sym;
4294 }
b3fac117 4295 return TRUE;
8387904d
AM
4296}
4297
97fed1c9
JJ
4298/* Undo hash table changes when an --as-needed input file is determined
4299 not to be needed. */
4300
4301static bfd_boolean
e5034e59
AM
4302ppc64_elf_notice_as_needed (bfd *ibfd,
4303 struct bfd_link_info *info,
4304 enum notice_asneeded_action act)
97fed1c9 4305{
e5034e59
AM
4306 if (act == notice_not_needed)
4307 {
4308 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4309
e5034e59
AM
4310 if (htab == NULL)
4311 return FALSE;
4dfe6ac6 4312
e5034e59
AM
4313 htab->dot_syms = NULL;
4314 }
4315 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4316}
4317
aa374f67
AM
4318/* If --just-symbols against a final linked binary, then assume we need
4319 toc adjusting stubs when calling functions defined there. */
4320
4321static void
4322ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4323{
4324 if ((sec->flags & SEC_CODE) != 0
4325 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4326 && is_ppc64_elf (sec->owner))
4327 {
2c3f079f
AM
4328 if (abiversion (sec->owner) >= 2
4329 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4330 sec->has_toc_reloc = 1;
4331 }
4332 _bfd_elf_link_just_syms (sec, info);
4333}
4334
e054468f 4335static struct plt_entry **
4ce794b7
AM
4336update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4337 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4338{
4339 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4340 struct plt_entry **local_plt;
f961d9dd 4341 unsigned char *local_got_tls_masks;
411e1bfb
AM
4342
4343 if (local_got_ents == NULL)
4344 {
4345 bfd_size_type size = symtab_hdr->sh_info;
4346
e054468f
AM
4347 size *= (sizeof (*local_got_ents)
4348 + sizeof (*local_plt)
4349 + sizeof (*local_got_tls_masks));
4ce794b7 4350 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4351 if (local_got_ents == NULL)
e054468f 4352 return NULL;
411e1bfb
AM
4353 elf_local_got_ents (abfd) = local_got_ents;
4354 }
4355
37da22e5 4356 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4357 {
4358 struct got_entry *ent;
4359
4360 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4361 if (ent->addend == r_addend
4362 && ent->owner == abfd
4363 && ent->tls_type == tls_type)
411e1bfb
AM
4364 break;
4365 if (ent == NULL)
4366 {
4367 bfd_size_type amt = sizeof (*ent);
4ce794b7 4368 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4369 if (ent == NULL)
4370 return FALSE;
4371 ent->next = local_got_ents[r_symndx];
4372 ent->addend = r_addend;
e717da7e 4373 ent->owner = abfd;
411e1bfb 4374 ent->tls_type = tls_type;
927be08e 4375 ent->is_indirect = FALSE;
411e1bfb
AM
4376 ent->got.refcount = 0;
4377 local_got_ents[r_symndx] = ent;
4378 }
4379 ent->got.refcount += 1;
4380 }
4381
e054468f 4382 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4383 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4384 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4385
4386 return local_plt + r_symndx;
65f38f15
AM
4387}
4388
411e1bfb 4389static bfd_boolean
e054468f 4390update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4391{
411e1bfb 4392 struct plt_entry *ent;
1e2f5b6e 4393
e054468f 4394 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4395 if (ent->addend == addend)
4396 break;
4397 if (ent == NULL)
1e2f5b6e 4398 {
411e1bfb 4399 bfd_size_type amt = sizeof (*ent);
4ce794b7 4400 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4401 if (ent == NULL)
4402 return FALSE;
e054468f 4403 ent->next = *plist;
411e1bfb
AM
4404 ent->addend = addend;
4405 ent->plt.refcount = 0;
e054468f 4406 *plist = ent;
1e2f5b6e 4407 }
411e1bfb 4408 ent->plt.refcount += 1;
b34976b6 4409 return TRUE;
1e2f5b6e
AM
4410}
4411
e054468f
AM
4412static bfd_boolean
4413is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4414{
4415 return (r_type == R_PPC64_REL24
05d0e962 4416 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
4417 || r_type == R_PPC64_REL14
4418 || r_type == R_PPC64_REL14_BRTAKEN
4419 || r_type == R_PPC64_REL14_BRNTAKEN
4420 || r_type == R_PPC64_ADDR24
4421 || r_type == R_PPC64_ADDR14
4422 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4423 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4424 || r_type == R_PPC64_PLTCALL
4425 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4426}
4427
4428/* Relocs on inline plt call sequence insns prior to the call. */
4429
4430static bfd_boolean
4431is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4432{
4433 return (r_type == R_PPC64_PLT16_HA
4434 || r_type == R_PPC64_PLT16_HI
4435 || r_type == R_PPC64_PLT16_LO
4436 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4437 || r_type == R_PPC64_PLT_PCREL34
4438 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4439 || r_type == R_PPC64_PLTSEQ
4440 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4441}
4442
5bd4f169 4443/* Look through the relocs for a section during the first phase, and
65f38f15 4444 calculate needed space in the global offset table, procedure
5d1634d7 4445 linkage table, and dynamic reloc sections. */
5bd4f169 4446
b34976b6 4447static bfd_boolean
4ce794b7
AM
4448ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4449 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4450{
65f38f15 4451 struct ppc_link_hash_table *htab;
5bd4f169 4452 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4453 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4454 const Elf_Internal_Rela *rel;
4455 const Elf_Internal_Rela *rel_end;
5bd4f169 4456 asection *sreloc;
3a71aa26 4457 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 4458 bfd_boolean is_opd;
5bd4f169 4459
0e1862bb 4460 if (bfd_link_relocatable (info))
b34976b6 4461 return TRUE;
5bd4f169 4462
680a3378
AM
4463 /* Don't do anything special with non-loaded, non-alloced sections.
4464 In particular, any relocs in such sections should not affect GOT
4465 and PLT reference counting (ie. we don't allow them to create GOT
4466 or PLT entries), there's no possibility or desire to optimize TLS
4467 relocs, and there's not much point in propagating relocs to shared
4468 libs that the dynamic linker won't relocate. */
4469 if ((sec->flags & SEC_ALLOC) == 0)
4470 return TRUE;
4471
0c8d6e5c 4472 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4473
65f38f15 4474 htab = ppc_hash_table (info);
4dfe6ac6
NC
4475 if (htab == NULL)
4476 return FALSE;
4477
3a71aa26
AM
4478 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4479 FALSE, FALSE, TRUE);
4480 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4481 FALSE, FALSE, TRUE);
0ffa91dd 4482 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4483 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4484 sreloc = NULL;
b9399fcf 4485 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4486 rel_end = relocs + sec->reloc_count;
4487 for (rel = relocs; rel < rel_end; rel++)
4488 {
4489 unsigned long r_symndx;
4490 struct elf_link_hash_entry *h;
04c9666a 4491 enum elf_ppc64_reloc_type r_type;
727fc41e 4492 int tls_type;
7c8fe5c4 4493 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4494 struct plt_entry **ifunc, **plt_list;
5663e321 4495 bfd_vma sym_addend;
5bd4f169
AM
4496
4497 r_symndx = ELF64_R_SYM (rel->r_info);
4498 if (r_symndx < symtab_hdr->sh_info)
4499 h = NULL;
4500 else
973a3492
L
4501 {
4502 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4503 h = elf_follow_link (h);
1c865ab2
AM
4504
4505 if (h == htab->elf.hgot)
4506 sec->has_toc_reloc = 1;
973a3492 4507 }
5bd4f169 4508
727fc41e 4509 tls_type = 0;
e054468f 4510 ifunc = NULL;
5663e321
AM
4511 r_type = ELF64_R_TYPE (rel->r_info);
4512 switch (r_type)
4513 {
04bdff6a
AM
4514 case R_PPC64_D34:
4515 case R_PPC64_D34_LO:
4516 case R_PPC64_D34_HI30:
4517 case R_PPC64_D34_HA30:
4518 case R_PPC64_D28:
4519 htab->powerxx_stubs = 1;
4520 /* Fall through. */
5663e321
AM
4521 default:
4522 /* Somewhat foolishly, because the ABIs don't specifically
4523 allow it, ppc64 gas and ld support GOT and PLT relocs
4524 with non-zero addends where the addend results in
4525 sym+addend being stored in the GOT or PLT entry. This
4526 can't be supported for pcrel relocs because the addend is
4527 used to specify the pcrel offset. */
4528 sym_addend = rel->r_addend;
4529 break;
04bdff6a
AM
4530
4531 case R_PPC64_PCREL34:
5663e321
AM
4532 case R_PPC64_GOT_PCREL34:
4533 case R_PPC64_PLT_PCREL34:
4534 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a
AM
4535 case R_PPC64_PCREL28:
4536 htab->powerxx_stubs = 1;
5663e321
AM
4537 sym_addend = 0;
4538 break;
4539 }
25f23106
AM
4540 if (h != NULL)
4541 {
4542 if (h->type == STT_GNU_IFUNC)
4543 {
4544 h->needs_plt = 1;
4545 ifunc = &h->plt.plist;
4546 }
4547 }
4548 else
4549 {
4550 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4551 abfd, r_symndx);
4552 if (isym == NULL)
4553 return FALSE;
4554
4555 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4556 {
4557 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4558 sym_addend,
37da22e5 4559 NON_GOT | PLT_IFUNC);
25f23106
AM
4560 if (ifunc == NULL)
4561 return FALSE;
4562 }
4563 }
727fc41e 4564
a33d1f77 4565 switch (r_type)
5bd4f169 4566 {
727fc41e
AM
4567 case R_PPC64_TLSGD:
4568 case R_PPC64_TLSLD:
4569 /* These special tls relocs tie a call to __tls_get_addr with
4570 its parameter symbol. */
37da22e5
AM
4571 if (h != NULL)
4572 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4573 else
4574 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4575 sym_addend,
37da22e5
AM
4576 NON_GOT | TLS_TLS | TLS_MARK))
4577 return FALSE;
4578 sec->has_tls_reloc = 1;
727fc41e
AM
4579 break;
4580
411e1bfb
AM
4581 case R_PPC64_GOT_TLSLD16:
4582 case R_PPC64_GOT_TLSLD16_LO:
4583 case R_PPC64_GOT_TLSLD16_HI:
4584 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 4585 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4586 goto dogottls;
4587
4588 case R_PPC64_GOT_TLSGD16:
4589 case R_PPC64_GOT_TLSGD16_LO:
4590 case R_PPC64_GOT_TLSGD16_HI:
4591 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 4592 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4593 goto dogottls;
4594
4595 case R_PPC64_GOT_TPREL16_DS:
4596 case R_PPC64_GOT_TPREL16_LO_DS:
4597 case R_PPC64_GOT_TPREL16_HI:
4598 case R_PPC64_GOT_TPREL16_HA:
7c8bbca5 4599 if (bfd_link_dll (info))
411e1bfb
AM
4600 info->flags |= DF_STATIC_TLS;
4601 tls_type = TLS_TLS | TLS_TPREL;
4602 goto dogottls;
4603
4604 case R_PPC64_GOT_DTPREL16_DS:
4605 case R_PPC64_GOT_DTPREL16_LO_DS:
4606 case R_PPC64_GOT_DTPREL16_HI:
4607 case R_PPC64_GOT_DTPREL16_HA:
4608 tls_type = TLS_TLS | TLS_DTPREL;
4609 dogottls:
4610 sec->has_tls_reloc = 1;
066f4018 4611 goto dogot;
411e1bfb 4612
65f38f15 4613 case R_PPC64_GOT16_HA:
066f4018 4614 case R_PPC64_GOT16_LO_DS:
4a421c53 4615 case R_PPC64_GOT_PCREL34:
066f4018
AM
4616 ppc64_elf_tdata (abfd)->has_gotrel = 1;
4617 ppc64_elf_section_data (sec)->has_gotrel = 1;
4618 /* Fall through. */
4619
bb22a418 4620 case R_PPC64_GOT16_DS:
066f4018 4621 case R_PPC64_GOT16:
65f38f15
AM
4622 case R_PPC64_GOT16_HI:
4623 case R_PPC64_GOT16_LO:
066f4018 4624 dogot:
65f38f15 4625 /* This symbol requires a global offset table entry. */
4c52953f 4626 sec->has_toc_reloc = 1;
33c0ec9d
AM
4627 if (r_type == R_PPC64_GOT_TLSLD16
4628 || r_type == R_PPC64_GOT_TLSGD16
4629 || r_type == R_PPC64_GOT_TPREL16_DS
4630 || r_type == R_PPC64_GOT_DTPREL16_DS
4631 || r_type == R_PPC64_GOT16
4632 || r_type == R_PPC64_GOT16_DS)
4633 {
4634 htab->do_multi_toc = 1;
d77c8a4b 4635 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4636 }
4637
e717da7e
AM
4638 if (ppc64_elf_tdata (abfd)->got == NULL
4639 && !create_got_section (abfd, info))
b34976b6 4640 return FALSE;
5bd4f169
AM
4641
4642 if (h != NULL)
4643 {
411e1bfb
AM
4644 struct ppc_link_hash_entry *eh;
4645 struct got_entry *ent;
65f38f15 4646
411e1bfb
AM
4647 eh = (struct ppc_link_hash_entry *) h;
4648 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5663e321 4649 if (ent->addend == sym_addend
e717da7e 4650 && ent->owner == abfd
411e1bfb
AM
4651 && ent->tls_type == tls_type)
4652 break;
4653 if (ent == NULL)
5bd4f169 4654 {
411e1bfb 4655 bfd_size_type amt = sizeof (*ent);
4ce794b7 4656 ent = bfd_alloc (abfd, amt);
411e1bfb 4657 if (ent == NULL)
b34976b6 4658 return FALSE;
411e1bfb 4659 ent->next = eh->elf.got.glist;
5663e321 4660 ent->addend = sym_addend;
e717da7e 4661 ent->owner = abfd;
411e1bfb 4662 ent->tls_type = tls_type;
927be08e 4663 ent->is_indirect = FALSE;
411e1bfb
AM
4664 ent->got.refcount = 0;
4665 eh->elf.got.glist = ent;
5bd4f169 4666 }
411e1bfb 4667 ent->got.refcount += 1;
e7b938ca 4668 eh->tls_mask |= tls_type;
5bd4f169 4669 }
411e1bfb
AM
4670 else
4671 /* This is a global offset table entry for a local symbol. */
4672 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4673 sym_addend, tls_type))
411e1bfb 4674 return FALSE;
a345bc8d
AM
4675
4676 /* We may also need a plt entry if the symbol turns out to be
4677 an ifunc. */
0e1862bb 4678 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d 4679 {
5663e321 4680 if (!update_plt_info (abfd, &h->plt.plist, sym_addend))
a345bc8d
AM
4681 return FALSE;
4682 }
5bd4f169
AM
4683 break;
4684
5bd4f169 4685 case R_PPC64_PLT16_HA:
65f38f15
AM
4686 case R_PPC64_PLT16_HI:
4687 case R_PPC64_PLT16_LO:
08be3224 4688 case R_PPC64_PLT16_LO_DS:
5663e321
AM
4689 case R_PPC64_PLT_PCREL34:
4690 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
4691 case R_PPC64_PLT32:
4692 case R_PPC64_PLT64:
cbf95972
AM
4693 /* This symbol requires a procedure linkage table entry. */
4694 plt_list = ifunc;
4695 if (h != NULL)
e054468f 4696 {
e054468f
AM
4697 h->needs_plt = 1;
4698 if (h->root.root.string[0] == '.'
4699 && h->root.root.string[1] != '\0')
4700 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2d7ad24e 4701 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4702 plt_list = &h->plt.plist;
4703 }
4704 if (plt_list == NULL)
2d7ad24e 4705 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4706 sym_addend,
2d7ad24e 4707 NON_GOT | PLT_KEEP);
5663e321 4708 if (!update_plt_info (abfd, plt_list, sym_addend))
cbf95972 4709 return FALSE;
5bd4f169
AM
4710 break;
4711
4712 /* The following relocations don't need to propagate the
4713 relocation if linking a shared object since they are
4714 section relative. */
4715 case R_PPC64_SECTOFF:
4716 case R_PPC64_SECTOFF_LO:
4717 case R_PPC64_SECTOFF_HI:
4718 case R_PPC64_SECTOFF_HA:
4719 case R_PPC64_SECTOFF_DS:
4720 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4721 case R_PPC64_DTPREL16:
4722 case R_PPC64_DTPREL16_LO:
4723 case R_PPC64_DTPREL16_HI:
4724 case R_PPC64_DTPREL16_HA:
4725 case R_PPC64_DTPREL16_DS:
4726 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
4727 case R_PPC64_DTPREL16_HIGH:
4728 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
4729 case R_PPC64_DTPREL16_HIGHER:
4730 case R_PPC64_DTPREL16_HIGHERA:
4731 case R_PPC64_DTPREL16_HIGHEST:
4732 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4733 break;
4734
ad8e1ba5 4735 /* Nor do these. */
25f23106
AM
4736 case R_PPC64_REL16:
4737 case R_PPC64_REL16_LO:
4738 case R_PPC64_REL16_HI:
4739 case R_PPC64_REL16_HA:
4a969973
AM
4740 case R_PPC64_REL16_HIGH:
4741 case R_PPC64_REL16_HIGHA:
4742 case R_PPC64_REL16_HIGHER:
4743 case R_PPC64_REL16_HIGHERA:
4744 case R_PPC64_REL16_HIGHEST:
4745 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
4746 case R_PPC64_REL16_HIGHER34:
4747 case R_PPC64_REL16_HIGHERA34:
4748 case R_PPC64_REL16_HIGHEST34:
4749 case R_PPC64_REL16_HIGHESTA34:
a680de9a 4750 case R_PPC64_REL16DX_HA:
25f23106
AM
4751 break;
4752
45965137
AM
4753 /* Not supported as a dynamic relocation. */
4754 case R_PPC64_ADDR64_LOCAL:
0e1862bb 4755 if (bfd_link_pic (info))
45965137
AM
4756 {
4757 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4758 ppc_howto_init ();
695344c0 4759 /* xgettext:c-format */
174d0a74 4760 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 4761 "in shared libraries and PIEs\n"),
45965137
AM
4762 abfd, sec, rel->r_offset,
4763 ppc64_elf_howto_table[r_type]->name);
4764 bfd_set_error (bfd_error_bad_value);
4765 return FALSE;
4766 }
4767 break;
4768
ad8e1ba5 4769 case R_PPC64_TOC16:
33c0ec9d
AM
4770 case R_PPC64_TOC16_DS:
4771 htab->do_multi_toc = 1;
d77c8a4b 4772 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 4773 /* Fall through. */
ad8e1ba5
AM
4774 case R_PPC64_TOC16_LO:
4775 case R_PPC64_TOC16_HI:
4776 case R_PPC64_TOC16_HA:
ad8e1ba5 4777 case R_PPC64_TOC16_LO_DS:
4c52953f 4778 sec->has_toc_reloc = 1;
ad8e1ba5
AM
4779 break;
4780
006589cf
AM
4781 /* Marker reloc. */
4782 case R_PPC64_ENTRY:
4783 break;
4784
5bd4f169
AM
4785 /* This relocation describes the C++ object vtable hierarchy.
4786 Reconstruct it for later use during GC. */
4787 case R_PPC64_GNU_VTINHERIT:
c152c796 4788 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4789 return FALSE;
5bd4f169
AM
4790 break;
4791
4792 /* This relocation describes which C++ vtable entries are actually
4793 used. Record for later use during GC. */
4794 case R_PPC64_GNU_VTENTRY:
a0ea3a14 4795 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4796 return FALSE;
5bd4f169
AM
4797 break;
4798
721956f4
AM
4799 case R_PPC64_REL14:
4800 case R_PPC64_REL14_BRTAKEN:
4801 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4802 {
4803 asection *dest = NULL;
4804
4805 /* Heuristic: If jumping outside our section, chances are
4806 we are going to need a stub. */
4807 if (h != NULL)
4808 {
4809 /* If the sym is weak it may be overridden later, so
4810 don't assume we know where a weak sym lives. */
4811 if (h->root.type == bfd_link_hash_defined)
4812 dest = h->root.u.def.section;
4813 }
4814 else
87d72d41
AM
4815 {
4816 Elf_Internal_Sym *isym;
4817
4818 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4819 abfd, r_symndx);
4820 if (isym == NULL)
4821 return FALSE;
4822
4823 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4824 }
4825
220c76dd 4826 if (dest != sec)
7c8fe5c4 4827 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4828 }
3e04d765
AM
4829 goto rel24;
4830
4831 case R_PPC64_PLTCALL:
5663e321 4832 case R_PPC64_PLTCALL_NOTOC:
3e04d765 4833 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
4834 /* Fall through. */
4835
5d1634d7 4836 case R_PPC64_REL24:
05d0e962 4837 case R_PPC64_REL24_NOTOC:
3e04d765 4838 rel24:
cbf95972
AM
4839 plt_list = ifunc;
4840 if (h != NULL)
5d1634d7 4841 {
e054468f
AM
4842 h->needs_plt = 1;
4843 if (h->root.root.string[0] == '.'
4844 && h->root.root.string[1] != '\0')
4845 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 4846
3a71aa26 4847 if (h == tga || h == dottga)
cbf95972
AM
4848 {
4849 sec->has_tls_reloc = 1;
4850 if (rel != relocs
4851 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4852 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4853 /* We have a new-style __tls_get_addr call with
4854 a marker reloc. */
4855 ;
4856 else
4857 /* Mark this section as having an old-style call. */
4858 sec->has_tls_get_addr_call = 1;
4859 }
4860 plt_list = &h->plt.plist;
411e1bfb 4861 }
cbf95972
AM
4862
4863 /* We may need a .plt entry if the function this reloc
4864 refers to is in a shared lib. */
4865 if (plt_list
5663e321 4866 && !update_plt_info (abfd, plt_list, sym_addend))
cbf95972 4867 return FALSE;
411e1bfb
AM
4868 break;
4869
cbf95972
AM
4870 case R_PPC64_ADDR14:
4871 case R_PPC64_ADDR14_BRNTAKEN:
4872 case R_PPC64_ADDR14_BRTAKEN:
4873 case R_PPC64_ADDR24:
4874 goto dodyn;
4875
411e1bfb
AM
4876 case R_PPC64_TPREL64:
4877 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 4878 if (bfd_link_dll (info))
411e1bfb
AM
4879 info->flags |= DF_STATIC_TLS;
4880 goto dotlstoc;
4881
4882 case R_PPC64_DTPMOD64:
4883 if (rel + 1 < rel_end
4884 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4885 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 4886 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 4887 else
951fd09b 4888 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
4889 goto dotlstoc;
4890
4891 case R_PPC64_DTPREL64:
4892 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4893 if (rel != relocs
4894 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4895 && rel[-1].r_offset == rel->r_offset - 8)
4896 /* This is the second reloc of a dtpmod, dtprel pair.
4897 Don't mark with TLS_DTPREL. */
4898 goto dodyn;
4899
4900 dotlstoc:
4901 sec->has_tls_reloc = 1;
4902 if (h != NULL)
4903 {
4904 struct ppc_link_hash_entry *eh;
4905 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 4906 eh->tls_mask |= tls_type;
411e1bfb
AM
4907 }
4908 else
4909 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4910 sym_addend, tls_type))
411e1bfb
AM
4911 return FALSE;
4912
7c8fe5c4
AM
4913 ppc64_sec = ppc64_elf_section_data (sec);
4914 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 4915 {
3a71aa26
AM
4916 bfd_size_type amt;
4917
e7b938ca 4918 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
4919 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4920 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4921 if (ppc64_sec->u.toc.symndx == NULL)
4922 return FALSE;
4923 amt = sec->size * sizeof (bfd_vma) / 8;
4924 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4925 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 4926 return FALSE;
7c8fe5c4
AM
4927 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4928 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
4929 }
4930 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26 4931 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5663e321 4932 ppc64_sec->u.toc.add[rel->r_offset / 8] = sym_addend;
951fd09b
AM
4933
4934 /* Mark the second slot of a GD or LD entry.
4935 -1 to indicate GD and -2 to indicate LD. */
4936 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 4937 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 4938 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 4939 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
4940 goto dodyn;
4941
4942 case R_PPC64_TPREL16:
4943 case R_PPC64_TPREL16_LO:
4944 case R_PPC64_TPREL16_HI:
4945 case R_PPC64_TPREL16_HA:
4946 case R_PPC64_TPREL16_DS:
4947 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
4948 case R_PPC64_TPREL16_HIGH:
4949 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
4950 case R_PPC64_TPREL16_HIGHER:
4951 case R_PPC64_TPREL16_HIGHERA:
4952 case R_PPC64_TPREL16_HIGHEST:
4953 case R_PPC64_TPREL16_HIGHESTA:
7c8bbca5
AM
4954 if (bfd_link_dll (info))
4955 info->flags |= DF_STATIC_TLS;
4956 goto dodyn;
5d1634d7 4957
e86ce104 4958 case R_PPC64_ADDR64:
b9399fcf 4959 if (is_opd
1e2f5b6e 4960 && rel + 1 < rel_end
4ce794b7 4961 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 4962 {
8387904d 4963 if (h != NULL)
8c5b4e52 4964 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 4965 }
e86ce104
AM
4966 /* Fall through. */
4967
65f38f15
AM
4968 case R_PPC64_ADDR16:
4969 case R_PPC64_ADDR16_DS:
4970 case R_PPC64_ADDR16_HA:
4971 case R_PPC64_ADDR16_HI:
f9c6b907
AM
4972 case R_PPC64_ADDR16_HIGH:
4973 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
4974 case R_PPC64_ADDR16_HIGHER:
4975 case R_PPC64_ADDR16_HIGHERA:
4976 case R_PPC64_ADDR16_HIGHEST:
4977 case R_PPC64_ADDR16_HIGHESTA:
4978 case R_PPC64_ADDR16_LO:
4979 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
4980 case R_PPC64_D34:
4981 case R_PPC64_D34_LO:
4982 case R_PPC64_D34_HI30:
4983 case R_PPC64_D34_HA30:
4984 case R_PPC64_ADDR16_HIGHER34:
4985 case R_PPC64_ADDR16_HIGHERA34:
4986 case R_PPC64_ADDR16_HIGHEST34:
4987 case R_PPC64_ADDR16_HIGHESTA34:
4988 case R_PPC64_D28:
0e1862bb 4989 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
4990 && rel->r_addend == 0)
4991 {
4992 /* We may need a .plt entry if this reloc refers to a
4993 function in a shared lib. */
5663e321 4994 if (!update_plt_info (abfd, &h->plt.plist, 0))
a345bc8d
AM
4995 return FALSE;
4996 h->pointer_equality_needed = 1;
4997 }
4998 /* Fall through. */
4999
5000 case R_PPC64_REL30:
5001 case R_PPC64_REL32:
5002 case R_PPC64_REL64:
65f38f15 5003 case R_PPC64_ADDR32:
65f38f15
AM
5004 case R_PPC64_UADDR16:
5005 case R_PPC64_UADDR32:
5006 case R_PPC64_UADDR64:
5bd4f169 5007 case R_PPC64_TOC:
0e1862bb 5008 if (h != NULL && !bfd_link_pic (info))
81848ca0 5009 /* We may need a copy reloc. */
f5385ebf 5010 h->non_got_ref = 1;
81848ca0 5011
41bd81ab 5012 /* Don't propagate .opd relocs. */
b9399fcf 5013 if (NO_OPD_RELOCS && is_opd)
e86ce104 5014 break;
e86ce104 5015
65f38f15
AM
5016 /* If we are creating a shared library, and this is a reloc
5017 against a global symbol, or a non PC relative reloc
5018 against a local symbol, then we need to copy the reloc
5019 into the shared library. However, if we are linking with
5020 -Bsymbolic, we do not need to copy a reloc against a
5021 global symbol which is defined in an object we are
5022 including in the link (i.e., DEF_REGULAR is set). At
5023 this point we have not seen all the input files, so it is
5024 possible that DEF_REGULAR is not set now but will be set
5025 later (it is never cleared). In case of a weak definition,
5026 DEF_REGULAR may be cleared later by a strong definition in
5027 a shared library. We account for that possibility below by
f4656909 5028 storing information in the dyn_relocs field of the hash
65f38f15
AM
5029 table entry. A similar situation occurs when creating
5030 shared libraries and symbol visibility changes render the
5031 symbol local.
5032
5033 If on the other hand, we are creating an executable, we
5034 may need to keep relocations for symbols satisfied by a
5035 dynamic library if we manage to avoid copy relocs for the
5036 symbol. */
411e1bfb 5037 dodyn:
0e1862bb 5038 if ((bfd_link_pic (info)
1d483afe 5039 && (must_be_dyn_reloc (info, r_type)
65f38f15 5040 || (h != NULL
198f1157 5041 && (!SYMBOLIC_BIND (info, h)
65f38f15 5042 || h->root.type == bfd_link_hash_defweak
f5385ebf 5043 || !h->def_regular))))
f4656909 5044 || (ELIMINATE_COPY_RELOCS
0e1862bb 5045 && !bfd_link_pic (info)
65f38f15
AM
5046 && h != NULL
5047 && (h->root.type == bfd_link_hash_defweak
25f23106 5048 || !h->def_regular))
0e1862bb 5049 || (!bfd_link_pic (info)
25f23106 5050 && ifunc != NULL))
5bd4f169 5051 {
65f38f15
AM
5052 /* We must copy these reloc types into the output file.
5053 Create a reloc section in dynobj and make room for
5054 this reloc. */
5bd4f169
AM
5055 if (sreloc == NULL)
5056 {
83bac4b0
NC
5057 sreloc = _bfd_elf_make_dynamic_reloc_section
5058 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5059
5bd4f169 5060 if (sreloc == NULL)
83bac4b0 5061 return FALSE;
5bd4f169
AM
5062 }
5063
65f38f15
AM
5064 /* If this is a global symbol, we count the number of
5065 relocations we need for this symbol. */
5066 if (h != NULL)
5067 {
19e08130
AM
5068 struct elf_dyn_relocs *p;
5069 struct elf_dyn_relocs **head;
5070
ec338859 5071 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5072 p = *head;
5073 if (p == NULL || p->sec != sec)
5074 {
5075 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5076 if (p == NULL)
5077 return FALSE;
5078 p->next = *head;
5079 *head = p;
5080 p->sec = sec;
5081 p->count = 0;
5082 p->pc_count = 0;
5083 }
5084 p->count += 1;
5085 if (!must_be_dyn_reloc (info, r_type))
5086 p->pc_count += 1;
65f38f15
AM
5087 }
5088 else
5089 {
ec338859
AM
5090 /* Track dynamic relocs needed for local syms too.
5091 We really need local syms available to do this
5092 easily. Oh well. */
19e08130
AM
5093 struct ppc_dyn_relocs *p;
5094 struct ppc_dyn_relocs **head;
5095 bfd_boolean is_ifunc;
ec338859 5096 asection *s;
6edfbbad 5097 void *vpp;
87d72d41 5098 Elf_Internal_Sym *isym;
6edfbbad 5099
87d72d41
AM
5100 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5101 abfd, r_symndx);
5102 if (isym == NULL)
b34976b6 5103 return FALSE;
ec338859 5104
87d72d41
AM
5105 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5106 if (s == NULL)
5107 s = sec;
5108
6edfbbad 5109 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5110 head = (struct ppc_dyn_relocs **) vpp;
5111 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5112 p = *head;
5113 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5114 p = p->next;
5115 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5116 {
5117 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5118 if (p == NULL)
5119 return FALSE;
5120 p->next = *head;
5121 *head = p;
5122 p->sec = sec;
5123 p->ifunc = is_ifunc;
5124 p->count = 0;
5125 }
5126 p->count += 1;
ec338859 5127 }
65f38f15 5128 }
5bd4f169 5129 break;
65f38f15
AM
5130
5131 default:
96e0dda4 5132 break;
5bd4f169
AM
5133 }
5134 }
5135
b34976b6 5136 return TRUE;
5bd4f169
AM
5137}
5138
ee67d69a
AM
5139/* Merge backend specific data from an object file to the output
5140 object file when linking. */
5141
5142static bfd_boolean
50e03d47 5143ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5144{
50e03d47 5145 bfd *obfd = info->output_bfd;
ee67d69a
AM
5146 unsigned long iflags, oflags;
5147
5148 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5149 return TRUE;
5150
5151 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5152 return TRUE;
5153
50e03d47 5154 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
5155 return FALSE;
5156
5157 iflags = elf_elfheader (ibfd)->e_flags;
5158 oflags = elf_elfheader (obfd)->e_flags;
5159
f6c7c3e8 5160 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5161 {
4eca0228 5162 _bfd_error_handler
695344c0 5163 /* xgettext:c-format */
871b3ab2 5164 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
5165 bfd_set_error (bfd_error_bad_value);
5166 return FALSE;
5167 }
f6c7c3e8 5168 else if (iflags != oflags && iflags != 0)
ee67d69a 5169 {
4eca0228 5170 _bfd_error_handler
695344c0 5171 /* xgettext:c-format */
871b3ab2 5172 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5173 ibfd, iflags, oflags);
5174 bfd_set_error (bfd_error_bad_value);
5175 return FALSE;
5176 }
5177
4a91d0ba
AM
5178 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5179 return FALSE;
005d79fd 5180
ee67d69a 5181 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5182 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5183}
5184
5185static bfd_boolean
5186ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5187{
5188 /* Print normal ELF private data. */
5189 _bfd_elf_print_private_bfd_data (abfd, ptr);
5190
5191 if (elf_elfheader (abfd)->e_flags != 0)
5192 {
5193 FILE *file = ptr;
5194
ee67d69a
AM
5195 fprintf (file, _("private flags = 0x%lx:"),
5196 elf_elfheader (abfd)->e_flags);
5197
5198 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5199 fprintf (file, _(" [abiv%ld]"),
5200 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5201 fputc ('\n', file);
5202 }
5203
5204 return TRUE;
5205}
5206
8387904d 5207/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5208 of the code entry point, and its section, which must be in the same
5209 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5210
5211static bfd_vma
5212opd_entry_value (asection *opd_sec,
5213 bfd_vma offset,
5214 asection **code_sec,
aef36ac1
AM
5215 bfd_vma *code_off,
5216 bfd_boolean in_code_sec)
8387904d
AM
5217{
5218 bfd *opd_bfd = opd_sec->owner;
8860955f 5219 Elf_Internal_Rela *relocs;
8387904d 5220 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5221 bfd_vma val;
8387904d 5222
9f296da3
AM
5223 /* No relocs implies we are linking a --just-symbols object, or looking
5224 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5225 if (opd_sec->reloc_count == 0)
5226 {
729eabd5 5227 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5228
729eabd5
AM
5229 if (contents == NULL)
5230 {
5231 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5232 return (bfd_vma) -1;
5233 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5234 }
ee1e4ede 5235
dbb3fbbb 5236 /* PR 17512: file: 64b9dfbb. */
451dfd38 5237 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5238 return (bfd_vma) -1;
5239
729eabd5 5240 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5241 if (code_sec != NULL)
5242 {
5243 asection *sec, *likely = NULL;
ee1e4ede 5244
aef36ac1 5245 if (in_code_sec)
4b85d634 5246 {
aef36ac1
AM
5247 sec = *code_sec;
5248 if (sec->vma <= val
5249 && val < sec->vma + sec->size)
5250 likely = sec;
5251 else
5252 val = -1;
5253 }
5254 else
5255 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5256 if (sec->vma <= val
5257 && (sec->flags & SEC_LOAD) != 0
5258 && (sec->flags & SEC_ALLOC) != 0)
5259 likely = sec;
5260 if (likely != NULL)
5261 {
5262 *code_sec = likely;
5263 if (code_off != NULL)
5264 *code_off = val - likely->vma;
4b85d634
AM
5265 }
5266 }
aef36ac1 5267 return val;
4b85d634
AM
5268 }
5269
0c8d6e5c 5270 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5271
729eabd5 5272 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5273 if (relocs == NULL)
5274 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
5275 /* PR 17512: file: df8e1fd6. */
5276 if (relocs == NULL)
5277 return (bfd_vma) -1;
645ea6a9 5278
8387904d 5279 /* Go find the opd reloc at the sym address. */
8860955f 5280 lo = relocs;
8387904d 5281 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5282 val = (bfd_vma) -1;
8387904d
AM
5283 while (lo < hi)
5284 {
5285 look = lo + (hi - lo) / 2;
5286 if (look->r_offset < offset)
5287 lo = look + 1;
5288 else if (look->r_offset > offset)
5289 hi = look;
5290 else
5291 {
0ffa91dd
NC
5292 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5293
8387904d
AM
5294 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5295 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5296 {
5297 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5298 asection *sec = NULL;
8387904d 5299
b53dfeb2
AM
5300 if (symndx >= symtab_hdr->sh_info
5301 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5302 {
5303 struct elf_link_hash_entry **sym_hashes;
5304 struct elf_link_hash_entry *rh;
5305
5306 sym_hashes = elf_sym_hashes (opd_bfd);
5307 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5308 if (rh != NULL)
5309 {
5310 rh = elf_follow_link (rh);
bb854a36
AM
5311 if (rh->root.type != bfd_link_hash_defined
5312 && rh->root.type != bfd_link_hash_defweak)
5313 break;
5314 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5315 {
bb854a36
AM
5316 val = rh->root.u.def.value;
5317 sec = rh->root.u.def.section;
b53dfeb2
AM
5318 }
5319 }
5320 }
5321
5322 if (sec == NULL)
5323 {
5324 Elf_Internal_Sym *sym;
5325
5326 if (symndx < symtab_hdr->sh_info)
5327 {
5328 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5329 if (sym == NULL)
5330 {
5331 size_t symcnt = symtab_hdr->sh_info;
5332 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5333 symcnt, 0,
5334 NULL, NULL, NULL);
5335 if (sym == NULL)
5336 break;
5337 symtab_hdr->contents = (bfd_byte *) sym;
5338 }
5339 sym += symndx;
128205bb
AM
5340 }
5341 else
5342 {
b53dfeb2
AM
5343 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5344 1, symndx,
5345 NULL, NULL, NULL);
128205bb
AM
5346 if (sym == NULL)
5347 break;
128205bb 5348 }
b53dfeb2
AM
5349 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5350 if (sec == NULL)
5351 break;
5352 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5353 val = sym->st_value;
8387904d 5354 }
b53dfeb2 5355
8387904d
AM
5356 val += look->r_addend;
5357 if (code_off != NULL)
5358 *code_off = val;
5359 if (code_sec != NULL)
aef36ac1
AM
5360 {
5361 if (in_code_sec && *code_sec != sec)
5362 return -1;
5363 else
5364 *code_sec = sec;
5365 }
b53dfeb2 5366 if (sec->output_section != NULL)
8387904d 5367 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5368 }
5369 break;
5370 }
5371 }
645ea6a9 5372
645ea6a9 5373 return val;
8387904d
AM
5374}
5375
aef36ac1
AM
5376/* If the ELF symbol SYM might be a function in SEC, return the
5377 function size and set *CODE_OFF to the function's entry point,
5378 otherwise return zero. */
9f296da3 5379
aef36ac1
AM
5380static bfd_size_type
5381ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5382 bfd_vma *code_off)
9f296da3 5383{
aef36ac1
AM
5384 bfd_size_type size;
5385
5386 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5387 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5388 return 0;
5389
5390 size = 0;
5391 if (!(sym->flags & BSF_SYNTHETIC))
5392 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5393
5394 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5395 {
b07bca4e
AM
5396 struct _opd_sec_data *opd = get_opd_info (sym->section);
5397 bfd_vma symval = sym->value;
5398
5399 if (opd != NULL
5400 && opd->adjust != NULL
5401 && elf_section_data (sym->section)->relocs != NULL)
5402 {
5403 /* opd_entry_value will use cached relocs that have been
5404 adjusted, but with raw symbols. That means both local
5405 and global symbols need adjusting. */
5406 long adjust = opd->adjust[OPD_NDX (symval)];
5407 if (adjust == -1)
5408 return 0;
5409 symval += adjust;
5410 }
5411
5412 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
5413 &sec, code_off, TRUE) == (bfd_vma) -1)
5414 return 0;
5415 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5416 symbol. This size has nothing to do with the code size of the
5417 function, which is what we're supposed to return, but the
5418 code size isn't available without looking up the dot-sym.
5419 However, doing that would be a waste of time particularly
5420 since elf_find_function will look at the dot-sym anyway.
5421 Now, elf_find_function will keep the largest size of any
5422 function sym found at the code address of interest, so return
5423 1 here to avoid it incorrectly caching a larger function size
5424 for a small function. This does mean we return the wrong
5425 size for a new-ABI function of size 24, but all that does is
5426 disable caching for such functions. */
5427 if (size == 24)
5428 size = 1;
9f296da3 5429 }
aef36ac1
AM
5430 else
5431 {
5432 if (sym->section != sec)
5433 return 0;
5434 *code_off = sym->value;
5435 }
5436 if (size == 0)
5437 size = 1;
5438 return size;
9f296da3
AM
5439}
5440
f378ab09
AM
5441/* Return true if symbol is a strong function defined in an ELFv2
5442 object with st_other localentry bits of zero, ie. its local entry
5443 point coincides with its global entry point. */
5444
5445static bfd_boolean
5446is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5447{
5448 return (h != NULL
5449 && h->type == STT_FUNC
5450 && h->root.type == bfd_link_hash_defined
5451 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 5452 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
5453 && is_ppc64_elf (h->root.u.def.section->owner)
5454 && abiversion (h->root.u.def.section->owner) >= 2);
5455}
5456
854b41e7
AM
5457/* Return true if symbol is defined in a regular object file. */
5458
5459static bfd_boolean
5460is_static_defined (struct elf_link_hash_entry *h)
5461{
5462 return ((h->root.type == bfd_link_hash_defined
5463 || h->root.type == bfd_link_hash_defweak)
5464 && h->root.u.def.section != NULL
5465 && h->root.u.def.section->output_section != NULL);
5466}
5467
b31867b6
AM
5468/* If FDH is a function descriptor symbol, return the associated code
5469 entry symbol if it is defined. Return NULL otherwise. */
5470
5471static struct ppc_link_hash_entry *
5472defined_code_entry (struct ppc_link_hash_entry *fdh)
5473{
5474 if (fdh->is_func_descriptor)
5475 {
5476 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5477 if (fh->elf.root.type == bfd_link_hash_defined
5478 || fh->elf.root.type == bfd_link_hash_defweak)
5479 return fh;
5480 }
5481 return NULL;
5482}
5483
5484/* If FH is a function code entry symbol, return the associated
5485 function descriptor symbol if it is defined. Return NULL otherwise. */
5486
5487static struct ppc_link_hash_entry *
5488defined_func_desc (struct ppc_link_hash_entry *fh)
5489{
5490 if (fh->oh != NULL
5491 && fh->oh->is_func_descriptor)
5492 {
5493 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5494 if (fdh->elf.root.type == bfd_link_hash_defined
5495 || fdh->elf.root.type == bfd_link_hash_defweak)
5496 return fdh;
5497 }
5498 return NULL;
5499}
5500
8c5b4e52
AM
5501static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5502
5503/* Garbage collect sections, after first dealing with dot-symbols. */
5504
5505static bfd_boolean
5506ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5507{
5508 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5509
5510 if (htab != NULL && htab->need_func_desc_adj)
5511 {
5512 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5513 htab->need_func_desc_adj = 0;
5514 }
5515 return bfd_elf_gc_sections (abfd, info);
5516}
5517
74f0fb50
AM
5518/* Mark all our entry sym sections, both opd and code section. */
5519
5520static void
5521ppc64_elf_gc_keep (struct bfd_link_info *info)
5522{
5523 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5524 struct bfd_sym_chain *sym;
5525
4dfe6ac6
NC
5526 if (htab == NULL)
5527 return;
5528
74f0fb50
AM
5529 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5530 {
b31867b6 5531 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5532 asection *sec;
5533
5534 eh = (struct ppc_link_hash_entry *)
b31867b6 5535 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5536 if (eh == NULL)
5537 continue;
5538 if (eh->elf.root.type != bfd_link_hash_defined
5539 && eh->elf.root.type != bfd_link_hash_defweak)
5540 continue;
5541
b31867b6
AM
5542 fh = defined_code_entry (eh);
5543 if (fh != NULL)
74f0fb50 5544 {
b31867b6 5545 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5546 sec->flags |= SEC_KEEP;
5547 }
5548 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5549 && opd_entry_value (eh->elf.root.u.def.section,
5550 eh->elf.root.u.def.value,
aef36ac1 5551 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
5552 sec->flags |= SEC_KEEP;
5553
5554 sec = eh->elf.root.u.def.section;
5555 sec->flags |= SEC_KEEP;
5556 }
5557}
5558
64d03ab5
AM
5559/* Mark sections containing dynamically referenced symbols. When
5560 building shared libraries, we must assume that any visible symbol is
5561 referenced. */
5562
5563static bfd_boolean
5564ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5565{
5566 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5567 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5568 struct ppc_link_hash_entry *fdh;
b407645f 5569 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5570
64d03ab5 5571 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5572 fdh = defined_func_desc (eh);
5573 if (fdh != NULL)
5574 eh = fdh;
64d03ab5
AM
5575
5576 if ((eh->elf.root.type == bfd_link_hash_defined
5577 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 5578 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5579 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5580 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5581 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5582 && (!bfd_link_executable (info)
e278ae05 5583 || info->gc_keep_exported
b407645f
AM
5584 || info->export_dynamic
5585 || (eh->elf.dynamic
5586 && d != NULL
2cdcc330
AM
5587 && (*d->match) (&d->head, NULL,
5588 eh->elf.root.root.string)))
e278ae05 5589 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5590 || !bfd_hide_sym_by_version (info->version_info,
5591 eh->elf.root.root.string)))))
64d03ab5
AM
5592 {
5593 asection *code_sec;
b31867b6 5594 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5595
5596 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5597
5598 /* Function descriptor syms cause the associated
5599 function code sym section to be marked. */
b31867b6
AM
5600 fh = defined_code_entry (eh);
5601 if (fh != NULL)
5602 {
5603 code_sec = fh->elf.root.u.def.section;
5604 code_sec->flags |= SEC_KEEP;
5605 }
64d03ab5
AM
5606 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5607 && opd_entry_value (eh->elf.root.u.def.section,
5608 eh->elf.root.u.def.value,
aef36ac1 5609 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
5610 code_sec->flags |= SEC_KEEP;
5611 }
5612
5613 return TRUE;
5614}
5615
5bd4f169
AM
5616/* Return the section that should be marked against GC for a given
5617 relocation. */
5618
5619static asection *
4ce794b7 5620ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5621 struct bfd_link_info *info,
4ce794b7
AM
5622 Elf_Internal_Rela *rel,
5623 struct elf_link_hash_entry *h,
5624 Elf_Internal_Sym *sym)
5bd4f169 5625{
ccfa59ea
AM
5626 asection *rsec;
5627
ccfa59ea
AM
5628 /* Syms return NULL if we're marking .opd, so we avoid marking all
5629 function sections, as all functions are referenced in .opd. */
5630 rsec = NULL;
5631 if (get_opd_info (sec) != NULL)
5632 return rsec;
1e2f5b6e 5633
5bd4f169
AM
5634 if (h != NULL)
5635 {
04c9666a 5636 enum elf_ppc64_reloc_type r_type;
b31867b6 5637 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5638
4ce794b7 5639 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5640 switch (r_type)
5bd4f169
AM
5641 {
5642 case R_PPC64_GNU_VTINHERIT:
5643 case R_PPC64_GNU_VTENTRY:
5644 break;
5645
5646 default:
5647 switch (h->root.type)
5648 {
5649 case bfd_link_hash_defined:
5650 case bfd_link_hash_defweak:
ccfa59ea 5651 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5652 fdh = defined_func_desc (eh);
5653 if (fdh != NULL)
8c5b4e52
AM
5654 {
5655 /* -mcall-aixdesc code references the dot-symbol on
5656 a call reloc. Mark the function descriptor too
5657 against garbage collection. */
5658 fdh->elf.mark = 1;
60d67dc8
AM
5659 if (fdh->elf.is_weakalias)
5660 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
5661 eh = fdh;
5662 }
1e2f5b6e
AM
5663
5664 /* Function descriptor syms cause the associated
5665 function code sym section to be marked. */
b31867b6
AM
5666 fh = defined_code_entry (eh);
5667 if (fh != NULL)
ccfa59ea
AM
5668 {
5669 /* They also mark their opd section. */
74f0fb50 5670 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5671
b31867b6 5672 rsec = fh->elf.root.u.def.section;
ccfa59ea 5673 }
8387904d
AM
5674 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5675 && opd_entry_value (eh->elf.root.u.def.section,
5676 eh->elf.root.u.def.value,
aef36ac1 5677 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 5678 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5679 else
1e2f5b6e
AM
5680 rsec = h->root.u.def.section;
5681 break;
5bd4f169
AM
5682
5683 case bfd_link_hash_common:
1e2f5b6e
AM
5684 rsec = h->root.u.c.p->section;
5685 break;
5bd4f169
AM
5686
5687 default:
fb34365b 5688 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5689 }
5690 }
5691 }
5692 else
5693 {
74f0fb50 5694 struct _opd_sec_data *opd;
1e2f5b6e
AM
5695
5696 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5697 opd = get_opd_info (rsec);
5698 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5699 {
74f0fb50 5700 rsec->gc_mark = 1;
ccfa59ea 5701
51aecdc5 5702 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 5703 }
5bd4f169
AM
5704 }
5705
1e2f5b6e 5706 return rsec;
5bd4f169
AM
5707}
5708
deb0e272
AM
5709/* The maximum size of .sfpr. */
5710#define SFPR_MAX (218*4)
5711
5712struct sfpr_def_parms
5713{
699733f6
AM
5714 const char name[12];
5715 unsigned char lo, hi;
2cdcc330
AM
5716 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5717 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
5718};
5719
a4b6fadd
AM
5720/* Auto-generate _save*, _rest* functions in .sfpr.
5721 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5722 instead. */
deb0e272 5723
4dfe6ac6 5724static bfd_boolean
a4b6fadd
AM
5725sfpr_define (struct bfd_link_info *info,
5726 const struct sfpr_def_parms *parm,
5727 asection *stub_sec)
deb0e272
AM
5728{
5729 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5730 unsigned int i;
5731 size_t len = strlen (parm->name);
5732 bfd_boolean writing = FALSE;
699733f6 5733 char sym[16];
deb0e272 5734
4dfe6ac6
NC
5735 if (htab == NULL)
5736 return FALSE;
5737
deb0e272
AM
5738 memcpy (sym, parm->name, len);
5739 sym[len + 2] = 0;
5740
5741 for (i = parm->lo; i <= parm->hi; i++)
5742 {
a4b6fadd 5743 struct ppc_link_hash_entry *h;
deb0e272
AM
5744
5745 sym[len + 0] = i / 10 + '0';
5746 sym[len + 1] = i % 10 + '0';
a4b6fadd 5747 h = (struct ppc_link_hash_entry *)
b32547cd 5748 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 5749 if (stub_sec != NULL)
deb0e272 5750 {
a4b6fadd
AM
5751 if (h != NULL
5752 && h->elf.root.type == bfd_link_hash_defined
5753 && h->elf.root.u.def.section == htab->sfpr)
5754 {
5755 struct elf_link_hash_entry *s;
5756 char buf[32];
5757 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5758 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5759 if (s == NULL)
5760 return FALSE;
5761 if (s->root.type == bfd_link_hash_new
5762 || (s->root.type = bfd_link_hash_defined
5763 && s->root.u.def.section == stub_sec))
5764 {
5765 s->root.type = bfd_link_hash_defined;
5766 s->root.u.def.section = stub_sec;
7dda8d3c 5767 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
5768 + h->elf.root.u.def.value);
5769 s->ref_regular = 1;
5770 s->def_regular = 1;
5771 s->ref_regular_nonweak = 1;
5772 s->forced_local = 1;
5773 s->non_elf = 0;
5774 s->root.linker_def = 1;
5775 }
5776 }
5777 continue;
5778 }
5779 if (h != NULL)
5780 {
5781 h->save_res = 1;
5782 if (!h->elf.def_regular)
deb0e272 5783 {
a4b6fadd
AM
5784 h->elf.root.type = bfd_link_hash_defined;
5785 h->elf.root.u.def.section = htab->sfpr;
5786 h->elf.root.u.def.value = htab->sfpr->size;
5787 h->elf.type = STT_FUNC;
5788 h->elf.def_regular = 1;
b32547cd 5789 h->elf.non_elf = 0;
a4b6fadd
AM
5790 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5791 writing = TRUE;
deb0e272 5792 if (htab->sfpr->contents == NULL)
a4b6fadd 5793 {
2cdcc330
AM
5794 htab->sfpr->contents
5795 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd
AM
5796 if (htab->sfpr->contents == NULL)
5797 return FALSE;
5798 }
deb0e272
AM
5799 }
5800 }
5801 if (writing)
5802 {
5803 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5804 if (i != parm->hi)
5805 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5806 else
5807 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5808 htab->sfpr->size = p - htab->sfpr->contents;
5809 }
5810 }
5811
5812 return TRUE;
5813}
5814
5815static bfd_byte *
5816savegpr0 (bfd *abfd, bfd_byte *p, int r)
5817{
5818 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5819 return p + 4;
5820}
5821
5822static bfd_byte *
5823savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5824{
5825 p = savegpr0 (abfd, p, r);
a078d95a 5826 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5827 p = p + 4;
5828 bfd_put_32 (abfd, BLR, p);
5829 return p + 4;
5830}
5831
5832static bfd_byte *
5833restgpr0 (bfd *abfd, bfd_byte *p, int r)
5834{
5835 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5836 return p + 4;
5837}
5838
5839static bfd_byte *
5840restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5841{
a078d95a 5842 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5843 p = p + 4;
5844 p = restgpr0 (abfd, p, r);
5845 bfd_put_32 (abfd, MTLR_R0, p);
5846 p = p + 4;
5847 if (r == 29)
5848 {
5849 p = restgpr0 (abfd, p, 30);
5850 p = restgpr0 (abfd, p, 31);
5851 }
5852 bfd_put_32 (abfd, BLR, p);
5853 return p + 4;
5854}
5855
5856static bfd_byte *
5857savegpr1 (bfd *abfd, bfd_byte *p, int r)
5858{
5859 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5860 return p + 4;
5861}
5862
5863static bfd_byte *
5864savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5865{
5866 p = savegpr1 (abfd, p, r);
5867 bfd_put_32 (abfd, BLR, p);
5868 return p + 4;
5869}
5870
5871static bfd_byte *
5872restgpr1 (bfd *abfd, bfd_byte *p, int r)
5873{
5874 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5875 return p + 4;
5876}
5877
5878static bfd_byte *
5879restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5880{
5881 p = restgpr1 (abfd, p, r);
5882 bfd_put_32 (abfd, BLR, p);
5883 return p + 4;
5884}
5885
5886static bfd_byte *
5887savefpr (bfd *abfd, bfd_byte *p, int r)
5888{
5889 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5890 return p + 4;
5891}
5892
5893static bfd_byte *
5894savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5895{
5896 p = savefpr (abfd, p, r);
a078d95a 5897 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5898 p = p + 4;
5899 bfd_put_32 (abfd, BLR, p);
5900 return p + 4;
5901}
5902
5903static bfd_byte *
5904restfpr (bfd *abfd, bfd_byte *p, int r)
5905{
5906 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5907 return p + 4;
5908}
5909
5910static bfd_byte *
5911restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5912{
a078d95a 5913 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5914 p = p + 4;
5915 p = restfpr (abfd, p, r);
5916 bfd_put_32 (abfd, MTLR_R0, p);
5917 p = p + 4;
5918 if (r == 29)
5919 {
5920 p = restfpr (abfd, p, 30);
5921 p = restfpr (abfd, p, 31);
5922 }
5923 bfd_put_32 (abfd, BLR, p);
5924 return p + 4;
5925}
5926
5927static bfd_byte *
5928savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5929{
5930 p = savefpr (abfd, p, r);
5931 bfd_put_32 (abfd, BLR, p);
5932 return p + 4;
5933}
5934
5935static bfd_byte *
5936restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5937{
5938 p = restfpr (abfd, p, r);
5939 bfd_put_32 (abfd, BLR, p);
5940 return p + 4;
5941}
5942
5943static bfd_byte *
5944savevr (bfd *abfd, bfd_byte *p, int r)
5945{
5946 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5947 p = p + 4;
5948 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5949 return p + 4;
5950}
5951
5952static bfd_byte *
5953savevr_tail (bfd *abfd, bfd_byte *p, int r)
5954{
5955 p = savevr (abfd, p, r);
5956 bfd_put_32 (abfd, BLR, p);
5957 return p + 4;
5958}
5959
5960static bfd_byte *
5961restvr (bfd *abfd, bfd_byte *p, int r)
5962{
5963 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5964 p = p + 4;
5965 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5966 return p + 4;
5967}
5968
5969static bfd_byte *
5970restvr_tail (bfd *abfd, bfd_byte *p, int r)
5971{
5972 p = restvr (abfd, p, r);
5973 bfd_put_32 (abfd, BLR, p);
5974 return p + 4;
5975}
5976
e86ce104
AM
5977/* Called via elf_link_hash_traverse to transfer dynamic linking
5978 information on function code symbol entries to their corresponding
5979 function descriptor symbol entries. */
deb0e272 5980
b34976b6 5981static bfd_boolean
4ce794b7 5982func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 5983{
e86ce104 5984 struct bfd_link_info *info;
65f38f15 5985 struct ppc_link_hash_table *htab;
50bc7936
AM
5986 struct ppc_link_hash_entry *fh;
5987 struct ppc_link_hash_entry *fdh;
5988 bfd_boolean force_local;
5bd4f169 5989
50bc7936
AM
5990 fh = (struct ppc_link_hash_entry *) h;
5991 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 5992 return TRUE;
e86ce104 5993
8c5b4e52
AM
5994 if (!fh->is_func)
5995 return TRUE;
5996
5997 if (fh->elf.root.root.string[0] != '.'
5998 || fh->elf.root.root.string[1] == '\0')
5999 return TRUE;
6000
4ce794b7 6001 info = inf;
65f38f15 6002 htab = ppc_hash_table (info);
4dfe6ac6
NC
6003 if (htab == NULL)
6004 return FALSE;
5bd4f169 6005
8c5b4e52
AM
6006 /* Find the corresponding function descriptor symbol. */
6007 fdh = lookup_fdh (fh, htab);
6008
c09bdfe5
AM
6009 /* Resolve undefined references to dot-symbols as the value
6010 in the function descriptor, if we have one in a regular object.
6011 This is to satisfy cases like ".quad .foo". Calls to functions
6012 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6013 if ((fh->elf.root.type == bfd_link_hash_undefined
6014 || fh->elf.root.type == bfd_link_hash_undefweak)
6015 && (fdh->elf.root.type == bfd_link_hash_defined
6016 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6017 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6018 && opd_entry_value (fdh->elf.root.u.def.section,
6019 fdh->elf.root.u.def.value,
c09bdfe5 6020 &fh->elf.root.u.def.section,
aef36ac1 6021 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6022 {
b31867b6 6023 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6024 fh->elf.forced_local = 1;
b31867b6
AM
6025 fh->elf.def_regular = fdh->elf.def_regular;
6026 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6027 }
6028
8c5b4e52
AM
6029 if (!fh->elf.dynamic)
6030 {
6031 struct plt_entry *ent;
5bd4f169 6032
8c5b4e52
AM
6033 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6034 if (ent->plt.refcount > 0)
6035 break;
6036 if (ent == NULL)
6037 return TRUE;
6038 }
5bd4f169 6039
8c5b4e52 6040 /* Create a descriptor as undefined if necessary. */
50bc7936 6041 if (fdh == NULL
0e1862bb 6042 && !bfd_link_executable (info)
50bc7936
AM
6043 && (fh->elf.root.type == bfd_link_hash_undefined
6044 || fh->elf.root.type == bfd_link_hash_undefweak))
6045 {
908b32fc 6046 fdh = make_fdh (info, fh);
bb700d78
AM
6047 if (fdh == NULL)
6048 return FALSE;
50bc7936 6049 }
648cca2c 6050
8c5b4e52 6051 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6052 if (fdh != NULL
6053 && fdh->fake
8c5b4e52
AM
6054 && (fh->elf.root.type == bfd_link_hash_defined
6055 || fh->elf.root.type == bfd_link_hash_defweak))
6056 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 6057
8c5b4e52
AM
6058 /* Transfer dynamic linking information to the function descriptor. */
6059 if (fdh != NULL)
6060 {
f5385ebf
AM
6061 fdh->elf.ref_regular |= fh->elf.ref_regular;
6062 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6063 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6064 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6065 fdh->elf.dynamic |= fh->elf.dynamic;
6066 fdh->elf.needs_plt |= (fh->elf.needs_plt
6067 || fh->elf.type == STT_FUNC
6068 || fh->elf.type == STT_GNU_IFUNC);
6069 move_plt_plist (fh, fdh);
6070
6071 if (!fdh->elf.forced_local
6072 && fh->elf.dynindx != -1)
6073 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6074 return FALSE;
e86ce104
AM
6075 }
6076
50bc7936
AM
6077 /* Now that the info is on the function descriptor, clear the
6078 function code sym info. Any function code syms for which we
6079 don't have a definition in a regular file, we force local.
6080 This prevents a shared library from exporting syms that have
6081 been imported from another library. Function code syms that
6082 are really in the library we must leave global to prevent the
6083 linker dragging in a definition from a static library. */
93f3fa99
AM
6084 force_local = (!fh->elf.def_regular
6085 || fdh == NULL
6086 || !fdh->elf.def_regular
6087 || fdh->elf.forced_local);
50bc7936
AM
6088 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6089
b34976b6 6090 return TRUE;
e86ce104 6091}
40b8271b 6092
a4b6fadd
AM
6093static const struct sfpr_def_parms save_res_funcs[] =
6094 {
6095 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6096 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6097 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6098 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6099 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6100 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6101 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6102 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6103 { "._savef", 14, 31, savefpr, savefpr1_tail },
6104 { "._restf", 14, 31, restfpr, restfpr1_tail },
6105 { "_savevr_", 20, 31, savevr, savevr_tail },
6106 { "_restvr_", 20, 31, restvr, restvr_tail }
6107 };
6108
e86ce104 6109/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6110 this hook to a) provide some gcc support functions, and b) transfer
6111 dynamic linking information gathered so far on function code symbol
6112 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6113
b34976b6 6114static bfd_boolean
4ce794b7
AM
6115ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6116 struct bfd_link_info *info)
e86ce104
AM
6117{
6118 struct ppc_link_hash_table *htab;
6119
6120 htab = ppc_hash_table (info);
4dfe6ac6
NC
6121 if (htab == NULL)
6122 return FALSE;
6123
b32547cd
AM
6124 /* Provide any missing _save* and _rest* functions. */
6125 if (htab->sfpr != NULL)
6126 {
6127 unsigned int i;
6128
6129 htab->sfpr->size = 0;
6130 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6131 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6132 return FALSE;
6133 if (htab->sfpr->size == 0)
6134 htab->sfpr->flags |= SEC_EXCLUDE;
6135 }
6136
6137 if (bfd_link_relocatable (info))
6138 return TRUE;
6139
6140 if (htab->elf.hgot != NULL)
dba6fa9b
AM
6141 {
6142 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6143 /* Make .TOC. defined so as to prevent it being made dynamic.
6144 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6145 if (!htab->elf.hgot->def_regular
6146 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6147 {
6148 htab->elf.hgot->root.type = bfd_link_hash_defined;
6149 htab->elf.hgot->root.u.def.value = 0;
6150 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6151 htab->elf.hgot->def_regular = 1;
6152 htab->elf.hgot->root.linker_def = 1;
6153 }
dba6fa9b 6154 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6155 htab->elf.hgot->other
6156 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6157 }
c66bb0ee 6158
8c5b4e52
AM
6159 if (htab->need_func_desc_adj)
6160 {
6161 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6162 htab->need_func_desc_adj = 0;
6163 }
805fc799 6164
b34976b6 6165 return TRUE;
e86ce104
AM
6166}
6167
98bbb1b8 6168/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 6169
98bbb1b8 6170static asection *
a345bc8d
AM
6171readonly_dynrelocs (struct elf_link_hash_entry *h)
6172{
6173 struct ppc_link_hash_entry *eh;
6174 struct elf_dyn_relocs *p;
6175
6176 eh = (struct ppc_link_hash_entry *) h;
6177 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6178 {
6179 asection *s = p->sec->output_section;
6180
6181 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 6182 return p->sec;
a345bc8d 6183 }
98bbb1b8 6184 return NULL;
a345bc8d
AM
6185}
6186
d311bc8b 6187/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6188 aliases, that apply to read-only sections. Cannot be used after
6189 size_dynamic_sections. */
d311bc8b
AM
6190
6191static bfd_boolean
6192alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6193{
6194 struct ppc_link_hash_entry *eh;
6195
6196 eh = (struct ppc_link_hash_entry *) h;
6197 do
6198 {
6199 if (readonly_dynrelocs (&eh->elf))
6200 return TRUE;
ab2477e1 6201 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
2cdcc330
AM
6202 }
6203 while (eh != NULL && &eh->elf != h);
d311bc8b
AM
6204
6205 return FALSE;
6206}
8a2058b5 6207
8a9e8e72
AM
6208/* Return whether EH has pc-relative dynamic relocs. */
6209
6210static bfd_boolean
6211pc_dynrelocs (struct ppc_link_hash_entry *eh)
6212{
6213 struct elf_dyn_relocs *p;
6214
6215 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6216 if (p->pc_count != 0)
6217 return TRUE;
6218 return FALSE;
6219}
6220
8a2058b5
AM
6221/* Return true if a global entry stub will be created for H. Valid
6222 for ELFv2 before plt entries have been allocated. */
6223
6224static bfd_boolean
6225global_entry_stub (struct elf_link_hash_entry *h)
6226{
6227 struct plt_entry *pent;
6228
6229 if (!h->pointer_equality_needed
6230 || h->def_regular)
6231 return FALSE;
6232
6233 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6234 if (pent->plt.refcount > 0
6235 && pent->addend == 0)
6236 return TRUE;
6237
6238 return FALSE;
6239}
6240
e86ce104
AM
6241/* Adjust a symbol defined by a dynamic object and referenced by a
6242 regular object. The current definition is in some section of the
6243 dynamic object, but we're not including those sections. We have to
6244 change the definition to something the rest of the link can
6245 understand. */
6246
b34976b6 6247static bfd_boolean
4ce794b7
AM
6248ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6249 struct elf_link_hash_entry *h)
e86ce104
AM
6250{
6251 struct ppc_link_hash_table *htab;
5474d94f 6252 asection *s, *srel;
e86ce104
AM
6253
6254 htab = ppc_hash_table (info);
4dfe6ac6
NC
6255 if (htab == NULL)
6256 return FALSE;
e86ce104
AM
6257
6258 /* Deal with function syms. */
6259 if (h->type == STT_FUNC
e054468f 6260 || h->type == STT_GNU_IFUNC
f5385ebf 6261 || h->needs_plt)
e86ce104 6262 {
529fe20e
AM
6263 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6264 || SYMBOL_CALLS_LOCAL (info, h)
6265 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6266 /* Discard dyn_relocs when non-pic if we've decided that a
6267 function symbol is local and not an ifunc. We keep dynamic
6268 relocs for ifuncs when local rather than always emitting a
6269 plt call stub for them and defining the symbol on the call
6270 stub. We can't do that for ELFv1 anyway (a function symbol
6271 is defined on a descriptor, not code) and it can be faster at
6272 run-time due to not needing to bounce through a stub. The
6273 dyn_relocs for ifuncs will be applied even in a static
6274 executable. */
6275 if (!bfd_link_pic (info)
6276 && h->type != STT_GNU_IFUNC
6277 && local)
6278 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6279
e86ce104
AM
6280 /* Clear procedure linkage table information for any symbol that
6281 won't need a .plt entry. */
411e1bfb
AM
6282 struct plt_entry *ent;
6283 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6284 if (ent->plt.refcount > 0)
6285 break;
8387904d 6286 if (ent == NULL
2d7ad24e
AM
6287 || (h->type != STT_GNU_IFUNC
6288 && local
3e04d765
AM
6289 && (htab->can_convert_all_inline_plt
6290 || (((struct ppc_link_hash_entry *) h)->tls_mask
6291 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6292 {
411e1bfb 6293 h->plt.plist = NULL;
f5385ebf 6294 h->needs_plt = 0;
d1eca1e4 6295 h->pointer_equality_needed = 0;
40b8271b 6296 }
8a2058b5 6297 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6298 {
d1eca1e4
AM
6299 /* Taking a function's address in a read/write section
6300 doesn't require us to define the function symbol in the
6301 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6302 be used instead. The reason we prefer a few more dynamic
6303 relocs is that calling via a global entry stub costs a
6304 few more instructions, and pointer_equality_needed causes
6305 extra work in ld.so when resolving these symbols. */
529fe20e 6306 if (global_entry_stub (h))
d1eca1e4 6307 {
ab2477e1 6308 if (!readonly_dynrelocs (h))
529fe20e
AM
6309 {
6310 h->pointer_equality_needed = 0;
04383fd1
AM
6311 /* If we haven't seen a branch reloc and the symbol
6312 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6313 if (!h->needs_plt)
6314 h->plt.plist = NULL;
6315 }
6316 else if (!bfd_link_pic (info))
6317 /* We are going to be defining the function symbol on the
6318 plt stub, so no dyn_relocs needed when non-pic. */
6319 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
6320 }
6321
3988aed5
AM
6322 /* ELFv2 function symbols can't have copy relocs. */
6323 return TRUE;
6324 }
6325 else if (!h->needs_plt
ab2477e1 6326 && !readonly_dynrelocs (h))
3988aed5 6327 {
04383fd1
AM
6328 /* If we haven't seen a branch reloc and the symbol isn't an
6329 ifunc then we don't need a plt entry. */
3988aed5
AM
6330 h->plt.plist = NULL;
6331 h->pointer_equality_needed = 0;
a345bc8d
AM
6332 return TRUE;
6333 }
5bd4f169 6334 }
bbd7ec4a 6335 else
411e1bfb 6336 h->plt.plist = NULL;
5bd4f169
AM
6337
6338 /* If this is a weak symbol, and there is a real definition, the
6339 processor independent code will have arranged for us to see the
6340 real definition first, and we can just use the same value. */
60d67dc8 6341 if (h->is_weakalias)
5bd4f169 6342 {
60d67dc8
AM
6343 struct elf_link_hash_entry *def = weakdef (h);
6344 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6345 h->root.u.def.section = def->root.u.def.section;
6346 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6347 if (def->root.u.def.section == htab->elf.sdynbss
6348 || def->root.u.def.section == htab->elf.sdynrelro)
6349 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 6350 return TRUE;
5bd4f169
AM
6351 }
6352
5bd4f169
AM
6353 /* If we are creating a shared library, we must presume that the
6354 only references to the symbol are via the global offset table.
6355 For such cases we need not do anything here; the relocations will
6356 be handled correctly by relocate_section. */
0e1862bb 6357 if (bfd_link_pic (info))
b34976b6 6358 return TRUE;
5bd4f169 6359
65f38f15
AM
6360 /* If there are no references to this symbol that do not use the
6361 GOT, we don't need to generate a copy reloc. */
f5385ebf 6362 if (!h->non_got_ref)
b34976b6 6363 return TRUE;
65f38f15 6364
b186458a 6365 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6366 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6367
d93d1c80
AM
6368 /* If -z nocopyreloc was given, don't generate them either. */
6369 || info->nocopyreloc
a127494f 6370
dce2246a 6371 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6372 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 6373 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 6374
d93d1c80
AM
6375 /* Protected variables do not work with .dynbss. The copy in
6376 .dynbss won't be used by the shared library with the protected
6377 definition for the variable. Text relocations are preferable
6378 to an incorrect program. */
6379 || h->protected_def)
529fe20e 6380 return TRUE;
a127494f 6381
5d35169e 6382 if (h->plt.plist != NULL)
97b639ba
AM
6383 {
6384 /* We should never get here, but unfortunately there are versions
6385 of gcc out there that improperly (for this ABI) put initialized
6386 function pointers, vtable refs and suchlike in read-only
6387 sections. Allow them to proceed, but warn that this might
6388 break at runtime. */
25f53a85 6389 info->callbacks->einfo
c1c8c1ef 6390 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6391 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6392 h->root.root.string);
6393 }
5d35169e
AM
6394
6395 /* This is a reference to a symbol defined by a dynamic object which
6396 is not a function. */
6397
5bd4f169
AM
6398 /* We must allocate the symbol in our .dynbss section, which will
6399 become part of the .bss section of the executable. There will be
6400 an entry for this symbol in the .dynsym section. The dynamic
6401 object will contain position independent code, so all references
6402 from the dynamic object to this symbol will go through the global
6403 offset table. The dynamic linker will use the .dynsym entry to
6404 determine the address it must put in the global offset table, so
6405 both the dynamic object and the regular object will refer to the
6406 same memory location for the variable. */
5474d94f
AM
6407 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6408 {
6409 s = htab->elf.sdynrelro;
6410 srel = htab->elf.sreldynrelro;
6411 }
6412 else
6413 {
6414 s = htab->elf.sdynbss;
6415 srel = htab->elf.srelbss;
6416 }
1d7e9d18 6417 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6418 {
4a7e5234
AM
6419 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6420 linker to copy the initial value out of the dynamic object
6421 and into the runtime process image. */
5474d94f 6422 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6423 h->needs_copy = 1;
5bd4f169
AM
6424 }
6425
529fe20e
AM
6426 /* We no longer want dyn_relocs. */
6427 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 6428 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6429}
6430
e86ce104
AM
6431/* If given a function descriptor symbol, hide both the function code
6432 sym and the descriptor. */
6433static void
4ce794b7
AM
6434ppc64_elf_hide_symbol (struct bfd_link_info *info,
6435 struct elf_link_hash_entry *h,
6436 bfd_boolean force_local)
e86ce104 6437{
34814b9f 6438 struct ppc_link_hash_entry *eh;
e86ce104
AM
6439 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6440
87469ba2
AM
6441 if (ppc_hash_table (info) == NULL)
6442 return;
6443
34814b9f
AM
6444 eh = (struct ppc_link_hash_entry *) h;
6445 if (eh->is_func_descriptor)
e86ce104 6446 {
34814b9f 6447 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6448
721956f4 6449 if (fh == NULL)
d1329ca3
AM
6450 {
6451 const char *p, *q;
b8ac2841 6452 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6453 char save;
6454
6455 /* We aren't supposed to use alloca in BFD because on
6456 systems which do not have alloca the version in libiberty
6457 calls xmalloc, which might cause the program to crash
6458 when it runs out of memory. This function doesn't have a
6459 return status, so there's no way to gracefully return an
6460 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6461 accessed; It's either a string in an ELF string table,
6462 or allocated in an objalloc structure. */
d1329ca3 6463
34814b9f 6464 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6465 save = *p;
6466 *(char *) p = '.';
34814b9f 6467 fh = (struct ppc_link_hash_entry *)
b8ac2841 6468 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6469 *(char *) p = save;
6470
6471 /* Unfortunately, if it so happens that the string we were
6472 looking for was allocated immediately before this string,
6473 then we overwrote the string terminator. That's the only
6474 reason the lookup should fail. */
6475 if (fh == NULL)
6476 {
34814b9f
AM
6477 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6478 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6479 --q, --p;
34814b9f
AM
6480 if (q < eh->elf.root.root.string && *p == '.')
6481 fh = (struct ppc_link_hash_entry *)
b8ac2841 6482 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6483 }
6484 if (fh != NULL)
6485 {
34814b9f
AM
6486 eh->oh = fh;
6487 fh->oh = eh;
d1329ca3
AM
6488 }
6489 }
e86ce104 6490 if (fh != NULL)
34814b9f 6491 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6492 }
6493}
6494
411e1bfb 6495static bfd_boolean
8843416a
AM
6496get_sym_h (struct elf_link_hash_entry **hp,
6497 Elf_Internal_Sym **symp,
6498 asection **symsecp,
f961d9dd 6499 unsigned char **tls_maskp,
8843416a
AM
6500 Elf_Internal_Sym **locsymsp,
6501 unsigned long r_symndx,
6502 bfd *ibfd)
411e1bfb 6503{
0ffa91dd 6504 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6505
6506 if (r_symndx >= symtab_hdr->sh_info)
6507 {
6508 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6509 struct elf_link_hash_entry *h;
6510
6511 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6512 h = elf_follow_link (h);
411e1bfb
AM
6513
6514 if (hp != NULL)
6515 *hp = h;
6516
6517 if (symp != NULL)
6518 *symp = NULL;
6519
6520 if (symsecp != NULL)
6521 {
6522 asection *symsec = NULL;
6523 if (h->root.type == bfd_link_hash_defined
6524 || h->root.type == bfd_link_hash_defweak)
6525 symsec = h->root.u.def.section;
6526 *symsecp = symsec;
6527 }
6528
e7b938ca 6529 if (tls_maskp != NULL)
411e1bfb
AM
6530 {
6531 struct ppc_link_hash_entry *eh;
6532
6533 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6534 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6535 }
6536 }
6537 else
6538 {
6539 Elf_Internal_Sym *sym;
6540 Elf_Internal_Sym *locsyms = *locsymsp;
6541
6542 if (locsyms == NULL)
6543 {
6544 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6545 if (locsyms == NULL)
6546 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6547 symtab_hdr->sh_info,
6548 0, NULL, NULL, NULL);
6549 if (locsyms == NULL)
6550 return FALSE;
6551 *locsymsp = locsyms;
6552 }
6553 sym = locsyms + r_symndx;
6554
6555 if (hp != NULL)
6556 *hp = NULL;
6557
6558 if (symp != NULL)
6559 *symp = sym;
6560
6561 if (symsecp != NULL)
cb33740c 6562 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6563
e7b938ca 6564 if (tls_maskp != NULL)
411e1bfb
AM
6565 {
6566 struct got_entry **lgot_ents;
f961d9dd 6567 unsigned char *tls_mask;
411e1bfb 6568
e7b938ca 6569 tls_mask = NULL;
411e1bfb
AM
6570 lgot_ents = elf_local_got_ents (ibfd);
6571 if (lgot_ents != NULL)
6572 {
e054468f
AM
6573 struct plt_entry **local_plt = (struct plt_entry **)
6574 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6575 unsigned char *lgot_masks = (unsigned char *)
e054468f 6576 (local_plt + symtab_hdr->sh_info);
e7b938ca 6577 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6578 }
e7b938ca 6579 *tls_maskp = tls_mask;
411e1bfb
AM
6580 }
6581 }
6582 return TRUE;
6583}
6584
e7b938ca 6585/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6586 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6587 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6588
6589static int
f961d9dd 6590get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6591 unsigned long *toc_symndx,
6592 bfd_vma *toc_addend,
0d4792f7 6593 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6594 const Elf_Internal_Rela *rel,
6595 bfd *ibfd)
411e1bfb
AM
6596{
6597 unsigned long r_symndx;
0d4792f7 6598 int next_r;
411e1bfb
AM
6599 struct elf_link_hash_entry *h;
6600 Elf_Internal_Sym *sym;
6601 asection *sec;
6602 bfd_vma off;
6603
6604 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6605 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6606 return 0;
411e1bfb 6607
37da22e5
AM
6608 if ((*tls_maskp != NULL
6609 && (**tls_maskp & TLS_TLS) != 0
6610 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 6611 || sec == NULL
6bee8834 6612 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6613 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6614 return 1;
411e1bfb
AM
6615
6616 /* Look inside a TOC section too. */
6617 if (h != NULL)
6618 {
6619 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6620 off = h->root.u.def.value;
6621 }
6622 else
6623 off = sym->st_value;
6624 off += rel->r_addend;
6625 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6626 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6627 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6628 if (toc_symndx != NULL)
6629 *toc_symndx = r_symndx;
3a71aa26
AM
6630 if (toc_addend != NULL)
6631 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6632 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6633 return 0;
854b41e7 6634 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6635 && (next_r == -1 || next_r == -2))
6636 return 1 - next_r;
951fd09b 6637 return 1;
411e1bfb
AM
6638}
6639
3b421ab3
AM
6640/* Find (or create) an entry in the tocsave hash table. */
6641
6642static struct tocsave_entry *
6643tocsave_find (struct ppc_link_hash_table *htab,
6644 enum insert_option insert,
6645 Elf_Internal_Sym **local_syms,
6646 const Elf_Internal_Rela *irela,
6647 bfd *ibfd)
6648{
6649 unsigned long r_indx;
6650 struct elf_link_hash_entry *h;
6651 Elf_Internal_Sym *sym;
6652 struct tocsave_entry ent, *p;
6653 hashval_t hash;
6654 struct tocsave_entry **slot;
6655
6656 r_indx = ELF64_R_SYM (irela->r_info);
6657 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6658 return NULL;
6659 if (ent.sec == NULL || ent.sec->output_section == NULL)
6660 {
4eca0228 6661 _bfd_error_handler
871b3ab2 6662 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
6663 return NULL;
6664 }
6665
6666 if (h != NULL)
6667 ent.offset = h->root.u.def.value;
6668 else
6669 ent.offset = sym->st_value;
6670 ent.offset += irela->r_addend;
6671
6672 hash = tocsave_htab_hash (&ent);
6673 slot = ((struct tocsave_entry **)
6674 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6675 if (slot == NULL)
6676 return NULL;
6677
6678 if (*slot == NULL)
6679 {
6680 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6681 if (p == NULL)
6682 return NULL;
6683 *p = ent;
6684 *slot = p;
6685 }
6686 return *slot;
6687}
6688
754021d0 6689/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6690 code for the old ABI, these will already have been done. */
754021d0
AM
6691
6692static bfd_boolean
6693adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6694{
6695 struct ppc_link_hash_entry *eh;
6696 asection *sym_sec;
74f0fb50 6697 struct _opd_sec_data *opd;
754021d0
AM
6698
6699 if (h->root.type == bfd_link_hash_indirect)
6700 return TRUE;
6701
754021d0
AM
6702 if (h->root.type != bfd_link_hash_defined
6703 && h->root.type != bfd_link_hash_defweak)
6704 return TRUE;
6705
6706 eh = (struct ppc_link_hash_entry *) h;
6707 if (eh->adjust_done)
6708 return TRUE;
6709
6710 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6711 opd = get_opd_info (sym_sec);
6712 if (opd != NULL && opd->adjust != NULL)
754021d0 6713 {
51aecdc5 6714 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
6715 if (adjust == -1)
6716 {
6717 /* This entry has been deleted. */
b3fac117 6718 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6719 if (dsec == NULL)
6720 {
6721 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 6722 if (discarded_section (dsec))
81688140 6723 {
b3fac117 6724 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6725 break;
6726 }
6727 }
4025353c 6728 eh->elf.root.u.def.value = 0;
81688140 6729 eh->elf.root.u.def.section = dsec;
4025353c
AM
6730 }
6731 else
6732 eh->elf.root.u.def.value += adjust;
754021d0
AM
6733 eh->adjust_done = 1;
6734 }
6735 return TRUE;
6736}
6737
8c1d1bb8 6738/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 6739 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
6740 have already been determined. */
6741
6742static bfd_boolean
6743dec_dynrel_count (bfd_vma r_info,
6744 asection *sec,
6745 struct bfd_link_info *info,
6746 Elf_Internal_Sym **local_syms,
6747 struct elf_link_hash_entry *h,
19e08130 6748 Elf_Internal_Sym *sym)
8c1d1bb8
AM
6749{
6750 enum elf_ppc64_reloc_type r_type;
19e08130 6751 asection *sym_sec = NULL;
8c1d1bb8
AM
6752
6753 /* Can this reloc be dynamic? This switch, and later tests here
6754 should be kept in sync with the code in check_relocs. */
6755 r_type = ELF64_R_TYPE (r_info);
6756 switch (r_type)
6757 {
6758 default:
6759 return TRUE;
6760
6761 case R_PPC64_TPREL16:
6762 case R_PPC64_TPREL16_LO:
6763 case R_PPC64_TPREL16_HI:
6764 case R_PPC64_TPREL16_HA:
6765 case R_PPC64_TPREL16_DS:
6766 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
6767 case R_PPC64_TPREL16_HIGH:
6768 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
6769 case R_PPC64_TPREL16_HIGHER:
6770 case R_PPC64_TPREL16_HIGHERA:
6771 case R_PPC64_TPREL16_HIGHEST:
6772 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8
AM
6773 case R_PPC64_TPREL64:
6774 case R_PPC64_DTPMOD64:
6775 case R_PPC64_DTPREL64:
6776 case R_PPC64_ADDR64:
6777 case R_PPC64_REL30:
6778 case R_PPC64_REL32:
6779 case R_PPC64_REL64:
6780 case R_PPC64_ADDR14:
6781 case R_PPC64_ADDR14_BRNTAKEN:
6782 case R_PPC64_ADDR14_BRTAKEN:
6783 case R_PPC64_ADDR16:
6784 case R_PPC64_ADDR16_DS:
6785 case R_PPC64_ADDR16_HA:
6786 case R_PPC64_ADDR16_HI:
f9c6b907
AM
6787 case R_PPC64_ADDR16_HIGH:
6788 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
6789 case R_PPC64_ADDR16_HIGHER:
6790 case R_PPC64_ADDR16_HIGHERA:
6791 case R_PPC64_ADDR16_HIGHEST:
6792 case R_PPC64_ADDR16_HIGHESTA:
6793 case R_PPC64_ADDR16_LO:
6794 case R_PPC64_ADDR16_LO_DS:
6795 case R_PPC64_ADDR24:
6796 case R_PPC64_ADDR32:
6797 case R_PPC64_UADDR16:
6798 case R_PPC64_UADDR32:
6799 case R_PPC64_UADDR64:
6800 case R_PPC64_TOC:
5663e321
AM
6801 case R_PPC64_D34:
6802 case R_PPC64_D34_LO:
6803 case R_PPC64_D34_HI30:
6804 case R_PPC64_D34_HA30:
6805 case R_PPC64_ADDR16_HIGHER34:
6806 case R_PPC64_ADDR16_HIGHERA34:
6807 case R_PPC64_ADDR16_HIGHEST34:
6808 case R_PPC64_ADDR16_HIGHESTA34:
6809 case R_PPC64_D28:
8c1d1bb8
AM
6810 break;
6811 }
6812
6813 if (local_syms != NULL)
6814 {
6815 unsigned long r_symndx;
8c1d1bb8
AM
6816 bfd *ibfd = sec->owner;
6817
6818 r_symndx = ELF64_R_SYM (r_info);
6819 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6820 return FALSE;
6821 }
6822
0e1862bb 6823 if ((bfd_link_pic (info)
1d483afe 6824 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 6825 || (h != NULL
198f1157 6826 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
6827 || h->root.type == bfd_link_hash_defweak
6828 || !h->def_regular))))
6829 || (ELIMINATE_COPY_RELOCS
0e1862bb 6830 && !bfd_link_pic (info)
8c1d1bb8
AM
6831 && h != NULL
6832 && (h->root.type == bfd_link_hash_defweak
6833 || !h->def_regular)))
6834 ;
6835 else
6836 return TRUE;
6837
6838 if (h != NULL)
6edfbbad 6839 {
19e08130
AM
6840 struct elf_dyn_relocs *p;
6841 struct elf_dyn_relocs **pp;
6842 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6843
6844 /* elf_gc_sweep may have already removed all dyn relocs associated
6845 with local syms for a given section. Also, symbol flags are
6846 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6847 report a dynreloc miscount. */
6848 if (*pp == NULL && info->gc_sections)
6849 return TRUE;
6850
6851 while ((p = *pp) != NULL)
60124e18 6852 {
19e08130
AM
6853 if (p->sec == sec)
6854 {
6855 if (!must_be_dyn_reloc (info, r_type))
6856 p->pc_count -= 1;
6857 p->count -= 1;
6858 if (p->count == 0)
6859 *pp = p->next;
6860 return TRUE;
6861 }
6862 pp = &p->next;
60124e18 6863 }
6edfbbad 6864 }
19e08130
AM
6865 else
6866 {
6867 struct ppc_dyn_relocs *p;
6868 struct ppc_dyn_relocs **pp;
6869 void *vpp;
6870 bfd_boolean is_ifunc;
8c1d1bb8 6871
19e08130
AM
6872 if (local_syms == NULL)
6873 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6874 if (sym_sec == NULL)
6875 sym_sec = sec;
c57da1a7 6876
19e08130
AM
6877 vpp = &elf_section_data (sym_sec)->local_dynrel;
6878 pp = (struct ppc_dyn_relocs **) vpp;
6879
6880 if (*pp == NULL && info->gc_sections)
6881 return TRUE;
6882
6883 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6884 while ((p = *pp) != NULL)
8c1d1bb8 6885 {
19e08130
AM
6886 if (p->sec == sec && p->ifunc == is_ifunc)
6887 {
6888 p->count -= 1;
6889 if (p->count == 0)
6890 *pp = p->next;
6891 return TRUE;
6892 }
6893 pp = &p->next;
8c1d1bb8 6894 }
8c1d1bb8
AM
6895 }
6896
695344c0 6897 /* xgettext:c-format */
cf97bcb0
AM
6898 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6899 sec->owner, sec);
8c1d1bb8
AM
6900 bfd_set_error (bfd_error_bad_value);
6901 return FALSE;
6902}
6903
754021d0
AM
6904/* Remove unused Official Procedure Descriptor entries. Currently we
6905 only remove those associated with functions in discarded link-once
6906 sections, or weakly defined functions that have been overridden. It
6907 would be possible to remove many more entries for statically linked
6908 applications. */
6909
b34976b6 6910bfd_boolean
e7d1c40c 6911ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
6912{
6913 bfd *ibfd;
754021d0 6914 bfd_boolean some_edited = FALSE;
3f764659 6915 asection *need_pad = NULL;
e7d1c40c
AM
6916 struct ppc_link_hash_table *htab;
6917
6918 htab = ppc_hash_table (info);
6919 if (htab == NULL)
6920 return FALSE;
1e2f5b6e 6921
c72f2fb2 6922 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
6923 {
6924 asection *sec;
6925 Elf_Internal_Rela *relstart, *rel, *relend;
6926 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 6927 Elf_Internal_Sym *local_syms;
74f0fb50 6928 struct _opd_sec_data *opd;
51aecdc5 6929 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 6930 bfd_size_type cnt_16b = 0;
1e2f5b6e 6931
854b41e7
AM
6932 if (!is_ppc64_elf (ibfd))
6933 continue;
6934
1e2f5b6e 6935 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 6936 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
6937 continue;
6938
dbaa2011 6939 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
6940 continue;
6941
1e2f5b6e
AM
6942 if (sec->output_section == bfd_abs_section_ptr)
6943 continue;
6944
6945 /* Look through the section relocs. */
6946 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6947 continue;
6948
6cdc0ccc 6949 local_syms = NULL;
0ffa91dd 6950 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
6951
6952 /* Read the relocations. */
4ce794b7 6953 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 6954 info->keep_memory);
1e2f5b6e 6955 if (relstart == NULL)
b34976b6 6956 return FALSE;
1e2f5b6e
AM
6957
6958 /* First run through the relocs to check they are sane, and to
6959 determine whether we need to edit this opd section. */
b34976b6 6960 need_edit = FALSE;
51aecdc5 6961 broken = FALSE;
3f764659 6962 need_pad = sec;
1e2f5b6e 6963 relend = relstart + sec->reloc_count;
50bc7936 6964 for (rel = relstart; rel < relend; )
1e2f5b6e 6965 {
04c9666a 6966 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
6967 unsigned long r_symndx;
6968 asection *sym_sec;
6969 struct elf_link_hash_entry *h;
6970 Elf_Internal_Sym *sym;
51aecdc5 6971 bfd_vma offset;
1e2f5b6e 6972
51aecdc5 6973 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
6974 only interested in the reloc pointing to a function entry
6975 point. */
51aecdc5
AM
6976 offset = rel->r_offset;
6977 if (rel + 1 == relend
6978 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
6979 {
6980 /* If someone messes with .opd alignment then after a
6981 "ld -r" we might have padding in the middle of .opd.
6982 Also, there's nothing to prevent someone putting
6983 something silly in .opd with the assembler. No .opd
b34976b6 6984 optimization for them! */
3f764659 6985 broken_opd:
4eca0228 6986 _bfd_error_handler
871b3ab2 6987 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 6988 broken = TRUE;
1e2f5b6e
AM
6989 break;
6990 }
6991
50bc7936
AM
6992 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6993 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6994 {
4eca0228 6995 _bfd_error_handler
695344c0 6996 /* xgettext:c-format */
871b3ab2 6997 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 6998 ibfd, r_type);
51aecdc5 6999 broken = TRUE;
50bc7936
AM
7000 break;
7001 }
7002
1e2f5b6e 7003 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7004 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7005 r_symndx, ibfd))
50bc7936 7006 goto error_ret;
1e2f5b6e
AM
7007
7008 if (sym_sec == NULL || sym_sec->owner == NULL)
7009 {
411e1bfb
AM
7010 const char *sym_name;
7011 if (h != NULL)
7012 sym_name = h->root.root.string;
7013 else
26c61ae5
L
7014 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7015 sym_sec);
411e1bfb 7016
4eca0228 7017 _bfd_error_handler
695344c0 7018 /* xgettext:c-format */
871b3ab2 7019 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7020 ibfd, sym_name);
51aecdc5 7021 broken = TRUE;
1e2f5b6e
AM
7022 break;
7023 }
7024
51020317
AM
7025 /* opd entries are always for functions defined in the
7026 current input bfd. If the symbol isn't defined in the
7027 input bfd, then we won't be using the function in this
7028 bfd; It must be defined in a linkonce section in another
7029 bfd, or is weak. It's also possible that we are
7030 discarding the function due to a linker script /DISCARD/,
7031 which we test for via the output_section. */
7032 if (sym_sec->owner != ibfd
7033 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7034 need_edit = TRUE;
1e2f5b6e 7035
50bc7936 7036 rel += 2;
51aecdc5
AM
7037 if (rel + 1 == relend
7038 || (rel + 2 < relend
7039 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7040 ++rel;
7041
7042 if (rel == relend)
3f764659
JJ
7043 {
7044 if (sec->size == offset + 24)
7045 {
7046 need_pad = NULL;
7047 break;
7048 }
51aecdc5 7049 if (sec->size == offset + 16)
3f764659
JJ
7050 {
7051 cnt_16b++;
7052 break;
7053 }
7054 goto broken_opd;
7055 }
3f764659
JJ
7056 else if (rel + 1 < relend
7057 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7058 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7059 {
51aecdc5
AM
7060 if (rel[0].r_offset == offset + 16)
7061 cnt_16b++;
7062 else if (rel[0].r_offset != offset + 24)
7063 goto broken_opd;
3f764659
JJ
7064 }
7065 else
7066 goto broken_opd;
1e2f5b6e
AM
7067 }
7068
e7d1c40c 7069 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7070
51aecdc5 7071 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7072 {
7073 Elf_Internal_Rela *write_rel;
d4730f92 7074 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7075 bfd_byte *rptr, *wptr;
983bddc8 7076 bfd_byte *new_contents;
74f0fb50
AM
7077 bfd_size_type amt;
7078
983bddc8 7079 new_contents = NULL;
51aecdc5 7080 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7081 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7082 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7083 if (opd->adjust == NULL)
7084 return FALSE;
1e2f5b6e
AM
7085
7086 /* This seems a waste of time as input .opd sections are all
7087 zeros as generated by gcc, but I suppose there's no reason
7088 this will always be so. We might start putting something in
7089 the third word of .opd entries. */
7090 if ((sec->flags & SEC_IN_MEMORY) == 0)
7091 {
eea6121a
AM
7092 bfd_byte *loc;
7093 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7094 {
eea6121a
AM
7095 if (loc != NULL)
7096 free (loc);
50bc7936 7097 error_ret:
6cdc0ccc
AM
7098 if (local_syms != NULL
7099 && symtab_hdr->contents != (unsigned char *) local_syms)
7100 free (local_syms);
6cdc0ccc
AM
7101 if (elf_section_data (sec)->relocs != relstart)
7102 free (relstart);
b34976b6 7103 return FALSE;
6cdc0ccc 7104 }
1e2f5b6e
AM
7105 sec->contents = loc;
7106 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7107 }
7108
7109 elf_section_data (sec)->relocs = relstart;
7110
3f764659 7111 new_contents = sec->contents;
3f764659
JJ
7112 if (add_aux_fields)
7113 {
7114 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7115 if (new_contents == NULL)
7116 return FALSE;
51aecdc5 7117 need_pad = NULL;
3f764659 7118 }
b4f4e59f
AM
7119 wptr = new_contents;
7120 rptr = sec->contents;
1e2f5b6e 7121 write_rel = relstart;
51aecdc5 7122 for (rel = relstart; rel < relend; )
1e2f5b6e 7123 {
50bc7936
AM
7124 unsigned long r_symndx;
7125 asection *sym_sec;
7126 struct elf_link_hash_entry *h;
51aecdc5 7127 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7128 Elf_Internal_Sym *sym;
51aecdc5
AM
7129 long opd_ent_size;
7130 Elf_Internal_Rela *next_rel;
7131 bfd_boolean skip;
50bc7936
AM
7132
7133 r_symndx = ELF64_R_SYM (rel->r_info);
7134 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7135 r_symndx, ibfd))
50bc7936
AM
7136 goto error_ret;
7137
51aecdc5
AM
7138 next_rel = rel + 2;
7139 if (next_rel + 1 == relend
7140 || (next_rel + 2 < relend
7141 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7142 ++next_rel;
7143
7144 /* See if the .opd entry is full 24 byte or
7145 16 byte (with fd_aux entry overlapped with next
7146 fd_func). */
7147 opd_ent_size = 24;
7148 if (next_rel == relend)
1e2f5b6e 7149 {
51aecdc5 7150 if (sec->size == rel->r_offset + 16)
3f764659 7151 opd_ent_size = 16;
51aecdc5
AM
7152 }
7153 else if (next_rel->r_offset == rel->r_offset + 16)
7154 opd_ent_size = 16;
3f764659 7155
51aecdc5
AM
7156 if (h != NULL
7157 && h->root.root.string[0] == '.')
7158 {
8c5b4e52
AM
7159 fdh = ((struct ppc_link_hash_entry *) h)->oh;
7160 if (fdh != NULL)
7161 {
7162 fdh = ppc_follow_link (fdh);
7163 if (fdh->elf.root.type != bfd_link_hash_defined
7164 && fdh->elf.root.type != bfd_link_hash_defweak)
7165 fdh = NULL;
7166 }
51aecdc5 7167 }
1e2f5b6e 7168
51aecdc5
AM
7169 skip = (sym_sec->owner != ibfd
7170 || sym_sec->output_section == bfd_abs_section_ptr);
7171 if (skip)
7172 {
7173 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7174 {
51aecdc5
AM
7175 /* Arrange for the function descriptor sym
7176 to be dropped. */
7177 fdh->elf.root.u.def.value = 0;
7178 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7179 }
51aecdc5 7180 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7181
0e1862bb 7182 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7183 rel = next_rel;
7184 else
7185 while (1)
7186 {
7187 if (!dec_dynrel_count (rel->r_info, sec, info,
7188 NULL, h, sym))
7189 goto error_ret;
754021d0 7190
51aecdc5
AM
7191 if (++rel == next_rel)
7192 break;
1e2f5b6e 7193
51aecdc5
AM
7194 r_symndx = ELF64_R_SYM (rel->r_info);
7195 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7196 r_symndx, ibfd))
7197 goto error_ret;
7198 }
50bc7936
AM
7199 }
7200 else
1e2f5b6e 7201 {
51aecdc5
AM
7202 /* We'll be keeping this opd entry. */
7203 long adjust;
7204
7205 if (fdh != NULL)
7206 {
7207 /* Redefine the function descriptor symbol to
7208 this location in the opd section. It is
7209 necessary to update the value here rather
7210 than using an array of adjustments as we do
7211 for local symbols, because various places
7212 in the generic ELF code use the value
7213 stored in u.def.value. */
7214 fdh->elf.root.u.def.value = wptr - new_contents;
7215 fdh->adjust_done = 1;
7216 }
7217
7218 /* Local syms are a bit tricky. We could
7219 tweak them as they can be cached, but
7220 we'd need to look through the local syms
7221 for the function descriptor sym which we
7222 don't have at the moment. So keep an
7223 array of adjustments. */
7224 adjust = (wptr - new_contents) - (rptr - sec->contents);
7225 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7226
7227 if (wptr != rptr)
7228 memcpy (wptr, rptr, opd_ent_size);
7229 wptr += opd_ent_size;
7230 if (add_aux_fields && opd_ent_size == 16)
7231 {
7232 memset (wptr, '\0', 8);
7233 wptr += 8;
7234 }
7235
50bc7936 7236 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7237 new opd entries. */
7238 for ( ; rel != next_rel; ++rel)
7239 {
7240 rel->r_offset += adjust;
7241 if (write_rel != rel)
7242 memcpy (write_rel, rel, sizeof (*rel));
7243 ++write_rel;
7244 }
1e2f5b6e 7245 }
51aecdc5
AM
7246
7247 rptr += opd_ent_size;
1e2f5b6e
AM
7248 }
7249
3f764659 7250 sec->size = wptr - new_contents;
1e2f5b6e 7251 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7252 if (add_aux_fields)
7253 {
7254 free (sec->contents);
7255 sec->contents = new_contents;
7256 }
7257
05bf9422 7258 /* Fudge the header size too, as this is used later in
cdcf6e38 7259 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7260 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7261 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7262 some_edited = TRUE;
1e2f5b6e 7263 }
6cdc0ccc 7264 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7265 free (relstart);
6cdc0ccc 7266
411e1bfb
AM
7267 if (local_syms != NULL
7268 && symtab_hdr->contents != (unsigned char *) local_syms)
7269 {
7270 if (!info->keep_memory)
7271 free (local_syms);
7272 else
7273 symtab_hdr->contents = (unsigned char *) local_syms;
7274 }
7275 }
7276
754021d0
AM
7277 if (some_edited)
7278 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7279
3f764659
JJ
7280 /* If we are doing a final link and the last .opd entry is just 16 byte
7281 long, add a 8 byte padding after it. */
0e1862bb 7282 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7283 {
7284 bfd_byte *p;
7285
7286 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7287 {
7288 BFD_ASSERT (need_pad->size > 0);
7289
7290 p = bfd_malloc (need_pad->size + 8);
7291 if (p == NULL)
7292 return FALSE;
699733f6 7293
2cdcc330
AM
7294 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7295 p, 0, need_pad->size))
3f764659
JJ
7296 return FALSE;
7297
7298 need_pad->contents = p;
7299 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7300 }
7301 else
7302 {
7303 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7304 if (p == NULL)
7305 return FALSE;
7306
7307 need_pad->contents = p;
7308 }
7309
7310 memset (need_pad->contents + need_pad->size, 0, 8);
7311 need_pad->size += 8;
7312 }
7313
411e1bfb
AM
7314 return TRUE;
7315}
7316
3e04d765
AM
7317/* Analyze inline PLT call relocations to see whether calls to locally
7318 defined functions can be converted to direct calls. */
7319
7320bfd_boolean
7321ppc64_elf_inline_plt (struct bfd_link_info *info)
7322{
7323 struct ppc_link_hash_table *htab;
7324 bfd *ibfd;
7325 asection *sec;
7326 bfd_vma low_vma, high_vma, limit;
7327
7328 htab = ppc_hash_table (info);
7329 if (htab == NULL)
7330 return FALSE;
7331
7332 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7333 reduced somewhat to cater for possible stubs that might be added
7334 between the call and its destination. */
7335 if (htab->params->group_size < 0)
7336 {
7337 limit = -htab->params->group_size;
7338 if (limit == 1)
7339 limit = 0x1e00000;
7340 }
7341 else
7342 {
7343 limit = htab->params->group_size;
7344 if (limit == 1)
7345 limit = 0x1c00000;
7346 }
7347
7348 low_vma = -1;
7349 high_vma = 0;
7350 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7351 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7352 {
7353 if (low_vma > sec->vma)
7354 low_vma = sec->vma;
7355 if (high_vma < sec->vma + sec->size)
7356 high_vma = sec->vma + sec->size;
7357 }
7358
7359 /* If a "bl" can reach anywhere in local code sections, then we can
7360 convert all inline PLT sequences to direct calls when the symbol
7361 is local. */
7362 if (high_vma - low_vma < limit)
7363 {
7364 htab->can_convert_all_inline_plt = 1;
7365 return TRUE;
7366 }
7367
7368 /* Otherwise, go looking through relocs for cases where a direct
7369 call won't reach. Mark the symbol on any such reloc to disable
7370 the optimization and keep the PLT entry as it seems likely that
7371 this will be better than creating trampolines. Note that this
7372 will disable the optimization for all inline PLT calls to a
7373 particular symbol, not just those that won't reach. The
7374 difficulty in doing a more precise optimization is that the
7375 linker needs to make a decision depending on whether a
7376 particular R_PPC64_PLTCALL insn can be turned into a direct
7377 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7378 the sequence, and there is nothing that ties those relocs
7379 together except their symbol. */
7380
7381 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7382 {
7383 Elf_Internal_Shdr *symtab_hdr;
7384 Elf_Internal_Sym *local_syms;
7385
7386 if (!is_ppc64_elf (ibfd))
7387 continue;
7388
7389 local_syms = NULL;
7390 symtab_hdr = &elf_symtab_hdr (ibfd);
7391
7392 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7393 if (ppc64_elf_section_data (sec)->has_pltcall
7394 && !bfd_is_abs_section (sec->output_section))
7395 {
7396 Elf_Internal_Rela *relstart, *rel, *relend;
7397
7398 /* Read the relocations. */
7399 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7400 info->keep_memory);
7401 if (relstart == NULL)
7402 return FALSE;
7403
7404 relend = relstart + sec->reloc_count;
7405 for (rel = relstart; rel < relend; )
7406 {
7407 enum elf_ppc64_reloc_type r_type;
7408 unsigned long r_symndx;
7409 asection *sym_sec;
7410 struct elf_link_hash_entry *h;
7411 Elf_Internal_Sym *sym;
7412 unsigned char *tls_maskp;
7413
7414 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7415 if (r_type != R_PPC64_PLTCALL
7416 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7417 continue;
7418
7419 r_symndx = ELF64_R_SYM (rel->r_info);
7420 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7421 r_symndx, ibfd))
7422 {
7423 if (elf_section_data (sec)->relocs != relstart)
7424 free (relstart);
7425 if (local_syms != NULL
2cdcc330 7426 && symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765
AM
7427 free (local_syms);
7428 return FALSE;
7429 }
7430
7431 if (sym_sec != NULL && sym_sec->output_section != NULL)
7432 {
7433 bfd_vma from, to;
7434 if (h != NULL)
7435 to = h->root.u.def.value;
7436 else
7437 to = sym->st_value;
7438 to += (rel->r_addend
7439 + sym_sec->output_offset
7440 + sym_sec->output_section->vma);
7441 from = (rel->r_offset
7442 + sec->output_offset
7443 + sec->output_section->vma);
5663e321
AM
7444 if (to - from + limit < 2 * limit
7445 && !(r_type == R_PPC64_PLTCALL_NOTOC
7446 && (((h ? h->other : sym->st_other)
7447 & STO_PPC64_LOCAL_MASK)
4a4e7361 7448 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7449 *tls_maskp &= ~PLT_KEEP;
7450 }
7451 }
7452 if (elf_section_data (sec)->relocs != relstart)
7453 free (relstart);
7454 }
7455
7456 if (local_syms != NULL
7457 && symtab_hdr->contents != (unsigned char *) local_syms)
7458 {
7459 if (!info->keep_memory)
7460 free (local_syms);
7461 else
7462 symtab_hdr->contents = (unsigned char *) local_syms;
7463 }
7464 }
7465
7466 return TRUE;
7467}
7468
e1918d23 7469/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7470
e1918d23 7471asection *
e7d1c40c 7472ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7473{
411e1bfb
AM
7474 struct ppc_link_hash_table *htab;
7475
411e1bfb 7476 htab = ppc_hash_table (info);
4dfe6ac6
NC
7477 if (htab == NULL)
7478 return NULL;
7479
ee67d69a
AM
7480 if (abiversion (info->output_bfd) == 1)
7481 htab->opd_abi = 1;
7482
e7d1c40c 7483 if (htab->params->no_multi_toc)
33c0ec9d
AM
7484 htab->do_multi_toc = 0;
7485 else if (!htab->do_multi_toc)
e7d1c40c 7486 htab->params->no_multi_toc = 1;
33c0ec9d 7487
8b5f1ed8
AM
7488 /* Default to --no-plt-localentry, as this option can cause problems
7489 with symbol interposition. For example, glibc libpthread.so and
7490 libc.so duplicate many pthread symbols, with a fallback
7491 implementation in libc.so. In some cases the fallback does more
7492 work than the pthread implementation. __pthread_condattr_destroy
7493 is one such symbol: the libpthread.so implementation is
7494 localentry:0 while the libc.so implementation is localentry:8.
7495 An app that "cleverly" uses dlopen to only load necessary
7496 libraries at runtime may omit loading libpthread.so when not
7497 running multi-threaded, which then results in the libc.so
7498 fallback symbols being used and ld.so complaining. Now there
7499 are workarounds in ld (see non_zero_localentry) to detect the
7500 pthread situation, but that may not be the only case where
7501 --plt-localentry can cause trouble. */
f378ab09 7502 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7503 htab->params->plt_localentry0 = 0;
d44c746a
AM
7504 if (htab->params->plt_localentry0
7505 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7506 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
7507 _bfd_error_handler
7508 (_("warning: --plt-localentry is especially dangerous without "
7509 "ld.so support to detect ABI violations"));
f378ab09 7510
3a71aa26
AM
7511 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7512 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7513 FALSE, FALSE, TRUE));
a7f2871e
AM
7514 /* Move dynamic linking info to the function descriptor sym. */
7515 if (htab->tls_get_addr != NULL)
7516 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7517 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7518 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7519 FALSE, FALSE, TRUE));
7c9cf415 7520 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
7521 {
7522 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7523
7524 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7525 FALSE, FALSE, TRUE);
7526 if (opt != NULL)
7527 func_desc_adjust (opt, info);
7528 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7529 FALSE, FALSE, TRUE);
7530 if (opt_fd != NULL
7531 && (opt_fd->root.type == bfd_link_hash_defined
7532 || opt_fd->root.type == bfd_link_hash_defweak))
7533 {
7534 /* If glibc supports an optimized __tls_get_addr call stub,
7535 signalled by the presence of __tls_get_addr_opt, and we'll
7536 be calling __tls_get_addr via a plt call stub, then
7537 make __tls_get_addr point to __tls_get_addr_opt. */
7538 tga_fd = &htab->tls_get_addr_fd->elf;
7539 if (htab->elf.dynamic_sections_created
7540 && tga_fd != NULL
7541 && (tga_fd->type == STT_FUNC
7542 || tga_fd->needs_plt)
7543 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 7544 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
7545 {
7546 struct plt_entry *ent;
7547
7548 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7549 if (ent->plt.refcount > 0)
7550 break;
7551 if (ent != NULL)
7552 {
7553 tga_fd->root.type = bfd_link_hash_indirect;
7554 tga_fd->root.u.i.link = &opt_fd->root;
7555 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 7556 opt_fd->mark = 1;
a7f2871e
AM
7557 if (opt_fd->dynindx != -1)
7558 {
7559 /* Use __tls_get_addr_opt in dynamic relocations. */
7560 opt_fd->dynindx = -1;
7561 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7562 opt_fd->dynstr_index);
7563 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7564 return NULL;
a7f2871e 7565 }
2cdcc330
AM
7566 htab->tls_get_addr_fd
7567 = (struct ppc_link_hash_entry *) opt_fd;
a7f2871e
AM
7568 tga = &htab->tls_get_addr->elf;
7569 if (opt != NULL && tga != NULL)
7570 {
7571 tga->root.type = bfd_link_hash_indirect;
7572 tga->root.u.i.link = &opt->root;
7573 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 7574 opt->mark = 1;
a7f2871e
AM
7575 _bfd_elf_link_hash_hide_symbol (info, opt,
7576 tga->forced_local);
7577 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7578 }
7579 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7580 htab->tls_get_addr_fd->is_func_descriptor = 1;
7581 if (htab->tls_get_addr != NULL)
7582 {
7583 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7584 htab->tls_get_addr->is_func = 1;
7585 }
7586 }
7587 }
7588 }
7c9cf415
AM
7589 else if (htab->params->tls_get_addr_opt < 0)
7590 htab->params->tls_get_addr_opt = 0;
a7f2871e 7591 }
33c0ec9d 7592 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7593}
8387904d 7594
3a71aa26
AM
7595/* Return TRUE iff REL is a branch reloc with a global symbol matching
7596 HASH1 or HASH2. */
8387904d 7597
3a71aa26
AM
7598static bfd_boolean
7599branch_reloc_hash_match (const bfd *ibfd,
7600 const Elf_Internal_Rela *rel,
7601 const struct ppc_link_hash_entry *hash1,
7602 const struct ppc_link_hash_entry *hash2)
7603{
7604 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7605 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7606 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7607
e054468f 7608 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7609 {
3a71aa26
AM
7610 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7611 struct elf_link_hash_entry *h;
8387904d 7612
3a71aa26 7613 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7614 h = elf_follow_link (h);
3a71aa26
AM
7615 if (h == &hash1->elf || h == &hash2->elf)
7616 return TRUE;
a48ebf4d 7617 }
3a71aa26 7618 return FALSE;
951fd09b 7619}
411e1bfb 7620
951fd09b
AM
7621/* Run through all the TLS relocs looking for optimization
7622 opportunities. The linker has been hacked (see ppc64elf.em) to do
7623 a preliminary section layout so that we know the TLS segment
7624 offsets. We can't optimize earlier because some optimizations need
7625 to know the tp offset, and we need to optimize before allocating
7626 dynamic relocations. */
7627
7628bfd_boolean
33c0ec9d 7629ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7630{
7631 bfd *ibfd;
7632 asection *sec;
7633 struct ppc_link_hash_table *htab;
663a1470 7634 unsigned char *toc_ref;
102890f0 7635 int pass;
951fd09b 7636
3cbc1e5e 7637 if (!bfd_link_executable (info))
411e1bfb
AM
7638 return TRUE;
7639
951fd09b 7640 htab = ppc_hash_table (info);
4dfe6ac6
NC
7641 if (htab == NULL)
7642 return FALSE;
7643
663a1470
AM
7644 /* Make two passes over the relocs. On the first pass, mark toc
7645 entries involved with tls relocs, and check that tls relocs
7646 involved in setting up a tls_get_addr call are indeed followed by
7647 such a call. If they are not, we can't do any tls optimization.
7648 On the second pass twiddle tls_mask flags to notify
7649 relocate_section that optimization can be done, and adjust got
7650 and plt refcounts. */
7651 toc_ref = NULL;
7652 for (pass = 0; pass < 2; ++pass)
c72f2fb2 7653 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
7654 {
7655 Elf_Internal_Sym *locsyms = NULL;
7656 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7657
102890f0
AM
7658 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7659 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7660 {
7661 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7662 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7663
102890f0
AM
7664 /* Read the relocations. */
7665 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7666 info->keep_memory);
7667 if (relstart == NULL)
2915c55b
JK
7668 {
7669 free (toc_ref);
7670 return FALSE;
7671 }
411e1bfb 7672
102890f0
AM
7673 relend = relstart + sec->reloc_count;
7674 for (rel = relstart; rel < relend; rel++)
7675 {
7676 enum elf_ppc64_reloc_type r_type;
7677 unsigned long r_symndx;
7678 struct elf_link_hash_entry *h;
7679 Elf_Internal_Sym *sym;
7680 asection *sym_sec;
f961d9dd
AM
7681 unsigned char *tls_mask;
7682 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
7683 bfd_vma value;
7684 bfd_boolean ok_tprel, is_local;
7685 long toc_ref_index = 0;
7686 int expecting_tls_get_addr = 0;
663a1470 7687 bfd_boolean ret = FALSE;
411e1bfb 7688
102890f0
AM
7689 r_symndx = ELF64_R_SYM (rel->r_info);
7690 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7691 r_symndx, ibfd))
7692 {
7693 err_free_rel:
7694 if (elf_section_data (sec)->relocs != relstart)
7695 free (relstart);
7696 if (toc_ref != NULL)
7697 free (toc_ref);
7698 if (locsyms != NULL
0ffa91dd 7699 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7700 != (unsigned char *) locsyms))
7701 free (locsyms);
663a1470 7702 return ret;
102890f0 7703 }
411e1bfb 7704
102890f0
AM
7705 if (h != NULL)
7706 {
766bc656
AM
7707 if (h->root.type == bfd_link_hash_defined
7708 || h->root.type == bfd_link_hash_defweak)
7709 value = h->root.u.def.value;
7710 else if (h->root.type == bfd_link_hash_undefweak)
7711 value = 0;
7712 else
663a1470
AM
7713 {
7714 found_tls_get_addr_arg = 0;
7715 continue;
7716 }
102890f0
AM
7717 }
7718 else
7719 /* Symbols referenced by TLS relocs must be of type
7720 STT_TLS. So no need for .opd local sym adjust. */
7721 value = sym->st_value;
7722
7723 ok_tprel = FALSE;
7724 is_local = FALSE;
7725 if (h == NULL
7726 || !h->def_dynamic)
7727 {
7728 is_local = TRUE;
766bc656
AM
7729 if (h != NULL
7730 && h->root.type == bfd_link_hash_undefweak)
7731 ok_tprel = TRUE;
c27b8c2a
AM
7732 else if (sym_sec != NULL
7733 && sym_sec->output_section != NULL)
766bc656
AM
7734 {
7735 value += sym_sec->output_offset;
7736 value += sym_sec->output_section->vma;
7737 value -= htab->elf.tls_sec->vma;
7738 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7739 < (bfd_vma) 1 << 32);
7740 }
102890f0 7741 }
951fd09b 7742
102890f0 7743 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7744 /* If this section has old-style __tls_get_addr calls
7745 without marker relocs, then check that each
7746 __tls_get_addr call reloc is preceded by a reloc
7747 that conceivably belongs to the __tls_get_addr arg
7748 setup insn. If we don't find matching arg setup
7749 relocs, don't do any tls optimization. */
7750 if (pass == 0
7751 && sec->has_tls_get_addr_call
7752 && h != NULL
7753 && (h == &htab->tls_get_addr->elf
7754 || h == &htab->tls_get_addr_fd->elf)
7755 && !found_tls_get_addr_arg
7756 && is_branch_reloc (r_type))
7757 {
25f53a85 7758 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
7759 "TLS optimization disabled\n"),
7760 ibfd, sec, rel->r_offset);
7761 ret = TRUE;
7762 goto err_free_rel;
7763 }
7764
7765 found_tls_get_addr_arg = 0;
102890f0
AM
7766 switch (r_type)
7767 {
7768 case R_PPC64_GOT_TLSLD16:
7769 case R_PPC64_GOT_TLSLD16_LO:
7770 expecting_tls_get_addr = 1;
663a1470 7771 found_tls_get_addr_arg = 1;
1a0670f3 7772 /* Fall through. */
102890f0
AM
7773
7774 case R_PPC64_GOT_TLSLD16_HI:
7775 case R_PPC64_GOT_TLSLD16_HA:
7776 /* These relocs should never be against a symbol
7777 defined in a shared lib. Leave them alone if
7778 that turns out to be the case. */
7779 if (!is_local)
7780 continue;
411e1bfb 7781
102890f0 7782 /* LD -> LE */
411e1bfb 7783 tls_set = 0;
102890f0
AM
7784 tls_clear = TLS_LD;
7785 tls_type = TLS_TLS | TLS_LD;
7786 break;
411e1bfb 7787
102890f0
AM
7788 case R_PPC64_GOT_TLSGD16:
7789 case R_PPC64_GOT_TLSGD16_LO:
7790 expecting_tls_get_addr = 1;
663a1470 7791 found_tls_get_addr_arg = 1;
1a0670f3 7792 /* Fall through. */
102890f0
AM
7793
7794 case R_PPC64_GOT_TLSGD16_HI:
7795 case R_PPC64_GOT_TLSGD16_HA:
7796 if (ok_tprel)
7797 /* GD -> LE */
411e1bfb 7798 tls_set = 0;
102890f0
AM
7799 else
7800 /* GD -> IE */
7801 tls_set = TLS_TLS | TLS_TPRELGD;
7802 tls_clear = TLS_GD;
7803 tls_type = TLS_TLS | TLS_GD;
7804 break;
7805
7806 case R_PPC64_GOT_TPREL16_DS:
7807 case R_PPC64_GOT_TPREL16_LO_DS:
7808 case R_PPC64_GOT_TPREL16_HI:
7809 case R_PPC64_GOT_TPREL16_HA:
7810 if (ok_tprel)
7811 {
7812 /* IE -> LE */
7813 tls_set = 0;
7814 tls_clear = TLS_TPREL;
7815 tls_type = TLS_TLS | TLS_TPREL;
7816 break;
7817 }
411e1bfb
AM
7818 continue;
7819
727fc41e
AM
7820 case R_PPC64_TLSGD:
7821 case R_PPC64_TLSLD:
23cedd1d
AM
7822 if (rel + 1 < relend
7823 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7824 {
7825 if (pass != 0
2cdcc330 7826 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
7827 != R_PPC64_PLTSEQ)
7828 && (ELF64_R_TYPE (rel[1].r_info)
7829 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
7830 {
7831 r_symndx = ELF64_R_SYM (rel[1].r_info);
7832 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 7833 r_symndx, ibfd))
23cedd1d
AM
7834 goto err_free_rel;
7835 if (h != NULL)
7836 {
7837 struct plt_entry *ent = NULL;
7838
7839 for (ent = h->plt.plist;
7840 ent != NULL;
7841 ent = ent->next)
7842 if (ent->addend == rel[1].r_addend)
7843 break;
7844
7845 if (ent != NULL
7846 && ent->plt.refcount > 0)
7847 ent->plt.refcount -= 1;
7848 }
7849 }
7850 continue;
7851 }
663a1470 7852 found_tls_get_addr_arg = 1;
1a0670f3 7853 /* Fall through. */
663a1470
AM
7854
7855 case R_PPC64_TLS:
7856 case R_PPC64_TOC16:
7857 case R_PPC64_TOC16_LO:
102890f0
AM
7858 if (sym_sec == NULL || sym_sec != toc)
7859 continue;
7860
7861 /* Mark this toc entry as referenced by a TLS
7862 code sequence. We can do that now in the
7863 case of R_PPC64_TLS, and after checking for
7864 tls_get_addr for the TOC16 relocs. */
7865 if (toc_ref == NULL)
2cdcc330
AM
7866 toc_ref
7867 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
7868 if (toc_ref == NULL)
7869 goto err_free_rel;
7870
102890f0
AM
7871 if (h != NULL)
7872 value = h->root.u.def.value;
7873 else
7874 value = sym->st_value;
7875 value += rel->r_addend;
73242275
AM
7876 if (value % 8 != 0)
7877 continue;
7878 BFD_ASSERT (value < toc->size
7879 && toc->output_offset % 8 == 0);
663a1470 7880 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7881 if (r_type == R_PPC64_TLS
7882 || r_type == R_PPC64_TLSGD
7883 || r_type == R_PPC64_TLSLD)
102890f0
AM
7884 {
7885 toc_ref[toc_ref_index] = 1;
7886 continue;
7887 }
7888
7889 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7890 continue;
7891
7892 tls_set = 0;
7893 tls_clear = 0;
7894 expecting_tls_get_addr = 2;
7895 break;
7896
7897 case R_PPC64_TPREL64:
7898 if (pass == 0
7899 || sec != toc
7900 || toc_ref == NULL
663a1470 7901 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7902 continue;
7903 if (ok_tprel)
7904 {
7905 /* IE -> LE */
7906 tls_set = TLS_EXPLICIT;
7907 tls_clear = TLS_TPREL;
7908 break;
7909 }
7910 continue;
7911
7912 case R_PPC64_DTPMOD64:
7913 if (pass == 0
7914 || sec != toc
7915 || toc_ref == NULL
663a1470 7916 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7917 continue;
7918 if (rel + 1 < relend
7919 && (rel[1].r_info
7920 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7921 && rel[1].r_offset == rel->r_offset + 8)
7922 {
7923 if (ok_tprel)
7924 /* GD -> LE */
7925 tls_set = TLS_EXPLICIT | TLS_GD;
7926 else
7927 /* GD -> IE */
7928 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7929 tls_clear = TLS_GD;
7930 }
7931 else
7932 {
7933 if (!is_local)
7934 continue;
7935
7936 /* LD -> LE */
7937 tls_set = TLS_EXPLICIT;
7938 tls_clear = TLS_LD;
7939 }
7940 break;
7941
7942 default:
7943 continue;
7944 }
7945
7946 if (pass == 0)
7947 {
727fc41e
AM
7948 if (!expecting_tls_get_addr
7949 || !sec->has_tls_get_addr_call)
102890f0
AM
7950 continue;
7951
3a71aa26
AM
7952 if (rel + 1 < relend
7953 && branch_reloc_hash_match (ibfd, rel + 1,
7954 htab->tls_get_addr,
7955 htab->tls_get_addr_fd))
102890f0 7956 {
3a71aa26 7957 if (expecting_tls_get_addr == 2)
102890f0 7958 {
3a71aa26 7959 /* Check for toc tls entries. */
f961d9dd 7960 unsigned char *toc_tls;
3a71aa26
AM
7961 int retval;
7962
7963 retval = get_tls_mask (&toc_tls, NULL, NULL,
7964 &locsyms,
7965 rel, ibfd);
7966 if (retval == 0)
7967 goto err_free_rel;
663a1470
AM
7968 if (toc_tls != NULL)
7969 {
37da22e5
AM
7970 if ((*toc_tls & TLS_TLS) != 0
7971 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
7972 found_tls_get_addr_arg = 1;
7973 if (retval > 1)
7974 toc_ref[toc_ref_index] = 1;
7975 }
102890f0 7976 }
3a71aa26 7977 continue;
102890f0
AM
7978 }
7979
102890f0
AM
7980 /* Uh oh, we didn't find the expected call. We
7981 could just mark this symbol to exclude it
7982 from tls optimization but it's safer to skip
663a1470 7983 the entire optimization. */
695344c0 7984 /* xgettext:c-format */
25f53a85 7985 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
7986 "TLS optimization disabled\n"),
7987 ibfd, sec, rel->r_offset);
7988 ret = TRUE;
7989 goto err_free_rel;
102890f0
AM
7990 }
7991
37da22e5
AM
7992 /* If we don't have old-style __tls_get_addr calls
7993 without TLSGD/TLSLD marker relocs, and we haven't
7994 found a new-style __tls_get_addr call with a
7995 marker for this symbol, then we either have a
7996 broken object file or an -mlongcall style
7997 indirect call to __tls_get_addr without a marker.
7998 Disable optimization in this case. */
7999 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8000 && (tls_set & TLS_EXPLICIT) == 0
8001 && !sec->has_tls_get_addr_call
8002 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8003 != (TLS_TLS | TLS_MARK)))
8004 continue;
8005
23cedd1d 8006 if (expecting_tls_get_addr)
102890f0 8007 {
23cedd1d
AM
8008 struct plt_entry *ent = NULL;
8009
8010 if (htab->tls_get_addr != NULL)
8011 for (ent = htab->tls_get_addr->elf.plt.plist;
8012 ent != NULL;
8013 ent = ent->next)
8014 if (ent->addend == 0)
102890f0 8015 break;
411e1bfb 8016
23cedd1d
AM
8017 if (ent == NULL && htab->tls_get_addr_fd != NULL)
8018 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8019 ent != NULL;
8020 ent = ent->next)
8021 if (ent->addend == 0)
102890f0 8022 break;
23cedd1d
AM
8023
8024 if (ent != NULL
8025 && ent->plt.refcount > 0)
8026 ent->plt.refcount -= 1;
102890f0 8027 }
411e1bfb 8028
102890f0 8029 if (tls_clear == 0)
30038c59
AM
8030 continue;
8031
102890f0
AM
8032 if ((tls_set & TLS_EXPLICIT) == 0)
8033 {
8034 struct got_entry *ent;
411e1bfb 8035
102890f0
AM
8036 /* Adjust got entry for this reloc. */
8037 if (h != NULL)
8038 ent = h->got.glist;
8039 else
8040 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8041
102890f0
AM
8042 for (; ent != NULL; ent = ent->next)
8043 if (ent->addend == rel->r_addend
8044 && ent->owner == ibfd
8045 && ent->tls_type == tls_type)
8046 break;
8047 if (ent == NULL)
8048 abort ();
411e1bfb 8049
102890f0
AM
8050 if (tls_set == 0)
8051 {
8052 /* We managed to get rid of a got entry. */
8053 if (ent->got.refcount > 0)
8054 ent->got.refcount -= 1;
8055 }
8056 }
8057 else
8058 {
8059 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8060 we'll lose one or two dyn relocs. */
8061 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8062 NULL, h, sym))
102890f0 8063 return FALSE;
411e1bfb 8064
102890f0
AM
8065 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8066 {
8067 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8068 NULL, h, sym))
102890f0
AM
8069 return FALSE;
8070 }
8071 }
411e1bfb 8072
102890f0
AM
8073 *tls_mask |= tls_set;
8074 *tls_mask &= ~tls_clear;
8075 }
8c1d1bb8 8076
102890f0
AM
8077 if (elf_section_data (sec)->relocs != relstart)
8078 free (relstart);
8079 }
411e1bfb 8080
663a1470
AM
8081 if (locsyms != NULL
8082 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8083 {
8084 if (!info->keep_memory)
8085 free (locsyms);
8086 else
8087 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8088 }
8089 }
411e1bfb 8090
663a1470
AM
8091 if (toc_ref != NULL)
8092 free (toc_ref);
9a23f96e 8093 htab->do_tls_opt = 1;
b34976b6 8094 return TRUE;
1e2f5b6e 8095}
b34976b6 8096
c5614fa4
AM
8097/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8098 the values of any global symbols in a toc section that has been
8099 edited. Globals in toc sections should be a rarity, so this function
8100 sets a flag if any are found in toc sections other than the one just
de194d85 8101 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8102
8103struct adjust_toc_info
8104{
8105 asection *toc;
8106 unsigned long *skip;
8107 bfd_boolean global_toc_syms;
8108};
8109
ba761f19
AM
8110enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8111
c5614fa4
AM
8112static bfd_boolean
8113adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8114{
8115 struct ppc_link_hash_entry *eh;
8116 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8117 unsigned long i;
c5614fa4 8118
c5614fa4
AM
8119 if (h->root.type != bfd_link_hash_defined
8120 && h->root.type != bfd_link_hash_defweak)
8121 return TRUE;
8122
8123 eh = (struct ppc_link_hash_entry *) h;
8124 if (eh->adjust_done)
8125 return TRUE;
8126
8127 if (eh->elf.root.u.def.section == toc_inf->toc)
8128 {
854b41e7
AM
8129 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8130 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8131 else
854b41e7
AM
8132 i = eh->elf.root.u.def.value >> 3;
8133
ba761f19 8134 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8135 {
4eca0228 8136 _bfd_error_handler
854b41e7
AM
8137 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8138 do
8139 ++i;
ba761f19 8140 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8141 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8142 }
854b41e7
AM
8143
8144 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8145 eh->adjust_done = 1;
8146 }
8147 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8148 toc_inf->global_toc_syms = TRUE;
8149
8150 return TRUE;
8151}
8152
39eeab25
AM
8153/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8154 on a _LO variety toc/got reloc. */
560c8763
AM
8155
8156static bfd_boolean
39eeab25 8157ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8158{
39eeab25
AM
8159 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8160 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
8161 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8162 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8163 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8164 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8165 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8166 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8167 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8168 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8169 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8170 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8171 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8172 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8173 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
8174 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8175 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8176 /* Exclude lfqu by testing reloc. If relocs are ever
8177 defined for the reduced D field in psq_lu then those
8178 will need testing too. */
8179 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8180 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8181 && (insn & 1) == 0)
8182 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8183 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8184 /* Exclude stfqu. psq_stu as above for psq_lu. */
8185 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8186 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8187 && (insn & 1) == 0));
560c8763
AM
8188}
8189
4a421c53
AM
8190/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8191 pld ra,symbol@got@pcrel
8192 load/store rt,0(ra)
8193 or
d4b87b1e 8194 pla ra,symbol@pcrel
4a421c53
AM
8195 load/store rt,0(ra)
8196 may be translated to
8197 pload/pstore rt,symbol@pcrel
8198 nop.
8199 This function returns true if the optimization is possible, placing
8200 the prefix insn in *PINSN1 and a NOP in *PINSN2.
8201
8202 On entry to this function, the linker has already determined that
d4b87b1e 8203 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8204 while *PINSN2 is the second instruction. */
8205
8206static bfd_boolean
8207xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2)
8208{
8209 uint32_t insn2 = *pinsn2 >> 32;
8210 uint64_t i1new;
8211
8212 /* Check that regs match. */
8213 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
8214 return FALSE;
8215
8216 switch ((insn2 >> 26) & 63)
8217 {
8218 default:
8219 return FALSE;
8220
8221 case 32: /* lwz */
8222 case 34: /* lbz */
8223 case 36: /* stw */
8224 case 38: /* stb */
8225 case 40: /* lhz */
8226 case 42: /* lha */
8227 case 44: /* sth */
8228 case 48: /* lfs */
8229 case 50: /* lfd */
8230 case 52: /* stfs */
8231 case 54: /* stfd */
8232 /* These are the PMLS cases, where we just need to tack a prefix
8233 on the insn. Check that the D field is zero. */
8234 if ((insn2 & 0xffff) != 0)
8235 return FALSE;
8236 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8237 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8238 break;
8239
8240 case 58: /* lwa, ld */
8241 if ((insn2 & 0xfffd) != 0)
8242 return FALSE;
8243 i1new = ((1ULL << 58) | (1ULL << 52)
8244 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8245 | (insn2 & (31ULL << 21)));
8246 break;
8247
8248 case 57: /* lxsd, lxssp */
8249 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
8250 return FALSE;
8251 i1new = ((1ULL << 58) | (1ULL << 52)
8252 | ((40ULL | (insn2 & 3)) << 26)
8253 | (insn2 & (31ULL << 21)));
8254 break;
8255
8256 case 61: /* stxsd, stxssp, lxv, stxv */
8257 if ((insn2 & 3) == 0)
8258 return FALSE;
8259 else if ((insn2 & 3) >= 2)
8260 {
8261 if ((insn2 & 0xfffc) != 0)
8262 return FALSE;
8263 i1new = ((1ULL << 58) | (1ULL << 52)
8264 | ((44ULL | (insn2 & 3)) << 26)
8265 | (insn2 & (31ULL << 21)));
8266 }
8267 else
8268 {
8269 if ((insn2 & 0xfff0) != 0)
8270 return FALSE;
8271 i1new = ((1ULL << 58) | (1ULL << 52)
8272 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8273 | (insn2 & (31ULL << 21)));
8274 }
8275 break;
8276
8277 case 56: /* lq */
8278 if ((insn2 & 0xffff) != 0)
8279 return FALSE;
8280 i1new = ((1ULL << 58) | (1ULL << 52)
8281 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8282 break;
8283
8284 case 62: /* std, stq */
8285 if ((insn2 & 0xfffd) != 0)
8286 return FALSE;
8287 i1new = ((1ULL << 58) | (1ULL << 52)
8288 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8289 | (insn2 & (31ULL << 21)));
8290 break;
8291 }
8292
8293 *pinsn1 = i1new;
8294 *pinsn2 = (uint64_t) NOP << 32;
8295 return TRUE;
8296}
8297
c5614fa4
AM
8298/* Examine all relocs referencing .toc sections in order to remove
8299 unused .toc entries. */
8300
8301bfd_boolean
33c0ec9d 8302ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8303{
8304 bfd *ibfd;
8305 struct adjust_toc_info toc_inf;
67f0cbdb 8306 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8307
67f0cbdb 8308 htab->do_toc_opt = 1;
c5614fa4 8309 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8310 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8311 {
8312 asection *toc, *sec;
8313 Elf_Internal_Shdr *symtab_hdr;
8314 Elf_Internal_Sym *local_syms;
425b145b 8315 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8316 unsigned long *skip, *drop;
8317 unsigned char *used;
8318 unsigned char *keep, last, some_unused;
8319
854b41e7
AM
8320 if (!is_ppc64_elf (ibfd))
8321 continue;
8322
c5614fa4
AM
8323 toc = bfd_get_section_by_name (ibfd, ".toc");
8324 if (toc == NULL
92b7a70f 8325 || toc->size == 0
dbaa2011
AM
8326 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8327 || discarded_section (toc))
c5614fa4
AM
8328 continue;
8329
425b145b 8330 toc_relocs = NULL;
c5614fa4 8331 local_syms = NULL;
0ffa91dd 8332 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8333
8334 /* Look at sections dropped from the final link. */
8335 skip = NULL;
8336 relstart = NULL;
8337 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8338 {
8339 if (sec->reloc_count == 0
dbaa2011 8340 || !discarded_section (sec)
c5614fa4
AM
8341 || get_opd_info (sec)
8342 || (sec->flags & SEC_ALLOC) == 0
8343 || (sec->flags & SEC_DEBUGGING) != 0)
8344 continue;
8345
8346 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8347 if (relstart == NULL)
8348 goto error_ret;
8349
8350 /* Run through the relocs to see which toc entries might be
8351 unused. */
8352 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8353 {
8354 enum elf_ppc64_reloc_type r_type;
8355 unsigned long r_symndx;
8356 asection *sym_sec;
8357 struct elf_link_hash_entry *h;
8358 Elf_Internal_Sym *sym;
8359 bfd_vma val;
8360
8361 r_type = ELF64_R_TYPE (rel->r_info);
8362 switch (r_type)
8363 {
8364 default:
8365 continue;
8366
8367 case R_PPC64_TOC16:
8368 case R_PPC64_TOC16_LO:
8369 case R_PPC64_TOC16_HI:
8370 case R_PPC64_TOC16_HA:
8371 case R_PPC64_TOC16_DS:
8372 case R_PPC64_TOC16_LO_DS:
8373 break;
8374 }
8375
8376 r_symndx = ELF64_R_SYM (rel->r_info);
8377 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8378 r_symndx, ibfd))
8379 goto error_ret;
8380
8381 if (sym_sec != toc)
8382 continue;
8383
8384 if (h != NULL)
8385 val = h->root.u.def.value;
8386 else
8387 val = sym->st_value;
8388 val += rel->r_addend;
8389
8390 if (val >= toc->size)
8391 continue;
8392
8393 /* Anything in the toc ought to be aligned to 8 bytes.
8394 If not, don't mark as unused. */
8395 if (val & 7)
8396 continue;
8397
8398 if (skip == NULL)
8399 {
854b41e7 8400 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8401 if (skip == NULL)
8402 goto error_ret;
8403 }
8404
ba761f19 8405 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8406 }
8407
8408 if (elf_section_data (sec)->relocs != relstart)
8409 free (relstart);
8410 }
8411
ba761f19
AM
8412 /* For largetoc loads of address constants, we can convert
8413 . addis rx,2,addr@got@ha
8414 . ld ry,addr@got@l(rx)
8415 to
8416 . addis rx,2,addr@toc@ha
8417 . addi ry,rx,addr@toc@l
8418 when addr is within 2G of the toc pointer. This then means
8419 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8420
ba761f19
AM
8421 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8422 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8423 && toc->reloc_count != 0)
8424 {
8425 /* Read toc relocs. */
425b145b
AM
8426 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8427 info->keep_memory);
8428 if (toc_relocs == NULL)
ba761f19
AM
8429 goto error_ret;
8430
425b145b 8431 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8432 {
8433 enum elf_ppc64_reloc_type r_type;
8434 unsigned long r_symndx;
8435 asection *sym_sec;
8436 struct elf_link_hash_entry *h;
8437 Elf_Internal_Sym *sym;
8438 bfd_vma val, addr;
8439
8440 r_type = ELF64_R_TYPE (rel->r_info);
8441 if (r_type != R_PPC64_ADDR64)
8442 continue;
8443
8444 r_symndx = ELF64_R_SYM (rel->r_info);
8445 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8446 r_symndx, ibfd))
8447 goto error_ret;
8448
425b145b 8449 if (sym_sec == NULL
c27b8c2a 8450 || sym_sec->output_section == NULL
dbaa2011 8451 || discarded_section (sym_sec))
425b145b
AM
8452 continue;
8453
afe397ea 8454 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8455 continue;
8456
8457 if (h != NULL)
bddc25c9
AM
8458 {
8459 if (h->type == STT_GNU_IFUNC)
8460 continue;
8461 val = h->root.u.def.value;
8462 }
ba761f19 8463 else
bddc25c9
AM
8464 {
8465 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8466 continue;
8467 val = sym->st_value;
8468 }
ba761f19
AM
8469 val += rel->r_addend;
8470 val += sym_sec->output_section->vma + sym_sec->output_offset;
8471
8472 /* We don't yet know the exact toc pointer value, but we
8473 know it will be somewhere in the toc section. Don't
8474 optimize if the difference from any possible toc
8475 pointer is outside [ff..f80008000, 7fff7fff]. */
8476 addr = toc->output_section->vma + TOC_BASE_OFF;
8477 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8478 continue;
8479
8480 addr = toc->output_section->vma + toc->output_section->rawsize;
8481 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8482 continue;
8483
8484 if (skip == NULL)
8485 {
8486 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8487 if (skip == NULL)
8488 goto error_ret;
8489 }
8490
8491 skip[rel->r_offset >> 3]
425b145b 8492 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8493 }
ba761f19
AM
8494 }
8495
c5614fa4
AM
8496 if (skip == NULL)
8497 continue;
8498
8499 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8500 if (used == NULL)
8501 {
8502 error_ret:
8503 if (local_syms != NULL
8504 && symtab_hdr->contents != (unsigned char *) local_syms)
8505 free (local_syms);
8506 if (sec != NULL
8507 && relstart != NULL
8508 && elf_section_data (sec)->relocs != relstart)
8509 free (relstart);
425b145b
AM
8510 if (toc_relocs != NULL
8511 && elf_section_data (toc)->relocs != toc_relocs)
8512 free (toc_relocs);
c5614fa4
AM
8513 if (skip != NULL)
8514 free (skip);
8515 return FALSE;
8516 }
8517
30038c59
AM
8518 /* Now check all kept sections that might reference the toc.
8519 Check the toc itself last. */
8520 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8521 : ibfd->sections);
c5614fa4 8522 sec != NULL;
c5614fa4 8523 sec = (sec == toc ? NULL
c5614fa4 8524 : sec->next == NULL ? toc
30038c59 8525 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8526 : sec->next))
8527 {
8528 int repeat;
8529
8530 if (sec->reloc_count == 0
dbaa2011 8531 || discarded_section (sec)
c5614fa4
AM
8532 || get_opd_info (sec)
8533 || (sec->flags & SEC_ALLOC) == 0
8534 || (sec->flags & SEC_DEBUGGING) != 0)
8535 continue;
8536
854b41e7
AM
8537 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8538 info->keep_memory);
c5614fa4 8539 if (relstart == NULL)
2915c55b
JK
8540 {
8541 free (used);
8542 goto error_ret;
8543 }
c5614fa4
AM
8544
8545 /* Mark toc entries referenced as used. */
c5614fa4 8546 do
d4f1ee75
AM
8547 {
8548 repeat = 0;
8549 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8550 {
8551 enum elf_ppc64_reloc_type r_type;
8552 unsigned long r_symndx;
8553 asection *sym_sec;
8554 struct elf_link_hash_entry *h;
8555 Elf_Internal_Sym *sym;
8556 bfd_vma val;
8557 enum {no_check, check_lo, check_ha} insn_check;
98528052 8558
d4f1ee75
AM
8559 r_type = ELF64_R_TYPE (rel->r_info);
8560 switch (r_type)
8561 {
8562 default:
8563 insn_check = no_check;
8564 break;
98528052 8565
d4f1ee75
AM
8566 case R_PPC64_GOT_TLSLD16_HA:
8567 case R_PPC64_GOT_TLSGD16_HA:
8568 case R_PPC64_GOT_TPREL16_HA:
8569 case R_PPC64_GOT_DTPREL16_HA:
8570 case R_PPC64_GOT16_HA:
8571 case R_PPC64_TOC16_HA:
8572 insn_check = check_ha;
8573 break;
98528052 8574
d4f1ee75
AM
8575 case R_PPC64_GOT_TLSLD16_LO:
8576 case R_PPC64_GOT_TLSGD16_LO:
8577 case R_PPC64_GOT_TPREL16_LO_DS:
8578 case R_PPC64_GOT_DTPREL16_LO_DS:
8579 case R_PPC64_GOT16_LO:
8580 case R_PPC64_GOT16_LO_DS:
8581 case R_PPC64_TOC16_LO:
8582 case R_PPC64_TOC16_LO_DS:
8583 insn_check = check_lo;
8584 break;
8585 }
560c8763 8586
d4f1ee75
AM
8587 if (insn_check != no_check)
8588 {
8589 bfd_vma off = rel->r_offset & ~3;
8590 unsigned char buf[4];
8591 unsigned int insn;
c5614fa4 8592
d4f1ee75
AM
8593 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8594 {
8595 free (used);
8596 goto error_ret;
8597 }
8598 insn = bfd_get_32 (ibfd, buf);
8599 if (insn_check == check_lo
39eeab25 8600 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
8601 : ((insn & ((0x3f << 26) | 0x1f << 16))
8602 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8603 {
8604 char str[12];
8605
8606 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8607 sprintf (str, "%#08x", insn);
8608 info->callbacks->einfo
695344c0 8609 /* xgettext:c-format */
174d0a74 8610 (_("%H: toc optimization is not supported for"
cf97bcb0 8611 " %s instruction\n"),
d4f1ee75
AM
8612 ibfd, sec, rel->r_offset & ~3, str);
8613 }
8614 }
c5614fa4 8615
d4f1ee75
AM
8616 switch (r_type)
8617 {
8618 case R_PPC64_TOC16:
8619 case R_PPC64_TOC16_LO:
8620 case R_PPC64_TOC16_HI:
8621 case R_PPC64_TOC16_HA:
8622 case R_PPC64_TOC16_DS:
8623 case R_PPC64_TOC16_LO_DS:
8624 /* In case we're taking addresses of toc entries. */
8625 case R_PPC64_ADDR64:
8626 break;
c5614fa4 8627
d4f1ee75
AM
8628 default:
8629 continue;
8630 }
c5614fa4 8631
d4f1ee75
AM
8632 r_symndx = ELF64_R_SYM (rel->r_info);
8633 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8634 r_symndx, ibfd))
8635 {
8636 free (used);
8637 goto error_ret;
8638 }
c5614fa4 8639
d4f1ee75
AM
8640 if (sym_sec != toc)
8641 continue;
c5614fa4 8642
d4f1ee75
AM
8643 if (h != NULL)
8644 val = h->root.u.def.value;
8645 else
8646 val = sym->st_value;
8647 val += rel->r_addend;
ba761f19 8648
d4f1ee75
AM
8649 if (val >= toc->size)
8650 continue;
ba761f19 8651
d4f1ee75
AM
8652 if ((skip[val >> 3] & can_optimize) != 0)
8653 {
8654 bfd_vma off;
8655 unsigned char opc;
8656
8657 switch (r_type)
8658 {
8659 case R_PPC64_TOC16_HA:
ba761f19 8660 break;
ba761f19 8661
d4f1ee75
AM
8662 case R_PPC64_TOC16_LO_DS:
8663 off = rel->r_offset;
8664 off += (bfd_big_endian (ibfd) ? -2 : 3);
8665 if (!bfd_get_section_contents (ibfd, sec, &opc,
8666 off, 1))
8667 {
8668 free (used);
8669 goto error_ret;
8670 }
8671 if ((opc & (0x3f << 2)) == (58u << 2))
8672 break;
1a0670f3 8673 /* Fall through. */
ba761f19 8674
d4f1ee75
AM
8675 default:
8676 /* Wrong sort of reloc, or not a ld. We may
8677 as well clear ref_from_discarded too. */
8678 skip[val >> 3] = 0;
8679 }
8680 }
8681
8682 if (sec != toc)
8683 used[val >> 3] = 1;
8684 /* For the toc section, we only mark as used if this
8685 entry itself isn't unused. */
8686 else if ((used[rel->r_offset >> 3]
8687 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8688 && !used[val >> 3])
8689 {
8690 /* Do all the relocs again, to catch reference
8691 chains. */
8692 repeat = 1;
8693 used[val >> 3] = 1;
8694 }
8695 }
8696 }
c5614fa4 8697 while (repeat);
854b41e7
AM
8698
8699 if (elf_section_data (sec)->relocs != relstart)
8700 free (relstart);
c5614fa4
AM
8701 }
8702
8703 /* Merge the used and skip arrays. Assume that TOC
8704 doublewords not appearing as either used or unused belong
de194d85 8705 to an entry more than one doubleword in size. */
c5614fa4
AM
8706 for (drop = skip, keep = used, last = 0, some_unused = 0;
8707 drop < skip + (toc->size + 7) / 8;
8708 ++drop, ++keep)
8709 {
8710 if (*keep)
8711 {
ba761f19
AM
8712 *drop &= ~ref_from_discarded;
8713 if ((*drop & can_optimize) != 0)
8714 some_unused = 1;
c5614fa4
AM
8715 last = 0;
8716 }
b140b010 8717 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8718 {
8719 some_unused = 1;
ba761f19 8720 last = ref_from_discarded;
c5614fa4
AM
8721 }
8722 else
8723 *drop = last;
8724 }
8725
8726 free (used);
8727
8728 if (some_unused)
8729 {
8730 bfd_byte *contents, *src;
8731 unsigned long off;
d62b3684 8732 Elf_Internal_Sym *sym;
ba761f19 8733 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8734
8735 /* Shuffle the toc contents, and at the same time convert the
8736 skip array from booleans into offsets. */
8737 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8738 goto error_ret;
8739
8740 elf_section_data (toc)->this_hdr.contents = contents;
8741
8742 for (src = contents, off = 0, drop = skip;
8743 src < contents + toc->size;
8744 src += 8, ++drop)
8745 {
ba761f19
AM
8746 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8747 off += 8;
c5614fa4
AM
8748 else if (off != 0)
8749 {
8750 *drop = off;
8751 memcpy (src - off, src, 8);
8752 }
8753 }
854b41e7 8754 *drop = off;
c5614fa4
AM
8755 toc->rawsize = toc->size;
8756 toc->size = src - contents - off;
8757
ba761f19
AM
8758 /* Adjust addends for relocs against the toc section sym,
8759 and optimize any accesses we can. */
c5614fa4
AM
8760 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8761 {
8762 if (sec->reloc_count == 0
dbaa2011 8763 || discarded_section (sec))
c5614fa4
AM
8764 continue;
8765
8766 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8767 info->keep_memory);
c5614fa4
AM
8768 if (relstart == NULL)
8769 goto error_ret;
8770
8771 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8772 {
8773 enum elf_ppc64_reloc_type r_type;
8774 unsigned long r_symndx;
8775 asection *sym_sec;
8776 struct elf_link_hash_entry *h;
854b41e7 8777 bfd_vma val;
c5614fa4
AM
8778
8779 r_type = ELF64_R_TYPE (rel->r_info);
8780 switch (r_type)
8781 {
8782 default:
8783 continue;
8784
8785 case R_PPC64_TOC16:
8786 case R_PPC64_TOC16_LO:
8787 case R_PPC64_TOC16_HI:
8788 case R_PPC64_TOC16_HA:
8789 case R_PPC64_TOC16_DS:
8790 case R_PPC64_TOC16_LO_DS:
8791 case R_PPC64_ADDR64:
8792 break;
8793 }
8794
8795 r_symndx = ELF64_R_SYM (rel->r_info);
8796 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8797 r_symndx, ibfd))
8798 goto error_ret;
8799
ba761f19 8800 if (sym_sec != toc)
c5614fa4
AM
8801 continue;
8802
ba761f19
AM
8803 if (h != NULL)
8804 val = h->root.u.def.value;
8805 else
8806 {
8807 val = sym->st_value;
8808 if (val != 0)
8809 local_toc_syms = TRUE;
8810 }
8811
8812 val += rel->r_addend;
854b41e7
AM
8813
8814 if (val > toc->rawsize)
8815 val = toc->rawsize;
ba761f19
AM
8816 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8817 continue;
8818 else if ((skip[val >> 3] & can_optimize) != 0)
8819 {
8820 Elf_Internal_Rela *tocrel
425b145b 8821 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8822 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8823
8824 switch (r_type)
8825 {
8826 case R_PPC64_TOC16_HA:
8827 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8828 break;
8829
8830 case R_PPC64_TOC16_LO_DS:
8831 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8832 break;
8833
8834 default:
28942f62
AM
8835 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8836 ppc_howto_init ();
b140b010 8837 info->callbacks->einfo
695344c0 8838 /* xgettext:c-format */
174d0a74 8839 (_("%H: %s references "
b140b010
AM
8840 "optimized away TOC entry\n"),
8841 ibfd, sec, rel->r_offset,
8842 ppc64_elf_howto_table[r_type]->name);
8843 bfd_set_error (bfd_error_bad_value);
8844 goto error_ret;
ba761f19
AM
8845 }
8846 rel->r_addend = tocrel->r_addend;
8847 elf_section_data (sec)->relocs = relstart;
8848 continue;
8849 }
8850
8851 if (h != NULL || sym->st_value != 0)
8852 continue;
854b41e7
AM
8853
8854 rel->r_addend -= skip[val >> 3];
8855 elf_section_data (sec)->relocs = relstart;
c5614fa4 8856 }
854b41e7
AM
8857
8858 if (elf_section_data (sec)->relocs != relstart)
8859 free (relstart);
c5614fa4
AM
8860 }
8861
8862 /* We shouldn't have local or global symbols defined in the TOC,
8863 but handle them anyway. */
df22d223
AM
8864 if (local_syms != NULL)
8865 for (sym = local_syms;
8866 sym < local_syms + symtab_hdr->sh_info;
8867 ++sym)
8868 if (sym->st_value != 0
8869 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8870 {
8871 unsigned long i;
854b41e7 8872
df22d223
AM
8873 if (sym->st_value > toc->rawsize)
8874 i = toc->rawsize >> 3;
8875 else
8876 i = sym->st_value >> 3;
854b41e7 8877
df22d223
AM
8878 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8879 {
8880 if (local_toc_syms)
4eca0228 8881 _bfd_error_handler
df22d223
AM
8882 (_("%s defined on removed toc entry"),
8883 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8884 do
8885 ++i;
8886 while ((skip[i] & (ref_from_discarded | can_optimize)));
8887 sym->st_value = (bfd_vma) i << 3;
8888 }
d62b3684 8889
df22d223
AM
8890 sym->st_value -= skip[i];
8891 symtab_hdr->contents = (unsigned char *) local_syms;
8892 }
c5614fa4 8893
854b41e7 8894 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8895 if (toc_inf.global_toc_syms)
8896 {
8897 toc_inf.toc = toc;
8898 toc_inf.skip = skip;
8899 toc_inf.global_toc_syms = FALSE;
8900 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8901 &toc_inf);
8902 }
854b41e7
AM
8903
8904 if (toc->reloc_count != 0)
8905 {
d4730f92 8906 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8907 Elf_Internal_Rela *wrel;
8908 bfd_size_type sz;
8909
854b41e7 8910 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8911 if (toc_relocs == NULL)
8912 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8913 info->keep_memory);
8914 if (toc_relocs == NULL)
8915 goto error_ret;
8916
425b145b
AM
8917 wrel = toc_relocs;
8918 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8919 if ((skip[rel->r_offset >> 3]
8920 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8921 {
8922 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8923 wrel->r_info = rel->r_info;
8924 wrel->r_addend = rel->r_addend;
8925 ++wrel;
8926 }
8927 else if (!dec_dynrel_count (rel->r_info, toc, info,
8928 &local_syms, NULL, NULL))
8929 goto error_ret;
8930
425b145b
AM
8931 elf_section_data (toc)->relocs = toc_relocs;
8932 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8933 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8934 sz = rel_hdr->sh_entsize;
8935 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8936 }
c5614fa4 8937 }
28be611c
AM
8938 else if (toc_relocs != NULL
8939 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8940 free (toc_relocs);
c5614fa4
AM
8941
8942 if (local_syms != NULL
8943 && symtab_hdr->contents != (unsigned char *) local_syms)
8944 {
8945 if (!info->keep_memory)
8946 free (local_syms);
8947 else
8948 symtab_hdr->contents = (unsigned char *) local_syms;
8949 }
8950 free (skip);
8951 }
8952
066f4018 8953 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
8954 a GOT relative or PC relative access, possibly reducing the
8955 number of GOT entries. */
066f4018
AM
8956 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8957 {
8958 asection *sec;
8959 Elf_Internal_Shdr *symtab_hdr;
8960 Elf_Internal_Sym *local_syms;
8961 Elf_Internal_Rela *relstart, *rel;
8962 bfd_vma got;
8963
8964 if (!is_ppc64_elf (ibfd))
8965 continue;
8966
8967 if (!ppc64_elf_tdata (ibfd)->has_gotrel)
8968 continue;
8969
8970 sec = ppc64_elf_tdata (ibfd)->got;
8971 got = sec->output_section->vma + sec->output_offset + 0x8000;
8972
8973 local_syms = NULL;
8974 symtab_hdr = &elf_symtab_hdr (ibfd);
8975
8976 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8977 {
8978 if (sec->reloc_count == 0
8979 || !ppc64_elf_section_data (sec)->has_gotrel
8980 || discarded_section (sec))
8981 continue;
8982
8983 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8984 info->keep_memory);
8985 if (relstart == NULL)
8986 {
8987 got_error_ret:
8988 if (local_syms != NULL
8989 && symtab_hdr->contents != (unsigned char *) local_syms)
8990 free (local_syms);
8991 if (sec != NULL
8992 && relstart != NULL
8993 && elf_section_data (sec)->relocs != relstart)
8994 free (relstart);
8995 return FALSE;
8996 }
8997
8998 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8999 {
9000 enum elf_ppc64_reloc_type r_type;
9001 unsigned long r_symndx;
9002 Elf_Internal_Sym *sym;
9003 asection *sym_sec;
9004 struct elf_link_hash_entry *h;
9005 struct got_entry *ent;
4a421c53
AM
9006 bfd_vma sym_addend, val, pc;
9007 unsigned char buf[8];
066f4018
AM
9008 unsigned int insn;
9009
9010 r_type = ELF64_R_TYPE (rel->r_info);
9011 switch (r_type)
9012 {
bb22a418
AM
9013 /* Note that we don't delete GOT entries for
9014 R_PPC64_GOT16_DS since we'd need a lot more
9015 analysis. For starters, the preliminary layout is
9016 before the GOT, PLT, dynamic sections and stubs are
9017 laid out. Then we'd need to allow for changes in
9018 distance between sections caused by alignment. */
066f4018
AM
9019 default:
9020 continue;
9021
066f4018
AM
9022 case R_PPC64_GOT16_HA:
9023 case R_PPC64_GOT16_LO_DS:
4a421c53
AM
9024 sym_addend = rel->r_addend;
9025 break;
9026
9027 case R_PPC64_GOT_PCREL34:
9028 sym_addend = 0;
066f4018
AM
9029 break;
9030 }
9031
9032 r_symndx = ELF64_R_SYM (rel->r_info);
9033 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9034 r_symndx, ibfd))
9035 goto got_error_ret;
9036
9037 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9038 continue;
9039
9040 if (h != NULL)
9041 val = h->root.u.def.value;
9042 else
9043 val = sym->st_value;
4a421c53 9044 val += sym_addend;
066f4018
AM
9045 val += sym_sec->output_section->vma + sym_sec->output_offset;
9046
bb22a418
AM
9047/* Fudge factor to allow for the fact that the preliminary layout
9048 isn't exact. Reduce limits by this factor. */
9049#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9050
066f4018
AM
9051 switch (r_type)
9052 {
9053 default:
9054 continue;
9055
066f4018 9056 case R_PPC64_GOT16_HA:
bb22a418
AM
9057 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9058 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9059 continue;
9060
9061 if (!bfd_get_section_contents (ibfd, sec, buf,
9062 rel->r_offset & ~3, 4))
9063 goto got_error_ret;
9064 insn = bfd_get_32 (ibfd, buf);
9065 if (((insn & ((0x3f << 26) | 0x1f << 16))
9066 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9067 continue;
9068 break;
9069
9070 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9071 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9072 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9073 continue;
9074 if (!bfd_get_section_contents (ibfd, sec, buf,
9075 rel->r_offset & ~3, 4))
9076 goto got_error_ret;
9077 insn = bfd_get_32 (ibfd, buf);
9078 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9079 continue;
9080 break;
4a421c53
AM
9081
9082 case R_PPC64_GOT_PCREL34:
9083 pc = rel->r_offset;
9084 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9085 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9086 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9087 continue;
9088 if (!bfd_get_section_contents (ibfd, sec, buf,
9089 rel->r_offset & ~3, 8))
9090 goto got_error_ret;
9091 insn = bfd_get_32 (ibfd, buf);
9092 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9093 continue;
9094 insn = bfd_get_32 (ibfd, buf + 4);
9095 if ((insn & (0x3f << 26)) != 57u << 26)
9096 continue;
9097 break;
066f4018 9098 }
bb22a418 9099#undef LIMIT_ADJUST
066f4018
AM
9100
9101 if (h != NULL)
9102 ent = h->got.glist;
9103 else
9104 {
9105 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9106 ent = local_got_ents[r_symndx];
9107 }
9108 for (; ent != NULL; ent = ent->next)
4a421c53 9109 if (ent->addend == sym_addend
066f4018
AM
9110 && ent->owner == ibfd
9111 && ent->tls_type == 0)
9112 break;
9113 BFD_ASSERT (ent && ent->got.refcount > 0);
9114 ent->got.refcount -= 1;
9115 }
9116
9117 if (elf_section_data (sec)->relocs != relstart)
9118 free (relstart);
9119 }
9120
9121 if (local_syms != NULL
9122 && symtab_hdr->contents != (unsigned char *) local_syms)
9123 {
9124 if (!info->keep_memory)
9125 free (local_syms);
9126 else
9127 symtab_hdr->contents = (unsigned char *) local_syms;
9128 }
9129 }
9130
c5614fa4
AM
9131 return TRUE;
9132}
9133
1bbe0902
AM
9134/* Return true iff input section I references the TOC using
9135 instructions limited to +/-32k offsets. */
9136
9137bfd_boolean
9138ppc64_elf_has_small_toc_reloc (asection *i)
9139{
9140 return (is_ppc64_elf (i->owner)
9141 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9142}
9143
927be08e
AM
9144/* Allocate space for one GOT entry. */
9145
9146static void
9147allocate_got (struct elf_link_hash_entry *h,
9148 struct bfd_link_info *info,
9149 struct got_entry *gent)
9150{
9151 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9152 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9153 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9154 ? 16 : 8);
9155 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9156 ? 2 : 1) * sizeof (Elf64_External_Rela);
9157 asection *got = ppc64_elf_tdata (gent->owner)->got;
9158
9159 gent->got.offset = got->size;
9160 got->size += entsize;
9161
19e08130 9162 if (h->type == STT_GNU_IFUNC)
927be08e 9163 {
33e44f2e 9164 htab->elf.irelplt->size += rentsize;
19e08130 9165 htab->got_reli_size += rentsize;
927be08e 9166 }
f15d0b54
AM
9167 else if (((bfd_link_pic (info)
9168 && !((gent->tls_type & TLS_TPREL) != 0
9169 && bfd_link_executable (info)
9170 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9171 || (htab->elf.dynamic_sections_created
9172 && h->dynindx != -1
9173 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9174 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9175 {
19e08130 9176 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9177 relgot->size += rentsize;
927be08e
AM
9178 }
9179}
9180
7865406b
AM
9181/* This function merges got entries in the same toc group. */
9182
9183static void
9184merge_got_entries (struct got_entry **pent)
9185{
9186 struct got_entry *ent, *ent2;
9187
9188 for (ent = *pent; ent != NULL; ent = ent->next)
9189 if (!ent->is_indirect)
9190 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9191 if (!ent2->is_indirect
9192 && ent2->addend == ent->addend
9193 && ent2->tls_type == ent->tls_type
9194 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9195 {
9196 ent2->is_indirect = TRUE;
9197 ent2->got.ent = ent;
9198 }
9199}
9200
46434633 9201/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9202
9203static bfd_boolean
46434633
AM
9204ensure_undef_dynamic (struct bfd_link_info *info,
9205 struct elf_link_hash_entry *h)
f0158f44
AM
9206{
9207 struct elf_link_hash_table *htab = elf_hash_table (info);
9208
9209 if (htab->dynamic_sections_created
46434633
AM
9210 && ((info->dynamic_undefined_weak != 0
9211 && h->root.type == bfd_link_hash_undefweak)
9212 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9213 && h->dynindx == -1
9214 && !h->forced_local
9215 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9216 return bfd_elf_link_record_dynamic_symbol (info, h);
9217 return TRUE;
9218}
9219
65f38f15
AM
9220/* Allocate space in .plt, .got and associated reloc sections for
9221 dynamic relocs. */
5bd4f169 9222
b34976b6 9223static bfd_boolean
4ce794b7 9224allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9225{
65f38f15
AM
9226 struct bfd_link_info *info;
9227 struct ppc_link_hash_table *htab;
5bd4f169 9228 asection *s;
65f38f15 9229 struct ppc_link_hash_entry *eh;
0b8bcf0d 9230 struct got_entry **pgent, *gent;
5bd4f169 9231
e92d460e 9232 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9233 return TRUE;
5bd4f169 9234
65f38f15
AM
9235 info = (struct bfd_link_info *) inf;
9236 htab = ppc_hash_table (info);
4dfe6ac6
NC
9237 if (htab == NULL)
9238 return FALSE;
5bd4f169 9239
951fd09b
AM
9240 eh = (struct ppc_link_hash_entry *) h;
9241 /* Run through the TLS GD got entries first if we're changing them
9242 to TPREL. */
37da22e5 9243 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
951fd09b
AM
9244 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9245 if (gent->got.refcount > 0
9246 && (gent->tls_type & TLS_GD) != 0)
9247 {
9248 /* This was a GD entry that has been converted to TPREL. If
9249 there happens to be a TPREL entry we can use that one. */
9250 struct got_entry *ent;
9251 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9252 if (ent->got.refcount > 0
9253 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9254 && ent->addend == gent->addend
9255 && ent->owner == gent->owner)
951fd09b
AM
9256 {
9257 gent->got.refcount = 0;
9258 break;
9259 }
9260
9261 /* If not, then we'll be using our own TPREL entry. */
9262 if (gent->got.refcount != 0)
9263 gent->tls_type = TLS_TLS | TLS_TPREL;
9264 }
9265
7865406b
AM
9266 /* Remove any list entry that won't generate a word in the GOT before
9267 we call merge_got_entries. Otherwise we risk merging to empty
9268 entries. */
0b8bcf0d
AM
9269 pgent = &h->got.glist;
9270 while ((gent = *pgent) != NULL)
411e1bfb 9271 if (gent->got.refcount > 0)
7865406b
AM
9272 {
9273 if ((gent->tls_type & TLS_LD) != 0
9274 && !h->def_dynamic)
9275 {
9276 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9277 *pgent = gent->next;
9278 }
9279 else
9280 pgent = &gent->next;
9281 }
9282 else
9283 *pgent = gent->next;
9284
9285 if (!htab->do_multi_toc)
9286 merge_got_entries (&h->got.glist);
9287
9288 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9289 if (!gent->is_indirect)
411e1bfb 9290 {
46434633
AM
9291 /* Make sure this symbol is output as a dynamic symbol. */
9292 if (!ensure_undef_dynamic (info, h))
f0158f44 9293 return FALSE;
65f38f15 9294
0c8d6e5c 9295 if (!is_ppc64_elf (gent->owner))
927be08e 9296 abort ();
0ffa91dd 9297
927be08e 9298 allocate_got (h, info, gent);
411e1bfb 9299 }
65f38f15 9300
954b63d4
AM
9301 /* If no dynamic sections we can't have dynamic relocs, except for
9302 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9303 if (!htab->elf.dynamic_sections_created
9304 && h->type != STT_GNU_IFUNC)
9305 eh->dyn_relocs = NULL;
9306
529fe20e
AM
9307 /* Discard relocs on undefined symbols that must be local. */
9308 else if (h->root.type == bfd_link_hash_undefined
9309 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9310 eh->dyn_relocs = NULL;
9311
954b63d4
AM
9312 /* Also discard relocs on undefined weak syms with non-default
9313 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9314 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
9315 eh->dyn_relocs = NULL;
9316
8a2058b5 9317 if (eh->dyn_relocs != NULL)
65f38f15 9318 {
8a2058b5
AM
9319 struct elf_dyn_relocs *p, **pp;
9320
57e7d118
AM
9321 /* In the shared -Bsymbolic case, discard space allocated for
9322 dynamic pc-relative relocs against symbols which turn out to
9323 be defined in regular objects. For the normal shared case,
9324 discard space for relocs that have become local due to symbol
9325 visibility changes. */
9326
9327 if (bfd_link_pic (info))
65f38f15 9328 {
57e7d118
AM
9329 /* Relocs that use pc_count are those that appear on a call
9330 insn, or certain REL relocs (see must_be_dyn_reloc) that
9331 can be generated via assembly. We want calls to
9332 protected symbols to resolve directly to the function
9333 rather than going via the plt. If people want function
9334 pointer comparisons to work as expected then they should
9335 avoid writing weird assembly. */
9336 if (SYMBOL_CALLS_LOCAL (info, h))
9337 {
57e7d118
AM
9338 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9339 {
9340 p->count -= p->pc_count;
9341 p->pc_count = 0;
9342 if (p->count == 0)
9343 *pp = p->next;
9344 else
9345 pp = &p->next;
9346 }
9347 }
65f38f15 9348
954b63d4 9349 if (eh->dyn_relocs != NULL)
5bd4f169 9350 {
46434633
AM
9351 /* Make sure this symbol is output as a dynamic symbol. */
9352 if (!ensure_undef_dynamic (info, h))
f0158f44 9353 return FALSE;
5bd4f169 9354 }
65f38f15 9355 }
529fe20e 9356 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 9357 {
8a2058b5 9358 /* For the non-pic case, discard space for relocs against
57e7d118
AM
9359 symbols which turn out to need copy relocs or are not
9360 dynamic. */
529fe20e
AM
9361 if (h->dynamic_adjusted
9362 && !h->def_regular
9363 && !ELF_COMMON_DEF_P (h))
f0158f44 9364 {
46434633
AM
9365 /* Make sure this symbol is output as a dynamic symbol. */
9366 if (!ensure_undef_dynamic (info, h))
f0158f44 9367 return FALSE;
dfbb6ac9 9368
f0158f44
AM
9369 if (h->dynindx == -1)
9370 eh->dyn_relocs = NULL;
9371 }
9372 else
8a2058b5 9373 eh->dyn_relocs = NULL;
57e7d118
AM
9374 }
9375
9376 /* Finally, allocate space. */
9377 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9378 {
9379 asection *sreloc = elf_section_data (p->sec)->sreloc;
9380 if (eh->elf.type == STT_GNU_IFUNC)
9381 sreloc = htab->elf.irelplt;
9382 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9383 }
65f38f15 9384 }
57e7d118 9385
2d7ad24e
AM
9386 /* We might need a PLT entry when the symbol
9387 a) is dynamic, or
9388 b) is an ifunc, or
9389 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9390 d) has plt16 relocs and we are linking statically. */
9391 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9392 || h->type == STT_GNU_IFUNC
9393 || (h->needs_plt && h->dynamic_adjusted)
9394 || (h->needs_plt
9395 && h->def_regular
9396 && !htab->elf.dynamic_sections_created
3e04d765 9397 && !htab->can_convert_all_inline_plt
2d7ad24e
AM
9398 && (((struct ppc_link_hash_entry *) h)->tls_mask
9399 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 9400 {
57e7d118
AM
9401 struct plt_entry *pent;
9402 bfd_boolean doneone = FALSE;
9403 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9404 if (pent->plt.refcount > 0)
9405 {
9406 if (!htab->elf.dynamic_sections_created
9407 || h->dynindx == -1)
9408 {
2d7ad24e
AM
9409 if (h->type == STT_GNU_IFUNC)
9410 {
9411 s = htab->elf.iplt;
9412 pent->plt.offset = s->size;
9413 s->size += PLT_ENTRY_SIZE (htab);
9414 s = htab->elf.irelplt;
9415 }
9416 else
9417 {
9418 s = htab->pltlocal;
9419 pent->plt.offset = s->size;
9420 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9421 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9422 }
57e7d118
AM
9423 }
9424 else
9425 {
9426 /* If this is the first .plt entry, make room for the special
9427 first entry. */
9428 s = htab->elf.splt;
9429 if (s->size == 0)
9430 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9431
57e7d118 9432 pent->plt.offset = s->size;
65f38f15 9433
57e7d118
AM
9434 /* Make room for this entry. */
9435 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9436
57e7d118
AM
9437 /* Make room for the .glink code. */
9438 s = htab->glink;
9439 if (s->size == 0)
9e390558 9440 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9441 if (htab->opd_abi)
9442 {
9443 /* We need bigger stubs past index 32767. */
9e390558 9444 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9445 s->size += 4;
9446 s->size += 2*4;
9447 }
9448 else
9449 s->size += 4;
65f38f15 9450
57e7d118
AM
9451 /* We also need to make an entry in the .rela.plt section. */
9452 s = htab->elf.srelplt;
9453 }
2d7ad24e
AM
9454 if (s != NULL)
9455 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
9456 doneone = TRUE;
9457 }
9458 else
9459 pent->plt.offset = (bfd_vma) -1;
9460 if (!doneone)
9461 {
9462 h->plt.plist = NULL;
9463 h->needs_plt = 0;
9464 }
65f38f15 9465 }
57e7d118 9466 else
65f38f15 9467 {
57e7d118
AM
9468 h->plt.plist = NULL;
9469 h->needs_plt = 0;
65f38f15
AM
9470 }
9471
b34976b6 9472 return TRUE;
65f38f15
AM
9473}
9474
9e390558
AM
9475#define PPC_LO(v) ((v) & 0xffff)
9476#define PPC_HI(v) (((v) >> 16) & 0xffff)
9477#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
9478#define D34(v) \
9479 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9480#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 9481
a345bc8d
AM
9482/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9483 to set up space for global entry stubs. These are put in glink,
9484 after the branch table. */
65f38f15 9485
b34976b6 9486static bfd_boolean
a345bc8d 9487size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9488{
a345bc8d
AM
9489 struct bfd_link_info *info;
9490 struct ppc_link_hash_table *htab;
9491 struct plt_entry *pent;
9e390558 9492 asection *s, *plt;
65f38f15 9493
a345bc8d
AM
9494 if (h->root.type == bfd_link_hash_indirect)
9495 return TRUE;
65f38f15 9496
a345bc8d
AM
9497 if (!h->pointer_equality_needed)
9498 return TRUE;
65f38f15 9499
a345bc8d
AM
9500 if (h->def_regular)
9501 return TRUE;
65f38f15 9502
a345bc8d
AM
9503 info = inf;
9504 htab = ppc_hash_table (info);
9505 if (htab == NULL)
9506 return FALSE;
9507
9e390558
AM
9508 s = htab->global_entry;
9509 plt = htab->elf.splt;
a345bc8d
AM
9510 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9511 if (pent->plt.offset != (bfd_vma) -1
9512 && pent->addend == 0)
9513 {
afe397ea
AM
9514 /* For ELFv2, if this symbol is not defined in a regular file
9515 and we are not generating a shared library or pie, then we
9516 need to define the symbol in the executable on a call stub.
9517 This is to avoid text relocations. */
9e390558
AM
9518 bfd_vma off, stub_align, stub_off, stub_size;
9519 unsigned int align_power;
9520
9521 stub_size = 16;
9522 stub_off = s->size;
9523 if (htab->params->plt_stub_align >= 0)
9524 align_power = htab->params->plt_stub_align;
9525 else
9526 align_power = -htab->params->plt_stub_align;
9527 /* Setting section alignment is delayed until we know it is
9528 non-empty. Otherwise the .text output section will be
9529 aligned at least to plt_stub_align even when no global
9530 entry stubs are needed. */
9531 if (s->alignment_power < align_power)
9532 s->alignment_power = align_power;
9533 stub_align = (bfd_vma) 1 << align_power;
9534 if (htab->params->plt_stub_align >= 0
9535 || ((((stub_off + stub_size - 1) & -stub_align)
9536 - (stub_off & -stub_align))
9537 > ((stub_size - 1) & -stub_align)))
9538 stub_off = (stub_off + stub_align - 1) & -stub_align;
9539 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9540 off -= stub_off + s->output_offset + s->output_section->vma;
9541 /* Note that for --plt-stub-align negative we have a possible
9542 dependency between stub offset and size. Break that
9543 dependency by assuming the max stub size when calculating
9544 the stub offset. */
9545 if (PPC_HA (off) == 0)
9546 stub_size -= 4;
8a2058b5 9547 h->root.type = bfd_link_hash_defined;
afe397ea 9548 h->root.u.def.section = s;
9e390558
AM
9549 h->root.u.def.value = stub_off;
9550 s->size = stub_off + stub_size;
a345bc8d
AM
9551 break;
9552 }
9553 return TRUE;
9554}
9555
9556/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9557 read-only sections. */
9558
9559static bfd_boolean
98bbb1b8 9560maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 9561{
98bbb1b8
AM
9562 asection *sec;
9563
a345bc8d
AM
9564 if (h->root.type == bfd_link_hash_indirect)
9565 return TRUE;
9566
98bbb1b8
AM
9567 sec = readonly_dynrelocs (h);
9568 if (sec != NULL)
a345bc8d 9569 {
98bbb1b8
AM
9570 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9571
9572 info->flags |= DF_TEXTREL;
2cdcc330
AM
9573 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9574 " in read-only section `%pA'\n"),
9575 sec->owner, h->root.root.string, sec);
a345bc8d
AM
9576
9577 /* Not an error, just cut short the traversal. */
9578 return FALSE;
65f38f15 9579 }
b34976b6 9580 return TRUE;
65f38f15
AM
9581}
9582
9583/* Set the sizes of the dynamic sections. */
9584
b34976b6 9585static bfd_boolean
ee67d69a 9586ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9587 struct bfd_link_info *info)
65f38f15
AM
9588{
9589 struct ppc_link_hash_table *htab;
9590 bfd *dynobj;
9591 asection *s;
b34976b6 9592 bfd_boolean relocs;
65f38f15 9593 bfd *ibfd;
7865406b 9594 struct got_entry *first_tlsld;
65f38f15
AM
9595
9596 htab = ppc_hash_table (info);
4dfe6ac6
NC
9597 if (htab == NULL)
9598 return FALSE;
9599
65f38f15
AM
9600 dynobj = htab->elf.dynobj;
9601 if (dynobj == NULL)
9602 abort ();
9603
9604 if (htab->elf.dynamic_sections_created)
9605 {
9606 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9607 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9608 {
3d4d4302 9609 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9610 if (s == NULL)
9611 abort ();
eea6121a 9612 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9613 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9614 }
9615 }
9616
9617 /* Set up .got offsets for local syms, and space for local dynamic
9618 relocs. */
c72f2fb2 9619 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9620 {
411e1bfb
AM
9621 struct got_entry **lgot_ents;
9622 struct got_entry **end_lgot_ents;
e054468f
AM
9623 struct plt_entry **local_plt;
9624 struct plt_entry **end_local_plt;
f961d9dd 9625 unsigned char *lgot_masks;
65f38f15
AM
9626 bfd_size_type locsymcount;
9627 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9628
0c8d6e5c 9629 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9630 continue;
9631
9632 for (s = ibfd->sections; s != NULL; s = s->next)
9633 {
19e08130 9634 struct ppc_dyn_relocs *p;
65f38f15 9635
6edfbbad 9636 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9637 {
ec338859
AM
9638 if (!bfd_is_abs_section (p->sec)
9639 && bfd_is_abs_section (p->sec->output_section))
9640 {
9641 /* Input section has been discarded, either because
9642 it is a copy of a linkonce section or due to
9643 linker script /DISCARD/, so we'll be discarding
9644 the relocs too. */
9645 }
248866a8 9646 else if (p->count != 0)
ec338859 9647 {
19e08130
AM
9648 asection *srel = elf_section_data (p->sec)->sreloc;
9649 if (p->ifunc)
33e44f2e 9650 srel = htab->elf.irelplt;
eea6121a 9651 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9652 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9653 info->flags |= DF_TEXTREL;
ec338859 9654 }
65f38f15
AM
9655 }
9656 }
9657
411e1bfb
AM
9658 lgot_ents = elf_local_got_ents (ibfd);
9659 if (!lgot_ents)
65f38f15
AM
9660 continue;
9661
0ffa91dd 9662 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9663 locsymcount = symtab_hdr->sh_info;
411e1bfb 9664 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9665 local_plt = (struct plt_entry **) end_lgot_ents;
9666 end_local_plt = local_plt + locsymcount;
f961d9dd 9667 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9668 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9669 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9670 {
0b8bcf0d 9671 struct got_entry **pent, *ent;
411e1bfb 9672
0b8bcf0d
AM
9673 pent = lgot_ents;
9674 while ((ent = *pent) != NULL)
411e1bfb
AM
9675 if (ent->got.refcount > 0)
9676 {
e7b938ca 9677 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9678 {
927be08e 9679 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9680 *pent = ent->next;
411e1bfb
AM
9681 }
9682 else
9683 {
19e08130
AM
9684 unsigned int ent_size = 8;
9685 unsigned int rel_size = sizeof (Elf64_External_Rela);
9686
eea6121a 9687 ent->got.offset = s->size;
e7b938ca 9688 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9689 {
19e08130
AM
9690 ent_size *= 2;
9691 rel_size *= 2;
9692 }
9693 s->size += ent_size;
37da22e5 9694 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 9695 {
33e44f2e 9696 htab->elf.irelplt->size += rel_size;
19e08130
AM
9697 htab->got_reli_size += rel_size;
9698 }
f15d0b54
AM
9699 else if (bfd_link_pic (info)
9700 && !((ent->tls_type & TLS_TPREL) != 0
9701 && bfd_link_executable (info)))
19e08130
AM
9702 {
9703 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9704 srel->size += rel_size;
927be08e 9705 }
0b8bcf0d 9706 pent = &ent->next;
411e1bfb
AM
9707 }
9708 }
9709 else
0b8bcf0d 9710 *pent = ent->next;
65f38f15 9711 }
e054468f 9712
2d7ad24e
AM
9713 /* Allocate space for plt calls to local syms. */
9714 lgot_masks = (unsigned char *) end_local_plt;
9715 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
9716 {
9717 struct plt_entry *ent;
9718
9719 for (ent = *local_plt; ent != NULL; ent = ent->next)
9720 if (ent->plt.refcount > 0)
9721 {
2d7ad24e
AM
9722 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9723 {
9724 s = htab->elf.iplt;
9725 ent->plt.offset = s->size;
9726 s->size += PLT_ENTRY_SIZE (htab);
9727 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9728 }
3e04d765
AM
9729 else if (htab->can_convert_all_inline_plt
9730 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
9731 ent->plt.offset = (bfd_vma) -1;
9732 else
9733 {
9734 s = htab->pltlocal;
9735 ent->plt.offset = s->size;
9736 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9737 if (bfd_link_pic (info))
9738 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9739 }
e054468f
AM
9740 }
9741 else
9742 ent->plt.offset = (bfd_vma) -1;
9743 }
65f38f15
AM
9744 }
9745
9746 /* Allocate global sym .plt and .got entries, and space for global
9747 sym dynamic relocs. */
4ce794b7 9748 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 9749
0e1862bb 9750 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 9751 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 9752
7865406b 9753 first_tlsld = NULL;
c72f2fb2 9754 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 9755 {
7865406b
AM
9756 struct got_entry *ent;
9757
0c8d6e5c 9758 if (!is_ppc64_elf (ibfd))
102890f0
AM
9759 continue;
9760
7865406b
AM
9761 ent = ppc64_tlsld_got (ibfd);
9762 if (ent->got.refcount > 0)
102890f0 9763 {
7865406b 9764 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9765 {
7865406b
AM
9766 ent->is_indirect = TRUE;
9767 ent->got.ent = first_tlsld;
9768 }
9769 else
9770 {
9771 if (first_tlsld == NULL)
9772 first_tlsld = ent;
9773 s = ppc64_elf_tdata (ibfd)->got;
9774 ent->got.offset = s->size;
9775 ent->owner = ibfd;
9776 s->size += 16;
0e1862bb 9777 if (bfd_link_pic (info))
7865406b
AM
9778 {
9779 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9780 srel->size += sizeof (Elf64_External_Rela);
9781 }
102890f0
AM
9782 }
9783 }
9784 else
7865406b 9785 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9786 }
9787
65f38f15
AM
9788 /* We now have determined the sizes of the various dynamic sections.
9789 Allocate memory for them. */
b34976b6 9790 relocs = FALSE;
65f38f15
AM
9791 for (s = dynobj->sections; s != NULL; s = s->next)
9792 {
9793 if ((s->flags & SEC_LINKER_CREATED) == 0)
9794 continue;
9795
4ce794b7 9796 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9797 /* These haven't been allocated yet; don't strip. */
9798 continue;
33e44f2e
AM
9799 else if (s == htab->elf.sgot
9800 || s == htab->elf.splt
9801 || s == htab->elf.iplt
2d7ad24e 9802 || s == htab->pltlocal
c456f082 9803 || s == htab->glink
9e390558 9804 || s == htab->global_entry
5474d94f
AM
9805 || s == htab->elf.sdynbss
9806 || s == htab->elf.sdynrelro)
65f38f15
AM
9807 {
9808 /* Strip this section if we don't need it; see the
9809 comment below. */
5bd4f169 9810 }
58d180e8
AM
9811 else if (s == htab->glink_eh_frame)
9812 {
9813 if (!bfd_is_abs_section (s->output_section))
9814 /* Not sized yet. */
9815 continue;
9816 }
70cc837d 9817 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9818 {
c456f082 9819 if (s->size != 0)
5bd4f169 9820 {
33e44f2e 9821 if (s != htab->elf.srelplt)
b34976b6 9822 relocs = TRUE;
5bd4f169
AM
9823
9824 /* We use the reloc_count field as a counter if we need
9825 to copy relocs into the output file. */
9826 s->reloc_count = 0;
9827 }
9828 }
65f38f15 9829 else
5bd4f169
AM
9830 {
9831 /* It's not one of our sections, so don't allocate space. */
9832 continue;
9833 }
9834
eea6121a 9835 if (s->size == 0)
5bd4f169 9836 {
c456f082
AM
9837 /* If we don't need this section, strip it from the
9838 output file. This is mostly to handle .rela.bss and
9839 .rela.plt. We must create both sections in
9840 create_dynamic_sections, because they must be created
9841 before the linker maps input sections to output
9842 sections. The linker does that before
9843 adjust_dynamic_symbol is called, and it is that
9844 function which decides whether anything needs to go
9845 into these sections. */
8423293d 9846 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9847 continue;
9848 }
9849
06bcf541
AM
9850 if (bfd_is_abs_section (s->output_section))
9851 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9852 s->name);
9853
c456f082 9854 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9855 continue;
9856
65f38f15
AM
9857 /* Allocate memory for the section contents. We use bfd_zalloc
9858 here in case unused entries are not reclaimed before the
9859 section's contents are written out. This should not happen,
411e1bfb
AM
9860 but this way if it does we get a R_PPC64_NONE reloc in .rela
9861 sections instead of garbage.
9862 We also rely on the section contents being zero when writing
5474d94f 9863 the GOT and .dynrelro. */
eea6121a 9864 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9865 if (s->contents == NULL)
b34976b6 9866 return FALSE;
5bd4f169
AM
9867 }
9868
c72f2fb2 9869 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 9870 {
0c8d6e5c 9871 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9872 continue;
9873
e717da7e 9874 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 9875 if (s != NULL && s != htab->elf.sgot)
e717da7e 9876 {
eea6121a 9877 if (s->size == 0)
8423293d 9878 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9879 else
9880 {
eea6121a 9881 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9882 if (s->contents == NULL)
9883 return FALSE;
9884 }
9885 }
9886 s = ppc64_elf_tdata (ibfd)->relgot;
9887 if (s != NULL)
9888 {
eea6121a 9889 if (s->size == 0)
8423293d 9890 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9891 else
9892 {
eea6121a 9893 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9894 if (s->contents == NULL)
9895 return FALSE;
9896 relocs = TRUE;
9897 s->reloc_count = 0;
9898 }
9899 }
9900 }
9901
e86ce104 9902 if (htab->elf.dynamic_sections_created)
5bd4f169 9903 {
e8910a83
AM
9904 bfd_boolean tls_opt;
9905
5bd4f169
AM
9906 /* Add some entries to the .dynamic section. We fill in the
9907 values later, in ppc64_elf_finish_dynamic_sections, but we
9908 must add the entries now so that we get the correct size for
9909 the .dynamic section. The DT_DEBUG entry is filled in by the
9910 dynamic linker and used by the debugger. */
dc810e39 9911#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9912 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9913
0e1862bb 9914 if (bfd_link_executable (info))
5bd4f169 9915 {
dc810e39 9916 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9917 return FALSE;
5bd4f169
AM
9918 }
9919
33e44f2e 9920 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 9921 {
dc810e39
AM
9922 if (!add_dynamic_entry (DT_PLTGOT, 0)
9923 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9924 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9925 || !add_dynamic_entry (DT_JMPREL, 0)
9926 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9927 return FALSE;
5bd4f169
AM
9928 }
9929
ee67d69a 9930 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
9931 {
9932 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9933 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9934 return FALSE;
19397422
AM
9935 }
9936
7c9cf415 9937 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
9938 && htab->tls_get_addr_fd != NULL
9939 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9940 if (tls_opt || !htab->opd_abi)
9941 {
9942 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9943 return FALSE;
9944 }
a7f2871e 9945
5bd4f169
AM
9946 if (relocs)
9947 {
dc810e39
AM
9948 if (!add_dynamic_entry (DT_RELA, 0)
9949 || !add_dynamic_entry (DT_RELASZ, 0)
9950 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9951 return FALSE;
5bd4f169 9952
65f38f15
AM
9953 /* If any dynamic relocs apply to a read-only section,
9954 then we need a DT_TEXTREL entry. */
248866a8 9955 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 9956 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 9957
65f38f15 9958 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9959 {
65f38f15 9960 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9961 return FALSE;
5bd4f169 9962 }
5bd4f169 9963 }
5bd4f169 9964 }
65f38f15 9965#undef add_dynamic_entry
5bd4f169 9966
b34976b6 9967 return TRUE;
5bd4f169
AM
9968}
9969
a345bc8d
AM
9970/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
9971
9972static bfd_boolean
9973ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9974{
9975 if (h->plt.plist != NULL
9976 && !h->def_regular
9977 && !h->pointer_equality_needed)
9978 return FALSE;
9979
9980 return _bfd_elf_hash_symbol (h);
9981}
9982
721956f4 9983/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9984
4ce794b7
AM
9985static inline enum ppc_stub_type
9986ppc_type_of_stub (asection *input_sec,
9987 const Elf_Internal_Rela *rel,
9988 struct ppc_link_hash_entry **hash,
e054468f 9989 struct plt_entry **plt_ent,
6911b7dc
AM
9990 bfd_vma destination,
9991 unsigned long local_off)
5bd4f169 9992{
721956f4
AM
9993 struct ppc_link_hash_entry *h = *hash;
9994 bfd_vma location;
9995 bfd_vma branch_offset;
9996 bfd_vma max_branch_offset;
4ce794b7 9997 enum elf_ppc64_reloc_type r_type;
5bd4f169 9998
721956f4
AM
9999 if (h != NULL)
10000 {
e054468f 10001 struct plt_entry *ent;
7fe2b9a6 10002 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10003 if (h->oh != NULL
10004 && h->oh->is_func_descriptor)
7b8f6675
AM
10005 {
10006 fdh = ppc_follow_link (h->oh);
10007 *hash = fdh;
10008 }
8387904d 10009
e054468f
AM
10010 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10011 if (ent->addend == rel->r_addend
10012 && ent->plt.offset != (bfd_vma) -1)
10013 {
e054468f
AM
10014 *plt_ent = ent;
10015 return ppc_stub_plt_call;
10016 }
5bd4f169 10017
7fe2b9a6
AM
10018 /* Here, we know we don't have a plt entry. If we don't have a
10019 either a defined function descriptor or a defined entry symbol
10020 in a regular object file, then it is pointless trying to make
10021 any other type of stub. */
854b41e7
AM
10022 if (!is_static_defined (&fdh->elf)
10023 && !is_static_defined (&h->elf))
721956f4 10024 return ppc_stub_none;
5d1634d7 10025 }
e054468f
AM
10026 else if (elf_local_got_ents (input_sec->owner) != NULL)
10027 {
10028 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10029 struct plt_entry **local_plt = (struct plt_entry **)
10030 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10031 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10032
10033 if (local_plt[r_symndx] != NULL)
10034 {
10035 struct plt_entry *ent;
10036
10037 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10038 if (ent->addend == rel->r_addend
10039 && ent->plt.offset != (bfd_vma) -1)
10040 {
10041 *plt_ent = ent;
10042 return ppc_stub_plt_call;
10043 }
10044 }
10045 }
5d1634d7 10046
721956f4
AM
10047 /* Determine where the call point is. */
10048 location = (input_sec->output_offset
10049 + input_sec->output_section->vma
10050 + rel->r_offset);
5d1634d7 10051
721956f4
AM
10052 branch_offset = destination - location;
10053 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10054
721956f4
AM
10055 /* Determine if a long branch stub is needed. */
10056 max_branch_offset = 1 << 25;
23cedd1d
AM
10057 if (r_type == R_PPC64_REL14
10058 || r_type == R_PPC64_REL14_BRTAKEN
10059 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10060 max_branch_offset = 1 << 15;
5d1634d7 10061
6911b7dc 10062 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10063 /* We need a stub. Figure out whether a long_branch or plt_branch
10064 is needed later. */
10065 return ppc_stub_long_branch;
5d1634d7 10066
721956f4 10067 return ppc_stub_none;
5d1634d7
AM
10068}
10069
f891966f
AM
10070/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10071 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10072 . mflr %r12
10073 . bcl 20,31,1f
10074 .1: mflr %r11
10075 . mtlr %r12
05d0e962 10076 . lis %r12,xxx-1b@highest
f891966f 10077 . ori %r12,%r12,xxx-1b@higher
05d0e962 10078 . sldi %r12,%r12,32
f891966f 10079 . oris %r12,%r12,xxx-1b@high
05d0e962 10080 . ori %r12,%r12,xxx-1b@l
f891966f 10081 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10082
10083static bfd_byte *
10084build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10085{
f891966f
AM
10086 bfd_put_32 (abfd, MFLR_R12, p);
10087 p += 4;
10088 bfd_put_32 (abfd, BCL_20_31, p);
10089 p += 4;
10090 bfd_put_32 (abfd, MFLR_R11, p);
10091 p += 4;
10092 bfd_put_32 (abfd, MTLR_R12, p);
10093 p += 4;
05d0e962
AM
10094 if (off + 0x8000 < 0x10000)
10095 {
10096 if (load)
10097 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10098 else
10099 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10100 p += 4;
10101 }
10102 else if (off + 0x80008000ULL < 0x100000000ULL)
10103 {
10104 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10105 p += 4;
10106 if (load)
10107 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10108 else
10109 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10110 p += 4;
10111 }
10112 else
10113 {
10114 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10115 {
10116 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10117 p += 4;
10118 }
10119 else
10120 {
10121 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10122 p += 4;
10123 if (((off >> 32) & 0xffff) != 0)
10124 {
10125 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10126 p += 4;
10127 }
10128 }
10129 if (((off >> 32) & 0xffffffffULL) != 0)
10130 {
10131 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10132 p += 4;
10133 }
10134 if (PPC_HI (off) != 0)
10135 {
10136 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10137 p += 4;
10138 }
10139 if (PPC_LO (off) != 0)
10140 {
10141 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10142 p += 4;
10143 }
10144 if (load)
10145 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10146 else
10147 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10148 p += 4;
10149 }
10150 return p;
10151}
10152
10153static unsigned int
10154size_offset (bfd_vma off)
10155{
10156 unsigned int size;
10157 if (off + 0x8000 < 0x10000)
10158 size = 4;
10159 else if (off + 0x80008000ULL < 0x100000000ULL)
10160 size = 8;
10161 else
10162 {
10163 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10164 size = 4;
10165 else
10166 {
10167 size = 4;
10168 if (((off >> 32) & 0xffff) != 0)
10169 size += 4;
10170 }
10171 if (((off >> 32) & 0xffffffffULL) != 0)
10172 size += 4;
10173 if (PPC_HI (off) != 0)
10174 size += 4;
10175 if (PPC_LO (off) != 0)
10176 size += 4;
10177 size += 4;
10178 }
f891966f 10179 return size + 16;
05d0e962
AM
10180}
10181
3d58e1fc
AM
10182static unsigned int
10183num_relocs_for_offset (bfd_vma off)
10184{
10185 unsigned int num_rel;
10186 if (off + 0x8000 < 0x10000)
10187 num_rel = 1;
10188 else if (off + 0x80008000ULL < 0x100000000ULL)
10189 num_rel = 2;
10190 else
10191 {
10192 num_rel = 1;
10193 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10194 && ((off >> 32) & 0xffff) != 0)
10195 num_rel += 1;
10196 if (PPC_HI (off) != 0)
10197 num_rel += 1;
10198 if (PPC_LO (off) != 0)
10199 num_rel += 1;
10200 }
10201 return num_rel;
10202}
10203
10204static Elf_Internal_Rela *
10205emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10206 bfd_vma roff, bfd_vma targ, bfd_vma off)
10207{
10208 bfd_vma relative_targ = targ - (roff - 8);
10209 if (bfd_big_endian (info->output_bfd))
10210 roff += 2;
10211 r->r_offset = roff;
10212 r->r_addend = relative_targ + roff;
10213 if (off + 0x8000 < 0x10000)
10214 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10215 else if (off + 0x80008000ULL < 0x100000000ULL)
10216 {
10217 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10218 ++r;
10219 roff += 4;
10220 r->r_offset = roff;
10221 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10222 r->r_addend = relative_targ + roff;
10223 }
10224 else
10225 {
10226 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10227 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10228 else
10229 {
10230 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10231 if (((off >> 32) & 0xffff) != 0)
10232 {
10233 ++r;
10234 roff += 4;
10235 r->r_offset = roff;
10236 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10237 r->r_addend = relative_targ + roff;
10238 }
10239 }
10240 if (((off >> 32) & 0xffffffffULL) != 0)
10241 roff += 4;
10242 if (PPC_HI (off) != 0)
10243 {
10244 ++r;
10245 roff += 4;
10246 r->r_offset = roff;
10247 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10248 r->r_addend = relative_targ + roff;
10249 }
10250 if (PPC_LO (off) != 0)
10251 {
10252 ++r;
10253 roff += 4;
10254 r->r_offset = roff;
10255 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10256 r->r_addend = relative_targ + roff;
10257 }
10258 }
10259 return r;
10260}
10261
04bdff6a
AM
10262static bfd_byte *
10263build_powerxx_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10264 bfd_boolean load)
10265{
10266 uint64_t insn;
10267 if (off - odd + (1ULL << 33) < 1ULL << 34)
10268 {
10269 off -= odd;
10270 if (odd)
10271 {
10272 bfd_put_32 (abfd, NOP, p);
10273 p += 4;
10274 }
10275 if (load)
10276 insn = PLD_R12_PC;
10277 else
10278 insn = PADDI_R12_PC;
10279 insn |= D34 (off);
10280 bfd_put_32 (abfd, insn >> 32, p);
10281 p += 4;
10282 bfd_put_32 (abfd, insn, p);
10283 }
10284 /* The minimum value for paddi is -0x200000000. The minimum value
10285 for li is -0x8000, which when shifted by 34 and added gives a
10286 minimum value of -0x2000200000000. The maximum value is
10287 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10288 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10289 {
10290 off -= 8 - odd;
10291 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10292 p += 4;
10293 if (!odd)
10294 {
10295 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10296 p += 4;
10297 }
10298 insn = PADDI_R12_PC | D34 (off);
10299 bfd_put_32 (abfd, insn >> 32, p);
10300 p += 4;
10301 bfd_put_32 (abfd, insn, p);
10302 p += 4;
10303 if (odd)
10304 {
10305 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10306 p += 4;
10307 }
10308 if (load)
10309 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10310 else
10311 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10312 }
10313 else
10314 {
10315 off -= odd + 8;
10316 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10317 p += 4;
10318 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10319 p += 4;
10320 if (odd)
10321 {
10322 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10323 p += 4;
10324 }
10325 insn = PADDI_R12_PC | D34 (off);
10326 bfd_put_32 (abfd, insn >> 32, p);
10327 p += 4;
10328 bfd_put_32 (abfd, insn, p);
10329 p += 4;
10330 if (!odd)
10331 {
10332 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10333 p += 4;
10334 }
10335 if (load)
10336 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10337 else
10338 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10339 }
10340 p += 4;
10341 return p;
10342}
10343
10344static unsigned int
10345size_powerxx_offset (bfd_vma off, int odd)
10346{
10347 if (off - odd + (1ULL << 33) < 1ULL << 34)
10348 return odd + 8;
10349 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10350 return 20;
10351 else
10352 return 24;
10353}
10354
10355static unsigned int
10356num_relocs_for_powerxx_offset (bfd_vma off, int odd)
10357{
10358 if (off - odd + (1ULL << 33) < 1ULL << 34)
10359 return 1;
10360 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10361 return 2;
10362 else
10363 return 3;
10364}
10365
10366static Elf_Internal_Rela *
10367emit_relocs_for_powerxx_offset (struct bfd_link_info *info,
10368 Elf_Internal_Rela *r, bfd_vma roff,
10369 bfd_vma targ, bfd_vma off, int odd)
10370{
10371 if (off - odd + (1ULL << 33) < 1ULL << 34)
10372 roff += odd;
10373 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10374 {
10375 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10376 r->r_offset = roff + d_offset;
10377 r->r_addend = targ + 8 - odd - d_offset;
10378 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10379 ++r;
10380 roff += 8 - odd;
10381 }
10382 else
10383 {
10384 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10385 r->r_offset = roff + d_offset;
10386 r->r_addend = targ + 8 + odd - d_offset;
10387 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10388 ++r;
10389 roff += 4;
10390 r->r_offset = roff + d_offset;
10391 r->r_addend = targ + 4 + odd - d_offset;
10392 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10393 ++r;
10394 roff += 4 + odd;
10395 }
10396 r->r_offset = roff;
10397 r->r_addend = targ;
10398 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10399 return r;
10400}
10401
df136d64
AM
10402/* Emit .eh_frame opcode to advance pc by DELTA. */
10403
10404static bfd_byte *
10405eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10406{
10407 delta /= 4;
10408 if (delta < 64)
10409 *eh++ = DW_CFA_advance_loc + delta;
10410 else if (delta < 256)
10411 {
10412 *eh++ = DW_CFA_advance_loc1;
10413 *eh++ = delta;
10414 }
10415 else if (delta < 65536)
10416 {
10417 *eh++ = DW_CFA_advance_loc2;
10418 bfd_put_16 (abfd, delta, eh);
10419 eh += 2;
10420 }
10421 else
10422 {
10423 *eh++ = DW_CFA_advance_loc4;
10424 bfd_put_32 (abfd, delta, eh);
10425 eh += 4;
10426 }
10427 return eh;
10428}
10429
10430/* Size of required .eh_frame opcode to advance pc by DELTA. */
10431
10432static unsigned int
10433eh_advance_size (unsigned int delta)
10434{
10435 if (delta < 64 * 4)
10436 /* DW_CFA_advance_loc+[1..63]. */
10437 return 1;
10438 if (delta < 256 * 4)
10439 /* DW_CFA_advance_loc1, byte. */
10440 return 2;
10441 if (delta < 65536 * 4)
10442 /* DW_CFA_advance_loc2, 2 bytes. */
10443 return 3;
10444 /* DW_CFA_advance_loc4, 4 bytes. */
10445 return 5;
10446}
10447
794e51c0
AM
10448/* With power7 weakly ordered memory model, it is possible for ld.so
10449 to update a plt entry in one thread and have another thread see a
10450 stale zero toc entry. To avoid this we need some sort of acquire
10451 barrier in the call stub. One solution is to make the load of the
10452 toc word seem to appear to depend on the load of the function entry
10453 word. Another solution is to test for r2 being zero, and branch to
10454 the appropriate glink entry if so.
10455
10456 . fake dep barrier compare
71a39c98
AM
10457 . ld 12,xxx(2) ld 12,xxx(2)
10458 . mtctr 12 mtctr 12
10459 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10460 . add 2,2,11 cmpldi 2,0
10461 . ld 2,xxx+8(2) bnectr+
10462 . bctr b <glink_entry>
10463
10464 The solution involving the compare turns out to be faster, so
10465 that's what we use unless the branch won't reach. */
10466
10467#define ALWAYS_USE_FAKE_DEP 0
10468#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10469
794e51c0
AM
10470static inline unsigned int
10471plt_stub_size (struct ppc_link_hash_table *htab,
10472 struct ppc_stub_hash_entry *stub_entry,
10473 bfd_vma off)
10474{
05d0e962 10475 unsigned size;
b9e5796b 10476
05d0e962
AM
10477 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10478 {
04bdff6a
AM
10479 if (htab->powerxx_stubs)
10480 {
10481 bfd_vma start = (stub_entry->stub_offset
10482 + stub_entry->group->stub_sec->output_offset
10483 + stub_entry->group->stub_sec->output_section->vma);
10484 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10485 start += 4;
10486 size = 8 + size_powerxx_offset (off, start & 4);
10487 }
10488 else
10489 size = 8 + size_offset (off - 8);
05d0e962
AM
10490 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10491 size += 4;
10492 return size;
10493 }
10494
10495 size = 12;
b9e5796b
AM
10496 if (ALWAYS_EMIT_R2SAVE
10497 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10498 size += 4;
10499 if (PPC_HA (off) != 0)
794e51c0 10500 size += 4;
b9e5796b
AM
10501 if (htab->opd_abi)
10502 {
10503 size += 4;
e7d1c40c 10504 if (htab->params->plt_static_chain)
b9e5796b 10505 size += 4;
bd4d2eaa
AM
10506 if (htab->params->plt_thread_safe
10507 && htab->elf.dynamic_sections_created
10508 && stub_entry->h != NULL
10509 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10510 size += 8;
e7d1c40c 10511 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10512 size += 4;
10513 }
794e51c0
AM
10514 if (stub_entry->h != NULL
10515 && (stub_entry->h == htab->tls_get_addr_fd
10516 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10517 && htab->params->tls_get_addr_opt)
f378ab09
AM
10518 {
10519 size += 7 * 4;
e81b4c93 10520 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 10521 size += 6 * 4;
f378ab09 10522 }
794e51c0
AM
10523 return size;
10524}
10525
2420fff6
AM
10526/* Depending on the sign of plt_stub_align:
10527 If positive, return the padding to align to a 2**plt_stub_align
10528 boundary.
10529 If negative, if this stub would cross fewer 2**plt_stub_align
10530 boundaries if we align, then return the padding needed to do so. */
10531
794e51c0
AM
10532static inline unsigned int
10533plt_stub_pad (struct ppc_link_hash_table *htab,
10534 struct ppc_stub_hash_entry *stub_entry,
10535 bfd_vma plt_off)
10536{
2420fff6 10537 int stub_align;
1aa42141 10538 unsigned stub_size;
6f20ed8a 10539 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10540
2420fff6
AM
10541 if (htab->params->plt_stub_align >= 0)
10542 {
10543 stub_align = 1 << htab->params->plt_stub_align;
10544 if ((stub_off & (stub_align - 1)) != 0)
10545 return stub_align - (stub_off & (stub_align - 1));
10546 return 0;
10547 }
10548
10549 stub_align = 1 << -htab->params->plt_stub_align;
1aa42141 10550 stub_size = plt_stub_size (htab, stub_entry, plt_off);
794e51c0 10551 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10552 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10553 return stub_align - (stub_off & (stub_align - 1));
10554 return 0;
10555}
10556
10557/* Build a .plt call stub. */
10558
10559static inline bfd_byte *
10560build_plt_stub (struct ppc_link_hash_table *htab,
10561 struct ppc_stub_hash_entry *stub_entry,
10562 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10563{
e7d1c40c 10564 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10565 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10566 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10567 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10568 && htab->elf.dynamic_sections_created
10569 && stub_entry->h != NULL
10570 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10571 bfd_boolean use_fake_dep = plt_thread_safe;
10572 bfd_vma cmp_branch_off = 0;
10573
10574 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10575 && plt_load_toc
794e51c0 10576 && plt_thread_safe
bd4d2eaa
AM
10577 && !((stub_entry->h == htab->tls_get_addr_fd
10578 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10579 && htab->params->tls_get_addr_opt))
794e51c0
AM
10580 {
10581 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10582 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10583 / PLT_ENTRY_SIZE (htab));
9e390558 10584 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10585 bfd_vma to, from;
10586
68d62958
AM
10587 if (pltindex > 32768)
10588 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10589 to = (glinkoff
10590 + htab->glink->output_offset
10591 + htab->glink->output_section->vma);
6f20ed8a 10592 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10593 + 4 * (ALWAYS_EMIT_R2SAVE
10594 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10595 + 4 * (PPC_HA (offset) != 0)
10596 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10597 != PPC_HA (offset))
10598 + 4 * (plt_static_chain != 0)
10599 + 20
6f20ed8a
AM
10600 + stub_entry->group->stub_sec->output_offset
10601 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10602 cmp_branch_off = to - from;
10603 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10604 }
10605
ac2df442
AM
10606 if (PPC_HA (offset) != 0)
10607 {
176a0d42
AM
10608 if (r != NULL)
10609 {
794e51c0
AM
10610 if (ALWAYS_EMIT_R2SAVE
10611 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10612 r[0].r_offset += 4;
176a0d42 10613 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10614 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10615 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10616 r[1].r_addend = r[0].r_addend;
b9e5796b 10617 if (plt_load_toc)
176a0d42 10618 {
b9e5796b 10619 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10620 {
b9e5796b
AM
10621 r[2].r_offset = r[1].r_offset + 4;
10622 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10623 r[2].r_addend = r[0].r_addend;
10624 }
10625 else
10626 {
10627 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10628 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10629 r[2].r_addend = r[0].r_addend + 8;
10630 if (plt_static_chain)
10631 {
10632 r[3].r_offset = r[2].r_offset + 4;
10633 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10634 r[3].r_addend = r[0].r_addend + 16;
10635 }
c7131b65 10636 }
176a0d42
AM
10637 }
10638 }
794e51c0
AM
10639 if (ALWAYS_EMIT_R2SAVE
10640 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10641 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10642 if (plt_load_toc)
10643 {
10644 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10645 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10646 }
10647 else
10648 {
10649 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10650 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10651 }
b9e5796b
AM
10652 if (plt_load_toc
10653 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10654 {
71a39c98 10655 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10656 offset = 0;
10657 }
71a39c98 10658 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10659 if (plt_load_toc)
794e51c0 10660 {
b9e5796b
AM
10661 if (use_fake_dep)
10662 {
10663 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10664 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10665 }
10666 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10667 if (plt_static_chain)
10668 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10669 }
ac2df442
AM
10670 }
10671 else
10672 {
176a0d42
AM
10673 if (r != NULL)
10674 {
794e51c0
AM
10675 if (ALWAYS_EMIT_R2SAVE
10676 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10677 r[0].r_offset += 4;
176a0d42 10678 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10679 if (plt_load_toc)
176a0d42 10680 {
b9e5796b 10681 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10682 {
b9e5796b
AM
10683 r[1].r_offset = r[0].r_offset + 4;
10684 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10685 r[1].r_addend = r[0].r_addend;
10686 }
10687 else
10688 {
10689 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10690 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10691 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10692 if (plt_static_chain)
10693 {
10694 r[2].r_offset = r[1].r_offset + 4;
10695 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10696 r[2].r_addend = r[0].r_addend + 8;
10697 }
c7131b65 10698 }
176a0d42
AM
10699 }
10700 }
794e51c0
AM
10701 if (ALWAYS_EMIT_R2SAVE
10702 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10703 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10704 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10705 if (plt_load_toc
10706 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10707 {
10708 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10709 offset = 0;
10710 }
71a39c98 10711 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10712 if (plt_load_toc)
794e51c0 10713 {
b9e5796b
AM
10714 if (use_fake_dep)
10715 {
10716 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10717 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10718 }
10719 if (plt_static_chain)
10720 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10721 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10722 }
ac2df442 10723 }
b9e5796b 10724 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10725 {
10726 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10727 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10728 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10729 }
10730 else
407aa07c 10731 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10732 return p;
10733}
10734
a7f2871e
AM
10735/* Build a special .plt call stub for __tls_get_addr. */
10736
10737#define LD_R11_0R3 0xe9630000
10738#define LD_R12_0R3 0xe9830000
10739#define MR_R0_R3 0x7c601b78
10740#define CMPDI_R11_0 0x2c2b0000
10741#define ADD_R3_R12_R13 0x7c6c6a14
10742#define BEQLR 0x4d820020
10743#define MR_R3_R0 0x7c030378
a7f2871e
AM
10744#define STD_R11_0R1 0xf9610000
10745#define BCTRL 0x4e800421
10746#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10747#define MTLR_R11 0x7d6803a6
10748
10749static inline bfd_byte *
794e51c0
AM
10750build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10751 struct ppc_stub_hash_entry *stub_entry,
10752 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10753{
e7d1c40c 10754 bfd *obfd = htab->params->stub_bfd;
df136d64 10755 bfd_byte *loc = p;
794e51c0 10756
a7f2871e
AM
10757 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10758 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10759 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10760 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10761 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10762 bfd_put_32 (obfd, BEQLR, p), p += 4;
10763 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
10764 if (r != NULL)
10765 r[0].r_offset += 7 * 4;
e81b4c93 10766 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
f378ab09
AM
10767 return build_plt_stub (htab, stub_entry, p, offset, r);
10768
a7f2871e 10769 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10770 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10771
10772 if (r != NULL)
f378ab09 10773 r[0].r_offset += 2 * 4;
794e51c0 10774 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 10775 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 10776
a078d95a 10777 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10778 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10779 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10780 bfd_put_32 (obfd, BLR, p), p += 4;
10781
df136d64
AM
10782 if (htab->glink_eh_frame != NULL
10783 && htab->glink_eh_frame->size != 0)
10784 {
10785 bfd_byte *base, *eh;
10786 unsigned int lr_used, delta;
10787
10788 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10789 eh = base + stub_entry->group->eh_size;
10790 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10791 delta = lr_used - stub_entry->group->lr_restore;
10792 stub_entry->group->lr_restore = lr_used + 16;
10793 eh = eh_advance (htab->elf.dynobj, eh, delta);
10794 *eh++ = DW_CFA_offset_extended_sf;
10795 *eh++ = 65;
10796 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10797 *eh++ = DW_CFA_advance_loc + 4;
10798 *eh++ = DW_CFA_restore_extended;
10799 *eh++ = 65;
10800 stub_entry->group->eh_size = eh - base;
10801 }
a7f2871e
AM
10802 return p;
10803}
10804
176a0d42
AM
10805static Elf_Internal_Rela *
10806get_relocs (asection *sec, int count)
10807{
10808 Elf_Internal_Rela *relocs;
10809 struct bfd_elf_section_data *elfsec_data;
10810
10811 elfsec_data = elf_section_data (sec);
10812 relocs = elfsec_data->relocs;
10813 if (relocs == NULL)
10814 {
10815 bfd_size_type relsize;
10816 relsize = sec->reloc_count * sizeof (*relocs);
10817 relocs = bfd_alloc (sec->owner, relsize);
10818 if (relocs == NULL)
10819 return NULL;
10820 elfsec_data->relocs = relocs;
d4730f92
BS
10821 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10822 sizeof (Elf_Internal_Shdr));
10823 if (elfsec_data->rela.hdr == NULL)
10824 return NULL;
10825 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10826 * sizeof (Elf64_External_Rela));
10827 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10828 sec->reloc_count = 0;
10829 }
10830 relocs += sec->reloc_count;
10831 sec->reloc_count += count;
10832 return relocs;
10833}
10834
3d58e1fc
AM
10835/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10836 forms, to the equivalent relocs against the global symbol given by
10837 STUB_ENTRY->H. */
10838
10839static bfd_boolean
10840use_global_in_relocs (struct ppc_link_hash_table *htab,
10841 struct ppc_stub_hash_entry *stub_entry,
10842 Elf_Internal_Rela *r, unsigned int num_rel)
10843{
10844 struct elf_link_hash_entry **hashes;
10845 unsigned long symndx;
10846 struct ppc_link_hash_entry *h;
10847 bfd_vma symval;
10848
10849 /* Relocs are always against symbols in their own object file. Fake
10850 up global sym hashes for the stub bfd (which has no symbols). */
10851 hashes = elf_sym_hashes (htab->params->stub_bfd);
10852 if (hashes == NULL)
10853 {
10854 bfd_size_type hsize;
10855
10856 /* When called the first time, stub_globals will contain the
10857 total number of symbols seen during stub sizing. After
10858 allocating, stub_globals is used as an index to fill the
10859 hashes array. */
10860 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10861 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10862 if (hashes == NULL)
10863 return FALSE;
10864 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10865 htab->stub_globals = 1;
10866 }
10867 symndx = htab->stub_globals++;
10868 h = stub_entry->h;
10869 hashes[symndx] = &h->elf;
10870 if (h->oh != NULL && h->oh->is_func)
10871 h = ppc_follow_link (h->oh);
10872 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10873 || h->elf.root.type == bfd_link_hash_defweak);
10874 symval = (h->elf.root.u.def.value
10875 + h->elf.root.u.def.section->output_offset
10876 + h->elf.root.u.def.section->output_section->vma);
10877 while (num_rel-- != 0)
10878 {
10879 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10880 if (h->elf.root.u.def.section != stub_entry->target_section)
10881 {
10882 /* H is an opd symbol. The addend must be zero, and the
10883 branch reloc is the only one we can convert. */
10884 r->r_addend = 0;
10885 break;
10886 }
10887 else
10888 r->r_addend -= symval;
10889 --r;
10890 }
10891 return TRUE;
10892}
10893
aa374f67 10894static bfd_vma
25f53a85 10895get_r2off (struct bfd_link_info *info,
aa374f67
AM
10896 struct ppc_stub_hash_entry *stub_entry)
10897{
25f53a85 10898 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10899 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10900
10901 if (r2off == 0)
10902 {
10903 /* Support linking -R objects. Get the toc pointer from the
10904 opd entry. */
10905 char buf[8];
b9e5796b
AM
10906 if (!htab->opd_abi)
10907 return r2off;
aa374f67
AM
10908 asection *opd = stub_entry->h->elf.root.u.def.section;
10909 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10910
10911 if (strcmp (opd->name, ".opd") != 0
10912 || opd->reloc_count != 0)
10913 {
2cdcc330
AM
10914 info->callbacks->einfo
10915 (_("%P: cannot find opd entry toc for `%pT'\n"),
10916 stub_entry->h->elf.root.root.string);
aa374f67 10917 bfd_set_error (bfd_error_bad_value);
a7c49797 10918 return (bfd_vma) -1;
aa374f67
AM
10919 }
10920 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10921 return (bfd_vma) -1;
aa374f67 10922 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10923 r2off -= elf_gp (info->output_bfd);
aa374f67 10924 }
6f20ed8a 10925 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10926 return r2off;
10927}
10928
b34976b6 10929static bfd_boolean
4ce794b7 10930ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10931{
721956f4
AM
10932 struct ppc_stub_hash_entry *stub_entry;
10933 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10934 struct bfd_link_info *info;
10935 struct ppc_link_hash_table *htab;
721956f4 10936 bfd_byte *loc;
3d58e1fc 10937 bfd_byte *p, *relp;
1aa42141 10938 bfd_vma targ, off;
176a0d42 10939 Elf_Internal_Rela *r;
e054468f 10940 asection *plt;
3d58e1fc 10941 int num_rel;
04bdff6a 10942 int odd;
5d1634d7 10943
721956f4
AM
10944 /* Massage our args to the form they really have. */
10945 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10946 info = in_arg;
5d1634d7 10947
5d1634d7 10948 htab = ppc_hash_table (info);
4dfe6ac6
NC
10949 if (htab == NULL)
10950 return FALSE;
5d1634d7 10951
1aa42141 10952 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 10953 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10954
4ce794b7 10955 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10956 switch (stub_entry->stub_type)
5d1634d7 10957 {
721956f4 10958 case ppc_stub_long_branch:
ad8e1ba5 10959 case ppc_stub_long_branch_r2off:
721956f4 10960 /* Branches are relative. This is where we are going to. */
1aa42141 10961 targ = (stub_entry->target_value
6911b7dc
AM
10962 + stub_entry->target_section->output_offset
10963 + stub_entry->target_section->output_section->vma);
1aa42141 10964 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 10965
721956f4 10966 /* And this is where we are coming from. */
1aa42141
AM
10967 off = (stub_entry->stub_offset
10968 + stub_entry->group->stub_sec->output_offset
10969 + stub_entry->group->stub_sec->output_section->vma);
10970 off = targ - off;
e86ce104 10971
9e390558 10972 p = loc;
ac2df442 10973 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10974 {
25f53a85 10975 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10976
a7c49797 10977 if (r2off == (bfd_vma) -1)
aa374f67
AM
10978 {
10979 htab->stub_error = TRUE;
10980 return FALSE;
10981 }
9e390558
AM
10982 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10983 p += 4;
ac2df442
AM
10984 if (PPC_HA (r2off) != 0)
10985 {
e7d1c40c 10986 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10987 ADDIS_R2_R2 | PPC_HA (r2off), p);
10988 p += 4;
a7c49797
AM
10989 }
10990 if (PPC_LO (r2off) != 0)
10991 {
10992 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10993 ADDI_R2_R2 | PPC_LO (r2off), p);
10994 p += 4;
ac2df442 10995 }
9e390558 10996 off -= p - loc;
ad8e1ba5 10997 }
9e390558
AM
10998 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10999 p += 4;
ad8e1ba5 11000
5c3dead3
AM
11001 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11002 {
cf97bcb0
AM
11003 _bfd_error_handler
11004 (_("long branch stub `%s' offset overflow"),
bc30df16 11005 stub_entry->root.string);
5c3dead3
AM
11006 htab->stub_error = TRUE;
11007 return FALSE;
11008 }
ee75fd95
AM
11009
11010 if (info->emitrelocations)
11011 {
6f20ed8a 11012 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
11013 if (r == NULL)
11014 return FALSE;
9e390558 11015 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11016 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11017 r->r_addend = targ;
3d58e1fc
AM
11018 if (stub_entry->h != NULL
11019 && !use_global_in_relocs (htab, stub_entry, r, 1))
11020 return FALSE;
ee75fd95 11021 }
721956f4 11022 break;
e86ce104 11023
721956f4 11024 case ppc_stub_plt_branch:
ad8e1ba5 11025 case ppc_stub_plt_branch_r2off:
721956f4
AM
11026 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11027 stub_entry->root.string + 9,
b34976b6 11028 FALSE, FALSE);
721956f4
AM
11029 if (br_entry == NULL)
11030 {
cf97bcb0
AM
11031 _bfd_error_handler (_("can't find branch stub `%s'"),
11032 stub_entry->root.string);
b34976b6
AM
11033 htab->stub_error = TRUE;
11034 return FALSE;
721956f4
AM
11035 }
11036
1aa42141 11037 targ = (stub_entry->target_value
176a0d42
AM
11038 + stub_entry->target_section->output_offset
11039 + stub_entry->target_section->output_section->vma);
6911b7dc 11040 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 11041 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11042
1aa42141 11043 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11044 htab->brlt->contents + br_entry->offset);
721956f4 11045
f94498ff 11046 if (br_entry->iter == htab->stub_iteration)
721956f4 11047 {
f94498ff 11048 br_entry->iter = 0;
84f5d08e 11049
f94498ff 11050 if (htab->relbrlt != NULL)
84f5d08e 11051 {
f94498ff
AM
11052 /* Create a reloc for the branch lookup table entry. */
11053 Elf_Internal_Rela rela;
11054 bfd_byte *rl;
11055
11056 rela.r_offset = (br_entry->offset
11057 + htab->brlt->output_offset
11058 + htab->brlt->output_section->vma);
11059 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11060 rela.r_addend = targ;
f94498ff
AM
11061
11062 rl = htab->relbrlt->contents;
11063 rl += (htab->relbrlt->reloc_count++
11064 * sizeof (Elf64_External_Rela));
11065 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11066 }
11067 else if (info->emitrelocations)
11068 {
176a0d42
AM
11069 r = get_relocs (htab->brlt, 1);
11070 if (r == NULL)
11071 return FALSE;
11072 /* brlt, being SEC_LINKER_CREATED does not go through the
11073 normal reloc processing. Symbols and offsets are not
11074 translated from input file to output file form, so
11075 set up the offset per the output file. */
f94498ff
AM
11076 r->r_offset = (br_entry->offset
11077 + htab->brlt->output_offset
11078 + htab->brlt->output_section->vma);
11079 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11080 r->r_addend = targ;
84f5d08e 11081 }
84f5d08e 11082 }
721956f4 11083
1aa42141 11084 targ = (br_entry->offset
176a0d42
AM
11085 + htab->brlt->output_offset
11086 + htab->brlt->output_section->vma);
11087
1aa42141
AM
11088 off = (elf_gp (info->output_bfd)
11089 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11090 off = targ - off;
721956f4 11091
ad8e1ba5 11092 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11093 {
25f53a85 11094 info->callbacks->einfo
c1c8c1ef 11095 (_("%P: linkage table error against `%pT'\n"),
721956f4 11096 stub_entry->root.string);
5d1634d7 11097 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11098 htab->stub_error = TRUE;
11099 return FALSE;
5d1634d7 11100 }
41bd81ab 11101
176a0d42
AM
11102 if (info->emitrelocations)
11103 {
6f20ed8a 11104 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11105 if (r == NULL)
11106 return FALSE;
6f20ed8a 11107 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11108 if (bfd_big_endian (info->output_bfd))
11109 r[0].r_offset += 2;
00f412ee 11110 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11111 r[0].r_offset += 4;
11112 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11113 r[0].r_addend = targ;
176a0d42
AM
11114 if (PPC_HA (off) != 0)
11115 {
11116 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11117 r[1].r_offset = r[0].r_offset + 4;
11118 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11119 r[1].r_addend = r[0].r_addend;
11120 }
11121 }
11122
9e390558 11123 p = loc;
00f412ee 11124 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11125 {
176a0d42 11126 if (PPC_HA (off) != 0)
ac2df442 11127 {
e7d1c40c 11128 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11129 ADDIS_R12_R2 | PPC_HA (off), p);
11130 p += 4;
e7d1c40c 11131 bfd_put_32 (htab->params->stub_bfd,
9e390558 11132 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11133 }
11134 else
9e390558
AM
11135 bfd_put_32 (htab->params->stub_bfd,
11136 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11137 }
11138 else
11139 {
25f53a85 11140 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11141
a7c49797 11142 if (r2off == (bfd_vma) -1)
aa374f67
AM
11143 {
11144 htab->stub_error = TRUE;
11145 return FALSE;
11146 }
ad8e1ba5 11147
9e390558
AM
11148 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11149 p += 4;
176a0d42 11150 if (PPC_HA (off) != 0)
ac2df442 11151 {
e7d1c40c 11152 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11153 ADDIS_R12_R2 | PPC_HA (off), p);
11154 p += 4;
e7d1c40c 11155 bfd_put_32 (htab->params->stub_bfd,
9e390558 11156 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11157 }
11158 else
9e390558 11159 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11160
11161 if (PPC_HA (r2off) != 0)
11162 {
9e390558 11163 p += 4;
e7d1c40c 11164 bfd_put_32 (htab->params->stub_bfd,
9e390558 11165 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11166 }
11167 if (PPC_LO (r2off) != 0)
11168 {
9e390558 11169 p += 4;
e7d1c40c 11170 bfd_put_32 (htab->params->stub_bfd,
9e390558 11171 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11172 }
ad8e1ba5 11173 }
9e390558
AM
11174 p += 4;
11175 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11176 p += 4;
407aa07c
AM
11177 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11178 p += 4;
721956f4 11179 break;
5d1634d7 11180
05d0e962
AM
11181 case ppc_stub_long_branch_notoc:
11182 case ppc_stub_long_branch_both:
11183 case ppc_stub_plt_branch_notoc:
11184 case ppc_stub_plt_branch_both:
11185 case ppc_stub_plt_call_notoc:
11186 case ppc_stub_plt_call_both:
11187 p = loc;
f891966f 11188 off = (stub_entry->stub_offset
05d0e962
AM
11189 + stub_entry->group->stub_sec->output_offset
11190 + stub_entry->group->stub_sec->output_section->vma);
11191 if (stub_entry->stub_type == ppc_stub_long_branch_both
11192 || stub_entry->stub_type == ppc_stub_plt_branch_both
11193 || stub_entry->stub_type == ppc_stub_plt_call_both)
11194 {
11195 off += 4;
11196 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11197 p += 4;
11198 }
11199 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11200 {
11201 targ = stub_entry->plt_ent->plt.offset & ~1;
11202 if (targ >= (bfd_vma) -2)
11203 abort ();
11204
11205 plt = htab->elf.splt;
11206 if (!htab->elf.dynamic_sections_created
11207 || stub_entry->h == NULL
11208 || stub_entry->h->elf.dynindx == -1)
11209 {
11210 if (stub_entry->symtype == STT_GNU_IFUNC)
11211 plt = htab->elf.iplt;
11212 else
11213 plt = htab->pltlocal;
11214 }
11215 targ += plt->output_offset + plt->output_section->vma;
11216 }
11217 else
11218 targ = (stub_entry->target_value
11219 + stub_entry->target_section->output_offset
11220 + stub_entry->target_section->output_section->vma);
04bdff6a 11221 odd = off & 4;
05d0e962 11222 off = targ - off;
f891966f 11223
3d58e1fc
AM
11224 relp = p;
11225 num_rel = 0;
04bdff6a
AM
11226 if (htab->powerxx_stubs)
11227 {
11228 bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
11229 p = build_powerxx_offset (htab->params->stub_bfd, p, off, odd, load);
11230 }
11231 else
11232 {
11233 /* The notoc stubs calculate their target (either a PLT entry or
11234 the global entry point of a function) relative to the PC
11235 returned by the "bcl" two instructions past the start of the
11236 sequence emitted by build_offset. The offset is therefore 8
11237 less than calculated from the start of the sequence. */
11238 off -= 8;
11239 p = build_offset (htab->params->stub_bfd, p, off,
11240 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11241 }
11242
f891966f 11243 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
05d0e962 11244 {
f891966f 11245 bfd_vma from;
3d58e1fc 11246 num_rel = 1;
f891966f
AM
11247 from = (stub_entry->stub_offset
11248 + stub_entry->group->stub_sec->output_offset
11249 + stub_entry->group->stub_sec->output_section->vma
11250 + (p - loc));
05d0e962 11251 bfd_put_32 (htab->params->stub_bfd,
f891966f 11252 B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
11253 }
11254 else
11255 {
11256 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11257 p += 4;
11258 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11259 }
11260 p += 4;
df136d64 11261
3d58e1fc
AM
11262 if (info->emitrelocations)
11263 {
04bdff6a
AM
11264 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
11265 if (htab->powerxx_stubs)
11266 num_rel += num_relocs_for_powerxx_offset (off, odd);
11267 else
11268 {
11269 num_rel += num_relocs_for_offset (off);
11270 roff += 16;
11271 }
3d58e1fc
AM
11272 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11273 if (r == NULL)
11274 return FALSE;
04bdff6a
AM
11275 if (htab->powerxx_stubs)
11276 r = emit_relocs_for_powerxx_offset (info, r, roff, targ, off, odd);
11277 else
11278 r = emit_relocs_for_offset (info, r, roff, targ, off);
3d58e1fc
AM
11279 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11280 || stub_entry->stub_type == ppc_stub_long_branch_both)
11281 {
11282 ++r;
11283 roff = p - 4 - stub_entry->group->stub_sec->contents;
11284 r->r_offset = roff;
11285 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11286 r->r_addend = targ;
11287 if (stub_entry->h != NULL
11288 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11289 return FALSE;
11290 }
11291 }
11292
04bdff6a
AM
11293 if (!htab->powerxx_stubs
11294 && htab->glink_eh_frame != NULL
11295 && htab->glink_eh_frame->size != 0)
df136d64
AM
11296 {
11297 bfd_byte *base, *eh;
11298 unsigned int lr_used, delta;
11299
11300 base = (htab->glink_eh_frame->contents
11301 + stub_entry->group->eh_base + 17);
11302 eh = base + stub_entry->group->eh_size;
11303 lr_used = stub_entry->stub_offset + 8;
11304 if (stub_entry->stub_type == ppc_stub_long_branch_both
11305 || stub_entry->stub_type == ppc_stub_plt_branch_both
11306 || stub_entry->stub_type == ppc_stub_plt_call_both)
11307 lr_used += 4;
11308 delta = lr_used - stub_entry->group->lr_restore;
11309 stub_entry->group->lr_restore = lr_used + 8;
11310 eh = eh_advance (htab->elf.dynobj, eh, delta);
11311 *eh++ = DW_CFA_register;
11312 *eh++ = 65;
11313 *eh++ = 12;
11314 *eh++ = DW_CFA_advance_loc + 2;
11315 *eh++ = DW_CFA_restore_extended;
11316 *eh++ = 65;
11317 stub_entry->group->eh_size = eh - base;
11318 }
05d0e962
AM
11319 break;
11320
721956f4 11321 case ppc_stub_plt_call:
794e51c0 11322 case ppc_stub_plt_call_r2save:
e054468f 11323 if (stub_entry->h != NULL
b31867b6
AM
11324 && stub_entry->h->is_func_descriptor
11325 && stub_entry->h->oh != NULL)
c862ae31 11326 {
b31867b6
AM
11327 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11328
11329 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11330 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11331 if (fh->elf.root.type == bfd_link_hash_undefined
11332 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11333 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11334 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11335 }
11336
721956f4 11337 /* Now build the stub. */
1aa42141
AM
11338 targ = stub_entry->plt_ent->plt.offset & ~1;
11339 if (targ >= (bfd_vma) -2)
721956f4
AM
11340 abort ();
11341
33e44f2e 11342 plt = htab->elf.splt;
25f23106
AM
11343 if (!htab->elf.dynamic_sections_created
11344 || stub_entry->h == NULL
11345 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11346 {
11347 if (stub_entry->symtype == STT_GNU_IFUNC)
11348 plt = htab->elf.iplt;
11349 else
11350 plt = htab->pltlocal;
11351 }
1aa42141 11352 targ += plt->output_offset + plt->output_section->vma;
e054468f 11353
1aa42141
AM
11354 off = (elf_gp (info->output_bfd)
11355 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11356 off = targ - off;
721956f4 11357
ad8e1ba5 11358 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11359 {
25f53a85 11360 info->callbacks->einfo
695344c0 11361 /* xgettext:c-format */
c1c8c1ef 11362 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11363 stub_entry->h != NULL
11364 ? stub_entry->h->elf.root.root.string
11365 : "<local sym>");
721956f4 11366 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11367 htab->stub_error = TRUE;
11368 return FALSE;
721956f4
AM
11369 }
11370
176a0d42
AM
11371 r = NULL;
11372 if (info->emitrelocations)
11373 {
6f20ed8a 11374 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11375 ((PPC_HA (off) != 0)
11376 + (htab->opd_abi
e7d1c40c 11377 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11378 && PPC_HA (off + 16) == PPC_HA (off))
11379 : 1)));
176a0d42
AM
11380 if (r == NULL)
11381 return FALSE;
6f20ed8a 11382 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11383 if (bfd_big_endian (info->output_bfd))
11384 r[0].r_offset += 2;
1aa42141 11385 r[0].r_addend = targ;
176a0d42 11386 }
a7f2871e
AM
11387 if (stub_entry->h != NULL
11388 && (stub_entry->h == htab->tls_get_addr_fd
11389 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11390 && htab->params->tls_get_addr_opt)
794e51c0 11391 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11392 else
794e51c0 11393 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11394 break;
11395
a4b6fadd
AM
11396 case ppc_stub_save_res:
11397 return TRUE;
11398
721956f4
AM
11399 default:
11400 BFD_FAIL ();
b34976b6 11401 return FALSE;
721956f4
AM
11402 }
11403
1aa42141 11404 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 11405
e7d1c40c 11406 if (htab->params->emit_stub_syms)
97b639ba
AM
11407 {
11408 struct elf_link_hash_entry *h;
ee75fd95
AM
11409 size_t len1, len2;
11410 char *name;
11411 const char *const stub_str[] = { "long_branch",
05d0e962
AM
11412 "long_branch",
11413 "long_branch",
3f6ff479
AM
11414 "long_branch",
11415 "plt_branch",
ee75fd95 11416 "plt_branch",
05d0e962
AM
11417 "plt_branch",
11418 "plt_branch",
11419 "plt_call",
11420 "plt_call",
794e51c0 11421 "plt_call",
ee75fd95
AM
11422 "plt_call" };
11423
11424 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11425 len2 = strlen (stub_entry->root.string);
11426 name = bfd_malloc (len1 + len2 + 2);
11427 if (name == NULL)
11428 return FALSE;
11429 memcpy (name, stub_entry->root.string, 9);
11430 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11431 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11432 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11433 if (h == NULL)
11434 return FALSE;
11435 if (h->root.type == bfd_link_hash_new)
11436 {
11437 h->root.type = bfd_link_hash_defined;
6f20ed8a 11438 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11439 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11440 h->ref_regular = 1;
11441 h->def_regular = 1;
11442 h->ref_regular_nonweak = 1;
11443 h->forced_local = 1;
11444 h->non_elf = 0;
2ec55de3 11445 h->root.linker_def = 1;
97b639ba
AM
11446 }
11447 }
11448
b34976b6 11449 return TRUE;
721956f4
AM
11450}
11451
11452/* As above, but don't actually build the stub. Just bump offset so
11453 we know stub section sizes, and select plt_branch stubs where
11454 long_branch stubs won't do. */
11455
b34976b6 11456static bfd_boolean
4ce794b7 11457ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11458{
11459 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11460 struct bfd_link_info *info;
721956f4 11461 struct ppc_link_hash_table *htab;
f891966f
AM
11462 asection *plt;
11463 bfd_vma targ, off, r2off;
04bdff6a 11464 unsigned int size, extra, lr_used, delta, odd;
721956f4
AM
11465
11466 /* Massage our args to the form they really have. */
11467 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11468 info = in_arg;
11469
11470 htab = ppc_hash_table (info);
4dfe6ac6
NC
11471 if (htab == NULL)
11472 return FALSE;
721956f4 11473
1aa42141
AM
11474 /* Make a note of the offset within the stubs for this entry. */
11475 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11476
a4b6fadd
AM
11477 if (stub_entry->h != NULL
11478 && stub_entry->h->save_res
11479 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11480 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11481 {
11482 /* Don't make stubs to out-of-line register save/restore
11483 functions. Instead, emit copies of the functions. */
11484 stub_entry->group->needs_save_res = 1;
11485 stub_entry->stub_type = ppc_stub_save_res;
11486 return TRUE;
11487 }
11488
f891966f 11489 switch (stub_entry->stub_type)
721956f4 11490 {
f891966f
AM
11491 case ppc_stub_plt_branch:
11492 case ppc_stub_plt_branch_r2off:
11493 /* Reset the stub type from the plt branch variant in case we now
11494 can reach with a shorter stub. */
11495 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11496 /* Fall through. */
11497 case ppc_stub_long_branch:
11498 case ppc_stub_long_branch_r2off:
1aa42141
AM
11499 targ = (stub_entry->target_value
11500 + stub_entry->target_section->output_offset
11501 + stub_entry->target_section->output_section->vma);
f891966f 11502 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
1aa42141
AM
11503 off = (stub_entry->stub_offset
11504 + stub_entry->group->stub_sec->output_offset
11505 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11506
ad8e1ba5 11507 size = 4;
f891966f 11508 r2off = 0;
ad8e1ba5
AM
11509 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11510 {
25f53a85 11511 r2off = get_r2off (info, stub_entry);
a7c49797 11512 if (r2off == (bfd_vma) -1)
aa374f67
AM
11513 {
11514 htab->stub_error = TRUE;
11515 return FALSE;
11516 }
a7c49797 11517 size = 8;
ac2df442 11518 if (PPC_HA (r2off) != 0)
a7c49797
AM
11519 size += 4;
11520 if (PPC_LO (r2off) != 0)
11521 size += 4;
1aa42141 11522 off += size - 4;
ad8e1ba5 11523 }
1aa42141 11524 off = targ - off;
ad8e1ba5 11525
f891966f
AM
11526 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11527 Do the same for -R objects without function descriptors. */
11528 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11529 && r2off == 0
11530 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11531 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 11532 {
f891966f 11533 struct ppc_branch_hash_entry *br_entry;
df136d64 11534
f891966f
AM
11535 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11536 stub_entry->root.string + 9,
11537 TRUE, FALSE);
11538 if (br_entry == NULL)
721956f4 11539 {
f891966f
AM
11540 _bfd_error_handler (_("can't build branch stub `%s'"),
11541 stub_entry->root.string);
11542 htab->stub_error = TRUE;
11543 return FALSE;
721956f4
AM
11544 }
11545
f891966f 11546 if (br_entry->iter != htab->stub_iteration)
721956f4 11547 {
f891966f
AM
11548 br_entry->iter = htab->stub_iteration;
11549 br_entry->offset = htab->brlt->size;
11550 htab->brlt->size += 8;
63bc6f6c 11551
f891966f
AM
11552 if (htab->relbrlt != NULL)
11553 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11554 else if (info->emitrelocations)
84f5d08e 11555 {
f891966f
AM
11556 htab->brlt->reloc_count += 1;
11557 htab->brlt->flags |= SEC_RELOC;
05d0e962 11558 }
f891966f 11559 }
ac2df442 11560
f891966f
AM
11561 targ = (br_entry->offset
11562 + htab->brlt->output_offset
11563 + htab->brlt->output_section->vma);
11564 off = (elf_gp (info->output_bfd)
11565 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11566 off = targ - off;
176a0d42 11567
f891966f
AM
11568 if (info->emitrelocations)
11569 {
11570 stub_entry->group->stub_sec->reloc_count
11571 += 1 + (PPC_HA (off) != 0);
11572 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11573 }
05d0e962 11574
f891966f
AM
11575 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11576 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11577 {
11578 size = 12;
11579 if (PPC_HA (off) != 0)
11580 size = 16;
ac2df442 11581 }
f891966f 11582 else
ac2df442 11583 {
f891966f
AM
11584 size = 16;
11585 if (PPC_HA (off) != 0)
11586 size += 4;
11587
11588 if (PPC_HA (r2off) != 0)
11589 size += 4;
11590 if (PPC_LO (r2off) != 0)
11591 size += 4;
ac2df442 11592 }
721956f4 11593 }
f891966f
AM
11594 else if (info->emitrelocations)
11595 {
11596 stub_entry->group->stub_sec->reloc_count += 1;
11597 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11598 }
11599 break;
11600
11601 case ppc_stub_plt_branch_notoc:
11602 case ppc_stub_plt_branch_both:
11603 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11604 /* Fall through. */
11605 case ppc_stub_long_branch_notoc:
11606 case ppc_stub_long_branch_both:
11607 off = (stub_entry->stub_offset
11608 + stub_entry->group->stub_sec->output_offset
11609 + stub_entry->group->stub_sec->output_section->vma);
11610 size = 0;
11611 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11612 size = 4;
11613 off += size;
11614 targ = (stub_entry->target_value
11615 + stub_entry->target_section->output_offset
11616 + stub_entry->target_section->output_section->vma);
04bdff6a 11617 odd = off & 4;
f891966f
AM
11618 off = targ - off;
11619
3d58e1fc
AM
11620 if (info->emitrelocations)
11621 {
04bdff6a
AM
11622 unsigned int num_rel;
11623 if (htab->powerxx_stubs)
11624 num_rel = num_relocs_for_powerxx_offset (off, odd);
11625 else
11626 num_rel = num_relocs_for_offset (off - 8);
11627 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
11628 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11629 }
11630
04bdff6a
AM
11631 if (htab->powerxx_stubs)
11632 extra = size_powerxx_offset (off, odd);
11633 else
11634 extra = size_offset (off - 8);
f891966f
AM
11635 /* Include branch insn plus those in the offset sequence. */
11636 size += 4 + extra;
11637 /* The branch insn is at the end, or "extra" bytes along. So
11638 its offset will be "extra" bytes less that that already
11639 calculated. */
11640 off -= extra;
11641
04bdff6a
AM
11642 if (!htab->powerxx_stubs)
11643 {
11644 /* After the bcl, lr has been modified so we need to emit
11645 .eh_frame info saying the return address is in r12. */
11646 lr_used = stub_entry->stub_offset + 8;
11647 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11648 lr_used += 4;
11649 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11650 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11651 DW_CFA_restore_extended 65. */
11652 delta = lr_used - stub_entry->group->lr_restore;
11653 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11654 stub_entry->group->lr_restore = lr_used + 8;
11655 }
f891966f
AM
11656
11657 /* If the branch can't reach, use a plt_branch. */
11658 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11659 {
11660 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11661 - ppc_stub_long_branch_notoc);
11662 size += 4;
11663 }
3d58e1fc
AM
11664 else if (info->emitrelocations)
11665 stub_entry->group->stub_sec->reloc_count +=1;
f891966f
AM
11666 break;
11667
11668 case ppc_stub_plt_call_notoc:
11669 case ppc_stub_plt_call_both:
11670 off = (stub_entry->stub_offset
11671 + stub_entry->group->stub_sec->output_offset
11672 + stub_entry->group->stub_sec->output_section->vma);
11673 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11674 off += 4;
11675 targ = stub_entry->plt_ent->plt.offset & ~1;
11676 if (targ >= (bfd_vma) -2)
11677 abort ();
11678
11679 plt = htab->elf.splt;
11680 if (!htab->elf.dynamic_sections_created
11681 || stub_entry->h == NULL
11682 || stub_entry->h->elf.dynindx == -1)
11683 {
11684 if (stub_entry->symtype == STT_GNU_IFUNC)
11685 plt = htab->elf.iplt;
11686 else
11687 plt = htab->pltlocal;
11688 }
11689 targ += plt->output_offset + plt->output_section->vma;
04bdff6a 11690 odd = off & 4;
f891966f
AM
11691 off = targ - off;
11692
11693 if (htab->params->plt_stub_align != 0)
11694 {
11695 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11696
11697 stub_entry->group->stub_sec->size += pad;
11698 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11699 off -= pad;
11700 }
11701
3d58e1fc
AM
11702 if (info->emitrelocations)
11703 {
04bdff6a
AM
11704 unsigned int num_rel;
11705 if (htab->powerxx_stubs)
11706 num_rel = num_relocs_for_powerxx_offset (off, odd);
11707 else
11708 num_rel = num_relocs_for_offset (off - 8);
11709 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
11710 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11711 }
11712
f891966f
AM
11713 size = plt_stub_size (htab, stub_entry, off);
11714
04bdff6a
AM
11715 if (!htab->powerxx_stubs)
11716 {
11717 /* After the bcl, lr has been modified so we need to emit
11718 .eh_frame info saying the return address is in r12. */
11719 lr_used = stub_entry->stub_offset + 8;
11720 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11721 lr_used += 4;
11722 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11723 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11724 DW_CFA_restore_extended 65. */
11725 delta = lr_used - stub_entry->group->lr_restore;
11726 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11727 stub_entry->group->lr_restore = lr_used + 8;
11728 }
f891966f
AM
11729 break;
11730
11731 case ppc_stub_plt_call:
11732 case ppc_stub_plt_call_r2save:
11733 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11734 if (targ >= (bfd_vma) -2)
11735 abort ();
11736 plt = htab->elf.splt;
11737 if (!htab->elf.dynamic_sections_created
11738 || stub_entry->h == NULL
11739 || stub_entry->h->elf.dynindx == -1)
11740 {
11741 if (stub_entry->symtype == STT_GNU_IFUNC)
11742 plt = htab->elf.iplt;
11743 else
11744 plt = htab->pltlocal;
11745 }
11746 targ += plt->output_offset + plt->output_section->vma;
11747
11748 off = (elf_gp (info->output_bfd)
11749 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11750 off = targ - off;
11751
11752 if (htab->params->plt_stub_align != 0)
11753 {
11754 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11755
11756 stub_entry->group->stub_sec->size += pad;
11757 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11758 }
11759
11760 if (info->emitrelocations)
11761 {
11762 stub_entry->group->stub_sec->reloc_count
11763 += ((PPC_HA (off) != 0)
11764 + (htab->opd_abi
11765 ? 2 + (htab->params->plt_static_chain
11766 && PPC_HA (off + 16) == PPC_HA (off))
11767 : 1));
11768 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11769 }
11770
11771 size = plt_stub_size (htab, stub_entry, off);
11772
11773 if (stub_entry->h != NULL
11774 && (stub_entry->h == htab->tls_get_addr_fd
11775 || stub_entry->h == htab->tls_get_addr)
11776 && htab->params->tls_get_addr_opt
11777 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11778 {
11779 /* After the bctrl, lr has been modified so we need to
11780 emit .eh_frame info saying the return address is
11781 on the stack. In fact we put the EH info specifying
11782 that the return address is on the stack *at* the
11783 call rather than after it, because the EH info for a
11784 call needs to be specified by that point.
11785 See libgcc/unwind-dw2.c execute_cfa_program. */
11786 lr_used = stub_entry->stub_offset + size - 20;
11787 /* The eh_frame info will consist of a DW_CFA_advance_loc
11788 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11789 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11790 delta = lr_used - stub_entry->group->lr_restore;
11791 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11792 stub_entry->group->lr_restore = size - 4;
11793 }
11794 break;
11795
11796 default:
11797 BFD_FAIL ();
11798 return FALSE;
721956f4
AM
11799 }
11800
6f20ed8a 11801 stub_entry->group->stub_sec->size += size;
b34976b6 11802 return TRUE;
721956f4
AM
11803}
11804
11805/* Set up various things so that we can make a list of input sections
11806 for each output section included in the link. Returns -1 on error,
cedb70c5 11807 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11808
11809int
e7d1c40c 11810ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11811{
6f20ed8a 11812 unsigned int id;
721956f4
AM
11813 bfd_size_type amt;
11814 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11815
4dfe6ac6
NC
11816 if (htab == NULL)
11817 return -1;
4c52953f 11818
7cf7fcc8 11819 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
11820 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11821 htab->sec_info = bfd_zmalloc (amt);
11822 if (htab->sec_info == NULL)
721956f4
AM
11823 return -1;
11824
3d6f9012
AM
11825 /* Set toc_off for com, und, abs and ind sections. */
11826 for (id = 0; id < 3; id++)
6f20ed8a 11827 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11828
721956f4
AM
11829 return 1;
11830}
11831
927be08e
AM
11832/* Set up for first pass at multitoc partitioning. */
11833
11834void
11835ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11836{
11837 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11838
1c865ab2 11839 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11840 htab->toc_bfd = NULL;
11841 htab->toc_first_sec = NULL;
11842}
11843
e717da7e
AM
11844/* The linker repeatedly calls this function for each TOC input section
11845 and linker generated GOT section. Group input bfds such that the toc
927be08e 11846 within a group is less than 64k in size. */
ad8e1ba5 11847
927be08e 11848bfd_boolean
4ce794b7 11849ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11850{
11851 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11852 bfd_vma addr, off, limit;
ad8e1ba5 11853
4dfe6ac6
NC
11854 if (htab == NULL)
11855 return FALSE;
11856
927be08e 11857 if (!htab->second_toc_pass)
4c52953f 11858 {
927be08e 11859 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11860 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11861
11862 if (new_bfd)
bf102f86
AM
11863 {
11864 htab->toc_bfd = isec->owner;
11865 htab->toc_first_sec = isec;
11866 }
927be08e 11867
bf102f86
AM
11868 addr = isec->output_offset + isec->output_section->vma;
11869 off = addr - htab->toc_curr;
d77c8a4b
AM
11870 limit = 0x80008000;
11871 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11872 limit = 0x10000;
11873 if (off + isec->size > limit)
bf102f86
AM
11874 {
11875 addr = (htab->toc_first_sec->output_offset
11876 + htab->toc_first_sec->output_section->vma);
11877 htab->toc_curr = addr;
a27e685f 11878 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11879 }
99877b66 11880
927be08e
AM
11881 /* toc_curr is the base address of this toc group. Set elf_gp
11882 for the input section to be the offset relative to the
11883 output toc base plus 0x8000. Making the input elf_gp an
11884 offset allows us to move the toc as a whole without
11885 recalculating input elf_gp. */
06bcf541 11886 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
11887 off += TOC_BASE_OFF;
11888
11889 /* Die if someone uses a linker script that doesn't keep input
11890 file .toc and .got together. */
a4fd3de5
AM
11891 if (new_bfd
11892 && elf_gp (isec->owner) != 0
927be08e
AM
11893 && elf_gp (isec->owner) != off)
11894 return FALSE;
11895
11896 elf_gp (isec->owner) = off;
11897 return TRUE;
4c52953f 11898 }
927be08e
AM
11899
11900 /* During the second pass toc_first_sec points to the start of
11901 a toc group, and toc_curr is used to track the old elf_gp.
11902 We use toc_bfd to ensure we only look at each bfd once. */
11903 if (htab->toc_bfd == isec->owner)
11904 return TRUE;
11905 htab->toc_bfd = isec->owner;
11906
11907 if (htab->toc_first_sec == NULL
11908 || htab->toc_curr != elf_gp (isec->owner))
11909 {
11910 htab->toc_curr = elf_gp (isec->owner);
11911 htab->toc_first_sec = isec;
11912 }
11913 addr = (htab->toc_first_sec->output_offset
11914 + htab->toc_first_sec->output_section->vma);
06bcf541 11915 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
11916 elf_gp (isec->owner) = off;
11917
11918 return TRUE;
ad8e1ba5
AM
11919}
11920
927be08e
AM
11921/* Called via elf_link_hash_traverse to merge GOT entries for global
11922 symbol H. */
11923
11924static bfd_boolean
11925merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11926{
11927 if (h->root.type == bfd_link_hash_indirect)
11928 return TRUE;
11929
927be08e
AM
11930 merge_got_entries (&h->got.glist);
11931
11932 return TRUE;
11933}
11934
11935/* Called via elf_link_hash_traverse to allocate GOT entries for global
11936 symbol H. */
11937
11938static bfd_boolean
11939reallocate_got (struct elf_link_hash_entry *h, void *inf)
11940{
11941 struct got_entry *gent;
11942
11943 if (h->root.type == bfd_link_hash_indirect)
11944 return TRUE;
11945
927be08e
AM
11946 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11947 if (!gent->is_indirect)
11948 allocate_got (h, (struct bfd_link_info *) inf, gent);
11949 return TRUE;
11950}
11951
11952/* Called on the first multitoc pass after the last call to
11953 ppc64_elf_next_toc_section. This function removes duplicate GOT
11954 entries. */
11955
11956bfd_boolean
11957ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11958{
11959 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11960 struct bfd *ibfd, *ibfd2;
11961 bfd_boolean done_something;
11962
11963 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11964
7865406b
AM
11965 if (!htab->do_multi_toc)
11966 return FALSE;
11967
d0fae19d 11968 /* Merge global sym got entries within a toc group. */
927be08e
AM
11969 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11970
11971 /* And tlsld_got. */
c72f2fb2 11972 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11973 {
11974 struct got_entry *ent, *ent2;
11975
11976 if (!is_ppc64_elf (ibfd))
11977 continue;
11978
11979 ent = ppc64_tlsld_got (ibfd);
11980 if (!ent->is_indirect
11981 && ent->got.offset != (bfd_vma) -1)
11982 {
c72f2fb2 11983 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11984 {
11985 if (!is_ppc64_elf (ibfd2))
11986 continue;
11987
11988 ent2 = ppc64_tlsld_got (ibfd2);
11989 if (!ent2->is_indirect
11990 && ent2->got.offset != (bfd_vma) -1
11991 && elf_gp (ibfd2) == elf_gp (ibfd))
11992 {
11993 ent2->is_indirect = TRUE;
11994 ent2->got.ent = ent;
11995 }
11996 }
11997 }
11998 }
11999
12000 /* Zap sizes of got sections. */
33e44f2e
AM
12001 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12002 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12003 htab->got_reli_size = 0;
12004
c72f2fb2 12005 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12006 {
12007 asection *got, *relgot;
12008
12009 if (!is_ppc64_elf (ibfd))
12010 continue;
12011
12012 got = ppc64_elf_tdata (ibfd)->got;
12013 if (got != NULL)
12014 {
12015 got->rawsize = got->size;
12016 got->size = 0;
12017 relgot = ppc64_elf_tdata (ibfd)->relgot;
12018 relgot->rawsize = relgot->size;
12019 relgot->size = 0;
12020 }
12021 }
12022
12023 /* Now reallocate the got, local syms first. We don't need to
12024 allocate section contents again since we never increase size. */
c72f2fb2 12025 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12026 {
12027 struct got_entry **lgot_ents;
12028 struct got_entry **end_lgot_ents;
12029 struct plt_entry **local_plt;
12030 struct plt_entry **end_local_plt;
f961d9dd 12031 unsigned char *lgot_masks;
927be08e
AM
12032 bfd_size_type locsymcount;
12033 Elf_Internal_Shdr *symtab_hdr;
19e08130 12034 asection *s;
927be08e
AM
12035
12036 if (!is_ppc64_elf (ibfd))
12037 continue;
12038
12039 lgot_ents = elf_local_got_ents (ibfd);
12040 if (!lgot_ents)
12041 continue;
12042
12043 symtab_hdr = &elf_symtab_hdr (ibfd);
12044 locsymcount = symtab_hdr->sh_info;
12045 end_lgot_ents = lgot_ents + locsymcount;
12046 local_plt = (struct plt_entry **) end_lgot_ents;
12047 end_local_plt = local_plt + locsymcount;
f961d9dd 12048 lgot_masks = (unsigned char *) end_local_plt;
927be08e 12049 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
12050 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12051 {
12052 struct got_entry *ent;
12053
12054 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12055 {
19e08130
AM
12056 unsigned int ent_size = 8;
12057 unsigned int rel_size = sizeof (Elf64_External_Rela);
12058
d0fae19d
AM
12059 ent->got.offset = s->size;
12060 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12061 {
19e08130
AM
12062 ent_size *= 2;
12063 rel_size *= 2;
12064 }
12065 s->size += ent_size;
37da22e5 12066 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12067 {
33e44f2e 12068 htab->elf.irelplt->size += rel_size;
19e08130
AM
12069 htab->got_reli_size += rel_size;
12070 }
6a3858a6
AM
12071 else if (bfd_link_pic (info)
12072 && !((ent->tls_type & TLS_TPREL) != 0
12073 && bfd_link_executable (info)))
19e08130
AM
12074 {
12075 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12076 srel->size += rel_size;
d0fae19d
AM
12077 }
12078 }
927be08e
AM
12079 }
12080 }
12081
12082 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12083
c72f2fb2 12084 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12085 {
12086 struct got_entry *ent;
12087
12088 if (!is_ppc64_elf (ibfd))
12089 continue;
12090
12091 ent = ppc64_tlsld_got (ibfd);
12092 if (!ent->is_indirect
12093 && ent->got.offset != (bfd_vma) -1)
12094 {
12095 asection *s = ppc64_elf_tdata (ibfd)->got;
12096 ent->got.offset = s->size;
12097 s->size += 16;
0e1862bb 12098 if (bfd_link_pic (info))
927be08e
AM
12099 {
12100 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12101 srel->size += sizeof (Elf64_External_Rela);
12102 }
12103 }
12104 }
12105
33e44f2e 12106 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12107 if (!done_something)
c72f2fb2 12108 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12109 {
12110 asection *got;
12111
12112 if (!is_ppc64_elf (ibfd))
12113 continue;
12114
12115 got = ppc64_elf_tdata (ibfd)->got;
12116 if (got != NULL)
12117 {
12118 done_something = got->rawsize != got->size;
12119 if (done_something)
12120 break;
12121 }
12122 }
12123
12124 if (done_something)
e7d1c40c 12125 (*htab->params->layout_sections_again) ();
927be08e
AM
12126
12127 /* Set up for second pass over toc sections to recalculate elf_gp
12128 on input sections. */
12129 htab->toc_bfd = NULL;
12130 htab->toc_first_sec = NULL;
12131 htab->second_toc_pass = TRUE;
12132 return done_something;
12133}
12134
12135/* Called after second pass of multitoc partitioning. */
12136
12137void
12138ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12139{
12140 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12141
12142 /* After the second pass, toc_curr tracks the TOC offset used
12143 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12144 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12145}
12146
9b5ecbd0
AM
12147/* No toc references were found in ISEC. If the code in ISEC makes no
12148 calls, then there's no need to use toc adjusting stubs when branching
12149 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12150 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12151 needed, and 2 if a cyclical call-graph was found but no other reason
12152 for a stub was detected. If called from the top level, a return of
12153 2 means the same as a return of 0. */
9b5ecbd0
AM
12154
12155static int
4ce794b7 12156toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12157{
9b5ecbd0 12158 int ret;
70cc837d
AM
12159
12160 /* Mark this section as checked. */
12161 isec->call_check_done = 1;
9b5ecbd0 12162
772119ce
AM
12163 /* We know none of our code bearing sections will need toc stubs. */
12164 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12165 return 0;
12166
eea6121a 12167 if (isec->size == 0)
082c50f8
AM
12168 return 0;
12169
4c52953f
AM
12170 if (isec->output_section == NULL)
12171 return 0;
12172
4c52953f 12173 ret = 0;
70cc837d 12174 if (isec->reloc_count != 0)
9b5ecbd0 12175 {
70cc837d
AM
12176 Elf_Internal_Rela *relstart, *rel;
12177 Elf_Internal_Sym *local_syms;
12178 struct ppc_link_hash_table *htab;
2917689a 12179
70cc837d
AM
12180 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12181 info->keep_memory);
12182 if (relstart == NULL)
12183 return -1;
90aecf7a 12184
70cc837d
AM
12185 /* Look for branches to outside of this section. */
12186 local_syms = NULL;
12187 htab = ppc_hash_table (info);
12188 if (htab == NULL)
12189 return -1;
4c52953f 12190
70cc837d 12191 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12192 {
70cc837d
AM
12193 enum elf_ppc64_reloc_type r_type;
12194 unsigned long r_symndx;
12195 struct elf_link_hash_entry *h;
12196 struct ppc_link_hash_entry *eh;
12197 Elf_Internal_Sym *sym;
12198 asection *sym_sec;
12199 struct _opd_sec_data *opd;
12200 bfd_vma sym_value;
12201 bfd_vma dest;
12202
12203 r_type = ELF64_R_TYPE (rel->r_info);
12204 if (r_type != R_PPC64_REL24
05d0e962 12205 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
12206 && r_type != R_PPC64_REL14
12207 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 12208 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
12209 && r_type != R_PPC64_PLTCALL
12210 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 12211 continue;
4c52953f 12212
70cc837d
AM
12213 r_symndx = ELF64_R_SYM (rel->r_info);
12214 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12215 isec->owner))
4c52953f 12216 {
70cc837d
AM
12217 ret = -1;
12218 break;
12219 }
4c52953f 12220
70cc837d
AM
12221 /* Calls to dynamic lib functions go through a plt call stub
12222 that uses r2. */
12223 eh = (struct ppc_link_hash_entry *) h;
12224 if (eh != NULL
12225 && (eh->elf.plt.plist != NULL
12226 || (eh->oh != NULL
12227 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12228 {
12229 ret = 1;
12230 break;
4c52953f
AM
12231 }
12232
70cc837d
AM
12233 if (sym_sec == NULL)
12234 /* Ignore other undefined symbols. */
4c52953f 12235 continue;
4c52953f 12236
70cc837d
AM
12237 /* Assume branches to other sections not included in the
12238 link need stubs too, to cover -R and absolute syms. */
12239 if (sym_sec->output_section == NULL)
12240 {
12241 ret = 1;
12242 break;
12243 }
4c52953f 12244
70cc837d
AM
12245 if (h == NULL)
12246 sym_value = sym->st_value;
12247 else
12248 {
12249 if (h->root.type != bfd_link_hash_defined
12250 && h->root.type != bfd_link_hash_defweak)
12251 abort ();
12252 sym_value = h->root.u.def.value;
12253 }
12254 sym_value += rel->r_addend;
4c52953f 12255
70cc837d
AM
12256 /* If this branch reloc uses an opd sym, find the code section. */
12257 opd = get_opd_info (sym_sec);
12258 if (opd != NULL)
12259 {
12260 if (h == NULL && opd->adjust != NULL)
12261 {
12262 long adjust;
4c52953f 12263
92a9c616 12264 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12265 if (adjust == -1)
12266 /* Assume deleted functions won't ever be called. */
12267 continue;
12268 sym_value += adjust;
12269 }
4c52953f 12270
aef36ac1
AM
12271 dest = opd_entry_value (sym_sec, sym_value,
12272 &sym_sec, NULL, FALSE);
70cc837d
AM
12273 if (dest == (bfd_vma) -1)
12274 continue;
12275 }
12276 else
12277 dest = (sym_value
12278 + sym_sec->output_offset
12279 + sym_sec->output_section->vma);
4c52953f 12280
70cc837d
AM
12281 /* Ignore branch to self. */
12282 if (sym_sec == isec)
12283 continue;
4c52953f 12284
70cc837d
AM
12285 /* If the called function uses the toc, we need a stub. */
12286 if (sym_sec->has_toc_reloc
12287 || sym_sec->makes_toc_func_call)
4c52953f 12288 {
70cc837d 12289 ret = 1;
4c52953f
AM
12290 break;
12291 }
70cc837d
AM
12292
12293 /* Assume any branch that needs a long branch stub might in fact
12294 need a plt_branch stub. A plt_branch stub uses r2. */
12295 else if (dest - (isec->output_offset
12296 + isec->output_section->vma
6911b7dc
AM
12297 + rel->r_offset) + (1 << 25)
12298 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12299 ? h->other
12300 : sym->st_other))
4c52953f 12301 {
70cc837d
AM
12302 ret = 1;
12303 break;
12304 }
12305
12306 /* If calling back to a section in the process of being
12307 tested, we can't say for sure that no toc adjusting stubs
12308 are needed, so don't return zero. */
12309 else if (sym_sec->call_check_in_progress)
12310 ret = 2;
12311
12312 /* Branches to another section that itself doesn't have any TOC
12313 references are OK. Recursively call ourselves to check. */
12314 else if (!sym_sec->call_check_done)
12315 {
12316 int recur;
12317
12318 /* Mark current section as indeterminate, so that other
12319 sections that call back to current won't be marked as
12320 known. */
12321 isec->call_check_in_progress = 1;
12322 recur = toc_adjusting_stub_needed (info, sym_sec);
12323 isec->call_check_in_progress = 0;
12324
4c52953f
AM
12325 if (recur != 0)
12326 {
70cc837d
AM
12327 ret = recur;
12328 if (recur != 2)
12329 break;
4c52953f
AM
12330 }
12331 }
4c52953f 12332 }
70cc837d
AM
12333
12334 if (local_syms != NULL
12335 && (elf_symtab_hdr (isec->owner).contents
12336 != (unsigned char *) local_syms))
12337 free (local_syms);
12338 if (elf_section_data (isec)->relocs != relstart)
12339 free (relstart);
9b5ecbd0
AM
12340 }
12341
70cc837d
AM
12342 if ((ret & 1) == 0
12343 && isec->map_head.s != NULL
12344 && (strcmp (isec->output_section->name, ".init") == 0
12345 || strcmp (isec->output_section->name, ".fini") == 0))
12346 {
12347 if (isec->map_head.s->has_toc_reloc
12348 || isec->map_head.s->makes_toc_func_call)
12349 ret = 1;
12350 else if (!isec->map_head.s->call_check_done)
12351 {
12352 int recur;
12353 isec->call_check_in_progress = 1;
12354 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12355 isec->call_check_in_progress = 0;
12356 if (recur != 0)
12357 ret = recur;
12358 }
12359 }
12360
12361 if (ret == 1)
12362 isec->makes_toc_func_call = 1;
4c52953f 12363
9b5ecbd0
AM
12364 return ret;
12365}
12366
721956f4
AM
12367/* The linker repeatedly calls this function for each input section,
12368 in the order that input sections are linked into output sections.
12369 Build lists of input sections to determine groupings between which
12370 we may insert linker stubs. */
12371
9b5ecbd0 12372bfd_boolean
4ce794b7 12373ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12374{
12375 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12376
4dfe6ac6
NC
12377 if (htab == NULL)
12378 return FALSE;
12379
734b6cf9 12380 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12381 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12382 {
3d6f9012
AM
12383 /* This happens to make the list in reverse order,
12384 which is what we want. */
6f20ed8a
AM
12385 htab->sec_info[isec->id].u.list
12386 = htab->sec_info[isec->output_section->id].u.list;
12387 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12388 }
ad8e1ba5 12389
4c52953f 12390 if (htab->multi_toc_needed)
9b5ecbd0 12391 {
8b974ba3
AM
12392 /* Analyse sections that aren't already flagged as needing a
12393 valid toc pointer. Exclude .fixup for the linux kernel.
12394 .fixup contains branches, but only back to the function that
12395 hit an exception. */
12396 if (!(isec->has_toc_reloc
12397 || (isec->flags & SEC_CODE) == 0
12398 || strcmp (isec->name, ".fixup") == 0
12399 || isec->call_check_done))
12400 {
12401 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12402 return FALSE;
8b974ba3
AM
12403 }
12404 /* Make all sections use the TOC assigned for this object file.
12405 This will be wrong for pasted sections; We fix that in
12406 check_pasted_section(). */
12407 if (elf_gp (isec->owner) != 0)
12408 htab->toc_curr = elf_gp (isec->owner);
12409 }
12410
6f20ed8a 12411 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12412 return TRUE;
721956f4
AM
12413}
12414
70cc837d
AM
12415/* Check that all .init and .fini sections use the same toc, if they
12416 have toc relocs. */
12417
12418static bfd_boolean
12419check_pasted_section (struct bfd_link_info *info, const char *name)
12420{
12421 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12422
12423 if (o != NULL)
12424 {
12425 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12426 bfd_vma toc_off = 0;
12427 asection *i;
12428
12429 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12430 if (i->has_toc_reloc)
12431 {
12432 if (toc_off == 0)
6f20ed8a
AM
12433 toc_off = htab->sec_info[i->id].toc_off;
12434 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12435 return FALSE;
12436 }
6683a28d
AM
12437
12438 if (toc_off == 0)
12439 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12440 if (i->makes_toc_func_call)
12441 {
6f20ed8a 12442 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12443 break;
12444 }
12445
70cc837d
AM
12446 /* Make sure the whole pasted function uses the same toc offset. */
12447 if (toc_off != 0)
12448 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12449 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12450 }
12451 return TRUE;
12452}
12453
12454bfd_boolean
12455ppc64_elf_check_init_fini (struct bfd_link_info *info)
12456{
12457 return (check_pasted_section (info, ".init")
12458 & check_pasted_section (info, ".fini"));
12459}
12460
721956f4
AM
12461/* See whether we can group stub sections together. Grouping stub
12462 sections may result in fewer stubs. More importantly, we need to
12463 put all .init* and .fini* stubs at the beginning of the .init or
12464 .fini output sections respectively, because glibc splits the
12465 _init and _fini functions into multiple parts. Putting a stub in
12466 the middle of a function is not a good idea. */
12467
6f20ed8a
AM
12468static bfd_boolean
12469group_sections (struct bfd_link_info *info,
4ce794b7
AM
12470 bfd_size_type stub_group_size,
12471 bfd_boolean stubs_always_before_branch)
721956f4 12472{
6f20ed8a
AM
12473 struct ppc_link_hash_table *htab;
12474 asection *osec;
7c8fe5c4
AM
12475 bfd_boolean suppress_size_errors;
12476
6f20ed8a
AM
12477 htab = ppc_hash_table (info);
12478 if (htab == NULL)
12479 return FALSE;
12480
7c8fe5c4 12481 suppress_size_errors = FALSE;
7c8fe5c4
AM
12482 if (stub_group_size == 1)
12483 {
12484 /* Default values. */
12485 if (stubs_always_before_branch)
09f92717 12486 stub_group_size = 0x1e00000;
7c8fe5c4 12487 else
09f92717 12488 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12489 suppress_size_errors = TRUE;
12490 }
12491
6f20ed8a 12492 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12493 {
6f20ed8a
AM
12494 asection *tail;
12495
12496 if (osec->id >= htab->sec_info_arr_size)
12497 continue;
12498
12499 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12500 while (tail != NULL)
721956f4 12501 {
734b6cf9
AM
12502 asection *curr;
12503 asection *prev;
12504 bfd_size_type total;
12505 bfd_boolean big_sec;
12506 bfd_vma curr_toc;
6f20ed8a 12507 struct map_stub *group;
09f92717 12508 bfd_size_type group_size;
734b6cf9
AM
12509
12510 curr = tail;
eea6121a 12511 total = tail->size;
09f92717
AM
12512 group_size = (ppc64_elf_section_data (tail) != NULL
12513 && ppc64_elf_section_data (tail)->has_14bit_branch
12514 ? stub_group_size >> 10 : stub_group_size);
12515
12516 big_sec = total > group_size;
7c8fe5c4 12517 if (big_sec && !suppress_size_errors)
695344c0 12518 /* xgettext:c-format */
871b3ab2 12519 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 12520 tail->owner, tail);
6f20ed8a 12521 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12522
6f20ed8a 12523 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12524 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12525 < (ppc64_elf_section_data (prev) != NULL
12526 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12527 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12528 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12529 curr = prev;
12530
12531 /* OK, the size from the start of CURR to the end is less
09f92717 12532 than group_size and thus can be handled by one stub
734b6cf9 12533 section. (or the tail section is itself larger than
09f92717
AM
12534 group_size, in which case we may be toast.) We should
12535 really be keeping track of the total size of stubs added
12536 here, as stubs contribute to the final output section
12537 size. That's a little tricky, and this way will only
12538 break if stubs added make the total size more than 2^25,
12539 ie. for the default stub_group_size, if stubs total more
12540 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12541 group = bfd_alloc (curr->owner, sizeof (*group));
12542 if (group == NULL)
12543 return FALSE;
12544 group->link_sec = curr;
12545 group->stub_sec = NULL;
a4b6fadd 12546 group->needs_save_res = 0;
df136d64
AM
12547 group->lr_restore = 0;
12548 group->eh_size = 0;
12549 group->eh_base = 0;
a4b6fadd
AM
12550 group->next = htab->group;
12551 htab->group = group;
734b6cf9 12552 do
721956f4 12553 {
6f20ed8a 12554 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12555 /* Set up this stub group. */
6f20ed8a 12556 htab->sec_info[tail->id].u.group = group;
721956f4 12557 }
734b6cf9
AM
12558 while (tail != curr && (tail = prev) != NULL);
12559
09f92717 12560 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12561 bytes before the stub section can be handled by it too.
12562 Don't do this if we have a really large section after the
12563 stubs, as adding more stubs increases the chance that
12564 branches may not reach into the stub section. */
12565 if (!stubs_always_before_branch && !big_sec)
12566 {
12567 total = 0;
12568 while (prev != NULL
12569 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12570 < (ppc64_elf_section_data (prev) != NULL
12571 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
12572 ? (group_size = stub_group_size >> 10)
12573 : group_size))
6f20ed8a 12574 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12575 {
12576 tail = prev;
6f20ed8a
AM
12577 prev = htab->sec_info[tail->id].u.list;
12578 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12579 }
12580 }
12581 tail = prev;
721956f4
AM
12582 }
12583 }
6f20ed8a 12584 return TRUE;
721956f4
AM
12585}
12586
58d180e8
AM
12587static const unsigned char glink_eh_frame_cie[] =
12588{
12589 0, 0, 0, 16, /* length. */
12590 0, 0, 0, 0, /* id. */
12591 1, /* CIE version. */
12592 'z', 'R', 0, /* Augmentation string. */
12593 4, /* Code alignment. */
12594 0x78, /* Data alignment. */
12595 65, /* RA reg. */
12596 1, /* Augmentation size. */
12597 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12598 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12599};
12600
d969d15f
AM
12601/* Stripping output sections is normally done before dynamic section
12602 symbols have been allocated. This function is called later, and
12603 handles cases like htab->brlt which is mapped to its own output
12604 section. */
12605
12606static void
12607maybe_strip_output (struct bfd_link_info *info, asection *isec)
12608{
12609 if (isec->size == 0
12610 && isec->output_section->size == 0
53d8967a 12611 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12612 && !bfd_section_removed_from_list (info->output_bfd,
12613 isec->output_section)
12614 && elf_section_data (isec->output_section)->dynindx == 0)
12615 {
12616 isec->output_section->flags |= SEC_EXCLUDE;
12617 bfd_section_list_remove (info->output_bfd, isec->output_section);
12618 info->output_bfd->section_count--;
12619 }
12620}
12621
721956f4
AM
12622/* Determine and set the size of the stub section for a final link.
12623
12624 The basic idea here is to examine all the relocations looking for
12625 PC-relative calls to a target that is unreachable with a "bl"
12626 instruction. */
12627
b34976b6 12628bfd_boolean
e7d1c40c 12629ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12630{
12631 bfd_size_type stub_group_size;
b34976b6 12632 bfd_boolean stubs_always_before_branch;
721956f4
AM
12633 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12634
4dfe6ac6
NC
12635 if (htab == NULL)
12636 return FALSE;
12637
0e1862bb 12638 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12639 htab->params->plt_thread_safe = 1;
b9e5796b 12640 if (!htab->opd_abi)
e7d1c40c
AM
12641 htab->params->plt_thread_safe = 0;
12642 else if (htab->params->plt_thread_safe == -1)
794e51c0 12643 {
e2458743 12644 static const char *const thread_starter[] =
794e51c0
AM
12645 {
12646 "pthread_create",
12647 /* libstdc++ */
12648 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12649 /* librt */
12650 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12651 "mq_notify", "create_timer",
12652 /* libanl */
12653 "getaddrinfo_a",
12654 /* libgomp */
2300b5a1 12655 "GOMP_parallel",
794e51c0 12656 "GOMP_parallel_start",
2300b5a1 12657 "GOMP_parallel_loop_static",
794e51c0 12658 "GOMP_parallel_loop_static_start",
2300b5a1 12659 "GOMP_parallel_loop_dynamic",
794e51c0 12660 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12661 "GOMP_parallel_loop_guided",
794e51c0 12662 "GOMP_parallel_loop_guided_start",
2300b5a1 12663 "GOMP_parallel_loop_runtime",
794e51c0 12664 "GOMP_parallel_loop_runtime_start",
2300b5a1 12665 "GOMP_parallel_sections",
68ffbac6 12666 "GOMP_parallel_sections_start",
f9dffbf0
AM
12667 /* libgo */
12668 "__go_go",
794e51c0
AM
12669 };
12670 unsigned i;
12671
a4b6fadd 12672 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12673 {
12674 struct elf_link_hash_entry *h;
12675 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12676 FALSE, FALSE, TRUE);
e7d1c40c
AM
12677 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12678 if (htab->params->plt_thread_safe)
794e51c0
AM
12679 break;
12680 }
12681 }
e7d1c40c
AM
12682 stubs_always_before_branch = htab->params->group_size < 0;
12683 if (htab->params->group_size < 0)
12684 stub_group_size = -htab->params->group_size;
721956f4 12685 else
e7d1c40c 12686 stub_group_size = htab->params->group_size;
721956f4 12687
6f20ed8a
AM
12688 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12689 return FALSE;
721956f4 12690
c9301e31
AM
12691#define STUB_SHRINK_ITER 20
12692 /* Loop until no stubs added. After iteration 20 of this loop we may
12693 exit on a stub section shrinking. This is to break out of a
12694 pathological case where adding stubs on one iteration decreases
12695 section gaps (perhaps due to alignment), which then requires
12696 fewer or smaller stubs on the next iteration. */
12697
721956f4
AM
12698 while (1)
12699 {
12700 bfd *input_bfd;
12701 unsigned int bfd_indx;
a4b6fadd 12702 struct map_stub *group;
721956f4
AM
12703
12704 htab->stub_iteration += 1;
721956f4
AM
12705
12706 for (input_bfd = info->input_bfds, bfd_indx = 0;
12707 input_bfd != NULL;
c72f2fb2 12708 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12709 {
12710 Elf_Internal_Shdr *symtab_hdr;
12711 asection *section;
6cdc0ccc 12712 Elf_Internal_Sym *local_syms = NULL;
721956f4 12713
0c8d6e5c 12714 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12715 continue;
12716
721956f4 12717 /* We'll need the symbol table in a second. */
0ffa91dd 12718 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12719 if (symtab_hdr->sh_info == 0)
12720 continue;
12721
721956f4
AM
12722 /* Walk over each section attached to the input bfd. */
12723 for (section = input_bfd->sections;
12724 section != NULL;
12725 section = section->next)
12726 {
721956f4 12727 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12728
12729 /* If there aren't any relocs, then there's nothing more
12730 to do. */
12731 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12732 || (section->flags & SEC_ALLOC) == 0
12733 || (section->flags & SEC_LOAD) == 0
12734 || (section->flags & SEC_CODE) == 0
721956f4
AM
12735 || section->reloc_count == 0)
12736 continue;
12737
12738 /* If this section is a link-once section that will be
12739 discarded, then don't create any stubs. */
12740 if (section->output_section == NULL
927be08e 12741 || section->output_section->owner != info->output_bfd)
721956f4
AM
12742 continue;
12743
1e2f5b6e
AM
12744 /* Get the relocs. */
12745 internal_relocs
4ce794b7 12746 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12747 info->keep_memory);
721956f4 12748 if (internal_relocs == NULL)
1e2f5b6e 12749 goto error_ret_free_local;
721956f4
AM
12750
12751 /* Now examine each relocation. */
12752 irela = internal_relocs;
12753 irelaend = irela + section->reloc_count;
12754 for (; irela < irelaend; irela++)
12755 {
4ce794b7
AM
12756 enum elf_ppc64_reloc_type r_type;
12757 unsigned int r_indx;
721956f4
AM
12758 enum ppc_stub_type stub_type;
12759 struct ppc_stub_hash_entry *stub_entry;
8387904d 12760 asection *sym_sec, *code_sec;
e054468f 12761 bfd_vma sym_value, code_value;
721956f4 12762 bfd_vma destination;
6911b7dc 12763 unsigned long local_off;
8843416a 12764 bfd_boolean ok_dest;
721956f4 12765 struct ppc_link_hash_entry *hash;
8387904d 12766 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12767 struct elf_link_hash_entry *h;
12768 Elf_Internal_Sym *sym;
721956f4
AM
12769 char *stub_name;
12770 const asection *id_sec;
74f0fb50 12771 struct _opd_sec_data *opd;
e054468f 12772 struct plt_entry *plt_ent;
721956f4
AM
12773
12774 r_type = ELF64_R_TYPE (irela->r_info);
12775 r_indx = ELF64_R_SYM (irela->r_info);
12776
4ce794b7 12777 if (r_type >= R_PPC64_max)
721956f4
AM
12778 {
12779 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12780 goto error_ret_free_internal;
721956f4
AM
12781 }
12782
12783 /* Only look for stubs on branch instructions. */
4ce794b7 12784 if (r_type != R_PPC64_REL24
05d0e962 12785 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
12786 && r_type != R_PPC64_REL14
12787 && r_type != R_PPC64_REL14_BRTAKEN
12788 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12789 continue;
12790
12791 /* Now determine the call target, its name, value,
12792 section. */
411e1bfb
AM
12793 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12794 r_indx, input_bfd))
12795 goto error_ret_free_internal;
12796 hash = (struct ppc_link_hash_entry *) h;
12797
8843416a 12798 ok_dest = FALSE;
8387904d 12799 fdh = NULL;
7fe2b9a6 12800 sym_value = 0;
411e1bfb 12801 if (hash == NULL)
721956f4 12802 {
411e1bfb 12803 sym_value = sym->st_value;
c27b8c2a
AM
12804 if (sym_sec != NULL
12805 && sym_sec->output_section != NULL)
12806 ok_dest = TRUE;
721956f4 12807 }
7fe2b9a6
AM
12808 else if (hash->elf.root.type == bfd_link_hash_defined
12809 || hash->elf.root.type == bfd_link_hash_defweak)
12810 {
12811 sym_value = hash->elf.root.u.def.value;
12812 if (sym_sec->output_section != NULL)
12813 ok_dest = TRUE;
12814 }
12815 else if (hash->elf.root.type == bfd_link_hash_undefweak
12816 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12817 {
99877b66 12818 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12819 use the func descriptor sym instead if it is
12820 defined. */
ceb1f1ef 12821 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12822 && hash->oh != NULL)
8387904d 12823 {
8c5b4e52 12824 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12825 if (fdh->elf.root.type == bfd_link_hash_defined
12826 || fdh->elf.root.type == bfd_link_hash_defweak)
12827 {
12828 sym_sec = fdh->elf.root.u.def.section;
12829 sym_value = fdh->elf.root.u.def.value;
12830 if (sym_sec->output_section != NULL)
12831 ok_dest = TRUE;
12832 }
99877b66
AM
12833 else
12834 fdh = NULL;
8387904d 12835 }
7fe2b9a6
AM
12836 }
12837 else
12838 {
12839 bfd_set_error (bfd_error_bad_value);
12840 goto error_ret_free_internal;
721956f4
AM
12841 }
12842
8843416a 12843 destination = 0;
6911b7dc 12844 local_off = 0;
8843416a
AM
12845 if (ok_dest)
12846 {
12847 sym_value += irela->r_addend;
12848 destination = (sym_value
12849 + sym_sec->output_offset
12850 + sym_sec->output_section->vma);
6911b7dc
AM
12851 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12852 ? hash->elf.other
12853 : sym->st_other);
8843416a
AM
12854 }
12855
8387904d 12856 code_sec = sym_sec;
e054468f 12857 code_value = sym_value;
74f0fb50
AM
12858 opd = get_opd_info (sym_sec);
12859 if (opd != NULL)
8387904d
AM
12860 {
12861 bfd_vma dest;
12862
74f0fb50 12863 if (hash == NULL && opd->adjust != NULL)
8387904d 12864 {
51aecdc5 12865 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12866 if (adjust == -1)
12867 continue;
e054468f 12868 code_value += adjust;
8387904d
AM
12869 sym_value += adjust;
12870 }
12871 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12872 &code_sec, &code_value, FALSE);
8387904d
AM
12873 if (dest != (bfd_vma) -1)
12874 {
12875 destination = dest;
12876 if (fdh != NULL)
12877 {
12878 /* Fixup old ABI sym to point at code
12879 entry. */
99877b66 12880 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12881 hash->elf.root.u.def.section = code_sec;
e054468f 12882 hash->elf.root.u.def.value = code_value;
8387904d
AM
12883 }
12884 }
12885 }
12886
721956f4 12887 /* Determine what (if any) linker stub is needed. */
e054468f 12888 plt_ent = NULL;
721956f4 12889 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12890 &plt_ent, destination,
12891 local_off);
ad8e1ba5 12892
05d0e962
AM
12893 if (r_type == R_PPC64_REL24_NOTOC)
12894 {
12895 if (stub_type == ppc_stub_plt_call)
12896 stub_type = ppc_stub_plt_call_notoc;
12897 else if (stub_type == ppc_stub_long_branch
12898 || (code_sec != NULL
12899 && code_sec->output_section != NULL
12900 && (((hash ? hash->elf.other : sym->st_other)
12901 & STO_PPC64_LOCAL_MASK)
4a4e7361 12902 > 1 << STO_PPC64_LOCAL_BIT)))
05d0e962
AM
12903 stub_type = ppc_stub_long_branch_notoc;
12904 }
12905 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
12906 {
12907 /* Check whether we need a TOC adjusting stub.
12908 Since the linker pastes together pieces from
12909 different object files when creating the
12910 _init and _fini functions, it may be that a
12911 call to what looks like a local sym is in
12912 fact a call needing a TOC adjustment. */
33cb30a1
AM
12913 if ((code_sec != NULL
12914 && code_sec->output_section != NULL
12915 && (htab->sec_info[code_sec->id].toc_off
12916 != htab->sec_info[section->id].toc_off)
12917 && (code_sec->has_toc_reloc
12918 || code_sec->makes_toc_func_call))
12919 || (((hash ? hash->elf.other : sym->st_other)
12920 & STO_PPC64_LOCAL_MASK)
12921 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
12922 stub_type = ppc_stub_long_branch_r2off;
12923 }
12924
721956f4
AM
12925 if (stub_type == ppc_stub_none)
12926 continue;
12927
411e1bfb
AM
12928 /* __tls_get_addr calls might be eliminated. */
12929 if (stub_type != ppc_stub_plt_call
05d0e962 12930 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 12931 && hash != NULL
8387904d
AM
12932 && (hash == htab->tls_get_addr
12933 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12934 && section->has_tls_reloc
12935 && irela != internal_relocs)
12936 {
12937 /* Get tls info. */
f961d9dd 12938 unsigned char *tls_mask;
411e1bfb 12939
3a71aa26 12940 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12941 irela - 1, input_bfd))
12942 goto error_ret_free_internal;
37da22e5 12943 if ((*tls_mask & TLS_TLS) != 0)
411e1bfb
AM
12944 continue;
12945 }
12946
f378ab09 12947 if (stub_type == ppc_stub_plt_call)
794e51c0 12948 {
6e1816be
AM
12949 if (!htab->opd_abi
12950 && htab->params->plt_localentry0 != 0
12951 && is_elfv2_localentry0 (&hash->elf))
12952 htab->has_plt_localentry0 = 1;
12953 else if (irela + 1 < irelaend
12954 && irela[1].r_offset == irela->r_offset + 4
12955 && (ELF64_R_TYPE (irela[1].r_info)
12956 == R_PPC64_TOCSAVE))
f378ab09
AM
12957 {
12958 if (!tocsave_find (htab, INSERT,
12959 &local_syms, irela + 1, input_bfd))
12960 goto error_ret_free_internal;
12961 }
f378ab09
AM
12962 else
12963 stub_type = ppc_stub_plt_call_r2save;
794e51c0 12964 }
3b421ab3 12965
721956f4 12966 /* Support for grouping stub sections. */
6f20ed8a 12967 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12968
12969 /* Get the name of this stub. */
12970 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12971 if (!stub_name)
12972 goto error_ret_free_internal;
12973
12974 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12975 stub_name, FALSE, FALSE);
721956f4
AM
12976 if (stub_entry != NULL)
12977 {
05d0e962
AM
12978 enum ppc_stub_type old_type;
12979 /* A stub has already been created, but it may
12980 not be the required type. We shouldn't be
12981 transitioning from plt_call to long_branch
12982 stubs or vice versa, but we might be
12983 upgrading from plt_call to plt_call_r2save or
12984 from long_branch to long_branch_r2off. */
721956f4 12985 free (stub_name);
05d0e962
AM
12986 old_type = stub_entry->stub_type;
12987 switch (old_type)
12988 {
12989 default:
12990 abort ();
12991
12992 case ppc_stub_save_res:
12993 continue;
12994
12995 case ppc_stub_plt_call:
12996 case ppc_stub_plt_call_r2save:
12997 case ppc_stub_plt_call_notoc:
12998 case ppc_stub_plt_call_both:
12999 if (stub_type == ppc_stub_plt_call)
13000 continue;
13001 else if (stub_type == ppc_stub_plt_call_r2save)
13002 {
13003 if (old_type == ppc_stub_plt_call_notoc)
13004 stub_type = ppc_stub_plt_call_both;
13005 }
13006 else if (stub_type == ppc_stub_plt_call_notoc)
13007 {
13008 if (old_type == ppc_stub_plt_call_r2save)
13009 stub_type = ppc_stub_plt_call_both;
13010 }
13011 else
13012 abort ();
13013 break;
13014
13015 case ppc_stub_plt_branch:
13016 case ppc_stub_plt_branch_r2off:
13017 case ppc_stub_plt_branch_notoc:
13018 case ppc_stub_plt_branch_both:
13019 old_type += (ppc_stub_long_branch
13020 - ppc_stub_plt_branch);
13021 /* Fall through. */
13022 case ppc_stub_long_branch:
13023 case ppc_stub_long_branch_r2off:
13024 case ppc_stub_long_branch_notoc:
13025 case ppc_stub_long_branch_both:
13026 if (stub_type == ppc_stub_long_branch)
13027 continue;
13028 else if (stub_type == ppc_stub_long_branch_r2off)
13029 {
13030 if (old_type == ppc_stub_long_branch_notoc)
13031 stub_type = ppc_stub_long_branch_both;
13032 }
13033 else if (stub_type == ppc_stub_long_branch_notoc)
13034 {
13035 if (old_type == ppc_stub_long_branch_r2off)
13036 stub_type = ppc_stub_long_branch_both;
13037 }
13038 else
13039 abort ();
13040 break;
13041 }
13042 if (old_type < stub_type)
794e51c0 13043 stub_entry->stub_type = stub_type;
721956f4
AM
13044 continue;
13045 }
13046
25f53a85 13047 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
13048 if (stub_entry == NULL)
13049 {
13050 free (stub_name);
6cdc0ccc
AM
13051 error_ret_free_internal:
13052 if (elf_section_data (section)->relocs == NULL)
13053 free (internal_relocs);
13054 error_ret_free_local:
13055 if (local_syms != NULL
13056 && (symtab_hdr->contents
13057 != (unsigned char *) local_syms))
13058 free (local_syms);
b34976b6 13059 return FALSE;
721956f4
AM
13060 }
13061
ad8e1ba5 13062 stub_entry->stub_type = stub_type;
05d0e962
AM
13063 if (stub_type >= ppc_stub_plt_call
13064 && stub_type <= ppc_stub_plt_call_both)
e054468f 13065 {
05d0e962
AM
13066 stub_entry->target_value = sym_value;
13067 stub_entry->target_section = sym_sec;
e054468f
AM
13068 }
13069 else
13070 {
05d0e962
AM
13071 stub_entry->target_value = code_value;
13072 stub_entry->target_section = code_sec;
e054468f 13073 }
721956f4 13074 stub_entry->h = hash;
e054468f 13075 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
13076 stub_entry->symtype
13077 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 13078 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 13079
3d58e1fc
AM
13080 if (hash != NULL
13081 && (hash->elf.root.type == bfd_link_hash_defined
13082 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 13083 htab->stub_globals += 1;
721956f4
AM
13084 }
13085
13086 /* We're done with the internal relocs, free them. */
6cdc0ccc 13087 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 13088 free (internal_relocs);
721956f4 13089 }
6cdc0ccc
AM
13090
13091 if (local_syms != NULL
13092 && symtab_hdr->contents != (unsigned char *) local_syms)
13093 {
13094 if (!info->keep_memory)
13095 free (local_syms);
13096 else
13097 symtab_hdr->contents = (unsigned char *) local_syms;
13098 }
721956f4
AM
13099 }
13100
5c3dead3 13101 /* We may have added some stubs. Find out the new size of the
721956f4 13102 stub sections. */
d4aaa2a0 13103 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13104 {
13105 group->lr_restore = 0;
13106 group->eh_size = 0;
13107 if (group->stub_sec != NULL)
13108 {
13109 asection *stub_sec = group->stub_sec;
13110
13111 if (htab->stub_iteration <= STUB_SHRINK_ITER
13112 || stub_sec->rawsize < stub_sec->size)
13113 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13114 stub_sec->rawsize = stub_sec->size;
13115 stub_sec->size = 0;
13116 stub_sec->reloc_count = 0;
13117 stub_sec->flags &= ~SEC_RELOC;
13118 }
13119 }
eea6121a 13120
ba21f564
AM
13121 if (htab->stub_iteration <= STUB_SHRINK_ITER
13122 || htab->brlt->rawsize < htab->brlt->size)
13123 htab->brlt->rawsize = htab->brlt->size;
eea6121a 13124 htab->brlt->size = 0;
84f5d08e
AM
13125 htab->brlt->reloc_count = 0;
13126 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 13127 if (htab->relbrlt != NULL)
eea6121a 13128 htab->relbrlt->size = 0;
721956f4 13129
63bc6f6c 13130 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 13131
a4b6fadd
AM
13132 for (group = htab->group; group != NULL; group = group->next)
13133 if (group->needs_save_res)
13134 group->stub_sec->size += htab->sfpr->size;
13135
176a0d42
AM
13136 if (info->emitrelocations
13137 && htab->glink != NULL && htab->glink->size != 0)
13138 {
13139 htab->glink->reloc_count = 1;
13140 htab->glink->flags |= SEC_RELOC;
13141 }
13142
58d180e8
AM
13143 if (htab->glink_eh_frame != NULL
13144 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 13145 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 13146 {
2e0ce1c8 13147 size_t size = 0, align = 4;
58d180e8 13148
d4aaa2a0 13149 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13150 if (group->eh_size != 0)
13151 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 13152 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 13153 size += (24 + align - 1) & -align;
58d180e8 13154 if (size != 0)
2e0ce1c8
AM
13155 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13156 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13157 size = (size + align - 1) & -align;
58d180e8
AM
13158 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13159 htab->glink_eh_frame->size = size;
13160 }
13161
e7d1c40c 13162 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13163 for (group = htab->group; group != NULL; group = group->next)
13164 if (group->stub_sec != NULL)
691d2e9a
AM
13165 {
13166 int align = abs (htab->params->plt_stub_align);
13167 group->stub_sec->size
13168 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13169 }
d4aaa2a0
AM
13170
13171 for (group = htab->group; group != NULL; group = group->next)
13172 if (group->stub_sec != NULL
13173 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 13174 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 13175 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
13176 break;
13177
d4aaa2a0 13178 if (group == NULL
ba21f564
AM
13179 && (htab->brlt->rawsize == htab->brlt->size
13180 || (htab->stub_iteration > STUB_SHRINK_ITER
13181 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
13182 && (htab->glink_eh_frame == NULL
13183 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
13184 break;
13185
721956f4 13186 /* Ask the linker to do its stuff. */
e7d1c40c 13187 (*htab->params->layout_sections_again) ();
721956f4
AM
13188 }
13189
da44f4e5
AM
13190 if (htab->glink_eh_frame != NULL
13191 && htab->glink_eh_frame->size != 0)
13192 {
13193 bfd_vma val;
13194 bfd_byte *p, *last_fde;
13195 size_t last_fde_len, size, align, pad;
d4aaa2a0 13196 struct map_stub *group;
da44f4e5 13197
df136d64
AM
13198 /* It is necessary to at least have a rough outline of the
13199 linker generated CIEs and FDEs written before
13200 bfd_elf_discard_info is run, in order for these FDEs to be
13201 indexed in .eh_frame_hdr. */
da44f4e5
AM
13202 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13203 if (p == NULL)
13204 return FALSE;
13205 htab->glink_eh_frame->contents = p;
13206 last_fde = p;
2e0ce1c8 13207 align = 4;
da44f4e5
AM
13208
13209 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13210 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 13211 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 13212 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 13213 p += last_fde_len + 4;
da44f4e5 13214
d4aaa2a0 13215 for (group = htab->group; group != NULL; group = group->next)
df136d64 13216 if (group->eh_size != 0)
da44f4e5 13217 {
df136d64 13218 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 13219 last_fde = p;
df136d64 13220 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 13221 /* FDE length. */
2e0ce1c8 13222 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13223 p += 4;
13224 /* CIE pointer. */
13225 val = p - htab->glink_eh_frame->contents;
13226 bfd_put_32 (htab->elf.dynobj, val, p);
13227 p += 4;
13228 /* Offset to stub section, written later. */
13229 p += 4;
13230 /* stub section size. */
d4aaa2a0 13231 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
13232 p += 4;
13233 /* Augmentation. */
13234 p += 1;
df136d64
AM
13235 /* Make sure we don't have all nops. This is enough for
13236 elf-eh-frame.c to detect the last non-nop opcode. */
13237 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 13238 p = last_fde + last_fde_len + 4;
da44f4e5
AM
13239 }
13240 if (htab->glink != NULL && htab->glink->size != 0)
13241 {
13242 last_fde = p;
2e0ce1c8 13243 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13244 /* FDE length. */
2e0ce1c8 13245 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13246 p += 4;
13247 /* CIE pointer. */
13248 val = p - htab->glink_eh_frame->contents;
13249 bfd_put_32 (htab->elf.dynobj, val, p);
13250 p += 4;
13251 /* Offset to .glink, written later. */
13252 p += 4;
13253 /* .glink size. */
13254 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13255 p += 4;
13256 /* Augmentation. */
13257 p += 1;
13258
13259 *p++ = DW_CFA_advance_loc + 1;
13260 *p++ = DW_CFA_register;
13261 *p++ = 65;
9f08fa5c 13262 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 13263 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
13264 *p++ = DW_CFA_restore_extended;
13265 *p++ = 65;
2e0ce1c8 13266 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13267 }
13268 /* Subsume any padding into the last FDE if user .eh_frame
13269 sections are aligned more than glink_eh_frame. Otherwise any
13270 zero padding will be seen as a terminator. */
2e0ce1c8 13271 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13272 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13273 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13274 htab->glink_eh_frame->size = size + pad;
13275 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13276 }
13277
d969d15f
AM
13278 maybe_strip_output (info, htab->brlt);
13279 if (htab->glink_eh_frame != NULL)
13280 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13281
b34976b6 13282 return TRUE;
721956f4
AM
13283}
13284
13285/* Called after we have determined section placement. If sections
805fc799 13286 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13287
805fc799 13288bfd_vma
1c865ab2 13289ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13290{
805fc799 13291 asection *s;
a27e685f 13292 bfd_vma TOCstart, adjust;
721956f4 13293
43417696
AM
13294 if (info != NULL)
13295 {
13296 struct elf_link_hash_entry *h;
13297 struct elf_link_hash_table *htab = elf_hash_table (info);
13298
13299 if (is_elf_hash_table (htab)
13300 && htab->hgot != NULL)
13301 h = htab->hgot;
13302 else
13303 {
13304 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13305 if (is_elf_hash_table (htab))
13306 htab->hgot = h;
13307 }
13308 if (h != NULL
13309 && h->root.type == bfd_link_hash_defined
13310 && !h->root.linker_def
13311 && (!is_elf_hash_table (htab)
13312 || h->def_regular))
13313 {
13314 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13315 + h->root.u.def.section->output_offset
13316 + h->root.u.def.section->output_section->vma);
13317 _bfd_set_gp_value (obfd, TOCstart);
13318 return TOCstart;
13319 }
13320 }
13321
805fc799
AM
13322 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13323 order. The TOC starts where the first of these sections starts. */
13324 s = bfd_get_section_by_name (obfd, ".got");
e054468f 13325 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13326 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 13327 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13328 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 13329 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13330 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 13331 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
13332 {
13333 /* This may happen for
13334 o references to TOC base (SYM@toc / TOC[tc0]) without a
13335 .toc directive
13336 o bad linker script
13337 o --gc-sections and empty TOC sections
13338
13339 FIXME: Warn user? */
13340
13341 /* Look for a likely section. We probably won't even be
13342 using TOCstart. */
13343 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13344 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13345 | SEC_EXCLUDE))
805fc799
AM
13346 == (SEC_ALLOC | SEC_SMALL_DATA))
13347 break;
721956f4 13348 if (s == NULL)
805fc799 13349 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13350 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
13351 == (SEC_ALLOC | SEC_SMALL_DATA))
13352 break;
721956f4 13353 if (s == NULL)
805fc799 13354 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13355 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13356 == SEC_ALLOC)
805fc799 13357 break;
721956f4 13358 if (s == NULL)
805fc799 13359 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13360 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
13361 break;
13362 }
721956f4 13363
805fc799
AM
13364 TOCstart = 0;
13365 if (s != NULL)
13366 TOCstart = s->output_section->vma + s->output_offset;
721956f4 13367
a27e685f
AM
13368 /* Force alignment. */
13369 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13370 TOCstart -= adjust;
1c865ab2
AM
13371 _bfd_set_gp_value (obfd, TOCstart);
13372
810d4e75 13373 if (info != NULL && s != NULL)
1c865ab2
AM
13374 {
13375 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13376
810d4e75
AM
13377 if (htab != NULL)
13378 {
13379 if (htab->elf.hgot != NULL)
13380 {
a27e685f 13381 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
13382 htab->elf.hgot->root.u.def.section = s;
13383 }
13384 }
13385 else
1c865ab2 13386 {
810d4e75
AM
13387 struct bfd_link_hash_entry *bh = NULL;
13388 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
13389 s, TOC_BASE_OFF - adjust,
13390 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
13391 }
13392 }
805fc799 13393 return TOCstart;
721956f4
AM
13394}
13395
a345bc8d 13396/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 13397 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
13398
13399static bfd_boolean
49c09209 13400build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
13401{
13402 struct bfd_link_info *info;
13403 struct ppc_link_hash_table *htab;
49c09209 13404 struct plt_entry *ent;
a345bc8d
AM
13405 asection *s;
13406
13407 if (h->root.type == bfd_link_hash_indirect)
13408 return TRUE;
13409
49c09209
AM
13410 info = inf;
13411 htab = ppc_hash_table (info);
13412 if (htab == NULL)
13413 return FALSE;
13414
13415 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13416 if (ent->plt.offset != (bfd_vma) -1)
13417 {
13418 /* This symbol has an entry in the procedure linkage
13419 table. Set it up. */
13420 Elf_Internal_Rela rela;
2d7ad24e 13421 asection *plt, *relplt;
49c09209
AM
13422 bfd_byte *loc;
13423
13424 if (!htab->elf.dynamic_sections_created
13425 || h->dynindx == -1)
13426 {
13427 if (!(h->def_regular
13428 && (h->root.type == bfd_link_hash_defined
13429 || h->root.type == bfd_link_hash_defweak)))
13430 continue;
2d7ad24e
AM
13431 if (h->type == STT_GNU_IFUNC)
13432 {
13433 plt = htab->elf.iplt;
13434 relplt = htab->elf.irelplt;
13435 htab->local_ifunc_resolver = 1;
13436 if (htab->opd_abi)
13437 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13438 else
13439 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13440 }
49c09209 13441 else
2d7ad24e
AM
13442 {
13443 plt = htab->pltlocal;
13444 if (bfd_link_pic (info))
13445 {
13446 relplt = htab->relpltlocal;
13447 if (htab->opd_abi)
13448 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13449 else
13450 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13451 }
13452 else
13453 relplt = NULL;
13454 }
49c09209
AM
13455 rela.r_addend = (h->root.u.def.value
13456 + h->root.u.def.section->output_offset
13457 + h->root.u.def.section->output_section->vma
13458 + ent->addend);
2d7ad24e
AM
13459
13460 if (relplt == NULL)
13461 {
13462 loc = plt->contents + ent->plt.offset;
13463 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13464 if (htab->opd_abi)
13465 {
13466 bfd_vma toc = elf_gp (info->output_bfd);
13467 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13468 bfd_put_64 (info->output_bfd, toc, loc + 8);
13469 }
13470 }
13471 else
13472 {
13473 rela.r_offset = (plt->output_section->vma
13474 + plt->output_offset
13475 + ent->plt.offset);
13476 loc = relplt->contents + (relplt->reloc_count++
13477 * sizeof (Elf64_External_Rela));
13478 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13479 }
49c09209
AM
13480 }
13481 else
13482 {
13483 rela.r_offset = (htab->elf.splt->output_section->vma
13484 + htab->elf.splt->output_offset
13485 + ent->plt.offset);
13486 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13487 rela.r_addend = ent->addend;
13488 loc = (htab->elf.srelplt->contents
13489 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13490 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13491 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13492 htab->maybe_local_ifunc_resolver = 1;
2d7ad24e 13493 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 13494 }
49c09209
AM
13495 }
13496
a345bc8d
AM
13497 if (!h->pointer_equality_needed)
13498 return TRUE;
13499
13500 if (h->def_regular)
13501 return TRUE;
13502
9e390558 13503 s = htab->global_entry;
49c09209
AM
13504 if (s == NULL || s->size == 0)
13505 return TRUE;
13506
13507 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13508 if (ent->plt.offset != (bfd_vma) -1
13509 && ent->addend == 0)
a345bc8d
AM
13510 {
13511 bfd_byte *p;
13512 asection *plt;
13513 bfd_vma off;
13514
a345bc8d 13515 p = s->contents + h->root.u.def.value;
33e44f2e 13516 plt = htab->elf.splt;
a345bc8d
AM
13517 if (!htab->elf.dynamic_sections_created
13518 || h->dynindx == -1)
2d7ad24e
AM
13519 {
13520 if (h->type == STT_GNU_IFUNC)
13521 plt = htab->elf.iplt;
13522 else
13523 plt = htab->pltlocal;
13524 }
49c09209 13525 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
13526 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13527
13528 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13529 {
13530 info->callbacks->einfo
c1c8c1ef 13531 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
13532 h->root.root.string);
13533 bfd_set_error (bfd_error_bad_value);
13534 htab->stub_error = TRUE;
13535 }
13536
7341d5e2
AM
13537 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13538 if (htab->params->emit_stub_syms)
13539 {
13540 size_t len = strlen (h->root.root.string);
13541 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13542
13543 if (name == NULL)
13544 return FALSE;
13545
13546 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13547 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13548 if (h == NULL)
13549 return FALSE;
13550 if (h->root.type == bfd_link_hash_new)
13551 {
13552 h->root.type = bfd_link_hash_defined;
13553 h->root.u.def.section = s;
13554 h->root.u.def.value = p - s->contents;
13555 h->ref_regular = 1;
13556 h->def_regular = 1;
13557 h->ref_regular_nonweak = 1;
13558 h->forced_local = 1;
13559 h->non_elf = 0;
2ec55de3 13560 h->root.linker_def = 1;
7341d5e2
AM
13561 }
13562 }
13563
a345bc8d
AM
13564 if (PPC_HA (off) != 0)
13565 {
13566 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13567 p += 4;
13568 }
13569 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13570 p += 4;
13571 bfd_put_32 (s->owner, MTCTR_R12, p);
13572 p += 4;
407aa07c 13573 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
13574 break;
13575 }
13576 return TRUE;
13577}
13578
49c09209
AM
13579/* Write PLT relocs for locals. */
13580
13581static bfd_boolean
13582write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13583{
13584 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13585 bfd *ibfd;
13586
13587 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13588 {
13589 struct got_entry **lgot_ents, **end_lgot_ents;
13590 struct plt_entry **local_plt, **lplt, **end_local_plt;
13591 Elf_Internal_Shdr *symtab_hdr;
13592 bfd_size_type locsymcount;
13593 Elf_Internal_Sym *local_syms = NULL;
13594 struct plt_entry *ent;
13595
13596 if (!is_ppc64_elf (ibfd))
13597 continue;
13598
13599 lgot_ents = elf_local_got_ents (ibfd);
13600 if (!lgot_ents)
13601 continue;
13602
13603 symtab_hdr = &elf_symtab_hdr (ibfd);
13604 locsymcount = symtab_hdr->sh_info;
13605 end_lgot_ents = lgot_ents + locsymcount;
13606 local_plt = (struct plt_entry **) end_lgot_ents;
13607 end_local_plt = local_plt + locsymcount;
13608 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13609 for (ent = *lplt; ent != NULL; ent = ent->next)
13610 if (ent->plt.offset != (bfd_vma) -1)
13611 {
13612 Elf_Internal_Sym *sym;
13613 asection *sym_sec;
13614 asection *plt, *relplt;
13615 bfd_byte *loc;
13616 bfd_vma val;
49c09209
AM
13617
13618 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13619 lplt - local_plt, ibfd))
13620 {
13621 if (local_syms != NULL
13622 && symtab_hdr->contents != (unsigned char *) local_syms)
13623 free (local_syms);
13624 return FALSE;
13625 }
13626
13627 val = sym->st_value + ent->addend;
ba85c15d
AM
13628 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13629 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
49c09209
AM
13630 if (sym_sec != NULL && sym_sec->output_section != NULL)
13631 val += sym_sec->output_offset + sym_sec->output_section->vma;
13632
2d7ad24e
AM
13633 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13634 {
13635 htab->local_ifunc_resolver = 1;
13636 plt = htab->elf.iplt;
13637 relplt = htab->elf.irelplt;
13638 }
13639 else
13640 {
13641 plt = htab->pltlocal;
13642 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13643 }
49c09209 13644
2d7ad24e
AM
13645 if (relplt == NULL)
13646 {
13647 loc = plt->contents + ent->plt.offset;
13648 bfd_put_64 (info->output_bfd, val, loc);
13649 if (htab->opd_abi)
13650 {
13651 bfd_vma toc = elf_gp (ibfd);
13652 bfd_put_64 (info->output_bfd, toc, loc + 8);
13653 }
13654 }
49c09209 13655 else
2d7ad24e
AM
13656 {
13657 Elf_Internal_Rela rela;
13658 rela.r_offset = (ent->plt.offset
13659 + plt->output_offset
13660 + plt->output_section->vma);
13661 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13662 {
13663 if (htab->opd_abi)
13664 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13665 else
13666 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13667 }
13668 else
13669 {
13670 if (htab->opd_abi)
13671 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13672 else
13673 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13674 }
13675 rela.r_addend = val;
13676 loc = relplt->contents + (relplt->reloc_count++
13677 * sizeof (Elf64_External_Rela));
13678 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13679 }
49c09209
AM
13680 }
13681
13682 if (local_syms != NULL
13683 && symtab_hdr->contents != (unsigned char *) local_syms)
13684 {
13685 if (!info->keep_memory)
13686 free (local_syms);
13687 else
13688 symtab_hdr->contents = (unsigned char *) local_syms;
13689 }
13690 }
13691 return TRUE;
13692}
13693
721956f4
AM
13694/* Build all the stubs associated with the current output file.
13695 The stubs are kept in a hash table attached to the main linker
13696 hash table. This function is called via gldelf64ppc_finish. */
13697
b34976b6 13698bfd_boolean
e7d1c40c 13699ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13700 char **stats)
5d1634d7
AM
13701{
13702 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13703 struct map_stub *group;
721956f4 13704 asection *stub_sec;
5d1634d7 13705 bfd_byte *p;
e717da7e 13706 int stub_sec_count = 0;
5d1634d7 13707
4dfe6ac6
NC
13708 if (htab == NULL)
13709 return FALSE;
13710
eea6121a 13711 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 13712 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13713 {
13714 group->eh_size = 0;
13715 group->lr_restore = 0;
13716 if ((stub_sec = group->stub_sec) != NULL
13717 && stub_sec->size != 0)
13718 {
13719 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13720 stub_sec->size);
13721 if (stub_sec->contents == NULL)
13722 return FALSE;
13723 stub_sec->size = 0;
13724 }
13725 }
5d1634d7 13726
23eb7e01 13727 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13728 {
9f951329 13729 unsigned int indx;
ad8e1ba5 13730 bfd_vma plt0;
9f951329 13731
721956f4 13732 /* Build the .glink plt call stub. */
e7d1c40c 13733 if (htab->params->emit_stub_syms)
97b639ba
AM
13734 {
13735 struct elf_link_hash_entry *h;
468392fb
AM
13736 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13737 TRUE, FALSE, FALSE);
97b639ba
AM
13738 if (h == NULL)
13739 return FALSE;
13740 if (h->root.type == bfd_link_hash_new)
13741 {
13742 h->root.type = bfd_link_hash_defined;
13743 h->root.u.def.section = htab->glink;
ee4bf8d2 13744 h->root.u.def.value = 8;
f5385ebf
AM
13745 h->ref_regular = 1;
13746 h->def_regular = 1;
13747 h->ref_regular_nonweak = 1;
13748 h->forced_local = 1;
13749 h->non_elf = 0;
2ec55de3 13750 h->root.linker_def = 1;
97b639ba
AM
13751 }
13752 }
33e44f2e
AM
13753 plt0 = (htab->elf.splt->output_section->vma
13754 + htab->elf.splt->output_offset
13755 - 16);
176a0d42
AM
13756 if (info->emitrelocations)
13757 {
13758 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13759 if (r == NULL)
13760 return FALSE;
13761 r->r_offset = (htab->glink->output_offset
13762 + htab->glink->output_section->vma);
13763 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13764 r->r_addend = plt0;
13765 }
4ce794b7 13766 p = htab->glink->contents;
176a0d42 13767 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13768 bfd_put_64 (htab->glink->owner, plt0, p);
13769 p += 8;
b9e5796b
AM
13770 if (htab->opd_abi)
13771 {
13772 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13773 p += 4;
13774 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13775 p += 4;
13776 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13777 p += 4;
13778 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13779 p += 4;
13780 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13781 p += 4;
13782 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13783 p += 4;
13784 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13785 p += 4;
13786 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13787 p += 4;
13788 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13789 p += 4;
13790 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13791 p += 4;
13792 }
13793 else
13794 {
13795 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13796 p += 4;
13797 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13798 p += 4;
13799 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13800 p += 4;
f378ab09
AM
13801 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13802 p += 4;
b9e5796b
AM
13803 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13804 p += 4;
13805 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13806 p += 4;
13807 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13808 p += 4;
13809 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13810 p += 4;
13811 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13812 p += 4;
13813 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13814 p += 4;
13815 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13816 p += 4;
13817 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13818 p += 4;
13819 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13820 p += 4;
13821 }
407aa07c
AM
13822 bfd_put_32 (htab->glink->owner, BCTR, p);
13823 p += 4;
c75bc4f7 13824 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 13825
9f951329
AM
13826 /* Build the .glink lazy link call stubs. */
13827 indx = 0;
9e390558 13828 while (p < htab->glink->contents + htab->glink->size)
9f951329 13829 {
b9e5796b 13830 if (htab->opd_abi)
9f951329 13831 {
b9e5796b
AM
13832 if (indx < 0x8000)
13833 {
13834 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13835 p += 4;
13836 }
13837 else
13838 {
13839 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13840 p += 4;
13841 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13842 p);
13843 p += 4;
13844 }
9f951329 13845 }
4ce794b7 13846 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13847 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13848 indx++;
9f951329
AM
13849 p += 4;
13850 }
5d1634d7 13851 }
5d1634d7 13852
49c09209
AM
13853 /* Build .glink global entry stubs, and PLT relocs for globals. */
13854 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13855
13856 if (!write_plt_relocs_for_local_syms (info))
13857 return FALSE;
9e390558 13858
7341d5e2 13859 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13860 {
4ce794b7 13861 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13862 htab->brlt->size);
4ce794b7 13863 if (htab->brlt->contents == NULL)
b34976b6 13864 return FALSE;
721956f4 13865 }
ee75fd95 13866 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13867 {
13868 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13869 htab->relbrlt->size);
63bc6f6c
AM
13870 if (htab->relbrlt->contents == NULL)
13871 return FALSE;
13872 }
5d1634d7 13873
721956f4
AM
13874 /* Build the stubs as directed by the stub hash table. */
13875 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13876
a4b6fadd
AM
13877 for (group = htab->group; group != NULL; group = group->next)
13878 if (group->needs_save_res)
7dda8d3c 13879 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 13880
aa8a7074
AM
13881 if (htab->relbrlt != NULL)
13882 htab->relbrlt->reloc_count = 0;
13883
e7d1c40c 13884 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13885 for (group = htab->group; group != NULL; group = group->next)
13886 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
13887 {
13888 int align = abs (htab->params->plt_stub_align);
13889 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13890 }
794e51c0 13891
7dda8d3c
AM
13892 for (group = htab->group; group != NULL; group = group->next)
13893 if (group->needs_save_res)
13894 {
13895 stub_sec = group->stub_sec;
13896 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13897 htab->sfpr->contents, htab->sfpr->size);
13898 if (htab->params->emit_stub_syms)
13899 {
13900 unsigned int i;
13901
13902 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13903 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13904 return FALSE;
13905 }
13906 }
13907
df136d64
AM
13908 if (htab->glink_eh_frame != NULL
13909 && htab->glink_eh_frame->size != 0)
13910 {
13911 bfd_vma val;
13912 size_t align = 4;
13913
13914 p = htab->glink_eh_frame->contents;
13915 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13916
13917 for (group = htab->group; group != NULL; group = group->next)
13918 if (group->eh_size != 0)
13919 {
13920 /* Offset to stub section. */
13921 val = (group->stub_sec->output_section->vma
13922 + group->stub_sec->output_offset);
13923 val -= (htab->glink_eh_frame->output_section->vma
13924 + htab->glink_eh_frame->output_offset
13925 + (p + 8 - htab->glink_eh_frame->contents));
13926 if (val + 0x80000000 > 0xffffffff)
13927 {
13928 _bfd_error_handler
13929 (_("%s offset too large for .eh_frame sdata4 encoding"),
13930 group->stub_sec->name);
13931 return FALSE;
13932 }
13933 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13934 p += (group->eh_size + 17 + 3) & -4;
13935 }
13936 if (htab->glink != NULL && htab->glink->size != 0)
13937 {
13938 /* Offset to .glink. */
13939 val = (htab->glink->output_section->vma
13940 + htab->glink->output_offset
13941 + 8);
13942 val -= (htab->glink_eh_frame->output_section->vma
13943 + htab->glink_eh_frame->output_offset
13944 + (p + 8 - htab->glink_eh_frame->contents));
13945 if (val + 0x80000000 > 0xffffffff)
13946 {
13947 _bfd_error_handler
13948 (_("%s offset too large for .eh_frame sdata4 encoding"),
13949 htab->glink->name);
13950 return FALSE;
13951 }
13952 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13953 p += (24 + align - 1) & -align;
13954 }
13955 }
13956
d4aaa2a0
AM
13957 for (group = htab->group; group != NULL; group = group->next)
13958 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
13959 {
13960 stub_sec_count += 1;
c9301e31
AM
13961 if (stub_sec->rawsize != stub_sec->size
13962 && (htab->stub_iteration <= STUB_SHRINK_ITER
13963 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13964 break;
13965 }
5d1634d7 13966
25516cc5 13967 if (group != NULL)
5d1634d7 13968 {
b34976b6 13969 htab->stub_error = TRUE;
cf97bcb0 13970 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 13971 }
721956f4 13972
d2a300cf
AM
13973 if (htab->stub_error)
13974 return FALSE;
13975
13976 if (stats != NULL)
13977 {
db84fff3 13978 size_t len;
d2a300cf
AM
13979 *stats = bfd_malloc (500);
13980 if (*stats == NULL)
13981 return FALSE;
13982
db84fff3
AM
13983 len = sprintf (*stats,
13984 ngettext ("linker stubs in %u group\n",
13985 "linker stubs in %u groups\n",
13986 stub_sec_count),
13987 stub_sec_count);
05d0e962
AM
13988 sprintf (*stats + len, _(" branch %lu\n"
13989 " branch toc adj %lu\n"
13990 " branch notoc %lu\n"
13991 " branch both %lu\n"
13992 " long branch %lu\n"
13993 " long toc adj %lu\n"
13994 " long notoc %lu\n"
13995 " long both %lu\n"
13996 " plt call %lu\n"
13997 " plt call save %lu\n"
13998 " plt call notoc %lu\n"
13999 " plt call both %lu\n"
14000 " global entry %lu"),
4ce794b7
AM
14001 htab->stub_count[ppc_stub_long_branch - 1],
14002 htab->stub_count[ppc_stub_long_branch_r2off - 1],
05d0e962
AM
14003 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14004 htab->stub_count[ppc_stub_long_branch_both - 1],
4ce794b7
AM
14005 htab->stub_count[ppc_stub_plt_branch - 1],
14006 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
05d0e962
AM
14007 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14008 htab->stub_count[ppc_stub_plt_branch_both - 1],
794e51c0 14009 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2 14010 htab->stub_count[ppc_stub_plt_call_r2save - 1],
05d0e962
AM
14011 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14012 htab->stub_count[ppc_stub_plt_call_both - 1],
7341d5e2 14013 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
14014 }
14015 return TRUE;
5bd4f169
AM
14016}
14017
60124e18
AM
14018/* What to do when ld finds relocations against symbols defined in
14019 discarded sections. */
14020
14021static unsigned int
14022ppc64_elf_action_discarded (asection *sec)
14023{
14024 if (strcmp (".opd", sec->name) == 0)
14025 return 0;
14026
14027 if (strcmp (".toc", sec->name) == 0)
14028 return 0;
14029
bce50a28
JJ
14030 if (strcmp (".toc1", sec->name) == 0)
14031 return 0;
14032
60124e18
AM
14033 return _bfd_elf_default_action_discarded (sec);
14034}
14035
5bd4f169
AM
14036/* The RELOCATE_SECTION function is called by the ELF backend linker
14037 to handle the relocations for a section.
14038
14039 The relocs are always passed as Rela structures; if the section
14040 actually uses Rel structures, the r_addend field will always be
14041 zero.
14042
14043 This function is responsible for adjust the section contents as
14044 necessary, and (if using Rela relocs and generating a
1049f94e 14045 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
14046 necessary.
14047
14048 This function does not have to worry about setting the reloc
14049 address or the reloc symbol index.
14050
14051 LOCAL_SYMS is a pointer to the swapped in local symbols.
14052
14053 LOCAL_SECTIONS is an array giving the section in the input file
14054 corresponding to the st_shndx field of each local symbol.
14055
14056 The global hash table entry for the global symbols can be found
14057 via elf_sym_hashes (input_bfd).
14058
1049f94e 14059 When generating relocatable output, this function must handle
5bd4f169
AM
14060 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14061 going to be the section symbol corresponding to the output
14062 section, which means that the addend must be adjusted
14063 accordingly. */
14064
b34976b6 14065static bfd_boolean
4ce794b7
AM
14066ppc64_elf_relocate_section (bfd *output_bfd,
14067 struct bfd_link_info *info,
14068 bfd *input_bfd,
14069 asection *input_section,
14070 bfd_byte *contents,
14071 Elf_Internal_Rela *relocs,
14072 Elf_Internal_Sym *local_syms,
14073 asection **local_sections)
5bd4f169 14074{
65f38f15 14075 struct ppc_link_hash_table *htab;
5bd4f169
AM
14076 Elf_Internal_Shdr *symtab_hdr;
14077 struct elf_link_hash_entry **sym_hashes;
5bd4f169 14078 Elf_Internal_Rela *rel;
c316a17c 14079 Elf_Internal_Rela *wrel;
5bd4f169 14080 Elf_Internal_Rela *relend;
411e1bfb
AM
14081 Elf_Internal_Rela outrel;
14082 bfd_byte *loc;
411e1bfb 14083 struct got_entry **local_got_ents;
5bd4f169 14084 bfd_vma TOCstart;
b34976b6
AM
14085 bfd_boolean ret = TRUE;
14086 bfd_boolean is_opd;
794e51c0
AM
14087 /* Assume 'at' branch hints. */
14088 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 14089 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 14090
65f38f15 14091 /* Initialize howto table if needed. */
5bd4f169 14092 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
14093 ppc_howto_init ();
14094
65f38f15 14095 htab = ppc_hash_table (info);
4dfe6ac6
NC
14096 if (htab == NULL)
14097 return FALSE;
ee75fd95
AM
14098
14099 /* Don't relocate stub sections. */
e7d1c40c 14100 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
14101 return TRUE;
14102
7af5d5c4
AM
14103 if (!is_ppc64_elf (input_bfd))
14104 {
14105 bfd_set_error (bfd_error_wrong_format);
14106 return FALSE;
14107 }
0ffa91dd 14108
411e1bfb 14109 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 14110 TOCstart = elf_gp (output_bfd);
0ffa91dd 14111 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 14112 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 14113 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 14114
c316a17c 14115 rel = wrel = relocs;
5bd4f169 14116 relend = relocs + input_section->reloc_count;
c316a17c 14117 for (; rel < relend; wrel++, rel++)
5bd4f169 14118 {
04c9666a 14119 enum elf_ppc64_reloc_type r_type;
31c76678 14120 bfd_vma addend;
5bd4f169
AM
14121 bfd_reloc_status_type r;
14122 Elf_Internal_Sym *sym;
14123 asection *sec;
039b3fef
AM
14124 struct elf_link_hash_entry *h_elf;
14125 struct ppc_link_hash_entry *h;
14126 struct ppc_link_hash_entry *fdh;
5bd4f169 14127 const char *sym_name;
0d4792f7 14128 unsigned long r_symndx, toc_symndx;
3a71aa26 14129 bfd_vma toc_addend;
f961d9dd
AM
14130 unsigned char tls_mask, tls_gd, tls_type;
14131 unsigned char sym_type;
5bd4f169 14132 bfd_vma relocation;
23cedd1d 14133 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 14134 bfd_boolean warned;
bc30df16 14135 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 14136 unsigned int insn;
e11840f9 14137 unsigned int mask;
721956f4
AM
14138 struct ppc_stub_hash_entry *stub_entry;
14139 bfd_vma max_br_offset;
14140 bfd_vma from;
c316a17c 14141 Elf_Internal_Rela orig_rel;
b80eed39
AM
14142 reloc_howto_type *howto;
14143 struct reloc_howto_struct alt_howto;
4a421c53
AM
14144 uint64_t pinsn;
14145 bfd_vma offset;
5bd4f169 14146
c316a17c
AM
14147 again:
14148 orig_rel = *rel;
14149
4ce794b7 14150 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 14151 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
14152
14153 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14154 symbol of the previous ADDR64 reloc. The symbol gives us the
14155 proper TOC base to use. */
14156 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
14157 && wrel != relocs
14158 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 14159 && is_opd)
c316a17c 14160 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 14161
4ce794b7
AM
14162 sym = NULL;
14163 sec = NULL;
039b3fef 14164 h_elf = NULL;
4ce794b7 14165 sym_name = NULL;
b34976b6
AM
14166 unresolved_reloc = FALSE;
14167 warned = FALSE;
65f38f15 14168
0b13192e 14169 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
14170 {
14171 /* It's a local symbol. */
74f0fb50 14172 struct _opd_sec_data *opd;
4025353c 14173
5bd4f169
AM
14174 sym = local_syms + r_symndx;
14175 sec = local_sections[r_symndx];
26c61ae5 14176 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 14177 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 14178 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
14179 opd = get_opd_info (sec);
14180 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 14181 {
51aecdc5
AM
14182 long adjust = opd->adjust[OPD_NDX (sym->st_value
14183 + rel->r_addend)];
4025353c
AM
14184 if (adjust == -1)
14185 relocation = 0;
14186 else
4cc603a5
AM
14187 {
14188 /* If this is a relocation against the opd section sym
14189 and we have edited .opd, adjust the reloc addend so
14190 that ld -r and ld --emit-relocs output is correct.
14191 If it is a reloc against some other .opd symbol,
14192 then the symbol value will be adjusted later. */
14193 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14194 rel->r_addend += adjust;
14195 else
14196 relocation += adjust;
14197 }
1e2f5b6e 14198 }
5bd4f169
AM
14199 }
14200 else
14201 {
62d887d4
L
14202 bfd_boolean ignored;
14203
b2a8e766
AM
14204 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14205 r_symndx, symtab_hdr, sym_hashes,
039b3fef 14206 h_elf, sec, relocation,
62d887d4 14207 unresolved_reloc, warned, ignored);
039b3fef
AM
14208 sym_name = h_elf->root.root.string;
14209 sym_type = h_elf->type;
b69fdb4e
AM
14210 if (sec != NULL
14211 && sec->owner == output_bfd
14212 && strcmp (sec->name, ".opd") == 0)
14213 {
14214 /* This is a symbol defined in a linker script. All
14215 such are defined in output sections, even those
14216 defined by simple assignment from a symbol defined in
14217 an input section. Transfer the symbol to an
14218 appropriate input .opd section, so that a branch to
14219 this symbol will be mapped to the location specified
14220 by the opd entry. */
14221 struct bfd_link_order *lo;
14222 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14223 if (lo->type == bfd_indirect_link_order)
14224 {
14225 asection *isec = lo->u.indirect.section;
14226 if (h_elf->root.u.def.value >= isec->output_offset
14227 && h_elf->root.u.def.value < (isec->output_offset
14228 + isec->size))
14229 {
14230 h_elf->root.u.def.value -= isec->output_offset;
14231 h_elf->root.u.def.section = isec;
14232 sec = isec;
14233 break;
14234 }
14235 }
14236 }
5bd4f169 14237 }
039b3fef 14238 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 14239
dbaa2011 14240 if (sec != NULL && discarded_section (sec))
c316a17c
AM
14241 {
14242 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14243 input_bfd, input_section,
0930cb30 14244 contents, rel->r_offset);
c316a17c
AM
14245 wrel->r_offset = rel->r_offset;
14246 wrel->r_info = 0;
14247 wrel->r_addend = 0;
14248
14249 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 14250 symbols defined in discarded sections. Not done for
c316a17c
AM
14251 non-debug to preserve relocs in .eh_frame which the
14252 eh_frame editing code expects to be present. */
14253 if (bfd_link_relocatable (info)
14254 && (input_section->flags & SEC_DEBUGGING))
14255 wrel--;
14256
14257 continue;
14258 }
ab96bf03 14259
0e1862bb 14260 if (bfd_link_relocatable (info))
c316a17c 14261 goto copy_reloc;
ab96bf03 14262
f40da81b
AM
14263 if (h != NULL && &h->elf == htab->elf.hgot)
14264 {
6f20ed8a 14265 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
14266 sec = bfd_abs_section_ptr;
14267 unresolved_reloc = FALSE;
14268 }
14269
951fd09b
AM
14270 /* TLS optimizations. Replace instruction sequences and relocs
14271 based on information we collected in tls_optimize. We edit
14272 RELOCS so that --emit-relocs will output something sensible
14273 for the final instruction stream. */
14274 tls_mask = 0;
14275 tls_gd = 0;
0d4792f7 14276 toc_symndx = 0;
727fc41e
AM
14277 if (h != NULL)
14278 tls_mask = h->tls_mask;
14279 else if (local_got_ents != NULL)
411e1bfb 14280 {
e054468f
AM
14281 struct plt_entry **local_plt = (struct plt_entry **)
14282 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 14283 unsigned char *lgot_masks = (unsigned char *)
e054468f 14284 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
14285 tls_mask = lgot_masks[r_symndx];
14286 }
37da22e5 14287 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
14288 && (r_type == R_PPC64_TLS
14289 || r_type == R_PPC64_TLSGD
14290 || r_type == R_PPC64_TLSLD))
14291 {
14292 /* Check for toc tls entries. */
f961d9dd 14293 unsigned char *toc_tls;
0d4792f7 14294
727fc41e
AM
14295 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14296 &local_syms, rel, input_bfd))
14297 return FALSE;
0d4792f7 14298
727fc41e
AM
14299 if (toc_tls)
14300 tls_mask = *toc_tls;
0d4792f7
AM
14301 }
14302
14303 /* Check that tls relocs are used with tls syms, and non-tls
14304 relocs are used with non-tls syms. */
cf35638d 14305 if (r_symndx != STN_UNDEF
0d4792f7
AM
14306 && r_type != R_PPC64_NONE
14307 && (h == NULL
039b3fef
AM
14308 || h->elf.root.type == bfd_link_hash_defined
14309 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
14310 && (IS_PPC64_TLS_RELOC (r_type)
14311 != (sym_type == STT_TLS
14312 || (sym_type == STT_SECTION
14313 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 14314 {
37da22e5 14315 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
14316 && (r_type == R_PPC64_TLS
14317 || r_type == R_PPC64_TLSGD
14318 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
14319 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14320 ;
14321 else
25f53a85 14322 info->callbacks->einfo
1d483afe 14323 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 14324 /* xgettext:c-format */
c1c8c1ef 14325 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 14326 /* xgettext:c-format */
c1c8c1ef 14327 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 14328 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
14329 ppc64_elf_howto_table[r_type]->name,
14330 sym_name);
411e1bfb
AM
14331 }
14332
14333 /* Ensure reloc mapping code below stays sane. */
14334 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14335 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14336 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14337 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14338 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14339 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14340 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14341 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14342 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14343 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14344 abort ();
0d4792f7 14345
411e1bfb
AM
14346 switch (r_type)
14347 {
14348 default:
411e1bfb
AM
14349 break;
14350
ba761f19 14351 case R_PPC64_LO_DS_OPT:
95f0d0d2 14352 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
14353 if ((insn & (0x3f << 26)) != 58u << 26)
14354 abort ();
14355 insn += (14u << 26) - (58u << 26);
95f0d0d2 14356 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
14357 r_type = R_PPC64_TOC16_LO;
14358 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14359 break;
14360
411e1bfb
AM
14361 case R_PPC64_TOC16:
14362 case R_PPC64_TOC16_LO:
14363 case R_PPC64_TOC16_DS:
14364 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
14365 {
14366 /* Check for toc tls entries. */
f961d9dd 14367 unsigned char *toc_tls;
951fd09b 14368 int retval;
411e1bfb 14369
3a71aa26
AM
14370 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14371 &local_syms, rel, input_bfd);
951fd09b 14372 if (retval == 0)
411e1bfb
AM
14373 return FALSE;
14374
14375 if (toc_tls)
14376 {
951fd09b 14377 tls_mask = *toc_tls;
411e1bfb
AM
14378 if (r_type == R_PPC64_TOC16_DS
14379 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 14380 {
37da22e5 14381 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
14382 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14383 goto toctprel;
14384 }
411e1bfb 14385 else
951fd09b
AM
14386 {
14387 /* If we found a GD reloc pair, then we might be
14388 doing a GD->IE transition. */
14389 if (retval == 2)
14390 {
14391 tls_gd = TLS_TPRELGD;
37da22e5
AM
14392 if ((tls_mask & TLS_TLS) != 0
14393 && (tls_mask & TLS_GD) == 0)
102890f0 14394 goto tls_ldgd_opt;
951fd09b
AM
14395 }
14396 else if (retval == 3)
14397 {
37da22e5
AM
14398 if ((tls_mask & TLS_TLS) != 0
14399 && (tls_mask & TLS_LD) == 0)
102890f0 14400 goto tls_ldgd_opt;
951fd09b
AM
14401 }
14402 }
411e1bfb
AM
14403 }
14404 }
14405 break;
14406
9d6ded02
AM
14407 case R_PPC64_GOT_TPREL16_HI:
14408 case R_PPC64_GOT_TPREL16_HA:
37da22e5 14409 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
14410 && (tls_mask & TLS_TPREL) == 0)
14411 {
14412 rel->r_offset -= d_offset;
95f0d0d2 14413 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
14414 r_type = R_PPC64_NONE;
14415 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14416 }
14417 break;
14418
411e1bfb
AM
14419 case R_PPC64_GOT_TPREL16_DS:
14420 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 14421 if ((tls_mask & TLS_TLS) != 0
951fd09b 14422 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14423 {
81407a69 14424 toctprel:
95f0d0d2 14425 insn = bfd_get_32 (input_bfd,
c316a17c 14426 contents + rel->r_offset - d_offset);
411e1bfb
AM
14427 insn &= 31 << 21;
14428 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 14429 bfd_put_32 (input_bfd, insn,
c316a17c 14430 contents + rel->r_offset - d_offset);
411e1bfb 14431 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
14432 if (toc_symndx != 0)
14433 {
14434 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14435 rel->r_addend = toc_addend;
0d4792f7
AM
14436 /* We changed the symbol. Start over in order to
14437 get h, sym, sec etc. right. */
c316a17c 14438 goto again;
0d4792f7
AM
14439 }
14440 else
14441 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14442 }
14443 break;
14444
14445 case R_PPC64_TLS:
37da22e5 14446 if ((tls_mask & TLS_TLS) != 0
951fd09b 14447 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14448 {
95f0d0d2 14449 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
14450 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14451 if (insn == 0)
411e1bfb 14452 abort ();
95f0d0d2 14453 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 14454 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
14455 PPC64_TPREL16_LO which is at low-order half-word. */
14456 rel->r_offset += d_offset;
0d4792f7
AM
14457 r_type = R_PPC64_TPREL16_LO;
14458 if (toc_symndx != 0)
14459 {
14460 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14461 rel->r_addend = toc_addend;
0d4792f7
AM
14462 /* We changed the symbol. Start over in order to
14463 get h, sym, sec etc. right. */
c316a17c 14464 goto again;
0d4792f7
AM
14465 }
14466 else
14467 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14468 }
14469 break;
14470
411e1bfb
AM
14471 case R_PPC64_GOT_TLSGD16_HI:
14472 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14473 tls_gd = TLS_TPRELGD;
37da22e5 14474 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
14475 goto tls_gdld_hi;
14476 break;
14477
411e1bfb
AM
14478 case R_PPC64_GOT_TLSLD16_HI:
14479 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 14480 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 14481 {
951fd09b
AM
14482 tls_gdld_hi:
14483 if ((tls_mask & tls_gd) != 0)
14484 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14485 + R_PPC64_GOT_TPREL16_DS);
14486 else
411e1bfb 14487 {
4fe5ca5b 14488 rel->r_offset -= d_offset;
95f0d0d2 14489 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 14490 r_type = R_PPC64_NONE;
411e1bfb 14491 }
951fd09b 14492 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14493 }
14494 break;
14495
951fd09b
AM
14496 case R_PPC64_GOT_TLSGD16:
14497 case R_PPC64_GOT_TLSGD16_LO:
14498 tls_gd = TLS_TPRELGD;
37da22e5 14499 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 14500 goto tls_ldgd_opt;
951fd09b 14501 break;
411e1bfb 14502
951fd09b
AM
14503 case R_PPC64_GOT_TLSLD16:
14504 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 14505 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 14506 {
b9f04fe0 14507 unsigned int insn1, insn2;
102890f0
AM
14508
14509 tls_ldgd_opt:
727fc41e
AM
14510 offset = (bfd_vma) -1;
14511 /* If not using the newer R_PPC64_TLSGD/LD to mark
14512 __tls_get_addr calls, we must trust that the call
14513 stays with its arg setup insns, ie. that the next
14514 reloc is the __tls_get_addr call associated with
14515 the current reloc. Edit both insns. */
14516 if (input_section->has_tls_get_addr_call
14517 && rel + 1 < relend
14518 && branch_reloc_hash_match (input_bfd, rel + 1,
14519 htab->tls_get_addr,
14520 htab->tls_get_addr_fd))
14521 offset = rel[1].r_offset;
b86ac8e3
AM
14522 /* We read the low GOT_TLS (or TOC16) insn because we
14523 need to keep the destination reg. It may be
14524 something other than the usual r3, and moved to r3
14525 before the call by intervening code. */
95f0d0d2 14526 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 14527 contents + rel->r_offset - d_offset);
102890f0 14528 if ((tls_mask & tls_gd) != 0)
411e1bfb 14529 {
102890f0 14530 /* IE */
b86ac8e3 14531 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
14532 insn1 |= 58 << 26; /* ld */
14533 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 14534 if (offset != (bfd_vma) -1)
f58d5a2d 14535 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
14536 if ((tls_mask & TLS_EXPLICIT) == 0)
14537 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14538 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 14539 else
102890f0
AM
14540 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14541 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14542 }
14543 else
14544 {
14545 /* LE */
b86ac8e3
AM
14546 insn1 &= 0x1f << 21;
14547 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
14548 insn2 = 0x38630000; /* addi 3,3,0 */
14549 if (tls_gd == 0)
951fd09b 14550 {
102890f0 14551 /* Was an LD reloc. */
1d483afe
AM
14552 if (toc_symndx)
14553 sec = local_sections[toc_symndx];
14554 for (r_symndx = 0;
14555 r_symndx < symtab_hdr->sh_info;
14556 r_symndx++)
14557 if (local_sections[r_symndx] == sec)
14558 break;
14559 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 14560 r_symndx = STN_UNDEF;
102890f0 14561 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 14562 if (r_symndx != STN_UNDEF)
1d483afe
AM
14563 rel->r_addend -= (local_syms[r_symndx].st_value
14564 + sec->output_offset
14565 + sec->output_section->vma);
951fd09b 14566 }
102890f0 14567 else if (toc_symndx != 0)
3a71aa26
AM
14568 {
14569 r_symndx = toc_symndx;
14570 rel->r_addend = toc_addend;
14571 }
102890f0
AM
14572 r_type = R_PPC64_TPREL16_HA;
14573 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
14574 if (offset != (bfd_vma) -1)
14575 {
14576 rel[1].r_info = ELF64_R_INFO (r_symndx,
14577 R_PPC64_TPREL16_LO);
14578 rel[1].r_offset = offset + d_offset;
14579 rel[1].r_addend = rel->r_addend;
14580 }
102890f0 14581 }
95f0d0d2 14582 bfd_put_32 (input_bfd, insn1,
3a71aa26 14583 contents + rel->r_offset - d_offset);
727fc41e 14584 if (offset != (bfd_vma) -1)
c96e0573
AM
14585 {
14586 bfd_put_32 (input_bfd, insn2, contents + offset);
14587 if (offset + 8 <= input_section->size)
14588 {
14589 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14590 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14591 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14592 }
14593 }
727fc41e
AM
14594 if ((tls_mask & tls_gd) == 0
14595 && (tls_gd == 0 || toc_symndx != 0))
14596 {
14597 /* We changed the symbol. Start over in order
14598 to get h, sym, sec etc. right. */
c316a17c 14599 goto again;
727fc41e
AM
14600 }
14601 }
14602 break;
14603
14604 case R_PPC64_TLSGD:
37da22e5 14605 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 14606 && rel + 1 < relend)
727fc41e 14607 {
b9f04fe0 14608 unsigned int insn2;
5663e321 14609 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 14610
4a421c53 14611 offset = rel->r_offset;
5663e321 14612 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
14613 {
14614 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
14615 if (r_type1 == R_PPC64_PLT_PCREL34
14616 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14617 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
14618 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14619 break;
14620 }
14621
14622 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14623 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14624
727fc41e
AM
14625 if ((tls_mask & TLS_TPRELGD) != 0)
14626 {
14627 /* IE */
14628 r_type = R_PPC64_NONE;
14629 insn2 = 0x7c636a14; /* add 3,3,13 */
14630 }
14631 else
14632 {
14633 /* LE */
14634 if (toc_symndx != 0)
14635 {
14636 r_symndx = toc_symndx;
14637 rel->r_addend = toc_addend;
14638 }
14639 r_type = R_PPC64_TPREL16_LO;
14640 rel->r_offset = offset + d_offset;
14641 insn2 = 0x38630000; /* addi 3,3,0 */
14642 }
14643 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14644 /* Zap the reloc on the _tls_get_addr call too. */
14645 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14646 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 14647 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 14648 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 14649 goto again;
411e1bfb 14650 }
411e1bfb
AM
14651 break;
14652
727fc41e 14653 case R_PPC64_TLSLD:
37da22e5 14654 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 14655 && rel + 1 < relend)
727fc41e 14656 {
b9f04fe0 14657 unsigned int insn2;
5663e321 14658 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 14659
4a421c53 14660 offset = rel->r_offset;
5663e321 14661 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
14662 {
14663 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
14664 if (r_type1 == R_PPC64_PLT_PCREL34
14665 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14666 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
14667 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14668 break;
14669 }
14670
14671 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14672 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14673
727fc41e
AM
14674 if (toc_symndx)
14675 sec = local_sections[toc_symndx];
14676 for (r_symndx = 0;
14677 r_symndx < symtab_hdr->sh_info;
14678 r_symndx++)
14679 if (local_sections[r_symndx] == sec)
14680 break;
14681 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 14682 r_symndx = STN_UNDEF;
727fc41e 14683 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 14684 if (r_symndx != STN_UNDEF)
727fc41e
AM
14685 rel->r_addend -= (local_syms[r_symndx].st_value
14686 + sec->output_offset
14687 + sec->output_section->vma);
14688
14689 r_type = R_PPC64_TPREL16_LO;
14690 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14691 rel->r_offset = offset + d_offset;
14692 /* Zap the reloc on the _tls_get_addr call too. */
14693 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14694 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 14695 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 14696 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 14697 goto again;
727fc41e
AM
14698 }
14699 break;
14700
411e1bfb 14701 case R_PPC64_DTPMOD64:
951fd09b
AM
14702 if (rel + 1 < relend
14703 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14704 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 14705 {
951fd09b
AM
14706 if ((tls_mask & TLS_GD) == 0)
14707 {
14708 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14709 if ((tls_mask & TLS_TPRELGD) != 0)
14710 r_type = R_PPC64_TPREL64;
14711 else
14712 {
4ce794b7 14713 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
14714 r_type = R_PPC64_NONE;
14715 }
14716 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14717 }
14718 }
14719 else
14720 {
14721 if ((tls_mask & TLS_LD) == 0)
411e1bfb 14722 {
4ce794b7 14723 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 14724 r_type = R_PPC64_NONE;
951fd09b 14725 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 14726 }
411e1bfb
AM
14727 }
14728 break;
14729
14730 case R_PPC64_TPREL64:
951fd09b 14731 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
14732 {
14733 r_type = R_PPC64_NONE;
14734 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14735 }
14736 break;
52a82034 14737
006589cf
AM
14738 case R_PPC64_ENTRY:
14739 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14740 if (!bfd_link_pic (info)
14741 && !info->traditional_format
14742 && relocation + 0x80008000 <= 0xffffffff)
14743 {
14744 unsigned int insn1, insn2;
14745
14746 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14747 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14748 if ((insn1 & ~0xfffc) == LD_R2_0R12
14749 && insn2 == ADD_R2_R2_R12)
14750 {
95f0d0d2 14751 bfd_put_32 (input_bfd,
006589cf
AM
14752 LIS_R2 + PPC_HA (relocation),
14753 contents + rel->r_offset);
95f0d0d2 14754 bfd_put_32 (input_bfd,
006589cf
AM
14755 ADDI_R2_R2 + PPC_LO (relocation),
14756 contents + rel->r_offset + 4);
14757 }
14758 }
14759 else
14760 {
14761 relocation -= (rel->r_offset
14762 + input_section->output_offset
14763 + input_section->output_section->vma);
14764 if (relocation + 0x80008000 <= 0xffffffff)
14765 {
14766 unsigned int insn1, insn2;
14767
14768 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14769 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14770 if ((insn1 & ~0xfffc) == LD_R2_0R12
14771 && insn2 == ADD_R2_R2_R12)
14772 {
95f0d0d2 14773 bfd_put_32 (input_bfd,
006589cf
AM
14774 ADDIS_R2_R12 + PPC_HA (relocation),
14775 contents + rel->r_offset);
95f0d0d2 14776 bfd_put_32 (input_bfd,
006589cf
AM
14777 ADDI_R2_R2 + PPC_LO (relocation),
14778 contents + rel->r_offset + 4);
14779 }
14780 }
14781 }
14782 break;
14783
52a82034
AM
14784 case R_PPC64_REL16_HA:
14785 /* If we are generating a non-PIC executable, edit
14786 . 0: addis 2,12,.TOC.-0b@ha
14787 . addi 2,2,.TOC.-0b@l
14788 used by ELFv2 global entry points to set up r2, to
14789 . lis 2,.TOC.@ha
14790 . addi 2,2,.TOC.@l
14791 if .TOC. is in range. */
0e1862bb 14792 if (!bfd_link_pic (info)
810d4e75 14793 && !info->traditional_format
006589cf 14794 && !htab->opd_abi
4f038ee5 14795 && rel->r_addend == d_offset
52a82034
AM
14796 && h != NULL && &h->elf == htab->elf.hgot
14797 && rel + 1 < relend
14798 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14799 && rel[1].r_offset == rel->r_offset + 4
14800 && rel[1].r_addend == rel->r_addend + 4
14801 && relocation + 0x80008000 <= 0xffffffff)
14802 {
14803 unsigned int insn1, insn2;
4a421c53 14804 offset = rel->r_offset - d_offset;
95f0d0d2
AM
14805 insn1 = bfd_get_32 (input_bfd, contents + offset);
14806 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14807 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14808 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14809 {
14810 r_type = R_PPC64_ADDR16_HA;
14811 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14812 rel->r_addend -= d_offset;
14813 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14814 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14815 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14816 }
14817 }
14818 break;
411e1bfb
AM
14819 }
14820
14821 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14822 insn = 0;
b25116a9
AM
14823 max_br_offset = 1 << 25;
14824 addend = rel->r_addend;
bc30df16 14825 reloc_dest = DEST_NORMAL;
65f38f15 14826 switch (r_type)
5bd4f169
AM
14827 {
14828 default:
65f38f15 14829 break;
5bd4f169 14830
3b421ab3
AM
14831 case R_PPC64_TOCSAVE:
14832 if (relocation + addend == (rel->r_offset
14833 + input_section->output_offset
14834 + input_section->output_section->vma)
14835 && tocsave_find (htab, NO_INSERT,
14836 &local_syms, rel, input_bfd))
14837 {
14838 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14839 if (insn == NOP
14840 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14841 bfd_put_32 (input_bfd,
14842 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14843 contents + rel->r_offset);
14844 }
14845 break;
14846
65f38f15
AM
14847 /* Branch taken prediction relocations. */
14848 case R_PPC64_ADDR14_BRTAKEN:
14849 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14850 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14851 /* Fall through. */
65f38f15 14852
86c76c7b 14853 /* Branch not taken prediction relocations. */
65f38f15
AM
14854 case R_PPC64_ADDR14_BRNTAKEN:
14855 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14856 insn |= bfd_get_32 (input_bfd,
411e1bfb 14857 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14858 /* Fall through. */
86c76c7b 14859
b25116a9
AM
14860 case R_PPC64_REL14:
14861 max_br_offset = 1 << 15;
1a0670f3 14862 /* Fall through. */
5bd4f169 14863
65f38f15 14864 case R_PPC64_REL24:
05d0e962 14865 case R_PPC64_REL24_NOTOC:
23cedd1d 14866 case R_PPC64_PLTCALL:
5663e321 14867 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
14868 /* Calls to functions with a different TOC, such as calls to
14869 shared objects, need to alter the TOC pointer. This is
14870 done using a linkage stub. A REL24 branching to these
14871 linkage stubs needs to be followed by a nop, as the nop
14872 will be replaced with an instruction to restore the TOC
14873 base pointer. */
8387904d 14874 fdh = h;
b31867b6
AM
14875 if (h != NULL
14876 && h->oh != NULL
14877 && h->oh->is_func_descriptor)
14878 fdh = ppc_follow_link (h->oh);
31c76678
DK
14879 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14880 htab);
5663e321
AM
14881 if ((r_type == R_PPC64_PLTCALL
14882 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 14883 && stub_entry != NULL
05d0e962
AM
14884 && stub_entry->stub_type >= ppc_stub_plt_call
14885 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
14886 stub_entry = NULL;
14887
6abec6bc 14888 if (stub_entry != NULL
d0abeec8
AM
14889 && ((stub_entry->stub_type >= ppc_stub_plt_call
14890 && stub_entry->stub_type <= ppc_stub_plt_call_both)
ad8e1ba5 14891 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
14892 || stub_entry->stub_type == ppc_stub_plt_branch_both
14893 || stub_entry->stub_type == ppc_stub_long_branch_r2off
14894 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 14895 {
b25116a9 14896 bfd_boolean can_plt_call = FALSE;
721956f4 14897
6e1816be
AM
14898 if (stub_entry->stub_type == ppc_stub_plt_call
14899 && !htab->opd_abi
14900 && htab->params->plt_localentry0 != 0
14901 && is_elfv2_localentry0 (&h->elf))
14902 {
14903 /* The function doesn't use or change r2. */
14904 can_plt_call = TRUE;
14905 }
05d0e962
AM
14906 else if (r_type == R_PPC64_REL24_NOTOC)
14907 {
14908 /* NOTOC calls don't need to restore r2. */
14909 can_plt_call = TRUE;
14910 }
6e1816be 14911
f378ab09 14912 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
14913 branch and link followed by a nop. The nop is
14914 replaced by an insn to restore r2. */
6e1816be 14915 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14916 {
ba8ca3e7
AM
14917 unsigned long br;
14918
14919 br = bfd_get_32 (input_bfd,
14920 contents + rel->r_offset);
14921 if ((br & 1) != 0)
41bd81ab 14922 {
ba8ca3e7
AM
14923 unsigned long nop;
14924
14925 nop = bfd_get_32 (input_bfd,
14926 contents + rel->r_offset + 4);
23cedd1d
AM
14927 if (nop == LD_R2_0R1 + STK_TOC (htab))
14928 can_plt_call = TRUE;
14929 else if (nop == NOP
14930 || nop == CROR_151515
14931 || nop == CROR_313131)
a7f2871e 14932 {
ba8ca3e7
AM
14933 if (h != NULL
14934 && (h == htab->tls_get_addr_fd
14935 || h == htab->tls_get_addr)
7c9cf415 14936 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14937 {
14938 /* Special stub used, leave nop alone. */
14939 }
14940 else
a078d95a
AM
14941 bfd_put_32 (input_bfd,
14942 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14943 contents + rel->r_offset + 4);
14944 can_plt_call = TRUE;
a7f2871e 14945 }
41bd81ab 14946 }
5bd4f169 14947 }
721956f4 14948
ba8ca3e7 14949 if (!can_plt_call && h != NULL)
721956f4 14950 {
ba8ca3e7
AM
14951 const char *name = h->elf.root.root.string;
14952
14953 if (*name == '.')
14954 ++name;
14955
14956 if (strncmp (name, "__libc_start_main", 17) == 0
14957 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14958 {
ba8ca3e7
AM
14959 /* Allow crt1 branch to go via a toc adjusting
14960 stub. Other calls that never return could do
14961 the same, if we could detect such. */
b25116a9 14962 can_plt_call = TRUE;
6ab189d5 14963 }
ba8ca3e7
AM
14964 }
14965
14966 if (!can_plt_call)
14967 {
14968 /* g++ as of 20130507 emits self-calls without a
14969 following nop. This is arguably wrong since we
14970 have conflicting information. On the one hand a
14971 global symbol and on the other a local call
14972 sequence, but don't error for this special case.
14973 It isn't possible to cheaply verify we have
14974 exactly such a call. Allow all calls to the same
14975 section. */
14976 asection *code_sec = sec;
14977
14978 if (get_opd_info (sec) != NULL)
ad8e1ba5 14979 {
ba8ca3e7
AM
14980 bfd_vma off = (relocation + addend
14981 - sec->output_section->vma
14982 - sec->output_offset);
bc30df16 14983
ba8ca3e7 14984 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14985 }
ba8ca3e7
AM
14986 if (code_sec == input_section)
14987 can_plt_call = TRUE;
14988 }
14989
14990 if (!can_plt_call)
14991 {
05d0e962
AM
14992 if (stub_entry->stub_type >= ppc_stub_plt_call
14993 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 14994 info->callbacks->einfo
695344c0 14995 /* xgettext:c-format */
c1c8c1ef 14996 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 14997 "(plt call stub)\n"),
4805fc55
AM
14998 input_bfd, input_section, rel->r_offset, sym_name);
14999 else
15000 info->callbacks->einfo
695344c0 15001 /* xgettext:c-format */
c1c8c1ef 15002 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15003 "(toc save/adjust stub)\n"),
4805fc55 15004 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
15005
15006 bfd_set_error (bfd_error_bad_value);
15007 ret = FALSE;
721956f4
AM
15008 }
15009
b25116a9 15010 if (can_plt_call
05d0e962
AM
15011 && stub_entry->stub_type >= ppc_stub_plt_call
15012 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
15013 unresolved_reloc = FALSE;
15014 }
15015
6abec6bc
AM
15016 if ((stub_entry == NULL
15017 || stub_entry->stub_type == ppc_stub_long_branch
15018 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
15019 && get_opd_info (sec) != NULL)
15020 {
15021 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
15022 bfd_vma off = (relocation + addend
15023 - sec->output_section->vma
15024 - sec->output_offset);
aef36ac1 15025 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
15026 if (dest != (bfd_vma) -1)
15027 {
15028 relocation = dest;
15029 addend = 0;
bc30df16 15030 reloc_dest = DEST_OPD;
8387904d
AM
15031 }
15032 }
15033
b25116a9
AM
15034 /* If the branch is out of reach we ought to have a long
15035 branch stub. */
15036 from = (rel->r_offset
15037 + input_section->output_offset
15038 + input_section->output_section->vma);
15039
6911b7dc
AM
15040 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15041 ? fdh->elf.other
15042 : sym->st_other);
15043
6abec6bc
AM
15044 if (stub_entry != NULL
15045 && (stub_entry->stub_type == ppc_stub_long_branch
15046 || stub_entry->stub_type == ppc_stub_plt_branch)
15047 && (r_type == R_PPC64_ADDR14_BRTAKEN
15048 || r_type == R_PPC64_ADDR14_BRNTAKEN
15049 || (relocation + addend - from + max_br_offset
15050 < 2 * max_br_offset)))
15051 /* Don't use the stub if this branch is in range. */
15052 stub_entry = NULL;
b25116a9 15053
05d0e962
AM
15054 if (stub_entry != NULL
15055 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15056 || stub_entry->stub_type == ppc_stub_long_branch_both
15057 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15058 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15059 && (r_type != R_PPC64_REL24_NOTOC
15060 || ((fdh ? fdh->elf.other : sym->st_other)
4a4e7361 15061 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
05d0e962
AM
15062 && (relocation + addend - from + max_br_offset
15063 < 2 * max_br_offset))
15064 stub_entry = NULL;
15065
15066 if (stub_entry != NULL
15067 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15068 || stub_entry->stub_type == ppc_stub_long_branch_both
15069 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15070 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15071 && r_type == R_PPC64_REL24_NOTOC
15072 && (relocation + addend - from + max_br_offset
15073 < 2 * max_br_offset))
15074 stub_entry = NULL;
15075
b25116a9
AM
15076 if (stub_entry != NULL)
15077 {
15078 /* Munge up the value and addend so that we call the stub
15079 rather than the procedure directly. */
a4b6fadd
AM
15080 asection *stub_sec = stub_entry->group->stub_sec;
15081
15082 if (stub_entry->stub_type == ppc_stub_save_res)
15083 relocation += (stub_sec->output_offset
15084 + stub_sec->output_section->vma
15085 + stub_sec->size - htab->sfpr->size
15086 - htab->sfpr->output_offset
15087 - htab->sfpr->output_section->vma);
15088 else
15089 relocation = (stub_entry->stub_offset
15090 + stub_sec->output_offset
15091 + stub_sec->output_section->vma);
b25116a9 15092 addend = 0;
bc30df16 15093 reloc_dest = DEST_STUB;
3b421ab3 15094
05d0e962
AM
15095 if (((stub_entry->stub_type == ppc_stub_plt_call
15096 && ALWAYS_EMIT_R2SAVE)
15097 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15098 || stub_entry->stub_type == ppc_stub_plt_call_both)
e81b4c93
AM
15099 && !(h != NULL
15100 && (h == htab->tls_get_addr_fd
15101 || h == htab->tls_get_addr)
15102 && htab->params->tls_get_addr_opt)
3b421ab3
AM
15103 && rel + 1 < relend
15104 && rel[1].r_offset == rel->r_offset + 4
15105 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15106 relocation += 4;
05d0e962
AM
15107 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15108 || stub_entry->stub_type == ppc_stub_plt_branch_both
15109 || stub_entry->stub_type == ppc_stub_plt_call_both)
15110 && r_type == R_PPC64_REL24_NOTOC)
15111 relocation += 4;
5663e321
AM
15112
15113 if (r_type == R_PPC64_REL24_NOTOC
15114 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
15115 || stub_entry->stub_type == ppc_stub_plt_call_both))
15116 htab->notoc_plt = 1;
b25116a9
AM
15117 }
15118
15119 if (insn != 0)
15120 {
794e51c0 15121 if (is_isa_v2)
721956f4 15122 {
b25116a9
AM
15123 /* Set 'a' bit. This is 0b00010 in BO field for branch
15124 on CR(BI) insns (BO == 001at or 011at), and 0b01000
15125 for branch on CTR insns (BO == 1a00t or 1a01t). */
15126 if ((insn & (0x14 << 21)) == (0x04 << 21))
15127 insn |= 0x02 << 21;
15128 else if ((insn & (0x14 << 21)) == (0x10 << 21))
15129 insn |= 0x08 << 21;
15130 else
15131 break;
15132 }
15133 else
15134 {
15135 /* Invert 'y' bit if not the default. */
4cc603a5 15136 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 15137 insn ^= 0x01 << 21;
721956f4 15138 }
b25116a9 15139
95f0d0d2 15140 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 15141 }
e86ce104 15142
06da1e8e
AM
15143 /* NOP out calls to undefined weak functions.
15144 We can thus call a weak function without first
15145 checking whether the function is defined. */
b25116a9 15146 else if (h != NULL
039b3fef 15147 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 15148 && h->elf.dynindx == -1
05d0e962
AM
15149 && (r_type == R_PPC64_REL24
15150 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 15151 && relocation == 0
4cc603a5 15152 && addend == 0)
e86ce104 15153 {
95f0d0d2 15154 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 15155 goto copy_reloc;
e86ce104 15156 }
65f38f15 15157 break;
066f4018
AM
15158
15159 case R_PPC64_GOT16_DS:
15160 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15161 if (relocation + addend - from + 0x8000 < 0x10000
15162 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15163 {
15164 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15165 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15166 {
15167 insn += (14u << 26) - (58u << 26);
15168 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15169 r_type = R_PPC64_TOC16;
15170 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15171 }
15172 }
15173 break;
15174
15175 case R_PPC64_GOT16_LO_DS:
15176 case R_PPC64_GOT16_HA:
15177 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15178 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
15179 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15180 {
15181 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15182 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15183 {
15184 insn += (14u << 26) - (58u << 26);
15185 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15186 r_type = R_PPC64_TOC16_LO;
15187 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15188 }
15189 else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
15190 {
15191 r_type = R_PPC64_TOC16_HA;
15192 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15193 }
15194 }
15195 break;
4a421c53
AM
15196
15197 case R_PPC64_GOT_PCREL34:
15198 from = (rel->r_offset
15199 + input_section->output_section->vma
15200 + input_section->output_offset);
15201 if (relocation - from + (1ULL << 33) < 1ULL << 34
15202 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15203 {
15204 offset = rel->r_offset;
15205 pinsn = bfd_get_32 (input_bfd, contents + offset);
15206 pinsn <<= 32;
15207 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15208 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15209 == ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
15210 {
15211 /* Replace with paddi. */
15212 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
15213 r_type = R_PPC64_PCREL34;
15214 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15215 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
15216 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
15217 goto pcrelopt;
15218 }
15219 }
15220 break;
15221
15222 case R_PPC64_PCREL34:
15223 if (SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15224 {
15225 offset = rel->r_offset;
15226 pinsn = bfd_get_32 (input_bfd, contents + offset);
15227 pinsn <<= 32;
15228 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15229 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15230 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
15231 | (14ULL << 26) /* paddi */))
15232 {
15233 pcrelopt:
15234 if (rel + 1 < relend
15235 && rel[1].r_offset == offset
15236 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT))
15237 {
15238 bfd_vma off2 = rel[1].r_addend;
15239 if (off2 == 0)
15240 /* zero means next insn. */
15241 off2 = 8;
15242 off2 += offset;
15243 if (off2 + 4 <= input_section->size)
15244 {
15245 uint64_t pinsn2;
15246 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
15247 pinsn2 <<= 32;
15248 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15249 break;
15250 if (xlate_pcrel_opt (&pinsn, &pinsn2))
15251 {
15252 bfd_put_32 (input_bfd, pinsn >> 32,
15253 contents + offset);
15254 bfd_put_32 (input_bfd, pinsn,
15255 contents + offset + 4);
15256 bfd_put_32 (input_bfd, pinsn2 >> 32,
15257 contents + off2);
15258 }
15259 }
15260 }
15261 }
15262 }
15263 break;
65f38f15 15264 }
5bd4f169 15265
65f38f15 15266 /* Set `addend'. */
411e1bfb 15267 tls_type = 0;
23cedd1d 15268 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
15269 switch (r_type)
15270 {
15271 default:
cf97bcb0
AM
15272 /* xgettext:c-format */
15273 _bfd_error_handler (_("%pB: %s unsupported"),
15274 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 15275
65f38f15 15276 bfd_set_error (bfd_error_bad_value);
b34976b6 15277 ret = FALSE;
c316a17c 15278 goto copy_reloc;
5bd4f169 15279
65f38f15 15280 case R_PPC64_NONE:
411e1bfb 15281 case R_PPC64_TLS:
727fc41e
AM
15282 case R_PPC64_TLSGD:
15283 case R_PPC64_TLSLD:
3b421ab3 15284 case R_PPC64_TOCSAVE:
04c9666a
AM
15285 case R_PPC64_GNU_VTINHERIT:
15286 case R_PPC64_GNU_VTENTRY:
006589cf 15287 case R_PPC64_ENTRY:
4a421c53 15288 case R_PPC64_PCREL_OPT:
c316a17c 15289 goto copy_reloc;
5bd4f169
AM
15290
15291 /* GOT16 relocations. Like an ADDR16 using the symbol's
15292 address in the GOT as relocation value instead of the
411e1bfb 15293 symbol's value itself. Also, create a GOT entry for the
5bd4f169 15294 symbol and put the symbol value there. */
411e1bfb
AM
15295 case R_PPC64_GOT_TLSGD16:
15296 case R_PPC64_GOT_TLSGD16_LO:
15297 case R_PPC64_GOT_TLSGD16_HI:
15298 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 15299 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
15300 goto dogot;
15301
15302 case R_PPC64_GOT_TLSLD16:
15303 case R_PPC64_GOT_TLSLD16_LO:
15304 case R_PPC64_GOT_TLSLD16_HI:
15305 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 15306 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
15307 goto dogot;
15308
15309 case R_PPC64_GOT_TPREL16_DS:
15310 case R_PPC64_GOT_TPREL16_LO_DS:
15311 case R_PPC64_GOT_TPREL16_HI:
15312 case R_PPC64_GOT_TPREL16_HA:
15313 tls_type = TLS_TLS | TLS_TPREL;
15314 goto dogot;
15315
15316 case R_PPC64_GOT_DTPREL16_DS:
15317 case R_PPC64_GOT_DTPREL16_LO_DS:
15318 case R_PPC64_GOT_DTPREL16_HI:
15319 case R_PPC64_GOT_DTPREL16_HA:
15320 tls_type = TLS_TLS | TLS_DTPREL;
15321 goto dogot;
15322
65f38f15
AM
15323 case R_PPC64_GOT16:
15324 case R_PPC64_GOT16_LO:
15325 case R_PPC64_GOT16_HI:
15326 case R_PPC64_GOT16_HA:
15327 case R_PPC64_GOT16_DS:
15328 case R_PPC64_GOT16_LO_DS:
5663e321 15329 case R_PPC64_GOT_PCREL34:
411e1bfb 15330 dogot:
5bd4f169
AM
15331 {
15332 /* Relocation is to the entry for this symbol in the global
15333 offset table. */
e717da7e 15334 asection *got;
d881513a 15335 bfd_vma *offp;
5bd4f169 15336 bfd_vma off;
d881513a 15337 unsigned long indx = 0;
927be08e 15338 struct got_entry *ent;
5663e321
AM
15339 bfd_vma sym_addend = orig_rel.r_addend;
15340
15341 if (r_type == R_PPC64_GOT_PCREL34)
15342 sym_addend = 0;
65f38f15 15343
d881513a
AM
15344 if (tls_type == (TLS_TLS | TLS_LD)
15345 && (h == NULL
f5385ebf 15346 || !h->elf.def_dynamic))
927be08e 15347 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 15348 else
5bd4f169 15349 {
d881513a
AM
15350 if (h != NULL)
15351 {
f0158f44
AM
15352 if (!htab->elf.dynamic_sections_created
15353 || h->elf.dynindx == -1
15354 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 15355 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
15356 /* This is actually a static link, or it is a
15357 -Bsymbolic link and the symbol is defined
15358 locally, or the symbol was forced to be local
15359 because of a version file. */
15360 ;
15361 else
15362 {
039b3fef 15363 indx = h->elf.dynindx;
d881513a
AM
15364 unresolved_reloc = FALSE;
15365 }
039b3fef 15366 ent = h->elf.got.glist;
d881513a 15367 }
411e1bfb 15368 else
5bd4f169 15369 {
d881513a
AM
15370 if (local_got_ents == NULL)
15371 abort ();
15372 ent = local_got_ents[r_symndx];
5bd4f169 15373 }
d881513a
AM
15374
15375 for (; ent != NULL; ent = ent->next)
5663e321 15376 if (ent->addend == sym_addend
e717da7e 15377 && ent->owner == input_bfd
d881513a
AM
15378 && ent->tls_type == tls_type)
15379 break;
5bd4f169 15380 }
411e1bfb 15381
927be08e
AM
15382 if (ent == NULL)
15383 abort ();
15384 if (ent->is_indirect)
15385 ent = ent->got.ent;
15386 offp = &ent->got.offset;
15387 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
15388 if (got == NULL)
15389 abort ();
15390
411e1bfb
AM
15391 /* The offset must always be a multiple of 8. We use the
15392 least significant bit to record whether we have already
15393 processed this entry. */
d881513a 15394 off = *offp;
411e1bfb
AM
15395 if ((off & 1) != 0)
15396 off &= ~1;
5bd4f169
AM
15397 else
15398 {
411e1bfb
AM
15399 /* Generate relocs for the dynamic linker, except in
15400 the case of TLSLD where we'll use one entry per
15401 module. */
25f23106
AM
15402 asection *relgot;
15403 bfd_boolean ifunc;
e717da7e 15404
d881513a 15405 *offp = off | 1;
25f23106
AM
15406 relgot = NULL;
15407 ifunc = (h != NULL
15408 ? h->elf.type == STT_GNU_IFUNC
15409 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 15410 if (ifunc)
82e66161
AM
15411 {
15412 relgot = htab->elf.irelplt;
15413 if (indx == 0)
15414 htab->local_ifunc_resolver = 1;
15415 else if (is_static_defined (&h->elf))
15416 htab->maybe_local_ifunc_resolver = 1;
15417 }
f0158f44
AM
15418 else if (indx != 0
15419 || (bfd_link_pic (info)
15420 && (h == NULL
21d68fcd 15421 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 15422 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
15423 && !h->elf.def_dynamic))
15424 && !(tls_type == (TLS_TLS | TLS_TPREL)
15425 && bfd_link_executable (info)
15426 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 15427 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 15428 if (relgot != NULL)
5bd4f169 15429 {
e717da7e
AM
15430 outrel.r_offset = (got->output_section->vma
15431 + got->output_offset
411e1bfb 15432 + off);
5663e321 15433 outrel.r_addend = sym_addend;
d881513a 15434 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 15435 {
411e1bfb 15436 outrel.r_addend = 0;
e515b051 15437 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
15438 if (tls_type == (TLS_TLS | TLS_GD))
15439 {
e717da7e
AM
15440 loc = relgot->contents;
15441 loc += (relgot->reloc_count++
d881513a
AM
15442 * sizeof (Elf64_External_Rela));
15443 bfd_elf64_swap_reloca_out (output_bfd,
15444 &outrel, loc);
e515b051 15445 outrel.r_offset += 8;
5663e321 15446 outrel.r_addend = sym_addend;
d881513a
AM
15447 outrel.r_info
15448 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 15449 }
411e1bfb 15450 }
951fd09b 15451 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 15452 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 15453 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 15454 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
15455 else if (indx != 0)
15456 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15457 else
81407a69 15458 {
25f23106
AM
15459 if (ifunc)
15460 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15461 else
15462 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
15463
15464 /* Write the .got section contents for the sake
15465 of prelink. */
e717da7e 15466 loc = got->contents + off;
23fbd6fa
JJ
15467 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15468 loc);
81407a69 15469 }
81407a69
AM
15470
15471 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
15472 {
15473 outrel.r_addend += relocation;
15474 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
15475 {
15476 if (htab->elf.tls_sec == NULL)
15477 outrel.r_addend = 0;
15478 else
15479 outrel.r_addend -= htab->elf.tls_sec->vma;
15480 }
e515b051 15481 }
e717da7e
AM
15482 loc = relgot->contents;
15483 loc += (relgot->reloc_count++
411e1bfb
AM
15484 * sizeof (Elf64_External_Rela));
15485 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15486 }
15487
ad8e1ba5 15488 /* Init the .got section contents here if we're not
81407a69 15489 emitting a reloc. */
d881513a 15490 else
411e1bfb 15491 {
5663e321 15492 relocation += sym_addend;
f0158f44 15493 if (tls_type != 0)
411e1bfb 15494 {
989f9879
AM
15495 if (htab->elf.tls_sec == NULL)
15496 relocation = 0;
15497 else
15498 {
f0158f44
AM
15499 if (tls_type & TLS_LD)
15500 relocation = 0;
15501 else
15502 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 15503 if (tls_type & TLS_TPREL)
989f9879
AM
15504 relocation += DTP_OFFSET - TP_OFFSET;
15505 }
5bd4f169 15506
f0158f44 15507 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
15508 {
15509 bfd_put_64 (output_bfd, relocation,
e717da7e 15510 got->contents + off + 8);
676ee2b5 15511 relocation = 1;
7b609f53 15512 }
411e1bfb
AM
15513 }
15514 bfd_put_64 (output_bfd, relocation,
e717da7e 15515 got->contents + off);
5bd4f169
AM
15516 }
15517 }
15518
65f38f15
AM
15519 if (off >= (bfd_vma) -2)
15520 abort ();
15521
bf102f86 15522 relocation = got->output_section->vma + got->output_offset + off;
5663e321
AM
15523 if (r_type != R_PPC64_GOT_PCREL34)
15524 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 15525 }
65f38f15
AM
15526 break;
15527
15528 case R_PPC64_PLT16_HA:
15529 case R_PPC64_PLT16_HI:
15530 case R_PPC64_PLT16_LO:
08be3224 15531 case R_PPC64_PLT16_LO_DS:
5663e321
AM
15532 case R_PPC64_PLT_PCREL34:
15533 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
15534 case R_PPC64_PLT32:
15535 case R_PPC64_PLT64:
23cedd1d 15536 case R_PPC64_PLTSEQ:
5663e321 15537 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 15538 case R_PPC64_PLTCALL:
5663e321 15539 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
15540 /* Relocation is to the entry for this symbol in the
15541 procedure linkage table. */
23cedd1d 15542 unresolved_reloc = TRUE;
cbf95972
AM
15543 {
15544 struct plt_entry **plt_list = NULL;
15545 if (h != NULL)
15546 plt_list = &h->elf.plt.plist;
15547 else if (local_got_ents != NULL)
15548 {
15549 struct plt_entry **local_plt = (struct plt_entry **)
15550 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 15551 plt_list = local_plt + r_symndx;
cbf95972
AM
15552 }
15553 if (plt_list)
15554 {
15555 struct plt_entry *ent;
5663e321
AM
15556 bfd_vma sym_addend = orig_rel.r_addend;
15557
15558 if (r_type == R_PPC64_PLT_PCREL34
15559 || r_type == R_PPC64_PLT_PCREL34_NOTOC)
15560 sym_addend = 0;
65f38f15 15561
cbf95972
AM
15562 for (ent = *plt_list; ent != NULL; ent = ent->next)
15563 if (ent->plt.offset != (bfd_vma) -1
5663e321 15564 && ent->addend == sym_addend)
cbf95972
AM
15565 {
15566 asection *plt;
08be3224 15567 bfd_vma got;
cbf95972
AM
15568
15569 plt = htab->elf.splt;
15570 if (!htab->elf.dynamic_sections_created
15571 || h == NULL
15572 || h->elf.dynindx == -1)
2d7ad24e
AM
15573 {
15574 if (h != NULL
15575 ? h->elf.type == STT_GNU_IFUNC
15576 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15577 plt = htab->elf.iplt;
15578 else
15579 plt = htab->pltlocal;
15580 }
15581 relocation = (plt->output_section->vma
15582 + plt->output_offset
15583 + ent->plt.offset);
08be3224 15584 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
15585 || r_type == R_PPC64_PLT16_HI
15586 || r_type == R_PPC64_PLT16_LO
15587 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
15588 {
15589 got = (elf_gp (output_bfd)
15590 + htab->sec_info[input_section->id].toc_off);
15591 relocation -= got;
15592 }
5663e321
AM
15593 if (r_type != R_PPC64_PLT_PCREL34
15594 && r_type != R_PPC64_PLT_PCREL34_NOTOC)
15595 addend = 0;
cbf95972
AM
15596 unresolved_reloc = FALSE;
15597 break;
15598 }
15599 }
15600 }
65f38f15 15601 break;
5bd4f169 15602
0b13192e
AM
15603 case R_PPC64_TOC:
15604 /* Relocation value is TOC base. */
15605 relocation = TOCstart;
cf35638d 15606 if (r_symndx == STN_UNDEF)
6f20ed8a 15607 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
15608 else if (unresolved_reloc)
15609 ;
6f20ed8a
AM
15610 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15611 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
15612 else
15613 unresolved_reloc = TRUE;
ab96bf03 15614 goto dodyn;
0b13192e 15615
5bd4f169
AM
15616 /* TOC16 relocs. We want the offset relative to the TOC base,
15617 which is the address of the start of the TOC plus 0x8000.
15618 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15619 in this order. */
65f38f15
AM
15620 case R_PPC64_TOC16:
15621 case R_PPC64_TOC16_LO:
15622 case R_PPC64_TOC16_HI:
15623 case R_PPC64_TOC16_DS:
15624 case R_PPC64_TOC16_LO_DS:
15625 case R_PPC64_TOC16_HA:
6f20ed8a 15626 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
15627 break;
15628
15629 /* Relocate against the beginning of the section. */
65f38f15
AM
15630 case R_PPC64_SECTOFF:
15631 case R_PPC64_SECTOFF_LO:
15632 case R_PPC64_SECTOFF_HI:
15633 case R_PPC64_SECTOFF_DS:
15634 case R_PPC64_SECTOFF_LO_DS:
15635 case R_PPC64_SECTOFF_HA:
4ce794b7 15636 if (sec != NULL)
65f38f15 15637 addend -= sec->output_section->vma;
5bd4f169
AM
15638 break;
15639
25f23106
AM
15640 case R_PPC64_REL16:
15641 case R_PPC64_REL16_LO:
15642 case R_PPC64_REL16_HI:
15643 case R_PPC64_REL16_HA:
4a969973
AM
15644 case R_PPC64_REL16_HIGH:
15645 case R_PPC64_REL16_HIGHA:
15646 case R_PPC64_REL16_HIGHER:
15647 case R_PPC64_REL16_HIGHERA:
15648 case R_PPC64_REL16_HIGHEST:
15649 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
15650 case R_PPC64_REL16_HIGHER34:
15651 case R_PPC64_REL16_HIGHERA34:
15652 case R_PPC64_REL16_HIGHEST34:
15653 case R_PPC64_REL16_HIGHESTA34:
a680de9a 15654 case R_PPC64_REL16DX_HA:
721956f4
AM
15655 case R_PPC64_REL14:
15656 case R_PPC64_REL14_BRNTAKEN:
15657 case R_PPC64_REL14_BRTAKEN:
5d1634d7 15658 case R_PPC64_REL24:
05d0e962 15659 case R_PPC64_REL24_NOTOC:
5663e321
AM
15660 case R_PPC64_PCREL34:
15661 case R_PPC64_PCREL28:
5d1634d7
AM
15662 break;
15663
411e1bfb
AM
15664 case R_PPC64_TPREL16:
15665 case R_PPC64_TPREL16_LO:
15666 case R_PPC64_TPREL16_HI:
15667 case R_PPC64_TPREL16_HA:
15668 case R_PPC64_TPREL16_DS:
15669 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
15670 case R_PPC64_TPREL16_HIGH:
15671 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
15672 case R_PPC64_TPREL16_HIGHER:
15673 case R_PPC64_TPREL16_HIGHERA:
15674 case R_PPC64_TPREL16_HIGHEST:
15675 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
15676 if (h != NULL
15677 && h->elf.root.type == bfd_link_hash_undefweak
15678 && h->elf.dynindx == -1)
15679 {
15680 /* Make this relocation against an undefined weak symbol
15681 resolve to zero. This is really just a tweak, since
15682 code using weak externs ought to check that they are
15683 defined before using them. */
15684 bfd_byte *p = contents + rel->r_offset - d_offset;
15685
95f0d0d2 15686 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
15687 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15688 if (insn != 0)
95f0d0d2 15689 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
15690 break;
15691 }
989f9879
AM
15692 if (htab->elf.tls_sec != NULL)
15693 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
15694 /* The TPREL16 relocs shouldn't really be used in shared
15695 libs or with non-local symbols as that will result in
15696 DT_TEXTREL being set, but support them anyway. */
15697 goto dodyn;
411e1bfb
AM
15698
15699 case R_PPC64_DTPREL16:
15700 case R_PPC64_DTPREL16_LO:
15701 case R_PPC64_DTPREL16_HI:
15702 case R_PPC64_DTPREL16_HA:
15703 case R_PPC64_DTPREL16_DS:
15704 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
15705 case R_PPC64_DTPREL16_HIGH:
15706 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
15707 case R_PPC64_DTPREL16_HIGHER:
15708 case R_PPC64_DTPREL16_HIGHERA:
15709 case R_PPC64_DTPREL16_HIGHEST:
15710 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
15711 if (htab->elf.tls_sec != NULL)
15712 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
15713 break;
15714
45965137
AM
15715 case R_PPC64_ADDR64_LOCAL:
15716 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15717 ? h->elf.other
15718 : sym->st_other);
15719 break;
15720
e515b051
AM
15721 case R_PPC64_DTPMOD64:
15722 relocation = 1;
15723 addend = 0;
15724 goto dodyn;
15725
411e1bfb 15726 case R_PPC64_TPREL64:
989f9879
AM
15727 if (htab->elf.tls_sec != NULL)
15728 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
15729 goto dodyn;
15730
15731 case R_PPC64_DTPREL64:
989f9879
AM
15732 if (htab->elf.tls_sec != NULL)
15733 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 15734 /* Fall through. */
411e1bfb 15735
65f38f15
AM
15736 /* Relocations that may need to be propagated if this is a
15737 dynamic object. */
04c9666a 15738 case R_PPC64_REL30:
65f38f15
AM
15739 case R_PPC64_REL32:
15740 case R_PPC64_REL64:
15741 case R_PPC64_ADDR14:
15742 case R_PPC64_ADDR14_BRNTAKEN:
15743 case R_PPC64_ADDR14_BRTAKEN:
15744 case R_PPC64_ADDR16:
15745 case R_PPC64_ADDR16_DS:
15746 case R_PPC64_ADDR16_HA:
15747 case R_PPC64_ADDR16_HI:
f9c6b907
AM
15748 case R_PPC64_ADDR16_HIGH:
15749 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15750 case R_PPC64_ADDR16_HIGHER:
15751 case R_PPC64_ADDR16_HIGHERA:
15752 case R_PPC64_ADDR16_HIGHEST:
15753 case R_PPC64_ADDR16_HIGHESTA:
15754 case R_PPC64_ADDR16_LO:
15755 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
15756 case R_PPC64_ADDR16_HIGHER34:
15757 case R_PPC64_ADDR16_HIGHERA34:
15758 case R_PPC64_ADDR16_HIGHEST34:
15759 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 15760 case R_PPC64_ADDR24:
65f38f15
AM
15761 case R_PPC64_ADDR32:
15762 case R_PPC64_ADDR64:
15763 case R_PPC64_UADDR16:
15764 case R_PPC64_UADDR32:
15765 case R_PPC64_UADDR64:
5663e321
AM
15766 case R_PPC64_D34:
15767 case R_PPC64_D34_LO:
15768 case R_PPC64_D34_HI30:
15769 case R_PPC64_D34_HA30:
15770 case R_PPC64_D28:
411e1bfb 15771 dodyn:
5d1634d7 15772 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
15773 break;
15774
41bd81ab
AM
15775 if (NO_OPD_RELOCS && is_opd)
15776 break;
15777
8a9e8e72 15778 if (bfd_link_pic (info)
b1b07054
AM
15779 ? ((h == NULL
15780 || h->dyn_relocs != NULL)
15781 && ((h != NULL && pc_dynrelocs (h))
15782 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
15783 : (h != NULL
15784 ? h->dyn_relocs != NULL
d311bc8b 15785 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 15786 {
b34976b6 15787 bfd_boolean skip, relocate;
65f38f15 15788 asection *sreloc;
1cf1f670 15789 bfd_vma out_off;
82e66161 15790 long indx = 0;
65f38f15
AM
15791
15792 /* When generating a dynamic object, these relocations
15793 are copied into the output file to be resolved at run
15794 time. */
15795
b34976b6
AM
15796 skip = FALSE;
15797 relocate = FALSE;
65f38f15 15798
1cf1f670
AM
15799 out_off = _bfd_elf_section_offset (output_bfd, info,
15800 input_section, rel->r_offset);
15801 if (out_off == (bfd_vma) -1)
b34976b6 15802 skip = TRUE;
1cf1f670 15803 else if (out_off == (bfd_vma) -2)
b34976b6 15804 skip = TRUE, relocate = TRUE;
1cf1f670
AM
15805 out_off += (input_section->output_section->vma
15806 + input_section->output_offset);
15807 outrel.r_offset = out_off;
411e1bfb 15808 outrel.r_addend = rel->r_addend;
65f38f15 15809
1cf1f670
AM
15810 /* Optimize unaligned reloc use. */
15811 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15812 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15813 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15814 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15815 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15816 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15817 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15818 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15819 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15820
65f38f15 15821 if (skip)
0bb2d96a 15822 memset (&outrel, 0, sizeof outrel);
afe397ea 15823 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
15824 && !is_opd
15825 && r_type != R_PPC64_TOC)
14acf4dc 15826 {
82e66161
AM
15827 indx = h->elf.dynindx;
15828 BFD_ASSERT (indx != -1);
15829 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 15830 }
65f38f15
AM
15831 else
15832 {
41bd81ab
AM
15833 /* This symbol is local, or marked to become local,
15834 or this is an opd section reloc which must point
15835 at a local function. */
65f38f15 15836 outrel.r_addend += relocation;
e86ce104 15837 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 15838 {
3fad3c7c 15839 if (is_opd && h != NULL)
afbe61cf
AM
15840 {
15841 /* Lie about opd entries. This case occurs
15842 when building shared libraries and we
15843 reference a function in another shared
3fad3c7c
AM
15844 lib. The same thing happens for a weak
15845 definition in an application that's
15846 overridden by a strong definition in a
15847 shared lib. (I believe this is a generic
15848 bug in binutils handling of weak syms.)
15849 In these cases we won't use the opd
1e2f5b6e 15850 entry in this lib. */
b34976b6 15851 unresolved_reloc = FALSE;
afbe61cf 15852 }
25f23106
AM
15853 if (!is_opd
15854 && r_type == R_PPC64_ADDR64
15855 && (h != NULL
15856 ? h->elf.type == STT_GNU_IFUNC
15857 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15858 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15859 else
15860 {
15861 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 15862
25f23106
AM
15863 /* We need to relocate .opd contents for ld.so.
15864 Prelink also wants simple and consistent rules
15865 for relocs. This make all RELATIVE relocs have
15866 *r_offset equal to r_addend. */
15867 relocate = TRUE;
15868 }
65f38f15
AM
15869 }
15870 else
15871 {
25f23106
AM
15872 if (h != NULL
15873 ? h->elf.type == STT_GNU_IFUNC
15874 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15875 {
25f53a85 15876 info->callbacks->einfo
695344c0 15877 /* xgettext:c-format */
174d0a74 15878 (_("%H: %s for indirect "
c1c8c1ef 15879 "function `%pT' unsupported\n"),
25f53a85 15880 input_bfd, input_section, rel->r_offset,
25f23106
AM
15881 ppc64_elf_howto_table[r_type]->name,
15882 sym_name);
15883 ret = FALSE;
15884 }
cf35638d 15885 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
15886 ;
15887 else if (sec == NULL || sec->owner == NULL)
15888 {
15889 bfd_set_error (bfd_error_bad_value);
b34976b6 15890 return FALSE;
65f38f15
AM
15891 }
15892 else
15893 {
f26a3287 15894 asection *osec = sec->output_section;
65f38f15 15895
f26a3287
AM
15896 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
15897 {
15898 /* TLS symbol values are relative to the
15899 TLS segment. Dynamic relocations for
15900 local TLS symbols therefore can't be
15901 reduced to a relocation against their
15902 section symbol because it holds the
15903 address of the section, not a value
15904 relative to the TLS segment. We could
15905 change the .tdata dynamic section symbol
15906 to be zero value but STN_UNDEF works
15907 and is used elsewhere, eg. for TPREL64
15908 GOT relocs against local TLS symbols. */
15909 osec = htab->elf.tls_sec;
15910 indx = 0;
15911 }
15912 else
74541ad4 15913 {
74541ad4 15914 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
15915 if (indx == 0)
15916 {
15917 if ((osec->flags & SEC_READONLY) == 0
15918 && htab->elf.data_index_section != NULL)
15919 osec = htab->elf.data_index_section;
15920 else
15921 osec = htab->elf.text_index_section;
15922 indx = elf_section_data (osec)->dynindx;
15923 }
15924 BFD_ASSERT (indx != 0);
74541ad4 15925 }
74541ad4 15926
65f38f15
AM
15927 /* We are turning this relocation into one
15928 against a section symbol, so subtract out
15929 the output section's address but not the
15930 offset of the input section in the output
15931 section. */
15932 outrel.r_addend -= osec->vma;
15933 }
15934
15935 outrel.r_info = ELF64_R_INFO (indx, r_type);
15936 }
15937 }
15938
15939 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
15940 if (h != NULL
15941 ? h->elf.type == STT_GNU_IFUNC
15942 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
15943 {
15944 sreloc = htab->elf.irelplt;
15945 if (indx == 0)
15946 htab->local_ifunc_resolver = 1;
15947 else if (is_static_defined (&h->elf))
15948 htab->maybe_local_ifunc_resolver = 1;
15949 }
65f38f15
AM
15950 if (sreloc == NULL)
15951 abort ();
15952
dfbb6ac9
AM
15953 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15954 >= sreloc->size)
15955 abort ();
947216bf
AM
15956 loc = sreloc->contents;
15957 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
15958 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15959
15960 /* If this reloc is against an external symbol, it will
15961 be computed at runtime, so there's no need to do
81407a69
AM
15962 anything now. However, for the sake of prelink ensure
15963 that the section contents are a known value. */
2cdcc330 15964 if (!relocate)
81407a69
AM
15965 {
15966 unresolved_reloc = FALSE;
15967 /* The value chosen here is quite arbitrary as ld.so
15968 ignores section contents except for the special
15969 case of .opd where the contents might be accessed
15970 before relocation. Choose zero, as that won't
15971 cause reloc overflow. */
15972 relocation = 0;
15973 addend = 0;
15974 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15975 to improve backward compatibility with older
15976 versions of ld. */
15977 if (r_type == R_PPC64_ADDR64)
15978 addend = outrel.r_addend;
15979 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 15980 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 15981 addend = outrel.r_offset;
81407a69 15982 }
65f38f15 15983 }
5bd4f169
AM
15984 break;
15985
65f38f15
AM
15986 case R_PPC64_COPY:
15987 case R_PPC64_GLOB_DAT:
15988 case R_PPC64_JMP_SLOT:
25f23106 15989 case R_PPC64_JMP_IREL:
65f38f15
AM
15990 case R_PPC64_RELATIVE:
15991 /* We shouldn't ever see these dynamic relocs in relocatable
15992 files. */
ae9a127f 15993 /* Fall through. */
65f38f15
AM
15994
15995 case R_PPC64_PLTGOT16:
15996 case R_PPC64_PLTGOT16_DS:
15997 case R_PPC64_PLTGOT16_HA:
15998 case R_PPC64_PLTGOT16_HI:
15999 case R_PPC64_PLTGOT16_LO:
16000 case R_PPC64_PLTGOT16_LO_DS:
16001 case R_PPC64_PLTREL32:
16002 case R_PPC64_PLTREL64:
16003 /* These ones haven't been implemented yet. */
16004
25f53a85 16005 info->callbacks->einfo
695344c0 16006 /* xgettext:c-format */
c1c8c1ef 16007 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 16008 input_bfd,
4ce794b7 16009 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
16010
16011 bfd_set_error (bfd_error_invalid_operation);
b34976b6 16012 ret = FALSE;
c316a17c 16013 goto copy_reloc;
65f38f15 16014 }
5bd4f169 16015
67f0cbdb
AM
16016 /* Multi-instruction sequences that access the TOC can be
16017 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 16018 to nop; addi rb,r2,x; */
67f0cbdb
AM
16019 switch (r_type)
16020 {
16021 default:
16022 break;
16023
16024 case R_PPC64_GOT_TLSLD16_HI:
16025 case R_PPC64_GOT_TLSGD16_HI:
16026 case R_PPC64_GOT_TPREL16_HI:
16027 case R_PPC64_GOT_DTPREL16_HI:
16028 case R_PPC64_GOT16_HI:
16029 case R_PPC64_TOC16_HI:
16030 /* These relocs would only be useful if building up an
16031 offset to later add to r2, perhaps in an indexed
16032 addressing mode instruction. Don't try to optimize.
16033 Unfortunately, the possibility of someone building up an
16034 offset like this or even with the HA relocs, means that
16035 we need to check the high insn when optimizing the low
16036 insn. */
16037 break;
16038
5663e321
AM
16039 case R_PPC64_PLTCALL_NOTOC:
16040 if (!unresolved_reloc)
16041 htab->notoc_plt = 1;
16042 /* Fall through. */
23cedd1d
AM
16043 case R_PPC64_PLTCALL:
16044 if (unresolved_reloc)
16045 {
16046 /* No plt entry. Make this into a direct call. */
16047 bfd_byte *p = contents + rel->r_offset;
16048 insn = bfd_get_32 (input_bfd, p);
16049 insn &= 1;
16050 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
16051 if (r_type == R_PPC64_PLTCALL)
16052 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
16053 unresolved_reloc = save_unresolved_reloc;
16054 r_type = R_PPC64_REL24;
16055 }
16056 break;
16057
5663e321 16058 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
16059 case R_PPC64_PLTSEQ:
16060 if (unresolved_reloc)
16061 {
16062 unresolved_reloc = FALSE;
16063 goto nop_it;
16064 }
16065 break;
16066
5663e321
AM
16067 case R_PPC64_PLT_PCREL34_NOTOC:
16068 if (!unresolved_reloc)
16069 htab->notoc_plt = 1;
16070 /* Fall through. */
16071 case R_PPC64_PLT_PCREL34:
16072 if (unresolved_reloc)
16073 {
16074 bfd_byte *p = contents + rel->r_offset;
16075 bfd_put_32 (input_bfd, PNOP >> 32, p);
16076 bfd_put_32 (input_bfd, PNOP, p + 4);
16077 unresolved_reloc = FALSE;
16078 goto copy_reloc;
16079 }
16080 break;
16081
23cedd1d
AM
16082 case R_PPC64_PLT16_HA:
16083 if (unresolved_reloc)
16084 {
16085 unresolved_reloc = FALSE;
16086 goto nop_it;
16087 }
16088 /* Fall through. */
67f0cbdb
AM
16089 case R_PPC64_GOT_TLSLD16_HA:
16090 case R_PPC64_GOT_TLSGD16_HA:
16091 case R_PPC64_GOT_TPREL16_HA:
16092 case R_PPC64_GOT_DTPREL16_HA:
16093 case R_PPC64_GOT16_HA:
16094 case R_PPC64_TOC16_HA:
98528052 16095 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16096 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 16097 {
23cedd1d
AM
16098 bfd_byte *p;
16099 nop_it:
16100 p = contents + (rel->r_offset & ~3);
98528052 16101 bfd_put_32 (input_bfd, NOP, p);
d830549d 16102 goto copy_reloc;
98528052 16103 }
67f0cbdb
AM
16104 break;
16105
23cedd1d
AM
16106 case R_PPC64_PLT16_LO:
16107 case R_PPC64_PLT16_LO_DS:
16108 if (unresolved_reloc)
16109 {
16110 unresolved_reloc = FALSE;
16111 goto nop_it;
16112 }
16113 /* Fall through. */
67f0cbdb
AM
16114 case R_PPC64_GOT_TLSLD16_LO:
16115 case R_PPC64_GOT_TLSGD16_LO:
16116 case R_PPC64_GOT_TPREL16_LO_DS:
16117 case R_PPC64_GOT_DTPREL16_LO_DS:
16118 case R_PPC64_GOT16_LO:
16119 case R_PPC64_GOT16_LO_DS:
16120 case R_PPC64_TOC16_LO:
16121 case R_PPC64_TOC16_LO_DS:
98528052 16122 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16123 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
16124 {
16125 bfd_byte *p = contents + (rel->r_offset & ~3);
16126 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
16127 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16128 {
16129 /* Transform addic to addi when we change reg. */
16130 insn &= ~((0x3f << 26) | (0x1f << 16));
16131 insn |= (14u << 26) | (2 << 16);
16132 }
16133 else
67f0cbdb 16134 {
98528052
AM
16135 insn &= ~(0x1f << 16);
16136 insn |= 2 << 16;
67f0cbdb 16137 }
560c8763 16138 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
16139 }
16140 break;
9a23f96e
AM
16141
16142 case R_PPC64_TPREL16_HA:
16143 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16144 {
16145 bfd_byte *p = contents + (rel->r_offset & ~3);
16146 insn = bfd_get_32 (input_bfd, p);
16147 if ((insn & ((0x3f << 26) | 0x1f << 16))
16148 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16149 /* xgettext:c-format */
16150 info->callbacks->minfo
16151 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
16152 input_bfd, input_section, rel->r_offset,
16153 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 16154 else
d830549d
AM
16155 {
16156 bfd_put_32 (input_bfd, NOP, p);
16157 goto copy_reloc;
16158 }
9a23f96e
AM
16159 }
16160 break;
16161
16162 case R_PPC64_TPREL16_LO:
16163 case R_PPC64_TPREL16_LO_DS:
16164 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16165 {
16166 bfd_byte *p = contents + (rel->r_offset & ~3);
16167 insn = bfd_get_32 (input_bfd, p);
16168 insn &= ~(0x1f << 16);
16169 insn |= 13 << 16;
16170 bfd_put_32 (input_bfd, insn, p);
16171 }
16172 break;
67f0cbdb
AM
16173 }
16174
65f38f15
AM
16175 /* Do any further special processing. */
16176 switch (r_type)
16177 {
16178 default:
16179 break;
16180
25f23106 16181 case R_PPC64_REL16_HA:
4a969973
AM
16182 case R_PPC64_REL16_HIGHA:
16183 case R_PPC64_REL16_HIGHERA:
16184 case R_PPC64_REL16_HIGHESTA:
a680de9a 16185 case R_PPC64_REL16DX_HA:
f9c6b907
AM
16186 case R_PPC64_ADDR16_HA:
16187 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
16188 case R_PPC64_ADDR16_HIGHERA:
16189 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
16190 case R_PPC64_TOC16_HA:
16191 case R_PPC64_SECTOFF_HA:
411e1bfb 16192 case R_PPC64_TPREL16_HA:
f9c6b907 16193 case R_PPC64_TPREL16_HIGHA:
411e1bfb 16194 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 16195 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
16196 case R_PPC64_DTPREL16_HA:
16197 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 16198 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 16199 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
16200 /* It's just possible that this symbol is a weak symbol
16201 that's not actually defined anywhere. In that case,
16202 'sec' would be NULL, and we should leave the symbol
16203 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
16204 if (sec == NULL)
16205 break;
1a0670f3 16206 /* Fall through. */
5c5f6e17
AM
16207
16208 case R_PPC64_GOT16_HA:
16209 case R_PPC64_PLTGOT16_HA:
16210 case R_PPC64_PLT16_HA:
16211 case R_PPC64_GOT_TLSGD16_HA:
16212 case R_PPC64_GOT_TLSLD16_HA:
16213 case R_PPC64_GOT_TPREL16_HA:
16214 case R_PPC64_GOT_DTPREL16_HA:
16215 /* Add 0x10000 if sign bit in 0:15 is set.
16216 Bits 0:15 are not used. */
16217 addend += 0x8000;
65f38f15
AM
16218 break;
16219
5663e321
AM
16220 case R_PPC64_D34_HA30:
16221 case R_PPC64_ADDR16_HIGHERA34:
16222 case R_PPC64_ADDR16_HIGHESTA34:
16223 case R_PPC64_REL16_HIGHERA34:
16224 case R_PPC64_REL16_HIGHESTA34:
16225 if (sec != NULL)
16226 addend += 1ULL << 33;
16227 break;
16228
65f38f15
AM
16229 case R_PPC64_ADDR16_DS:
16230 case R_PPC64_ADDR16_LO_DS:
16231 case R_PPC64_GOT16_DS:
16232 case R_PPC64_GOT16_LO_DS:
16233 case R_PPC64_PLT16_LO_DS:
16234 case R_PPC64_SECTOFF_DS:
16235 case R_PPC64_SECTOFF_LO_DS:
16236 case R_PPC64_TOC16_DS:
16237 case R_PPC64_TOC16_LO_DS:
16238 case R_PPC64_PLTGOT16_DS:
16239 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
16240 case R_PPC64_GOT_TPREL16_DS:
16241 case R_PPC64_GOT_TPREL16_LO_DS:
16242 case R_PPC64_GOT_DTPREL16_DS:
16243 case R_PPC64_GOT_DTPREL16_LO_DS:
16244 case R_PPC64_TPREL16_DS:
16245 case R_PPC64_TPREL16_LO_DS:
16246 case R_PPC64_DTPREL16_DS:
16247 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
16248 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16249 mask = 3;
a680de9a
PB
16250 /* If this reloc is against an lq, lxv, or stxv insn, then
16251 the value must be a multiple of 16. This is somewhat of
16252 a hack, but the "correct" way to do this by defining _DQ
16253 forms of all the _DS relocs bloats all reloc switches in
16254 this file. It doesn't make much sense to use these
16255 relocs in data, so testing the insn should be safe. */
16256 if ((insn & (0x3f << 26)) == (56u << 26)
16257 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 16258 mask = 15;
a680de9a
PB
16259 relocation += addend;
16260 addend = insn & (mask ^ 3);
16261 if ((relocation & mask) != 0)
65f38f15 16262 {
a680de9a 16263 relocation ^= relocation & mask;
25f53a85 16264 info->callbacks->einfo
695344c0 16265 /* xgettext:c-format */
174d0a74 16266 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 16267 input_bfd, input_section, rel->r_offset,
d830549d 16268 ppc64_elf_howto_table[r_type]->name,
adadcc0c 16269 mask + 1);
65f38f15 16270 bfd_set_error (bfd_error_bad_value);
b34976b6 16271 ret = FALSE;
c316a17c 16272 goto copy_reloc;
65f38f15
AM
16273 }
16274 break;
5bd4f169
AM
16275 }
16276
239e1f3a
AM
16277 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16278 because such sections are not SEC_ALLOC and thus ld.so will
16279 not process them. */
d830549d 16280 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 16281 if (unresolved_reloc
239e1f3a 16282 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
16283 && h->elf.def_dynamic)
16284 && _bfd_elf_section_offset (output_bfd, info, input_section,
16285 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 16286 {
25f53a85 16287 info->callbacks->einfo
695344c0 16288 /* xgettext:c-format */
c1c8c1ef 16289 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 16290 input_bfd, input_section, rel->r_offset,
b80eed39 16291 howto->name,
039b3fef 16292 h->elf.root.root.string);
b34976b6 16293 ret = FALSE;
9c07fe7c 16294 }
5bd4f169 16295
b80eed39
AM
16296 /* 16-bit fields in insns mostly have signed values, but a
16297 few insns have 16-bit unsigned values. Really, we should
16298 have different reloc types. */
16299 if (howto->complain_on_overflow != complain_overflow_dont
16300 && howto->dst_mask == 0xffff
16301 && (input_section->flags & SEC_CODE) != 0)
16302 {
16303 enum complain_overflow complain = complain_overflow_signed;
16304
16305 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
16306 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16307 complain = complain_overflow_bitfield;
16308 else if (howto->rightshift == 0
16309 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16310 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16311 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16312 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16313 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16314 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
16315 complain = complain_overflow_unsigned;
16316 if (howto->complain_on_overflow != complain)
16317 {
16318 alt_howto = *howto;
16319 alt_howto.complain_on_overflow = complain;
16320 howto = &alt_howto;
16321 }
16322 }
16323
5663e321 16324 switch (r_type)
a680de9a 16325 {
5663e321
AM
16326 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
16327 case R_PPC64_D34:
16328 case R_PPC64_D34_LO:
16329 case R_PPC64_D34_HI30:
16330 case R_PPC64_D34_HA30:
16331 case R_PPC64_PCREL34:
16332 case R_PPC64_GOT_PCREL34:
16333 case R_PPC64_PLT_PCREL34:
16334 case R_PPC64_PLT_PCREL34_NOTOC:
16335 case R_PPC64_D28:
16336 case R_PPC64_PCREL28:
16337 if (rel->r_offset + 8 > input_section->size)
16338 r = bfd_reloc_outofrange;
16339 else
16340 {
5663e321
AM
16341 relocation += addend;
16342 if (howto->pc_relative)
16343 relocation -= (rel->r_offset
16344 + input_section->output_offset
16345 + input_section->output_section->vma);
16346 relocation >>= howto->rightshift;
16347
16348 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16349 pinsn <<= 32;
16350 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16351
16352 pinsn &= ~howto->dst_mask;
16353 pinsn |= (((relocation << 16) | (relocation & 0xffff))
16354 & howto->dst_mask);
16355 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
16356 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
16357 r = bfd_reloc_ok;
16358 if (howto->complain_on_overflow == complain_overflow_signed
16359 && (relocation + (1ULL << (howto->bitsize - 1))
16360 >= 1ULL << howto->bitsize))
16361 r = bfd_reloc_overflow;
16362 }
16363 break;
16364
16365 case R_PPC64_REL16DX_HA:
a680de9a
PB
16366 if (rel->r_offset + 4 > input_section->size)
16367 r = bfd_reloc_outofrange;
16368 else
16369 {
16370 relocation += addend;
16371 relocation -= (rel->r_offset
16372 + input_section->output_offset
16373 + input_section->output_section->vma);
3de43e7b 16374 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
16375 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16376 insn &= ~0x1fffc1;
3de43e7b 16377 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
16378 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16379 r = bfd_reloc_ok;
3de43e7b 16380 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
16381 r = bfd_reloc_overflow;
16382 }
5663e321
AM
16383 break;
16384
16385 default:
16386 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
16387 contents, rel->r_offset,
16388 relocation, addend);
a680de9a 16389 }
5bd4f169 16390
ef60b7ff 16391 if (r != bfd_reloc_ok)
5bd4f169 16392 {
bc30df16 16393 char *more_info = NULL;
b80eed39 16394 const char *reloc_name = howto->name;
bc30df16
AM
16395
16396 if (reloc_dest != DEST_NORMAL)
16397 {
16398 more_info = bfd_malloc (strlen (reloc_name) + 8);
16399 if (more_info != NULL)
16400 {
16401 strcpy (more_info, reloc_name);
16402 strcat (more_info, (reloc_dest == DEST_OPD
16403 ? " (OPD)" : " (stub)"));
16404 reloc_name = more_info;
16405 }
16406 }
16407
cd27b276 16408 if (r == bfd_reloc_overflow)
5bd4f169 16409 {
8131c122
AM
16410 /* On code like "if (foo) foo();" don't report overflow
16411 on a branch to zero when foo is undefined. */
16412 if (!warned
16413 && (reloc_dest == DEST_STUB
16414 || !(h != NULL
16415 && (h->elf.root.type == bfd_link_hash_undefweak
16416 || h->elf.root.type == bfd_link_hash_undefined)
16417 && is_branch_reloc (r_type))))
1a72702b
AM
16418 info->callbacks->reloc_overflow (info, &h->elf.root,
16419 sym_name, reloc_name,
16420 orig_rel.r_addend,
16421 input_bfd, input_section,
16422 rel->r_offset);
ef60b7ff
AM
16423 }
16424 else
16425 {
25f53a85 16426 info->callbacks->einfo
695344c0 16427 /* xgettext:c-format */
c1c8c1ef 16428 (_("%H: %s against `%pT': error %d\n"),
25f53a85 16429 input_bfd, input_section, rel->r_offset,
bc30df16 16430 reloc_name, sym_name, (int) r);
b34976b6 16431 ret = FALSE;
ef60b7ff 16432 }
bc30df16
AM
16433 if (more_info != NULL)
16434 free (more_info);
5bd4f169 16435 }
c316a17c
AM
16436 copy_reloc:
16437 if (wrel != rel)
16438 *wrel = *rel;
16439 }
16440
16441 if (wrel != rel)
16442 {
16443 Elf_Internal_Shdr *rel_hdr;
16444 size_t deleted = rel - wrel;
16445
16446 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16447 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16448 if (rel_hdr->sh_size == 0)
16449 {
16450 /* It is too late to remove an empty reloc section. Leave
16451 one NONE reloc.
16452 ??? What is wrong with an empty section??? */
16453 rel_hdr->sh_size = rel_hdr->sh_entsize;
16454 deleted -= 1;
16455 }
16456 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16457 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16458 input_section->reloc_count -= deleted;
5bd4f169
AM
16459 }
16460
645ea6a9
AM
16461 /* If we're emitting relocations, then shortly after this function
16462 returns, reloc offsets and addends for this section will be
16463 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
16464 file rather than the input. Save a copy of the relocs for
16465 opd_entry_value. */
0e1862bb 16466 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
16467 {
16468 bfd_size_type amt;
16469 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16470 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
16471 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16472 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
16473 if (rel == NULL)
16474 return FALSE;
16475 memcpy (rel, relocs, amt);
16476 }
5bd4f169
AM
16477 return ret;
16478}
16479
754021d0
AM
16480/* Adjust the value of any local symbols in opd sections. */
16481
6e0b88f1 16482static int
754021d0
AM
16483ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16484 const char *name ATTRIBUTE_UNUSED,
16485 Elf_Internal_Sym *elfsym,
16486 asection *input_sec,
16487 struct elf_link_hash_entry *h)
16488{
74f0fb50
AM
16489 struct _opd_sec_data *opd;
16490 long adjust;
754021d0
AM
16491 bfd_vma value;
16492
4025353c 16493 if (h != NULL)
6e0b88f1 16494 return 1;
4025353c 16495
74f0fb50
AM
16496 opd = get_opd_info (input_sec);
16497 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 16498 return 1;
754021d0
AM
16499
16500 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 16501 if (!bfd_link_relocatable (info))
754021d0
AM
16502 value -= input_sec->output_section->vma;
16503
51aecdc5 16504 adjust = opd->adjust[OPD_NDX (value)];
4025353c 16505 if (adjust == -1)
6e0b88f1
AM
16506 return 2;
16507
16508 elfsym->st_value += adjust;
16509 return 1;
754021d0
AM
16510}
16511
5bd4f169
AM
16512/* Finish up dynamic symbol handling. We set the contents of various
16513 dynamic sections here. */
16514
b34976b6 16515static bfd_boolean
4ce794b7
AM
16516ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16517 struct bfd_link_info *info,
16518 struct elf_link_hash_entry *h,
4aef7643 16519 Elf_Internal_Sym *sym)
5bd4f169 16520{
65f38f15 16521 struct ppc_link_hash_table *htab;
8387904d 16522 struct plt_entry *ent;
5bd4f169 16523
65f38f15 16524 htab = ppc_hash_table (info);
4dfe6ac6
NC
16525 if (htab == NULL)
16526 return FALSE;
5bd4f169 16527
49c09209
AM
16528 if (!htab->opd_abi && !h->def_regular)
16529 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16530 if (ent->plt.offset != (bfd_vma) -1)
16531 {
16532 /* Mark the symbol as undefined, rather than as
16533 defined in glink. Leave the value if there were
16534 any relocations where pointer equality matters
16535 (this is a clue for the dynamic linker, to make
16536 function pointer comparisons work between an
16537 application and shared library), otherwise set it
16538 to zero. */
16539 sym->st_shndx = SHN_UNDEF;
16540 if (!h->pointer_equality_needed)
16541 sym->st_value = 0;
16542 else if (!h->ref_regular_nonweak)
16543 {
16544 /* This breaks function pointer comparisons, but
16545 that is better than breaking tests for a NULL
16546 function pointer. */
16547 sym->st_value = 0;
16548 }
16549 break;
16550 }
5bd4f169 16551
f5385ebf 16552 if (h->needs_copy)
5bd4f169 16553 {
65f38f15 16554 /* This symbol needs a copy reloc. Set it up. */
49c09209 16555 Elf_Internal_Rela rela;
5474d94f 16556 asection *srel;
49c09209 16557 bfd_byte *loc;
5bd4f169 16558
65f38f15
AM
16559 if (h->dynindx == -1
16560 || (h->root.type != bfd_link_hash_defined
16561 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
16562 || htab->elf.srelbss == NULL
16563 || htab->elf.sreldynrelro == NULL)
65f38f15 16564 abort ();
5bd4f169
AM
16565
16566 rela.r_offset = (h->root.u.def.value
16567 + h->root.u.def.section->output_section->vma
16568 + h->root.u.def.section->output_offset);
16569 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16570 rela.r_addend = 0;
afbf7e8e 16571 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
16572 srel = htab->elf.sreldynrelro;
16573 else
16574 srel = htab->elf.srelbss;
16575 loc = srel->contents;
16576 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 16577 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
16578 }
16579
b34976b6 16580 return TRUE;
5bd4f169
AM
16581}
16582
65f38f15
AM
16583/* Used to decide how to sort relocs in an optimal manner for the
16584 dynamic linker, before writing them out. */
16585
16586static enum elf_reloc_type_class
7e612e98
AM
16587ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16588 const asection *rel_sec,
16589 const Elf_Internal_Rela *rela)
65f38f15 16590{
04c9666a 16591 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
16592 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16593
33e44f2e 16594 if (rel_sec == htab->elf.irelplt)
7e612e98 16595 return reloc_class_ifunc;
a33d1f77 16596
4ce794b7 16597 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 16598 switch (r_type)
65f38f15
AM
16599 {
16600 case R_PPC64_RELATIVE:
16601 return reloc_class_relative;
16602 case R_PPC64_JMP_SLOT:
16603 return reloc_class_plt;
16604 case R_PPC64_COPY:
16605 return reloc_class_copy;
16606 default:
16607 return reloc_class_normal;
16608 }
16609}
16610
5bd4f169
AM
16611/* Finish up the dynamic sections. */
16612
b34976b6 16613static bfd_boolean
4ce794b7
AM
16614ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16615 struct bfd_link_info *info)
5bd4f169 16616{
65f38f15
AM
16617 struct ppc_link_hash_table *htab;
16618 bfd *dynobj;
5bd4f169 16619 asection *sdyn;
5bd4f169 16620
65f38f15 16621 htab = ppc_hash_table (info);
4dfe6ac6
NC
16622 if (htab == NULL)
16623 return FALSE;
16624
65f38f15 16625 dynobj = htab->elf.dynobj;
3d4d4302 16626 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 16627
65f38f15 16628 if (htab->elf.dynamic_sections_created)
5bd4f169 16629 {
5bd4f169
AM
16630 Elf64_External_Dyn *dyncon, *dynconend;
16631
33e44f2e 16632 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 16633 abort ();
5bd4f169
AM
16634
16635 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 16636 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
16637 for (; dyncon < dynconend; dyncon++)
16638 {
16639 Elf_Internal_Dyn dyn;
19397422 16640 asection *s;
5bd4f169
AM
16641
16642 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16643
16644 switch (dyn.d_tag)
16645 {
65f38f15
AM
16646 default:
16647 continue;
5bd4f169 16648
5d1634d7 16649 case DT_PPC64_GLINK:
4ce794b7 16650 s = htab->glink;
6348e046 16651 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
16652 /* We stupidly defined DT_PPC64_GLINK to be the start
16653 of glink rather than the first entry point, which is
16654 what ld.so needs, and now have a bigger stub to
16655 support automatic multiple TOCs. */
9e390558 16656 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
16657 break;
16658
19397422
AM
16659 case DT_PPC64_OPD:
16660 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16661 if (s == NULL)
16662 continue;
16663 dyn.d_un.d_ptr = s->vma;
19397422
AM
16664 break;
16665
e8910a83 16666 case DT_PPC64_OPT:
5663e321
AM
16667 if ((htab->do_multi_toc && htab->multi_toc_needed)
16668 || htab->notoc_plt)
e8910a83 16669 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
16670 if (htab->has_plt_localentry0)
16671 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
16672 break;
16673
19397422
AM
16674 case DT_PPC64_OPDSZ:
16675 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16676 if (s == NULL)
16677 continue;
eea6121a 16678 dyn.d_un.d_val = s->size;
19397422
AM
16679 break;
16680
65f38f15 16681 case DT_PLTGOT:
33e44f2e 16682 s = htab->elf.splt;
6348e046 16683 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
16684 break;
16685
16686 case DT_JMPREL:
33e44f2e 16687 s = htab->elf.srelplt;
6348e046 16688 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 16689 break;
5bd4f169 16690
65f38f15 16691 case DT_PLTRELSZ:
33e44f2e 16692 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 16693 break;
82e66161
AM
16694
16695 case DT_TEXTREL:
16696 if (htab->local_ifunc_resolver)
16697 info->callbacks->einfo
16698 (_("%X%P: text relocations and GNU indirect "
16699 "functions will result in a segfault at runtime\n"));
16700 else if (htab->maybe_local_ifunc_resolver)
16701 info->callbacks->einfo
16702 (_("%P: warning: text relocations and GNU indirect "
16703 "functions may result in a segfault at runtime\n"));
16704 continue;
5bd4f169 16705 }
5bd4f169 16706
65f38f15 16707 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 16708 }
5bd4f169
AM
16709 }
16710
6528b6eb
AM
16711 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16712 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16713 {
16714 /* Fill in the first entry in the global offset table.
16715 We use it to hold the link-time TOCbase. */
16716 bfd_put_64 (output_bfd,
60ee0d4a 16717 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 16718 htab->elf.sgot->contents);
5d1634d7
AM
16719
16720 /* Set .got entry size. */
2cdcc330
AM
16721 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16722 = 8;
5d1634d7
AM
16723 }
16724
6528b6eb
AM
16725 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16726 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16727 {
16728 /* Set .plt entry size. */
33e44f2e 16729 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 16730 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
16731 }
16732
84f5d08e
AM
16733 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16734 brlt ourselves if emitrelocations. */
16735 if (htab->brlt != NULL
16736 && htab->brlt->reloc_count != 0
16737 && !_bfd_elf_link_output_relocs (output_bfd,
16738 htab->brlt,
d4730f92 16739 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
16740 elf_section_data (htab->brlt)->relocs,
16741 NULL))
16742 return FALSE;
16743
176a0d42
AM
16744 if (htab->glink != NULL
16745 && htab->glink->reloc_count != 0
16746 && !_bfd_elf_link_output_relocs (output_bfd,
16747 htab->glink,
d4730f92 16748 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
16749 elf_section_data (htab->glink)->relocs,
16750 NULL))
16751 return FALSE;
16752
da44f4e5 16753
df136d64
AM
16754 if (htab->glink_eh_frame != NULL
16755 && htab->glink_eh_frame->size != 0
16756 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16757 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16758 htab->glink_eh_frame,
16759 htab->glink_eh_frame->contents))
16760 return FALSE;
58d180e8 16761
e717da7e 16762 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
16763 since we didn't add them to DYNOBJ. We know dynobj is the first
16764 bfd. */
c72f2fb2 16765 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
16766 {
16767 asection *s;
7b53ace3 16768
0c8d6e5c 16769 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
16770 continue;
16771
e717da7e
AM
16772 s = ppc64_elf_tdata (dynobj)->got;
16773 if (s != NULL
eea6121a 16774 && s->size != 0
e717da7e
AM
16775 && s->output_section != bfd_abs_section_ptr
16776 && !bfd_set_section_contents (output_bfd, s->output_section,
16777 s->contents, s->output_offset,
eea6121a 16778 s->size))
e717da7e
AM
16779 return FALSE;
16780 s = ppc64_elf_tdata (dynobj)->relgot;
16781 if (s != NULL
eea6121a 16782 && s->size != 0
e717da7e
AM
16783 && s->output_section != bfd_abs_section_ptr
16784 && !bfd_set_section_contents (output_bfd, s->output_section,
16785 s->contents, s->output_offset,
eea6121a 16786 s->size))
e717da7e
AM
16787 return FALSE;
16788 }
f6c52c13 16789
b34976b6 16790 return TRUE;
5bd4f169
AM
16791}
16792
5bd4f169 16793#include "elf64-target.h"
7b8e7dad
AM
16794
16795/* FreeBSD support */
16796
16797#undef TARGET_LITTLE_SYM
16798#undef TARGET_LITTLE_NAME
16799
16800#undef TARGET_BIG_SYM
6d00b590 16801#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
16802#undef TARGET_BIG_NAME
16803#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16804
16805#undef ELF_OSABI
16806#define ELF_OSABI ELFOSABI_FREEBSD
16807
16808#undef elf64_bed
16809#define elf64_bed elf64_powerpc_fbsd_bed
16810
16811#include "elf64-target.h"
This page took 2.859834 seconds and 4 git commands to generate.