PowerPC PIC vs. DLL TLS issues
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
82704155 2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
5bd4f169
AM
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 5 Largely rewritten by Alan Modra.
5bd4f169 6
ae9a127f 7 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 8
ae9a127f
NC
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
cd123cb7 11 the Free Software Foundation; either version 3 of the License, or
ae9a127f 12 (at your option) any later version.
5bd4f169 13
ae9a127f
NC
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
5bd4f169 18
4ce794b7
AM
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 22
cd123cb7 23
4ce794b7
AM
24/* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 27
3db64b00 28#include "sysdep.h"
183e98be 29#include <stdarg.h>
5bd4f169 30#include "bfd.h"
5bd4f169
AM
31#include "bfdlink.h"
32#include "libbfd.h"
33#include "elf-bfd.h"
04c9666a 34#include "elf/ppc64.h"
5d1634d7 35#include "elf64-ppc.h"
58d180e8 36#include "dwarf2.h"
5bd4f169 37
805fc799 38static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
40static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 42static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 44static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 46static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 48static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
5663e321
AM
54static bfd_reloc_status_type ppc64_elf_prefix_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 56static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 58static bfd_vma opd_entry_value
aef36ac1 59 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
5bd4f169 60
6d00b590 61#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
ad8e1ba5 62#define TARGET_LITTLE_NAME "elf64-powerpcle"
6d00b590 63#define TARGET_BIG_SYM powerpc_elf64_vec
ad8e1ba5
AM
64#define TARGET_BIG_NAME "elf64-powerpc"
65#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 66#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
67#define ELF_MACHINE_CODE EM_PPC64
68#define ELF_MAXPAGESIZE 0x10000
702d1671
AM
69#define ELF_COMMONPAGESIZE 0x1000
70#define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
ad8e1ba5
AM
71#define elf_info_to_howto ppc64_elf_info_to_howto
72
73#define elf_backend_want_got_sym 0
74#define elf_backend_want_plt_sym 0
75#define elf_backend_plt_alignment 3
76#define elf_backend_plt_not_loaded 1
ad8e1ba5 77#define elf_backend_got_header_size 8
5474d94f 78#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
79#define elf_backend_can_gc_sections 1
80#define elf_backend_can_refcount 1
81#define elf_backend_rela_normal 1
64f52338 82#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 83#define elf_backend_default_execstack 0
ad8e1ba5 84
e717da7e 85#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 86#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 87#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
88#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
89#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
90#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
91#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 92#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 93#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 94#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
95
96#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
97#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
98#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 99#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 100#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 101#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 102#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 103#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 104#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 105#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 106#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 107#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 108#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 109#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 110#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
111#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
112#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 113#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
ad8e1ba5
AM
114#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
115#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 116#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 117#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 118#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
119#define elf_backend_relocate_section ppc64_elf_relocate_section
120#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
121#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
122#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 123#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 124#define elf_backend_special_sections ppc64_elf_special_sections
6911b7dc 125#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 126#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 127#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 128
5bd4f169
AM
129/* The name of the dynamic interpreter. This is put in the .interp
130 section. */
131#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
132
133/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 134#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
2d7ad24e 135#define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
5bd4f169
AM
136
137/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 138#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 139
a078d95a
AM
140/* Offsets to some stack save slots. */
141#define STK_LR 16
142#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 143/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
144 CR save slot. Used only by optimised __tls_get_addr call stub,
145 relying on __tls_get_addr_opt not saving CR.. */
146#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
147
5bd4f169 148/* TOC base pointers offset from start of TOC. */
411e1bfb 149#define TOC_BASE_OFF 0x8000
a27e685f
AM
150/* TOC base alignment. */
151#define TOC_BASE_ALIGN 256
411e1bfb
AM
152
153/* Offset of tp and dtp pointers from start of TLS block. */
154#define TP_OFFSET 0x7000
155#define DTP_OFFSET 0x8000
5bd4f169 156
ad8e1ba5
AM
157/* .plt call stub instructions. The normal stub is like this, but
158 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 159 insert an addi to adjust r11. */
a078d95a 160#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
161#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
162#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
163#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
164#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
165#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
166#define BCTR 0x4e800420 /* bctr */
167
07d6d2b8 168#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
05d0e962
AM
169#define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
170#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
07d6d2b8
AM
171#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
172#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
173
174#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
175#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
176#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
177#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
178#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
179#define BNECTR 0x4ca20420 /* bnectr+ */
180#define BNECTR_P4 0x4ce20420 /* bnectr+ */
794e51c0 181
71a39c98 182#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 183#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 184#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 185
07d6d2b8
AM
186#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
187#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
188#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 189
04bdff6a 190#define LI_R11_0 0x39600000 /* li %r11,0 */
07d6d2b8 191#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
04bdff6a 192#define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
05d0e962 193#define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
006589cf
AM
194#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
195#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
05d0e962 196#define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
a345bc8d 197#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
05d0e962 198#define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
04bdff6a 199#define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
05d0e962 200#define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
07d6d2b8 201#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
04bdff6a 202#define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
05d0e962
AM
203#define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
204#define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
205#define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
04bdff6a
AM
206#define PADDI_R12_PC 0x0610000039800000ULL
207#define PLD_R12_PC 0x04100000e5800000ULL
5663e321 208#define PNOP 0x0700000000000000ULL
a345bc8d 209
9e390558
AM
210/* __glink_PLTresolve stub instructions. We enter with the index in R0. */
211#define GLINK_PLTRESOLVE_SIZE(htab) \
407aa07c 212 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
ee4bf8d2
AM
213 /* 0: */
214 /* .quad plt0-1f */
215 /* __glink: */
216#define MFLR_R12 0x7d8802a6 /* mflr %12 */
217#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
218 /* 1: */
219#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 220 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 221#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
222#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
223 /* ld %12,0(%11) */
224 /* ld %2,8(%11) */
225 /* mtctr %12 */
226 /* ld %11,16(%11) */
ee4bf8d2 227 /* bctr */
b9e5796b
AM
228#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
229#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
230#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
231#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
232#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
233
234/* Pad with this. */
235#define NOP 0x60000000
236
721956f4
AM
237/* Some other nops. */
238#define CROR_151515 0x4def7b82
239#define CROR_313131 0x4ffffb82
240
cedb70c5 241/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
242#define LI_R0_0 0x38000000 /* li %r0,0 */
243#define B_DOT 0x48000000 /* b . */
244
245/* After that, we need two instructions to load the index, followed by
246 a branch. */
247#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 248#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 249
deb0e272
AM
250/* Instructions used by the save and restore reg functions. */
251#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
252#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
253#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
254#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
255#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
256#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
257#define LI_R12_0 0x39800000 /* li %r12,0 */
258#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
259#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
260#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
261#define BLR 0x4e800020 /* blr */
262
41bd81ab
AM
263/* Since .opd is an array of descriptors and each entry will end up
264 with identical R_PPC64_RELATIVE relocs, there is really no need to
265 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 266 relocate .opd without reloc entries. */
41bd81ab
AM
267#ifndef NO_OPD_RELOCS
268#define NO_OPD_RELOCS 0
269#endif
810d4e75 270
a4b6fadd
AM
271#ifndef ARRAY_SIZE
272#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
273#endif
274
810d4e75
AM
275static inline int
276abiversion (bfd *abfd)
277{
278 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
279}
280
281static inline void
282set_abiversion (bfd *abfd, int ver)
283{
284 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
285 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
286}
5bd4f169
AM
287\f
288/* Relocation HOWTO's. */
46807bf4
AM
289/* Like other ELF RELA targets that don't apply multiple
290 field-altering relocations to the same localation, src_mask is
291 always zero and pcrel_offset is the same as pc_relative.
292 PowerPC can always use a zero bitpos, even when the field is not at
293 the LSB. For example, a REL24 could use rightshift=2, bisize=24
294 and bitpos=2 which matches the ABI description, or as we do here,
295 rightshift=0, bitsize=26 and bitpos=0. */
296#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
297 complain, special_func) \
298 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
299 complain_overflow_ ## complain, special_func, \
300 #type, FALSE, 0, mask, pc_relative)
301
04c9666a 302static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 303
f3185997
NC
304static reloc_howto_type ppc64_elf_howto_raw[] =
305{
5bd4f169 306 /* This reloc does nothing. */
46807bf4
AM
307 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
308 bfd_elf_generic_reloc),
5bd4f169
AM
309
310 /* A standard 32 bit relocation. */
46807bf4
AM
311 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
312 bfd_elf_generic_reloc),
5bd4f169
AM
313
314 /* An absolute 26 bit branch; the lower two bits must be zero.
315 FIXME: we don't check that, we just clear them. */
46807bf4
AM
316 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
317 bfd_elf_generic_reloc),
5bd4f169
AM
318
319 /* A standard 16 bit relocation. */
46807bf4
AM
320 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
321 bfd_elf_generic_reloc),
5bd4f169
AM
322
323 /* A 16 bit relocation without overflow. */
46807bf4
AM
324 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
325 bfd_elf_generic_reloc),
5bd4f169
AM
326
327 /* Bits 16-31 of an address. */
46807bf4
AM
328 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
329 bfd_elf_generic_reloc),
5bd4f169
AM
330
331 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
332 bits, treated as a signed number, is negative. */
46807bf4
AM
333 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
334 ppc64_elf_ha_reloc),
5bd4f169
AM
335
336 /* An absolute 16 bit branch; the lower two bits must be zero.
337 FIXME: we don't check that, we just clear them. */
46807bf4
AM
338 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
339 ppc64_elf_branch_reloc),
5bd4f169
AM
340
341 /* An absolute 16 bit branch, for which bit 10 should be set to
342 indicate that the branch is expected to be taken. The lower two
343 bits must be zero. */
46807bf4
AM
344 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
345 ppc64_elf_brtaken_reloc),
5bd4f169
AM
346
347 /* An absolute 16 bit branch, for which bit 10 should be set to
348 indicate that the branch is not expected to be taken. The lower
349 two bits must be zero. */
46807bf4
AM
350 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
351 ppc64_elf_brtaken_reloc),
5bd4f169
AM
352
353 /* A relative 26 bit branch; the lower two bits must be zero. */
46807bf4
AM
354 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
355 ppc64_elf_branch_reloc),
5bd4f169 356
05d0e962 357 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
46807bf4
AM
358 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
359 ppc64_elf_branch_reloc),
05d0e962 360
5bd4f169 361 /* A relative 16 bit branch; the lower two bits must be zero. */
46807bf4
AM
362 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
363 ppc64_elf_branch_reloc),
5bd4f169
AM
364
365 /* A relative 16 bit branch. Bit 10 should be set to indicate that
366 the branch is expected to be taken. The lower two bits must be
367 zero. */
46807bf4
AM
368 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
369 ppc64_elf_brtaken_reloc),
5bd4f169
AM
370
371 /* A relative 16 bit branch. Bit 10 should be set to indicate that
372 the branch is not expected to be taken. The lower two bits must
373 be zero. */
46807bf4
AM
374 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
375 ppc64_elf_brtaken_reloc),
5bd4f169
AM
376
377 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
378 symbol. */
46807bf4
AM
379 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
380 ppc64_elf_unhandled_reloc),
5bd4f169
AM
381
382 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
383 the symbol. */
46807bf4
AM
384 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
385 ppc64_elf_unhandled_reloc),
5bd4f169
AM
386
387 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
388 the symbol. */
46807bf4
AM
389 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
390 ppc64_elf_unhandled_reloc),
5bd4f169
AM
391
392 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
393 the symbol. */
46807bf4
AM
394 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
395 ppc64_elf_unhandled_reloc),
5bd4f169
AM
396
397 /* This is used only by the dynamic linker. The symbol should exist
398 both in the object being run and in some shared library. The
399 dynamic linker copies the data addressed by the symbol from the
400 shared library into the object, because the object being
401 run has to have the data at some particular address. */
46807bf4
AM
402 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
403 ppc64_elf_unhandled_reloc),
5bd4f169
AM
404
405 /* Like R_PPC64_ADDR64, but used when setting global offset table
406 entries. */
46807bf4
AM
407 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
408 ppc64_elf_unhandled_reloc),
5bd4f169
AM
409
410 /* Created by the link editor. Marks a procedure linkage table
411 entry for a symbol. */
46807bf4
AM
412 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
413 ppc64_elf_unhandled_reloc),
5bd4f169
AM
414
415 /* Used only by the dynamic linker. When the object is run, this
416 doubleword64 is set to the load address of the object, plus the
417 addend. */
46807bf4
AM
418 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
419 bfd_elf_generic_reloc),
5bd4f169
AM
420
421 /* Like R_PPC64_ADDR32, but may be unaligned. */
46807bf4
AM
422 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
423 bfd_elf_generic_reloc),
5bd4f169
AM
424
425 /* Like R_PPC64_ADDR16, but may be unaligned. */
46807bf4
AM
426 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
427 bfd_elf_generic_reloc),
5bd4f169
AM
428
429 /* 32-bit PC relative. */
46807bf4
AM
430 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
431 bfd_elf_generic_reloc),
5bd4f169 432
10ed1bba 433 /* 32-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
434 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
435 ppc64_elf_unhandled_reloc),
5bd4f169
AM
436
437 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
438 FIXME: R_PPC64_PLTREL32 not supported. */
46807bf4
AM
439 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
440 ppc64_elf_unhandled_reloc),
5bd4f169
AM
441
442 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
443 the symbol. */
46807bf4
AM
444 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
445 ppc64_elf_unhandled_reloc),
5bd4f169
AM
446
447 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
448 the symbol. */
46807bf4
AM
449 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
450 ppc64_elf_unhandled_reloc),
5bd4f169
AM
451
452 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
453 the symbol. */
46807bf4
AM
454 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
455 ppc64_elf_unhandled_reloc),
5bd4f169 456
c061c2d8 457 /* 16-bit section relative relocation. */
46807bf4
AM
458 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
459 ppc64_elf_sectoff_reloc),
5bd4f169 460
c061c2d8 461 /* Like R_PPC64_SECTOFF, but no overflow warning. */
46807bf4
AM
462 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
463 ppc64_elf_sectoff_reloc),
5bd4f169
AM
464
465 /* 16-bit upper half section relative relocation. */
46807bf4
AM
466 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
467 ppc64_elf_sectoff_reloc),
5bd4f169
AM
468
469 /* 16-bit upper half adjusted section relative relocation. */
46807bf4
AM
470 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
471 ppc64_elf_sectoff_ha_reloc),
5bd4f169 472
04c9666a 473 /* Like R_PPC64_REL24 without touching the two least significant bits. */
46807bf4
AM
474 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
475 bfd_elf_generic_reloc),
5bd4f169
AM
476
477 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
478
479 /* A standard 64-bit relocation. */
46807bf4
AM
480 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
481 bfd_elf_generic_reloc),
5bd4f169
AM
482
483 /* The bits 32-47 of an address. */
46807bf4
AM
484 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
485 bfd_elf_generic_reloc),
5bd4f169
AM
486
487 /* The bits 32-47 of an address, plus 1 if the contents of the low
488 16 bits, treated as a signed number, is negative. */
46807bf4
AM
489 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
490 ppc64_elf_ha_reloc),
5bd4f169
AM
491
492 /* The bits 48-63 of an address. */
46807bf4
AM
493 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
494 bfd_elf_generic_reloc),
5bd4f169
AM
495
496 /* The bits 48-63 of an address, plus 1 if the contents of the low
497 16 bits, treated as a signed number, is negative. */
46807bf4
AM
498 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
499 ppc64_elf_ha_reloc),
5bd4f169
AM
500
501 /* Like ADDR64, but may be unaligned. */
46807bf4
AM
502 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
503 bfd_elf_generic_reloc),
5bd4f169
AM
504
505 /* 64-bit relative relocation. */
46807bf4
AM
506 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
507 bfd_elf_generic_reloc),
5bd4f169 508
cedb70c5 509 /* 64-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
510 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
511 ppc64_elf_unhandled_reloc),
5bd4f169
AM
512
513 /* 64-bit PC relative relocation to the symbol's procedure linkage
514 table. */
515 /* FIXME: R_PPC64_PLTREL64 not supported. */
46807bf4
AM
516 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
517 ppc64_elf_unhandled_reloc),
5bd4f169
AM
518
519 /* 16 bit TOC-relative relocation. */
5bd4f169 520 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
46807bf4
AM
521 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
522 ppc64_elf_toc_reloc),
5bd4f169
AM
523
524 /* 16 bit TOC-relative relocation without overflow. */
5bd4f169 525 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
46807bf4
AM
526 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
527 ppc64_elf_toc_reloc),
5bd4f169
AM
528
529 /* 16 bit TOC-relative relocation, high 16 bits. */
5bd4f169 530 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
46807bf4
AM
531 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
532 ppc64_elf_toc_reloc),
5bd4f169
AM
533
534 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
535 contents of the low 16 bits, treated as a signed number, is
536 negative. */
5bd4f169 537 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
46807bf4
AM
538 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
539 ppc64_elf_toc_ha_reloc),
5bd4f169
AM
540
541 /* 64-bit relocation; insert value of TOC base (.TOC.). */
5bd4f169 542 /* R_PPC64_TOC 51 doubleword64 .TOC. */
46807bf4
AM
543 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
544 ppc64_elf_toc64_reloc),
5bd4f169
AM
545
546 /* Like R_PPC64_GOT16, but also informs the link editor that the
547 value to relocate may (!) refer to a PLT entry which the link
548 editor (a) may replace with the symbol value. If the link editor
549 is unable to fully resolve the symbol, it may (b) create a PLT
550 entry and store the address to the new PLT entry in the GOT.
551 This permits lazy resolution of function symbols at run time.
552 The link editor may also skip all of this and just (c) emit a
553 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
554 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
46807bf4
AM
555 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
556 ppc64_elf_unhandled_reloc),
411e1bfb
AM
557
558 /* Like R_PPC64_PLTGOT16, but without overflow. */
559 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
560 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
561 ppc64_elf_unhandled_reloc),
411e1bfb
AM
562
563 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
564 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
46807bf4
AM
565 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
566 ppc64_elf_unhandled_reloc),
411e1bfb
AM
567
568 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
569 1 if the contents of the low 16 bits, treated as a signed number,
570 is negative. */
571 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
46807bf4
AM
572 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
573 ppc64_elf_unhandled_reloc),
411e1bfb
AM
574
575 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
46807bf4
AM
576 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
577 bfd_elf_generic_reloc),
411e1bfb
AM
578
579 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
46807bf4
AM
580 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
581 bfd_elf_generic_reloc),
411e1bfb
AM
582
583 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
46807bf4
AM
584 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
585 ppc64_elf_unhandled_reloc),
411e1bfb
AM
586
587 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
46807bf4
AM
588 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
589 ppc64_elf_unhandled_reloc),
411e1bfb
AM
590
591 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
46807bf4
AM
592 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
593 ppc64_elf_unhandled_reloc),
411e1bfb
AM
594
595 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
46807bf4
AM
596 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
597 ppc64_elf_sectoff_reloc),
411e1bfb
AM
598
599 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
46807bf4
AM
600 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
601 ppc64_elf_sectoff_reloc),
411e1bfb
AM
602
603 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
46807bf4
AM
604 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
605 ppc64_elf_toc_reloc),
411e1bfb
AM
606
607 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
46807bf4
AM
608 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
609 ppc64_elf_toc_reloc),
411e1bfb
AM
610
611 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
612 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
46807bf4
AM
613 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
614 ppc64_elf_unhandled_reloc),
411e1bfb
AM
615
616 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
617 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
618 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
619 ppc64_elf_unhandled_reloc),
411e1bfb 620
727fc41e 621 /* Marker relocs for TLS. */
46807bf4
AM
622 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
623 bfd_elf_generic_reloc),
624
625 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
626 bfd_elf_generic_reloc),
627
628 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
629 bfd_elf_generic_reloc),
727fc41e 630
23cedd1d
AM
631 /* Marker reloc for optimizing r2 save in prologue rather than on
632 each plt call stub. */
46807bf4
AM
633 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
634 bfd_elf_generic_reloc),
3b421ab3 635
23cedd1d 636 /* Marker relocs on inline plt call instructions. */
46807bf4
AM
637 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
638 bfd_elf_generic_reloc),
639
640 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
641 bfd_elf_generic_reloc),
23cedd1d 642
411e1bfb
AM
643 /* Computes the load module index of the load module that contains the
644 definition of its TLS sym. */
46807bf4
AM
645 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
646 ppc64_elf_unhandled_reloc),
411e1bfb
AM
647
648 /* Computes a dtv-relative displacement, the difference between the value
649 of sym+add and the base address of the thread-local storage block that
650 contains the definition of sym, minus 0x8000. */
46807bf4
AM
651 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
652 ppc64_elf_unhandled_reloc),
411e1bfb
AM
653
654 /* A 16 bit dtprel reloc. */
46807bf4
AM
655 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
656 ppc64_elf_unhandled_reloc),
411e1bfb
AM
657
658 /* Like DTPREL16, but no overflow. */
46807bf4
AM
659 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
660 ppc64_elf_unhandled_reloc),
411e1bfb
AM
661
662 /* Like DTPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
663 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
664 ppc64_elf_unhandled_reloc),
411e1bfb
AM
665
666 /* Like DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
667 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
668 ppc64_elf_unhandled_reloc),
411e1bfb
AM
669
670 /* Like DTPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
671 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
672 ppc64_elf_unhandled_reloc),
411e1bfb
AM
673
674 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
675 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
676 ppc64_elf_unhandled_reloc),
411e1bfb
AM
677
678 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
679 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
680 ppc64_elf_unhandled_reloc),
411e1bfb
AM
681
682 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
683 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
684 ppc64_elf_unhandled_reloc),
411e1bfb
AM
685
686 /* Like DTPREL16, but for insns with a DS field. */
46807bf4
AM
687 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
688 ppc64_elf_unhandled_reloc),
411e1bfb
AM
689
690 /* Like DTPREL16_DS, but no overflow. */
46807bf4
AM
691 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
692 ppc64_elf_unhandled_reloc),
411e1bfb
AM
693
694 /* Computes a tp-relative displacement, the difference between the value of
695 sym+add and the value of the thread pointer (r13). */
46807bf4
AM
696 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
697 ppc64_elf_unhandled_reloc),
411e1bfb
AM
698
699 /* A 16 bit tprel reloc. */
46807bf4
AM
700 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
701 ppc64_elf_unhandled_reloc),
411e1bfb
AM
702
703 /* Like TPREL16, but no overflow. */
46807bf4
AM
704 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
705 ppc64_elf_unhandled_reloc),
411e1bfb
AM
706
707 /* Like TPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
708 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
709 ppc64_elf_unhandled_reloc),
411e1bfb
AM
710
711 /* Like TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
712 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
713 ppc64_elf_unhandled_reloc),
411e1bfb
AM
714
715 /* Like TPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
716 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
717 ppc64_elf_unhandled_reloc),
411e1bfb
AM
718
719 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
720 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
721 ppc64_elf_unhandled_reloc),
411e1bfb
AM
722
723 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
724 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
725 ppc64_elf_unhandled_reloc),
411e1bfb
AM
726
727 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
728 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
729 ppc64_elf_unhandled_reloc),
411e1bfb
AM
730
731 /* Like TPREL16, but for insns with a DS field. */
46807bf4
AM
732 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
733 ppc64_elf_unhandled_reloc),
411e1bfb
AM
734
735 /* Like TPREL16_DS, but no overflow. */
46807bf4
AM
736 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
737 ppc64_elf_unhandled_reloc),
411e1bfb
AM
738
739 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
740 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
741 to the first entry relative to the TOC base (r2). */
46807bf4
AM
742 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
743 ppc64_elf_unhandled_reloc),
5bd4f169 744
411e1bfb 745 /* Like GOT_TLSGD16, but no overflow. */
46807bf4
AM
746 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
747 ppc64_elf_unhandled_reloc),
5bd4f169 748
411e1bfb 749 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
46807bf4
AM
750 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
751 ppc64_elf_unhandled_reloc),
5bd4f169 752
411e1bfb 753 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
46807bf4
AM
754 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
755 ppc64_elf_unhandled_reloc),
5bd4f169 756
411e1bfb
AM
757 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
758 with values (sym+add)@dtpmod and zero, and computes the offset to the
759 first entry relative to the TOC base (r2). */
46807bf4
AM
760 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
761 ppc64_elf_unhandled_reloc),
5bd4f169 762
411e1bfb 763 /* Like GOT_TLSLD16, but no overflow. */
46807bf4
AM
764 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
765 ppc64_elf_unhandled_reloc),
5bd4f169 766
411e1bfb 767 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
46807bf4
AM
768 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
769 ppc64_elf_unhandled_reloc),
5bd4f169 770
411e1bfb 771 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
46807bf4
AM
772 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
773 ppc64_elf_unhandled_reloc),
5bd4f169 774
411e1bfb
AM
775 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
776 the offset to the entry relative to the TOC base (r2). */
46807bf4
AM
777 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
778 ppc64_elf_unhandled_reloc),
5bd4f169 779
411e1bfb 780 /* Like GOT_DTPREL16_DS, but no overflow. */
46807bf4
AM
781 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
782 ppc64_elf_unhandled_reloc),
5bd4f169 783
411e1bfb 784 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
785 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
786 ppc64_elf_unhandled_reloc),
5bd4f169 787
411e1bfb 788 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
789 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
790 ppc64_elf_unhandled_reloc),
411e1bfb
AM
791
792 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
793 offset to the entry relative to the TOC base (r2). */
46807bf4
AM
794 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
795 ppc64_elf_unhandled_reloc),
5bd4f169 796
411e1bfb 797 /* Like GOT_TPREL16_DS, but no overflow. */
46807bf4
AM
798 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
799 ppc64_elf_unhandled_reloc),
5bd4f169 800
411e1bfb 801 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
802 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
803 ppc64_elf_unhandled_reloc),
5bd4f169 804
411e1bfb 805 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
806 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
807 ppc64_elf_unhandled_reloc),
808
809 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
810 ppc64_elf_unhandled_reloc),
811
812 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
813 bfd_elf_generic_reloc),
e054468f 814
25f23106 815 /* A 16 bit relative relocation. */
46807bf4
AM
816 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
817 bfd_elf_generic_reloc),
25f23106
AM
818
819 /* A 16 bit relative relocation without overflow. */
46807bf4
AM
820 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
821 bfd_elf_generic_reloc),
25f23106
AM
822
823 /* The high order 16 bits of a relative address. */
46807bf4
AM
824 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
825 bfd_elf_generic_reloc),
25f23106
AM
826
827 /* The high order 16 bits of a relative address, plus 1 if the contents of
828 the low 16 bits, treated as a signed number, is negative. */
46807bf4
AM
829 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
830 ppc64_elf_ha_reloc),
25f23106 831
4a969973
AM
832 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
833 bfd_elf_generic_reloc),
834
835 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
836 ppc64_elf_ha_reloc),
837
838 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
839 bfd_elf_generic_reloc),
840
841 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
842 ppc64_elf_ha_reloc),
843
844 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
845 bfd_elf_generic_reloc),
846
847 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
848 ppc64_elf_ha_reloc),
849
a680de9a 850 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
46807bf4
AM
851 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
852 ppc64_elf_ha_reloc),
a680de9a 853
7ba71655 854 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
46807bf4
AM
855 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
856 ppc64_elf_ha_reloc),
7ba71655 857
f9c6b907 858 /* Like R_PPC64_ADDR16_HI, but no overflow. */
46807bf4
AM
859 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
860 bfd_elf_generic_reloc),
f9c6b907
AM
861
862 /* Like R_PPC64_ADDR16_HA, but no overflow. */
46807bf4
AM
863 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
864 ppc64_elf_ha_reloc),
f9c6b907
AM
865
866 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
46807bf4
AM
867 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
868 ppc64_elf_unhandled_reloc),
f9c6b907
AM
869
870 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
46807bf4
AM
871 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
872 ppc64_elf_unhandled_reloc),
f9c6b907
AM
873
874 /* Like R_PPC64_TPREL16_HI, but no overflow. */
46807bf4
AM
875 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
876 ppc64_elf_unhandled_reloc),
f9c6b907
AM
877
878 /* Like R_PPC64_TPREL16_HA, but no overflow. */
46807bf4
AM
879 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
880 ppc64_elf_unhandled_reloc),
f9c6b907 881
006589cf 882 /* Marker reloc on ELFv2 large-model function entry. */
46807bf4
AM
883 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
884 bfd_elf_generic_reloc),
006589cf 885
45965137 886 /* Like ADDR64, but use local entry point of function. */
46807bf4
AM
887 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
888 bfd_elf_generic_reloc),
45965137 889
5663e321
AM
890 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, FALSE, dont,
891 bfd_elf_generic_reloc),
892
893 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, FALSE, dont,
894 bfd_elf_generic_reloc),
895
896 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, FALSE, dont,
897 bfd_elf_generic_reloc),
898
899 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
900 ppc64_elf_prefix_reloc),
901
902 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, dont,
903 ppc64_elf_prefix_reloc),
904
905 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
906 ppc64_elf_prefix_reloc),
907
908 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
909 ppc64_elf_prefix_reloc),
910
911 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
912 ppc64_elf_prefix_reloc),
913
914 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
915 ppc64_elf_unhandled_reloc),
916
917 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
918 ppc64_elf_unhandled_reloc),
919
920 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
921 ppc64_elf_unhandled_reloc),
922
c213164a
AM
923 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
924 ppc64_elf_unhandled_reloc),
925
926 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
927 ppc64_elf_unhandled_reloc),
928
929 HOW (R_PPC64_GOT_TLSGD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
930 ppc64_elf_unhandled_reloc),
931
932 HOW (R_PPC64_GOT_TLSLD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
933 ppc64_elf_unhandled_reloc),
934
935 HOW (R_PPC64_GOT_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
936 ppc64_elf_unhandled_reloc),
937
938 HOW (R_PPC64_GOT_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
939 ppc64_elf_unhandled_reloc),
940
5663e321
AM
941 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
942 bfd_elf_generic_reloc),
943
944 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
945 ppc64_elf_ha_reloc),
946
947 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
948 bfd_elf_generic_reloc),
949
950 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
951 ppc64_elf_ha_reloc),
952
953 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
954 bfd_elf_generic_reloc),
955
956 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
957 ppc64_elf_ha_reloc),
958
959 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
960 bfd_elf_generic_reloc),
961
962 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
963 ppc64_elf_ha_reloc),
964
965 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
966 ppc64_elf_prefix_reloc),
967
968 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
969 ppc64_elf_prefix_reloc),
970
5bd4f169 971 /* GNU extension to record C++ vtable hierarchy. */
46807bf4
AM
972 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
973 NULL),
5bd4f169
AM
974
975 /* GNU extension to record C++ vtable member usage. */
46807bf4
AM
976 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
977 NULL),
5bd4f169
AM
978};
979
980\f
981/* Initialize the ppc64_elf_howto_table, so that linear accesses can
982 be done. */
983
984static void
4ce794b7 985ppc_howto_init (void)
5bd4f169
AM
986{
987 unsigned int i, type;
988
a4b6fadd 989 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
990 {
991 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 992 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
993 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
994 }
995}
996
997static reloc_howto_type *
f3185997 998ppc64_elf_reloc_type_lookup (bfd *abfd,
4ce794b7 999 bfd_reloc_code_real_type code)
5bd4f169 1000{
411e1bfb 1001 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
1002
1003 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1004 /* Initialize howto table if needed. */
1005 ppc_howto_init ();
1006
4ce794b7 1007 switch (code)
5bd4f169
AM
1008 {
1009 default:
f3185997 1010 /* xgettext:c-format */
2cdcc330
AM
1011 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1012 (int) code);
f3185997 1013 bfd_set_error (bfd_error_bad_value);
4ce794b7 1014 return NULL;
5bd4f169 1015
411e1bfb
AM
1016 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1017 break;
1018 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1019 break;
1020 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1021 break;
1022 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1023 break;
1024 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1025 break;
1026 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1027 break;
f9c6b907
AM
1028 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1029 break;
411e1bfb 1030 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 1031 break;
f9c6b907
AM
1032 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1033 break;
411e1bfb 1034 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 1035 break;
411e1bfb 1036 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 1037 break;
411e1bfb 1038 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 1039 break;
411e1bfb 1040 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 1041 break;
05d0e962
AM
1042 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1043 break;
411e1bfb 1044 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 1045 break;
411e1bfb 1046 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 1047 break;
411e1bfb 1048 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 1049 break;
411e1bfb 1050 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 1051 break;
411e1bfb 1052 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 1053 break;
411e1bfb 1054 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 1055 break;
411e1bfb 1056 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 1057 break;
411e1bfb 1058 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 1059 break;
411e1bfb 1060 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 1061 break;
411e1bfb 1062 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 1063 break;
411e1bfb 1064 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 1065 break;
411e1bfb 1066 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 1067 break;
411e1bfb 1068 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 1069 break;
411e1bfb 1070 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 1071 break;
411e1bfb 1072 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 1073 break;
411e1bfb 1074 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 1075 break;
411e1bfb 1076 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 1077 break;
411e1bfb 1078 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 1079 break;
411e1bfb 1080 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 1081 break;
411e1bfb 1082 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 1083 break;
411e1bfb 1084 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 1085 break;
411e1bfb 1086 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 1087 break;
411e1bfb 1088 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 1089 break;
411e1bfb 1090 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1091 break;
411e1bfb 1092 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1093 break;
411e1bfb 1094 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1095 break;
411e1bfb 1096 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1097 break;
411e1bfb 1098 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1099 break;
411e1bfb 1100 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1101 break;
411e1bfb 1102 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1103 break;
411e1bfb 1104 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1105 break;
411e1bfb 1106 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1107 break;
411e1bfb 1108 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1109 break;
411e1bfb 1110 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1111 break;
411e1bfb 1112 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1113 break;
411e1bfb 1114 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1115 break;
411e1bfb 1116 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1117 break;
411e1bfb 1118 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1119 break;
411e1bfb 1120 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1121 break;
411e1bfb 1122 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1123 break;
411e1bfb 1124 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1125 break;
411e1bfb 1126 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1127 break;
411e1bfb 1128 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1129 break;
411e1bfb 1130 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1131 break;
411e1bfb 1132 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1133 break;
411e1bfb 1134 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1135 break;
411e1bfb 1136 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1137 break;
411e1bfb 1138 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1139 break;
c213164a 1140 case BFD_RELOC_PPC64_TLS_PCREL:
411e1bfb 1141 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1142 break;
727fc41e
AM
1143 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1144 break;
1145 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1146 break;
411e1bfb 1147 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1148 break;
411e1bfb 1149 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1150 break;
411e1bfb 1151 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1152 break;
411e1bfb 1153 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1154 break;
f9c6b907
AM
1155 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1156 break;
411e1bfb 1157 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1158 break;
f9c6b907
AM
1159 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1160 break;
411e1bfb 1161 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1162 break;
411e1bfb
AM
1163 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1164 break;
1165 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1166 break;
1167 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1168 break;
f9c6b907
AM
1169 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1170 break;
411e1bfb
AM
1171 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1172 break;
f9c6b907
AM
1173 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1174 break;
411e1bfb
AM
1175 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1176 break;
1177 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1178 break;
1179 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1180 break;
1181 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1182 break;
1183 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1184 break;
1185 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1186 break;
1187 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1188 break;
1189 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1190 break;
1191 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1192 break;
1193 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1194 break;
1195 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1196 break;
1197 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1198 break;
1199 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1200 break;
1201 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1202 break;
1203 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1204 break;
1205 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1206 break;
1207 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1208 break;
1209 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1210 break;
1211 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1212 break;
1213 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1214 break;
1215 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1216 break;
1217 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1218 break;
1219 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1220 break;
1221 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1222 break;
1223 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1224 break;
1225 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1226 break;
1227 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1228 break;
1229 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1230 break;
1231 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1232 break;
25f23106
AM
1233 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1234 break;
1235 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1236 break;
1237 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1238 break;
1239 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1240 break;
4a969973
AM
1241 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1242 break;
1243 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1244 break;
1245 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1246 break;
1247 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1248 break;
1249 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1250 break;
1251 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1252 break;
7ba71655
AM
1253 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1254 break;
a680de9a
PB
1255 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1256 break;
006589cf
AM
1257 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1258 break;
45965137
AM
1259 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1260 break;
5663e321
AM
1261 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1262 break;
1263 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1264 break;
1265 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1266 break;
1267 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1268 break;
1269 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1270 break;
1271 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1272 break;
1273 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1274 break;
c213164a
AM
1275 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1276 break;
1277 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1278 break;
1279 case BFD_RELOC_PPC64_GOT_TLSGD34: r = R_PPC64_GOT_TLSGD34;
1280 break;
1281 case BFD_RELOC_PPC64_GOT_TLSLD34: r = R_PPC64_GOT_TLSLD34;
1282 break;
1283 case BFD_RELOC_PPC64_GOT_TPREL34: r = R_PPC64_GOT_TPREL34;
1284 break;
1285 case BFD_RELOC_PPC64_GOT_DTPREL34: r = R_PPC64_GOT_DTPREL34;
1286 break;
5663e321
AM
1287 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1288 break;
1289 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1290 break;
1291 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1292 break;
1293 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1294 break;
1295 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1296 break;
1297 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1298 break;
1299 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1300 break;
1301 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1302 break;
1303 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1304 break;
1305 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1306 break;
411e1bfb
AM
1307 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1308 break;
1309 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
1310 break;
1311 }
1312
4ce794b7 1313 return ppc64_elf_howto_table[r];
5bd4f169
AM
1314};
1315
157090f7
AM
1316static reloc_howto_type *
1317ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1318 const char *r_name)
1319{
1320 unsigned int i;
1321
a4b6fadd 1322 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
1323 if (ppc64_elf_howto_raw[i].name != NULL
1324 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1325 return &ppc64_elf_howto_raw[i];
1326
1327 return NULL;
1328}
1329
5bd4f169
AM
1330/* Set the howto pointer for a PowerPC ELF reloc. */
1331
f3185997 1332static bfd_boolean
4aef7643 1333ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 1334 Elf_Internal_Rela *dst)
5bd4f169 1335{
65f38f15
AM
1336 unsigned int type;
1337
ef60b7ff 1338 /* Initialize howto table if needed. */
5bd4f169 1339 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
1340 ppc_howto_init ();
1341
65f38f15 1342 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 1343 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 1344 {
695344c0 1345 /* xgettext:c-format */
0aa13fee 1346 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 1347 abfd, type);
f3185997
NC
1348 bfd_set_error (bfd_error_bad_value);
1349 return FALSE;
d0fb9a8d 1350 }
65f38f15 1351 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
1352 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1353 {
1354 /* xgettext:c-format */
1355 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1356 abfd, type);
1357 bfd_set_error (bfd_error_bad_value);
1358 return FALSE;
1359 }
2cdcc330 1360
f3185997 1361 return TRUE;
5bd4f169
AM
1362}
1363
04c9666a 1364/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
1365
1366static bfd_reloc_status_type
4ce794b7
AM
1367ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1368 void *data, asection *input_section,
1369 bfd *output_bfd, char **error_message)
5bd4f169 1370{
a680de9a
PB
1371 enum elf_ppc64_reloc_type r_type;
1372 long insn;
1373 bfd_size_type octets;
3de43e7b 1374 bfd_vma value;
a680de9a 1375
805fc799
AM
1376 /* If this is a relocatable link (output_bfd test tells us), just
1377 call the generic function. Any adjustment will be done at final
1378 link time. */
1379 if (output_bfd != NULL)
cedb70c5 1380 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1381 input_section, output_bfd, error_message);
1382
5663e321
AM
1383 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1384 We won't actually be using the low bits, so trashing them
805fc799 1385 doesn't matter. */
a680de9a 1386 r_type = reloc_entry->howto->type;
5663e321
AM
1387 if (r_type == R_PPC64_ADDR16_HIGHERA34
1388 || r_type == R_PPC64_ADDR16_HIGHESTA34
1389 || r_type == R_PPC64_REL16_HIGHERA34
1390 || r_type == R_PPC64_REL16_HIGHESTA34)
1391 reloc_entry->addend += 1ULL << 33;
1392 else
1393 reloc_entry->addend += 1U << 15;
a680de9a
PB
1394 if (r_type != R_PPC64_REL16DX_HA)
1395 return bfd_reloc_continue;
1396
1397 value = 0;
1398 if (!bfd_is_com_section (symbol->section))
1399 value = symbol->value;
1400 value += (reloc_entry->addend
1401 + symbol->section->output_offset
1402 + symbol->section->output_section->vma);
1403 value -= (reloc_entry->address
1404 + input_section->output_offset
1405 + input_section->output_section->vma);
3de43e7b 1406 value = (bfd_signed_vma) value >> 16;
a680de9a
PB
1407
1408 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1409 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1410 insn &= ~0x1fffc1;
3de43e7b 1411 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 1412 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 1413 if (value + 0x8000 > 0xffff)
a680de9a
PB
1414 return bfd_reloc_overflow;
1415 return bfd_reloc_ok;
805fc799 1416}
5bd4f169 1417
2441e016
AM
1418static bfd_reloc_status_type
1419ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1420 void *data, asection *input_section,
1421 bfd *output_bfd, char **error_message)
1422{
1423 if (output_bfd != NULL)
1424 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1425 input_section, output_bfd, error_message);
1426
699733f6
AM
1427 if (strcmp (symbol->section->name, ".opd") == 0
1428 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
1429 {
1430 bfd_vma dest = opd_entry_value (symbol->section,
1431 symbol->value + reloc_entry->addend,
aef36ac1 1432 NULL, NULL, FALSE);
2441e016
AM
1433 if (dest != (bfd_vma) -1)
1434 reloc_entry->addend = dest - (symbol->value
1435 + symbol->section->output_section->vma
1436 + symbol->section->output_offset);
1437 }
810d4e75
AM
1438 else
1439 {
1440 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1441
1442 if (symbol->section->owner != abfd
9f284bf9 1443 && symbol->section->owner != NULL
810d4e75
AM
1444 && abiversion (symbol->section->owner) >= 2)
1445 {
1446 unsigned int i;
1447
1448 for (i = 0; i < symbol->section->owner->symcount; ++i)
1449 {
1450 asymbol *symdef = symbol->section->owner->outsymbols[i];
1451
1452 if (strcmp (symdef->name, symbol->name) == 0)
1453 {
1454 elfsym = (elf_symbol_type *) symdef;
1455 break;
1456 }
1457 }
1458 }
1459 reloc_entry->addend
1460 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1461 }
2441e016
AM
1462 return bfd_reloc_continue;
1463}
1464
805fc799 1465static bfd_reloc_status_type
4ce794b7
AM
1466ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1467 void *data, asection *input_section,
1468 bfd *output_bfd, char **error_message)
805fc799
AM
1469{
1470 long insn;
04c9666a 1471 enum elf_ppc64_reloc_type r_type;
805fc799 1472 bfd_size_type octets;
794e51c0
AM
1473 /* Assume 'at' branch hints. */
1474 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
1475
1476 /* If this is a relocatable link (output_bfd test tells us), just
1477 call the generic function. Any adjustment will be done at final
1478 link time. */
5bd4f169 1479 if (output_bfd != NULL)
cedb70c5 1480 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1481 input_section, output_bfd, error_message);
1482
1483 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1484 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1485 insn &= ~(0x01 << 21);
4ce794b7 1486 r_type = reloc_entry->howto->type;
805fc799
AM
1487 if (r_type == R_PPC64_ADDR14_BRTAKEN
1488 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 1489 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 1490
794e51c0 1491 if (is_isa_v2)
5bd4f169 1492 {
805fc799
AM
1493 /* Set 'a' bit. This is 0b00010 in BO field for branch
1494 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1495 for branch on CTR insns (BO == 1a00t or 1a01t). */
1496 if ((insn & (0x14 << 21)) == (0x04 << 21))
1497 insn |= 0x02 << 21;
1498 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1499 insn |= 0x08 << 21;
1500 else
2441e016 1501 goto out;
5bd4f169 1502 }
805fc799
AM
1503 else
1504 {
1505 bfd_vma target = 0;
1506 bfd_vma from;
5bd4f169 1507
805fc799
AM
1508 if (!bfd_is_com_section (symbol->section))
1509 target = symbol->value;
1510 target += symbol->section->output_section->vma;
1511 target += symbol->section->output_offset;
1512 target += reloc_entry->addend;
5bd4f169 1513
805fc799
AM
1514 from = (reloc_entry->address
1515 + input_section->output_offset
1516 + input_section->output_section->vma);
5bd4f169 1517
805fc799
AM
1518 /* Invert 'y' bit if not the default. */
1519 if ((bfd_signed_vma) (target - from) < 0)
1520 insn ^= 0x01 << 21;
1521 }
4ce794b7 1522 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
1523 out:
1524 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1525 input_section, output_bfd, error_message);
805fc799 1526}
5bd4f169 1527
805fc799 1528static bfd_reloc_status_type
4ce794b7
AM
1529ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1530 void *data, asection *input_section,
1531 bfd *output_bfd, char **error_message)
805fc799
AM
1532{
1533 /* If this is a relocatable link (output_bfd test tells us), just
1534 call the generic function. Any adjustment will be done at final
1535 link time. */
1536 if (output_bfd != NULL)
cedb70c5 1537 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 1538 input_section, output_bfd, error_message);
5bd4f169 1539
805fc799
AM
1540 /* Subtract the symbol section base address. */
1541 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
1542 return bfd_reloc_continue;
1543}
1544
805fc799 1545static bfd_reloc_status_type
4ce794b7
AM
1546ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1547 void *data, asection *input_section,
1548 bfd *output_bfd, char **error_message)
805fc799
AM
1549{
1550 /* If this is a relocatable link (output_bfd test tells us), just
1551 call the generic function. Any adjustment will be done at final
1552 link time. */
1553 if (output_bfd != NULL)
cedb70c5 1554 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1555 input_section, output_bfd, error_message);
1556
1557 /* Subtract the symbol section base address. */
1558 reloc_entry->addend -= symbol->section->output_section->vma;
1559
1560 /* Adjust the addend for sign extension of the low 16 bits. */
1561 reloc_entry->addend += 0x8000;
1562 return bfd_reloc_continue;
1563}
1564
1565static bfd_reloc_status_type
4ce794b7
AM
1566ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1567 void *data, asection *input_section,
1568 bfd *output_bfd, char **error_message)
805fc799
AM
1569{
1570 bfd_vma TOCstart;
1571
1572 /* If this is a relocatable link (output_bfd test tells us), just
1573 call the generic function. Any adjustment will be done at final
1574 link time. */
1575 if (output_bfd != NULL)
cedb70c5 1576 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1577 input_section, output_bfd, error_message);
1578
1579 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1580 if (TOCstart == 0)
1c865ab2 1581 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1582
1583 /* Subtract the TOC base address. */
1584 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1585 return bfd_reloc_continue;
1586}
1587
1588static bfd_reloc_status_type
4ce794b7
AM
1589ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1590 void *data, asection *input_section,
1591 bfd *output_bfd, char **error_message)
805fc799
AM
1592{
1593 bfd_vma TOCstart;
1594
1595 /* If this is a relocatable link (output_bfd test tells us), just
1596 call the generic function. Any adjustment will be done at final
1597 link time. */
1598 if (output_bfd != NULL)
cedb70c5 1599 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1600 input_section, output_bfd, error_message);
1601
1602 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1603 if (TOCstart == 0)
1c865ab2 1604 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1605
1606 /* Subtract the TOC base address. */
1607 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1608
1609 /* Adjust the addend for sign extension of the low 16 bits. */
1610 reloc_entry->addend += 0x8000;
1611 return bfd_reloc_continue;
1612}
1613
1614static bfd_reloc_status_type
4ce794b7
AM
1615ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1616 void *data, asection *input_section,
1617 bfd *output_bfd, char **error_message)
805fc799
AM
1618{
1619 bfd_vma TOCstart;
1620 bfd_size_type octets;
1621
1622 /* If this is a relocatable link (output_bfd test tells us), just
1623 call the generic function. Any adjustment will be done at final
1624 link time. */
1625 if (output_bfd != NULL)
cedb70c5 1626 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1627 input_section, output_bfd, error_message);
1628
1629 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1630 if (TOCstart == 0)
1c865ab2 1631 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1632
1633 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1634 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1635 return bfd_reloc_ok;
1636}
1637
5663e321
AM
1638static bfd_reloc_status_type
1639ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1640 void *data, asection *input_section,
1641 bfd *output_bfd, char **error_message)
1642{
1643 uint64_t insn;
1644 bfd_vma targ;
1645
1646 if (output_bfd != NULL)
1647 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1648 input_section, output_bfd, error_message);
1649
1650 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1651 insn <<= 32;
1652 insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1653
1654 targ = (symbol->section->output_section->vma
1655 + symbol->section->output_offset
1656 + reloc_entry->addend);
1657 if (!bfd_is_com_section (symbol->section))
1658 targ += symbol->value;
1659 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1660 targ += 1ULL << 33;
1661 if (reloc_entry->howto->pc_relative)
1662 {
1663 bfd_vma from = (reloc_entry->address
1664 + input_section->output_offset
1665 + input_section->output_section->vma);
1666 targ -=from;
1667 }
1668 targ >>= reloc_entry->howto->rightshift;
1669 insn &= ~reloc_entry->howto->dst_mask;
1670 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1671 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1672 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1673 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1674 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1675 >= 1ULL << reloc_entry->howto->bitsize))
1676 return bfd_reloc_overflow;
1677 return bfd_reloc_ok;
1678}
1679
805fc799 1680static bfd_reloc_status_type
4ce794b7
AM
1681ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1682 void *data, asection *input_section,
1683 bfd *output_bfd, char **error_message)
805fc799
AM
1684{
1685 /* If this is a relocatable link (output_bfd test tells us), just
1686 call the generic function. Any adjustment will be done at final
1687 link time. */
1688 if (output_bfd != NULL)
cedb70c5 1689 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1690 input_section, output_bfd, error_message);
1691
1692 if (error_message != NULL)
1693 {
1694 static char buf[60];
1695 sprintf (buf, "generic linker can't handle %s",
1696 reloc_entry->howto->name);
1697 *error_message = buf;
1698 }
1699 return bfd_reloc_dangerous;
1700}
1701
927be08e
AM
1702/* Track GOT entries needed for a given symbol. We might need more
1703 than one got entry per symbol. */
1704struct got_entry
1705{
1706 struct got_entry *next;
1707
1708 /* The symbol addend that we'll be placing in the GOT. */
1709 bfd_vma addend;
1710
1711 /* Unlike other ELF targets, we use separate GOT entries for the same
1712 symbol referenced from different input files. This is to support
1713 automatic multiple TOC/GOT sections, where the TOC base can vary
1714 from one input file to another. After partitioning into TOC groups
1715 we merge entries within the group.
1716
1717 Point to the BFD owning this GOT entry. */
1718 bfd *owner;
1719
1720 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1721 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1722 unsigned char tls_type;
927be08e
AM
1723
1724 /* Non-zero if got.ent points to real entry. */
f961d9dd 1725 unsigned char is_indirect;
927be08e
AM
1726
1727 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1728 union
2cdcc330
AM
1729 {
1730 bfd_signed_vma refcount;
1731 bfd_vma offset;
1732 struct got_entry *ent;
1733 } got;
927be08e
AM
1734};
1735
1736/* The same for PLT. */
1737struct plt_entry
1738{
1739 struct plt_entry *next;
1740
1741 bfd_vma addend;
1742
1743 union
2cdcc330
AM
1744 {
1745 bfd_signed_vma refcount;
1746 bfd_vma offset;
1747 } plt;
927be08e
AM
1748};
1749
e717da7e
AM
1750struct ppc64_elf_obj_tdata
1751{
1752 struct elf_obj_tdata elf;
1753
1754 /* Shortcuts to dynamic linker sections. */
1755 asection *got;
1756 asection *relgot;
1757
b3fac117
AM
1758 /* Used during garbage collection. We attach global symbols defined
1759 on removed .opd entries to this section so that the sym is removed. */
1760 asection *deleted_section;
81688140 1761
927be08e 1762 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1763 sections means we potentially need one of these for each input bfd. */
927be08e 1764 struct got_entry tlsld_got;
8860955f 1765
2cdcc330
AM
1766 union
1767 {
729eabd5
AM
1768 /* A copy of relocs before they are modified for --emit-relocs. */
1769 Elf_Internal_Rela *relocs;
1770
1771 /* Section contents. */
1772 bfd_byte *contents;
1773 } opd;
d77c8a4b
AM
1774
1775 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1776 the reloc to be in the range -32768 to 32767. */
98528052
AM
1777 unsigned int has_small_toc_reloc : 1;
1778
560c8763
AM
1779 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1780 instruction not one we handle. */
1781 unsigned int unexpected_toc_insn : 1;
066f4018 1782
903b777d
AM
1783 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1784 this file. */
1785 unsigned int has_optrel : 1;
e717da7e
AM
1786};
1787
1788#define ppc64_elf_tdata(bfd) \
1789 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1790
1791#define ppc64_tlsld_got(bfd) \
1792 (&ppc64_elf_tdata (bfd)->tlsld_got)
1793
0c8d6e5c
AM
1794#define is_ppc64_elf(bfd) \
1795 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1796 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1797
e717da7e
AM
1798/* Override the generic function because we store some extras. */
1799
1800static bfd_boolean
1801ppc64_elf_mkobject (bfd *abfd)
1802{
0ffa91dd 1803 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1804 PPC64_ELF_DATA);
e717da7e
AM
1805}
1806
feee612b 1807/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1808 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1809
b34976b6 1810static bfd_boolean
4ce794b7 1811ppc64_elf_object_p (bfd *abfd)
feee612b 1812{
14b57c7c
AM
1813 if (!abfd->arch_info->the_default)
1814 return TRUE;
1815
1816 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1817 {
1818 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1819
1820 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1821 {
1822 /* Relies on arch after 32 bit default being 64 bit default. */
1823 abfd->arch_info = abfd->arch_info->next;
1824 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1825 }
1826 }
14b57c7c 1827 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1828}
1829
d37c89e5
AM
1830/* Support for core dump NOTE sections. */
1831
1832static bfd_boolean
1833ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1834{
eea6121a 1835 size_t offset, size;
d37c89e5
AM
1836
1837 if (note->descsz != 504)
1838 return FALSE;
1839
1840 /* pr_cursig */
228e534f 1841 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1842
1843 /* pr_pid */
228e534f 1844 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1845
1846 /* pr_reg */
1847 offset = 112;
eea6121a 1848 size = 384;
d37c89e5
AM
1849
1850 /* Make a ".reg/999" section. */
1851 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1852 size, note->descpos + offset);
d37c89e5
AM
1853}
1854
1855static bfd_boolean
1856ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1857{
1858 if (note->descsz != 136)
1859 return FALSE;
1860
228e534f 1861 elf_tdata (abfd)->core->pid
bc989cdc 1862 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1863 elf_tdata (abfd)->core->program
d37c89e5 1864 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1865 elf_tdata (abfd)->core->command
d37c89e5
AM
1866 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1867
1868 return TRUE;
1869}
1870
183e98be
AM
1871static char *
1872ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1873 ...)
1874{
1875 switch (note_type)
1876 {
1877 default:
1878 return NULL;
1879
1880 case NT_PRPSINFO:
1881 {
9ef6d1e3 1882 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1883 va_list ap;
1884
1885 va_start (ap, note_type);
75cd47ed 1886 memset (data, 0, sizeof (data));
183e98be 1887 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1888#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1889 DIAGNOSTIC_PUSH;
be3e27bb 1890 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1891 -Wstringop-truncation:
1892 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1893 */
95da9854
L
1894 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1895#endif
183e98be 1896 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1897#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1898 DIAGNOSTIC_POP;
fe75810f 1899#endif
183e98be
AM
1900 va_end (ap);
1901 return elfcore_write_note (abfd, buf, bufsiz,
1902 "CORE", note_type, data, sizeof (data));
1903 }
1904
1905 case NT_PRSTATUS:
1906 {
1907 char data[504];
1908 va_list ap;
1909 long pid;
1910 int cursig;
1911 const void *greg;
1912
1913 va_start (ap, note_type);
1914 memset (data, 0, 112);
1915 pid = va_arg (ap, long);
1916 bfd_put_32 (abfd, pid, data + 32);
1917 cursig = va_arg (ap, int);
1918 bfd_put_16 (abfd, cursig, data + 12);
1919 greg = va_arg (ap, const void *);
1920 memcpy (data + 112, greg, 384);
1921 memset (data + 496, 0, 8);
1922 va_end (ap);
1923 return elfcore_write_note (abfd, buf, bufsiz,
1924 "CORE", note_type, data, sizeof (data));
1925 }
1926 }
1927}
1928
5d35169e
AM
1929/* Add extra PPC sections. */
1930
2cdcc330 1931static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1932{
07d6d2b8
AM
1933 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1934 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1935 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1936 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1937 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1938 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1939 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1940};
1941
7c8fe5c4
AM
1942enum _ppc64_sec_type {
1943 sec_normal = 0,
1944 sec_opd = 1,
1945 sec_toc = 2
1946};
1947
f0abc2a1
AM
1948struct _ppc64_elf_section_data
1949{
1950 struct bfd_elf_section_data elf;
411e1bfb 1951
f0abc2a1
AM
1952 union
1953 {
51aecdc5
AM
1954 /* An array with one entry for each opd function descriptor,
1955 and some spares since opd entries may be either 16 or 24 bytes. */
1956#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
1957 struct _opd_sec_data
1958 {
1959 /* Points to the function code section for local opd entries. */
1960 asection **func_sec;
1961
1962 /* After editing .opd, adjust references to opd local syms. */
1963 long *adjust;
1964 } opd;
7c8fe5c4 1965
3a71aa26
AM
1966 /* An array for toc sections, indexed by offset/8. */
1967 struct _toc_sec_data
1968 {
1969 /* Specifies the relocation symbol index used at a given toc offset. */
1970 unsigned *symndx;
1971
1972 /* And the relocation addend. */
1973 bfd_vma *add;
1974 } toc;
7c8fe5c4
AM
1975 } u;
1976
1977 enum _ppc64_sec_type sec_type:2;
411e1bfb 1978
7c8fe5c4
AM
1979 /* Flag set when small branches are detected. Used to
1980 select suitable defaults for the stub group size. */
1981 unsigned int has_14bit_branch:1;
3e04d765
AM
1982
1983 /* Flag set when PLTCALL relocs are detected. */
1984 unsigned int has_pltcall:1;
066f4018 1985
903b777d
AM
1986 /* Flag set when section has PLT/GOT/TOC relocations that can be
1987 optimised. */
1988 unsigned int has_optrel:1;
f0abc2a1
AM
1989};
1990
1991#define ppc64_elf_section_data(sec) \
411e1bfb 1992 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
1993
1994static bfd_boolean
4ce794b7 1995ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1996{
f592407e
AM
1997 if (!sec->used_by_bfd)
1998 {
1999 struct _ppc64_elf_section_data *sdata;
2000 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 2001
f592407e
AM
2002 sdata = bfd_zalloc (abfd, amt);
2003 if (sdata == NULL)
2004 return FALSE;
2005 sec->used_by_bfd = sdata;
2006 }
f0abc2a1
AM
2007
2008 return _bfd_elf_new_section_hook (abfd, sec);
2009}
4025353c 2010
74f0fb50 2011static struct _opd_sec_data *
4025353c
AM
2012get_opd_info (asection * sec)
2013{
2014 if (sec != NULL
2015 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2016 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2017 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2018 return NULL;
2019}
90e3cdf2
JJ
2020\f
2021/* Parameters for the qsort hook. */
90e3cdf2 2022static bfd_boolean synthetic_relocatable;
cd285db5 2023static asection *synthetic_opd;
90e3cdf2 2024
699733f6 2025/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2026
2027static int
2028compare_symbols (const void *ap, const void *bp)
2029{
2cdcc330
AM
2030 const asymbol *a = *(const asymbol **) ap;
2031 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 2032
699733f6
AM
2033 /* Section symbols first. */
2034 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2035 return -1;
699733f6 2036 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2037 return 1;
2038
699733f6 2039 /* then .opd symbols. */
cd285db5
AM
2040 if (synthetic_opd != NULL)
2041 {
2042 if (strcmp (a->section->name, ".opd") == 0
2043 && strcmp (b->section->name, ".opd") != 0)
2044 return -1;
2045 if (strcmp (a->section->name, ".opd") != 0
2046 && strcmp (b->section->name, ".opd") == 0)
2047 return 1;
2048 }
90e3cdf2 2049
699733f6 2050 /* then other code symbols. */
2cdcc330
AM
2051 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2052 == (SEC_CODE | SEC_ALLOC))
2053 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2054 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2055 return -1;
2056
2cdcc330
AM
2057 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2058 != (SEC_CODE | SEC_ALLOC))
2059 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2060 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2061 return 1;
2062
2063 if (synthetic_relocatable)
2064 {
2065 if (a->section->id < b->section->id)
2066 return -1;
2067
2068 if (a->section->id > b->section->id)
2069 return 1;
2070 }
2071
2072 if (a->value + a->section->vma < b->value + b->section->vma)
2073 return -1;
2074
2075 if (a->value + a->section->vma > b->value + b->section->vma)
2076 return 1;
2077
4d35a0aa
AM
2078 /* For syms with the same value, prefer strong dynamic global function
2079 syms over other syms. */
2080 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2081 return -1;
2082
2083 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2084 return 1;
2085
2086 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2087 return -1;
2088
2089 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2090 return 1;
2091
2092 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2093 return -1;
2094
2095 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2096 return 1;
2097
2098 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2099 return -1;
2100
2101 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2102 return 1;
2103
aaed6f5b 2104 return a > b;
90e3cdf2
JJ
2105}
2106
699733f6 2107/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2108
699733f6 2109static asymbol *
7292b3ac 2110sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 2111{
699733f6 2112 long mid;
90e3cdf2 2113
7292b3ac 2114 if (id == (unsigned) -1)
699733f6
AM
2115 {
2116 while (lo < hi)
2117 {
2118 mid = (lo + hi) >> 1;
2119 if (syms[mid]->value + syms[mid]->section->vma < value)
2120 lo = mid + 1;
2121 else if (syms[mid]->value + syms[mid]->section->vma > value)
2122 hi = mid;
2123 else
2124 return syms[mid];
2125 }
2126 }
2127 else
2128 {
2129 while (lo < hi)
2130 {
2131 mid = (lo + hi) >> 1;
2132 if (syms[mid]->section->id < id)
2133 lo = mid + 1;
2134 else if (syms[mid]->section->id > id)
2135 hi = mid;
2136 else if (syms[mid]->value < value)
2137 lo = mid + 1;
2138 else if (syms[mid]->value > value)
2139 hi = mid;
2140 else
2141 return syms[mid];
2142 }
2143 }
2144 return NULL;
90e3cdf2
JJ
2145}
2146
468392fb
AM
2147static bfd_boolean
2148section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2149{
2150 bfd_vma vma = *(bfd_vma *) ptr;
2151 return ((section->flags & SEC_ALLOC) != 0
2152 && section->vma <= vma
2153 && vma < section->vma + section->size);
2154}
2155
699733f6 2156/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
2157 entry syms. Also generate @plt symbols for the glink branch table.
2158 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
2159
2160static long
a7535cf3
AM
2161ppc64_elf_get_synthetic_symtab (bfd *abfd,
2162 long static_count, asymbol **static_syms,
2163 long dyn_count, asymbol **dyn_syms,
c9727e01 2164 asymbol **ret)
90e3cdf2
JJ
2165{
2166 asymbol *s;
0ccf57bd 2167 size_t i, j, count;
90e3cdf2 2168 char *names;
0ccf57bd 2169 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 2170 asection *opd = NULL;
90e3cdf2 2171 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2172 asymbol **syms;
ee67d69a 2173 int abi = abiversion (abfd);
90e3cdf2
JJ
2174
2175 *ret = NULL;
2176
ee67d69a
AM
2177 if (abi < 2)
2178 {
2179 opd = bfd_get_section_by_name (abfd, ".opd");
2180 if (opd == NULL && abi == 1)
2181 return 0;
2182 }
90e3cdf2 2183
a5259595
AM
2184 syms = NULL;
2185 codesecsym = 0;
2186 codesecsymend = 0;
2187 secsymend = 0;
2188 opdsymend = 0;
2189 symcount = 0;
2190 if (opd != NULL)
c9727e01 2191 {
a5259595
AM
2192 symcount = static_count;
2193 if (!relocatable)
2194 symcount += dyn_count;
2195 if (symcount == 0)
2196 return 0;
c9727e01 2197
a5259595
AM
2198 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2199 if (syms == NULL)
2200 return -1;
90e3cdf2 2201
a5259595
AM
2202 if (!relocatable && static_count != 0 && dyn_count != 0)
2203 {
2204 /* Use both symbol tables. */
2205 memcpy (syms, static_syms, static_count * sizeof (*syms));
2206 memcpy (syms + static_count, dyn_syms,
2207 (dyn_count + 1) * sizeof (*syms));
2208 }
2209 else if (!relocatable && static_count == 0)
2210 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2211 else
2212 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2213
0ccf57bd
AM
2214 /* Trim uninteresting symbols. Interesting symbols are section,
2215 function, and notype symbols. */
2216 for (i = 0, j = 0; i < symcount; ++i)
2217 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2218 | BSF_RELC | BSF_SRELC)) == 0)
2219 syms[j++] = syms[i];
2220 symcount = j;
2221
a5259595
AM
2222 synthetic_relocatable = relocatable;
2223 synthetic_opd = opd;
2224 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2225
a5259595
AM
2226 if (!relocatable && symcount > 1)
2227 {
bfa5bd2a
PA
2228 /* Trim duplicate syms, since we may have merged the normal
2229 and dynamic symbols. Actually, we only care about syms
2230 that have different values, so trim any with the same
2231 value. Don't consider ifunc and ifunc resolver symbols
2232 duplicates however, because GDB wants to know whether a
2233 text symbol is an ifunc resolver. */
a5259595 2234 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2235 {
2236 const asymbol *s0 = syms[i - 1];
2237 const asymbol *s1 = syms[i];
2238
2239 if ((s0->value + s0->section->vma
2240 != s1->value + s1->section->vma)
2241 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2242 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2243 syms[j++] = syms[i];
2244 }
a5259595
AM
2245 symcount = j;
2246 }
699733f6 2247
a5259595
AM
2248 i = 0;
2249 /* Note that here and in compare_symbols we can't compare opd and
2250 sym->section directly. With separate debug info files, the
2251 symbols will be extracted from the debug file while abfd passed
2252 to this function is the real binary. */
0ccf57bd 2253 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2254 ++i;
2255 codesecsym = i;
2256
2257 for (; i < symcount; ++i)
2258 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2259 | SEC_THREAD_LOCAL))
2260 != (SEC_CODE | SEC_ALLOC))
2261 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2262 break;
2263 codesecsymend = i;
2264
2265 for (; i < symcount; ++i)
2266 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2267 break;
2268 secsymend = i;
2269
2270 for (; i < symcount; ++i)
2271 if (strcmp (syms[i]->section->name, ".opd") != 0)
2272 break;
2273 opdsymend = i;
2274
2275 for (; i < symcount; ++i)
2cdcc330
AM
2276 if (((syms[i]->section->flags
2277 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2278 != (SEC_CODE | SEC_ALLOC))
2279 break;
2280 symcount = i;
2281 }
c9727e01 2282 count = 0;
90e3cdf2 2283
699733f6 2284 if (relocatable)
90e3cdf2 2285 {
699733f6
AM
2286 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2287 arelent *r;
2288 size_t size;
0ccf57bd 2289 size_t relcount;
90e3cdf2 2290
468392fb
AM
2291 if (opdsymend == secsymend)
2292 goto done;
2293
699733f6 2294 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2295 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2296 if (relcount == 0)
c9727e01 2297 goto done;
90e3cdf2 2298
7356fed5
AM
2299 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2300 {
2301 count = -1;
2302 goto done;
2303 }
2304
699733f6 2305 size = 0;
595da8c5 2306 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2307 {
2308 asymbol *sym;
90e3cdf2 2309
595da8c5 2310 while (r < opd->relocation + relcount
699733f6
AM
2311 && r->address < syms[i]->value + opd->vma)
2312 ++r;
90e3cdf2 2313
595da8c5 2314 if (r == opd->relocation + relcount)
699733f6 2315 break;
90e3cdf2 2316
699733f6
AM
2317 if (r->address != syms[i]->value + opd->vma)
2318 continue;
90e3cdf2 2319
699733f6
AM
2320 if (r->howto->type != R_PPC64_ADDR64)
2321 continue;
90e3cdf2 2322
699733f6
AM
2323 sym = *r->sym_ptr_ptr;
2324 if (!sym_exists_at (syms, opdsymend, symcount,
2325 sym->section->id, sym->value + r->addend))
2326 {
2327 ++count;
2328 size += sizeof (asymbol);
2329 size += strlen (syms[i]->name) + 2;
2330 }
2331 }
90e3cdf2 2332
c4b0b099
AM
2333 if (size == 0)
2334 goto done;
699733f6
AM
2335 s = *ret = bfd_malloc (size);
2336 if (s == NULL)
2337 {
7356fed5 2338 count = -1;
c9727e01 2339 goto done;
699733f6 2340 }
90e3cdf2 2341
699733f6 2342 names = (char *) (s + count);
90e3cdf2 2343
595da8c5 2344 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2345 {
699733f6 2346 asymbol *sym;
90e3cdf2 2347
595da8c5 2348 while (r < opd->relocation + relcount
699733f6
AM
2349 && r->address < syms[i]->value + opd->vma)
2350 ++r;
90e3cdf2 2351
595da8c5 2352 if (r == opd->relocation + relcount)
699733f6
AM
2353 break;
2354
2355 if (r->address != syms[i]->value + opd->vma)
2356 continue;
2357
2358 if (r->howto->type != R_PPC64_ADDR64)
2359 continue;
90e3cdf2 2360
699733f6
AM
2361 sym = *r->sym_ptr_ptr;
2362 if (!sym_exists_at (syms, opdsymend, symcount,
2363 sym->section->id, sym->value + r->addend))
2364 {
2365 size_t len;
2366
2367 *s = *syms[i];
6ba2a415 2368 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2369 s->section = sym->section;
2370 s->value = sym->value + r->addend;
2371 s->name = names;
2372 *names++ = '.';
2373 len = strlen (syms[i]->name);
2374 memcpy (names, syms[i]->name, len + 1);
2375 names += len + 1;
6f610d07
UW
2376 /* Have udata.p point back to the original symbol this
2377 synthetic symbol was derived from. */
2378 s->udata.p = syms[i];
699733f6
AM
2379 s++;
2380 }
2381 }
2382 }
2383 else
90e3cdf2 2384 {
468392fb 2385 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 2386 bfd_byte *contents = NULL;
699733f6 2387 size_t size;
0ccf57bd 2388 size_t plt_count = 0;
468392fb
AM
2389 bfd_vma glink_vma = 0, resolv_vma = 0;
2390 asection *dynamic, *glink = NULL, *relplt = NULL;
2391 arelent *p;
90e3cdf2 2392
ee67d69a 2393 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2394 {
c4b0b099
AM
2395 free_contents_and_exit_err:
2396 count = -1;
ee67d69a 2397 free_contents_and_exit:
699733f6 2398 if (contents)
ee67d69a 2399 free (contents);
c9727e01 2400 goto done;
699733f6 2401 }
90e3cdf2 2402
699733f6
AM
2403 size = 0;
2404 for (i = secsymend; i < opdsymend; ++i)
2405 {
2406 bfd_vma ent;
90e3cdf2 2407
5ef11c02
AM
2408 /* Ignore bogus symbols. */
2409 if (syms[i]->value > opd->size - 8)
2410 continue;
2411
699733f6
AM
2412 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2413 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2414 {
2415 ++count;
2416 size += sizeof (asymbol);
2417 size += strlen (syms[i]->name) + 2;
2418 }
2419 }
90e3cdf2 2420
468392fb 2421 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2422 if (dyn_count != 0
2423 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2424 {
2425 bfd_byte *dynbuf, *extdyn, *extdynend;
2426 size_t extdynsize;
2427 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2428
2429 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2430 goto free_contents_and_exit_err;
468392fb
AM
2431
2432 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2433 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2434
2435 extdyn = dynbuf;
2436 extdynend = extdyn + dynamic->size;
2437 for (; extdyn < extdynend; extdyn += extdynsize)
2438 {
2439 Elf_Internal_Dyn dyn;
2440 (*swap_dyn_in) (abfd, extdyn, &dyn);
2441
2442 if (dyn.d_tag == DT_NULL)
2443 break;
2444
2445 if (dyn.d_tag == DT_PPC64_GLINK)
2446 {
9e390558
AM
2447 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2448 See comment in ppc64_elf_finish_dynamic_sections. */
2449 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2450 /* The .glink section usually does not survive the final
2451 link; search for the section (usually .text) where the
2452 glink stubs now reside. */
2453 glink = bfd_sections_find_if (abfd, section_covers_vma,
2454 &glink_vma);
2455 break;
2456 }
2457 }
2458
2459 free (dynbuf);
2460 }
2461
2462 if (glink != NULL)
2463 {
2464 /* Determine __glink trampoline by reading the relative branch
2465 from the first glink stub. */
2466 bfd_byte buf[4];
b9e5796b
AM
2467 unsigned int off = 0;
2468
2469 while (bfd_get_section_contents (abfd, glink, buf,
2470 glink_vma + off - glink->vma, 4))
468392fb
AM
2471 {
2472 unsigned int insn = bfd_get_32 (abfd, buf);
2473 insn ^= B_DOT;
2474 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2475 {
2cdcc330
AM
2476 resolv_vma
2477 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2478 break;
2479 }
2480 off += 4;
2481 if (off > 4)
2482 break;
468392fb
AM
2483 }
2484
2485 if (resolv_vma)
2486 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2487
066ee829
AM
2488 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2489 if (relplt != NULL)
2490 {
2491 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2cdcc330 2492 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 2493 goto free_contents_and_exit_err;
68ffbac6 2494
066ee829
AM
2495 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2496 size += plt_count * sizeof (asymbol);
468392fb 2497
066ee829
AM
2498 p = relplt->relocation;
2499 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2500 {
2501 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2502 if (p->addend != 0)
2503 size += sizeof ("+0x") - 1 + 16;
2504 }
066ee829 2505 }
468392fb
AM
2506 }
2507
c4b0b099
AM
2508 if (size == 0)
2509 goto free_contents_and_exit;
699733f6
AM
2510 s = *ret = bfd_malloc (size);
2511 if (s == NULL)
c4b0b099 2512 goto free_contents_and_exit_err;
90e3cdf2 2513
468392fb 2514 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2515
699733f6 2516 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2517 {
699733f6 2518 bfd_vma ent;
90e3cdf2 2519
5ef11c02
AM
2520 if (syms[i]->value > opd->size - 8)
2521 continue;
2522
699733f6
AM
2523 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2524 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2525 {
0ccf57bd 2526 size_t lo, hi;
699733f6 2527 size_t len;
c9727e01 2528 asection *sec = abfd->sections;
90e3cdf2 2529
699733f6
AM
2530 *s = *syms[i];
2531 lo = codesecsym;
2532 hi = codesecsymend;
2533 while (lo < hi)
2534 {
0ccf57bd 2535 size_t mid = (lo + hi) >> 1;
699733f6
AM
2536 if (syms[mid]->section->vma < ent)
2537 lo = mid + 1;
2538 else if (syms[mid]->section->vma > ent)
2539 hi = mid;
2540 else
c9727e01
AM
2541 {
2542 sec = syms[mid]->section;
2543 break;
2544 }
699733f6
AM
2545 }
2546
c9727e01 2547 if (lo >= hi && lo > codesecsym)
699733f6 2548 sec = syms[lo - 1]->section;
699733f6
AM
2549
2550 for (; sec != NULL; sec = sec->next)
2551 {
2552 if (sec->vma > ent)
2553 break;
63524580
JK
2554 /* SEC_LOAD may not be set if SEC is from a separate debug
2555 info file. */
2556 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2557 break;
2558 if ((sec->flags & SEC_CODE) != 0)
2559 s->section = sec;
2560 }
6ba2a415 2561 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2562 s->value = ent - s->section->vma;
2563 s->name = names;
2564 *names++ = '.';
2565 len = strlen (syms[i]->name);
2566 memcpy (names, syms[i]->name, len + 1);
2567 names += len + 1;
6f610d07
UW
2568 /* Have udata.p point back to the original symbol this
2569 synthetic symbol was derived from. */
2570 s->udata.p = syms[i];
699733f6 2571 s++;
90e3cdf2 2572 }
90e3cdf2 2573 }
699733f6 2574 free (contents);
468392fb
AM
2575
2576 if (glink != NULL && relplt != NULL)
2577 {
2578 if (resolv_vma)
2579 {
2580 /* Add a symbol for the main glink trampoline. */
86a4952b 2581 memset (s, 0, sizeof *s);
468392fb 2582 s->the_bfd = abfd;
6ba2a415 2583 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2584 s->section = glink;
2585 s->value = resolv_vma - glink->vma;
2586 s->name = names;
2cdcc330
AM
2587 memcpy (names, "__glink_PLTresolve",
2588 sizeof ("__glink_PLTresolve"));
468392fb
AM
2589 names += sizeof ("__glink_PLTresolve");
2590 s++;
2591 count++;
2592 }
2593
2594 /* FIXME: It would be very much nicer to put sym@plt on the
2595 stub rather than on the glink branch table entry. The
2596 objdump disassembler would then use a sensible symbol
2597 name on plt calls. The difficulty in doing so is
2598 a) finding the stubs, and,
2599 b) matching stubs against plt entries, and,
2600 c) there can be multiple stubs for a given plt entry.
2601
2602 Solving (a) could be done by code scanning, but older
2603 ppc64 binaries used different stubs to current code.
2604 (b) is the tricky one since you need to known the toc
2605 pointer for at least one function that uses a pic stub to
2606 be able to calculate the plt address referenced.
2607 (c) means gdb would need to set multiple breakpoints (or
2608 find the glink branch itself) when setting breakpoints
2609 for pending shared library loads. */
2610 p = relplt->relocation;
2611 for (i = 0; i < plt_count; i++, p++)
2612 {
2613 size_t len;
2614
2615 *s = **p->sym_ptr_ptr;
2616 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2617 we are defining a symbol, ensure one of them is set. */
2618 if ((s->flags & BSF_LOCAL) == 0)
2619 s->flags |= BSF_GLOBAL;
6ba2a415 2620 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2621 s->section = glink;
2622 s->value = glink_vma - glink->vma;
2623 s->name = names;
2624 s->udata.p = NULL;
2625 len = strlen ((*p->sym_ptr_ptr)->name);
2626 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2627 names += len;
e054468f
AM
2628 if (p->addend != 0)
2629 {
2630 memcpy (names, "+0x", sizeof ("+0x") - 1);
2631 names += sizeof ("+0x") - 1;
2632 bfd_sprintf_vma (abfd, names, p->addend);
2633 names += strlen (names);
2634 }
468392fb
AM
2635 memcpy (names, "@plt", sizeof ("@plt"));
2636 names += sizeof ("@plt");
2637 s++;
b9e5796b
AM
2638 if (abi < 2)
2639 {
2640 glink_vma += 8;
2641 if (i >= 0x8000)
2642 glink_vma += 4;
2643 }
2644 else
468392fb
AM
2645 glink_vma += 4;
2646 }
2647 count += plt_count;
2648 }
90e3cdf2
JJ
2649 }
2650
c9727e01 2651 done:
a7535cf3 2652 free (syms);
90e3cdf2
JJ
2653 return count;
2654}
5bd4f169 2655\f
65f38f15
AM
2656/* The following functions are specific to the ELF linker, while
2657 functions above are used generally. Those named ppc64_elf_* are
2658 called by the main ELF linker code. They appear in this file more
2659 or less in the order in which they are called. eg.
2660 ppc64_elf_check_relocs is called early in the link process,
2661 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2662 called.
2663
2664 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2665 functions have both a function code symbol and a function descriptor
2666 symbol. A call to foo in a relocatable object file looks like:
2667
2668 . .text
2669 . x:
2670 . bl .foo
2671 . nop
2672
2673 The function definition in another object file might be:
2674
2675 . .section .opd
2676 . foo: .quad .foo
2677 . .quad .TOC.@tocbase
2678 . .quad 0
2679 .
2680 . .text
2681 . .foo: blr
2682
2683 When the linker resolves the call during a static link, the branch
2684 unsurprisingly just goes to .foo and the .opd information is unused.
2685 If the function definition is in a shared library, things are a little
2686 different: The call goes via a plt call stub, the opd information gets
2687 copied to the plt, and the linker patches the nop.
2688
2689 . x:
2690 . bl .foo_stub
2691 . ld 2,40(1)
2692 .
2693 .
2694 . .foo_stub:
71a39c98
AM
2695 . std 2,40(1) # in practice, the call stub
2696 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2697 . addi 11,11,Lfoo@toc@l # this is the general idea
2698 . ld 12,0(11)
2699 . ld 2,8(11)
2700 . mtctr 12
2701 . ld 11,16(11)
e86ce104
AM
2702 . bctr
2703 .
2704 . .section .plt
2705 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2706
2707 The "reloc ()" notation is supposed to indicate that the linker emits
2708 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2709 copying.
2710
2711 What are the difficulties here? Well, firstly, the relocations
2712 examined by the linker in check_relocs are against the function code
2713 sym .foo, while the dynamic relocation in the plt is emitted against
2714 the function descriptor symbol, foo. Somewhere along the line, we need
2715 to carefully copy dynamic link information from one symbol to the other.
2716 Secondly, the generic part of the elf linker will make .foo a dynamic
2717 symbol as is normal for most other backends. We need foo dynamic
2718 instead, at least for an application final link. However, when
2719 creating a shared library containing foo, we need to have both symbols
2720 dynamic so that references to .foo are satisfied during the early
2721 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2722 definition from some other object, eg. a static library.
2723
2724 Update: As of August 2004, we support a new convention. Function
2725 calls may use the function descriptor symbol, ie. "bl foo". This
2726 behaves exactly as "bl .foo". */
65f38f15 2727
7c8bbca5
AM
2728/* Of those relocs that might be copied as dynamic relocs, this
2729 function selects those that must be copied when linking a shared
2730 library or PIE, even when the symbol is local. */
65f38f15 2731
1d483afe
AM
2732static int
2733must_be_dyn_reloc (struct bfd_link_info *info,
2734 enum elf_ppc64_reloc_type r_type)
2735{
2736 switch (r_type)
2737 {
2738 default:
7c8bbca5
AM
2739 /* Only relative relocs can be resolved when the object load
2740 address isn't fixed. DTPREL64 is excluded because the
2741 dynamic linker needs to differentiate global dynamic from
2742 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2743 return 1;
2744
2745 case R_PPC64_REL32:
2746 case R_PPC64_REL64:
2747 case R_PPC64_REL30:
1bdd8fac
AM
2748 case R_PPC64_TOC16:
2749 case R_PPC64_TOC16_DS:
2750 case R_PPC64_TOC16_LO:
2751 case R_PPC64_TOC16_HI:
2752 case R_PPC64_TOC16_HA:
2753 case R_PPC64_TOC16_LO_DS:
1d483afe
AM
2754 return 0;
2755
2756 case R_PPC64_TPREL16:
2757 case R_PPC64_TPREL16_LO:
2758 case R_PPC64_TPREL16_HI:
2759 case R_PPC64_TPREL16_HA:
2760 case R_PPC64_TPREL16_DS:
2761 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2762 case R_PPC64_TPREL16_HIGH:
2763 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2764 case R_PPC64_TPREL16_HIGHER:
2765 case R_PPC64_TPREL16_HIGHERA:
2766 case R_PPC64_TPREL16_HIGHEST:
2767 case R_PPC64_TPREL16_HIGHESTA:
2768 case R_PPC64_TPREL64:
c213164a 2769 case R_PPC64_TPREL34:
7c8bbca5
AM
2770 /* These relocations are relative but in a shared library the
2771 linker doesn't know the thread pointer base. */
2772 return bfd_link_dll (info);
1d483afe
AM
2773 }
2774}
65f38f15 2775
f4656909
AM
2776/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2777 copying dynamic variables from a shared lib into an app's dynbss
2778 section, and instead use a dynamic relocation to point into the
5d35169e
AM
2779 shared lib. With code that gcc generates, it's vital that this be
2780 enabled; In the PowerPC64 ABI, the address of a function is actually
2781 the address of a function descriptor, which resides in the .opd
2782 section. gcc uses the descriptor directly rather than going via the
2783 GOT as some other ABI's do, which means that initialized function
2784 pointers must reference the descriptor. Thus, a function pointer
2785 initialized to the address of a function in a shared library will
2786 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 2787 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
2788 presents a problem as a plt entry for that function is also
2789 initialized from the function descriptor symbol and the copy reloc
2790 may not be initialized first. */
a23b6845 2791#define ELIMINATE_COPY_RELOCS 1
f4656909 2792
721956f4 2793/* Section name for stubs is the associated section name plus this
29942be8
NC
2794 string. */
2795#define STUB_SUFFIX ".stub"
721956f4
AM
2796
2797/* Linker stubs.
2798 ppc_stub_long_branch:
2799 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2800 destination, but a 24 bit branch in a stub section will reach.
2801 . b dest
2802
2803 ppc_stub_plt_branch:
2804 Similar to the above, but a 24 bit branch in the stub section won't
2805 reach its destination.
71a39c98
AM
2806 . addis %r11,%r2,xxx@toc@ha
2807 . ld %r12,xxx@toc@l(%r11)
2808 . mtctr %r12
721956f4
AM
2809 . bctr
2810
2811 ppc_stub_plt_call:
2c66dc6c
AM
2812 Used to call a function in a shared library. If it so happens that
2813 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2814 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 2815 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
2816 . addis %r11,%r2,xxx@toc@ha
2817 . ld %r12,xxx+0@toc@l(%r11)
2818 . mtctr %r12
2819 . ld %r2,xxx+8@toc@l(%r11)
2820 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2821 . bctr
ad8e1ba5
AM
2822
2823 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2824 code to adjust the value and save r2 to support multiple toc sections.
2825 A ppc_stub_long_branch with an r2 offset looks like:
2826 . std %r2,40(%r1)
2827 . addis %r2,%r2,off@ha
2828 . addi %r2,%r2,off@l
2829 . b dest
2830
2831 A ppc_stub_plt_branch with an r2 offset looks like:
2832 . std %r2,40(%r1)
71a39c98
AM
2833 . addis %r11,%r2,xxx@toc@ha
2834 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
2835 . addis %r2,%r2,off@ha
2836 . addi %r2,%r2,off@l
71a39c98 2837 . mtctr %r12
ad8e1ba5 2838 . bctr
ac2df442 2839
05d0e962
AM
2840 All of the above stubs are shown as their ELFv1 variants. ELFv2
2841 variants exist too, simpler for plt calls since a new toc pointer
2842 and static chain are not loaded by the stub. In addition, ELFv2
2843 has some more complex stubs to handle calls marked with NOTOC
2844 relocs from functions where r2 is not a valid toc pointer. These
2845 come in two flavours, the ones shown below, and _both variants that
2846 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2847 one call is from a function where r2 is used as the toc pointer but
2848 needs a toc adjusting stub for small-model multi-toc, and another
2849 call is from a function where r2 is not valid.
2850 ppc_stub_long_branch_notoc:
2851 . mflr %r12
2852 . bcl 20,31,1f
2853 . 1:
2854 . mflr %r11
2855 . mtlr %r12
2cdcc330
AM
2856 . addis %r12,%r11,dest-1b@ha
2857 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2858 . b dest
2859
2860 ppc_stub_plt_branch_notoc:
2861 . mflr %r12
2862 . bcl 20,31,1f
2863 . 1:
2864 . mflr %r11
2865 . mtlr %r12
2866 . lis %r12,xxx-1b@highest
f891966f 2867 . ori %r12,%r12,xxx-1b@higher
05d0e962 2868 . sldi %r12,%r12,32
f891966f 2869 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2870 . ori %r12,%r12,xxx-1b@l
2871 . add %r12,%r11,%r12
2872 . mtctr %r12
2873 . bctr
2874
2875 ppc_stub_plt_call_notoc:
2876 . mflr %r12
2877 . bcl 20,31,1f
2878 . 1:
2879 . mflr %r11
2880 . mtlr %r12
2881 . lis %r12,xxx-1b@highest
f891966f 2882 . ori %r12,%r12,xxx-1b@higher
05d0e962 2883 . sldi %r12,%r12,32
f891966f 2884 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2885 . ori %r12,%r12,xxx-1b@l
2886 . ldx %r12,%r11,%r12
2887 . mtctr %r12
2888 . bctr
2889
04bdff6a
AM
2890 There are also ELFv1 powerxx variants of these stubs.
2891 ppc_stub_long_branch_notoc:
d4b87b1e 2892 . pla %r12,dest@pcrel
04bdff6a
AM
2893 . b dest
2894 ppc_stub_plt_branch_notoc:
2895 . lis %r11,(dest-1f)@highesta34
2896 . ori %r11,%r11,(dest-1f)@highera34
2897 . sldi %r11,%r11,34
d4b87b1e 2898 . 1: pla %r12,dest@pcrel
04bdff6a
AM
2899 . add %r12,%r11,%r12
2900 . mtctr %r12
2901 . bctr
2902 ppc_stub_plt_call_notoc:
2903 . lis %r11,(xxx-1f)@highesta34
2904 . ori %r11,%r11,(xxx-1f)@highera34
2905 . sldi %r11,%r11,34
d4b87b1e 2906 . 1: pla %r12,xxx@pcrel
04bdff6a
AM
2907 . ldx %r12,%r11,%r12
2908 . mtctr %r12
2909 . bctr
2910
05d0e962
AM
2911 In cases where the high instructions would add zero, they are
2912 omitted and following instructions modified in some cases.
04bdff6a
AM
2913 For example, a powerxx ppc_stub_plt_call_notoc might simplify down
2914 to
2915 . pld %r12,xxx@pcrel
2916 . mtctr %r12
2917 . bctr
05d0e962
AM
2918
2919 For a given stub group (a set of sections all using the same toc
2920 pointer value) there will be just one stub type used for any
2921 particular function symbol. For example, if printf is called from
2922 code with the tocsave optimization (ie. r2 saved in function
2923 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2924 and from other code without the tocsave optimization requiring a
2925 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2926 type will be created. Calls with the tocsave optimization will
2927 enter this stub after the instruction saving r2. A similar
2928 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2929 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2930 to call an external function like printf. If other calls to printf
2931 require a ppc_stub_plt_call linkage stub then a single
2932 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2933 call. If other calls to printf require a ppc_stub_plt_call_r2save
2934 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2935 be created and calls not requiring r2 to be saved will enter the
2936 stub after the r2 save instruction. There is an analogous
2937 hierarchy of long branch and plt branch stubs for local call
2938 linkage. */
721956f4 2939
2cdcc330
AM
2940enum ppc_stub_type
2941{
721956f4
AM
2942 ppc_stub_none,
2943 ppc_stub_long_branch,
ad8e1ba5 2944 ppc_stub_long_branch_r2off,
05d0e962
AM
2945 ppc_stub_long_branch_notoc,
2946 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 2947 ppc_stub_plt_branch,
ad8e1ba5 2948 ppc_stub_plt_branch_r2off,
05d0e962
AM
2949 ppc_stub_plt_branch_notoc,
2950 ppc_stub_plt_branch_both,
794e51c0 2951 ppc_stub_plt_call,
7341d5e2 2952 ppc_stub_plt_call_r2save,
05d0e962
AM
2953 ppc_stub_plt_call_notoc,
2954 ppc_stub_plt_call_both,
a4b6fadd
AM
2955 ppc_stub_global_entry,
2956 ppc_stub_save_res
721956f4
AM
2957};
2958
6f20ed8a
AM
2959/* Information on stub grouping. */
2960struct map_stub
2961{
2962 /* The stub section. */
2963 asection *stub_sec;
2964 /* This is the section to which stubs in the group will be attached. */
2965 asection *link_sec;
a4b6fadd
AM
2966 /* Next group. */
2967 struct map_stub *next;
2968 /* Whether to emit a copy of register save/restore functions in this
2969 group. */
2970 int needs_save_res;
df136d64
AM
2971 /* Current offset within stubs after the insn restoring lr in a
2972 _notoc or _both stub using bcl for pc-relative addressing, or
2973 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2974 unsigned int lr_restore;
2975 /* Accumulated size of EH info emitted to describe return address
2976 if stubs modify lr. Does not include 17 byte FDE header. */
2977 unsigned int eh_size;
2978 /* Offset in glink_eh_frame to the start of EH info for this group. */
2979 unsigned int eh_base;
6f20ed8a
AM
2980};
2981
2cdcc330
AM
2982struct ppc_stub_hash_entry
2983{
721956f4
AM
2984 /* Base hash table entry structure. */
2985 struct bfd_hash_entry root;
2986
ad8e1ba5
AM
2987 enum ppc_stub_type stub_type;
2988
6f20ed8a
AM
2989 /* Group information. */
2990 struct map_stub *group;
721956f4
AM
2991
2992 /* Offset within stub_sec of the beginning of this stub. */
2993 bfd_vma stub_offset;
2994
2995 /* Given the symbol's value and its section we can determine its final
2996 value when building the stubs (so the stub knows where to jump. */
2997 bfd_vma target_value;
2998 asection *target_section;
2999
721956f4
AM
3000 /* The symbol table entry, if any, that this was derived from. */
3001 struct ppc_link_hash_entry *h;
e054468f 3002 struct plt_entry *plt_ent;
721956f4 3003
2d7ad24e
AM
3004 /* Symbol type. */
3005 unsigned char symtype;
3006
6911b7dc
AM
3007 /* Symbol st_other. */
3008 unsigned char other;
721956f4
AM
3009};
3010
2cdcc330
AM
3011struct ppc_branch_hash_entry
3012{
721956f4
AM
3013 /* Base hash table entry structure. */
3014 struct bfd_hash_entry root;
3015
c456f082 3016 /* Offset within branch lookup table. */
721956f4
AM
3017 unsigned int offset;
3018
3019 /* Generation marker. */
3020 unsigned int iter;
3021};
65f38f15 3022
19e08130
AM
3023/* Used to track dynamic relocations for local symbols. */
3024struct ppc_dyn_relocs
3025{
3026 struct ppc_dyn_relocs *next;
3027
3028 /* The input section of the reloc. */
3029 asection *sec;
3030
3031 /* Total number of relocs copied for the input section. */
3032 unsigned int count : 31;
3033
3034 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3035 unsigned int ifunc : 1;
3036};
3037
65f38f15
AM
3038struct ppc_link_hash_entry
3039{
3040 struct elf_link_hash_entry elf;
3041
2cdcc330
AM
3042 union
3043 {
b3fac117
AM
3044 /* A pointer to the most recently used stub hash entry against this
3045 symbol. */
3046 struct ppc_stub_hash_entry *stub_cache;
3047
3048 /* A pointer to the next symbol starting with a '.' */
3049 struct ppc_link_hash_entry *next_dot_sym;
3050 } u;
721956f4 3051
65f38f15 3052 /* Track dynamic relocs copied for this symbol. */
6061a67d 3053 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3054
721956f4 3055 /* Link between function code and descriptor symbols. */
34814b9f 3056 struct ppc_link_hash_entry *oh;
721956f4 3057
e86ce104
AM
3058 /* Flag function code and descriptor symbols. */
3059 unsigned int is_func:1;
3060 unsigned int is_func_descriptor:1;
908b32fc 3061 unsigned int fake:1;
411e1bfb 3062
c5614fa4
AM
3063 /* Whether global opd/toc sym has been adjusted or not.
3064 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3065 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3066 unsigned int adjust_done:1;
3067
a4b6fadd
AM
3068 /* Set if this is an out-of-line register save/restore function,
3069 with non-standard calling convention. */
3070 unsigned int save_res:1;
3071
8b5f1ed8
AM
3072 /* Set if a duplicate symbol with non-zero localentry is detected,
3073 even when the duplicate symbol does not provide a definition. */
3074 unsigned int non_zero_localentry:1;
3075
411e1bfb 3076 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3077 Bits are or'd into the mask as the corresponding relocs are
3078 encountered during check_relocs, with TLS_TLS being set when any
3079 of the other TLS bits are set. tls_optimize clears bits when
3080 optimizing to indicate the corresponding GOT entry type is not
3081 needed. If set, TLS_TLS is never cleared. tls_optimize may also
b00a0a86 3082 set TLS_GDIE when a GD reloc turns into an IE one.
37da22e5
AM
3083 These flags are also kept for local symbols. */
3084#define TLS_TLS 1 /* Any TLS reloc. */
3085#define TLS_GD 2 /* GD reloc. */
3086#define TLS_LD 4 /* LD reloc. */
3087#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3088#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3089#define TLS_MARK 32 /* __tls_get_addr call marked. */
b00a0a86 3090#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
46e9995a 3091#define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
f961d9dd 3092 unsigned char tls_mask;
37da22e5
AM
3093
3094 /* The above field is also used to mark function symbols. In which
3095 case TLS_TLS will be 0. */
3096#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3097#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3098#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3099};
3100
3101/* ppc64 ELF linker hash table. */
3102
3103struct ppc_link_hash_table
3104{
3105 struct elf_link_hash_table elf;
3106
721956f4
AM
3107 /* The stub hash table. */
3108 struct bfd_hash_table stub_hash_table;
3109
3110 /* Another hash table for plt_branch stubs. */
3111 struct bfd_hash_table branch_hash_table;
3112
3b421ab3
AM
3113 /* Hash table for function prologue tocsave. */
3114 htab_t tocsave_htab;
3115
e7d1c40c
AM
3116 /* Various options and other info passed from the linker. */
3117 struct ppc64_elf_params *params;
721956f4 3118
6f20ed8a
AM
3119 /* The size of sec_info below. */
3120 unsigned int sec_info_arr_size;
3121
3122 /* Per-section array of extra section info. Done this way rather
3123 than as part of ppc64_elf_section_data so we have the info for
3124 non-ppc64 sections. */
3125 struct
3126 {
3127 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3128 bfd_vma toc_off;
6f20ed8a
AM
3129
3130 union
3131 {
3132 /* The section group that this section belongs to. */
3133 struct map_stub *group;
3134 /* A temp section list pointer. */
3135 asection *list;
3136 } u;
3137 } *sec_info;
721956f4 3138
a4b6fadd
AM
3139 /* Linked list of groups. */
3140 struct map_stub *group;
3141
ad8e1ba5
AM
3142 /* Temp used when calculating TOC pointers. */
3143 bfd_vma toc_curr;
bf102f86
AM
3144 bfd *toc_bfd;
3145 asection *toc_first_sec;
ad8e1ba5 3146
b3fac117
AM
3147 /* Used when adding symbols. */
3148 struct ppc_link_hash_entry *dot_syms;
3149
33e44f2e 3150 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3151 asection *glink;
9e390558 3152 asection *global_entry;
82bd7b59 3153 asection *sfpr;
2d7ad24e
AM
3154 asection *pltlocal;
3155 asection *relpltlocal;
4ce794b7
AM
3156 asection *brlt;
3157 asection *relbrlt;
58d180e8 3158 asection *glink_eh_frame;
ec338859 3159
8387904d
AM
3160 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3161 struct ppc_link_hash_entry *tls_get_addr;
3162 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3163
927be08e
AM
3164 /* The size of reliplt used by got entry relocs. */
3165 bfd_size_type got_reli_size;
3166
9b5ecbd0 3167 /* Statistics. */
7341d5e2 3168 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 3169
ee75fd95
AM
3170 /* Number of stubs against global syms. */
3171 unsigned long stub_globals;
3172
ee67d69a
AM
3173 /* Set if we're linking code with function descriptors. */
3174 unsigned int opd_abi:1;
3175
4c52953f 3176 /* Support for multiple toc sections. */
33c0ec9d 3177 unsigned int do_multi_toc:1;
4c52953f 3178 unsigned int multi_toc_needed:1;
927be08e 3179 unsigned int second_toc_pass:1;
67f0cbdb 3180 unsigned int do_toc_opt:1;
4c52953f 3181
9a23f96e
AM
3182 /* Set if tls optimization is enabled. */
3183 unsigned int do_tls_opt:1;
3184
3e04d765
AM
3185 /* Set if inline plt calls should be converted to direct calls. */
3186 unsigned int can_convert_all_inline_plt:1;
3187
5d1634d7 3188 /* Set on error. */
99877b66 3189 unsigned int stub_error:1;
721956f4 3190
8c5b4e52
AM
3191 /* Whether func_desc_adjust needs to be run over symbols. */
3192 unsigned int need_func_desc_adj:1;
721956f4 3193
82e66161
AM
3194 /* Whether there exist local gnu indirect function resolvers,
3195 referenced by dynamic relocations. */
3196 unsigned int local_ifunc_resolver:1;
3197 unsigned int maybe_local_ifunc_resolver:1;
3198
f378ab09
AM
3199 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3200 unsigned int has_plt_localentry0:1;
3201
5663e321
AM
3202 /* Whether calls are made via the PLT from NOTOC functions. */
3203 unsigned int notoc_plt:1;
3204
04bdff6a
AM
3205 /* Whether to use powerxx instructions in linkage stubs. */
3206 unsigned int powerxx_stubs:1;
3207
721956f4
AM
3208 /* Incremented every time we size stubs. */
3209 unsigned int stub_iteration;
5d1634d7 3210
87d72d41
AM
3211 /* Small local sym cache. */
3212 struct sym_cache sym_cache;
65f38f15
AM
3213};
3214
4c52953f
AM
3215/* Rename some of the generic section flags to better document how they
3216 are used here. */
b0dddeec
AM
3217
3218/* Nonzero if this section has TLS related relocations. */
3219#define has_tls_reloc sec_flg0
3220
23cedd1d 3221/* Nonzero if this section has an old-style call to __tls_get_addr. */
b0dddeec
AM
3222#define has_tls_get_addr_call sec_flg1
3223
3224/* Nonzero if this section has any toc or got relocs. */
3225#define has_toc_reloc sec_flg2
3226
3227/* Nonzero if this section has a call to another section that uses
3228 the toc or got. */
d77c8a4b 3229#define makes_toc_func_call sec_flg3
b0dddeec
AM
3230
3231/* Recursion protection when determining above flag. */
d77c8a4b 3232#define call_check_in_progress sec_flg4
70cc837d 3233#define call_check_done sec_flg5
4c52953f 3234
65f38f15
AM
3235/* Get the ppc64 ELF linker hash table from a link_info structure. */
3236
3237#define ppc_hash_table(p) \
4dfe6ac6
NC
3238 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3239 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3240
721956f4
AM
3241#define ppc_stub_hash_lookup(table, string, create, copy) \
3242 ((struct ppc_stub_hash_entry *) \
3243 bfd_hash_lookup ((table), (string), (create), (copy)))
3244
3245#define ppc_branch_hash_lookup(table, string, create, copy) \
3246 ((struct ppc_branch_hash_entry *) \
3247 bfd_hash_lookup ((table), (string), (create), (copy)))
3248
3249/* Create an entry in the stub hash table. */
3250
3251static struct bfd_hash_entry *
4ce794b7
AM
3252stub_hash_newfunc (struct bfd_hash_entry *entry,
3253 struct bfd_hash_table *table,
3254 const char *string)
721956f4
AM
3255{
3256 /* Allocate the structure if it has not already been allocated by a
3257 subclass. */
3258 if (entry == NULL)
3259 {
3260 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3261 if (entry == NULL)
3262 return entry;
3263 }
3264
3265 /* Call the allocation method of the superclass. */
3266 entry = bfd_hash_newfunc (entry, table, string);
3267 if (entry != NULL)
3268 {
3269 struct ppc_stub_hash_entry *eh;
3270
3271 /* Initialize the local fields. */
3272 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3273 eh->stub_type = ppc_stub_none;
6f20ed8a 3274 eh->group = NULL;
721956f4
AM
3275 eh->stub_offset = 0;
3276 eh->target_value = 0;
3277 eh->target_section = NULL;
721956f4 3278 eh->h = NULL;
6911b7dc 3279 eh->plt_ent = NULL;
6911b7dc 3280 eh->other = 0;
721956f4
AM
3281 }
3282
3283 return entry;
3284}
3285
3286/* Create an entry in the branch hash table. */
3287
3288static struct bfd_hash_entry *
4ce794b7
AM
3289branch_hash_newfunc (struct bfd_hash_entry *entry,
3290 struct bfd_hash_table *table,
3291 const char *string)
721956f4
AM
3292{
3293 /* Allocate the structure if it has not already been allocated by a
3294 subclass. */
3295 if (entry == NULL)
3296 {
3297 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3298 if (entry == NULL)
3299 return entry;
3300 }
3301
3302 /* Call the allocation method of the superclass. */
3303 entry = bfd_hash_newfunc (entry, table, string);
3304 if (entry != NULL)
3305 {
3306 struct ppc_branch_hash_entry *eh;
3307
3308 /* Initialize the local fields. */
3309 eh = (struct ppc_branch_hash_entry *) entry;
3310 eh->offset = 0;
3311 eh->iter = 0;
3312 }
3313
3314 return entry;
3315}
3316
65f38f15
AM
3317/* Create an entry in a ppc64 ELF linker hash table. */
3318
3319static struct bfd_hash_entry *
4ce794b7
AM
3320link_hash_newfunc (struct bfd_hash_entry *entry,
3321 struct bfd_hash_table *table,
3322 const char *string)
65f38f15
AM
3323{
3324 /* Allocate the structure if it has not already been allocated by a
3325 subclass. */
3326 if (entry == NULL)
3327 {
3328 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3329 if (entry == NULL)
3330 return entry;
3331 }
3332
3333 /* Call the allocation method of the superclass. */
3334 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3335 if (entry != NULL)
3336 {
3337 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3338
b3fac117 3339 memset (&eh->u.stub_cache, 0,
908b32fc 3340 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3341 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3342
3343 /* When making function calls, old ABI code references function entry
3344 points (dot symbols), while new ABI code references the function
3345 descriptor symbol. We need to make any combination of reference and
3346 definition work together, without breaking archive linking.
3347
3348 For a defined function "foo" and an undefined call to "bar":
3349 An old object defines "foo" and ".foo", references ".bar" (possibly
3350 "bar" too).
3351 A new object defines "foo" and references "bar".
3352
3353 A new object thus has no problem with its undefined symbols being
3354 satisfied by definitions in an old object. On the other hand, the
3355 old object won't have ".bar" satisfied by a new object.
3356
3357 Keep a list of newly added dot-symbols. */
3358
3359 if (string[0] == '.')
3360 {
3361 struct ppc_link_hash_table *htab;
3362
3363 htab = (struct ppc_link_hash_table *) table;
3364 eh->u.next_dot_sym = htab->dot_syms;
3365 htab->dot_syms = eh;
3366 }
65f38f15
AM
3367 }
3368
3369 return entry;
3370}
3371
2cdcc330
AM
3372struct tocsave_entry
3373{
3b421ab3
AM
3374 asection *sec;
3375 bfd_vma offset;
3376};
3377
3378static hashval_t
3379tocsave_htab_hash (const void *p)
3380{
3381 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3382 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3383}
3384
3385static int
3386tocsave_htab_eq (const void *p1, const void *p2)
3387{
3388 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3389 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3390 return e1->sec == e2->sec && e1->offset == e2->offset;
3391}
3392
68faa637
AM
3393/* Destroy a ppc64 ELF linker hash table. */
3394
3395static void
d495ab0d 3396ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3397{
d495ab0d 3398 struct ppc_link_hash_table *htab;
68faa637 3399
d495ab0d 3400 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3401 if (htab->tocsave_htab)
3402 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3403 bfd_hash_table_free (&htab->branch_hash_table);
3404 bfd_hash_table_free (&htab->stub_hash_table);
3405 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3406}
3407
65f38f15
AM
3408/* Create a ppc64 ELF linker hash table. */
3409
3410static struct bfd_link_hash_table *
4ce794b7 3411ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3412{
3413 struct ppc_link_hash_table *htab;
3414 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3415
4ce794b7 3416 htab = bfd_zmalloc (amt);
65f38f15
AM
3417 if (htab == NULL)
3418 return NULL;
3419
66eb6687 3420 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3421 sizeof (struct ppc_link_hash_entry),
3422 PPC64_ELF_DATA))
65f38f15 3423 {
e2d34d7d 3424 free (htab);
65f38f15
AM
3425 return NULL;
3426 }
3427
721956f4 3428 /* Init the stub hash table too. */
66eb6687
AM
3429 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3430 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3431 {
d495ab0d 3432 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3433 return NULL;
3434 }
721956f4
AM
3435
3436 /* And the branch hash table. */
66eb6687
AM
3437 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3438 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3439 {
3440 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3441 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3442 return NULL;
3443 }
721956f4 3444
3b421ab3
AM
3445 htab->tocsave_htab = htab_try_create (1024,
3446 tocsave_htab_hash,
3447 tocsave_htab_eq,
3448 NULL);
3449 if (htab->tocsave_htab == NULL)
2915c55b 3450 {
d495ab0d 3451 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3452 return NULL;
3453 }
d495ab0d 3454 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3455
3254fd24
AM
3456 /* Initializing two fields of the union is just cosmetic. We really
3457 only care about glist, but when compiled on a 32-bit host the
3458 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3459 debugger inspection of these fields look nicer. */
a6aa5195
AM
3460 htab->elf.init_got_refcount.refcount = 0;
3461 htab->elf.init_got_refcount.glist = NULL;
3462 htab->elf.init_plt_refcount.refcount = 0;
3463 htab->elf.init_plt_refcount.glist = NULL;
3464 htab->elf.init_got_offset.offset = 0;
3465 htab->elf.init_got_offset.glist = NULL;
3466 htab->elf.init_plt_offset.offset = 0;
3467 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3468
65f38f15
AM
3469 return &htab->elf.root;
3470}
3471
bfeb4a28
AM
3472/* Create sections for linker generated code. */
3473
3474static bfd_boolean
3475create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3476{
3477 struct ppc_link_hash_table *htab;
3478 flagword flags;
3479
3480 htab = ppc_hash_table (info);
3481
bfeb4a28
AM
3482 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3483 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3484 if (htab->params->save_restore_funcs)
3485 {
3486 /* Create .sfpr for code to save and restore fp regs. */
3487 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3488 flags);
3489 if (htab->sfpr == NULL
fd361982 3490 || !bfd_set_section_alignment (htab->sfpr, 2))
b32547cd
AM
3491 return FALSE;
3492 }
3493
3494 if (bfd_link_relocatable (info))
3495 return TRUE;
bfeb4a28
AM
3496
3497 /* Create .glink for lazy dynamic linking support. */
3498 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3499 flags);
3500 if (htab->glink == NULL
fd361982 3501 || !bfd_set_section_alignment (htab->glink, 3))
bfeb4a28
AM
3502 return FALSE;
3503
9e390558
AM
3504 /* The part of .glink used by global entry stubs, separate so that
3505 it can be aligned appropriately without affecting htab->glink. */
3506 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3507 flags);
3508 if (htab->global_entry == NULL
fd361982 3509 || !bfd_set_section_alignment (htab->global_entry, 2))
9e390558
AM
3510 return FALSE;
3511
bfeb4a28
AM
3512 if (!info->no_ld_generated_unwind_info)
3513 {
3514 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3515 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3516 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3517 ".eh_frame",
3518 flags);
3519 if (htab->glink_eh_frame == NULL
fd361982 3520 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
bfeb4a28
AM
3521 return FALSE;
3522 }
3523
3524 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3525 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3526 if (htab->elf.iplt == NULL
fd361982 3527 || !bfd_set_section_alignment (htab->elf.iplt, 3))
bfeb4a28
AM
3528 return FALSE;
3529
3530 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3531 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3532 htab->elf.irelplt
3533 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3534 if (htab->elf.irelplt == NULL
fd361982 3535 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
bfeb4a28
AM
3536 return FALSE;
3537
3538 /* Create branch lookup table for plt_branch stubs. */
3539 flags = (SEC_ALLOC | SEC_LOAD
3540 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3541 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3542 flags);
3543 if (htab->brlt == NULL
fd361982 3544 || !bfd_set_section_alignment (htab->brlt, 3))
bfeb4a28
AM
3545 return FALSE;
3546
2d7ad24e
AM
3547 /* Local plt entries, put in .branch_lt but a separate section for
3548 convenience. */
3549 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3550 flags);
3551 if (htab->pltlocal == NULL
fd361982 3552 || !bfd_set_section_alignment (htab->pltlocal, 3))
2d7ad24e
AM
3553 return FALSE;
3554
0e1862bb 3555 if (!bfd_link_pic (info))
bfeb4a28
AM
3556 return TRUE;
3557
3558 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3559 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3560 htab->relbrlt
3561 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3562 if (htab->relbrlt == NULL
fd361982 3563 || !bfd_set_section_alignment (htab->relbrlt, 3))
bfeb4a28
AM
3564 return FALSE;
3565
2d7ad24e
AM
3566 htab->relpltlocal
3567 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3568 if (htab->relpltlocal == NULL
fd361982 3569 || !bfd_set_section_alignment (htab->relpltlocal, 3))
2d7ad24e
AM
3570 return FALSE;
3571
bfeb4a28
AM
3572 return TRUE;
3573}
3574
e717da7e
AM
3575/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3576
bfeb4a28 3577bfd_boolean
e7d1c40c
AM
3578ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3579 struct ppc64_elf_params *params)
e717da7e
AM
3580{
3581 struct ppc_link_hash_table *htab;
3582
e7d1c40c 3583 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3584
3585/* Always hook our dynamic sections into the first bfd, which is the
3586 linker created stub bfd. This ensures that the GOT header is at
3587 the start of the output TOC section. */
3588 htab = ppc_hash_table (info);
e7d1c40c
AM
3589 htab->elf.dynobj = params->stub_bfd;
3590 htab->params = params;
bfeb4a28 3591
bfeb4a28 3592 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3593}
3594
721956f4
AM
3595/* Build a name for an entry in the stub hash table. */
3596
3597static char *
4ce794b7
AM
3598ppc_stub_name (const asection *input_section,
3599 const asection *sym_sec,
3600 const struct ppc_link_hash_entry *h,
3601 const Elf_Internal_Rela *rel)
721956f4
AM
3602{
3603 char *stub_name;
bcaa2f82 3604 ssize_t len;
721956f4
AM
3605
3606 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3607 offsets from a sym as a branch target? In fact, we could
3608 probably assume the addend is always zero. */
3609 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3610
3611 if (h)
3612 {
3613 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3614 stub_name = bfd_malloc (len);
46de2a7c
AM
3615 if (stub_name == NULL)
3616 return stub_name;
3617
bcaa2f82
AM
3618 len = sprintf (stub_name, "%08x.%s+%x",
3619 input_section->id & 0xffffffff,
3620 h->elf.root.root.string,
3621 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3622 }
3623 else
3624 {
ad8e1ba5 3625 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3626 stub_name = bfd_malloc (len);
46de2a7c
AM
3627 if (stub_name == NULL)
3628 return stub_name;
3629
bcaa2f82
AM
3630 len = sprintf (stub_name, "%08x.%x:%x+%x",
3631 input_section->id & 0xffffffff,
3632 sym_sec->id & 0xffffffff,
3633 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3634 (int) rel->r_addend & 0xffffffff);
721956f4 3635 }
bcaa2f82 3636 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3637 stub_name[len - 2] = 0;
721956f4
AM
3638 return stub_name;
3639}
3640
3641/* Look up an entry in the stub hash. Stub entries are cached because
3642 creating the stub name takes a bit of time. */
3643
3644static struct ppc_stub_hash_entry *
4ce794b7
AM
3645ppc_get_stub_entry (const asection *input_section,
3646 const asection *sym_sec,
039b3fef 3647 struct ppc_link_hash_entry *h,
4ce794b7
AM
3648 const Elf_Internal_Rela *rel,
3649 struct ppc_link_hash_table *htab)
721956f4
AM
3650{
3651 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3652 struct map_stub *group;
721956f4
AM
3653
3654 /* If this input section is part of a group of sections sharing one
3655 stub section, then use the id of the first section in the group.
3656 Stub names need to include a section id, as there may well be
3657 more than one stub used to reach say, printf, and we need to
3658 distinguish between them. */
6f20ed8a 3659 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3660 if (group == NULL)
3661 return NULL;
721956f4 3662
b3fac117
AM
3663 if (h != NULL && h->u.stub_cache != NULL
3664 && h->u.stub_cache->h == h
6f20ed8a 3665 && h->u.stub_cache->group == group)
721956f4 3666 {
b3fac117 3667 stub_entry = h->u.stub_cache;
721956f4
AM
3668 }
3669 else
3670 {
3671 char *stub_name;
3672
6f20ed8a 3673 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3674 if (stub_name == NULL)
3675 return NULL;
3676
3677 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3678 stub_name, FALSE, FALSE);
721956f4 3679 if (h != NULL)
b3fac117 3680 h->u.stub_cache = stub_entry;
721956f4
AM
3681
3682 free (stub_name);
3683 }
3684
3685 return stub_entry;
3686}
3687
3688/* Add a new stub entry to the stub hash. Not all fields of the new
3689 stub entry are initialised. */
3690
3691static struct ppc_stub_hash_entry *
4ce794b7
AM
3692ppc_add_stub (const char *stub_name,
3693 asection *section,
25f53a85 3694 struct bfd_link_info *info)
721956f4 3695{
25f53a85 3696 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3697 struct map_stub *group;
721956f4
AM
3698 asection *link_sec;
3699 asection *stub_sec;
3700 struct ppc_stub_hash_entry *stub_entry;
3701
6f20ed8a
AM
3702 group = htab->sec_info[section->id].u.group;
3703 link_sec = group->link_sec;
3704 stub_sec = group->stub_sec;
721956f4
AM
3705 if (stub_sec == NULL)
3706 {
6f20ed8a
AM
3707 size_t namelen;
3708 bfd_size_type len;
3709 char *s_name;
721956f4 3710
6f20ed8a
AM
3711 namelen = strlen (link_sec->name);
3712 len = namelen + sizeof (STUB_SUFFIX);
3713 s_name = bfd_alloc (htab->params->stub_bfd, len);
3714 if (s_name == NULL)
3715 return NULL;
721956f4 3716
6f20ed8a
AM
3717 memcpy (s_name, link_sec->name, namelen);
3718 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3719 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3720 if (stub_sec == NULL)
3721 return NULL;
3722 group->stub_sec = stub_sec;
721956f4
AM
3723 }
3724
3725 /* Enter this entry into the linker stub hash table. */
3726 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3727 TRUE, FALSE);
721956f4
AM
3728 if (stub_entry == NULL)
3729 {
695344c0 3730 /* xgettext:c-format */
cf97bcb0
AM
3731 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3732 section->owner, stub_name);
721956f4
AM
3733 return NULL;
3734 }
3735
6f20ed8a 3736 stub_entry->group = group;
721956f4 3737 stub_entry->stub_offset = 0;
721956f4
AM
3738 return stub_entry;
3739}
3740
e717da7e
AM
3741/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3742 not already done. */
65f38f15 3743
b34976b6 3744static bfd_boolean
e717da7e 3745create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3746{
e717da7e
AM
3747 asection *got, *relgot;
3748 flagword flags;
3749 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3750
0c8d6e5c 3751 if (!is_ppc64_elf (abfd))
0ffa91dd 3752 return FALSE;
4dfe6ac6
NC
3753 if (htab == NULL)
3754 return FALSE;
0ffa91dd 3755
33e44f2e
AM
3756 if (!htab->elf.sgot
3757 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3758 return FALSE;
e717da7e
AM
3759
3760 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3761 | SEC_LINKER_CREATED);
3762
c456f082 3763 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3764 if (!got
fd361982 3765 || !bfd_set_section_alignment (got, 3))
e717da7e 3766 return FALSE;
65f38f15 3767
c456f082
AM
3768 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3769 flags | SEC_READONLY);
e717da7e 3770 if (!relgot
fd361982 3771 || !bfd_set_section_alignment (relgot, 3))
b34976b6 3772 return FALSE;
e717da7e
AM
3773
3774 ppc64_elf_tdata (abfd)->got = got;
3775 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 3776 return TRUE;
65f38f15 3777}
5bd4f169 3778
b31867b6
AM
3779/* Follow indirect and warning symbol links. */
3780
3781static inline struct bfd_link_hash_entry *
3782follow_link (struct bfd_link_hash_entry *h)
3783{
3784 while (h->type == bfd_link_hash_indirect
3785 || h->type == bfd_link_hash_warning)
3786 h = h->u.i.link;
3787 return h;
3788}
3789
3790static inline struct elf_link_hash_entry *
3791elf_follow_link (struct elf_link_hash_entry *h)
3792{
3793 return (struct elf_link_hash_entry *) follow_link (&h->root);
3794}
3795
3796static inline struct ppc_link_hash_entry *
3797ppc_follow_link (struct ppc_link_hash_entry *h)
3798{
3799 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3800}
3801
40d16e0b
AM
3802/* Merge PLT info on FROM with that on TO. */
3803
3804static void
3805move_plt_plist (struct ppc_link_hash_entry *from,
3806 struct ppc_link_hash_entry *to)
3807{
3808 if (from->elf.plt.plist != NULL)
3809 {
3810 if (to->elf.plt.plist != NULL)
3811 {
3812 struct plt_entry **entp;
3813 struct plt_entry *ent;
3814
3815 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3816 {
3817 struct plt_entry *dent;
3818
3819 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3820 if (dent->addend == ent->addend)
3821 {
3822 dent->plt.refcount += ent->plt.refcount;
3823 *entp = ent->next;
3824 break;
3825 }
3826 if (dent == NULL)
3827 entp = &ent->next;
3828 }
3829 *entp = to->elf.plt.plist;
3830 }
3831
3832 to->elf.plt.plist = from->elf.plt.plist;
3833 from->elf.plt.plist = NULL;
3834 }
3835}
3836
65f38f15
AM
3837/* Copy the extra info we tack onto an elf_link_hash_entry. */
3838
3839static void
fcfa13d2
AM
3840ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3841 struct elf_link_hash_entry *dir,
3842 struct elf_link_hash_entry *ind)
65f38f15
AM
3843{
3844 struct ppc_link_hash_entry *edir, *eind;
3845
3846 edir = (struct ppc_link_hash_entry *) dir;
3847 eind = (struct ppc_link_hash_entry *) ind;
3848
c79d6685
AM
3849 edir->is_func |= eind->is_func;
3850 edir->is_func_descriptor |= eind->is_func_descriptor;
3851 edir->tls_mask |= eind->tls_mask;
3852 if (eind->oh != NULL)
3853 edir->oh = ppc_follow_link (eind->oh);
3854
474436e6 3855 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
3856 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3857 edir->elf.ref_regular |= eind->elf.ref_regular;
3858 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 3859 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
3860 edir->elf.needs_plt |= eind->elf.needs_plt;
3861 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 3862
d311bc8b
AM
3863 /* If we were called to copy over info for a weak sym, don't copy
3864 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3865 in order to simplify readonly_dynrelocs and save a field in the
3866 symbol hash entry, but that means dyn_relocs can't be used in any
3867 tests about a specific symbol, or affect other symbol flags which
ab2477e1 3868 are then tested. */
d311bc8b 3869 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 3870 return;
d311bc8b 3871
411e1bfb 3872 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 3873 if (eind->dyn_relocs != NULL)
65f38f15 3874 {
bbd7ec4a
AM
3875 if (edir->dyn_relocs != NULL)
3876 {
6061a67d
AM
3877 struct elf_dyn_relocs **pp;
3878 struct elf_dyn_relocs *p;
bbd7ec4a 3879
fcfa13d2 3880 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
3881 list. Merge any entries against the same section. */
3882 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3883 {
6061a67d 3884 struct elf_dyn_relocs *q;
bbd7ec4a
AM
3885
3886 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3887 if (q->sec == p->sec)
3888 {
3889 q->pc_count += p->pc_count;
3890 q->count += p->count;
3891 *pp = p->next;
3892 break;
3893 }
3894 if (q == NULL)
3895 pp = &p->next;
3896 }
3897 *pp = edir->dyn_relocs;
3898 }
3899
65f38f15
AM
3900 edir->dyn_relocs = eind->dyn_relocs;
3901 eind->dyn_relocs = NULL;
3902 }
65f38f15 3903
81848ca0
AM
3904 /* Copy over got entries that we may have already seen to the
3905 symbol which just became indirect. */
411e1bfb
AM
3906 if (eind->elf.got.glist != NULL)
3907 {
3908 if (edir->elf.got.glist != NULL)
3909 {
3910 struct got_entry **entp;
3911 struct got_entry *ent;
3912
3913 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3914 {
3915 struct got_entry *dent;
3916
3917 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3918 if (dent->addend == ent->addend
e717da7e 3919 && dent->owner == ent->owner
411e1bfb
AM
3920 && dent->tls_type == ent->tls_type)
3921 {
3922 dent->got.refcount += ent->got.refcount;
3923 *entp = ent->next;
3924 break;
3925 }
3926 if (dent == NULL)
3927 entp = &ent->next;
3928 }
3929 *entp = edir->elf.got.glist;
3930 }
3931
3932 edir->elf.got.glist = eind->elf.got.glist;
3933 eind->elf.got.glist = NULL;
3934 }
3935
3936 /* And plt entries. */
40d16e0b 3937 move_plt_plist (eind, edir);
411e1bfb 3938
fcfa13d2 3939 if (eind->elf.dynindx != -1)
411e1bfb 3940 {
fcfa13d2
AM
3941 if (edir->elf.dynindx != -1)
3942 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3943 edir->elf.dynstr_index);
411e1bfb
AM
3944 edir->elf.dynindx = eind->elf.dynindx;
3945 edir->elf.dynstr_index = eind->elf.dynstr_index;
3946 eind->elf.dynindx = -1;
3947 eind->elf.dynstr_index = 0;
3948 }
411e1bfb
AM
3949}
3950
8387904d
AM
3951/* Find the function descriptor hash entry from the given function code
3952 hash entry FH. Link the entries via their OH fields. */
3953
3954static struct ppc_link_hash_entry *
b31867b6 3955lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
3956{
3957 struct ppc_link_hash_entry *fdh = fh->oh;
3958
3959 if (fdh == NULL)
3960 {
3961 const char *fd_name = fh->elf.root.root.string + 1;
3962
3963 fdh = (struct ppc_link_hash_entry *)
3964 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
3965 if (fdh == NULL)
3966 return fdh;
3967
3968 fdh->is_func_descriptor = 1;
3969 fdh->oh = fh;
3970 fh->is_func = 1;
3971 fh->oh = fdh;
8387904d
AM
3972 }
3973
8c5b4e52
AM
3974 fdh = ppc_follow_link (fdh);
3975 fdh->is_func_descriptor = 1;
3976 fdh->oh = fh;
3977 return fdh;
8387904d
AM
3978}
3979
8c5b4e52 3980/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
3981
3982static struct ppc_link_hash_entry *
3983make_fdh (struct bfd_link_info *info,
908b32fc 3984 struct ppc_link_hash_entry *fh)
bb700d78 3985{
8c5b4e52
AM
3986 bfd *abfd = fh->elf.root.u.undef.abfd;
3987 struct bfd_link_hash_entry *bh = NULL;
bb700d78 3988 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
3989 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3990 ? BSF_WEAK
3991 : BSF_GLOBAL);
3992
3993 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3994 fh->elf.root.root.string + 1,
3995 flags, bfd_und_section_ptr, 0,
3996 NULL, FALSE, FALSE, &bh))
bb700d78
AM
3997 return NULL;
3998
3999 fdh = (struct ppc_link_hash_entry *) bh;
4000 fdh->elf.non_elf = 0;
908b32fc
AM
4001 fdh->fake = 1;
4002 fdh->is_func_descriptor = 1;
4003 fdh->oh = fh;
4004 fh->is_func = 1;
4005 fh->oh = fdh;
bb700d78
AM
4006 return fdh;
4007}
4008
8387904d
AM
4009/* Fix function descriptor symbols defined in .opd sections to be
4010 function type. */
555cd476
AM
4011
4012static bfd_boolean
c16153ae 4013ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4014 struct bfd_link_info *info,
555cd476 4015 Elf_Internal_Sym *isym,
6911b7dc 4016 const char **name,
555cd476
AM
4017 flagword *flags ATTRIBUTE_UNUSED,
4018 asection **sec,
b53dfeb2 4019 bfd_vma *value)
555cd476 4020{
b53dfeb2 4021 if (*sec != NULL
f1885d1e 4022 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4023 {
4024 asection *code_sec;
4025
4026 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4027 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4028 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4029
4030 /* If the symbol is a function defined in .opd, and the function
4031 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4032 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4033 && (*sec)->reloc_count != 0
4034 && opd_entry_value (*sec, *value, &code_sec, NULL,
4035 FALSE) != (bfd_vma) -1
4036 && discarded_section (code_sec))
4037 {
4038 *sec = bfd_und_section_ptr;
4039 isym->st_shndx = SHN_UNDEF;
4040 }
4041 }
dbd1e97e
AM
4042 else if (*sec != NULL
4043 && strcmp ((*sec)->name, ".toc") == 0
4044 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4045 {
4046 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4047 if (htab != NULL)
4048 htab->params->object_in_toc = 1;
4049 }
433817dd 4050
6911b7dc
AM
4051 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4052 {
4053 if (abiversion (ibfd) == 0)
4054 set_abiversion (ibfd, 2);
4055 else if (abiversion (ibfd) == 1)
4056 {
cf97bcb0
AM
4057 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4058 " for ABI version 1"), *name);
6911b7dc
AM
4059 bfd_set_error (bfd_error_bad_value);
4060 return FALSE;
4061 }
4062 }
4063
555cd476
AM
4064 return TRUE;
4065}
4066
6911b7dc
AM
4067/* Merge non-visibility st_other attributes: local entry point. */
4068
4069static void
4070ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4071 const Elf_Internal_Sym *isym,
4072 bfd_boolean definition,
4073 bfd_boolean dynamic)
4074{
f378ab09 4075 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
4076 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4077 | ELF_ST_VISIBILITY (h->other));
4078}
4079
8c5b4e52
AM
4080/* Hook called on merging a symbol. We use this to clear "fake" since
4081 we now have a real symbol. */
4082
4083static bfd_boolean
4084ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4085 const Elf_Internal_Sym *isym,
8c5b4e52
AM
4086 asection **psec ATTRIBUTE_UNUSED,
4087 bfd_boolean newdef ATTRIBUTE_UNUSED,
4088 bfd_boolean olddef ATTRIBUTE_UNUSED,
4089 bfd *oldbfd ATTRIBUTE_UNUSED,
4090 const asection *oldsec ATTRIBUTE_UNUSED)
4091{
4092 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
4093 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4094 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
4095 return TRUE;
4096}
4097
8387904d 4098/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4099 inclusion of a new ABI object archive that defines "bar".
4100 NAME is a symbol defined in an archive. Return a symbol in the hash
4101 table that might be satisfied by the archive symbols. */
8387904d
AM
4102
4103static struct elf_link_hash_entry *
4104ppc64_elf_archive_symbol_lookup (bfd *abfd,
4105 struct bfd_link_info *info,
4106 const char *name)
4107{
4108 struct elf_link_hash_entry *h;
4109 char *dot_name;
4110 size_t len;
4111
4112 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4113 if (h != NULL
4114 /* Don't return this sym if it is a fake function descriptor
4115 created by add_symbol_adjust. */
8c5b4e52 4116 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
4117 return h;
4118
4119 if (name[0] == '.')
4120 return h;
4121
4122 len = strlen (name);
4123 dot_name = bfd_alloc (abfd, len + 2);
4124 if (dot_name == NULL)
e99955cd 4125 return (struct elf_link_hash_entry *) -1;
8387904d
AM
4126 dot_name[0] = '.';
4127 memcpy (dot_name + 1, name, len + 1);
4128 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4129 bfd_release (abfd, dot_name);
4130 return h;
4131}
4132
4133/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4134 new ABI object defines "bar". Well, at least, undefined dot symbols
4135 are made weak. This stops later archive searches from including an
4136 object if we already have a function descriptor definition. It also
35b0ce59
AM
4137 prevents the linker complaining about undefined symbols.
4138 We also check and correct mismatched symbol visibility here. The
4139 most restrictive visibility of the function descriptor and the
4140 function entry symbol is used. */
8387904d
AM
4141
4142static bfd_boolean
b3fac117 4143add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4144{
8387904d
AM
4145 struct ppc_link_hash_table *htab;
4146 struct ppc_link_hash_entry *fdh;
4147
b3fac117
AM
4148 if (eh->elf.root.type == bfd_link_hash_warning)
4149 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4150
8400d40d
AM
4151 if (eh->elf.root.type == bfd_link_hash_indirect)
4152 return TRUE;
4153
b3fac117
AM
4154 if (eh->elf.root.root.string[0] != '.')
4155 abort ();
8387904d 4156
b3fac117 4157 htab = ppc_hash_table (info);
4dfe6ac6
NC
4158 if (htab == NULL)
4159 return FALSE;
4160
b31867b6 4161 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4162 if (fdh == NULL
4163 && !bfd_link_relocatable (info)
4164 && (eh->elf.root.type == bfd_link_hash_undefined
4165 || eh->elf.root.type == bfd_link_hash_undefweak)
4166 && eh->elf.ref_regular)
4167 {
4168 /* Make an undefined function descriptor sym, in order to
4169 pull in an --as-needed shared lib. Archives are handled
4170 elsewhere. */
4171 fdh = make_fdh (info, eh);
4172 if (fdh == NULL)
4173 return FALSE;
bb700d78 4174 }
8c5b4e52
AM
4175
4176 if (fdh != NULL)
8387904d 4177 {
35b0ce59
AM
4178 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4179 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4180
4181 /* Make both descriptor and entry symbol have the most
4182 constraining visibility of either symbol. */
35b0ce59
AM
4183 if (entry_vis < descr_vis)
4184 fdh->elf.other += entry_vis - descr_vis;
4185 else if (entry_vis > descr_vis)
4186 eh->elf.other += descr_vis - entry_vis;
4187
8c5b4e52
AM
4188 /* Propagate reference flags from entry symbol to function
4189 descriptor symbol. */
bc4e12de 4190 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4191 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4192 fdh->elf.ref_regular |= eh->elf.ref_regular;
4193 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4194
4195 if (!fdh->elf.forced_local
4196 && fdh->elf.dynindx == -1
4197 && fdh->elf.versioned != versioned_hidden
4198 && (bfd_link_dll (info)
4199 || fdh->elf.def_dynamic
4200 || fdh->elf.ref_dynamic)
4201 && (eh->elf.ref_regular
4202 || eh->elf.def_regular))
4203 {
2cdcc330 4204 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
8c5b4e52 4205 return FALSE;
35b0ce59 4206 }
8387904d 4207 }
99877b66 4208
8387904d
AM
4209 return TRUE;
4210}
4211
f6c7c3e8
AM
4212/* Set up opd section info and abiversion for IBFD, and process list
4213 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4214
8387904d 4215static bfd_boolean
f6c7c3e8 4216ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4217{
99877b66 4218 struct ppc_link_hash_table *htab;
b3fac117 4219 struct ppc_link_hash_entry **p, *eh;
459609d6 4220 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4221
459609d6 4222 if (opd != NULL && opd->size != 0)
b3fac117 4223 {
b9399fcf
AM
4224 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4225 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4226
459609d6
AM
4227 if (abiversion (ibfd) == 0)
4228 set_abiversion (ibfd, 1);
8a2058b5 4229 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4230 {
695344c0 4231 /* xgettext:c-format */
cf97bcb0
AM
4232 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4233 ibfd, abiversion (ibfd));
459609d6
AM
4234 bfd_set_error (bfd_error_bad_value);
4235 return FALSE;
f6c7c3e8 4236 }
b9399fcf 4237 }
f6c7c3e8 4238
b9399fcf
AM
4239 if (is_ppc64_elf (info->output_bfd))
4240 {
4241 /* For input files without an explicit abiversion in e_flags
4242 we should have flagged any with symbol st_other bits set
4243 as ELFv1 and above flagged those with .opd as ELFv2.
4244 Set the output abiversion if not yet set, and for any input
4245 still ambiguous, take its abiversion from the output.
4246 Differences in ABI are reported later. */
4247 if (abiversion (info->output_bfd) == 0)
4248 set_abiversion (info->output_bfd, abiversion (ibfd));
4249 else if (abiversion (ibfd) == 0)
4250 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4251 }
4252
459609d6
AM
4253 htab = ppc_hash_table (info);
4254 if (htab == NULL)
b9399fcf 4255 return TRUE;
459609d6 4256
b9399fcf
AM
4257 if (opd != NULL && opd->size != 0
4258 && (ibfd->flags & DYNAMIC) == 0
4259 && (opd->flags & SEC_RELOC) != 0
4260 && opd->reloc_count != 0
4261 && !bfd_is_abs_section (opd->output_section)
4262 && info->gc_sections)
4263 {
4264 /* Garbage collection needs some extra help with .opd sections.
4265 We don't want to necessarily keep everything referenced by
4266 relocs in .opd, as that would keep all functions. Instead,
4267 if we reference an .opd symbol (a function descriptor), we
4268 want to keep the function code symbol's section. This is
4269 easy for global symbols, but for local syms we need to keep
4270 information about the associated function section. */
4271 bfd_size_type amt;
4272 asection **opd_sym_map;
4273 Elf_Internal_Shdr *symtab_hdr;
4274 Elf_Internal_Rela *relocs, *rel_end, *rel;
4275
4276 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4277 opd_sym_map = bfd_zalloc (ibfd, amt);
4278 if (opd_sym_map == NULL)
4279 return FALSE;
4280 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4281 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4282 info->keep_memory);
4283 if (relocs == NULL)
4284 return FALSE;
4285 symtab_hdr = &elf_symtab_hdr (ibfd);
4286 rel_end = relocs + opd->reloc_count - 1;
4287 for (rel = relocs; rel < rel_end; rel++)
4288 {
4289 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4290 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4291
4292 if (r_type == R_PPC64_ADDR64
4293 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4294 && r_symndx < symtab_hdr->sh_info)
4295 {
4296 Elf_Internal_Sym *isym;
4297 asection *s;
4298
4299 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4300 if (isym == NULL)
4301 {
4302 if (elf_section_data (opd)->relocs != relocs)
4303 free (relocs);
4304 return FALSE;
4305 }
4306
4307 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4308 if (s != NULL && s != opd)
4309 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4310 }
4311 }
4312 if (elf_section_data (opd)->relocs != relocs)
4313 free (relocs);
4314 }
459609d6
AM
4315
4316 p = &htab->dot_syms;
4317 while ((eh = *p) != NULL)
4318 {
4319 *p = NULL;
4320 if (&eh->elf == htab->elf.hgot)
4321 ;
4322 else if (htab->elf.hgot == NULL
4323 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4324 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4325 else if (abiversion (ibfd) <= 1)
4326 {
4327 htab->need_func_desc_adj = 1;
4328 if (!add_symbol_adjust (eh, info))
4329 return FALSE;
4330 }
459609d6
AM
4331 p = &eh->u.next_dot_sym;
4332 }
b3fac117 4333 return TRUE;
8387904d
AM
4334}
4335
97fed1c9
JJ
4336/* Undo hash table changes when an --as-needed input file is determined
4337 not to be needed. */
4338
4339static bfd_boolean
e5034e59
AM
4340ppc64_elf_notice_as_needed (bfd *ibfd,
4341 struct bfd_link_info *info,
4342 enum notice_asneeded_action act)
97fed1c9 4343{
e5034e59
AM
4344 if (act == notice_not_needed)
4345 {
4346 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4347
e5034e59
AM
4348 if (htab == NULL)
4349 return FALSE;
4dfe6ac6 4350
e5034e59
AM
4351 htab->dot_syms = NULL;
4352 }
4353 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4354}
4355
aa374f67
AM
4356/* If --just-symbols against a final linked binary, then assume we need
4357 toc adjusting stubs when calling functions defined there. */
4358
4359static void
4360ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4361{
4362 if ((sec->flags & SEC_CODE) != 0
4363 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4364 && is_ppc64_elf (sec->owner))
4365 {
2c3f079f
AM
4366 if (abiversion (sec->owner) >= 2
4367 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4368 sec->has_toc_reloc = 1;
4369 }
4370 _bfd_elf_link_just_syms (sec, info);
4371}
4372
e054468f 4373static struct plt_entry **
4ce794b7
AM
4374update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4375 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4376{
4377 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4378 struct plt_entry **local_plt;
f961d9dd 4379 unsigned char *local_got_tls_masks;
411e1bfb
AM
4380
4381 if (local_got_ents == NULL)
4382 {
4383 bfd_size_type size = symtab_hdr->sh_info;
4384
e054468f
AM
4385 size *= (sizeof (*local_got_ents)
4386 + sizeof (*local_plt)
4387 + sizeof (*local_got_tls_masks));
4ce794b7 4388 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4389 if (local_got_ents == NULL)
e054468f 4390 return NULL;
411e1bfb
AM
4391 elf_local_got_ents (abfd) = local_got_ents;
4392 }
4393
37da22e5 4394 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4395 {
4396 struct got_entry *ent;
4397
4398 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4399 if (ent->addend == r_addend
4400 && ent->owner == abfd
4401 && ent->tls_type == tls_type)
411e1bfb
AM
4402 break;
4403 if (ent == NULL)
4404 {
4405 bfd_size_type amt = sizeof (*ent);
4ce794b7 4406 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4407 if (ent == NULL)
4408 return FALSE;
4409 ent->next = local_got_ents[r_symndx];
4410 ent->addend = r_addend;
e717da7e 4411 ent->owner = abfd;
411e1bfb 4412 ent->tls_type = tls_type;
927be08e 4413 ent->is_indirect = FALSE;
411e1bfb
AM
4414 ent->got.refcount = 0;
4415 local_got_ents[r_symndx] = ent;
4416 }
4417 ent->got.refcount += 1;
4418 }
4419
e054468f 4420 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4421 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4422 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4423
4424 return local_plt + r_symndx;
65f38f15
AM
4425}
4426
411e1bfb 4427static bfd_boolean
e054468f 4428update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4429{
411e1bfb 4430 struct plt_entry *ent;
1e2f5b6e 4431
e054468f 4432 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4433 if (ent->addend == addend)
4434 break;
4435 if (ent == NULL)
1e2f5b6e 4436 {
411e1bfb 4437 bfd_size_type amt = sizeof (*ent);
4ce794b7 4438 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4439 if (ent == NULL)
4440 return FALSE;
e054468f 4441 ent->next = *plist;
411e1bfb
AM
4442 ent->addend = addend;
4443 ent->plt.refcount = 0;
e054468f 4444 *plist = ent;
1e2f5b6e 4445 }
411e1bfb 4446 ent->plt.refcount += 1;
b34976b6 4447 return TRUE;
1e2f5b6e
AM
4448}
4449
e054468f
AM
4450static bfd_boolean
4451is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4452{
4453 return (r_type == R_PPC64_REL24
05d0e962 4454 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
4455 || r_type == R_PPC64_REL14
4456 || r_type == R_PPC64_REL14_BRTAKEN
4457 || r_type == R_PPC64_REL14_BRNTAKEN
4458 || r_type == R_PPC64_ADDR24
4459 || r_type == R_PPC64_ADDR14
4460 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4461 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4462 || r_type == R_PPC64_PLTCALL
4463 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4464}
4465
4466/* Relocs on inline plt call sequence insns prior to the call. */
4467
4468static bfd_boolean
4469is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4470{
4471 return (r_type == R_PPC64_PLT16_HA
4472 || r_type == R_PPC64_PLT16_HI
4473 || r_type == R_PPC64_PLT16_LO
4474 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4475 || r_type == R_PPC64_PLT_PCREL34
4476 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4477 || r_type == R_PPC64_PLTSEQ
4478 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4479}
4480
5bd4f169 4481/* Look through the relocs for a section during the first phase, and
65f38f15 4482 calculate needed space in the global offset table, procedure
5d1634d7 4483 linkage table, and dynamic reloc sections. */
5bd4f169 4484
b34976b6 4485static bfd_boolean
4ce794b7
AM
4486ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4487 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4488{
65f38f15 4489 struct ppc_link_hash_table *htab;
5bd4f169 4490 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4491 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4492 const Elf_Internal_Rela *rel;
4493 const Elf_Internal_Rela *rel_end;
5bd4f169 4494 asection *sreloc;
3a71aa26 4495 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 4496 bfd_boolean is_opd;
5bd4f169 4497
0e1862bb 4498 if (bfd_link_relocatable (info))
b34976b6 4499 return TRUE;
5bd4f169 4500
680a3378
AM
4501 /* Don't do anything special with non-loaded, non-alloced sections.
4502 In particular, any relocs in such sections should not affect GOT
4503 and PLT reference counting (ie. we don't allow them to create GOT
4504 or PLT entries), there's no possibility or desire to optimize TLS
4505 relocs, and there's not much point in propagating relocs to shared
4506 libs that the dynamic linker won't relocate. */
4507 if ((sec->flags & SEC_ALLOC) == 0)
4508 return TRUE;
4509
0c8d6e5c 4510 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4511
65f38f15 4512 htab = ppc_hash_table (info);
4dfe6ac6
NC
4513 if (htab == NULL)
4514 return FALSE;
4515
3a71aa26
AM
4516 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4517 FALSE, FALSE, TRUE);
4518 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4519 FALSE, FALSE, TRUE);
0ffa91dd 4520 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4521 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4522 sreloc = NULL;
b9399fcf 4523 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4524 rel_end = relocs + sec->reloc_count;
4525 for (rel = relocs; rel < rel_end; rel++)
4526 {
4527 unsigned long r_symndx;
4528 struct elf_link_hash_entry *h;
04c9666a 4529 enum elf_ppc64_reloc_type r_type;
727fc41e 4530 int tls_type;
7c8fe5c4 4531 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4532 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
4533
4534 r_symndx = ELF64_R_SYM (rel->r_info);
4535 if (r_symndx < symtab_hdr->sh_info)
4536 h = NULL;
4537 else
973a3492
L
4538 {
4539 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4540 h = elf_follow_link (h);
1c865ab2
AM
4541
4542 if (h == htab->elf.hgot)
4543 sec->has_toc_reloc = 1;
973a3492 4544 }
5bd4f169 4545
5663e321
AM
4546 r_type = ELF64_R_TYPE (rel->r_info);
4547 switch (r_type)
4548 {
04bdff6a
AM
4549 case R_PPC64_D34:
4550 case R_PPC64_D34_LO:
4551 case R_PPC64_D34_HI30:
4552 case R_PPC64_D34_HA30:
4553 case R_PPC64_D28:
c213164a
AM
4554 case R_PPC64_TPREL34:
4555 case R_PPC64_DTPREL34:
04bdff6a 4556 case R_PPC64_PCREL34:
5663e321 4557 case R_PPC64_GOT_PCREL34:
c213164a
AM
4558 case R_PPC64_GOT_TLSGD34:
4559 case R_PPC64_GOT_TLSLD34:
4560 case R_PPC64_GOT_TPREL34:
4561 case R_PPC64_GOT_DTPREL34:
5663e321
AM
4562 case R_PPC64_PLT_PCREL34:
4563 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a
AM
4564 case R_PPC64_PCREL28:
4565 htab->powerxx_stubs = 1;
133a1f60
AM
4566 break;
4567 default:
5663e321
AM
4568 break;
4569 }
903b777d
AM
4570
4571 switch (r_type)
4572 {
4573 case R_PPC64_PLT16_HA:
4574 case R_PPC64_GOT_TLSLD16_HA:
4575 case R_PPC64_GOT_TLSGD16_HA:
4576 case R_PPC64_GOT_TPREL16_HA:
4577 case R_PPC64_GOT_DTPREL16_HA:
4578 case R_PPC64_GOT16_HA:
4579 case R_PPC64_TOC16_HA:
4580 case R_PPC64_PLT16_LO:
4581 case R_PPC64_PLT16_LO_DS:
4582 case R_PPC64_GOT_TLSLD16_LO:
4583 case R_PPC64_GOT_TLSGD16_LO:
4584 case R_PPC64_GOT_TPREL16_LO_DS:
4585 case R_PPC64_GOT_DTPREL16_LO_DS:
4586 case R_PPC64_GOT16_LO:
4587 case R_PPC64_GOT16_LO_DS:
4588 case R_PPC64_TOC16_LO:
4589 case R_PPC64_TOC16_LO_DS:
4590 case R_PPC64_GOT_PCREL34:
4591 ppc64_elf_tdata (abfd)->has_optrel = 1;
4592 ppc64_elf_section_data (sec)->has_optrel = 1;
4593 break;
4594 default:
4595 break;
4596 }
4597
f749f26e 4598 ifunc = NULL;
25f23106
AM
4599 if (h != NULL)
4600 {
4601 if (h->type == STT_GNU_IFUNC)
4602 {
4603 h->needs_plt = 1;
4604 ifunc = &h->plt.plist;
4605 }
4606 }
4607 else
4608 {
4609 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4610 abfd, r_symndx);
4611 if (isym == NULL)
4612 return FALSE;
4613
4614 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4615 {
4616 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4617 rel->r_addend,
37da22e5 4618 NON_GOT | PLT_IFUNC);
25f23106
AM
4619 if (ifunc == NULL)
4620 return FALSE;
4621 }
4622 }
727fc41e 4623
f749f26e 4624 tls_type = 0;
a33d1f77 4625 switch (r_type)
5bd4f169 4626 {
727fc41e
AM
4627 case R_PPC64_TLSGD:
4628 case R_PPC64_TLSLD:
4629 /* These special tls relocs tie a call to __tls_get_addr with
4630 its parameter symbol. */
37da22e5
AM
4631 if (h != NULL)
4632 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4633 else
4634 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4635 rel->r_addend,
37da22e5
AM
4636 NON_GOT | TLS_TLS | TLS_MARK))
4637 return FALSE;
4638 sec->has_tls_reloc = 1;
727fc41e
AM
4639 break;
4640
411e1bfb
AM
4641 case R_PPC64_GOT_TLSLD16:
4642 case R_PPC64_GOT_TLSLD16_LO:
4643 case R_PPC64_GOT_TLSLD16_HI:
4644 case R_PPC64_GOT_TLSLD16_HA:
c213164a 4645 case R_PPC64_GOT_TLSLD34:
951fd09b 4646 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4647 goto dogottls;
4648
4649 case R_PPC64_GOT_TLSGD16:
4650 case R_PPC64_GOT_TLSGD16_LO:
4651 case R_PPC64_GOT_TLSGD16_HI:
4652 case R_PPC64_GOT_TLSGD16_HA:
c213164a 4653 case R_PPC64_GOT_TLSGD34:
951fd09b 4654 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4655 goto dogottls;
4656
4657 case R_PPC64_GOT_TPREL16_DS:
4658 case R_PPC64_GOT_TPREL16_LO_DS:
4659 case R_PPC64_GOT_TPREL16_HI:
4660 case R_PPC64_GOT_TPREL16_HA:
c213164a 4661 case R_PPC64_GOT_TPREL34:
7c8bbca5 4662 if (bfd_link_dll (info))
411e1bfb
AM
4663 info->flags |= DF_STATIC_TLS;
4664 tls_type = TLS_TLS | TLS_TPREL;
4665 goto dogottls;
4666
4667 case R_PPC64_GOT_DTPREL16_DS:
4668 case R_PPC64_GOT_DTPREL16_LO_DS:
4669 case R_PPC64_GOT_DTPREL16_HI:
4670 case R_PPC64_GOT_DTPREL16_HA:
c213164a 4671 case R_PPC64_GOT_DTPREL34:
411e1bfb
AM
4672 tls_type = TLS_TLS | TLS_DTPREL;
4673 dogottls:
4674 sec->has_tls_reloc = 1;
066f4018 4675 goto dogot;
411e1bfb 4676
903b777d
AM
4677 case R_PPC64_GOT16:
4678 case R_PPC64_GOT16_LO:
4679 case R_PPC64_GOT16_HI:
65f38f15 4680 case R_PPC64_GOT16_HA:
903b777d 4681 case R_PPC64_GOT16_DS:
066f4018 4682 case R_PPC64_GOT16_LO_DS:
4a421c53 4683 case R_PPC64_GOT_PCREL34:
066f4018 4684 dogot:
65f38f15 4685 /* This symbol requires a global offset table entry. */
4c52953f 4686 sec->has_toc_reloc = 1;
33c0ec9d
AM
4687 if (r_type == R_PPC64_GOT_TLSLD16
4688 || r_type == R_PPC64_GOT_TLSGD16
4689 || r_type == R_PPC64_GOT_TPREL16_DS
4690 || r_type == R_PPC64_GOT_DTPREL16_DS
4691 || r_type == R_PPC64_GOT16
4692 || r_type == R_PPC64_GOT16_DS)
4693 {
4694 htab->do_multi_toc = 1;
d77c8a4b 4695 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4696 }
4697
e717da7e
AM
4698 if (ppc64_elf_tdata (abfd)->got == NULL
4699 && !create_got_section (abfd, info))
b34976b6 4700 return FALSE;
5bd4f169
AM
4701
4702 if (h != NULL)
4703 {
411e1bfb
AM
4704 struct ppc_link_hash_entry *eh;
4705 struct got_entry *ent;
65f38f15 4706
411e1bfb
AM
4707 eh = (struct ppc_link_hash_entry *) h;
4708 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
133a1f60 4709 if (ent->addend == rel->r_addend
e717da7e 4710 && ent->owner == abfd
411e1bfb
AM
4711 && ent->tls_type == tls_type)
4712 break;
4713 if (ent == NULL)
5bd4f169 4714 {
411e1bfb 4715 bfd_size_type amt = sizeof (*ent);
4ce794b7 4716 ent = bfd_alloc (abfd, amt);
411e1bfb 4717 if (ent == NULL)
b34976b6 4718 return FALSE;
411e1bfb 4719 ent->next = eh->elf.got.glist;
133a1f60 4720 ent->addend = rel->r_addend;
e717da7e 4721 ent->owner = abfd;
411e1bfb 4722 ent->tls_type = tls_type;
927be08e 4723 ent->is_indirect = FALSE;
411e1bfb
AM
4724 ent->got.refcount = 0;
4725 eh->elf.got.glist = ent;
5bd4f169 4726 }
411e1bfb 4727 ent->got.refcount += 1;
e7b938ca 4728 eh->tls_mask |= tls_type;
5bd4f169 4729 }
411e1bfb
AM
4730 else
4731 /* This is a global offset table entry for a local symbol. */
4732 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4733 rel->r_addend, tls_type))
411e1bfb 4734 return FALSE;
a345bc8d
AM
4735
4736 /* We may also need a plt entry if the symbol turns out to be
4737 an ifunc. */
0e1862bb 4738 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d 4739 {
133a1f60 4740 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
a345bc8d
AM
4741 return FALSE;
4742 }
5bd4f169
AM
4743 break;
4744
5bd4f169 4745 case R_PPC64_PLT16_HA:
65f38f15
AM
4746 case R_PPC64_PLT16_HI:
4747 case R_PPC64_PLT16_LO:
08be3224 4748 case R_PPC64_PLT16_LO_DS:
5663e321
AM
4749 case R_PPC64_PLT_PCREL34:
4750 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
4751 case R_PPC64_PLT32:
4752 case R_PPC64_PLT64:
cbf95972
AM
4753 /* This symbol requires a procedure linkage table entry. */
4754 plt_list = ifunc;
4755 if (h != NULL)
e054468f 4756 {
e054468f
AM
4757 h->needs_plt = 1;
4758 if (h->root.root.string[0] == '.'
4759 && h->root.root.string[1] != '\0')
4760 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2d7ad24e 4761 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4762 plt_list = &h->plt.plist;
4763 }
4764 if (plt_list == NULL)
2d7ad24e 4765 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4766 rel->r_addend,
2d7ad24e 4767 NON_GOT | PLT_KEEP);
133a1f60 4768 if (!update_plt_info (abfd, plt_list, rel->r_addend))
cbf95972 4769 return FALSE;
5bd4f169
AM
4770 break;
4771
4772 /* The following relocations don't need to propagate the
4773 relocation if linking a shared object since they are
4774 section relative. */
4775 case R_PPC64_SECTOFF:
4776 case R_PPC64_SECTOFF_LO:
4777 case R_PPC64_SECTOFF_HI:
4778 case R_PPC64_SECTOFF_HA:
4779 case R_PPC64_SECTOFF_DS:
4780 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4781 case R_PPC64_DTPREL16:
4782 case R_PPC64_DTPREL16_LO:
4783 case R_PPC64_DTPREL16_HI:
4784 case R_PPC64_DTPREL16_HA:
4785 case R_PPC64_DTPREL16_DS:
4786 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
4787 case R_PPC64_DTPREL16_HIGH:
4788 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
4789 case R_PPC64_DTPREL16_HIGHER:
4790 case R_PPC64_DTPREL16_HIGHERA:
4791 case R_PPC64_DTPREL16_HIGHEST:
4792 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4793 break;
4794
ad8e1ba5 4795 /* Nor do these. */
25f23106
AM
4796 case R_PPC64_REL16:
4797 case R_PPC64_REL16_LO:
4798 case R_PPC64_REL16_HI:
4799 case R_PPC64_REL16_HA:
4a969973
AM
4800 case R_PPC64_REL16_HIGH:
4801 case R_PPC64_REL16_HIGHA:
4802 case R_PPC64_REL16_HIGHER:
4803 case R_PPC64_REL16_HIGHERA:
4804 case R_PPC64_REL16_HIGHEST:
4805 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
4806 case R_PPC64_REL16_HIGHER34:
4807 case R_PPC64_REL16_HIGHERA34:
4808 case R_PPC64_REL16_HIGHEST34:
4809 case R_PPC64_REL16_HIGHESTA34:
a680de9a 4810 case R_PPC64_REL16DX_HA:
25f23106
AM
4811 break;
4812
45965137
AM
4813 /* Not supported as a dynamic relocation. */
4814 case R_PPC64_ADDR64_LOCAL:
0e1862bb 4815 if (bfd_link_pic (info))
45965137
AM
4816 {
4817 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4818 ppc_howto_init ();
695344c0 4819 /* xgettext:c-format */
174d0a74 4820 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 4821 "in shared libraries and PIEs\n"),
45965137
AM
4822 abfd, sec, rel->r_offset,
4823 ppc64_elf_howto_table[r_type]->name);
4824 bfd_set_error (bfd_error_bad_value);
4825 return FALSE;
4826 }
4827 break;
4828
ad8e1ba5 4829 case R_PPC64_TOC16:
33c0ec9d
AM
4830 case R_PPC64_TOC16_DS:
4831 htab->do_multi_toc = 1;
d77c8a4b 4832 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 4833 /* Fall through. */
ad8e1ba5
AM
4834 case R_PPC64_TOC16_LO:
4835 case R_PPC64_TOC16_HI:
4836 case R_PPC64_TOC16_HA:
ad8e1ba5 4837 case R_PPC64_TOC16_LO_DS:
4c52953f 4838 sec->has_toc_reloc = 1;
ec73ddcd 4839 if (h != NULL && bfd_link_executable (info))
1bdd8fac
AM
4840 {
4841 /* We may need a copy reloc. */
4842 h->non_got_ref = 1;
4843 /* Strongly prefer a copy reloc over a dynamic reloc.
4844 glibc ld.so as of 2019-08 will error out if one of
4845 these relocations is emitted. */
4846 h->needs_copy = 1;
4847 goto dodyn;
4848 }
ad8e1ba5
AM
4849 break;
4850
006589cf
AM
4851 /* Marker reloc. */
4852 case R_PPC64_ENTRY:
4853 break;
4854
5bd4f169
AM
4855 /* This relocation describes the C++ object vtable hierarchy.
4856 Reconstruct it for later use during GC. */
4857 case R_PPC64_GNU_VTINHERIT:
c152c796 4858 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4859 return FALSE;
5bd4f169
AM
4860 break;
4861
4862 /* This relocation describes which C++ vtable entries are actually
4863 used. Record for later use during GC. */
4864 case R_PPC64_GNU_VTENTRY:
a0ea3a14 4865 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4866 return FALSE;
5bd4f169
AM
4867 break;
4868
721956f4
AM
4869 case R_PPC64_REL14:
4870 case R_PPC64_REL14_BRTAKEN:
4871 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4872 {
4873 asection *dest = NULL;
4874
4875 /* Heuristic: If jumping outside our section, chances are
4876 we are going to need a stub. */
4877 if (h != NULL)
4878 {
4879 /* If the sym is weak it may be overridden later, so
4880 don't assume we know where a weak sym lives. */
4881 if (h->root.type == bfd_link_hash_defined)
4882 dest = h->root.u.def.section;
4883 }
4884 else
87d72d41
AM
4885 {
4886 Elf_Internal_Sym *isym;
4887
4888 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4889 abfd, r_symndx);
4890 if (isym == NULL)
4891 return FALSE;
4892
4893 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4894 }
4895
220c76dd 4896 if (dest != sec)
7c8fe5c4 4897 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4898 }
3e04d765
AM
4899 goto rel24;
4900
4901 case R_PPC64_PLTCALL:
5663e321 4902 case R_PPC64_PLTCALL_NOTOC:
3e04d765 4903 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
4904 /* Fall through. */
4905
5d1634d7 4906 case R_PPC64_REL24:
05d0e962 4907 case R_PPC64_REL24_NOTOC:
3e04d765 4908 rel24:
cbf95972
AM
4909 plt_list = ifunc;
4910 if (h != NULL)
5d1634d7 4911 {
e054468f
AM
4912 h->needs_plt = 1;
4913 if (h->root.root.string[0] == '.'
4914 && h->root.root.string[1] != '\0')
4915 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 4916
3a71aa26 4917 if (h == tga || h == dottga)
cbf95972
AM
4918 {
4919 sec->has_tls_reloc = 1;
4920 if (rel != relocs
4921 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4922 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4923 /* We have a new-style __tls_get_addr call with
4924 a marker reloc. */
4925 ;
4926 else
4927 /* Mark this section as having an old-style call. */
4928 sec->has_tls_get_addr_call = 1;
4929 }
4930 plt_list = &h->plt.plist;
411e1bfb 4931 }
cbf95972
AM
4932
4933 /* We may need a .plt entry if the function this reloc
4934 refers to is in a shared lib. */
4935 if (plt_list
133a1f60 4936 && !update_plt_info (abfd, plt_list, rel->r_addend))
cbf95972 4937 return FALSE;
411e1bfb
AM
4938 break;
4939
cbf95972
AM
4940 case R_PPC64_ADDR14:
4941 case R_PPC64_ADDR14_BRNTAKEN:
4942 case R_PPC64_ADDR14_BRTAKEN:
4943 case R_PPC64_ADDR24:
4944 goto dodyn;
4945
411e1bfb
AM
4946 case R_PPC64_TPREL64:
4947 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 4948 if (bfd_link_dll (info))
411e1bfb
AM
4949 info->flags |= DF_STATIC_TLS;
4950 goto dotlstoc;
4951
4952 case R_PPC64_DTPMOD64:
4953 if (rel + 1 < rel_end
4954 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4955 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 4956 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 4957 else
951fd09b 4958 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
4959 goto dotlstoc;
4960
4961 case R_PPC64_DTPREL64:
4962 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4963 if (rel != relocs
4964 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4965 && rel[-1].r_offset == rel->r_offset - 8)
4966 /* This is the second reloc of a dtpmod, dtprel pair.
4967 Don't mark with TLS_DTPREL. */
4968 goto dodyn;
4969
4970 dotlstoc:
4971 sec->has_tls_reloc = 1;
4972 if (h != NULL)
4973 {
4974 struct ppc_link_hash_entry *eh;
4975 eh = (struct ppc_link_hash_entry *) h;
46e9995a 4976 eh->tls_mask |= tls_type & 0xff;
411e1bfb
AM
4977 }
4978 else
4979 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4980 rel->r_addend, tls_type))
411e1bfb
AM
4981 return FALSE;
4982
7c8fe5c4
AM
4983 ppc64_sec = ppc64_elf_section_data (sec);
4984 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 4985 {
3a71aa26
AM
4986 bfd_size_type amt;
4987
e7b938ca 4988 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
4989 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4990 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4991 if (ppc64_sec->u.toc.symndx == NULL)
4992 return FALSE;
4993 amt = sec->size * sizeof (bfd_vma) / 8;
4994 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4995 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 4996 return FALSE;
7c8fe5c4
AM
4997 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4998 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
4999 }
5000 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26 5001 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
133a1f60 5002 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5003
5004 /* Mark the second slot of a GD or LD entry.
5005 -1 to indicate GD and -2 to indicate LD. */
5006 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5007 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5008 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5009 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5010 goto dodyn;
5011
5012 case R_PPC64_TPREL16:
5013 case R_PPC64_TPREL16_LO:
5014 case R_PPC64_TPREL16_HI:
5015 case R_PPC64_TPREL16_HA:
5016 case R_PPC64_TPREL16_DS:
5017 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5018 case R_PPC64_TPREL16_HIGH:
5019 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5020 case R_PPC64_TPREL16_HIGHER:
5021 case R_PPC64_TPREL16_HIGHERA:
5022 case R_PPC64_TPREL16_HIGHEST:
5023 case R_PPC64_TPREL16_HIGHESTA:
c213164a 5024 case R_PPC64_TPREL34:
7c8bbca5
AM
5025 if (bfd_link_dll (info))
5026 info->flags |= DF_STATIC_TLS;
5027 goto dodyn;
5d1634d7 5028
e86ce104 5029 case R_PPC64_ADDR64:
b9399fcf 5030 if (is_opd
1e2f5b6e 5031 && rel + 1 < rel_end
4ce794b7 5032 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5033 {
8387904d 5034 if (h != NULL)
8c5b4e52 5035 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 5036 }
e86ce104
AM
5037 /* Fall through. */
5038
65f38f15
AM
5039 case R_PPC64_ADDR16:
5040 case R_PPC64_ADDR16_DS:
5041 case R_PPC64_ADDR16_HA:
5042 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5043 case R_PPC64_ADDR16_HIGH:
5044 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5045 case R_PPC64_ADDR16_HIGHER:
5046 case R_PPC64_ADDR16_HIGHERA:
5047 case R_PPC64_ADDR16_HIGHEST:
5048 case R_PPC64_ADDR16_HIGHESTA:
5049 case R_PPC64_ADDR16_LO:
5050 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
5051 case R_PPC64_D34:
5052 case R_PPC64_D34_LO:
5053 case R_PPC64_D34_HI30:
5054 case R_PPC64_D34_HA30:
5055 case R_PPC64_ADDR16_HIGHER34:
5056 case R_PPC64_ADDR16_HIGHERA34:
5057 case R_PPC64_ADDR16_HIGHEST34:
5058 case R_PPC64_ADDR16_HIGHESTA34:
5059 case R_PPC64_D28:
0e1862bb 5060 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5061 && rel->r_addend == 0)
5062 {
5063 /* We may need a .plt entry if this reloc refers to a
5064 function in a shared lib. */
5663e321 5065 if (!update_plt_info (abfd, &h->plt.plist, 0))
a345bc8d
AM
5066 return FALSE;
5067 h->pointer_equality_needed = 1;
5068 }
5069 /* Fall through. */
5070
5071 case R_PPC64_REL30:
5072 case R_PPC64_REL32:
5073 case R_PPC64_REL64:
65f38f15 5074 case R_PPC64_ADDR32:
65f38f15
AM
5075 case R_PPC64_UADDR16:
5076 case R_PPC64_UADDR32:
5077 case R_PPC64_UADDR64:
5bd4f169 5078 case R_PPC64_TOC:
ec73ddcd 5079 if (h != NULL && bfd_link_executable (info))
81848ca0 5080 /* We may need a copy reloc. */
f5385ebf 5081 h->non_got_ref = 1;
81848ca0 5082
41bd81ab 5083 /* Don't propagate .opd relocs. */
b9399fcf 5084 if (NO_OPD_RELOCS && is_opd)
e86ce104 5085 break;
e86ce104 5086
65f38f15
AM
5087 /* If we are creating a shared library, and this is a reloc
5088 against a global symbol, or a non PC relative reloc
5089 against a local symbol, then we need to copy the reloc
5090 into the shared library. However, if we are linking with
5091 -Bsymbolic, we do not need to copy a reloc against a
5092 global symbol which is defined in an object we are
5093 including in the link (i.e., DEF_REGULAR is set). At
5094 this point we have not seen all the input files, so it is
5095 possible that DEF_REGULAR is not set now but will be set
5096 later (it is never cleared). In case of a weak definition,
5097 DEF_REGULAR may be cleared later by a strong definition in
5098 a shared library. We account for that possibility below by
f4656909 5099 storing information in the dyn_relocs field of the hash
65f38f15
AM
5100 table entry. A similar situation occurs when creating
5101 shared libraries and symbol visibility changes render the
5102 symbol local.
5103
5104 If on the other hand, we are creating an executable, we
5105 may need to keep relocations for symbols satisfied by a
5106 dynamic library if we manage to avoid copy relocs for the
5107 symbol. */
411e1bfb 5108 dodyn:
ec73ddcd
AM
5109 if ((h != NULL
5110 && (h->root.type == bfd_link_hash_defweak
5111 || !h->def_regular))
5112 || (h != NULL
5113 && !bfd_link_executable (info)
5114 && !SYMBOLIC_BIND (info, h))
5115 || (bfd_link_pic (info)
5116 && must_be_dyn_reloc (info, r_type))
0e1862bb 5117 || (!bfd_link_pic (info)
25f23106 5118 && ifunc != NULL))
5bd4f169 5119 {
65f38f15
AM
5120 /* We must copy these reloc types into the output file.
5121 Create a reloc section in dynobj and make room for
5122 this reloc. */
5bd4f169
AM
5123 if (sreloc == NULL)
5124 {
83bac4b0
NC
5125 sreloc = _bfd_elf_make_dynamic_reloc_section
5126 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5127
5bd4f169 5128 if (sreloc == NULL)
83bac4b0 5129 return FALSE;
5bd4f169
AM
5130 }
5131
65f38f15
AM
5132 /* If this is a global symbol, we count the number of
5133 relocations we need for this symbol. */
5134 if (h != NULL)
5135 {
19e08130
AM
5136 struct elf_dyn_relocs *p;
5137 struct elf_dyn_relocs **head;
5138
ec338859 5139 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5140 p = *head;
5141 if (p == NULL || p->sec != sec)
5142 {
5143 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5144 if (p == NULL)
5145 return FALSE;
5146 p->next = *head;
5147 *head = p;
5148 p->sec = sec;
5149 p->count = 0;
5150 p->pc_count = 0;
5151 }
5152 p->count += 1;
5153 if (!must_be_dyn_reloc (info, r_type))
5154 p->pc_count += 1;
65f38f15
AM
5155 }
5156 else
5157 {
ec338859
AM
5158 /* Track dynamic relocs needed for local syms too.
5159 We really need local syms available to do this
5160 easily. Oh well. */
19e08130
AM
5161 struct ppc_dyn_relocs *p;
5162 struct ppc_dyn_relocs **head;
5163 bfd_boolean is_ifunc;
ec338859 5164 asection *s;
6edfbbad 5165 void *vpp;
87d72d41 5166 Elf_Internal_Sym *isym;
6edfbbad 5167
87d72d41
AM
5168 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5169 abfd, r_symndx);
5170 if (isym == NULL)
b34976b6 5171 return FALSE;
ec338859 5172
87d72d41
AM
5173 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5174 if (s == NULL)
5175 s = sec;
5176
6edfbbad 5177 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5178 head = (struct ppc_dyn_relocs **) vpp;
5179 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5180 p = *head;
5181 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5182 p = p->next;
5183 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5184 {
5185 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5186 if (p == NULL)
5187 return FALSE;
5188 p->next = *head;
5189 *head = p;
5190 p->sec = sec;
5191 p->ifunc = is_ifunc;
5192 p->count = 0;
5193 }
5194 p->count += 1;
ec338859 5195 }
65f38f15 5196 }
5bd4f169 5197 break;
65f38f15
AM
5198
5199 default:
96e0dda4 5200 break;
5bd4f169
AM
5201 }
5202 }
5203
b34976b6 5204 return TRUE;
5bd4f169
AM
5205}
5206
ee67d69a
AM
5207/* Merge backend specific data from an object file to the output
5208 object file when linking. */
5209
5210static bfd_boolean
50e03d47 5211ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5212{
50e03d47 5213 bfd *obfd = info->output_bfd;
ee67d69a
AM
5214 unsigned long iflags, oflags;
5215
5216 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5217 return TRUE;
5218
5219 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5220 return TRUE;
5221
50e03d47 5222 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
5223 return FALSE;
5224
5225 iflags = elf_elfheader (ibfd)->e_flags;
5226 oflags = elf_elfheader (obfd)->e_flags;
5227
f6c7c3e8 5228 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5229 {
4eca0228 5230 _bfd_error_handler
695344c0 5231 /* xgettext:c-format */
871b3ab2 5232 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
5233 bfd_set_error (bfd_error_bad_value);
5234 return FALSE;
5235 }
f6c7c3e8 5236 else if (iflags != oflags && iflags != 0)
ee67d69a 5237 {
4eca0228 5238 _bfd_error_handler
695344c0 5239 /* xgettext:c-format */
871b3ab2 5240 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5241 ibfd, iflags, oflags);
5242 bfd_set_error (bfd_error_bad_value);
5243 return FALSE;
5244 }
5245
4a91d0ba
AM
5246 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5247 return FALSE;
005d79fd 5248
ee67d69a 5249 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5250 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5251}
5252
5253static bfd_boolean
5254ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5255{
5256 /* Print normal ELF private data. */
5257 _bfd_elf_print_private_bfd_data (abfd, ptr);
5258
5259 if (elf_elfheader (abfd)->e_flags != 0)
5260 {
5261 FILE *file = ptr;
5262
ee67d69a
AM
5263 fprintf (file, _("private flags = 0x%lx:"),
5264 elf_elfheader (abfd)->e_flags);
5265
5266 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5267 fprintf (file, _(" [abiv%ld]"),
5268 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5269 fputc ('\n', file);
5270 }
5271
5272 return TRUE;
5273}
5274
8387904d 5275/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5276 of the code entry point, and its section, which must be in the same
5277 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5278
5279static bfd_vma
5280opd_entry_value (asection *opd_sec,
5281 bfd_vma offset,
5282 asection **code_sec,
aef36ac1
AM
5283 bfd_vma *code_off,
5284 bfd_boolean in_code_sec)
8387904d
AM
5285{
5286 bfd *opd_bfd = opd_sec->owner;
8860955f 5287 Elf_Internal_Rela *relocs;
8387904d 5288 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5289 bfd_vma val;
8387904d 5290
9f296da3
AM
5291 /* No relocs implies we are linking a --just-symbols object, or looking
5292 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5293 if (opd_sec->reloc_count == 0)
5294 {
729eabd5 5295 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5296
729eabd5
AM
5297 if (contents == NULL)
5298 {
5299 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5300 return (bfd_vma) -1;
5301 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5302 }
ee1e4ede 5303
dbb3fbbb 5304 /* PR 17512: file: 64b9dfbb. */
451dfd38 5305 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5306 return (bfd_vma) -1;
5307
729eabd5 5308 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5309 if (code_sec != NULL)
5310 {
5311 asection *sec, *likely = NULL;
ee1e4ede 5312
aef36ac1 5313 if (in_code_sec)
4b85d634 5314 {
aef36ac1
AM
5315 sec = *code_sec;
5316 if (sec->vma <= val
5317 && val < sec->vma + sec->size)
5318 likely = sec;
5319 else
5320 val = -1;
5321 }
5322 else
5323 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5324 if (sec->vma <= val
5325 && (sec->flags & SEC_LOAD) != 0
5326 && (sec->flags & SEC_ALLOC) != 0)
5327 likely = sec;
5328 if (likely != NULL)
5329 {
5330 *code_sec = likely;
5331 if (code_off != NULL)
5332 *code_off = val - likely->vma;
4b85d634
AM
5333 }
5334 }
aef36ac1 5335 return val;
4b85d634
AM
5336 }
5337
0c8d6e5c 5338 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5339
729eabd5 5340 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5341 if (relocs == NULL)
5342 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
5343 /* PR 17512: file: df8e1fd6. */
5344 if (relocs == NULL)
5345 return (bfd_vma) -1;
645ea6a9 5346
8387904d 5347 /* Go find the opd reloc at the sym address. */
8860955f 5348 lo = relocs;
8387904d 5349 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5350 val = (bfd_vma) -1;
8387904d
AM
5351 while (lo < hi)
5352 {
5353 look = lo + (hi - lo) / 2;
5354 if (look->r_offset < offset)
5355 lo = look + 1;
5356 else if (look->r_offset > offset)
5357 hi = look;
5358 else
5359 {
0ffa91dd
NC
5360 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5361
8387904d
AM
5362 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5363 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5364 {
5365 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5366 asection *sec = NULL;
8387904d 5367
b53dfeb2
AM
5368 if (symndx >= symtab_hdr->sh_info
5369 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5370 {
5371 struct elf_link_hash_entry **sym_hashes;
5372 struct elf_link_hash_entry *rh;
5373
5374 sym_hashes = elf_sym_hashes (opd_bfd);
5375 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5376 if (rh != NULL)
5377 {
5378 rh = elf_follow_link (rh);
bb854a36
AM
5379 if (rh->root.type != bfd_link_hash_defined
5380 && rh->root.type != bfd_link_hash_defweak)
5381 break;
5382 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5383 {
bb854a36
AM
5384 val = rh->root.u.def.value;
5385 sec = rh->root.u.def.section;
b53dfeb2
AM
5386 }
5387 }
5388 }
5389
5390 if (sec == NULL)
5391 {
5392 Elf_Internal_Sym *sym;
5393
5394 if (symndx < symtab_hdr->sh_info)
5395 {
5396 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5397 if (sym == NULL)
5398 {
5399 size_t symcnt = symtab_hdr->sh_info;
5400 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5401 symcnt, 0,
5402 NULL, NULL, NULL);
5403 if (sym == NULL)
5404 break;
5405 symtab_hdr->contents = (bfd_byte *) sym;
5406 }
5407 sym += symndx;
128205bb
AM
5408 }
5409 else
5410 {
b53dfeb2
AM
5411 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5412 1, symndx,
5413 NULL, NULL, NULL);
128205bb
AM
5414 if (sym == NULL)
5415 break;
128205bb 5416 }
b53dfeb2
AM
5417 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5418 if (sec == NULL)
5419 break;
5420 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5421 val = sym->st_value;
8387904d 5422 }
b53dfeb2 5423
8387904d
AM
5424 val += look->r_addend;
5425 if (code_off != NULL)
5426 *code_off = val;
5427 if (code_sec != NULL)
aef36ac1
AM
5428 {
5429 if (in_code_sec && *code_sec != sec)
5430 return -1;
5431 else
5432 *code_sec = sec;
5433 }
b53dfeb2 5434 if (sec->output_section != NULL)
8387904d 5435 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5436 }
5437 break;
5438 }
5439 }
645ea6a9 5440
645ea6a9 5441 return val;
8387904d
AM
5442}
5443
aef36ac1
AM
5444/* If the ELF symbol SYM might be a function in SEC, return the
5445 function size and set *CODE_OFF to the function's entry point,
5446 otherwise return zero. */
9f296da3 5447
aef36ac1
AM
5448static bfd_size_type
5449ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5450 bfd_vma *code_off)
9f296da3 5451{
aef36ac1
AM
5452 bfd_size_type size;
5453
5454 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5455 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5456 return 0;
5457
5458 size = 0;
5459 if (!(sym->flags & BSF_SYNTHETIC))
5460 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5461
5462 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5463 {
b07bca4e
AM
5464 struct _opd_sec_data *opd = get_opd_info (sym->section);
5465 bfd_vma symval = sym->value;
5466
5467 if (opd != NULL
5468 && opd->adjust != NULL
5469 && elf_section_data (sym->section)->relocs != NULL)
5470 {
5471 /* opd_entry_value will use cached relocs that have been
5472 adjusted, but with raw symbols. That means both local
5473 and global symbols need adjusting. */
5474 long adjust = opd->adjust[OPD_NDX (symval)];
5475 if (adjust == -1)
5476 return 0;
5477 symval += adjust;
5478 }
5479
5480 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
5481 &sec, code_off, TRUE) == (bfd_vma) -1)
5482 return 0;
5483 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5484 symbol. This size has nothing to do with the code size of the
5485 function, which is what we're supposed to return, but the
5486 code size isn't available without looking up the dot-sym.
5487 However, doing that would be a waste of time particularly
5488 since elf_find_function will look at the dot-sym anyway.
5489 Now, elf_find_function will keep the largest size of any
5490 function sym found at the code address of interest, so return
5491 1 here to avoid it incorrectly caching a larger function size
5492 for a small function. This does mean we return the wrong
5493 size for a new-ABI function of size 24, but all that does is
5494 disable caching for such functions. */
5495 if (size == 24)
5496 size = 1;
9f296da3 5497 }
aef36ac1
AM
5498 else
5499 {
5500 if (sym->section != sec)
5501 return 0;
5502 *code_off = sym->value;
5503 }
5504 if (size == 0)
5505 size = 1;
5506 return size;
9f296da3
AM
5507}
5508
f378ab09
AM
5509/* Return true if symbol is a strong function defined in an ELFv2
5510 object with st_other localentry bits of zero, ie. its local entry
5511 point coincides with its global entry point. */
5512
5513static bfd_boolean
5514is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5515{
5516 return (h != NULL
5517 && h->type == STT_FUNC
5518 && h->root.type == bfd_link_hash_defined
5519 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 5520 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
5521 && is_ppc64_elf (h->root.u.def.section->owner)
5522 && abiversion (h->root.u.def.section->owner) >= 2);
5523}
5524
854b41e7
AM
5525/* Return true if symbol is defined in a regular object file. */
5526
5527static bfd_boolean
5528is_static_defined (struct elf_link_hash_entry *h)
5529{
5530 return ((h->root.type == bfd_link_hash_defined
5531 || h->root.type == bfd_link_hash_defweak)
5532 && h->root.u.def.section != NULL
5533 && h->root.u.def.section->output_section != NULL);
5534}
5535
b31867b6
AM
5536/* If FDH is a function descriptor symbol, return the associated code
5537 entry symbol if it is defined. Return NULL otherwise. */
5538
5539static struct ppc_link_hash_entry *
5540defined_code_entry (struct ppc_link_hash_entry *fdh)
5541{
5542 if (fdh->is_func_descriptor)
5543 {
5544 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5545 if (fh->elf.root.type == bfd_link_hash_defined
5546 || fh->elf.root.type == bfd_link_hash_defweak)
5547 return fh;
5548 }
5549 return NULL;
5550}
5551
5552/* If FH is a function code entry symbol, return the associated
5553 function descriptor symbol if it is defined. Return NULL otherwise. */
5554
5555static struct ppc_link_hash_entry *
5556defined_func_desc (struct ppc_link_hash_entry *fh)
5557{
5558 if (fh->oh != NULL
5559 && fh->oh->is_func_descriptor)
5560 {
5561 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5562 if (fdh->elf.root.type == bfd_link_hash_defined
5563 || fdh->elf.root.type == bfd_link_hash_defweak)
5564 return fdh;
5565 }
5566 return NULL;
5567}
5568
8c5b4e52
AM
5569static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5570
5571/* Garbage collect sections, after first dealing with dot-symbols. */
5572
5573static bfd_boolean
5574ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5575{
5576 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5577
5578 if (htab != NULL && htab->need_func_desc_adj)
5579 {
5580 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5581 htab->need_func_desc_adj = 0;
5582 }
5583 return bfd_elf_gc_sections (abfd, info);
5584}
5585
74f0fb50
AM
5586/* Mark all our entry sym sections, both opd and code section. */
5587
5588static void
5589ppc64_elf_gc_keep (struct bfd_link_info *info)
5590{
5591 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5592 struct bfd_sym_chain *sym;
5593
4dfe6ac6
NC
5594 if (htab == NULL)
5595 return;
5596
74f0fb50
AM
5597 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5598 {
b31867b6 5599 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5600 asection *sec;
5601
5602 eh = (struct ppc_link_hash_entry *)
b31867b6 5603 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5604 if (eh == NULL)
5605 continue;
5606 if (eh->elf.root.type != bfd_link_hash_defined
5607 && eh->elf.root.type != bfd_link_hash_defweak)
5608 continue;
5609
b31867b6
AM
5610 fh = defined_code_entry (eh);
5611 if (fh != NULL)
74f0fb50 5612 {
b31867b6 5613 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5614 sec->flags |= SEC_KEEP;
5615 }
5616 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5617 && opd_entry_value (eh->elf.root.u.def.section,
5618 eh->elf.root.u.def.value,
aef36ac1 5619 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
5620 sec->flags |= SEC_KEEP;
5621
5622 sec = eh->elf.root.u.def.section;
5623 sec->flags |= SEC_KEEP;
5624 }
5625}
5626
64d03ab5
AM
5627/* Mark sections containing dynamically referenced symbols. When
5628 building shared libraries, we must assume that any visible symbol is
5629 referenced. */
5630
5631static bfd_boolean
5632ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5633{
5634 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5635 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5636 struct ppc_link_hash_entry *fdh;
b407645f 5637 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5638
64d03ab5 5639 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5640 fdh = defined_func_desc (eh);
5641 if (fdh != NULL)
5642 eh = fdh;
64d03ab5
AM
5643
5644 if ((eh->elf.root.type == bfd_link_hash_defined
5645 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 5646 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5647 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5648 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5649 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5650 && (!bfd_link_executable (info)
e278ae05 5651 || info->gc_keep_exported
b407645f
AM
5652 || info->export_dynamic
5653 || (eh->elf.dynamic
5654 && d != NULL
2cdcc330
AM
5655 && (*d->match) (&d->head, NULL,
5656 eh->elf.root.root.string)))
e278ae05 5657 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5658 || !bfd_hide_sym_by_version (info->version_info,
5659 eh->elf.root.root.string)))))
64d03ab5
AM
5660 {
5661 asection *code_sec;
b31867b6 5662 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5663
5664 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5665
5666 /* Function descriptor syms cause the associated
5667 function code sym section to be marked. */
b31867b6
AM
5668 fh = defined_code_entry (eh);
5669 if (fh != NULL)
5670 {
5671 code_sec = fh->elf.root.u.def.section;
5672 code_sec->flags |= SEC_KEEP;
5673 }
64d03ab5
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 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
5678 code_sec->flags |= SEC_KEEP;
5679 }
5680
5681 return TRUE;
5682}
5683
5bd4f169
AM
5684/* Return the section that should be marked against GC for a given
5685 relocation. */
5686
5687static asection *
4ce794b7 5688ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5689 struct bfd_link_info *info,
4ce794b7
AM
5690 Elf_Internal_Rela *rel,
5691 struct elf_link_hash_entry *h,
5692 Elf_Internal_Sym *sym)
5bd4f169 5693{
ccfa59ea
AM
5694 asection *rsec;
5695
ccfa59ea
AM
5696 /* Syms return NULL if we're marking .opd, so we avoid marking all
5697 function sections, as all functions are referenced in .opd. */
5698 rsec = NULL;
5699 if (get_opd_info (sec) != NULL)
5700 return rsec;
1e2f5b6e 5701
5bd4f169
AM
5702 if (h != NULL)
5703 {
04c9666a 5704 enum elf_ppc64_reloc_type r_type;
b31867b6 5705 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5706
4ce794b7 5707 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5708 switch (r_type)
5bd4f169
AM
5709 {
5710 case R_PPC64_GNU_VTINHERIT:
5711 case R_PPC64_GNU_VTENTRY:
5712 break;
5713
5714 default:
5715 switch (h->root.type)
5716 {
5717 case bfd_link_hash_defined:
5718 case bfd_link_hash_defweak:
ccfa59ea 5719 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5720 fdh = defined_func_desc (eh);
5721 if (fdh != NULL)
8c5b4e52
AM
5722 {
5723 /* -mcall-aixdesc code references the dot-symbol on
5724 a call reloc. Mark the function descriptor too
5725 against garbage collection. */
5726 fdh->elf.mark = 1;
60d67dc8
AM
5727 if (fdh->elf.is_weakalias)
5728 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
5729 eh = fdh;
5730 }
1e2f5b6e
AM
5731
5732 /* Function descriptor syms cause the associated
5733 function code sym section to be marked. */
b31867b6
AM
5734 fh = defined_code_entry (eh);
5735 if (fh != NULL)
ccfa59ea
AM
5736 {
5737 /* They also mark their opd section. */
74f0fb50 5738 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5739
b31867b6 5740 rsec = fh->elf.root.u.def.section;
ccfa59ea 5741 }
8387904d
AM
5742 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5743 && opd_entry_value (eh->elf.root.u.def.section,
5744 eh->elf.root.u.def.value,
aef36ac1 5745 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 5746 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5747 else
1e2f5b6e
AM
5748 rsec = h->root.u.def.section;
5749 break;
5bd4f169
AM
5750
5751 case bfd_link_hash_common:
1e2f5b6e
AM
5752 rsec = h->root.u.c.p->section;
5753 break;
5bd4f169
AM
5754
5755 default:
fb34365b 5756 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5757 }
5758 }
5759 }
5760 else
5761 {
74f0fb50 5762 struct _opd_sec_data *opd;
1e2f5b6e
AM
5763
5764 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5765 opd = get_opd_info (rsec);
5766 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5767 {
74f0fb50 5768 rsec->gc_mark = 1;
ccfa59ea 5769
51aecdc5 5770 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 5771 }
5bd4f169
AM
5772 }
5773
1e2f5b6e 5774 return rsec;
5bd4f169
AM
5775}
5776
deb0e272
AM
5777/* The maximum size of .sfpr. */
5778#define SFPR_MAX (218*4)
5779
5780struct sfpr_def_parms
5781{
699733f6
AM
5782 const char name[12];
5783 unsigned char lo, hi;
2cdcc330
AM
5784 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5785 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
5786};
5787
a4b6fadd
AM
5788/* Auto-generate _save*, _rest* functions in .sfpr.
5789 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5790 instead. */
deb0e272 5791
4dfe6ac6 5792static bfd_boolean
a4b6fadd
AM
5793sfpr_define (struct bfd_link_info *info,
5794 const struct sfpr_def_parms *parm,
5795 asection *stub_sec)
deb0e272
AM
5796{
5797 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5798 unsigned int i;
5799 size_t len = strlen (parm->name);
5800 bfd_boolean writing = FALSE;
699733f6 5801 char sym[16];
deb0e272 5802
4dfe6ac6
NC
5803 if (htab == NULL)
5804 return FALSE;
5805
deb0e272
AM
5806 memcpy (sym, parm->name, len);
5807 sym[len + 2] = 0;
5808
5809 for (i = parm->lo; i <= parm->hi; i++)
5810 {
a4b6fadd 5811 struct ppc_link_hash_entry *h;
deb0e272
AM
5812
5813 sym[len + 0] = i / 10 + '0';
5814 sym[len + 1] = i % 10 + '0';
a4b6fadd 5815 h = (struct ppc_link_hash_entry *)
b32547cd 5816 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 5817 if (stub_sec != NULL)
deb0e272 5818 {
a4b6fadd
AM
5819 if (h != NULL
5820 && h->elf.root.type == bfd_link_hash_defined
5821 && h->elf.root.u.def.section == htab->sfpr)
5822 {
5823 struct elf_link_hash_entry *s;
5824 char buf[32];
5825 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5826 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5827 if (s == NULL)
5828 return FALSE;
5829 if (s->root.type == bfd_link_hash_new
5830 || (s->root.type = bfd_link_hash_defined
5831 && s->root.u.def.section == stub_sec))
5832 {
5833 s->root.type = bfd_link_hash_defined;
5834 s->root.u.def.section = stub_sec;
7dda8d3c 5835 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
5836 + h->elf.root.u.def.value);
5837 s->ref_regular = 1;
5838 s->def_regular = 1;
5839 s->ref_regular_nonweak = 1;
5840 s->forced_local = 1;
5841 s->non_elf = 0;
5842 s->root.linker_def = 1;
5843 }
5844 }
5845 continue;
5846 }
5847 if (h != NULL)
5848 {
5849 h->save_res = 1;
5850 if (!h->elf.def_regular)
deb0e272 5851 {
a4b6fadd
AM
5852 h->elf.root.type = bfd_link_hash_defined;
5853 h->elf.root.u.def.section = htab->sfpr;
5854 h->elf.root.u.def.value = htab->sfpr->size;
5855 h->elf.type = STT_FUNC;
5856 h->elf.def_regular = 1;
b32547cd 5857 h->elf.non_elf = 0;
a4b6fadd
AM
5858 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5859 writing = TRUE;
deb0e272 5860 if (htab->sfpr->contents == NULL)
a4b6fadd 5861 {
2cdcc330
AM
5862 htab->sfpr->contents
5863 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd
AM
5864 if (htab->sfpr->contents == NULL)
5865 return FALSE;
5866 }
deb0e272
AM
5867 }
5868 }
5869 if (writing)
5870 {
5871 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5872 if (i != parm->hi)
5873 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5874 else
5875 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5876 htab->sfpr->size = p - htab->sfpr->contents;
5877 }
5878 }
5879
5880 return TRUE;
5881}
5882
5883static bfd_byte *
5884savegpr0 (bfd *abfd, bfd_byte *p, int r)
5885{
5886 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5887 return p + 4;
5888}
5889
5890static bfd_byte *
5891savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5892{
5893 p = savegpr0 (abfd, p, r);
a078d95a 5894 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5895 p = p + 4;
5896 bfd_put_32 (abfd, BLR, p);
5897 return p + 4;
5898}
5899
5900static bfd_byte *
5901restgpr0 (bfd *abfd, bfd_byte *p, int r)
5902{
5903 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5904 return p + 4;
5905}
5906
5907static bfd_byte *
5908restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5909{
a078d95a 5910 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5911 p = p + 4;
5912 p = restgpr0 (abfd, p, r);
5913 bfd_put_32 (abfd, MTLR_R0, p);
5914 p = p + 4;
5915 if (r == 29)
5916 {
5917 p = restgpr0 (abfd, p, 30);
5918 p = restgpr0 (abfd, p, 31);
5919 }
5920 bfd_put_32 (abfd, BLR, p);
5921 return p + 4;
5922}
5923
5924static bfd_byte *
5925savegpr1 (bfd *abfd, bfd_byte *p, int r)
5926{
5927 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5928 return p + 4;
5929}
5930
5931static bfd_byte *
5932savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5933{
5934 p = savegpr1 (abfd, p, r);
5935 bfd_put_32 (abfd, BLR, p);
5936 return p + 4;
5937}
5938
5939static bfd_byte *
5940restgpr1 (bfd *abfd, bfd_byte *p, int r)
5941{
5942 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5943 return p + 4;
5944}
5945
5946static bfd_byte *
5947restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5948{
5949 p = restgpr1 (abfd, p, r);
5950 bfd_put_32 (abfd, BLR, p);
5951 return p + 4;
5952}
5953
5954static bfd_byte *
5955savefpr (bfd *abfd, bfd_byte *p, int r)
5956{
5957 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5958 return p + 4;
5959}
5960
5961static bfd_byte *
5962savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5963{
5964 p = savefpr (abfd, p, r);
a078d95a 5965 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5966 p = p + 4;
5967 bfd_put_32 (abfd, BLR, p);
5968 return p + 4;
5969}
5970
5971static bfd_byte *
5972restfpr (bfd *abfd, bfd_byte *p, int r)
5973{
5974 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5975 return p + 4;
5976}
5977
5978static bfd_byte *
5979restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5980{
a078d95a 5981 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5982 p = p + 4;
5983 p = restfpr (abfd, p, r);
5984 bfd_put_32 (abfd, MTLR_R0, p);
5985 p = p + 4;
5986 if (r == 29)
5987 {
5988 p = restfpr (abfd, p, 30);
5989 p = restfpr (abfd, p, 31);
5990 }
5991 bfd_put_32 (abfd, BLR, p);
5992 return p + 4;
5993}
5994
5995static bfd_byte *
5996savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5997{
5998 p = savefpr (abfd, p, r);
5999 bfd_put_32 (abfd, BLR, p);
6000 return p + 4;
6001}
6002
6003static bfd_byte *
6004restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6005{
6006 p = restfpr (abfd, p, r);
6007 bfd_put_32 (abfd, BLR, p);
6008 return p + 4;
6009}
6010
6011static bfd_byte *
6012savevr (bfd *abfd, bfd_byte *p, int r)
6013{
6014 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6015 p = p + 4;
6016 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6017 return p + 4;
6018}
6019
6020static bfd_byte *
6021savevr_tail (bfd *abfd, bfd_byte *p, int r)
6022{
6023 p = savevr (abfd, p, r);
6024 bfd_put_32 (abfd, BLR, p);
6025 return p + 4;
6026}
6027
6028static bfd_byte *
6029restvr (bfd *abfd, bfd_byte *p, int r)
6030{
6031 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6032 p = p + 4;
6033 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6034 return p + 4;
6035}
6036
6037static bfd_byte *
6038restvr_tail (bfd *abfd, bfd_byte *p, int r)
6039{
6040 p = restvr (abfd, p, r);
6041 bfd_put_32 (abfd, BLR, p);
6042 return p + 4;
6043}
6044
e86ce104
AM
6045/* Called via elf_link_hash_traverse to transfer dynamic linking
6046 information on function code symbol entries to their corresponding
6047 function descriptor symbol entries. */
deb0e272 6048
b34976b6 6049static bfd_boolean
4ce794b7 6050func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6051{
e86ce104 6052 struct bfd_link_info *info;
65f38f15 6053 struct ppc_link_hash_table *htab;
50bc7936
AM
6054 struct ppc_link_hash_entry *fh;
6055 struct ppc_link_hash_entry *fdh;
6056 bfd_boolean force_local;
5bd4f169 6057
50bc7936
AM
6058 fh = (struct ppc_link_hash_entry *) h;
6059 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6060 return TRUE;
e86ce104 6061
8c5b4e52
AM
6062 if (!fh->is_func)
6063 return TRUE;
6064
6065 if (fh->elf.root.root.string[0] != '.'
6066 || fh->elf.root.root.string[1] == '\0')
6067 return TRUE;
6068
4ce794b7 6069 info = inf;
65f38f15 6070 htab = ppc_hash_table (info);
4dfe6ac6
NC
6071 if (htab == NULL)
6072 return FALSE;
5bd4f169 6073
8c5b4e52
AM
6074 /* Find the corresponding function descriptor symbol. */
6075 fdh = lookup_fdh (fh, htab);
6076
c09bdfe5
AM
6077 /* Resolve undefined references to dot-symbols as the value
6078 in the function descriptor, if we have one in a regular object.
6079 This is to satisfy cases like ".quad .foo". Calls to functions
6080 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6081 if ((fh->elf.root.type == bfd_link_hash_undefined
6082 || fh->elf.root.type == bfd_link_hash_undefweak)
6083 && (fdh->elf.root.type == bfd_link_hash_defined
6084 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6085 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6086 && opd_entry_value (fdh->elf.root.u.def.section,
6087 fdh->elf.root.u.def.value,
c09bdfe5 6088 &fh->elf.root.u.def.section,
aef36ac1 6089 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6090 {
b31867b6 6091 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6092 fh->elf.forced_local = 1;
b31867b6
AM
6093 fh->elf.def_regular = fdh->elf.def_regular;
6094 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6095 }
6096
8c5b4e52
AM
6097 if (!fh->elf.dynamic)
6098 {
6099 struct plt_entry *ent;
5bd4f169 6100
8c5b4e52
AM
6101 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6102 if (ent->plt.refcount > 0)
6103 break;
6104 if (ent == NULL)
6105 return TRUE;
6106 }
5bd4f169 6107
8c5b4e52 6108 /* Create a descriptor as undefined if necessary. */
50bc7936 6109 if (fdh == NULL
0e1862bb 6110 && !bfd_link_executable (info)
50bc7936
AM
6111 && (fh->elf.root.type == bfd_link_hash_undefined
6112 || fh->elf.root.type == bfd_link_hash_undefweak))
6113 {
908b32fc 6114 fdh = make_fdh (info, fh);
bb700d78
AM
6115 if (fdh == NULL)
6116 return FALSE;
50bc7936 6117 }
648cca2c 6118
8c5b4e52 6119 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6120 if (fdh != NULL
6121 && fdh->fake
8c5b4e52
AM
6122 && (fh->elf.root.type == bfd_link_hash_defined
6123 || fh->elf.root.type == bfd_link_hash_defweak))
6124 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 6125
8c5b4e52
AM
6126 /* Transfer dynamic linking information to the function descriptor. */
6127 if (fdh != NULL)
6128 {
f5385ebf
AM
6129 fdh->elf.ref_regular |= fh->elf.ref_regular;
6130 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6131 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6132 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6133 fdh->elf.dynamic |= fh->elf.dynamic;
6134 fdh->elf.needs_plt |= (fh->elf.needs_plt
6135 || fh->elf.type == STT_FUNC
6136 || fh->elf.type == STT_GNU_IFUNC);
6137 move_plt_plist (fh, fdh);
6138
6139 if (!fdh->elf.forced_local
6140 && fh->elf.dynindx != -1)
6141 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6142 return FALSE;
e86ce104
AM
6143 }
6144
50bc7936
AM
6145 /* Now that the info is on the function descriptor, clear the
6146 function code sym info. Any function code syms for which we
6147 don't have a definition in a regular file, we force local.
6148 This prevents a shared library from exporting syms that have
6149 been imported from another library. Function code syms that
6150 are really in the library we must leave global to prevent the
6151 linker dragging in a definition from a static library. */
93f3fa99
AM
6152 force_local = (!fh->elf.def_regular
6153 || fdh == NULL
6154 || !fdh->elf.def_regular
6155 || fdh->elf.forced_local);
50bc7936
AM
6156 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6157
b34976b6 6158 return TRUE;
e86ce104 6159}
40b8271b 6160
a4b6fadd
AM
6161static const struct sfpr_def_parms save_res_funcs[] =
6162 {
6163 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6164 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6165 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6166 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6167 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6168 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6169 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6170 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6171 { "._savef", 14, 31, savefpr, savefpr1_tail },
6172 { "._restf", 14, 31, restfpr, restfpr1_tail },
6173 { "_savevr_", 20, 31, savevr, savevr_tail },
6174 { "_restvr_", 20, 31, restvr, restvr_tail }
6175 };
6176
e86ce104 6177/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6178 this hook to a) provide some gcc support functions, and b) transfer
6179 dynamic linking information gathered so far on function code symbol
6180 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6181
b34976b6 6182static bfd_boolean
4ce794b7
AM
6183ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6184 struct bfd_link_info *info)
e86ce104
AM
6185{
6186 struct ppc_link_hash_table *htab;
6187
6188 htab = ppc_hash_table (info);
4dfe6ac6
NC
6189 if (htab == NULL)
6190 return FALSE;
6191
b32547cd
AM
6192 /* Provide any missing _save* and _rest* functions. */
6193 if (htab->sfpr != NULL)
6194 {
6195 unsigned int i;
6196
6197 htab->sfpr->size = 0;
6198 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6199 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6200 return FALSE;
6201 if (htab->sfpr->size == 0)
6202 htab->sfpr->flags |= SEC_EXCLUDE;
6203 }
6204
6205 if (bfd_link_relocatable (info))
6206 return TRUE;
6207
6208 if (htab->elf.hgot != NULL)
dba6fa9b
AM
6209 {
6210 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6211 /* Make .TOC. defined so as to prevent it being made dynamic.
6212 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6213 if (!htab->elf.hgot->def_regular
6214 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6215 {
6216 htab->elf.hgot->root.type = bfd_link_hash_defined;
6217 htab->elf.hgot->root.u.def.value = 0;
6218 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6219 htab->elf.hgot->def_regular = 1;
6220 htab->elf.hgot->root.linker_def = 1;
6221 }
dba6fa9b 6222 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6223 htab->elf.hgot->other
6224 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6225 }
c66bb0ee 6226
8c5b4e52
AM
6227 if (htab->need_func_desc_adj)
6228 {
6229 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6230 htab->need_func_desc_adj = 0;
6231 }
805fc799 6232
b34976b6 6233 return TRUE;
e86ce104
AM
6234}
6235
98bbb1b8 6236/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 6237
98bbb1b8 6238static asection *
a345bc8d
AM
6239readonly_dynrelocs (struct elf_link_hash_entry *h)
6240{
6241 struct ppc_link_hash_entry *eh;
6242 struct elf_dyn_relocs *p;
6243
6244 eh = (struct ppc_link_hash_entry *) h;
6245 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6246 {
6247 asection *s = p->sec->output_section;
6248
6249 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 6250 return p->sec;
a345bc8d 6251 }
98bbb1b8 6252 return NULL;
a345bc8d
AM
6253}
6254
d311bc8b 6255/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6256 aliases, that apply to read-only sections. Cannot be used after
6257 size_dynamic_sections. */
d311bc8b
AM
6258
6259static bfd_boolean
6260alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6261{
6262 struct ppc_link_hash_entry *eh;
6263
6264 eh = (struct ppc_link_hash_entry *) h;
6265 do
6266 {
6267 if (readonly_dynrelocs (&eh->elf))
6268 return TRUE;
ab2477e1 6269 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
2cdcc330
AM
6270 }
6271 while (eh != NULL && &eh->elf != h);
d311bc8b
AM
6272
6273 return FALSE;
6274}
8a2058b5 6275
8a9e8e72
AM
6276/* Return whether EH has pc-relative dynamic relocs. */
6277
6278static bfd_boolean
6279pc_dynrelocs (struct ppc_link_hash_entry *eh)
6280{
6281 struct elf_dyn_relocs *p;
6282
6283 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6284 if (p->pc_count != 0)
6285 return TRUE;
6286 return FALSE;
6287}
6288
8a2058b5
AM
6289/* Return true if a global entry stub will be created for H. Valid
6290 for ELFv2 before plt entries have been allocated. */
6291
6292static bfd_boolean
6293global_entry_stub (struct elf_link_hash_entry *h)
6294{
6295 struct plt_entry *pent;
6296
6297 if (!h->pointer_equality_needed
6298 || h->def_regular)
6299 return FALSE;
6300
6301 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6302 if (pent->plt.refcount > 0
6303 && pent->addend == 0)
6304 return TRUE;
6305
6306 return FALSE;
6307}
6308
e86ce104
AM
6309/* Adjust a symbol defined by a dynamic object and referenced by a
6310 regular object. The current definition is in some section of the
6311 dynamic object, but we're not including those sections. We have to
6312 change the definition to something the rest of the link can
6313 understand. */
6314
b34976b6 6315static bfd_boolean
4ce794b7
AM
6316ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6317 struct elf_link_hash_entry *h)
e86ce104
AM
6318{
6319 struct ppc_link_hash_table *htab;
5474d94f 6320 asection *s, *srel;
e86ce104
AM
6321
6322 htab = ppc_hash_table (info);
4dfe6ac6
NC
6323 if (htab == NULL)
6324 return FALSE;
e86ce104
AM
6325
6326 /* Deal with function syms. */
6327 if (h->type == STT_FUNC
e054468f 6328 || h->type == STT_GNU_IFUNC
f5385ebf 6329 || h->needs_plt)
e86ce104 6330 {
529fe20e
AM
6331 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6332 || SYMBOL_CALLS_LOCAL (info, h)
6333 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6334 /* Discard dyn_relocs when non-pic if we've decided that a
6335 function symbol is local and not an ifunc. We keep dynamic
6336 relocs for ifuncs when local rather than always emitting a
6337 plt call stub for them and defining the symbol on the call
6338 stub. We can't do that for ELFv1 anyway (a function symbol
6339 is defined on a descriptor, not code) and it can be faster at
6340 run-time due to not needing to bounce through a stub. The
6341 dyn_relocs for ifuncs will be applied even in a static
6342 executable. */
6343 if (!bfd_link_pic (info)
6344 && h->type != STT_GNU_IFUNC
6345 && local)
6346 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6347
e86ce104
AM
6348 /* Clear procedure linkage table information for any symbol that
6349 won't need a .plt entry. */
411e1bfb
AM
6350 struct plt_entry *ent;
6351 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6352 if (ent->plt.refcount > 0)
6353 break;
8387904d 6354 if (ent == NULL
2d7ad24e
AM
6355 || (h->type != STT_GNU_IFUNC
6356 && local
3e04d765
AM
6357 && (htab->can_convert_all_inline_plt
6358 || (((struct ppc_link_hash_entry *) h)->tls_mask
6359 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6360 {
411e1bfb 6361 h->plt.plist = NULL;
f5385ebf 6362 h->needs_plt = 0;
d1eca1e4 6363 h->pointer_equality_needed = 0;
40b8271b 6364 }
8a2058b5 6365 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6366 {
d1eca1e4
AM
6367 /* Taking a function's address in a read/write section
6368 doesn't require us to define the function symbol in the
6369 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6370 be used instead. The reason we prefer a few more dynamic
6371 relocs is that calling via a global entry stub costs a
6372 few more instructions, and pointer_equality_needed causes
6373 extra work in ld.so when resolving these symbols. */
529fe20e 6374 if (global_entry_stub (h))
d1eca1e4 6375 {
ab2477e1 6376 if (!readonly_dynrelocs (h))
529fe20e
AM
6377 {
6378 h->pointer_equality_needed = 0;
04383fd1
AM
6379 /* If we haven't seen a branch reloc and the symbol
6380 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6381 if (!h->needs_plt)
6382 h->plt.plist = NULL;
6383 }
6384 else if (!bfd_link_pic (info))
6385 /* We are going to be defining the function symbol on the
6386 plt stub, so no dyn_relocs needed when non-pic. */
6387 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
6388 }
6389
3988aed5
AM
6390 /* ELFv2 function symbols can't have copy relocs. */
6391 return TRUE;
6392 }
6393 else if (!h->needs_plt
ab2477e1 6394 && !readonly_dynrelocs (h))
3988aed5 6395 {
04383fd1
AM
6396 /* If we haven't seen a branch reloc and the symbol isn't an
6397 ifunc then we don't need a plt entry. */
3988aed5
AM
6398 h->plt.plist = NULL;
6399 h->pointer_equality_needed = 0;
a345bc8d
AM
6400 return TRUE;
6401 }
5bd4f169 6402 }
bbd7ec4a 6403 else
411e1bfb 6404 h->plt.plist = NULL;
5bd4f169
AM
6405
6406 /* If this is a weak symbol, and there is a real definition, the
6407 processor independent code will have arranged for us to see the
6408 real definition first, and we can just use the same value. */
60d67dc8 6409 if (h->is_weakalias)
5bd4f169 6410 {
60d67dc8
AM
6411 struct elf_link_hash_entry *def = weakdef (h);
6412 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6413 h->root.u.def.section = def->root.u.def.section;
6414 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6415 if (def->root.u.def.section == htab->elf.sdynbss
6416 || def->root.u.def.section == htab->elf.sdynrelro)
6417 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 6418 return TRUE;
5bd4f169
AM
6419 }
6420
5bd4f169
AM
6421 /* If we are creating a shared library, we must presume that the
6422 only references to the symbol are via the global offset table.
6423 For such cases we need not do anything here; the relocations will
6424 be handled correctly by relocate_section. */
ec73ddcd 6425 if (!bfd_link_executable (info))
b34976b6 6426 return TRUE;
5bd4f169 6427
65f38f15
AM
6428 /* If there are no references to this symbol that do not use the
6429 GOT, we don't need to generate a copy reloc. */
f5385ebf 6430 if (!h->non_got_ref)
b34976b6 6431 return TRUE;
65f38f15 6432
b186458a 6433 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6434 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6435
d93d1c80
AM
6436 /* If -z nocopyreloc was given, don't generate them either. */
6437 || info->nocopyreloc
a127494f 6438
dce2246a 6439 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6440 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1bdd8fac
AM
6441 || (ELIMINATE_COPY_RELOCS
6442 && !h->needs_copy
6443 && !alias_readonly_dynrelocs (h))
65f38f15 6444
d93d1c80
AM
6445 /* Protected variables do not work with .dynbss. The copy in
6446 .dynbss won't be used by the shared library with the protected
6447 definition for the variable. Text relocations are preferable
6448 to an incorrect program. */
6449 || h->protected_def)
529fe20e 6450 return TRUE;
a127494f 6451
5d35169e 6452 if (h->plt.plist != NULL)
97b639ba
AM
6453 {
6454 /* We should never get here, but unfortunately there are versions
6455 of gcc out there that improperly (for this ABI) put initialized
6456 function pointers, vtable refs and suchlike in read-only
6457 sections. Allow them to proceed, but warn that this might
6458 break at runtime. */
25f53a85 6459 info->callbacks->einfo
c1c8c1ef 6460 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6461 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6462 h->root.root.string);
6463 }
5d35169e
AM
6464
6465 /* This is a reference to a symbol defined by a dynamic object which
6466 is not a function. */
6467
5bd4f169
AM
6468 /* We must allocate the symbol in our .dynbss section, which will
6469 become part of the .bss section of the executable. There will be
6470 an entry for this symbol in the .dynsym section. The dynamic
6471 object will contain position independent code, so all references
6472 from the dynamic object to this symbol will go through the global
6473 offset table. The dynamic linker will use the .dynsym entry to
6474 determine the address it must put in the global offset table, so
6475 both the dynamic object and the regular object will refer to the
6476 same memory location for the variable. */
5474d94f
AM
6477 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6478 {
6479 s = htab->elf.sdynrelro;
6480 srel = htab->elf.sreldynrelro;
6481 }
6482 else
6483 {
6484 s = htab->elf.sdynbss;
6485 srel = htab->elf.srelbss;
6486 }
1d7e9d18 6487 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6488 {
4a7e5234
AM
6489 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6490 linker to copy the initial value out of the dynamic object
6491 and into the runtime process image. */
5474d94f 6492 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6493 h->needs_copy = 1;
5bd4f169
AM
6494 }
6495
529fe20e
AM
6496 /* We no longer want dyn_relocs. */
6497 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 6498 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6499}
6500
e86ce104
AM
6501/* If given a function descriptor symbol, hide both the function code
6502 sym and the descriptor. */
6503static void
4ce794b7
AM
6504ppc64_elf_hide_symbol (struct bfd_link_info *info,
6505 struct elf_link_hash_entry *h,
6506 bfd_boolean force_local)
e86ce104 6507{
34814b9f 6508 struct ppc_link_hash_entry *eh;
e86ce104
AM
6509 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6510
87469ba2
AM
6511 if (ppc_hash_table (info) == NULL)
6512 return;
6513
34814b9f
AM
6514 eh = (struct ppc_link_hash_entry *) h;
6515 if (eh->is_func_descriptor)
e86ce104 6516 {
34814b9f 6517 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6518
721956f4 6519 if (fh == NULL)
d1329ca3
AM
6520 {
6521 const char *p, *q;
b8ac2841 6522 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6523 char save;
6524
6525 /* We aren't supposed to use alloca in BFD because on
6526 systems which do not have alloca the version in libiberty
6527 calls xmalloc, which might cause the program to crash
6528 when it runs out of memory. This function doesn't have a
6529 return status, so there's no way to gracefully return an
6530 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6531 accessed; It's either a string in an ELF string table,
6532 or allocated in an objalloc structure. */
d1329ca3 6533
34814b9f 6534 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6535 save = *p;
6536 *(char *) p = '.';
34814b9f 6537 fh = (struct ppc_link_hash_entry *)
b8ac2841 6538 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6539 *(char *) p = save;
6540
6541 /* Unfortunately, if it so happens that the string we were
6542 looking for was allocated immediately before this string,
6543 then we overwrote the string terminator. That's the only
6544 reason the lookup should fail. */
6545 if (fh == NULL)
6546 {
34814b9f
AM
6547 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6548 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6549 --q, --p;
34814b9f
AM
6550 if (q < eh->elf.root.root.string && *p == '.')
6551 fh = (struct ppc_link_hash_entry *)
b8ac2841 6552 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6553 }
6554 if (fh != NULL)
6555 {
34814b9f
AM
6556 eh->oh = fh;
6557 fh->oh = eh;
d1329ca3
AM
6558 }
6559 }
e86ce104 6560 if (fh != NULL)
34814b9f 6561 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6562 }
6563}
6564
411e1bfb 6565static bfd_boolean
8843416a
AM
6566get_sym_h (struct elf_link_hash_entry **hp,
6567 Elf_Internal_Sym **symp,
6568 asection **symsecp,
f961d9dd 6569 unsigned char **tls_maskp,
8843416a
AM
6570 Elf_Internal_Sym **locsymsp,
6571 unsigned long r_symndx,
6572 bfd *ibfd)
411e1bfb 6573{
0ffa91dd 6574 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6575
6576 if (r_symndx >= symtab_hdr->sh_info)
6577 {
6578 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6579 struct elf_link_hash_entry *h;
6580
6581 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6582 h = elf_follow_link (h);
411e1bfb
AM
6583
6584 if (hp != NULL)
6585 *hp = h;
6586
6587 if (symp != NULL)
6588 *symp = NULL;
6589
6590 if (symsecp != NULL)
6591 {
6592 asection *symsec = NULL;
6593 if (h->root.type == bfd_link_hash_defined
6594 || h->root.type == bfd_link_hash_defweak)
6595 symsec = h->root.u.def.section;
6596 *symsecp = symsec;
6597 }
6598
e7b938ca 6599 if (tls_maskp != NULL)
411e1bfb
AM
6600 {
6601 struct ppc_link_hash_entry *eh;
6602
6603 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6604 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6605 }
6606 }
6607 else
6608 {
6609 Elf_Internal_Sym *sym;
6610 Elf_Internal_Sym *locsyms = *locsymsp;
6611
6612 if (locsyms == NULL)
6613 {
6614 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6615 if (locsyms == NULL)
6616 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6617 symtab_hdr->sh_info,
6618 0, NULL, NULL, NULL);
6619 if (locsyms == NULL)
6620 return FALSE;
6621 *locsymsp = locsyms;
6622 }
6623 sym = locsyms + r_symndx;
6624
6625 if (hp != NULL)
6626 *hp = NULL;
6627
6628 if (symp != NULL)
6629 *symp = sym;
6630
6631 if (symsecp != NULL)
cb33740c 6632 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6633
e7b938ca 6634 if (tls_maskp != NULL)
411e1bfb
AM
6635 {
6636 struct got_entry **lgot_ents;
f961d9dd 6637 unsigned char *tls_mask;
411e1bfb 6638
e7b938ca 6639 tls_mask = NULL;
411e1bfb
AM
6640 lgot_ents = elf_local_got_ents (ibfd);
6641 if (lgot_ents != NULL)
6642 {
e054468f
AM
6643 struct plt_entry **local_plt = (struct plt_entry **)
6644 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6645 unsigned char *lgot_masks = (unsigned char *)
e054468f 6646 (local_plt + symtab_hdr->sh_info);
e7b938ca 6647 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6648 }
e7b938ca 6649 *tls_maskp = tls_mask;
411e1bfb
AM
6650 }
6651 }
6652 return TRUE;
6653}
6654
e7b938ca 6655/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6656 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6657 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6658
6659static int
f961d9dd 6660get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6661 unsigned long *toc_symndx,
6662 bfd_vma *toc_addend,
0d4792f7 6663 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6664 const Elf_Internal_Rela *rel,
6665 bfd *ibfd)
411e1bfb
AM
6666{
6667 unsigned long r_symndx;
0d4792f7 6668 int next_r;
411e1bfb
AM
6669 struct elf_link_hash_entry *h;
6670 Elf_Internal_Sym *sym;
6671 asection *sec;
6672 bfd_vma off;
6673
6674 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6675 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6676 return 0;
411e1bfb 6677
37da22e5
AM
6678 if ((*tls_maskp != NULL
6679 && (**tls_maskp & TLS_TLS) != 0
6680 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 6681 || sec == NULL
6bee8834 6682 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6683 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6684 return 1;
411e1bfb
AM
6685
6686 /* Look inside a TOC section too. */
6687 if (h != NULL)
6688 {
6689 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6690 off = h->root.u.def.value;
6691 }
6692 else
6693 off = sym->st_value;
6694 off += rel->r_addend;
6695 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6696 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6697 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6698 if (toc_symndx != NULL)
6699 *toc_symndx = r_symndx;
3a71aa26
AM
6700 if (toc_addend != NULL)
6701 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6702 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6703 return 0;
854b41e7 6704 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6705 && (next_r == -1 || next_r == -2))
6706 return 1 - next_r;
951fd09b 6707 return 1;
411e1bfb
AM
6708}
6709
3b421ab3
AM
6710/* Find (or create) an entry in the tocsave hash table. */
6711
6712static struct tocsave_entry *
6713tocsave_find (struct ppc_link_hash_table *htab,
6714 enum insert_option insert,
6715 Elf_Internal_Sym **local_syms,
6716 const Elf_Internal_Rela *irela,
6717 bfd *ibfd)
6718{
6719 unsigned long r_indx;
6720 struct elf_link_hash_entry *h;
6721 Elf_Internal_Sym *sym;
6722 struct tocsave_entry ent, *p;
6723 hashval_t hash;
6724 struct tocsave_entry **slot;
6725
6726 r_indx = ELF64_R_SYM (irela->r_info);
6727 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6728 return NULL;
6729 if (ent.sec == NULL || ent.sec->output_section == NULL)
6730 {
4eca0228 6731 _bfd_error_handler
871b3ab2 6732 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
6733 return NULL;
6734 }
6735
6736 if (h != NULL)
6737 ent.offset = h->root.u.def.value;
6738 else
6739 ent.offset = sym->st_value;
6740 ent.offset += irela->r_addend;
6741
6742 hash = tocsave_htab_hash (&ent);
6743 slot = ((struct tocsave_entry **)
6744 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6745 if (slot == NULL)
6746 return NULL;
6747
6748 if (*slot == NULL)
6749 {
6750 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6751 if (p == NULL)
6752 return NULL;
6753 *p = ent;
6754 *slot = p;
6755 }
6756 return *slot;
6757}
6758
754021d0 6759/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6760 code for the old ABI, these will already have been done. */
754021d0
AM
6761
6762static bfd_boolean
6763adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6764{
6765 struct ppc_link_hash_entry *eh;
6766 asection *sym_sec;
74f0fb50 6767 struct _opd_sec_data *opd;
754021d0
AM
6768
6769 if (h->root.type == bfd_link_hash_indirect)
6770 return TRUE;
6771
754021d0
AM
6772 if (h->root.type != bfd_link_hash_defined
6773 && h->root.type != bfd_link_hash_defweak)
6774 return TRUE;
6775
6776 eh = (struct ppc_link_hash_entry *) h;
6777 if (eh->adjust_done)
6778 return TRUE;
6779
6780 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6781 opd = get_opd_info (sym_sec);
6782 if (opd != NULL && opd->adjust != NULL)
754021d0 6783 {
51aecdc5 6784 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
6785 if (adjust == -1)
6786 {
6787 /* This entry has been deleted. */
b3fac117 6788 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6789 if (dsec == NULL)
6790 {
6791 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 6792 if (discarded_section (dsec))
81688140 6793 {
b3fac117 6794 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6795 break;
6796 }
6797 }
4025353c 6798 eh->elf.root.u.def.value = 0;
81688140 6799 eh->elf.root.u.def.section = dsec;
4025353c
AM
6800 }
6801 else
6802 eh->elf.root.u.def.value += adjust;
754021d0
AM
6803 eh->adjust_done = 1;
6804 }
6805 return TRUE;
6806}
6807
8c1d1bb8 6808/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 6809 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
6810 have already been determined. */
6811
6812static bfd_boolean
6813dec_dynrel_count (bfd_vma r_info,
6814 asection *sec,
6815 struct bfd_link_info *info,
6816 Elf_Internal_Sym **local_syms,
6817 struct elf_link_hash_entry *h,
19e08130 6818 Elf_Internal_Sym *sym)
8c1d1bb8
AM
6819{
6820 enum elf_ppc64_reloc_type r_type;
19e08130 6821 asection *sym_sec = NULL;
8c1d1bb8
AM
6822
6823 /* Can this reloc be dynamic? This switch, and later tests here
6824 should be kept in sync with the code in check_relocs. */
6825 r_type = ELF64_R_TYPE (r_info);
6826 switch (r_type)
6827 {
6828 default:
6829 return TRUE;
6830
1bdd8fac
AM
6831 case R_PPC64_TOC16:
6832 case R_PPC64_TOC16_DS:
6833 case R_PPC64_TOC16_LO:
6834 case R_PPC64_TOC16_HI:
6835 case R_PPC64_TOC16_HA:
6836 case R_PPC64_TOC16_LO_DS:
6837 if (h == NULL)
6838 return TRUE;
6839 break;
6840
8c1d1bb8
AM
6841 case R_PPC64_TPREL16:
6842 case R_PPC64_TPREL16_LO:
6843 case R_PPC64_TPREL16_HI:
6844 case R_PPC64_TPREL16_HA:
6845 case R_PPC64_TPREL16_DS:
6846 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
6847 case R_PPC64_TPREL16_HIGH:
6848 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
6849 case R_PPC64_TPREL16_HIGHER:
6850 case R_PPC64_TPREL16_HIGHERA:
6851 case R_PPC64_TPREL16_HIGHEST:
6852 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8 6853 case R_PPC64_TPREL64:
c213164a 6854 case R_PPC64_TPREL34:
8c1d1bb8
AM
6855 case R_PPC64_DTPMOD64:
6856 case R_PPC64_DTPREL64:
6857 case R_PPC64_ADDR64:
6858 case R_PPC64_REL30:
6859 case R_PPC64_REL32:
6860 case R_PPC64_REL64:
6861 case R_PPC64_ADDR14:
6862 case R_PPC64_ADDR14_BRNTAKEN:
6863 case R_PPC64_ADDR14_BRTAKEN:
6864 case R_PPC64_ADDR16:
6865 case R_PPC64_ADDR16_DS:
6866 case R_PPC64_ADDR16_HA:
6867 case R_PPC64_ADDR16_HI:
f9c6b907
AM
6868 case R_PPC64_ADDR16_HIGH:
6869 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
6870 case R_PPC64_ADDR16_HIGHER:
6871 case R_PPC64_ADDR16_HIGHERA:
6872 case R_PPC64_ADDR16_HIGHEST:
6873 case R_PPC64_ADDR16_HIGHESTA:
6874 case R_PPC64_ADDR16_LO:
6875 case R_PPC64_ADDR16_LO_DS:
6876 case R_PPC64_ADDR24:
6877 case R_PPC64_ADDR32:
6878 case R_PPC64_UADDR16:
6879 case R_PPC64_UADDR32:
6880 case R_PPC64_UADDR64:
6881 case R_PPC64_TOC:
5663e321
AM
6882 case R_PPC64_D34:
6883 case R_PPC64_D34_LO:
6884 case R_PPC64_D34_HI30:
6885 case R_PPC64_D34_HA30:
6886 case R_PPC64_ADDR16_HIGHER34:
6887 case R_PPC64_ADDR16_HIGHERA34:
6888 case R_PPC64_ADDR16_HIGHEST34:
6889 case R_PPC64_ADDR16_HIGHESTA34:
6890 case R_PPC64_D28:
8c1d1bb8
AM
6891 break;
6892 }
6893
6894 if (local_syms != NULL)
6895 {
6896 unsigned long r_symndx;
8c1d1bb8
AM
6897 bfd *ibfd = sec->owner;
6898
6899 r_symndx = ELF64_R_SYM (r_info);
6900 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6901 return FALSE;
6902 }
6903
ec73ddcd
AM
6904 if ((h != NULL
6905 && (h->root.type == bfd_link_hash_defweak
6906 || !h->def_regular))
6907 || (h != NULL
6908 && !bfd_link_executable (info)
6909 && !SYMBOLIC_BIND (info, h))
6910 || (bfd_link_pic (info)
6911 && must_be_dyn_reloc (info, r_type))
6912 || (!bfd_link_pic (info)
6913 && (h != NULL
6914 ? h->type == STT_GNU_IFUNC
6915 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
8c1d1bb8
AM
6916 ;
6917 else
6918 return TRUE;
6919
6920 if (h != NULL)
6edfbbad 6921 {
19e08130
AM
6922 struct elf_dyn_relocs *p;
6923 struct elf_dyn_relocs **pp;
6924 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6925
6926 /* elf_gc_sweep may have already removed all dyn relocs associated
6927 with local syms for a given section. Also, symbol flags are
6928 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6929 report a dynreloc miscount. */
6930 if (*pp == NULL && info->gc_sections)
6931 return TRUE;
6932
6933 while ((p = *pp) != NULL)
60124e18 6934 {
19e08130
AM
6935 if (p->sec == sec)
6936 {
6937 if (!must_be_dyn_reloc (info, r_type))
6938 p->pc_count -= 1;
6939 p->count -= 1;
6940 if (p->count == 0)
6941 *pp = p->next;
6942 return TRUE;
6943 }
6944 pp = &p->next;
60124e18 6945 }
6edfbbad 6946 }
19e08130
AM
6947 else
6948 {
6949 struct ppc_dyn_relocs *p;
6950 struct ppc_dyn_relocs **pp;
6951 void *vpp;
6952 bfd_boolean is_ifunc;
8c1d1bb8 6953
19e08130
AM
6954 if (local_syms == NULL)
6955 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6956 if (sym_sec == NULL)
6957 sym_sec = sec;
c57da1a7 6958
19e08130
AM
6959 vpp = &elf_section_data (sym_sec)->local_dynrel;
6960 pp = (struct ppc_dyn_relocs **) vpp;
6961
6962 if (*pp == NULL && info->gc_sections)
6963 return TRUE;
6964
6965 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6966 while ((p = *pp) != NULL)
8c1d1bb8 6967 {
19e08130
AM
6968 if (p->sec == sec && p->ifunc == is_ifunc)
6969 {
6970 p->count -= 1;
6971 if (p->count == 0)
6972 *pp = p->next;
6973 return TRUE;
6974 }
6975 pp = &p->next;
8c1d1bb8 6976 }
8c1d1bb8
AM
6977 }
6978
695344c0 6979 /* xgettext:c-format */
cf97bcb0
AM
6980 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6981 sec->owner, sec);
8c1d1bb8
AM
6982 bfd_set_error (bfd_error_bad_value);
6983 return FALSE;
6984}
6985
754021d0
AM
6986/* Remove unused Official Procedure Descriptor entries. Currently we
6987 only remove those associated with functions in discarded link-once
6988 sections, or weakly defined functions that have been overridden. It
6989 would be possible to remove many more entries for statically linked
6990 applications. */
6991
b34976b6 6992bfd_boolean
e7d1c40c 6993ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
6994{
6995 bfd *ibfd;
754021d0 6996 bfd_boolean some_edited = FALSE;
3f764659 6997 asection *need_pad = NULL;
e7d1c40c
AM
6998 struct ppc_link_hash_table *htab;
6999
7000 htab = ppc_hash_table (info);
7001 if (htab == NULL)
7002 return FALSE;
1e2f5b6e 7003
c72f2fb2 7004 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7005 {
7006 asection *sec;
7007 Elf_Internal_Rela *relstart, *rel, *relend;
7008 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7009 Elf_Internal_Sym *local_syms;
74f0fb50 7010 struct _opd_sec_data *opd;
51aecdc5 7011 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7012 bfd_size_type cnt_16b = 0;
1e2f5b6e 7013
854b41e7
AM
7014 if (!is_ppc64_elf (ibfd))
7015 continue;
7016
1e2f5b6e 7017 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7018 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7019 continue;
7020
dbaa2011 7021 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7022 continue;
7023
1e2f5b6e
AM
7024 if (sec->output_section == bfd_abs_section_ptr)
7025 continue;
7026
7027 /* Look through the section relocs. */
7028 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7029 continue;
7030
6cdc0ccc 7031 local_syms = NULL;
0ffa91dd 7032 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7033
7034 /* Read the relocations. */
4ce794b7 7035 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7036 info->keep_memory);
1e2f5b6e 7037 if (relstart == NULL)
b34976b6 7038 return FALSE;
1e2f5b6e
AM
7039
7040 /* First run through the relocs to check they are sane, and to
7041 determine whether we need to edit this opd section. */
b34976b6 7042 need_edit = FALSE;
51aecdc5 7043 broken = FALSE;
3f764659 7044 need_pad = sec;
1e2f5b6e 7045 relend = relstart + sec->reloc_count;
50bc7936 7046 for (rel = relstart; rel < relend; )
1e2f5b6e 7047 {
04c9666a 7048 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7049 unsigned long r_symndx;
7050 asection *sym_sec;
7051 struct elf_link_hash_entry *h;
7052 Elf_Internal_Sym *sym;
51aecdc5 7053 bfd_vma offset;
1e2f5b6e 7054
51aecdc5 7055 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7056 only interested in the reloc pointing to a function entry
7057 point. */
51aecdc5
AM
7058 offset = rel->r_offset;
7059 if (rel + 1 == relend
7060 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7061 {
7062 /* If someone messes with .opd alignment then after a
7063 "ld -r" we might have padding in the middle of .opd.
7064 Also, there's nothing to prevent someone putting
7065 something silly in .opd with the assembler. No .opd
b34976b6 7066 optimization for them! */
3f764659 7067 broken_opd:
4eca0228 7068 _bfd_error_handler
871b3ab2 7069 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7070 broken = TRUE;
1e2f5b6e
AM
7071 break;
7072 }
7073
50bc7936
AM
7074 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7075 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7076 {
4eca0228 7077 _bfd_error_handler
695344c0 7078 /* xgettext:c-format */
871b3ab2 7079 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7080 ibfd, r_type);
51aecdc5 7081 broken = TRUE;
50bc7936
AM
7082 break;
7083 }
7084
1e2f5b6e 7085 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7086 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7087 r_symndx, ibfd))
50bc7936 7088 goto error_ret;
1e2f5b6e
AM
7089
7090 if (sym_sec == NULL || sym_sec->owner == NULL)
7091 {
411e1bfb
AM
7092 const char *sym_name;
7093 if (h != NULL)
7094 sym_name = h->root.root.string;
7095 else
26c61ae5
L
7096 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7097 sym_sec);
411e1bfb 7098
4eca0228 7099 _bfd_error_handler
695344c0 7100 /* xgettext:c-format */
871b3ab2 7101 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7102 ibfd, sym_name);
51aecdc5 7103 broken = TRUE;
1e2f5b6e
AM
7104 break;
7105 }
7106
51020317
AM
7107 /* opd entries are always for functions defined in the
7108 current input bfd. If the symbol isn't defined in the
7109 input bfd, then we won't be using the function in this
7110 bfd; It must be defined in a linkonce section in another
7111 bfd, or is weak. It's also possible that we are
7112 discarding the function due to a linker script /DISCARD/,
7113 which we test for via the output_section. */
7114 if (sym_sec->owner != ibfd
7115 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7116 need_edit = TRUE;
1e2f5b6e 7117
50bc7936 7118 rel += 2;
51aecdc5
AM
7119 if (rel + 1 == relend
7120 || (rel + 2 < relend
7121 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7122 ++rel;
7123
7124 if (rel == relend)
3f764659
JJ
7125 {
7126 if (sec->size == offset + 24)
7127 {
7128 need_pad = NULL;
7129 break;
7130 }
51aecdc5 7131 if (sec->size == offset + 16)
3f764659
JJ
7132 {
7133 cnt_16b++;
7134 break;
7135 }
7136 goto broken_opd;
7137 }
3f764659
JJ
7138 else if (rel + 1 < relend
7139 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7140 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7141 {
51aecdc5
AM
7142 if (rel[0].r_offset == offset + 16)
7143 cnt_16b++;
7144 else if (rel[0].r_offset != offset + 24)
7145 goto broken_opd;
3f764659
JJ
7146 }
7147 else
7148 goto broken_opd;
1e2f5b6e
AM
7149 }
7150
e7d1c40c 7151 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7152
51aecdc5 7153 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7154 {
7155 Elf_Internal_Rela *write_rel;
d4730f92 7156 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7157 bfd_byte *rptr, *wptr;
983bddc8 7158 bfd_byte *new_contents;
74f0fb50
AM
7159 bfd_size_type amt;
7160
983bddc8 7161 new_contents = NULL;
51aecdc5 7162 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7163 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7164 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7165 if (opd->adjust == NULL)
7166 return FALSE;
1e2f5b6e
AM
7167
7168 /* This seems a waste of time as input .opd sections are all
7169 zeros as generated by gcc, but I suppose there's no reason
7170 this will always be so. We might start putting something in
7171 the third word of .opd entries. */
7172 if ((sec->flags & SEC_IN_MEMORY) == 0)
7173 {
eea6121a
AM
7174 bfd_byte *loc;
7175 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7176 {
eea6121a
AM
7177 if (loc != NULL)
7178 free (loc);
50bc7936 7179 error_ret:
6cdc0ccc
AM
7180 if (local_syms != NULL
7181 && symtab_hdr->contents != (unsigned char *) local_syms)
7182 free (local_syms);
6cdc0ccc
AM
7183 if (elf_section_data (sec)->relocs != relstart)
7184 free (relstart);
b34976b6 7185 return FALSE;
6cdc0ccc 7186 }
1e2f5b6e
AM
7187 sec->contents = loc;
7188 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7189 }
7190
7191 elf_section_data (sec)->relocs = relstart;
7192
3f764659 7193 new_contents = sec->contents;
3f764659
JJ
7194 if (add_aux_fields)
7195 {
7196 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7197 if (new_contents == NULL)
7198 return FALSE;
51aecdc5 7199 need_pad = NULL;
3f764659 7200 }
b4f4e59f
AM
7201 wptr = new_contents;
7202 rptr = sec->contents;
1e2f5b6e 7203 write_rel = relstart;
51aecdc5 7204 for (rel = relstart; rel < relend; )
1e2f5b6e 7205 {
50bc7936
AM
7206 unsigned long r_symndx;
7207 asection *sym_sec;
7208 struct elf_link_hash_entry *h;
51aecdc5 7209 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7210 Elf_Internal_Sym *sym;
51aecdc5
AM
7211 long opd_ent_size;
7212 Elf_Internal_Rela *next_rel;
7213 bfd_boolean skip;
50bc7936
AM
7214
7215 r_symndx = ELF64_R_SYM (rel->r_info);
7216 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7217 r_symndx, ibfd))
50bc7936
AM
7218 goto error_ret;
7219
51aecdc5
AM
7220 next_rel = rel + 2;
7221 if (next_rel + 1 == relend
7222 || (next_rel + 2 < relend
7223 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7224 ++next_rel;
7225
7226 /* See if the .opd entry is full 24 byte or
7227 16 byte (with fd_aux entry overlapped with next
7228 fd_func). */
7229 opd_ent_size = 24;
7230 if (next_rel == relend)
1e2f5b6e 7231 {
51aecdc5 7232 if (sec->size == rel->r_offset + 16)
3f764659 7233 opd_ent_size = 16;
51aecdc5
AM
7234 }
7235 else if (next_rel->r_offset == rel->r_offset + 16)
7236 opd_ent_size = 16;
3f764659 7237
51aecdc5
AM
7238 if (h != NULL
7239 && h->root.root.string[0] == '.')
7240 {
8c5b4e52
AM
7241 fdh = ((struct ppc_link_hash_entry *) h)->oh;
7242 if (fdh != NULL)
7243 {
7244 fdh = ppc_follow_link (fdh);
7245 if (fdh->elf.root.type != bfd_link_hash_defined
7246 && fdh->elf.root.type != bfd_link_hash_defweak)
7247 fdh = NULL;
7248 }
51aecdc5 7249 }
1e2f5b6e 7250
51aecdc5
AM
7251 skip = (sym_sec->owner != ibfd
7252 || sym_sec->output_section == bfd_abs_section_ptr);
7253 if (skip)
7254 {
7255 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7256 {
51aecdc5
AM
7257 /* Arrange for the function descriptor sym
7258 to be dropped. */
7259 fdh->elf.root.u.def.value = 0;
7260 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7261 }
51aecdc5 7262 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7263
0e1862bb 7264 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7265 rel = next_rel;
7266 else
7267 while (1)
7268 {
7269 if (!dec_dynrel_count (rel->r_info, sec, info,
7270 NULL, h, sym))
7271 goto error_ret;
754021d0 7272
51aecdc5
AM
7273 if (++rel == next_rel)
7274 break;
1e2f5b6e 7275
51aecdc5
AM
7276 r_symndx = ELF64_R_SYM (rel->r_info);
7277 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7278 r_symndx, ibfd))
7279 goto error_ret;
7280 }
50bc7936
AM
7281 }
7282 else
1e2f5b6e 7283 {
51aecdc5
AM
7284 /* We'll be keeping this opd entry. */
7285 long adjust;
7286
7287 if (fdh != NULL)
7288 {
7289 /* Redefine the function descriptor symbol to
7290 this location in the opd section. It is
7291 necessary to update the value here rather
7292 than using an array of adjustments as we do
7293 for local symbols, because various places
7294 in the generic ELF code use the value
7295 stored in u.def.value. */
7296 fdh->elf.root.u.def.value = wptr - new_contents;
7297 fdh->adjust_done = 1;
7298 }
7299
7300 /* Local syms are a bit tricky. We could
7301 tweak them as they can be cached, but
7302 we'd need to look through the local syms
7303 for the function descriptor sym which we
7304 don't have at the moment. So keep an
7305 array of adjustments. */
7306 adjust = (wptr - new_contents) - (rptr - sec->contents);
7307 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7308
7309 if (wptr != rptr)
7310 memcpy (wptr, rptr, opd_ent_size);
7311 wptr += opd_ent_size;
7312 if (add_aux_fields && opd_ent_size == 16)
7313 {
7314 memset (wptr, '\0', 8);
7315 wptr += 8;
7316 }
7317
50bc7936 7318 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7319 new opd entries. */
7320 for ( ; rel != next_rel; ++rel)
7321 {
7322 rel->r_offset += adjust;
7323 if (write_rel != rel)
7324 memcpy (write_rel, rel, sizeof (*rel));
7325 ++write_rel;
7326 }
1e2f5b6e 7327 }
51aecdc5
AM
7328
7329 rptr += opd_ent_size;
1e2f5b6e
AM
7330 }
7331
3f764659 7332 sec->size = wptr - new_contents;
1e2f5b6e 7333 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7334 if (add_aux_fields)
7335 {
7336 free (sec->contents);
7337 sec->contents = new_contents;
7338 }
7339
05bf9422 7340 /* Fudge the header size too, as this is used later in
cdcf6e38 7341 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7342 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7343 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7344 some_edited = TRUE;
1e2f5b6e 7345 }
6cdc0ccc 7346 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7347 free (relstart);
6cdc0ccc 7348
411e1bfb
AM
7349 if (local_syms != NULL
7350 && symtab_hdr->contents != (unsigned char *) local_syms)
7351 {
7352 if (!info->keep_memory)
7353 free (local_syms);
7354 else
7355 symtab_hdr->contents = (unsigned char *) local_syms;
7356 }
7357 }
7358
754021d0
AM
7359 if (some_edited)
7360 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7361
3f764659
JJ
7362 /* If we are doing a final link and the last .opd entry is just 16 byte
7363 long, add a 8 byte padding after it. */
0e1862bb 7364 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7365 {
7366 bfd_byte *p;
7367
7368 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7369 {
7370 BFD_ASSERT (need_pad->size > 0);
7371
7372 p = bfd_malloc (need_pad->size + 8);
7373 if (p == NULL)
7374 return FALSE;
699733f6 7375
2cdcc330
AM
7376 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7377 p, 0, need_pad->size))
3f764659
JJ
7378 return FALSE;
7379
7380 need_pad->contents = p;
7381 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7382 }
7383 else
7384 {
7385 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7386 if (p == NULL)
7387 return FALSE;
7388
7389 need_pad->contents = p;
7390 }
7391
7392 memset (need_pad->contents + need_pad->size, 0, 8);
7393 need_pad->size += 8;
7394 }
7395
411e1bfb
AM
7396 return TRUE;
7397}
7398
3e04d765
AM
7399/* Analyze inline PLT call relocations to see whether calls to locally
7400 defined functions can be converted to direct calls. */
7401
7402bfd_boolean
7403ppc64_elf_inline_plt (struct bfd_link_info *info)
7404{
7405 struct ppc_link_hash_table *htab;
7406 bfd *ibfd;
7407 asection *sec;
7408 bfd_vma low_vma, high_vma, limit;
7409
7410 htab = ppc_hash_table (info);
7411 if (htab == NULL)
7412 return FALSE;
7413
7414 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7415 reduced somewhat to cater for possible stubs that might be added
7416 between the call and its destination. */
7417 if (htab->params->group_size < 0)
7418 {
7419 limit = -htab->params->group_size;
7420 if (limit == 1)
7421 limit = 0x1e00000;
7422 }
7423 else
7424 {
7425 limit = htab->params->group_size;
7426 if (limit == 1)
7427 limit = 0x1c00000;
7428 }
7429
7430 low_vma = -1;
7431 high_vma = 0;
7432 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7433 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7434 {
7435 if (low_vma > sec->vma)
7436 low_vma = sec->vma;
7437 if (high_vma < sec->vma + sec->size)
7438 high_vma = sec->vma + sec->size;
7439 }
7440
7441 /* If a "bl" can reach anywhere in local code sections, then we can
7442 convert all inline PLT sequences to direct calls when the symbol
7443 is local. */
7444 if (high_vma - low_vma < limit)
7445 {
7446 htab->can_convert_all_inline_plt = 1;
7447 return TRUE;
7448 }
7449
7450 /* Otherwise, go looking through relocs for cases where a direct
7451 call won't reach. Mark the symbol on any such reloc to disable
7452 the optimization and keep the PLT entry as it seems likely that
7453 this will be better than creating trampolines. Note that this
7454 will disable the optimization for all inline PLT calls to a
7455 particular symbol, not just those that won't reach. The
7456 difficulty in doing a more precise optimization is that the
7457 linker needs to make a decision depending on whether a
7458 particular R_PPC64_PLTCALL insn can be turned into a direct
7459 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7460 the sequence, and there is nothing that ties those relocs
7461 together except their symbol. */
7462
7463 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7464 {
7465 Elf_Internal_Shdr *symtab_hdr;
7466 Elf_Internal_Sym *local_syms;
7467
7468 if (!is_ppc64_elf (ibfd))
7469 continue;
7470
7471 local_syms = NULL;
7472 symtab_hdr = &elf_symtab_hdr (ibfd);
7473
7474 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7475 if (ppc64_elf_section_data (sec)->has_pltcall
7476 && !bfd_is_abs_section (sec->output_section))
7477 {
7478 Elf_Internal_Rela *relstart, *rel, *relend;
7479
7480 /* Read the relocations. */
7481 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7482 info->keep_memory);
7483 if (relstart == NULL)
7484 return FALSE;
7485
7486 relend = relstart + sec->reloc_count;
7487 for (rel = relstart; rel < relend; )
7488 {
7489 enum elf_ppc64_reloc_type r_type;
7490 unsigned long r_symndx;
7491 asection *sym_sec;
7492 struct elf_link_hash_entry *h;
7493 Elf_Internal_Sym *sym;
7494 unsigned char *tls_maskp;
7495
7496 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7497 if (r_type != R_PPC64_PLTCALL
7498 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7499 continue;
7500
7501 r_symndx = ELF64_R_SYM (rel->r_info);
7502 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7503 r_symndx, ibfd))
7504 {
7505 if (elf_section_data (sec)->relocs != relstart)
7506 free (relstart);
7507 if (local_syms != NULL
2cdcc330 7508 && symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765
AM
7509 free (local_syms);
7510 return FALSE;
7511 }
7512
7513 if (sym_sec != NULL && sym_sec->output_section != NULL)
7514 {
7515 bfd_vma from, to;
7516 if (h != NULL)
7517 to = h->root.u.def.value;
7518 else
7519 to = sym->st_value;
7520 to += (rel->r_addend
7521 + sym_sec->output_offset
7522 + sym_sec->output_section->vma);
7523 from = (rel->r_offset
7524 + sec->output_offset
7525 + sec->output_section->vma);
5663e321
AM
7526 if (to - from + limit < 2 * limit
7527 && !(r_type == R_PPC64_PLTCALL_NOTOC
7528 && (((h ? h->other : sym->st_other)
7529 & STO_PPC64_LOCAL_MASK)
4a4e7361 7530 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7531 *tls_maskp &= ~PLT_KEEP;
7532 }
7533 }
7534 if (elf_section_data (sec)->relocs != relstart)
7535 free (relstart);
7536 }
7537
7538 if (local_syms != NULL
7539 && symtab_hdr->contents != (unsigned char *) local_syms)
7540 {
7541 if (!info->keep_memory)
7542 free (local_syms);
7543 else
7544 symtab_hdr->contents = (unsigned char *) local_syms;
7545 }
7546 }
7547
7548 return TRUE;
7549}
7550
e1918d23 7551/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7552
e1918d23 7553asection *
e7d1c40c 7554ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7555{
411e1bfb
AM
7556 struct ppc_link_hash_table *htab;
7557
411e1bfb 7558 htab = ppc_hash_table (info);
4dfe6ac6
NC
7559 if (htab == NULL)
7560 return NULL;
7561
ee67d69a
AM
7562 if (abiversion (info->output_bfd) == 1)
7563 htab->opd_abi = 1;
7564
e7d1c40c 7565 if (htab->params->no_multi_toc)
33c0ec9d
AM
7566 htab->do_multi_toc = 0;
7567 else if (!htab->do_multi_toc)
e7d1c40c 7568 htab->params->no_multi_toc = 1;
33c0ec9d 7569
8b5f1ed8
AM
7570 /* Default to --no-plt-localentry, as this option can cause problems
7571 with symbol interposition. For example, glibc libpthread.so and
7572 libc.so duplicate many pthread symbols, with a fallback
7573 implementation in libc.so. In some cases the fallback does more
7574 work than the pthread implementation. __pthread_condattr_destroy
7575 is one such symbol: the libpthread.so implementation is
7576 localentry:0 while the libc.so implementation is localentry:8.
7577 An app that "cleverly" uses dlopen to only load necessary
7578 libraries at runtime may omit loading libpthread.so when not
7579 running multi-threaded, which then results in the libc.so
7580 fallback symbols being used and ld.so complaining. Now there
7581 are workarounds in ld (see non_zero_localentry) to detect the
7582 pthread situation, but that may not be the only case where
7583 --plt-localentry can cause trouble. */
f378ab09 7584 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7585 htab->params->plt_localentry0 = 0;
d44c746a
AM
7586 if (htab->params->plt_localentry0
7587 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7588 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
7589 _bfd_error_handler
7590 (_("warning: --plt-localentry is especially dangerous without "
7591 "ld.so support to detect ABI violations"));
f378ab09 7592
3a71aa26
AM
7593 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7594 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7595 FALSE, FALSE, TRUE));
a7f2871e
AM
7596 /* Move dynamic linking info to the function descriptor sym. */
7597 if (htab->tls_get_addr != NULL)
7598 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7599 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7600 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7601 FALSE, FALSE, TRUE));
7c9cf415 7602 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
7603 {
7604 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7605
7606 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7607 FALSE, FALSE, TRUE);
7608 if (opt != NULL)
7609 func_desc_adjust (opt, info);
7610 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7611 FALSE, FALSE, TRUE);
7612 if (opt_fd != NULL
7613 && (opt_fd->root.type == bfd_link_hash_defined
7614 || opt_fd->root.type == bfd_link_hash_defweak))
7615 {
7616 /* If glibc supports an optimized __tls_get_addr call stub,
7617 signalled by the presence of __tls_get_addr_opt, and we'll
7618 be calling __tls_get_addr via a plt call stub, then
7619 make __tls_get_addr point to __tls_get_addr_opt. */
7620 tga_fd = &htab->tls_get_addr_fd->elf;
7621 if (htab->elf.dynamic_sections_created
7622 && tga_fd != NULL
7623 && (tga_fd->type == STT_FUNC
7624 || tga_fd->needs_plt)
7625 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 7626 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
7627 {
7628 struct plt_entry *ent;
7629
7630 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7631 if (ent->plt.refcount > 0)
7632 break;
7633 if (ent != NULL)
7634 {
7635 tga_fd->root.type = bfd_link_hash_indirect;
7636 tga_fd->root.u.i.link = &opt_fd->root;
7637 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 7638 opt_fd->mark = 1;
a7f2871e
AM
7639 if (opt_fd->dynindx != -1)
7640 {
7641 /* Use __tls_get_addr_opt in dynamic relocations. */
7642 opt_fd->dynindx = -1;
7643 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7644 opt_fd->dynstr_index);
7645 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7646 return NULL;
a7f2871e 7647 }
2cdcc330
AM
7648 htab->tls_get_addr_fd
7649 = (struct ppc_link_hash_entry *) opt_fd;
a7f2871e
AM
7650 tga = &htab->tls_get_addr->elf;
7651 if (opt != NULL && tga != NULL)
7652 {
7653 tga->root.type = bfd_link_hash_indirect;
7654 tga->root.u.i.link = &opt->root;
7655 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 7656 opt->mark = 1;
a7f2871e
AM
7657 _bfd_elf_link_hash_hide_symbol (info, opt,
7658 tga->forced_local);
7659 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7660 }
7661 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7662 htab->tls_get_addr_fd->is_func_descriptor = 1;
7663 if (htab->tls_get_addr != NULL)
7664 {
7665 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7666 htab->tls_get_addr->is_func = 1;
7667 }
7668 }
7669 }
7670 }
7c9cf415
AM
7671 else if (htab->params->tls_get_addr_opt < 0)
7672 htab->params->tls_get_addr_opt = 0;
a7f2871e 7673 }
33c0ec9d 7674 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7675}
8387904d 7676
3a71aa26
AM
7677/* Return TRUE iff REL is a branch reloc with a global symbol matching
7678 HASH1 or HASH2. */
8387904d 7679
3a71aa26
AM
7680static bfd_boolean
7681branch_reloc_hash_match (const bfd *ibfd,
7682 const Elf_Internal_Rela *rel,
7683 const struct ppc_link_hash_entry *hash1,
7684 const struct ppc_link_hash_entry *hash2)
7685{
7686 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7687 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7688 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7689
e054468f 7690 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7691 {
3a71aa26
AM
7692 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7693 struct elf_link_hash_entry *h;
8387904d 7694
3a71aa26 7695 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7696 h = elf_follow_link (h);
3a71aa26
AM
7697 if (h == &hash1->elf || h == &hash2->elf)
7698 return TRUE;
a48ebf4d 7699 }
3a71aa26 7700 return FALSE;
951fd09b 7701}
411e1bfb 7702
951fd09b
AM
7703/* Run through all the TLS relocs looking for optimization
7704 opportunities. The linker has been hacked (see ppc64elf.em) to do
7705 a preliminary section layout so that we know the TLS segment
7706 offsets. We can't optimize earlier because some optimizations need
7707 to know the tp offset, and we need to optimize before allocating
7708 dynamic relocations. */
7709
7710bfd_boolean
33c0ec9d 7711ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7712{
7713 bfd *ibfd;
7714 asection *sec;
7715 struct ppc_link_hash_table *htab;
663a1470 7716 unsigned char *toc_ref;
102890f0 7717 int pass;
951fd09b 7718
3cbc1e5e 7719 if (!bfd_link_executable (info))
411e1bfb
AM
7720 return TRUE;
7721
951fd09b 7722 htab = ppc_hash_table (info);
4dfe6ac6
NC
7723 if (htab == NULL)
7724 return FALSE;
7725
663a1470
AM
7726 /* Make two passes over the relocs. On the first pass, mark toc
7727 entries involved with tls relocs, and check that tls relocs
7728 involved in setting up a tls_get_addr call are indeed followed by
7729 such a call. If they are not, we can't do any tls optimization.
7730 On the second pass twiddle tls_mask flags to notify
7731 relocate_section that optimization can be done, and adjust got
7732 and plt refcounts. */
7733 toc_ref = NULL;
7734 for (pass = 0; pass < 2; ++pass)
c72f2fb2 7735 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
7736 {
7737 Elf_Internal_Sym *locsyms = NULL;
7738 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7739
102890f0
AM
7740 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7741 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7742 {
7743 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7744 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7745
102890f0
AM
7746 /* Read the relocations. */
7747 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7748 info->keep_memory);
7749 if (relstart == NULL)
2915c55b
JK
7750 {
7751 free (toc_ref);
7752 return FALSE;
7753 }
411e1bfb 7754
102890f0
AM
7755 relend = relstart + sec->reloc_count;
7756 for (rel = relstart; rel < relend; rel++)
7757 {
7758 enum elf_ppc64_reloc_type r_type;
7759 unsigned long r_symndx;
7760 struct elf_link_hash_entry *h;
7761 Elf_Internal_Sym *sym;
7762 asection *sym_sec;
f961d9dd 7763 unsigned char *tls_mask;
46e9995a 7764 unsigned int tls_set, tls_clear, tls_type = 0;
102890f0
AM
7765 bfd_vma value;
7766 bfd_boolean ok_tprel, is_local;
7767 long toc_ref_index = 0;
7768 int expecting_tls_get_addr = 0;
663a1470 7769 bfd_boolean ret = FALSE;
411e1bfb 7770
102890f0
AM
7771 r_symndx = ELF64_R_SYM (rel->r_info);
7772 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7773 r_symndx, ibfd))
7774 {
7775 err_free_rel:
7776 if (elf_section_data (sec)->relocs != relstart)
7777 free (relstart);
7778 if (toc_ref != NULL)
7779 free (toc_ref);
7780 if (locsyms != NULL
0ffa91dd 7781 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7782 != (unsigned char *) locsyms))
7783 free (locsyms);
663a1470 7784 return ret;
102890f0 7785 }
411e1bfb 7786
102890f0
AM
7787 if (h != NULL)
7788 {
766bc656
AM
7789 if (h->root.type == bfd_link_hash_defined
7790 || h->root.type == bfd_link_hash_defweak)
7791 value = h->root.u.def.value;
7792 else if (h->root.type == bfd_link_hash_undefweak)
7793 value = 0;
7794 else
663a1470
AM
7795 {
7796 found_tls_get_addr_arg = 0;
7797 continue;
7798 }
102890f0
AM
7799 }
7800 else
7801 /* Symbols referenced by TLS relocs must be of type
7802 STT_TLS. So no need for .opd local sym adjust. */
7803 value = sym->st_value;
7804
7805 ok_tprel = FALSE;
f749f26e
AM
7806 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
7807 if (is_local)
102890f0 7808 {
766bc656
AM
7809 if (h != NULL
7810 && h->root.type == bfd_link_hash_undefweak)
7811 ok_tprel = TRUE;
c27b8c2a
AM
7812 else if (sym_sec != NULL
7813 && sym_sec->output_section != NULL)
766bc656
AM
7814 {
7815 value += sym_sec->output_offset;
7816 value += sym_sec->output_section->vma;
0b147428 7817 value -= htab->elf.tls_sec->vma + TP_OFFSET;
c213164a
AM
7818 /* Note that even though the prefix insns
7819 allow a 1<<33 offset we use the same test
7820 as for addis;addi. There may be a mix of
7821 pcrel and non-pcrel code and the decision
7822 to optimise is per symbol, not per TLS
7823 sequence. */
0b147428 7824 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
766bc656 7825 }
102890f0 7826 }
951fd09b 7827
102890f0 7828 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7829 /* If this section has old-style __tls_get_addr calls
7830 without marker relocs, then check that each
7831 __tls_get_addr call reloc is preceded by a reloc
7832 that conceivably belongs to the __tls_get_addr arg
7833 setup insn. If we don't find matching arg setup
7834 relocs, don't do any tls optimization. */
7835 if (pass == 0
7836 && sec->has_tls_get_addr_call
7837 && h != NULL
7838 && (h == &htab->tls_get_addr->elf
7839 || h == &htab->tls_get_addr_fd->elf)
7840 && !found_tls_get_addr_arg
7841 && is_branch_reloc (r_type))
7842 {
25f53a85 7843 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
7844 "TLS optimization disabled\n"),
7845 ibfd, sec, rel->r_offset);
7846 ret = TRUE;
7847 goto err_free_rel;
7848 }
7849
7850 found_tls_get_addr_arg = 0;
102890f0
AM
7851 switch (r_type)
7852 {
7853 case R_PPC64_GOT_TLSLD16:
7854 case R_PPC64_GOT_TLSLD16_LO:
c213164a 7855 case R_PPC64_GOT_TLSLD34:
102890f0 7856 expecting_tls_get_addr = 1;
663a1470 7857 found_tls_get_addr_arg = 1;
1a0670f3 7858 /* Fall through. */
102890f0
AM
7859
7860 case R_PPC64_GOT_TLSLD16_HI:
7861 case R_PPC64_GOT_TLSLD16_HA:
7862 /* These relocs should never be against a symbol
7863 defined in a shared lib. Leave them alone if
7864 that turns out to be the case. */
7865 if (!is_local)
7866 continue;
411e1bfb 7867
102890f0 7868 /* LD -> LE */
411e1bfb 7869 tls_set = 0;
102890f0
AM
7870 tls_clear = TLS_LD;
7871 tls_type = TLS_TLS | TLS_LD;
7872 break;
411e1bfb 7873
102890f0
AM
7874 case R_PPC64_GOT_TLSGD16:
7875 case R_PPC64_GOT_TLSGD16_LO:
c213164a 7876 case R_PPC64_GOT_TLSGD34:
102890f0 7877 expecting_tls_get_addr = 1;
663a1470 7878 found_tls_get_addr_arg = 1;
1a0670f3 7879 /* Fall through. */
102890f0
AM
7880
7881 case R_PPC64_GOT_TLSGD16_HI:
7882 case R_PPC64_GOT_TLSGD16_HA:
7883 if (ok_tprel)
7884 /* GD -> LE */
411e1bfb 7885 tls_set = 0;
102890f0
AM
7886 else
7887 /* GD -> IE */
b00a0a86 7888 tls_set = TLS_TLS | TLS_GDIE;
102890f0
AM
7889 tls_clear = TLS_GD;
7890 tls_type = TLS_TLS | TLS_GD;
7891 break;
7892
c213164a 7893 case R_PPC64_GOT_TPREL34:
102890f0
AM
7894 case R_PPC64_GOT_TPREL16_DS:
7895 case R_PPC64_GOT_TPREL16_LO_DS:
7896 case R_PPC64_GOT_TPREL16_HI:
7897 case R_PPC64_GOT_TPREL16_HA:
7898 if (ok_tprel)
7899 {
7900 /* IE -> LE */
7901 tls_set = 0;
7902 tls_clear = TLS_TPREL;
7903 tls_type = TLS_TLS | TLS_TPREL;
7904 break;
7905 }
411e1bfb
AM
7906 continue;
7907
727fc41e
AM
7908 case R_PPC64_TLSGD:
7909 case R_PPC64_TLSLD:
23cedd1d
AM
7910 if (rel + 1 < relend
7911 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7912 {
7913 if (pass != 0
2cdcc330 7914 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
7915 != R_PPC64_PLTSEQ)
7916 && (ELF64_R_TYPE (rel[1].r_info)
7917 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
7918 {
7919 r_symndx = ELF64_R_SYM (rel[1].r_info);
7920 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 7921 r_symndx, ibfd))
23cedd1d
AM
7922 goto err_free_rel;
7923 if (h != NULL)
7924 {
7925 struct plt_entry *ent = NULL;
7926
7927 for (ent = h->plt.plist;
7928 ent != NULL;
7929 ent = ent->next)
7930 if (ent->addend == rel[1].r_addend)
7931 break;
7932
7933 if (ent != NULL
7934 && ent->plt.refcount > 0)
7935 ent->plt.refcount -= 1;
7936 }
7937 }
7938 continue;
7939 }
663a1470 7940 found_tls_get_addr_arg = 1;
1a0670f3 7941 /* Fall through. */
663a1470
AM
7942
7943 case R_PPC64_TLS:
7944 case R_PPC64_TOC16:
7945 case R_PPC64_TOC16_LO:
102890f0
AM
7946 if (sym_sec == NULL || sym_sec != toc)
7947 continue;
7948
7949 /* Mark this toc entry as referenced by a TLS
7950 code sequence. We can do that now in the
7951 case of R_PPC64_TLS, and after checking for
7952 tls_get_addr for the TOC16 relocs. */
7953 if (toc_ref == NULL)
2cdcc330
AM
7954 toc_ref
7955 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
7956 if (toc_ref == NULL)
7957 goto err_free_rel;
7958
102890f0
AM
7959 if (h != NULL)
7960 value = h->root.u.def.value;
7961 else
7962 value = sym->st_value;
7963 value += rel->r_addend;
73242275
AM
7964 if (value % 8 != 0)
7965 continue;
7966 BFD_ASSERT (value < toc->size
7967 && toc->output_offset % 8 == 0);
663a1470 7968 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7969 if (r_type == R_PPC64_TLS
7970 || r_type == R_PPC64_TLSGD
7971 || r_type == R_PPC64_TLSLD)
102890f0
AM
7972 {
7973 toc_ref[toc_ref_index] = 1;
7974 continue;
7975 }
7976
7977 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7978 continue;
7979
7980 tls_set = 0;
7981 tls_clear = 0;
7982 expecting_tls_get_addr = 2;
7983 break;
7984
7985 case R_PPC64_TPREL64:
7986 if (pass == 0
7987 || sec != toc
7988 || toc_ref == NULL
663a1470 7989 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7990 continue;
7991 if (ok_tprel)
7992 {
7993 /* IE -> LE */
7994 tls_set = TLS_EXPLICIT;
7995 tls_clear = TLS_TPREL;
7996 break;
7997 }
7998 continue;
7999
8000 case R_PPC64_DTPMOD64:
8001 if (pass == 0
8002 || sec != toc
8003 || toc_ref == NULL
663a1470 8004 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8005 continue;
8006 if (rel + 1 < relend
8007 && (rel[1].r_info
8008 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8009 && rel[1].r_offset == rel->r_offset + 8)
8010 {
8011 if (ok_tprel)
8012 /* GD -> LE */
8013 tls_set = TLS_EXPLICIT | TLS_GD;
8014 else
8015 /* GD -> IE */
b00a0a86 8016 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
102890f0
AM
8017 tls_clear = TLS_GD;
8018 }
8019 else
8020 {
8021 if (!is_local)
8022 continue;
8023
8024 /* LD -> LE */
8025 tls_set = TLS_EXPLICIT;
8026 tls_clear = TLS_LD;
8027 }
8028 break;
8029
8030 default:
8031 continue;
8032 }
8033
8034 if (pass == 0)
8035 {
727fc41e
AM
8036 if (!expecting_tls_get_addr
8037 || !sec->has_tls_get_addr_call)
102890f0
AM
8038 continue;
8039
3a71aa26
AM
8040 if (rel + 1 < relend
8041 && branch_reloc_hash_match (ibfd, rel + 1,
8042 htab->tls_get_addr,
8043 htab->tls_get_addr_fd))
102890f0 8044 {
3a71aa26 8045 if (expecting_tls_get_addr == 2)
102890f0 8046 {
3a71aa26 8047 /* Check for toc tls entries. */
f961d9dd 8048 unsigned char *toc_tls;
3a71aa26
AM
8049 int retval;
8050
8051 retval = get_tls_mask (&toc_tls, NULL, NULL,
8052 &locsyms,
8053 rel, ibfd);
8054 if (retval == 0)
8055 goto err_free_rel;
663a1470
AM
8056 if (toc_tls != NULL)
8057 {
37da22e5
AM
8058 if ((*toc_tls & TLS_TLS) != 0
8059 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8060 found_tls_get_addr_arg = 1;
8061 if (retval > 1)
8062 toc_ref[toc_ref_index] = 1;
8063 }
102890f0 8064 }
3a71aa26 8065 continue;
102890f0
AM
8066 }
8067
102890f0
AM
8068 /* Uh oh, we didn't find the expected call. We
8069 could just mark this symbol to exclude it
8070 from tls optimization but it's safer to skip
663a1470 8071 the entire optimization. */
695344c0 8072 /* xgettext:c-format */
25f53a85 8073 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8074 "TLS optimization disabled\n"),
8075 ibfd, sec, rel->r_offset);
8076 ret = TRUE;
8077 goto err_free_rel;
102890f0
AM
8078 }
8079
37da22e5
AM
8080 /* If we don't have old-style __tls_get_addr calls
8081 without TLSGD/TLSLD marker relocs, and we haven't
8082 found a new-style __tls_get_addr call with a
8083 marker for this symbol, then we either have a
8084 broken object file or an -mlongcall style
8085 indirect call to __tls_get_addr without a marker.
8086 Disable optimization in this case. */
8087 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8088 && (tls_set & TLS_EXPLICIT) == 0
8089 && !sec->has_tls_get_addr_call
8090 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8091 != (TLS_TLS | TLS_MARK)))
8092 continue;
8093
23cedd1d 8094 if (expecting_tls_get_addr)
102890f0 8095 {
23cedd1d
AM
8096 struct plt_entry *ent = NULL;
8097
8098 if (htab->tls_get_addr != NULL)
8099 for (ent = htab->tls_get_addr->elf.plt.plist;
8100 ent != NULL;
8101 ent = ent->next)
8102 if (ent->addend == 0)
102890f0 8103 break;
411e1bfb 8104
23cedd1d
AM
8105 if (ent == NULL && htab->tls_get_addr_fd != NULL)
8106 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8107 ent != NULL;
8108 ent = ent->next)
8109 if (ent->addend == 0)
102890f0 8110 break;
23cedd1d
AM
8111
8112 if (ent != NULL
8113 && ent->plt.refcount > 0)
8114 ent->plt.refcount -= 1;
102890f0 8115 }
411e1bfb 8116
102890f0 8117 if (tls_clear == 0)
30038c59
AM
8118 continue;
8119
102890f0
AM
8120 if ((tls_set & TLS_EXPLICIT) == 0)
8121 {
8122 struct got_entry *ent;
411e1bfb 8123
102890f0
AM
8124 /* Adjust got entry for this reloc. */
8125 if (h != NULL)
8126 ent = h->got.glist;
8127 else
8128 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8129
102890f0
AM
8130 for (; ent != NULL; ent = ent->next)
8131 if (ent->addend == rel->r_addend
8132 && ent->owner == ibfd
8133 && ent->tls_type == tls_type)
8134 break;
8135 if (ent == NULL)
8136 abort ();
411e1bfb 8137
102890f0
AM
8138 if (tls_set == 0)
8139 {
8140 /* We managed to get rid of a got entry. */
8141 if (ent->got.refcount > 0)
8142 ent->got.refcount -= 1;
8143 }
8144 }
8145 else
8146 {
8147 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8148 we'll lose one or two dyn relocs. */
8149 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8150 NULL, h, sym))
102890f0 8151 return FALSE;
411e1bfb 8152
102890f0
AM
8153 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8154 {
8155 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8156 NULL, h, sym))
102890f0
AM
8157 return FALSE;
8158 }
8159 }
411e1bfb 8160
46e9995a 8161 *tls_mask |= tls_set & 0xff;
102890f0
AM
8162 *tls_mask &= ~tls_clear;
8163 }
8c1d1bb8 8164
102890f0
AM
8165 if (elf_section_data (sec)->relocs != relstart)
8166 free (relstart);
8167 }
411e1bfb 8168
663a1470
AM
8169 if (locsyms != NULL
8170 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8171 {
8172 if (!info->keep_memory)
8173 free (locsyms);
8174 else
8175 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8176 }
8177 }
411e1bfb 8178
663a1470
AM
8179 if (toc_ref != NULL)
8180 free (toc_ref);
9a23f96e 8181 htab->do_tls_opt = 1;
b34976b6 8182 return TRUE;
1e2f5b6e 8183}
b34976b6 8184
c5614fa4
AM
8185/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8186 the values of any global symbols in a toc section that has been
8187 edited. Globals in toc sections should be a rarity, so this function
8188 sets a flag if any are found in toc sections other than the one just
de194d85 8189 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8190
8191struct adjust_toc_info
8192{
8193 asection *toc;
8194 unsigned long *skip;
8195 bfd_boolean global_toc_syms;
8196};
8197
ba761f19
AM
8198enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8199
c5614fa4
AM
8200static bfd_boolean
8201adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8202{
8203 struct ppc_link_hash_entry *eh;
8204 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8205 unsigned long i;
c5614fa4 8206
c5614fa4
AM
8207 if (h->root.type != bfd_link_hash_defined
8208 && h->root.type != bfd_link_hash_defweak)
8209 return TRUE;
8210
8211 eh = (struct ppc_link_hash_entry *) h;
8212 if (eh->adjust_done)
8213 return TRUE;
8214
8215 if (eh->elf.root.u.def.section == toc_inf->toc)
8216 {
854b41e7
AM
8217 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8218 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8219 else
854b41e7
AM
8220 i = eh->elf.root.u.def.value >> 3;
8221
ba761f19 8222 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8223 {
4eca0228 8224 _bfd_error_handler
854b41e7
AM
8225 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8226 do
8227 ++i;
ba761f19 8228 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8229 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8230 }
854b41e7
AM
8231
8232 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8233 eh->adjust_done = 1;
8234 }
8235 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8236 toc_inf->global_toc_syms = TRUE;
8237
8238 return TRUE;
8239}
8240
39eeab25
AM
8241/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8242 on a _LO variety toc/got reloc. */
560c8763
AM
8243
8244static bfd_boolean
39eeab25 8245ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8246{
39eeab25
AM
8247 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8248 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
8249 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8250 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8251 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8252 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8253 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8254 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8255 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8256 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8257 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8258 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8259 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8260 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8261 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
8262 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8263 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8264 /* Exclude lfqu by testing reloc. If relocs are ever
8265 defined for the reduced D field in psq_lu then those
8266 will need testing too. */
8267 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8268 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8269 && (insn & 1) == 0)
8270 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8271 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8272 /* Exclude stfqu. psq_stu as above for psq_lu. */
8273 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8274 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8275 && (insn & 1) == 0));
560c8763
AM
8276}
8277
4a421c53
AM
8278/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8279 pld ra,symbol@got@pcrel
dd9b12c2 8280 load/store rt,off(ra)
4a421c53 8281 or
d4b87b1e 8282 pla ra,symbol@pcrel
dd9b12c2 8283 load/store rt,off(ra)
4a421c53 8284 may be translated to
dd9b12c2 8285 pload/pstore rt,symbol+off@pcrel
4a421c53
AM
8286 nop.
8287 This function returns true if the optimization is possible, placing
dd9b12c2 8288 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
4a421c53
AM
8289
8290 On entry to this function, the linker has already determined that
d4b87b1e 8291 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8292 while *PINSN2 is the second instruction. */
8293
8294static bfd_boolean
dd9b12c2 8295xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
4a421c53 8296{
77486630
AM
8297 uint64_t insn1 = *pinsn1;
8298 uint64_t insn2 = *pinsn2;
dd9b12c2 8299 bfd_signed_vma off;
4a421c53 8300
77486630
AM
8301 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8302 {
8303 /* Check that regs match. */
8304 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8305 return FALSE;
8306
8307 /* P8LS or PMLS form, non-pcrel. */
8308 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8309 return FALSE;
8310
8311 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8312 *pinsn2 = PNOP;
8313 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8314 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8315 return TRUE;
8316 }
8317
8318 insn2 >>= 32;
8319
4a421c53 8320 /* Check that regs match. */
77486630 8321 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
4a421c53
AM
8322 return FALSE;
8323
8324 switch ((insn2 >> 26) & 63)
8325 {
8326 default:
8327 return FALSE;
8328
8329 case 32: /* lwz */
8330 case 34: /* lbz */
8331 case 36: /* stw */
8332 case 38: /* stb */
8333 case 40: /* lhz */
8334 case 42: /* lha */
8335 case 44: /* sth */
8336 case 48: /* lfs */
8337 case 50: /* lfd */
8338 case 52: /* stfs */
8339 case 54: /* stfd */
8340 /* These are the PMLS cases, where we just need to tack a prefix
dd9b12c2 8341 on the insn. */
77486630 8342 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
4a421c53 8343 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8344 off = insn2 & 0xffff;
4a421c53
AM
8345 break;
8346
8347 case 58: /* lwa, ld */
dd9b12c2 8348 if ((insn2 & 1) != 0)
4a421c53 8349 return FALSE;
77486630 8350 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8351 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8352 | (insn2 & (31ULL << 21)));
dd9b12c2 8353 off = insn2 & 0xfffc;
4a421c53
AM
8354 break;
8355
8356 case 57: /* lxsd, lxssp */
dd9b12c2 8357 if ((insn2 & 3) < 2)
4a421c53 8358 return FALSE;
77486630 8359 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8360 | ((40ULL | (insn2 & 3)) << 26)
8361 | (insn2 & (31ULL << 21)));
dd9b12c2 8362 off = insn2 & 0xfffc;
4a421c53
AM
8363 break;
8364
8365 case 61: /* stxsd, stxssp, lxv, stxv */
8366 if ((insn2 & 3) == 0)
8367 return FALSE;
8368 else if ((insn2 & 3) >= 2)
8369 {
77486630 8370 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8371 | ((44ULL | (insn2 & 3)) << 26)
8372 | (insn2 & (31ULL << 21)));
dd9b12c2 8373 off = insn2 & 0xfffc;
4a421c53
AM
8374 }
8375 else
8376 {
77486630 8377 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8378 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8379 | (insn2 & (31ULL << 21)));
dd9b12c2 8380 off = insn2 & 0xfff0;
4a421c53
AM
8381 }
8382 break;
8383
8384 case 56: /* lq */
77486630 8385 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53 8386 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8387 off = insn2 & 0xffff;
4a421c53
AM
8388 break;
8389
8390 case 62: /* std, stq */
dd9b12c2 8391 if ((insn2 & 1) != 0)
4a421c53 8392 return FALSE;
77486630 8393 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8394 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8395 | (insn2 & (31ULL << 21)));
dd9b12c2 8396 off = insn2 & 0xfffc;
4a421c53
AM
8397 break;
8398 }
8399
77486630 8400 *pinsn1 = insn1;
4a421c53 8401 *pinsn2 = (uint64_t) NOP << 32;
dd9b12c2 8402 *poff = (off ^ 0x8000) - 0x8000;
4a421c53
AM
8403 return TRUE;
8404}
8405
c5614fa4
AM
8406/* Examine all relocs referencing .toc sections in order to remove
8407 unused .toc entries. */
8408
8409bfd_boolean
33c0ec9d 8410ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8411{
8412 bfd *ibfd;
8413 struct adjust_toc_info toc_inf;
67f0cbdb 8414 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8415
67f0cbdb 8416 htab->do_toc_opt = 1;
c5614fa4 8417 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8418 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8419 {
8420 asection *toc, *sec;
8421 Elf_Internal_Shdr *symtab_hdr;
8422 Elf_Internal_Sym *local_syms;
425b145b 8423 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8424 unsigned long *skip, *drop;
8425 unsigned char *used;
8426 unsigned char *keep, last, some_unused;
8427
854b41e7
AM
8428 if (!is_ppc64_elf (ibfd))
8429 continue;
8430
c5614fa4
AM
8431 toc = bfd_get_section_by_name (ibfd, ".toc");
8432 if (toc == NULL
92b7a70f 8433 || toc->size == 0
dbaa2011
AM
8434 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8435 || discarded_section (toc))
c5614fa4
AM
8436 continue;
8437
425b145b 8438 toc_relocs = NULL;
c5614fa4 8439 local_syms = NULL;
0ffa91dd 8440 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8441
8442 /* Look at sections dropped from the final link. */
8443 skip = NULL;
8444 relstart = NULL;
8445 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8446 {
8447 if (sec->reloc_count == 0
dbaa2011 8448 || !discarded_section (sec)
c5614fa4
AM
8449 || get_opd_info (sec)
8450 || (sec->flags & SEC_ALLOC) == 0
8451 || (sec->flags & SEC_DEBUGGING) != 0)
8452 continue;
8453
8454 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8455 if (relstart == NULL)
8456 goto error_ret;
8457
8458 /* Run through the relocs to see which toc entries might be
8459 unused. */
8460 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8461 {
8462 enum elf_ppc64_reloc_type r_type;
8463 unsigned long r_symndx;
8464 asection *sym_sec;
8465 struct elf_link_hash_entry *h;
8466 Elf_Internal_Sym *sym;
8467 bfd_vma val;
8468
8469 r_type = ELF64_R_TYPE (rel->r_info);
8470 switch (r_type)
8471 {
8472 default:
8473 continue;
8474
8475 case R_PPC64_TOC16:
8476 case R_PPC64_TOC16_LO:
8477 case R_PPC64_TOC16_HI:
8478 case R_PPC64_TOC16_HA:
8479 case R_PPC64_TOC16_DS:
8480 case R_PPC64_TOC16_LO_DS:
8481 break;
8482 }
8483
8484 r_symndx = ELF64_R_SYM (rel->r_info);
8485 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8486 r_symndx, ibfd))
8487 goto error_ret;
8488
8489 if (sym_sec != toc)
8490 continue;
8491
8492 if (h != NULL)
8493 val = h->root.u.def.value;
8494 else
8495 val = sym->st_value;
8496 val += rel->r_addend;
8497
8498 if (val >= toc->size)
8499 continue;
8500
8501 /* Anything in the toc ought to be aligned to 8 bytes.
8502 If not, don't mark as unused. */
8503 if (val & 7)
8504 continue;
8505
8506 if (skip == NULL)
8507 {
854b41e7 8508 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8509 if (skip == NULL)
8510 goto error_ret;
8511 }
8512
ba761f19 8513 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8514 }
8515
8516 if (elf_section_data (sec)->relocs != relstart)
8517 free (relstart);
8518 }
8519
ba761f19
AM
8520 /* For largetoc loads of address constants, we can convert
8521 . addis rx,2,addr@got@ha
8522 . ld ry,addr@got@l(rx)
8523 to
8524 . addis rx,2,addr@toc@ha
8525 . addi ry,rx,addr@toc@l
8526 when addr is within 2G of the toc pointer. This then means
8527 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8528
ba761f19
AM
8529 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8530 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8531 && toc->reloc_count != 0)
8532 {
8533 /* Read toc relocs. */
425b145b
AM
8534 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8535 info->keep_memory);
8536 if (toc_relocs == NULL)
ba761f19
AM
8537 goto error_ret;
8538
425b145b 8539 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8540 {
8541 enum elf_ppc64_reloc_type r_type;
8542 unsigned long r_symndx;
8543 asection *sym_sec;
8544 struct elf_link_hash_entry *h;
8545 Elf_Internal_Sym *sym;
8546 bfd_vma val, addr;
8547
8548 r_type = ELF64_R_TYPE (rel->r_info);
8549 if (r_type != R_PPC64_ADDR64)
8550 continue;
8551
8552 r_symndx = ELF64_R_SYM (rel->r_info);
8553 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8554 r_symndx, ibfd))
8555 goto error_ret;
8556
425b145b 8557 if (sym_sec == NULL
c27b8c2a 8558 || sym_sec->output_section == NULL
dbaa2011 8559 || discarded_section (sym_sec))
425b145b
AM
8560 continue;
8561
afe397ea 8562 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8563 continue;
8564
8565 if (h != NULL)
bddc25c9
AM
8566 {
8567 if (h->type == STT_GNU_IFUNC)
8568 continue;
8569 val = h->root.u.def.value;
8570 }
ba761f19 8571 else
bddc25c9
AM
8572 {
8573 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8574 continue;
8575 val = sym->st_value;
8576 }
ba761f19
AM
8577 val += rel->r_addend;
8578 val += sym_sec->output_section->vma + sym_sec->output_offset;
8579
8580 /* We don't yet know the exact toc pointer value, but we
8581 know it will be somewhere in the toc section. Don't
8582 optimize if the difference from any possible toc
8583 pointer is outside [ff..f80008000, 7fff7fff]. */
8584 addr = toc->output_section->vma + TOC_BASE_OFF;
8585 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8586 continue;
8587
8588 addr = toc->output_section->vma + toc->output_section->rawsize;
8589 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8590 continue;
8591
8592 if (skip == NULL)
8593 {
8594 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8595 if (skip == NULL)
8596 goto error_ret;
8597 }
8598
8599 skip[rel->r_offset >> 3]
425b145b 8600 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8601 }
ba761f19
AM
8602 }
8603
c5614fa4
AM
8604 if (skip == NULL)
8605 continue;
8606
8607 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8608 if (used == NULL)
8609 {
8610 error_ret:
8611 if (local_syms != NULL
8612 && symtab_hdr->contents != (unsigned char *) local_syms)
8613 free (local_syms);
8614 if (sec != NULL
8615 && relstart != NULL
8616 && elf_section_data (sec)->relocs != relstart)
8617 free (relstart);
425b145b
AM
8618 if (toc_relocs != NULL
8619 && elf_section_data (toc)->relocs != toc_relocs)
8620 free (toc_relocs);
c5614fa4
AM
8621 if (skip != NULL)
8622 free (skip);
8623 return FALSE;
8624 }
8625
30038c59
AM
8626 /* Now check all kept sections that might reference the toc.
8627 Check the toc itself last. */
8628 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8629 : ibfd->sections);
c5614fa4 8630 sec != NULL;
c5614fa4 8631 sec = (sec == toc ? NULL
c5614fa4 8632 : sec->next == NULL ? toc
30038c59 8633 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8634 : sec->next))
8635 {
8636 int repeat;
8637
8638 if (sec->reloc_count == 0
dbaa2011 8639 || discarded_section (sec)
c5614fa4
AM
8640 || get_opd_info (sec)
8641 || (sec->flags & SEC_ALLOC) == 0
8642 || (sec->flags & SEC_DEBUGGING) != 0)
8643 continue;
8644
854b41e7
AM
8645 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8646 info->keep_memory);
c5614fa4 8647 if (relstart == NULL)
2915c55b
JK
8648 {
8649 free (used);
8650 goto error_ret;
8651 }
c5614fa4
AM
8652
8653 /* Mark toc entries referenced as used. */
c5614fa4 8654 do
d4f1ee75
AM
8655 {
8656 repeat = 0;
8657 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8658 {
8659 enum elf_ppc64_reloc_type r_type;
8660 unsigned long r_symndx;
8661 asection *sym_sec;
8662 struct elf_link_hash_entry *h;
8663 Elf_Internal_Sym *sym;
8664 bfd_vma val;
98528052 8665
d4f1ee75 8666 r_type = ELF64_R_TYPE (rel->r_info);
d4f1ee75
AM
8667 switch (r_type)
8668 {
8669 case R_PPC64_TOC16:
8670 case R_PPC64_TOC16_LO:
8671 case R_PPC64_TOC16_HI:
8672 case R_PPC64_TOC16_HA:
8673 case R_PPC64_TOC16_DS:
8674 case R_PPC64_TOC16_LO_DS:
8675 /* In case we're taking addresses of toc entries. */
8676 case R_PPC64_ADDR64:
8677 break;
c5614fa4 8678
d4f1ee75
AM
8679 default:
8680 continue;
8681 }
c5614fa4 8682
d4f1ee75
AM
8683 r_symndx = ELF64_R_SYM (rel->r_info);
8684 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8685 r_symndx, ibfd))
8686 {
8687 free (used);
8688 goto error_ret;
8689 }
c5614fa4 8690
d4f1ee75
AM
8691 if (sym_sec != toc)
8692 continue;
c5614fa4 8693
d4f1ee75
AM
8694 if (h != NULL)
8695 val = h->root.u.def.value;
8696 else
8697 val = sym->st_value;
8698 val += rel->r_addend;
ba761f19 8699
d4f1ee75
AM
8700 if (val >= toc->size)
8701 continue;
ba761f19 8702
d4f1ee75
AM
8703 if ((skip[val >> 3] & can_optimize) != 0)
8704 {
8705 bfd_vma off;
8706 unsigned char opc;
8707
8708 switch (r_type)
8709 {
8710 case R_PPC64_TOC16_HA:
ba761f19 8711 break;
ba761f19 8712
d4f1ee75
AM
8713 case R_PPC64_TOC16_LO_DS:
8714 off = rel->r_offset;
8715 off += (bfd_big_endian (ibfd) ? -2 : 3);
8716 if (!bfd_get_section_contents (ibfd, sec, &opc,
8717 off, 1))
8718 {
8719 free (used);
8720 goto error_ret;
8721 }
8722 if ((opc & (0x3f << 2)) == (58u << 2))
8723 break;
1a0670f3 8724 /* Fall through. */
ba761f19 8725
d4f1ee75
AM
8726 default:
8727 /* Wrong sort of reloc, or not a ld. We may
8728 as well clear ref_from_discarded too. */
8729 skip[val >> 3] = 0;
8730 }
8731 }
8732
8733 if (sec != toc)
8734 used[val >> 3] = 1;
8735 /* For the toc section, we only mark as used if this
8736 entry itself isn't unused. */
8737 else if ((used[rel->r_offset >> 3]
8738 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8739 && !used[val >> 3])
8740 {
8741 /* Do all the relocs again, to catch reference
8742 chains. */
8743 repeat = 1;
8744 used[val >> 3] = 1;
8745 }
8746 }
8747 }
c5614fa4 8748 while (repeat);
854b41e7
AM
8749
8750 if (elf_section_data (sec)->relocs != relstart)
8751 free (relstart);
c5614fa4
AM
8752 }
8753
8754 /* Merge the used and skip arrays. Assume that TOC
8755 doublewords not appearing as either used or unused belong
de194d85 8756 to an entry more than one doubleword in size. */
c5614fa4
AM
8757 for (drop = skip, keep = used, last = 0, some_unused = 0;
8758 drop < skip + (toc->size + 7) / 8;
8759 ++drop, ++keep)
8760 {
8761 if (*keep)
8762 {
ba761f19
AM
8763 *drop &= ~ref_from_discarded;
8764 if ((*drop & can_optimize) != 0)
8765 some_unused = 1;
c5614fa4
AM
8766 last = 0;
8767 }
b140b010 8768 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8769 {
8770 some_unused = 1;
ba761f19 8771 last = ref_from_discarded;
c5614fa4
AM
8772 }
8773 else
8774 *drop = last;
8775 }
8776
8777 free (used);
8778
8779 if (some_unused)
8780 {
8781 bfd_byte *contents, *src;
8782 unsigned long off;
d62b3684 8783 Elf_Internal_Sym *sym;
ba761f19 8784 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8785
8786 /* Shuffle the toc contents, and at the same time convert the
8787 skip array from booleans into offsets. */
8788 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8789 goto error_ret;
8790
8791 elf_section_data (toc)->this_hdr.contents = contents;
8792
8793 for (src = contents, off = 0, drop = skip;
8794 src < contents + toc->size;
8795 src += 8, ++drop)
8796 {
ba761f19
AM
8797 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8798 off += 8;
c5614fa4
AM
8799 else if (off != 0)
8800 {
8801 *drop = off;
8802 memcpy (src - off, src, 8);
8803 }
8804 }
854b41e7 8805 *drop = off;
c5614fa4
AM
8806 toc->rawsize = toc->size;
8807 toc->size = src - contents - off;
8808
ba761f19
AM
8809 /* Adjust addends for relocs against the toc section sym,
8810 and optimize any accesses we can. */
c5614fa4
AM
8811 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8812 {
8813 if (sec->reloc_count == 0
dbaa2011 8814 || discarded_section (sec))
c5614fa4
AM
8815 continue;
8816
8817 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8818 info->keep_memory);
c5614fa4
AM
8819 if (relstart == NULL)
8820 goto error_ret;
8821
8822 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8823 {
8824 enum elf_ppc64_reloc_type r_type;
8825 unsigned long r_symndx;
8826 asection *sym_sec;
8827 struct elf_link_hash_entry *h;
854b41e7 8828 bfd_vma val;
c5614fa4
AM
8829
8830 r_type = ELF64_R_TYPE (rel->r_info);
8831 switch (r_type)
8832 {
8833 default:
8834 continue;
8835
8836 case R_PPC64_TOC16:
8837 case R_PPC64_TOC16_LO:
8838 case R_PPC64_TOC16_HI:
8839 case R_PPC64_TOC16_HA:
8840 case R_PPC64_TOC16_DS:
8841 case R_PPC64_TOC16_LO_DS:
8842 case R_PPC64_ADDR64:
8843 break;
8844 }
8845
8846 r_symndx = ELF64_R_SYM (rel->r_info);
8847 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8848 r_symndx, ibfd))
8849 goto error_ret;
8850
ba761f19 8851 if (sym_sec != toc)
c5614fa4
AM
8852 continue;
8853
ba761f19
AM
8854 if (h != NULL)
8855 val = h->root.u.def.value;
8856 else
8857 {
8858 val = sym->st_value;
8859 if (val != 0)
8860 local_toc_syms = TRUE;
8861 }
8862
8863 val += rel->r_addend;
854b41e7
AM
8864
8865 if (val > toc->rawsize)
8866 val = toc->rawsize;
ba761f19
AM
8867 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8868 continue;
8869 else if ((skip[val >> 3] & can_optimize) != 0)
8870 {
8871 Elf_Internal_Rela *tocrel
425b145b 8872 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8873 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8874
8875 switch (r_type)
8876 {
8877 case R_PPC64_TOC16_HA:
8878 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8879 break;
8880
8881 case R_PPC64_TOC16_LO_DS:
8882 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8883 break;
8884
8885 default:
28942f62
AM
8886 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8887 ppc_howto_init ();
b140b010 8888 info->callbacks->einfo
695344c0 8889 /* xgettext:c-format */
174d0a74 8890 (_("%H: %s references "
b140b010
AM
8891 "optimized away TOC entry\n"),
8892 ibfd, sec, rel->r_offset,
8893 ppc64_elf_howto_table[r_type]->name);
8894 bfd_set_error (bfd_error_bad_value);
8895 goto error_ret;
ba761f19
AM
8896 }
8897 rel->r_addend = tocrel->r_addend;
8898 elf_section_data (sec)->relocs = relstart;
8899 continue;
8900 }
8901
8902 if (h != NULL || sym->st_value != 0)
8903 continue;
854b41e7
AM
8904
8905 rel->r_addend -= skip[val >> 3];
8906 elf_section_data (sec)->relocs = relstart;
c5614fa4 8907 }
854b41e7
AM
8908
8909 if (elf_section_data (sec)->relocs != relstart)
8910 free (relstart);
c5614fa4
AM
8911 }
8912
8913 /* We shouldn't have local or global symbols defined in the TOC,
8914 but handle them anyway. */
df22d223
AM
8915 if (local_syms != NULL)
8916 for (sym = local_syms;
8917 sym < local_syms + symtab_hdr->sh_info;
8918 ++sym)
8919 if (sym->st_value != 0
8920 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8921 {
8922 unsigned long i;
854b41e7 8923
df22d223
AM
8924 if (sym->st_value > toc->rawsize)
8925 i = toc->rawsize >> 3;
8926 else
8927 i = sym->st_value >> 3;
854b41e7 8928
df22d223
AM
8929 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8930 {
8931 if (local_toc_syms)
4eca0228 8932 _bfd_error_handler
df22d223
AM
8933 (_("%s defined on removed toc entry"),
8934 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8935 do
8936 ++i;
8937 while ((skip[i] & (ref_from_discarded | can_optimize)));
8938 sym->st_value = (bfd_vma) i << 3;
8939 }
d62b3684 8940
df22d223
AM
8941 sym->st_value -= skip[i];
8942 symtab_hdr->contents = (unsigned char *) local_syms;
8943 }
c5614fa4 8944
854b41e7 8945 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8946 if (toc_inf.global_toc_syms)
8947 {
8948 toc_inf.toc = toc;
8949 toc_inf.skip = skip;
8950 toc_inf.global_toc_syms = FALSE;
8951 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8952 &toc_inf);
8953 }
854b41e7
AM
8954
8955 if (toc->reloc_count != 0)
8956 {
d4730f92 8957 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8958 Elf_Internal_Rela *wrel;
8959 bfd_size_type sz;
8960
854b41e7 8961 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8962 if (toc_relocs == NULL)
8963 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8964 info->keep_memory);
8965 if (toc_relocs == NULL)
8966 goto error_ret;
8967
425b145b
AM
8968 wrel = toc_relocs;
8969 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8970 if ((skip[rel->r_offset >> 3]
8971 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8972 {
8973 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8974 wrel->r_info = rel->r_info;
8975 wrel->r_addend = rel->r_addend;
8976 ++wrel;
8977 }
8978 else if (!dec_dynrel_count (rel->r_info, toc, info,
8979 &local_syms, NULL, NULL))
8980 goto error_ret;
8981
425b145b
AM
8982 elf_section_data (toc)->relocs = toc_relocs;
8983 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8984 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8985 sz = rel_hdr->sh_entsize;
8986 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8987 }
c5614fa4 8988 }
28be611c
AM
8989 else if (toc_relocs != NULL
8990 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8991 free (toc_relocs);
c5614fa4
AM
8992
8993 if (local_syms != NULL
8994 && symtab_hdr->contents != (unsigned char *) local_syms)
8995 {
8996 if (!info->keep_memory)
8997 free (local_syms);
8998 else
8999 symtab_hdr->contents = (unsigned char *) local_syms;
9000 }
9001 free (skip);
9002 }
9003
066f4018 9004 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
9005 a GOT relative or PC relative access, possibly reducing the
9006 number of GOT entries. */
066f4018
AM
9007 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9008 {
9009 asection *sec;
9010 Elf_Internal_Shdr *symtab_hdr;
9011 Elf_Internal_Sym *local_syms;
9012 Elf_Internal_Rela *relstart, *rel;
9013 bfd_vma got;
9014
9015 if (!is_ppc64_elf (ibfd))
9016 continue;
9017
903b777d 9018 if (!ppc64_elf_tdata (ibfd)->has_optrel)
066f4018
AM
9019 continue;
9020
9021 sec = ppc64_elf_tdata (ibfd)->got;
903b777d
AM
9022 got = 0;
9023 if (sec != NULL)
9024 got = sec->output_section->vma + sec->output_offset + 0x8000;
066f4018
AM
9025
9026 local_syms = NULL;
9027 symtab_hdr = &elf_symtab_hdr (ibfd);
9028
9029 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9030 {
9031 if (sec->reloc_count == 0
903b777d 9032 || !ppc64_elf_section_data (sec)->has_optrel
066f4018
AM
9033 || discarded_section (sec))
9034 continue;
9035
9036 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9037 info->keep_memory);
9038 if (relstart == NULL)
9039 {
9040 got_error_ret:
9041 if (local_syms != NULL
9042 && symtab_hdr->contents != (unsigned char *) local_syms)
9043 free (local_syms);
9044 if (sec != NULL
9045 && relstart != NULL
9046 && elf_section_data (sec)->relocs != relstart)
9047 free (relstart);
9048 return FALSE;
9049 }
9050
9051 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9052 {
9053 enum elf_ppc64_reloc_type r_type;
9054 unsigned long r_symndx;
9055 Elf_Internal_Sym *sym;
9056 asection *sym_sec;
9057 struct elf_link_hash_entry *h;
9058 struct got_entry *ent;
133a1f60 9059 bfd_vma val, pc;
4a421c53 9060 unsigned char buf[8];
066f4018 9061 unsigned int insn;
903b777d 9062 enum {no_check, check_lo, check_ha} insn_check;
066f4018
AM
9063
9064 r_type = ELF64_R_TYPE (rel->r_info);
903b777d
AM
9065 switch (r_type)
9066 {
9067 default:
9068 insn_check = no_check;
9069 break;
9070
9071 case R_PPC64_PLT16_HA:
9072 case R_PPC64_GOT_TLSLD16_HA:
9073 case R_PPC64_GOT_TLSGD16_HA:
9074 case R_PPC64_GOT_TPREL16_HA:
9075 case R_PPC64_GOT_DTPREL16_HA:
9076 case R_PPC64_GOT16_HA:
9077 case R_PPC64_TOC16_HA:
9078 insn_check = check_ha;
9079 break;
9080
9081 case R_PPC64_PLT16_LO:
9082 case R_PPC64_PLT16_LO_DS:
9083 case R_PPC64_GOT_TLSLD16_LO:
9084 case R_PPC64_GOT_TLSGD16_LO:
9085 case R_PPC64_GOT_TPREL16_LO_DS:
9086 case R_PPC64_GOT_DTPREL16_LO_DS:
9087 case R_PPC64_GOT16_LO:
9088 case R_PPC64_GOT16_LO_DS:
9089 case R_PPC64_TOC16_LO:
9090 case R_PPC64_TOC16_LO_DS:
9091 insn_check = check_lo;
9092 break;
9093 }
9094
9095 if (insn_check != no_check)
9096 {
9097 bfd_vma off = rel->r_offset & ~3;
9098
9099 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9100 goto got_error_ret;
9101
9102 insn = bfd_get_32 (ibfd, buf);
9103 if (insn_check == check_lo
9104 ? !ok_lo_toc_insn (insn, r_type)
9105 : ((insn & ((0x3f << 26) | 0x1f << 16))
9106 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9107 {
9108 char str[12];
9109
9110 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9111 sprintf (str, "%#08x", insn);
9112 info->callbacks->einfo
9113 /* xgettext:c-format */
9114 (_("%H: got/toc optimization is not supported for"
9115 " %s instruction\n"),
9116 ibfd, sec, rel->r_offset & ~3, str);
9117 continue;
9118 }
9119 }
9120
066f4018
AM
9121 switch (r_type)
9122 {
bb22a418
AM
9123 /* Note that we don't delete GOT entries for
9124 R_PPC64_GOT16_DS since we'd need a lot more
9125 analysis. For starters, the preliminary layout is
9126 before the GOT, PLT, dynamic sections and stubs are
9127 laid out. Then we'd need to allow for changes in
9128 distance between sections caused by alignment. */
066f4018
AM
9129 default:
9130 continue;
9131
066f4018
AM
9132 case R_PPC64_GOT16_HA:
9133 case R_PPC64_GOT16_LO_DS:
4a421c53 9134 case R_PPC64_GOT_PCREL34:
066f4018
AM
9135 break;
9136 }
9137
9138 r_symndx = ELF64_R_SYM (rel->r_info);
9139 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9140 r_symndx, ibfd))
9141 goto got_error_ret;
9142
6d5554a6
AM
9143 if (sym_sec == NULL
9144 || sym_sec->output_section == NULL
9145 || discarded_section (sym_sec))
9146 continue;
9147
066f4018
AM
9148 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9149 continue;
9150
9151 if (h != NULL)
9152 val = h->root.u.def.value;
9153 else
9154 val = sym->st_value;
133a1f60 9155 val += rel->r_addend;
066f4018
AM
9156 val += sym_sec->output_section->vma + sym_sec->output_offset;
9157
bb22a418
AM
9158/* Fudge factor to allow for the fact that the preliminary layout
9159 isn't exact. Reduce limits by this factor. */
9160#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9161
066f4018
AM
9162 switch (r_type)
9163 {
9164 default:
9165 continue;
9166
066f4018 9167 case R_PPC64_GOT16_HA:
bb22a418
AM
9168 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9169 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9170 continue;
9171
9172 if (!bfd_get_section_contents (ibfd, sec, buf,
9173 rel->r_offset & ~3, 4))
9174 goto got_error_ret;
9175 insn = bfd_get_32 (ibfd, buf);
9176 if (((insn & ((0x3f << 26) | 0x1f << 16))
9177 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9178 continue;
9179 break;
9180
9181 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9182 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9183 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9184 continue;
9185 if (!bfd_get_section_contents (ibfd, sec, buf,
9186 rel->r_offset & ~3, 4))
9187 goto got_error_ret;
9188 insn = bfd_get_32 (ibfd, buf);
9189 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9190 continue;
9191 break;
4a421c53
AM
9192
9193 case R_PPC64_GOT_PCREL34:
9194 pc = rel->r_offset;
9195 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9196 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9197 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9198 continue;
9199 if (!bfd_get_section_contents (ibfd, sec, buf,
9200 rel->r_offset & ~3, 8))
9201 goto got_error_ret;
9202 insn = bfd_get_32 (ibfd, buf);
9203 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9204 continue;
9205 insn = bfd_get_32 (ibfd, buf + 4);
9206 if ((insn & (0x3f << 26)) != 57u << 26)
9207 continue;
9208 break;
066f4018 9209 }
bb22a418 9210#undef LIMIT_ADJUST
066f4018
AM
9211
9212 if (h != NULL)
9213 ent = h->got.glist;
9214 else
9215 {
9216 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9217 ent = local_got_ents[r_symndx];
9218 }
9219 for (; ent != NULL; ent = ent->next)
133a1f60 9220 if (ent->addend == rel->r_addend
066f4018
AM
9221 && ent->owner == ibfd
9222 && ent->tls_type == 0)
9223 break;
9224 BFD_ASSERT (ent && ent->got.refcount > 0);
9225 ent->got.refcount -= 1;
9226 }
9227
9228 if (elf_section_data (sec)->relocs != relstart)
9229 free (relstart);
9230 }
9231
9232 if (local_syms != NULL
9233 && symtab_hdr->contents != (unsigned char *) local_syms)
9234 {
9235 if (!info->keep_memory)
9236 free (local_syms);
9237 else
9238 symtab_hdr->contents = (unsigned char *) local_syms;
9239 }
9240 }
9241
c5614fa4
AM
9242 return TRUE;
9243}
9244
1bbe0902
AM
9245/* Return true iff input section I references the TOC using
9246 instructions limited to +/-32k offsets. */
9247
9248bfd_boolean
9249ppc64_elf_has_small_toc_reloc (asection *i)
9250{
9251 return (is_ppc64_elf (i->owner)
9252 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9253}
9254
927be08e
AM
9255/* Allocate space for one GOT entry. */
9256
9257static void
9258allocate_got (struct elf_link_hash_entry *h,
9259 struct bfd_link_info *info,
9260 struct got_entry *gent)
9261{
9262 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9263 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9264 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9265 ? 16 : 8);
9266 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9267 ? 2 : 1) * sizeof (Elf64_External_Rela);
9268 asection *got = ppc64_elf_tdata (gent->owner)->got;
9269
9270 gent->got.offset = got->size;
9271 got->size += entsize;
9272
19e08130 9273 if (h->type == STT_GNU_IFUNC)
927be08e 9274 {
33e44f2e 9275 htab->elf.irelplt->size += rentsize;
19e08130 9276 htab->got_reli_size += rentsize;
927be08e 9277 }
f15d0b54 9278 else if (((bfd_link_pic (info)
f749f26e 9279 && !(gent->tls_type != 0
f15d0b54
AM
9280 && bfd_link_executable (info)
9281 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9282 || (htab->elf.dynamic_sections_created
9283 && h->dynindx != -1
9284 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9285 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9286 {
19e08130 9287 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9288 relgot->size += rentsize;
927be08e
AM
9289 }
9290}
9291
7865406b
AM
9292/* This function merges got entries in the same toc group. */
9293
9294static void
9295merge_got_entries (struct got_entry **pent)
9296{
9297 struct got_entry *ent, *ent2;
9298
9299 for (ent = *pent; ent != NULL; ent = ent->next)
9300 if (!ent->is_indirect)
9301 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9302 if (!ent2->is_indirect
9303 && ent2->addend == ent->addend
9304 && ent2->tls_type == ent->tls_type
9305 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9306 {
9307 ent2->is_indirect = TRUE;
9308 ent2->got.ent = ent;
9309 }
9310}
9311
46434633 9312/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9313
9314static bfd_boolean
46434633
AM
9315ensure_undef_dynamic (struct bfd_link_info *info,
9316 struct elf_link_hash_entry *h)
f0158f44
AM
9317{
9318 struct elf_link_hash_table *htab = elf_hash_table (info);
9319
9320 if (htab->dynamic_sections_created
46434633
AM
9321 && ((info->dynamic_undefined_weak != 0
9322 && h->root.type == bfd_link_hash_undefweak)
9323 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9324 && h->dynindx == -1
9325 && !h->forced_local
9326 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9327 return bfd_elf_link_record_dynamic_symbol (info, h);
9328 return TRUE;
9329}
9330
65f38f15
AM
9331/* Allocate space in .plt, .got and associated reloc sections for
9332 dynamic relocs. */
5bd4f169 9333
b34976b6 9334static bfd_boolean
4ce794b7 9335allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9336{
65f38f15
AM
9337 struct bfd_link_info *info;
9338 struct ppc_link_hash_table *htab;
5bd4f169 9339 asection *s;
65f38f15 9340 struct ppc_link_hash_entry *eh;
0b8bcf0d 9341 struct got_entry **pgent, *gent;
5bd4f169 9342
e92d460e 9343 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9344 return TRUE;
5bd4f169 9345
65f38f15
AM
9346 info = (struct bfd_link_info *) inf;
9347 htab = ppc_hash_table (info);
4dfe6ac6
NC
9348 if (htab == NULL)
9349 return FALSE;
5bd4f169 9350
951fd09b
AM
9351 eh = (struct ppc_link_hash_entry *) h;
9352 /* Run through the TLS GD got entries first if we're changing them
9353 to TPREL. */
b00a0a86 9354 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
951fd09b
AM
9355 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9356 if (gent->got.refcount > 0
9357 && (gent->tls_type & TLS_GD) != 0)
9358 {
9359 /* This was a GD entry that has been converted to TPREL. If
9360 there happens to be a TPREL entry we can use that one. */
9361 struct got_entry *ent;
9362 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9363 if (ent->got.refcount > 0
9364 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9365 && ent->addend == gent->addend
9366 && ent->owner == gent->owner)
951fd09b
AM
9367 {
9368 gent->got.refcount = 0;
9369 break;
9370 }
9371
9372 /* If not, then we'll be using our own TPREL entry. */
9373 if (gent->got.refcount != 0)
9374 gent->tls_type = TLS_TLS | TLS_TPREL;
9375 }
9376
7865406b
AM
9377 /* Remove any list entry that won't generate a word in the GOT before
9378 we call merge_got_entries. Otherwise we risk merging to empty
9379 entries. */
0b8bcf0d
AM
9380 pgent = &h->got.glist;
9381 while ((gent = *pgent) != NULL)
411e1bfb 9382 if (gent->got.refcount > 0)
7865406b
AM
9383 {
9384 if ((gent->tls_type & TLS_LD) != 0
f749f26e 9385 && SYMBOL_REFERENCES_LOCAL (info, h))
7865406b
AM
9386 {
9387 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9388 *pgent = gent->next;
9389 }
9390 else
9391 pgent = &gent->next;
9392 }
9393 else
9394 *pgent = gent->next;
9395
9396 if (!htab->do_multi_toc)
9397 merge_got_entries (&h->got.glist);
9398
9399 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9400 if (!gent->is_indirect)
411e1bfb 9401 {
ec73ddcd
AM
9402 /* Ensure we catch all the cases where this symbol should
9403 be made dynamic. */
46434633 9404 if (!ensure_undef_dynamic (info, h))
f0158f44 9405 return FALSE;
65f38f15 9406
0c8d6e5c 9407 if (!is_ppc64_elf (gent->owner))
927be08e 9408 abort ();
0ffa91dd 9409
927be08e 9410 allocate_got (h, info, gent);
411e1bfb 9411 }
65f38f15 9412
954b63d4
AM
9413 /* If no dynamic sections we can't have dynamic relocs, except for
9414 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9415 if (!htab->elf.dynamic_sections_created
9416 && h->type != STT_GNU_IFUNC)
9417 eh->dyn_relocs = NULL;
9418
529fe20e
AM
9419 /* Discard relocs on undefined symbols that must be local. */
9420 else if (h->root.type == bfd_link_hash_undefined
9421 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9422 eh->dyn_relocs = NULL;
9423
954b63d4
AM
9424 /* Also discard relocs on undefined weak syms with non-default
9425 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9426 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
9427 eh->dyn_relocs = NULL;
9428
8a2058b5 9429 if (eh->dyn_relocs != NULL)
65f38f15 9430 {
8a2058b5
AM
9431 struct elf_dyn_relocs *p, **pp;
9432
57e7d118
AM
9433 /* In the shared -Bsymbolic case, discard space allocated for
9434 dynamic pc-relative relocs against symbols which turn out to
9435 be defined in regular objects. For the normal shared case,
9436 discard space for relocs that have become local due to symbol
9437 visibility changes. */
57e7d118 9438 if (bfd_link_pic (info))
65f38f15 9439 {
57e7d118
AM
9440 /* Relocs that use pc_count are those that appear on a call
9441 insn, or certain REL relocs (see must_be_dyn_reloc) that
9442 can be generated via assembly. We want calls to
9443 protected symbols to resolve directly to the function
9444 rather than going via the plt. If people want function
9445 pointer comparisons to work as expected then they should
9446 avoid writing weird assembly. */
9447 if (SYMBOL_CALLS_LOCAL (info, h))
9448 {
57e7d118
AM
9449 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9450 {
9451 p->count -= p->pc_count;
9452 p->pc_count = 0;
9453 if (p->count == 0)
9454 *pp = p->next;
9455 else
9456 pp = &p->next;
9457 }
9458 }
65f38f15 9459
954b63d4 9460 if (eh->dyn_relocs != NULL)
5bd4f169 9461 {
ec73ddcd
AM
9462 /* Ensure we catch all the cases where this symbol
9463 should be made dynamic. */
46434633 9464 if (!ensure_undef_dynamic (info, h))
f0158f44 9465 return FALSE;
5bd4f169 9466 }
65f38f15 9467 }
ec73ddcd
AM
9468
9469 /* For a fixed position executable, discard space for
9470 relocs against symbols which are not dynamic. */
9471 else if (h->type != STT_GNU_IFUNC)
57e7d118 9472 {
529fe20e
AM
9473 if (h->dynamic_adjusted
9474 && !h->def_regular
9475 && !ELF_COMMON_DEF_P (h))
f0158f44 9476 {
ec73ddcd
AM
9477 /* Ensure we catch all the cases where this symbol
9478 should be made dynamic. */
46434633 9479 if (!ensure_undef_dynamic (info, h))
f0158f44 9480 return FALSE;
dfbb6ac9 9481
ec73ddcd 9482 /* But if that didn't work out, discard dynamic relocs. */
f0158f44
AM
9483 if (h->dynindx == -1)
9484 eh->dyn_relocs = NULL;
9485 }
9486 else
8a2058b5 9487 eh->dyn_relocs = NULL;
57e7d118
AM
9488 }
9489
9490 /* Finally, allocate space. */
9491 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9492 {
9493 asection *sreloc = elf_section_data (p->sec)->sreloc;
9494 if (eh->elf.type == STT_GNU_IFUNC)
9495 sreloc = htab->elf.irelplt;
9496 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9497 }
65f38f15 9498 }
57e7d118 9499
2d7ad24e
AM
9500 /* We might need a PLT entry when the symbol
9501 a) is dynamic, or
9502 b) is an ifunc, or
9503 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9504 d) has plt16 relocs and we are linking statically. */
9505 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9506 || h->type == STT_GNU_IFUNC
9507 || (h->needs_plt && h->dynamic_adjusted)
9508 || (h->needs_plt
9509 && h->def_regular
9510 && !htab->elf.dynamic_sections_created
3e04d765 9511 && !htab->can_convert_all_inline_plt
2d7ad24e
AM
9512 && (((struct ppc_link_hash_entry *) h)->tls_mask
9513 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 9514 {
57e7d118
AM
9515 struct plt_entry *pent;
9516 bfd_boolean doneone = FALSE;
9517 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9518 if (pent->plt.refcount > 0)
9519 {
9520 if (!htab->elf.dynamic_sections_created
9521 || h->dynindx == -1)
9522 {
2d7ad24e
AM
9523 if (h->type == STT_GNU_IFUNC)
9524 {
9525 s = htab->elf.iplt;
9526 pent->plt.offset = s->size;
9527 s->size += PLT_ENTRY_SIZE (htab);
9528 s = htab->elf.irelplt;
9529 }
9530 else
9531 {
9532 s = htab->pltlocal;
9533 pent->plt.offset = s->size;
9534 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9535 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9536 }
57e7d118
AM
9537 }
9538 else
9539 {
9540 /* If this is the first .plt entry, make room for the special
9541 first entry. */
9542 s = htab->elf.splt;
9543 if (s->size == 0)
9544 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9545
57e7d118 9546 pent->plt.offset = s->size;
65f38f15 9547
57e7d118
AM
9548 /* Make room for this entry. */
9549 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9550
57e7d118
AM
9551 /* Make room for the .glink code. */
9552 s = htab->glink;
9553 if (s->size == 0)
9e390558 9554 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9555 if (htab->opd_abi)
9556 {
9557 /* We need bigger stubs past index 32767. */
9e390558 9558 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9559 s->size += 4;
9560 s->size += 2*4;
9561 }
9562 else
9563 s->size += 4;
65f38f15 9564
57e7d118
AM
9565 /* We also need to make an entry in the .rela.plt section. */
9566 s = htab->elf.srelplt;
9567 }
2d7ad24e
AM
9568 if (s != NULL)
9569 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
9570 doneone = TRUE;
9571 }
9572 else
9573 pent->plt.offset = (bfd_vma) -1;
9574 if (!doneone)
9575 {
9576 h->plt.plist = NULL;
9577 h->needs_plt = 0;
9578 }
65f38f15 9579 }
57e7d118 9580 else
65f38f15 9581 {
57e7d118
AM
9582 h->plt.plist = NULL;
9583 h->needs_plt = 0;
65f38f15
AM
9584 }
9585
b34976b6 9586 return TRUE;
65f38f15
AM
9587}
9588
9e390558
AM
9589#define PPC_LO(v) ((v) & 0xffff)
9590#define PPC_HI(v) (((v) >> 16) & 0xffff)
9591#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
9592#define D34(v) \
9593 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9594#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 9595
a345bc8d
AM
9596/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9597 to set up space for global entry stubs. These are put in glink,
9598 after the branch table. */
65f38f15 9599
b34976b6 9600static bfd_boolean
a345bc8d 9601size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9602{
a345bc8d
AM
9603 struct bfd_link_info *info;
9604 struct ppc_link_hash_table *htab;
9605 struct plt_entry *pent;
9e390558 9606 asection *s, *plt;
65f38f15 9607
a345bc8d
AM
9608 if (h->root.type == bfd_link_hash_indirect)
9609 return TRUE;
65f38f15 9610
a345bc8d
AM
9611 if (!h->pointer_equality_needed)
9612 return TRUE;
65f38f15 9613
a345bc8d
AM
9614 if (h->def_regular)
9615 return TRUE;
65f38f15 9616
a345bc8d
AM
9617 info = inf;
9618 htab = ppc_hash_table (info);
9619 if (htab == NULL)
9620 return FALSE;
9621
9e390558
AM
9622 s = htab->global_entry;
9623 plt = htab->elf.splt;
a345bc8d
AM
9624 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9625 if (pent->plt.offset != (bfd_vma) -1
9626 && pent->addend == 0)
9627 {
afe397ea
AM
9628 /* For ELFv2, if this symbol is not defined in a regular file
9629 and we are not generating a shared library or pie, then we
9630 need to define the symbol in the executable on a call stub.
9631 This is to avoid text relocations. */
9e390558
AM
9632 bfd_vma off, stub_align, stub_off, stub_size;
9633 unsigned int align_power;
9634
9635 stub_size = 16;
9636 stub_off = s->size;
9637 if (htab->params->plt_stub_align >= 0)
9638 align_power = htab->params->plt_stub_align;
9639 else
9640 align_power = -htab->params->plt_stub_align;
9641 /* Setting section alignment is delayed until we know it is
9642 non-empty. Otherwise the .text output section will be
9643 aligned at least to plt_stub_align even when no global
9644 entry stubs are needed. */
9645 if (s->alignment_power < align_power)
9646 s->alignment_power = align_power;
9647 stub_align = (bfd_vma) 1 << align_power;
9648 if (htab->params->plt_stub_align >= 0
9649 || ((((stub_off + stub_size - 1) & -stub_align)
9650 - (stub_off & -stub_align))
9651 > ((stub_size - 1) & -stub_align)))
9652 stub_off = (stub_off + stub_align - 1) & -stub_align;
9653 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9654 off -= stub_off + s->output_offset + s->output_section->vma;
9655 /* Note that for --plt-stub-align negative we have a possible
9656 dependency between stub offset and size. Break that
9657 dependency by assuming the max stub size when calculating
9658 the stub offset. */
9659 if (PPC_HA (off) == 0)
9660 stub_size -= 4;
8a2058b5 9661 h->root.type = bfd_link_hash_defined;
afe397ea 9662 h->root.u.def.section = s;
9e390558
AM
9663 h->root.u.def.value = stub_off;
9664 s->size = stub_off + stub_size;
a345bc8d
AM
9665 break;
9666 }
9667 return TRUE;
9668}
9669
9670/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9671 read-only sections. */
9672
9673static bfd_boolean
98bbb1b8 9674maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 9675{
98bbb1b8
AM
9676 asection *sec;
9677
a345bc8d
AM
9678 if (h->root.type == bfd_link_hash_indirect)
9679 return TRUE;
9680
98bbb1b8
AM
9681 sec = readonly_dynrelocs (h);
9682 if (sec != NULL)
a345bc8d 9683 {
98bbb1b8
AM
9684 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9685
9686 info->flags |= DF_TEXTREL;
2cdcc330
AM
9687 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9688 " in read-only section `%pA'\n"),
9689 sec->owner, h->root.root.string, sec);
a345bc8d
AM
9690
9691 /* Not an error, just cut short the traversal. */
9692 return FALSE;
65f38f15 9693 }
b34976b6 9694 return TRUE;
65f38f15
AM
9695}
9696
9697/* Set the sizes of the dynamic sections. */
9698
b34976b6 9699static bfd_boolean
ee67d69a 9700ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9701 struct bfd_link_info *info)
65f38f15
AM
9702{
9703 struct ppc_link_hash_table *htab;
9704 bfd *dynobj;
9705 asection *s;
b34976b6 9706 bfd_boolean relocs;
65f38f15 9707 bfd *ibfd;
7865406b 9708 struct got_entry *first_tlsld;
65f38f15
AM
9709
9710 htab = ppc_hash_table (info);
4dfe6ac6
NC
9711 if (htab == NULL)
9712 return FALSE;
9713
65f38f15
AM
9714 dynobj = htab->elf.dynobj;
9715 if (dynobj == NULL)
9716 abort ();
9717
9718 if (htab->elf.dynamic_sections_created)
9719 {
9720 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9721 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9722 {
3d4d4302 9723 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9724 if (s == NULL)
9725 abort ();
eea6121a 9726 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9727 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9728 }
9729 }
9730
9731 /* Set up .got offsets for local syms, and space for local dynamic
9732 relocs. */
c72f2fb2 9733 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9734 {
411e1bfb
AM
9735 struct got_entry **lgot_ents;
9736 struct got_entry **end_lgot_ents;
e054468f
AM
9737 struct plt_entry **local_plt;
9738 struct plt_entry **end_local_plt;
f961d9dd 9739 unsigned char *lgot_masks;
65f38f15
AM
9740 bfd_size_type locsymcount;
9741 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9742
0c8d6e5c 9743 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9744 continue;
9745
9746 for (s = ibfd->sections; s != NULL; s = s->next)
9747 {
19e08130 9748 struct ppc_dyn_relocs *p;
65f38f15 9749
6edfbbad 9750 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9751 {
ec338859
AM
9752 if (!bfd_is_abs_section (p->sec)
9753 && bfd_is_abs_section (p->sec->output_section))
9754 {
9755 /* Input section has been discarded, either because
9756 it is a copy of a linkonce section or due to
9757 linker script /DISCARD/, so we'll be discarding
9758 the relocs too. */
9759 }
248866a8 9760 else if (p->count != 0)
ec338859 9761 {
19e08130
AM
9762 asection *srel = elf_section_data (p->sec)->sreloc;
9763 if (p->ifunc)
33e44f2e 9764 srel = htab->elf.irelplt;
eea6121a 9765 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9766 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9767 info->flags |= DF_TEXTREL;
ec338859 9768 }
65f38f15
AM
9769 }
9770 }
9771
411e1bfb
AM
9772 lgot_ents = elf_local_got_ents (ibfd);
9773 if (!lgot_ents)
65f38f15
AM
9774 continue;
9775
0ffa91dd 9776 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9777 locsymcount = symtab_hdr->sh_info;
411e1bfb 9778 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9779 local_plt = (struct plt_entry **) end_lgot_ents;
9780 end_local_plt = local_plt + locsymcount;
f961d9dd 9781 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9782 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9783 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9784 {
0b8bcf0d 9785 struct got_entry **pent, *ent;
411e1bfb 9786
0b8bcf0d
AM
9787 pent = lgot_ents;
9788 while ((ent = *pent) != NULL)
411e1bfb
AM
9789 if (ent->got.refcount > 0)
9790 {
e7b938ca 9791 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9792 {
927be08e 9793 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9794 *pent = ent->next;
411e1bfb
AM
9795 }
9796 else
9797 {
19e08130
AM
9798 unsigned int ent_size = 8;
9799 unsigned int rel_size = sizeof (Elf64_External_Rela);
9800
eea6121a 9801 ent->got.offset = s->size;
e7b938ca 9802 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9803 {
19e08130
AM
9804 ent_size *= 2;
9805 rel_size *= 2;
9806 }
9807 s->size += ent_size;
37da22e5 9808 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 9809 {
33e44f2e 9810 htab->elf.irelplt->size += rel_size;
19e08130
AM
9811 htab->got_reli_size += rel_size;
9812 }
f749f26e 9813 else if (bfd_link_dll (info))
19e08130
AM
9814 {
9815 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9816 srel->size += rel_size;
927be08e 9817 }
0b8bcf0d 9818 pent = &ent->next;
411e1bfb
AM
9819 }
9820 }
9821 else
0b8bcf0d 9822 *pent = ent->next;
65f38f15 9823 }
e054468f 9824
2d7ad24e
AM
9825 /* Allocate space for plt calls to local syms. */
9826 lgot_masks = (unsigned char *) end_local_plt;
9827 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
9828 {
9829 struct plt_entry *ent;
9830
9831 for (ent = *local_plt; ent != NULL; ent = ent->next)
9832 if (ent->plt.refcount > 0)
9833 {
2d7ad24e
AM
9834 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9835 {
9836 s = htab->elf.iplt;
9837 ent->plt.offset = s->size;
9838 s->size += PLT_ENTRY_SIZE (htab);
9839 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9840 }
3e04d765
AM
9841 else if (htab->can_convert_all_inline_plt
9842 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
9843 ent->plt.offset = (bfd_vma) -1;
9844 else
9845 {
9846 s = htab->pltlocal;
9847 ent->plt.offset = s->size;
9848 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9849 if (bfd_link_pic (info))
9850 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9851 }
e054468f
AM
9852 }
9853 else
9854 ent->plt.offset = (bfd_vma) -1;
9855 }
65f38f15
AM
9856 }
9857
9858 /* Allocate global sym .plt and .got entries, and space for global
9859 sym dynamic relocs. */
4ce794b7 9860 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 9861
0e1862bb 9862 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 9863 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 9864
7865406b 9865 first_tlsld = NULL;
c72f2fb2 9866 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 9867 {
7865406b
AM
9868 struct got_entry *ent;
9869
0c8d6e5c 9870 if (!is_ppc64_elf (ibfd))
102890f0
AM
9871 continue;
9872
7865406b
AM
9873 ent = ppc64_tlsld_got (ibfd);
9874 if (ent->got.refcount > 0)
102890f0 9875 {
7865406b 9876 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9877 {
7865406b
AM
9878 ent->is_indirect = TRUE;
9879 ent->got.ent = first_tlsld;
9880 }
9881 else
9882 {
9883 if (first_tlsld == NULL)
9884 first_tlsld = ent;
9885 s = ppc64_elf_tdata (ibfd)->got;
9886 ent->got.offset = s->size;
9887 ent->owner = ibfd;
9888 s->size += 16;
f749f26e 9889 if (bfd_link_dll (info))
7865406b
AM
9890 {
9891 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9892 srel->size += sizeof (Elf64_External_Rela);
9893 }
102890f0
AM
9894 }
9895 }
9896 else
7865406b 9897 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9898 }
9899
65f38f15
AM
9900 /* We now have determined the sizes of the various dynamic sections.
9901 Allocate memory for them. */
b34976b6 9902 relocs = FALSE;
65f38f15
AM
9903 for (s = dynobj->sections; s != NULL; s = s->next)
9904 {
9905 if ((s->flags & SEC_LINKER_CREATED) == 0)
9906 continue;
9907
4ce794b7 9908 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9909 /* These haven't been allocated yet; don't strip. */
9910 continue;
33e44f2e
AM
9911 else if (s == htab->elf.sgot
9912 || s == htab->elf.splt
9913 || s == htab->elf.iplt
2d7ad24e 9914 || s == htab->pltlocal
c456f082 9915 || s == htab->glink
9e390558 9916 || s == htab->global_entry
5474d94f
AM
9917 || s == htab->elf.sdynbss
9918 || s == htab->elf.sdynrelro)
65f38f15
AM
9919 {
9920 /* Strip this section if we don't need it; see the
9921 comment below. */
5bd4f169 9922 }
58d180e8
AM
9923 else if (s == htab->glink_eh_frame)
9924 {
9925 if (!bfd_is_abs_section (s->output_section))
9926 /* Not sized yet. */
9927 continue;
9928 }
70cc837d 9929 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9930 {
c456f082 9931 if (s->size != 0)
5bd4f169 9932 {
33e44f2e 9933 if (s != htab->elf.srelplt)
b34976b6 9934 relocs = TRUE;
5bd4f169
AM
9935
9936 /* We use the reloc_count field as a counter if we need
9937 to copy relocs into the output file. */
9938 s->reloc_count = 0;
9939 }
9940 }
65f38f15 9941 else
5bd4f169
AM
9942 {
9943 /* It's not one of our sections, so don't allocate space. */
9944 continue;
9945 }
9946
eea6121a 9947 if (s->size == 0)
5bd4f169 9948 {
c456f082
AM
9949 /* If we don't need this section, strip it from the
9950 output file. This is mostly to handle .rela.bss and
9951 .rela.plt. We must create both sections in
9952 create_dynamic_sections, because they must be created
9953 before the linker maps input sections to output
9954 sections. The linker does that before
9955 adjust_dynamic_symbol is called, and it is that
9956 function which decides whether anything needs to go
9957 into these sections. */
8423293d 9958 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9959 continue;
9960 }
9961
06bcf541
AM
9962 if (bfd_is_abs_section (s->output_section))
9963 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9964 s->name);
9965
c456f082 9966 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9967 continue;
9968
65f38f15
AM
9969 /* Allocate memory for the section contents. We use bfd_zalloc
9970 here in case unused entries are not reclaimed before the
9971 section's contents are written out. This should not happen,
411e1bfb
AM
9972 but this way if it does we get a R_PPC64_NONE reloc in .rela
9973 sections instead of garbage.
9974 We also rely on the section contents being zero when writing
5474d94f 9975 the GOT and .dynrelro. */
eea6121a 9976 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9977 if (s->contents == NULL)
b34976b6 9978 return FALSE;
5bd4f169
AM
9979 }
9980
c72f2fb2 9981 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 9982 {
0c8d6e5c 9983 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9984 continue;
9985
e717da7e 9986 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 9987 if (s != NULL && s != htab->elf.sgot)
e717da7e 9988 {
eea6121a 9989 if (s->size == 0)
8423293d 9990 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9991 else
9992 {
eea6121a 9993 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9994 if (s->contents == NULL)
9995 return FALSE;
9996 }
9997 }
9998 s = ppc64_elf_tdata (ibfd)->relgot;
9999 if (s != NULL)
10000 {
eea6121a 10001 if (s->size == 0)
8423293d 10002 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10003 else
10004 {
eea6121a 10005 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10006 if (s->contents == NULL)
10007 return FALSE;
10008 relocs = TRUE;
10009 s->reloc_count = 0;
10010 }
10011 }
10012 }
10013
e86ce104 10014 if (htab->elf.dynamic_sections_created)
5bd4f169 10015 {
e8910a83
AM
10016 bfd_boolean tls_opt;
10017
5bd4f169
AM
10018 /* Add some entries to the .dynamic section. We fill in the
10019 values later, in ppc64_elf_finish_dynamic_sections, but we
10020 must add the entries now so that we get the correct size for
10021 the .dynamic section. The DT_DEBUG entry is filled in by the
10022 dynamic linker and used by the debugger. */
dc810e39 10023#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10024 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10025
0e1862bb 10026 if (bfd_link_executable (info))
5bd4f169 10027 {
dc810e39 10028 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10029 return FALSE;
5bd4f169
AM
10030 }
10031
33e44f2e 10032 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10033 {
dc810e39
AM
10034 if (!add_dynamic_entry (DT_PLTGOT, 0)
10035 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10036 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10037 || !add_dynamic_entry (DT_JMPREL, 0)
10038 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10039 return FALSE;
5bd4f169
AM
10040 }
10041
ee67d69a 10042 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10043 {
10044 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10045 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10046 return FALSE;
19397422
AM
10047 }
10048
7c9cf415 10049 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10050 && htab->tls_get_addr_fd != NULL
10051 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10052 if (tls_opt || !htab->opd_abi)
10053 {
10054 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10055 return FALSE;
10056 }
a7f2871e 10057
5bd4f169
AM
10058 if (relocs)
10059 {
dc810e39
AM
10060 if (!add_dynamic_entry (DT_RELA, 0)
10061 || !add_dynamic_entry (DT_RELASZ, 0)
10062 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10063 return FALSE;
5bd4f169 10064
65f38f15
AM
10065 /* If any dynamic relocs apply to a read-only section,
10066 then we need a DT_TEXTREL entry. */
248866a8 10067 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10068 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10069
65f38f15 10070 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10071 {
65f38f15 10072 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10073 return FALSE;
5bd4f169 10074 }
5bd4f169 10075 }
5bd4f169 10076 }
65f38f15 10077#undef add_dynamic_entry
5bd4f169 10078
b34976b6 10079 return TRUE;
5bd4f169
AM
10080}
10081
a345bc8d
AM
10082/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10083
10084static bfd_boolean
10085ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10086{
10087 if (h->plt.plist != NULL
10088 && !h->def_regular
10089 && !h->pointer_equality_needed)
10090 return FALSE;
10091
10092 return _bfd_elf_hash_symbol (h);
10093}
10094
721956f4 10095/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10096
4ce794b7
AM
10097static inline enum ppc_stub_type
10098ppc_type_of_stub (asection *input_sec,
10099 const Elf_Internal_Rela *rel,
10100 struct ppc_link_hash_entry **hash,
e054468f 10101 struct plt_entry **plt_ent,
6911b7dc
AM
10102 bfd_vma destination,
10103 unsigned long local_off)
5bd4f169 10104{
721956f4
AM
10105 struct ppc_link_hash_entry *h = *hash;
10106 bfd_vma location;
10107 bfd_vma branch_offset;
10108 bfd_vma max_branch_offset;
4ce794b7 10109 enum elf_ppc64_reloc_type r_type;
5bd4f169 10110
721956f4
AM
10111 if (h != NULL)
10112 {
e054468f 10113 struct plt_entry *ent;
7fe2b9a6 10114 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10115 if (h->oh != NULL
10116 && h->oh->is_func_descriptor)
7b8f6675
AM
10117 {
10118 fdh = ppc_follow_link (h->oh);
10119 *hash = fdh;
10120 }
8387904d 10121
e054468f
AM
10122 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10123 if (ent->addend == rel->r_addend
10124 && ent->plt.offset != (bfd_vma) -1)
10125 {
e054468f
AM
10126 *plt_ent = ent;
10127 return ppc_stub_plt_call;
10128 }
5bd4f169 10129
7fe2b9a6
AM
10130 /* Here, we know we don't have a plt entry. If we don't have a
10131 either a defined function descriptor or a defined entry symbol
10132 in a regular object file, then it is pointless trying to make
10133 any other type of stub. */
854b41e7
AM
10134 if (!is_static_defined (&fdh->elf)
10135 && !is_static_defined (&h->elf))
721956f4 10136 return ppc_stub_none;
5d1634d7 10137 }
e054468f
AM
10138 else if (elf_local_got_ents (input_sec->owner) != NULL)
10139 {
10140 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10141 struct plt_entry **local_plt = (struct plt_entry **)
10142 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10143 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10144
10145 if (local_plt[r_symndx] != NULL)
10146 {
10147 struct plt_entry *ent;
10148
10149 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10150 if (ent->addend == rel->r_addend
10151 && ent->plt.offset != (bfd_vma) -1)
10152 {
10153 *plt_ent = ent;
10154 return ppc_stub_plt_call;
10155 }
10156 }
10157 }
5d1634d7 10158
721956f4
AM
10159 /* Determine where the call point is. */
10160 location = (input_sec->output_offset
10161 + input_sec->output_section->vma
10162 + rel->r_offset);
5d1634d7 10163
721956f4
AM
10164 branch_offset = destination - location;
10165 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10166
721956f4
AM
10167 /* Determine if a long branch stub is needed. */
10168 max_branch_offset = 1 << 25;
23cedd1d
AM
10169 if (r_type == R_PPC64_REL14
10170 || r_type == R_PPC64_REL14_BRTAKEN
10171 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10172 max_branch_offset = 1 << 15;
5d1634d7 10173
6911b7dc 10174 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10175 /* We need a stub. Figure out whether a long_branch or plt_branch
10176 is needed later. */
10177 return ppc_stub_long_branch;
5d1634d7 10178
721956f4 10179 return ppc_stub_none;
5d1634d7
AM
10180}
10181
f891966f
AM
10182/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10183 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10184 . mflr %r12
10185 . bcl 20,31,1f
10186 .1: mflr %r11
10187 . mtlr %r12
05d0e962 10188 . lis %r12,xxx-1b@highest
f891966f 10189 . ori %r12,%r12,xxx-1b@higher
05d0e962 10190 . sldi %r12,%r12,32
f891966f 10191 . oris %r12,%r12,xxx-1b@high
05d0e962 10192 . ori %r12,%r12,xxx-1b@l
f891966f 10193 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10194
10195static bfd_byte *
10196build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10197{
f891966f
AM
10198 bfd_put_32 (abfd, MFLR_R12, p);
10199 p += 4;
10200 bfd_put_32 (abfd, BCL_20_31, p);
10201 p += 4;
10202 bfd_put_32 (abfd, MFLR_R11, p);
10203 p += 4;
10204 bfd_put_32 (abfd, MTLR_R12, p);
10205 p += 4;
05d0e962
AM
10206 if (off + 0x8000 < 0x10000)
10207 {
10208 if (load)
10209 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10210 else
10211 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10212 p += 4;
10213 }
10214 else if (off + 0x80008000ULL < 0x100000000ULL)
10215 {
10216 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10217 p += 4;
10218 if (load)
10219 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10220 else
10221 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10222 p += 4;
10223 }
10224 else
10225 {
10226 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10227 {
10228 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10229 p += 4;
10230 }
10231 else
10232 {
10233 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10234 p += 4;
10235 if (((off >> 32) & 0xffff) != 0)
10236 {
10237 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10238 p += 4;
10239 }
10240 }
10241 if (((off >> 32) & 0xffffffffULL) != 0)
10242 {
10243 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10244 p += 4;
10245 }
10246 if (PPC_HI (off) != 0)
10247 {
10248 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10249 p += 4;
10250 }
10251 if (PPC_LO (off) != 0)
10252 {
10253 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10254 p += 4;
10255 }
10256 if (load)
10257 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10258 else
10259 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10260 p += 4;
10261 }
10262 return p;
10263}
10264
10265static unsigned int
10266size_offset (bfd_vma off)
10267{
10268 unsigned int size;
10269 if (off + 0x8000 < 0x10000)
10270 size = 4;
10271 else if (off + 0x80008000ULL < 0x100000000ULL)
10272 size = 8;
10273 else
10274 {
10275 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10276 size = 4;
10277 else
10278 {
10279 size = 4;
10280 if (((off >> 32) & 0xffff) != 0)
10281 size += 4;
10282 }
10283 if (((off >> 32) & 0xffffffffULL) != 0)
10284 size += 4;
10285 if (PPC_HI (off) != 0)
10286 size += 4;
10287 if (PPC_LO (off) != 0)
10288 size += 4;
10289 size += 4;
10290 }
f891966f 10291 return size + 16;
05d0e962
AM
10292}
10293
3d58e1fc
AM
10294static unsigned int
10295num_relocs_for_offset (bfd_vma off)
10296{
10297 unsigned int num_rel;
10298 if (off + 0x8000 < 0x10000)
10299 num_rel = 1;
10300 else if (off + 0x80008000ULL < 0x100000000ULL)
10301 num_rel = 2;
10302 else
10303 {
10304 num_rel = 1;
10305 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10306 && ((off >> 32) & 0xffff) != 0)
10307 num_rel += 1;
10308 if (PPC_HI (off) != 0)
10309 num_rel += 1;
10310 if (PPC_LO (off) != 0)
10311 num_rel += 1;
10312 }
10313 return num_rel;
10314}
10315
10316static Elf_Internal_Rela *
10317emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10318 bfd_vma roff, bfd_vma targ, bfd_vma off)
10319{
10320 bfd_vma relative_targ = targ - (roff - 8);
10321 if (bfd_big_endian (info->output_bfd))
10322 roff += 2;
10323 r->r_offset = roff;
10324 r->r_addend = relative_targ + roff;
10325 if (off + 0x8000 < 0x10000)
10326 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10327 else if (off + 0x80008000ULL < 0x100000000ULL)
10328 {
10329 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10330 ++r;
10331 roff += 4;
10332 r->r_offset = roff;
10333 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10334 r->r_addend = relative_targ + roff;
10335 }
10336 else
10337 {
10338 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10339 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10340 else
10341 {
10342 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10343 if (((off >> 32) & 0xffff) != 0)
10344 {
10345 ++r;
10346 roff += 4;
10347 r->r_offset = roff;
10348 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10349 r->r_addend = relative_targ + roff;
10350 }
10351 }
10352 if (((off >> 32) & 0xffffffffULL) != 0)
10353 roff += 4;
10354 if (PPC_HI (off) != 0)
10355 {
10356 ++r;
10357 roff += 4;
10358 r->r_offset = roff;
10359 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10360 r->r_addend = relative_targ + roff;
10361 }
10362 if (PPC_LO (off) != 0)
10363 {
10364 ++r;
10365 roff += 4;
10366 r->r_offset = roff;
10367 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10368 r->r_addend = relative_targ + roff;
10369 }
10370 }
10371 return r;
10372}
10373
04bdff6a
AM
10374static bfd_byte *
10375build_powerxx_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10376 bfd_boolean load)
10377{
10378 uint64_t insn;
10379 if (off - odd + (1ULL << 33) < 1ULL << 34)
10380 {
10381 off -= odd;
10382 if (odd)
10383 {
10384 bfd_put_32 (abfd, NOP, p);
10385 p += 4;
10386 }
10387 if (load)
10388 insn = PLD_R12_PC;
10389 else
10390 insn = PADDI_R12_PC;
10391 insn |= D34 (off);
10392 bfd_put_32 (abfd, insn >> 32, p);
10393 p += 4;
10394 bfd_put_32 (abfd, insn, p);
10395 }
10396 /* The minimum value for paddi is -0x200000000. The minimum value
10397 for li is -0x8000, which when shifted by 34 and added gives a
10398 minimum value of -0x2000200000000. The maximum value is
10399 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10400 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10401 {
10402 off -= 8 - odd;
10403 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10404 p += 4;
10405 if (!odd)
10406 {
10407 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10408 p += 4;
10409 }
10410 insn = PADDI_R12_PC | D34 (off);
10411 bfd_put_32 (abfd, insn >> 32, p);
10412 p += 4;
10413 bfd_put_32 (abfd, insn, p);
10414 p += 4;
10415 if (odd)
10416 {
10417 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10418 p += 4;
10419 }
10420 if (load)
10421 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10422 else
10423 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10424 }
10425 else
10426 {
10427 off -= odd + 8;
10428 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10429 p += 4;
10430 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10431 p += 4;
10432 if (odd)
10433 {
10434 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10435 p += 4;
10436 }
10437 insn = PADDI_R12_PC | D34 (off);
10438 bfd_put_32 (abfd, insn >> 32, p);
10439 p += 4;
10440 bfd_put_32 (abfd, insn, p);
10441 p += 4;
10442 if (!odd)
10443 {
10444 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10445 p += 4;
10446 }
10447 if (load)
10448 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10449 else
10450 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10451 }
10452 p += 4;
10453 return p;
10454}
10455
10456static unsigned int
10457size_powerxx_offset (bfd_vma off, int odd)
10458{
10459 if (off - odd + (1ULL << 33) < 1ULL << 34)
10460 return odd + 8;
10461 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10462 return 20;
10463 else
10464 return 24;
10465}
10466
10467static unsigned int
10468num_relocs_for_powerxx_offset (bfd_vma off, int odd)
10469{
10470 if (off - odd + (1ULL << 33) < 1ULL << 34)
10471 return 1;
10472 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10473 return 2;
10474 else
10475 return 3;
10476}
10477
10478static Elf_Internal_Rela *
10479emit_relocs_for_powerxx_offset (struct bfd_link_info *info,
10480 Elf_Internal_Rela *r, bfd_vma roff,
10481 bfd_vma targ, bfd_vma off, int odd)
10482{
10483 if (off - odd + (1ULL << 33) < 1ULL << 34)
10484 roff += odd;
10485 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10486 {
10487 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10488 r->r_offset = roff + d_offset;
10489 r->r_addend = targ + 8 - odd - d_offset;
10490 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10491 ++r;
10492 roff += 8 - odd;
10493 }
10494 else
10495 {
10496 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10497 r->r_offset = roff + d_offset;
10498 r->r_addend = targ + 8 + odd - d_offset;
10499 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10500 ++r;
10501 roff += 4;
10502 r->r_offset = roff + d_offset;
10503 r->r_addend = targ + 4 + odd - d_offset;
10504 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10505 ++r;
10506 roff += 4 + odd;
10507 }
10508 r->r_offset = roff;
10509 r->r_addend = targ;
10510 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10511 return r;
10512}
10513
df136d64
AM
10514/* Emit .eh_frame opcode to advance pc by DELTA. */
10515
10516static bfd_byte *
10517eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10518{
10519 delta /= 4;
10520 if (delta < 64)
10521 *eh++ = DW_CFA_advance_loc + delta;
10522 else if (delta < 256)
10523 {
10524 *eh++ = DW_CFA_advance_loc1;
10525 *eh++ = delta;
10526 }
10527 else if (delta < 65536)
10528 {
10529 *eh++ = DW_CFA_advance_loc2;
10530 bfd_put_16 (abfd, delta, eh);
10531 eh += 2;
10532 }
10533 else
10534 {
10535 *eh++ = DW_CFA_advance_loc4;
10536 bfd_put_32 (abfd, delta, eh);
10537 eh += 4;
10538 }
10539 return eh;
10540}
10541
10542/* Size of required .eh_frame opcode to advance pc by DELTA. */
10543
10544static unsigned int
10545eh_advance_size (unsigned int delta)
10546{
10547 if (delta < 64 * 4)
10548 /* DW_CFA_advance_loc+[1..63]. */
10549 return 1;
10550 if (delta < 256 * 4)
10551 /* DW_CFA_advance_loc1, byte. */
10552 return 2;
10553 if (delta < 65536 * 4)
10554 /* DW_CFA_advance_loc2, 2 bytes. */
10555 return 3;
10556 /* DW_CFA_advance_loc4, 4 bytes. */
10557 return 5;
10558}
10559
794e51c0
AM
10560/* With power7 weakly ordered memory model, it is possible for ld.so
10561 to update a plt entry in one thread and have another thread see a
10562 stale zero toc entry. To avoid this we need some sort of acquire
10563 barrier in the call stub. One solution is to make the load of the
10564 toc word seem to appear to depend on the load of the function entry
10565 word. Another solution is to test for r2 being zero, and branch to
10566 the appropriate glink entry if so.
10567
10568 . fake dep barrier compare
71a39c98
AM
10569 . ld 12,xxx(2) ld 12,xxx(2)
10570 . mtctr 12 mtctr 12
10571 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10572 . add 2,2,11 cmpldi 2,0
10573 . ld 2,xxx+8(2) bnectr+
10574 . bctr b <glink_entry>
10575
10576 The solution involving the compare turns out to be faster, so
10577 that's what we use unless the branch won't reach. */
10578
10579#define ALWAYS_USE_FAKE_DEP 0
10580#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10581
794e51c0
AM
10582static inline unsigned int
10583plt_stub_size (struct ppc_link_hash_table *htab,
10584 struct ppc_stub_hash_entry *stub_entry,
10585 bfd_vma off)
10586{
05d0e962 10587 unsigned size;
b9e5796b 10588
05d0e962
AM
10589 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10590 {
04bdff6a
AM
10591 if (htab->powerxx_stubs)
10592 {
10593 bfd_vma start = (stub_entry->stub_offset
10594 + stub_entry->group->stub_sec->output_offset
10595 + stub_entry->group->stub_sec->output_section->vma);
10596 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10597 start += 4;
10598 size = 8 + size_powerxx_offset (off, start & 4);
10599 }
10600 else
10601 size = 8 + size_offset (off - 8);
05d0e962
AM
10602 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10603 size += 4;
10604 return size;
10605 }
10606
10607 size = 12;
b9e5796b
AM
10608 if (ALWAYS_EMIT_R2SAVE
10609 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10610 size += 4;
10611 if (PPC_HA (off) != 0)
794e51c0 10612 size += 4;
b9e5796b
AM
10613 if (htab->opd_abi)
10614 {
10615 size += 4;
e7d1c40c 10616 if (htab->params->plt_static_chain)
b9e5796b 10617 size += 4;
bd4d2eaa
AM
10618 if (htab->params->plt_thread_safe
10619 && htab->elf.dynamic_sections_created
10620 && stub_entry->h != NULL
10621 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10622 size += 8;
e7d1c40c 10623 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10624 size += 4;
10625 }
794e51c0
AM
10626 if (stub_entry->h != NULL
10627 && (stub_entry->h == htab->tls_get_addr_fd
10628 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10629 && htab->params->tls_get_addr_opt)
f378ab09
AM
10630 {
10631 size += 7 * 4;
e81b4c93 10632 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 10633 size += 6 * 4;
f378ab09 10634 }
794e51c0
AM
10635 return size;
10636}
10637
2420fff6
AM
10638/* Depending on the sign of plt_stub_align:
10639 If positive, return the padding to align to a 2**plt_stub_align
10640 boundary.
10641 If negative, if this stub would cross fewer 2**plt_stub_align
10642 boundaries if we align, then return the padding needed to do so. */
10643
794e51c0
AM
10644static inline unsigned int
10645plt_stub_pad (struct ppc_link_hash_table *htab,
10646 struct ppc_stub_hash_entry *stub_entry,
10647 bfd_vma plt_off)
10648{
2420fff6 10649 int stub_align;
1aa42141 10650 unsigned stub_size;
6f20ed8a 10651 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10652
2420fff6
AM
10653 if (htab->params->plt_stub_align >= 0)
10654 {
10655 stub_align = 1 << htab->params->plt_stub_align;
10656 if ((stub_off & (stub_align - 1)) != 0)
10657 return stub_align - (stub_off & (stub_align - 1));
10658 return 0;
10659 }
10660
10661 stub_align = 1 << -htab->params->plt_stub_align;
1aa42141 10662 stub_size = plt_stub_size (htab, stub_entry, plt_off);
794e51c0 10663 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10664 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10665 return stub_align - (stub_off & (stub_align - 1));
10666 return 0;
10667}
10668
10669/* Build a .plt call stub. */
10670
10671static inline bfd_byte *
10672build_plt_stub (struct ppc_link_hash_table *htab,
10673 struct ppc_stub_hash_entry *stub_entry,
10674 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10675{
e7d1c40c 10676 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10677 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10678 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10679 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10680 && htab->elf.dynamic_sections_created
10681 && stub_entry->h != NULL
10682 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10683 bfd_boolean use_fake_dep = plt_thread_safe;
10684 bfd_vma cmp_branch_off = 0;
10685
10686 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10687 && plt_load_toc
794e51c0 10688 && plt_thread_safe
bd4d2eaa
AM
10689 && !((stub_entry->h == htab->tls_get_addr_fd
10690 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10691 && htab->params->tls_get_addr_opt))
794e51c0
AM
10692 {
10693 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10694 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10695 / PLT_ENTRY_SIZE (htab));
9e390558 10696 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10697 bfd_vma to, from;
10698
68d62958
AM
10699 if (pltindex > 32768)
10700 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10701 to = (glinkoff
10702 + htab->glink->output_offset
10703 + htab->glink->output_section->vma);
6f20ed8a 10704 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10705 + 4 * (ALWAYS_EMIT_R2SAVE
10706 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10707 + 4 * (PPC_HA (offset) != 0)
10708 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10709 != PPC_HA (offset))
10710 + 4 * (plt_static_chain != 0)
10711 + 20
6f20ed8a
AM
10712 + stub_entry->group->stub_sec->output_offset
10713 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10714 cmp_branch_off = to - from;
10715 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10716 }
10717
ac2df442
AM
10718 if (PPC_HA (offset) != 0)
10719 {
176a0d42
AM
10720 if (r != NULL)
10721 {
794e51c0
AM
10722 if (ALWAYS_EMIT_R2SAVE
10723 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10724 r[0].r_offset += 4;
176a0d42 10725 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10726 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10727 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10728 r[1].r_addend = r[0].r_addend;
b9e5796b 10729 if (plt_load_toc)
176a0d42 10730 {
b9e5796b 10731 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10732 {
b9e5796b
AM
10733 r[2].r_offset = r[1].r_offset + 4;
10734 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10735 r[2].r_addend = r[0].r_addend;
10736 }
10737 else
10738 {
10739 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10740 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10741 r[2].r_addend = r[0].r_addend + 8;
10742 if (plt_static_chain)
10743 {
10744 r[3].r_offset = r[2].r_offset + 4;
10745 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10746 r[3].r_addend = r[0].r_addend + 16;
10747 }
c7131b65 10748 }
176a0d42
AM
10749 }
10750 }
794e51c0
AM
10751 if (ALWAYS_EMIT_R2SAVE
10752 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10753 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10754 if (plt_load_toc)
10755 {
10756 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10757 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10758 }
10759 else
10760 {
10761 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10762 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10763 }
b9e5796b
AM
10764 if (plt_load_toc
10765 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10766 {
71a39c98 10767 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10768 offset = 0;
10769 }
71a39c98 10770 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10771 if (plt_load_toc)
794e51c0 10772 {
b9e5796b
AM
10773 if (use_fake_dep)
10774 {
10775 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10776 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10777 }
10778 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10779 if (plt_static_chain)
10780 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10781 }
ac2df442
AM
10782 }
10783 else
10784 {
176a0d42
AM
10785 if (r != NULL)
10786 {
794e51c0
AM
10787 if (ALWAYS_EMIT_R2SAVE
10788 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10789 r[0].r_offset += 4;
176a0d42 10790 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10791 if (plt_load_toc)
176a0d42 10792 {
b9e5796b 10793 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10794 {
b9e5796b
AM
10795 r[1].r_offset = r[0].r_offset + 4;
10796 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10797 r[1].r_addend = r[0].r_addend;
10798 }
10799 else
10800 {
10801 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10802 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10803 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10804 if (plt_static_chain)
10805 {
10806 r[2].r_offset = r[1].r_offset + 4;
10807 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10808 r[2].r_addend = r[0].r_addend + 8;
10809 }
c7131b65 10810 }
176a0d42
AM
10811 }
10812 }
794e51c0
AM
10813 if (ALWAYS_EMIT_R2SAVE
10814 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10815 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10816 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10817 if (plt_load_toc
10818 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10819 {
10820 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10821 offset = 0;
10822 }
71a39c98 10823 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10824 if (plt_load_toc)
794e51c0 10825 {
b9e5796b
AM
10826 if (use_fake_dep)
10827 {
10828 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10829 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10830 }
10831 if (plt_static_chain)
10832 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10833 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10834 }
ac2df442 10835 }
b9e5796b 10836 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10837 {
10838 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10839 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10840 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10841 }
10842 else
407aa07c 10843 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10844 return p;
10845}
10846
a7f2871e
AM
10847/* Build a special .plt call stub for __tls_get_addr. */
10848
10849#define LD_R11_0R3 0xe9630000
10850#define LD_R12_0R3 0xe9830000
10851#define MR_R0_R3 0x7c601b78
10852#define CMPDI_R11_0 0x2c2b0000
10853#define ADD_R3_R12_R13 0x7c6c6a14
10854#define BEQLR 0x4d820020
10855#define MR_R3_R0 0x7c030378
a7f2871e
AM
10856#define STD_R11_0R1 0xf9610000
10857#define BCTRL 0x4e800421
10858#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10859#define MTLR_R11 0x7d6803a6
10860
10861static inline bfd_byte *
794e51c0
AM
10862build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10863 struct ppc_stub_hash_entry *stub_entry,
10864 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10865{
e7d1c40c 10866 bfd *obfd = htab->params->stub_bfd;
df136d64 10867 bfd_byte *loc = p;
794e51c0 10868
a7f2871e
AM
10869 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10870 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10871 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10872 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10873 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10874 bfd_put_32 (obfd, BEQLR, p), p += 4;
10875 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
10876 if (r != NULL)
10877 r[0].r_offset += 7 * 4;
e81b4c93 10878 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
f378ab09
AM
10879 return build_plt_stub (htab, stub_entry, p, offset, r);
10880
a7f2871e 10881 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10882 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10883
10884 if (r != NULL)
f378ab09 10885 r[0].r_offset += 2 * 4;
794e51c0 10886 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 10887 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 10888
a078d95a 10889 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10890 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10891 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10892 bfd_put_32 (obfd, BLR, p), p += 4;
10893
df136d64
AM
10894 if (htab->glink_eh_frame != NULL
10895 && htab->glink_eh_frame->size != 0)
10896 {
10897 bfd_byte *base, *eh;
10898 unsigned int lr_used, delta;
10899
10900 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10901 eh = base + stub_entry->group->eh_size;
10902 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10903 delta = lr_used - stub_entry->group->lr_restore;
10904 stub_entry->group->lr_restore = lr_used + 16;
10905 eh = eh_advance (htab->elf.dynobj, eh, delta);
10906 *eh++ = DW_CFA_offset_extended_sf;
10907 *eh++ = 65;
10908 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10909 *eh++ = DW_CFA_advance_loc + 4;
10910 *eh++ = DW_CFA_restore_extended;
10911 *eh++ = 65;
10912 stub_entry->group->eh_size = eh - base;
10913 }
a7f2871e
AM
10914 return p;
10915}
10916
176a0d42
AM
10917static Elf_Internal_Rela *
10918get_relocs (asection *sec, int count)
10919{
10920 Elf_Internal_Rela *relocs;
10921 struct bfd_elf_section_data *elfsec_data;
10922
10923 elfsec_data = elf_section_data (sec);
10924 relocs = elfsec_data->relocs;
10925 if (relocs == NULL)
10926 {
10927 bfd_size_type relsize;
10928 relsize = sec->reloc_count * sizeof (*relocs);
10929 relocs = bfd_alloc (sec->owner, relsize);
10930 if (relocs == NULL)
10931 return NULL;
10932 elfsec_data->relocs = relocs;
d4730f92
BS
10933 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10934 sizeof (Elf_Internal_Shdr));
10935 if (elfsec_data->rela.hdr == NULL)
10936 return NULL;
10937 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10938 * sizeof (Elf64_External_Rela));
10939 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10940 sec->reloc_count = 0;
10941 }
10942 relocs += sec->reloc_count;
10943 sec->reloc_count += count;
10944 return relocs;
10945}
10946
3d58e1fc
AM
10947/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10948 forms, to the equivalent relocs against the global symbol given by
10949 STUB_ENTRY->H. */
10950
10951static bfd_boolean
10952use_global_in_relocs (struct ppc_link_hash_table *htab,
10953 struct ppc_stub_hash_entry *stub_entry,
10954 Elf_Internal_Rela *r, unsigned int num_rel)
10955{
10956 struct elf_link_hash_entry **hashes;
10957 unsigned long symndx;
10958 struct ppc_link_hash_entry *h;
10959 bfd_vma symval;
10960
10961 /* Relocs are always against symbols in their own object file. Fake
10962 up global sym hashes for the stub bfd (which has no symbols). */
10963 hashes = elf_sym_hashes (htab->params->stub_bfd);
10964 if (hashes == NULL)
10965 {
10966 bfd_size_type hsize;
10967
10968 /* When called the first time, stub_globals will contain the
10969 total number of symbols seen during stub sizing. After
10970 allocating, stub_globals is used as an index to fill the
10971 hashes array. */
10972 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10973 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10974 if (hashes == NULL)
10975 return FALSE;
10976 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10977 htab->stub_globals = 1;
10978 }
10979 symndx = htab->stub_globals++;
10980 h = stub_entry->h;
10981 hashes[symndx] = &h->elf;
10982 if (h->oh != NULL && h->oh->is_func)
10983 h = ppc_follow_link (h->oh);
10984 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10985 || h->elf.root.type == bfd_link_hash_defweak);
10986 symval = (h->elf.root.u.def.value
10987 + h->elf.root.u.def.section->output_offset
10988 + h->elf.root.u.def.section->output_section->vma);
10989 while (num_rel-- != 0)
10990 {
10991 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10992 if (h->elf.root.u.def.section != stub_entry->target_section)
10993 {
10994 /* H is an opd symbol. The addend must be zero, and the
10995 branch reloc is the only one we can convert. */
10996 r->r_addend = 0;
10997 break;
10998 }
10999 else
11000 r->r_addend -= symval;
11001 --r;
11002 }
11003 return TRUE;
11004}
11005
aa374f67 11006static bfd_vma
25f53a85 11007get_r2off (struct bfd_link_info *info,
aa374f67
AM
11008 struct ppc_stub_hash_entry *stub_entry)
11009{
25f53a85 11010 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 11011 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
11012
11013 if (r2off == 0)
11014 {
11015 /* Support linking -R objects. Get the toc pointer from the
11016 opd entry. */
11017 char buf[8];
b9e5796b
AM
11018 if (!htab->opd_abi)
11019 return r2off;
aa374f67
AM
11020 asection *opd = stub_entry->h->elf.root.u.def.section;
11021 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11022
11023 if (strcmp (opd->name, ".opd") != 0
11024 || opd->reloc_count != 0)
11025 {
2cdcc330
AM
11026 info->callbacks->einfo
11027 (_("%P: cannot find opd entry toc for `%pT'\n"),
11028 stub_entry->h->elf.root.root.string);
aa374f67 11029 bfd_set_error (bfd_error_bad_value);
a7c49797 11030 return (bfd_vma) -1;
aa374f67
AM
11031 }
11032 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11033 return (bfd_vma) -1;
aa374f67 11034 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11035 r2off -= elf_gp (info->output_bfd);
aa374f67 11036 }
6f20ed8a 11037 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11038 return r2off;
11039}
11040
b34976b6 11041static bfd_boolean
4ce794b7 11042ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11043{
721956f4
AM
11044 struct ppc_stub_hash_entry *stub_entry;
11045 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11046 struct bfd_link_info *info;
11047 struct ppc_link_hash_table *htab;
721956f4 11048 bfd_byte *loc;
3d58e1fc 11049 bfd_byte *p, *relp;
1aa42141 11050 bfd_vma targ, off;
176a0d42 11051 Elf_Internal_Rela *r;
e054468f 11052 asection *plt;
3d58e1fc 11053 int num_rel;
04bdff6a 11054 int odd;
5d1634d7 11055
721956f4
AM
11056 /* Massage our args to the form they really have. */
11057 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11058 info = in_arg;
5d1634d7 11059
5d1634d7 11060 htab = ppc_hash_table (info);
4dfe6ac6
NC
11061 if (htab == NULL)
11062 return FALSE;
5d1634d7 11063
1aa42141 11064 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 11065 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11066
4ce794b7 11067 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 11068 switch (stub_entry->stub_type)
5d1634d7 11069 {
721956f4 11070 case ppc_stub_long_branch:
ad8e1ba5 11071 case ppc_stub_long_branch_r2off:
721956f4 11072 /* Branches are relative. This is where we are going to. */
1aa42141 11073 targ = (stub_entry->target_value
6911b7dc
AM
11074 + stub_entry->target_section->output_offset
11075 + stub_entry->target_section->output_section->vma);
1aa42141 11076 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11077
721956f4 11078 /* And this is where we are coming from. */
1aa42141
AM
11079 off = (stub_entry->stub_offset
11080 + stub_entry->group->stub_sec->output_offset
11081 + stub_entry->group->stub_sec->output_section->vma);
11082 off = targ - off;
e86ce104 11083
9e390558 11084 p = loc;
ac2df442 11085 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 11086 {
25f53a85 11087 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11088
a7c49797 11089 if (r2off == (bfd_vma) -1)
aa374f67
AM
11090 {
11091 htab->stub_error = TRUE;
11092 return FALSE;
11093 }
9e390558
AM
11094 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11095 p += 4;
ac2df442
AM
11096 if (PPC_HA (r2off) != 0)
11097 {
e7d1c40c 11098 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11099 ADDIS_R2_R2 | PPC_HA (r2off), p);
11100 p += 4;
a7c49797
AM
11101 }
11102 if (PPC_LO (r2off) != 0)
11103 {
11104 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11105 ADDI_R2_R2 | PPC_LO (r2off), p);
11106 p += 4;
ac2df442 11107 }
9e390558 11108 off -= p - loc;
ad8e1ba5 11109 }
9e390558
AM
11110 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11111 p += 4;
ad8e1ba5 11112
5c3dead3
AM
11113 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11114 {
cf97bcb0
AM
11115 _bfd_error_handler
11116 (_("long branch stub `%s' offset overflow"),
bc30df16 11117 stub_entry->root.string);
5c3dead3
AM
11118 htab->stub_error = TRUE;
11119 return FALSE;
11120 }
ee75fd95
AM
11121
11122 if (info->emitrelocations)
11123 {
6f20ed8a 11124 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
11125 if (r == NULL)
11126 return FALSE;
9e390558 11127 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11128 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11129 r->r_addend = targ;
3d58e1fc
AM
11130 if (stub_entry->h != NULL
11131 && !use_global_in_relocs (htab, stub_entry, r, 1))
11132 return FALSE;
ee75fd95 11133 }
721956f4 11134 break;
e86ce104 11135
721956f4 11136 case ppc_stub_plt_branch:
ad8e1ba5 11137 case ppc_stub_plt_branch_r2off:
721956f4
AM
11138 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11139 stub_entry->root.string + 9,
b34976b6 11140 FALSE, FALSE);
721956f4
AM
11141 if (br_entry == NULL)
11142 {
cf97bcb0
AM
11143 _bfd_error_handler (_("can't find branch stub `%s'"),
11144 stub_entry->root.string);
b34976b6
AM
11145 htab->stub_error = TRUE;
11146 return FALSE;
721956f4
AM
11147 }
11148
1aa42141 11149 targ = (stub_entry->target_value
176a0d42
AM
11150 + stub_entry->target_section->output_offset
11151 + stub_entry->target_section->output_section->vma);
6911b7dc 11152 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 11153 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11154
1aa42141 11155 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11156 htab->brlt->contents + br_entry->offset);
721956f4 11157
f94498ff 11158 if (br_entry->iter == htab->stub_iteration)
721956f4 11159 {
f94498ff 11160 br_entry->iter = 0;
84f5d08e 11161
f94498ff 11162 if (htab->relbrlt != NULL)
84f5d08e 11163 {
f94498ff
AM
11164 /* Create a reloc for the branch lookup table entry. */
11165 Elf_Internal_Rela rela;
11166 bfd_byte *rl;
11167
11168 rela.r_offset = (br_entry->offset
11169 + htab->brlt->output_offset
11170 + htab->brlt->output_section->vma);
11171 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11172 rela.r_addend = targ;
f94498ff
AM
11173
11174 rl = htab->relbrlt->contents;
11175 rl += (htab->relbrlt->reloc_count++
11176 * sizeof (Elf64_External_Rela));
11177 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11178 }
11179 else if (info->emitrelocations)
11180 {
176a0d42
AM
11181 r = get_relocs (htab->brlt, 1);
11182 if (r == NULL)
11183 return FALSE;
11184 /* brlt, being SEC_LINKER_CREATED does not go through the
11185 normal reloc processing. Symbols and offsets are not
11186 translated from input file to output file form, so
11187 set up the offset per the output file. */
f94498ff
AM
11188 r->r_offset = (br_entry->offset
11189 + htab->brlt->output_offset
11190 + htab->brlt->output_section->vma);
11191 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11192 r->r_addend = targ;
84f5d08e 11193 }
84f5d08e 11194 }
721956f4 11195
1aa42141 11196 targ = (br_entry->offset
176a0d42
AM
11197 + htab->brlt->output_offset
11198 + htab->brlt->output_section->vma);
11199
1aa42141
AM
11200 off = (elf_gp (info->output_bfd)
11201 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11202 off = targ - off;
721956f4 11203
ad8e1ba5 11204 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11205 {
25f53a85 11206 info->callbacks->einfo
c1c8c1ef 11207 (_("%P: linkage table error against `%pT'\n"),
721956f4 11208 stub_entry->root.string);
5d1634d7 11209 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11210 htab->stub_error = TRUE;
11211 return FALSE;
5d1634d7 11212 }
41bd81ab 11213
176a0d42
AM
11214 if (info->emitrelocations)
11215 {
6f20ed8a 11216 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11217 if (r == NULL)
11218 return FALSE;
6f20ed8a 11219 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11220 if (bfd_big_endian (info->output_bfd))
11221 r[0].r_offset += 2;
00f412ee 11222 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11223 r[0].r_offset += 4;
11224 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11225 r[0].r_addend = targ;
176a0d42
AM
11226 if (PPC_HA (off) != 0)
11227 {
11228 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11229 r[1].r_offset = r[0].r_offset + 4;
11230 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11231 r[1].r_addend = r[0].r_addend;
11232 }
11233 }
11234
9e390558 11235 p = loc;
00f412ee 11236 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11237 {
176a0d42 11238 if (PPC_HA (off) != 0)
ac2df442 11239 {
e7d1c40c 11240 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11241 ADDIS_R12_R2 | PPC_HA (off), p);
11242 p += 4;
e7d1c40c 11243 bfd_put_32 (htab->params->stub_bfd,
9e390558 11244 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11245 }
11246 else
9e390558
AM
11247 bfd_put_32 (htab->params->stub_bfd,
11248 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11249 }
11250 else
11251 {
25f53a85 11252 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11253
a7c49797 11254 if (r2off == (bfd_vma) -1)
aa374f67
AM
11255 {
11256 htab->stub_error = TRUE;
11257 return FALSE;
11258 }
ad8e1ba5 11259
9e390558
AM
11260 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11261 p += 4;
176a0d42 11262 if (PPC_HA (off) != 0)
ac2df442 11263 {
e7d1c40c 11264 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11265 ADDIS_R12_R2 | PPC_HA (off), p);
11266 p += 4;
e7d1c40c 11267 bfd_put_32 (htab->params->stub_bfd,
9e390558 11268 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11269 }
11270 else
9e390558 11271 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11272
11273 if (PPC_HA (r2off) != 0)
11274 {
9e390558 11275 p += 4;
e7d1c40c 11276 bfd_put_32 (htab->params->stub_bfd,
9e390558 11277 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11278 }
11279 if (PPC_LO (r2off) != 0)
11280 {
9e390558 11281 p += 4;
e7d1c40c 11282 bfd_put_32 (htab->params->stub_bfd,
9e390558 11283 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11284 }
ad8e1ba5 11285 }
9e390558
AM
11286 p += 4;
11287 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11288 p += 4;
407aa07c
AM
11289 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11290 p += 4;
721956f4 11291 break;
5d1634d7 11292
05d0e962
AM
11293 case ppc_stub_long_branch_notoc:
11294 case ppc_stub_long_branch_both:
11295 case ppc_stub_plt_branch_notoc:
11296 case ppc_stub_plt_branch_both:
11297 case ppc_stub_plt_call_notoc:
11298 case ppc_stub_plt_call_both:
11299 p = loc;
f891966f 11300 off = (stub_entry->stub_offset
05d0e962
AM
11301 + stub_entry->group->stub_sec->output_offset
11302 + stub_entry->group->stub_sec->output_section->vma);
11303 if (stub_entry->stub_type == ppc_stub_long_branch_both
11304 || stub_entry->stub_type == ppc_stub_plt_branch_both
11305 || stub_entry->stub_type == ppc_stub_plt_call_both)
11306 {
11307 off += 4;
11308 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11309 p += 4;
11310 }
11311 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11312 {
11313 targ = stub_entry->plt_ent->plt.offset & ~1;
11314 if (targ >= (bfd_vma) -2)
11315 abort ();
11316
11317 plt = htab->elf.splt;
11318 if (!htab->elf.dynamic_sections_created
11319 || stub_entry->h == NULL
11320 || stub_entry->h->elf.dynindx == -1)
11321 {
11322 if (stub_entry->symtype == STT_GNU_IFUNC)
11323 plt = htab->elf.iplt;
11324 else
11325 plt = htab->pltlocal;
11326 }
11327 targ += plt->output_offset + plt->output_section->vma;
11328 }
11329 else
11330 targ = (stub_entry->target_value
11331 + stub_entry->target_section->output_offset
11332 + stub_entry->target_section->output_section->vma);
04bdff6a 11333 odd = off & 4;
05d0e962 11334 off = targ - off;
f891966f 11335
3d58e1fc
AM
11336 relp = p;
11337 num_rel = 0;
04bdff6a
AM
11338 if (htab->powerxx_stubs)
11339 {
11340 bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
11341 p = build_powerxx_offset (htab->params->stub_bfd, p, off, odd, load);
11342 }
11343 else
11344 {
11345 /* The notoc stubs calculate their target (either a PLT entry or
11346 the global entry point of a function) relative to the PC
11347 returned by the "bcl" two instructions past the start of the
11348 sequence emitted by build_offset. The offset is therefore 8
11349 less than calculated from the start of the sequence. */
11350 off -= 8;
11351 p = build_offset (htab->params->stub_bfd, p, off,
11352 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11353 }
11354
f891966f 11355 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
05d0e962 11356 {
f891966f 11357 bfd_vma from;
3d58e1fc 11358 num_rel = 1;
f891966f
AM
11359 from = (stub_entry->stub_offset
11360 + stub_entry->group->stub_sec->output_offset
11361 + stub_entry->group->stub_sec->output_section->vma
11362 + (p - loc));
05d0e962 11363 bfd_put_32 (htab->params->stub_bfd,
f891966f 11364 B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
11365 }
11366 else
11367 {
11368 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11369 p += 4;
11370 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11371 }
11372 p += 4;
df136d64 11373
3d58e1fc
AM
11374 if (info->emitrelocations)
11375 {
04bdff6a
AM
11376 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
11377 if (htab->powerxx_stubs)
11378 num_rel += num_relocs_for_powerxx_offset (off, odd);
11379 else
11380 {
11381 num_rel += num_relocs_for_offset (off);
11382 roff += 16;
11383 }
3d58e1fc
AM
11384 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11385 if (r == NULL)
11386 return FALSE;
04bdff6a
AM
11387 if (htab->powerxx_stubs)
11388 r = emit_relocs_for_powerxx_offset (info, r, roff, targ, off, odd);
11389 else
11390 r = emit_relocs_for_offset (info, r, roff, targ, off);
3d58e1fc
AM
11391 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11392 || stub_entry->stub_type == ppc_stub_long_branch_both)
11393 {
11394 ++r;
11395 roff = p - 4 - stub_entry->group->stub_sec->contents;
11396 r->r_offset = roff;
11397 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11398 r->r_addend = targ;
11399 if (stub_entry->h != NULL
11400 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11401 return FALSE;
11402 }
11403 }
11404
04bdff6a
AM
11405 if (!htab->powerxx_stubs
11406 && htab->glink_eh_frame != NULL
11407 && htab->glink_eh_frame->size != 0)
df136d64
AM
11408 {
11409 bfd_byte *base, *eh;
11410 unsigned int lr_used, delta;
11411
11412 base = (htab->glink_eh_frame->contents
11413 + stub_entry->group->eh_base + 17);
11414 eh = base + stub_entry->group->eh_size;
11415 lr_used = stub_entry->stub_offset + 8;
11416 if (stub_entry->stub_type == ppc_stub_long_branch_both
11417 || stub_entry->stub_type == ppc_stub_plt_branch_both
11418 || stub_entry->stub_type == ppc_stub_plt_call_both)
11419 lr_used += 4;
11420 delta = lr_used - stub_entry->group->lr_restore;
11421 stub_entry->group->lr_restore = lr_used + 8;
11422 eh = eh_advance (htab->elf.dynobj, eh, delta);
11423 *eh++ = DW_CFA_register;
11424 *eh++ = 65;
11425 *eh++ = 12;
11426 *eh++ = DW_CFA_advance_loc + 2;
11427 *eh++ = DW_CFA_restore_extended;
11428 *eh++ = 65;
11429 stub_entry->group->eh_size = eh - base;
11430 }
05d0e962
AM
11431 break;
11432
721956f4 11433 case ppc_stub_plt_call:
794e51c0 11434 case ppc_stub_plt_call_r2save:
e054468f 11435 if (stub_entry->h != NULL
b31867b6
AM
11436 && stub_entry->h->is_func_descriptor
11437 && stub_entry->h->oh != NULL)
c862ae31 11438 {
b31867b6
AM
11439 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11440
11441 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11442 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11443 if (fh->elf.root.type == bfd_link_hash_undefined
11444 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11445 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11446 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11447 }
11448
721956f4 11449 /* Now build the stub. */
1aa42141
AM
11450 targ = stub_entry->plt_ent->plt.offset & ~1;
11451 if (targ >= (bfd_vma) -2)
721956f4
AM
11452 abort ();
11453
33e44f2e 11454 plt = htab->elf.splt;
25f23106
AM
11455 if (!htab->elf.dynamic_sections_created
11456 || stub_entry->h == NULL
11457 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11458 {
11459 if (stub_entry->symtype == STT_GNU_IFUNC)
11460 plt = htab->elf.iplt;
11461 else
11462 plt = htab->pltlocal;
11463 }
1aa42141 11464 targ += plt->output_offset + plt->output_section->vma;
e054468f 11465
1aa42141
AM
11466 off = (elf_gp (info->output_bfd)
11467 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11468 off = targ - off;
721956f4 11469
ad8e1ba5 11470 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11471 {
25f53a85 11472 info->callbacks->einfo
695344c0 11473 /* xgettext:c-format */
c1c8c1ef 11474 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11475 stub_entry->h != NULL
11476 ? stub_entry->h->elf.root.root.string
11477 : "<local sym>");
721956f4 11478 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11479 htab->stub_error = TRUE;
11480 return FALSE;
721956f4
AM
11481 }
11482
176a0d42
AM
11483 r = NULL;
11484 if (info->emitrelocations)
11485 {
6f20ed8a 11486 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11487 ((PPC_HA (off) != 0)
11488 + (htab->opd_abi
e7d1c40c 11489 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11490 && PPC_HA (off + 16) == PPC_HA (off))
11491 : 1)));
176a0d42
AM
11492 if (r == NULL)
11493 return FALSE;
6f20ed8a 11494 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11495 if (bfd_big_endian (info->output_bfd))
11496 r[0].r_offset += 2;
1aa42141 11497 r[0].r_addend = targ;
176a0d42 11498 }
a7f2871e
AM
11499 if (stub_entry->h != NULL
11500 && (stub_entry->h == htab->tls_get_addr_fd
11501 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11502 && htab->params->tls_get_addr_opt)
794e51c0 11503 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11504 else
794e51c0 11505 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11506 break;
11507
a4b6fadd
AM
11508 case ppc_stub_save_res:
11509 return TRUE;
11510
721956f4
AM
11511 default:
11512 BFD_FAIL ();
b34976b6 11513 return FALSE;
721956f4
AM
11514 }
11515
1aa42141 11516 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 11517
e7d1c40c 11518 if (htab->params->emit_stub_syms)
97b639ba
AM
11519 {
11520 struct elf_link_hash_entry *h;
ee75fd95
AM
11521 size_t len1, len2;
11522 char *name;
11523 const char *const stub_str[] = { "long_branch",
05d0e962
AM
11524 "long_branch",
11525 "long_branch",
3f6ff479
AM
11526 "long_branch",
11527 "plt_branch",
ee75fd95 11528 "plt_branch",
05d0e962
AM
11529 "plt_branch",
11530 "plt_branch",
11531 "plt_call",
11532 "plt_call",
794e51c0 11533 "plt_call",
ee75fd95
AM
11534 "plt_call" };
11535
11536 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11537 len2 = strlen (stub_entry->root.string);
11538 name = bfd_malloc (len1 + len2 + 2);
11539 if (name == NULL)
11540 return FALSE;
11541 memcpy (name, stub_entry->root.string, 9);
11542 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11543 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11544 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11545 if (h == NULL)
11546 return FALSE;
11547 if (h->root.type == bfd_link_hash_new)
11548 {
11549 h->root.type = bfd_link_hash_defined;
6f20ed8a 11550 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11551 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11552 h->ref_regular = 1;
11553 h->def_regular = 1;
11554 h->ref_regular_nonweak = 1;
11555 h->forced_local = 1;
11556 h->non_elf = 0;
2ec55de3 11557 h->root.linker_def = 1;
97b639ba
AM
11558 }
11559 }
11560
b34976b6 11561 return TRUE;
721956f4
AM
11562}
11563
11564/* As above, but don't actually build the stub. Just bump offset so
11565 we know stub section sizes, and select plt_branch stubs where
11566 long_branch stubs won't do. */
11567
b34976b6 11568static bfd_boolean
4ce794b7 11569ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11570{
11571 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11572 struct bfd_link_info *info;
721956f4 11573 struct ppc_link_hash_table *htab;
f891966f
AM
11574 asection *plt;
11575 bfd_vma targ, off, r2off;
04bdff6a 11576 unsigned int size, extra, lr_used, delta, odd;
721956f4
AM
11577
11578 /* Massage our args to the form they really have. */
11579 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11580 info = in_arg;
11581
11582 htab = ppc_hash_table (info);
4dfe6ac6
NC
11583 if (htab == NULL)
11584 return FALSE;
721956f4 11585
1aa42141
AM
11586 /* Make a note of the offset within the stubs for this entry. */
11587 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11588
a4b6fadd
AM
11589 if (stub_entry->h != NULL
11590 && stub_entry->h->save_res
11591 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11592 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11593 {
11594 /* Don't make stubs to out-of-line register save/restore
11595 functions. Instead, emit copies of the functions. */
11596 stub_entry->group->needs_save_res = 1;
11597 stub_entry->stub_type = ppc_stub_save_res;
11598 return TRUE;
11599 }
11600
f891966f 11601 switch (stub_entry->stub_type)
721956f4 11602 {
f891966f
AM
11603 case ppc_stub_plt_branch:
11604 case ppc_stub_plt_branch_r2off:
11605 /* Reset the stub type from the plt branch variant in case we now
11606 can reach with a shorter stub. */
11607 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11608 /* Fall through. */
11609 case ppc_stub_long_branch:
11610 case ppc_stub_long_branch_r2off:
1aa42141
AM
11611 targ = (stub_entry->target_value
11612 + stub_entry->target_section->output_offset
11613 + stub_entry->target_section->output_section->vma);
f891966f 11614 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
1aa42141
AM
11615 off = (stub_entry->stub_offset
11616 + stub_entry->group->stub_sec->output_offset
11617 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11618
ad8e1ba5 11619 size = 4;
f891966f 11620 r2off = 0;
ad8e1ba5
AM
11621 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11622 {
25f53a85 11623 r2off = get_r2off (info, stub_entry);
a7c49797 11624 if (r2off == (bfd_vma) -1)
aa374f67
AM
11625 {
11626 htab->stub_error = TRUE;
11627 return FALSE;
11628 }
a7c49797 11629 size = 8;
ac2df442 11630 if (PPC_HA (r2off) != 0)
a7c49797
AM
11631 size += 4;
11632 if (PPC_LO (r2off) != 0)
11633 size += 4;
1aa42141 11634 off += size - 4;
ad8e1ba5 11635 }
1aa42141 11636 off = targ - off;
ad8e1ba5 11637
f891966f
AM
11638 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11639 Do the same for -R objects without function descriptors. */
11640 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11641 && r2off == 0
11642 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11643 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 11644 {
f891966f 11645 struct ppc_branch_hash_entry *br_entry;
df136d64 11646
f891966f
AM
11647 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11648 stub_entry->root.string + 9,
11649 TRUE, FALSE);
11650 if (br_entry == NULL)
721956f4 11651 {
f891966f
AM
11652 _bfd_error_handler (_("can't build branch stub `%s'"),
11653 stub_entry->root.string);
11654 htab->stub_error = TRUE;
11655 return FALSE;
721956f4
AM
11656 }
11657
f891966f 11658 if (br_entry->iter != htab->stub_iteration)
721956f4 11659 {
f891966f
AM
11660 br_entry->iter = htab->stub_iteration;
11661 br_entry->offset = htab->brlt->size;
11662 htab->brlt->size += 8;
63bc6f6c 11663
f891966f
AM
11664 if (htab->relbrlt != NULL)
11665 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11666 else if (info->emitrelocations)
84f5d08e 11667 {
f891966f
AM
11668 htab->brlt->reloc_count += 1;
11669 htab->brlt->flags |= SEC_RELOC;
05d0e962 11670 }
f891966f 11671 }
ac2df442 11672
f891966f
AM
11673 targ = (br_entry->offset
11674 + htab->brlt->output_offset
11675 + htab->brlt->output_section->vma);
11676 off = (elf_gp (info->output_bfd)
11677 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11678 off = targ - off;
176a0d42 11679
f891966f
AM
11680 if (info->emitrelocations)
11681 {
11682 stub_entry->group->stub_sec->reloc_count
11683 += 1 + (PPC_HA (off) != 0);
11684 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11685 }
05d0e962 11686
f891966f
AM
11687 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11688 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11689 {
11690 size = 12;
11691 if (PPC_HA (off) != 0)
11692 size = 16;
ac2df442 11693 }
f891966f 11694 else
ac2df442 11695 {
f891966f
AM
11696 size = 16;
11697 if (PPC_HA (off) != 0)
11698 size += 4;
11699
11700 if (PPC_HA (r2off) != 0)
11701 size += 4;
11702 if (PPC_LO (r2off) != 0)
11703 size += 4;
ac2df442 11704 }
721956f4 11705 }
f891966f
AM
11706 else if (info->emitrelocations)
11707 {
11708 stub_entry->group->stub_sec->reloc_count += 1;
11709 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11710 }
11711 break;
11712
11713 case ppc_stub_plt_branch_notoc:
11714 case ppc_stub_plt_branch_both:
11715 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11716 /* Fall through. */
11717 case ppc_stub_long_branch_notoc:
11718 case ppc_stub_long_branch_both:
11719 off = (stub_entry->stub_offset
11720 + stub_entry->group->stub_sec->output_offset
11721 + stub_entry->group->stub_sec->output_section->vma);
11722 size = 0;
11723 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11724 size = 4;
11725 off += size;
11726 targ = (stub_entry->target_value
11727 + stub_entry->target_section->output_offset
11728 + stub_entry->target_section->output_section->vma);
04bdff6a 11729 odd = off & 4;
f891966f
AM
11730 off = targ - off;
11731
3d58e1fc
AM
11732 if (info->emitrelocations)
11733 {
04bdff6a
AM
11734 unsigned int num_rel;
11735 if (htab->powerxx_stubs)
11736 num_rel = num_relocs_for_powerxx_offset (off, odd);
11737 else
11738 num_rel = num_relocs_for_offset (off - 8);
11739 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
11740 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11741 }
11742
04bdff6a
AM
11743 if (htab->powerxx_stubs)
11744 extra = size_powerxx_offset (off, odd);
11745 else
11746 extra = size_offset (off - 8);
f891966f
AM
11747 /* Include branch insn plus those in the offset sequence. */
11748 size += 4 + extra;
11749 /* The branch insn is at the end, or "extra" bytes along. So
11750 its offset will be "extra" bytes less that that already
11751 calculated. */
11752 off -= extra;
11753
04bdff6a
AM
11754 if (!htab->powerxx_stubs)
11755 {
11756 /* After the bcl, lr has been modified so we need to emit
11757 .eh_frame info saying the return address is in r12. */
11758 lr_used = stub_entry->stub_offset + 8;
11759 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11760 lr_used += 4;
11761 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11762 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11763 DW_CFA_restore_extended 65. */
11764 delta = lr_used - stub_entry->group->lr_restore;
11765 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11766 stub_entry->group->lr_restore = lr_used + 8;
11767 }
f891966f
AM
11768
11769 /* If the branch can't reach, use a plt_branch. */
11770 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11771 {
11772 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11773 - ppc_stub_long_branch_notoc);
11774 size += 4;
11775 }
3d58e1fc
AM
11776 else if (info->emitrelocations)
11777 stub_entry->group->stub_sec->reloc_count +=1;
f891966f
AM
11778 break;
11779
11780 case ppc_stub_plt_call_notoc:
11781 case ppc_stub_plt_call_both:
11782 off = (stub_entry->stub_offset
11783 + stub_entry->group->stub_sec->output_offset
11784 + stub_entry->group->stub_sec->output_section->vma);
11785 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11786 off += 4;
11787 targ = stub_entry->plt_ent->plt.offset & ~1;
11788 if (targ >= (bfd_vma) -2)
11789 abort ();
11790
11791 plt = htab->elf.splt;
11792 if (!htab->elf.dynamic_sections_created
11793 || stub_entry->h == NULL
11794 || stub_entry->h->elf.dynindx == -1)
11795 {
11796 if (stub_entry->symtype == STT_GNU_IFUNC)
11797 plt = htab->elf.iplt;
11798 else
11799 plt = htab->pltlocal;
11800 }
11801 targ += plt->output_offset + plt->output_section->vma;
04bdff6a 11802 odd = off & 4;
f891966f
AM
11803 off = targ - off;
11804
11805 if (htab->params->plt_stub_align != 0)
11806 {
11807 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11808
11809 stub_entry->group->stub_sec->size += pad;
11810 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11811 off -= pad;
11812 }
11813
3d58e1fc
AM
11814 if (info->emitrelocations)
11815 {
04bdff6a
AM
11816 unsigned int num_rel;
11817 if (htab->powerxx_stubs)
11818 num_rel = num_relocs_for_powerxx_offset (off, odd);
11819 else
11820 num_rel = num_relocs_for_offset (off - 8);
11821 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
11822 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11823 }
11824
f891966f
AM
11825 size = plt_stub_size (htab, stub_entry, off);
11826
04bdff6a
AM
11827 if (!htab->powerxx_stubs)
11828 {
11829 /* After the bcl, lr has been modified so we need to emit
11830 .eh_frame info saying the return address is in r12. */
11831 lr_used = stub_entry->stub_offset + 8;
11832 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11833 lr_used += 4;
11834 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11835 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11836 DW_CFA_restore_extended 65. */
11837 delta = lr_used - stub_entry->group->lr_restore;
11838 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11839 stub_entry->group->lr_restore = lr_used + 8;
11840 }
f891966f
AM
11841 break;
11842
11843 case ppc_stub_plt_call:
11844 case ppc_stub_plt_call_r2save:
11845 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11846 if (targ >= (bfd_vma) -2)
11847 abort ();
11848 plt = htab->elf.splt;
11849 if (!htab->elf.dynamic_sections_created
11850 || stub_entry->h == NULL
11851 || stub_entry->h->elf.dynindx == -1)
11852 {
11853 if (stub_entry->symtype == STT_GNU_IFUNC)
11854 plt = htab->elf.iplt;
11855 else
11856 plt = htab->pltlocal;
11857 }
11858 targ += plt->output_offset + plt->output_section->vma;
11859
11860 off = (elf_gp (info->output_bfd)
11861 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11862 off = targ - off;
11863
11864 if (htab->params->plt_stub_align != 0)
11865 {
11866 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11867
11868 stub_entry->group->stub_sec->size += pad;
11869 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11870 }
11871
11872 if (info->emitrelocations)
11873 {
11874 stub_entry->group->stub_sec->reloc_count
11875 += ((PPC_HA (off) != 0)
11876 + (htab->opd_abi
11877 ? 2 + (htab->params->plt_static_chain
11878 && PPC_HA (off + 16) == PPC_HA (off))
11879 : 1));
11880 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11881 }
11882
11883 size = plt_stub_size (htab, stub_entry, off);
11884
11885 if (stub_entry->h != NULL
11886 && (stub_entry->h == htab->tls_get_addr_fd
11887 || stub_entry->h == htab->tls_get_addr)
11888 && htab->params->tls_get_addr_opt
11889 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11890 {
11891 /* After the bctrl, lr has been modified so we need to
11892 emit .eh_frame info saying the return address is
11893 on the stack. In fact we put the EH info specifying
11894 that the return address is on the stack *at* the
11895 call rather than after it, because the EH info for a
11896 call needs to be specified by that point.
11897 See libgcc/unwind-dw2.c execute_cfa_program. */
11898 lr_used = stub_entry->stub_offset + size - 20;
11899 /* The eh_frame info will consist of a DW_CFA_advance_loc
11900 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11901 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11902 delta = lr_used - stub_entry->group->lr_restore;
11903 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11904 stub_entry->group->lr_restore = size - 4;
11905 }
11906 break;
11907
11908 default:
11909 BFD_FAIL ();
11910 return FALSE;
721956f4
AM
11911 }
11912
6f20ed8a 11913 stub_entry->group->stub_sec->size += size;
b34976b6 11914 return TRUE;
721956f4
AM
11915}
11916
11917/* Set up various things so that we can make a list of input sections
11918 for each output section included in the link. Returns -1 on error,
cedb70c5 11919 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11920
11921int
e7d1c40c 11922ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11923{
6f20ed8a 11924 unsigned int id;
721956f4
AM
11925 bfd_size_type amt;
11926 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11927
4dfe6ac6
NC
11928 if (htab == NULL)
11929 return -1;
4c52953f 11930
7cf7fcc8 11931 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
11932 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11933 htab->sec_info = bfd_zmalloc (amt);
11934 if (htab->sec_info == NULL)
721956f4
AM
11935 return -1;
11936
3d6f9012
AM
11937 /* Set toc_off for com, und, abs and ind sections. */
11938 for (id = 0; id < 3; id++)
6f20ed8a 11939 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11940
721956f4
AM
11941 return 1;
11942}
11943
927be08e
AM
11944/* Set up for first pass at multitoc partitioning. */
11945
11946void
11947ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11948{
11949 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11950
1c865ab2 11951 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11952 htab->toc_bfd = NULL;
11953 htab->toc_first_sec = NULL;
11954}
11955
e717da7e
AM
11956/* The linker repeatedly calls this function for each TOC input section
11957 and linker generated GOT section. Group input bfds such that the toc
927be08e 11958 within a group is less than 64k in size. */
ad8e1ba5 11959
927be08e 11960bfd_boolean
4ce794b7 11961ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11962{
11963 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11964 bfd_vma addr, off, limit;
ad8e1ba5 11965
4dfe6ac6
NC
11966 if (htab == NULL)
11967 return FALSE;
11968
927be08e 11969 if (!htab->second_toc_pass)
4c52953f 11970 {
927be08e 11971 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11972 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11973
11974 if (new_bfd)
bf102f86
AM
11975 {
11976 htab->toc_bfd = isec->owner;
11977 htab->toc_first_sec = isec;
11978 }
927be08e 11979
bf102f86
AM
11980 addr = isec->output_offset + isec->output_section->vma;
11981 off = addr - htab->toc_curr;
d77c8a4b
AM
11982 limit = 0x80008000;
11983 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11984 limit = 0x10000;
11985 if (off + isec->size > limit)
bf102f86
AM
11986 {
11987 addr = (htab->toc_first_sec->output_offset
11988 + htab->toc_first_sec->output_section->vma);
11989 htab->toc_curr = addr;
a27e685f 11990 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11991 }
99877b66 11992
927be08e
AM
11993 /* toc_curr is the base address of this toc group. Set elf_gp
11994 for the input section to be the offset relative to the
11995 output toc base plus 0x8000. Making the input elf_gp an
11996 offset allows us to move the toc as a whole without
11997 recalculating input elf_gp. */
06bcf541 11998 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
11999 off += TOC_BASE_OFF;
12000
12001 /* Die if someone uses a linker script that doesn't keep input
12002 file .toc and .got together. */
a4fd3de5
AM
12003 if (new_bfd
12004 && elf_gp (isec->owner) != 0
927be08e
AM
12005 && elf_gp (isec->owner) != off)
12006 return FALSE;
12007
12008 elf_gp (isec->owner) = off;
12009 return TRUE;
4c52953f 12010 }
927be08e
AM
12011
12012 /* During the second pass toc_first_sec points to the start of
12013 a toc group, and toc_curr is used to track the old elf_gp.
12014 We use toc_bfd to ensure we only look at each bfd once. */
12015 if (htab->toc_bfd == isec->owner)
12016 return TRUE;
12017 htab->toc_bfd = isec->owner;
12018
12019 if (htab->toc_first_sec == NULL
12020 || htab->toc_curr != elf_gp (isec->owner))
12021 {
12022 htab->toc_curr = elf_gp (isec->owner);
12023 htab->toc_first_sec = isec;
12024 }
12025 addr = (htab->toc_first_sec->output_offset
12026 + htab->toc_first_sec->output_section->vma);
06bcf541 12027 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
12028 elf_gp (isec->owner) = off;
12029
12030 return TRUE;
ad8e1ba5
AM
12031}
12032
927be08e
AM
12033/* Called via elf_link_hash_traverse to merge GOT entries for global
12034 symbol H. */
12035
12036static bfd_boolean
12037merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12038{
12039 if (h->root.type == bfd_link_hash_indirect)
12040 return TRUE;
12041
927be08e
AM
12042 merge_got_entries (&h->got.glist);
12043
12044 return TRUE;
12045}
12046
12047/* Called via elf_link_hash_traverse to allocate GOT entries for global
12048 symbol H. */
12049
12050static bfd_boolean
12051reallocate_got (struct elf_link_hash_entry *h, void *inf)
12052{
12053 struct got_entry *gent;
12054
12055 if (h->root.type == bfd_link_hash_indirect)
12056 return TRUE;
12057
927be08e
AM
12058 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12059 if (!gent->is_indirect)
12060 allocate_got (h, (struct bfd_link_info *) inf, gent);
12061 return TRUE;
12062}
12063
12064/* Called on the first multitoc pass after the last call to
12065 ppc64_elf_next_toc_section. This function removes duplicate GOT
12066 entries. */
12067
12068bfd_boolean
12069ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12070{
12071 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
12072 struct bfd *ibfd, *ibfd2;
12073 bfd_boolean done_something;
12074
12075 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12076
7865406b
AM
12077 if (!htab->do_multi_toc)
12078 return FALSE;
12079
d0fae19d 12080 /* Merge global sym got entries within a toc group. */
927be08e
AM
12081 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12082
12083 /* And tlsld_got. */
c72f2fb2 12084 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12085 {
12086 struct got_entry *ent, *ent2;
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 {
c72f2fb2 12095 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12096 {
12097 if (!is_ppc64_elf (ibfd2))
12098 continue;
12099
12100 ent2 = ppc64_tlsld_got (ibfd2);
12101 if (!ent2->is_indirect
12102 && ent2->got.offset != (bfd_vma) -1
12103 && elf_gp (ibfd2) == elf_gp (ibfd))
12104 {
12105 ent2->is_indirect = TRUE;
12106 ent2->got.ent = ent;
12107 }
12108 }
12109 }
12110 }
12111
12112 /* Zap sizes of got sections. */
33e44f2e
AM
12113 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12114 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12115 htab->got_reli_size = 0;
12116
c72f2fb2 12117 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12118 {
12119 asection *got, *relgot;
12120
12121 if (!is_ppc64_elf (ibfd))
12122 continue;
12123
12124 got = ppc64_elf_tdata (ibfd)->got;
12125 if (got != NULL)
12126 {
12127 got->rawsize = got->size;
12128 got->size = 0;
12129 relgot = ppc64_elf_tdata (ibfd)->relgot;
12130 relgot->rawsize = relgot->size;
12131 relgot->size = 0;
12132 }
12133 }
12134
12135 /* Now reallocate the got, local syms first. We don't need to
12136 allocate section contents again since we never increase size. */
c72f2fb2 12137 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12138 {
12139 struct got_entry **lgot_ents;
12140 struct got_entry **end_lgot_ents;
12141 struct plt_entry **local_plt;
12142 struct plt_entry **end_local_plt;
f961d9dd 12143 unsigned char *lgot_masks;
927be08e
AM
12144 bfd_size_type locsymcount;
12145 Elf_Internal_Shdr *symtab_hdr;
19e08130 12146 asection *s;
927be08e
AM
12147
12148 if (!is_ppc64_elf (ibfd))
12149 continue;
12150
12151 lgot_ents = elf_local_got_ents (ibfd);
12152 if (!lgot_ents)
12153 continue;
12154
12155 symtab_hdr = &elf_symtab_hdr (ibfd);
12156 locsymcount = symtab_hdr->sh_info;
12157 end_lgot_ents = lgot_ents + locsymcount;
12158 local_plt = (struct plt_entry **) end_lgot_ents;
12159 end_local_plt = local_plt + locsymcount;
f961d9dd 12160 lgot_masks = (unsigned char *) end_local_plt;
927be08e 12161 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
12162 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12163 {
12164 struct got_entry *ent;
12165
12166 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12167 {
19e08130
AM
12168 unsigned int ent_size = 8;
12169 unsigned int rel_size = sizeof (Elf64_External_Rela);
12170
d0fae19d
AM
12171 ent->got.offset = s->size;
12172 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12173 {
19e08130
AM
12174 ent_size *= 2;
12175 rel_size *= 2;
12176 }
12177 s->size += ent_size;
37da22e5 12178 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12179 {
33e44f2e 12180 htab->elf.irelplt->size += rel_size;
19e08130
AM
12181 htab->got_reli_size += rel_size;
12182 }
6a3858a6 12183 else if (bfd_link_pic (info)
f749f26e 12184 && !(ent->tls_type != 0
6a3858a6 12185 && bfd_link_executable (info)))
19e08130
AM
12186 {
12187 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12188 srel->size += rel_size;
d0fae19d
AM
12189 }
12190 }
927be08e
AM
12191 }
12192 }
12193
12194 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12195
c72f2fb2 12196 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12197 {
12198 struct got_entry *ent;
12199
12200 if (!is_ppc64_elf (ibfd))
12201 continue;
12202
12203 ent = ppc64_tlsld_got (ibfd);
12204 if (!ent->is_indirect
12205 && ent->got.offset != (bfd_vma) -1)
12206 {
12207 asection *s = ppc64_elf_tdata (ibfd)->got;
12208 ent->got.offset = s->size;
12209 s->size += 16;
f749f26e 12210 if (bfd_link_dll (info))
927be08e
AM
12211 {
12212 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12213 srel->size += sizeof (Elf64_External_Rela);
12214 }
12215 }
12216 }
12217
33e44f2e 12218 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12219 if (!done_something)
c72f2fb2 12220 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12221 {
12222 asection *got;
12223
12224 if (!is_ppc64_elf (ibfd))
12225 continue;
12226
12227 got = ppc64_elf_tdata (ibfd)->got;
12228 if (got != NULL)
12229 {
12230 done_something = got->rawsize != got->size;
12231 if (done_something)
12232 break;
12233 }
12234 }
12235
12236 if (done_something)
e7d1c40c 12237 (*htab->params->layout_sections_again) ();
927be08e
AM
12238
12239 /* Set up for second pass over toc sections to recalculate elf_gp
12240 on input sections. */
12241 htab->toc_bfd = NULL;
12242 htab->toc_first_sec = NULL;
12243 htab->second_toc_pass = TRUE;
12244 return done_something;
12245}
12246
12247/* Called after second pass of multitoc partitioning. */
12248
12249void
12250ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12251{
12252 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12253
12254 /* After the second pass, toc_curr tracks the TOC offset used
12255 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12256 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12257}
12258
9b5ecbd0
AM
12259/* No toc references were found in ISEC. If the code in ISEC makes no
12260 calls, then there's no need to use toc adjusting stubs when branching
12261 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12262 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12263 needed, and 2 if a cyclical call-graph was found but no other reason
12264 for a stub was detected. If called from the top level, a return of
12265 2 means the same as a return of 0. */
9b5ecbd0
AM
12266
12267static int
4ce794b7 12268toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12269{
9b5ecbd0 12270 int ret;
70cc837d
AM
12271
12272 /* Mark this section as checked. */
12273 isec->call_check_done = 1;
9b5ecbd0 12274
772119ce
AM
12275 /* We know none of our code bearing sections will need toc stubs. */
12276 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12277 return 0;
12278
eea6121a 12279 if (isec->size == 0)
082c50f8
AM
12280 return 0;
12281
4c52953f
AM
12282 if (isec->output_section == NULL)
12283 return 0;
12284
4c52953f 12285 ret = 0;
70cc837d 12286 if (isec->reloc_count != 0)
9b5ecbd0 12287 {
70cc837d
AM
12288 Elf_Internal_Rela *relstart, *rel;
12289 Elf_Internal_Sym *local_syms;
12290 struct ppc_link_hash_table *htab;
2917689a 12291
70cc837d
AM
12292 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12293 info->keep_memory);
12294 if (relstart == NULL)
12295 return -1;
90aecf7a 12296
70cc837d
AM
12297 /* Look for branches to outside of this section. */
12298 local_syms = NULL;
12299 htab = ppc_hash_table (info);
12300 if (htab == NULL)
12301 return -1;
4c52953f 12302
70cc837d 12303 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12304 {
70cc837d
AM
12305 enum elf_ppc64_reloc_type r_type;
12306 unsigned long r_symndx;
12307 struct elf_link_hash_entry *h;
12308 struct ppc_link_hash_entry *eh;
12309 Elf_Internal_Sym *sym;
12310 asection *sym_sec;
12311 struct _opd_sec_data *opd;
12312 bfd_vma sym_value;
12313 bfd_vma dest;
12314
12315 r_type = ELF64_R_TYPE (rel->r_info);
12316 if (r_type != R_PPC64_REL24
05d0e962 12317 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
12318 && r_type != R_PPC64_REL14
12319 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 12320 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
12321 && r_type != R_PPC64_PLTCALL
12322 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 12323 continue;
4c52953f 12324
70cc837d
AM
12325 r_symndx = ELF64_R_SYM (rel->r_info);
12326 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12327 isec->owner))
4c52953f 12328 {
70cc837d
AM
12329 ret = -1;
12330 break;
12331 }
4c52953f 12332
70cc837d
AM
12333 /* Calls to dynamic lib functions go through a plt call stub
12334 that uses r2. */
12335 eh = (struct ppc_link_hash_entry *) h;
12336 if (eh != NULL
12337 && (eh->elf.plt.plist != NULL
12338 || (eh->oh != NULL
12339 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12340 {
12341 ret = 1;
12342 break;
4c52953f
AM
12343 }
12344
70cc837d
AM
12345 if (sym_sec == NULL)
12346 /* Ignore other undefined symbols. */
4c52953f 12347 continue;
4c52953f 12348
70cc837d
AM
12349 /* Assume branches to other sections not included in the
12350 link need stubs too, to cover -R and absolute syms. */
12351 if (sym_sec->output_section == NULL)
12352 {
12353 ret = 1;
12354 break;
12355 }
4c52953f 12356
70cc837d
AM
12357 if (h == NULL)
12358 sym_value = sym->st_value;
12359 else
12360 {
12361 if (h->root.type != bfd_link_hash_defined
12362 && h->root.type != bfd_link_hash_defweak)
12363 abort ();
12364 sym_value = h->root.u.def.value;
12365 }
12366 sym_value += rel->r_addend;
4c52953f 12367
70cc837d
AM
12368 /* If this branch reloc uses an opd sym, find the code section. */
12369 opd = get_opd_info (sym_sec);
12370 if (opd != NULL)
12371 {
12372 if (h == NULL && opd->adjust != NULL)
12373 {
12374 long adjust;
4c52953f 12375
92a9c616 12376 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12377 if (adjust == -1)
12378 /* Assume deleted functions won't ever be called. */
12379 continue;
12380 sym_value += adjust;
12381 }
4c52953f 12382
aef36ac1
AM
12383 dest = opd_entry_value (sym_sec, sym_value,
12384 &sym_sec, NULL, FALSE);
70cc837d
AM
12385 if (dest == (bfd_vma) -1)
12386 continue;
12387 }
12388 else
12389 dest = (sym_value
12390 + sym_sec->output_offset
12391 + sym_sec->output_section->vma);
4c52953f 12392
70cc837d
AM
12393 /* Ignore branch to self. */
12394 if (sym_sec == isec)
12395 continue;
4c52953f 12396
70cc837d
AM
12397 /* If the called function uses the toc, we need a stub. */
12398 if (sym_sec->has_toc_reloc
12399 || sym_sec->makes_toc_func_call)
4c52953f 12400 {
70cc837d 12401 ret = 1;
4c52953f
AM
12402 break;
12403 }
70cc837d
AM
12404
12405 /* Assume any branch that needs a long branch stub might in fact
12406 need a plt_branch stub. A plt_branch stub uses r2. */
12407 else if (dest - (isec->output_offset
12408 + isec->output_section->vma
6911b7dc
AM
12409 + rel->r_offset) + (1 << 25)
12410 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12411 ? h->other
12412 : sym->st_other))
4c52953f 12413 {
70cc837d
AM
12414 ret = 1;
12415 break;
12416 }
12417
12418 /* If calling back to a section in the process of being
12419 tested, we can't say for sure that no toc adjusting stubs
12420 are needed, so don't return zero. */
12421 else if (sym_sec->call_check_in_progress)
12422 ret = 2;
12423
12424 /* Branches to another section that itself doesn't have any TOC
12425 references are OK. Recursively call ourselves to check. */
12426 else if (!sym_sec->call_check_done)
12427 {
12428 int recur;
12429
12430 /* Mark current section as indeterminate, so that other
12431 sections that call back to current won't be marked as
12432 known. */
12433 isec->call_check_in_progress = 1;
12434 recur = toc_adjusting_stub_needed (info, sym_sec);
12435 isec->call_check_in_progress = 0;
12436
4c52953f
AM
12437 if (recur != 0)
12438 {
70cc837d
AM
12439 ret = recur;
12440 if (recur != 2)
12441 break;
4c52953f
AM
12442 }
12443 }
4c52953f 12444 }
70cc837d
AM
12445
12446 if (local_syms != NULL
12447 && (elf_symtab_hdr (isec->owner).contents
12448 != (unsigned char *) local_syms))
12449 free (local_syms);
12450 if (elf_section_data (isec)->relocs != relstart)
12451 free (relstart);
9b5ecbd0
AM
12452 }
12453
70cc837d
AM
12454 if ((ret & 1) == 0
12455 && isec->map_head.s != NULL
12456 && (strcmp (isec->output_section->name, ".init") == 0
12457 || strcmp (isec->output_section->name, ".fini") == 0))
12458 {
12459 if (isec->map_head.s->has_toc_reloc
12460 || isec->map_head.s->makes_toc_func_call)
12461 ret = 1;
12462 else if (!isec->map_head.s->call_check_done)
12463 {
12464 int recur;
12465 isec->call_check_in_progress = 1;
12466 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12467 isec->call_check_in_progress = 0;
12468 if (recur != 0)
12469 ret = recur;
12470 }
12471 }
12472
12473 if (ret == 1)
12474 isec->makes_toc_func_call = 1;
4c52953f 12475
9b5ecbd0
AM
12476 return ret;
12477}
12478
721956f4
AM
12479/* The linker repeatedly calls this function for each input section,
12480 in the order that input sections are linked into output sections.
12481 Build lists of input sections to determine groupings between which
12482 we may insert linker stubs. */
12483
9b5ecbd0 12484bfd_boolean
4ce794b7 12485ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12486{
12487 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12488
4dfe6ac6
NC
12489 if (htab == NULL)
12490 return FALSE;
12491
734b6cf9 12492 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12493 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12494 {
3d6f9012
AM
12495 /* This happens to make the list in reverse order,
12496 which is what we want. */
6f20ed8a
AM
12497 htab->sec_info[isec->id].u.list
12498 = htab->sec_info[isec->output_section->id].u.list;
12499 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12500 }
ad8e1ba5 12501
4c52953f 12502 if (htab->multi_toc_needed)
9b5ecbd0 12503 {
8b974ba3
AM
12504 /* Analyse sections that aren't already flagged as needing a
12505 valid toc pointer. Exclude .fixup for the linux kernel.
12506 .fixup contains branches, but only back to the function that
12507 hit an exception. */
12508 if (!(isec->has_toc_reloc
12509 || (isec->flags & SEC_CODE) == 0
12510 || strcmp (isec->name, ".fixup") == 0
12511 || isec->call_check_done))
12512 {
12513 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12514 return FALSE;
8b974ba3
AM
12515 }
12516 /* Make all sections use the TOC assigned for this object file.
12517 This will be wrong for pasted sections; We fix that in
12518 check_pasted_section(). */
12519 if (elf_gp (isec->owner) != 0)
12520 htab->toc_curr = elf_gp (isec->owner);
12521 }
12522
6f20ed8a 12523 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12524 return TRUE;
721956f4
AM
12525}
12526
70cc837d
AM
12527/* Check that all .init and .fini sections use the same toc, if they
12528 have toc relocs. */
12529
12530static bfd_boolean
12531check_pasted_section (struct bfd_link_info *info, const char *name)
12532{
12533 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12534
12535 if (o != NULL)
12536 {
12537 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12538 bfd_vma toc_off = 0;
12539 asection *i;
12540
12541 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12542 if (i->has_toc_reloc)
12543 {
12544 if (toc_off == 0)
6f20ed8a
AM
12545 toc_off = htab->sec_info[i->id].toc_off;
12546 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12547 return FALSE;
12548 }
6683a28d
AM
12549
12550 if (toc_off == 0)
12551 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12552 if (i->makes_toc_func_call)
12553 {
6f20ed8a 12554 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12555 break;
12556 }
12557
70cc837d
AM
12558 /* Make sure the whole pasted function uses the same toc offset. */
12559 if (toc_off != 0)
12560 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12561 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12562 }
12563 return TRUE;
12564}
12565
12566bfd_boolean
12567ppc64_elf_check_init_fini (struct bfd_link_info *info)
12568{
12569 return (check_pasted_section (info, ".init")
12570 & check_pasted_section (info, ".fini"));
12571}
12572
721956f4
AM
12573/* See whether we can group stub sections together. Grouping stub
12574 sections may result in fewer stubs. More importantly, we need to
12575 put all .init* and .fini* stubs at the beginning of the .init or
12576 .fini output sections respectively, because glibc splits the
12577 _init and _fini functions into multiple parts. Putting a stub in
12578 the middle of a function is not a good idea. */
12579
6f20ed8a
AM
12580static bfd_boolean
12581group_sections (struct bfd_link_info *info,
4ce794b7
AM
12582 bfd_size_type stub_group_size,
12583 bfd_boolean stubs_always_before_branch)
721956f4 12584{
6f20ed8a
AM
12585 struct ppc_link_hash_table *htab;
12586 asection *osec;
7c8fe5c4
AM
12587 bfd_boolean suppress_size_errors;
12588
6f20ed8a
AM
12589 htab = ppc_hash_table (info);
12590 if (htab == NULL)
12591 return FALSE;
12592
7c8fe5c4 12593 suppress_size_errors = FALSE;
7c8fe5c4
AM
12594 if (stub_group_size == 1)
12595 {
12596 /* Default values. */
12597 if (stubs_always_before_branch)
09f92717 12598 stub_group_size = 0x1e00000;
7c8fe5c4 12599 else
09f92717 12600 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12601 suppress_size_errors = TRUE;
12602 }
12603
6f20ed8a 12604 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12605 {
6f20ed8a
AM
12606 asection *tail;
12607
12608 if (osec->id >= htab->sec_info_arr_size)
12609 continue;
12610
12611 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12612 while (tail != NULL)
721956f4 12613 {
734b6cf9
AM
12614 asection *curr;
12615 asection *prev;
12616 bfd_size_type total;
12617 bfd_boolean big_sec;
12618 bfd_vma curr_toc;
6f20ed8a 12619 struct map_stub *group;
09f92717 12620 bfd_size_type group_size;
734b6cf9
AM
12621
12622 curr = tail;
eea6121a 12623 total = tail->size;
09f92717
AM
12624 group_size = (ppc64_elf_section_data (tail) != NULL
12625 && ppc64_elf_section_data (tail)->has_14bit_branch
12626 ? stub_group_size >> 10 : stub_group_size);
12627
12628 big_sec = total > group_size;
7c8fe5c4 12629 if (big_sec && !suppress_size_errors)
695344c0 12630 /* xgettext:c-format */
871b3ab2 12631 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 12632 tail->owner, tail);
6f20ed8a 12633 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12634
6f20ed8a 12635 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12636 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12637 < (ppc64_elf_section_data (prev) != NULL
12638 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12639 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12640 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12641 curr = prev;
12642
12643 /* OK, the size from the start of CURR to the end is less
09f92717 12644 than group_size and thus can be handled by one stub
734b6cf9 12645 section. (or the tail section is itself larger than
09f92717
AM
12646 group_size, in which case we may be toast.) We should
12647 really be keeping track of the total size of stubs added
12648 here, as stubs contribute to the final output section
12649 size. That's a little tricky, and this way will only
12650 break if stubs added make the total size more than 2^25,
12651 ie. for the default stub_group_size, if stubs total more
12652 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12653 group = bfd_alloc (curr->owner, sizeof (*group));
12654 if (group == NULL)
12655 return FALSE;
12656 group->link_sec = curr;
12657 group->stub_sec = NULL;
a4b6fadd 12658 group->needs_save_res = 0;
df136d64
AM
12659 group->lr_restore = 0;
12660 group->eh_size = 0;
12661 group->eh_base = 0;
a4b6fadd
AM
12662 group->next = htab->group;
12663 htab->group = group;
734b6cf9 12664 do
721956f4 12665 {
6f20ed8a 12666 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12667 /* Set up this stub group. */
6f20ed8a 12668 htab->sec_info[tail->id].u.group = group;
721956f4 12669 }
734b6cf9
AM
12670 while (tail != curr && (tail = prev) != NULL);
12671
09f92717 12672 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12673 bytes before the stub section can be handled by it too.
12674 Don't do this if we have a really large section after the
12675 stubs, as adding more stubs increases the chance that
12676 branches may not reach into the stub section. */
12677 if (!stubs_always_before_branch && !big_sec)
12678 {
12679 total = 0;
12680 while (prev != NULL
12681 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12682 < (ppc64_elf_section_data (prev) != NULL
12683 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
12684 ? (group_size = stub_group_size >> 10)
12685 : group_size))
6f20ed8a 12686 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12687 {
12688 tail = prev;
6f20ed8a
AM
12689 prev = htab->sec_info[tail->id].u.list;
12690 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12691 }
12692 }
12693 tail = prev;
721956f4
AM
12694 }
12695 }
6f20ed8a 12696 return TRUE;
721956f4
AM
12697}
12698
58d180e8
AM
12699static const unsigned char glink_eh_frame_cie[] =
12700{
12701 0, 0, 0, 16, /* length. */
12702 0, 0, 0, 0, /* id. */
12703 1, /* CIE version. */
12704 'z', 'R', 0, /* Augmentation string. */
12705 4, /* Code alignment. */
12706 0x78, /* Data alignment. */
12707 65, /* RA reg. */
12708 1, /* Augmentation size. */
12709 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12710 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12711};
12712
d969d15f
AM
12713/* Stripping output sections is normally done before dynamic section
12714 symbols have been allocated. This function is called later, and
12715 handles cases like htab->brlt which is mapped to its own output
12716 section. */
12717
12718static void
12719maybe_strip_output (struct bfd_link_info *info, asection *isec)
12720{
12721 if (isec->size == 0
12722 && isec->output_section->size == 0
53d8967a 12723 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12724 && !bfd_section_removed_from_list (info->output_bfd,
12725 isec->output_section)
12726 && elf_section_data (isec->output_section)->dynindx == 0)
12727 {
12728 isec->output_section->flags |= SEC_EXCLUDE;
12729 bfd_section_list_remove (info->output_bfd, isec->output_section);
12730 info->output_bfd->section_count--;
12731 }
12732}
12733
721956f4
AM
12734/* Determine and set the size of the stub section for a final link.
12735
12736 The basic idea here is to examine all the relocations looking for
12737 PC-relative calls to a target that is unreachable with a "bl"
12738 instruction. */
12739
b34976b6 12740bfd_boolean
e7d1c40c 12741ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12742{
12743 bfd_size_type stub_group_size;
b34976b6 12744 bfd_boolean stubs_always_before_branch;
721956f4
AM
12745 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12746
4dfe6ac6
NC
12747 if (htab == NULL)
12748 return FALSE;
12749
0e1862bb 12750 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12751 htab->params->plt_thread_safe = 1;
b9e5796b 12752 if (!htab->opd_abi)
e7d1c40c
AM
12753 htab->params->plt_thread_safe = 0;
12754 else if (htab->params->plt_thread_safe == -1)
794e51c0 12755 {
e2458743 12756 static const char *const thread_starter[] =
794e51c0
AM
12757 {
12758 "pthread_create",
12759 /* libstdc++ */
12760 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12761 /* librt */
12762 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12763 "mq_notify", "create_timer",
12764 /* libanl */
12765 "getaddrinfo_a",
12766 /* libgomp */
2300b5a1 12767 "GOMP_parallel",
794e51c0 12768 "GOMP_parallel_start",
2300b5a1 12769 "GOMP_parallel_loop_static",
794e51c0 12770 "GOMP_parallel_loop_static_start",
2300b5a1 12771 "GOMP_parallel_loop_dynamic",
794e51c0 12772 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12773 "GOMP_parallel_loop_guided",
794e51c0 12774 "GOMP_parallel_loop_guided_start",
2300b5a1 12775 "GOMP_parallel_loop_runtime",
794e51c0 12776 "GOMP_parallel_loop_runtime_start",
2300b5a1 12777 "GOMP_parallel_sections",
68ffbac6 12778 "GOMP_parallel_sections_start",
f9dffbf0
AM
12779 /* libgo */
12780 "__go_go",
794e51c0
AM
12781 };
12782 unsigned i;
12783
a4b6fadd 12784 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12785 {
12786 struct elf_link_hash_entry *h;
12787 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12788 FALSE, FALSE, TRUE);
e7d1c40c
AM
12789 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12790 if (htab->params->plt_thread_safe)
794e51c0
AM
12791 break;
12792 }
12793 }
e7d1c40c
AM
12794 stubs_always_before_branch = htab->params->group_size < 0;
12795 if (htab->params->group_size < 0)
12796 stub_group_size = -htab->params->group_size;
721956f4 12797 else
e7d1c40c 12798 stub_group_size = htab->params->group_size;
721956f4 12799
6f20ed8a
AM
12800 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12801 return FALSE;
721956f4 12802
c9301e31
AM
12803#define STUB_SHRINK_ITER 20
12804 /* Loop until no stubs added. After iteration 20 of this loop we may
12805 exit on a stub section shrinking. This is to break out of a
12806 pathological case where adding stubs on one iteration decreases
12807 section gaps (perhaps due to alignment), which then requires
12808 fewer or smaller stubs on the next iteration. */
12809
721956f4
AM
12810 while (1)
12811 {
12812 bfd *input_bfd;
12813 unsigned int bfd_indx;
a4b6fadd 12814 struct map_stub *group;
721956f4
AM
12815
12816 htab->stub_iteration += 1;
721956f4
AM
12817
12818 for (input_bfd = info->input_bfds, bfd_indx = 0;
12819 input_bfd != NULL;
c72f2fb2 12820 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12821 {
12822 Elf_Internal_Shdr *symtab_hdr;
12823 asection *section;
6cdc0ccc 12824 Elf_Internal_Sym *local_syms = NULL;
721956f4 12825
0c8d6e5c 12826 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12827 continue;
12828
721956f4 12829 /* We'll need the symbol table in a second. */
0ffa91dd 12830 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12831 if (symtab_hdr->sh_info == 0)
12832 continue;
12833
721956f4
AM
12834 /* Walk over each section attached to the input bfd. */
12835 for (section = input_bfd->sections;
12836 section != NULL;
12837 section = section->next)
12838 {
721956f4 12839 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12840
12841 /* If there aren't any relocs, then there's nothing more
12842 to do. */
12843 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12844 || (section->flags & SEC_ALLOC) == 0
12845 || (section->flags & SEC_LOAD) == 0
12846 || (section->flags & SEC_CODE) == 0
721956f4
AM
12847 || section->reloc_count == 0)
12848 continue;
12849
12850 /* If this section is a link-once section that will be
12851 discarded, then don't create any stubs. */
12852 if (section->output_section == NULL
927be08e 12853 || section->output_section->owner != info->output_bfd)
721956f4
AM
12854 continue;
12855
1e2f5b6e
AM
12856 /* Get the relocs. */
12857 internal_relocs
4ce794b7 12858 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12859 info->keep_memory);
721956f4 12860 if (internal_relocs == NULL)
1e2f5b6e 12861 goto error_ret_free_local;
721956f4
AM
12862
12863 /* Now examine each relocation. */
12864 irela = internal_relocs;
12865 irelaend = irela + section->reloc_count;
12866 for (; irela < irelaend; irela++)
12867 {
4ce794b7
AM
12868 enum elf_ppc64_reloc_type r_type;
12869 unsigned int r_indx;
721956f4
AM
12870 enum ppc_stub_type stub_type;
12871 struct ppc_stub_hash_entry *stub_entry;
8387904d 12872 asection *sym_sec, *code_sec;
e054468f 12873 bfd_vma sym_value, code_value;
721956f4 12874 bfd_vma destination;
6911b7dc 12875 unsigned long local_off;
8843416a 12876 bfd_boolean ok_dest;
721956f4 12877 struct ppc_link_hash_entry *hash;
8387904d 12878 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12879 struct elf_link_hash_entry *h;
12880 Elf_Internal_Sym *sym;
721956f4
AM
12881 char *stub_name;
12882 const asection *id_sec;
74f0fb50 12883 struct _opd_sec_data *opd;
e054468f 12884 struct plt_entry *plt_ent;
721956f4
AM
12885
12886 r_type = ELF64_R_TYPE (irela->r_info);
12887 r_indx = ELF64_R_SYM (irela->r_info);
12888
4ce794b7 12889 if (r_type >= R_PPC64_max)
721956f4
AM
12890 {
12891 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12892 goto error_ret_free_internal;
721956f4
AM
12893 }
12894
12895 /* Only look for stubs on branch instructions. */
4ce794b7 12896 if (r_type != R_PPC64_REL24
05d0e962 12897 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
12898 && r_type != R_PPC64_REL14
12899 && r_type != R_PPC64_REL14_BRTAKEN
12900 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12901 continue;
12902
12903 /* Now determine the call target, its name, value,
12904 section. */
411e1bfb
AM
12905 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12906 r_indx, input_bfd))
12907 goto error_ret_free_internal;
12908 hash = (struct ppc_link_hash_entry *) h;
12909
8843416a 12910 ok_dest = FALSE;
8387904d 12911 fdh = NULL;
7fe2b9a6 12912 sym_value = 0;
411e1bfb 12913 if (hash == NULL)
721956f4 12914 {
411e1bfb 12915 sym_value = sym->st_value;
c27b8c2a
AM
12916 if (sym_sec != NULL
12917 && sym_sec->output_section != NULL)
12918 ok_dest = TRUE;
721956f4 12919 }
7fe2b9a6
AM
12920 else if (hash->elf.root.type == bfd_link_hash_defined
12921 || hash->elf.root.type == bfd_link_hash_defweak)
12922 {
12923 sym_value = hash->elf.root.u.def.value;
12924 if (sym_sec->output_section != NULL)
12925 ok_dest = TRUE;
12926 }
12927 else if (hash->elf.root.type == bfd_link_hash_undefweak
12928 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12929 {
99877b66 12930 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12931 use the func descriptor sym instead if it is
12932 defined. */
ceb1f1ef 12933 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12934 && hash->oh != NULL)
8387904d 12935 {
8c5b4e52 12936 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12937 if (fdh->elf.root.type == bfd_link_hash_defined
12938 || fdh->elf.root.type == bfd_link_hash_defweak)
12939 {
12940 sym_sec = fdh->elf.root.u.def.section;
12941 sym_value = fdh->elf.root.u.def.value;
12942 if (sym_sec->output_section != NULL)
12943 ok_dest = TRUE;
12944 }
99877b66
AM
12945 else
12946 fdh = NULL;
8387904d 12947 }
7fe2b9a6
AM
12948 }
12949 else
12950 {
12951 bfd_set_error (bfd_error_bad_value);
12952 goto error_ret_free_internal;
721956f4
AM
12953 }
12954
8843416a 12955 destination = 0;
6911b7dc 12956 local_off = 0;
8843416a
AM
12957 if (ok_dest)
12958 {
12959 sym_value += irela->r_addend;
12960 destination = (sym_value
12961 + sym_sec->output_offset
12962 + sym_sec->output_section->vma);
6911b7dc
AM
12963 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12964 ? hash->elf.other
12965 : sym->st_other);
8843416a
AM
12966 }
12967
8387904d 12968 code_sec = sym_sec;
e054468f 12969 code_value = sym_value;
74f0fb50
AM
12970 opd = get_opd_info (sym_sec);
12971 if (opd != NULL)
8387904d
AM
12972 {
12973 bfd_vma dest;
12974
74f0fb50 12975 if (hash == NULL && opd->adjust != NULL)
8387904d 12976 {
51aecdc5 12977 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12978 if (adjust == -1)
12979 continue;
e054468f 12980 code_value += adjust;
8387904d
AM
12981 sym_value += adjust;
12982 }
12983 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12984 &code_sec, &code_value, FALSE);
8387904d
AM
12985 if (dest != (bfd_vma) -1)
12986 {
12987 destination = dest;
12988 if (fdh != NULL)
12989 {
12990 /* Fixup old ABI sym to point at code
12991 entry. */
99877b66 12992 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12993 hash->elf.root.u.def.section = code_sec;
e054468f 12994 hash->elf.root.u.def.value = code_value;
8387904d
AM
12995 }
12996 }
12997 }
12998
721956f4 12999 /* Determine what (if any) linker stub is needed. */
e054468f 13000 plt_ent = NULL;
721956f4 13001 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
13002 &plt_ent, destination,
13003 local_off);
ad8e1ba5 13004
05d0e962
AM
13005 if (r_type == R_PPC64_REL24_NOTOC)
13006 {
13007 if (stub_type == ppc_stub_plt_call)
13008 stub_type = ppc_stub_plt_call_notoc;
13009 else if (stub_type == ppc_stub_long_branch
13010 || (code_sec != NULL
13011 && code_sec->output_section != NULL
13012 && (((hash ? hash->elf.other : sym->st_other)
13013 & STO_PPC64_LOCAL_MASK)
4a4e7361 13014 > 1 << STO_PPC64_LOCAL_BIT)))
05d0e962
AM
13015 stub_type = ppc_stub_long_branch_notoc;
13016 }
13017 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
13018 {
13019 /* Check whether we need a TOC adjusting stub.
13020 Since the linker pastes together pieces from
13021 different object files when creating the
13022 _init and _fini functions, it may be that a
13023 call to what looks like a local sym is in
13024 fact a call needing a TOC adjustment. */
33cb30a1
AM
13025 if ((code_sec != NULL
13026 && code_sec->output_section != NULL
13027 && (htab->sec_info[code_sec->id].toc_off
13028 != htab->sec_info[section->id].toc_off)
13029 && (code_sec->has_toc_reloc
13030 || code_sec->makes_toc_func_call))
13031 || (((hash ? hash->elf.other : sym->st_other)
13032 & STO_PPC64_LOCAL_MASK)
13033 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
13034 stub_type = ppc_stub_long_branch_r2off;
13035 }
13036
721956f4
AM
13037 if (stub_type == ppc_stub_none)
13038 continue;
13039
411e1bfb
AM
13040 /* __tls_get_addr calls might be eliminated. */
13041 if (stub_type != ppc_stub_plt_call
05d0e962 13042 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 13043 && hash != NULL
8387904d
AM
13044 && (hash == htab->tls_get_addr
13045 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
13046 && section->has_tls_reloc
13047 && irela != internal_relocs)
13048 {
13049 /* Get tls info. */
f961d9dd 13050 unsigned char *tls_mask;
411e1bfb 13051
3a71aa26 13052 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
13053 irela - 1, input_bfd))
13054 goto error_ret_free_internal;
37da22e5 13055 if ((*tls_mask & TLS_TLS) != 0)
411e1bfb
AM
13056 continue;
13057 }
13058
f378ab09 13059 if (stub_type == ppc_stub_plt_call)
794e51c0 13060 {
6e1816be
AM
13061 if (!htab->opd_abi
13062 && htab->params->plt_localentry0 != 0
13063 && is_elfv2_localentry0 (&hash->elf))
13064 htab->has_plt_localentry0 = 1;
13065 else if (irela + 1 < irelaend
13066 && irela[1].r_offset == irela->r_offset + 4
13067 && (ELF64_R_TYPE (irela[1].r_info)
13068 == R_PPC64_TOCSAVE))
f378ab09
AM
13069 {
13070 if (!tocsave_find (htab, INSERT,
13071 &local_syms, irela + 1, input_bfd))
13072 goto error_ret_free_internal;
13073 }
f378ab09
AM
13074 else
13075 stub_type = ppc_stub_plt_call_r2save;
794e51c0 13076 }
3b421ab3 13077
721956f4 13078 /* Support for grouping stub sections. */
6f20ed8a 13079 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
13080
13081 /* Get the name of this stub. */
13082 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13083 if (!stub_name)
13084 goto error_ret_free_internal;
13085
13086 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 13087 stub_name, FALSE, FALSE);
721956f4
AM
13088 if (stub_entry != NULL)
13089 {
05d0e962
AM
13090 enum ppc_stub_type old_type;
13091 /* A stub has already been created, but it may
13092 not be the required type. We shouldn't be
13093 transitioning from plt_call to long_branch
13094 stubs or vice versa, but we might be
13095 upgrading from plt_call to plt_call_r2save or
13096 from long_branch to long_branch_r2off. */
721956f4 13097 free (stub_name);
05d0e962
AM
13098 old_type = stub_entry->stub_type;
13099 switch (old_type)
13100 {
13101 default:
13102 abort ();
13103
13104 case ppc_stub_save_res:
13105 continue;
13106
13107 case ppc_stub_plt_call:
13108 case ppc_stub_plt_call_r2save:
13109 case ppc_stub_plt_call_notoc:
13110 case ppc_stub_plt_call_both:
13111 if (stub_type == ppc_stub_plt_call)
13112 continue;
13113 else if (stub_type == ppc_stub_plt_call_r2save)
13114 {
13115 if (old_type == ppc_stub_plt_call_notoc)
13116 stub_type = ppc_stub_plt_call_both;
13117 }
13118 else if (stub_type == ppc_stub_plt_call_notoc)
13119 {
13120 if (old_type == ppc_stub_plt_call_r2save)
13121 stub_type = ppc_stub_plt_call_both;
13122 }
13123 else
13124 abort ();
13125 break;
13126
13127 case ppc_stub_plt_branch:
13128 case ppc_stub_plt_branch_r2off:
13129 case ppc_stub_plt_branch_notoc:
13130 case ppc_stub_plt_branch_both:
13131 old_type += (ppc_stub_long_branch
13132 - ppc_stub_plt_branch);
13133 /* Fall through. */
13134 case ppc_stub_long_branch:
13135 case ppc_stub_long_branch_r2off:
13136 case ppc_stub_long_branch_notoc:
13137 case ppc_stub_long_branch_both:
13138 if (stub_type == ppc_stub_long_branch)
13139 continue;
13140 else if (stub_type == ppc_stub_long_branch_r2off)
13141 {
13142 if (old_type == ppc_stub_long_branch_notoc)
13143 stub_type = ppc_stub_long_branch_both;
13144 }
13145 else if (stub_type == ppc_stub_long_branch_notoc)
13146 {
13147 if (old_type == ppc_stub_long_branch_r2off)
13148 stub_type = ppc_stub_long_branch_both;
13149 }
13150 else
13151 abort ();
13152 break;
13153 }
13154 if (old_type < stub_type)
794e51c0 13155 stub_entry->stub_type = stub_type;
721956f4
AM
13156 continue;
13157 }
13158
25f53a85 13159 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
13160 if (stub_entry == NULL)
13161 {
13162 free (stub_name);
6cdc0ccc
AM
13163 error_ret_free_internal:
13164 if (elf_section_data (section)->relocs == NULL)
13165 free (internal_relocs);
13166 error_ret_free_local:
13167 if (local_syms != NULL
13168 && (symtab_hdr->contents
13169 != (unsigned char *) local_syms))
13170 free (local_syms);
b34976b6 13171 return FALSE;
721956f4
AM
13172 }
13173
ad8e1ba5 13174 stub_entry->stub_type = stub_type;
05d0e962
AM
13175 if (stub_type >= ppc_stub_plt_call
13176 && stub_type <= ppc_stub_plt_call_both)
e054468f 13177 {
05d0e962
AM
13178 stub_entry->target_value = sym_value;
13179 stub_entry->target_section = sym_sec;
e054468f
AM
13180 }
13181 else
13182 {
05d0e962
AM
13183 stub_entry->target_value = code_value;
13184 stub_entry->target_section = code_sec;
e054468f 13185 }
721956f4 13186 stub_entry->h = hash;
e054468f 13187 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
13188 stub_entry->symtype
13189 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 13190 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 13191
3d58e1fc
AM
13192 if (hash != NULL
13193 && (hash->elf.root.type == bfd_link_hash_defined
13194 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 13195 htab->stub_globals += 1;
721956f4
AM
13196 }
13197
13198 /* We're done with the internal relocs, free them. */
6cdc0ccc 13199 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 13200 free (internal_relocs);
721956f4 13201 }
6cdc0ccc
AM
13202
13203 if (local_syms != NULL
13204 && symtab_hdr->contents != (unsigned char *) local_syms)
13205 {
13206 if (!info->keep_memory)
13207 free (local_syms);
13208 else
13209 symtab_hdr->contents = (unsigned char *) local_syms;
13210 }
721956f4
AM
13211 }
13212
5c3dead3 13213 /* We may have added some stubs. Find out the new size of the
721956f4 13214 stub sections. */
d4aaa2a0 13215 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13216 {
13217 group->lr_restore = 0;
13218 group->eh_size = 0;
13219 if (group->stub_sec != NULL)
13220 {
13221 asection *stub_sec = group->stub_sec;
13222
13223 if (htab->stub_iteration <= STUB_SHRINK_ITER
13224 || stub_sec->rawsize < stub_sec->size)
13225 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13226 stub_sec->rawsize = stub_sec->size;
13227 stub_sec->size = 0;
13228 stub_sec->reloc_count = 0;
13229 stub_sec->flags &= ~SEC_RELOC;
13230 }
13231 }
eea6121a 13232
ba21f564
AM
13233 if (htab->stub_iteration <= STUB_SHRINK_ITER
13234 || htab->brlt->rawsize < htab->brlt->size)
13235 htab->brlt->rawsize = htab->brlt->size;
eea6121a 13236 htab->brlt->size = 0;
84f5d08e
AM
13237 htab->brlt->reloc_count = 0;
13238 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 13239 if (htab->relbrlt != NULL)
eea6121a 13240 htab->relbrlt->size = 0;
721956f4 13241
63bc6f6c 13242 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 13243
a4b6fadd
AM
13244 for (group = htab->group; group != NULL; group = group->next)
13245 if (group->needs_save_res)
13246 group->stub_sec->size += htab->sfpr->size;
13247
176a0d42
AM
13248 if (info->emitrelocations
13249 && htab->glink != NULL && htab->glink->size != 0)
13250 {
13251 htab->glink->reloc_count = 1;
13252 htab->glink->flags |= SEC_RELOC;
13253 }
13254
58d180e8
AM
13255 if (htab->glink_eh_frame != NULL
13256 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 13257 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 13258 {
2e0ce1c8 13259 size_t size = 0, align = 4;
58d180e8 13260
d4aaa2a0 13261 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13262 if (group->eh_size != 0)
13263 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 13264 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 13265 size += (24 + align - 1) & -align;
58d180e8 13266 if (size != 0)
2e0ce1c8
AM
13267 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13268 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13269 size = (size + align - 1) & -align;
58d180e8
AM
13270 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13271 htab->glink_eh_frame->size = size;
13272 }
13273
e7d1c40c 13274 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13275 for (group = htab->group; group != NULL; group = group->next)
13276 if (group->stub_sec != NULL)
691d2e9a
AM
13277 {
13278 int align = abs (htab->params->plt_stub_align);
13279 group->stub_sec->size
13280 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13281 }
d4aaa2a0
AM
13282
13283 for (group = htab->group; group != NULL; group = group->next)
13284 if (group->stub_sec != NULL
13285 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 13286 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 13287 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
13288 break;
13289
d4aaa2a0 13290 if (group == NULL
ba21f564
AM
13291 && (htab->brlt->rawsize == htab->brlt->size
13292 || (htab->stub_iteration > STUB_SHRINK_ITER
13293 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
13294 && (htab->glink_eh_frame == NULL
13295 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
13296 break;
13297
721956f4 13298 /* Ask the linker to do its stuff. */
e7d1c40c 13299 (*htab->params->layout_sections_again) ();
721956f4
AM
13300 }
13301
da44f4e5
AM
13302 if (htab->glink_eh_frame != NULL
13303 && htab->glink_eh_frame->size != 0)
13304 {
13305 bfd_vma val;
13306 bfd_byte *p, *last_fde;
13307 size_t last_fde_len, size, align, pad;
d4aaa2a0 13308 struct map_stub *group;
da44f4e5 13309
df136d64
AM
13310 /* It is necessary to at least have a rough outline of the
13311 linker generated CIEs and FDEs written before
13312 bfd_elf_discard_info is run, in order for these FDEs to be
13313 indexed in .eh_frame_hdr. */
da44f4e5
AM
13314 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13315 if (p == NULL)
13316 return FALSE;
13317 htab->glink_eh_frame->contents = p;
13318 last_fde = p;
2e0ce1c8 13319 align = 4;
da44f4e5
AM
13320
13321 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13322 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 13323 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 13324 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 13325 p += last_fde_len + 4;
da44f4e5 13326
d4aaa2a0 13327 for (group = htab->group; group != NULL; group = group->next)
df136d64 13328 if (group->eh_size != 0)
da44f4e5 13329 {
df136d64 13330 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 13331 last_fde = p;
df136d64 13332 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 13333 /* FDE length. */
2e0ce1c8 13334 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13335 p += 4;
13336 /* CIE pointer. */
13337 val = p - htab->glink_eh_frame->contents;
13338 bfd_put_32 (htab->elf.dynobj, val, p);
13339 p += 4;
13340 /* Offset to stub section, written later. */
13341 p += 4;
13342 /* stub section size. */
d4aaa2a0 13343 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
13344 p += 4;
13345 /* Augmentation. */
13346 p += 1;
df136d64
AM
13347 /* Make sure we don't have all nops. This is enough for
13348 elf-eh-frame.c to detect the last non-nop opcode. */
13349 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 13350 p = last_fde + last_fde_len + 4;
da44f4e5
AM
13351 }
13352 if (htab->glink != NULL && htab->glink->size != 0)
13353 {
13354 last_fde = p;
2e0ce1c8 13355 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13356 /* FDE length. */
2e0ce1c8 13357 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13358 p += 4;
13359 /* CIE pointer. */
13360 val = p - htab->glink_eh_frame->contents;
13361 bfd_put_32 (htab->elf.dynobj, val, p);
13362 p += 4;
13363 /* Offset to .glink, written later. */
13364 p += 4;
13365 /* .glink size. */
13366 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13367 p += 4;
13368 /* Augmentation. */
13369 p += 1;
13370
13371 *p++ = DW_CFA_advance_loc + 1;
13372 *p++ = DW_CFA_register;
13373 *p++ = 65;
9f08fa5c 13374 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 13375 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
13376 *p++ = DW_CFA_restore_extended;
13377 *p++ = 65;
2e0ce1c8 13378 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13379 }
13380 /* Subsume any padding into the last FDE if user .eh_frame
13381 sections are aligned more than glink_eh_frame. Otherwise any
13382 zero padding will be seen as a terminator. */
2e0ce1c8 13383 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13384 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13385 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13386 htab->glink_eh_frame->size = size + pad;
13387 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13388 }
13389
d969d15f
AM
13390 maybe_strip_output (info, htab->brlt);
13391 if (htab->glink_eh_frame != NULL)
13392 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13393
b34976b6 13394 return TRUE;
721956f4
AM
13395}
13396
13397/* Called after we have determined section placement. If sections
805fc799 13398 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13399
805fc799 13400bfd_vma
1c865ab2 13401ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13402{
805fc799 13403 asection *s;
a27e685f 13404 bfd_vma TOCstart, adjust;
721956f4 13405
43417696
AM
13406 if (info != NULL)
13407 {
13408 struct elf_link_hash_entry *h;
13409 struct elf_link_hash_table *htab = elf_hash_table (info);
13410
13411 if (is_elf_hash_table (htab)
13412 && htab->hgot != NULL)
13413 h = htab->hgot;
13414 else
13415 {
13416 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13417 if (is_elf_hash_table (htab))
13418 htab->hgot = h;
13419 }
13420 if (h != NULL
13421 && h->root.type == bfd_link_hash_defined
13422 && !h->root.linker_def
13423 && (!is_elf_hash_table (htab)
13424 || h->def_regular))
13425 {
13426 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13427 + h->root.u.def.section->output_offset
13428 + h->root.u.def.section->output_section->vma);
13429 _bfd_set_gp_value (obfd, TOCstart);
13430 return TOCstart;
13431 }
13432 }
13433
805fc799
AM
13434 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13435 order. The TOC starts where the first of these sections starts. */
13436 s = bfd_get_section_by_name (obfd, ".got");
e054468f 13437 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13438 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 13439 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13440 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 13441 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13442 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 13443 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
13444 {
13445 /* This may happen for
13446 o references to TOC base (SYM@toc / TOC[tc0]) without a
13447 .toc directive
13448 o bad linker script
13449 o --gc-sections and empty TOC sections
13450
13451 FIXME: Warn user? */
13452
13453 /* Look for a likely section. We probably won't even be
13454 using TOCstart. */
13455 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13456 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13457 | SEC_EXCLUDE))
805fc799
AM
13458 == (SEC_ALLOC | SEC_SMALL_DATA))
13459 break;
721956f4 13460 if (s == NULL)
805fc799 13461 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13462 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
13463 == (SEC_ALLOC | SEC_SMALL_DATA))
13464 break;
721956f4 13465 if (s == NULL)
805fc799 13466 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13467 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13468 == SEC_ALLOC)
805fc799 13469 break;
721956f4 13470 if (s == NULL)
805fc799 13471 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13472 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
13473 break;
13474 }
721956f4 13475
805fc799
AM
13476 TOCstart = 0;
13477 if (s != NULL)
13478 TOCstart = s->output_section->vma + s->output_offset;
721956f4 13479
a27e685f
AM
13480 /* Force alignment. */
13481 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13482 TOCstart -= adjust;
1c865ab2
AM
13483 _bfd_set_gp_value (obfd, TOCstart);
13484
810d4e75 13485 if (info != NULL && s != NULL)
1c865ab2
AM
13486 {
13487 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13488
810d4e75
AM
13489 if (htab != NULL)
13490 {
13491 if (htab->elf.hgot != NULL)
13492 {
a27e685f 13493 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
13494 htab->elf.hgot->root.u.def.section = s;
13495 }
13496 }
13497 else
1c865ab2 13498 {
810d4e75
AM
13499 struct bfd_link_hash_entry *bh = NULL;
13500 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
13501 s, TOC_BASE_OFF - adjust,
13502 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
13503 }
13504 }
805fc799 13505 return TOCstart;
721956f4
AM
13506}
13507
a345bc8d 13508/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 13509 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
13510
13511static bfd_boolean
49c09209 13512build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
13513{
13514 struct bfd_link_info *info;
13515 struct ppc_link_hash_table *htab;
49c09209 13516 struct plt_entry *ent;
a345bc8d
AM
13517 asection *s;
13518
13519 if (h->root.type == bfd_link_hash_indirect)
13520 return TRUE;
13521
49c09209
AM
13522 info = inf;
13523 htab = ppc_hash_table (info);
13524 if (htab == NULL)
13525 return FALSE;
13526
13527 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13528 if (ent->plt.offset != (bfd_vma) -1)
13529 {
13530 /* This symbol has an entry in the procedure linkage
13531 table. Set it up. */
13532 Elf_Internal_Rela rela;
2d7ad24e 13533 asection *plt, *relplt;
49c09209
AM
13534 bfd_byte *loc;
13535
13536 if (!htab->elf.dynamic_sections_created
13537 || h->dynindx == -1)
13538 {
13539 if (!(h->def_regular
13540 && (h->root.type == bfd_link_hash_defined
13541 || h->root.type == bfd_link_hash_defweak)))
13542 continue;
2d7ad24e
AM
13543 if (h->type == STT_GNU_IFUNC)
13544 {
13545 plt = htab->elf.iplt;
13546 relplt = htab->elf.irelplt;
13547 htab->local_ifunc_resolver = 1;
13548 if (htab->opd_abi)
13549 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13550 else
13551 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13552 }
49c09209 13553 else
2d7ad24e
AM
13554 {
13555 plt = htab->pltlocal;
13556 if (bfd_link_pic (info))
13557 {
13558 relplt = htab->relpltlocal;
13559 if (htab->opd_abi)
13560 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13561 else
13562 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13563 }
13564 else
13565 relplt = NULL;
13566 }
49c09209
AM
13567 rela.r_addend = (h->root.u.def.value
13568 + h->root.u.def.section->output_offset
13569 + h->root.u.def.section->output_section->vma
13570 + ent->addend);
2d7ad24e
AM
13571
13572 if (relplt == NULL)
13573 {
13574 loc = plt->contents + ent->plt.offset;
13575 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13576 if (htab->opd_abi)
13577 {
13578 bfd_vma toc = elf_gp (info->output_bfd);
13579 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13580 bfd_put_64 (info->output_bfd, toc, loc + 8);
13581 }
13582 }
13583 else
13584 {
13585 rela.r_offset = (plt->output_section->vma
13586 + plt->output_offset
13587 + ent->plt.offset);
13588 loc = relplt->contents + (relplt->reloc_count++
13589 * sizeof (Elf64_External_Rela));
13590 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13591 }
49c09209
AM
13592 }
13593 else
13594 {
13595 rela.r_offset = (htab->elf.splt->output_section->vma
13596 + htab->elf.splt->output_offset
13597 + ent->plt.offset);
13598 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13599 rela.r_addend = ent->addend;
13600 loc = (htab->elf.srelplt->contents
13601 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13602 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13603 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13604 htab->maybe_local_ifunc_resolver = 1;
2d7ad24e 13605 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 13606 }
49c09209
AM
13607 }
13608
a345bc8d
AM
13609 if (!h->pointer_equality_needed)
13610 return TRUE;
13611
13612 if (h->def_regular)
13613 return TRUE;
13614
9e390558 13615 s = htab->global_entry;
49c09209
AM
13616 if (s == NULL || s->size == 0)
13617 return TRUE;
13618
13619 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13620 if (ent->plt.offset != (bfd_vma) -1
13621 && ent->addend == 0)
a345bc8d
AM
13622 {
13623 bfd_byte *p;
13624 asection *plt;
13625 bfd_vma off;
13626
a345bc8d 13627 p = s->contents + h->root.u.def.value;
33e44f2e 13628 plt = htab->elf.splt;
a345bc8d
AM
13629 if (!htab->elf.dynamic_sections_created
13630 || h->dynindx == -1)
2d7ad24e
AM
13631 {
13632 if (h->type == STT_GNU_IFUNC)
13633 plt = htab->elf.iplt;
13634 else
13635 plt = htab->pltlocal;
13636 }
49c09209 13637 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
13638 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13639
13640 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13641 {
13642 info->callbacks->einfo
c1c8c1ef 13643 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
13644 h->root.root.string);
13645 bfd_set_error (bfd_error_bad_value);
13646 htab->stub_error = TRUE;
13647 }
13648
7341d5e2
AM
13649 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13650 if (htab->params->emit_stub_syms)
13651 {
13652 size_t len = strlen (h->root.root.string);
13653 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13654
13655 if (name == NULL)
13656 return FALSE;
13657
13658 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13659 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13660 if (h == NULL)
13661 return FALSE;
13662 if (h->root.type == bfd_link_hash_new)
13663 {
13664 h->root.type = bfd_link_hash_defined;
13665 h->root.u.def.section = s;
13666 h->root.u.def.value = p - s->contents;
13667 h->ref_regular = 1;
13668 h->def_regular = 1;
13669 h->ref_regular_nonweak = 1;
13670 h->forced_local = 1;
13671 h->non_elf = 0;
2ec55de3 13672 h->root.linker_def = 1;
7341d5e2
AM
13673 }
13674 }
13675
a345bc8d
AM
13676 if (PPC_HA (off) != 0)
13677 {
13678 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13679 p += 4;
13680 }
13681 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13682 p += 4;
13683 bfd_put_32 (s->owner, MTCTR_R12, p);
13684 p += 4;
407aa07c 13685 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
13686 break;
13687 }
13688 return TRUE;
13689}
13690
49c09209
AM
13691/* Write PLT relocs for locals. */
13692
13693static bfd_boolean
13694write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13695{
13696 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13697 bfd *ibfd;
13698
13699 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13700 {
13701 struct got_entry **lgot_ents, **end_lgot_ents;
13702 struct plt_entry **local_plt, **lplt, **end_local_plt;
13703 Elf_Internal_Shdr *symtab_hdr;
13704 bfd_size_type locsymcount;
13705 Elf_Internal_Sym *local_syms = NULL;
13706 struct plt_entry *ent;
13707
13708 if (!is_ppc64_elf (ibfd))
13709 continue;
13710
13711 lgot_ents = elf_local_got_ents (ibfd);
13712 if (!lgot_ents)
13713 continue;
13714
13715 symtab_hdr = &elf_symtab_hdr (ibfd);
13716 locsymcount = symtab_hdr->sh_info;
13717 end_lgot_ents = lgot_ents + locsymcount;
13718 local_plt = (struct plt_entry **) end_lgot_ents;
13719 end_local_plt = local_plt + locsymcount;
13720 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13721 for (ent = *lplt; ent != NULL; ent = ent->next)
13722 if (ent->plt.offset != (bfd_vma) -1)
13723 {
13724 Elf_Internal_Sym *sym;
13725 asection *sym_sec;
13726 asection *plt, *relplt;
13727 bfd_byte *loc;
13728 bfd_vma val;
49c09209
AM
13729
13730 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13731 lplt - local_plt, ibfd))
13732 {
13733 if (local_syms != NULL
13734 && symtab_hdr->contents != (unsigned char *) local_syms)
13735 free (local_syms);
13736 return FALSE;
13737 }
13738
13739 val = sym->st_value + ent->addend;
ba85c15d
AM
13740 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13741 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
49c09209
AM
13742 if (sym_sec != NULL && sym_sec->output_section != NULL)
13743 val += sym_sec->output_offset + sym_sec->output_section->vma;
13744
2d7ad24e
AM
13745 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13746 {
13747 htab->local_ifunc_resolver = 1;
13748 plt = htab->elf.iplt;
13749 relplt = htab->elf.irelplt;
13750 }
13751 else
13752 {
13753 plt = htab->pltlocal;
13754 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13755 }
49c09209 13756
2d7ad24e
AM
13757 if (relplt == NULL)
13758 {
13759 loc = plt->contents + ent->plt.offset;
13760 bfd_put_64 (info->output_bfd, val, loc);
13761 if (htab->opd_abi)
13762 {
13763 bfd_vma toc = elf_gp (ibfd);
13764 bfd_put_64 (info->output_bfd, toc, loc + 8);
13765 }
13766 }
49c09209 13767 else
2d7ad24e
AM
13768 {
13769 Elf_Internal_Rela rela;
13770 rela.r_offset = (ent->plt.offset
13771 + plt->output_offset
13772 + plt->output_section->vma);
13773 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13774 {
13775 if (htab->opd_abi)
13776 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13777 else
13778 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13779 }
13780 else
13781 {
13782 if (htab->opd_abi)
13783 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13784 else
13785 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13786 }
13787 rela.r_addend = val;
13788 loc = relplt->contents + (relplt->reloc_count++
13789 * sizeof (Elf64_External_Rela));
13790 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13791 }
49c09209
AM
13792 }
13793
13794 if (local_syms != NULL
13795 && symtab_hdr->contents != (unsigned char *) local_syms)
13796 {
13797 if (!info->keep_memory)
13798 free (local_syms);
13799 else
13800 symtab_hdr->contents = (unsigned char *) local_syms;
13801 }
13802 }
13803 return TRUE;
13804}
13805
721956f4
AM
13806/* Build all the stubs associated with the current output file.
13807 The stubs are kept in a hash table attached to the main linker
13808 hash table. This function is called via gldelf64ppc_finish. */
13809
b34976b6 13810bfd_boolean
e7d1c40c 13811ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13812 char **stats)
5d1634d7
AM
13813{
13814 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13815 struct map_stub *group;
721956f4 13816 asection *stub_sec;
5d1634d7 13817 bfd_byte *p;
e717da7e 13818 int stub_sec_count = 0;
5d1634d7 13819
4dfe6ac6
NC
13820 if (htab == NULL)
13821 return FALSE;
13822
eea6121a 13823 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 13824 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13825 {
13826 group->eh_size = 0;
13827 group->lr_restore = 0;
13828 if ((stub_sec = group->stub_sec) != NULL
13829 && stub_sec->size != 0)
13830 {
13831 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13832 stub_sec->size);
13833 if (stub_sec->contents == NULL)
13834 return FALSE;
13835 stub_sec->size = 0;
13836 }
13837 }
5d1634d7 13838
23eb7e01 13839 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13840 {
9f951329 13841 unsigned int indx;
ad8e1ba5 13842 bfd_vma plt0;
9f951329 13843
721956f4 13844 /* Build the .glink plt call stub. */
e7d1c40c 13845 if (htab->params->emit_stub_syms)
97b639ba
AM
13846 {
13847 struct elf_link_hash_entry *h;
468392fb
AM
13848 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13849 TRUE, FALSE, FALSE);
97b639ba
AM
13850 if (h == NULL)
13851 return FALSE;
13852 if (h->root.type == bfd_link_hash_new)
13853 {
13854 h->root.type = bfd_link_hash_defined;
13855 h->root.u.def.section = htab->glink;
ee4bf8d2 13856 h->root.u.def.value = 8;
f5385ebf
AM
13857 h->ref_regular = 1;
13858 h->def_regular = 1;
13859 h->ref_regular_nonweak = 1;
13860 h->forced_local = 1;
13861 h->non_elf = 0;
2ec55de3 13862 h->root.linker_def = 1;
97b639ba
AM
13863 }
13864 }
33e44f2e
AM
13865 plt0 = (htab->elf.splt->output_section->vma
13866 + htab->elf.splt->output_offset
13867 - 16);
176a0d42
AM
13868 if (info->emitrelocations)
13869 {
13870 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13871 if (r == NULL)
13872 return FALSE;
13873 r->r_offset = (htab->glink->output_offset
13874 + htab->glink->output_section->vma);
13875 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13876 r->r_addend = plt0;
13877 }
4ce794b7 13878 p = htab->glink->contents;
176a0d42 13879 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13880 bfd_put_64 (htab->glink->owner, plt0, p);
13881 p += 8;
b9e5796b
AM
13882 if (htab->opd_abi)
13883 {
13884 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13885 p += 4;
13886 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13887 p += 4;
13888 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13889 p += 4;
13890 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13891 p += 4;
13892 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13893 p += 4;
13894 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13895 p += 4;
13896 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13897 p += 4;
13898 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13899 p += 4;
13900 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13901 p += 4;
13902 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13903 p += 4;
13904 }
13905 else
13906 {
13907 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13908 p += 4;
13909 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13910 p += 4;
13911 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13912 p += 4;
f378ab09
AM
13913 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13914 p += 4;
b9e5796b
AM
13915 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13916 p += 4;
13917 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13918 p += 4;
13919 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13920 p += 4;
13921 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13922 p += 4;
13923 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13924 p += 4;
13925 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13926 p += 4;
13927 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13928 p += 4;
13929 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13930 p += 4;
13931 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13932 p += 4;
13933 }
407aa07c
AM
13934 bfd_put_32 (htab->glink->owner, BCTR, p);
13935 p += 4;
c75bc4f7 13936 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 13937
9f951329
AM
13938 /* Build the .glink lazy link call stubs. */
13939 indx = 0;
9e390558 13940 while (p < htab->glink->contents + htab->glink->size)
9f951329 13941 {
b9e5796b 13942 if (htab->opd_abi)
9f951329 13943 {
b9e5796b
AM
13944 if (indx < 0x8000)
13945 {
13946 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13947 p += 4;
13948 }
13949 else
13950 {
13951 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13952 p += 4;
13953 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13954 p);
13955 p += 4;
13956 }
9f951329 13957 }
4ce794b7 13958 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13959 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13960 indx++;
9f951329
AM
13961 p += 4;
13962 }
5d1634d7 13963 }
5d1634d7 13964
49c09209
AM
13965 /* Build .glink global entry stubs, and PLT relocs for globals. */
13966 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13967
13968 if (!write_plt_relocs_for_local_syms (info))
13969 return FALSE;
9e390558 13970
7341d5e2 13971 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13972 {
4ce794b7 13973 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13974 htab->brlt->size);
4ce794b7 13975 if (htab->brlt->contents == NULL)
b34976b6 13976 return FALSE;
721956f4 13977 }
ee75fd95 13978 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13979 {
13980 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13981 htab->relbrlt->size);
63bc6f6c
AM
13982 if (htab->relbrlt->contents == NULL)
13983 return FALSE;
13984 }
5d1634d7 13985
721956f4
AM
13986 /* Build the stubs as directed by the stub hash table. */
13987 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13988
a4b6fadd
AM
13989 for (group = htab->group; group != NULL; group = group->next)
13990 if (group->needs_save_res)
7dda8d3c 13991 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 13992
aa8a7074
AM
13993 if (htab->relbrlt != NULL)
13994 htab->relbrlt->reloc_count = 0;
13995
e7d1c40c 13996 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13997 for (group = htab->group; group != NULL; group = group->next)
13998 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
13999 {
14000 int align = abs (htab->params->plt_stub_align);
14001 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14002 }
794e51c0 14003
7dda8d3c
AM
14004 for (group = htab->group; group != NULL; group = group->next)
14005 if (group->needs_save_res)
14006 {
14007 stub_sec = group->stub_sec;
14008 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14009 htab->sfpr->contents, htab->sfpr->size);
14010 if (htab->params->emit_stub_syms)
14011 {
14012 unsigned int i;
14013
14014 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14015 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14016 return FALSE;
14017 }
14018 }
14019
df136d64
AM
14020 if (htab->glink_eh_frame != NULL
14021 && htab->glink_eh_frame->size != 0)
14022 {
14023 bfd_vma val;
14024 size_t align = 4;
14025
14026 p = htab->glink_eh_frame->contents;
14027 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14028
14029 for (group = htab->group; group != NULL; group = group->next)
14030 if (group->eh_size != 0)
14031 {
14032 /* Offset to stub section. */
14033 val = (group->stub_sec->output_section->vma
14034 + group->stub_sec->output_offset);
14035 val -= (htab->glink_eh_frame->output_section->vma
14036 + htab->glink_eh_frame->output_offset
14037 + (p + 8 - htab->glink_eh_frame->contents));
14038 if (val + 0x80000000 > 0xffffffff)
14039 {
14040 _bfd_error_handler
14041 (_("%s offset too large for .eh_frame sdata4 encoding"),
14042 group->stub_sec->name);
14043 return FALSE;
14044 }
14045 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14046 p += (group->eh_size + 17 + 3) & -4;
14047 }
14048 if (htab->glink != NULL && htab->glink->size != 0)
14049 {
14050 /* Offset to .glink. */
14051 val = (htab->glink->output_section->vma
14052 + htab->glink->output_offset
14053 + 8);
14054 val -= (htab->glink_eh_frame->output_section->vma
14055 + htab->glink_eh_frame->output_offset
14056 + (p + 8 - htab->glink_eh_frame->contents));
14057 if (val + 0x80000000 > 0xffffffff)
14058 {
14059 _bfd_error_handler
14060 (_("%s offset too large for .eh_frame sdata4 encoding"),
14061 htab->glink->name);
14062 return FALSE;
14063 }
14064 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14065 p += (24 + align - 1) & -align;
14066 }
14067 }
14068
d4aaa2a0
AM
14069 for (group = htab->group; group != NULL; group = group->next)
14070 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
14071 {
14072 stub_sec_count += 1;
c9301e31
AM
14073 if (stub_sec->rawsize != stub_sec->size
14074 && (htab->stub_iteration <= STUB_SHRINK_ITER
14075 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
14076 break;
14077 }
5d1634d7 14078
25516cc5 14079 if (group != NULL)
5d1634d7 14080 {
b34976b6 14081 htab->stub_error = TRUE;
cf97bcb0 14082 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 14083 }
721956f4 14084
d2a300cf
AM
14085 if (htab->stub_error)
14086 return FALSE;
14087
14088 if (stats != NULL)
14089 {
db84fff3 14090 size_t len;
d2a300cf
AM
14091 *stats = bfd_malloc (500);
14092 if (*stats == NULL)
14093 return FALSE;
14094
db84fff3
AM
14095 len = sprintf (*stats,
14096 ngettext ("linker stubs in %u group\n",
14097 "linker stubs in %u groups\n",
14098 stub_sec_count),
14099 stub_sec_count);
05d0e962
AM
14100 sprintf (*stats + len, _(" branch %lu\n"
14101 " branch toc adj %lu\n"
14102 " branch notoc %lu\n"
14103 " branch both %lu\n"
14104 " long branch %lu\n"
14105 " long toc adj %lu\n"
14106 " long notoc %lu\n"
14107 " long both %lu\n"
14108 " plt call %lu\n"
14109 " plt call save %lu\n"
14110 " plt call notoc %lu\n"
14111 " plt call both %lu\n"
14112 " global entry %lu"),
4ce794b7
AM
14113 htab->stub_count[ppc_stub_long_branch - 1],
14114 htab->stub_count[ppc_stub_long_branch_r2off - 1],
05d0e962
AM
14115 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14116 htab->stub_count[ppc_stub_long_branch_both - 1],
4ce794b7
AM
14117 htab->stub_count[ppc_stub_plt_branch - 1],
14118 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
05d0e962
AM
14119 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14120 htab->stub_count[ppc_stub_plt_branch_both - 1],
794e51c0 14121 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2 14122 htab->stub_count[ppc_stub_plt_call_r2save - 1],
05d0e962
AM
14123 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14124 htab->stub_count[ppc_stub_plt_call_both - 1],
7341d5e2 14125 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
14126 }
14127 return TRUE;
5bd4f169
AM
14128}
14129
60124e18
AM
14130/* What to do when ld finds relocations against symbols defined in
14131 discarded sections. */
14132
14133static unsigned int
14134ppc64_elf_action_discarded (asection *sec)
14135{
14136 if (strcmp (".opd", sec->name) == 0)
14137 return 0;
14138
14139 if (strcmp (".toc", sec->name) == 0)
14140 return 0;
14141
bce50a28
JJ
14142 if (strcmp (".toc1", sec->name) == 0)
14143 return 0;
14144
60124e18
AM
14145 return _bfd_elf_default_action_discarded (sec);
14146}
14147
e59a1001
AM
14148/* These are the dynamic relocations supported by glibc. */
14149
14150static bfd_boolean
14151ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
14152{
14153 switch (r_type)
14154 {
14155 case R_PPC64_RELATIVE:
14156 case R_PPC64_NONE:
14157 case R_PPC64_ADDR64:
14158 case R_PPC64_GLOB_DAT:
14159 case R_PPC64_IRELATIVE:
14160 case R_PPC64_JMP_IREL:
14161 case R_PPC64_JMP_SLOT:
14162 case R_PPC64_DTPMOD64:
14163 case R_PPC64_DTPREL64:
14164 case R_PPC64_TPREL64:
14165 case R_PPC64_TPREL16_LO_DS:
14166 case R_PPC64_TPREL16_DS:
14167 case R_PPC64_TPREL16:
14168 case R_PPC64_TPREL16_LO:
14169 case R_PPC64_TPREL16_HI:
14170 case R_PPC64_TPREL16_HIGH:
14171 case R_PPC64_TPREL16_HA:
14172 case R_PPC64_TPREL16_HIGHA:
14173 case R_PPC64_TPREL16_HIGHER:
14174 case R_PPC64_TPREL16_HIGHEST:
14175 case R_PPC64_TPREL16_HIGHERA:
14176 case R_PPC64_TPREL16_HIGHESTA:
14177 case R_PPC64_ADDR16_LO_DS:
14178 case R_PPC64_ADDR16_LO:
14179 case R_PPC64_ADDR16_HI:
14180 case R_PPC64_ADDR16_HIGH:
14181 case R_PPC64_ADDR16_HA:
14182 case R_PPC64_ADDR16_HIGHA:
14183 case R_PPC64_REL30:
14184 case R_PPC64_COPY:
14185 case R_PPC64_UADDR64:
14186 case R_PPC64_UADDR32:
14187 case R_PPC64_ADDR32:
14188 case R_PPC64_ADDR24:
14189 case R_PPC64_ADDR16:
14190 case R_PPC64_UADDR16:
14191 case R_PPC64_ADDR16_DS:
14192 case R_PPC64_ADDR16_HIGHER:
14193 case R_PPC64_ADDR16_HIGHEST:
14194 case R_PPC64_ADDR16_HIGHERA:
14195 case R_PPC64_ADDR16_HIGHESTA:
14196 case R_PPC64_ADDR14:
14197 case R_PPC64_ADDR14_BRTAKEN:
14198 case R_PPC64_ADDR14_BRNTAKEN:
14199 case R_PPC64_REL32:
14200 case R_PPC64_REL64:
14201 return TRUE;
14202
14203 default:
14204 return FALSE;
14205 }
14206}
14207
5bd4f169
AM
14208/* The RELOCATE_SECTION function is called by the ELF backend linker
14209 to handle the relocations for a section.
14210
14211 The relocs are always passed as Rela structures; if the section
14212 actually uses Rel structures, the r_addend field will always be
14213 zero.
14214
14215 This function is responsible for adjust the section contents as
14216 necessary, and (if using Rela relocs and generating a
1049f94e 14217 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
14218 necessary.
14219
14220 This function does not have to worry about setting the reloc
14221 address or the reloc symbol index.
14222
14223 LOCAL_SYMS is a pointer to the swapped in local symbols.
14224
14225 LOCAL_SECTIONS is an array giving the section in the input file
14226 corresponding to the st_shndx field of each local symbol.
14227
14228 The global hash table entry for the global symbols can be found
14229 via elf_sym_hashes (input_bfd).
14230
1049f94e 14231 When generating relocatable output, this function must handle
5bd4f169
AM
14232 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14233 going to be the section symbol corresponding to the output
14234 section, which means that the addend must be adjusted
14235 accordingly. */
14236
b34976b6 14237static bfd_boolean
4ce794b7
AM
14238ppc64_elf_relocate_section (bfd *output_bfd,
14239 struct bfd_link_info *info,
14240 bfd *input_bfd,
14241 asection *input_section,
14242 bfd_byte *contents,
14243 Elf_Internal_Rela *relocs,
14244 Elf_Internal_Sym *local_syms,
14245 asection **local_sections)
5bd4f169 14246{
65f38f15 14247 struct ppc_link_hash_table *htab;
5bd4f169
AM
14248 Elf_Internal_Shdr *symtab_hdr;
14249 struct elf_link_hash_entry **sym_hashes;
5bd4f169 14250 Elf_Internal_Rela *rel;
c316a17c 14251 Elf_Internal_Rela *wrel;
5bd4f169 14252 Elf_Internal_Rela *relend;
411e1bfb
AM
14253 Elf_Internal_Rela outrel;
14254 bfd_byte *loc;
411e1bfb 14255 struct got_entry **local_got_ents;
5bd4f169 14256 bfd_vma TOCstart;
b34976b6
AM
14257 bfd_boolean ret = TRUE;
14258 bfd_boolean is_opd;
794e51c0
AM
14259 /* Assume 'at' branch hints. */
14260 bfd_boolean is_isa_v2 = TRUE;
e59a1001 14261 bfd_boolean warned_dynamic = FALSE;
95f0d0d2 14262 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 14263
65f38f15 14264 /* Initialize howto table if needed. */
5bd4f169 14265 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
14266 ppc_howto_init ();
14267
65f38f15 14268 htab = ppc_hash_table (info);
4dfe6ac6
NC
14269 if (htab == NULL)
14270 return FALSE;
ee75fd95
AM
14271
14272 /* Don't relocate stub sections. */
e7d1c40c 14273 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
14274 return TRUE;
14275
7af5d5c4
AM
14276 if (!is_ppc64_elf (input_bfd))
14277 {
14278 bfd_set_error (bfd_error_wrong_format);
14279 return FALSE;
14280 }
0ffa91dd 14281
411e1bfb 14282 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 14283 TOCstart = elf_gp (output_bfd);
0ffa91dd 14284 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 14285 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 14286 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 14287
c316a17c 14288 rel = wrel = relocs;
5bd4f169 14289 relend = relocs + input_section->reloc_count;
c316a17c 14290 for (; rel < relend; wrel++, rel++)
5bd4f169 14291 {
04c9666a 14292 enum elf_ppc64_reloc_type r_type;
31c76678 14293 bfd_vma addend;
5bd4f169
AM
14294 bfd_reloc_status_type r;
14295 Elf_Internal_Sym *sym;
14296 asection *sec;
039b3fef
AM
14297 struct elf_link_hash_entry *h_elf;
14298 struct ppc_link_hash_entry *h;
14299 struct ppc_link_hash_entry *fdh;
5bd4f169 14300 const char *sym_name;
0d4792f7 14301 unsigned long r_symndx, toc_symndx;
3a71aa26 14302 bfd_vma toc_addend;
f961d9dd
AM
14303 unsigned char tls_mask, tls_gd, tls_type;
14304 unsigned char sym_type;
5bd4f169 14305 bfd_vma relocation;
23cedd1d 14306 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 14307 bfd_boolean warned;
bc30df16 14308 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 14309 unsigned int insn;
e11840f9 14310 unsigned int mask;
721956f4
AM
14311 struct ppc_stub_hash_entry *stub_entry;
14312 bfd_vma max_br_offset;
14313 bfd_vma from;
c316a17c 14314 Elf_Internal_Rela orig_rel;
b80eed39
AM
14315 reloc_howto_type *howto;
14316 struct reloc_howto_struct alt_howto;
4a421c53
AM
14317 uint64_t pinsn;
14318 bfd_vma offset;
5bd4f169 14319
c316a17c
AM
14320 again:
14321 orig_rel = *rel;
14322
4ce794b7 14323 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 14324 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
14325
14326 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14327 symbol of the previous ADDR64 reloc. The symbol gives us the
14328 proper TOC base to use. */
14329 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
14330 && wrel != relocs
14331 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 14332 && is_opd)
c316a17c 14333 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 14334
4ce794b7
AM
14335 sym = NULL;
14336 sec = NULL;
039b3fef 14337 h_elf = NULL;
4ce794b7 14338 sym_name = NULL;
b34976b6
AM
14339 unresolved_reloc = FALSE;
14340 warned = FALSE;
65f38f15 14341
0b13192e 14342 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
14343 {
14344 /* It's a local symbol. */
74f0fb50 14345 struct _opd_sec_data *opd;
4025353c 14346
5bd4f169
AM
14347 sym = local_syms + r_symndx;
14348 sec = local_sections[r_symndx];
26c61ae5 14349 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 14350 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 14351 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
14352 opd = get_opd_info (sec);
14353 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 14354 {
51aecdc5
AM
14355 long adjust = opd->adjust[OPD_NDX (sym->st_value
14356 + rel->r_addend)];
4025353c
AM
14357 if (adjust == -1)
14358 relocation = 0;
14359 else
4cc603a5
AM
14360 {
14361 /* If this is a relocation against the opd section sym
14362 and we have edited .opd, adjust the reloc addend so
14363 that ld -r and ld --emit-relocs output is correct.
14364 If it is a reloc against some other .opd symbol,
14365 then the symbol value will be adjusted later. */
14366 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14367 rel->r_addend += adjust;
14368 else
14369 relocation += adjust;
14370 }
1e2f5b6e 14371 }
5bd4f169
AM
14372 }
14373 else
14374 {
62d887d4
L
14375 bfd_boolean ignored;
14376
b2a8e766
AM
14377 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14378 r_symndx, symtab_hdr, sym_hashes,
039b3fef 14379 h_elf, sec, relocation,
62d887d4 14380 unresolved_reloc, warned, ignored);
039b3fef
AM
14381 sym_name = h_elf->root.root.string;
14382 sym_type = h_elf->type;
b69fdb4e
AM
14383 if (sec != NULL
14384 && sec->owner == output_bfd
14385 && strcmp (sec->name, ".opd") == 0)
14386 {
14387 /* This is a symbol defined in a linker script. All
14388 such are defined in output sections, even those
14389 defined by simple assignment from a symbol defined in
14390 an input section. Transfer the symbol to an
14391 appropriate input .opd section, so that a branch to
14392 this symbol will be mapped to the location specified
14393 by the opd entry. */
14394 struct bfd_link_order *lo;
14395 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14396 if (lo->type == bfd_indirect_link_order)
14397 {
14398 asection *isec = lo->u.indirect.section;
14399 if (h_elf->root.u.def.value >= isec->output_offset
14400 && h_elf->root.u.def.value < (isec->output_offset
14401 + isec->size))
14402 {
14403 h_elf->root.u.def.value -= isec->output_offset;
14404 h_elf->root.u.def.section = isec;
14405 sec = isec;
14406 break;
14407 }
14408 }
14409 }
5bd4f169 14410 }
039b3fef 14411 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 14412
dbaa2011 14413 if (sec != NULL && discarded_section (sec))
c316a17c
AM
14414 {
14415 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14416 input_bfd, input_section,
0930cb30 14417 contents, rel->r_offset);
c316a17c
AM
14418 wrel->r_offset = rel->r_offset;
14419 wrel->r_info = 0;
14420 wrel->r_addend = 0;
14421
14422 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 14423 symbols defined in discarded sections. Not done for
c316a17c
AM
14424 non-debug to preserve relocs in .eh_frame which the
14425 eh_frame editing code expects to be present. */
14426 if (bfd_link_relocatable (info)
14427 && (input_section->flags & SEC_DEBUGGING))
14428 wrel--;
14429
14430 continue;
14431 }
ab96bf03 14432
0e1862bb 14433 if (bfd_link_relocatable (info))
c316a17c 14434 goto copy_reloc;
ab96bf03 14435
f40da81b
AM
14436 if (h != NULL && &h->elf == htab->elf.hgot)
14437 {
6f20ed8a 14438 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
14439 sec = bfd_abs_section_ptr;
14440 unresolved_reloc = FALSE;
14441 }
14442
951fd09b
AM
14443 /* TLS optimizations. Replace instruction sequences and relocs
14444 based on information we collected in tls_optimize. We edit
14445 RELOCS so that --emit-relocs will output something sensible
14446 for the final instruction stream. */
14447 tls_mask = 0;
14448 tls_gd = 0;
0d4792f7 14449 toc_symndx = 0;
727fc41e
AM
14450 if (h != NULL)
14451 tls_mask = h->tls_mask;
14452 else if (local_got_ents != NULL)
411e1bfb 14453 {
e054468f
AM
14454 struct plt_entry **local_plt = (struct plt_entry **)
14455 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 14456 unsigned char *lgot_masks = (unsigned char *)
e054468f 14457 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
14458 tls_mask = lgot_masks[r_symndx];
14459 }
37da22e5 14460 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
14461 && (r_type == R_PPC64_TLS
14462 || r_type == R_PPC64_TLSGD
14463 || r_type == R_PPC64_TLSLD))
14464 {
14465 /* Check for toc tls entries. */
f961d9dd 14466 unsigned char *toc_tls;
0d4792f7 14467
727fc41e
AM
14468 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14469 &local_syms, rel, input_bfd))
14470 return FALSE;
0d4792f7 14471
727fc41e
AM
14472 if (toc_tls)
14473 tls_mask = *toc_tls;
0d4792f7
AM
14474 }
14475
14476 /* Check that tls relocs are used with tls syms, and non-tls
14477 relocs are used with non-tls syms. */
cf35638d 14478 if (r_symndx != STN_UNDEF
0d4792f7
AM
14479 && r_type != R_PPC64_NONE
14480 && (h == NULL
039b3fef
AM
14481 || h->elf.root.type == bfd_link_hash_defined
14482 || h->elf.root.type == bfd_link_hash_defweak)
71c4e95a 14483 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
0d4792f7 14484 {
37da22e5 14485 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
14486 && (r_type == R_PPC64_TLS
14487 || r_type == R_PPC64_TLSGD
14488 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
14489 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14490 ;
14491 else
25f53a85 14492 info->callbacks->einfo
1d483afe 14493 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 14494 /* xgettext:c-format */
c1c8c1ef 14495 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 14496 /* xgettext:c-format */
c1c8c1ef 14497 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 14498 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
14499 ppc64_elf_howto_table[r_type]->name,
14500 sym_name);
411e1bfb
AM
14501 }
14502
14503 /* Ensure reloc mapping code below stays sane. */
14504 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14505 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14506 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14507 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14508 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14509 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14510 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14511 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14512 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14513 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14514 abort ();
0d4792f7 14515
411e1bfb
AM
14516 switch (r_type)
14517 {
14518 default:
411e1bfb
AM
14519 break;
14520
ba761f19 14521 case R_PPC64_LO_DS_OPT:
95f0d0d2 14522 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
14523 if ((insn & (0x3f << 26)) != 58u << 26)
14524 abort ();
14525 insn += (14u << 26) - (58u << 26);
95f0d0d2 14526 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
14527 r_type = R_PPC64_TOC16_LO;
14528 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14529 break;
14530
411e1bfb
AM
14531 case R_PPC64_TOC16:
14532 case R_PPC64_TOC16_LO:
14533 case R_PPC64_TOC16_DS:
14534 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
14535 {
14536 /* Check for toc tls entries. */
f961d9dd 14537 unsigned char *toc_tls;
951fd09b 14538 int retval;
411e1bfb 14539
3a71aa26
AM
14540 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14541 &local_syms, rel, input_bfd);
951fd09b 14542 if (retval == 0)
411e1bfb
AM
14543 return FALSE;
14544
14545 if (toc_tls)
14546 {
951fd09b 14547 tls_mask = *toc_tls;
411e1bfb
AM
14548 if (r_type == R_PPC64_TOC16_DS
14549 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 14550 {
37da22e5 14551 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
14552 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14553 goto toctprel;
14554 }
411e1bfb 14555 else
951fd09b
AM
14556 {
14557 /* If we found a GD reloc pair, then we might be
14558 doing a GD->IE transition. */
14559 if (retval == 2)
14560 {
b00a0a86 14561 tls_gd = TLS_GDIE;
37da22e5
AM
14562 if ((tls_mask & TLS_TLS) != 0
14563 && (tls_mask & TLS_GD) == 0)
102890f0 14564 goto tls_ldgd_opt;
951fd09b
AM
14565 }
14566 else if (retval == 3)
14567 {
37da22e5
AM
14568 if ((tls_mask & TLS_TLS) != 0
14569 && (tls_mask & TLS_LD) == 0)
102890f0 14570 goto tls_ldgd_opt;
951fd09b
AM
14571 }
14572 }
411e1bfb
AM
14573 }
14574 }
14575 break;
14576
9d6ded02
AM
14577 case R_PPC64_GOT_TPREL16_HI:
14578 case R_PPC64_GOT_TPREL16_HA:
37da22e5 14579 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
14580 && (tls_mask & TLS_TPREL) == 0)
14581 {
14582 rel->r_offset -= d_offset;
95f0d0d2 14583 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
14584 r_type = R_PPC64_NONE;
14585 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14586 }
14587 break;
14588
411e1bfb
AM
14589 case R_PPC64_GOT_TPREL16_DS:
14590 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 14591 if ((tls_mask & TLS_TLS) != 0
951fd09b 14592 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14593 {
81407a69 14594 toctprel:
95f0d0d2 14595 insn = bfd_get_32 (input_bfd,
c316a17c 14596 contents + rel->r_offset - d_offset);
411e1bfb
AM
14597 insn &= 31 << 21;
14598 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 14599 bfd_put_32 (input_bfd, insn,
c316a17c 14600 contents + rel->r_offset - d_offset);
411e1bfb 14601 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
14602 if (toc_symndx != 0)
14603 {
14604 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14605 rel->r_addend = toc_addend;
0d4792f7
AM
14606 /* We changed the symbol. Start over in order to
14607 get h, sym, sec etc. right. */
c316a17c 14608 goto again;
0d4792f7
AM
14609 }
14610 else
14611 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14612 }
14613 break;
14614
c213164a
AM
14615 case R_PPC64_GOT_TPREL34:
14616 if ((tls_mask & TLS_TLS) != 0
14617 && (tls_mask & TLS_TPREL) == 0)
14618 {
14619 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
14620 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14621 pinsn <<= 32;
14622 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14623 pinsn += ((2ULL << 56) + (-1ULL << 52)
14624 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
14625 bfd_put_32 (input_bfd, pinsn >> 32,
14626 contents + rel->r_offset);
14627 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14628 contents + rel->r_offset + 4);
14629 r_type = R_PPC64_TPREL34;
14630 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14631 }
14632 break;
14633
411e1bfb 14634 case R_PPC64_TLS:
37da22e5 14635 if ((tls_mask & TLS_TLS) != 0
951fd09b 14636 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14637 {
c213164a 14638 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2d0f3896
AM
14639 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14640 if (insn == 0)
c213164a
AM
14641 break;
14642 if ((rel->r_offset & 3) == 0)
0d4792f7 14643 {
c213164a
AM
14644 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14645 /* Was PPC64_TLS which sits on insn boundary, now
14646 PPC64_TPREL16_LO which is at low-order half-word. */
14647 rel->r_offset += d_offset;
14648 r_type = R_PPC64_TPREL16_LO;
14649 if (toc_symndx != 0)
14650 {
14651 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14652 rel->r_addend = toc_addend;
14653 /* We changed the symbol. Start over in order to
14654 get h, sym, sec etc. right. */
14655 goto again;
14656 }
14657 else
14658 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14659 }
14660 else if ((rel->r_offset & 3) == 1)
14661 {
14662 /* For pcrel IE to LE we already have the full
14663 offset and thus don't need an addi here. A nop
14664 or mr will do. */
14665 if ((insn & (0x3f << 26)) == 14 << 26)
14666 {
14667 /* Extract regs from addi rt,ra,si. */
14668 unsigned int rt = (insn >> 21) & 0x1f;
14669 unsigned int ra = (insn >> 16) & 0x1f;
14670 if (rt == ra)
14671 insn = NOP;
14672 else
14673 {
14674 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
14675 insn = (rt << 16) | (ra << 21) | (ra << 11);
14676 insn |= (31u << 26) | (444u << 1);
14677 }
14678 }
14679 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
0d4792f7 14680 }
411e1bfb
AM
14681 }
14682 break;
14683
411e1bfb
AM
14684 case R_PPC64_GOT_TLSGD16_HI:
14685 case R_PPC64_GOT_TLSGD16_HA:
b00a0a86 14686 tls_gd = TLS_GDIE;
37da22e5 14687 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
14688 goto tls_gdld_hi;
14689 break;
14690
411e1bfb
AM
14691 case R_PPC64_GOT_TLSLD16_HI:
14692 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 14693 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 14694 {
951fd09b
AM
14695 tls_gdld_hi:
14696 if ((tls_mask & tls_gd) != 0)
14697 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14698 + R_PPC64_GOT_TPREL16_DS);
14699 else
411e1bfb 14700 {
4fe5ca5b 14701 rel->r_offset -= d_offset;
95f0d0d2 14702 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 14703 r_type = R_PPC64_NONE;
411e1bfb 14704 }
951fd09b 14705 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14706 }
14707 break;
14708
951fd09b
AM
14709 case R_PPC64_GOT_TLSGD16:
14710 case R_PPC64_GOT_TLSGD16_LO:
b00a0a86 14711 tls_gd = TLS_GDIE;
37da22e5 14712 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 14713 goto tls_ldgd_opt;
951fd09b 14714 break;
411e1bfb 14715
951fd09b
AM
14716 case R_PPC64_GOT_TLSLD16:
14717 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 14718 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 14719 {
b9f04fe0 14720 unsigned int insn1, insn2;
102890f0
AM
14721
14722 tls_ldgd_opt:
727fc41e
AM
14723 offset = (bfd_vma) -1;
14724 /* If not using the newer R_PPC64_TLSGD/LD to mark
14725 __tls_get_addr calls, we must trust that the call
14726 stays with its arg setup insns, ie. that the next
14727 reloc is the __tls_get_addr call associated with
14728 the current reloc. Edit both insns. */
14729 if (input_section->has_tls_get_addr_call
14730 && rel + 1 < relend
14731 && branch_reloc_hash_match (input_bfd, rel + 1,
14732 htab->tls_get_addr,
14733 htab->tls_get_addr_fd))
14734 offset = rel[1].r_offset;
b86ac8e3
AM
14735 /* We read the low GOT_TLS (or TOC16) insn because we
14736 need to keep the destination reg. It may be
14737 something other than the usual r3, and moved to r3
14738 before the call by intervening code. */
95f0d0d2 14739 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 14740 contents + rel->r_offset - d_offset);
102890f0 14741 if ((tls_mask & tls_gd) != 0)
411e1bfb 14742 {
102890f0 14743 /* IE */
b86ac8e3 14744 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
14745 insn1 |= 58 << 26; /* ld */
14746 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 14747 if (offset != (bfd_vma) -1)
f58d5a2d 14748 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
46e9995a
AM
14749 if (r_type == R_PPC64_TOC16
14750 || r_type == R_PPC64_TOC16_LO)
102890f0 14751 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
46e9995a
AM
14752 else
14753 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
14754 + R_PPC64_GOT_TPREL16_DS);
102890f0
AM
14755 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14756 }
14757 else
14758 {
14759 /* LE */
b86ac8e3
AM
14760 insn1 &= 0x1f << 21;
14761 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
14762 insn2 = 0x38630000; /* addi 3,3,0 */
14763 if (tls_gd == 0)
951fd09b 14764 {
102890f0 14765 /* Was an LD reloc. */
71c4e95a 14766 r_symndx = STN_UNDEF;
102890f0 14767 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 14768 }
102890f0 14769 else if (toc_symndx != 0)
3a71aa26
AM
14770 {
14771 r_symndx = toc_symndx;
14772 rel->r_addend = toc_addend;
14773 }
102890f0
AM
14774 r_type = R_PPC64_TPREL16_HA;
14775 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
14776 if (offset != (bfd_vma) -1)
14777 {
14778 rel[1].r_info = ELF64_R_INFO (r_symndx,
14779 R_PPC64_TPREL16_LO);
14780 rel[1].r_offset = offset + d_offset;
14781 rel[1].r_addend = rel->r_addend;
14782 }
102890f0 14783 }
95f0d0d2 14784 bfd_put_32 (input_bfd, insn1,
3a71aa26 14785 contents + rel->r_offset - d_offset);
727fc41e 14786 if (offset != (bfd_vma) -1)
c96e0573
AM
14787 {
14788 bfd_put_32 (input_bfd, insn2, contents + offset);
14789 if (offset + 8 <= input_section->size)
14790 {
14791 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14792 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14793 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14794 }
14795 }
727fc41e
AM
14796 if ((tls_mask & tls_gd) == 0
14797 && (tls_gd == 0 || toc_symndx != 0))
14798 {
14799 /* We changed the symbol. Start over in order
14800 to get h, sym, sec etc. right. */
c316a17c 14801 goto again;
727fc41e
AM
14802 }
14803 }
14804 break;
14805
c213164a
AM
14806 case R_PPC64_GOT_TLSGD34:
14807 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14808 {
14809 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14810 pinsn <<= 32;
14811 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14812 if ((tls_mask & TLS_GDIE) != 0)
14813 {
14814 /* IE, pla -> pld */
14815 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
14816 r_type = R_PPC64_GOT_TPREL34;
14817 }
14818 else
14819 {
14820 /* LE, pla pcrel -> paddi r13 */
14821 pinsn += (-1ULL << 52) + (13ULL << 16);
14822 r_type = R_PPC64_TPREL34;
14823 }
14824 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14825 bfd_put_32 (input_bfd, pinsn >> 32,
14826 contents + rel->r_offset);
14827 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14828 contents + rel->r_offset + 4);
14829 }
14830 break;
14831
14832 case R_PPC64_GOT_TLSLD34:
14833 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14834 {
14835 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14836 pinsn <<= 32;
14837 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14838 pinsn += (-1ULL << 52) + (13ULL << 16);
14839 bfd_put_32 (input_bfd, pinsn >> 32,
14840 contents + rel->r_offset);
14841 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14842 contents + rel->r_offset + 4);
14843 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14844 r_symndx = STN_UNDEF;
14845 r_type = R_PPC64_TPREL34;
14846 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14847 goto again;
14848 }
14849 break;
14850
727fc41e 14851 case R_PPC64_TLSGD:
37da22e5 14852 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 14853 && rel + 1 < relend)
727fc41e 14854 {
b9f04fe0 14855 unsigned int insn2;
5663e321 14856 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 14857
4a421c53 14858 offset = rel->r_offset;
5663e321 14859 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
14860 {
14861 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
14862 if (r_type1 == R_PPC64_PLT_PCREL34
14863 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14864 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
14865 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14866 break;
14867 }
14868
14869 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14870 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14871
b00a0a86 14872 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
14873 {
14874 /* IE */
14875 r_type = R_PPC64_NONE;
14876 insn2 = 0x7c636a14; /* add 3,3,13 */
14877 }
14878 else
14879 {
14880 /* LE */
14881 if (toc_symndx != 0)
14882 {
14883 r_symndx = toc_symndx;
14884 rel->r_addend = toc_addend;
14885 }
c213164a
AM
14886 if (r_type1 == R_PPC64_REL24_NOTOC
14887 || r_type1 == R_PPC64_PLTCALL_NOTOC)
14888 {
14889 r_type = R_PPC64_NONE;
14890 insn2 = NOP;
14891 }
14892 else
14893 {
14894 rel->r_offset = offset + d_offset;
14895 r_type = R_PPC64_TPREL16_LO;
14896 insn2 = 0x38630000; /* addi 3,3,0 */
14897 }
727fc41e
AM
14898 }
14899 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14900 /* Zap the reloc on the _tls_get_addr call too. */
14901 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14902 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 14903 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
14904 if ((tls_mask & TLS_GDIE) == 0
14905 && toc_symndx != 0
14906 && r_type != R_PPC64_NONE)
c316a17c 14907 goto again;
411e1bfb 14908 }
411e1bfb
AM
14909 break;
14910
727fc41e 14911 case R_PPC64_TLSLD:
37da22e5 14912 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 14913 && rel + 1 < relend)
727fc41e 14914 {
b9f04fe0 14915 unsigned int insn2;
5663e321 14916 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 14917
4a421c53 14918 offset = rel->r_offset;
5663e321 14919 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
14920 {
14921 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
14922 if (r_type1 == R_PPC64_PLT_PCREL34
14923 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14924 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
14925 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14926 break;
14927 }
14928
14929 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14930 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14931
c213164a
AM
14932 if (r_type1 == R_PPC64_REL24_NOTOC
14933 || r_type1 == R_PPC64_PLTCALL_NOTOC)
14934 {
14935 r_type = R_PPC64_NONE;
14936 insn2 = NOP;
14937 }
14938 else
14939 {
14940 rel->r_offset = offset + d_offset;
14941 r_symndx = STN_UNDEF;
14942 r_type = R_PPC64_TPREL16_LO;
14943 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14944 insn2 = 0x38630000; /* addi 3,3,0 */
14945 }
727fc41e 14946 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
14947 /* Zap the reloc on the _tls_get_addr call too. */
14948 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14949 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 14950 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
14951 if (r_type != R_PPC64_NONE)
14952 goto again;
727fc41e
AM
14953 }
14954 break;
14955
411e1bfb 14956 case R_PPC64_DTPMOD64:
951fd09b
AM
14957 if (rel + 1 < relend
14958 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14959 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 14960 {
951fd09b
AM
14961 if ((tls_mask & TLS_GD) == 0)
14962 {
14963 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
b00a0a86 14964 if ((tls_mask & TLS_GDIE) != 0)
951fd09b
AM
14965 r_type = R_PPC64_TPREL64;
14966 else
14967 {
4ce794b7 14968 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
14969 r_type = R_PPC64_NONE;
14970 }
14971 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14972 }
14973 }
14974 else
14975 {
14976 if ((tls_mask & TLS_LD) == 0)
411e1bfb 14977 {
4ce794b7 14978 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 14979 r_type = R_PPC64_NONE;
951fd09b 14980 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 14981 }
411e1bfb
AM
14982 }
14983 break;
14984
14985 case R_PPC64_TPREL64:
951fd09b 14986 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
14987 {
14988 r_type = R_PPC64_NONE;
14989 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14990 }
14991 break;
52a82034 14992
006589cf
AM
14993 case R_PPC64_ENTRY:
14994 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14995 if (!bfd_link_pic (info)
14996 && !info->traditional_format
14997 && relocation + 0x80008000 <= 0xffffffff)
14998 {
14999 unsigned int insn1, insn2;
15000
15001 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15002 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15003 if ((insn1 & ~0xfffc) == LD_R2_0R12
15004 && insn2 == ADD_R2_R2_R12)
15005 {
95f0d0d2 15006 bfd_put_32 (input_bfd,
006589cf
AM
15007 LIS_R2 + PPC_HA (relocation),
15008 contents + rel->r_offset);
95f0d0d2 15009 bfd_put_32 (input_bfd,
006589cf
AM
15010 ADDI_R2_R2 + PPC_LO (relocation),
15011 contents + rel->r_offset + 4);
15012 }
15013 }
15014 else
15015 {
15016 relocation -= (rel->r_offset
15017 + input_section->output_offset
15018 + input_section->output_section->vma);
15019 if (relocation + 0x80008000 <= 0xffffffff)
15020 {
15021 unsigned int insn1, insn2;
15022
15023 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15024 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15025 if ((insn1 & ~0xfffc) == LD_R2_0R12
15026 && insn2 == ADD_R2_R2_R12)
15027 {
95f0d0d2 15028 bfd_put_32 (input_bfd,
006589cf
AM
15029 ADDIS_R2_R12 + PPC_HA (relocation),
15030 contents + rel->r_offset);
95f0d0d2 15031 bfd_put_32 (input_bfd,
006589cf
AM
15032 ADDI_R2_R2 + PPC_LO (relocation),
15033 contents + rel->r_offset + 4);
15034 }
15035 }
15036 }
15037 break;
15038
52a82034
AM
15039 case R_PPC64_REL16_HA:
15040 /* If we are generating a non-PIC executable, edit
15041 . 0: addis 2,12,.TOC.-0b@ha
15042 . addi 2,2,.TOC.-0b@l
15043 used by ELFv2 global entry points to set up r2, to
15044 . lis 2,.TOC.@ha
15045 . addi 2,2,.TOC.@l
15046 if .TOC. is in range. */
0e1862bb 15047 if (!bfd_link_pic (info)
810d4e75 15048 && !info->traditional_format
006589cf 15049 && !htab->opd_abi
4f038ee5 15050 && rel->r_addend == d_offset
52a82034
AM
15051 && h != NULL && &h->elf == htab->elf.hgot
15052 && rel + 1 < relend
15053 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15054 && rel[1].r_offset == rel->r_offset + 4
15055 && rel[1].r_addend == rel->r_addend + 4
15056 && relocation + 0x80008000 <= 0xffffffff)
15057 {
15058 unsigned int insn1, insn2;
4a421c53 15059 offset = rel->r_offset - d_offset;
95f0d0d2
AM
15060 insn1 = bfd_get_32 (input_bfd, contents + offset);
15061 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
15062 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15063 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
15064 {
15065 r_type = R_PPC64_ADDR16_HA;
15066 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15067 rel->r_addend -= d_offset;
15068 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15069 rel[1].r_addend -= d_offset + 4;
95f0d0d2 15070 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
15071 }
15072 }
15073 break;
411e1bfb
AM
15074 }
15075
15076 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 15077 insn = 0;
b25116a9
AM
15078 max_br_offset = 1 << 25;
15079 addend = rel->r_addend;
bc30df16 15080 reloc_dest = DEST_NORMAL;
65f38f15 15081 switch (r_type)
5bd4f169
AM
15082 {
15083 default:
65f38f15 15084 break;
5bd4f169 15085
3b421ab3
AM
15086 case R_PPC64_TOCSAVE:
15087 if (relocation + addend == (rel->r_offset
15088 + input_section->output_offset
15089 + input_section->output_section->vma)
15090 && tocsave_find (htab, NO_INSERT,
15091 &local_syms, rel, input_bfd))
15092 {
15093 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15094 if (insn == NOP
15095 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
15096 bfd_put_32 (input_bfd,
15097 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
15098 contents + rel->r_offset);
15099 }
15100 break;
15101
65f38f15
AM
15102 /* Branch taken prediction relocations. */
15103 case R_PPC64_ADDR14_BRTAKEN:
15104 case R_PPC64_REL14_BRTAKEN:
cedb70c5 15105 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 15106 /* Fall through. */
65f38f15 15107
86c76c7b 15108 /* Branch not taken prediction relocations. */
65f38f15
AM
15109 case R_PPC64_ADDR14_BRNTAKEN:
15110 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 15111 insn |= bfd_get_32 (input_bfd,
411e1bfb 15112 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 15113 /* Fall through. */
86c76c7b 15114
b25116a9
AM
15115 case R_PPC64_REL14:
15116 max_br_offset = 1 << 15;
1a0670f3 15117 /* Fall through. */
5bd4f169 15118
65f38f15 15119 case R_PPC64_REL24:
05d0e962 15120 case R_PPC64_REL24_NOTOC:
23cedd1d 15121 case R_PPC64_PLTCALL:
5663e321 15122 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
15123 /* Calls to functions with a different TOC, such as calls to
15124 shared objects, need to alter the TOC pointer. This is
15125 done using a linkage stub. A REL24 branching to these
15126 linkage stubs needs to be followed by a nop, as the nop
15127 will be replaced with an instruction to restore the TOC
15128 base pointer. */
8387904d 15129 fdh = h;
b31867b6
AM
15130 if (h != NULL
15131 && h->oh != NULL
15132 && h->oh->is_func_descriptor)
15133 fdh = ppc_follow_link (h->oh);
31c76678
DK
15134 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15135 htab);
5663e321
AM
15136 if ((r_type == R_PPC64_PLTCALL
15137 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 15138 && stub_entry != NULL
05d0e962
AM
15139 && stub_entry->stub_type >= ppc_stub_plt_call
15140 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
15141 stub_entry = NULL;
15142
6abec6bc 15143 if (stub_entry != NULL
d0abeec8
AM
15144 && ((stub_entry->stub_type >= ppc_stub_plt_call
15145 && stub_entry->stub_type <= ppc_stub_plt_call_both)
ad8e1ba5 15146 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
15147 || stub_entry->stub_type == ppc_stub_plt_branch_both
15148 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15149 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 15150 {
b25116a9 15151 bfd_boolean can_plt_call = FALSE;
721956f4 15152
6e1816be
AM
15153 if (stub_entry->stub_type == ppc_stub_plt_call
15154 && !htab->opd_abi
15155 && htab->params->plt_localentry0 != 0
15156 && is_elfv2_localentry0 (&h->elf))
15157 {
15158 /* The function doesn't use or change r2. */
15159 can_plt_call = TRUE;
15160 }
05d0e962
AM
15161 else if (r_type == R_PPC64_REL24_NOTOC)
15162 {
15163 /* NOTOC calls don't need to restore r2. */
15164 can_plt_call = TRUE;
15165 }
6e1816be 15166
f378ab09 15167 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
15168 branch and link followed by a nop. The nop is
15169 replaced by an insn to restore r2. */
6e1816be 15170 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 15171 {
ba8ca3e7
AM
15172 unsigned long br;
15173
15174 br = bfd_get_32 (input_bfd,
15175 contents + rel->r_offset);
15176 if ((br & 1) != 0)
41bd81ab 15177 {
ba8ca3e7
AM
15178 unsigned long nop;
15179
15180 nop = bfd_get_32 (input_bfd,
15181 contents + rel->r_offset + 4);
23cedd1d
AM
15182 if (nop == LD_R2_0R1 + STK_TOC (htab))
15183 can_plt_call = TRUE;
15184 else if (nop == NOP
15185 || nop == CROR_151515
15186 || nop == CROR_313131)
a7f2871e 15187 {
ba8ca3e7
AM
15188 if (h != NULL
15189 && (h == htab->tls_get_addr_fd
15190 || h == htab->tls_get_addr)
7c9cf415 15191 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
15192 {
15193 /* Special stub used, leave nop alone. */
15194 }
15195 else
a078d95a
AM
15196 bfd_put_32 (input_bfd,
15197 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
15198 contents + rel->r_offset + 4);
15199 can_plt_call = TRUE;
a7f2871e 15200 }
41bd81ab 15201 }
5bd4f169 15202 }
721956f4 15203
ba8ca3e7 15204 if (!can_plt_call && h != NULL)
721956f4 15205 {
ba8ca3e7
AM
15206 const char *name = h->elf.root.root.string;
15207
15208 if (*name == '.')
15209 ++name;
15210
15211 if (strncmp (name, "__libc_start_main", 17) == 0
15212 && (name[17] == 0 || name[17] == '@'))
6ab189d5 15213 {
ba8ca3e7
AM
15214 /* Allow crt1 branch to go via a toc adjusting
15215 stub. Other calls that never return could do
15216 the same, if we could detect such. */
b25116a9 15217 can_plt_call = TRUE;
6ab189d5 15218 }
ba8ca3e7
AM
15219 }
15220
15221 if (!can_plt_call)
15222 {
15223 /* g++ as of 20130507 emits self-calls without a
15224 following nop. This is arguably wrong since we
15225 have conflicting information. On the one hand a
15226 global symbol and on the other a local call
15227 sequence, but don't error for this special case.
15228 It isn't possible to cheaply verify we have
15229 exactly such a call. Allow all calls to the same
15230 section. */
15231 asection *code_sec = sec;
15232
15233 if (get_opd_info (sec) != NULL)
ad8e1ba5 15234 {
ba8ca3e7
AM
15235 bfd_vma off = (relocation + addend
15236 - sec->output_section->vma
15237 - sec->output_offset);
bc30df16 15238
ba8ca3e7 15239 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 15240 }
ba8ca3e7
AM
15241 if (code_sec == input_section)
15242 can_plt_call = TRUE;
15243 }
15244
15245 if (!can_plt_call)
15246 {
05d0e962
AM
15247 if (stub_entry->stub_type >= ppc_stub_plt_call
15248 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 15249 info->callbacks->einfo
695344c0 15250 /* xgettext:c-format */
c1c8c1ef 15251 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15252 "(plt call stub)\n"),
4805fc55
AM
15253 input_bfd, input_section, rel->r_offset, sym_name);
15254 else
15255 info->callbacks->einfo
695344c0 15256 /* xgettext:c-format */
c1c8c1ef 15257 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15258 "(toc save/adjust stub)\n"),
4805fc55 15259 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
15260
15261 bfd_set_error (bfd_error_bad_value);
15262 ret = FALSE;
721956f4
AM
15263 }
15264
b25116a9 15265 if (can_plt_call
05d0e962
AM
15266 && stub_entry->stub_type >= ppc_stub_plt_call
15267 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
15268 unresolved_reloc = FALSE;
15269 }
15270
6abec6bc
AM
15271 if ((stub_entry == NULL
15272 || stub_entry->stub_type == ppc_stub_long_branch
15273 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
15274 && get_opd_info (sec) != NULL)
15275 {
15276 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
15277 bfd_vma off = (relocation + addend
15278 - sec->output_section->vma
15279 - sec->output_offset);
aef36ac1 15280 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
15281 if (dest != (bfd_vma) -1)
15282 {
15283 relocation = dest;
15284 addend = 0;
bc30df16 15285 reloc_dest = DEST_OPD;
8387904d
AM
15286 }
15287 }
15288
b25116a9
AM
15289 /* If the branch is out of reach we ought to have a long
15290 branch stub. */
15291 from = (rel->r_offset
15292 + input_section->output_offset
15293 + input_section->output_section->vma);
15294
6911b7dc
AM
15295 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15296 ? fdh->elf.other
15297 : sym->st_other);
15298
6abec6bc
AM
15299 if (stub_entry != NULL
15300 && (stub_entry->stub_type == ppc_stub_long_branch
15301 || stub_entry->stub_type == ppc_stub_plt_branch)
15302 && (r_type == R_PPC64_ADDR14_BRTAKEN
15303 || r_type == R_PPC64_ADDR14_BRNTAKEN
15304 || (relocation + addend - from + max_br_offset
15305 < 2 * max_br_offset)))
15306 /* Don't use the stub if this branch is in range. */
15307 stub_entry = NULL;
b25116a9 15308
05d0e962
AM
15309 if (stub_entry != NULL
15310 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15311 || stub_entry->stub_type == ppc_stub_long_branch_both
15312 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15313 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15314 && (r_type != R_PPC64_REL24_NOTOC
15315 || ((fdh ? fdh->elf.other : sym->st_other)
4a4e7361 15316 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
05d0e962
AM
15317 && (relocation + addend - from + max_br_offset
15318 < 2 * max_br_offset))
15319 stub_entry = NULL;
15320
15321 if (stub_entry != NULL
15322 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15323 || stub_entry->stub_type == ppc_stub_long_branch_both
15324 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15325 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15326 && r_type == R_PPC64_REL24_NOTOC
15327 && (relocation + addend - from + max_br_offset
15328 < 2 * max_br_offset))
15329 stub_entry = NULL;
15330
b25116a9
AM
15331 if (stub_entry != NULL)
15332 {
15333 /* Munge up the value and addend so that we call the stub
15334 rather than the procedure directly. */
a4b6fadd
AM
15335 asection *stub_sec = stub_entry->group->stub_sec;
15336
15337 if (stub_entry->stub_type == ppc_stub_save_res)
15338 relocation += (stub_sec->output_offset
15339 + stub_sec->output_section->vma
15340 + stub_sec->size - htab->sfpr->size
15341 - htab->sfpr->output_offset
15342 - htab->sfpr->output_section->vma);
15343 else
15344 relocation = (stub_entry->stub_offset
15345 + stub_sec->output_offset
15346 + stub_sec->output_section->vma);
b25116a9 15347 addend = 0;
bc30df16 15348 reloc_dest = DEST_STUB;
3b421ab3 15349
05d0e962
AM
15350 if (((stub_entry->stub_type == ppc_stub_plt_call
15351 && ALWAYS_EMIT_R2SAVE)
15352 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15353 || stub_entry->stub_type == ppc_stub_plt_call_both)
e81b4c93
AM
15354 && !(h != NULL
15355 && (h == htab->tls_get_addr_fd
15356 || h == htab->tls_get_addr)
15357 && htab->params->tls_get_addr_opt)
3b421ab3
AM
15358 && rel + 1 < relend
15359 && rel[1].r_offset == rel->r_offset + 4
15360 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15361 relocation += 4;
05d0e962
AM
15362 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15363 || stub_entry->stub_type == ppc_stub_plt_branch_both
15364 || stub_entry->stub_type == ppc_stub_plt_call_both)
15365 && r_type == R_PPC64_REL24_NOTOC)
15366 relocation += 4;
5663e321
AM
15367
15368 if (r_type == R_PPC64_REL24_NOTOC
15369 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
15370 || stub_entry->stub_type == ppc_stub_plt_call_both))
15371 htab->notoc_plt = 1;
b25116a9
AM
15372 }
15373
15374 if (insn != 0)
15375 {
794e51c0 15376 if (is_isa_v2)
721956f4 15377 {
b25116a9
AM
15378 /* Set 'a' bit. This is 0b00010 in BO field for branch
15379 on CR(BI) insns (BO == 001at or 011at), and 0b01000
15380 for branch on CTR insns (BO == 1a00t or 1a01t). */
15381 if ((insn & (0x14 << 21)) == (0x04 << 21))
15382 insn |= 0x02 << 21;
15383 else if ((insn & (0x14 << 21)) == (0x10 << 21))
15384 insn |= 0x08 << 21;
15385 else
15386 break;
15387 }
15388 else
15389 {
15390 /* Invert 'y' bit if not the default. */
4cc603a5 15391 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 15392 insn ^= 0x01 << 21;
721956f4 15393 }
b25116a9 15394
95f0d0d2 15395 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 15396 }
e86ce104 15397
06da1e8e
AM
15398 /* NOP out calls to undefined weak functions.
15399 We can thus call a weak function without first
15400 checking whether the function is defined. */
b25116a9 15401 else if (h != NULL
039b3fef 15402 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 15403 && h->elf.dynindx == -1
05d0e962
AM
15404 && (r_type == R_PPC64_REL24
15405 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 15406 && relocation == 0
4cc603a5 15407 && addend == 0)
e86ce104 15408 {
95f0d0d2 15409 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 15410 goto copy_reloc;
e86ce104 15411 }
65f38f15 15412 break;
066f4018
AM
15413
15414 case R_PPC64_GOT16_DS:
15415 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15416 if (relocation + addend - from + 0x8000 < 0x10000
15417 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15418 {
15419 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15420 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15421 {
15422 insn += (14u << 26) - (58u << 26);
15423 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15424 r_type = R_PPC64_TOC16;
15425 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15426 }
15427 }
15428 break;
15429
15430 case R_PPC64_GOT16_LO_DS:
15431 case R_PPC64_GOT16_HA:
15432 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15433 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
15434 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15435 {
15436 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15437 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15438 {
15439 insn += (14u << 26) - (58u << 26);
15440 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15441 r_type = R_PPC64_TOC16_LO;
15442 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15443 }
15444 else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
15445 {
15446 r_type = R_PPC64_TOC16_HA;
15447 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15448 }
15449 }
15450 break;
4a421c53
AM
15451
15452 case R_PPC64_GOT_PCREL34:
15453 from = (rel->r_offset
15454 + input_section->output_section->vma
15455 + input_section->output_offset);
15456 if (relocation - from + (1ULL << 33) < 1ULL << 34
15457 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15458 {
15459 offset = rel->r_offset;
15460 pinsn = bfd_get_32 (input_bfd, contents + offset);
15461 pinsn <<= 32;
15462 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15463 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15464 == ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
15465 {
15466 /* Replace with paddi. */
15467 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
15468 r_type = R_PPC64_PCREL34;
15469 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15470 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
15471 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
15472 goto pcrelopt;
15473 }
15474 }
15475 break;
15476
15477 case R_PPC64_PCREL34:
15478 if (SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15479 {
15480 offset = rel->r_offset;
15481 pinsn = bfd_get_32 (input_bfd, contents + offset);
15482 pinsn <<= 32;
15483 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15484 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15485 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
15486 | (14ULL << 26) /* paddi */))
15487 {
15488 pcrelopt:
15489 if (rel + 1 < relend
15490 && rel[1].r_offset == offset
15491 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT))
15492 {
15493 bfd_vma off2 = rel[1].r_addend;
15494 if (off2 == 0)
15495 /* zero means next insn. */
15496 off2 = 8;
15497 off2 += offset;
15498 if (off2 + 4 <= input_section->size)
15499 {
15500 uint64_t pinsn2;
dd9b12c2 15501 bfd_signed_vma addend_off;
4a421c53
AM
15502 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
15503 pinsn2 <<= 32;
15504 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
77486630
AM
15505 {
15506 if (off2 + 8 > input_section->size)
15507 break;
15508 pinsn2 |= bfd_get_32 (input_bfd,
15509 contents + off2 + 4);
15510 }
dd9b12c2 15511 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
4a421c53 15512 {
dd9b12c2
AM
15513 addend += addend_off;
15514 rel->r_addend = addend;
4a421c53
AM
15515 bfd_put_32 (input_bfd, pinsn >> 32,
15516 contents + offset);
15517 bfd_put_32 (input_bfd, pinsn,
15518 contents + offset + 4);
15519 bfd_put_32 (input_bfd, pinsn2 >> 32,
15520 contents + off2);
77486630
AM
15521 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15522 bfd_put_32 (input_bfd, pinsn2,
15523 contents + off2 + 4);
4a421c53
AM
15524 }
15525 }
15526 }
15527 }
15528 }
15529 break;
65f38f15 15530 }
5bd4f169 15531
411e1bfb 15532 tls_type = 0;
23cedd1d 15533 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
15534 switch (r_type)
15535 {
15536 default:
cf97bcb0
AM
15537 /* xgettext:c-format */
15538 _bfd_error_handler (_("%pB: %s unsupported"),
15539 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 15540
65f38f15 15541 bfd_set_error (bfd_error_bad_value);
b34976b6 15542 ret = FALSE;
c316a17c 15543 goto copy_reloc;
5bd4f169 15544
65f38f15 15545 case R_PPC64_NONE:
411e1bfb 15546 case R_PPC64_TLS:
727fc41e
AM
15547 case R_PPC64_TLSGD:
15548 case R_PPC64_TLSLD:
3b421ab3 15549 case R_PPC64_TOCSAVE:
04c9666a
AM
15550 case R_PPC64_GNU_VTINHERIT:
15551 case R_PPC64_GNU_VTENTRY:
006589cf 15552 case R_PPC64_ENTRY:
4a421c53 15553 case R_PPC64_PCREL_OPT:
c316a17c 15554 goto copy_reloc;
5bd4f169
AM
15555
15556 /* GOT16 relocations. Like an ADDR16 using the symbol's
15557 address in the GOT as relocation value instead of the
411e1bfb 15558 symbol's value itself. Also, create a GOT entry for the
5bd4f169 15559 symbol and put the symbol value there. */
411e1bfb
AM
15560 case R_PPC64_GOT_TLSGD16:
15561 case R_PPC64_GOT_TLSGD16_LO:
15562 case R_PPC64_GOT_TLSGD16_HI:
15563 case R_PPC64_GOT_TLSGD16_HA:
c213164a 15564 case R_PPC64_GOT_TLSGD34:
951fd09b 15565 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
15566 goto dogot;
15567
15568 case R_PPC64_GOT_TLSLD16:
15569 case R_PPC64_GOT_TLSLD16_LO:
15570 case R_PPC64_GOT_TLSLD16_HI:
15571 case R_PPC64_GOT_TLSLD16_HA:
c213164a 15572 case R_PPC64_GOT_TLSLD34:
951fd09b 15573 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
15574 goto dogot;
15575
15576 case R_PPC64_GOT_TPREL16_DS:
15577 case R_PPC64_GOT_TPREL16_LO_DS:
15578 case R_PPC64_GOT_TPREL16_HI:
15579 case R_PPC64_GOT_TPREL16_HA:
c213164a 15580 case R_PPC64_GOT_TPREL34:
411e1bfb
AM
15581 tls_type = TLS_TLS | TLS_TPREL;
15582 goto dogot;
15583
15584 case R_PPC64_GOT_DTPREL16_DS:
15585 case R_PPC64_GOT_DTPREL16_LO_DS:
15586 case R_PPC64_GOT_DTPREL16_HI:
15587 case R_PPC64_GOT_DTPREL16_HA:
c213164a 15588 case R_PPC64_GOT_DTPREL34:
411e1bfb
AM
15589 tls_type = TLS_TLS | TLS_DTPREL;
15590 goto dogot;
15591
65f38f15
AM
15592 case R_PPC64_GOT16:
15593 case R_PPC64_GOT16_LO:
15594 case R_PPC64_GOT16_HI:
15595 case R_PPC64_GOT16_HA:
15596 case R_PPC64_GOT16_DS:
15597 case R_PPC64_GOT16_LO_DS:
5663e321 15598 case R_PPC64_GOT_PCREL34:
411e1bfb 15599 dogot:
5bd4f169
AM
15600 {
15601 /* Relocation is to the entry for this symbol in the global
15602 offset table. */
e717da7e 15603 asection *got;
d881513a 15604 bfd_vma *offp;
5bd4f169 15605 bfd_vma off;
d881513a 15606 unsigned long indx = 0;
927be08e 15607 struct got_entry *ent;
65f38f15 15608
d881513a 15609 if (tls_type == (TLS_TLS | TLS_LD)
f749f26e 15610 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
927be08e 15611 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 15612 else
5bd4f169 15613 {
d881513a
AM
15614 if (h != NULL)
15615 {
f0158f44
AM
15616 if (!htab->elf.dynamic_sections_created
15617 || h->elf.dynindx == -1
15618 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 15619 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
15620 /* This is actually a static link, or it is a
15621 -Bsymbolic link and the symbol is defined
15622 locally, or the symbol was forced to be local
15623 because of a version file. */
15624 ;
15625 else
15626 {
039b3fef 15627 indx = h->elf.dynindx;
d881513a
AM
15628 unresolved_reloc = FALSE;
15629 }
039b3fef 15630 ent = h->elf.got.glist;
d881513a 15631 }
411e1bfb 15632 else
5bd4f169 15633 {
d881513a
AM
15634 if (local_got_ents == NULL)
15635 abort ();
15636 ent = local_got_ents[r_symndx];
5bd4f169 15637 }
d881513a
AM
15638
15639 for (; ent != NULL; ent = ent->next)
133a1f60 15640 if (ent->addend == orig_rel.r_addend
e717da7e 15641 && ent->owner == input_bfd
d881513a
AM
15642 && ent->tls_type == tls_type)
15643 break;
5bd4f169 15644 }
411e1bfb 15645
927be08e
AM
15646 if (ent == NULL)
15647 abort ();
15648 if (ent->is_indirect)
15649 ent = ent->got.ent;
15650 offp = &ent->got.offset;
15651 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
15652 if (got == NULL)
15653 abort ();
15654
411e1bfb
AM
15655 /* The offset must always be a multiple of 8. We use the
15656 least significant bit to record whether we have already
15657 processed this entry. */
d881513a 15658 off = *offp;
411e1bfb
AM
15659 if ((off & 1) != 0)
15660 off &= ~1;
5bd4f169
AM
15661 else
15662 {
411e1bfb
AM
15663 /* Generate relocs for the dynamic linker, except in
15664 the case of TLSLD where we'll use one entry per
15665 module. */
25f23106
AM
15666 asection *relgot;
15667 bfd_boolean ifunc;
e717da7e 15668
d881513a 15669 *offp = off | 1;
25f23106
AM
15670 relgot = NULL;
15671 ifunc = (h != NULL
15672 ? h->elf.type == STT_GNU_IFUNC
15673 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 15674 if (ifunc)
82e66161
AM
15675 {
15676 relgot = htab->elf.irelplt;
15677 if (indx == 0)
15678 htab->local_ifunc_resolver = 1;
15679 else if (is_static_defined (&h->elf))
15680 htab->maybe_local_ifunc_resolver = 1;
15681 }
f0158f44
AM
15682 else if (indx != 0
15683 || (bfd_link_pic (info)
15684 && (h == NULL
f749f26e
AM
15685 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15686 && !(tls_type != 0
f15d0b54
AM
15687 && bfd_link_executable (info)
15688 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 15689 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 15690 if (relgot != NULL)
5bd4f169 15691 {
e717da7e
AM
15692 outrel.r_offset = (got->output_section->vma
15693 + got->output_offset
411e1bfb 15694 + off);
133a1f60 15695 outrel.r_addend = orig_rel.r_addend;
d881513a 15696 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 15697 {
411e1bfb 15698 outrel.r_addend = 0;
e515b051 15699 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
15700 if (tls_type == (TLS_TLS | TLS_GD))
15701 {
e717da7e
AM
15702 loc = relgot->contents;
15703 loc += (relgot->reloc_count++
d881513a
AM
15704 * sizeof (Elf64_External_Rela));
15705 bfd_elf64_swap_reloca_out (output_bfd,
15706 &outrel, loc);
e515b051 15707 outrel.r_offset += 8;
133a1f60 15708 outrel.r_addend = orig_rel.r_addend;
d881513a
AM
15709 outrel.r_info
15710 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 15711 }
411e1bfb 15712 }
951fd09b 15713 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 15714 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 15715 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 15716 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
15717 else if (indx != 0)
15718 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15719 else
81407a69 15720 {
25f23106
AM
15721 if (ifunc)
15722 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15723 else
15724 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
15725
15726 /* Write the .got section contents for the sake
15727 of prelink. */
e717da7e 15728 loc = got->contents + off;
23fbd6fa
JJ
15729 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15730 loc);
81407a69 15731 }
81407a69
AM
15732
15733 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
15734 {
15735 outrel.r_addend += relocation;
15736 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
15737 {
15738 if (htab->elf.tls_sec == NULL)
15739 outrel.r_addend = 0;
15740 else
15741 outrel.r_addend -= htab->elf.tls_sec->vma;
15742 }
e515b051 15743 }
e717da7e
AM
15744 loc = relgot->contents;
15745 loc += (relgot->reloc_count++
411e1bfb
AM
15746 * sizeof (Elf64_External_Rela));
15747 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15748 }
15749
ad8e1ba5 15750 /* Init the .got section contents here if we're not
81407a69 15751 emitting a reloc. */
d881513a 15752 else
411e1bfb 15753 {
133a1f60 15754 relocation += orig_rel.r_addend;
f0158f44 15755 if (tls_type != 0)
411e1bfb 15756 {
989f9879
AM
15757 if (htab->elf.tls_sec == NULL)
15758 relocation = 0;
15759 else
15760 {
f0158f44
AM
15761 if (tls_type & TLS_LD)
15762 relocation = 0;
15763 else
15764 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 15765 if (tls_type & TLS_TPREL)
989f9879
AM
15766 relocation += DTP_OFFSET - TP_OFFSET;
15767 }
5bd4f169 15768
f0158f44 15769 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
15770 {
15771 bfd_put_64 (output_bfd, relocation,
e717da7e 15772 got->contents + off + 8);
676ee2b5 15773 relocation = 1;
7b609f53 15774 }
411e1bfb
AM
15775 }
15776 bfd_put_64 (output_bfd, relocation,
e717da7e 15777 got->contents + off);
5bd4f169
AM
15778 }
15779 }
15780
65f38f15
AM
15781 if (off >= (bfd_vma) -2)
15782 abort ();
15783
bf102f86 15784 relocation = got->output_section->vma + got->output_offset + off;
133a1f60 15785 addend = 0;
c213164a
AM
15786 if (!(r_type == R_PPC64_GOT_PCREL34
15787 || r_type == R_PPC64_GOT_TLSGD34
15788 || r_type == R_PPC64_GOT_TLSLD34
15789 || r_type == R_PPC64_GOT_TPREL34
15790 || r_type == R_PPC64_GOT_DTPREL34))
5663e321 15791 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 15792 }
65f38f15
AM
15793 break;
15794
15795 case R_PPC64_PLT16_HA:
15796 case R_PPC64_PLT16_HI:
15797 case R_PPC64_PLT16_LO:
08be3224 15798 case R_PPC64_PLT16_LO_DS:
5663e321
AM
15799 case R_PPC64_PLT_PCREL34:
15800 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
15801 case R_PPC64_PLT32:
15802 case R_PPC64_PLT64:
23cedd1d 15803 case R_PPC64_PLTSEQ:
5663e321 15804 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 15805 case R_PPC64_PLTCALL:
5663e321 15806 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
15807 /* Relocation is to the entry for this symbol in the
15808 procedure linkage table. */
23cedd1d 15809 unresolved_reloc = TRUE;
cbf95972
AM
15810 {
15811 struct plt_entry **plt_list = NULL;
15812 if (h != NULL)
15813 plt_list = &h->elf.plt.plist;
15814 else if (local_got_ents != NULL)
15815 {
15816 struct plt_entry **local_plt = (struct plt_entry **)
15817 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 15818 plt_list = local_plt + r_symndx;
cbf95972
AM
15819 }
15820 if (plt_list)
15821 {
15822 struct plt_entry *ent;
65f38f15 15823
cbf95972
AM
15824 for (ent = *plt_list; ent != NULL; ent = ent->next)
15825 if (ent->plt.offset != (bfd_vma) -1
133a1f60 15826 && ent->addend == orig_rel.r_addend)
cbf95972
AM
15827 {
15828 asection *plt;
08be3224 15829 bfd_vma got;
cbf95972
AM
15830
15831 plt = htab->elf.splt;
15832 if (!htab->elf.dynamic_sections_created
15833 || h == NULL
15834 || h->elf.dynindx == -1)
2d7ad24e
AM
15835 {
15836 if (h != NULL
15837 ? h->elf.type == STT_GNU_IFUNC
15838 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15839 plt = htab->elf.iplt;
15840 else
15841 plt = htab->pltlocal;
15842 }
15843 relocation = (plt->output_section->vma
15844 + plt->output_offset
15845 + ent->plt.offset);
08be3224 15846 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
15847 || r_type == R_PPC64_PLT16_HI
15848 || r_type == R_PPC64_PLT16_LO
15849 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
15850 {
15851 got = (elf_gp (output_bfd)
15852 + htab->sec_info[input_section->id].toc_off);
15853 relocation -= got;
15854 }
133a1f60 15855 addend = 0;
cbf95972
AM
15856 unresolved_reloc = FALSE;
15857 break;
15858 }
15859 }
15860 }
65f38f15 15861 break;
5bd4f169 15862
0b13192e
AM
15863 case R_PPC64_TOC:
15864 /* Relocation value is TOC base. */
15865 relocation = TOCstart;
cf35638d 15866 if (r_symndx == STN_UNDEF)
6f20ed8a 15867 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
15868 else if (unresolved_reloc)
15869 ;
6f20ed8a
AM
15870 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15871 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
15872 else
15873 unresolved_reloc = TRUE;
ab96bf03 15874 goto dodyn;
0b13192e 15875
5bd4f169
AM
15876 /* TOC16 relocs. We want the offset relative to the TOC base,
15877 which is the address of the start of the TOC plus 0x8000.
15878 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15879 in this order. */
65f38f15
AM
15880 case R_PPC64_TOC16:
15881 case R_PPC64_TOC16_LO:
15882 case R_PPC64_TOC16_HI:
15883 case R_PPC64_TOC16_DS:
15884 case R_PPC64_TOC16_LO_DS:
15885 case R_PPC64_TOC16_HA:
6f20ed8a 15886 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
1bdd8fac
AM
15887 if (h != NULL)
15888 goto dodyn;
5bd4f169
AM
15889 break;
15890
15891 /* Relocate against the beginning of the section. */
65f38f15
AM
15892 case R_PPC64_SECTOFF:
15893 case R_PPC64_SECTOFF_LO:
15894 case R_PPC64_SECTOFF_HI:
15895 case R_PPC64_SECTOFF_DS:
15896 case R_PPC64_SECTOFF_LO_DS:
15897 case R_PPC64_SECTOFF_HA:
4ce794b7 15898 if (sec != NULL)
65f38f15 15899 addend -= sec->output_section->vma;
5bd4f169
AM
15900 break;
15901
25f23106
AM
15902 case R_PPC64_REL16:
15903 case R_PPC64_REL16_LO:
15904 case R_PPC64_REL16_HI:
15905 case R_PPC64_REL16_HA:
4a969973
AM
15906 case R_PPC64_REL16_HIGH:
15907 case R_PPC64_REL16_HIGHA:
15908 case R_PPC64_REL16_HIGHER:
15909 case R_PPC64_REL16_HIGHERA:
15910 case R_PPC64_REL16_HIGHEST:
15911 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
15912 case R_PPC64_REL16_HIGHER34:
15913 case R_PPC64_REL16_HIGHERA34:
15914 case R_PPC64_REL16_HIGHEST34:
15915 case R_PPC64_REL16_HIGHESTA34:
a680de9a 15916 case R_PPC64_REL16DX_HA:
721956f4
AM
15917 case R_PPC64_REL14:
15918 case R_PPC64_REL14_BRNTAKEN:
15919 case R_PPC64_REL14_BRTAKEN:
5d1634d7 15920 case R_PPC64_REL24:
05d0e962 15921 case R_PPC64_REL24_NOTOC:
5663e321
AM
15922 case R_PPC64_PCREL34:
15923 case R_PPC64_PCREL28:
5d1634d7
AM
15924 break;
15925
411e1bfb
AM
15926 case R_PPC64_TPREL16:
15927 case R_PPC64_TPREL16_LO:
15928 case R_PPC64_TPREL16_HI:
15929 case R_PPC64_TPREL16_HA:
15930 case R_PPC64_TPREL16_DS:
15931 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
15932 case R_PPC64_TPREL16_HIGH:
15933 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
15934 case R_PPC64_TPREL16_HIGHER:
15935 case R_PPC64_TPREL16_HIGHERA:
15936 case R_PPC64_TPREL16_HIGHEST:
15937 case R_PPC64_TPREL16_HIGHESTA:
c213164a 15938 case R_PPC64_TPREL34:
766bc656
AM
15939 if (h != NULL
15940 && h->elf.root.type == bfd_link_hash_undefweak
15941 && h->elf.dynindx == -1)
15942 {
15943 /* Make this relocation against an undefined weak symbol
15944 resolve to zero. This is really just a tweak, since
15945 code using weak externs ought to check that they are
15946 defined before using them. */
15947 bfd_byte *p = contents + rel->r_offset - d_offset;
15948
95f0d0d2 15949 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
15950 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15951 if (insn != 0)
95f0d0d2 15952 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
15953 break;
15954 }
989f9879
AM
15955 if (htab->elf.tls_sec != NULL)
15956 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
15957 /* The TPREL16 relocs shouldn't really be used in shared
15958 libs or with non-local symbols as that will result in
15959 DT_TEXTREL being set, but support them anyway. */
15960 goto dodyn;
411e1bfb
AM
15961
15962 case R_PPC64_DTPREL16:
15963 case R_PPC64_DTPREL16_LO:
15964 case R_PPC64_DTPREL16_HI:
15965 case R_PPC64_DTPREL16_HA:
15966 case R_PPC64_DTPREL16_DS:
15967 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
15968 case R_PPC64_DTPREL16_HIGH:
15969 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
15970 case R_PPC64_DTPREL16_HIGHER:
15971 case R_PPC64_DTPREL16_HIGHERA:
15972 case R_PPC64_DTPREL16_HIGHEST:
15973 case R_PPC64_DTPREL16_HIGHESTA:
c213164a 15974 case R_PPC64_DTPREL34:
989f9879
AM
15975 if (htab->elf.tls_sec != NULL)
15976 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
15977 break;
15978
45965137
AM
15979 case R_PPC64_ADDR64_LOCAL:
15980 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15981 ? h->elf.other
15982 : sym->st_other);
15983 break;
15984
e515b051
AM
15985 case R_PPC64_DTPMOD64:
15986 relocation = 1;
15987 addend = 0;
15988 goto dodyn;
15989
411e1bfb 15990 case R_PPC64_TPREL64:
989f9879
AM
15991 if (htab->elf.tls_sec != NULL)
15992 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
15993 goto dodyn;
15994
15995 case R_PPC64_DTPREL64:
989f9879
AM
15996 if (htab->elf.tls_sec != NULL)
15997 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 15998 /* Fall through. */
411e1bfb 15999
65f38f15
AM
16000 /* Relocations that may need to be propagated if this is a
16001 dynamic object. */
04c9666a 16002 case R_PPC64_REL30:
65f38f15
AM
16003 case R_PPC64_REL32:
16004 case R_PPC64_REL64:
16005 case R_PPC64_ADDR14:
16006 case R_PPC64_ADDR14_BRNTAKEN:
16007 case R_PPC64_ADDR14_BRTAKEN:
16008 case R_PPC64_ADDR16:
16009 case R_PPC64_ADDR16_DS:
16010 case R_PPC64_ADDR16_HA:
16011 case R_PPC64_ADDR16_HI:
f9c6b907
AM
16012 case R_PPC64_ADDR16_HIGH:
16013 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
16014 case R_PPC64_ADDR16_HIGHER:
16015 case R_PPC64_ADDR16_HIGHERA:
16016 case R_PPC64_ADDR16_HIGHEST:
16017 case R_PPC64_ADDR16_HIGHESTA:
16018 case R_PPC64_ADDR16_LO:
16019 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
16020 case R_PPC64_ADDR16_HIGHER34:
16021 case R_PPC64_ADDR16_HIGHERA34:
16022 case R_PPC64_ADDR16_HIGHEST34:
16023 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 16024 case R_PPC64_ADDR24:
65f38f15
AM
16025 case R_PPC64_ADDR32:
16026 case R_PPC64_ADDR64:
16027 case R_PPC64_UADDR16:
16028 case R_PPC64_UADDR32:
16029 case R_PPC64_UADDR64:
5663e321
AM
16030 case R_PPC64_D34:
16031 case R_PPC64_D34_LO:
16032 case R_PPC64_D34_HI30:
16033 case R_PPC64_D34_HA30:
16034 case R_PPC64_D28:
411e1bfb 16035 dodyn:
5d1634d7 16036 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
16037 break;
16038
41bd81ab
AM
16039 if (NO_OPD_RELOCS && is_opd)
16040 break;
16041
8a9e8e72 16042 if (bfd_link_pic (info)
b1b07054
AM
16043 ? ((h == NULL
16044 || h->dyn_relocs != NULL)
16045 && ((h != NULL && pc_dynrelocs (h))
16046 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
16047 : (h != NULL
16048 ? h->dyn_relocs != NULL
d311bc8b 16049 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 16050 {
b34976b6 16051 bfd_boolean skip, relocate;
65f38f15 16052 asection *sreloc;
1cf1f670 16053 bfd_vma out_off;
82e66161 16054 long indx = 0;
65f38f15
AM
16055
16056 /* When generating a dynamic object, these relocations
16057 are copied into the output file to be resolved at run
16058 time. */
16059
b34976b6
AM
16060 skip = FALSE;
16061 relocate = FALSE;
65f38f15 16062
1cf1f670
AM
16063 out_off = _bfd_elf_section_offset (output_bfd, info,
16064 input_section, rel->r_offset);
16065 if (out_off == (bfd_vma) -1)
b34976b6 16066 skip = TRUE;
1cf1f670 16067 else if (out_off == (bfd_vma) -2)
b34976b6 16068 skip = TRUE, relocate = TRUE;
1cf1f670
AM
16069 out_off += (input_section->output_section->vma
16070 + input_section->output_offset);
16071 outrel.r_offset = out_off;
411e1bfb 16072 outrel.r_addend = rel->r_addend;
65f38f15 16073
1cf1f670
AM
16074 /* Optimize unaligned reloc use. */
16075 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
16076 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
16077 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
16078 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
16079 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
16080 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
16081 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16082 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16083 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16084
65f38f15 16085 if (skip)
0bb2d96a 16086 memset (&outrel, 0, sizeof outrel);
afe397ea 16087 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
16088 && !is_opd
16089 && r_type != R_PPC64_TOC)
14acf4dc 16090 {
82e66161
AM
16091 indx = h->elf.dynindx;
16092 BFD_ASSERT (indx != -1);
16093 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 16094 }
65f38f15
AM
16095 else
16096 {
41bd81ab
AM
16097 /* This symbol is local, or marked to become local,
16098 or this is an opd section reloc which must point
16099 at a local function. */
65f38f15 16100 outrel.r_addend += relocation;
e86ce104 16101 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 16102 {
3fad3c7c 16103 if (is_opd && h != NULL)
afbe61cf
AM
16104 {
16105 /* Lie about opd entries. This case occurs
16106 when building shared libraries and we
16107 reference a function in another shared
3fad3c7c
AM
16108 lib. The same thing happens for a weak
16109 definition in an application that's
16110 overridden by a strong definition in a
16111 shared lib. (I believe this is a generic
16112 bug in binutils handling of weak syms.)
16113 In these cases we won't use the opd
1e2f5b6e 16114 entry in this lib. */
b34976b6 16115 unresolved_reloc = FALSE;
afbe61cf 16116 }
25f23106
AM
16117 if (!is_opd
16118 && r_type == R_PPC64_ADDR64
16119 && (h != NULL
16120 ? h->elf.type == STT_GNU_IFUNC
16121 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16122 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16123 else
16124 {
16125 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 16126
25f23106
AM
16127 /* We need to relocate .opd contents for ld.so.
16128 Prelink also wants simple and consistent rules
16129 for relocs. This make all RELATIVE relocs have
16130 *r_offset equal to r_addend. */
16131 relocate = TRUE;
16132 }
65f38f15
AM
16133 }
16134 else
16135 {
25f23106
AM
16136 if (h != NULL
16137 ? h->elf.type == STT_GNU_IFUNC
16138 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16139 {
25f53a85 16140 info->callbacks->einfo
695344c0 16141 /* xgettext:c-format */
174d0a74 16142 (_("%H: %s for indirect "
c1c8c1ef 16143 "function `%pT' unsupported\n"),
25f53a85 16144 input_bfd, input_section, rel->r_offset,
25f23106
AM
16145 ppc64_elf_howto_table[r_type]->name,
16146 sym_name);
16147 ret = FALSE;
16148 }
cf35638d 16149 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
16150 ;
16151 else if (sec == NULL || sec->owner == NULL)
16152 {
16153 bfd_set_error (bfd_error_bad_value);
b34976b6 16154 return FALSE;
65f38f15
AM
16155 }
16156 else
16157 {
f26a3287 16158 asection *osec = sec->output_section;
65f38f15 16159
f26a3287
AM
16160 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16161 {
16162 /* TLS symbol values are relative to the
16163 TLS segment. Dynamic relocations for
16164 local TLS symbols therefore can't be
16165 reduced to a relocation against their
16166 section symbol because it holds the
16167 address of the section, not a value
16168 relative to the TLS segment. We could
16169 change the .tdata dynamic section symbol
16170 to be zero value but STN_UNDEF works
16171 and is used elsewhere, eg. for TPREL64
16172 GOT relocs against local TLS symbols. */
16173 osec = htab->elf.tls_sec;
16174 indx = 0;
16175 }
16176 else
74541ad4 16177 {
74541ad4 16178 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
16179 if (indx == 0)
16180 {
16181 if ((osec->flags & SEC_READONLY) == 0
16182 && htab->elf.data_index_section != NULL)
16183 osec = htab->elf.data_index_section;
16184 else
16185 osec = htab->elf.text_index_section;
16186 indx = elf_section_data (osec)->dynindx;
16187 }
16188 BFD_ASSERT (indx != 0);
74541ad4 16189 }
74541ad4 16190
65f38f15
AM
16191 /* We are turning this relocation into one
16192 against a section symbol, so subtract out
16193 the output section's address but not the
16194 offset of the input section in the output
16195 section. */
16196 outrel.r_addend -= osec->vma;
16197 }
16198
16199 outrel.r_info = ELF64_R_INFO (indx, r_type);
16200 }
16201 }
16202
16203 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
16204 if (h != NULL
16205 ? h->elf.type == STT_GNU_IFUNC
16206 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
16207 {
16208 sreloc = htab->elf.irelplt;
16209 if (indx == 0)
16210 htab->local_ifunc_resolver = 1;
16211 else if (is_static_defined (&h->elf))
16212 htab->maybe_local_ifunc_resolver = 1;
16213 }
65f38f15
AM
16214 if (sreloc == NULL)
16215 abort ();
16216
dfbb6ac9
AM
16217 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16218 >= sreloc->size)
16219 abort ();
947216bf
AM
16220 loc = sreloc->contents;
16221 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
16222 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16223
e59a1001
AM
16224 if (!warned_dynamic
16225 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
16226 {
16227 info->callbacks->einfo
16228 /* xgettext:c-format */
16229 (_("%X%P: %pB: %s against %pT "
16230 "is not supported by glibc as a dynamic relocation\n"),
16231 input_bfd,
16232 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
16233 sym_name);
16234 warned_dynamic = TRUE;
16235 }
16236
65f38f15
AM
16237 /* If this reloc is against an external symbol, it will
16238 be computed at runtime, so there's no need to do
81407a69
AM
16239 anything now. However, for the sake of prelink ensure
16240 that the section contents are a known value. */
2cdcc330 16241 if (!relocate)
81407a69
AM
16242 {
16243 unresolved_reloc = FALSE;
16244 /* The value chosen here is quite arbitrary as ld.so
16245 ignores section contents except for the special
16246 case of .opd where the contents might be accessed
16247 before relocation. Choose zero, as that won't
16248 cause reloc overflow. */
16249 relocation = 0;
16250 addend = 0;
16251 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16252 to improve backward compatibility with older
16253 versions of ld. */
16254 if (r_type == R_PPC64_ADDR64)
16255 addend = outrel.r_addend;
16256 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 16257 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 16258 addend = outrel.r_offset;
81407a69 16259 }
65f38f15 16260 }
5bd4f169
AM
16261 break;
16262
65f38f15
AM
16263 case R_PPC64_COPY:
16264 case R_PPC64_GLOB_DAT:
16265 case R_PPC64_JMP_SLOT:
25f23106 16266 case R_PPC64_JMP_IREL:
65f38f15
AM
16267 case R_PPC64_RELATIVE:
16268 /* We shouldn't ever see these dynamic relocs in relocatable
16269 files. */
ae9a127f 16270 /* Fall through. */
65f38f15
AM
16271
16272 case R_PPC64_PLTGOT16:
16273 case R_PPC64_PLTGOT16_DS:
16274 case R_PPC64_PLTGOT16_HA:
16275 case R_PPC64_PLTGOT16_HI:
16276 case R_PPC64_PLTGOT16_LO:
16277 case R_PPC64_PLTGOT16_LO_DS:
16278 case R_PPC64_PLTREL32:
16279 case R_PPC64_PLTREL64:
16280 /* These ones haven't been implemented yet. */
16281
25f53a85 16282 info->callbacks->einfo
695344c0 16283 /* xgettext:c-format */
c1c8c1ef 16284 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 16285 input_bfd,
4ce794b7 16286 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
16287
16288 bfd_set_error (bfd_error_invalid_operation);
b34976b6 16289 ret = FALSE;
c316a17c 16290 goto copy_reloc;
65f38f15 16291 }
5bd4f169 16292
67f0cbdb
AM
16293 /* Multi-instruction sequences that access the TOC can be
16294 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 16295 to nop; addi rb,r2,x; */
67f0cbdb
AM
16296 switch (r_type)
16297 {
16298 default:
16299 break;
16300
16301 case R_PPC64_GOT_TLSLD16_HI:
16302 case R_PPC64_GOT_TLSGD16_HI:
16303 case R_PPC64_GOT_TPREL16_HI:
16304 case R_PPC64_GOT_DTPREL16_HI:
16305 case R_PPC64_GOT16_HI:
16306 case R_PPC64_TOC16_HI:
16307 /* These relocs would only be useful if building up an
16308 offset to later add to r2, perhaps in an indexed
16309 addressing mode instruction. Don't try to optimize.
16310 Unfortunately, the possibility of someone building up an
16311 offset like this or even with the HA relocs, means that
16312 we need to check the high insn when optimizing the low
16313 insn. */
16314 break;
16315
5663e321
AM
16316 case R_PPC64_PLTCALL_NOTOC:
16317 if (!unresolved_reloc)
16318 htab->notoc_plt = 1;
16319 /* Fall through. */
23cedd1d
AM
16320 case R_PPC64_PLTCALL:
16321 if (unresolved_reloc)
16322 {
16323 /* No plt entry. Make this into a direct call. */
16324 bfd_byte *p = contents + rel->r_offset;
16325 insn = bfd_get_32 (input_bfd, p);
16326 insn &= 1;
16327 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
16328 if (r_type == R_PPC64_PLTCALL)
16329 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
16330 unresolved_reloc = save_unresolved_reloc;
16331 r_type = R_PPC64_REL24;
16332 }
16333 break;
16334
5663e321 16335 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
16336 case R_PPC64_PLTSEQ:
16337 if (unresolved_reloc)
16338 {
16339 unresolved_reloc = FALSE;
16340 goto nop_it;
16341 }
16342 break;
16343
5663e321
AM
16344 case R_PPC64_PLT_PCREL34_NOTOC:
16345 if (!unresolved_reloc)
16346 htab->notoc_plt = 1;
16347 /* Fall through. */
16348 case R_PPC64_PLT_PCREL34:
16349 if (unresolved_reloc)
16350 {
16351 bfd_byte *p = contents + rel->r_offset;
16352 bfd_put_32 (input_bfd, PNOP >> 32, p);
16353 bfd_put_32 (input_bfd, PNOP, p + 4);
16354 unresolved_reloc = FALSE;
16355 goto copy_reloc;
16356 }
16357 break;
16358
23cedd1d
AM
16359 case R_PPC64_PLT16_HA:
16360 if (unresolved_reloc)
16361 {
16362 unresolved_reloc = FALSE;
16363 goto nop_it;
16364 }
16365 /* Fall through. */
67f0cbdb
AM
16366 case R_PPC64_GOT_TLSLD16_HA:
16367 case R_PPC64_GOT_TLSGD16_HA:
16368 case R_PPC64_GOT_TPREL16_HA:
16369 case R_PPC64_GOT_DTPREL16_HA:
16370 case R_PPC64_GOT16_HA:
16371 case R_PPC64_TOC16_HA:
98528052 16372 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16373 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 16374 {
23cedd1d
AM
16375 bfd_byte *p;
16376 nop_it:
16377 p = contents + (rel->r_offset & ~3);
98528052 16378 bfd_put_32 (input_bfd, NOP, p);
d830549d 16379 goto copy_reloc;
98528052 16380 }
67f0cbdb
AM
16381 break;
16382
23cedd1d
AM
16383 case R_PPC64_PLT16_LO:
16384 case R_PPC64_PLT16_LO_DS:
16385 if (unresolved_reloc)
16386 {
16387 unresolved_reloc = FALSE;
16388 goto nop_it;
16389 }
16390 /* Fall through. */
67f0cbdb
AM
16391 case R_PPC64_GOT_TLSLD16_LO:
16392 case R_PPC64_GOT_TLSGD16_LO:
16393 case R_PPC64_GOT_TPREL16_LO_DS:
16394 case R_PPC64_GOT_DTPREL16_LO_DS:
16395 case R_PPC64_GOT16_LO:
16396 case R_PPC64_GOT16_LO_DS:
16397 case R_PPC64_TOC16_LO:
16398 case R_PPC64_TOC16_LO_DS:
98528052 16399 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16400 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
16401 {
16402 bfd_byte *p = contents + (rel->r_offset & ~3);
16403 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
16404 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16405 {
16406 /* Transform addic to addi when we change reg. */
16407 insn &= ~((0x3f << 26) | (0x1f << 16));
16408 insn |= (14u << 26) | (2 << 16);
16409 }
16410 else
67f0cbdb 16411 {
98528052
AM
16412 insn &= ~(0x1f << 16);
16413 insn |= 2 << 16;
67f0cbdb 16414 }
560c8763 16415 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
16416 }
16417 break;
9a23f96e
AM
16418
16419 case R_PPC64_TPREL16_HA:
16420 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16421 {
16422 bfd_byte *p = contents + (rel->r_offset & ~3);
16423 insn = bfd_get_32 (input_bfd, p);
16424 if ((insn & ((0x3f << 26) | 0x1f << 16))
16425 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16426 /* xgettext:c-format */
16427 info->callbacks->minfo
16428 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
16429 input_bfd, input_section, rel->r_offset,
16430 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 16431 else
d830549d
AM
16432 {
16433 bfd_put_32 (input_bfd, NOP, p);
16434 goto copy_reloc;
16435 }
9a23f96e
AM
16436 }
16437 break;
16438
16439 case R_PPC64_TPREL16_LO:
16440 case R_PPC64_TPREL16_LO_DS:
16441 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16442 {
16443 bfd_byte *p = contents + (rel->r_offset & ~3);
16444 insn = bfd_get_32 (input_bfd, p);
16445 insn &= ~(0x1f << 16);
16446 insn |= 13 << 16;
16447 bfd_put_32 (input_bfd, insn, p);
16448 }
16449 break;
67f0cbdb
AM
16450 }
16451
65f38f15
AM
16452 /* Do any further special processing. */
16453 switch (r_type)
16454 {
16455 default:
16456 break;
16457
25f23106 16458 case R_PPC64_REL16_HA:
4a969973
AM
16459 case R_PPC64_REL16_HIGHA:
16460 case R_PPC64_REL16_HIGHERA:
16461 case R_PPC64_REL16_HIGHESTA:
a680de9a 16462 case R_PPC64_REL16DX_HA:
f9c6b907
AM
16463 case R_PPC64_ADDR16_HA:
16464 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
16465 case R_PPC64_ADDR16_HIGHERA:
16466 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
16467 case R_PPC64_TOC16_HA:
16468 case R_PPC64_SECTOFF_HA:
411e1bfb 16469 case R_PPC64_TPREL16_HA:
f9c6b907 16470 case R_PPC64_TPREL16_HIGHA:
411e1bfb 16471 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 16472 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
16473 case R_PPC64_DTPREL16_HA:
16474 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 16475 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 16476 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
16477 /* It's just possible that this symbol is a weak symbol
16478 that's not actually defined anywhere. In that case,
16479 'sec' would be NULL, and we should leave the symbol
16480 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
16481 if (sec == NULL)
16482 break;
1a0670f3 16483 /* Fall through. */
5c5f6e17
AM
16484
16485 case R_PPC64_GOT16_HA:
16486 case R_PPC64_PLTGOT16_HA:
16487 case R_PPC64_PLT16_HA:
16488 case R_PPC64_GOT_TLSGD16_HA:
16489 case R_PPC64_GOT_TLSLD16_HA:
16490 case R_PPC64_GOT_TPREL16_HA:
16491 case R_PPC64_GOT_DTPREL16_HA:
16492 /* Add 0x10000 if sign bit in 0:15 is set.
16493 Bits 0:15 are not used. */
16494 addend += 0x8000;
65f38f15
AM
16495 break;
16496
5663e321
AM
16497 case R_PPC64_D34_HA30:
16498 case R_PPC64_ADDR16_HIGHERA34:
16499 case R_PPC64_ADDR16_HIGHESTA34:
16500 case R_PPC64_REL16_HIGHERA34:
16501 case R_PPC64_REL16_HIGHESTA34:
16502 if (sec != NULL)
16503 addend += 1ULL << 33;
16504 break;
16505
65f38f15
AM
16506 case R_PPC64_ADDR16_DS:
16507 case R_PPC64_ADDR16_LO_DS:
16508 case R_PPC64_GOT16_DS:
16509 case R_PPC64_GOT16_LO_DS:
16510 case R_PPC64_PLT16_LO_DS:
16511 case R_PPC64_SECTOFF_DS:
16512 case R_PPC64_SECTOFF_LO_DS:
16513 case R_PPC64_TOC16_DS:
16514 case R_PPC64_TOC16_LO_DS:
16515 case R_PPC64_PLTGOT16_DS:
16516 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
16517 case R_PPC64_GOT_TPREL16_DS:
16518 case R_PPC64_GOT_TPREL16_LO_DS:
16519 case R_PPC64_GOT_DTPREL16_DS:
16520 case R_PPC64_GOT_DTPREL16_LO_DS:
16521 case R_PPC64_TPREL16_DS:
16522 case R_PPC64_TPREL16_LO_DS:
16523 case R_PPC64_DTPREL16_DS:
16524 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
16525 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16526 mask = 3;
a680de9a
PB
16527 /* If this reloc is against an lq, lxv, or stxv insn, then
16528 the value must be a multiple of 16. This is somewhat of
16529 a hack, but the "correct" way to do this by defining _DQ
16530 forms of all the _DS relocs bloats all reloc switches in
16531 this file. It doesn't make much sense to use these
16532 relocs in data, so testing the insn should be safe. */
16533 if ((insn & (0x3f << 26)) == (56u << 26)
16534 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 16535 mask = 15;
a680de9a
PB
16536 relocation += addend;
16537 addend = insn & (mask ^ 3);
16538 if ((relocation & mask) != 0)
65f38f15 16539 {
a680de9a 16540 relocation ^= relocation & mask;
25f53a85 16541 info->callbacks->einfo
695344c0 16542 /* xgettext:c-format */
174d0a74 16543 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 16544 input_bfd, input_section, rel->r_offset,
d830549d 16545 ppc64_elf_howto_table[r_type]->name,
adadcc0c 16546 mask + 1);
65f38f15 16547 bfd_set_error (bfd_error_bad_value);
b34976b6 16548 ret = FALSE;
c316a17c 16549 goto copy_reloc;
65f38f15
AM
16550 }
16551 break;
5bd4f169
AM
16552 }
16553
239e1f3a
AM
16554 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16555 because such sections are not SEC_ALLOC and thus ld.so will
16556 not process them. */
d830549d 16557 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 16558 if (unresolved_reloc
239e1f3a 16559 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
16560 && h->elf.def_dynamic)
16561 && _bfd_elf_section_offset (output_bfd, info, input_section,
16562 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 16563 {
25f53a85 16564 info->callbacks->einfo
695344c0 16565 /* xgettext:c-format */
c1c8c1ef 16566 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 16567 input_bfd, input_section, rel->r_offset,
b80eed39 16568 howto->name,
039b3fef 16569 h->elf.root.root.string);
b34976b6 16570 ret = FALSE;
9c07fe7c 16571 }
5bd4f169 16572
b80eed39
AM
16573 /* 16-bit fields in insns mostly have signed values, but a
16574 few insns have 16-bit unsigned values. Really, we should
16575 have different reloc types. */
16576 if (howto->complain_on_overflow != complain_overflow_dont
16577 && howto->dst_mask == 0xffff
16578 && (input_section->flags & SEC_CODE) != 0)
16579 {
16580 enum complain_overflow complain = complain_overflow_signed;
16581
16582 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
16583 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16584 complain = complain_overflow_bitfield;
16585 else if (howto->rightshift == 0
16586 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16587 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16588 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16589 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16590 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16591 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
16592 complain = complain_overflow_unsigned;
16593 if (howto->complain_on_overflow != complain)
16594 {
16595 alt_howto = *howto;
16596 alt_howto.complain_on_overflow = complain;
16597 howto = &alt_howto;
16598 }
16599 }
16600
5663e321 16601 switch (r_type)
a680de9a 16602 {
5663e321
AM
16603 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
16604 case R_PPC64_D34:
16605 case R_PPC64_D34_LO:
16606 case R_PPC64_D34_HI30:
16607 case R_PPC64_D34_HA30:
16608 case R_PPC64_PCREL34:
16609 case R_PPC64_GOT_PCREL34:
c213164a
AM
16610 case R_PPC64_TPREL34:
16611 case R_PPC64_DTPREL34:
16612 case R_PPC64_GOT_TLSGD34:
16613 case R_PPC64_GOT_TLSLD34:
16614 case R_PPC64_GOT_TPREL34:
16615 case R_PPC64_GOT_DTPREL34:
5663e321
AM
16616 case R_PPC64_PLT_PCREL34:
16617 case R_PPC64_PLT_PCREL34_NOTOC:
16618 case R_PPC64_D28:
16619 case R_PPC64_PCREL28:
16620 if (rel->r_offset + 8 > input_section->size)
16621 r = bfd_reloc_outofrange;
16622 else
16623 {
5663e321
AM
16624 relocation += addend;
16625 if (howto->pc_relative)
16626 relocation -= (rel->r_offset
16627 + input_section->output_offset
16628 + input_section->output_section->vma);
16629 relocation >>= howto->rightshift;
16630
16631 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16632 pinsn <<= 32;
16633 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16634
16635 pinsn &= ~howto->dst_mask;
16636 pinsn |= (((relocation << 16) | (relocation & 0xffff))
16637 & howto->dst_mask);
16638 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
16639 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
16640 r = bfd_reloc_ok;
16641 if (howto->complain_on_overflow == complain_overflow_signed
16642 && (relocation + (1ULL << (howto->bitsize - 1))
16643 >= 1ULL << howto->bitsize))
16644 r = bfd_reloc_overflow;
16645 }
16646 break;
16647
16648 case R_PPC64_REL16DX_HA:
a680de9a
PB
16649 if (rel->r_offset + 4 > input_section->size)
16650 r = bfd_reloc_outofrange;
16651 else
16652 {
16653 relocation += addend;
16654 relocation -= (rel->r_offset
16655 + input_section->output_offset
16656 + input_section->output_section->vma);
3de43e7b 16657 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
16658 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16659 insn &= ~0x1fffc1;
3de43e7b 16660 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
16661 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16662 r = bfd_reloc_ok;
3de43e7b 16663 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
16664 r = bfd_reloc_overflow;
16665 }
5663e321
AM
16666 break;
16667
16668 default:
16669 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
16670 contents, rel->r_offset,
16671 relocation, addend);
a680de9a 16672 }
5bd4f169 16673
ef60b7ff 16674 if (r != bfd_reloc_ok)
5bd4f169 16675 {
bc30df16 16676 char *more_info = NULL;
b80eed39 16677 const char *reloc_name = howto->name;
bc30df16
AM
16678
16679 if (reloc_dest != DEST_NORMAL)
16680 {
16681 more_info = bfd_malloc (strlen (reloc_name) + 8);
16682 if (more_info != NULL)
16683 {
16684 strcpy (more_info, reloc_name);
16685 strcat (more_info, (reloc_dest == DEST_OPD
16686 ? " (OPD)" : " (stub)"));
16687 reloc_name = more_info;
16688 }
16689 }
16690
cd27b276 16691 if (r == bfd_reloc_overflow)
5bd4f169 16692 {
8131c122
AM
16693 /* On code like "if (foo) foo();" don't report overflow
16694 on a branch to zero when foo is undefined. */
16695 if (!warned
16696 && (reloc_dest == DEST_STUB
16697 || !(h != NULL
16698 && (h->elf.root.type == bfd_link_hash_undefweak
16699 || h->elf.root.type == bfd_link_hash_undefined)
16700 && is_branch_reloc (r_type))))
1a72702b
AM
16701 info->callbacks->reloc_overflow (info, &h->elf.root,
16702 sym_name, reloc_name,
16703 orig_rel.r_addend,
16704 input_bfd, input_section,
16705 rel->r_offset);
ef60b7ff
AM
16706 }
16707 else
16708 {
25f53a85 16709 info->callbacks->einfo
695344c0 16710 /* xgettext:c-format */
c1c8c1ef 16711 (_("%H: %s against `%pT': error %d\n"),
25f53a85 16712 input_bfd, input_section, rel->r_offset,
bc30df16 16713 reloc_name, sym_name, (int) r);
b34976b6 16714 ret = FALSE;
ef60b7ff 16715 }
bc30df16
AM
16716 if (more_info != NULL)
16717 free (more_info);
5bd4f169 16718 }
c316a17c
AM
16719 copy_reloc:
16720 if (wrel != rel)
16721 *wrel = *rel;
16722 }
16723
16724 if (wrel != rel)
16725 {
16726 Elf_Internal_Shdr *rel_hdr;
16727 size_t deleted = rel - wrel;
16728
16729 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16730 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16731 if (rel_hdr->sh_size == 0)
16732 {
16733 /* It is too late to remove an empty reloc section. Leave
16734 one NONE reloc.
16735 ??? What is wrong with an empty section??? */
16736 rel_hdr->sh_size = rel_hdr->sh_entsize;
16737 deleted -= 1;
16738 }
16739 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16740 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16741 input_section->reloc_count -= deleted;
5bd4f169
AM
16742 }
16743
645ea6a9
AM
16744 /* If we're emitting relocations, then shortly after this function
16745 returns, reloc offsets and addends for this section will be
16746 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
16747 file rather than the input. Save a copy of the relocs for
16748 opd_entry_value. */
0e1862bb 16749 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
16750 {
16751 bfd_size_type amt;
16752 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16753 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
16754 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16755 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
16756 if (rel == NULL)
16757 return FALSE;
16758 memcpy (rel, relocs, amt);
16759 }
5bd4f169
AM
16760 return ret;
16761}
16762
754021d0
AM
16763/* Adjust the value of any local symbols in opd sections. */
16764
6e0b88f1 16765static int
754021d0
AM
16766ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16767 const char *name ATTRIBUTE_UNUSED,
16768 Elf_Internal_Sym *elfsym,
16769 asection *input_sec,
16770 struct elf_link_hash_entry *h)
16771{
74f0fb50
AM
16772 struct _opd_sec_data *opd;
16773 long adjust;
754021d0
AM
16774 bfd_vma value;
16775
4025353c 16776 if (h != NULL)
6e0b88f1 16777 return 1;
4025353c 16778
74f0fb50
AM
16779 opd = get_opd_info (input_sec);
16780 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 16781 return 1;
754021d0
AM
16782
16783 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 16784 if (!bfd_link_relocatable (info))
754021d0
AM
16785 value -= input_sec->output_section->vma;
16786
51aecdc5 16787 adjust = opd->adjust[OPD_NDX (value)];
4025353c 16788 if (adjust == -1)
6e0b88f1
AM
16789 return 2;
16790
16791 elfsym->st_value += adjust;
16792 return 1;
754021d0
AM
16793}
16794
5bd4f169
AM
16795/* Finish up dynamic symbol handling. We set the contents of various
16796 dynamic sections here. */
16797
b34976b6 16798static bfd_boolean
4ce794b7
AM
16799ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16800 struct bfd_link_info *info,
16801 struct elf_link_hash_entry *h,
4aef7643 16802 Elf_Internal_Sym *sym)
5bd4f169 16803{
65f38f15 16804 struct ppc_link_hash_table *htab;
8387904d 16805 struct plt_entry *ent;
5bd4f169 16806
65f38f15 16807 htab = ppc_hash_table (info);
4dfe6ac6
NC
16808 if (htab == NULL)
16809 return FALSE;
5bd4f169 16810
49c09209
AM
16811 if (!htab->opd_abi && !h->def_regular)
16812 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16813 if (ent->plt.offset != (bfd_vma) -1)
16814 {
16815 /* Mark the symbol as undefined, rather than as
16816 defined in glink. Leave the value if there were
16817 any relocations where pointer equality matters
16818 (this is a clue for the dynamic linker, to make
16819 function pointer comparisons work between an
16820 application and shared library), otherwise set it
16821 to zero. */
16822 sym->st_shndx = SHN_UNDEF;
16823 if (!h->pointer_equality_needed)
16824 sym->st_value = 0;
16825 else if (!h->ref_regular_nonweak)
16826 {
16827 /* This breaks function pointer comparisons, but
16828 that is better than breaking tests for a NULL
16829 function pointer. */
16830 sym->st_value = 0;
16831 }
16832 break;
16833 }
5bd4f169 16834
1bdd8fac
AM
16835 if (h->needs_copy
16836 && (h->root.type == bfd_link_hash_defined
16837 || h->root.type == bfd_link_hash_defweak)
16838 && (h->root.u.def.section == htab->elf.sdynbss
16839 || h->root.u.def.section == htab->elf.sdynrelro))
5bd4f169 16840 {
65f38f15 16841 /* This symbol needs a copy reloc. Set it up. */
49c09209 16842 Elf_Internal_Rela rela;
5474d94f 16843 asection *srel;
49c09209 16844 bfd_byte *loc;
5bd4f169 16845
1bdd8fac 16846 if (h->dynindx == -1)
65f38f15 16847 abort ();
5bd4f169
AM
16848
16849 rela.r_offset = (h->root.u.def.value
16850 + h->root.u.def.section->output_section->vma
16851 + h->root.u.def.section->output_offset);
16852 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16853 rela.r_addend = 0;
afbf7e8e 16854 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
16855 srel = htab->elf.sreldynrelro;
16856 else
16857 srel = htab->elf.srelbss;
16858 loc = srel->contents;
16859 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 16860 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
16861 }
16862
b34976b6 16863 return TRUE;
5bd4f169
AM
16864}
16865
65f38f15
AM
16866/* Used to decide how to sort relocs in an optimal manner for the
16867 dynamic linker, before writing them out. */
16868
16869static enum elf_reloc_type_class
7e612e98
AM
16870ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16871 const asection *rel_sec,
16872 const Elf_Internal_Rela *rela)
65f38f15 16873{
04c9666a 16874 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
16875 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16876
33e44f2e 16877 if (rel_sec == htab->elf.irelplt)
7e612e98 16878 return reloc_class_ifunc;
a33d1f77 16879
4ce794b7 16880 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 16881 switch (r_type)
65f38f15
AM
16882 {
16883 case R_PPC64_RELATIVE:
16884 return reloc_class_relative;
16885 case R_PPC64_JMP_SLOT:
16886 return reloc_class_plt;
16887 case R_PPC64_COPY:
16888 return reloc_class_copy;
16889 default:
16890 return reloc_class_normal;
16891 }
16892}
16893
5bd4f169
AM
16894/* Finish up the dynamic sections. */
16895
b34976b6 16896static bfd_boolean
4ce794b7
AM
16897ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16898 struct bfd_link_info *info)
5bd4f169 16899{
65f38f15
AM
16900 struct ppc_link_hash_table *htab;
16901 bfd *dynobj;
5bd4f169 16902 asection *sdyn;
5bd4f169 16903
65f38f15 16904 htab = ppc_hash_table (info);
4dfe6ac6
NC
16905 if (htab == NULL)
16906 return FALSE;
16907
65f38f15 16908 dynobj = htab->elf.dynobj;
3d4d4302 16909 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 16910
65f38f15 16911 if (htab->elf.dynamic_sections_created)
5bd4f169 16912 {
5bd4f169
AM
16913 Elf64_External_Dyn *dyncon, *dynconend;
16914
33e44f2e 16915 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 16916 abort ();
5bd4f169
AM
16917
16918 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 16919 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
16920 for (; dyncon < dynconend; dyncon++)
16921 {
16922 Elf_Internal_Dyn dyn;
19397422 16923 asection *s;
5bd4f169
AM
16924
16925 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16926
16927 switch (dyn.d_tag)
16928 {
65f38f15
AM
16929 default:
16930 continue;
5bd4f169 16931
5d1634d7 16932 case DT_PPC64_GLINK:
4ce794b7 16933 s = htab->glink;
6348e046 16934 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
16935 /* We stupidly defined DT_PPC64_GLINK to be the start
16936 of glink rather than the first entry point, which is
16937 what ld.so needs, and now have a bigger stub to
16938 support automatic multiple TOCs. */
9e390558 16939 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
16940 break;
16941
19397422
AM
16942 case DT_PPC64_OPD:
16943 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16944 if (s == NULL)
16945 continue;
16946 dyn.d_un.d_ptr = s->vma;
19397422
AM
16947 break;
16948
e8910a83 16949 case DT_PPC64_OPT:
5663e321
AM
16950 if ((htab->do_multi_toc && htab->multi_toc_needed)
16951 || htab->notoc_plt)
e8910a83 16952 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
16953 if (htab->has_plt_localentry0)
16954 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
16955 break;
16956
19397422
AM
16957 case DT_PPC64_OPDSZ:
16958 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16959 if (s == NULL)
16960 continue;
eea6121a 16961 dyn.d_un.d_val = s->size;
19397422
AM
16962 break;
16963
65f38f15 16964 case DT_PLTGOT:
33e44f2e 16965 s = htab->elf.splt;
6348e046 16966 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
16967 break;
16968
16969 case DT_JMPREL:
33e44f2e 16970 s = htab->elf.srelplt;
6348e046 16971 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 16972 break;
5bd4f169 16973
65f38f15 16974 case DT_PLTRELSZ:
33e44f2e 16975 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 16976 break;
82e66161
AM
16977
16978 case DT_TEXTREL:
16979 if (htab->local_ifunc_resolver)
16980 info->callbacks->einfo
16981 (_("%X%P: text relocations and GNU indirect "
16982 "functions will result in a segfault at runtime\n"));
16983 else if (htab->maybe_local_ifunc_resolver)
16984 info->callbacks->einfo
16985 (_("%P: warning: text relocations and GNU indirect "
16986 "functions may result in a segfault at runtime\n"));
16987 continue;
5bd4f169 16988 }
5bd4f169 16989
65f38f15 16990 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 16991 }
5bd4f169
AM
16992 }
16993
6528b6eb
AM
16994 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16995 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16996 {
16997 /* Fill in the first entry in the global offset table.
16998 We use it to hold the link-time TOCbase. */
16999 bfd_put_64 (output_bfd,
60ee0d4a 17000 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 17001 htab->elf.sgot->contents);
5d1634d7
AM
17002
17003 /* Set .got entry size. */
2cdcc330
AM
17004 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
17005 = 8;
5d1634d7
AM
17006 }
17007
6528b6eb
AM
17008 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
17009 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
17010 {
17011 /* Set .plt entry size. */
33e44f2e 17012 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 17013 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
17014 }
17015
84f5d08e
AM
17016 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
17017 brlt ourselves if emitrelocations. */
17018 if (htab->brlt != NULL
17019 && htab->brlt->reloc_count != 0
17020 && !_bfd_elf_link_output_relocs (output_bfd,
17021 htab->brlt,
d4730f92 17022 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
17023 elf_section_data (htab->brlt)->relocs,
17024 NULL))
17025 return FALSE;
17026
176a0d42
AM
17027 if (htab->glink != NULL
17028 && htab->glink->reloc_count != 0
17029 && !_bfd_elf_link_output_relocs (output_bfd,
17030 htab->glink,
d4730f92 17031 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
17032 elf_section_data (htab->glink)->relocs,
17033 NULL))
17034 return FALSE;
17035
da44f4e5 17036
df136d64
AM
17037 if (htab->glink_eh_frame != NULL
17038 && htab->glink_eh_frame->size != 0
17039 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
17040 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
17041 htab->glink_eh_frame,
17042 htab->glink_eh_frame->contents))
17043 return FALSE;
58d180e8 17044
e717da7e 17045 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
17046 since we didn't add them to DYNOBJ. We know dynobj is the first
17047 bfd. */
c72f2fb2 17048 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
17049 {
17050 asection *s;
7b53ace3 17051
0c8d6e5c 17052 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
17053 continue;
17054
e717da7e
AM
17055 s = ppc64_elf_tdata (dynobj)->got;
17056 if (s != NULL
eea6121a 17057 && s->size != 0
e717da7e
AM
17058 && s->output_section != bfd_abs_section_ptr
17059 && !bfd_set_section_contents (output_bfd, s->output_section,
17060 s->contents, s->output_offset,
eea6121a 17061 s->size))
e717da7e
AM
17062 return FALSE;
17063 s = ppc64_elf_tdata (dynobj)->relgot;
17064 if (s != NULL
eea6121a 17065 && s->size != 0
e717da7e
AM
17066 && s->output_section != bfd_abs_section_ptr
17067 && !bfd_set_section_contents (output_bfd, s->output_section,
17068 s->contents, s->output_offset,
eea6121a 17069 s->size))
e717da7e
AM
17070 return FALSE;
17071 }
f6c52c13 17072
b34976b6 17073 return TRUE;
5bd4f169
AM
17074}
17075
5bd4f169 17076#include "elf64-target.h"
7b8e7dad
AM
17077
17078/* FreeBSD support */
17079
17080#undef TARGET_LITTLE_SYM
17081#undef TARGET_LITTLE_NAME
17082
17083#undef TARGET_BIG_SYM
6d00b590 17084#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
17085#undef TARGET_BIG_NAME
17086#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
17087
17088#undef ELF_OSABI
17089#define ELF_OSABI ELFOSABI_FREEBSD
17090
17091#undef elf64_bed
17092#define elf64_bed elf64_powerpc_fbsd_bed
17093
17094#include "elf64-target.h"
This page took 3.167661 seconds and 4 git commands to generate.